File: _Doc.c

package info (click to toggle)
pyrite 0.9.3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,504 kB
  • ctags: 1,924
  • sloc: python: 6,064; ansic: 5,094; makefile: 275; sh: 172
file content (270 lines) | stat: -rw-r--r-- 6,134 bytes parent folder | download
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
/*
 * _Doc.c - Python module to do DOC e-text (de)compression
 *
 * The original version of this algorithm is found in makedoc7.cpp by
 * Harold Bamford, Rick Bram, and Pat Bierne.
 *
 * Rewritten in C and pythonified by Rob Tillotson <robt@debian.org>.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee or royalty is
 * hereby granted, provided that the above copyright notice appear in
 * all copies and that both the copyright notice and this permission
 * notice appear in supporting documentation or portions thereof,
 * including modifications, that you you make.
 *
 * THE AUTHOR ROB TILLOTSON DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE!
 *
 * $Id: _Doc.c,v 1.2 1999/08/09 21:59:14 rob Exp $
 */

#include "stdio.h"
#include "stdlib.h"
#include "Python.h"

#define DISP_BITS 11
#define COUNT_BITS 3

#define BUFSIZE 6000
#define MAXTEXTSIZE 4096

/* The original code uses a C++ class to handle the compressed stream.
 */

typedef struct {
    unsigned char *buf;
    int   len;
    int   bSpace;
} Buffer;

static unsigned char* memfind(unsigned char* t, int t_len, unsigned char* m, int m_len)
{
	int i;

	for (i = t_len - m_len + 1 ; i>0; i--, t++)
		if (t[0]==m[0] && memcmp(t,m,m_len)==0)
			return t;
	return 0;
}

static unsigned int issue(Buffer *b, unsigned char src) {

    int iDest = b->len;
    unsigned char *dest = b->buf;
    
    if (b->bSpace) {
	if (src >= 0x40 && src <= 0x7f)
	    dest[iDest++] = src ^ 0x80;
	else {
	    dest[iDest++] = ' ';
	    if (src < 0x80 && (src == 0 || src > 8))
		dest[iDest++] = src;
	    else {
		dest[iDest++] = 1;
		dest[iDest++] = src;
	    }
	}
	b->bSpace = 0;
    }
    else {
	if (src == ' ')
	    b->bSpace = 1;
	else {
	    if (src < 0x80 && (src == 0 || src > 8))
		dest[iDest++] = src;
	    else {
		dest[iDest++] = 1;
		dest[iDest++] = src;
	    }
	}
    }
    b->len = iDest;
    return iDest;
}

static unsigned int doc_compress(Buffer *b) {
    int i, j;

    unsigned char *pBuffer;
    unsigned char *pHit;
    unsigned char *pPrevHit;
    unsigned char *pTestHead;
    unsigned char *pTestTail;
    unsigned char *pEnd;

    unsigned int dist, compound, k;

    pHit = pPrevHit = pTestHead = pBuffer = b->buf;
    pTestTail = pTestHead+1;
    pEnd = b->buf + b->len;

    b->buf = malloc(6000);
    b->len = 0;

    for (; pTestHead != pEnd; pTestTail++) {
	if (pTestHead - pPrevHit > ((1 << DISP_BITS)-1))
	    pPrevHit = pTestHead - ((1 << DISP_BITS)-1);
	pHit = memfind(pPrevHit, pTestTail - pPrevHit, pTestHead, pTestTail - pTestHead);
	if (pHit == 0)
	    fprintf(stderr, "!!bug!!");
	if (pHit == 0
	    || pHit == pTestHead
	    || pTestTail-pTestHead > (1 << COUNT_BITS)+2
	    || pTestTail == pEnd) {
	    if (pTestTail-pTestHead < 4) {
		issue(b, pTestHead[0]);
		pTestHead++;
	    } else {
		if (b->bSpace) {
		    b->buf[b->len++] = ' ';
		    b->bSpace = 0;
		}
		dist = pTestHead - pPrevHit;
		compound = (dist << COUNT_BITS) + pTestTail-pTestHead-4;
		b->buf[b->len++] = 0x80 + (compound >> 8);
		b->buf[b->len++] = compound & 0xff;
		pTestHead = pTestTail - 1;
	    }
	    pPrevHit = pBuffer;
	} else {
	    pPrevHit = pHit;
	}
	if (pTestTail == pEnd) pTestTail--;
    }

    if (b->bSpace) b->buf[b->len++] = ' ';

    for (i=k=0; i < b->len; i++, k++) {
	b->buf[k] = b->buf[i];
	if (b->buf[k] >= 0x80 && b->buf[k] < 0xc0)
	    b->buf[++k] = b->buf[++i];
	else if (b->buf[k] == 1) {
	    b->buf[k+1] = b->buf[i+1];
	    while (i + 2 < b->len && b->buf[i+2] == 1 && b->buf[k] < 8) {
		b->buf[k]++;
		b->buf[k+b->buf[k]] = b->buf[i+3];
		i += 2;
	    }
	    k += b->buf[k];
	    i++;
	}
    }

    free(pBuffer);
    b->len = k;

    return k;
}


static unsigned int doc_uncompress(Buffer *b) {
    unsigned char *pOut;
    unsigned char *in_buf;
    unsigned char *out_buf;
    int i, j, m, n;
    unsigned int c;
    
    pOut = malloc(6000);
    in_buf = b->buf;
    out_buf = pOut;

    for (j=i=0; j < b->len; ) {
	c = in_buf[j++];

	if (c > 0 && c < 9)
	    while (c--) out_buf[i++] = in_buf[j++];
	else if (c < 0x80)
	    out_buf[i++] = c;
	else if (c >= 0xc0) {
	    out_buf[i++] = ' ';
	    out_buf[i++] = c ^ 0x80;
	} else {
	    c <<= 8;
	    c += in_buf[j++];
	    m = (c & 0x3fff) >> COUNT_BITS;
	    n = c & ((1 << COUNT_BITS)-1);
	    n += 3;
	    while (n--) {
		out_buf[i] = out_buf[i-m];
		i++;
	    }
	}
    }

    free(b->buf);
    b->buf = pOut;
    b->len = i;

    return i;
}


static PyObject *compress_str(PyObject *self, PyObject *args) {
    char *str;
    int len;
    Buffer b;
    PyObject *o;
    
    if (!PyArg_ParseTuple(args, "s#", &str, &len)) return NULL;

    if (len > MAXTEXTSIZE || len < 1) {
	PyErr_SetString(PyExc_ValueError, "string must be 1 to 4096 bytes long");
	return NULL;
    }

    b.buf = malloc(BUFSIZE);
    memcpy(b.buf, str, len);
    b.len = len;
    b.bSpace = 0;

    doc_compress(&b);

    o = Py_BuildValue("s#", b.buf, b.len);
    free(b.buf);

    return o;
}

static PyObject *uncompress_str(PyObject *self, PyObject *args) {
    char *str;
    int len;
    Buffer b;
    PyObject *o;
    
    if (!PyArg_ParseTuple(args, "s#", &str, &len)) return NULL;

    if (len > MAXTEXTSIZE || len < 1) {
	PyErr_SetString(PyExc_ValueError, "string must be 1 to 4096 bytes long");
	return NULL;
    }

    b.buf = malloc(BUFSIZE);
    memcpy(b.buf, str, len);
    b.len = len;
    b.bSpace = 0;
    
    doc_uncompress(&b);

    o = Py_BuildValue("s#", b.buf, b.len);
    free(b.buf);

    return o;
}


static PyMethodDef _DocMethods[] = {
    { "compress", compress_str, 1 },
    { "uncompress", uncompress_str, 1 },
    { NULL, NULL }
};

void init_Doc() {
    (void) Py_InitModule("_Doc", _DocMethods);
}