summaryrefslogtreecommitdiff
path: root/support/dktools/dk4strmco.c
blob: 6c8609aaf4e88448fda4529a3e93ac1821712b4f (plain)
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4strmco.ctr
*/

/*
Copyright (C) 2016-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**	@file dk4strmco.c The dk4strmco module.
*/


#line 116 "dk4strmco.ctr"

#include "dk4conf.h"

#if	DK4_HAVE_ZLIB_H
#ifndef	ZLIB_H_INCLUDED
#include <zlib.h>
#define	ZLIB_H_INCLUDED 1
#endif
#endif

#include "dk4rle.h"
#include "dk4a85e.h"
#include "dk4strmco.h"
#include "dk4mem.h"
#include "dk4memrs.h"
#include "dk4lzwe.h"




#line 135 "dk4strmco.ctr"



#ifndef	DK4_STRMCO_ZBUF_SIZE
#define	DK4_STRMCO_ZBUF_SIZE	4096
#endif



/**	Compressor data structure.
*/
typedef struct {
	char				 zbuf[DK4_STRMCO_ZBUF_SIZE];	/**< Output buffer. */
	union {
#if DK4_HAVE_ZLIB_H
		z_stream		 zs;		/**< Flate compression structure. */
#endif
		dk4_lzwe_t		 lzw;		/**< LZW compression structure. */
	}					 u_co;		/**< Compression structures. */
	union {
		dk4_rl_enc_t	 rle;		/**< Run-length encoder. */
	}					 u_pp;		/**< Preprocessing structures. */
	union {
		dk4_a85_enc_t	 a85;		/**< ASCII-85 encoder. */
	}					 u_oe;		/**< Output encoding structures. */
	dk4_stream_t		*dststrm;	/**< Destination stream. */
	size_t				 ll;		/**< Line length. */
	int					 pp;		/**< Preprocessing. */
	int					 co;		/**< Compression. */
	int					 oe;		/**< Output encoding. */
	int					 fl;		/**< Flags. */
} compressor_t;



/**	Newline.
*/
static char nl[] = {
	'\n', '\0'
};



/**	Carriage return and newline.
*/
static char crnl[] = {
	'\r', '\n', '\0'
};



/**	Finalizer for ASCII-Hex encoding.
*/
static char ahfin[] = {
	'>', '\0'
};



/**	Finalizer for ASCII-85 encoding.
*/
static char a85fin[] = {
	'~', '>', '\0'
};



/**	Hexadecimal digits using lower case characters.
*/
static char lower[] = {
	'0', '1', '2', '3', '4', '5', '6', '7',
	'8', '9', 'a', 'b', 'c', 'd', 'e', 'f', '\0'
};



/**	Hexadecimal digits using upper case characters.
*/
static char upper[] = {
	'0', '1', '2', '3', '4', '5', '6', '7',
	'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '\0'
};



/**	Write newline to destination stream.
	@param	co	Compressor structure.
	@return	1 on success, 0 on error.
*/
static
int
dk4strmco_nl(compressor_t *co)
{
	int		back	= 0;
	

#line 230 "dk4strmco.ctr"
	if (0 != (DK4_STRMCO_FL_CR_NL & (co->fl))) {
		back = dk4stream_write(co->dststrm, crnl, 2, NULL);
	}
	else {
		back = dk4stream_write(co->dststrm, nl, 1, NULL);
	}
	

#line 237 "dk4strmco.ctr"
	return back;
}




/**	Write bytes to compressor at output encoding level.
	@param	co	Compressor to write to.
	@param	b	Buffer start address.
	@param	sz	Number of bytes to write.
	@param	szo	Address of variable for number of bytes written, may be NULL.
	@return	1 on success, 0 on error.
*/
static
int
dk4strmco_co_oe(
	compressor_t	*co,
	const char		*b,
	size_t			 sz,
	size_t			*szo
)
{
	char			 buf[8];		/* Buffer for hexadecimal digits. */
	const char		*dptr;			/* Pointer to A85 encoding result. */
	size_t			 osz	= 0;	/* Number of bytes processed. */
	size_t			 szd	= 0;	/* A85 encoding result length. */
	size_t			 i		= 0;	/* Index of current byte to process. */
	size_t			 j		= 0;	/* Index of cur out char. */
	int				 back	= 0;	/* Function result. */
	int				 res;			/* Result from various operations. */
	unsigned		 us;			/* Index in hex digits array. */
	unsigned char	 uc;			/* UC version of current character */
	

#line 270 "dk4strmco.ctr"
	switch (co->oe) {
		case DK4_STRMCO_OE_ASCII85 : {
			

#line 273 "dk4strmco.ctr"
			back = 1;
			for (i = 0; ((i < sz) && (1 == back)); i++) {
				switch (dk4a85_enc_add(&(co->u_oe.a85), b[i], NULL)) {
					case DK4_EDSTM_ACCEPT : {
						osz++;
					} break;
					case DK4_EDSTM_FINISHED : {
						dptr = NULL;
						szd   = 0;
						res = dk4a85_enc_output(
							&dptr, &szd, &(co->u_oe.a85), NULL
						);
						if (0 < res) {
							if (0 < szd) {
#if	VERSION_BEFORE_20161019
								res = dk4stream_write(
									co->dststrm, dptr, szd, NULL
								);
								if (0 < res) {
									osz++;
									co->ll += szd;
									if (0 != (DK4_STRMCO_FL_NL_OE & (co->fl))) {
										if (74 <= co->ll) {
											res = dk4strmco_nl(co);
											if (0 < res) {
												co->ll = 0;
											}
											else {
												back = 0;
											}
										}
									}
								}
								else {
									back = 0;
								}
#else
								back = 1;
								for (j = 0; ((j < szd) && (1 == back)); j++) {
									res = dk4stream_write_byte(
										co->dststrm, dptr[j], NULL
									);
									if (0 < res) {
										osz++;
										co->ll += 1;
										if (75 <= co->ll) {
											res = dk4strmco_nl(co);
											if (0 < res) {
												co->ll = 0;
											}
											else {
												back = 0;
											}
										}
									}
									else {
										back = 0;
									}
								}
#endif
							}
							else {
								osz++;
							}
						}
						else {
							back = 0;
						}
					} break;
					default : {
						back = 0;
					} break;
				}
			}
		} break;
		case DK4_STRMCO_OE_HEX : {
			

#line 350 "dk4strmco.ctr"
			back = 1;
			buf[2] = '\0';
			for (i = 0; ((i < sz) && (1 == back)); i++) {
				uc = (unsigned char)(b[i]);
				us = 0x000FU & ((unsigned char)uc);
				if (0 != (DK4_STRMCO_FL_UPPER_OE & (co->fl))) {
					buf[1] = upper[us];
				}
				else {
					buf[1] = lower[us];
				}
				us = 0x000FU & (((unsigned char)uc) >> 4);
				if (0 != (DK4_STRMCO_FL_UPPER_OE & (co->fl))) {
					buf[0] = upper[us];
				}
				else {
					buf[0] = lower[us];
				}
				res = dk4stream_write(co->dststrm, buf, 2, NULL);
				co->ll += 2;
				if (0 < res) {
					osz++;
					if (0 != (DK4_STRMCO_FL_NL_OE & (co->fl))) {
						if (78 <= co->ll) {
							res = dk4strmco_nl(co);
							if (0 < res) {
								co->ll = 0;
							}
							else {
								back = 0;
							}
						}
					}
				}
				else {
					back = 0;
				}
			}
		} break;
		default : {
			

#line 391 "dk4strmco.ctr"
			back = dk4stream_write(co->dststrm, b, sz, NULL);
			if (0 != back) {
				osz = sz;
			}
		} break;
	}
	if (NULL != szo) { *szo = osz; }
	

#line 399 "dk4strmco.ctr"
	return back;
}



/**	Write bytes to compressor at compression level.
	@param	co	Compressor to write to.
	@param	b	Buffer start address.
	@param	sz	Number of bytes to write.
	@param	szo	Address of variable for number of bytes written, may be NULL.
	@return	1 on success, 0 on error.
*/
static
int
dk4strmco_co_co(
	compressor_t	*co,
	const char		*b,
	size_t			 sz,
	size_t			*szo
)
{
	const unsigned char	*cptr;
	size_t	osz		= 0;	/* Number of bytes processed successfully. */
	size_t	used	= 0;	/* Used bytes in output buffer. */
	size_t	i		= 0;	/* Index of current LZW byte to process. */
	size_t	lzwsz	= 0;	/* Bytes from LZW compression. */
	int		back	= 0;	/* Function result. */
	int		res		= 0;	/* Result from various operations. */
	

#line 428 "dk4strmco.ctr"
	switch (co->co) {
		case DK4_STRMCO_CO_FLATE : {
			

#line 431 "dk4strmco.ctr"
			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;
							}
							else {
								back = 0;
							}
						}
						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;
		case DK4_STRMCO_CO_LZW : {
			back = 1;
			for (i = 0; ((1 == back) && (i < sz)); i++) {
				res = dk4lzwe_add(&(co->u_co.lzw), (unsigned char)(b[i]), NULL);
				switch (res) {
					case DK4_EDSTM_ACCEPT : {
						osz++;
					} break;
					case DK4_EDSTM_FINISHED : {
						cptr	= NULL;
						lzwsz	= 0;
						res		= dk4lzwe_output(
							&cptr, &lzwsz, &(co->u_co.lzw), NULL
						);
						if ((0 != res) && (NULL != cptr) && (0 < lzwsz)) {
							back = dk4strmco_co_oe(
								co, (const char *)cptr, lzwsz, NULL
							);
							if (0 != back) {
								osz++;
							}
						}
						else {
							back = 0;
						}
					} break;
					case DK4_EDSTM_ERROR : {
						back = 0;
					} break;
				}
			}
			if (NULL != szo) { *szo = osz; }
		} break;
		default : {
			

#line 496 "dk4strmco.ctr"
			back = dk4strmco_co_oe(co, b, sz, szo);
		} break;
	}
	

#line 500 "dk4strmco.ctr"
	return back;
}



/**	Write bytes to compressor at preprocessor level.
	@param	co	Compressor to write to.
	@param	b	Buffer start address.
	@param	sz	Number of bytes to write.
	@param	szo	Address of variable for number of bytes written, may be NULL.
	@return	1 on success, 0 on error.
*/
static
int
dk4strmco_co_write(
	compressor_t	*co,
	const char		*b,
	size_t			 sz,
	size_t			*szo
)
{
	const unsigned char		*rleptr;		/* RL encoding result */
	size_t					 osz	= 0;	/* Bytes processed successfully */
	size_t					 i;				/* Running index of current byte */
	size_t					 rlesz;			/* Size of RL encoding result */
	int						 back	= 0;	/* Function result */
	int						 res;			/* Result from various operations */
	

#line 528 "dk4strmco.ctr"
	switch (co->pp) {
		case DK4_STRMCO_PP_RUNLENGTH : {
			

#line 531 "dk4strmco.ctr"
			back = 1;
			for (i = 0; ((i < sz) && (0 != back)); i++) {
				switch (dk4rle_add(&(co->u_pp.rle),(unsigned char)(b[i]),NULL))
				{
					case DK4_EDSTM_FINISHED : {
						rleptr = NULL;
						rlesz  = 0;
						res = dk4rle_output(
							&rleptr, &rlesz, &(co->u_pp.rle), NULL
						);
						if (0 != res) {
							res = dk4strmco_co_co(
								co, (const char *)rleptr, rlesz, NULL
							);
							if (0 != res) {
								osz++;
							}
							else {
								back = 0;
							}
						}
						else {
							back = 0;
						}
					} break;
					case DK4_EDSTM_ERROR : {
						back = 0;
					} break;
					default : {
						osz++;
					} break;
				}
			}
			if (NULL != szo) { *szo = osz; }
		} break;
		default : {
			

#line 568 "dk4strmco.ctr"
			back = dk4strmco_co_co(co, b, sz, szo);
		} break;
	}
	

#line 572 "dk4strmco.ctr"
	return back;
}



/**	Flush data in output encoders if used.
	@param	co	Compressor structure.
	@return	1 on success, 0 on error.
*/
static
int
dk4strmco_co_oe_close(compressor_t *co)
{
	const char	*dptr	= NULL;
	size_t		 sz		= 0;
	size_t		 j		= 0;
	int			 back	= 1;
	int			 res	= 0;
	

#line 591 "dk4strmco.ctr"
	switch (co->oe) {
		case DK4_STRMCO_OE_ASCII85 : {
			

#line 594 "dk4strmco.ctr"
			switch (dk4a85_enc_finish(&(co->u_oe.a85), NULL)) {
				

#line 596 "dk4strmco.ctr"
				case DK4_EDSTM_FINISHED : {
					if (0 < dk4a85_enc_output(&dptr,&sz,&(co->u_oe.a85),NULL)) {
						if (0 < sz) {
#if	VERSION_BEFORE_20161019
							back = dk4stream_write(co->dststrm, dptr, sz, NULL);
#else
							back = 1;
							for (j = 0; ((j < sz) && (1 == back)); j++) {
								res = dk4stream_write_byte(
									co->dststrm, dptr[j], NULL
								);
								if (0 < res) {
									co->ll += 1;
									if (75 <= co->ll) {
										res = dk4strmco_nl(co);
										if (0 < res) {
											co->ll = 0;
										}
										else {
											back = 0;
										}
									}
								}
								else {
									back = 0;
								}
							}
#endif
						}
					}
					else {
						back = 0;
					}
				} break;
			}
			if (0 != (DK4_STRMCO_FL_EOD_OE & (co->fl))) {
				

#line 633 "dk4strmco.ctr"
				res = dk4stream_write(co->dststrm, a85fin, 2, NULL);
				if (0 == res) {
					back = 0;
				}
			}
			if (0 != (DK4_STRMCO_FL_NL_OE & (co->fl))) {
				

#line 640 "dk4strmco.ctr"
				if (0 == dk4strmco_nl(co)) {
					back = 0;
				}
			}
		} break;
		case DK4_STRMCO_OE_HEX : {
			if (0 != (DK4_STRMCO_FL_EOD_OE & (co->fl))) {
				

#line 648 "dk4strmco.ctr"
				back = dk4stream_write(co->dststrm, ahfin, 1, NULL);
			}
			if (0 != (DK4_STRMCO_FL_NL_OE & (co->fl))) {
				

#line 652 "dk4strmco.ctr"
				if (0 == dk4strmco_nl(co)) {
					back = 0;
				}
			}
		} break;
	}
	

#line 659 "dk4strmco.ctr"
	return back;
}



/**	Flush data from flate compressor if used.
	@param	co	Compressor structure.
	@return	1 on success, 0 on error.
*/
static
int
dk4strmco_co_co_close(compressor_t *co)
{
	const unsigned char	*cptr;
	size_t	 used	= 0;
	int		 back	= 1;
	int		 zres	= Z_OK;
	int		 res	= 0;
	

#line 678 "dk4strmco.ctr"
	switch (co->co) {
		case DK4_STRMCO_CO_FLATE : {
			

#line 681 "dk4strmco.ctr"
			co->u_co.zs.next_in		= (unsigned char *)nl;
			co->u_co.zs.avail_in	= 0;
			do {
				co->u_co.zs.next_out	=	(unsigned char *)(&(co->zbuf[0]));
				co->u_co.zs.avail_out	=	DK4_STRMCO_ZBUF_SIZE;
				zres = deflate(&(co->u_co.zs), Z_FINISH);
				

#line 688 "dk4strmco.ctr"
				switch (zres) {
					case Z_OK : case Z_STREAM_END : {
						used = DK4_STRMCO_ZBUF_SIZE - co->u_co.zs.avail_out;
						

#line 692 "dk4strmco.ctr"
						if (0 < used) {
							res = dk4strmco_co_oe(co,&(co->zbuf[0]),used,NULL);
							if (0 == res) {
								back = 0;
							}
						}
					} break;
					default : {
						back = 0;
					} break;
				}
			} while ((Z_OK == zres) && (1 == back));
			

#line 705 "dk4strmco.ctr"
			if (Z_OK != deflateEnd(&(co->u_co.zs))) {
				back = 0;
			}
		} break;
		case DK4_STRMCO_CO_LZW : {
			res = dk4lzwe_finish(&(co->u_co.lzw), NULL);
			switch (res) {
				case DK4_EDSTM_FINISHED : {
					cptr = NULL;
					used = 0;
					res = dk4lzwe_output(
						&cptr, &used, &(co->u_co.lzw), NULL
					);
					if ((0 != res) && (NULL != cptr) && (0 < used)) {
						back = dk4strmco_co_oe(
							co, (const char *)cptr, used, NULL
						);
					}
					else {
						back = 0;
					}
				} break;
				case DK4_EDSTM_ERROR : {
					back = 0;
				} break;
			}
		} break;
	}
	

#line 734 "dk4strmco.ctr"
	return back;
}



/**	Flush data from run-length encoder if used.
	@param	co	Compressor structure.
	@return	1 on success, 0 on error.
*/
static
int
dk4strmco_co_pp_close(compressor_t *co)
{
	const unsigned char	*dptr	= NULL;
	size_t				 szd	= 0;
	int					 back	= 1;
	int					 res;
	

#line 752 "dk4strmco.ctr"
	switch (co->pp) {
		case DK4_STRMCO_PP_RUNLENGTH : {
			

#line 755 "dk4strmco.ctr"
			switch (dk4rle_finish(&(co->u_pp.rle), NULL)) {
				case DK4_EDSTM_ACCEPT : {
				} break;
				case DK4_EDSTM_FINISHED : {
					

#line 760 "dk4strmco.ctr"
					if (0 < dk4rle_output(&dptr, &szd, &(co->u_pp.rle), NULL)) {
						res = dk4strmco_co_write(
							co, (const char *)dptr, szd, NULL
						);
						if (0 == res) {	

#line 765 "dk4strmco.ctr"
							back = 0;
						}
					}
					else {				

#line 769 "dk4strmco.ctr"
						back = 0;
					}
				} break;
				default : {
				} break;
			}
		} break;
	}
	

#line 778 "dk4strmco.ctr"
	return back;
}



/**	Finish output (flush data stored in the encoders).
	@param	co	Compressor structure.
	@return	1 on success, 0 on error.
*/
static
int
dk4strmco_co_close(compressor_t *co)
{
	int					 back	= 1;
	

#line 793 "dk4strmco.ctr"
	/*	Flush preprocessor (run-length encoder).
	*/
	if (0 == dk4strmco_co_pp_close(co)) {
		back = 0;
	}
	/*	Flush compressor (flate stream).
	*/
	if (0 == dk4strmco_co_co_close(co)) {
		back = 0;
	}
	/*	Flush output encoder (ASCII-Hex or ASCII-85 encoder).
	*/
	if (0 == dk4strmco_co_oe_close(co)) {
		back = 0;
	}
	/*	Release memory.
	*/
	

#line 811 "dk4strmco.ctr"
	if (0 == dk4mem_reset_secure(co, sizeof(compressor_t), NULL)) {
		back = 0;
	}
	

#line 815 "dk4strmco.ctr"
	dk4mem_free(co);
	

#line 817 "dk4strmco.ctr"
	return back;
}



void
dk4strmco_handler(dk4_stream_api_t *api)
{
	compressor_t	*co	= NULL;
	

#line 827 "dk4strmco.ctr"
	if (NULL != api) {
		api->res = 0;
		api->sz_out = 0;
		co = (compressor_t *)(api->d);
		switch (api->cmd) {
			case DK4_STREAM_API_WRITE : {		

#line 833 "dk4strmco.ctr"
				api->res = dk4strmco_co_write(
					co, api->b, api->sz_in, &(api->sz_out)
				);
			} break;
			case DK4_STREAM_API_FLUSH : {		

#line 838 "dk4strmco.ctr"
				/*
					Do nothing but indicate success.
					Otherwise closing a stream will return an error.
				*/
				api->res = 1;
			} break;
			case DK4_STREAM_API_CLOSE : {		

#line 845 "dk4strmco.ctr"
				api->res = dk4strmco_co_close(co);
			} break;
			case DK4_STREAM_API_ZERO_READ_IS_EOF : {	

#line 848 "dk4strmco.ctr"
				api->res = 0;
			} break;
			default : {							

#line 851 "dk4strmco.ctr"
				/* Error: Functions not available */
			} break;
		}
	}
	

#line 856 "dk4strmco.ctr"
}



dk4_stream_t *
dk4strmco_open(
	dk4_stream_t		*strm,
	enum dk4_strmco_pp	 pp,
	enum dk4_strmco_co	 co,
	enum dk4_strmco_oe	 oe,
	int					 fl,
	dk4_er_t			*erp
)
{
	dk4_stream_t	*back	= NULL;
	compressor_t	*cc		= NULL;
	

#line 873 "dk4strmco.ctr"
	/*	Allocate compressor and set up.
	*/
	

#line 876 "dk4strmco.ctr"
	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 : {
				

#line 887 "dk4strmco.ctr"
				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 : {
				

#line 899 "dk4strmco.ctr"
				cc->u_co.zs.zalloc = Z_NULL;
				cc->u_co.zs.zfree  = Z_NULL;
				cc->u_co.zs.opaque = Z_NULL;
				

#line 903 "dk4strmco.ctr"
				if (Z_OK != deflateInit(&(cc->u_co.zs), 9)) {
					

#line 905 "dk4strmco.ctr"
					dk4mem_free(cc);
					cc = NULL;
					

#line 908 "dk4strmco.ctr"
				}
			} break;
			case DK4_STRMCO_CO_LZW : {
				

#line 912 "dk4strmco.ctr"
				if (0 == dk4lzwe_init(&(cc->u_co.lzw), NULL)) {
					

#line 914 "dk4strmco.ctr"
					dk4mem_free(cc);
					cc = NULL;
					

#line 917 "dk4strmco.ctr"
				}
			} break;
			default : {
			} break;
		}
		switch (oe) {
			case DK4_STRMCO_OE_ASCII85 : {
				

#line 925 "dk4strmco.ctr"
				dk4a85_enc_init(&(cc->u_oe.a85), 1, erp);
			} break;
			default : {
			} break;
		}
	}
#if TRACE_DEBUG
	else {										

#line 933 "dk4strmco.ctr"
	}
#endif

	/*	If the compressor was set up successfully, create stream.
	*/
	if (NULL != cc) {
		

#line 940 "dk4strmco.ctr"
		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.
			*/
			

#line 949 "dk4strmco.ctr"
			switch (co) {
				case DK4_STRMCO_CO_FLATE : {
					

#line 952 "dk4strmco.ctr"
					(void)deflateEnd(&(cc->u_co.zs));
				} break;
				case DK4_STRMCO_CO_LZW : {
					

#line 956 "dk4strmco.ctr"
					(void)dk4lzwe_finish(&(cc->u_co.lzw), NULL);
				} break;
				default : {
				} break;
			}
			

#line 962 "dk4strmco.ctr"
			dk4mem_free(cc);
		}
#if TRACE_DEBUG
		else {				

#line 966 "dk4strmco.ctr"
		}
#endif
	}
	

#line 970 "dk4strmco.ctr"
	return back;
}



/* vim: set ai sw=4 ts=4 : */