~kris/9p

9hist

ref: d756a8a5fa47893021c604ed818a4fa2e1fcb857 9hist/port/thwack.h -rw-r--r-- 2.0 KiB
d756a8a5 — David du Colombier Plan 9 from Bell Labs 1999-10-19 26 years ago
                                                                                
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
typedef struct Thwack		Thwack;
typedef struct Unthwack		Unthwack;
typedef struct ThwBlock		ThwBlock;
typedef struct UnthwBlock	UnthwBlock;

enum
{
	ThwMaxBlock	= 1600,		/* max size of compressible block */

	MinMatch	= 3,		/* shortest match possible */
	HashLog		= 10,
	HashSize	= 1<<HashLog,
	HashMask	= HashSize - 1,

	MaxFastLen	= 9,
	BigLenCode	= 0xf4,		/* minimum code for large lenth encoding */
	BigLenBase	= 4,		/* starting items to encode for big lens */
	BigLenBits	= 8,
	MaxOff		= 8,
	OffBase		= 6,

	MinDecode	= 9,		/* minimum bits to decode a match or lit */
	MaxOffDecode	= 4 + MaxOff + OffBase - 1,
	MaxLenDecode	= 16,

	EWinBlocks	= 32,		/* blocks held in encoder window */
	DWinBlocks	= 32,		/* blocks held in decoder window */
	CompBlocks	= 5,		/* max blocks used to encode data */

	MaxSeqMask	= 8,		/* number of bits in coding block mask */
	MaxSeqStart	= 256,		/* max offset of initial coding block */
};

struct ThwBlock
{
	ulong	seq;			/* sequence number for this data */
	uchar	acked;			/* ok to use this block; the decoder has it */
	ushort	begin;			/* time of first byte in hash */
	uchar	*edata;			/* last byte of valid data */
	ushort	maxoff;			/* time of last valid hash entry */
	ushort	*hash;
	uchar	*data;
};

struct Thwack
{
	ulong		nbits;		/* output bit buffer */
	ulong		bits;
	uchar		*dst;		/* output buffer */
	uchar		*dmax;

	int		slot;		/* next block to use */
	ThwBlock	blocks[EWinBlocks];
	ushort		hash[EWinBlocks][HashSize];
	uchar		data[EWinBlocks][ThwMaxBlock];
};

struct UnthwBlock
{
	ulong	seq;			/* sequence number for this data */
	ushort	maxoff;			/* valid data in each block */
	uchar	*data;
};

struct Unthwack
{
	int		slot;		/* next block to use */
	UnthwBlock	blocks[DWinBlocks];
	uchar		data[DWinBlocks][ThwMaxBlock];
};

void	thwackinit(Thwack*);
void	unthwackinit(Unthwack*);
int	thwack(Thwack*, uchar *dst, uchar *src, int nsrc, ulong seq);
void	thwackack(Thwack*, ulong seq, ulong mask);
int	unthwack(Unthwack*, uchar *dst, int ndst, uchar *src, int nsrc, ulong seq);