File: bc_emit.c

package info (click to toggle)
kolab-cyrus-imapd 2.2.13-2
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 14,028 kB
  • ctags: 8,058
  • sloc: ansic: 83,921; sh: 36,867; perl: 3,994; makefile: 1,416; yacc: 949; awk: 302; lex: 249; asm: 214
file content (673 lines) | stat: -rw-r--r-- 16,427 bytes parent folder | download | duplicates (8)
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/* bc_emit.c -- sieve bytecode - pass 2 of the compiler
 * Rob Siemborski
 * Jen Smith
 * $Id: bc_emit.c,v 1.2 2003/10/22 18:03:24 rjs3 Exp $
 */
/***********************************************************
        Copyright 2001 by Carnegie Mellon University

                      All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Carnegie Mellon
University not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.

CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR
ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "xmalloc.h"
#include "sieve_interface.h"

 
#include "bytecode.h"

#include <syslog.h>
#include <sys/types.h>
#include <unistd.h>


#if DUMPCODE
void dump(bytecode_info_t *d);
#endif

inline int write_int (int fd, int x)
{
    int y=htonl(x);
    return (write(fd, &y, sizeof(int)));
}
 
    

struct bytecode_info 
{
    bytecode_t *data;/* pointer to almost-flat bytecode */
    size_t scriptend; /* used by emit code to know final length of bytecode  */
    size_t reallen; /* allocated length of 'data' */
};

/* Pad null bytes onto the end of the string we just wrote */
/* returns -1 on failure or number of bytes written on success */
static int align_string(int fd, int string_len) 
{
    /* Keep in mind that we always want to pad a string with *at least*
     * one zero, that's why sometimes we have to pad with 4 */
    int needed = sizeof(int) - (string_len % sizeof(int));
    if (needed>= 0 && needed <=4)
    {
    	if(write(fd, "\0\0\0\0", needed) == -1) return -1;
    }
    return needed;
}

/*all functions keep codep up to date as they use it.
  the amount that has been written to the file is maintained by the
  filelen variable in bc_action_emit
  the other bc_xxx_emit funtions keep track of how much they (and any functions they call) have written and return this value
*/


/* Write out a stringlist to a given file descriptor.
 * return # of bytes written on success and -1 on error */

/* stringlist: <# listitems>
               <pos of listend (bytes)>
               <string:(size)(aligned string)>
*/
static int bc_stringlist_emit(int fd, int *codep, bytecode_info_t *bc) 
{
    int len = bc->data[(*codep)++].len;
    int i;
    int ret;
    int wrote = 2*sizeof(int);
    int begin,end;

    /* Write out number of items in the list */
    if (write_int(fd, len)== -1) return -1 ;
    
    /* skip one spot end of list position*/
    begin=lseek(fd,0,SEEK_CUR);
    lseek(fd,sizeof(int),SEEK_CUR);
    
    /* Loop through all the items of the list, writing out length and string
     * in sequence */
    for(i=0; i < len; i++)
    {
	int datalen = bc->data[(*codep)++].len;
	
	if(write_int(fd, datalen) == -1) return -1;
	wrote += sizeof(int);
	
	if(write(fd, bc->data[(*codep)++].str, datalen) == -1) return -1;
	wrote += datalen;
	
	ret = align_string(fd,datalen);
	if(ret == -1) return -1;
	
	wrote+=ret;
    }
    end=lseek(fd,0,SEEK_CUR);
 
    /* go back and write end of list position */
    lseek(fd,begin,SEEK_SET);
    if(write_int(fd, end) == -1) return -1;

    /* return to the end */
    lseek(fd,end,SEEK_SET);
    return wrote;
}

static int bc_test_emit(int fd, int *codep, bytecode_info_t *bc);

/* Write out a testlist to a given file descriptor.
 * return # of bytes written on success and -1 on error */
static int bc_testlist_emit(int fd, int *codep, bytecode_info_t *bc) 
{
    int len = bc->data[(*codep)++].len;
    int i;
    int ret;
    int begin, end;
    int wrote = 2*sizeof(int);
        
    /* Write out number of items in the list */
    if(write_int(fd, len)== -1) return -1;

    /* skip one spot for end of list position*/
    begin = lseek(fd, 0, SEEK_CUR);
    lseek(fd, sizeof(int), SEEK_CUR);
      
    /* Loop through all the items of the list, writing out each
     * test as we reach it in sequence. */
    for(i=0; i < len; i++) {
	int nextcodep = bc->data[(*codep)++].jump;
	
	ret = bc_test_emit(fd, codep, bc);
	if(ret < 0 ) return -1;
	
	wrote+=ret;
	*codep = nextcodep;
    }
    end = lseek(fd, 0, SEEK_CUR);

    /* go back and write the end of list position */
    lseek(fd,begin,SEEK_SET);
    if(write_int(fd, end) == -1) return -1;

    /*return to the end */
    lseek(fd,end,SEEK_SET);

    return wrote;
}

/* emit the bytecode for a test.  returns -1 on failure or size of
 * emitted bytecode on success */
static int bc_test_emit(int fd, int *codep, bytecode_info_t *bc) 
{
    int wrote=0;/* Relative offset to account for interleaved strings */
    
    
    int ret; /* Temporary Return Value Variable */
    
    /* Output this opcode */
    if(write_int(fd, bc->data[(*codep)].op) == -1)
	return -1;
    wrote += sizeof(int);
    
    switch(bc->data[(*codep)++].op) {
    case BC_TRUE:
    case BC_FALSE:
	/* No parameter opcodes */
	break;
	
    case BC_NOT:
    {
	/* Single parameter: another test */
	ret = bc_test_emit(fd, codep, bc);
	if(ret < 0)
	    return -1;
	else
	    wrote+=ret;
	break;
    }
    
    case BC_ALLOF:
    case BC_ANYOF:
	/*where we jump to?*/
	/* Just drop a testlist */
	ret = bc_testlist_emit(fd, codep, bc);
	if(ret < 0)
	    return -1;
	else
	    wrote+=ret;
	break;
	
    case BC_SIZE:
	/* Drop tag and number */
	if(write_int(fd, bc->data[(*codep)].value) == -1)
	    return -1;
	if(write_int(fd, bc->data[(*codep)+1].value) == -1)
	    return -1;
	
	wrote += 2 * sizeof(int);
	(*codep) += 2;
	break;
	
    case BC_EXISTS:
    {
	int ret;
	ret = bc_stringlist_emit(fd, codep, bc);
	if(ret < 0) return -1;
	wrote += ret;
	break;
    }
    
    case BC_HEADER:
    {
	int ret;
	/* Drop match type */
	if(write_int(fd, bc->data[(*codep)].value) == -1)
	    return -1;
	wrote += sizeof(int);
	(*codep)++;
	/*drop comparator */
	if(write_int(fd, bc->data[(*codep)].value) == -1)
	    return -1;
	wrote += sizeof(int);
	(*codep)++;    
	/*now drop relation*/
	if(write_int(fd, bc->data[(*codep)].value) == -1)
	    return -1;
	wrote += sizeof(int);
	(*codep)++;
	/* Now drop headers */
	ret = bc_stringlist_emit(fd, codep, bc);
	if(ret < 0) return -1;
	wrote+=ret;
	/* Now drop data */
	ret = bc_stringlist_emit(fd, codep, bc);
	if(ret < 0) return -1;
	wrote+=ret;
	break;
    }
    
    case BC_ADDRESS:
    case BC_ENVELOPE:
    {
	int ret;
	/* Drop match type */
	if(write_int(fd, bc->data[(*codep)].value) == -1)
	    return -1;
	wrote += sizeof(int);
	(*codep)++;
	/*drop comparator */
	if(write_int(fd, bc->data[(*codep)].value) == -1)
	    return -1;
	wrote += sizeof(int);
	(*codep)++;
	/*now drop relation*/
	if(write_int(fd, bc->data[(*codep)].value) == -1)
	    return -1;
	wrote += sizeof(int);
	(*codep)++;
	/*now drop address part*/
	if(write_int(fd, bc->data[(*codep)].value) == -1)
	    return -1;
	wrote += sizeof(int);
	(*codep)++;
	/* Now drop headers */
	ret = bc_stringlist_emit(fd, codep, bc);
	if(ret < 0) return -1;
	wrote+=ret;
	/* Now drop data */
	ret = bc_stringlist_emit(fd, codep, bc);
	if(ret < 0) return -1;
	wrote+=ret;
	break;
    }
    
    default:
	/* Unknown testcode? */
	return -1;
    }
    return wrote;
}

/* emit the bytecode to a file descriptor given a flattened parse tree
 * returns -1 on failure, size of emitted bytecode on success.
 *
 * this takes care of everything except the comparisons */
static int bc_action_emit(int fd, int codep, int stopcodep,
			  bytecode_info_t *bc, int filelen) 
{
    int len; /* Temporary Length Variable */
    int ret; /* Temporary Return Value Variable */
    int start_filelen = filelen;
    int i;
    
    /*debugging variable to check filelen*/
    /*int location;*/
    
    syslog(LOG_DEBUG, "entered bc_action_emit with filelen: %d", filelen);
    
    /* All non-string data MUST be sizeof(int)
       byte alligned so the end of each string may require a pad */
    /*
     * Note that for purposes of jumps you must multiply codep by sizeof(int)
     */
    while(codep < stopcodep) {
	/* Output this opcode */
	if(write_int(fd, bc->data[codep].op) == -1)
	    return -1; 
	
	filelen+=sizeof(int);
	
	switch(bc->data[codep++].op) {

	case B_IF:
	{
	    /* IF
	     *  test
	     *  jump (false condition)
	     *  then
	     * (if there is an else) jump(finish) 
	     * (if there is an else) else
	     */

	    int testEndLoc=-1;
	    int testdist, thendist, elsedist;
	    int c;
	    
	    int jumpFalseLoc=-1;/*this is the location that is being reserved
				  for the first jump command
				  we jump to the false condition of the test*/
	    
	    int jumpEndLoc=-1; /* this is the location that is being reserved
				  for the optional jump command
				  it jumps over the else statement to the end*/
	    int jumpto=-1;
	    int jumpop= B_JUMP;

	    /*leave space to store the location of end of the test*/
	    ret = lseek(fd, sizeof(int), SEEK_CUR);
	    if(ret == -1) return ret;
	    
	    testEndLoc=filelen;
	    filelen+=sizeof(int);
	    
	    /* spew the test */

	    c=codep+3;
	    testdist = bc_test_emit(fd, &c, bc);
	    if(testdist == -1)return -1;
	    filelen +=testdist;
	    
            /*store the location for hte end of the test
	     *this is important for short circuiting of allof/anyof*/
	    jumpto=filelen/4;
	    if(lseek(fd, testEndLoc, SEEK_SET) == -1)
		return -1;
	    if(write_int(fd,jumpto) == -1)
		return -1;

	    if(lseek(fd,filelen,SEEK_SET) == -1)
		return -1;

	    /* leave space for jump */
	    if(write_int(fd, jumpop) == -1)
		return -1;
	    ret = lseek(fd, sizeof(int), SEEK_CUR);
	    if(ret == -1)
		return ret;
	    jumpFalseLoc=filelen+sizeof(int);
	    
	    filelen +=2*sizeof(int); /*jumpop + jump*/
	    
	    /* spew the then code */ 
	    thendist = bc_action_emit(fd, bc->data[codep].value,
				      bc->data[codep+1].value, bc,
				      filelen);
	 
	    filelen+=thendist;
	  	    
	    /* there is an else case */
	    if(bc->data[codep+2].value != -1)
	    {
		/* leave space for jump */
		if(write_int(fd, jumpop) == -1)
		    return -1;
		ret = lseek(fd, sizeof(int), SEEK_CUR);
		if(ret == -1)
		    return ret;

		jumpEndLoc=filelen+sizeof(int);
		filelen+=2*sizeof(int);/*jumpop + jump*/
	    }
	  
	    /*put previous jump to the end of the then code,
	     *or the end of the jump if there is an else case */
	    jumpto=filelen/4;
	    if(lseek(fd, jumpFalseLoc, SEEK_SET) == -1)
		return -1;
	    if(write_int(fd,jumpto) == -1)
		return -1;
	    if(lseek(fd,filelen,SEEK_SET) == -1)
		return -1;
	    
	    /* there is an else case */
	    if(bc->data[codep+2].value != -1) {
		/* spew the else code */
		elsedist = bc_action_emit(fd, bc->data[codep+1].value,
					 bc->data[codep+2].value, bc,
					 filelen);
	
		filelen+=elsedist;
		
		/*put jump to the end of the else code*/
	        jumpto=filelen/4;
		if(lseek(fd, jumpEndLoc, SEEK_SET) == -1)
		    return -1;
		if(write_int(fd,jumpto) == -1)
		    return -1;
		if(lseek(fd,filelen,SEEK_SET) == -1)
		    return -1;
		
		codep = bc->data[codep+2].value;
	    } else {
		codep = bc->data[codep+1].value;
	    }
	    
	    break;
	}
	
	case B_REJECT:
	case B_FILEINTO:
	case B_REDIRECT:
	    /*just a string*/
	    len = bc->data[codep++].len;
	    if(write_int(fd,len) == -1)
		return -1;

	    filelen+=sizeof(int);
	    
	    if(write(fd,bc->data[codep++].str,len) == -1)
		return -1;
	    
	    ret = align_string(fd, len);
	    if(ret == -1)
		return -1;

	    filelen += len + ret;
	    
	    break; 

	case B_SETFLAG:
	case B_ADDFLAG:
	case B_REMOVEFLAG:
	    /* Dump just a stringlist */
	    ret = bc_stringlist_emit(fd, &codep, bc);
	    if(ret < 0)
		return -1;
	    filelen += ret;
	    break;
	    
	case B_NOTIFY:
	    /* method string, id string, options string list,
	       priotity, Message String */
	    /*method and id*/
	    for(i=0; i<2; i++) {
		len = bc->data[codep++].len;
		if(write_int(fd,len) == -1)
		    return -1;
		filelen += sizeof(int);
		if(len == -1)
		{
                    /* this will probably only happen for the id */
		    /* this is a nil string */
		    /* skip the null pointer and make up for it 
		     * by adjusting the offset */
		    codep++;
		}
		else
		{	
		    if(write(fd,bc->data[codep++].str,len) == -1)
			return -1;
		    
		    ret = align_string(fd, len);
		    if(ret == -1)
			return -1;
		    
		    filelen += len + ret;
		}
		
	    }
	    /*options */
	    ret = bc_stringlist_emit(fd, &codep, bc);
	    if(ret < 0)
		return -1;
	    filelen+=ret;
	    
	    /*priority*/
	    if(write_int(fd, bc->data[codep].value) == -1)
		return -1;
	    codep++;
	    filelen += sizeof(int);
	    
	    len = bc->data[codep++].len;
	    if(write_int(fd,len) == -1)
		return -1;
	    filelen += sizeof(int);
	    
	    if(write(fd,bc->data[codep++].str,len) == -1)
		return -1;
	    
	    ret = align_string(fd, len);
	    if(ret == -1) return -1;
	    
 	    filelen += len + ret;
	    break;

		
	case B_DENOTIFY:
	    /* priority num,comptype  num,relat num, comp string*/ 

	    /* priority*/
	    if(write_int(fd, bc->data[codep].value) == -1)
		return -1;
	    filelen += sizeof(int);
	    codep++;
	    /* comptype */
	    if(write_int(fd, bc->data[codep].value) == -1)
		return -1;
	    filelen += sizeof(int);
	    codep++;
	    /* relational*/
	    if(write_int(fd, bc->data[codep].value) == -1)
		return -1;
	    filelen += sizeof(int);
	    codep++;
	    /* comp string*/
	    
	    len = bc->data[codep++].len;
	    if(write_int(fd,len) == -1)
		return -1;
	    filelen += sizeof(int);
	    
	    if(len == -1)
	    {
		/* this is a nil string */
		/* skip the null pointer and make up for it 
		 * by adjusting the offset */
		codep++;
	    }
	    else
	    {
		if(write(fd,bc->data[codep++].str,len) == -1)
		    return -1;
		
		ret = align_string(fd, len);
		if(ret == -1) return -1;
		
		filelen += len + ret;
	    }
	    	    break;
	case B_VACATION:
	    /* Address list, Subject String, Message String,
	       Days (word), Mime (word) */
	   
	        /*new code-this might be broken*/
	    ret = bc_stringlist_emit(fd, &codep, bc);
	    if(ret < 0) return -1;
	    filelen += ret;
	    /*end of new code*/

	    for(i=0; i<2; i++) {/*writing strings*/

		/*write length of string*/
		len = bc->data[codep++].len;
		if(write_int(fd,len) == -1)
		    return -1;
		filelen += sizeof(int);
		    
		if(len == -1)
		{
		    /* this is a nil string */
		    /* skip the null pointer and make up for it 
		     * by adjusting the offset */
		    codep++;
		}
		else
		{
		    /*write string*/
		    if(write(fd,bc->data[codep++].str,len) == -1)
			return -1;
		    
		    ret = align_string(fd, len);
		    if(ret == -1) return -1;
		    
		    filelen += len + ret;
		}
		
	    }
	    /* Days*/
	    if(write_int(fd,bc->data[codep].value) == -1)
		return -1;
	    codep++;
	    filelen += sizeof(int);
            /*Mime */
	    if(write_int(fd,bc->data[codep].value) == -1)
		return -1;
	    codep++;
	    filelen += sizeof(int);
	    
	    break;
	case B_NULL:
	case B_STOP:
	case B_DISCARD:
	case B_KEEP:
	case B_MARK:
	case B_UNMARK:
	    /* No Parameters! */
	    break;

	default:
	    /* Unknown opcode? */
	    return -1;
	}
    }
    return filelen - start_filelen;
}

/* spew the bytecode to disk */
int sieve_emit_bytecode(int fd, bytecode_info_t *bc)  
{
    /* First output version number (4 bytes) */
    int data = BYTECODE_VERSION;

    /*this is a string, so it is happy*/
    if(write(fd, BYTECODE_MAGIC, BYTECODE_MAGIC_LEN) == -1)
	return -1;

    if(write_int(fd, data) == -1) return -1;

#if DUMPCODE
    dump(bc);
#endif

    /*the sizeof(int) is to account for the version # at the begining*/
    return bc_action_emit(fd, 0, bc->scriptend, bc, sizeof(int) + BYTECODE_MAGIC_LEN);
}