diff options
author | 2013-01-11 07:32:46 +0000 | |
---|---|---|
committer | 2013-01-11 07:32:46 +0000 | |
commit | 0e19f641d3186cdbf51f8ca857e2b07ab95779c2 (patch) | |
tree | 234109de1123b13f217494af4b3f8efad346d5cc /src/lib/formats/td0_dsk.c | |
parent | 111157ca09a9ff60fe4a9ba49173c315e94314fa (diff) |
Cleanups and version bumpmame0148
Diffstat (limited to 'src/lib/formats/td0_dsk.c')
-rw-r--r-- | src/lib/formats/td0_dsk.c | 530 |
1 files changed, 265 insertions, 265 deletions
diff --git a/src/lib/formats/td0_dsk.c b/src/lib/formats/td0_dsk.c index 363d996d633..9fbec75fa6a 100644 --- a/src/lib/formats/td0_dsk.c +++ b/src/lib/formats/td0_dsk.c @@ -28,12 +28,12 @@ /* Huffman coding parameters */ #define N_CHAR (256 - THRESHOLD + F) - /* character code (= 0..N_CHAR-1) */ + /* character code (= 0..N_CHAR-1) */ #define T (N_CHAR * 2 - 1) /* Size of table */ #define R (T - 1) /* root position */ #define MAX_FREQ 0x8000 - /* update when cumulative frequency */ - /* reaches to this value */ + /* update when cumulative frequency */ + /* reaches to this value */ struct td0dsk_tag { @@ -46,9 +46,9 @@ struct td0dsk_tag struct tdlzhuf { UINT16 r, - bufcnt,bufndx,bufpos, // string buffer - // the following to allow block reads from input in next_word() - ibufcnt,ibufndx; // input buffer counters + bufcnt,bufndx,bufpos, // string buffer + // the following to allow block reads from input in next_word() + ibufcnt,ibufndx; // input buffer counters UINT8 inbuf[BUFSZ]; // input buffer }; @@ -328,116 +328,116 @@ int td0dsk_t::data_read(UINT8 *buf, UINT16 size) /* decoder table */ static const UINT8 d_code[256] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, - 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, - 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, - 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, - 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, - 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, - 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, - 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, - 0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D, - 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F, - 0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11, - 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13, - 0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15, - 0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17, - 0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B, - 0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F, - 0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23, - 0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27, - 0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B, - 0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, + 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, + 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D, + 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F, + 0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11, + 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13, + 0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15, + 0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17, + 0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B, + 0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F, + 0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23, + 0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27, + 0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B, + 0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, }; static const UINT8 d_len[256] = { - 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, - 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, - 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, - 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, - 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, - 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, - 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, - 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, - 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, }; int td0dsk_t::next_word() { - if(tdctl.ibufndx >= tdctl.ibufcnt) - { - tdctl.ibufndx = 0; - tdctl.ibufcnt = data_read(tdctl.inbuf,BUFSZ); - if(tdctl.ibufcnt <= 0) - return(-1); - } - while (getlen <= 8) { // typically reads a word at a time - getbuf |= tdctl.inbuf[tdctl.ibufndx++] << (8 - getlen); - getlen += 8; - } - return(0); + if(tdctl.ibufndx >= tdctl.ibufcnt) + { + tdctl.ibufndx = 0; + tdctl.ibufcnt = data_read(tdctl.inbuf,BUFSZ); + if(tdctl.ibufcnt <= 0) + return(-1); + } + while (getlen <= 8) { // typically reads a word at a time + getbuf |= tdctl.inbuf[tdctl.ibufndx++] << (8 - getlen); + getlen += 8; + } + return(0); } int td0dsk_t::GetBit() /* get one bit */ { - INT16 i; - if(next_word() < 0) - return(-1); - i = getbuf; - getbuf <<= 1; - getlen--; - if(i < 0) - return(1); - else - return(0); + INT16 i; + if(next_word() < 0) + return(-1); + i = getbuf; + getbuf <<= 1; + getlen--; + if(i < 0) + return(1); + else + return(0); } int td0dsk_t::GetByte() /* get a byte */ { - UINT16 i; - if(next_word() != 0) - return(-1); - i = getbuf; - getbuf <<= 8; - getlen -= 8; - i = i >> 8; - return((int) i); + UINT16 i; + if(next_word() != 0) + return(-1); + i = getbuf; + getbuf <<= 8; + getlen -= 8; + i = i >> 8; + return((int) i); } @@ -446,22 +446,22 @@ int td0dsk_t::GetByte() /* get a byte */ void td0dsk_t::StartHuff() { - int i, j; - - for (i = 0; i < N_CHAR; i++) { - freq[i] = 1; - son[i] = i + T; - prnt[i + T] = i; - } - i = 0; j = N_CHAR; - while (j <= R) { - freq[j] = freq[i] + freq[i + 1]; - son[j] = i; - prnt[i] = prnt[i + 1] = j; - i += 2; j++; - } - freq[T] = 0xffff; - prnt[R] = 0; + int i, j; + + for (i = 0; i < N_CHAR; i++) { + freq[i] = 1; + son[i] = i + T; + prnt[i + T] = i; + } + i = 0; j = N_CHAR; + while (j <= R) { + freq[j] = freq[i] + freq[i + 1]; + son[j] = i; + prnt[i] = prnt[i + 1] = j; + i += 2; j++; + } + freq[T] = 0xffff; + prnt[R] = 0; } @@ -469,44 +469,44 @@ void td0dsk_t::StartHuff() void td0dsk_t::reconst() { - INT16 i, j, k; - UINT16 f, l; - - /* halven cumulative freq for leaf nodes */ - j = 0; - for (i = 0; i < T; i++) { - if (son[i] >= T) { - freq[j] = (freq[i] + 1) / 2; - son[j] = son[i]; - j++; - } - } - /* make a tree : first, connect children nodes */ - for (i = 0, j = N_CHAR; j < T; i += 2, j++) { - k = i + 1; - f = freq[j] = freq[i] + freq[k]; - for (k = j - 1; f < freq[k]; k--); - k++; - l = (j - k) * 2; - - /* movmem() is Turbo-C dependent - rewritten to memmove() by Kenji */ - - /* movmem(&freq[k], &freq[k + 1], l); */ - (void)memmove(&freq[k + 1], &freq[k], l); - freq[k] = f; - /* movmem(&son[k], &son[k + 1], l); */ - (void)memmove(&son[k + 1], &son[k], l); - son[k] = i; - } - /* connect parent nodes */ - for (i = 0; i < T; i++) { - if ((k = son[i]) >= T) { - prnt[k] = i; - } else { - prnt[k] = prnt[k + 1] = i; - } - } + INT16 i, j, k; + UINT16 f, l; + + /* halven cumulative freq for leaf nodes */ + j = 0; + for (i = 0; i < T; i++) { + if (son[i] >= T) { + freq[j] = (freq[i] + 1) / 2; + son[j] = son[i]; + j++; + } + } + /* make a tree : first, connect children nodes */ + for (i = 0, j = N_CHAR; j < T; i += 2, j++) { + k = i + 1; + f = freq[j] = freq[i] + freq[k]; + for (k = j - 1; f < freq[k]; k--); + k++; + l = (j - k) * 2; + + /* movmem() is Turbo-C dependent + rewritten to memmove() by Kenji */ + + /* movmem(&freq[k], &freq[k + 1], l); */ + (void)memmove(&freq[k + 1], &freq[k], l); + freq[k] = f; + /* movmem(&son[k], &son[k + 1], l); */ + (void)memmove(&son[k + 1], &son[k], l); + son[k] = i; + } + /* connect parent nodes */ + for (i = 0; i < T; i++) { + if ((k = son[i]) >= T) { + prnt[k] = i; + } else { + prnt[k] = prnt[k + 1] = i; + } + } } @@ -514,82 +514,82 @@ void td0dsk_t::reconst() void td0dsk_t::update(int c) { - int i, j, k, l; - - if (freq[R] == MAX_FREQ) { - reconst(); - } - c = prnt[c + T]; - do { - k = ++freq[c]; - - /* swap nodes to keep the tree freq-ordered */ - if (k > freq[l = c + 1]) { - while (k > freq[++l]); - l--; - freq[c] = freq[l]; - freq[l] = k; - - i = son[c]; - prnt[i] = l; - if (i < T) prnt[i + 1] = l; - - j = son[l]; - son[l] = i; - - prnt[j] = c; - if (j < T) prnt[j + 1] = c; - son[c] = j; - - c = l; - } - } while ((c = prnt[c]) != 0); /* do it until reaching the root */ + int i, j, k, l; + + if (freq[R] == MAX_FREQ) { + reconst(); + } + c = prnt[c + T]; + do { + k = ++freq[c]; + + /* swap nodes to keep the tree freq-ordered */ + if (k > freq[l = c + 1]) { + while (k > freq[++l]); + l--; + freq[c] = freq[l]; + freq[l] = k; + + i = son[c]; + prnt[i] = l; + if (i < T) prnt[i + 1] = l; + + j = son[l]; + son[l] = i; + + prnt[j] = c; + if (j < T) prnt[j + 1] = c; + son[c] = j; + + c = l; + } + } while ((c = prnt[c]) != 0); /* do it until reaching the root */ } INT16 td0dsk_t::DecodeChar() { - int ret; - UINT16 c; - - c = son[R]; - - /* - * start searching tree from the root to leaves. - * choose node #(son[]) if input bit == 0 - * else choose #(son[]+1) (input bit == 1) - */ - while (c < T) { - if((ret = GetBit()) < 0) - return(-1); - c += (unsigned) ret; - c = son[c]; - } - c -= T; - update(c); - return c; + int ret; + UINT16 c; + + c = son[R]; + + /* + * start searching tree from the root to leaves. + * choose node #(son[]) if input bit == 0 + * else choose #(son[]+1) (input bit == 1) + */ + while (c < T) { + if((ret = GetBit()) < 0) + return(-1); + c += (unsigned) ret; + c = son[c]; + } + c -= T; + update(c); + return c; } INT16 td0dsk_t::DecodePosition() { - INT16 bit; - UINT16 i, j, c; - - /* decode upper 6 bits from given table */ - if((bit=GetByte()) < 0) - return(-1); - i = (UINT16) bit; - c = (UINT16)d_code[i] << 6; - j = d_len[i]; - - /* input lower 6 bits directly */ - j -= 2; - while (j--) { - if((bit = GetBit()) < 0) - return(-1); - i = (i << 1) + bit; - } - return(c | (i & 0x3f)); + INT16 bit; + UINT16 i, j, c; + + /* decode upper 6 bits from given table */ + if((bit=GetByte()) < 0) + return(-1); + i = (UINT16) bit; + c = (UINT16)d_code[i] << 6; + j = d_len[i]; + + /* input lower 6 bits directly */ + j -= 2; + while (j--) { + if((bit = GetBit()) < 0) + return(-1); + i = (i << 1) + bit; + } + return(c | (i & 0x3f)); } /* DeCompression @@ -600,55 +600,55 @@ split out initialization code to init_Decode() void td0dsk_t::init_Decode() { - int i; - getbuf = 0; - getlen = 0; - tdctl.ibufcnt= tdctl.ibufndx = 0; // input buffer is empty - tdctl.bufcnt = 0; - StartHuff(); - for (i = 0; i < N - F; i++) - text_buf[i] = ' '; - tdctl.r = N - F; + int i; + getbuf = 0; + getlen = 0; + tdctl.ibufcnt= tdctl.ibufndx = 0; // input buffer is empty + tdctl.bufcnt = 0; + StartHuff(); + for (i = 0; i < N - F; i++) + text_buf[i] = ' '; + tdctl.r = N - F; } int td0dsk_t::Decode(UINT8 *buf, int len) /* Decoding/Uncompressing */ { - INT16 c,pos; - int count; // was an unsigned long, seems unnecessary - for (count = 0; count < len; ) { - if(tdctl.bufcnt == 0) { - if((c = DecodeChar()) < 0) - return(count); // fatal error - if (c < 256) { - *(buf++) = c; - text_buf[tdctl.r++] = c; - tdctl.r &= (N - 1); - count++; - } - else { - if((pos = DecodePosition()) < 0) - return(count); // fatal error - tdctl.bufpos = (tdctl.r - pos - 1) & (N - 1); - tdctl.bufcnt = c - 255 + THRESHOLD; - tdctl.bufndx = 0; - } - } - else { // still chars from last string - while( tdctl.bufndx < tdctl.bufcnt && count < len ) { - c = text_buf[(tdctl.bufpos + tdctl.bufndx) & (N - 1)]; - *(buf++) = c; - tdctl.bufndx++; - text_buf[tdctl.r++] = c; - tdctl.r &= (N - 1); - count++; - } - // reset bufcnt after copy string from text_buf[] - if(tdctl.bufndx >= tdctl.bufcnt) - tdctl.bufndx = tdctl.bufcnt = 0; - } - } - return(count); // count == len, success + INT16 c,pos; + int count; // was an unsigned long, seems unnecessary + for (count = 0; count < len; ) { + if(tdctl.bufcnt == 0) { + if((c = DecodeChar()) < 0) + return(count); // fatal error + if (c < 256) { + *(buf++) = c; + text_buf[tdctl.r++] = c; + tdctl.r &= (N - 1); + count++; + } + else { + if((pos = DecodePosition()) < 0) + return(count); // fatal error + tdctl.bufpos = (tdctl.r - pos - 1) & (N - 1); + tdctl.bufcnt = c - 255 + THRESHOLD; + tdctl.bufndx = 0; + } + } + else { // still chars from last string + while( tdctl.bufndx < tdctl.bufcnt && count < len ) { + c = text_buf[(tdctl.bufpos + tdctl.bufndx) & (N - 1)]; + *(buf++) = c; + tdctl.bufndx++; + text_buf[tdctl.r++] = c; + tdctl.r &= (N - 1); + count++; + } + // reset bufcnt after copy string from text_buf[] + if(tdctl.bufndx >= tdctl.bufcnt) + tdctl.bufndx = tdctl.bufcnt = 0; + } + } + return(count); // count == len, success } |