Pajdeg
0.2.2
Pajdeg
|
The stream filter interface. More...
Files | |
file | PDStreamFilter.h |
Data Structures | |
struct | PDStreamFilter |
Macros | |
#define | PDStreamFilterPrepare(f, buf_in, len_in, buf_out, len_out) |
Typedefs | |
typedef struct PDStreamFilter * | PDStreamFilterRef |
typedef PDInteger(* | PDStreamFilterFunc) (PDStreamFilterRef filter) |
typedef PDStreamFilterRef(* | PDStreamFilterPrcs) (PDStreamFilterRef filter) |
typedef PDStreamFilterRef(* | PDStreamDualFilterConstr) (PDBool inputEnd, PDDictionaryRef options) |
The stream filter interface.
Stream filters are used to read or write content that is filtered in some way, such as FlateDecode streams (compressed).
Filters can be registered globally using PDStreamFilterRegisterDualFilter() and be accessed from anywhere via PDStreamFilterObtain().
By default, if PD_SUPPORT_ZLIB is defined, the first call to pd_pdf_implementation_use() will permanently register two filters, "FlateDecode" and "Predictor".
Registered filters can be overridden by registering a filter with the same name as an already existing one – the most recently registered filter always takes precedence.
Filters can be chained together by linking them to each other via the nextFilter property.
Some filters will hook child filters up automatically if they are passed options that they recognize.
The FlateDecode (compression) filter will add a Predictor filter to itself, if initialized with options that include the key "Predictor".
This goes both ways:
Chained filters are transparent to the caller, in the sense that the first filter in the chain holds the output buffer and capacity for the whole filter chain.
Currently all filters are, but need not be, dual. This means all filters come in pairs complementing each other, so that if data is passed into one and through the other, presuming the options are identical for both, the data remains unmodified.
FlateDecode has the compression (inflate) and decompression (deflate) filters, and Predictor has the predict and unpredict filters.
A non-dual filter must return NULL for the unsupported method (input or output) in the dual filter construction callback. There is no such thing as a non dual filter construction callback.
All filters are, but again, need not be, inversible. This means the filter has the means to create a new filter that is its inverse, including the correct filter options.
Chained filters can also be inversed, if all filters in the chain can be inversed. A PNG_UP (6 col) predictor with a FlateDecode compressor attached to it is laid out as such
Setting decode
to the results of PDStreamFilterCreateInversionForFilter() for encode
produces
Passing plaintext data into encode
would produce predictor-filtered compressed data, and passing that data into decode
gives back the original plaintext data.
Using the inversion mechanism is strongly recommended when modifying filtered content via Pajdeg, rather than setting up two filter chains manually, as the inverter code evolves with the filters (while manual code stays the same).
struct PDStreamFilter |
The stream filter struct.
Data Fields | |
PDBool | initialized |
Determines if filter was set up or not. | |
PDBool | compatible |
If false after initialization, the filter initialized fine but encountered compatibility issues that could potentially cause problems. | |
PDBool | needsInput |
Whether the filter needs data. If true, its input buffer must not be modified. | |
PDBool | hasInput |
Whether the filter has pending input that, for capacity reasons, has not been added to the buffer yet. | |
PDBool | finished |
Whether the filter is finished. | |
PDBool | failing |
Whether the filter is failing to handle its input. | |
PDDictionaryRef | options |
Filter options. | |
void * | data |
User info object. | |
unsigned char * | bufIn |
Input buffer. | |
unsigned char * | bufOut |
Output buffer. | |
PDInteger | bufInAvailable |
Available data. | |
PDInteger | bufOutCapacity |
Output buffer capacity. | |
PDStreamFilterFunc | init |
Initialization function. Called once before first use. | |
PDStreamFilterFunc | done |
Deinitialization function. Called once after last use. | |
PDStreamFilterFunc | begin |
Processing function. Called any number of times, at most once per new input buffer. | |
PDStreamFilterFunc | proceed |
Proceed function. Called any number of times to request more output from last begin call. | |
PDStreamFilterPrcs | createInversion |
Inversion function. Returns, if possible, a filter chain that reverts the current filter. | |
PDStreamFilterRef | nextFilter |
The next filter that should receive the output end of this filter as its input, or NULL if no such requirement exists. | |
float | growthHint |
The growth hint is an indicator for how the filter expects the size of its resulting data to be relative to the unfiltered data. | |
unsigned char * | bufOutOwned |
Internal output buffer, that will be freed on destruction. This is used internally for chained filters. | |
PDInteger | bufOutOwnedCapacity |
Capacity of internal output buffer. | |
#define PDStreamFilterPrepare | ( | f, | |
buf_in, | |||
len_in, | |||
buf_out, | |||
len_out | |||
) |
Convenience macro for setting buffers and sizes.
typedef PDStreamFilterRef(* PDStreamDualFilterConstr) (PDBool inputEnd, PDDictionaryRef options) |
Dual filter construction signature.
If inputEnd is set, the reader variant is returned, otherwise the writer variant is returned.
typedef PDInteger(* PDStreamFilterFunc) (PDStreamFilterRef filter) |
Filter function signature.
typedef PDStreamFilterRef(* PDStreamFilterPrcs) (PDStreamFilterRef filter) |
Filter processing signature. Used for inverting filters currently.
typedef struct PDStreamFilter* PDStreamFilterRef |
The stream filter type.
PDStreamFilterRef PDStreamFilterAlloc | ( | void | ) |
Convert a PDScanner dictionary stack into a stream filter options stack.
dictStack | The dictionary stack to convert |
void PDStreamFilterAppendFilter | ( | PDStreamFilterRef | filter, |
PDStreamFilterRef | next | ||
) |
Append a filter to the filter, causing a chain.
The filter is appended to the end (i.e. it will run at the very last), if there are more than one filters connected already.
filter | The pre-filter. |
next | The filter to append. |
PDBool PDStreamFilterApply | ( | PDStreamFilterRef | filter, |
unsigned char * | src, | ||
unsigned char ** | dstPtr, | ||
PDInteger | len, | ||
PDInteger * | newlenPtr, | ||
PDInteger * | allocatedlenPtr | ||
) |
Apply a filter to the given buffer, creating a new buffer and size. This is a convenience method for applying a filter (chain) to some data and getting a newly allocated buffer containing the results back, along with the result size.
filter | The filter to apply |
src | The source buffer |
dstPtr | The destination buffer pointer |
len | The length of the source buffer content |
newlenPtr | The filtered content length pointer |
allocatedlenPtr | The resulting allocation size of the destination buffer (optional, use NULL if unneeded) |
PDInteger PDStreamFilterBegin | ( | PDStreamFilterRef | filter | ) |
Begin processing, meaning that the filter should take its input buffer as new content.
filter | The filter |
PDStreamFilterRef PDStreamFilterCreate | ( | PDStreamFilterFunc | init, |
PDStreamFilterFunc | done, | ||
PDStreamFilterFunc | begin, | ||
PDStreamFilterFunc | proceed, | ||
PDStreamFilterPrcs | createInversion, | ||
PDDictionaryRef | options | ||
) |
Set up a stream filter with given callbacks.
init | The initializer. Returns 0 on failure, some other value on success. |
done | The deinitializer. Returns 0 on failure, some other vaule on success. |
begin | The begin function; called once when new data is put into bufIn. Returns # of bytes stored into output buffer. |
proceed | The proceed function; called repeatedly after a begin call was made, until the filter returns 0 lengths. Returns # of bytes stored into output buffer. |
createInversion | The inversion function; calling this will produce a filter that inverts this filter. |
options | Options passed to the filter, in the form of a dictionary. |
PDStreamFilterRef PDStreamFilterCreateInversionForFilter | ( | PDStreamFilterRef | filter | ) |
Create the inversion of the given filter, so that invert(filter(data)) == data
filter | The filter to invert. |
PDBool PDStreamFilterDone | ( | PDStreamFilterRef | filter | ) |
Deinitialize a filter.
filter | The filter. |
PDBool PDStreamFilterInit | ( | PDStreamFilterRef | filter | ) |
Initialize a filter.
filter | The filter. |
PDStreamFilterRef PDStreamFilterObtain | ( | const char * | name, |
PDBool | inputEnd, | ||
PDDictionaryRef | options | ||
) |
Obtain a filter for given name and type, where the type is a boolean value for whether the filter should be a reader (i.e. decompress) or writer (i.e. compress).
name | The name of the filter. |
inputEnd | Whether the input end or output end should be returned. The input end is the reader part (decoder) and the output end is the writer part (encoder). |
options | Options passed to the filter, in the form of a simplified pd_stack dictionary (with keys and values and no complex objects). A scanner-produced pd_stack dictionary can be converted into a stream filter options dictionary via PDStreamFilterGenerateOptionsFromDictionaryStack(). |
PDInteger PDStreamFilterProceed | ( | PDStreamFilterRef | filter | ) |
Proceed with a filter, meaning that it should continue filtering its input, which must not have been altered. This is called if a filter's output hits the output buffer capacity when processing its current input.
filter | The filter |
void PDStreamFilterRegisterDualFilter | ( | const char * | name, |
PDStreamDualFilterConstr | constr | ||
) |
Register a dual filter with a given name.
name | The name of the filter |
constr | The dual filter construction function. |