source: trunk/server/source3/libsmb/libsmb_path.c@ 745

Last change on this file since 745 was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 11.2 KB
Line 
1/*
2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003-2008
9 Copyright (C) Jeremy Allison 2007, 2008
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "includes.h"
26#include "libsmbclient.h"
27#include "libsmb_internal.h"
28
29
30/* Used by urldecode_talloc() */
31static int
32hex2int( unsigned int _char )
33{
34 if ( _char >= 'A' && _char <='F')
35 return _char - 'A' + 10;
36 if ( _char >= 'a' && _char <='f')
37 return _char - 'a' + 10;
38 if ( _char >= '0' && _char <='9')
39 return _char - '0';
40 return -1;
41}
42
43/*
44 * smbc_urldecode()
45 * and urldecode_talloc() (internal fn.)
46 *
47 * Convert strings of %xx to their single character equivalent. Each 'x' must
48 * be a valid hexadecimal digit, or that % sequence is left undecoded.
49 *
50 * dest may, but need not be, the same pointer as src.
51 *
52 * Returns the number of % sequences which could not be converted due to lack
53 * of two following hexadecimal digits.
54 */
55static int
56urldecode_talloc(TALLOC_CTX *ctx, char **pp_dest, const char *src)
57{
58 int old_length = strlen(src);
59 int i = 0;
60 int err_count = 0;
61 size_t newlen = 1;
62 char *p, *dest;
63
64 if (old_length == 0) {
65 return 0;
66 }
67
68 *pp_dest = NULL;
69 for (i = 0; i < old_length; ) {
70 unsigned char character = src[i++];
71
72 if (character == '%') {
73 int a = i+1 < old_length ? hex2int(src[i]) : -1;
74 int b = i+1 < old_length ? hex2int(src[i+1]) : -1;
75
76 /* Replace valid sequence */
77 if (a != -1 && b != -1) {
78 /* Replace valid %xx sequence with %dd */
79 character = (a * 16) + b;
80 if (character == '\0') {
81 break; /* Stop at %00 */
82 }
83 i += 2;
84 } else {
85 err_count++;
86 }
87 }
88 newlen++;
89 }
90
91 dest = TALLOC_ARRAY(ctx, char, newlen);
92 if (!dest) {
93 return err_count;
94 }
95
96 err_count = 0;
97 for (p = dest, i = 0; i < old_length; ) {