コミットメタ情報

リビジョンb09978fd9b8f239d15d1489aa1dc1cfc064bd857 (tree)
日時2014-03-14 22:56:51
作者ttwilb <ttwilb@user...>
コミッターttwilb

ログメッセージ

Merge branch 'master' of git.sourceforge.jp:/gitroot/heavyosecpu/HeavyOSECPU

Conflicts:
main.c
tek.c

変更サマリ

差分

--- a/main.c
+++ b/main.c
@@ -98,7 +98,7 @@ int HeavyOSECPUMain(int argc, char **argv)
9898 if (env.appBin[2] == 0xf0) {
9999 // tek5圧縮がかかっている
100100 #if (USE_TEK5 != 0)
101- env.appSize1 = tek5Decomp(env.appBin + 2, env.appBin + env.appSize0, systmp0); // 始めの2byte はOSECPUのシグネチャ(05 E1)だからとばす
101+ env.appSize1 = tek5Decomp(env.appBin + 2, env.appBin + env.appSize0, systmp0);
102102 env.appSize1 += 2;
103103 #else
104104 env.appSize1 = -9;
@@ -336,7 +336,7 @@ void LoadAppBin(HOSECPU_RuntimeEnvironment *env)
336336 fputs("app-file too large.\n", stderr);
337337 exit(EXIT_FAILURE);
338338 }
339- if (env->appSize0 < 3) { // 本来はここで 05 E1もチェックすべきでは?
339+ if (env->appSize0 < 3) {
340340 fputs("app-file header error.\n", stderr);
341341 exit(EXIT_FAILURE);
342342 }
--- a/tek.c
+++ b/tek.c
@@ -1,21 +1,19 @@
1-#include "tek.h"
1+#include <setjmp.h>
2+#include <string.h>
3+#include <stdlib.h>
24
3-static unsigned int tek_getnum_s7s(UCHAR **pp);
4-/*
5-// not used.
6-static int tek_lzrestore_stk1(int srcsiz, UCHAR *src, int outsiz, UCHAR *q);
5+#if (!defined(NULL))
6+ #define NULL 0
7+#endif
8+
9+typedef unsigned char UCHAR;
10+typedef unsigned int UINT32;
11+typedef UINT32 tek_TPRB;
12+
13+/* not used
714 static int tek_decode1(int siz, UCHAR *p, UCHAR *q);
8-static unsigned int tek_getnum_s7(UCHAR **pp);
9-static int tek_lzrestore_stk2(int srcsiz, UCHAR *src, int outsiz, UCHAR *q);
1015 static int tek_decode2(int siz, UCHAR *p, UCHAR *q);
1116 */
12-static int tek_lzrestore_tek5(int srcsiz, UCHAR *src, int outsiz, UCHAR *outbuf);
13-static void tek_setbm5(struct tek_STR_BITMODEL *bm, int t, int m);
14-static int tek_rdget0(struct tek_STR_RNGDEC *rd, int n, int i);
15-static int tek_rdget1(struct tek_STR_RNGDEC *rd, tek_TPRB *prob0, int n, int j, struct tek_STR_BITMODEL *bm);
16-static UINT32 tek_revbit(UINT32 data, int len);
17-static int tek_getlen5(struct tek_STR_RNGDEC *rd, int m, int s_pos, int stk);
18-static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int pb, int lp, int flags);
1917 static int tek_decode5(int siz, UCHAR *p, UCHAR *q);
2018
2119 static unsigned int tek_getnum_s7s(UCHAR **pp)
@@ -49,7 +47,7 @@ int tek_getsize(unsigned char *p)
4947
5048 #endif
5149
52-int tek_decomp(unsigned char *p, unsigned char *q, int size)
50+int tek_decomp(unsigned char *p, char *q, int size)
5351 {
5452 int err = -1;
5553 #if 0
@@ -61,13 +59,11 @@ int tek_decomp(unsigned char *p, unsigned char *q, int size)
6159 err = tek_decode5(size, p, q);
6260 }
6361 #else
64- if (*p == 0x89) {
65- err = tek_decode5(size, p, q);
62+if (*p == 0x89) {
63+ err = tek_decode5(size, p, (UCHAR *)q);
6664 }
6765 #endif
6866 if (err != 0) {
69-
70- printf("tek_decomp() serious error, err: %d\n", err);
7167 return -1; /* 失敗 */
7268 }
7369 return 0; /* 成功 */
@@ -187,7 +183,7 @@ static int tek_lzrestore_stk2(int srcsiz, UCHAR *src, int outsiz, UCHAR *q)
187183 } while (--j);
188184 if (q >= q1)
189185 break;
190-
186+
191187 /* lzフェーズ */
192188 j = 0;
193189 do {
@@ -265,10 +261,12 @@ static int tek_decode2(int siz, UCHAR *p, UCHAR *q)
265261
266262 #endif
267263
264+static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int pb, int lp, int flags);
265+
268266 static int tek_lzrestore_tek5(int srcsiz, UCHAR *src, int outsiz, UCHAR *outbuf)
269267 {
270268 int wrksiz, lc, lp, pb, flags = 0, *work, prop0, fl;
271-
269+
272270 if ((fl = (prop0 = *src) & 0x0f) == 0x01) /* 0001 */
273271 flags |= -1;
274272 else if (fl == 0x05)
@@ -313,6 +311,35 @@ static int tek_lzrestore_tek5(int srcsiz, UCHAR *src, int outsiz, UCHAR *outbuf)
313311 return flags;
314312 }
315313
314+struct tek_STR_BITMODEL {
315+ UCHAR t, m, s, dmy;
316+ UINT32 prb0, prb1, tmsk, ntm, lt, lt0, dmy4;
317+};
318+
319+struct tek_STR_PRB {
320+ struct tek_STR_PRB_PB {
321+ struct tek_STR_PRB_PBST {
322+ tek_TPRB mch, rep0l1;
323+ } st[12];
324+ tek_TPRB lenlow[2][8], lenmid[2][8];
325+ } pb[16];
326+ struct tek_STR_PRB_ST {
327+ tek_TPRB rep, repg0, repg1, repg2;
328+ } st[12];
329+ tek_TPRB lensel[2][2], lenhigh[2][256], pslot[4][64], algn[64];
330+ tek_TPRB spdis[2][2+4+8+16+32], lenext[2+4+8+16+32];
331+ tek_TPRB repg3, fchgprm[2 * 32], tbmt[16], tbmm[16], fchglt;
332+ tek_TPRB lit[1];
333+};
334+
335+struct tek_STR_RNGDEC {
336+ UCHAR *p;
337+ UINT32 range, code, rmsk;
338+ jmp_buf errjmp;
339+ struct tek_STR_BITMODEL bm[32], *ptbm[16];
340+ struct tek_STR_PRB probs;
341+};
342+
316343 static void tek_setbm5(struct tek_STR_BITMODEL *bm, int t, int m)
317344 {
318345 bm->t = t;
@@ -356,18 +383,14 @@ static int tek_rdget1(struct tek_STR_RNGDEC *rd, tek_TPRB *prob0, int n, int j,
356383 /* 寿命切れ */
357384 if (tek_rdget1(rd, &rd->probs.fchglt, 0x71, 0, &rd->bm[3]) == 0) {
358385 /* 寿命変更はまだサポートしてない */
359- printf("Fatal: Life change is not supported. in tek_rdget1\n");
360- printf(" rd->range: %lu, rd->code: %lu, rd->rmsk: %lu\n", rd->range, rd->code, rd->rmsk);
361- err:
386+err:
362387 longjmp(rd->errjmp, 1);
363388 }
364389 i = bm - rd->bm;
365390 if ((bm->s = tek_rdget1(rd, &rd->probs.fchgprm[i * 2 + bm->s], 0x71, 0, &rd->bm[1])) == 0) {
366391 i = tek_rdget1(rd, rd->probs.tbmt, 0x74, 1, &rd->bm[2]) & 15;
367392 if (i == 15)
368- {
369393 goto err;
370- }
371394 tek_setbm5(bm, i, ((tek_rdget1(rd, rd->probs.tbmm, 0x74, 1, &rd->bm[2]) - 1) & 15) + 1);
372395 }
373396 bm->lt = bm->lt0;
@@ -382,17 +405,18 @@ static int tek_rdget1(struct tek_STR_RNGDEC *rd, tek_TPRB *prob0, int n, int j,
382405 }
383406 if (p & bm->ntm) {
384407 p &= bm->tmsk;
385- fixprob:
408+ fixprob:
386409 *prob = p;
387410 }
388411 }
389-
412+
390413 while (rd->range < (UINT32) (1 << 24)) {
391414 rd->range <<= 8;
392415 rd->code = rd->code << 8 | *rd->p++;
393416 }
394417 j += j;
395- i = (UINT32)((rd->range & rd->rmsk) * p) >> 16;
418+ // 64ビット幅が必要
419+ i = (unsigned int)(((unsigned long long)(rd->range & rd->rmsk) * p) >> 16);
396420 if (rd->code < i) {
397421 j |= 1;
398422 rd->range = i;
@@ -449,21 +473,16 @@ static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int p
449473 UINT32 *lit1;
450474 struct tek_STR_RNGDEC *rd = (struct tek_STR_RNGDEC *) work;
451475 struct tek_STR_PRB *prb = &rd->probs;
452-
476+
453477 rd->p = &src[4];
454478 rd->range |= -1;
455479 rd->code = src[0] << 24 | src[1] << 16 | src[2] << 8 | src[3];
456480 for (i = 0; i < 4; i++)
457481 rep[i] = ~i;
458- if (setjmp(rd->errjmp)) // tek_rdget1() 内でエラーが発生したときに飛んでくる
459- {
482+ if (setjmp(rd->errjmp))
460483 goto err;
461- }
462-
463- for (i = sizeof (struct tek_STR_PRB) / sizeof (tek_TPRB)+(0x300 << (lc + lp)) - 2; i >= 0; i--)
464- {
484+ for (i = sizeof (struct tek_STR_PRB) / sizeof (tek_TPRB) + (0x300 << (lc + lp)) - 2; i >= 0; i--)
465485 ((tek_TPRB *) prb)[i] = 1 << 15;
466- }
467486 for (i = 0; i < 32; i++) {
468487 rd->bm[i].lt = (i >= 4); /* 0..3は寿命なし */
469488 rd->bm[i].lt0 = (i < 24) ? 16 * 1024 : 8 * 1024;
@@ -474,27 +493,23 @@ static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int p
474493 if (stk) {
475494 rd->rmsk = -1 << 11;
476495 for (i = 0; i < 32; i++)
477- {
478496 rd->bm[i].lt = 0; /* 全て寿命なし */
479- }
480497 for (i = 0; i < 14; i++)
481- {
482498 rd->ptbm[i] = &rd->bm[0];
483- }
484499 } else {
485500 UCHAR pt[14];
486501 static UCHAR pt1[14] = {
487- 8, 8, 8, 8, 8, 8, 8, 8,
488- 8, 8, 18, 18, 18, 8
502+ 8, 8, 8, 8, 8, 8, 8, 8,
503+ 8, 8, 18, 18, 18, 8
489504 };
490505 static UCHAR pt2[14] = {
491- 8, 8, 10, 11, 12, 12, 14, 15,
506+ 8, 8, 10, 11, 12, 12, 14, 15,
492507 16, 16, 18, 18, 20, 21
493508 };
494509 /*
495- 0- 7:mch, mch, lit1, lensel, lenlow, lenmid, lenhigh, lenext
496- 8-15:pslot, pslot, sdis, sdis, align, rep-repg2
497- */
510+ 0- 7:mch, mch, lit1, lensel, lenlow, lenmid, lenhigh, lenext
511+ 8-15:pslot, pslot, sdis, sdis, align, rep-repg2
512+ */
498513 rd->rmsk |= -1;
499514 rd->bm[1].t = 5; rd->bm[1].m = 3; /* for fchgprm */
500515 rd->bm[2].t = 9; rd->bm[2].m = 2; /* for tbmt, tbmm */
@@ -507,14 +522,10 @@ static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int p
507522 if (flags == -2) { /* z1 */
508523 rd->bm[22].lt = 0; /* repg3のltを0に */
509524 for (i = 0; i < 14; i++)
510- {
511525 pt[i] = pt1[i];
512- }
513526 } else {
514527 for (i = 0; i < 14; i++)
515- {
516528 pt[i] = pt2[i];
517- }
518529 lit0cntmsk = (7 >> (flags & 3)) << 4 | 8;
519530 pt[ 1] = 8 + ((flags & 0x04) != 0); /* mch */
520531 pt[ 5] = 12 + ((flags & 0x08) != 0); /* llm */
@@ -522,20 +533,13 @@ static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int p
522533 pt[11] = 18 + ((flags & 0x20) != 0); /* sds */
523534 }
524535 for (i = 0; i < 14; i++)
525- {
526536 rd->ptbm[i] = &rd->bm[pt[i]];
527- }
528537 }
529538 for (i = 0; i < 32; i++)
530- {
531539 tek_setbm5(&rd->bm[i], rd->bm[i].t, rd->bm[i].m);
532- }
533-
540+
534541 if ((tek_rdget1(rd, &prb->pb[0].st[0].mch, 0x71, 0, rd->ptbm[0]) ^ stk) == 0)
535- {
536542 goto err;
537- }
538-
539543 *q++ = tek_rdget1(rd, prb->lit, lit0cntmsk, 1, &rd->bm[24]) & 0xff;
540544 pmch &= 0; s &= 0; pos = 1;
541545 while (pos < osiz) {
@@ -544,9 +548,7 @@ static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int p
544548 i = (q[-1] >> lcr | (pos & m_lp) << lc) << 8;
545549 s = state_table[s];
546550 if (pmch == 0)
547- {
548551 *q = tek_rdget1(rd, &prb->lit[i], lit0cntmsk, 1, &rd->bm[24]) & 0xff;
549- }
550552 else {
551553 struct tek_STR_BITMODEL *bm = &rd->bm[24];
552554 j = 1; /* lit1は最初から2を減じてある */
@@ -556,9 +558,7 @@ static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int p
556558 j += j + tek_rdget1(rd, &lit1[(i + j) << 1 | pmch >> 7], 0x71, 0, rd->ptbm[2]);
557559 k--;
558560 if ((k & (lit0cntmsk >> 4)) == 0)
559- {
560561 bm++;
561- }
562562 if ((((pmch >> 7) ^ j) & 1) != 0 && k != 0) {
563563 j = tek_rdget1(rd, &prb->lit[i + j - 1], k | (lit0cntmsk & 0x70), j, bm);
564564 break;
@@ -578,14 +578,15 @@ static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int p
578578 rep[1] = rep[0];
579579 j = i = tek_getlen5(rd, 0, s_pos, stk);
580580 s = s < 7 ? 7 : 10;
581- if (j >= 4) j = 3;
581+ if (j >= 4)
582+ j = 3;
582583 rep[0] = j = tek_rdget1(rd, prb->pslot[j], 0x76, 1, rd->ptbm[8 + (j == 3)]) & 0x3f;
583584 if (j >= 4) {
584585 k = (j >> 1) - 1; /* k = [1, 30] */
585586 rep[0] = (2 | (j & 1)) << k;
586- if (j < 14){ /* k < 6 */
587+ if (j < 14) /* k < 6 */
587588 rep[0] |= tek_revbit(tek_rdget1(rd, &prb->spdis[j & 1][(1 << k) - 2], k | 0x70, 1, rd->ptbm[10 + (k >= 4)]), k);
588- } else {
589+ else {
589590 if (stk == 0) {
590591 if (k -= 6)
591592 rep[0] |= tek_rdget0(rd, k, ~0) << 6;
@@ -605,18 +606,15 @@ static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int p
605606 goto skip;
606607 }
607608 } else {
608- if (tek_rdget1(rd, &prb->st[s].repg1, 0x71, 0, rd->ptbm[13]) ^ stk){ /* rep1 */
609+ if (tek_rdget1(rd, &prb->st[s].repg1, 0x71, 0, rd->ptbm[13]) ^ stk) /* rep1 */
609610 i = rep[1];
610- } else {
611- if (tek_rdget1(rd, &prb->st[s].repg2, 0x71, 0, rd->ptbm[13]) ^ stk){ /* rep2 */
611+ else {
612+ if (tek_rdget1(rd, &prb->st[s].repg2, 0x71, 0, rd->ptbm[13]) ^ stk) /* rep2 */
612613 i = rep[2];
613- } else {
614+ else {
614615 if (stk == 0) {
615- if (tek_rdget1(rd, &prb->repg3, 0x71, 0, &rd->bm[22]) == 0)
616- {
616+ if (tek_rdget1(rd, &prb->repg3, 0x71, 0, &rd->bm[22]) == 0)
617617 goto err;
618- }
619-
620618 }
621619 i = rep[3]; /* rep3 */
622620 rep[3] = rep[2];
@@ -629,14 +627,12 @@ static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int p
629627 i = tek_getlen5(rd, 1, s_pos, stk);
630628 s = s < 7 ? 8 : 11;
631629 }
632- skip:
630+skip:
633631 i += 2;
634632 if (pos + rep[0] < 0)
635- {
636633 goto err;
637- }
638-
639- if (pos + i > osiz) i = osiz - pos;
634+ if (pos + i > osiz)
635+ i = osiz - pos;
640636 pos += i;
641637 do {
642638 *q = q[rep[0]];
@@ -657,10 +653,7 @@ static int tek_decode5(int siz, UCHAR *p, UCHAR *q)
657653 if ((dsiz = tek_getnum_s7s(&p)) > 0) {
658654 hed = tek_getnum_s7s(&p);
659655 if ((hed & 1) == 0)
660- {
661656 st = tek_lzrestore_tek5(p1 - p + 1, p - 1, dsiz, q);
662- printf("tek_lzrestore_tek5(%d, %d, %d, %d)\n", p1 - p + 1, p - 1, dsiz, q);
663- }
664657 else {
665658 bsiz = 1 << (((hed >> 1) & 0x0f) + 8);
666659 if (hed & 0x20)
旧リポジトリブラウザで表示