File: decode.c

package info (click to toggle)
zoo 2.10-9
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 780 kB
  • ctags: 1,288
  • sloc: ansic: 9,041; asm: 793; makefile: 211
file content (69 lines) | stat: -rw-r--r-- 1,444 bytes parent folder | download | duplicates (9)
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
/*$Source: /usr/home/dhesi/zoo/RCS/decode.c,v $*/
/*$Id: decode.c,v 1.6 91/07/09 01:39:49 dhesi Exp $*/
/***********************************************************
	decode.c

Adapted from "ar" archiver written by Haruhiko Okumura.
***********************************************************/

#include "options.h"
#include "zoo.h"
#include "ar.h"
#include "lzh.h"

extern int decoded;		/* from huf.c */

static int j;  /* remaining bytes to copy */

void decode_start()
{
	huf_decode_start();
	j = 0;
	decoded = 0;
}

/*
decodes; returns no. of chars decoded 
*/

int decode(count, buffer)
uint count;
uchar buffer[];
	/* The calling function must keep the number of
	   bytes to be processed.  This function decodes
	   either 'count' bytes or 'DICSIZ' bytes, whichever
	   is smaller, into the array 'buffer[]' of size
	   'DICSIZ' or more.
	   Call decode_start() once for each new file
	   before calling this function. */
{
	static uint i;
	uint r, c;

	r = 0;
	while (--j >= 0) {
		buffer[r] = buffer[i];
		i = (i + 1) & (DICSIZ - 1);
		if (++r == count)
			return r;
	}
	for ( ; ; ) {
		c = decode_c();
		if (decoded)
			return r;
		if (c <= UCHAR_MAX) {
			buffer[r] = c;
			if (++r == count) 
				return r;
		} else {
			j = c - (UCHAR_MAX + 1 - THRESHOLD);
			i = (r - decode_p() - 1) & (DICSIZ - 1);
			while (--j >= 0) {
				buffer[r] = buffer[i];
				i = (i + 1) & (DICSIZ - 1);
				if (++r == count)
					return r;
			}
		}
	}
}