Imported Upstream version 3.1.0
[debian/amanda] / ndmp-src / ndml_cstr.c
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
58 int
59 ndmcstr_from_str (char *src, char *dst, unsigned dst_max)
60 {
61         unsigned char *         p = (unsigned char *)src;
62         unsigned char *         q = (unsigned char *)dst;
63         unsigned char *         q_end = q + dst_max - 1;
64         int                     c;
65
66         while ((c = *p++) != 0) {
67                 if (c <= ' ' || c > 0x7E || c == NDMCSTR_WARN) {
68                         if (q+3 > q_end)
69                                 return -1;
70                         *q++ = NDMCSTR_WARN;
71                         *q++ = ndmcstr_to_hex[(c>>4)&0xF];
72                         *q++ = ndmcstr_to_hex[c&0xF];
73                 } else {
74                         if (q+1 > q_end)
75                                 return -1;
76                         *q++ = c;
77                 }
78         }
79         *q = 0;
80
81         return q - (unsigned char *)dst;
82 }
83
84 int
85 ndmcstr_to_str (char *src, char *dst, unsigned dst_max)
86 {
87         unsigned char *         p = (unsigned char *)src;
88         unsigned char *         q = (unsigned char *)dst;
89         unsigned char *         q_end = q + dst_max - 1;
90         int                     c, c1, c2;
91
92         while ((c = *p++) != 0) {
93                 if (q+1 > q_end)
94                         return -1;
95                 if (c != NDMCSTR_WARN) {
96                         *q++ = c;
97                         continue;
98                 }
99                 c1 = ndmcstr_from_hex (p[0]);
100                 c2 = ndmcstr_from_hex (p[1]);
101
102                 if (c1 < 0 || c2 < 0) {
103                         /* busted conversion */
104                         return -2;
105                 }
106
107                 c = (c1<<4) + c2;
108                 *q++ = c;
109                 p += 2;
110         }
111         *q = 0;
112
113         return q - (unsigned char *)dst;
114 }
115
116 int
117 ndmcstr_from_hex (int c)
118 {
119         if ('0' <= c && c <= '9')
120                 return c - '0';
121         if ('a' <= c && c <= 'f')
122                 return (c - 'a') + 10;
123         if ('A' <= c && c <= 'F')
124                 return (c - 'A') + 10;
125         return -1;
126 }