Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
troydhanson committed Aug 17, 2015
1 parent f29fcfc commit a82d65a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 36 deletions.
18 changes: 7 additions & 11 deletions lzw/code.c
Expand Up @@ -3,7 +3,7 @@
#include <stdio.h>
#include "code.h"

size_t lzw_compute_olen(int mode, unsigned char *ib, size_t ilen, symbol_stats *s)
size_t lzw_compute_olen(int mode, unsigned char *ib, size_t ilen, lzw *s)
{
size_t olen = 0;

Expand All @@ -27,7 +27,7 @@ size_t lzw_compute_olen(int mode, unsigned char *ib, size_t ilen, symbol_stats *

/* the memory pointed to by seq must be static through the
* lifetime of encoding or decoding the buffer */
static void add_seq(symbol_stats *s, unsigned char *seq, size_t len) {
static void add_seq(lzw *s, unsigned char *seq, size_t len) {
struct seq *q;

/* our dictionary has a hard limit- no recycling */
Expand All @@ -41,7 +41,7 @@ static void add_seq(symbol_stats *s, unsigned char *seq, size_t len) {
//fprintf(stderr,"add [%.*s]<len %u> @ index %lu\n", (int)len, seq, (int)len, q-s->seq_all);
}

static int have_seq(symbol_stats *s, unsigned char *seq, size_t len, unsigned long *index) {
static int have_seq(lzw *s, unsigned char *seq, size_t len, unsigned long *index) {
struct seq *q;

HASH_FIND(hh, s->dict, seq, len, q);
Expand All @@ -53,7 +53,7 @@ static int have_seq(symbol_stats *s, unsigned char *seq, size_t len, unsigned lo
}

static unsigned char bytes_all[256];
int lzw_init(symbol_stats *s) {
int lzw_init(lzw *s) {
int rc = -1;
size_t j;

Expand Down Expand Up @@ -82,7 +82,7 @@ int lzw_init(symbol_stats *s) {
* in doing so this implementation uses variable-width indexes
* into the dictionary. the encoder and decoder sync permits it.
*/
static unsigned char get_num_bits(symbol_stats *s, int post) {
static unsigned char get_num_bits(lzw *s, int post) {
unsigned long d = HASH_COUNT(s->dict) + post;
assert(d >= 256); /* one-byte seqs always in the dict */

Expand All @@ -108,7 +108,7 @@ static unsigned char get_num_bits(symbol_stats *s, int post) {
} while(0)

int lzw_recode(int mode, unsigned char *ib, size_t ilen, unsigned char *ob,
size_t *olen, symbol_stats *s) {
size_t *olen, lzw *s) {
unsigned char b, *i=ib, *o=ob;
unsigned long x=0;
int rc = -1;
Expand Down Expand Up @@ -209,12 +209,8 @@ int lzw_recode(int mode, unsigned char *ib, size_t ilen, unsigned char *ob,
return rc;
}

void lzw_release(symbol_stats *s) {
void lzw_release(lzw *s) {
HASH_CLEAR(hh, s->dict);
if (s->seq_all) free(s->seq_all);
}

int lzw_save_codebook(char *file, symbol_stats *s) {
assert(0); /* TODO */
return 0;
}
11 changes: 5 additions & 6 deletions lzw/code.h
Expand Up @@ -25,7 +25,7 @@ typedef struct {
struct seq *dict;
size_t seq_used;
size_t max_dict_entries;
} symbol_stats;
} lzw;

/* standard bit vector macros */
#define BIT_TEST(c,i) ((c[(i)/8] & (1 << ((i) % 8))) ? 1 : 0)
Expand All @@ -35,11 +35,10 @@ typedef struct {
#define MODE_ENCODE (1U << 0)
#define MODE_DECODE (1U << 1)

int lzw_init(symbol_stats *s);
void lzw_release(symbol_stats *s);
size_t lzw_compute_olen(int mode, unsigned char *ib, size_t ilen,
symbol_stats *s);
int lzw_init(lzw *s);
void lzw_release(lzw *s);
size_t lzw_compute_olen(int mode, unsigned char *ib, size_t ilen, lzw *s);
int lzw_recode(int mode, unsigned char *ib, size_t ilen, unsigned char *ob,
size_t *olen, symbol_stats *s);
size_t *olen, lzw *s);

#endif /* _LZCODE_H_ */
2 changes: 1 addition & 1 deletion lzw/lzw.c
Expand Up @@ -27,7 +27,7 @@ struct {
unsigned char *obuf;
size_t olen;

symbol_stats s;
lzw s;

} CF = {
.s.max_dict_entries = 1048576, /* powers of two make the most of index bits */
Expand Down
2 changes: 1 addition & 1 deletion mini-lzw/README.md
Expand Up @@ -33,7 +33,7 @@ that fixed dictionary.
The mlzw command is demonstrating the underlying C API which is the real
purpose of mini-lzw.

dict = mlzw_load_dictionary("names.lzw");
mlzw_load(dict, "names.lzw");
rc = mlzw_recode(mode, dict, input, input_len, output, &output_len);
if (rc < 0) ...

Expand Down
18 changes: 9 additions & 9 deletions mini-lzw/code.c
Expand Up @@ -5,7 +5,7 @@

/* the memory pointed to by seq must be static through the
* lifetime of encoding or decoding the buffer */
static void add_seq(symbol_stats *s, unsigned char *seq, size_t len) {
static void add_seq(lzw *s, unsigned char *seq, size_t len) {
struct seq *q;

/* our dictionary has a hard limit- no recycling */
Expand All @@ -19,7 +19,7 @@ static void add_seq(symbol_stats *s, unsigned char *seq, size_t len) {
//fprintf(stderr,"add [%.*s]<len %u> @ index %lu\n", (int)len, seq, (int)len, q-s->seq_all);
}

static int have_seq(symbol_stats *s, unsigned char *seq, size_t len, unsigned long *index) {
static int have_seq(lzw *s, unsigned char *seq, size_t len, unsigned long *index) {
struct seq *q;

HASH_FIND(hh, s->dict, seq, len, q);
Expand All @@ -31,7 +31,7 @@ static int have_seq(symbol_stats *s, unsigned char *seq, size_t len, unsigned lo
}

static unsigned char bytes_all[256];
int mlzw_init(symbol_stats *s) {
int mlzw_init(lzw *s) {
int rc = -1;
size_t j;

Expand All @@ -55,7 +55,7 @@ int mlzw_init(symbol_stats *s) {
}

/* used instead of mlzw_init to read a saved dictionary */
int mlzw_load(symbol_stats *s, char *file) {
int mlzw_load(lzw *s, char *file) {
int rc = -1;

rc = 0;
Expand All @@ -70,7 +70,7 @@ int mlzw_load(symbol_stats *s, char *file) {
* in doing so this implementation uses variable-width indexes
* into the dictionary. the encoder and decoder sync permits it.
*/
static unsigned char get_num_bits(symbol_stats *s, int bump) {
static unsigned char get_num_bits(lzw *s, int bump) {
unsigned long d = HASH_COUNT(s->dict) + bump;
assert(d >= 256); /* one-byte seqs always in the dict */

Expand All @@ -95,7 +95,7 @@ static unsigned char get_num_bits(symbol_stats *s, int bump) {
s->seq_all[x].hits++; \
} while(0)

int mlzw_recode(int mode, symbol_stats *s, unsigned char *ib, size_t ilen,
int mlzw_recode(int mode, lzw *s, unsigned char *ib, size_t ilen,
unsigned char *ob, size_t *olen) {
unsigned char b, *i=ib, *o=ob;
unsigned long x=0;
Expand Down Expand Up @@ -211,7 +211,7 @@ int mlzw_recode(int mode, symbol_stats *s, unsigned char *ib, size_t ilen,
return rc;
}

void mlzw_release(symbol_stats *s) {
void mlzw_release(lzw *s) {
HASH_CLEAR(hh, s->dict);
if (s->seq_all) free(s->seq_all);
}
Expand All @@ -226,7 +226,7 @@ do { \
} \
} while(0)

int mlzw_save_codebook(symbol_stats *s, char *file) {
int mlzw_save_codebook(lzw *s, char *file) {
int fd=-1, rc = -1;
struct seq *q;
size_t i;
Expand All @@ -251,7 +251,7 @@ int mlzw_save_codebook(symbol_stats *s, char *file) {
return rc;
}
#define is_ascii(x) ((x >= 0x20) && (x <= 0x7e))
int mlzw_show_codebook(symbol_stats *s) {
int mlzw_show_codebook(lzw *s) {
int rc = -1, w, b;
size_t i, j, l=0;
unsigned char x;
Expand Down
14 changes: 7 additions & 7 deletions mini-lzw/code.h
Expand Up @@ -25,7 +25,7 @@ typedef struct {
struct seq *dict;
size_t seq_used;
size_t max_dict_entries;
} symbol_stats;
} lzw;

/* standard bit vector macros */
#define BIT_TEST(c,i) ((c[(i)/8] & (1 << ((i) % 8))) ? 1 : 0)
Expand All @@ -37,12 +37,12 @@ typedef struct {
#define MODE_MAKE_CODES (1U << 2)
#define MODE_SHOW_CODES (1U << 3)

int mlzw_init(symbol_stats *s);
int mlzw_load(symbol_stats *s, char *file );
int mlzw_save_codebook(symbol_stats *s, char *file);
int mlzw_show_codebook(symbol_stats *s);
void mlzw_release(symbol_stats *s);
int mlzw_recode(int mode, symbol_stats *s, unsigned char *ib, size_t ilen,
int mlzw_init(lzw *s);
int mlzw_load(lzw *s, char *file );
int mlzw_save_codebook(lzw *s, char *file);
int mlzw_show_codebook(lzw *s);
void mlzw_release(lzw *s);
int mlzw_recode(int mode, lzw *s, unsigned char *ib, size_t ilen,
unsigned char *ob, size_t *olen);

#endif /* _LZCODE_H_ */
2 changes: 1 addition & 1 deletion mini-lzw/mlzw.c
Expand Up @@ -29,7 +29,7 @@ struct {
unsigned char *obuf;
size_t olen;

symbol_stats s;
lzw s;

} CF = {
.s.max_dict_entries = 1048576, /* powers of two make the most of index bits */
Expand Down

0 comments on commit a82d65a

Please sign in to comment.