]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/ldif.c
Happy New Year!
[openldap] / libraries / libldap / ldif.c
index 529b0a9a207bbb7716ea85597855f4a6184204b9..f6c68407a0c17b390f95671938ccc563c217e093 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2013 The OpenLDAP Foundation.
+ * Copyright 1998-2016 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -486,8 +486,8 @@ ldif_must_b64_encode( LDAP_CONST char *s )
        return 0;
 }
 
-/* compatibility with U-Mich off by one bug */
-#define LDIF_KLUDGE 1
+/* compatibility with U-Mich off by two bug */
+#define LDIF_KLUDGE 2
 
 /* NOTE: only preserved for binary compatibility */
 void
@@ -498,7 +498,7 @@ ldif_sput(
        LDAP_CONST char *val,
        ber_len_t vlen )
 {
-       ldif_sput_wrap( out, type, name, val, vlen, LDIF_LINE_WIDTH );
+       ldif_sput_wrap( out, type, name, val, vlen, LDIF_LINE_WIDTH+LDIF_KLUDGE );
 }
 
 void
@@ -521,7 +521,8 @@ ldif_sput_wrap(
        ber_len_t len=0;
        ber_len_t i;
 
-       wrap = LDIF_LINE_WIDTH_WRAP( wrap );
+       if ( !wrap )
+               wrap = LDIF_LINE_WIDTH+LDIF_KLUDGE;
 
        /* prefix */
        switch( type ) {
@@ -633,7 +634,7 @@ ldif_sput_wrap(
                                b64 = 1;
                                break;
                        }
-                       if ( len - LDIF_KLUDGE > wrap ) {
+                       if ( len >= wrap ) {
                                *(*out)++ = '\n';
                                *(*out)++ = ' ';
                                len = 1;
@@ -662,7 +663,7 @@ ldif_sput_wrap(
                bits |= (byte[2] & 0xff);
 
                for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
-                       if ( len - LDIF_KLUDGE > wrap ) {
+                       if ( len >= wrap ) {
                                *(*out)++ = '\n';
                                *(*out)++ = ' ';
                                len = 1;
@@ -687,7 +688,7 @@ ldif_sput_wrap(
                bits |= (byte[2] & 0xff);
 
                for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
-                       if ( len - LDIF_KLUDGE > wrap ) {
+                       if ( len >= wrap ) {
                                *(*out)++ = '\n';
                                *(*out)++ = ' ';
                                len = 1;
@@ -837,27 +838,36 @@ ldif_read_record(
                                break;
                        }
                }
-               if ( stop )
-                       break;
+               if ( !stop ) {
+                       if ( fgets( line, sizeof( line ), lfp->fp ) == NULL ) {
+                               stop = 1;
+                               len = 0;
+                       } else {
+                               len = strlen( line );
+                       }
+               }
 
-               if ( fgets( line, sizeof( line ), lfp->fp ) == NULL ) {
-                       stop = 1;
-                       len = 0;
-               } else {
-                       len = strlen( line );
+               if ( stop ) {
+                       /* Add \n in case the file does not end with newline */
+                       if (last_ch != '\n') {
+                               len = 1;
+                               line[0] = '\n';
+                               line[1] = '\0';
+                               goto last;
+                       }
+                       break;
                }
 
-               if ( len == 0 || line[len-1] != '\n' ) {
-                       /* Add \n in case the line/file does not end with newline */
-                       line[len] = '\n';
-                       line[++len] = '\0';
+               /* Squash \r\n to \n */
+               if ( len > 1 && line[len-2] == '\r' ) {
+                       len--;
+                       line[len-1] = '\n';
                }
 
                if ( last_ch == '\n' ) {
                        (*lno)++;
 
-                       if ( line[0] == '\n' ||
-                               ( line[0] == '\r' && line[1] == '\n' )) {
+                       if ( line[0] == '\n' ) {
                                if ( !found_entry ) {
                                        lcur = 0;
                                        top_comment = 0;
@@ -887,10 +897,6 @@ ldif_read_record(
                                                        len--;
                                                        line[len] = '\0';
                                                }
-                                               if ( line[len-1] == '\r' ) {
-                                                       len--;
-                                                       line[len] = '\0';
-                                               }
 
                                                ptr = line + STRLENOF("include:");
                                                while (isspace((unsigned char) *ptr)) ptr++;
@@ -918,9 +924,10 @@ ldif_read_record(
                                                }
                                        }
                                }
-                       }                       
+                       }
                }
 
+last:
                if ( *buflenp - lcur <= len ) {
                        *buflenp += len + LDIF_MAXLINE;
                        nbufp = ber_memrealloc( *bufp, *buflenp );