]> git.sur5r.net Git - openldap/blob - libraries/libldif/line64.c
Add copyright notices
[openldap] / libraries / libldif / line64.c
1 /* line64.c - routines for dealing with the slapd line format */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12 #include <ac/ctype.h>
13
14 #include <ac/string.h>
15 #include <ac/socket.h>
16 #include <ac/time.h>
17
18 int ldif_debug = 0;
19
20 #include "ldap_log.h"
21 #include "lber_pvt.h"
22 #include "ldif.h"
23
24 #define RIGHT2                  0x03
25 #define RIGHT4                  0x0f
26 #define CONTINUED_LINE_MARKER   '\001'
27
28 static const char nib2b64[0x40] =
29         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
30
31 static const unsigned char b642nib[0x80] = {
32         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
33         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
34         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
35         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
36         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
37         0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
38         0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
39         0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
40         0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
41         0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
42         0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
43         0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
44         0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
45         0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
46         0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
47         0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
48 };
49
50 /*
51  * ldif_parse_line - takes a line of the form "type:[:] value" and splits it
52  * into components "type" and "value".  if a double colon separates type from
53  * value, then value is encoded in base 64, and parse_line un-decodes it
54  * (in place) before returning.
55  */
56
57 int
58 ldif_parse_line(
59     LDAP_CONST char     *line,
60     char        **typep,
61     char        **valuep,
62     ber_len_t *vlenp
63 )
64 {
65         char    *s, *p, *d; 
66         char    nib;
67         int     b64, url;
68         char    *freeme, *type, *value;
69         ber_len_t vlen;
70
71         *typep = NULL;
72         *valuep = NULL;
73         *vlenp = 0;
74
75         /* skip any leading space */
76         while ( isspace( (unsigned char) *line ) ) {
77                 line++;
78         }
79
80         freeme = ber_strdup( line );
81
82         if( freeme == NULL ) {
83                 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
84                         "ldif_parse_line: line malloc failed\n");
85                 return( -1 );
86         }
87
88         type = freeme;
89
90         s = strchr( type, ':' );
91
92         if ( s == NULL ) {
93                 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
94                         "ldif_parse_line: missing ':' after %s\n",
95                         type );
96                 ber_memfree( freeme );
97                 return( -1 );
98         }
99
100         /* trim any space between type and : */
101         for ( p = &s[-1]; p > type && isspace( * (unsigned char *) p ); p-- ) {
102                 *p = '\0';
103         }
104         *s++ = '\0';
105
106         if ( *s == '\0' ) {
107                 /* no value */
108                 value = NULL;
109                 vlen = 0;
110                 goto done;
111         }
112                 
113         url = 0;
114         b64 = 0;
115
116         if ( *s == '<' ) {
117                 s++;
118                 url = 1;
119         } else if ( *s == ':' ) {
120                 /* base 64 encoded value */
121                 s++;
122                 b64 = 1;
123         }
124
125         /* skip space between : and value */
126         while ( isspace( (unsigned char) *s ) ) {
127                 s++;
128         }
129
130         /* if no value is present, error out */
131         if ( *s == '\0' ) {
132                 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
133                         "ldif_parse_line: %s missing %svalue\n", type,
134                                 url ? "URL " : b64 ? "base64 " : "" );
135                 value = NULL;
136                 vlen = 0;
137                 goto done;
138         }
139
140         /* check for continued line markers that should be deleted */
141         for ( p = s, d = s; *p; p++ ) {
142                 if ( *p != CONTINUED_LINE_MARKER )
143                         *d++ = *p;
144         }
145         *d = '\0';
146
147         if ( b64 ) {
148                 char *byte = s;
149
150                 value = s;
151
152                 for ( p = s, vlen = 0; p < d; p += 4, vlen += 3 ) {
153                         int i;
154                         for ( i = 0; i < 4; i++ ) {
155                                 if ( p[i] != '=' && (p[i] & 0x80 ||
156                                     b642nib[ p[i] & 0x7f ] > 0x3f) ) {
157                                         ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
158                                                 "ldif_parse_line: %s: invalid base64 encoding"
159                                                 " char (%c) 0x%x\n",
160                                             type, p[i], p[i] );
161                                         ber_memfree( freeme );
162                                         return( -1 );
163                                 }
164                         }
165
166                         /* first digit */
167                         nib = b642nib[ p[0] & 0x7f ];
168                         byte[0] = nib << 2;
169                         /* second digit */
170                         nib = b642nib[ p[1] & 0x7f ];
171                         byte[0] |= nib >> 4;
172                         byte[1] = (nib & RIGHT4) << 4;
173                         /* third digit */
174                         if ( p[2] == '=' ) {
175                                 vlen += 1;
176                                 break;
177                         }
178                         nib = b642nib[ p[2] & 0x7f ];
179                         byte[1] |= nib >> 2;
180                         byte[2] = (nib & RIGHT2) << 6;
181                         /* fourth digit */
182                         if ( p[3] == '=' ) {
183                                 vlen += 2;
184                                 break;
185                         }
186                         nib = b642nib[ p[3] & 0x7f ];
187                         byte[2] |= nib;
188
189                         byte += 3;
190                 }
191                 s[ vlen ] = '\0';
192
193         } else if ( url ) {
194                 if( ldif_fetch_url( s, &value, &vlen ) ) {
195                         ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
196                                 "ldif_parse_line: %s: URL \"%s\" fetch failed\n",
197                                 type, s );
198                         ber_memfree( freeme );
199                         return( -1 );
200                 }
201
202         } else {
203                 value = s;
204                 vlen = (int) (d - s);
205         }
206
207 done:
208         type = ber_strdup( type );
209
210         if( type == NULL ) {
211                 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
212                         "ldif_parse_line: type malloc failed\n");
213                 ber_memfree( freeme );
214                 return( -1 );
215         }
216
217         if( !url && value != NULL ) {
218                 value = ber_strdup( value );
219                 if( value == NULL ) {
220                         ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
221                                 "ldif_parse_line: value malloc failed\n");
222                         ber_memfree( type );
223                         ber_memfree( freeme );
224                         return( -1 );
225                 }
226         }
227
228         ber_memfree( freeme );
229
230         *typep = type;
231         *valuep = value;
232         *vlenp = vlen;
233
234         return( 0 );
235 }
236
237 /*
238  * ldif_getline - return the next "line" (minus newline) of input from a
239  * string buffer of lines separated by newlines, terminated by \n\n
240  * or \0.  this routine handles continued lines, bundling them into
241  * a single big line before returning.  if a line begins with a white
242  * space character, it is a continuation of the previous line. the white
243  * space character (nb: only one char), and preceeding newline are changed
244  * into CONTINUED_LINE_MARKER chars, to be deleted later by the
245  * ldif_parse_line() routine above.
246  *
247  * ldif_getline will skip over any line which starts '#'.
248  *
249  * ldif_getline takes a pointer to a pointer to the buffer on the first call,
250  * which it updates and must be supplied on subsequent calls.
251  */
252
253 char *
254 ldif_getline( char **next )
255 {
256         char *line;
257
258         do {
259                 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
260                         return( NULL );
261                 }
262
263                 line = *next;
264
265                 while ( (*next = strchr( *next, '\n' )) != NULL ) {
266                         unsigned char c = *(*next + 1);
267
268                         if ( !isspace( c ) || c == '\n' ) {
269                                 *(*next)++ = '\0';
270                                 break;
271                         }
272
273                         **next = CONTINUED_LINE_MARKER;
274                         *(*next+1) = CONTINUED_LINE_MARKER;
275                         (*next)++;
276                 }
277         } while( *line == '#' );
278
279         return( line );
280 }
281
282 /* compatibility with U-Mich off by one bug */
283 #define LDIF_KLUDGE 1
284
285 void
286 ldif_sput(
287         char **out,
288         int type,
289         LDAP_CONST char *name,
290         LDAP_CONST char *val,
291         ber_len_t vlen )
292 {
293         unsigned char   *byte, *stop;
294         unsigned char   buf[3];
295         unsigned long   bits;
296         char            *save;
297         int             pad;
298
299         ber_len_t savelen;
300         ber_len_t len=0;
301         ber_len_t i;
302
303         /* prefix */
304         switch( type ) {
305         case LDIF_PUT_COMMENT:
306                 if( name != NULL ) break;
307
308                 *(*out)++ = '#';
309                 len++;
310
311                 if( vlen ) {
312                         *(*out)++ = ' ';
313                         len++;
314                         break;
315                 }
316
317                 /* no value, fall-thru */
318
319         case LDIF_PUT_SEP:
320                 *(*out)++ = '\n';
321                 return;
322         }
323
324         /* name (attribute type) */
325         if( name != NULL ) {
326                 /* put the name + ":" */
327                 for ( i=0 ; name[i]; i++ ) {
328                         *(*out)++ = name[i];
329                         len++;
330                 }
331
332                 if( type != LDIF_PUT_COMMENT ) {
333                         *(*out)++ = ':';
334                         len++;
335                 }
336
337 #ifdef LDAP_DEBUG
338         } else {
339                 assert( type == LDIF_PUT_COMMENT );
340 #endif
341         }
342
343         switch( type ) {
344         case LDIF_PUT_NOVALUE:
345                 *(*out)++ = '\n';
346                 return;
347
348         case LDIF_PUT_URL: /* url value */
349                 *(*out)++ = '<';
350                 len++;
351                 break;
352
353         case LDIF_PUT_B64: /* base64 value */
354                 *(*out)++ = ':';
355                 len++;
356                 break;
357         }
358
359         switch( type ) {
360         case LDIF_PUT_TEXT:
361         case LDIF_PUT_URL:
362         case LDIF_PUT_B64:
363                 *(*out)++ = ' ';
364                 len++;
365                 /* fall-thru */
366
367         case LDIF_PUT_COMMENT:
368                 /* pre-encoded names */
369                 for ( i=0; i < vlen; i++ ) {
370                         if ( len > LDIF_LINE_WIDTH ) {
371                                 *(*out)++ = '\n';
372                                 *(*out)++ = ' ';
373                                 len = 1;
374                         }
375
376                         *(*out)++ = val[i];
377                         len++;
378                 }
379                 *(*out)++ = '\n';
380                 return;
381         }
382
383         save = *out;
384         savelen = len;
385
386         *(*out)++ = ' ';
387         len++;
388
389         if( vlen == 0 ) {
390                 *(*out)++ = '\n';
391                 return;
392         }
393
394         stop = (unsigned char *) (val + vlen);
395
396         if ( type == LDIF_PUT_VALUE &&
397                 isgraph( val[0] ) && val[0] != ':' && val[0] != '<' &&
398                 isgraph( val[vlen-1] ) )
399         {
400                 int b64 = 0;
401
402                 for ( byte = (unsigned char *) val; byte < stop;
403                     byte++, len++ )
404                 {
405                         if ( !isascii( *byte ) || !isprint( *byte ) ) {
406                                 b64 = 1;
407                                 break;
408                         }
409                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
410                                 *(*out)++ = '\n';
411                                 *(*out)++ = ' ';
412                                 len = 1;
413                         }
414                         *(*out)++ = *byte;
415                 }
416
417                 if( !b64 ) {
418                         *(*out)++ = '\n';
419                         return;
420                 }
421         }
422
423         *out = save;
424         *(*out)++ = ':';
425         *(*out)++ = ' ';
426         len = savelen + 2;
427
428         /* convert to base 64 (3 bytes => 4 base 64 digits) */
429         for ( byte = (unsigned char *) val;
430                 byte < stop - 2;
431             byte += 3 )
432         {
433                 bits = (byte[0] & 0xff) << 16;
434                 bits |= (byte[1] & 0xff) << 8;
435                 bits |= (byte[2] & 0xff);
436
437                 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
438                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
439                                 *(*out)++ = '\n';
440                                 *(*out)++ = ' ';
441                                 len = 1;
442                         }
443
444                         /* get b64 digit from high order 6 bits */
445                         *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
446                 }
447         }
448
449         /* add padding if necessary */
450         if ( byte < stop ) {
451                 for ( i = 0; byte + i < stop; i++ ) {
452                         buf[i] = byte[i];
453                 }
454                 for ( pad = 0; i < 3; i++, pad++ ) {
455                         buf[i] = '\0';
456                 }
457                 byte = buf;
458                 bits = (byte[0] & 0xff) << 16;
459                 bits |= (byte[1] & 0xff) << 8;
460                 bits |= (byte[2] & 0xff);
461
462                 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
463                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
464                                 *(*out)++ = '\n';
465                                 *(*out)++ = ' ';
466                                 len = 1;
467                         }
468
469                         if( i + pad < 4 ) {
470                                 /* get b64 digit from low order 6 bits */
471                                 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
472                         } else {
473                                 *(*out)++ = '=';
474                         }
475                 }
476         }
477         *(*out)++ = '\n';
478 }
479
480
481 /*
482  * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
483  */
484 char *
485 ldif_put(
486         int type,
487         LDAP_CONST char *name,
488         LDAP_CONST char *val,
489         ber_len_t vlen )
490 {
491     char        *buf, *p;
492     ber_len_t nlen;
493
494     nlen = ( name != NULL ) ? strlen( name ) : 0;
495
496         buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
497
498     if ( buf == NULL ) {
499                 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
500                         "ldif_type_and_value: malloc failed!" );
501                 return NULL;
502     }
503
504     p = buf;
505     ldif_sput( &p, type, name, val, vlen );
506     *p = '\0';
507
508     return( buf );
509 }
510
511 int ldif_is_not_printable(
512         LDAP_CONST char *val,
513         ber_len_t vlen )
514 {
515         if( vlen == 0 || val == NULL  ) {
516                 return -1;
517         }
518
519         if( isgraph( val[0] ) && val[0] != ':' && val[0] != '<' &&
520                 isgraph( val[vlen-1] ) )
521         {
522                 ber_len_t i;
523
524                 for ( i = 0; val[i]; i++ ) {
525                         if ( !isascii( val[i] ) || !isprint( val[i] ) ) {
526                                 return 1;
527                         }
528                 }
529
530                 return 0;
531         }
532
533         return 1;
534 }