summaryrefslogtreecommitdiff
path: root/rpmio/sexp/sexp-output.c
blob: cf2b23522b143bf26104a25e5b54158a93b7c4e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/* SEXP implementation code sexp-output.c
 * Ron Rivest
 * 5/5/1997
 */

#include <stdio.h>
#include <malloc.h>
#include "sexp.h"

/*@access sexpOutputStream @*/
/*@access sexpSimpleString @*/

/*@access sexpList @*/
/*@access sexpString @*/
/*@access sexpObject @*/

struct sexpOutputStream_s {
    long int column;          /* column where next character will go */
    long int maxcolumn;       /* max usable column, or -1 if no maximum */
    long int indent;          /* current indentation level (starts at 0) */
    void (*putChar)();        /* output a character */
    void (*newLine)();        /* go to next line (and indent) */
    int byteSize;             /* 4 or 6 or 8 depending on output mode */
    int bits;                 /* bits waiting to go out */
    int nBits;                /* number of bits waiting to go out */
    long int base64Count;     /* number of hex or base64 chars printed 
			         this region */
    int mode;                 /* ADVANCED, BASE64, or CANONICAL */
/*@shared@*/ /*@relnull@*/
    FILE *outputFile;         /* where to put output, if not stdout */
};

/*@unchecked@*/ /*@observer@*/
static char *hexDigits = "0123456789ABCDEF";

/*@unchecked@*/ /*@observer@*/
static char *base64Digits =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/***********************/
/* SEXP Output Streams */
/***********************/

/* putChar(os,c)
 * Puts the character c out on the output stream os.
 * Keeps track of the "column" the next output char will go to.
 */
void putChar(sexpOutputStream os, int c)
{
    (void) putc(c,os->outputFile);
    os->column++;
}

/* varPutChar(os,c)
 * putChar with variable sized output bytes considered.
 */
void varPutChar(sexpOutputStream os, int c)
{
    c &= 0xFF;
    os->bits = (os->bits << 8) | c;
    os->nBits += 8;
    while (os->nBits >= os->byteSize) {
	if ((os->byteSize==6 || os->byteSize==4
	   || c == '}' || c == '{' || c == '#' || c == '|')
	 && os->maxcolumn > 0
	 && os->column >= os->maxcolumn)
	    os->newLine(os,os->mode);
	if (os->byteSize == 4)
	    os->putChar(os,
		    hexDigits[(os->bits >> (os->nBits-4)) & 0x0F]);
	else if (os->byteSize == 6)
	    os->putChar(os,
		    base64Digits[(os->bits >> (os->nBits-6)) & 0x3F]);
	else if (os->byteSize == 8)
	    os->putChar(os,os->bits & 0xFF);
	os->nBits -= os->byteSize;
	os->base64Count++;
    }
}

/* changeOutputByteSize(os,newByteSize,mode)
 * Change os->byteSize to newByteSize
 * record mode in output stream for automatic line breaks
 */
void changeOutputByteSize(sexpOutputStream os, int newByteSize, int mode)
{
    if (newByteSize != 4 && newByteSize !=6 && newByteSize !=8)
	ErrorMessage(ERROR,"Illegal output base %d.",newByteSize);
    if (newByteSize !=8 && os->byteSize != 8)
	ErrorMessage(ERROR,"Illegal change of output byte size from %d to %d.",
		 os->byteSize, newByteSize);
    os->byteSize = newByteSize;
    os->nBits = 0;
    os->bits = 0;
    os->base64Count = 0;
    os->mode = mode;
}

/* flushOutput(os)
 * flush out any remaining bits
 */
void flushOutput(sexpOutputStream os)
{ if (os->nBits > 0)
    {
      if (os->byteSize == 4)
	os->putChar(os,hexDigits[(os->bits << (4 - os->nBits)) & 0x0F]);
      else if (os->byteSize == 6)
	os->putChar(os,base64Digits[(os->bits << (6 - os->nBits)) & 0x3F]);
      else if (os->byteSize == 8)
	os->putChar(os,os->bits & 0xFF);
      os->nBits = 0;
      os->base64Count++;
    }
  if (os->byteSize == 6) /* and add switch here */
    while ((os->base64Count & 3) != 0)
      { if (os->maxcolumn > 0 && os->column >= os->maxcolumn)
	  os->newLine(os,os->mode);
	os->putChar(os,'=');
	os->base64Count++;
      }
}

/* newLine(os,mode)
 * Outputs a newline symbol to the output stream os.
 * For ADVANCED mode, also outputs indentation as one blank per
 * indentation level (but never indents more than half of maxcolumn).
 * Resets column for next output character.
 */
void newLine(sexpOutputStream os, int mode)
{
    if (mode == ADVANCED || mode == BASE64) {
	os->putChar(os, '\n');
	os->column = 0;
    }
    if (mode == ADVANCED) {
	int i;
	for (i=0; i<os->indent && (4*i)<os->maxcolumn; i++)
	    os->putChar(os, ' ');
    }
}

void sexpOFnewLine(sexpOutputStream os, int mode)
{
    os->newLine(os, mode);
}

void sexpOFwidth(sexpOutputStream os, int width)
{
    if (width >= 0)
	os->maxcolumn = width;
}

/* sexpOFopen()
 * Creates and initializes new sexpOutputStream object.
 */
sexpOutputStream sexpOFopen(const char * ofn, const char * fmode)
{
    sexpOutputStream os;
    os = (sexpOutputStream) sexpAlloc(sizeof(*os));
    os->column = 0;
    os->maxcolumn = DEFAULTLINELENGTH;
    os->indent = 0;
    os->putChar = putChar;
    os->newLine = newLine;
    os->byteSize = 8;
    os->bits = 0;
    os->nBits = 0;
    os->mode = CANONICAL;

    if (ofn == NULL) {
/*@-assignexpose@*/
	os->outputFile = stdout;
/*@=assignexpose@*/
    } else {
	os->outputFile = fopen(ofn, fmode);
	if (os->outputFile == NULL)
	    ErrorMessage(ERROR, "Can't open output file %s.", ofn);
    }
    return os;
}

/*******************/
/* OUTPUT ROUTINES */
/*******************/

/* printDecimal(os,n)
 * print out n in decimal to output stream os
 */
void printDecimal(sexpOutputStream os, long int n)
{
    char buffer[50];
    int i;

/*@-bufferoverflowhigh@*/
    sprintf(buffer, "%ld", n);
/*@=bufferoverflowhigh@*/
    for (i=0; buffer[i]!=0; i++)
	varPutChar(os, buffer[i]);
}

/********************/
/* CANONICAL OUTPUT */
/********************/

/* canonicalPrintVerbatimSimpleString(os,ss)
 * Print out simple string ss on output stream os as verbatim string.
 */
void canonicalPrintVerbatimSimpleString(sexpOutputStream os, sexpSimpleString ss)
{
    octet * c = simpleStringString(ss);
    size_t len = simpleStringLength(ss);
    size_t i;

    if (c == NULL)
	ErrorMessage(ERROR, "Can't print NULL string verbatim");
    /* print out len: */
    printDecimal(os, (long int)len);
    varPutChar(os, ':');
    /* print characters in fragment */
    for (i=0; i<len; i++) varPutChar(os, (int)*c++);
}

/* canonicalPrintString(os,s)
 * Prints out sexp string s onto output stream os
 */
void canonicalPrintString(sexpOutputStream os, sexpString s)
{
    sexpSimpleString ph = sexpStringPresentationHint(s);
    sexpSimpleString ss;

    if (ph != NULL) {
	varPutChar(os, '[');
	canonicalPrintVerbatimSimpleString(os,ph);
	varPutChar(os, ']');
    }
    ss = sexpStringString(s);
    if (ss == NULL)
	ErrorMessage(ERROR,"NULL string can't be printed.");
    else
	canonicalPrintVerbatimSimpleString(os, ss);
}

/* canonicalPrintList(os,list)
 * Prints out the list "list" onto output stream os
 */
void canonicalPrintList(sexpOutputStream os, sexpList list)
{
    sexpIter iter = sexpListIter(list);
    sexpObject object;
    varPutChar(os, '(');
    while (iter != NULL) {
	object = sexpIterObject(iter);
	if (object != NULL)
	    canonicalPrintObject(os, object);
	iter = sexpIterNext(iter);
    }
    varPutChar(os, ')');
}

/* canonicalPrintObject(os,object)
 * Prints out object on output stream os
 * Note that this uses the common "type" field of lists and strings.
 */
void canonicalPrintObject(sexpOutputStream os, sexpObject object)
{
    if (isObjectString(object))
	canonicalPrintString(os, (sexpString)object);
    else if (isObjectList(object))
	canonicalPrintList(os, (sexpList)object);
    else ErrorMessage(ERROR,"NULL object can't be printed.");
}

/* *************/
/* BASE64 MODE */
/* *************/
/* Same as canonical, except all characters get put out as base 64 ones */

void base64PrintWholeObject(sexpOutputStream os, sexpObject object)
{
    changeOutputByteSize(os, 8, BASE64);
    varPutChar(os,'{');
    changeOutputByteSize(os, 6, BASE64);
    canonicalPrintObject(os, object);
    flushOutput(os);
    changeOutputByteSize(os, 8, BASE64);
    varPutChar(os,'}');
}

/*****************/
/* ADVANCED MODE */
/*****************/

/* TOKEN */

/* canPrintAsToken(ss)
 * Returns TRUE if simple string ss can be printed as a token.
 * Doesn't begin with a digit, and all characters are tokenchars.
 */
int canPrintAsToken(sexpOutputStream os, sexpSimpleString ss)
{
    octet * c = simpleStringString(ss);
    size_t len = simpleStringLength(ss);
    size_t i;

    if (c == NULL) return FALSE;
/*@-unsignedcompare@*/		/* XXX len now unsigned size_t. */
    if (len <= 0) return FALSE;
/*@=unsignedcompare@*/
    if (isDecDigit((int)*c)) return FALSE;
    if (os->maxcolumn > 0 && os->column + len >= os->maxcolumn)
	return FALSE;
    for (i=0; i<len; i++)
	if (!isTokenChar((int)(*c++))) return FALSE;
    return TRUE;
}

/* advancedPrintTokenSimpleString(os,ss)
 * Prints out simple string ss as a token (assumes that this is OK).
 * May run over max-column, but there is no fragmentation allowed...
 */
void advancedPrintTokenSimpleString(sexpOutputStream os, sexpSimpleString ss)
{
    octet * c = simpleStringString(ss);
    size_t len = simpleStringLength(ss);
    size_t i;

    if (os->maxcolumn>0 && os->column > (os->maxcolumn - len))
	os->newLine(os, ADVANCED);
    for (i=0; i<len; i++)
	os->putChar(os, (int)(*c++));
}

/* advancedLengthSimpleStringToken(ss)
 * Returns length for printing simple string ss as a token
 */
size_t advancedLengthSimpleStringToken(sexpSimpleString ss)
{
    return simpleStringLength(ss);
}


/* VERBATIM */

/* advancedPrintVerbatimSimpleString(os,ss)
 * Print out simple string ss on output stream os as verbatim string.
 * Again, can't fragment string, so max-column is just a suggestion...
 */
void advancedPrintVerbatimSimpleString(sexpOutputStream os, sexpSimpleString ss)
{
    octet * c = simpleStringString(ss);
    size_t len = simpleStringLength(ss);
    size_t i;

    if (c == NULL)
	ErrorMessage(ERROR, "Can't print NULL string verbatim");
    if (os->maxcolumn>0 && os->column > (os->maxcolumn - len))
	os->newLine(os, ADVANCED);
    printDecimal(os, (long int)len);
    os->putChar(os, ':');
    for (i=0; i<len; i++) os->putChar(os, (int)*c++);
}

/* advancedLengthSimpleStringVerbatim(ss)
 * Returns length for printing simple string ss in verbatim mode
 */
size_t advancedLengthSimpleStringVerbatim(sexpSimpleString ss)
{
    size_t len = simpleStringLength(ss);
    size_t i = 1;
    while (len > 9L) { i++; len = len / 10; }
    return (i+1+len);
}

/* BASE64 */

/* advancedPrintBase64SimpleString(os,ss)
 * Prints out simple string ss as a base64 value.
 */
void advancedPrintBase64SimpleString(sexpOutputStream os, sexpSimpleString ss)
{
    octet * c = simpleStringString(ss);
    size_t len = simpleStringLength(ss);
    size_t i;

    if (c == NULL)
	ErrorMessage(ERROR, "Can't print NULL string base 64");
    varPutChar(os,'|');
    changeOutputByteSize(os, 6, ADVANCED);
    for (i=0; i<len; i++)
	varPutChar(os, (int)(*c++));
    flushOutput(os);
    changeOutputByteSize(os, 8, ADVANCED);
    varPutChar(os,'|');
}

/* HEXADECIMAL */

/* advancedPrintHexSimpleString(os,ss)
 * Prints out simple string ss as a hexadecimal value.
 */
void advancedPrintHexSimpleString(sexpOutputStream os, sexpSimpleString ss)
{
    octet * c = simpleStringString(ss);
    size_t len = simpleStringLength(ss);
    size_t i;

    if (c == NULL)
	ErrorMessage(ERROR, "Can't print NULL string hexadecimal");
    os->putChar(os, '#');
    changeOutputByteSize(os, 4, ADVANCED);
    for (i=0; i<len; i++)
	varPutChar(os, (int)(*c++));
    flushOutput(os);
    changeOutputByteSize(os, 8, ADVANCED);
    os->putChar(os, '#');
}

/* advancedLengthSimpleStringHexadecimal(ss)
 * Returns length for printing simple string ss in hexadecimal mode
 */
int advancedLengthSimpleStringHexadecimal(sexpSimpleString ss)
{
    size_t len = simpleStringLength(ss);

    return (1+2*len+1);
}

/* QUOTED STRING */

/* canPrintAsQuotedString(ss)
 * Returns TRUE if simple string ss can be printed as a quoted string.
 * Must have only tokenchars and blanks.
 */
int canPrintAsQuotedString(sexpSimpleString ss)
{
    octet * c = simpleStringString(ss);
    size_t len = simpleStringLength(ss);
    size_t i;

    if (c == NULL) return FALSE ;
/*@-unsignedcompare@*/		/* XXX len now unsigned size_t. */
    if (len < 0) return FALSE;
/*@=unsignedcompare@*/
    for (i=0; i<len; i++,c++) {
	if (!isTokenChar((int)(*c)) && *c != ' ')
	    return FALSE;
    }
    return TRUE;
}

/* advancedPrintQuotedStringSimpleString(os,ss)
 * Prints out simple string ss as a quoted string
 * This code assumes that all characters are tokenchars and blanks,
 *  so no escape sequences need to be generated.
 * May run over max-column, but there is no fragmentation allowed...
 */
void advancedPrintQuotedStringSimpleString(sexpOutputStream os, sexpSimpleString ss)
{
    octet * c = simpleStringString(ss);
    size_t len = simpleStringLength(ss);
    size_t i;

    os->putChar(os, '\"');
    for (i=0; i<len; i++) {
	if ( os->maxcolumn>0 && os->column >= os->maxcolumn-2 ) {
	    os->putChar(os, '\\');
	    os->putChar(os, '\n');
	    os->column = 0;
	}
	os->putChar(os, *c++);
    }
    os->putChar(os, '\"');
}

/* advancedLengthSimpleStringQuotedString(ss)
 * Returns length for printing simple string ss in quoted-string mode
 */
int advancedLengthSimpleStringQuotedString(sexpSimpleString ss)
{
    size_t len = simpleStringLength(ss);

    return (1+len+1);
}

/* SIMPLE STRING */

/* advancedPrintSimpleString(os,ss)
 * Prints out simple string ss onto output stream ss
 */
void advancedPrintSimpleString(sexpOutputStream os, sexpSimpleString ss)
{
    size_t len = simpleStringLength(ss);

    if (canPrintAsToken(os, ss))
	advancedPrintTokenSimpleString(os, ss);
    else if (canPrintAsQuotedString(ss))
	advancedPrintQuotedStringSimpleString(os, ss);
    else if (len <= 4 && os->byteSize == 8)
	advancedPrintHexSimpleString(os,ss);
    else if (os->byteSize == 8)
	advancedPrintBase64SimpleString(os, ss);
    else
	ErrorMessage(ERROR,"Can't print advanced mode with restricted output character set.");
}

/* advancedPrintString(os,s)
 * Prints out sexp string s onto output stream os
 */
void advancedPrintString(sexpOutputStream os, sexpString s)
{
    sexpSimpleString ph = sexpStringPresentationHint(s);
    sexpSimpleString ss = sexpStringString(s);

    if (ph != NULL) {
	os->putChar(os, '[');
	advancedPrintSimpleString(os, ph);
	os->putChar(os, ']');
    }
    if (ss == NULL)
	ErrorMessage(ERROR, "NULL string can't be printed.");
    else
	advancedPrintSimpleString(os, ss);
}

/* advancedLengthSimpleStringBase64(ss)
 * Returns length for printing simple string ss as a base64 string
 */
int advancedLengthSimpleStringBase64(sexpSimpleString ss)
{
    return (2+4*((simpleStringLength(ss)+2)/3));
}

/* advancedLengthSimpleString(os,ss)
 * Returns length of printed image of s
 */
size_t advancedLengthSimpleString(sexpOutputStream os, sexpSimpleString ss)
{
    size_t len = simpleStringLength(ss);

    if (canPrintAsToken(os,ss))
	return advancedLengthSimpleStringToken(ss);
    else if (canPrintAsQuotedString(ss))
	return advancedLengthSimpleStringQuotedString(ss);
    else if (len <= 4 && os->byteSize == 8)
	return advancedLengthSimpleStringHexadecimal(ss);
    else if (os->byteSize == 8)
	return advancedLengthSimpleStringBase64(ss);
    else
	return 0;  /* an error condition */
}

/* advancedLengthString(os,s)
 * Returns length of printed image of string s
 */
size_t advancedLengthString(sexpOutputStream os, sexpString s)
{
    sexpSimpleString ph = sexpStringPresentationHint(s);
    sexpSimpleString ss = sexpStringString(s);
    size_t len = 0;

    if (ph != NULL)
	len += 2+advancedLengthSimpleString(os, ph);
    if (ss != NULL)
	len += advancedLengthSimpleString(os, ss);
    return len;
}

/* advancedLengthList(os,list)
 * Returns length of printed image of list given as iterator
 */
size_t advancedLengthList(sexpOutputStream os, sexpList list)
{
    sexpIter iter = sexpListIter(list);
    sexpObject object;
    size_t len = 1;			/* for left paren */

    while (iter != NULL) {
	object = sexpIterObject(iter);
	if (object != NULL) {
	    if (isObjectString(object))
		len += advancedLengthString(os, ((sexpString)object));
	    /* else */
	    if (isObjectList(object))
		len += advancedLengthList(os, ((sexpList)object));
	    len++;			/* for space after item */
	}
	iter = sexpIterNext(iter);
    }
    return len+1;			/* for final paren */
}

/* advancedPrintList(os,list)
 * Prints out the list "list" onto output stream os.
 * Uses print-length to determine length of the image.  If it all fits
 * on the current line, then it is printed that way.  Otherwise, it is
 * written out in "vertical" mode, with items of the list starting in
 * the same column on successive lines.
 */
void advancedPrintList(sexpOutputStream os, sexpList list)
{
    int vertical = FALSE;
    int firstelement = TRUE;
    sexpIter iter;
    sexpObject object;

    os->putChar(os,'(');
    os->indent++;
    if (advancedLengthList(os,list) > os->maxcolumn - os->column)
	vertical = TRUE;
    iter = sexpListIter(list);
    while (iter != NULL) {
	object = sexpIterObject(iter);
	if (object != NULL) {
	    if (!firstelement) {
		if (vertical) os->newLine(os,ADVANCED);
		else          os->putChar(os,' ');
	    }
	  advancedPrintObject(os, object);
	}
      iter = sexpIterNext(iter);
      firstelement = FALSE;
    }
    if (os->maxcolumn>0 && os->column>os->maxcolumn-2)
	os->newLine(os, ADVANCED);
    os->indent--;
    os->putChar(os,')');
}

/* advancedPrintObject(os,object)
 * Prints out object on output stream os
 */
void advancedPrintObject(sexpOutputStream os, sexpObject object)
{
    if (os->maxcolumn>0 && os->column>os->maxcolumn-4)
	os->newLine(os, ADVANCED);
    if (isObjectString(object))
	advancedPrintString(os, (sexpString)object);
    else if (isObjectList(object))
	advancedPrintList(os, (sexpList)object);
    else
	ErrorMessage(ERROR, "NULL object can't be printed.");
}