#include <sys/stat.h>
#include "ar.h"
-static unsigned long
-store(FILE *rfp, FILE *wfp, unsigned int *crc)
+static off_t
+store(FILE *rfp, FILE *wfp, uint16_t *crc)
{
- uint n;
+ size_t n;
char buf[MAXDICSIZ];
- unsigned long sz = 0;
+ off_t sz = 0;
*crc = INIT_CRC;
while ((n = fread(buf, 1, sizeof(buf), rfp)) != 0) {
add_1(struct lzh_ostream *wp, int replace_flag, struct lzh_header *h)
{
long headerpos, arcpos, arcendpos;
- uint r;
+ unsigned int r;
FILE *rfp;
if ((rfp = fopen(h->filename, "rb")) == NULL) {
exit(0);
}
-uint
-ratio(ulong a, ulong b)
+unsigned int
+ratio(off_t a, off_t b)
{ /* [(1000a + [b/2]) / b] */
int i;
for (i = 0; i < 3; i++)
- if (a <= ULONG_MAX / 10)
+ if (a <= ULONG_MAX / 10) /* XXX: should use max of off_t instead of ULONG_MAX */
a *= 10;
else
b /= 10;
- if ((ulong) (a + (b >> 1)) < a) {
+ if ((a + (b >> 1)) < a) {
a >>= 1;
b >>= 1;
}
if (b == 0)
return 0;
- return (uint) ((a + (b >> 1)) / b);
+ return (unsigned int) ((a + (b >> 1)) / b);
}
void
copy(FILE *arcfile, FILE *outfile, struct lzh_header *h)
{
char buf[BUFSIZ];
- unsigned long remainder = h->compsize;
+ off_t remainder = h->compsize;
write_header(outfile, h);
while (remainder != 0) {
- uint n = (uint) MIN(remainder, sizeof(buf));
+ size_t n = (size_t) MIN(remainder, sizeof(buf));
- if (fread((char *) buf, 1, n, arcfile) != n)
+ if (fread(buf, 1, n, arcfile) != n)
error("Can't read");
- if (fwrite((char *) buf, 1, n, outfile) != n)
+ if (fwrite(buf, 1, n, outfile) != n)
error("Can't write");
remainder -= n;
}
#endif
#define TBIT 5 /* smallest integer such that (1U << TBIT) > NT */
-typedef unsigned char uchar; /* 8 bits or more */
-typedef unsigned int uint; /* 16 bits or more */
-typedef unsigned short ushort; /* 16 bits or more */
-typedef unsigned long ulong; /* 32 bits or more */
-
struct lzh_istream {
FILE *fp;
- unsigned long compsize;
+ off_t compsize;
- unsigned short bitbuf;
- unsigned char subbitbuf;
+ uint16_t bitbuf;
+ uint8_t subbitbuf;
int bitcount;
/* huf.c */
unsigned int blocksize;
- unsigned char c_len[NC];
- unsigned short c_table[4096];
- unsigned char p_len[NP];
- unsigned short p_table[256];
+ uint8_t c_len[NC];
+ uint16_t c_table[4096];
+ uint8_t p_len[NP];
+ uint16_t p_table[256];
int np;
int pbit;
struct lzh_ostream {
FILE *fp;
- unsigned long compsize;
- unsigned long origsize;
+ off_t compsize;
+ off_t origsize;
int unpackable;
- unsigned char subbitbuf;
+ uint8_t subbitbuf;
int bitcount;
- unsigned int crc;
+ uint16_t crc;
/* huf.c */
- unsigned char *buf;
- unsigned int bufsiz;
+ uint8_t *buf;
+ size_t bufsiz;
/* huf.c:output() */
- unsigned int output_pos, output_mask;
- unsigned int cpos;
+ size_t output_pos, output_mask;
+ size_t cpos;
- unsigned short c_freq[2 * NC - 1];
- unsigned short p_freq[2 * NP - 1];
+ uint16_t c_freq[2 * NC - 1];
+ uint16_t p_freq[2 * NP - 1];
int np;
int pbit;
char filename[1024];
int namelen;
char method[5];
- int compsize;
- int origsize;
+ off_t compsize;
+ off_t origsize;
time_t mtime;
- int file_crc;
+ uint16_t file_crc;
char os_id;
int level;
};
if (remainder == 0) return; \
\
pos = 0; \
- n = (uint)MIN(remainder, MAXDICSIZ); \
+ n = (size_t)MIN(remainder, MAXDICSIZ); \
} \
} while (0)
void
-decode(struct lzh_istream *rp, FILE *outfile, unsigned long remainder, unsigned int *crc)
+decode(struct lzh_istream *rp, FILE *outfile, off_t remainder, uint16_t *crc)
{
char buf[MAXDICSIZ];
struct huf_t huf;
- unsigned int pos, n;
+ uint32_t pos;
+ size_t n;
huf_decode_start(rp, opts.method);
pos = 0;
- n = (uint)MIN(remainder, MAXDICSIZ);
+ n = (size_t)MIN(remainder, MAXDICSIZ);
for (;;) {
- uint c = decode_c(&huf, rp);
+ uint16_t c = decode_c(&huf, rp);
if (c <= UCHAR_MAX) {
PUTBUF(c);
}
else {
- unsigned int off;
- int len, i;
+ uint32_t off;
+ uint16_t len, i;
len = c - (UCHAR_MAX + 1 - THRESHOLD);
off = (pos - decode_p(&huf, rp) - 1) & (MAXDICSIZ - 1);
typedef short node;
#endif
-static uchar *text, *childcount;
+static uint8_t *text, *childcount;
static node pos, avail, *position, *parent, *prev, *next = NULL;
static int remainder;
#if MAXMATCH <= (UCHAR_MAX + 1)
-static uchar *level;
+static uint8_t *level;
#else
-static ushort *level;
+static uint16_t *level;
#endif
extern struct lha_opts opts;
}
static node
-child(node q, uchar c)
+child(node q, uint8_t c)
/* q's child for character c (NIL if not found) */
{
node r;
}
static void
-makechild(node q, uchar c, node r)
+makechild(node q, uint8_t c, node r)
/* Let r be q's child for character c. */
{
node h, t;
insert_node(struct match_t *match)
{
node q, r, j, t;
- uchar c, *t1, *t2;
+ uint8_t c, *t1, *t2;
if (match->len >= 4) {
match->len--;
static void
copy_stream_crc(FILE *arcfile, FILE *outfile,
- unsigned long remainder, unsigned int *crc)
+ off_t remainder, uint16_t *crc)
{
char buf[BUFSIZ];
while (remainder != 0) {
- uint n = (uint)MIN(remainder, BUFSIZ);
+ size_t n = (size_t)MIN(remainder, BUFSIZ);
/* no compress */
if (fread(buf, 1, n, arcfile) != n)
static void
extract(struct lzh_istream *rp, FILE *outfile, struct lzh_header *h)
{
- unsigned int crc;
+ uint16_t crc;
crc = INIT_CRC;
if (opts.method->dicbit != 0)
put_header(char *buf, int *i, int n, uint32_t x)
{
while (--n >= 0) {
- buf[(*i)++] = (uchar) (x & 0xFF);
+ buf[(*i)++] = (unsigned char) (x & 0xFF);
x >>= 8;
}
}
*i += size;
}
-static uint
+static uint8_t
calc_headersum(char *buf, int size)
{
int i;
- uint s;
+ uint8_t s;
s = 0;
for (i = 0; i < size; i++)
while (ext_headersize != 0) {
char extbuf[4096];
- uchar ext_type;
+ unsigned char ext_type;
int extpos = 0;
h->compsize -= ext_headersize;
while (ext_headersize != 0) {
char extbuf[4096];
- uchar ext_type;
+ unsigned char ext_type;
int extpos = 0;
remainder -= ext_headersize;
int ret;
ret = fgetc(fp);
- buf[0] = (uchar)ret;
+ buf[0] = (unsigned char)ret;
if (buf[0] == 0 || ret == EOF)
return 0; /* end of archive */
fread_crc(buf + 1, 21 - 1, fp, 0);
{
char buf[4096], *crcptr;
int headersize;
- extern ushort crctable[];
+ extern uint16_t crctable[];
char dirname[1024] = "", *fname;
int dirnamelen, len;
int pos = 0;
{
int i;
- unsigned int crc;
+ uint16_t crc;
crc = INIT_CRC;
for (i = 0; i < headersize; i++)
/***** encoding *****/
static void
-count_t_freq(struct lzh_ostream *wp, uchar *c_len, ushort *t_freq)
+count_t_freq(struct lzh_ostream *wp, uint8_t *c_len, uint16_t *t_freq)
{
int i, k, n, count;
static void
write_pt_len(struct lzh_ostream *wp, int n, int nbit, int i_special,
- uchar *pt_len)
+ uint8_t *pt_len)
{
int i, k;
}
static void
-write_c_len(struct lzh_ostream *wp, uchar *c_len, uchar *t_len, ushort *t_code)
+write_c_len(struct lzh_ostream *wp, uint8_t *c_len, uint8_t *t_len, uint16_t *t_code)
{
int i, k, n, count;
}
static void
-encode_c(struct lzh_ostream *wp, int c, uchar *c_len, ushort *c_code)
+encode_c(struct lzh_ostream *wp, int c, uint8_t *c_len, uint16_t *c_code)
{
putbits(wp, c_len[c], c_code[c]);
}
static void
-encode_p(struct lzh_ostream *wp, uint p, uchar *p_len, ushort *p_code)
+encode_p(struct lzh_ostream *wp, uint32_t p, uint8_t *p_len, uint16_t *p_code)
{
- uint c, q;
+ uint16_t c;
+ uint32_t q;
c = 0;
q = p;
static void
send_block(struct lzh_ostream *wp)
{
- uint i, k, flags, root, pos, size;
+ unsigned int i, k, flags, root, pos, size; /* XXX: variable type is vague */
- uchar c_len[NC];
- ushort c_code[NC];
+ uint8_t c_len[NC];
+ uint16_t c_code[NC];
- uchar t_len[NT];
- ushort t_code[NT];
+ uint8_t t_len[NT];
+ uint16_t t_code[NT];
- uchar p_len[NP];
- ushort p_code[NP];
+ uint8_t p_len[NP];
+ uint16_t p_code[NP];
/* make Huffman tree for characters and length */
root = make_tree(NC, wp->c_freq, c_len, c_code);
size = wp->c_freq[root];
putbits(wp, 16, size);
if (root >= NC) {
- ushort t_freq[2 * NT - 1];
+ uint16_t t_freq[2 * NT - 1];
/* make Huffman tree for c_len */
count_t_freq(wp, c_len, t_freq);
p_freq[] has frequency of length of off bits.
*/
void
-output(struct lzh_ostream *wp, uint c, uint p)
+output(struct lzh_ostream *wp, uint16_t c, uint32_t p)
{
wp->output_mask >>= 1;
if (wp->output_mask == 0) {
wp->cpos = wp->output_pos++;
wp->buf[wp->cpos] = 0;
}
- wp->buf[wp->output_pos++] = (uchar) c; /* char or length */
+ wp->buf[wp->output_pos++] = (uint8_t) c; /* char or length */
wp->c_freq[c]++;
if (c >= (1U << CHAR_BIT)) {
/* c is length, p is offset */
wp->buf[wp->cpos] |= wp->output_mask;
- wp->buf[wp->output_pos++] = (uchar) (p >> CHAR_BIT);
- wp->buf[wp->output_pos++] = (uchar) p;
+ wp->buf[wp->output_pos++] = (uint8_t) (p >> CHAR_BIT);
+ wp->buf[wp->output_pos++] = (uint8_t) p;
{
/* count frequency of p's bit length */
static void
read_pt_len(struct huf_t *huf, struct lzh_istream *rp, int nn, int nbit,
- int i_special, uchar *pt_len, ushort *pt_table)
+ int i_special, uint8_t *pt_len, uint16_t *pt_table)
{
int i, c, n;
- uint mask;
+ unsigned int mask;
n = getbits(rp, nbit);
if (n == 0) {
}
static void
-read_c_len(struct huf_t *huf, struct lzh_istream *rp, uchar *t_len, ushort *t_table)
+read_c_len(struct huf_t *huf, struct lzh_istream *rp, uint8_t *t_len, uint16_t *t_table)
{
int i, c, n;
- uint mask;
+ unsigned int mask;
n = getbits(rp, CBIT);
if (n == 0) {
}
}
-uint
+uint16_t
decode_c(struct huf_t *huf, struct lzh_istream *rp)
{
- uint j, mask;
+ uint16_t j, mask;
if (rp->blocksize == 0) {
- uchar t_len[NT];
- unsigned short t_table[256];
+ uint8_t t_len[NT];
+ uint16_t t_table[256];
rp->blocksize = getbits(rp, 16);
read_pt_len(huf, rp, NT, TBIT, 3, t_len, t_table);
return j;
}
-uint
+uint16_t
decode_p(struct huf_t *huf, struct lzh_istream *rp)
{
- uint j, mask;
+ uint16_t j, mask;
j = rp->p_table[rp->bitbuf >> (BITBUFSIZ - 8)];
if (j >= rp->np) {
#define CRCPOLY 0xA001 /* ANSI CRC-16 */
/* CCITT: 0x8408 */
-ushort crctable[UCHAR_MAX + 1];
+uint16_t crctable[UCHAR_MAX + 1];
void
make_crctable(void)
{
- uint i, j, r;
+ unsigned int i, j, r;
for (i = 0; i <= UCHAR_MAX; i++) {
r = i;
}
static const char *
-bitstring(unsigned int bitbuf, int n, char *ptr, size_t sz)
+bitstring(uint16_t bitbuf, int n, char *ptr, size_t sz)
{
static char str[256];
size_t size = sz;
rp->bitbuf |= rp->subbitbuf << n;
if (rp->compsize != 0) {
rp->compsize--;
- rp->subbitbuf = (uchar) getc(rp->fp);
+ rp->subbitbuf = getc(rp->fp);
}
else
rp->subbitbuf = 0;
rp->bitbuf |= rp->subbitbuf >> rp->bitcount;
}
-uint
+unsigned int
getbits(struct lzh_istream *rp, int n)
{
- uint x;
+ unsigned int x;
x = rp->bitbuf >> (BITBUFSIZ - n);
fillbuf(rp, n);
}
void
-putbits(struct lzh_ostream *wp, int n, uint x)
+putbits(struct lzh_ostream *wp, int n, unsigned int x)
{ /* Write rightmost n bits of x */
if (n < wp->bitcount) {
wp->subbitbuf |= x << (wp->bitcount -= n);
}
int
-fread_crc(void *p, int n, FILE * f, unsigned int *crc)
+fread_crc(void *p, int n, FILE * f, uint16_t *crc)
{
int i;
}
void
-fwrite_crc(void *p, int n, FILE * f, unsigned int *crc)
+fwrite_crc(void *p, int n, FILE * f, uint16_t *crc)
{
if (fwrite(p, 1, n, f) < n)
error("Unable to write");
void
list(struct lzh_header *h)
{
- uint r;
+ unsigned int r;
printf("%-14.*s", h->namelen, h->filename);
if (h->namelen > 14)
#include "ar.h"
void
-make_table(struct huf_t *huf, int nchar, uchar bitlen[], int tablebits, ushort table[])
+make_table(struct huf_t *huf, int nchar, uint8_t *bitlen, int tablebits, uint16_t *table)
{
- ushort count[17], weight[17], start[18], *p;
- uint i, k, len, ch, jutbits, avail, nextcode, mask;
+ uint16_t count[17], weight[17], start[18], *p;
+ unsigned int i, k, len, ch, jutbits, avail, nextcode, mask; /* XXX variable type is vague */
for (i = 1; i <= 16; i++)
count[i] = 0;
start[1] = 0;
for (i = 1; i <= 16; i++)
start[i + 1] = start[i] + (count[i] << (16 - i));
- if (start[17] != (ushort) (1U << 16))
+ if (start[17] != (uint16_t) (1U << 16))
error("Bad table");
jutbits = 16 - tablebits;
/* ar.c */
struct lha_method *which_method P_((char *id));
-uint ratio P_((ulong a, ulong b));
+unsigned int ratio P_((off_t a, off_t b));
void skip P_((FILE *fp, struct lzh_header *h));
int get_line P_((char *s, int n));
void parse_args P_((int argc, char **argv));
/* io.c */
void make_crctable P_((void));
void fillbuf P_((struct lzh_istream *rp, int n));
-uint getbits P_((struct lzh_istream *rp, int n));
-void putbits P_((struct lzh_ostream *wp, int n, uint x));
-int fread_crc P_((void *p, int n, FILE *f, unsigned int *crc));
-void fwrite_crc P_((void *p, int n, FILE *f, unsigned int *crc));
+unsigned int getbits P_((struct lzh_istream *rp, int n));
+void putbits P_((struct lzh_ostream *wp, int n, unsigned int x));
+int fread_crc P_((void *p, int n, FILE *f, uint16_t *crc));
+void fwrite_crc P_((void *p, int n, FILE *f, uint16_t *crc));
void init_getbits P_((struct lzh_istream *rp));
void init_putbits P_((struct lzh_ostream *wp));
/* encode.c */
void encode P_((struct lzh_ostream *wp, FILE *rfp));
/* decode.c */
-void decode P_((struct lzh_istream *rp, FILE *outfile, unsigned long remainder, unsigned int *crc));
+void decode P_((struct lzh_istream *rp, FILE *outfile, off_t remainder, uint16_t *crc));
/* maketree.c */
int make_tree P_((uint16_t nparm, uint16_t *freqparm, uint8_t *lenparm, uint16_t *codeparm));
/* maketbl.c */
-void make_table P_((struct huf_t *huf, int nchar, uchar bitlen[], int tablebits, ushort table[]));
+void make_table P_((struct huf_t *huf, int nchar, uint8_t *bitlen, int tablebits, uint16_t *table));
/* huf.c */
-void output P_((struct lzh_ostream *wp, uint c, uint p));
+void output P_((struct lzh_ostream *wp, uint16_t c, uint32_t p));
void huf_encode_start P_((struct lzh_ostream *wp, struct lha_method *m));
void huf_encode_end P_((struct lzh_ostream *wp));
-uint decode_c P_((struct huf_t *huf, struct lzh_istream *rp));
-uint decode_p P_((struct huf_t *huf, struct lzh_istream *rp));
+uint16_t decode_c P_((struct huf_t *huf, struct lzh_istream *rp));
+uint16_t decode_p P_((struct huf_t *huf, struct lzh_istream *rp));
void huf_decode_start P_((struct lzh_istream *rp, struct lha_method *m));
/* strlib.c */
int string_equal P_((char *str1, char *str2));