diff --git a/cli/lz/ulzenc.c b/cli/lz/ulzenc.c index 9eb08b9..bde1baf 100644 --- a/cli/lz/ulzenc.c +++ b/cli/lz/ulzenc.c @@ -17,33 +17,37 @@ char *raw, *mem; int uxn_lz_compress(void *output, const void *input, int input_size) { - int dict_len, match_len, i, string_len, match_ctl; + int i, dict_len, match_len, string_len, match_ctl; unsigned char *out = output, *combine = 0; const unsigned char *dict, *dict_best = 0, *in = input, *start = in, *end = in + input_size; while(in != end) { - dict_len = (int)(in - start); /* Get available dictionary size (history of original output) */ + /* Get available dictionary size (history of original output) */ + dict_len = (int)(in - start); + /* Limit history lookback to 256 bytes */ if(dict_len > 256) - dict_len = 256; /* Limit history lookback to 256 bytes */ - dict = in - dict_len; /* Start of dictionary */ - string_len = (int)(end - in); /* Size of the string to search for */ + dict_len = 256; + /* Start of dictionary */ + dict = in - dict_len; + /* Size of the string to search for */ + string_len = (int)(end - in); if(string_len > 0x3FFF + MinMatchLength) string_len = 0x3FFF + MinMatchLength; - /* ^ Limit string length to what we can fit in 14 bits, plus the minimum - * match length */ - match_len = 0; /* This will hold the length of our best match */ - for(; dict_len; dict += 1, dict_len -= 1) /* Iterate through the dictionary */ - { - for(i = 0;; i++) /* Find common prefix length with the string */ - { + /* ^ Limit string length to what we can fit in 14 bits, plus the minimum match length */ + /* This will hold the length of our best match */ + match_len = 0; + /* Iterate through the dictionary */ + for(; dict_len; dict += 1, dict_len -= 1) { + /* Find common prefix length with the string */ + for(i = 0;; i++) { if(i == string_len) { match_len = i; dict_best = dict; goto done_search; } - /* ^ If we reach the end of the string, this is the best possible match. - * End. */ + /* ^ If we reach the end of the string, this is the best possible match. End. */ + /* Dictionary repeats if we hit the end */ if(in[i] != dict[i % dict_len]) - break; /* Dictionary repeats if we hit the end */ + break; } if(i > match_len) { match_len = i; @@ -51,17 +55,18 @@ uxn_lz_compress(void *output, const void *input, int input_size) } } done_search: - if(match_len >= MinMatchLength) /* Long enough? Use dictionary match */ - { - match_ctl = match_len - MinMatchLength; /* More numeric range: treat 0 as 4, 1 as 5, etc. */ - if(match_ctl > 0x3F) /* Match is long enough to use 2 bytes for the size */ - { + /* Long enough? Use dictionary match */ + if(match_len >= MinMatchLength) { + /* More numeric range: treat 0 as 4, 1 as 5, etc. */ + match_ctl = match_len - MinMatchLength; + /* Match is long enough to use 2 bytes for the size */ + if(match_ctl > 0x3F) { *out++ = match_ctl >> 8 | 0x40 | 0x80; /* High byte of the size, with both flags set */ *out++ = match_ctl; /* Low byte of the size */ - } else /* Use 1 byte for the size */ - { + } else { + /* Set the "dictionary" flag */ *out++ = match_ctl | 0x80; - } /* Set the "dictionary" flag */ + } *out++ = in - dict_best - 1; /* Write offset into history. (0 is -1, 1 is -2, ...) */ in += match_len; /* Advance input by size of the match */ combine = 0; /* Disable combining previous literal, if any */ @@ -79,7 +84,8 @@ uxn_lz_compress(void *output, const void *input, int input_size) /* The 0 here means literal of length 1. */ *combine = 0; } - *out++ = *in++; /* Write 1 literal byte from the input to the output. */ + /* Write 1 literal byte from the input to the output. */ + *out++ = *in++; } return (int)(out - (unsigned char *)output); }