1 /*
2  * pts_fax.h -- a compact CCITTFax compressor and uncompressor) interface
3  * compiled by pts@fazekas.hu at Sun Jul  7 19:51:42 CEST 2002
4  *
5  * For usage example, see fax_test.c.
6  *
7  * algorithm ripped from GNU Ghostscript, implementation and (C):
8  *
9 Copyright (C) 1993, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All
10 rights reserved.
11 
12 GNU Ghostscript is free software; you can redistribute it and/or
13 modify it under the terms of version 2 of the GNU General Public
14 License as published by the Free Software Foundation.
15 
16 GNU Ghostscript is distributed in the hope that it will be
17 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
18 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 General Public License for more details.
20 
21 You should have received a copy of the GNU General Public License
22 along with this program so you can know your rights and responsibilities.
23 It should be in a file named doc/COPYING. If not, write to the
24 Free Software Foundation, Inc., 59 Temple Place Suite 330, Boston, MA
25 02111-1307, USA.
26  */
27 
28 /*$Id: pts_fax.h,v 1.4 2005/07/20 21:15:08 pts Exp $ */
29 /* CCITTFax filter state definition */
30 /* Requires strimpl.h */
31 
32 #ifndef PTS_FAX_H
33 #  define PTS_FAX_H 1
34 
35 #ifdef __GNUC__
36 #ifndef __clang__
37 #pragma interface
38 #endif
39 #endif
40 
41 /* #include "scommon.h" */
42 /*$Id: pts_fax.h,v 1.4 2005/07/20 21:15:08 pts Exp $ */
43 /* Definitions common to stream clients and implementors */
44 
45 /* #ifndef scommon_DEFINED */
46 /* #  define scommon_DEFINED */
47 
48 /**** pts ****/
49 #if 0
50 #include "gsmemory.h"
51 #include "gsstype.h"		/* for extern_st */
52 #endif
53 
54 /**** pts ****/
55 #include "config2.h"
56 /* ^^^ should #define SIZEOF_INT
57  * ^^^ should #define _, and ___
58  * ^^^ should #define? USE_BULITIN_FAXD
59  * ^^^ should #define? USE_BULITIN_FAXE
60  * ^^^ should define const
61  */
62 #if 0 /* example for Linux i386 gcc 2.95: */
63   #define SIZEOF_INT 4
64   #if ((defined(__STDC__) || defined(__PROTOTYPES__)) && !defined(NO_PROTO)) || defined(__cplusplus)
65   # define _(args) args
66   # define OF(args) args
67   # define ___(arg2s,arg1s,argafter) arg2s /* Dat: no direct comma allowed in args :-( */
68   #else
69   # define _(args) ()
70   # define OF(args) ()
71   # define ___(arg2s,arg1s,argafter) arg1s argafter /* Dat: no direct comma allowed in args :-( */
72   #endif
73   #define USE_BUILTIN_FAXE 1
74   #define USE_BUILITN_FAXD 1
75 #endif
76 
77 typedef void (*gssss_memset_t)(void *s, int c, unsigned n); /* Imp: not `unsigned' len */
78 /** The function must not return out of memory. */
79 typedef void* (*gssss_xalloc_t)(unsigned len); /* Imp: not `unsigned' len */
80 typedef void (*gssss_free_t)(void *ptr);
81 typedef void (*gssss_memcpy_t)(void *dest, const void *src, unsigned len); /* Imp: not `unsigned' len */
82 
83 /*
84  * There are three major structures involved in the stream package.
85  *
86  * A stream is an "object" that owns a buffer, which it uses to implement
87  * unsigned char-oriented sequential access in a standard way, and a set of
88  * procedures that handle things like buffer refilling.  See stream.h
89  * for more information about streams.
90  */
91 #ifndef stream_DEFINED
92 #  define stream_DEFINED
93 typedef struct stream_s stream;
94 
95 #endif
96 /*
97  * A stream_state records the state specific to a given variety of stream.
98  * The buffer processing function of a stream maintains this state.
99  */
100 typedef struct stream_state_s stream_state;
101 
102 /*
103  * A stream_template provides the information needed to create a stream.
104  * The client must fill in any needed setup parameters in the appropriate
105  * variety of stream_state, and then call the initialization function
106  * provided by the template.  See strimpl.h for more information about
107  * stream_templates.
108  */
109 typedef struct stream_template_s stream_template;
110 
111 /*
112  * The stream package works with bytes, not chars.
113  * This is to ensure unsigned representation on all systems.
114  * A stream currently can only be read or written, not both.
115  * Note also that the read procedure returns an int, not a char or a unsigned char;
116  * we use negative values to indicate exceptional conditions.
117  * (We cast these values to int explicitly, because some compilers
118  * don't do this if the other arm of a conditional is a unsigned char.)
119  */
120 /* End of data */
121 #define PTSFAX_EOFC ((int)(-1))
122 /* Error */
123 #define PTSFAX_ERRC ((int)(-2))
124 /* Interrupt */
125 /* #define INTC ((int)(-3)) */
126 /****** INTC IS NOT USED YET ******/
127 /* Callout */
128 /* #define CALLC ((int)(-4)) */
129 #define max_stream_exception 4
130 /* The following hack is needed for initializing scan_char_array in iscan.c. */
131 #define stream_exception_repeat(x) x, x, x, x
132 
133 /*
134  * Define cursors for reading from or writing into a buffer.
135  * We lay them out this way so that we can alias
136  * the write pointer and the read limit.
137  */
138 typedef struct stream_cursor_read_s {
139     const unsigned char *ptr;
140     const unsigned char *limit;
141     unsigned char *_skip;
142 } stream_cursor_read;
143 typedef struct stream_cursor_write_s {
144     const unsigned char *_skip;
145     unsigned char *ptr;
146     unsigned char *limit;
147 } stream_cursor_write;
148 typedef union stream_cursor_s {
149     stream_cursor_read r;
150     stream_cursor_write w;
151 } stream_cursor;
152 
153 /*
154  * Define the prototype for the procedures known to both the generic
155  * stream code and the stream implementations.
156  */
157 
158 /* Initialize the stream state (after the client parameters are set). */
159 #define stream_proc_init(proc)\
160   int proc _((stream_state *))
161 
162 /* Process a buffer.  See strimpl.h for details. */
163 #define stream_proc_process(proc)\
164   int proc _((stream_state *, stream_cursor_read *,\
165     stream_cursor_write *, bool))
166 
167 /* Release the stream state when closing. */
168 #define stream_proc_release(proc)\
169   void proc _((stream_state *))
170 
171 /* Initialize the client parameters to default values. */
172 #define stream_proc_set_defaults(proc)\
173   void proc _((stream_state *))
174 
175 /* Reinitialize any internal stream state.  Note that this does not */
176 /* affect buffered data.  We declare this as returning an int so that */
177 /* it can be the same as the init procedure; however, reinit cannot fail. */
178 #define stream_proc_reinit(proc)\
179   int proc _((stream_state *))
180 
181 /* Report an error.  Note that this procedure is stored in the state, */
182 /* not in the main stream structure. */
183 #define stream_proc_report_error(proc)\
184   int proc _((stream_state *, const char *))
185 stream_proc_report_error(s_no_report_error);
186 
187 /*
188  * Some types of streams have the ability to read their parameters from
189  * a parameter list, and to write all (or only the non-default)
190  * parameters to a parameter list.  Since these are not virtual
191  * procedures for the stream (they operate on stream_state structures
192  * even if no actual stream has been created), we name them differently.
193  */
194 #define stream_state_proc_get_params(proc, state_type)\
195   int proc _((gs_param_list *plist, const state_type *ss, bool all))
196 #define stream_state_proc_put_params(proc, state_type)\
197   int proc _((gs_param_list *plist, state_type *ss))
198 
199 /*
200  * Define a generic stream state.  If a processing procedure has no
201  * state of its own, it can use stream_state; otherwise, it must
202  * create a "subclass".  There is a hack in stream.h to allow the stream
203  * itself to serve as the "state" of a couple of heavily used stream types.
204  *
205  * In order to simplify the structure descriptors for concrete streams,
206  * we require that the generic stream state not contain any pointers
207  * to garbage-collectable storage.
208  */
209 #define STREAM_MAX_ERROR_STRING 79
210 #define stream_state_common\
211 	const stream_template *template_; /**** pts C++ */\
212 	gssss_memset_t memset_;\
213 	gssss_xalloc_t xalloc_;\
214 	gssss_free_t   free_;\
215 	gssss_memcpy_t memcpy_;\
216 	/* gs_memory_t *memory; */ /**** pts ****/ \
217 	stream_proc_report_error((*report_error));\
218         int min_left; /* required bytes for lookahead */ \
219 	char error_string[STREAM_MAX_ERROR_STRING + 1]
220 struct stream_state_s {
221     stream_state_common;
222 };
223 
224 /* extern_st(st_stream_state); */ /**** pts ****/
225 #define public_st_stream_state() /* in stream.c */\
226   gs_public_st_simple(st_stream_state, stream_state, "stream_state")
227 
228 /**** pts ****/
229 /* original strimpl.h coming */
230 /*$Id: pts_fax.h,v 1.4 2005/07/20 21:15:08 pts Exp $ */
231 /* Definitions for stream implementors */
232 /* Requires stdio.h */
233 
234 /*
235  * The 'process' procedure does the real work of the stream.
236  * It must process as much input information (from pr->ptr + 1 through
237  * pr->limit) as it can, subject to space available for output
238  * (pw->ptr + 1 through pw->limit), updating pr->ptr and pw->ptr.
239  *
240  * The procedure return value must be one of:
241  *      PTSFAX_EOFC - an end-of-data pattern was detected in the input,
242  *        or no more input can be processed for some other reason (e.g.,
243  *        the stream was told only to read a certain amount of data).
244  *      PTSFAX_ERRC - a syntactic error was detected in the input.
245  *      0 - more input data is needed.
246  *      1 - more output space is needed.
247  * If the procedure returns PTSFAX_EOFC, it can assume it will never be called
248  * again for that stream.
249  *
250  * If the procedure is called with last = 1, this is an indication that
251  * no more input will ever be supplied (after the input in the current
252  * buffer defined by *pr); the procedure should produce as much output
253  * as possible, including an end-of-data marker if applicable.  In this
254  * case:
255  *      - If the procedure returns 1, it may be called again (also with
256  *        last = 1).
257  *      - If the procedure returns any other value other than 1, the
258  *        procedure will never be called again for that stream.
259  *      - If the procedure returns 0, this is taken as equivalent to
260  *        returning PTSFAX_EOFC.
261  *      - If the procedure returns PTSFAX_EOFC (or 0), the stream's end_status
262  *        is set to PTSFAX_EOFC, meaning no more writing is allowed.
263  *
264  * Note that these specifications do not distinguish input from output
265  * streams.  This is deliberate: The processing procedures should work
266  * regardless of which way they are oriented in a stream pipeline.
267  * (The PostScript language does take a position as whether any given
268  * filter may be used for input or output, but this occurs at a higher level.)
269  *
270  * The value returned by the process procedure of a stream whose data source
271  * or sink is external (i.e., not another stream) is interpreted slightly
272  * differently.  For an external data source, a return value of 0 means
273  * "no more input data are available now, but more might become available
274  * later."  For an external data sink, a return value of 1 means "there is
275  * no more room for output data now, but there might be room later."
276  *
277  * It appears that the Adobe specifications, read correctly, require that when
278  * the process procedure of a decoding filter has filled up the output
279  * buffer, it must still peek ahead in the input to determine whether or not
280  * the next thing in the input stream is EOD.  If the next thing is an EOD (or
281  * end-of-data, indicated by running out of input data with last = true), the
282  * process procedure must return PTSFAX_EOFC; if the next thing is definitely not
283  * an EOD, the process procedure must return 1 (output full) (without, of
284  * course, consuming the non-EOD datum); if the procedure cannot determine
285  * whether or not the next thing is an EOD, it must return 0 (need more input).
286  * Decoding filters that don't have EOD (for example, NullDecode) can use
287  * a simpler algorithm: if the output buffer is full, then if there is more
288  * input, return 1, otherwise return 0 (which is taken as PTSFAX_EOFC if last
289  * is true).  All this may seem a little awkward, but it is needed in order
290  * to have consistent behavior regardless of where buffer boundaries fall --
291  * in particular, if a buffer boundary falls just before an EOD.  It is
292  * actually quite easy to implement if the main loop of the process
293  * procedure tests for running out of input rather than for filling the
294  * output: with this structure, exhausting the input always returns 0,
295  * and discovering that the output buffer is full when attempting to store
296  * more output always returns 1.
297  *
298  * Even this algorithm for handling end-of-buffer is not sufficient if an
299  * EOD falls just after a buffer boundary, but the generic stream code
300  * handles this case: the process procedures need only do what was just
301  * described.
302  */
303 
304 /*
305  * The set_defaults procedure in the template has a dual purpose: it sets
306  * default values for all parameters that the client can set before calling
307  * the init procedure, and it also must initialize all pointers in the
308  * stream state to a value that will be valid for the garbage collector
309  * (normally 0).  The latter implies that:
310  *
311  *	Any stream whose state includes additional pointers (beyond those
312  *	in stream_state_common) must have a set_defaults procedure.
313  */
314 
315 /*
316  * Note that all decoding filters that require an explicit EOD in the
317  * source data must have an init procedure that sets min_left = 1.
318  * This effectively provides a 1-unsigned char lookahead in the source data,
319  * which is required so that the stream can close itself "after reading
320  * the last unsigned char of data" (per Adobe specification), as noted above.
321  */
322 
323 /*
324  * Define a template for creating a stream.
325  *
326  * The meaning of min_in_size and min_out_size is the following:
327  * If the amount of input information is at least min_in_size,
328  * and the available output space is at least min_out_size,
329  * the process procedure guarantees that it will make some progress.
330  * (It may make progress even if this condition is not met, but this is
331  * not guaranteed.)
332  */
333 struct stream_template_s {
334 
335     /* Define the structure type for the stream state. */
336     /* gs_memory_type_ptr_t stype; */ /**** pts ****/
337 
338     /* Define an optional initialization procedure. */
339     stream_proc_init((*init));
340 
341     /* Define the processing procedure. */
342     /* (The init procedure can reset other procs if it wants.) */
343     stream_proc_process((*process));
344 
345     /* Define the minimum buffer sizes. */
346     unsigned int min_in_size;		/* minimum size for process input */
347     unsigned int min_out_size;		/* minimum size for process output */
348 
349     /* Define an optional releasing procedure. */
350     stream_proc_release((*release));
351 
352     /* Define an optional parameter defaulting and pointer initialization */
353     /* procedure. */
354     stream_proc_set_defaults((*set_defaults));
355 
356     /* Define an optional reinitialization procedure. */
357     stream_proc_reinit((*reinit));
358 
359 };
360 
361 
362 #if 0
363 /* Hex decoding utility procedure */
364 typedef enum {
365     hex_ignore_garbage = 0,
366     hex_ignore_whitespace = 1,
367     hex_ignore_leading_whitespace = 2
368 } hex_syntax;
369 int s_hex_process _((stream_cursor_read *, stream_cursor_write *, int *, hex_syntax));	/* in sstring.c */
370 #endif
371 /* end of former strimpl.h */
372 /* #endif */ /* scommon_INCLUDED */
373 /* end of former scommon.h */
374 
375 
376 
377 /* ------ Common state ------ */
378 
379 /*
380  * Define the common stream state for Huffman-coded filters.
381  * Invariants when writing:
382  *      0 <= bits_left <= hc_bits_size;
383  *      Only the leftmost (hc_bits_size - bits_left) bits of bits
384  *        contain valid data.
385  */
386 #define stream_hc_state_common\
387 	stream_state_common;\
388 		/* The client sets the following before initialization. */\
389 	bool FirstBitLowOrder;\
390 		/* The following are updated dynamically. */\
391 	unsigned int bits;		/* most recent bits of input or */\
392 				/* current bits of output */\
393 	int bits_left		/* # of valid low bits (input) or */\
394 				/* unused low bits (output) in above, */\
395 				/* 0 <= bits_left <= 7 */
396 typedef struct stream_hc_state_s {
397     stream_hc_state_common;
398 } stream_hc_state;
399 
400 /* Common state */
401 #define stream_CF_state_common\
402 	stream_hc_state_common;\
403 		/* The client sets the following before initialization. */\
404 	bool Uncompressed;\
405 	int K;\
406 	bool EndOfLine;\
407 	bool EncodedByteAlign;\
408 	int Columns;\
409 	int Rows;\
410 	bool EndOfBlock;\
411 	bool BlackIs1;\
412 	int DamagedRowsBeforeError;	/* (Decode only) */\
413 	/*bool FirstBitLowOrder;*/	/* in stream_hc_state_common */\
414 	int DecodedByteAlign;\
415 		/* The init procedure sets the following. */\
416 	unsigned int raster;\
417 	unsigned char *lbuf;		/* current scan line buffer */\
418 				/* (only if decoding or 2-D encoding) */\
419 	unsigned char *lprev;		/* previous scan line buffer (only if 2-D) */\
420 		/* The following are updated dynamically. */\
421 	int k_left		/* number of next rows to encode in 2-D */\
422 				/* (only if K > 0) */
423 typedef struct stream_CF_state_s {
424     stream_CF_state_common;
425 } stream_CF_state;
426 
427 /* Define common default parameter setting. */
428 #define s_CF_set_defaults_inline(ss)\
429   ((ss)->Uncompressed = false,\
430    (ss)->K = 0,\
431    (ss)->EndOfLine = false,\
432    (ss)->EncodedByteAlign = false,\
433    (ss)->Columns = 1728,\
434    (ss)->Rows = 0,\
435    (ss)->EndOfBlock = true,\
436    (ss)->BlackIs1 = false,\
437 		/* Added by Adobe since the Red Book */\
438    (ss)->DamagedRowsBeforeError = 0, /* always set, for s_CF_get_params */\
439    (ss)->FirstBitLowOrder = false,\
440 		/* Added by us */\
441    (ss)->DecodedByteAlign = 1,\
442 	/* Clear pointers */\
443    (ss)->lbuf = 0, (ss)->lprev = 0)
444 
445 /* CCITTFaxEncode */
446 typedef struct stream_CFE_state_s {
447     stream_CF_state_common;
448     /* The init procedure sets the following. */
449     int max_code_bytes;		/* max # of bytes for an encoded line */
450     unsigned char *lcode;		/* buffer for encoded output line */
451     /* The following change dynamically. */
452     int read_count;		/* # of bytes to copy into lbuf */
453     int write_count;		/* # of bytes to copy out of lcode */
454     int code_bytes;		/* # of occupied bytes in lcode */
455 } stream_CFE_state;
456 
457 #define private_st_CFE_state()	/* in scfe.c */\
458   gs_private_st_ptrs3(st_CFE_state, stream_CFE_state, "CCITTFaxEncode state",\
459     cfe_enum_ptrs, cfe_reloc_ptrs, lbuf, lprev, lcode)
460 #define s_CFE_set_defaults_inline(ss)\
461   (s_CF_set_defaults_inline(ss), (ss)->lcode = 0)
462 #if USE_BUILTIN_FAXE
463 /**** pts ****/
464 #if SIZEOF_INT > 2
465 #  define cfe_max_width (2560 * 32000 * 2 / 3)
466 #else
467 #  define cfe_max_width ((int)((unsigned)-1/2 - 40))	/* avoid overflows */
468 #endif
469 #ifdef __cplusplus
470 extern "C"
471 #else
472 extern
473 #endif
474 const stream_template s_CFE_template;
475 #endif
476 
477 /* CCITTFaxDecode */
478 typedef struct stream_CFD_state_s {
479     stream_CF_state_common;
480     int cbit;			/* bits left to fill in current decoded */
481     /* unsigned char at lbuf[wpos] (0..7) */
482     int rows_left;		/* number of rows left */
483     int rpos;			/* rptr for copying lbuf to client */
484     int wpos;			/* rlimit/wptr for filling lbuf or */
485     /* copying to client */
486     int eol_count;		/* number of EOLs seen so far */
487     unsigned char invert;		/* current value of 'white' */
488     /* for 2-D decoding */
489     int run_color;		/* -1 if processing white run, */
490     /* 0 if between runs but white is next, */
491     /* 1 if between runs and black is next, */
492     /* 2 if processing black run */
493     int damaged_rows;		/* # of consecutive damaged rows preceding */
494     /* the current row */
495     bool skipping_damage;	/* true if skipping a damaged row looking */
496     /* for EOL */
497     /* The following are not used yet. */
498     int uncomp_run;		/* non-0 iff we are in an uncompressed */
499     /* run straddling a scan line (-1 if white, */
500     /* 1 if black) */
501     int uncomp_left;		/* # of bits left in the run */
502     int uncomp_exit;		/* non-0 iff this is an exit run */
503     /* (-1 if next run white, 1 if black) */
504 } stream_CFD_state;
505 
506 #define private_st_CFD_state()	/* in scfd.c */\
507   gs_private_st_ptrs2(st_CFD_state, stream_CFD_state, "CCITTFaxDecode state",\
508     cfd_enum_ptrs, cfd_reloc_ptrs, lbuf, lprev)
509 #define s_CFD_set_defaults_inline(ss)\
510   s_CF_set_defaults_inline(ss)
511 #if USE_BUILTIN_FAXD
512 extern const stream_template s_CFD_template;
513 #endif
514 
515 #endif /* pts_fax.h */
516