summaryrefslogtreecommitdiff
path: root/support/dktools/dk4strmco.ctr
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2020-09-20 03:03:26 +0000
committerNorbert Preining <norbert@preining.info>2020-09-20 03:03:26 +0000
commit1f457376b478257b88d4a857f5ec1b6155442dd7 (patch)
tree2a06a60551dea362cf8cb0cb0ba66c78608717c4 /support/dktools/dk4strmco.ctr
parentac690ca29ad5bf8a5203a65fd6252f7b564f4727 (diff)
CTAN sync 202009200303
Diffstat (limited to 'support/dktools/dk4strmco.ctr')
-rw-r--r--support/dktools/dk4strmco.ctr308
1 files changed, 196 insertions, 112 deletions
diff --git a/support/dktools/dk4strmco.ctr b/support/dktools/dk4strmco.ctr
index 6bd073c309..57b1f36ff7 100644
--- a/support/dktools/dk4strmco.ctr
+++ b/support/dktools/dk4strmco.ctr
@@ -2,7 +2,7 @@
copyright owner = Dirk Krause
copyright year = 2016-xxxx
-license = bsd
+SPDX-License-Identifier: BSD-3-Clause
%% header
@@ -32,15 +32,27 @@ stream.
*/
#ifndef DK4CONF_H_INCLUDED
+#if DK4_BUILDING_DKTOOLS4
#include "dk4conf.h"
+#else
+#include <dktools-4/dk4conf.h>
+#endif
#endif
#ifndef DK4ERROR_H_INCLUDED
+#if DK4_BUILDING_DKTOOLS4
#include "dk4error.h"
+#else
+#include <dktools-4/dk4error.h>
+#endif
#endif
#ifndef DK4STRM_H_INCLUDED
+#if DK4_BUILDING_DKTOOLS4
#include "dk4strm.h"
+#else
+#include <dktools-4/dk4strm.h>
+#endif
#endif
@@ -95,6 +107,15 @@ extern "C" {
@param fl Flags.
@param erp Error report, may be NULL.
@return Valid pointer to new stream on success, NULL on error.
+
+ Error codes:
+ - DK4_E_INVALID_ARGUMENTS<br>
+ if strm is NULL,
+ - DK4_E_MATH_OVERFLOW<br>
+ on numeric overflow when calculating the product of elsize and nelem,
+ - DK4_E_MEMORY_ALLOCATION_FAILED<br>
+ with mem.elsize and mem.nelem set if there is not enough memory
+ available.
*/
dk4_stream_t *
dk4strmco_open(
@@ -123,6 +144,13 @@ dk4strmco_open(
#endif
#endif
+#if DK4_HAVE_LIMITS_H
+#ifndef LIMITS_H_INCLUDED
+#include <limits.h>
+#define LIMITS_H_INCLUDED 1
+#endif
+#endif
+
#include "dk4rle.h"
#include "dk4a85e.h"
#include "dk4strmco.h"
@@ -130,6 +158,12 @@ dk4strmco_open(
#include "dk4memrs.h"
#include "dk4lzwe.h"
+#if DK4_HAVE_ASSERT_H
+#ifndef ASSERT_H_INCLUDED
+#include <assert.h>
+#define ASSERT_H_INCLUDED 1
+#endif
+#endif
$!trace-include
@@ -227,6 +261,9 @@ dk4strmco_nl(compressor_t *co)
{
int back = 0;
$? "+ dk4strmco_nl"
+#if DK4_USE_ASSERT
+ assert(NULL != co);
+#endif
if (0 != (DK4_STRMCO_FL_CR_NL & (co->fl))) {
back = dk4stream_write(co->dststrm, crnl, 2, NULL);
}
@@ -267,6 +304,11 @@ dk4strmco_co_oe(
unsigned us; /* Index in hex digits array. */
unsigned char uc; /* UC version of current character */
$? "+ dk4strmco_co_oe"
+#if DK4_USE_ASSERT
+ assert(NULL != co);
+ assert(NULL != b);
+ assert(0 < sz);
+#endif
switch (co->oe) {
case DK4_STRMCO_OE_ASCII85 : {
$? ". ASCII-85 encoding"
@@ -425,38 +467,48 @@ dk4strmco_co_co(
int back = 0; /* Function result. */
int res = 0; /* Result from various operations. */
$? "+ dk4strmco_co_co"
+#if DK4_USE_ASSERT
+ assert(NULL != co);
+ assert(NULL != b);
+ assert(0 < sz);
+#endif
switch (co->co) {
case DK4_STRMCO_CO_FLATE : {
$? ". flate compression"
- back = 1;
- co->u_co.zs.next_in = (unsigned char *)b;
- co->u_co.zs.avail_in = sz;
- do {
- co->u_co.zs.next_out = (unsigned char *)(&(co->zbuf[0]));
- co->u_co.zs.avail_out = DK4_STRMCO_ZBUF_SIZE;
- res = deflate(&(co->u_co.zs), Z_NO_FLUSH);
- switch (res) {
- case Z_OK : {
- used = DK4_STRMCO_ZBUF_SIZE - co->u_co.zs.avail_out;
- if (0 < used) {
- res = dk4strmco_co_oe(co,&(co->zbuf[0]),used,NULL);
- if (0 < res) {
- osz = sz - co->u_co.zs.avail_in;
+ if (((dk4_um_t)(UINT_MAX)) >= ((dk4_um_t)sz)) {
+ back = 1;
+ co->u_co.zs.next_in = (unsigned char *)b;
+ co->u_co.zs.avail_in = (unsigned int)sz;
+ do {
+ co->u_co.zs.next_out = (unsigned char *)(&(co->zbuf[0]));
+ co->u_co.zs.avail_out = DK4_STRMCO_ZBUF_SIZE;
+ res = deflate(&(co->u_co.zs), Z_NO_FLUSH);
+ switch (res) {
+ case Z_OK : {
+ used = DK4_STRMCO_ZBUF_SIZE - co->u_co.zs.avail_out;
+ if (0 < used) {
+ res = dk4strmco_co_oe(
+ co, &(co->zbuf[0]), used, NULL
+ );
+ if (0 < res) {
+ osz = sz - co->u_co.zs.avail_in;
+ }
+ else {
+ back = 0;
+ }
}
else {
- back = 0;
+ osz = sz - co->u_co.zs.avail_in;
}
- }
- else {
- osz = sz - co->u_co.zs.avail_in;
- }
- } break;
- default : {
- back = 0;
- } break;
- }
- } while ((0 < co->u_co.zs.avail_in) && (1 == back));
- if (NULL != szo) { *szo = osz; }
+ } break;
+ default : {
+ back = 0;
+ } break;
+ }
+ } while ((0 < co->u_co.zs.avail_in) && (1 == back));
+ if (NULL != szo) { *szo = osz; }
+ } else {
+ }
} break;
case DK4_STRMCO_CO_LZW : {
back = 1;
@@ -525,6 +577,9 @@ dk4strmco_co_write(
int back = 0; /* Function result */
int res; /* Result from various operations */
$? "+ dk4strmco_co_write"
+#if DK4_USE_ASSERT
+ assert(NULL != co);
+#endif
switch (co->pp) {
case DK4_STRMCO_PP_RUNLENGTH : {
$? ". run-length preprocessing"
@@ -588,11 +643,13 @@ dk4strmco_co_oe_close(compressor_t *co)
int back = 1;
int res = 0;
$? "+ dk4strmco_co_oe_close"
+#if DK4_USE_ASSERT
+ assert(NULL != co);
+#endif
switch (co->oe) {
case DK4_STRMCO_OE_ASCII85 : {
$? ". ASCII-85"
switch (dk4a85_enc_finish(&(co->u_oe.a85), NULL)) {
- $? ". handle final A85 data"
case DK4_EDSTM_FINISHED : {
if (0 < dk4a85_enc_output(&dptr,&sz,&(co->u_oe.a85),NULL)) {
if (0 < sz) {
@@ -675,6 +732,9 @@ dk4strmco_co_co_close(compressor_t *co)
int zres = Z_OK;
int res = 0;
$? "+ dk4strmco_co_co_close"
+#if DK4_USE_ASSERT
+ assert(NULL != co);
+#endif
switch (co->co) {
case DK4_STRMCO_CO_FLATE : {
$? ". flate compression used"
@@ -749,6 +809,9 @@ dk4strmco_co_pp_close(compressor_t *co)
int back = 1;
int res;
$? "+ dk4strmco_co_pp_close"
+#if DK4_USE_ASSERT
+ assert(NULL != co);
+#endif
switch (co->pp) {
case DK4_STRMCO_PP_RUNLENGTH : {
$? ". run-length preprocessing used"
@@ -790,6 +853,9 @@ dk4strmco_co_close(compressor_t *co)
{
int back = 1;
$? "+ dk4strmco_co_close"
+#if DK4_USE_ASSERT
+ assert(NULL != co);
+#endif
/* Flush preprocessor (run-length encoder).
*/
if (0 == dk4strmco_co_pp_close(co)) {
@@ -819,20 +885,29 @@ dk4strmco_co_close(compressor_t *co)
+static
void
dk4strmco_handler(dk4_stream_api_t *api)
{
compressor_t *co = NULL;
$? "+ dk4strmco_handler %d", api->cmd
+#if DK4_USE_ASSERT
+ assert(NULL != api);
+#endif
if (NULL != api) {
api->res = 0;
api->sz_out = 0;
co = (compressor_t *)(api->d);
switch (api->cmd) {
case DK4_STREAM_API_WRITE : { $? ". write"
- api->res = dk4strmco_co_write(
- co, api->b, api->sz_in, &(api->sz_out)
- );
+ if (0 < api->sz_in) {
+ api->res = dk4strmco_co_write(
+ co, api->b, api->sz_in, &(api->sz_out)
+ );
+ }
+ else {
+ api->res = 1;
+ }
} break;
case DK4_STREAM_API_FLUSH : { $? ". flush (ignored)"
/*
@@ -870,101 +945,110 @@ dk4strmco_open(
dk4_stream_t *back = NULL;
compressor_t *cc = NULL;
$? "+ dk4strmco_open"
- /* Allocate compressor and set up.
- */
- $? ". allocate memory"
- cc = dk4mem_new(compressor_t,1,erp);
- if (NULL != cc) {
- cc->dststrm = strm;
- cc->pp = pp;
- cc->co = co;
- cc->oe = oe;
- cc->fl = fl;
- cc->ll = 0;
- switch (pp) {
- case DK4_STRMCO_PP_RUNLENGTH : {
- $? ". initialize run-length encoder"
- dk4rle_init(
- &(cc->u_pp.rle),
- ((0 != (DK4_STRMCO_FL_EOD_PP & fl)) ? 1 : 0),
- erp
- );
- } break;
- default : {
- } break;
- }
- switch (co) {
- case DK4_STRMCO_CO_FLATE : {
- $? ". initialize flate compression"
- cc->u_co.zs.zalloc = Z_NULL;
- cc->u_co.zs.zfree = Z_NULL;
- cc->u_co.zs.opaque = Z_NULL;
- $? ". deflateInit"
- if (Z_OK != deflateInit(&(cc->u_co.zs), 9)) {
- $? ". release memory"
- dk4mem_free(cc);
- cc = NULL;
- $? "! failed to initialize flate compression"
- }
- } break;
- case DK4_STRMCO_CO_LZW : {
- $? ". initialize LZW compression"
- if (0 == dk4lzwe_init(&(cc->u_co.lzw), NULL)) {
- $? ". release memory"
- dk4mem_free(cc);
- cc = NULL;
- $? "! failed to initialize LZW compression"
- }
- } break;
- default : {
- } break;
- }
- switch (oe) {
- case DK4_STRMCO_OE_ASCII85 : {
- $? ". initialize ASCII-85 output encoding"
- dk4a85_enc_init(&(cc->u_oe.a85), 1, erp);
- } break;
- default : {
- } break;
- }
- }
-#if TRACE_DEBUG
- else { $? "! memory"
- }
+#if DK4_USE_ASSERT
+ assert(NULL != strm);
#endif
-
- /* If the compressor was set up successfully, create stream.
- */
- if (NULL != cc) {
- $? ". create stream"
- back = dk4stream_open(cc,dk4strmco_handler,DK4_STREAM_WRITE,0,4096,erp);
- /*
- When failed to create stream, we must release the compressor.
+ if (NULL != strm) {
+ /* Allocate compressor and set up.
*/
- if (NULL == back) {
- /*
- Release dynamic resources assigned to components.
- */
- $? "! failed to create stream"
+ $? ". allocate memory"
+ cc = dk4mem_new(compressor_t,1,erp);
+ if (NULL != cc) {
+ cc->dststrm = strm;
+ cc->pp = pp;
+ cc->co = co;
+ cc->oe = oe;
+ cc->fl = fl;
+ cc->ll = 0;
+ switch (pp) {
+ case DK4_STRMCO_PP_RUNLENGTH : {
+ $? ". initialize run-length encoder"
+ dk4rle_init(
+ &(cc->u_pp.rle),
+ ((0 != (DK4_STRMCO_FL_EOD_PP & fl)) ? 1 : 0),
+ NULL
+ );
+ } break;
+ default : {
+ } break;
+ }
switch (co) {
case DK4_STRMCO_CO_FLATE : {
- $? ". deflateEnd"
- (void)deflateEnd(&(cc->u_co.zs));
+ $? ". initialize flate compression"
+ cc->u_co.zs.zalloc = Z_NULL;
+ cc->u_co.zs.zfree = Z_NULL;
+ cc->u_co.zs.opaque = Z_NULL;
+ $? ". deflateInit"
+ if (Z_OK != deflateInit(&(cc->u_co.zs), 9)) {
+ $? ". release memory"
+ dk4mem_free(cc);
+ cc = NULL;
+ $? "! failed to initialize flate compression"
+ }
} break;
case DK4_STRMCO_CO_LZW : {
- $? ". LZW finish"
- (void)dk4lzwe_finish(&(cc->u_co.lzw), NULL);
+ $? ". initialize LZW compression"
+ if (0 == dk4lzwe_init(&(cc->u_co.lzw), NULL)) {
+ $? ". release memory"
+ dk4mem_free(cc);
+ cc = NULL;
+ $? "! failed to initialize LZW compression"
+ }
+ } break;
+ default : {
+ } break;
+ }
+ switch (oe) {
+ case DK4_STRMCO_OE_ASCII85 : {
+ $? ". initialize ASCII-85 output encoding"
+ dk4a85_enc_init(&(cc->u_oe.a85), 1, NULL);
} break;
default : {
} break;
}
- $? ". release memory"
- dk4mem_free(cc);
}
#if TRACE_DEBUG
- else { $? ". stream created"
+ else { $? "! memory"
}
#endif
+ /* If the compressor was set up successfully, create stream.
+ */
+ if (NULL != cc) {
+ $? ". create stream"
+ back = dk4stream_open(
+ cc, dk4strmco_handler, DK4_STREAM_WRITE, 0, 4096, erp
+ );
+ /*
+ When failed to create stream, we must release the compressor.
+ */
+ if (NULL == back) {
+ /*
+ Release dynamic resources assigned to components.
+ */
+ $? "! failed to create stream"
+ switch (co) {
+ case DK4_STRMCO_CO_FLATE : {
+ $? ". deflateEnd"
+ (void)deflateEnd(&(cc->u_co.zs));
+ } break;
+ case DK4_STRMCO_CO_LZW : {
+ $? ". LZW finish"
+ (void)dk4lzwe_finish(&(cc->u_co.lzw), NULL);
+ } break;
+ default : {
+ } break;
+ }
+ $? ". release memory"
+ dk4mem_free(cc);
+ }
+#if TRACE_DEBUG
+ else { $? ". stream created"
+ }
+#endif
+ }
+ }
+ else {
+ dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
$? "- dk4strmco_open %d", TR_IPTR(back)
return back;