File: boundary-stack.c

package info (click to toggle)
altermime 0.3.10-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 920 kB
  • sloc: ansic: 11,103; makefile: 75; sh: 15
file content (570 lines) | stat: -rw-r--r-- 13,955 bytes parent folder | download | duplicates (15)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <ctype.h>

#include "logger.h"
#include "pldstr.h"
#include "boundary-stack.h"

#ifndef FL
#define FL __FILE__,__LINE__
#endif

#define BS_STRLEN_MAX 1024
#define BS_BOUNDARY_DETECT_LIMIT_DEFAULT 4
#define DBS if (glb.debug)

struct BS_globals {
	int debug;
	int verbose;
	int syslogging;
	int errlogging;
	int count;
	int detect_limit;
	int hold_limit;
	int smallest_length;
	int have_empty_boundary;
	struct BS_node *boundarystack;
	char boundarystacksafe[BS_STRLEN_MAX];
};

struct BS_node {
	char *boundary;
	int boundary_length;
	int boundary_nhl;  // length of boundary without hyphens
	struct BS_node *next;
};


static struct BS_globals glb;





/*-----------------------------------------------------------------\
 Function Name	: BS_init
 Returns Type	: int
 	----Parameter List
	1. void , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int BS_init( void )
{
	glb.debug = 0;
	glb.verbose = 0;
	glb.syslogging = 1;
	glb.errlogging = 0;
	glb.count = 0;
	glb.detect_limit = BS_BOUNDARY_DETECT_LIMIT_DEFAULT;
	glb.hold_limit = 0;
	glb.boundarystack = NULL;
	glb.smallest_length = -1;
	glb.have_empty_boundary = 0;

	return 0;
}

/*-----------------------------------------------------------------\
 Function Name	: BS_set_hold_limit
 Returns Type	: int
 	----Parameter List
	1. int limit , how many boundary strings to hold
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int BS_set_hold_limit( int limit )
{
	glb.hold_limit = limit;

	return 0;
}

/*-----------------------------------------------------------------\
 Function Name	: BS_set_verbose
 Returns Type	: int
 	----Parameter List
	1. int level , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int BS_set_verbose( int level )
{
	glb.verbose = level;

	return glb.verbose;
}

/*-----------------------------------------------------------------\
 Function Name	: BS_set_debug
 Returns Type	: int
 	----Parameter List
	1. int level , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int BS_set_debug( int level )
{
	glb.debug = level;

	return glb.debug;
}

/*-----------------------------------------------------------------\
 Function Name	: BS_set_boundary_detect_limit
 Returns Type	: int
 	----Parameter List
	1. int limit , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int BS_set_boundary_detect_limit( int limit )
{
	if ((limit > 0)&&(limit < BS_STRLEN_MAX))
	{
		glb.detect_limit = limit;
	}

	return glb.detect_limit;
}


/*-----------------------------------------------------------------\
 Function Name	: BS_clear
 Returns Type	: int
 	----Parameter List
	1. void , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int BS_clear( void )
{
	struct BS_node *next;

	while (glb.boundarystack)
	{
		DBS LOGGER_log("%s:%d:BS_clear:DEBUG: Popping off %p",FL,glb.boundarystack);
		next = glb.boundarystack->next;
		free(glb.boundarystack->boundary);
		free(glb.boundarystack);
		glb.boundarystack = next;
	}

	glb.boundarystack = NULL;
	glb.count = 0;
	glb.smallest_length = -1;

	return 0;
}



/*-----------------------------------------------------------------\
 Function Name	: BS_non_hyphen_length
 Returns Type	: int
 	----Parameter List
	1. char *boundary , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int BS_non_hyphen_length( char *boundary )
{
	int count = 0;
	char *p = boundary;

	while (*p) { if (isalnum((int)*p)) count++; p++; };

	return count;
}

/*-----------------------------------------------------------------\
 Function Name	: BS_push
 Returns Type	: int
 	----Parameter List
	1. char *boundary , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int BS_push( char *boundary )
{

	struct BS_node *node;

	if ((glb.hold_limit > 0)&&(glb.count >= glb.hold_limit)) 
	{
		DBS LOGGER_log("%s:%d:BS_push:DEBUG: Number of boundaries to hold is at limit (limit=%d)",FL,glb.hold_limit);
		return 0;
	}
  
	node	= malloc(sizeof(struct BS_node));

	DBS LOGGER_log("%s:%d:BS_push:DEBUG: head = %p, nn = %p boundary = '%s'",FL, glb.boundarystack, node, boundary);

	if (node)
	{
		node->next = glb.boundarystack;
		glb.boundarystack = node;
		glb.boundarystack->boundary = strdup(boundary);
		glb.boundarystack->boundary_length = strlen(glb.boundarystack->boundary);
		if (glb.boundarystack->boundary_length == 0) glb.have_empty_boundary = 1;
		glb.boundarystack->boundary_nhl = BS_non_hyphen_length(boundary);
		glb.count++;

		// Set the smallest length
		if (glb.smallest_length == -1) glb.smallest_length = glb.boundarystack->boundary_length;
		else if (glb.boundarystack->boundary_length < glb.smallest_length) glb.smallest_length = glb.boundarystack->boundary_length;

		DBS LOGGER_log("%s:%d:DEBUGX: smallest = %d, NHL = %d,  boundary = '%s'",FL,glb.smallest_length, node->boundary_nhl, boundary);
	}
	else
	    {
		LOGGER_log("%s:%d:BS_push:ERROR: Cannot allocate memory for boundary stack PUSH, %s", FL, strerror(errno));
	}

	return 0;
}


/*-----------------------------------------------------------------\
 Function Name	: *BS_pop
 Returns Type	: char
 	----Parameter List
	1. void , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
char *BS_pop( void )
{

	struct BS_node *node = glb.boundarystack;

	if (glb.boundarystack)
	{
		glb.boundarystack = glb.boundarystack->next;
		PLD_strncpy(glb.boundarystacksafe,node->boundary, BS_STRLEN_MAX);
		free(node->boundary);
		free(node);
		glb.count--;
	}

	return glb.boundarystacksafe;
}

/*-----------------------------------------------------------------\
 Function Name	: *BS_top
 Returns Type	: char
 	----Parameter List
	1. void , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
char *BS_top( void )
{

	if (glb.boundarystack)
	{
		return glb.boundarystack->boundary;
	}
	else return NULL;
}

/*-----------------------------------------------------------------\
 Function Name	: BS_count
 Returns Type	: int
 	----Parameter List
	1. void , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int BS_count( void )
{
	return glb.count;
}


/*-----------------------------------------------------------------\
 Function Name	: BS_is_long_enough
 Returns Type	: int
 	----Parameter List
	1. int blen , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int BS_is_long_enough( int blen )
{
	if (glb.smallest_length == -1) return 0;
	if (blen >= glb.smallest_length) return 1;

	return 0;
}

/*-----------------------------------------------------------------\
 Function Name	: BS_boundary_detect
 Returns Type	: int
 	----Parameter List
	1. char *needle, 
	2.  char *haystack , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int BS_boundary_detect( char *haystack, char *needle, int needle_length )
{
	int result=1;
	int current_start = glb.detect_limit;
	char *haystack_start;

	if ((glb.have_empty_boundary == 1)&&(needle_length < 1))
	{
		result = strncmp(haystack,"--",2);
		DBS LOGGER_log("%s:%d:BS_boundary_detect:DEBUG: empty-boundary test, result = %d",FL, result);
		return result;

	}

	if ((needle_length < 1)&&(glb.have_empty_boundary == 0)) return 1;


	DBS LOGGER_log("%s:%d:BS_boundary_detect: needle='%s', length=%d, haystack='%s', shift-window=%d"
			,FL
			,needle
			,needle_length
			,haystack
			,current_start
			);

	haystack_start = haystack;
	while ((current_start-- > 0)&&(*haystack_start != '\0'))
	{
		DBS LOGGER_log("%s:%d:BS_boundary_detect:DEBUG: CMP '%s' to '%s'",FL, needle, haystack_start);
		if (strncmp( needle, haystack_start, needle_length )==0)
		{
			DBS LOGGER_log("%s:%d:BS_boundary_detect:DEBUG: Hit on compare",FL);
			result = 0;  
			break; 
		}
		haystack_start++;
	}

	return result;
}



/*-----------------------------------------------------------------\
 Function Name	: BS_cmp
 Returns Type	: int
 	----Parameter List
	1. char *boundary, the boundary we want to check to see if is in
								the stack
	2.  int len , the length of the boundary
 	------------------
 Exit Codes	: 1 == boundary found, 0 == no boundary found
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int BS_cmp( char *boundary, int len )
{

	char testspace[128]; // was 1024
	int testspacelen=127; // was 1023
	int spin=1;
	int nhl=0;
	struct BS_node *node=glb.boundarystack;
	struct BS_node *nodetmp=NULL, *nodedel=NULL;

	if ((!boundary)||(glb.count == 0)) return 0;
	//if ((glb.smallest_length > 0)&&(len < glb.smallest_length)) return 0;
	if (BS_is_long_enough(len) == 0) return 0;

	nhl = BS_non_hyphen_length(boundary);

	DBS LOGGER_log("%s:%d:BS_cmp:DEBUG: possible-boundary='%s', len=%d, smallest=%d, count=%d, NHL=%d"
			, FL
			, boundary
			, len
			, glb.smallest_length
			, glb.count
			, nhl
			);


	// Crop the incoming string to fit in our testspace length
	if (len > testspacelen) len = testspacelen;

	// Copy the potential boundary into our testspace
	snprintf(testspace, testspacelen, "%s", boundary);

	// First, search through the stack looking for a boundary that matches
	// our search criterion
	//
	// When we do find one, we will jump out of this WHILE loop by setting
	// 'spin' to 0.

	while((node)&&(spin))
	{
//		if (node->boundary_length <= len)
		if (node->boundary_nhl == nhl)
		{	
			DBS LOGGER_log("%s:%d:BS_cmp:DEBUG: Comparing '%s' to '%s'", FL, boundary, node->boundary);
			// * 20040903-08H57:PLD: Set boundary length comparison from > 0 to >= 0
			if ((node->boundary != NULL)&&(node->boundary_length >= 0))
			{
				if ((BS_boundary_detect(testspace, node->boundary, node->boundary_length))==0)
				{
					DBS LOGGER_log("%s:%d:BS_cmp:DEBUG: Boundary HIT",FL);
					spin = 0;
				}
			}
		}

		if (spin != 0) node = node->next;
	}

	// If we have a hit on the matching, then, according
	// to nested MIME rules, we must "remove" any previous
	// boundaries
	//
	// We know that we had a HIT in matching if spin == 0, because
	// in our previous code block that's what we set spin to if
	if(spin==0)
	{

		DBS LOGGER_log("%s:%d:BS_cmp:DEBUG: Boundary hit on '%s' == '%s'",FL, boundary,node->boundary);

		// If our "HIT" node is /NOT/ the one on the top of the
		// stack, then we need to pop off and deallocate the nodes
		// PRIOR/Above the hit node.
		//
		// ie, if "NODE" is not the top, then pop off until we
		// do get to the node

		if (node != glb.boundarystack)
		{
			nodetmp = glb.boundarystack;
			while ((nodetmp)&&(nodetmp != node))
			{
				// - Set the node to delete (nodedel) to the current temp
				// node (notetmp)
				// - Increment the nodetmp to the next node in the stack
				// - Free the node to delete (nodedel)

				nodedel = nodetmp;
				nodetmp = nodetmp->next;
				free(nodedel->boundary);
				free(nodedel);
			}
			glb.boundarystack = node;
		}
		return 1;

	}
	else
	{
		return 0;
	}

	return 0;
}