]> git.sur5r.net Git - openldap/blob - libraries/libldif/line64.c
Some gcc -W cleanup
[openldap] / libraries / libldif / line64.c
1 /* line64.c - routines for dealing with the slapd line format */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6 #include <ctype.h>
7
8 #include <ac/string.h>
9 #include <ac/socket.h>
10 #include <ac/time.h>
11
12 #include "ldap_log.h"
13 #include "ldif.h"
14
15
16 #define RIGHT2                  0x03
17 #define RIGHT4                  0x0f
18 #define CONTINUED_LINE_MARKER   '\001'
19
20 static char nib2b64[0x40f] =
21         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
22
23 static unsigned char b642nib[0x80] = {
24         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
25         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
26         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
27         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
28         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
29         0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
30         0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
31         0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
32         0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
33         0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
34         0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
35         0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
36         0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
37         0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
38         0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
39         0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
40 };
41
42 /*
43  * str_parse_line - takes a line of the form "type:[:] value" and splits it
44  * into components "type" and "value".  if a double colon separates type from
45  * value, then value is encoded in base 64, and parse_line un-decodes it
46  * (in place) before returning.
47  */
48
49 int
50 str_parse_line(
51     char        *line,
52     char        **type,
53     char        **value,
54     int         *vlen
55 )
56 {
57         char    *p, *s, *d, *byte, *stop;
58         char    nib;
59         int     i, b64;
60
61         /* skip any leading space */
62         while ( isspace( *line ) ) {
63                 line++;
64         }
65         *type = line;
66
67         for ( s = line; *s && *s != ':'; s++ )
68                 ;       /* NULL */
69         if ( *s == '\0' ) {
70                 Debug( LDAP_DEBUG_PARSE, "parse_line missing ':'\n", 0, 0, 0 );
71                 return( -1 );
72         }
73
74         /* trim any space between type and : */
75         for ( p = s - 1; p > line && isspace( *p ); p-- ) {
76                 *p = '\0';
77         }
78         *s++ = '\0';
79
80         /* check for double : - indicates base 64 encoded value */
81         if ( *s == ':' ) {
82                 s++;
83                 b64 = 1;
84
85         /* single : - normally encoded value */
86         } else {
87                 b64 = 0;
88         }
89
90         /* skip space between : and value */
91         while ( isspace( *s ) ) {
92                 s++;
93         }
94
95         /* if no value is present, error out */
96         if ( *s == '\0' ) {
97                 Debug( LDAP_DEBUG_PARSE, "parse_line missing value\n", 0,0,0 );
98                 return( -1 );
99         }
100
101         /* check for continued line markers that should be deleted */
102         for ( p = s, d = s; *p; p++ ) {
103                 if ( *p != CONTINUED_LINE_MARKER )
104                         *d++ = *p;
105         }
106         *d = '\0';
107
108         *value = s;
109         if ( b64 ) {
110                 stop = strchr( s, '\0' );
111                 byte = s;
112                 for ( p = s, *vlen = 0; p < stop; p += 4, *vlen += 3 ) {
113                         for ( i = 0; i < 3; i++ ) {
114                                 if ( p[i] != '=' && (p[i] & 0x80 ||
115                                     b642nib[ p[i] & 0x7f ] > 0x3f) ) {
116                                         Debug( LDAP_DEBUG_ANY,
117                                     "invalid base 64 encoding char (%c) 0x%x\n",
118                                             p[i], p[i], 0 );
119                                         return( -1 );
120                                 }
121                         }
122
123                         /* first digit */
124                         nib = b642nib[ p[0] & 0x7f ];
125                         byte[0] = nib << 2;
126                         /* second digit */
127                         nib = b642nib[ p[1] & 0x7f ];
128                         byte[0] |= nib >> 4;
129                         byte[1] = (nib & RIGHT4) << 4;
130                         /* third digit */
131                         if ( p[2] == '=' ) {
132                                 *vlen += 1;
133                                 break;
134                         }
135                         nib = b642nib[ p[2] & 0x7f ];
136                         byte[1] |= nib >> 2;
137                         byte[2] = (nib & RIGHT2) << 6;
138                         /* fourth digit */
139                         if ( p[3] == '=' ) {
140                                 *vlen += 2;
141                                 break;
142                         }
143                         nib = b642nib[ p[3] & 0x7f ];
144                         byte[2] |= nib;
145
146                         byte += 3;
147                 }
148                 s[ *vlen ] = '\0';
149         } else {
150                 *vlen = (int) (d - s);
151         }
152
153         return( 0 );
154 }
155
156 /*
157  * str_getline - return the next "line" (minus newline) of input from a
158  * string buffer of lines separated by newlines, terminated by \n\n
159  * or \0.  this routine handles continued lines, bundling them into
160  * a single big line before returning.  if a line begins with a white
161  * space character, it is a continuation of the previous line. the white
162  * space character (nb: only one char), and preceeding newline are changed
163  * into CONTINUED_LINE_MARKER chars, to be deleted later by the
164  * str_parse_line() routine above.
165  *
166  * it takes a pointer to a pointer to the buffer on the first call,
167  * which it updates and must be supplied on subsequent calls.
168  */
169
170 char *
171 str_getline( char **next )
172 {
173         char    *l;
174         char    c;
175
176         if ( *next == NULL || **next == '\n' || **next == '\0' ) {
177                 return( NULL );
178         }
179
180         l = *next;
181         while ( (*next = strchr( *next, '\n' )) != NULL ) {
182                 c = *(*next + 1);
183                 if ( isspace( c ) && c != '\n' ) {
184                         **next = CONTINUED_LINE_MARKER;
185                         *(*next+1) = CONTINUED_LINE_MARKER;
186                 } else {
187                         *(*next)++ = '\0';
188                         break;
189                 }
190                 (*next)++;
191         }
192
193         return( l );
194 }
195
196 void
197 put_type_and_value( char **out, char *t, char *val, int vlen )
198 {
199         unsigned char   *byte, *p, *stop;
200         unsigned char   buf[3];
201         unsigned long   bits;
202         char            *save;
203         int             i, b64, pad, len, savelen;
204         len = 0;
205
206         /* put the type + ": " */
207         for ( p = (unsigned char *) t; *p; p++, len++ ) {
208                 *(*out)++ = *p;
209         }
210         *(*out)++ = ':';
211         len++;
212         save = *out;
213         savelen = len;
214         *(*out)++ = ' ';
215         b64 = 0;
216
217         stop = (unsigned char *) (val + vlen);
218         if ( isascii( val[0] ) && (isspace( val[0] ) || val[0] == ':') ) {
219                 b64 = 1;
220         } else {
221                 for ( byte = (unsigned char *) val; byte < stop;
222                     byte++, len++ ) {
223                         if ( !isascii( *byte ) || !isprint( *byte ) ) {
224                                 b64 = 1;
225                                 break;
226                         }
227                         if ( len > LINE_WIDTH ) {
228                                 *(*out)++ = '\n';
229                                 *(*out)++ = ' ';
230                                 len = 1;
231                         }
232                         *(*out)++ = *byte;
233                 }
234         }
235         if ( b64 ) {
236                 *out = save;
237                 *(*out)++ = ':';
238                 *(*out)++ = ' ';
239                 len = savelen + 2;
240                 /* convert to base 64 (3 bytes => 4 base 64 digits) */
241                 for ( byte = (unsigned char *) val; byte < stop - 2;
242                     byte += 3 ) {
243                         bits = (byte[0] & 0xff) << 16;
244                         bits |= (byte[1] & 0xff) << 8;
245                         bits |= (byte[2] & 0xff);
246
247                         for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
248                                 if ( len > LINE_WIDTH ) {
249                                         *(*out)++ = '\n';
250                                         *(*out)++ = ' ';
251                                         len = 1;
252                                 }
253
254                                 /* get b64 digit from high order 6 bits */
255                                 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
256                         }
257                 }
258
259                 /* add padding if necessary */
260                 if ( byte < stop ) {
261                         for ( i = 0; byte + i < stop; i++ ) {
262                                 buf[i] = byte[i];
263                         }
264                         for ( pad = 0; i < 3; i++, pad++ ) {
265                                 buf[i] = '\0';
266                         }
267                         byte = buf;
268                         bits = (byte[0] & 0xff) << 16;
269                         bits |= (byte[1] & 0xff) << 8;
270                         bits |= (byte[2] & 0xff);
271
272                         for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
273                                 if ( len > LINE_WIDTH ) {
274                                         *(*out)++ = '\n';
275                                         *(*out)++ = ' ';
276                                         len = 1;
277                                 }
278
279                                 if( i + pad < 4 ) {
280                                         /* get b64 digit from low order 6 bits */
281                                         *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
282                                 } else {
283                                         *(*out)++ = '=';
284                                 }
285                         }
286                 }
287         }
288         *(*out)++ = '\n';
289 }
290
291
292 char *
293 ldif_type_and_value( char *type, char *val, int vlen )
294 /*
295  * return malloc'd, zero-terminated LDIF line
296  */
297 {
298     char        *buf, *p;
299     int         tlen;
300
301     tlen = strlen( type );
302     if (( buf = (char *)malloc( LDIF_SIZE_NEEDED( tlen, vlen ) + 1 )) !=
303             NULL ) {
304     }
305
306     p = buf;
307     put_type_and_value( &p, type, val, vlen );
308     *p = '\0';
309
310     return( buf );
311 }