Lines Matching refs:state

111 fill(State_t* state)
115 if (state->eof)
117 if ((r = sfrd(state->codex->sp, state->buf, sizeof(state->buf), &state->codex->sfdisc)) <= 0)
119 state->eof = 1;
122 state->ie = (state->ip = state->buf) + r;
123 return *state->ip++;
131 getbits(register State_t* state, int n, int peek)
133 while (state->bitcount < n)
135 state->bitbuf <<= 8;
136 state->bitbuf |= GETCHAR(state);
137 state->bitcount += 8;
141 state->eof = 0;
142 return PEEKBITS(state, n);
144 return GETBITS(state, n);
148 make_table(State_t* state, int nchar, ui1* bitlen, int tablebits, ui2* table, ui4 tablesize)
164 if (state->eof)
191 if (state->codex->disc->errorf)
192 (*state->codex->disc->errorf)(NiL, state->codex->disc, 2, "%s: invalid compression table", state->codex->meth->name);
226 if (state->codex->disc->errorf)
227 (*state->codex->disc->errorf)(NiL, state->codex->disc, 2, "%s: compression table overflow", state->codex->meth->name);
250 state->right[available] = state->left[available] = 0;
254 p = &state->right[*p];
256 p = &state->left[*p];
275 if (state->codex->disc->errorf)
276 (*state->codex->disc->errorf)(NiL, state->codex->disc, 2, "%s: invalid compression table", state->codex->meth->name);
304 if (state->codex->disc->errorf)
305 (*state->codex->disc->errorf)(NiL, state->codex->disc, 2, "%s: compression table overflow", state->codex->meth->name);
315 state->right[avail] = state->left[avail] = 0;
318 if (k & mask) p = &state->right[*p];
319 else p = &state->left[*p];
331 get_p_len(State_t* state, int k, int nbit, int i_special)
337 if (!(n = GETBITS(state, nbit)))
339 c = GETBITS(state, nbit);
340 for (i = 0; i < elementsof(state->p_table); i++)
341 state->p_table[i] = c;
343 state->p_len[i] = 0;
350 if ((c = GETBITS(state, 3)) == 7)
351 while (GETBITS(state, 1))
353 state->p_len[i++] = c;
356 c = GETBITS(state, 2);
358 state->p_len[i++] = 0;
362 state->p_len[i++] = 0;
363 if (make_table(state, k, state->p_len, 8, state->p_table, elementsof(state->p_table)))
370 get_c_len(State_t* state)
377 n = GETBITS(state, CBIT);
378 if (state->eof)
382 c = GETBITS(state, CBIT);
383 for (i = 0; i < elementsof(state->c_table); i++)
384 state->c_table[i] = c;
385 for (i = 0; i < elementsof(state->c_len); i++)
386 state->c_len[i] = 0;
393 b = PEEKBITS(state, 16);
394 c = state->p_table[b >> 8];
396 c = ((b <<= 1) & 0x100) ? state->right[c] : state->left[c];
397 SKIPBITS(state, state->p_len[c]);
403 c = GETBITS(state, 4) + 3;
405 c = GETBITS(state, CBIT) + 20;
407 state->c_len[i++] = 0;
410 state->c_len[i++] = c - 2;
413 state->c_len[i++] = 0;
414 if (make_table(state, NC, state->c_len, 12, state->c_table, elementsof(state->c_table)))
421 reconst(State_t* state, int start, int end)
434 if ((k = state->child[i]) < 0)
436 state->freq[j] = (state->freq[i] + 1) / 2;
437 state->child[j] = k;
440 if (state->edge[b = state->block[i]] == i)
441 state->stock[--state->avail] = b;
450 state->freq[i] = state->freq[j];
451 state->child[i] = state->child[j];
455 f = state->freq[l] + state->freq[l + 1];
456 for (k = start; f < state->freq[k]; k++)
460 state->freq[i] = state->freq[j];
461 state->child[i] = state->child[j];
465 state->freq[i] = f;
466 state->child[i] = l + 1;
473 if ((j = state->child[i]) < 0)
474 state->node[~j] = i;
476 state->parent[j] = state->parent[j - 1] = i;
477 if ((g = state->freq[i]) == f)
478 state->block[i] = b;
481 state->edge[b = state->block[i] = state->stock[state->avail++]] = i;
488 swap_inc(State_t* state, int p)
495 b = state->block[p];
496 if ((q = state->edge[b]) != p)
498 r = state->child[p];
499 s = state->child[q];
500 state->child[p] = s;
501 state->child[q] = r;
503 state->parent[r] = state->parent[r - 1] = q;
505 state->node[~r] = q;
507 state->parent[s] = state->parent[s - 1] = p;
509 state->node[~s] = p;
512 else if (b != state->block[p + 1])
514 if (++state->freq[p] == state->freq[p - 1])
516 state->stock[--state->avail] = b;
517 state->block[p] = state->block[p - 1];
519 return state->parent[p];
521 state->edge[b]++;
522 if (++state->freq[p] == state->freq[p - 1])
523 state->block[p] = state->block[p - 1];
525 state->edge[state->block[p] = state->stock[state->avail++]] = p;
526 return state->parent[p];
530 update_c(State_t* state, int p)
534 if (state->freq[ROOT_C] == 0x8000)
535 reconst(state, 0, state->n_max * 2 - 1);
536 state->freq[ROOT_C]++;
537 q = state->node[p];
540 q = swap_inc(state, q);
545 update_p(State_t* state, int p)
549 if (state->total_p == 0x8000)
551 reconst(state, ROOT_P, state->most_p + 1);
552 state->total_p = state->freq[ROOT_P];
553 state->freq[ROOT_P] = 0xffff;
555 q = state->node[p + N_CHAR];
558 q = swap_inc(state, q);
560 state->total_p++;
564 make_new_node(State_t* state, int p)
569 r = state->most_p + 1;
571 state->node[~(state->child[r] = state->child[state->most_p])] = r;
572 state->child[q] = ~(p + N_CHAR);
573 state->child[state->most_p] = q;
574 state->freq[r] = state->freq[state->most_p];
575 state->freq[q] = 0;
576 state->block[r] = state->block[state->most_p];
577 if (state->most_p == ROOT_P)
579 state->freq[ROOT_P] = 0xffff;
580 state->edge[state->block[ROOT_P]]++;
582 state->parent[r] = state->parent[q] = state->most_p;
583 state->edge[state->block[q] = state->stock[state->avail++]]
584 = state->node[p + N_CHAR] = state->most_p = q;
585 update_p(state, p);
589 ready_made(State_t* state, ui1* tbl)
599 for (i = 0; i < state->snp; i++)
607 state->p_len[i] = j;
613 read_tree_c(State_t* state)
621 if (GETBITS(state, 1))
622 state->c_len[i] = GETBITS(state, LENFIELD) + 1;
624 state->c_len[i] = 0;
625 if (++i == 3 && state->c_len[0] == 1 && state->c_len[1] == 1 && state->c_len[2] == 1)
627 c = GETBITS(state, CBIT);
629 state->c_len[i] = 0;
631 state->c_table[i] = c;
635 return make_table(state, N1, state->c_len, 12, state->c_table, elementsof(state->c_table));
639 read_tree_p(State_t* state)
647 state->p_len[i] = GETBITS(state, LENFIELD);
648 if (++i == 3 && state->p_len[0] == 1 && state->p_len[1] == 1 && state->p_len[2] == 1)
650 c = GETBITS(state, WINBIT_MAX - 6);
652 state->c_len[i] = 0;
654 state->c_table[i] = c;
661 set_c_d0(State_t* state, ui4 n_max, ui4 maxmatch, ui4 snp)
667 state->n_max = n_max;
668 state->maxmatch = maxmatch;
669 state->snp = snp;
670 state->n1 = (state->n_max >= 256 + state->maxmatch - THRESHOLD + 1)
671 ? 512 : state->n_max - 1;
674 state->stock[i] = i;
675 state->block[i] = 0;
677 for (i = 0, j = state->n_max * 2 - 2; i < state->n_max; i++, j--)
679 state->freq[j] = 1;
680 state->child[j] = ~i;
681 state->node[i] = j;
682 state->block[j] = 1;
684 state->avail = 2;
685 state->edge[1] = state->n_max - 1;
686 i = state->n_max * 2 - 2;
689 f = state->freq[j] = state->freq[i] + state->freq[i - 1];
690 state->child[j] = i;
691 state->parent[i] = state->parent[i - 1] = j;
692 if (f == state->freq[j + 1])
693 state->edge[state->block[j] = state->block[j + 1]] = j;
695 state->edge[state->block[j] = state->stock[state->avail++]] = j;
708 init_c_d0(State_t* state)
710 set_c_d0(state, 314, 60, 1 << (12 - 6));
711 ready_made(state, table_fix);
712 return make_table(state, state->snp, state->p_len, 8, state->p_table, elementsof(state->p_table));
716 decode_c_d0(State_t* state)
720 c = state->child[ROOT_C];
722 c = state->child[c - GETBITS(state, 1)];
724 update_c(state, c);
725 if (c == state->n1)
726 c += GETBITS(state, 8);
731 init_p_d0(State_t* state)
733 state->freq[ROOT_P] = 1;
734 state->child[ROOT_P] = ~(N_CHAR);
735 state->node[N_CHAR] = ROOT_P;
736 state->edge[state->block[ROOT_P] = state->stock[state->avail++]] = ROOT_P;
737 state->most_p = ROOT_P;
738 state->total_p = 0;
739 state->nextcount = 64;
744 decode_p_d0(State_t* state)
748 while (state->count > state->nextcount)
750 make_new_node(state, (int)(state->nextcount / 64));
751 if ((state->nextcount += 64) >= state->window)
752 state->nextcount = 0xffffffff;
754 c = state->child[ROOT_P];
756 c = state->child[c - GETBITS(state, 1)];
758 update_p(state, c);
759 return (c << 6) + GETBITS(state, 6);
763 init_c_d1(State_t* state)
765 return set_c_d0(state, 286, MATCH_MAX, 1 << (12 - 6));
769 decode_start_fix(State_t* state)
771 state->n_max = 314;
772 state->maxmatch = 60;
773 state->snp = 1 << (12 - 6);
775 start_c_dyn(state);
777 ready_made(state, table_fix);
778 return make_table(state, state->snp, state->p_len, 8, state->p_table, elementsof(state->p_table));
782 init_c_s0(State_t* state)
784 state->n_max = 286;
785 state->maxmatch = MATCH_MAX;
786 state->snp = 1 << (WINBIT_MAX - 6);
787 state->blocksize = 0;
797 decode_c_s0(State_t* state)
801 if (state->blocksize == 0) /* read block head */
804 state->blocksize = GETBITS(state, BUFBITS);
805 if (read_tree_c(state))
807 if (GETBITS(state, 1))
808 read_tree_p(state);
810 ready_made(state, table_s0);
811 if (make_table(state, SNP, state->p_len, 8, state->p_table, elementsof(state->p_table)))
814 state->blocksize--;
815 j = state->c_table[GETBITS(state, 4)];
818 if (GETBITS(state, 1))
819 j = state->right[j];
821 j = state->left [j];
824 j += GETBITS(state, EXTRABITS);
829 init_p_s0(State_t* state)
835 decode_p_s0(State_t* state)
839 j = state->p_table[GETBITS(state, 8)];
840 if (j >= state->snp)
844 if (GETBITS(state, 1))
845 j = state->right[j];
847 j = state->left[j];
848 } while (j >= state->snp);
850 return (j << 6) + GETBITS(state, 6);
854 init_c_s1(State_t* state)
856 if (state->window <= (14 * 1024))
858 state->snp = 14;
859 state->pbit = 4;
863 state->snp = 16;
864 state->pbit = 5;
866 state->blocksize = 0;
871 decode_c_s1(State_t* state)
876 if (!state->blocksize && (!(state->blocksize = GETBITS(state, 16)) || get_p_len(state, NT, TBIT, 3) || get_c_len(state) || get_p_len(state, state->snp, state->pbit, -1)))
878 state->blocksize--;
879 b = PEEKBITS(state, 16);
880 j = state->c_table[b >> 4];
882 j = ((b <<= 1) & 0x10) ? state->right[j] : state->left[j];
883 SKIPBITS(state, state->c_len[j]);
884 return state->eof ? -1 : j;
888 init_p_s1(State_t* state)
894 decode_p_s1(State_t* state)
899 b = PEEKBITS(state, 16);
900 j = state->p_table[b >> 8];
901 while (j >= state->snp)
902 j = ((b <<= 1) & 0x100) ? state->right[j] : state->left[j];
903 SKIPBITS(state, state->p_len[j]);
905 j = (1 << (j - 1)) + GETBITS(state, j - 1);
910 init_c_s2(State_t* state)
912 state->offset = 0x100 - 2;
917 decode_c_s2(State_t* state)
919 if (GETBITS(state, 1))
920 return GETBITS(state, 8);
921 state->matchpos = GETBITS(state, 11);
922 return GETBITS(state, 4) + 0x100;
926 init_p_s2(State_t* state)
932 decode_p_s2(State_t* state)
934 return (state->loc - state->matchpos - MAGIC0) & 0x7ff;
938 init_c_s3(State_t* state)
942 state->flagcnt = 0;
944 memset(&state->text[i * 13 + 18], i, 13);
946 state->text[256 * 13 + 18 + i] = i;
948 state->text[256 * 13 + 256 + 18 + i] = 255 - i;
949 memset(&state->text[256 * 13 + 512 + 18], 0, 128);
950 memset(&state->text[256 * 13 + 512 + 128 + 18], ' ', 128 - 18);
955 decode_c_s3(State_t* state)
959 if (state->flagcnt == 0)
961 state->flagcnt = 8;
962 state->flag = GETCHAR(state);
964 state->flagcnt--;
965 c = GETCHAR(state);
966 if ((state->flag & 1) == 0)
968 state->matchpos = c;
969 c = GETCHAR(state);
970 state->matchpos += (c & 0xf0) << 4;
974 state->flag >>= 1;
979 init_p_s3(State_t* state)
985 decode_p_s3(State_t* state)
987 return (state->loc - state->matchpos - MAGIC5) & 0xfff;
1086 register State_t* state;
1137 if (!(state = newof(0, State_t, 1, w - 1)))
1143 state->window = w;
1144 state->init_c = decoders[i];
1145 state->decode_c = decoders[i + 1];
1146 state->init_p = decoders[j];
1147 state->decode_p = decoders[j + 1];
1148 state->codex = p;
1149 p->data = state;
1156 register State_t* state = (State_t*)p->data;
1158 memset(&state->count, 0, sizeof(*state) - offsetof(State_t, count));
1159 state->ip = state->ie = 0;
1160 state->bitcount = 0;
1161 state->cpylen = 0;
1162 state->eof = 0;
1163 state->offset = 0x100 - 3;
1164 return ((*state->init_c)(state) || (*state->init_p)(state)) ? -1 : 0;
1170 register State_t* state = (State_t*)CODEX(disc)->data;
1177 if (n == 0 || state->eof)
1179 while (state->cpylen)
1181 *s++ = state->text[state->loc++] = state->text[state->cpy++];
1182 if (state->loc >= state->window)
1183 state->loc = 0;
1184 if (state->cpy >= state->window)
1185 state->cpy = 0;
1186 state->cpylen--;
1192 c = (*state->decode_c)(state);
1197 state->count++;
1198 *s++ = state->text[state->loc++] = c;
1199 if (state->loc >= state->window)
1200 state->loc = 0;
1206 j = c - state->offset;
1207 i = state->loc - (*state->decode_p)(state) - 1;
1209 i += state->window;
1210 state->count += j;
1213 *s++ = state->text[state->loc++] = state->text[i++];
1214 if (i >= state->window)
1216 if (state->loc >= state->window)
1217 state->loc = 0;
1220 if (state->cpylen = j)
1221 state->cpy = i;