1 /* Copyright (C) 2001-2012 Artifex Software, Inc. 2 All Rights Reserved. 3 4 This software is provided AS-IS with no warranty, either express or 5 implied. 6 7 This software is distributed under license and may not be copied, 8 modified or distributed except as expressly authorized under the terms 9 of the license contained in the file LICENSE in this distribution. 10 11 Refer to licensing information at http://www.artifex.com or contact 12 Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134, San Rafael, 13 CA 94903, U.S.A., +1(415)492-9861, for further information. 14 */ 15 16 17 /* Definitions for stream implementors */ 18 /* Requires stdio.h */ 19 20 #ifndef strimpl_INCLUDED 21 # define strimpl_INCLUDED 22 23 #include "scommon.h" 24 #include "gstypes.h" /* for gsstruct.h */ 25 #include "gsstruct.h" 26 27 /* 28 * The 'process' procedure does the real work of the stream. 29 * It must process as much input information (from pr->ptr + 1 through 30 * pr->limit) as it can, subject to space available for output 31 * (pw->ptr + 1 through pw->limit), updating pr->ptr and pw->ptr. 32 * 33 * The procedure return value must be one of: 34 * EOFC - an end-of-data pattern was detected in the input, 35 * or no more input can be processed for some other reason (e.g., 36 * the stream was told only to read a certain amount of data). 37 * ERRC - a syntactic error was detected in the input. 38 * 0 - more input data is needed. 39 * 1 - more output space is needed. 40 * If the procedure returns EOFC, it can assume it will never be called 41 * again for that stream. 42 * 43 * If the procedure is called with last = 1, this is an indication that 44 * no more input will ever be supplied (after the input in the current 45 * buffer defined by *pr); the procedure should produce as much output 46 * as possible, including an end-of-data marker if applicable. In this 47 * case: 48 * - If the procedure returns 1, it may be called again (also with 49 * last = 1). 50 * - If the procedure returns any other value other than 1, the 51 * procedure will never be called again for that stream. 52 * - If the procedure returns 0, this is taken as equivalent to 53 * returning EOFC. 54 * - If the procedure returns EOFC (or 0), the stream's end_status 55 * is set to EOFC, meaning no more writing is allowed. 56 * 57 * Note that these specifications do not distinguish input from output 58 * streams. This is deliberate: The processing procedures should work 59 * regardless of which way they are oriented in a stream pipeline. 60 * (The PostScript language does take a position as whether any given 61 * filter may be used for input or output, but this occurs at a higher level.) 62 * 63 * The value returned by the process procedure of a stream whose data source 64 * or sink is external (i.e., not another stream) is interpreted slightly 65 * differently. For an external data source, a return value of 0 means 66 * "no more input data are available now, but more might become available 67 * later." For an external data sink, a return value of 1 means "there is 68 * no more room for output data now, but there might be room later." 69 * 70 * It appears that the Adobe specifications, read correctly, require that when 71 * the process procedure of a decoding filter has filled up the output 72 * buffer, it must still peek ahead in the input to determine whether or not 73 * the next thing in the input stream is EOD. If the next thing is an EOD (or 74 * end-of-data, indicated by running out of input data with last = true), the 75 * process procedure must return EOFC; if the next thing is definitely not 76 * an EOD, the process procedure must return 1 (output full) (without, of 77 * course, consuming the non-EOD datum); if the procedure cannot determine 78 * whether or not the next thing is an EOD, it must return 0 (need more input). 79 * Decoding filters that don't have EOD (for example, NullDecode) can use 80 * a simpler algorithm: if the output buffer is full, then if there is more 81 * input, return 1, otherwise return 0 (which is taken as EOFC if last 82 * is true). All this may seem a little awkward, but it is needed in order 83 * to have consistent behavior regardless of where buffer boundaries fall -- 84 * in particular, if a buffer boundary falls just before an EOD. It is 85 * actually quite easy to implement if the main loop of the process 86 * procedure tests for running out of input rather than for filling the 87 * output: with this structure, exhausting the input always returns 0, 88 * and discovering that the output buffer is full when attempting to store 89 * more output always returns 1. 90 * 91 * Even this algorithm for handling end-of-buffer is not sufficient if an 92 * EOD falls just after a buffer boundary, but the generic stream code 93 * handles this case: the process procedures need only do what was just 94 * described. 95 */ 96 97 /* 98 * The set_defaults procedure in the template has a dual purpose: it sets 99 * default values for all parameters that the client can set before calling 100 * the init procedure, and it also must initialize all pointers in the 101 * stream state to a value that will be valid for the garbage collector 102 * (normally 0). The latter implies that: 103 * 104 * Any stream whose state includes additional pointers (beyond those 105 * in stream_state_common) must have a set_defaults procedure. 106 */ 107 108 /* 109 * Note that all decoding filters that require an explicit EOD in the 110 * source data must have an init procedure that sets min_left = 1. 111 * This effectively provides a 1-byte lookahead in the source data, 112 * which is required so that the stream can close itself "after reading 113 * the last byte of data" (per Adobe specification), as noted above. 114 */ 115 116 /* 117 * Define a template for creating a stream. 118 * 119 * The meaning of min_in_size and min_out_size is the following: 120 * If the amount of input information is at least min_in_size, 121 * and the available output space is at least min_out_size, 122 * the process procedure guarantees that it will make some progress. 123 * (It may make progress even if this condition is not met, but this is 124 * not guaranteed.) 125 */ 126 struct stream_template_s { 127 128 /* Define the structure type for the stream state. */ 129 gs_memory_type_ptr_t stype; 130 131 /* Define an optional initialization procedure. */ 132 stream_proc_init((*init)); 133 134 /* Define the processing procedure. */ 135 /* (The init procedure can reset other procs if it wants.) */ 136 stream_proc_process((*process)); 137 138 /* Define the minimum buffer sizes. */ 139 uint min_in_size; /* minimum size for process input */ 140 uint min_out_size; /* minimum size for process output */ 141 142 /* Define an optional releasing procedure. */ 143 stream_proc_release((*release)); 144 145 /* Define an optional parameter defaulting and pointer initialization */ 146 /* procedure. */ 147 stream_proc_set_defaults((*set_defaults)); 148 149 /* Define an optional reinitialization procedure. */ 150 stream_proc_reinit((*reinit)); 151 152 }; 153 154 /* Utility procedures */ 155 int stream_move(stream_cursor_read *, stream_cursor_write *); /* in stream.c */ 156 157 /* Hex decoding utility procedure */ 158 typedef enum { 159 hex_ignore_garbage = 0, 160 hex_ignore_whitespace = 1, 161 hex_ignore_leading_whitespace = 2, 162 hex_break_on_whitespace = 3 163 } hex_syntax; 164 int s_hex_process(stream_cursor_read *, stream_cursor_write *, int *, hex_syntax); /* in sstring.c */ 165 166 #endif /* strimpl_INCLUDED */ 167