3 * Traakan, Inc., Los Altos, CA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Convert strings to/from a canonical strings (CSTR).
36 * The main reason for this is to eliminate spaces
37 * in strings thus making multiple strings easily
38 * delimited by white space.
40 * Canonical strings use the HTTP convention of
41 * percent sign followed by two hex digits (%xx).
42 * Characters outside the printable ASCII range,
43 * space, and percent sign are so converted.
45 * Both interfaces return the length of the resulting
46 * string, -1 if there is an overflow, or -2
47 * there is a conversion error.
54 static char ndmcstr_to_hex[] = "0123456789ABCDEF";
56 extern int ndmcstr_from_hex (int c);
59 ndmcstr_from_str (char *src, char *dst, unsigned dst_max)
61 unsigned char * p = (unsigned char *)src;
62 unsigned char * q = (unsigned char *)dst;
63 unsigned char * q_end = q + dst_max - 1;
66 while ((c = *p++) != 0) {
67 if (c <= ' ' || c > 0x7E || c == NDMCSTR_WARN) {
71 *q++ = ndmcstr_to_hex[(c>>4)&0xF];
72 *q++ = ndmcstr_to_hex[c&0xF];
81 return q - (unsigned char *)dst;
85 ndmcstr_to_str (char *src, char *dst, unsigned dst_max)
87 unsigned char * p = (unsigned char *)src;
88 unsigned char * q = (unsigned char *)dst;
89 unsigned char * q_end = q + dst_max - 1;
92 while ((c = *p++) != 0) {
95 if (c != NDMCSTR_WARN) {
99 c1 = ndmcstr_from_hex (p[0]);
100 c2 = ndmcstr_from_hex (p[1]);
102 if (c1 < 0 || c2 < 0) {
103 /* busted conversion */
113 return q - (unsigned char *)dst;
117 ndmcstr_from_hex (int c)
119 if ('0' <= c && c <= '9')
121 if ('a' <= c && c <= 'f')
122 return (c - 'a') + 10;
123 if ('A' <= c && c <= 'F')
124 return (c - 'A') + 10;