1 /***************************************************************************
2  *                                                                         *
3  *    LIBDSK: General floppy and diskimage access library                  *
4  *    Copyright (C) 2002, 2014  John Elliott <seasip.webmaster@gmail.com>      *
5  *                                                                         *
6  *    This library is free software; you can redistribute it and/or        *
7  *    modify it under the terms of the GNU Library General Public          *
8  *    License as published by the Free Software Foundation; either         *
9  *    version 2 of the License, or (at your option) any later version.     *
10  *                                                                         *
11  *    This library is distributed in the hope that it will be useful,      *
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of       *
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    *
14  *    Library General Public License for more details.                     *
15  *                                                                         *
16  *    You should have received a copy of the GNU Library General Public    *
17  *    License along with this library; if not, write to the Free           *
18  *    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,      *
19  *    MA 02111-1307, USA                                                   *
20  *                                                                         *
21  ***************************************************************************/
22 
23 /* LZHUF (as used in Teledisk). Decompression only, no compression. */
24 /* Based on tdlzhuf.c by Will Kranz, released under GPLv2. Permission to
25  * relicense this source file under LGPLv2 was granted by email.
26  *
27  * Rewritten to hold decoder state in the TLZH_COMPRESS_DATA structure
28  * rather than static variables. */
29 
30 /******************************* compsq.h **********************************/
31 
32 /* Buffer size */
33 #define TLZH_BUFSZ 512
34 /* LZSS Parameters */
35 #define TLZH_N         4096    /* Size of string buffer */
36 #define TLZH_F           60    /* Size of look-ahead buffer */
37 #define TLZH_THRESHOLD    2
38 #define TLZH_NIL     TLZH_N    /* End of tree's node  */
39 /* Huffman coding parameters */
40 
41 #define TLZH_NCHAR      (256 - TLZH_THRESHOLD + TLZH_F)
42                 /* character code (= 0..TLZH_NCHAR-1) */
43 #define TLZH_T         (TLZH_NCHAR * 2 - 1)    /* Size of table */
44 #define TLZH_R         (TLZH_T - 1)            /* root position */
45 #define TLZH_MAX_FREQ    0x8000
46                     /* update when cumulative frequency */
47                     /* reaches to this value */
48 
49 
50 
51 typedef struct
52 {
53         COMPRESS_DATA tlzh_super;
54 
55 	FILE *fp_in;
56 	FILE *fp_out;
57 
58 	unsigned short r, bufcnt,bufndx,bufpos,  // string buffer
59          // the following to allow block reads from input in next_word()
60         ibufcnt,ibufndx; // input buffer counters
61 	unsigned char  inbuf[TLZH_BUFSZ];    // input buffer
62 	unsigned char  obuf[TLZH_BUFSZ];	// output buffer
63 
64 	unsigned char text_buf[TLZH_N + TLZH_F - 1];
65 	short    match_position, match_length;
66 /* Tree structure, held as 3 arrays: children on left, children on right,
67    parents */
68 	short    lson[TLZH_N + 1], rson[TLZH_N + 257], dad[TLZH_N + 1];
69 /* Huffman data */
70 	unsigned short freq[TLZH_T + 1];    /* cumulative freq table */
71 
72 /*
73  * pointing parent nodes.
74  * area [T..(T + TLZH_NCHAR - 1)] are pointers for leaves
75  */
76 	short prnt[TLZH_T + TLZH_NCHAR];
77 
78 /* pointing children nodes (son[], son[] + 1)*/
79 	short son[TLZH_T];
80 
81 	unsigned short getbuf /*= 0*/;
82 	unsigned char getlen /*= 0*/;
83 
84 
85 
86 } TLZH_COMPRESS_DATA;
87 
88 
89 extern COMPRESS_CLASS cc_tlzh;
90 
91 
92 dsk_err_t tlzh_open(COMPRESS_DATA *self);
93 dsk_err_t tlzh_creat(COMPRESS_DATA *self);
94 dsk_err_t tlzh_commit(COMPRESS_DATA *self);
95 dsk_err_t tlzh_abort(COMPRESS_DATA *self);
96 
97