1 /*
2  * Copyright (c) 2000
3  *      Traakan, Inc., Los Altos, CA
4  *      All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Project:  NDMJOB
31  * Ident:    $Id: $
32  *
33  * Description:
34  *      Convert strings to/from a canonical strings (CSTR).
35  *
36  *      The main reason for this is to eliminate spaces
37  *      in strings thus making multiple strings easily
38  *      delimited by white space.
39  *
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.
44  *
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.
48  */
49 
50 
51 #include "ndmlib.h"
52 
53 
54 static char ndmcstr_to_hex[] = "0123456789ABCDEF";
55 
56 extern int ndmcstr_from_hex(int c);
57 
ndmcstr_from_str(char * src,char * dst,unsigned dst_max)58 int ndmcstr_from_str(char* src, char* dst, unsigned dst_max)
59 {
60   unsigned char* p = (unsigned char*)src;
61   unsigned char* q = (unsigned char*)dst;
62   unsigned char* q_end = q + dst_max - 1;
63   int c;
64 
65   while ((c = *p++) != 0) {
66     if (c <= ' ' || c > 0x7E || c == NDMCSTR_WARN) {
67       if (q + 3 > q_end) return -1;
68       *q++ = NDMCSTR_WARN;
69       *q++ = ndmcstr_to_hex[(c >> 4) & 0xF];
70       *q++ = ndmcstr_to_hex[c & 0xF];
71     } else {
72       if (q + 1 > q_end) return -1;
73       *q++ = c;
74     }
75   }
76   *q = 0;
77 
78   return q - (unsigned char*)dst;
79 }
80 
ndmcstr_to_str(char * src,char * dst,unsigned dst_max)81 int ndmcstr_to_str(char* src, char* dst, unsigned dst_max)
82 {
83   unsigned char* p = (unsigned char*)src;
84   unsigned char* q = (unsigned char*)dst;
85   unsigned char* q_end = q + dst_max - 1;
86   int c, c1, c2;
87 
88   while ((c = *p++) != 0) {
89     if (q + 1 > q_end) return -1;
90     if (c != NDMCSTR_WARN) {
91       *q++ = c;
92       continue;
93     }
94     c1 = ndmcstr_from_hex(p[0]);
95     c2 = ndmcstr_from_hex(p[1]);
96 
97     if (c1 < 0 || c2 < 0) {
98       /* busted conversion */
99       return -2;
100     }
101 
102     c = (c1 << 4) + c2;
103     *q++ = c;
104     p += 2;
105   }
106   *q = 0;
107 
108   return q - (unsigned char*)dst;
109 }
110 
ndmcstr_from_hex(int c)111 int ndmcstr_from_hex(int c)
112 {
113   if ('0' <= c && c <= '9') return c - '0';
114   if ('a' <= c && c <= 'f') return (c - 'a') + 10;
115   if ('A' <= c && c <= 'F') return (c - 'A') + 10;
116   return -1;
117 }
118