3 * $Id: ldif.c,v 1.5 2005/07/29 06:28:55 jheinonen Exp $
4 * adapted to use with abook by JH <jheinonen@users.sourceforge.net>
9 * Copyright (c) 1992-1996 Regents of the University of Michigan.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms are permitted
13 * provided that this notice is preserved and that due credit is given
14 * to the University of Michigan at Ann Arbor. The name of the University
15 * may not be used to endorse or promote products derived from this
16 * software without specific prior written permission. This software
17 * is provided ``as is'' without express or implied warranty.
25 #include <sys/types.h>
26 #include <sys/socket.h>
29 #define ISSPACE(c) isspace((unsigned char)c)
31 #define LDAP_DEBUG_PARSE 0x800
32 #define LDAP_DEBUG_ANY 0xffff
33 #define LDIF_LINE_WIDTH 76 /* maximum length of LDIF lines */
34 #define LDIF_BASE64_LEN(vlen) (((vlen) * 4 / 3 ) + 3)
36 #define LDIF_SIZE_NEEDED(tlen,vlen) \
37 ((tlen) + 4 + LDIF_BASE64_LEN(vlen) \
38 + ((LDIF_BASE64_LEN(vlen) + tlen + 3) / LDIF_LINE_WIDTH * 2 ))
41 #define Debug( level, fmt, arg1, arg2, arg3 )
45 #define CONTINUED_LINE_MARKER '\001'
47 static char nib2b64
[0x40f] =
48 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
50 static unsigned char b642nib
[0x80] = {
51 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
52 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
53 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
54 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
55 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
56 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
57 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
58 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
59 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
60 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
61 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
62 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
63 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
64 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
65 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
66 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
70 * str_parse_line - takes a line of the form "type:[:] value" and splits it
71 * into components "type" and "value". if a double colon separates type from
72 * value, then value is encoded in base 64, and parse_line un-decodes it
73 * (in place) before returning.
77 str_parse_line(char *line
, char **type
, char **value
, int *vlen
)
79 char *p
, *s
, *d
, *byte
, *stop
;
83 /* skip any leading space */
84 while(ISSPACE(*line
)) {
89 for(s
= line
; *s
&& *s
!= ':'; s
++); /* NULL */
91 Debug(LDAP_DEBUG_PARSE
, "parse_line missing ':'\n", 0, 0,
96 /* trim any space between type and : */
97 for(p
= s
- 1; p
> line
&& ISSPACE(*p
); p
--) {
102 /* check for double : - indicates base 64 encoded value */
107 /* single : - normally encoded value */
112 /* skip space between : and value */
117 /* if no value is present, error out */
119 Debug(LDAP_DEBUG_PARSE
, "parse_line missing value\n", 0, 0,
124 /* check for continued line markers that should be deleted */
125 for(p
= s
, d
= s
; *p
; p
++) {
126 if(*p
!= CONTINUED_LINE_MARKER
)
133 stop
= strchr(s
, '\0');
135 for(p
= s
, *vlen
= 0; p
< stop
; p
+= 4, *vlen
+= 3) {
136 for(i
= 0; i
< 3; i
++) {
137 if(p
[i
] != '=' && (p
[i
] & 0x80 ||
138 b642nib
[p
[i
] & 0x7f] >
140 Debug(LDAP_DEBUG_ANY
,
141 "invalid base 64 encoding char (%c) 0x%x\n",
148 nib
= b642nib
[p
[0] & 0x7f];
151 nib
= b642nib
[p
[1] & 0x7f];
153 byte
[1] = (nib
& RIGHT4
) << 4;
159 nib
= b642nib
[p
[2] & 0x7f];
161 byte
[2] = (nib
& RIGHT2
) << 6;
167 nib
= b642nib
[p
[3] & 0x7f];
174 *vlen
= (int) (d
- s
);
183 * str_getline - return the next "line" (minus newline) of input from a
184 * string buffer of lines separated by newlines, terminated by \n\n
185 * or \0. this routine handles continued lines, bundling them into
186 * a single big line before returning. if a line begins with a white
187 * space character, it is a continuation of the previous line. the white
188 * space character (nb: only one char), and preceeding newline are changed
189 * into CONTINUED_LINE_MARKER chars, to be deleted later by the
190 * str_parse_line() routine above.
192 * it takes a pointer to a pointer to the buffer on the first call,
193 * which it updates and must be supplied on subsequent calls.
197 str_getline(char **next
)
202 if(*next
== NULL
|| **next
== '\n' || **next
== '\0') {
207 while((*next
= strchr(*next
, '\n')) != NULL
) {
209 if(ISSPACE(c
) && c
!= '\n') {
210 **next
= CONTINUED_LINE_MARKER
;
211 *(*next
+ 1) = CONTINUED_LINE_MARKER
;
225 put_type_and_value(char **out
, char *t
, char *val
, int vlen
)
227 unsigned char *byte
, *p
, *stop
;
228 unsigned char buf
[3];
231 int i
, b64
, pad
, len
, savelen
;
234 /* put the type + ": " */
235 for(p
= (unsigned char *) t
; *p
; p
++, len
++) {
245 stop
= (unsigned char *) (val
+ vlen
);
246 if(isascii(val
[0]) && (ISSPACE(val
[0]) || val
[0] == ':')) {
249 for(byte
= (unsigned char *) val
; byte
< stop
;
251 if(!isascii(*byte
) || !isprint(*byte
)) {
255 if(len
> LDIF_LINE_WIDTH
) {
268 /* convert to base 64 (3 bytes => 4 base 64 digits) */
269 for(byte
= (unsigned char *) val
; byte
< stop
- 2;
271 bits
= (byte
[0] & 0xff) << 16;
272 bits
|= (byte
[1] & 0xff) << 8;
273 bits
|= (byte
[2] & 0xff);
275 for(i
= 0; i
< 4; i
++, len
++, bits
<<= 6) {
276 if(len
> LDIF_LINE_WIDTH
) {
282 /* get b64 digit from high order 6 bits */
284 nib2b64
[(bits
& 0xfc0000L
) >> 18];
288 /* add padding if necessary */
290 for(i
= 0; byte
+ i
< stop
; i
++) {
293 for(pad
= 0; i
< 3; i
++, pad
++) {
297 bits
= (byte
[0] & 0xff) << 16;
298 bits
|= (byte
[1] & 0xff) << 8;
299 bits
|= (byte
[2] & 0xff);
301 for(i
= 0; i
< 4; i
++, len
++, bits
<<= 6) {
302 if(len
> LDIF_LINE_WIDTH
) {
308 /* get b64 digit from low order 6 bits */
310 nib2b64
[(bits
& 0xfc0000L
) >> 18];
313 for(; pad
> 0; pad
--) {
323 ldif_type_and_value(char *type
, char *val
, int vlen
)
325 * return malloc'd, zero-terminated LDIF line
334 t
= LDIF_SIZE_NEEDED(tlen
, vlen
);
335 if((bufsize
= t
+ 1) <= t
)
338 if((buf
= malloc(bufsize
)) == NULL
) {
343 put_type_and_value(&p
, type
, val
, vlen
);