File: dictstr.c

package info (click to toggle)
dico 2.12-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,300 kB
  • sloc: ansic: 94,671; sh: 52,520; lex: 4,023; tcl: 1,439; yacc: 1,439; makefile: 1,387; python: 1,310; perl: 1,200; lisp: 489; awk: 157; pascal: 127; javascript: 71; cpp: 50; fortran: 28; asm: 21; sed: 16; xml: 8
file content (585 lines) | stat: -rw-r--r-- 14,686 bytes parent folder | download | duplicates (2)
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
/* This file is part of Dico.
   Copyright (C) 2008-2024 Sergey Poznyakoff

   Dico is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   Dico is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Dico.  If not, see <http://www.gnu.org/licenses/>. */

#include "dictorg.h"

#define DE_UNKNOWN_FORMAT -1
#define DE_UNSUPPORTED_FORMAT -2
#define DE_UNSUPPORTED_VERSION -3
#define DE_BAD_HEADER -4
#define DE_GZIP_SEEK -5
#define DE_INFLATE -6

struct _dict_chunk {
    unsigned length;             /* Chunk length */
    unsigned offset;             /* Chunk offset */
};

struct _dict_chunk_cache {
    int num;                     /* Chunk number */
    char *buffer;                /* Cunk contents */
    size_t size;                 /* Size of data in buffer */
    unsigned refcount;           /* Reference count */
};

struct _dict_stream {
    int type;                    /* Stream type (see DICTORG_ constants) */
    dico_stream_t transport;     /* Underlying transport stream */
    int transport_error;         /* Last error on transport stream */
    /* Gzip/dict.org header */
    size_t header_length;        /* Header length. */
    int method;                  /* Compression method */
    int flags;
    time_t mtime;                /* Mtime of the original file */
    int extra_flags;
    int os;                      /* OS (see GZ_OS_ constants) */
    int version;                 /* Format version (must be 1) */
    unsigned chunk_size;         /* Chunk size */
    unsigned chunk_count;        /* Number of chunks in the file */
    struct _dict_chunk *chunk;   /* Chunk descriptions */
    char *orig_name;             /* Original file name */
    char *comment;               /* Comment */
    unsigned long crc;
    unsigned long size;          /* Original file size */
    unsigned long compressed_size; /* Compressed file size */
    size_t offset;               /* Offset in stream */
    char *buffer;                /* Decompression buffer (chunk_size bytes) */
    /* Z stream */
    z_stream      zstream;
    int           zstr_ready;

    /* Chunk cache */
    size_t cache_size;                /* Max. number of elements in cache */
    size_t cache_used;                /* Actual number of elements in cache */
    struct _dict_chunk_cache **cache; /**/
};


static struct _dict_chunk_cache *
cache_create_chunk(struct _dict_stream *str)
{
    struct _dict_chunk_cache *cp = malloc(sizeof(*cp) + str->chunk_size);
    if (cp) {
	cp->buffer = (char*) (cp + 1);
	cp->num = -1;
	cp->refcount = 0;
	cp->size = 0;
    }
    return cp;
}

static int
cache_alloc(struct _dict_stream *str)
{
    int n;
    struct _dict_chunk_cache *cp;

    if (!str->cache) {
	str->cache = calloc(str->cache_size, sizeof(str->cache[0]));
	if (!str->cache)
	    return 1;
    }
    if (str->cache_used < str->cache_size) {
	cp = cache_create_chunk(str);
	if (!cp)
	    return 1;
	n = str->cache_used++;
	str->cache[n] = cp;
    } else {
	n = str->cache_used - 1;
	cp = str->cache[n];
	cp->refcount = 0;
	cp->num = -1;
    }
    return n;
}

static void
cache_destroy(struct _dict_stream *str)
{
    if (str->cache) {
	size_t i;
	for (i = 0; i < str->cache_used; i++) {
	    if (!str->cache[i])
		break;
	    free(str->cache[i]);
	}
	free(str->cache);
	str->cache = NULL;
    }
}

static void
cache_promote(struct _dict_stream *str, int n)
{
    int i;
    unsigned refcount = ++str->cache[n]->refcount;

    for (i = n - 1; i >= 0; i--)
	if (str->cache[i]->refcount >= refcount)
	    break;
    i++;
    if (i != n) {
	struct _dict_chunk_cache *tmp = str->cache[n];
	str->cache[n] = str->cache[i];
	str->cache[i] = tmp;
    }
}

static int
cache_get(struct _dict_stream *str, int chunk_num,
	  struct _dict_chunk_cache **retptr)
{
    int i;
    struct _dict_chunk_cache *cp;
    size_t rdbytes;

    for (i = 0; i < str->cache_used; i++) {
	if (str->cache[i]->num == chunk_num) {
	    cp = str->cache[i];
	    cache_promote(str, i);
	    *retptr = cp;
	    return 0;
	}
    }

    i = cache_alloc(str);
    cp = str->cache[i];
    cp->num = chunk_num;
    cache_promote(str, i);
    if (dico_stream_seek(str->transport, str->chunk[chunk_num].offset,
			 DICO_SEEK_SET) < 0)
	return str->transport_error = dico_stream_last_error(str->transport);

    if (!str->zstr_ready) {
	str->zstream.zalloc    = NULL;
	str->zstream.zfree     = NULL;
	str->zstream.opaque    = NULL;
	str->zstream.next_in   = 0;
	str->zstream.avail_in  = 0;
	str->zstream.next_out  = NULL;
	str->zstream.avail_out = 0;
	if (inflateInit2(&str->zstream, -15 ) != Z_OK) {
	    dico_log(L_ERR, 0, _("cannot initialize inflation engine: %s"),
		     str->zstream.msg);
	    return DE_INFLATE;
	}
	str->zstr_ready = 1;
    }

    if (dico_stream_read(str->transport, str->buffer,
			 str->chunk[chunk_num].length, &rdbytes) < 0)
	return str->transport_error = dico_stream_last_error(str->transport);

    str->zstream.next_in   = (unsigned char*) str->buffer;
    str->zstream.avail_in  = rdbytes;
    str->zstream.next_out  = (unsigned char*) cp->buffer;
    str->zstream.avail_out = str->chunk_size;

    if (inflate(&str->zstream, Z_PARTIAL_FLUSH) != Z_OK) {
	dico_log(L_ERR, 0, "inflate: %s", str->zstream.msg);
	return DE_INFLATE;
    }

    if (str->zstream.avail_in) {
	dico_log(L_ERR, 0, _("%s:%d: INTERNAL ERROR: "
			     "inflate did not flush (%d pending, %d avail)"),
		 __FILE__, __LINE__,
		 str->zstream.avail_in, str->zstream.avail_out );
	return DE_INFLATE;
    }

    cp->size = str->chunk_size - str->zstream.avail_out;

    *retptr = cp;
    return 0;
}

static int
_dict_read_dzip(struct _dict_stream *str, char *buf, size_t size, size_t *pret)
{
    int chunk_num = str->offset / str->chunk_size;
    size_t chunk_off = str->offset - chunk_num * str->chunk_size;
    size_t rdbytes = 0;
    int rc;

    while (size) {
	struct _dict_chunk_cache *cp;
	size_t n;

	rc = cache_get(str, chunk_num, &cp);
	if (rc)
	    break;
	n = cp->size - chunk_off; /* FIXME = str->chunk[chunk_num].length;? */
	if (n > size)
	    n = size;
	memcpy(buf, cp->buffer + chunk_off, n);
	size -= n;
	buf += n;
	rdbytes += n;
	str->offset += n;
	chunk_num++;
	chunk_off = 0;
    }
    *pret = rdbytes;
    return rc;
}

static int
_dict_seek_dzip(struct _dict_stream *str, off_t needle, int whence,
		off_t *presult)
{
    off_t offset;

    switch (whence) {
    case DICO_SEEK_SET:
	offset = needle;
	break;

    case DICO_SEEK_CUR:
	offset = str->offset + needle;
	break;

    case DICO_SEEK_END:
	offset = str->size + needle;
	break;

    default:
	return EINVAL;
    }

    if (offset < 0 || offset > str->size)
	return EINVAL;

    *presult = str->offset = offset;
    return 0;
}


static int
_dict_close(void *data)
{
    struct _dict_stream *str = data;
    return dico_stream_close(str->transport);
}

static int
_dict_destroy(void *data)
{
    struct _dict_stream *str = data;

    if (str->zstr_ready) {
	if (inflateEnd(&str->zstream))
	    dico_log(L_ERR, 0, _("%s:%d: INTERNAL ERROR: "
				 "cannot shut down inflation engine: %s"),
		     __FILE__, __LINE__, str->zstream.msg);
	    /* Continue anyway */
    }

    cache_destroy(str);
    free(str->buffer);
    dico_stream_destroy(&str->transport);
    free(str);
    return 0;
}

static int
stream_get8(dico_stream_t str, unsigned char *p)
{
    return dico_stream_read(str, p, 1, NULL);
}

static int
stream_get16(dico_stream_t str, uint16_t *pres)
{
    unsigned char buf[2];
    int rc = dico_stream_read(str, buf, sizeof buf, NULL);
    if (rc == 0)
	*pres = buf[0] + (((uint16_t) buf[1]) << 8);
    return rc;
}

static int
stream_get32(dico_stream_t str, uint32_t *pres)
{
    unsigned char buf[4];
    int rc = dico_stream_read(str, buf, sizeof buf, NULL);
    if (rc == 0)
	*pres = (((uint16_t) buf[3]) << 24) +
		 (((uint16_t) buf[2]) << 16) +
		 (((uint16_t) buf[1]) << 8) +
		 buf[0];
    return rc;
}

static int
_dict_open(void *data, int flags)
{
    struct _dict_stream *str = data;
    unsigned char id[2];
    int rc;
    unsigned char buf8;
    uint16_t buf16;
    uint32_t buf32;
    unsigned i;
    unsigned offset;
    off_t pos;

    if (dico_stream_open(str->transport))
	return str->transport_error = dico_stream_last_error(str->transport);

    str->header_length = GZ_XLEN - 1;
    str->type = DICTORG_UNKNOWN;

    rc = dico_stream_read(str->transport, id, 2, NULL);
    if (rc) {
	dico_stream_close(str->transport);
	return str->transport_error = dico_stream_last_error(str->transport);
    }

    if (id[0] != GZ_MAGIC1 || id[1] != GZ_MAGIC2) {
	str->type = DICTORG_TEXT;
	dico_stream_size(str->transport, &pos);
	str->compressed_size = str->size = pos;
	return 0;
    }

    str->type = DICTORG_GZIP;

    stream_get8(str->transport, &buf8);
    str->method = buf8;
    stream_get8(str->transport, &buf8);
    str->flags  = buf8;

    stream_get32(str->transport, &buf32);
    str->mtime = buf32;

    stream_get8(str->transport, &buf8);
    str->extra_flags  = buf8;
    stream_get8(str->transport, &buf8);
    str->os = buf8;

    if (str->flags & GZ_FEXTRA) {
	stream_get16(str->transport, &buf16);
	str->header_length += buf16 + 2;
	dico_stream_read(str->transport, id, 2, NULL);

	if (id[0] == GZ_RND_S1 && id[1] == GZ_RND_S2) {
	    dico_stream_seek(str->transport, 2, DICO_SEEK_CUR);

	    stream_get16(str->transport, &buf16);
	    str->version = buf16;

	    if (str->version != 1)
		return DE_UNSUPPORTED_VERSION;

	    stream_get16(str->transport, &buf16);
	    str->chunk_size = buf16;
	    str->buffer = malloc(buf16);
	    if (!str->buffer)
		return ENOMEM;

	    stream_get16(str->transport, &buf16);
	    str->chunk_count = buf16;

	    if (str->chunk_count == 0)
		return DE_BAD_HEADER;

	    str->chunk = calloc(str->chunk_count, sizeof(str->chunk[0]));

	    for (i = 0; i < str->chunk_count; i++) {
		stream_get16(str->transport, &buf16);
		str->chunk[i].length = buf16;
	    }
	    str->type = DICTORG_DZIP;
	} else
	    dico_stream_seek(str->transport, str->header_length,
			     DICO_SEEK_SET);
    }

    str->orig_name = NULL;
    if (str->flags & GZ_FNAME) {
	size_t size = 0, rdbytes;
	dico_stream_getdelim(str->transport, &str->orig_name, &size,
			     0, &rdbytes);
	str->header_length += rdbytes;
    } else
	str->orig_name = NULL;

    str->comment = NULL;
    if (str->flags & GZ_COMMENT) {
	size_t size = 0, rdbytes;
	dico_stream_getdelim(str->transport, &str->comment, &size,
			     0, &rdbytes);
	str->header_length += rdbytes;
    }

    if (str->flags & GZ_FHCRC) {
	dico_stream_seek(str->transport, 2, DICO_SEEK_CUR);
	str->header_length += 2;
    }

    pos = dico_stream_seek(str->transport, 0, DICO_SEEK_CUR);
    if (pos != str->header_length + 1) {
	dico_log(L_ERR, 0, _("file position (%lu) != header length + 1 (%lu)"),
		 (unsigned long) pos,
		 (unsigned long) (str->header_length + 1));
	return DE_BAD_HEADER;
    }

    offset = str->header_length + 1;
    for (i = 0; i < str->chunk_count; i++) {
	str->chunk[i].offset = offset;
	offset += str->chunk[i].length;
    }

    dico_stream_seek(str->transport, -8, DICO_SEEK_END);

    stream_get32(str->transport, &buf32);
    str->crc = buf32;

    stream_get32(str->transport, &buf32);
    str->size = buf32;
    str->compressed_size = dico_stream_seek(str->transport, 0, DICO_SEEK_CUR);

    if (dico_stream_last_error(str->transport))
	return DE_BAD_HEADER;
    return 0;
}

static const char *
_dict_strerror(void *data, int rc)
{
    struct _dict_stream *str = data;

    if (str->transport_error) {
	str->transport_error = 0;
	return dico_stream_strerror(str->transport, rc);
    }

    switch (rc) {
    case DE_UNKNOWN_FORMAT:
	return _("unknown dictionary format");

    case DE_UNSUPPORTED_FORMAT:
	return _("unsupported dictionary format");

    case DE_UNSUPPORTED_VERSION:
	return _("unsupported dictionary version");

    case DE_BAD_HEADER:
	return _("malformed header");

    case DE_GZIP_SEEK:
	return _("cannot seek on pure gzip format files");

    case DE_INFLATE:
	return _("error decompressing stream");

    default:
	return strerror(rc);
    }
}

static int
_dict_read_text(struct _dict_stream *str, char *buf, size_t size, size_t *pret)
{
    if (dico_stream_read(str->transport, buf, size, pret))
	return str->transport_error = dico_stream_last_error(str->transport);
    return 0;
}

static int
_dict_read(void *data, char *buf, size_t size, size_t *pret)
{
    struct _dict_stream *str = data;

    switch (str->type) {
    case DICTORG_UNKNOWN:
	return DE_UNKNOWN_FORMAT;

    case DICTORG_TEXT:
	return _dict_read_text(str, buf, size, pret);

    case DICTORG_GZIP:
	return DE_GZIP_SEEK;

    case DICTORG_DZIP:
	return _dict_read_dzip(str, buf, size, pret);
    }
    return DE_UNSUPPORTED_FORMAT;
}

static int
_dict_seek_text(struct _dict_stream *str, off_t needle, int whence,
		off_t *presult)
{
    off_t off = dico_stream_seek(str->transport, needle, whence);
    if (off < 0)
	return str->transport_error = dico_stream_last_error(str->transport);
    *presult = off;
    return 0;
}

static int
_dict_seek(void *data, off_t needle, int whence, off_t *presult)
{
    struct _dict_stream *str = data;
    switch (str->type) {
    case DICTORG_UNKNOWN:
	return DE_UNKNOWN_FORMAT;

    case DICTORG_TEXT:
	return _dict_seek_text(str, needle, whence, presult);

    case DICTORG_GZIP:
	return DE_GZIP_SEEK;

    case DICTORG_DZIP:
	return _dict_seek_dzip(str, needle, whence, presult);
    }
    return DE_UNSUPPORTED_FORMAT;
}

dico_stream_t
dict_stream_create(const char *filename, size_t cache_size)
{
    int rc;
    dico_stream_t str;
    struct _dict_stream *s = malloc(sizeof(*s));

    if (!s)
	return NULL;
    rc = dico_stream_create(&str, DICO_STREAM_READ|DICO_STREAM_SEEK, s);
    if (rc) {
	free(s);
	return NULL;
    }

    memset(s, 0, sizeof(*s));
    s->type = DICTORG_UNKNOWN;
    s->cache_size = cache_size ? cache_size : 10;
    s->transport = dico_mapfile_stream_create(filename,
					     DICO_STREAM_READ|DICO_STREAM_SEEK);
    dico_stream_set_open(str, _dict_open);
    dico_stream_set_read(str, _dict_read);
    dico_stream_set_seek(str, _dict_seek);
    dico_stream_set_close(str, _dict_close);
    dico_stream_set_destroy(str, _dict_destroy);
    dico_stream_set_error_string(str, _dict_strerror);

    return str;
}