File: sfilter1.c

package info (click to toggle)
gs 3.33-7
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 7,436 kB
  • ctags: 15,511
  • sloc: ansic: 92,150; asm: 684; sh: 486; makefile: 91
file content (430 lines) | stat: -rw-r--r-- 9,148 bytes parent folder | download
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
/* Copyright (C) 1993, 1995 Aladdin Enterprises.  All rights reserved.
  
  This file is part of GNU Ghostscript.
  
  GNU Ghostscript is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  anyone for the consequences of using it or for whether it serves any
  particular purpose or works at all, unless he says so in writing.  Refer
  to the GNU Ghostscript General Public License for full details.
  
*/

/* sfilter1.c */
/* Filters included in Level 1 systems: NullEncode/Decode, PFBDecode, */
/*   SubFileDecode, RunLengthEncode/Decode. */
#include "stdio_.h"		/* includes std.h */
#include "memory_.h"
#include "strimpl.h"
#include "sfilter.h"
#include "srlx.h"

/* ------ NullEncode/Decode ------ */

/* Process a buffer */
private int
s_Null_process(stream_state *st, stream_cursor_read *pr,
  stream_cursor_write *pw, bool last)
{	return stream_move(pr, pw);
}

/* Stream template */
const stream_template s_Null_template =
{	&st_stream_state, NULL, s_Null_process, 1, 1
};

/* ------ PFBDecode ------ */

private_st_PFBD_state();

#define ss ((stream_PFBD_state *)st)

/* Initialize the state */
private int
s_PFBD_init(stream_state *st)
{	ss->record_type = -1;
	return 0;
}

/* Process a buffer */
private int
s_PFBD_process(stream_state *st, stream_cursor_read *pr,
  stream_cursor_write *pw, bool last)
{	register const byte *p = pr->ptr;
	register byte *q = pw->ptr;
	int rcount, wcount;
	int c;
	int status = 0;
top:	rcount = pr->limit - p;
	wcount = pw->limit - q;
	switch ( ss->record_type )
	{
	case -1:			/* new record */
		if ( rcount < 2 )
			goto out;
		if ( p[1] != 0x80 )
			goto err;
		c = p[2];
		switch ( c )
		{
		case 1: case 2:
			break;
		case 3:
			status = EOFC;
			p += 2;
			goto out;
		default:
			p += 2;
			goto err;
		}
		if ( rcount < 6 )
			goto out;
		ss->record_type = c;
		ss->record_left = p[3] + ((uint)p[4] << 8) +
					((ulong)p[5] << 16) +
					((ulong)p[6] << 24);
		p += 6;
		goto top;
	case 1:				/* text data */
		/* Translate \r to \n. */
	{	int count = (wcount < rcount ? (status = 1, wcount) : rcount);
		if ( count > ss->record_left )
			count = ss->record_left,
			status = 0;
		ss->record_left -= count;
		for ( ; count != 0; count-- )
		{	c = *++p;
			*++q = (c == '\r' ? '\n' : c);
		}
	}	break;
	case 2:				/* binary data */
		if ( ss->binary_to_hex )
		{	/* Translate binary to hex. */
			int count;
			register const char _ds *hex_digits =
							"0123456789abcdef";
			wcount >>= 1;		/* 2 chars per input byte */
			count = (wcount < rcount ? (status = 1, wcount) : rcount);
			if ( count > ss->record_left )
				count = ss->record_left,
				status = 0;
			ss->record_left -= count;
			for ( ; count != 0; count-- )
			{	c = *++p;
				q[1] = hex_digits[c >> 4];
				q[2] = hex_digits[c & 0xf];
				q += 2;
			}
		}
		else
		{	/* Just read binary data. */
			int count = (wcount < rcount ? (status = 1, wcount) : rcount);
			if ( count > ss->record_left )
				count = ss->record_left,
				status = 0;
			ss->record_left -= count;
			memcpy(q + 1, p + 1, count);
			p += count;
			q += count;
		}
		break;
	   }
	if ( ss->record_left == 0 )
	{	ss->record_type = -1;
		goto top;
	}
out:	pr->ptr = p;
	pw->ptr = q;
	return status;
err:	pr->ptr = p;
	pw->ptr = q;
	return ERRC;
}

#undef ss

/* Stream template */
const stream_template s_PFBD_template =
{	&st_PFBD_state, s_PFBD_init, s_PFBD_process, 6, 2
};

/* ------ RunLengthEncode ------ */

private_st_RLE_state();

#define ss ((stream_RLE_state *)st)

/* Initialize */
private int
s_RLE_init(stream_state *st)
{	return s_RLE_init_inline(ss);
}

/* Process a buffer */
private int
s_RLE_process(stream_state *st, stream_cursor_read *pr,
  stream_cursor_write *pw, bool last)
{	register const byte *p = pr->ptr;
	register byte *q = pw->ptr;
	const byte *rlimit = pr->limit;
	byte *wlimit = pw->limit;
	int status = 0;
	ulong rleft = ss->record_left;
	while ( p < rlimit )
	{	const byte *beg = p;
		const byte *p1;
		uint count = rlimit - p;
		byte next;
		if ( count > rleft )
			count = rleft;
		if ( count > 127 )
			count = 127;
		p1 = p + count;
		if ( count > 2 && (next = p[1]) == p[2] && next == p[3] )
		{	if ( wlimit - q < 2 )
			{	status = 1;
				break;
			}
			/* Recognize leading repeated byte */
			p1--;
			do { p++; }
			while ( p < p1 && p[2] == next );
			p++;
			*++q = (byte)(257 - (p - beg));
			*++q = next;
		}
		else
		{	p1 -= 2;
			while ( p < p1 && (p[2] != p[1] || p[3] != p[1]) )
				p++;
			if ( p >= p1 )
				p = p1 + 2;
			count = p - beg;
			if ( wlimit - q < count + 1 )
			{	p = beg;
				status = 1;
				break;
			}
			*++q = count - 1;
			memcpy(q + 1, beg + 1, count);
			q += count;
		}
		rleft -= p - beg;
		if ( rleft == 0 )
			rleft = ss->record_size;
	}
	if ( last && status == 0 && ss->EndOfData )
	  {	if ( q < wlimit )
		  *++q = 128;
		else
		  status = 1;
	  }
	pr->ptr = p;
	pw->ptr = q;
	ss->record_left = rleft;
	return status;
}

#undef ss

/* Stream template */
const stream_template s_RLE_template =
{	&st_RLE_state, s_RLE_init, s_RLE_process, 128, 129
};

/* ------ RunLengthDecode ------ */

private_st_RLD_state();

#define ss ((stream_RLD_state *)st)

/* Refill the buffer */
private int
s_RLD_process(stream_state *st, stream_cursor_read *pr,
  stream_cursor_write *pw, bool last)
{	register const byte *p = pr->ptr;
	register byte *q = pw->ptr;
	const byte *rlimit = pr->limit;
	byte *wlimit = pw->limit;
	int status = 0;

	while ( p < rlimit )
	{	int b = *++p;
		if ( b < 128 )
		{	if ( b >= rlimit - p )
			{	p--;
				break;
			}
			else if ( b >= wlimit - q )
			{	p--;
				status = 1;
				break;
			}
			memcpy(q + 1, p + 1, ++b);
			p += b;
			q += b;
		}
		else if ( b == 128 )	/* end of data */
		{	if ( ss->EndOfData )
			  {	status = EOFC;
				break;
			  }
		}
		else if ( p == rlimit )
		{	p--;
			break;
		}
		else if ( (b = 257 - b) > wlimit - q )
		{	p--;
			status = 1;
			break;		/* won't fit */
		}
		else
		{	memset(q + 1, *++p, b);
			q += b;
		}
	}
	pr->ptr = p;
	pw->ptr = q;
	return status;
}

#undef ss

/* Stream template */
const stream_template s_RLD_template =
{	&st_RLD_state, NULL, s_RLD_process, 129, 128
};

/* ------ SubFileDecode ------ */

private_st_SFD_state();
/* GC procedures */
private ENUM_PTRS_BEGIN(sfd_enum_ptrs) return 0;
	ENUM_CONST_STRING_PTR(0, stream_SFD_state, eod);
ENUM_PTRS_END
private RELOC_PTRS_BEGIN(sfd_reloc_ptrs) ;
	RELOC_CONST_STRING_PTR(stream_SFD_state, eod);
RELOC_PTRS_END

#define ss ((stream_SFD_state *)st)

/* Initialize the stream */
private int
s_SFD_init(stream_state *st)
{	ss->match = 0;
	ss->copy_count = 0;
	return 0;
}

/* Refill the buffer */
private int
s_SFD_process(stream_state *st, stream_cursor_read *pr,
  stream_cursor_write *pw, bool last)
{	register const byte *p = pr->ptr;
	register byte *q = pw->ptr;
	const byte *rlimit = pr->limit;
	byte *wlimit = pw->limit;
	int status = 0;
	if ( ss->eod.size == 0 )
	{	/* Just read, with no EOD pattern. */
		int rcount = rlimit - p;
		int wcount = wlimit - q;
		int count = min(rcount, wcount);
		if ( ss->count == 0 )		/* no EOD limit */
			return stream_move(pr, pw);
		else if ( ss->count > count )	/* not EOD yet */
		{	ss->count -= count;
			return stream_move(pr, pw);
		}
		else
		{	/* We're going to reach EOD. */
			count = ss->count;
			memcpy(q + 1, p + 1, count);
			pr->ptr = p + count;
			pw->ptr = q + count;
			return EOFC;
		}
	}
	else
	{	/* Read looking for an EOD pattern. */
		const byte *pattern = ss->eod.data;
		uint match = ss->match;
cp:		/* Check whether we're still copying a partial match. */
		if ( ss->copy_count )
		{	int count = min(wlimit - q, ss->copy_count);
			memcpy(q + 1, ss->eod.data + ss->copy_ptr, count);
			ss->copy_count -= count;
			ss->copy_ptr += count;
			q += count;
			if ( ss->copy_count != 0 )	/* hit wlimit */
			{	status = 1;
				goto xit;
			}
			else if ( ss->count < 0 )
			{	status = EOFC;
				goto xit;
			}
		}
		while ( p < rlimit )
		{	int c = *++p;
			if ( c == pattern[match] )
			{	if ( ++match == ss->eod.size )
				{	switch ( ss->count )
					{
					case 0:
						status = EOFC;
						goto xit;
					case 1:
						ss->count = -1;
						break;
					default:
						ss->count--;
					}
					ss->copy_ptr = 0;
					ss->copy_count = match;
					match = 0;
					goto cp;
				}
				continue;
			}
			/* No match here, back up to find the longest one. */
			/* This may be quadratic in string_size, but */
			/* we don't expect this to be a real problem. */
			if ( match > 0 )
			  {	int end = match;
				while ( match > 0 )
				  {	match--;
					if ( !memcmp(pattern,
						     pattern + end - match,
						     match)
					   )
					  break;
				  }
				/* Copy the unmatched initial portion of */
				/* the EOD string to the output. */
				p--;
				ss->copy_ptr = 0;
				ss->copy_count = end - match;
				goto cp;
			  }
			if ( q == wlimit )
			{	p--;
				status = 1;
				break;
			}
			*++q = c;
		}
xit:		pr->ptr = p;
		pw->ptr = q;
		ss->match = match;
	}
	return status;
}

#undef ss

/* Stream template */
const stream_template s_SFD_template =
{	&st_SFD_state, s_SFD_init, s_SFD_process, 1, 1
};