]> git.sur5r.net Git - openldap/blobdiff - libraries/libldif/line64.c
ldap-int.h
[openldap] / libraries / libldif / line64.c
index 429e1d6ab18f6cf3027b45d20a89b7c071390283..527bd4f9d9c46960794302a481f2d4dbd103dc0b 100644 (file)
@@ -3,24 +3,28 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <ctype.h>
+
+#include <ac/stdlib.h>
+#include <ac/ctype.h>
 
 #include <ac/string.h>
 #include <ac/socket.h>
 #include <ac/time.h>
 
-#include "lber.h"
-#include "ldap.h"
+int ldif_debug = 0;
+
+#include "ldap_log.h"
+#include "lber_pvt.h"
 #include "ldif.h"
 
 #define RIGHT2                 0x03
 #define RIGHT4                 0x0f
 #define CONTINUED_LINE_MARKER  '\001'
 
-static char nib2b64[0x40f] =
+static const char nib2b64[0x40f] =
         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
-static unsigned char b642nib[0x80] = {
+static const unsigned char b642nib[0x80] = {
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
@@ -40,15 +44,15 @@ static unsigned char b642nib[0x80] = {
 };
 
 /*
- * str_parse_line - takes a line of the form "type:[:] value" and splits it
+ * ldif_parse_line - takes a line of the form "type:[:] value" and splits it
  * into components "type" and "value".  if a double colon separates type from
  * value, then value is encoded in base 64, and parse_line un-decodes it
  * (in place) before returning.
  */
 
 int
-str_parse_line(
-    char       *line,
+ldif_parse_line(
+    LDAP_CONST char    *line,
     char       **type,
     char       **value,
     int                *vlen
@@ -59,7 +63,7 @@ str_parse_line(
        int     i, b64;
 
        /* skip any leading space */
-       while ( isspace( *line ) ) {
+       while ( isspace( (unsigned char) *line ) ) {
                line++;
        }
        *type = line;
@@ -67,12 +71,13 @@ str_parse_line(
        for ( s = line; *s && *s != ':'; s++ )
                ;       /* NULL */
        if ( *s == '\0' ) {
-               Debug( LDAP_DEBUG_PARSE, "parse_line missing ':'\n", 0, 0, 0 );
+               ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
+                       "ldif_parse_line missing ':'\n");
                return( -1 );
        }
 
        /* trim any space between type and : */
-       for ( p = s - 1; p > line && isspace( *p ); p-- ) {
+       for ( p = s - 1; p > line && isspace( (unsigned char) *p ); p-- ) {
                *p = '\0';
        }
        *s++ = '\0';
@@ -88,13 +93,14 @@ str_parse_line(
        }
 
        /* skip space between : and value */
-       while ( isspace( *s ) ) {
+       while ( isspace( (unsigned char) *s ) ) {
                s++;
        }
 
        /* if no value is present, error out */
        if ( *s == '\0' ) {
-               Debug( LDAP_DEBUG_PARSE, "parse_line missing value\n", 0,0,0 );
+               ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
+                       "ldif_parse_line missing value\n");
                return( -1 );
        }
 
@@ -110,12 +116,12 @@ str_parse_line(
                stop = strchr( s, '\0' );
                byte = s;
                for ( p = s, *vlen = 0; p < stop; p += 4, *vlen += 3 ) {
-                       for ( i = 0; i < 3; i++ ) {
+                       for ( i = 0; i < 4; i++ ) {
                                if ( p[i] != '=' && (p[i] & 0x80 ||
                                    b642nib[ p[i] & 0x7f ] > 0x3f) ) {
-                                       Debug( LDAP_DEBUG_ANY,
-                                   "invalid base 64 encoding char (%c) 0x%x\n",
-                                           p[i], p[i], 0 );
+                                       ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
+"ldif_parse_line: invalid base 64 encoding char (%c) 0x%x\n",
+                                           p[i], p[i] );
                                        return( -1 );
                                }
                        }
@@ -154,7 +160,7 @@ str_parse_line(
 }
 
 /*
- * str_getline - return the next "line" (minus newline) of input from a
+ * ldif_getline - return the next "line" (minus newline) of input from a
  * string buffer of lines separated by newlines, terminated by \n\n
  * or \0.  this routine handles continued lines, bundling them into
  * a single big line before returning.  if a line begins with a white
@@ -168,10 +174,9 @@ str_parse_line(
  */
 
 char *
-str_getline( char **next )
+ldif_getline( char **next )
 {
-       char    *l;
-       char    c;
+       char            *l;
 
        if ( *next == NULL || **next == '\n' || **next == '\0' ) {
                return( NULL );
@@ -179,7 +184,7 @@ str_getline( char **next )
 
        l = *next;
        while ( (*next = strchr( *next, '\n' )) != NULL ) {
-               c = *(*next + 1);
+               unsigned char c = *(*next + 1);
                if ( isspace( c ) && c != '\n' ) {
                        **next = CONTINUED_LINE_MARKER;
                        *(*next+1) = CONTINUED_LINE_MARKER;
@@ -187,14 +192,18 @@ str_getline( char **next )
                        *(*next)++ = '\0';
                        break;
                }
-               *(*next)++;
+               (*next)++;
        }
 
        return( l );
 }
 
 void
-put_type_and_value( char **out, char *t, char *val, int vlen )
+ldif_put_type_and_value(
+       char **out,
+       LDAP_CONST char *t,
+       LDAP_CONST char *val,
+       int vlen )
 {
        unsigned char   *byte, *p, *stop;
        unsigned char   buf[3];
@@ -215,7 +224,7 @@ put_type_and_value( char **out, char *t, char *val, int vlen )
        b64 = 0;
 
        stop = (unsigned char *) (val + vlen);
-       if ( isascii( val[0] ) && isspace( val[0] ) || val[0] == ':' ) {
+       if ( isascii( val[0] ) && (isspace( val[0] ) || val[0] == ':') ) {
                b64 = 1;
        } else {
                for ( byte = (unsigned char *) val; byte < stop;
@@ -224,7 +233,7 @@ put_type_and_value( char **out, char *t, char *val, int vlen )
                                b64 = 1;
                                break;
                        }
-                       if ( len > LINE_WIDTH ) {
+                       if ( len > LDIF_LINE_WIDTH ) {
                                *(*out)++ = '\n';
                                *(*out)++ = ' ';
                                len = 1;
@@ -245,7 +254,7 @@ put_type_and_value( char **out, char *t, char *val, int vlen )
                        bits |= (byte[2] & 0xff);
 
                        for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
-                               if ( len > LINE_WIDTH ) {
+                               if ( len > LDIF_LINE_WIDTH ) {
                                        *(*out)++ = '\n';
                                        *(*out)++ = ' ';
                                        len = 1;
@@ -270,7 +279,7 @@ put_type_and_value( char **out, char *t, char *val, int vlen )
                        bits |= (byte[2] & 0xff);
 
                        for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
-                               if ( len > LINE_WIDTH ) {
+                               if ( len > LDIF_LINE_WIDTH ) {
                                        *(*out)++ = '\n';
                                        *(*out)++ = ' ';
                                        len = 1;
@@ -290,21 +299,25 @@ put_type_and_value( char **out, char *t, char *val, int vlen )
 
 
 char *
-ldif_type_and_value( char *type, char *val, int vlen )
+ldif_type_and_value( LDAP_CONST char *type, LDAP_CONST char *val, int vlen )
 /*
- * return malloc'd, zero-terminated LDIF line
+ * return BER malloc'd, zero-terminated LDIF line
  */
 {
     char       *buf, *p;
     int                tlen;
 
     tlen = strlen( type );
-    if (( buf = (char *)malloc( LDIF_SIZE_NEEDED( tlen, vlen ) + 1 )) !=
-           NULL ) {
+    if (( buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( tlen, vlen ) + 1 ))
+               == NULL )
+       {
+               ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
+                       "ldif_type_and_value: malloc failed!" );
+               return NULL;
     }
 
     p = buf;
-    put_type_and_value( &p, type, val, vlen );
+    ldif_put_type_and_value( &p, type, val, vlen );
     *p = '\0';
 
     return( buf );