File: pma_common.c

package info (click to toggle)
lhasa 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,364 kB
  • sloc: ansic: 6,796; sh: 1,938; makefile: 168; python: 129
file content (161 lines) | stat: -rw-r--r-- 4,182 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
/*

Copyright (c) 2011, 2012, Simon Howard

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR 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, DIRECT, 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.

 */

//
// Common functions used by PMarc decoders.
//

typedef struct {
	unsigned int offset;
	unsigned int bits;
} VariableLengthTable;

// Read a variable length code, given the header bits already read.
// Returns the decoded value, or -1 for error.

static int decode_variable_length(BitStreamReader *reader,
                                  const VariableLengthTable *table,
                                  unsigned int header)
{
	int value;

	value = read_bits(reader, table[header].bits);

	if (value < 0) {
		return -1;
	}

	return (int) table[header].offset + value;
}

typedef struct {
	uint8_t prev;
	uint8_t next;
} HistoryNode;

// History linked list. In the decode stream, codes representing
// characters are not the character itself, but the number of
// nodes to count back in time in the linked list. Every time
// a character is output, it is moved to the front of the linked
// list. The entry point index into the list is the last output
// character, given by history_head;

typedef struct {
	HistoryNode history[256];
	uint8_t history_head;
} HistoryLinkedList;

// Initialize the history buffer.

static void init_history_list(HistoryLinkedList *list)
{
	unsigned int i;

	// History buffer is initialized to a linear chain to
	// start off with:

	for (i = 0; i < 256; ++i) {
		list->history[i].prev = (uint8_t) (i + 1);
		list->history[i].next = (uint8_t) (i - 1);
	}

	// The chain is cut into groups and initially arranged so
	// that the ASCII characters are closest to the start of
	// the chain. This is followed by ASCII control characters,
	// then various other groups.

	list->history_head = 0x20;

	list->history[0x7f].prev = 0x00;  // 0x20 ... 0x7f -> 0x00
	list->history[0x00].next = 0x7f;

	list->history[0x1f].prev = 0xa0;  // 0x00 ... 0x1f -> 0xa0
	list->history[0xa0].next = 0x1f;

	list->history[0xdf].prev = 0x80;  // 0xa0 ... 0xdf -> 0x80
	list->history[0x80].next = 0xdf;

	list->history[0x9f].prev = 0xe0;  // 0x80 ... 0x9f -> 0xe0
	list->history[0xe0].next = 0x9f;

	list->history[0xff].prev = 0x20;  // 0xe0 ... 0xff -> 0x20
	list->history[0x20].next = 0xff;
}

// Look up an entry in the history list, returning the code found.

static uint8_t find_in_history_list(HistoryLinkedList *list, uint8_t count)
{
	unsigned int i;
	uint8_t code;

	// Start from the last outputted byte.

	code = list->history_head;

	// Walk along the history chain until we reach the desired
	// node.  If we will have to walk more than half the chain,
	// go the other way around.

	if (count < 128) {
		for (i = 0; i < count; ++i) {
			code = list->history[code].prev;
		}
	} else {
		for (i = 0; i < 256U - count; ++i) {
			code = list->history[code].next;
		}
	}

	return code;
}

// Update history list, by moving the specified byte to the head
// of the queue.

static void update_history_list(HistoryLinkedList *list, uint8_t b)
{
	HistoryNode *node, *old_head;

	// No update necessary?

	if (list->history_head == b) {
		return;
	}

	// Unhook the entry from its current position:

	node = &list->history[b];
	list->history[node->next].prev = node->prev;
	list->history[node->prev].next = node->next;

	// Hook in between the old head and old_head->next:

	old_head = &list->history[list->history_head];
	node->prev = list->history_head;
	node->next = old_head->next;

	list->history[old_head->next].prev = b;
	old_head->next = b;

	// 'b' is now the head of the queue:

	list->history_head = b;
}