annotate lib/bunzip.c @ 1172:db517206d752 draft

Cosmetic tweak.
author Rob Landley <rob@landley.net>
date Sat, 28 Dec 2013 17:06:55 -0600
parents 12246ab3fd9c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* micro-bunzip, a small, simple bzip2 decompression implementation.
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
2 *
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
3 * Copyright 2003, 2006 by Rob Landley (rob@landley.net).
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
4 *
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
5 * Based on a close reading (but not the actual code) of the original bzip2
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
6 * decompression code by Julian R Seward (jseward@acm.org), which also
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
7 * acknowledges contributions by Mike Burrows, David Wheeler, Peter Fenwick,
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
8 * Alistair Moffat, Radford Neal, Ian H. Witten, Robert Sedgewick, and
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
9 * Jon L. Bentley.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
10 */
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
11
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
12 #include "toys.h"
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
13
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
14 #define THREADS 1
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
15
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
16 // Constants for huffman coding
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
17 #define MAX_GROUPS 6
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
18 #define GROUP_SIZE 50 /* 64 would have been more efficient */
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
19 #define MAX_HUFCODE_BITS 20 /* Longest huffman code allowed */
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
20 #define MAX_SYMBOLS 258 /* 256 literals + RUNA + RUNB */
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
21 #define SYMBOL_RUNA 0
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
22 #define SYMBOL_RUNB 1
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
23
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
24 // Other housekeeping constants
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
25 #define IOBUF_SIZE 4096
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
26
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
27 // Status return values
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
28 #define RETVAL_LAST_BLOCK (-100)
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
29 #define RETVAL_NOT_BZIP_DATA (-1)
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
30 #define RETVAL_DATA_ERROR (-2)
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
31 #define RETVAL_OBSOLETE_INPUT (-3)
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
32
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
33 // This is what we know about each huffman coding group
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
34 struct group_data {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
35 int limit[MAX_HUFCODE_BITS+1], base[MAX_HUFCODE_BITS], permute[MAX_SYMBOLS];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
36 char minLen, maxLen;
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
37 };
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
38
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
39 // Data for burrows wheeler transform
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
40
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
41 struct bwdata {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
42 unsigned int origPtr;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
43 int byteCount[256];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
44 // State saved when interrupting output
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
45 int writePos, writeRun, writeCount, writeCurrent;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
46 unsigned int dataCRC, headerCRC;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
47 unsigned int *dbuf;
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
48 };
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
49
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
50 // Structure holding all the housekeeping data, including IO buffers and
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
51 // memory that persists between calls to bunzip
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
52 struct bunzip_data {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
53 // Input stream, input buffer, input bit buffer
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
54 int in_fd, inbufCount, inbufPos;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
55 char *inbuf;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
56 unsigned int inbufBitCount, inbufBits;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
57
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
58 // Output buffer
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
59 char outbuf[IOBUF_SIZE];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
60 int outbufPos;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
61
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
62 unsigned int totalCRC;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
63
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
64 // First pass decompression data (Huffman and MTF decoding)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
65 char selectors[32768]; // nSelectors=15 bits
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
66 struct group_data groups[MAX_GROUPS]; // huffman coding tables
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
67 int symTotal, groupCount, nSelectors;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
68 unsigned char symToByte[256], mtfSymbol[256];
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
69
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
70 // The CRC values stored in the block header and calculated from the data
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
71 unsigned int crc32Table[256];
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
72
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
73 // Second pass decompression data (burrows-wheeler transform)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
74 unsigned int dbufSize;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
75 struct bwdata bwdata[THREADS];
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
76 };
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
77
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
78 // Return the next nnn bits of input. All reads from the compressed input
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
79 // are done through this function. All reads are big endian.
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
80 static unsigned int get_bits(struct bunzip_data *bd, char bits_wanted)
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
81 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
82 unsigned int bits = 0;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
83
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
84 // If we need to get more data from the byte buffer, do so. (Loop getting
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
85 // one byte at a time to enforce endianness and avoid unaligned access.)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
86 while (bd->inbufBitCount < bits_wanted) {
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
87
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
88 // If we need to read more data from file into byte buffer, do so
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
89 if (bd->inbufPos == bd->inbufCount) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
90 if (0 >= (bd->inbufCount = read(bd->in_fd, bd->inbuf, IOBUF_SIZE)))
881
2945e3cbebf2 Eliminate leaked global bunzip_errors.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
91 error_exit("input EOF");
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
92 bd->inbufPos = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
93 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
94
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
95 // Avoid 32-bit overflow (dump bit buffer to top of output)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
96 if (bd->inbufBitCount>=24) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
97 bits = bd->inbufBits&((1<<bd->inbufBitCount)-1);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
98 bits_wanted -= bd->inbufBitCount;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
99 bits <<= bits_wanted;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
100 bd->inbufBitCount = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
101 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
102
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
103 // Grab next 8 bits of input from buffer.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
104 bd->inbufBits = (bd->inbufBits<<8) | bd->inbuf[bd->inbufPos++];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
105 bd->inbufBitCount += 8;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
106 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
107
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
108 // Calculate result
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
109 bd->inbufBitCount -= bits_wanted;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
110 bits |= (bd->inbufBits>>bd->inbufBitCount) & ((1<<bits_wanted)-1);
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
111
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
112 return bits;
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
113 }
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
114
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
115 /* Read block header at start of a new compressed data block. Consists of:
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
116 *
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
117 * 48 bits : Block signature, either pi (data block) or e (EOF block).
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
118 * 32 bits : bw->headerCRC
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
119 * 1 bit : obsolete feature flag.
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
120 * 24 bits : origPtr (Burrows-wheeler unwind index, only 20 bits ever used)
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
121 * 16 bits : Mapping table index.
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
122 *[16 bits]: symToByte[symTotal] (Mapping table. For each bit set in mapping
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
123 * table index above, read another 16 bits of mapping table data.
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
124 * If correspondig bit is unset, all bits in that mapping table
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
125 * section are 0.)
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
126 * 3 bits : groupCount (how many huffman tables used to encode, anywhere
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
127 * from 2 to MAX_GROUPS)
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
128 * variable: hufGroup[groupCount] (MTF encoded huffman table data.)
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
129 */
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
130
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
131 static int read_block_header(struct bunzip_data *bd, struct bwdata *bw)
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
132 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
133 struct group_data *hufGroup;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
134 int hh, ii, jj, kk, symCount, *base, *limit;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
135 unsigned char uc;
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
136
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
137 // Read in header signature and CRC (which is stored big endian)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
138 ii = get_bits(bd, 24);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
139 jj = get_bits(bd, 24);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
140 bw->headerCRC = get_bits(bd,32);
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
141
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
142 // Is this the EOF block with CRC for whole file? (Constant is "e")
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
143 if (ii==0x177245 && jj==0x385090) return RETVAL_LAST_BLOCK;
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
144
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
145 // Is this a valid data block? (Constant is "pi".)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
146 if (ii!=0x314159 || jj!=0x265359) return RETVAL_NOT_BZIP_DATA;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
147
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
148 // We can add support for blockRandomised if anybody complains.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
149 if (get_bits(bd,1)) return RETVAL_OBSOLETE_INPUT;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
150 if ((bw->origPtr = get_bits(bd,24)) > bd->dbufSize) return RETVAL_DATA_ERROR;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
151
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
152 // mapping table: if some byte values are never used (encoding things
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
153 // like ascii text), the compression code removes the gaps to have fewer
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
154 // symbols to deal with, and writes a sparse bitfield indicating which
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
155 // values were present. We make a translation table to convert the symbols
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
156 // back to the corresponding bytes.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
157 hh = get_bits(bd, 16);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
158 bd->symTotal = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
159 for (ii=0; ii<16; ii++) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
160 if (hh & (1 << (15 - ii))) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
161 kk = get_bits(bd, 16);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
162 for (jj=0; jj<16; jj++)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
163 if (kk & (1 << (15 - jj)))
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
164 bd->symToByte[bd->symTotal++] = (16 * ii) + jj;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
165 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
166 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
167
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
168 // How many different huffman coding groups does this block use?
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
169 bd->groupCount = get_bits(bd,3);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
170 if (bd->groupCount<2 || bd->groupCount>MAX_GROUPS) return RETVAL_DATA_ERROR;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
171
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
172 // nSelectors: Every GROUP_SIZE many symbols we switch huffman coding
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
173 // tables. Each group has a selector, which is an index into the huffman
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
174 // coding table arrays.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
175 //
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
176 // Read in the group selector array, which is stored as MTF encoded
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
177 // bit runs. (MTF = Move To Front. Every time a symbol occurs it's moved
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
178 // to the front of the table, so it has a shorter encoding next time.)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
179 if (!(bd->nSelectors = get_bits(bd, 15))) return RETVAL_DATA_ERROR;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
180 for (ii=0; ii<bd->groupCount; ii++) bd->mtfSymbol[ii] = ii;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
181 for (ii=0; ii<bd->nSelectors; ii++) {
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
182
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
183 // Get next value
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
184 for(jj=0;get_bits(bd,1);jj++)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
185 if (jj>=bd->groupCount) return RETVAL_DATA_ERROR;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
186
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
187 // Decode MTF to get the next selector, and move it to the front.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
188 uc = bd->mtfSymbol[jj];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
189 memmove(bd->mtfSymbol+1, bd->mtfSymbol, jj);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
190 bd->mtfSymbol[0] = bd->selectors[ii] = uc;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
191 }
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
192
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
193 // Read the huffman coding tables for each group, which code for symTotal
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
194 // literal symbols, plus two run symbols (RUNA, RUNB)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
195 symCount = bd->symTotal+2;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
196 for (jj=0; jj<bd->groupCount; jj++) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
197 unsigned char length[MAX_SYMBOLS];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
198 unsigned temp[MAX_HUFCODE_BITS+1];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
199 int minLen, maxLen, pp;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
200
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
201 // Read lengths
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
202 hh = get_bits(bd, 5);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
203 for (ii = 0; ii < symCount; ii++) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
204 for(;;) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
205 // !hh || hh > MAX_HUFCODE_BITS in one test.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
206 if (MAX_HUFCODE_BITS-1 < (unsigned)hh-1) return RETVAL_DATA_ERROR;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
207 // Grab 2 bits instead of 1 (slightly smaller/faster). Stop if
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
208 // first bit is 0, otherwise second bit says whether to
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
209 // increment or decrement.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
210 kk = get_bits(bd, 2);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
211 if (kk & 2) hh += 1 - ((kk&1)<<1);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
212 else {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
213 bd->inbufBitCount++;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
214 break;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
215 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
216 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
217 length[ii] = hh;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
218 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
219
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
220 // Find largest and smallest lengths in this group
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
221 minLen = maxLen = length[0];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
222 for (ii = 1; ii < symCount; ii++) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
223 if(length[ii] > maxLen) maxLen = length[ii];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
224 else if(length[ii] < minLen) minLen = length[ii];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
225 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
226
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
227 /* Calculate permute[], base[], and limit[] tables from length[].
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
228 *
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
229 * permute[] is the lookup table for converting huffman coded symbols
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
230 * into decoded symbols. It contains symbol values sorted by length.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
231 *
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
232 * base[] is the amount to subtract from the value of a huffman symbol
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
233 * of a given length when using permute[].
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
234 *
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
235 * limit[] indicates the largest numerical value a symbol with a given
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
236 * number of bits can have. It lets us know when to stop reading.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
237 *
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
238 * To use these, keep reading bits until value <= limit[bitcount] or
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
239 * you've read over 20 bits (error). Then the decoded symbol
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
240 * equals permute[hufcode_value - base[hufcode_bitcount]].
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
241 */
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
242 hufGroup = bd->groups+jj;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
243 hufGroup->minLen = minLen;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
244 hufGroup->maxLen = maxLen;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
245
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
246 // Note that minLen can't be smaller than 1, so we adjust the base
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
247 // and limit array pointers so we're not always wasting the first
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
248 // entry. We do this again when using them (during symbol decoding).
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
249 base = hufGroup->base-1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
250 limit = hufGroup->limit-1;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
251
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
252 // zero temp[] and limit[], and calculate permute[]
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
253 pp = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
254 for (ii = minLen; ii <= maxLen; ii++) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
255 temp[ii] = limit[ii] = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
256 for (hh = 0; hh < symCount; hh++)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
257 if (length[hh] == ii) hufGroup->permute[pp++] = hh;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
258 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
259
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
260 // Count symbols coded for at each bit length
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
261 for (ii = 0; ii < symCount; ii++) temp[length[ii]]++;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
262
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
263 /* Calculate limit[] (the largest symbol-coding value at each bit
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
264 * length, which is (previous limit<<1)+symbols at this level), and
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
265 * base[] (number of symbols to ignore at each bit length, which is
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
266 * limit minus the cumulative count of symbols coded for already). */
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
267 pp = hh = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
268 for (ii = minLen; ii < maxLen; ii++) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
269 pp += temp[ii];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
270 limit[ii] = pp-1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
271 pp <<= 1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
272 base[ii+1] = pp-(hh+=temp[ii]);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
273 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
274 limit[maxLen] = pp+temp[maxLen]-1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
275 limit[maxLen+1] = INT_MAX;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
276 base[minLen] = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
277 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
278
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
279 return 0;
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
280 }
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
281
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
282 /* First pass, read block's symbols into dbuf[dbufCount].
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
283 *
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
284 * This undoes three types of compression: huffman coding, run length encoding,
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
285 * and move to front encoding. We have to undo all those to know when we've
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
286 * read enough input.
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
287 */
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
288
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
289 static int read_huffman_data(struct bunzip_data *bd, struct bwdata *bw)
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
290 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
291 struct group_data *hufGroup;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
292 int hh, ii, jj, kk, runPos, dbufCount, symCount, selector, nextSym,
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
293 *byteCount, *base, *limit;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
294 unsigned int *dbuf = bw->dbuf;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
295 unsigned char uc;
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
296
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
297 // We've finished reading and digesting the block header. Now read this
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
298 // block's huffman coded symbols from the file and undo the huffman coding
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
299 // and run length encoding, saving the result into dbuf[dbufCount++] = uc
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
300
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
301 // Initialize symbol occurrence counters and symbol mtf table
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
302 byteCount = bw->byteCount;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
303 for(ii=0; ii<256; ii++) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
304 byteCount[ii] = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
305 bd->mtfSymbol[ii] = ii;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
306 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
307
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
308 // Loop through compressed symbols. This is the first "tight inner loop"
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
309 // that needs to be micro-optimized for speed. (This one fills out dbuf[]
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
310 // linearly, staying in cache more, so isn't as limited by DRAM access.)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
311 runPos = dbufCount = symCount = selector = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
312 // Some unnecessary initializations to shut gcc up.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
313 base = limit = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
314 hufGroup = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
315 hh = 0;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
316
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
317 for (;;) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
318 // Have we reached the end of this huffman group?
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
319 if (!(symCount--)) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
320 // Determine which huffman coding group to use.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
321 symCount = GROUP_SIZE-1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
322 if (selector >= bd->nSelectors) return RETVAL_DATA_ERROR;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
323 hufGroup = bd->groups + bd->selectors[selector++];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
324 base = hufGroup->base-1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
325 limit = hufGroup->limit-1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
326 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
327
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
328 // Read next huffman-coded symbol (into jj).
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
329 ii = hufGroup->minLen;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
330 jj = get_bits(bd, ii);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
331 while (jj > limit[ii]) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
332 // if (ii > hufGroup->maxLen) return RETVAL_DATA_ERROR;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
333 ii++;
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
334
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
335 // Unroll get_bits() to avoid a function call when the data's in
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
336 // the buffer already.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
337 kk = bd->inbufBitCount
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
338 ? (bd->inbufBits >> --(bd->inbufBitCount)) & 1 : get_bits(bd, 1);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
339 jj = (jj << 1) | kk;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
340 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
341 // Huffman decode jj into nextSym (with bounds checking)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
342 jj-=base[ii];
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
343
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
344 if (ii > hufGroup->maxLen || (unsigned)jj >= MAX_SYMBOLS)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
345 return RETVAL_DATA_ERROR;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
346 nextSym = hufGroup->permute[jj];
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
347
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
348 // If this is a repeated run, loop collecting data
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
349 if ((unsigned)nextSym <= SYMBOL_RUNB) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
350 // If this is the start of a new run, zero out counter
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
351 if(!runPos) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
352 runPos = 1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
353 hh = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
354 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
355
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
356 /* Neat trick that saves 1 symbol: instead of or-ing 0 or 1 at
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
357 each bit position, add 1 or 2 instead. For example,
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
358 1011 is 1<<0 + 1<<1 + 2<<2. 1010 is 2<<0 + 2<<1 + 1<<2.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
359 You can make any bit pattern that way using 1 less symbol than
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
360 the basic or 0/1 method (except all bits 0, which would use no
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
361 symbols, but a run of length 0 doesn't mean anything in this
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
362 context). Thus space is saved. */
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
363 hh += (runPos << nextSym); // +runPos if RUNA; +2*runPos if RUNB
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
364 runPos <<= 1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
365 continue;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
366 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
367
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
368 /* When we hit the first non-run symbol after a run, we now know
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
369 how many times to repeat the last literal, so append that many
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
370 copies to our buffer of decoded symbols (dbuf) now. (The last
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
371 literal used is the one at the head of the mtfSymbol array.) */
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
372 if (runPos) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
373 runPos = 0;
1078
7b6fbc0475d8 It's not a problem to exactly fill up the buffer with a run if the next symbol is the terminating symbol. Fixes https://jira.cyanogenmod.org/browse/CYAN-1896
Rob Landley <rob@landley.net>
parents: 881
diff changeset
374 if (dbufCount+hh > bd->dbufSize) return RETVAL_DATA_ERROR;
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
375
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
376 uc = bd->symToByte[bd->mtfSymbol[0]];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
377 byteCount[uc] += hh;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
378 while (hh--) dbuf[dbufCount++] = uc;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
379 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
380
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
381 // Is this the terminating symbol?
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
382 if (nextSym>bd->symTotal) break;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
383
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
384 /* At this point, the symbol we just decoded indicates a new literal
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
385 character. Subtract one to get the position in the MTF array
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
386 at which this literal is currently to be found. (Note that the
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
387 result can't be -1 or 0, because 0 and 1 are RUNA and RUNB.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
388 Another instance of the first symbol in the mtf array, position 0,
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
389 would have been handled as part of a run.) */
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
390 if (dbufCount>=bd->dbufSize) return RETVAL_DATA_ERROR;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
391 ii = nextSym - 1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
392 uc = bd->mtfSymbol[ii];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
393 // On my laptop, unrolling this memmove() into a loop shaves 3.5% off
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
394 // the total running time.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
395 while(ii--) bd->mtfSymbol[ii+1] = bd->mtfSymbol[ii];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
396 bd->mtfSymbol[0] = uc;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
397 uc = bd->symToByte[uc];
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
398
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
399 // We have our literal byte. Save it into dbuf.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
400 byteCount[uc]++;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
401 dbuf[dbufCount++] = (unsigned int)uc;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
402 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
403
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
404 // Now we know what dbufCount is, do a better sanity check on origPtr.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
405 if (bw->origPtr >= (bw->writeCount = dbufCount)) return RETVAL_DATA_ERROR;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
406
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
407 return 0;
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
408 }
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
409
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
410 // Flush output buffer to disk
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
411 void flush_bunzip_outbuf(struct bunzip_data *bd, int out_fd)
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
412 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
413 if (bd->outbufPos) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
414 if (write(out_fd, bd->outbuf, bd->outbufPos) != bd->outbufPos)
881
2945e3cbebf2 Eliminate leaked global bunzip_errors.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
415 error_exit("output EOF");
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
416 bd->outbufPos = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
417 }
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
418 }
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
419
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
420 void burrows_wheeler_prep(struct bunzip_data *bd, struct bwdata *bw)
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
421 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
422 int ii, jj;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
423 unsigned int *dbuf = bw->dbuf;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
424 int *byteCount = bw->byteCount;
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
425
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
426 // Technically this part is preparation for the burrows-wheeler
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
427 // transform, but it's quick and convenient to do here.
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
428
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
429 // Turn byteCount into cumulative occurrence counts of 0 to n-1.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
430 jj = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
431 for (ii=0; ii<256; ii++) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
432 int kk = jj + byteCount[ii];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
433 byteCount[ii] = jj;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
434 jj = kk;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
435 }
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
436
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
437 // Use occurrence counts to quickly figure out what order dbuf would be in
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
438 // if we sorted it.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
439 for (ii=0; ii < bw->writeCount; ii++) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
440 unsigned char uc = dbuf[ii];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
441 dbuf[byteCount[uc]] |= (ii << 8);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
442 byteCount[uc]++;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
443 }
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
444
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
445 // blockRandomised support would go here.
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
446
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
447 // Using ii as position, jj as previous character, hh as current character,
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
448 // and uc as run count.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
449 bw->dataCRC = 0xffffffffL;
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
450
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
451 /* Decode first byte by hand to initialize "previous" byte. Note that it
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
452 doesn't get output, and if the first three characters are identical
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
453 it doesn't qualify as a run (hence uc=255, which will either wrap
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
454 to 1 or get reset). */
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
455 if (bw->writeCount) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
456 bw->writePos = dbuf[bw->origPtr];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
457 bw->writeCurrent = (unsigned char)bw->writePos;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
458 bw->writePos >>= 8;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
459 bw->writeRun = -1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
460 }
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
461 }
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
462
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
463 // Decompress a block of text to intermediate buffer
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
464 int read_bunzip_data(struct bunzip_data *bd)
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
465 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
466 int rc = read_block_header(bd, bd->bwdata);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
467 if (!rc) rc=read_huffman_data(bd, bd->bwdata);
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
468
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
469 // First thing that can be done by a background thread.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
470 burrows_wheeler_prep(bd, bd->bwdata);
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
471
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
472 return rc;
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
473 }
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
474
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
475 // Undo burrows-wheeler transform on intermediate buffer to produce output.
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
476 // If !len, write up to len bytes of data to buf. Otherwise write to out_fd.
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
477 // Returns len ? bytes written : 0. Notice all errors are negative #'s.
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
478 //
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
479 // Burrows-wheeler transform is described at:
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
480 // http://dogma.net/markn/articles/bwt/bwt.htm
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
481 // http://marknelson.us/1996/09/01/bwt/
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
482
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
483 int write_bunzip_data(struct bunzip_data *bd, struct bwdata *bw, int out_fd, char *outbuf, int len)
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
484 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
485 unsigned int *dbuf = bw->dbuf;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
486 int count, pos, current, run, copies, outbyte, previous, gotcount = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
487
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
488 for (;;) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
489 // If last read was short due to end of file, return last block now
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
490 if (bw->writeCount < 0) return bw->writeCount;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
491
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
492 // If we need to refill dbuf, do it.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
493 if (!bw->writeCount) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
494 int i = read_bunzip_data(bd);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
495 if (i) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
496 if (i == RETVAL_LAST_BLOCK) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
497 bw->writeCount = i;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
498 return gotcount;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
499 } else return i;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
500 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
501 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
502
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
503 // loop generating output
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
504 count = bw->writeCount;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
505 pos = bw->writePos;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
506 current = bw->writeCurrent;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
507 run = bw->writeRun;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
508 while (count) {
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
509
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
510 // If somebody (like tar) wants a certain number of bytes of
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
511 // data from memory instead of written to a file, humor them.
1080
12246ab3fd9c Whitespace tweaks and remove unused variable.
Rob Landley <rob@landley.net>
parents: 1078
diff changeset
512 if (len && bd->outbufPos >= len) goto dataus_interruptus;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
513 count--;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
514
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
515 // Follow sequence vector to undo Burrows-Wheeler transform.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
516 previous = current;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
517 pos = dbuf[pos];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
518 current = pos&0xff;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
519 pos >>= 8;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
520
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
521 // Whenever we see 3 consecutive copies of the same byte,
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
522 // the 4th is a repeat count
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
523 if (run++ == 3) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
524 copies = current;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
525 outbyte = previous;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
526 current = -1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
527 } else {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
528 copies = 1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
529 outbyte = current;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
530 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
531
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
532 // Output bytes to buffer, flushing to file if necessary
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
533 while (copies--) {
1080
12246ab3fd9c Whitespace tweaks and remove unused variable.
Rob Landley <rob@landley.net>
parents: 1078
diff changeset
534 if (bd->outbufPos == IOBUF_SIZE) flush_bunzip_outbuf(bd, out_fd);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
535 bd->outbuf[bd->outbufPos++] = outbyte;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
536 bw->dataCRC = (bw->dataCRC << 8)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
537 ^ bd->crc32Table[(bw->dataCRC >> 24) ^ outbyte];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
538 }
1080
12246ab3fd9c Whitespace tweaks and remove unused variable.
Rob Landley <rob@landley.net>
parents: 1078
diff changeset
539 if (current != previous) run=0;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
540 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
541
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
542 // decompression of this block completed successfully
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
543 bw->dataCRC = ~(bw->dataCRC);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
544 bd->totalCRC = ((bd->totalCRC << 1) | (bd->totalCRC >> 31)) ^ bw->dataCRC;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
545
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
546 // if this block had a crc error, force file level crc error.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
547 if (bw->dataCRC != bw->headerCRC) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
548 bd->totalCRC = bw->headerCRC+1;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
549
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
550 return RETVAL_LAST_BLOCK;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
551 }
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
552 dataus_interruptus:
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
553 bw->writeCount = count;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
554 if (len) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
555 gotcount += bd->outbufPos;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
556 memcpy(outbuf, bd->outbuf, len);
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
557
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
558 // If we got enough data, checkpoint loop state and return
1080
12246ab3fd9c Whitespace tweaks and remove unused variable.
Rob Landley <rob@landley.net>
parents: 1078
diff changeset
559 if ((len -= bd->outbufPos)<1) {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
560 bd->outbufPos -= len;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
561 if (bd->outbufPos) memmove(bd->outbuf, bd->outbuf+len, bd->outbufPos);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
562 bw->writePos = pos;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
563 bw->writeCurrent = current;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
564 bw->writeRun = run;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
565
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
566 return gotcount;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
567 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
568 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
569 }
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
570 }
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
571
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
572 // Allocate the structure, read file header. If !len, src_fd contains
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
573 // filehandle to read from. Else inbuf contains data.
215
34e36dffd47a Major refactoring of bunzip.c in preparation for doing a multi-threaded version.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
574 int start_bunzip(struct bunzip_data **bdp, int src_fd, char *inbuf, int len)
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
575 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
576 struct bunzip_data *bd;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
577 unsigned int i;
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
578
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
579 // Figure out how much data to allocate.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
580 i = sizeof(struct bunzip_data);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
581 if (!len) i += IOBUF_SIZE;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
582
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
583 // Allocate bunzip_data. Most fields initialize to zero.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
584 bd = *bdp = xzalloc(i);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
585 if (len) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
586 bd->inbuf = inbuf;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
587 bd->inbufCount = len;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
588 bd->in_fd = -1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
589 } else {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
590 bd->inbuf = (char *)(bd+1);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
591 bd->in_fd = src_fd;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
592 }
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
593
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
594 crc_init(bd->crc32Table, 0);
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
595
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
596 // Ensure that file starts with "BZh".
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
597 for (i=0;i<3;i++) if (get_bits(bd,8)!="BZh"[i]) return RETVAL_NOT_BZIP_DATA;
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
598
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
599 // Next byte ascii '1'-'9', indicates block size in units of 100k of
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
600 // uncompressed data. Allocate intermediate buffer for block.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
601 i = get_bits(bd, 8);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
602 if (i<'1' || i>'9') return RETVAL_NOT_BZIP_DATA;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
603 bd->dbufSize = 100000*(i-'0')*THREADS;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
604 for (i=0; i<THREADS; i++)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
605 bd->bwdata[i].dbuf = xmalloc(bd->dbufSize * sizeof(int));
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
606
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
607 return 0;
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
608 }
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
609
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
610 // Example usage: decompress src_fd to dst_fd. (Stops at end of bzip data,
39
00e73c4dea66 More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents: 37
diff changeset
611 // not end of file.)
64
67ee3a0b76e1 In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents: 62
diff changeset
612 void bunzipStream(int src_fd, int dst_fd)
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
613 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
614 struct bunzip_data *bd;
881
2945e3cbebf2 Eliminate leaked global bunzip_errors.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
615 char *bunzip_errors[]={NULL, "not bzip", "bad data", "old format"};
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
616 int i, j;
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
617
1080
12246ab3fd9c Whitespace tweaks and remove unused variable.
Rob Landley <rob@landley.net>
parents: 1078
diff changeset
618 if (!(i = start_bunzip(&bd,src_fd, 0, 0))) {
12246ab3fd9c Whitespace tweaks and remove unused variable.
Rob Landley <rob@landley.net>
parents: 1078
diff changeset
619 i = write_bunzip_data(bd,bd->bwdata, dst_fd, 0, 0);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
620 if (i==RETVAL_LAST_BLOCK && bd->bwdata[0].headerCRC==bd->totalCRC) i = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
621 }
1080
12246ab3fd9c Whitespace tweaks and remove unused variable.
Rob Landley <rob@landley.net>
parents: 1078
diff changeset
622 flush_bunzip_outbuf(bd, dst_fd);
12246ab3fd9c Whitespace tweaks and remove unused variable.
Rob Landley <rob@landley.net>
parents: 1078
diff changeset
623
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
624 for (j=0; j<THREADS; j++) free(bd->bwdata[j].dbuf);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
625 free(bd);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
626 if (i) error_exit(bunzip_errors[-i]);
37
14d27eab3acf Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff changeset
627 }