• R/O
  • SSH
  • HTTPS

osask: Commit


Commit MetaInfo

Revision204 (tree)
Time2010-02-07 18:07:23
Authorhideyosi

Log Message

Ver 1.7 リリース版

Change Summary

Incremental Difference

--- hijk/hijk/tags/1.7/make.bat (nonexistent)
+++ hijk/hijk/tags/1.7/make.bat (revision 204)
@@ -0,0 +1 @@
1+..\z_tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9
\ No newline at end of file
--- hijk/hijk/tags/1.7/makeexe.bat (nonexistent)
+++ hijk/hijk/tags/1.7/makeexe.bat (revision 204)
@@ -0,0 +1,12 @@
1+..\z_tools\efg01.exe ../z_tools/sjisconv.g01 sjis in:efg01.c out:efg01.ca
2+..\z_tools\cc1.exe -I../z_tools/win32/ -Dmain=main0 -DUSE_INCLUDE_G01 -Os -Wall -quiet -o efg01.gas efg01.ca
3+..\z_tools\efg01.exe ../z_tools/gas2nask.g01 -a D:4 -A in:efg01.gas out:efg01.nas
4+..\z_tools\efg01.exe ../z_tools/nask.g01 in:efg01.nas out:efg01.obj
5+..\z_tools\efg01.exe ../z_tools/sjisconv.g01 sjis in:tek.c out:tek.ca
6+..\z_tools\cc1.exe -I../z_tools/win32/ -Dmain=main0 -Os -Wall -quiet -o tek.gas tek.ca
7+..\z_tools\efg01.exe ../z_tools/gas2nask.g01 -a D:4 -A in:tek.gas out:tek.nas
8+..\z_tools\efg01.exe ../z_tools/nask.g01 in:tek.nas out:tek.obj
9+..\z_tools\efg01.exe ../z_tools/nask.g01 in:naskfunc.nas out:naskfunc.obj
10+..\z_tools\efg01.exe -noadc ../z_tools/bin2obj.g01 -h in:%1.g01 out:inclg01.obj label:_inclg01
11+..\z_tools\ld.exe -s -Bdynamic --stack 0x1c00000 -o %1.exe -Map %1.map efg01.obj tek.obj naskfunc.obj inclg01.obj ../z_tools/win32/w32clibc.lib ../z_tools/win32/golibc.lib ../z_tools/win32/libmingw.lib
12+..\z_tools\upx.exe -9 %1.exe
\ No newline at end of file
--- hijk/hijk/tags/1.7/tek.c (nonexistent)
+++ hijk/hijk/tags/1.7/tek.c (revision 204)
@@ -0,0 +1,656 @@
1+#include <setjmp.h>
2+#include <string.h>
3+#include <stdlib.h>
4+
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+static int tek_decode1(int siz, UCHAR *p, UCHAR *q);
14+static int tek_decode2(int siz, UCHAR *p, UCHAR *q);
15+static int tek_decode5(int siz, UCHAR *p, UCHAR *q);
16+
17+static unsigned int tek_getnum_s7s(UCHAR **pp)
18+/* これは必ずbig-endian */
19+/* 下駄がないので中身をいじりやすい */
20+{
21+ unsigned int s = 0;
22+ UCHAR *p = *pp;
23+ do {
24+ s = s << 7 | *p++;
25+ } while ((s & 1) == 0);
26+ s >>= 1;
27+ *pp = p;
28+ return s;
29+}
30+
31+int tek_getsize(unsigned char *p)
32+{
33+ static char header[15] = {
34+ 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x4f, 0x53, 0x41, 0x53, 0x4b, 0x43, 0x4d, 0x50
35+ };
36+ int size = -1;
37+ if (memcmp(p + 1, header, 15) == 0 && (*p == 0x83 || *p == 0x85 || *p == 0x89)) {
38+ p += 16;
39+ size = tek_getnum_s7s(&p);
40+ }
41+ return size;
42+} /* (註)memcmpはstrncmpの仲間で、文字列中に0があっても指定された15文字まで比較する関数 */
43+
44+int tek_decomp(unsigned char *p, char *q, int size)
45+{
46+ int err = -1;
47+ if (*p == 0x83) {
48+ err = tek_decode1(size, p, q);
49+ } else if (*p == 0x85) {
50+ err = tek_decode2(size, p, q);
51+ } else if (*p == 0x89) {
52+ err = tek_decode5(size, p, q);
53+ }
54+ if (err != 0) {
55+ return -1; /* 失敗 */
56+ }
57+ return 0; /* 成功 */
58+}
59+
60+static int tek_lzrestore_stk1(int srcsiz, UCHAR *src, int outsiz, UCHAR *q)
61+{
62+ int by, lz, cp, ds;
63+ UCHAR *q1 = q + outsiz, *s7ptr = src, *q0 = q;
64+ do {
65+ if ((by = (lz = *s7ptr++) & 0x0f) == 0)
66+ by = tek_getnum_s7s(&s7ptr);
67+ if ((lz >>= 4) == 0)
68+ lz = tek_getnum_s7s(&s7ptr);
69+ do {
70+ *q++ = *s7ptr++;
71+ } while (--by);
72+ if (q >= q1)
73+ break;
74+ do {
75+ ds = (cp = *s7ptr++) & 0x0f;
76+ if ((ds & 1) == 0) {
77+ do {
78+ ds = ds << 7 | *s7ptr++;
79+ } while ((ds & 1) == 0);
80+ }
81+ ds = ~(ds >> 1);
82+ if ((cp >>= 4) == 0) {
83+ do {
84+ cp = cp << 7 | *s7ptr++;
85+ } while ((cp & 1) == 0);
86+ cp >>= 1;
87+ } /* 0がこないことをあてにする */
88+ cp++;
89+ if (q + ds < q0)
90+ goto err;
91+ if (q + cp > q1)
92+ cp = q1 - q;
93+ do {
94+ *q = *(q + ds);
95+ q++;
96+ } while (--cp);
97+ } while (--lz);
98+ } while (q < q1);
99+ return 0;
100+err:
101+ return 1;
102+}
103+
104+static int tek_decode1(int siz, UCHAR *p, UCHAR *q)
105+{
106+ int dsiz, hed, bsiz;
107+ UCHAR *p1 = p + siz;
108+ p += 16;
109+ if ((dsiz = tek_getnum_s7s(&p)) > 0) {
110+ hed = tek_getnum_s7s(&p);
111+ bsiz = 1 << (((hed >> 1) & 0x0f) + 8);
112+ if (dsiz > bsiz || (hed & 0x21) != 0x01)
113+ return 1;
114+ if (hed & 0x40)
115+ tek_getnum_s7s(&p); /* オプション情報へのポインタを読み飛ばす */
116+ if (tek_getnum_s7s(&p) != 0)
117+ return 1; /* 補助バッファ使用 */
118+ return tek_lzrestore_stk1(p1 - p, p, dsiz, q);
119+ }
120+ return 0;
121+}
122+
123+static unsigned int tek_getnum_s7(UCHAR **pp)
124+/* これは必ずbig-endian */
125+{
126+ unsigned int s = 0, b = 0, a = 1;
127+ UCHAR *p = *pp;
128+ for (;;) {
129+ s = s << 7 | *p++;
130+ if (s & 1)
131+ break;
132+ a <<= 7;
133+ b += a;
134+ }
135+ s >>= 1;
136+ *pp = p;
137+ return s + b;
138+}
139+
140+static int tek_lzrestore_stk2(int srcsiz, UCHAR *src, int outsiz, UCHAR *q)
141+{
142+ int cp, ds, repdis[4], i, j;
143+ UCHAR *q1 = q + outsiz, *s7ptr = src, *q0 = q, bylz, cbylz;
144+ for (j = 0; j < 4; j++)
145+ repdis[j] = -1 - j;
146+ bylz = cbylz = 0;
147+ if (outsiz) {
148+ if (tek_getnum_s7s(&s7ptr))
149+ return 1;
150+ do {
151+ /* byフェーズ */
152+ j = 0;
153+ do {
154+ j++;
155+ if (j >= 17) {
156+ j += tek_getnum_s7s(&s7ptr);
157+ break;
158+ }
159+ if (cbylz == 0) {
160+ cbylz = 8;
161+ bylz = *s7ptr++;
162+ }
163+ cbylz--;
164+ i = bylz & 1;
165+ bylz >>= 1;
166+ } while (i == 0);
167+ do {
168+ *q++ = *s7ptr++;
169+ } while (--j);
170+ if (q >= q1)
171+ break;
172+
173+ /* lzフェーズ */
174+ j = 0;
175+ do {
176+ j++;
177+ if (j >= 17) {
178+ j += tek_getnum_s7s(&s7ptr);
179+ break;
180+ }
181+ if (cbylz == 0) {
182+ cbylz = 8;
183+ bylz = *s7ptr++;
184+ }
185+ cbylz--;
186+ i = bylz & 1;
187+ bylz >>= 1;
188+ } while (i == 0);
189+ do {
190+ i = *s7ptr++;
191+ cp = i >> 4;
192+ i &= 0x0f;
193+ if ((i & 1) == 0)
194+ i |= (tek_getnum_s7(&s7ptr) + 1) << 4;
195+ i >>= 1;
196+ ds = ~(i - 6);
197+ if (i < 4)
198+ ds = repdis[i];
199+ if (i == 4)
200+ ds = repdis[0] - tek_getnum_s7(&s7ptr) - 1;
201+ if (i == 5)
202+ ds = repdis[0] + tek_getnum_s7(&s7ptr) + 1;
203+ if (cp == 0)
204+ cp = tek_getnum_s7(&s7ptr) + 16;
205+ cp++;
206+ if (i > 0) {
207+ if (i > 1) {
208+ if (i > 2)
209+ repdis[3] = repdis[2];
210+ repdis[2] = repdis[1];
211+ }
212+ repdis[1] = repdis[0];
213+ repdis[0] = ds;
214+ }
215+ if (q + ds < q0)
216+ goto err;
217+ if (q + cp > q1)
218+ cp = q1 - q;
219+ do {
220+ *q = *(q + ds);
221+ q++;
222+ } while (--cp);
223+ } while (--j);
224+ } while (q < q1);
225+ }
226+ return 0;
227+err:
228+ return 1;
229+}
230+
231+static int tek_decode2(int siz, UCHAR *p, UCHAR *q)
232+{
233+ UCHAR *p1 = p + siz;
234+ int dsiz, hed, bsiz, st = 0;
235+ p += 16;
236+ if ((dsiz = tek_getnum_s7s(&p)) > 0) {
237+ hed = tek_getnum_s7s(&p);
238+ bsiz = 1 << (((hed >> 1) & 0x0f) + 8);
239+ if (dsiz > bsiz || (hed & 0x21) != 0x01)
240+ return 1;
241+ if (hed & 0x40)
242+ tek_getnum_s7s(&p); /* オプション情報へのポインタを読み飛ばす */
243+ st = tek_lzrestore_stk2(p1 - p, p, dsiz, q);
244+ }
245+ return st;
246+}
247+
248+static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int pb, int lp, int flags);
249+
250+static int tek_lzrestore_tek5(int srcsiz, UCHAR *src, int outsiz, UCHAR *outbuf)
251+{
252+ int wrksiz, lc, lp, pb, flags, *work, prop0, fl;
253+
254+ if ((fl = (prop0 = *src) & 0x0f) == 0x01) /* 0001 */
255+ flags |= -1;
256+ else if (fl == 0x05)
257+ flags = -2;
258+ else if (fl == 0x09)
259+ flags &= 0;
260+ else
261+ return 1;
262+ src++;
263+ prop0 >>= 4;
264+ if (prop0 == 0)
265+ prop0 = *src++;
266+ else {
267+ static UCHAR prop0_table[] = { 0x5d, 0x00 }, prop1_table[] = { 0x00 };
268+ if (flags == -1) {
269+ if (prop0 >= 3)
270+ return 1;
271+ prop0 = prop0_table[prop0 - 1];
272+ } else {
273+ if (prop0 >= 2)
274+ return 1;
275+ prop0 = prop1_table[prop0 - 1];
276+ }
277+ }
278+ lp = prop0 / (9 * 5);
279+ prop0 %= 9 * 5;
280+ pb = prop0 / 9;
281+ lc = prop0 % 9;
282+ if (flags == 0) /* tek5:z2 */
283+ flags = *src++;
284+ if (flags == -1) { /* stk5 */
285+ wrksiz = lp;
286+ lp = pb;
287+ pb = wrksiz;
288+ }
289+ wrksiz = 0x180 * sizeof (UINT32) + (0x840 + (0x300 << (lc + lp))) * sizeof (tek_TPRB); /* 最低15KB, lc+lp=3なら、36KB */
290+ work = (int *) malloc(wrksiz);
291+ if (work == NULL)
292+ return -1;
293+ flags = tek_decmain5(work, src, outsiz, outbuf, lc, pb, lp, flags);
294+ free(work);
295+ return flags;
296+}
297+
298+struct tek_STR_BITMODEL {
299+ UCHAR t, m, s, dmy;
300+ UINT32 prb0, prb1, tmsk, ntm, lt, lt0, dmy4;
301+};
302+
303+struct tek_STR_PRB {
304+ struct tek_STR_PRB_PB {
305+ struct tek_STR_PRB_PBST {
306+ tek_TPRB mch, rep0l1;
307+ } st[12];
308+ tek_TPRB lenlow[2][8], lenmid[2][8];
309+ } pb[16];
310+ struct tek_STR_PRB_ST {
311+ tek_TPRB rep, repg0, repg1, repg2;
312+ } st[12];
313+ tek_TPRB lensel[2][2], lenhigh[2][256], pslot[4][64], algn[64];
314+ tek_TPRB spdis[2][2+4+8+16+32], lenext[2+4+8+16+32];
315+ tek_TPRB repg3, fchgprm[2 * 32], tbmt[16], tbmm[16], fchglt;
316+ tek_TPRB lit[1];
317+};
318+
319+struct tek_STR_RNGDEC {
320+ UCHAR *p;
321+ UINT32 range, code, rmsk;
322+ jmp_buf errjmp;
323+ struct tek_STR_BITMODEL bm[32], *ptbm[16];
324+ struct tek_STR_PRB probs;
325+};
326+
327+static void tek_setbm5(struct tek_STR_BITMODEL *bm, int t, int m)
328+{
329+ bm->t = t;
330+ bm->m = m;
331+ bm->prb1 = -1 << (m + t);
332+ bm->prb0 = ~bm->prb1;
333+ bm->prb1 |= 1 << t;
334+ bm->tmsk = (-1 << t) & 0xffff;
335+ bm->prb0 &= bm->tmsk;
336+ bm->prb1 &= bm->tmsk;
337+ bm->ntm = ~bm->tmsk;
338+ return;
339+}
340+
341+static int tek_rdget0(struct tek_STR_RNGDEC *rd, int n, int i)
342+{
343+ do {
344+ while (rd->range < (UINT32) (1 << 24)) {
345+ rd->range <<= 8;
346+ rd->code = rd->code << 8 | *rd->p++;
347+ }
348+ rd->range >>= 1;
349+ i += i;
350+ if (rd->code >= rd->range) {
351+ rd->code -= rd->range;
352+ i |= 1;
353+ }
354+ } while (--n);
355+ return ~i;
356+}
357+
358+static int tek_rdget1(struct tek_STR_RNGDEC *rd, tek_TPRB *prob0, int n, int j, struct tek_STR_BITMODEL *bm)
359+{
360+ UINT32 p, i, *prob, nm = n >> 4;
361+ n &= 0x0f;
362+ prob0 -= j;
363+ do {
364+ p = *(prob = prob0 + j);
365+ if (bm->lt > 0) {
366+ if (--bm->lt == 0) {
367+ /* 寿命切れ */
368+ if (tek_rdget1(rd, &rd->probs.fchglt, 0x71, 0, &rd->bm[3]) == 0) {
369+ /* 寿命変更はまだサポートしてない */
370+err:
371+ longjmp(rd->errjmp, 1);
372+ }
373+ i = bm - rd->bm;
374+ if ((bm->s = tek_rdget1(rd, &rd->probs.fchgprm[i * 2 + bm->s], 0x71, 0, &rd->bm[1])) == 0) {
375+ i = tek_rdget1(rd, rd->probs.tbmt, 0x74, 1, &rd->bm[2]) & 15;
376+ if (i == 15)
377+ goto err;
378+ tek_setbm5(bm, i, ((tek_rdget1(rd, rd->probs.tbmm, 0x74, 1, &rd->bm[2]) - 1) & 15) + 1);
379+ }
380+ bm->lt = bm->lt0;
381+ }
382+ if (p < bm->prb0) {
383+ p = bm->prb0;
384+ goto fixprob;
385+ }
386+ if (p > bm->prb1) {
387+ p = bm->prb1;
388+ goto fixprob;
389+ }
390+ if (p & bm->ntm) {
391+ p &= bm->tmsk;
392+ fixprob:
393+ *prob = p;
394+ }
395+ }
396+
397+ while (rd->range < (UINT32) (1 << 24)) {
398+ rd->range <<= 8;
399+ rd->code = rd->code << 8 | *rd->p++;
400+ }
401+ j += j;
402+ i = ((unsigned long long) (rd->range & rd->rmsk) * p) >> 16;
403+ if (rd->code < i) {
404+ j |= 1;
405+ rd->range = i;
406+ *prob += ((0x10000 - p) >> bm->m) & bm->tmsk;
407+ } else {
408+ rd->range -= i;
409+ rd->code -= i;
410+ *prob -= (p >> bm->m) & bm->tmsk;
411+ }
412+ --n;
413+ if ((n & nm) == 0)
414+ bm++;
415+ } while (n);
416+ return j;
417+}
418+
419+static UINT32 tek_revbit(UINT32 data, int len)
420+{
421+ UINT32 rev = 0;
422+ do {
423+ rev += rev + (data & 1);
424+ data >>= 1;
425+ } while (--len);
426+ return rev;
427+}
428+
429+static int tek_getlen5(struct tek_STR_RNGDEC *rd, int m, int s_pos, int stk)
430+{
431+ int i;
432+ if (tek_rdget1(rd, &rd->probs.lensel[m][0], 0x71, 0, rd->ptbm[3]) ^ stk) /* low */
433+ i = tek_rdget1(rd, rd->probs.pb[s_pos].lenlow[m], 0x73, 1, rd->ptbm[4]) & 7;
434+ else if (tek_rdget1(rd, &rd->probs.lensel[m][1], 0x71, 0, rd->ptbm[3]) ^ stk) /* mid */
435+ i = tek_rdget1(rd, rd->probs.pb[s_pos].lenmid[m], 0x73, 1, rd->ptbm[5]);
436+ else {
437+ /* high */
438+ i = tek_rdget1(rd, rd->probs.lenhigh[m], 0x78, 1, rd->ptbm[6]) - (256 + 256 - 8);
439+ if (i > 0) {
440+ if (i < 6 && stk == 0)
441+ i = tek_rdget1(rd, &rd->probs.lenext[(1 << i) - 2], i | 0x70, 1, rd->ptbm[7]) - 1;
442+ else
443+ i = tek_rdget0(rd, i, ~1) - 1;
444+ i = tek_rdget0(rd, i, ~1) - 1;
445+ }
446+ i += 256 - 8 + 16;
447+ }
448+ return i;
449+}
450+
451+static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int pb, int lp, int flags)
452+{
453+ static int state_table[] = { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
454+ int i, j, k, pmch, rep[4], s, pos, m_pos = (1 << pb) - 1, m_lp = (1 << lp) - 1;
455+ int stk = (flags == -1), lcr = 8 - lc, s_pos, lit0cntmsk = 0x78;
456+ UINT32 *lit1;
457+ struct tek_STR_RNGDEC *rd = (struct tek_STR_RNGDEC *) work;
458+ struct tek_STR_PRB *prb = &rd->probs;
459+
460+ rd->p = &src[4];
461+ rd->range |= -1;
462+ rd->code = src[0] << 24 | src[1] << 16 | src[2] << 8 | src[3];
463+ for (i = 0; i < 4; i++)
464+ rep[i] = ~i;
465+ if (setjmp(rd->errjmp))
466+ goto err;
467+ for (i = sizeof (struct tek_STR_PRB) / sizeof (tek_TPRB) + (0x300 << (lc + lp)) - 2; i >= 0; i--)
468+ ((tek_TPRB *) prb)[i] = 1 << 15;
469+ for (i = 0; i < 32; i++) {
470+ rd->bm[i].lt = (i >= 4); /* 0..3は寿命なし */
471+ rd->bm[i].lt0 = (i < 24) ? 16 * 1024 : 8 * 1024;
472+ rd->bm[i].s &= 0;
473+ rd->bm[i].t = rd->bm[i].m = 5;
474+ }
475+ lit1 = prb->lit + ((256 << (lc + lp)) - 2);
476+ if (stk) {
477+ rd->rmsk = -1 << 11;
478+ for (i = 0; i < 32; i++)
479+ rd->bm[i].lt = 0; /* 全て寿命なし */
480+ for (i = 0; i < 14; i++)
481+ rd->ptbm[i] = &rd->bm[0];
482+ } else {
483+ UCHAR pt[14];
484+ static UCHAR pt1[14] = {
485+ 8, 8, 8, 8, 8, 8, 8, 8,
486+ 8, 8, 18, 18, 18, 8
487+ };
488+ static UCHAR pt2[14] = {
489+ 8, 8, 10, 11, 12, 12, 14, 15,
490+ 16, 16, 18, 18, 20, 21
491+ };
492+ /*
493+ 0- 7:mch, mch, lit1, lensel, lenlow, lenmid, lenhigh, lenext
494+ 8-15:pslot, pslot, sdis, sdis, align, rep-repg2
495+ */
496+ rd->rmsk |= -1;
497+ rd->bm[1].t = 5; rd->bm[1].m = 3; /* for fchgprm */
498+ rd->bm[2].t = 9; rd->bm[2].m = 2; /* for tbmt, tbmm */
499+ if (flags & 0x40) { /* lt-flag */
500+ rd->bm[3].t = 0; rd->bm[3].m = 1;
501+ prb->fchglt = 0xffff;
502+ }
503+ rd->bm[22].t = 0; rd->bm[22].m = 1;
504+ prb->repg3 = 0xffff;
505+ if (flags == -2) { /* z1 */
506+ rd->bm[22].lt = 0; /* repg3のltを0に */
507+ for (i = 0; i < 14; i++)
508+ pt[i] = pt1[i];
509+ } else {
510+ for (i = 0; i < 14; i++)
511+ pt[i] = pt2[i];
512+ lit0cntmsk = (7 >> (flags & 3)) << 4 | 8;
513+ pt[ 1] = 8 + ((flags & 0x04) != 0); /* mch */
514+ pt[ 5] = 12 + ((flags & 0x08) != 0); /* llm */
515+ pt[ 9] = 16 + ((flags & 0x10) != 0); /* pst */
516+ pt[11] = 18 + ((flags & 0x20) != 0); /* sds */
517+ }
518+ for (i = 0; i < 14; i++)
519+ rd->ptbm[i] = &rd->bm[pt[i]];
520+ }
521+ for (i = 0; i < 32; i++)
522+ tek_setbm5(&rd->bm[i], rd->bm[i].t, rd->bm[i].m);
523+
524+ if ((tek_rdget1(rd, &prb->pb[0].st[0].mch, 0x71, 0, rd->ptbm[0]) ^ stk) == 0)
525+ goto err;
526+ *q++ = tek_rdget1(rd, prb->lit, lit0cntmsk, 1, &rd->bm[24]) & 0xff;
527+ pmch &= 0; s &= 0; pos = 1;
528+ while (pos < osiz) {
529+ s_pos = pos & m_pos;
530+ if (tek_rdget1(rd, &prb->pb[s_pos].st[s].mch, 0x71, 0, rd->ptbm[s > 0]) ^ stk) { /* 非lz */
531+ i = (q[-1] >> lcr | (pos & m_lp) << lc) << 8;
532+ s = state_table[s];
533+ if (pmch == 0)
534+ *q = tek_rdget1(rd, &prb->lit[i], lit0cntmsk, 1, &rd->bm[24]) & 0xff;
535+ else {
536+ struct tek_STR_BITMODEL *bm = &rd->bm[24];
537+ j = 1; /* lit1は最初から2を減じてある */
538+ k = 8;
539+ pmch = q[rep[0]];
540+ do {
541+ j += j + tek_rdget1(rd, &lit1[(i + j) << 1 | pmch >> 7], 0x71, 0, rd->ptbm[2]);
542+ k--;
543+ if ((k & (lit0cntmsk >> 4)) == 0)
544+ bm++;
545+ if ((((pmch >> 7) ^ j) & 1) != 0 && k != 0) {
546+ j = tek_rdget1(rd, &prb->lit[i + j - 1], k | (lit0cntmsk & 0x70), j, bm);
547+ break;
548+ }
549+ pmch <<= 1;
550+ } while (k);
551+ *q = j & 0xff;
552+ pmch &= 0;
553+ }
554+ pos++;
555+ q++;
556+ } else { /* lz */
557+ pmch |= 1;
558+ if (tek_rdget1(rd, &prb->st[s].rep, 0x71, 0, rd->ptbm[13]) ^ stk) { /* len/dis */
559+ rep[3] = rep[2];
560+ rep[2] = rep[1];
561+ rep[1] = rep[0];
562+ j = i = tek_getlen5(rd, 0, s_pos, stk);
563+ s = s < 7 ? 7 : 10;
564+ if (j >= 4)
565+ j = 3;
566+ rep[0] = j = tek_rdget1(rd, prb->pslot[j], 0x76, 1, rd->ptbm[8 + (j == 3)]) & 0x3f;
567+ if (j >= 4) {
568+ k = (j >> 1) - 1; /* k = [1, 30] */
569+ rep[0] = (2 | (j & 1)) << k;
570+ if (j < 14) /* k < 6 */
571+ rep[0] |= tek_revbit(tek_rdget1(rd, &prb->spdis[j & 1][(1 << k) - 2], k | 0x70, 1, rd->ptbm[10 + (k >= 4)]), k);
572+ else {
573+ if (stk == 0) {
574+ if (k -= 6)
575+ rep[0] |= tek_rdget0(rd, k, ~0) << 6;
576+ rep[0] |= tek_revbit(tek_rdget1(rd, prb->algn, 0x76, 1, rd->ptbm[12]), 6);
577+ } else {
578+ rep[0] |= tek_rdget0(rd, k - 4, ~0) << 4;
579+ rep[0] |= tek_revbit(tek_rdget1(rd, prb->algn, 0x74, 1, rd->ptbm[12]), 4);
580+ }
581+ }
582+ }
583+ rep[0] = ~rep[0];
584+ } else { /* repeat-dis */
585+ if (tek_rdget1(rd, &prb->st[s].repg0, 0x71, 0, rd->ptbm[13]) ^ stk) { /* rep0 */
586+ i |= -1;
587+ if (tek_rdget1(rd, &prb->pb[s_pos].st[s].rep0l1, 0x71, 0, rd->ptbm[13]) == 0) {
588+ s = s < 7 ? 9 : 11;
589+ goto skip;
590+ }
591+ } else {
592+ if (tek_rdget1(rd, &prb->st[s].repg1, 0x71, 0, rd->ptbm[13]) ^ stk) /* rep1 */
593+ i = rep[1];
594+ else {
595+ if (tek_rdget1(rd, &prb->st[s].repg2, 0x71, 0, rd->ptbm[13]) ^ stk) /* rep2 */
596+ i = rep[2];
597+ else {
598+ if (stk == 0) {
599+ if (tek_rdget1(rd, &prb->repg3, 0x71, 0, &rd->bm[22]) == 0)
600+ goto err;
601+ }
602+ i = rep[3]; /* rep3 */
603+ rep[3] = rep[2];
604+ }
605+ rep[2] = rep[1];
606+ }
607+ rep[1] = rep[0];
608+ rep[0] = i;
609+ }
610+ i = tek_getlen5(rd, 1, s_pos, stk);
611+ s = s < 7 ? 8 : 11;
612+ }
613+skip:
614+ i += 2;
615+ if (pos + rep[0] < 0)
616+ goto err;
617+ if (pos + i > osiz)
618+ i = osiz - pos;
619+ pos += i;
620+ do {
621+ *q = q[rep[0]];
622+ q++;
623+ } while (--i);
624+ }
625+ }
626+ return 0;
627+err:
628+ return 1;
629+}
630+
631+static int tek_decode5(int siz, UCHAR *p, UCHAR *q)
632+{
633+ UCHAR *p1 = p + siz;
634+ int dsiz, hed, bsiz, st = 0;
635+ p += 16;
636+ if ((dsiz = tek_getnum_s7s(&p)) > 0) {
637+ hed = tek_getnum_s7s(&p);
638+ if ((hed & 1) == 0)
639+ st = tek_lzrestore_tek5(p1 - p + 1, p - 1, dsiz, q);
640+ else {
641+ bsiz = 1 << (((hed >> 1) & 0x0f) + 8);
642+ if (hed & 0x20)
643+ return 1;
644+ if (bsiz == 256)
645+ st = tek_lzrestore_tek5(p1 - p, p, dsiz, q);
646+ else {
647+ if (dsiz > bsiz)
648+ return 1;
649+ if (hed & 0x40)
650+ tek_getnum_s7s(&p); /* オプション情報へのポインタを読み飛ばす */
651+ st = tek_lzrestore_tek5(p1 - p, p, dsiz, q);
652+ }
653+ }
654+ }
655+ return st;
656+}
--- hijk/hijk/tags/1.7/hijk.c (nonexistent)
+++ hijk/hijk/tags/1.7/hijk.c (revision 204)
@@ -0,0 +1,2571 @@
1+/* Executer For Guigui01 */
2+
3+#include <stdio.h>
4+#include <stdlib.h>
5+#include <string.h>
6+
7+typedef unsigned char UCHAR;
8+
9+#if (!defined(USE_WIN32))
10+ #define USE_WIN32 0
11+#endif
12+
13+#if (!defined(USE_POSIX))
14+ #define USE_POSIX 0
15+#endif
16+
17+#if (USE_WIN32)
18+ #include <windows.h>
19+ #include <conio.h>
20+ #include <time.h>
21+#endif
22+
23+#if (USE_POSIX)
24+ #if (!defined(_POSIX_C_SOURCE))
25+ #define _POSIX_C_SOURCE 199309
26+ #endif
27+ #include <time.h>
28+ #include <termios.h>
29+ #include <signal.h>
30+#endif
31+
32+#define DEBUG_MODE 0
33+
34+#define READ_EH4_BUFLEN 16
35+
36+struct READ_EH4 { /* 構造体名、関数名はEH4になっているが実際はGH4 */
37+ UCHAR buf[READ_EH4_BUFLEN], *p;
38+ int r, w, l;
39+};
40+
41+struct READ_API {
42+ struct READ_EH4 reh4, reh4_b;
43+ int rep, mod, mod0, flags, *reg32, *st0;
44+ char gosub, term;
45+};
46+
47+struct STR_SLOT {
48+ int t, p[3];
49+};
50+
51+struct STR_SLOT slot[64];
52+
53+void init_reh4(struct READ_EH4 *reh4, UCHAR *p);
54+int getnum_u(struct READ_EH4 *reh4, char f);
55+int debug_dump_tag(struct READ_EH4 *reh4, int trm);
56+void dump_g01(struct READ_EH4 *reh4, int trm);
57+void skip_tags(struct READ_EH4 *reh4);
58+int load_g01(struct READ_EH4 *reh4, UCHAR *p1, UCHAR *code0, UCHAR *work0, int *esi0);
59+void init_ra(struct READ_API *ra, UCHAR *p);
60+int getnum_api(struct READ_API *ra, char f);
61+
62+void start_app(int *esi);
63+void asm_api();
64+void asm_end();
65+void c_api();
66+int rjc(UCHAR *p0, UCHAR *p1, int ofs0, int ofs, int ofs1, int mode);
67+int inkey();
68+void consctrl1(int x, int y);
69+void consctrl2(int f, int b);
70+void consctrl3();
71+void consctrl4(int x, int y);
72+
73+#if (USE_POSIX)
74+void end_app(void);
75+void signal_handler(int sig);
76+static struct termios save_term;
77+static struct termios temp_term;
78+#endif
79+
80+int main_argc, main_flags;
81+UCHAR **main_argv;
82+
83+int tek_getsize(UCHAR *p);
84+int tek_decomp(unsigned char *p, char *q, int size);
85+
86+#if (defined(USE_INCLUDE_G01))
87+ extern UCHAR inclg01[5];
88+#endif
89+
90+void esi20()
91+{
92+ puts("use CALL([ESI+20])!");
93+ exit(1);
94+}
95+
96+struct FUNC06STR_SUB00 {
97+ UCHAR tag[32], usg[32], def[32], typ, flg, dmy_c[2];
98+ int def_int, dmy_i[6];
99+ /* 0:file-path, 1:int, 2:flag, 3:str */
100+};
101+
102+struct FUNC06STR {
103+ struct FUNC06STR_SUB00 usg[64];
104+ int def_arg0, flags, usgs;
105+ UCHAR *argv0, argv_f[256], argv_all[64 * 1024];
106+};
107+
108+static struct FUNC06STR *func06str;
109+
110+void func06_init();
111+void func06_putusage();
112+void func06_setup();
113+int my_strtol(UCHAR *p, UCHAR **pp);
114+int getreg32idx(struct READ_API *ra);
115+int read_reg32(struct READ_API *ra, int i);
116+void write_reg32(struct READ_API *ra, int i, int v);
117+
118+int main(int argc, UCHAR **argv)
119+{
120+ UCHAR *g01_0 = malloc(1024 * 1024), *g01_1, *p;
121+ UCHAR *code0 = malloc(1024 * 1024);
122+ UCHAR *work0 = malloc(4 * 1024 * 1024); /* 1MB+1MB+2MB */
123+ UCHAR *argv0 = *argv, *tmp = malloc(64 * 1024);
124+ FILE *fp;
125+ struct READ_EH4 reh4;
126+ int entry, work_esi[9];
127+//int i;
128+
129+ #if (!defined(USE_INCLUDE_G01))
130+ for (;;) {
131+ argv++;
132+ argc--;
133+ if (argc == 0)
134+ goto cantopen;
135+ if (**argv != '-')
136+ break;
137+ if (strcmp(*argv, "-lc") == 0)
138+ main_flags |= 1; /* lesser compatible mode */
139+ if (strcmp(*argv, "-noadc") == 0)
140+ main_flags |= 2; /* no auto de-compress mode */
141+ }
142+ #endif
143+ main_argc = argc;
144+ main_argv = argv;
145+
146+ /* *argvを読み込んで実行 */
147+ func06_init();
148+ #if (defined(USE_INCLUDE_G01))
149+ work_esi[0] = inclg01[0] | inclg01[1] << 8 | inclg01[2] << 16 | inclg01[3] << 24;
150+ for (entry = 0; entry < work_esi[0]; entry++)
151+ g01_0[entry] = (inclg01 + 4)[entry];
152+ g01_1 = g01_0 + work_esi[0];
153+ #else
154+ fp = fopen(*argv, "rb");
155+ if (fp == NULL) {
156+ sprintf(tmp, "%s.g01", *argv);
157+ fp = fopen(tmp, "rb");
158+ if (fp == NULL) {
159+ sprintf(tmp, argv0);
160+ entry = strlen(tmp);
161+ while (entry > 0 && tmp[entry - 1] != '\\' && tmp[entry - 1] != '/')
162+ entry--;
163+ sprintf(tmp + entry, *argv);
164+ fp = fopen(tmp, "rb");
165+ if (fp == NULL) {
166+ sprintf(tmp + entry, "%s.g01", *argv);
167+ fp = fopen(tmp, "rb");
168+ if (fp == NULL) {
169+cantopen:
170+ // puts("Can't open app-file.");
171+ puts("hijk Ver 1.7\n\tCan't open app-file.");
172+ return 1;
173+ }
174+ }
175+ }
176+ }
177+ g01_1 = g01_0 + fread(g01_0, 1, 1024 * 1024 - 16, fp);
178+ fclose(fp);
179+ #endif
180+ if ((work_esi[0] = tek_getsize(g01_0)) > 0) {
181+ if (tek_decomp(g01_0, code0, g01_1 - g01_0) == 0) {
182+ for (entry = 0; entry < work_esi[0]; entry++)
183+ g01_0[entry] = code0[entry];
184+ g01_1 = g01_0 + work_esi[0];
185+ }
186+ }
187+// *g01_1 = 0x9f; /* 31:end */
188+
189+ if (g01_0[0] != 'G' || g01_0[1] != 0x01) {
190+ puts("Not GUIGUI01 file.");
191+ return 1;
192+ }
193+ #if (DEBUG_MODE)
194+ init_reh4(&reh4, &g01_0[2]);
195+ while (debug_dump_tag(&reh4, 31) != 31);
196+ putchar('\n');
197+ putchar('\n');
198+ #endif
199+
200+ #if (DEBUG_MODE)
201+ init_reh4(&reh4, &g01_0[2]);
202+ // dump_g01(&reh4, 31);
203+ putchar('\n');
204+ #endif
205+
206+ if (g01_0[2] == 0x70) {
207+ if ((g01_0[3] & 0xf0) != 0x10) {
208+unkwn:
209+ puts("Unknown .g01 format");
210+ exit(1);
211+ }
212+ for (entry = 0; entry < 16; entry++) {
213+ static char head[16] = {
214+ 0x89, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00,
215+ 0x4f, 0x53, 0x41, 0x53, 0x4b, 0x43, 0x4d, 0x50
216+ };
217+ code0[entry] = head[entry];
218+ }
219+ p = &g01_0[4];
220+ if ((*p & 0x80) == 0) {
221+ entry = *p++;
222+ } else if ((*p & 0xc0) == 0x80) {
223+ entry = (p[0] & 0x3f) << 8 | p[1];
224+ p += 2;
225+ } else if ((*p & 0xe0) == 0xc0) {
226+ entry = (p[0] & 0x1f) << 16 | p[1] << 8 | p[2];
227+ p += 3;
228+ } else if ((*p & 0xf0) == 0xe0) {
229+ entry = (p[0] & 0x0f) << 24 | p[1] << 16 | p[2] << 8 | p[3];
230+ p += 4;
231+ } else {
232+ goto unkwn;
233+ }
234+ entry += 4;
235+ code0[16] = (entry >> 27) & 0xfe;
236+ code0[17] = (entry >> 20) & 0xfe;
237+ code0[18] = (entry >> 13) & 0xfe;
238+ code0[19] = (entry >> 6) & 0xfe;
239+ code0[20] = (entry << 1 | 1) & 0xff;
240+ work_esi[0] = entry;
241+ entry = 21;
242+ if ((g01_0[3] &= 0x0f) != 0) {
243+ entry++;
244+ if (g01_0[3] == 1)
245+ code0[21] = 0x15;
246+ if (g01_0[3] == 2)
247+ code0[21] = 0x19;
248+ if (g01_0[3] == 3)
249+ code0[21] = 0x21;
250+ if (g01_0[3] >= 4) {
251+ puts("Unknown .g01 format");
252+ exit(1);
253+ }
254+ }
255+ do {
256+ code0[entry++] = *p++;
257+ } while (p < g01_1);
258+ if (tek_decomp(code0, g01_0 + 2, entry) != 0) {
259+ puts("Broken .g01 format");
260+ exit(1);
261+ }
262+ g01_1 = g01_0 + 2 + work_esi[0];
263+ }
264+
265+ init_reh4(&reh4, &g01_0[2]);
266+ work_esi[0] = (int) &asm_api;
267+ work_esi[1] = 0; /* 未定, mma? */
268+ work_esi[2] = 0;
269+ work_esi[3] = (int) work0 + 2 * 1024 * 1024; /* edxからデフォルト2MBは自由に使える。つまりbssのデフォルトサイズが2MBみたいなもの */
270+ work_esi[4] = (int) code0;
271+ work_esi[5] = (int) work0 + 1024 * 1024 - 0x70;
272+ work_esi[6] = 0; /* 未定, mma? */
273+ work_esi[7] = (int) work_esi;
274+ work_esi[8] = 0;
275+ /* edxは一般にはアラインされたbss終了アドレス。いやそれはおかしい。edxはbss開始アドレス。bssサイズがなければ2MB */
276+ /* edxは.dataの末端というか.bssの先頭 */
277+ /* malloc域として使うのなら、edxに足せばいい。 */
278+ /* データ開始アドレスは、ESP+0x70 */
279+ entry = load_g01(&reh4, g01_1, code0, work0, work_esi);
280+
281+ *(int *) &work0[1024 * 1024 - 0x74] = (int) code0 + entry;
282+ *(int *) &work0[1024 * 1024 - 0x70] = (int) asm_end;
283+ /* -0x70 - -0x40 くらいはミニプログラムのためにあけてやる。これはst0[]で使いやすいからな */
284+
285+
286+//for (i=0;i < 50; i++) printf("%02X ", code0[i]);
287+
288+ #if (USE_POSIX)
289+ if(tcgetattr(fileno(stdin), &save_term) != 0) {
290+ puts("tcgetattr error");
291+ exit(1);
292+ }
293+ temp_term = save_term;
294+ temp_term.c_iflag &= IGNCR;
295+ temp_term.c_lflag &= ~ICANON;
296+ temp_term.c_lflag &= ~ECHO;
297+ temp_term.c_cc[VMIN] = 0;
298+ temp_term.c_cc[VTIME] = 0;
299+ if(tcsetattr(fileno(stdin), TCSANOW, &temp_term) != 0) {
300+ puts("tcsetattr error");
301+ exit(1);
302+ }
303+ signal(SIGINT , signal_handler);
304+ signal(SIGTERM, signal_handler);
305+ signal(SIGTSTP, signal_handler);
306+ signal(SIGCONT, signal_handler);
307+ atexit(end_app);
308+ #endif
309+ /* setjump */
310+ start_app(work_esi);
311+
312+ return 0;
313+}
314+
315+void init_reh4(struct READ_EH4 *reh4, UCHAR *p)
316+{
317+ int i;
318+ for (i = 0; i < READ_EH4_BUFLEN; i += 2) {
319+ reh4->buf[i + 0] = *p >> 4;
320+ reh4->buf[i + 1] = *p & 0x0f;
321+ p++;
322+ }
323+ reh4->p = p;
324+ reh4->r = 0;
325+ reh4->w = 0;
326+ reh4->l = READ_EH4_BUFLEN;
327+ return;
328+}
329+
330+void fill_reh4(struct READ_EH4 *reh4)
331+{
332+ while (reh4->l <= READ_EH4_BUFLEN - 2) {
333+ reh4->buf[reh4->w + 0] = *(reh4->p) >> 4;
334+ reh4->buf[reh4->w + 1] = *(reh4->p) & 0x0f;
335+ reh4->l += 2;
336+ reh4->p++;
337+ reh4->w = (reh4->w + 2) & (READ_EH4_BUFLEN - 1);
338+ }
339+ return;
340+}
341+
342+int getlen(struct READ_EH4 *reh4)
343+{
344+ UCHAR c;
345+ fill_reh4(reh4);
346+ c = reh4->buf[reh4->r];
347+ if ((c & 0x8) == 0)
348+ return 1;
349+ if ((c & 0xc) == 0x8)
350+ return 2;
351+ if ((c & 0xe) == 0xc)
352+ return 3;
353+ if (c == 0xe)
354+ return 4;
355+ c = reh4->buf[(reh4->r + 1) & (READ_EH4_BUFLEN - 1)];
356+ if ((c & 0x8) == 0)
357+ return 5;
358+ if ((c & 0xc) == 0x8)
359+ return 6;
360+ if ((c & 0xe) == 0xc)
361+ return 7;
362+ if (c == 0xe)
363+ return 8;
364+ c = reh4->buf[(reh4->r + 2) & (READ_EH4_BUFLEN - 1)];
365+ if ((c & 0x8) == 0)
366+ return 16;
367+ if ((c & 0xc) == 0x8)
368+ return 20;
369+ if ((c & 0xe) == 0xc)
370+ return 24;
371+ if (c == 0xe)
372+ return 32;
373+ puts("getlen: too long.");
374+ exit(1);
375+}
376+
377+int getnum_u0(struct READ_EH4 *reh4)
378+{
379+ return getnum_u(reh4, 0);
380+}
381+
382+int getnum_u1(struct READ_EH4 *reh4)
383+{
384+ return getnum_u(reh4, 1);
385+}
386+
387+int getnum_u(struct READ_EH4 *reh4, char f)
388+{
389+ static unsigned int mask[] = {
390+ 0x7, 0x3f, 0x1ff, 0xfff, 0x7fff, 0x3ffff, 0x1fffff, 0xffffff, 0, 0xffffffff, 0, /* 4-44 */
391+ 0xffffffff, 0, 0xffffffff, 0, 0xffffffff, 0, 0, 0, 0xffffffff, 0, 0, 0, /* 48-92 */
392+ 0xffffffff, 0, 0, 0, 0xffffffff /* 96-128 */
393+ };
394+ int l = getlen(reh4), j = l, i;
395+ if (f != 0 && reh4->buf[reh4->r] == 6 /* && l == 1 */) {
396+ reh4->r = (reh4->r + 1) & (READ_EH4_BUFLEN - 1);
397+ reh4->l--;
398+ return -1;
399+ }
400+ if (reh4->buf[reh4->r] == 7 /* && l == 1 */) {
401+ while (reh4->buf[reh4->r] == 7) {
402+ reh4->r = (reh4->r + 1) & (READ_EH4_BUFLEN - 1);
403+ reh4->l--;
404+ fill_reh4(reh4);
405+ }
406+ j = l = getlen(reh4);
407+ if (l < 8) { /* special */
408+ if (l == 1) {
409+ j = reh4->buf[reh4->r] << 1; /* 4のとき8、5のとき10、6のとき12 */
410+ l = j + 2;
411+ reh4->r = (reh4->r + 1) & (READ_EH4_BUFLEN - 1);
412+ reh4->l--;
413+ fill_reh4(reh4);
414+ if (j == 0 >> 1)
415+ return -1; /* inf / null-ptr */
416+ } else if (l == 2) {
417+ i = reh4->buf[reh4->r] << 4 | reh4->buf[(reh4->r + 1) & (READ_EH4_BUFLEN - 1)];
418+ reh4->r = (reh4->r + 2) & (READ_EH4_BUFLEN - 1);
419+ reh4->l -= 2;
420+ fill_reh4(reh4);
421+ if (i == 0xb8) {
422+ j = getnum_u0(reh4);
423+ return getnum_u0(reh4) << j;
424+ } else if (i == 0xb9) {
425+ #if (DEBUG_MODE)
426+ printf("getnum_u: 7B9 ");
427+ #endif
428+ j = getnum_u0(reh4);
429+ return getnum_u(reh4, 0) << j | ((1 << j) - 1);
430+ } else if (i == 0xba) {
431+ j = getnum_u0(reh4);
432+ l = 10;
433+ } else if (i == 0xbb) {
434+ puts("error-5");
435+ exit(1);
436+ } else if (i == 0xbc) {
437+ i = reh4->buf[reh4->r] << 4;
438+ i |= reh4->buf[(reh4->r + 1) & (READ_EH4_BUFLEN - 1)];
439+ i |= reh4->buf[(reh4->r + 2) & (READ_EH4_BUFLEN - 1)] << 12;
440+ i |= reh4->buf[(reh4->r + 3) & (READ_EH4_BUFLEN - 1)] << 8;
441+ reh4->r = (reh4->r + 4) & (READ_EH4_BUFLEN - 1);
442+ reh4->l -= 4;
443+ return i;
444+ } else if (i == 0xbd) {
445+ i = reh4->buf[reh4->r] << 4;
446+ i |= reh4->buf[(reh4->r + 1) & (READ_EH4_BUFLEN - 1)];
447+ i |= reh4->buf[(reh4->r + 2) & (READ_EH4_BUFLEN - 1)] << 12;
448+ i |= reh4->buf[(reh4->r + 3) & (READ_EH4_BUFLEN - 1)] << 8;
449+ i |= reh4->buf[(reh4->r + 4) & (READ_EH4_BUFLEN - 1)] << 20;
450+ i |= reh4->buf[(reh4->r + 5) & (READ_EH4_BUFLEN - 1)] << 16;
451+ i |= reh4->buf[(reh4->r + 6) & (READ_EH4_BUFLEN - 1)] << 28;
452+ i |= reh4->buf[(reh4->r + 7) & (READ_EH4_BUFLEN - 1)] << 24;
453+ reh4->r = (reh4->r + 8) & (READ_EH4_BUFLEN - 1);
454+ reh4->l -= 8;
455+ return i;
456+ } else if (i == 0xbe) {
457+ i = reh4->buf[reh4->r] << 4;
458+ i |= reh4->buf[(reh4->r + 1) & (READ_EH4_BUFLEN - 1)];
459+ i |= reh4->buf[(reh4->r + 2) & (READ_EH4_BUFLEN - 1)] << 12;
460+ i |= reh4->buf[(reh4->r + 3) & (READ_EH4_BUFLEN - 1)] << 8;
461+ i |= reh4->buf[(reh4->r + 4) & (READ_EH4_BUFLEN - 1)] << 20;
462+ i |= reh4->buf[(reh4->r + 5) & (READ_EH4_BUFLEN - 1)] << 16;
463+ i |= reh4->buf[(reh4->r + 6) & (READ_EH4_BUFLEN - 1)] << 28;
464+ i |= reh4->buf[(reh4->r + 7) & (READ_EH4_BUFLEN - 1)] << 24;
465+ reh4->r = (reh4->r + 16) & (READ_EH4_BUFLEN - 1);
466+ reh4->l -= 16;
467+ return i;
468+ } else {
469+ puts("error-6");
470+ exit(1);
471+ }
472+ } else {
473+ puts("error-4");
474+ exit(1);
475+ }
476+ }
477+ }
478+ i = 0;
479+ do {
480+ i = i << 4 | reh4->buf[reh4->r];
481+ reh4->r = (reh4->r + 1) & (READ_EH4_BUFLEN - 1);
482+ reh4->l--;
483+ } while (--j);
484+ return i & mask[l - 1];
485+}
486+
487+#if (DEBUG_MODE)
488+
489+int debug_dump_tag(struct READ_EH4 *reh4, int trm)
490+/* G01のdump用 */
491+{
492+ int i = getnum_u0(reh4), l;
493+ if (i == trm)
494+ return i;
495+ if (i == 0) { /* NOP */
496+ printf("[0] ");
497+ }
498+ if (1 <= i && i <= 7) { /* 1-7 */
499+ printf("[%X %X] ", i, getnum_u0(reh4));
500+ }
501+ if (8 <= i && i <= 15) { /* 8-15 */
502+ printf("[%X (%X) ", i, l = getnum_u0(reh4));
503+ while (l > 0) {
504+ debug_dump_tag(reh4, trm);
505+ l--;
506+ }
507+ printf("\b] ");
508+ }
509+ if (16 <= i && i <= 23) {
510+ printf("[%X (%X) ", i, l = getnum_u0(reh4));
511+ while (l > 0) {
512+ fill_reh4(reh4);
513+ printf("%X", reh4->buf[reh4->r]);
514+ reh4->r = (reh4->r + 1) & (READ_EH4_BUFLEN - 1);
515+ reh4->l--;
516+ l--;
517+ }
518+ printf("] ");
519+ }
520+ if (24 <= i && i <= 31) {
521+ printf("[%X (%X) ", i, l = getnum_u0(reh4));
522+ while (l > 0) {
523+ printf("%X ", getnum_u0(reh4));
524+ l--;
525+ }
526+ printf("] ");
527+ }
528+ return i;
529+}
530+
531+void dump_g01(struct READ_EH4 *reh4, int trm)
532+{
533+ int stack_unit = 1 << 12, stack_size = 1024 * 1024 >> 12;
534+ int heap_unit = 1 << 12, heap_size = 2 * 1024 * 1024 >> 12;
535+ int file_access_level = 0, last_section = -1;
536+ static char *secname[] = { ".text", ".data", ".bss" };
537+ int flags = 0, i, j, k, l;
538+
539+ for (;;) {
540+ i = getnum_u0(reh4);
541+ if (i == trm)
542+ return;
543+ if (i == 0)
544+ continue;
545+ if (i == 3)
546+ file_access_level = getnum_u0(reh4) >> 1;
547+ else if (i == 4) {
548+ stack_unit = 4096 << (getnum_u0(reh4) << 1);
549+ stack_size = 1;
550+ } else if (i == 5)
551+ stack_size = getnum_u0(reh4) + 1;
552+ else if (i == 8) {
553+ heap_unit = 4096 << (getnum_u0(reh4) << 1);
554+ heap_size = 1;
555+ } else if (i == 9)
556+ heap_size = getnum_u0(reh4) + 1;
557+ else if (i == 12) { /* skip (comment out) */
558+ j = getnum_u0(reh4);
559+ while (j > 0) {
560+ skip_tags(reh4);
561+ j--;
562+ }
563+ } else if (i == 13) { /* section */
564+ int sectype = last_section + 1, align = 1 << 12, entry = 0, size = 0;
565+ if ((flags & 1) == 0) {
566+ printf("file_access_level:%d\n", file_access_level);
567+ printf("stack:0x%X\n", stack_unit * stack_size);
568+ printf("heap: 0x%X\n", heap_unit * heap_size);
569+ flags |= 1;
570+ }
571+ j = getnum_u0(reh4);
572+ while (j > 0) {
573+ k = getnum_u0(reh4);
574+ if (k == 0)
575+ ;
576+ else if (k == 1)
577+ sectype = last_section = getnum_u0(reh4);
578+ else if (k == 2)
579+ align = 1 << getnum_u0(reh4);
580+ else if (k == 4)
581+ entry = size = getnum_u0(reh4);
582+ else if (k == 20) {
583+ size = l = getnum_u0(reh4);
584+ while (l > 0) {
585+ fill_reh4(reh4);
586+ reh4->r = (reh4->r + 1) & (READ_EH4_BUFLEN - 1);
587+ reh4->l--;
588+ l--;
589+ }
590+ size >>= 1;
591+ } else {
592+ puts("error");
593+ exit(1);
594+ }
595+ j--;
596+ }
597+ printf("secton-type: %s\n", secname[sectype]);
598+ printf(" align:%d\n", align);
599+ printf(" size:%d\n", size);
600+ if (sectype == 0)
601+ printf(" entry-point:0x%X\n", entry);
602+ } else {
603+ puts("error");
604+ exit(1);
605+ }
606+ }
607+ return;
608+}
609+
610+#endif
611+
612+void skip_tags(struct READ_EH4 *reh4)
613+{
614+ int i = getnum_u0(reh4), j;
615+ if (1 <= i && i <= 11)
616+ getnum_u(reh4, 0);
617+ if (12 <= i && i <= 15) {
618+ j = getnum_u0(reh4);
619+ while (j > 0) {
620+ skip_tags(reh4);
621+ j--;
622+ }
623+ }
624+ if (16 <= i && i <= 23) {
625+ j = getnum_u0(reh4);
626+ while (j > 0) {
627+ fill_reh4(reh4);
628+ reh4->r = (reh4->r + 1) & (READ_EH4_BUFLEN - 1);
629+ reh4->l--;
630+ j--;
631+ }
632+ }
633+ if (24 <= i && i <= 31) {
634+ j = getnum_u0(reh4);
635+ while (j > 0) {
636+ getnum_u0(reh4);
637+ j--;
638+ }
639+ }
640+ return;
641+}
642+
643+static char exit_nl = 1, errmod = 1, bufmod = 1, last_putc = 0; /* できるだけcuiで対話して解決 */
644+ /* 0:エラー返す, 1:エラー返さない・解決しない(エラーメッセージ), 2:黙ってできる範囲でのみ解決, 3:cui, 4:gui */
645+ /* bufmod=0:打ち切り, 1:エラー返さない・解決しない(エラーメッセージ), 2:黙ってできる範囲でのみ解決, 3:cui, 4:gui */
646+ /* eui=0:システム任せ, 1:沈黙, 2:cui, 3:gui */
647+
648+/*
649+一時設定、永久設定
650+fwriteは分割読み込みを意図しているのか、一括読み込みを意図しているのかを示すオプションを
651+*/
652+
653+int load_g01(struct READ_EH4 *reh4, UCHAR *p1, UCHAR *code0, UCHAR *work0, int *esi0)
654+{
655+ int stack_unit = 4096, stack_size = 1024 * 1024 / 4096;
656+ int heap_unit = 4096, heap_size = 2 * 1024 * 1024 / 4096;
657+ int mma_unit = 4096, mma_size = 1 * 1024 * 1024 / 4096;
658+ int file_access_level = 0, last_section = -1;
659+ int flags = 0, i, j, k, l, m, n, rjc_flag = 1, ii;
660+ int entry = 0, size;
661+ UCHAR *code00 = code0, *data00 = work0 + 1024 * 1024;
662+
663+ for (l = 0; l < 32; l++)
664+ work0[l] = 0x00;
665+
666+ ii = getnum_u0(reh4);
667+ if (ii == 0) {
668+ flags = getnum_u0(reh4) ^ 6; /* bit0:CALL(EBP)自動挿入, bit1: 自動改行, bit2: rjc */
669+//printf("flags=0x%X\n", flags);
670+ rjc_flag = (flags >> 2) & 1;
671+ exit_nl = (flags >> 1) & 1;
672+ ii = (flags >> 3) & 3;
673+ if (ii > 0) {
674+ if (ii <= 2) {
675+ esi0[1] = getnum_u0(reh4); /* 今のところ単純定数のみ */
676+ if (ii == 2)
677+ esi0[2] = getnum_u0(reh4); /* 今のところ単純定数のみ */
678+//printf("eax0=0x%X\n", esi0[1]);
679+//printf("ecx0=0x%X\n", esi0[2]);
680+ } else {
681+ puts("reg32-init: internal error");
682+ exit(1);
683+ }
684+ }
685+ reh4->p -= reh4->l >> 1;
686+//printf("next=0x%X\n", *reh4->p);
687+ // reh4->w = reh4->r;
688+ // reh4->l &= 1;
689+ // reh4->w = (reh4->w + reh4->l) & (READ_EH4_BUFLEN - 1);
690+ if ((flags & 1) != 0) {
691+ code0[0] = 0xff;
692+ code0[1] = 0x16;
693+ code0 += 2;
694+ }
695+ while (reh4->p < p1)
696+ *code0++ = *(reh4->p)++;
697+ size = code0 - code00;
698+ if (rjc_flag != 0)
699+ rjc(code00, code0, 0, 0, size, 0);
700+ *code0++ = 0xc3;
701+ goto fin;
702+ }
703+ for (; ii > 0; ii--) {
704+ i = getnum_u0(reh4);
705+ // if (i == trm)
706+ // return entry;
707+ if (i == 0)
708+ continue;
709+ if (i == 3) {
710+ file_access_level = getnum_u0(reh4) ^ 6;
711+ rjc_flag = (file_access_level >> 2) & 1;
712+ exit_nl = (file_access_level >> 1) & 1;
713+ file_access_level >>= 5;
714+ } else if (i == 4) {
715+ stack_unit = 4096 << (getnum_u0(reh4) << 1);
716+ stack_size = 1;
717+ } else if (i == 5)
718+ stack_size = getnum_u0(reh4) + 1;
719+ else if (i == 8) {
720+ heap_unit = 4096 << (getnum_u0(reh4) << 1);
721+ heap_size = 1;
722+ } else if (i == 9)
723+ heap_size = getnum_u0(reh4) + 1;
724+ else if (i == 10) {
725+ mma_unit = 4096 << (getnum_u0(reh4) << 1);
726+ mma_size = 1;
727+ } else if (i == 11)
728+ mma_size = getnum_u0(reh4) + 1;
729+ else if (i == 12) { /* skip (comment out) */
730+ j = getnum_u0(reh4);
731+ while (j > 0) {
732+ skip_tags(reh4);
733+ j--;
734+ }
735+ } else if (i == 13) { /* section */
736+ int sectype = ++last_section, align = 1 << 12, size = 0;
737+ j = getnum_u0(reh4);
738+ if ((flags & 1) == 0) {
739+ // work0 += stack_unit * stack_size;
740+ work0 += 1024 * 1024;
741+ flags |= 1;
742+ }
743+ while (j > 0) {
744+ k = getnum_u0(reh4);
745+ if (k == 0)
746+ ;
747+ else if (k == 1)
748+ sectype = last_section = getnum_u0(reh4);
749+ else if (k == 2)
750+ align = 1 << getnum_u0(reh4);
751+ else if (k == 4) {
752+ if (sectype == 0)
753+ entry = getnum_u0(reh4);
754+ else
755+ size = getnum_u0(reh4); /* bss用 */
756+ } else if (k == 20) {
757+ size = l = getnum_u0(reh4);
758+ l >>= 1;
759+ if (sectype == 0) {
760+ while (l > 0) {
761+ fill_reh4(reh4);
762+ *code0++ = reh4->buf[reh4->r] << 4 | reh4->buf[(reh4->r + 1) & (READ_EH4_BUFLEN - 1)];
763+ reh4->r = (reh4->r + 2) & (READ_EH4_BUFLEN - 1);
764+ reh4->l -= 2;
765+ l--;
766+ }
767+ if (rjc_flag != 0)
768+ rjc(code0 - size / 2, code0, 0, 0, size / 2, 0);
769+ *code0++ = 0xc3;
770+ }
771+ if (sectype == 1) {
772+ while (l > 0) {
773+ fill_reh4(reh4);
774+ *work0++ = reh4->buf[reh4->r] << 4 | reh4->buf[(reh4->r + 1) & (READ_EH4_BUFLEN - 1)];
775+ reh4->r = (reh4->r + 2) & (READ_EH4_BUFLEN - 1);
776+ reh4->l -= 2;
777+ l--;
778+ }
779+ for (l = 0; l < 32; l++)
780+ *work0++ = 0x00;
781+ }
782+ size >>= 1;
783+ } else if (k == 25) { /* 19.quick-links */
784+ l = getnum_u0(reh4) - 1;
785+ m = getnum_u0(reh4);
786+ if (m == 0)
787+ m = (int) code00;
788+ else if (m == 1)
789+ m = (int) data00;
790+ else {
791+ puts("error");
792+ exit(1);
793+ }
794+ n = 0;
795+ while (l > 0) {
796+ n += getnum_u0(reh4);
797+ if (sectype == 0) {
798+ *(int *) &code00[n] += m;
799+ n += 4;
800+ } else if (sectype == 1) {
801+ *(int *) &data00[n * 4] += m;
802+ n++;
803+ } else {
804+ puts("error-1");
805+ exit(1);
806+ }
807+ l--;
808+ }
809+ } else {
810+ puts("error-2");
811+ exit(1);
812+ }
813+ j--;
814+ }
815+ } else {
816+ puts("error-3");
817+ exit(1);
818+ }
819+ }
820+fin:
821+ return entry;
822+}
823+
824+void init_ra(struct READ_API *ra, UCHAR *p)
825+{
826+ init_reh4(&ra->reh4, p);
827+ ra->rep = 0x7fffffff;
828+ ra->mod = 0;
829+ ra->mod0 = 0;
830+ ra->flags = 0;
831+ ra->gosub = 0;
832+ ra->term = 0;
833+ return;
834+}
835+
836+void ungetnum_gh4(struct READ_EH4 *reh4, char g)
837+{
838+ if (reh4->l > READ_EH4_BUFLEN - 1) {
839+ reh4->l -= 2;
840+ reh4->p--;
841+ reh4->w = (reh4->w + (READ_EH4_BUFLEN - 2)) & (READ_EH4_BUFLEN - 1);
842+ }
843+ reh4->r = (reh4->r + (READ_EH4_BUFLEN - 1)) & (READ_EH4_BUFLEN - 1);
844+ reh4->buf[reh4->r] = g;
845+ reh4->l++;
846+ return;
847+}
848+
849+char test_sig4(struct READ_API *ra, char i)
850+{
851+ if (ra->mod == 0) {
852+ if (ra->reh4.l == 0)
853+ fill_reh4(&ra->reh4);
854+ if (ra->reh4.buf[ra->reh4.r] == i) { /* デフォルトキャンセルは、4bitの5 */
855+ ra->reh4.r = (ra->reh4.r + 1) & (READ_EH4_BUFLEN - 1);
856+ ra->reh4.l--;
857+ return -1;
858+ }
859+ }
860+ return 0;
861+}
862+
863+void insert_6_0(struct READ_API *ra)
864+/* 6_0 */
865+{
866+ if (ra->mod == 0 && test_sig4(ra, 4) == 0) { /* デフォルトキャンセルは、4bitの4 */
867+// ungetnum_gh4(&ra->reh4, 0x0);
868+ ungetnum_gh4(&ra->reh4, 0x6);
869+ }
870+ return;
871+}
872+
873+void insert_6x(struct READ_API *ra)
874+/* 6_504 */
875+{
876+ if (ra->mod == 0 && test_sig4(ra, 5) == 0) { /* デフォルトキャンセルは、4bitの5 */
877+ ungetnum_gh4(&ra->reh4, 0x8);
878+ ungetnum_gh4(&ra->reh4, 0xe);
879+ ungetnum_gh4(&ra->reh4, 0xd);
880+ ungetnum_gh4(&ra->reh4, 0x6);
881+ }
882+ return;
883+}
884+
885+void insert_6y(struct READ_API *ra)
886+/* 6_505 */
887+{
888+ if (ra->mod == 0 && test_sig4(ra, 5) == 0) { /* デフォルトキャンセルは、4bitの5 */
889+ ungetnum_gh4(&ra->reh4, 0x9);
890+ ungetnum_gh4(&ra->reh4, 0xe);
891+ ungetnum_gh4(&ra->reh4, 0xd);
892+ ungetnum_gh4(&ra->reh4, 0x6);
893+ }
894+ return;
895+}
896+
897+void insert_mini(struct READ_API *ra, char s)
898+/* s */
899+{
900+ if (ra->mod == 0 && test_sig4(ra, 5) == 0) /* デフォルトキャンセルは、4bitの5 */
901+ ungetnum_gh4(&ra->reh4, s);
902+ return;
903+}
904+
905+static UCHAR osask7 = 0, osask7_buf;
906+
907+void enable_osask7()
908+{
909+ osask7 = 1;
910+ osask7_buf = 1;
911+ return;
912+}
913+
914+void disable_osask7()
915+{
916+ osask7 = 0;
917+ return;
918+}
919+
920+int getnum_api(struct READ_API *ra, char f)
921+/* 将来はバッファリングなどを活用して高速化 */
922+/* f=1:個数フィールド, f=2:ターミネータ通知 */
923+{
924+ int i, j, k;
925+ if (osask7 != 0 && osask7_buf >= 0x80) {
926+ i = osask7_buf & 0x7f;
927+ osask7_buf = 1;
928+ if (i != 0)
929+ osask7 |= 0x02;
930+ if ((osask7 & 0x02) != 0) {
931+ if (ra->term != 0) {
932+ if (i == ra->rep) {
933+ ra->term = 0;
934+ ra->mod = ra->mod0;
935+ ra->rep = 0x7fffffff;
936+ if (ra->gosub != 0)
937+ ra->reh4 = ra->reh4_b;
938+ if (f != 9)
939+ goto retry;
940+ return -2; /* term */
941+ }
942+ } else {
943+ ra->rep--;
944+ if (ra->rep == 0) { /* gosubからの帰還 */
945+ // ra->term = 0;
946+ ra->mod = ra->mod0;
947+ ra->rep = 0x7fffffff;
948+ if (ra->gosub != 0)
949+ ra->reh4 = ra->reh4_b;
950+ }
951+ }
952+ goto fin1;
953+ }
954+ }
955+
956+retry:
957+ if (ra->mod == 0) {
958+#if 0
959+if (ra->reh4.buf[ra->reh4.r] == 6) {
960+printf("l=%d ", ra->reh4.l);
961+printf("next: %X ", ra->reh4.buf[ra->reh4.r]);
962+printf("%X ", ra->reh4.buf[(ra->reh4.r + 1) & (READ_EH4_BUFLEN - 1)]);
963+if (ra->reh4.l == 1) { puts("fill"); fill_reh4(&ra->reh4);
964+printf("l=%d ", ra->reh4.l);
965+printf("next: %X ", ra->reh4.buf[ra->reh4.r]);
966+printf("%X ", ra->reh4.buf[(ra->reh4.r + 1) & (READ_EH4_BUFLEN - 1)]);
967+}}
968+#endif
969+ j = getlen(&ra->reh4);
970+ i = getnum_u1(&ra->reh4);
971+ if (f == 0) {
972+ if (0x3c <= i && i <= 0x3f && j == 2)
973+ i = 0x40 << (i - 0x3c);
974+ if (0x1f0 <= i && i <= 0x1ff && j == 3)
975+ i = 0x400 << (i - 0x1f0);
976+ if (0xfc0 <= i && i <= 0xfc5 && j == 4)
977+ i = 0x4000000 << (i - 0xfc0);
978+ }
979+ if (i == -1) {
980+ i = getreg32idx(ra);
981+//printf("6_%d", i);
982+ if (i <= 0x3f || i >= 0x7ffffff4)
983+ i = read_reg32(ra, i);
984+ else if (i == 0x1e8) {
985+ j = getnum_api(ra, 0);
986+ ra->gosub = 1;
987+ ra->mod0 = ra->mod;
988+ ra->term = j & 1;
989+ insert_mini(ra, 1);
990+ k = getnum_api(ra, 0);
991+//printf("_%d_%d", j, k);
992+ if (k == 3)
993+ k++;
994+ if (ra->term != 0 && k == 1)
995+ insert_mini(ra, 0); /* byteだとterm-zeroがデフォルトになる */
996+ i = getnum_api(ra, 0);
997+//printf("_%d", i);
998+ if (j <= 1)
999+ ra->gosub = 0;
1000+ else if (j <= 3) {
1001+ insert_6_0(ra);
1002+ j = getnum_api(ra, 0);
1003+//printf("_%d", j);
1004+ ra->reh4_b = ra->reh4;
1005+ init_reh4(&ra->reh4, (UCHAR *) j);
1006+ } else {
1007+ puts("getnum_api: internal error: 6x...");
1008+ exit(1);
1009+ }
1010+//putchar(';');
1011+//putchar(' ');
1012+ ra->rep = i;
1013+ ra->mod = k * 8;
1014+ if (ra->term != 0)
1015+ i = -2;
1016+ else {
1017+ ra->rep++;
1018+#if 0
1019+ if (ra->rep == 0) {
1020+ // ra->term = 0;
1021+ ra->mod = ra->mod0;
1022+ ra->rep = 0x7fffffff;
1023+ if (ra->gosub != 0)
1024+ ra->reh4 = ra->reh4_b;
1025+ }
1026+#endif
1027+ }
1028+ } else {
1029+ puts("getnum_api: internal error: 6...");
1030+printf("i=%d", i);
1031+ exit(1);
1032+ }
1033+ }
1034+ } else if (ra->mod == 8) {
1035+ ra->reh4.p -= ra->reh4.l >> 1;
1036+ ra->reh4.w = ra->reh4.r;
1037+ ra->reh4.l &= 1;
1038+ ra->reh4.w = (ra->reh4.w + ra->reh4.l) & (READ_EH4_BUFLEN - 1);
1039+ i = *(ra->reh4.p)++;
1040+ } else if (ra->mod == 16) {
1041+ fill_reh4(&ra->reh4);
1042+ while ((((int) ra->reh4.p) & 1) != 0) {
1043+ ra->reh4.p--;
1044+ ra->reh4.l -= 2;
1045+ ra->reh4.w = (ra->reh4.w - 2 + READ_EH4_BUFLEN) & (READ_EH4_BUFLEN - 1);
1046+ }
1047+ while (ra->reh4.l >= 4) {
1048+ ra->reh4.p -= 2;
1049+ ra->reh4.l -= 4;
1050+ ra->reh4.w = (ra->reh4.w - 4 + READ_EH4_BUFLEN) & (READ_EH4_BUFLEN - 1);
1051+ }
1052+ i = *(unsigned short *) ra->reh4.p;
1053+ ra->reh4.p += 2;
1054+ } else if (ra->mod == 32) {
1055+ fill_reh4(&ra->reh4);
1056+ while ((((int) ra->reh4.p) & 3) != 0) {
1057+ ra->reh4.p--;
1058+ ra->reh4.l -= 2;
1059+ ra->reh4.w = (ra->reh4.w - 2 + READ_EH4_BUFLEN) & (READ_EH4_BUFLEN - 1);
1060+ }
1061+ while (ra->reh4.l >= 8) {
1062+ ra->reh4.p -= 4;
1063+ ra->reh4.l -= 8;
1064+ ra->reh4.w = (ra->reh4.w - 8 + READ_EH4_BUFLEN) & (READ_EH4_BUFLEN - 1);
1065+ }
1066+ i = *(int *) ra->reh4.p;
1067+ ra->reh4.p += 4;
1068+ } else {
1069+ puts("getnum_api: error");
1070+ exit(1);
1071+ }
1072+ if (ra->term != 0) {
1073+ if (i == ra->rep) {
1074+ ra->term = 0;
1075+ ra->mod = ra->mod0;
1076+ ra->rep = 0x7fffffff;
1077+ if (ra->gosub != 0)
1078+ ra->reh4 = ra->reh4_b;
1079+ if (f != 9)
1080+ goto retry;
1081+ return -2; /* term */
1082+ }
1083+ } else {
1084+ ra->rep--;
1085+ if (ra->rep == 0) { /* gosubからの帰還 */
1086+ // ra->term = 0;
1087+ ra->mod = ra->mod0;
1088+ ra->rep = 0x7fffffff;
1089+ if (ra->gosub != 0)
1090+ ra->reh4 = ra->reh4_b;
1091+ }
1092+ }
1093+fin:
1094+ if (osask7 != 0) {
1095+ osask7_buf = osask7_buf << 1 | ((i >> 7) & 1);
1096+ i &= 0x7f;
1097+fin1:
1098+ if (0x10 <= i && i <= 0x19)
1099+ i += 0x20;
1100+ if (0x1a <= i && i <= 0x1f)
1101+ i += 'A' - 0x1a;
1102+ }
1103+ #if (DEBUG_MODE)
1104+ printf("%d ", i);
1105+ #endif
1106+ return i;
1107+
1108+#if 0
1109+retry:
1110+ if (ra->rep == 0) {
1111+ ra->mod = ra->mod0;
1112+ do {
1113+ i = getnum_u1(&ra->reh4);
1114+ } while (i == 0);
1115+ if (i == 1) {
1116+ /* mode */
1117+ #if (DEBUG_MODE)
1118+ puts("getnum_api: mode");
1119+ #endif
1120+ j = getnum_u(&ra->reh4);
1121+ if ((j & 1) != 0) {
1122+ puts("getnum_api: error");
1123+ exit(1);
1124+ }
1125+ if ((j & 8) != 0) {
1126+ puts("getnum_api: error");
1127+ exit(1);
1128+ }
1129+ k = getnum_u(&ra->reh4);
1130+ if (k > 0)
1131+ k = ((j >> 7) + 1) << (k - 1);
1132+ ra->mod = k;
1133+ if ((j & 4) != 0)
1134+ ra->mod0 = k;
1135+ #if (DEBUG_MODE)
1136+ printf("api: mod%d ", ra->mod);
1137+ #endif
1138+ i = getnum_u(&ra->reh4);
1139+ if (i == 0)
1140+ goto retry;
1141+ ra->rep = i;
1142+ #if (DEBUG_MODE)
1143+ printf("api: modrep%d ", ra->rep);
1144+ #endif
1145+ if ((j & 2) != 0)
1146+ goto fin;
1147+ } else {
1148+ ra->rep = i - 1;
1149+ #if (DEBUG_MODE)
1150+ printf("api: rep%d ", ra->rep);
1151+ #endif
1152+ }
1153+ }
1154+#endif
1155+}
1156+
1157+void c_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int eax, int eip)
1158+{
1159+ UCHAR *cmd = (UCHAR *) edi, *p, *q, /* *r, */ tmpbuf[32], f3 = 1;
1160+ struct READ_API ra;
1161+ int i, j, k, l, m, n, o, h, edi0 = edi;
1162+ if (edi == 0)
1163+ cmd = (UCHAR *) eip;
1164+ init_ra(&ra, cmd);
1165+ ra.reg32 = &edi;
1166+ ra.st0 = &eip;
1167+ for (;;) {
1168+ if (f3 == 0)
1169+ goto fin;
1170+ f3--;
1171+ i = getnum_api(&ra, 0);
1172+ if (i == 0)
1173+ continue;
1174+//printf("i=%d ", i);
1175+ if (i == 3) {
1176+ if (f3 == 0) {
1177+ f3 = 0xff;
1178+ continue;
1179+ }
1180+fin:
1181+ if (edi0 == 0)
1182+ eip = (int) ra.reh4.p - (ra.reh4.l >> 1);
1183+ // i = (&eip)[2]; /* これがないとなぜか落ちる(CPUの隠れた仕様?:デバックでprintfしてて偶然発見) */
1184+ return;
1185+ }
1186+ if (i == 4) { /* 今のnaskfuncではRETで戻ったときのESI値は不定 */
1187+ if (exit_nl != 0 && last_putc != 0x0a && last_putc != 0)
1188+ putchar('\n');
1189+ i = getnum_api(&ra, 0);
1190+ if (i == 0)
1191+ exit(0);
1192+ if (i == 1)
1193+ exit(1);
1194+ if (i == 3) /* 数字を返す異常終了 */
1195+ exit(getnum_api(&ra, 0));
1196+ puts("c_api: func4: error");
1197+ exit(1);
1198+ }
1199+ if (i == 5) {
1200+ if (ra.mod == 0 && test_sig4(&ra, 4) == 0) /* デフォルトキャンセルは、4bitの4 */
1201+ ungetnum_gh4(&ra.reh4, 0);
1202+ if (getnum_api(&ra, 0) != 0) {
1203+ puts("c_api: func5: error");
1204+ exit(1);
1205+ }
1206+ insert_6x(&ra);
1207+ j = getnum_api(&ra, 1);
1208+enable_osask7();
1209+ if (j >= 0) {
1210+//printf("func5: l=%d ", j);
1211+ while (j > 0) {
1212+ putchar(last_putc = getnum_api(&ra, 0));
1213+ j--;
1214+ }
1215+ } else {
1216+ while ((j = getnum_api(&ra, 9)) >= 0)
1217+ putchar(last_putc = j);
1218+ }
1219+disable_osask7();
1220+ fflush(stdout);
1221+ } else if (i == 6) {
1222+ if (test_sig4(&ra, 5) != 0) {
1223+ if ((func06str->flags & 1) == 0) {
1224+ func06str->flags |= 1;
1225+ func06str->def_arg0 = 0x7fffffff;
1226+ if (test_sig4(&ra, 5) == 0)
1227+ func06str->def_arg0 = getnum_api(&ra, 0);
1228+ for (j = 0;; j++) {
1229+ i = getnum_api(&ra, 0);
1230+ if (i == 4) {
1231+ func06str->usgs = j;
1232+ break;
1233+ }
1234+ if (j == 64) {
1235+ puts("func6: internal error (2)");
1236+ exit(1);
1237+ }
1238+ if (i == 5)
1239+ i = 0xfff0 << 2 | 0;
1240+ if (i == 6)
1241+ i = 0xfff1 << 2 | 2;
1242+ if (i == 7) {
1243+ /* 改行 */
1244+ if (j == 0) {
1245+ puts("func6: internal error (3)");
1246+ exit(1);
1247+ }
1248+ j--;
1249+ for (i = 0; func06str->usg[j].usg[i] != '\0'; i++);
1250+ func06str->usg[j].usg[i] = 0x1f;
1251+ func06str->usg[j].usg[i + 1] = '\0';
1252+ continue;
1253+ }
1254+ func06str->usg[j].flg = i & 3;
1255+ i >>= 2;
1256+ if (i == 0) {
1257+ k = getnum_api(&ra, 0) + 1;
1258+ ra.reh4.p -= ra.reh4.l >> 1;
1259+ ra.reh4.l &= 1;
1260+ ra.reh4.w = (ra.reh4.r + ra.reh4.l) & (READ_EH4_BUFLEN - 1);
1261+ for (l = 0; l < k; l++)
1262+ func06str->usg[j].tag[l] = *(ra.reh4.p)++;
1263+ func06str->usg[j].tag[l] = '\0';
1264+ func06str->usg[j].typ = getnum_api(&ra, 0);
1265+ k = getnum_api(&ra, 0);
1266+ ra.reh4.p -= ra.reh4.l >> 1;
1267+ ra.reh4.l &= 1;
1268+ ra.reh4.w = (ra.reh4.r + ra.reh4.l) & (READ_EH4_BUFLEN - 1);
1269+ for (l = 0; l < k; l++)
1270+ func06str->usg[j].usg[l] = *(ra.reh4.p)++;
1271+ func06str->usg[j].usg[l] = '\0';
1272+ } else if (i == 2) { /* in:input-file */
1273+ func06str->usg[j].tag[0] = 'i';
1274+ func06str->usg[j].tag[1] = 'n';
1275+ func06str->usg[j].tag[2] = '\0';
1276+ func06str->usg[j].typ = 0;
1277+ func06str->usg[j].usg[0] = 0x01;
1278+ func06str->usg[j].usg[1] = 'p';
1279+ func06str->usg[j].usg[2] = 'u';
1280+ func06str->usg[j].usg[3] = 't';
1281+ func06str->usg[j].usg[4] = '-';
1282+ func06str->usg[j].usg[5] = 0x02;
1283+ func06str->usg[j].usg[6] = '\0';
1284+ } else if (i == 3) { /* out:output-file */
1285+ func06str->usg[j].tag[0] = 'o';
1286+ func06str->usg[j].tag[1] = 'u';
1287+ func06str->usg[j].tag[2] = 't';
1288+ func06str->usg[j].tag[3] = '\0';
1289+ func06str->usg[j].typ = 0;
1290+ func06str->usg[j].usg[0] = 0x01;
1291+ func06str->usg[j].usg[1] = 'p';
1292+ func06str->usg[j].usg[2] = 'u';
1293+ func06str->usg[j].usg[3] = 't';
1294+ func06str->usg[j].usg[4] = '-';
1295+ func06str->usg[j].usg[5] = 0x02;
1296+ func06str->usg[j].usg[6] = '\0';
1297+ } else if (i == 0xfff0) { /* */
1298+ puts("func6: internal error (6)");
1299+ exit(1);
1300+ } else if (i == 0xfff1) { /* */
1301+ puts("func6: internal error (5)");
1302+ exit(1);
1303+ } else {
1304+ puts("func6: internal error (4)");
1305+ printf("i=%d j=%d\n", i, j);
1306+ exit(1);
1307+ }
1308+ if (test_sig4(&ra, 5) != 0) { /* デフォルト値設定 */
1309+ puts("func6: internal error (7)");
1310+ exit(1);
1311+ }
1312+ }
1313+ // func06_putusage();
1314+ func06_setup();
1315+ } else {
1316+ func06_putusage();
1317+ }
1318+ } else {
1319+ if ((func06str->flags & 1) == 0) {
1320+ func06str->flags |= 1;
1321+ func06_setup();
1322+ }
1323+ i = getnum_api(&ra, 1);
1324+ if (i == 0x3f) {
1325+ func06_putusage();
1326+ exit(1);
1327+ }
1328+ if (i == 0x3e) {
1329+ i = getnum_api(&ra, 1);
1330+ j = getreg32idx(&ra);
1331+ for (l &= 0, k = 1; func06str->argv_f[k] != 0xff; k++) {
1332+ if ((func06str->argv_f[k] & 0x7f) == i)
1333+ l++;
1334+ }
1335+ if (func06str->usg[i].typ == 0xff)
1336+ l = 1;
1337+ if (func06str->usg[i].typ == 0xfe) {
1338+ for (l = 1, k = 1; func06str->argv_f[k] != 0xff; k++) {
1339+ if ((func06str->argv_f[k] & 0x80) != 0)
1340+ l++;
1341+ }
1342+ }
1343+ write_reg32(&ra, j, l);
1344+ continue;
1345+ }
1346+ j &= 0;
1347+ k = 1;
1348+ if (0x3c <= i && i <= 0x3d) { /* argの内容を加工せずに出力(エラー用), 3dは整理してから表示&タグは出ない */
1349+ l = i;
1350+ i = getnum_api(&ra, 1);
1351+ if (i >= func06str->usgs) {
1352+ puts("func6: internal error (9)");
1353+ exit(1);
1354+ }
1355+ if ((func06str->usg[i].flg & 2) != 0) {
1356+ insert_6_0(&ra);
1357+ j = getnum_api(&ra, 0);
1358+ }
1359+ if (func06str->usg[i].typ == 0xff) { /* コマンドライン全体型 */
1360+ printf(func06str->argv_all);
1361+ last_putc = 1;
1362+ continue;
1363+ } else if (func06str->usg[i].typ == 0xfe) {
1364+ /* コマンドライン部分型 */
1365+ p = func06str->argv0;
1366+ if (j > 0) {
1367+ do {
1368+ while (k < main_argc && (func06str->argv_f[k] & 0x80) == 0)
1369+ k++;
1370+ k++;
1371+ } while (--j > 0);
1372+ k--;
1373+ p = NULL;
1374+ if (k < main_argc)
1375+ p = main_argv[k];
1376+ }
1377+ if (p != NULL) {
1378+ printf(p);
1379+ last_putc = 1;
1380+ }
1381+ continue;
1382+ }
1383+ k = 1;
1384+ do {
1385+ while (k < main_argc && (func06str->argv_f[k] & 0x7f) != i)
1386+ k++;
1387+ k++;
1388+ } while (--j >= 0);
1389+ k--;
1390+ p = NULL;
1391+ if (k < main_argc) {
1392+ p = main_argv[k];
1393+ if ((func06str->argv_f[k] & 0x80) == 0 && l == 0x3d)
1394+ p += strlen(func06str->usg[i].tag) + 1;
1395+ }
1396+ if (p != NULL) {
1397+ printf(p);
1398+ last_putc = 1;
1399+ }
1400+ continue;
1401+ }
1402+ if (i >= func06str->usgs) {
1403+ puts("func6: internal error (8)");
1404+ exit(1);
1405+ }
1406+ if ((func06str->usg[i].flg & 2) != 0) {
1407+ insert_6_0(&ra);
1408+ j = getnum_api(&ra, 0);
1409+ }
1410+ if (func06str->usg[i].typ == 0xff) { /* コマンドライン全体型 */
1411+ p = func06str->argv_all;
1412+ goto func06_typ03;
1413+ } else if (func06str->usg[i].typ == 0xfe) {
1414+ /* コマンドライン部分型 */
1415+ p = func06str->argv0;
1416+ if (j > 0) {
1417+ do {
1418+ while (k < main_argc && (func06str->argv_f[k] & 0x80) == 0)
1419+ k++;
1420+ k++;
1421+ } while (--j > 0);
1422+ k--;
1423+ p = NULL;
1424+ if (k < main_argc)
1425+ p = main_argv[k];
1426+ }
1427+ j = getreg32idx(&ra);
1428+ k &= 0;
1429+ if (p != NULL)
1430+ k |= 1;
1431+ write_reg32(&ra, j, k);
1432+ goto func06_typ03;
1433+ }
1434+ k = 1;
1435+ do {
1436+ while (k < main_argc && (func06str->argv_f[k] & 0x7f) != i)
1437+ k++;
1438+ k++;
1439+ } while (--j >= 0);
1440+ k--;
1441+ p = NULL;
1442+ if (k < main_argc) {
1443+ p = main_argv[k];
1444+ if ((func06str->argv_f[k] & 0x80) == 0)
1445+ p += strlen(func06str->usg[i].tag) + 1;
1446+ }
1447+ if ((func06str->usg[i].flg & 3) != 0 /* || func06str->usg[i].typ == 2 */) {
1448+ j = getreg32idx(&ra);
1449+ k &= 0;
1450+ if (p != NULL)
1451+ k |= 1;
1452+ write_reg32(&ra, j, k);
1453+ }
1454+ if (func06str->usg[i].typ == 0) {
1455+ i = getnum_api(&ra, 0); /* opt */
1456+ j = getnum_api(&ra, 0); /* slot */
1457+
1458+if (p != NULL && i != 0 && i != 3) printf("debug: cmdlin_fopen: %d %d %s\n", i, j, p);
1459+
1460+ if (p != NULL)
1461+ goto fopen_i_j_p;
1462+ } else if (func06str->usg[i].typ == 1) {
1463+ j = getreg32idx(&ra);
1464+ if (p != NULL) {
1465+ k = my_strtol(p, NULL);
1466+ write_reg32(&ra, j, k);
1467+ }
1468+ } else if (func06str->usg[i].typ == 2) {
1469+ /* 何もしない、フラグは必ず省略可能フラグが1 */
1470+ } else if (func06str->usg[i].typ == 3) {
1471+func06_typ03:
1472+ /* = 2 max ? ? */
1473+ /* = 3 max ? */
1474+ j = getnum_api(&ra, 0);
1475+ if (j < 2 || 3 < j) {
1476+ puts("func6: mode != 2/3");
1477+ exit(1);
1478+ }
1479+ m = getnum_api(&ra, 0); /* max */
1480+ l |= -1;
1481+ if (j == 2)
1482+ l = getreg32idx(&ra);
1483+ insert_6_0(&ra);
1484+ q = (void *) getnum_api(&ra, 0);
1485+ if (p != NULL) {
1486+ if ((k = strlen(p)) >= m) {
1487+ if (last_putc != '\n' && last_putc != 0)
1488+ putchar('\n');
1489+ if (m < 48)
1490+ printf("Too long Command line (\"%s\" max:%d)\n", p, m);
1491+ else
1492+ printf("Too long Command line (max:%d)\n", m);
1493+ exit(1);
1494+ }
1495+ while (*p != '\0')
1496+ *q++ = *p++;
1497+ if (l < 0)
1498+ *q = '\0';
1499+ if (l >= 0)
1500+ write_reg32(&ra, l, k);
1501+ }
1502+ } else {
1503+ puts("func6: internal error (0)");
1504+ exit(1);
1505+ }
1506+ }
1507+ } else if (i == 0x00ffffff) {
1508+//printf("junk: ");
1509+ i = getnum_api(&ra, 0);
1510+//printf("%d ", i);
1511+ if (i == 1) {
1512+ /* レガシーコマンドライン取得 */
1513+ /* 1つの文字列で与えられる */
1514+ /* エンコードはIBM-US */
1515+ /* opt バッファ最大, バッファ使用量, バッファの中身 */
1516+ /* これはそういうもの。この文脈で6_505がくるとcの前に最大サイズが入る */
1517+ /* そしてgh4モードのときに限りこの6_505は省略できる。 */
1518+ /* 6_505以外を使いたいときは、6を置け。そうすればあとは自由だ */
1519+
1520+ /* 典型例 0 (6_505_)2_(1)_max_(6_0_)?(reg32/st[]で受ける)_(6_0_)? */
1521+ /* = 0 2 max ? ? */
1522+ /* 0 2 max 6 6 3 3 0 ? ? */
1523+ /* 典型例 0 (6_505_)3_(1)_max_(0)_(6_0_)? */
1524+ /* = 0 3 max ? */
1525+
1526+ /* 以下古い */
1527+ /* opt, [書き込み即値], バッファ最大サイズ, バッファアドレス */
1528+ /* 6_5...じゃだめなのか? ほんとはそのほうがいい */
1529+ /* でこれだと基本形は opt, [書き込み即値], バッファ最大サイズ, バッファの中身 になる */
1530+ /* しかしいつも6でエスケープするのが無駄なので、6を省略。むしろ6が来たら通常へ。 */
1531+ /* そうすると(6)6を表現できないが、それは(6)66で。 */
1532+
1533+ if ((func06str->flags & 1) == 0) {
1534+ func06str->flags |= 1;
1535+ func06_setup();
1536+ }
1537+
1538+ if (getnum_api(&ra, 0) != 0 && ra.mod != 0) {
1539+ puts("jg01_getcmdlin: opt!=0");
1540+ exit(1);
1541+ }
1542+ k = getnum_api(&ra, 0);
1543+ if (k < 2 || 3 < k) {
1544+ puts("jg01_getcmdlin: mode!=2_ or 3_");
1545+ exit(1);
1546+ }
1547+ insert_mini(&ra, 1);
1548+ if (getnum_api(&ra, 0) != 1) {
1549+ printf("jg01_getcmdlin: mode!=%d_1", k);
1550+ exit(1);
1551+ }
1552+ j = getnum_api(&ra, 0); /* max */
1553+ /* 本来はここで4がきていないことを確認するべき */
1554+ i |= -1;
1555+ if (k == 2)
1556+ i = getnum_api(&ra, 0); /* *len */
1557+ insert_6_0(&ra);
1558+ p = (UCHAR *) getnum_api(&ra, 0);
1559+ if ((k = strlen(func06str->argv_all)) >= j) {
1560+ if (last_putc != '\n' && last_putc != 0)
1561+ putchar('\n');
1562+ printf("Too long Command line (max:%d)\n", j);
1563+ exit(1);
1564+ }
1565+ q = func06str->argv_all;
1566+ while (*q != '\0')
1567+ *p++ = *q++;
1568+ if (i < 0)
1569+ *p = '\0';
1570+
1571+#if 0
1572+ q = main_argv[0];
1573+ while (*q != 0)
1574+ q++;
1575+ q--;
1576+ while (q > main_argv[0] && q[-1] != '\\' && q[-1] != '/')
1577+ q--;
1578+ k = 0;
1579+ l = 1;
1580+ for (;;) {
1581+ for (r = q; *r != 0 && *r != 0x20; r++);
1582+ if (*r != 0) {
1583+ if (j > k)
1584+ p[k] = 0x22;
1585+ k++;
1586+ }
1587+ while (*q != 0) {
1588+ if (j > k)
1589+ p[k] = *q;
1590+ q++;
1591+ k++;
1592+ }
1593+ if (*r != 0) {
1594+ if (j > k)
1595+ p[k] = 0x22;
1596+ k++;
1597+ }
1598+ if (main_argc <= l)
1599+ break;
1600+ if (j > k)
1601+ p[k] = 0x20;
1602+ k++;
1603+ q = main_argv[l++];
1604+ }
1605+ if (j > k)
1606+ p[k] = 0; /* これはサービス。この動作は保証されない */
1607+ if (bufmod != 0 && ((i >= 0 && k >= j) || (i < 0 && k >= j - 1))) {
1608+ if (last_putc != '\n' && last_putc != 0)
1609+ putchar('\n');
1610+ printf("Too long Command line (max:%d)\n", k);
1611+ exit(1);
1612+ }
1613+#endif
1614+ if (i >= 0) {
1615+ if (i < 8)
1616+ ra.reg32[7 - i] = k;
1617+ else
1618+ ra.st0[i - 8] = k;
1619+ }
1620+ } else if (i == 3) {
1621+ /* まだtekの自動展開を入れてない(p[1]とかを使う), -nodecmp */
1622+ i = getnum_api(&ra, 0) >> 3; /* mode/opt */
1623+ j = getnum_api(&ra, 0);
1624+ if (getnum_api(&ra, 0) != 3) {
1625+ puts("jg01_fopen: mode!=3_");
1626+ exit(1);
1627+ }
1628+ insert_6_0(&ra);
1629+ p = (UCHAR *) getnum_api(&ra, 0);
1630+ if (j < 4 || j > 63) {
1631+ printf("c_api: junk_fopen: slot over %d\n", j);
1632+ exit(1);
1633+ }
1634+#if 0
1635+ if (p[m] != 0) {
1636+ puts("c_api: junk_fopen: mikan error");
1637+ exit(1);
1638+ }
1639+#endif
1640+ fopen_i_j_p:
1641+ if (slot[j].t == 2) {
1642+ if (slot[j].p[2] == 0)
1643+ fclose((FILE *) slot[j].p[0]);
1644+ else {
1645+ if (slot[j].p[1] > 0)
1646+ free((void *) slot[j].p[2]);
1647+ }
1648+ slot[j].t = 0;
1649+ }
1650+ if (i == 0) {
1651+ slot[j].t = 2;
1652+ slot[j].p[0] = (int) fopen(p, "rb");
1653+ slot[j].p[1] = slot[j].p[2] = 0;
1654+ if (slot[j].p[0] == 0) {
1655+ slot[j].t = 0;
1656+ if (errmod != 0) {
1657+ if (last_putc != '\n' && last_putc != 0)
1658+ putchar('\n');
1659+ printf("File read open error: %s\n", p);
1660+ exit(1);
1661+ }
1662+ } else if ((main_flags & 2) == 0) {
1663+ m = fread(tmpbuf, 1, 32, (FILE *) slot[j].p[0]);
1664+ fseek((FILE *) slot[j].p[0], 0, SEEK_SET);
1665+ for (; m < 32; m++)
1666+ tmpbuf[m] = 0;
1667+ if ((m = tek_getsize(tmpbuf)) > 0) {
1668+ fseek((FILE *) slot[j].p[0], 0, SEEK_END);
1669+ k = ftell((FILE *) slot[j].p[0]);
1670+ fseek((FILE *) slot[j].p[0], 0, SEEK_SET);
1671+ p = malloc(m);
1672+ q = malloc(k);
1673+ if (p == NULL || q == NULL) {
1674+ if (p != NULL)
1675+ free(p);
1676+ if (q != NULL)
1677+ free(q);
1678+ } else {
1679+ if (fread(q, 1, k, (FILE *) slot[j].p[0]) != k) {
1680+ decomperr:
1681+ free(p);
1682+ free(q);
1683+ fseek((FILE *) slot[j].p[0], 0, SEEK_SET);
1684+ } else {
1685+ if (m > 0 && tek_decomp(q, p, k) != 0)
1686+ goto decomperr;
1687+ free(q);
1688+ fclose((FILE *) slot[j].p[0]);
1689+ slot[j].p[1] = m; /* fsiz */
1690+ slot[j].p[2] = (int) p; /* fbuf */
1691+ slot[j].p[0] = 0; /* fpos */
1692+ }
1693+ }
1694+ }
1695+ }
1696+ } else if (i == 3) {
1697+ slot[j].t = 2;
1698+ slot[j].p[0] = (int) fopen(p, "wb");
1699+ if (slot[j].p[0] == 0) {
1700+ slot[j].t = 0;
1701+ if (errmod != 0) {
1702+ if (last_putc != '\n' && last_putc != 0)
1703+ putchar('\n');
1704+ printf("File write open error: %s\n", p);
1705+ exit(1);
1706+ }
1707+ }
1708+ } else {
1709+ puts("c_api: junk_fopen: error");
1710+ exit(1);
1711+ }
1712+ } else if (i == 2) {
1713+ getnum_api(&ra, 0); /* optのskip, いろんなタイプのslotをクローズさせる, 連続closeなど */
1714+ /* 個数指定がbit0, タイプ比較がbit1 */
1715+ j = getnum_api(&ra, 0);
1716+ if (j < 4 || j > 63) {
1717+ printf("c_api: junk_fclose: slot over %d\n", j);
1718+ exit(1);
1719+ }
1720+ if (slot[j].t == 2) {
1721+ if (slot[j].p[2] == 0)
1722+ fclose((FILE *) slot[j].p[0]);
1723+ else {
1724+ if (slot[j].p[1] > 0)
1725+ free((void *) slot[j].p[2]);
1726+ }
1727+ slot[i].t = 0;
1728+ }
1729+ } else if (i == 4) {
1730+ i = getnum_api(&ra, 0); /* opt: bit0 r/w, bit1 partial/full */
1731+ j = getnum_api(&ra, 0); /* slot */
1732+ n = getnum_api(&ra, 0); /* 2 or 3 */
1733+ /* fread1: 0 s 2 n (6_0) l b */
1734+ /* fread0: 0 s 3 n b */
1735+ /* fread1f: 2 s 2 n (6_0) l b */
1736+ /* fread0f: 2 s 3 n b */
1737+ /* fwrite1: 1 s 2 n (6_0) l b */
1738+ /* fwrite0: 1 s 3 (6_0) l b */
1739+ /* fwrite1f: 3 s 2 n b */
1740+ /* fwrite0f: 3 s 3 b */
1741+ k = -1;
1742+ if ((i & 1) == 0 || n == 2)
1743+ k = getnum_api(&ra, 0); /* n */
1744+ o = -1;
1745+ if (((i & 1) == 0 && n == 2) || i == 1)
1746+ o = getnum_api(&ra, 0); /* *len */
1747+ insert_6_0(&ra);
1748+ p = (void *) getnum_api(&ra, 0); /* p */
1749+ if (k < 0)
1750+ k = strlen(p);
1751+ if (j < 4 || j > 63) {
1752+ printf("c_api: junk_freadwrite: slot over %d\n", j);
1753+ exit(1);
1754+ }
1755+ m |= -1;
1756+ if (slot[j].t == 2) {
1757+ m &= 0;
1758+ if ((i & 1) == 0) { /* fread */
1759+ if (n == 3)
1760+ k--;
1761+ if (slot[j].p[2] == 0) {
1762+ // if (!feof((FILE *) slot[j].p[0])) /* tolsetのwin32のfeofはバグがある模様 */
1763+ m = fread(p, 1, k, (FILE *) slot[j].p[0]);
1764+ } else {
1765+ h = k;
1766+ while (h > 0 && slot[j].p[0] < slot[j].p[1]) {
1767+ p[m++] = ((char *) slot[j].p[2])[slot[j].p[0]++];
1768+ h--;
1769+ }
1770+ }
1771+ if (n == 3)
1772+ p[m] = 0x00;
1773+ if ((i & 2) != 0 && m >= k) {
1774+ if (last_putc != '\n' && last_putc != 0)
1775+ putchar('\n');
1776+ printf("File read buffer full (max:%d): (filepath)\n", k);
1777+ exit(1);
1778+ }
1779+//printf("fread: %d(%d)\n", m, k);
1780+ } else { /* fwrite */
1781+ if (k > 0) {
1782+ m = fwrite(p, 1, k, (FILE *) slot[j].p[0]);
1783+ if ((i & 2) != 0 && m < k) {
1784+ if (last_putc != '\n' && last_putc != 0)
1785+ putchar('\n');
1786+ puts("File write error: (filepath)");
1787+ exit(1);
1788+ }
1789+ }
1790+//printf("fwrite: %d\n", m);
1791+ }
1792+ }
1793+ if (o >= 0) {
1794+ if (o < 8)
1795+ ra.reg32[7 - o] = m;
1796+ else
1797+ ra.st0[o - 8] = m;
1798+ }
1799+#if 0
1800+ } else if (i == 6) {
1801+ i = getnum_api(&ra); /* bit0-2 : pの長さ */
1802+ j = getnum_api(&ra);
1803+ if (j < 4 || j > 63) {
1804+ printf("c_api: junk_testslot: slot over %d\n", j);
1805+ exit(1);
1806+ }
1807+ p = (void *) getnum_api(&ra); /* p */
1808+ *(char *) p = slot[j].t;
1809+#endif
1810+ } else if (i == 7) {
1811+ i = getnum_api(&ra, 0);
1812+ if (i == 1) {
1813+ insert_6_0(&ra);
1814+ j = tek_getsize((char *) getnum_api(&ra, 0));
1815+ } else if (i == 2) {
1816+ i = getnum_api(&ra, 0); /* tekサイズ */
1817+ insert_6_0(&ra);
1818+ j = getnum_api(&ra, 0); /* tekポインタ */
1819+ insert_6_0(&ra);
1820+ j = tek_decomp((char *) j, (char *) getnum_api(&ra, 0), i);
1821+ } else if (i == 3) {
1822+ i = getnum_api(&ra, 0);
1823+ if (getnum_api(&ra, 0) != 2) {
1824+ puts("c_api: rjc error");
1825+ exit(1);
1826+ }
1827+ j = getnum_api(&ra, 0);
1828+ insert_6_0(&ra);
1829+ p = (char *) getnum_api(&ra, 0);
1830+ k = getnum_api(&ra, 0);
1831+ l = getnum_api(&ra, 0);
1832+ j = rjc(p, p + j, k, k + l, k + getnum_api(&ra, 0), i);
1833+ } else {
1834+ printf("c_api: junk_tek: error i=%d\n", i);
1835+ exit(1);
1836+ }
1837+ k = getnum_api(&ra, 0);
1838+ if (k >= 0) {
1839+ if (k < 8)
1840+ ra.reg32[7 - k] = j;
1841+ else
1842+ ra.st0[k - 8] = j;
1843+ }
1844+ } else if (i == 8) {
1845+ /* malloc: 0 n (6_0_)p */
1846+ getnum_api(&ra, 0);
1847+ i = getnum_api(&ra, 0);
1848+ j = getnum_api(&ra, 0);
1849+ q = malloc(i);
1850+ if (q == NULL) {
1851+ printf("Out of memory (need:%d)\n", i);
1852+ exit(1);
1853+ }
1854+ if (j < 8)
1855+ ra.reg32[7 - j] = (int) q;
1856+ else
1857+ ra.st0[j - 8] = (int) q;
1858+ } else if (i == 9) {
1859+ getnum_api(&ra, 0);
1860+ getnum_api(&ra, 0);
1861+ getnum_api(&ra, 0);
1862+ } else if (i == 10) {
1863+ getnum_api(&ra, 0);
1864+ i = getnum_api(&ra, 0) - 32; /* 分解能(1/4294967296が基準) */
1865+ j = getnum_api(&ra, 0);
1866+ #if (USE_WIN32)
1867+ j *= 1000;
1868+ if (i > 0)
1869+ j <<= i;
1870+ if (i < 0)
1871+ j = (j + ~(-1 << (- i))) >> (- i);
1872+ Sleep(j);
1873+ #elif (USE_POSIX)
1874+ {
1875+ struct timespec ts0, ts1;
1876+ j *= 1000;
1877+ if (i > 0)
1878+ j <<= i;
1879+ if (i < 0)
1880+ j = (j + ~(-1 << (- i))) >> (- i);
1881+ ts0.tv_sec = j / 1000;
1882+ ts0.tv_nsec = (j % 1000) * 1000000;
1883+//printf("%d %d %d\n", j, ts0.tv_sec, ts0.tv_nsec);
1884+//exit(1);
1885+ while (nanosleep(&ts0, &ts1) != 0)
1886+ ts0 = ts1;
1887+ }
1888+ #else
1889+ puts("c_api: junk_sleep: not supported");
1890+ exit(1);
1891+ #endif
1892+ } else if (i == 11) {
1893+ #if (USE_WIN32 || USE_POSIX)
1894+ i = getnum_api(&ra, 0);
1895+ j = inkey();
1896+ k = getreg32idx(&ra);
1897+ if (i < 2 || i > 3) {
1898+ printf("c_api: junk_inkey: bad option (%d)\n", i);
1899+ exit(1);
1900+ }
1901+ if (i == 3) {
1902+ while (j == 0) {
1903+ #if (USE_WIN32)
1904+ Sleep(10);
1905+ #elif (USE_POSIX)
1906+ struct timespec ts;
1907+ ts.tv_sec = 0;
1908+ ts.tv_nsec = 10000000;
1909+ nanosleep(&ts, NULL);
1910+ #endif
1911+ j = inkey();
1912+ }
1913+ }
1914+ write_reg32(&ra, k, j);
1915+ #endif
1916+ } else if (i == 12) {
1917+ for (;;) {
1918+ i = getnum_api(&ra, 0);
1919+ if (i == 0)
1920+ break;
1921+ if (i == 1) {
1922+ j = getnum_api(&ra, 0);
1923+ consctrl1(j, getnum_api(&ra, 0));
1924+ }
1925+ if (i == 2) {
1926+ j = getnum_api(&ra, 0);
1927+ consctrl2(j, getnum_api(&ra, 0));
1928+ }
1929+ if (i == 3)
1930+ consctrl3();
1931+ if (i == 4) {
1932+ j = getnum_api(&ra, 0);
1933+ consctrl4(j, getnum_api(&ra, 0));
1934+ }
1935+ }
1936+ } else if (i == 13) {
1937+ #if (USE_WIN32 || USE_POSIX)
1938+ time_t timer;
1939+ getnum_api(&ra, 0);
1940+ k = getreg32idx(&ra);
1941+ time(&timer);
1942+ write_reg32(&ra, k, (int) timer);
1943+ #endif
1944+ } else {
1945+ printf("c_api: junk_error i=%d\n", i);
1946+ exit(1);
1947+ }
1948+ } else {
1949+ printf("c_api: error i=%d\n", i);
1950+ exit(1);
1951+ }
1952+ }
1953+}
1954+
1955+int rjc(UCHAR *p0, UCHAR *p1, int ofs0, int ofs, int ofs1, int mode)
1956+/* mode: 0:decode, 1:encode */
1957+{
1958+ UCHAR *p = p0, *pp = p0 - 4;
1959+ int i, j, k, m = 0;
1960+ while (p < p1) {
1961+ if (0xe8 <= *p && *p <= 0xe9 && &p[4] < p1) { /* e8 (call32), e9 (jmp32) */
1962+ r32:
1963+ p++;
1964+ if (p - pp < 4)
1965+ continue;
1966+ i = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
1967+ k = i;
1968+ j = (p - p0) + ofs + 4; /* 相対アドレス基点 */
1969+ pp = p;
1970+ if (i == 0 || i == 0x80808080) {
1971+ i ^= 0x80808080;
1972+ // puts("rjc: warning: extend convert enabled");
1973+ /*
1974+ この拡張はabcdw006で導入された。それ以前のものについては、
1975+ この条件に当てはまるパターンが存在しないだろうということで、
1976+ 互換性に問題なしとしている。この警告は古いbim2g01で生成された
1977+ バイナリにのみ意味があり、古いバイナリでこれが出たら互換性に
1978+ 問題があることを示している。
1979+ のちにテストしたところ、以前のものについてもこの条件に当て
1980+ はまるパターンは存在していた。golib00, obj2bim, naskが該当。
1981+
1982+ この拡張が導入されたいきさつ:
1983+ 現在はDLLをサポートしていないので影響はないのだが、将来DLL等を
1984+ サポートした際には、.textセクション内で E8 00 00 00 00 という
1985+ コードがそれなりの頻度で出現する可能性が高い。これをこの拡張
1986+ 変換なしで処理すると、それぞれのアドレスは出現位置によって
1987+ 異なる数値に変換されることになり、これは圧縮効率を低下させる
1988+ 要因となる。
1989+ ということで、従来のrjc変換の前に00000000と80808080とを交換する
1990+ ことで、ほぼ確実にrjc変換対象から除外し、すべて同じ値に
1991+ 変換されることを期待する。ちなみにrjcの後にまた00000000と80808080
1992+ とを交換する。これで0は0のままになる可能性が高い。
1993+ 80808080にした理由だが、もっともrjc変換から除外される可能性が
1994+ 高いのは80000000であり、しかしこれはefg01の圧縮が効きにくい可能性が
1995+ あるので、80808080にした。
1996+ */
1997+ }
1998+ if (mode) { /* encode */
1999+ if (ofs0 - j <= i && i < ofs1 - j)
2000+ i += j;
2001+ else if (ofs1 - j <= i && i < ofs1)
2002+ i -= ofs1 - ofs0;
2003+ } else { /* decode */
2004+ if (ofs0 <= i && i < ofs1)
2005+ i -= j;
2006+ else if (ofs0 - j <= i && i < ofs0)
2007+ i += ofs1 - ofs0;
2008+ }
2009+ if (i == 0 || i == 0x80808080) {
2010+ if (i == 0)
2011+ puts("rjc: warning: extend convert enabled"); /* 上記コメント参照 */
2012+ i ^= 0x80808080;
2013+ }
2014+ if (i != k) {
2015+ p[0] = i & 0xff;
2016+ p[1] = (i >> 8) & 0xff;
2017+ p[2] = (i >> 16) & 0xff;
2018+ p[3] = (i >> 24) & 0xff;
2019+ p += 4;
2020+ m++;
2021+ }
2022+ continue;
2023+ }
2024+ p++;
2025+ if (p[-1] == 0x0f && &p[4] < p1 && (p[0] & 0xf0) == 0x80) /* 0f 8x (jcc32) */
2026+ goto r32;
2027+ }
2028+ return m;
2029+}
2030+
2031+void func06_init()
2032+{
2033+ UCHAR *p;
2034+ func06str = malloc(sizeof (struct FUNC06STR));
2035+ func06str->flags &= 0;
2036+ func06str->usgs = 2;
2037+ func06str->def_arg0 = 1;
2038+ func06str->usg[0].tag[0] = '\0';
2039+ func06str->usg[0].flg = 0;
2040+ func06str->usg[0].usg[0] = '\0';
2041+ func06str->usg[0].typ = 0xff;
2042+ func06str->usg[1].tag[0] = '\0';
2043+ func06str->usg[1].flg = 3;
2044+ func06str->usg[1].usg[0] = '\0';
2045+ func06str->usg[1].typ = 0xfe; /* 3:str */
2046+ for (func06str->argv0 = p = *main_argv; *p != '\0'; p++) {
2047+ if (*p == '\\' || *p == '/')
2048+ func06str->argv0 = p + 1;
2049+ }
2050+ return;
2051+}
2052+
2053+char func06_putusage_sub(int i, char f, char g)
2054+{
2055+ int j;
2056+ char c, d = 0;
2057+ g |= func06str->usg[i].flg;
2058+ if ((g & 1) != 0)
2059+ putchar('[');
2060+ if (func06str->usg[i].typ != 2) {
2061+ if (f)
2062+ putchar('[');
2063+ }
2064+ printf(func06str->usg[i].tag);
2065+ if (func06str->usg[i].typ != 2) {
2066+ putchar(':');
2067+ if (f)
2068+ putchar(']');
2069+ }
2070+ for (j = 0; (c = func06str->usg[i].usg[j]) != '\0'; j++) {
2071+ if (c >= ' ')
2072+ putchar(c);
2073+ else if (c == 0x01)
2074+ printf(func06str->usg[i].tag);
2075+ else if (c == 0x02)
2076+ printf("file");
2077+ else if (c == 0x1f)
2078+ d = 1;
2079+ else {
2080+ puts("func06_putusage_sub: error");
2081+ printf("c=0x%02x\n", c);
2082+ exit(1);
2083+ }
2084+ }
2085+ if ((g & 1) != 0)
2086+ putchar(']');
2087+ return d;
2088+}
2089+
2090+void func06_putusage()
2091+{
2092+ int i;
2093+ char f = 0, g;
2094+ printf("usage>%s", func06str->argv0);
2095+ if (func06str->usg[0].tag[0] == '\0') {
2096+ puts(" - legacy-mode -");
2097+ return;
2098+ }
2099+ for (i = 0; i < func06str->usgs; i++) {
2100+ if (i == func06str->def_arg0)
2101+ f = 1;
2102+ putchar(' ');
2103+ g = func06_putusage_sub(i, f, 0);
2104+ if ((func06str->usg[i].flg & 2) != 0) {
2105+ putchar(' ');
2106+ func06_putusage_sub(i, f, 1);
2107+ printf("...");
2108+ f = 0;
2109+ }
2110+ if (g)
2111+ printf("\n ");
2112+ }
2113+ putchar('\n');
2114+ return;
2115+}
2116+
2117+void func06_setup()
2118+{
2119+ UCHAR *p, *q = func06str->argv_all, *q0, f;
2120+ int i, j, k;
2121+ p = func06str->argv0;
2122+ while (*p != '\0')
2123+ *q++ = *p++;
2124+ *q++ = ' ';
2125+ for (i = 1; i < main_argc; i++) {
2126+ func06str->argv_f[i] = 0x7e;
2127+ p = main_argv[i];
2128+ for (j = 0; j < func06str->usgs; j++) {
2129+ for (k = 0; func06str->usg[j].tag[k] != '\0' && p[k] != '\0'; k++) {
2130+ if (func06str->usg[j].tag[k] != p[k])
2131+ break;
2132+ }
2133+ if (func06str->usg[j].tag[k] == '\0' && (p[k] == ':' || (p[k] == '\0' && func06str->usg[j].typ == 2))) {
2134+ func06str->argv_f[i] = j;
2135+ func06str->usg[j].flg |= 4;
2136+ break;
2137+ }
2138+ if (strcmp(p, "-efg01-lc") == 0) {
2139+ func06str->argv_f[i] = 0x7d;
2140+ main_flags |= 1; /* lesser compatible mode */
2141+ break;
2142+ }
2143+ if (strcmp(p, "-efg01-noadc") == 0) {
2144+ func06str->argv_f[i] = 0x7d;
2145+ main_flags |= 2; /* no auto de-compress mode */
2146+ break;
2147+ }
2148+ if (strcmp(p, "-usage") == 0) {
2149+ main_flags |= 4;
2150+ break;
2151+ }
2152+ }
2153+ f = 0;
2154+ q0 = q;
2155+ if (func06str->argv_f[i] != 0x7d) {
2156+retry:
2157+ p = main_argv[i];
2158+ while (*p != '\0') {
2159+ if (*p == ':') {
2160+ if (p[1] == ':')
2161+ p++;
2162+ else {
2163+ f = 0;
2164+ q = q0;
2165+ break;
2166+ }
2167+ }
2168+ if (*p == ' ' && f == 0) {
2169+ f = 1;
2170+ q = q0;
2171+ *q++ = 0x22;
2172+ goto retry;
2173+ }
2174+ *q++ = *p++;
2175+ }
2176+ if (f != 0)
2177+ *q++ = 0x22;
2178+ if (q > q0) {
2179+ *q++ = ' ';
2180+ func06str->argv_f[i] |= 0x80;
2181+ }
2182+ }
2183+ }
2184+ func06str->argv_f[i] = 0xff;
2185+ q[-1] = '\0';
2186+
2187+ j = func06str->def_arg0;
2188+ for (i = 1; i < main_argc; i++) {
2189+ if (func06str->argv_f[i] != 0xfe)
2190+ continue;
2191+ for (;;) {
2192+ if ((func06str->usg[j].flg & 4) == 0)
2193+ break;
2194+ if ((func06str->usg[j].flg & 2) != 0)
2195+ break;
2196+ if (j >= func06str->usgs)
2197+ break;
2198+ j++;
2199+ }
2200+ func06str->usg[j].flg |= 4;
2201+ func06str->argv_f[i] = (func06str->argv_f[i] & 0x80) | j;
2202+ }
2203+
2204+ if ((main_flags & 4) != 0) {
2205+ func06_putusage();
2206+ exit(0);
2207+ }
2208+
2209+ for (i = 0; i < func06str->usgs; i++) {
2210+ if ((func06str->usg[i].flg & 5) == 0 && func06str->usg[i].typ < 0xfe) { /* 省略不能なのに該当なし */
2211+ func06_putusage();
2212+ exit(1);
2213+ }
2214+ }
2215+
2216+#if 0
2217+ for (i = 0;; i++) {
2218+ printf("%02X ", func06str->argv_f[i]);
2219+ if (func06str->argv_f[i] == 0xff)
2220+ break;
2221+ }
2222+ putchar('\n');
2223+#endif
2224+
2225+ return;
2226+}
2227+
2228+#define INVALID -0x7fffffff
2229+
2230+char *calc_skipspace(char *p)
2231+{
2232+ for (; *p == ' '; p++) { } /* スペースを読み飛ばす */
2233+ return p;
2234+}
2235+
2236+int calc_getnum(char **pp, int priority)
2237+{
2238+ char *p = *pp;
2239+ int i = INVALID, j;
2240+ p = calc_skipspace(p);
2241+
2242+ /* 単項演算子 */
2243+ if (*p == '+') {
2244+ p = calc_skipspace(p + 1);
2245+ i = calc_getnum(&p, 0);
2246+ } else if (*p == '-') {
2247+ p = calc_skipspace(p + 1);
2248+ i = calc_getnum(&p, 0);
2249+ if (i != INVALID) {
2250+ i = - i;
2251+ }
2252+ } else if (*p == '~') {
2253+ p = calc_skipspace(p + 1);
2254+ i = calc_getnum(&p, 0);
2255+ if (i != INVALID) {
2256+ i = ~i;
2257+ }
2258+ } else if (*p == '(') { /* かっこ */
2259+ p = calc_skipspace(p + 1);
2260+ i = calc_getnum(&p, 9);
2261+ if (*p == ')') {
2262+ p = calc_skipspace(p + 1);
2263+ } else {
2264+ i = INVALID;
2265+ }
2266+ } else if ('0' <= *p && *p <= '9') { /* 数値 */
2267+ i = strtol(p, (void *) /* (const char **) */ &p, 0);
2268+ if (*p == 'k' || *p == 'K') {
2269+ i <<= 10;
2270+ p++;
2271+ }
2272+ if (*p == 'M') {
2273+ i <<= 20;
2274+ p++;
2275+ }
2276+ if (*p == 'G') {
2277+ i <<= 30;
2278+ p++;
2279+ }
2280+ } else { /* エラー */
2281+ i = INVALID;
2282+ }
2283+
2284+ /* 二項演算子 */
2285+ for (;;) {
2286+ if (i == INVALID) {
2287+ break;
2288+ }
2289+ p = calc_skipspace(p);
2290+ if (*p == '+' && priority > 2) {
2291+ p = calc_skipspace(p + 1);
2292+ j = calc_getnum(&p, 2);
2293+ if (j != INVALID) {
2294+ i += j;
2295+ } else {
2296+ i = INVALID;
2297+ }
2298+ } else if (*p == '-' && priority > 2) {
2299+ p = calc_skipspace(p + 1);
2300+ j = calc_getnum(&p, 2);
2301+ if (j != INVALID) {
2302+ i -= j;
2303+ } else {
2304+ i = INVALID;
2305+ }
2306+ } else if (*p == '*' && priority > 1) {
2307+ p = calc_skipspace(p + 1);
2308+ j = calc_getnum(&p, 1);
2309+ if (j != INVALID) {
2310+ i *= j;
2311+ } else {
2312+ i = INVALID;
2313+ }
2314+ } else if (*p == '/' && priority > 1) {
2315+ p = calc_skipspace(p + 1);
2316+ j = calc_getnum(&p, 1);
2317+ if (j != INVALID && j != 0) {
2318+ i /= j;
2319+ } else {
2320+ i = INVALID;
2321+ }
2322+ } else if (*p == '%' && priority > 1) {
2323+ p = calc_skipspace(p + 1);
2324+ j = calc_getnum(&p, 1);
2325+ if (j != INVALID && j != 0) {
2326+ i %= j;
2327+ } else {
2328+ i = INVALID;
2329+ }
2330+ } else if (*p == '<' && p[1] == '<' && priority > 3) {
2331+ p = calc_skipspace(p + 2);
2332+ j = calc_getnum(&p, 3);
2333+ if (j != INVALID && j != 0) {
2334+ i <<= j;
2335+ } else {
2336+ i = INVALID;
2337+ }
2338+ } else if (*p == '>' && p[1] == '>' && priority > 3) {
2339+ p = calc_skipspace(p + 2);
2340+ j = calc_getnum(&p, 3);
2341+ if (j != INVALID && j != 0) {
2342+ i >>= j;
2343+ } else {
2344+ i = INVALID;
2345+ }
2346+ } else if (*p == '&' && priority > 4) {
2347+ p = calc_skipspace(p + 1);
2348+ j = calc_getnum(&p, 4);
2349+ if (j != INVALID) {
2350+ i &= j;
2351+ } else {
2352+ i = INVALID;
2353+ }
2354+ } else if (*p == '^' && priority > 5) {
2355+ p = calc_skipspace(p + 1);
2356+ j = calc_getnum(&p, 5);
2357+ if (j != INVALID) {
2358+ i ^= j;
2359+ } else {
2360+ i = INVALID;
2361+ }
2362+ } else if (*p == '|' && priority > 6) {
2363+ p = calc_skipspace(p + 1);
2364+ j = calc_getnum(&p, 6);
2365+ if (j != INVALID) {
2366+ i |= j;
2367+ } else {
2368+ i = INVALID;
2369+ }
2370+ } else {
2371+ break;
2372+ }
2373+ }
2374+ p = calc_skipspace(p);
2375+ *pp = p;
2376+ return i;
2377+}
2378+
2379+int my_strtol(UCHAR *p, UCHAR **pp)
2380+{
2381+ int i = calc_getnum((void *) &p, 9);
2382+ if (pp != NULL)
2383+ *pp = p;
2384+ return i;
2385+}
2386+
2387+int getreg32idx(struct READ_API *ra)
2388+{
2389+ int i, l;
2390+ l &= 0;
2391+ if (ra->mod == 0)
2392+ l = getlen(&ra->reh4);
2393+ i = getnum_api(ra, 1);
2394+ if (l == 2 && 0x30 <= i && i <= 0x3f)
2395+ i = 0x7ffffff0 | (i & 0xf);
2396+ if (i == 0x35) {
2397+ /* charで4バイト取ってきて、raに格納 */
2398+ puts("getreg32idx: internal error");
2399+ exit(1);
2400+ }
2401+ return i;
2402+}
2403+
2404+int read_reg32(struct READ_API *ra, int i)
2405+{
2406+ int v = 0;
2407+ if (i < 8)
2408+ v = ra->reg32[7 - i];
2409+ else if (i < 0x7ffffff0)
2410+ v = ra->st0[i - 8];
2411+ else if (i == 0x7ffffff4)
2412+ ;
2413+ else if (0x7ffffff8 <= i && i <= 0x7ffffffb)
2414+ v = *(UCHAR *) &ra->reg32[7 - (i & 3)];
2415+ else if (0x7ffffffc <= i && i <= 0x7fffffff)
2416+ v = *(1 + (UCHAR *) &ra->reg32[7 - (i & 3)]);
2417+ else {
2418+ puts("read_reg32: internal error");
2419+ exit(1);
2420+ }
2421+ return v;
2422+}
2423+
2424+void write_reg32(struct READ_API *ra, int i, int v)
2425+{
2426+ if (i < 8)
2427+ ra->reg32[7 - i] = v;
2428+ else if (i < 0x7ffffff0)
2429+ ra->st0[i - 8] = v;
2430+ else if (i == 0x7ffffff4)
2431+ ;
2432+ else if (0x7ffffff8 <= i && i <= 0x7ffffffb)
2433+ *(char *) &ra->reg32[7 - (i & 3)] = v;
2434+ else if (0x7ffffffc <= i && i <= 0x7fffffff)
2435+ *(1 + (char *) &ra->reg32[7 - (i & 3)]) = v;
2436+ else {
2437+ puts("write_reg32: internal error");
2438+ exit(1);
2439+ }
2440+ return;
2441+}
2442+
2443+int inkey()
2444+{
2445+ #if (USE_WIN32)
2446+ int i;
2447+ if (_kbhit() == 0)
2448+ return 0;
2449+ i = _getch();
2450+ if ((0x20 <= i && i <= 0x7e) || i == '\r' || i == '\t' || i == '\b')
2451+ return i;
2452+ while (_kbhit())
2453+ _getch();
2454+ #elif (USE_POSIX)
2455+ char i;
2456+ do {
2457+ read(fileno(stdin), &i, 1);
2458+ if ((0x20 <= i && i <= 0x7e) || i == '\r' || i == '\t' || i == '\b')
2459+ return i;
2460+ } while(i != 0);
2461+ #endif
2462+ return 0;
2463+}
2464+
2465+void consctrl1(int x, int y)
2466+{
2467+ #if (USE_WIN32)
2468+ COORD c;
2469+ HANDLE h;
2470+ h = GetStdHandle(STD_OUTPUT_HANDLE);
2471+ c.X = x;
2472+ c.Y = y;
2473+ SetConsoleCursorPosition(h, c);
2474+ #elif (USE_POSIX)
2475+ printf("\033[%d;%dH", y + 1, x + 1);
2476+ #endif
2477+ return;
2478+}
2479+
2480+void consctrl2(int f, int b)
2481+{
2482+ #if (USE_WIN32)
2483+ static char t[] = {
2484+ 0, 9, 10, 11, 12, 13, 14, 15, 8, 1, 2, 3, 4, 5, 6, 7
2485+ };
2486+ HANDLE h;
2487+ h = GetStdHandle(STD_OUTPUT_HANDLE);
2488+ if (0 <= f && f <= 15 && 0 <= b && b <= 15)
2489+ SetConsoleTextAttribute(h, t[f] + t[b] * 16);
2490+ #elif (USE_POSIX)
2491+ static char t[] = {
2492+ 0, 4, 2, 6, 1, 5, 3, 7
2493+ };
2494+ if (0 <= f && f <= 15 && 0 <= b && b <= 15)
2495+ printf("\033[3%d;4%dm", t[f & 7], t[b & 7]);
2496+ #endif
2497+ return;
2498+}
2499+
2500+void consctrl3()
2501+{
2502+ #if (USE_WIN32)
2503+ COORD c;
2504+ HANDLE h;
2505+ DWORD dmy, attr;
2506+ CONSOLE_SCREEN_BUFFER_INFO sbinf;
2507+ h = GetStdHandle(STD_OUTPUT_HANDLE);
2508+ GetConsoleScreenBufferInfo(h, &sbinf);
2509+ c.X = 0;
2510+ c.Y = 0;
2511+ if (FillConsoleOutputCharacter(h, ' ', sbinf.dwSize.X * sbinf.dwSize.Y, c, &dmy) == 0)
2512+ return;
2513+ SetConsoleCursorPosition(h, c);
2514+ printf(" \r");
2515+ if (ReadConsoleOutputAttribute(h, (WORD *) &attr, 1, c, &dmy) == 0)
2516+ return;
2517+ if (FillConsoleOutputAttribute(h, attr, sbinf.dwSize.X * sbinf.dwSize.Y, c, &dmy) == 0)
2518+ #elif (USE_POSIX)
2519+ int x, y;
2520+ printf("\033[2J\033[1;1H");
2521+ for (y = 0; y < 25; y++) {
2522+ printf("\033[%d;1H", y + 1);
2523+ for (x = 0; x < 79; x++)
2524+ putchar(' ');
2525+ if (y < 24)
2526+ putchar(' ');
2527+ }
2528+ printf("\033[1;1H");
2529+ #endif
2530+ return;
2531+}
2532+
2533+void consctrl4(int x, int y)
2534+{
2535+ #if (USE_WIN32 || USE_POSIX)
2536+ if (x > 80 && y > 25) {
2537+ printf("consctrl1: console size over (%d,%d)\n", x, y);
2538+ exit(1);
2539+ }
2540+ #else
2541+ puts("consctrl1: not supported");
2542+ exit(1);
2543+ #endif
2544+ return;
2545+}
2546+
2547+#if (USE_POSIX)
2548+
2549+void signal_handler(int sig)
2550+{
2551+ switch(sig) {
2552+ case SIGINT:
2553+ exit(1);
2554+ case SIGTERM:
2555+ exit(143);
2556+ case SIGTSTP:
2557+ tcsetattr(fileno(stdin), TCSANOW, &save_term);
2558+ raise(SIGSTOP);
2559+ break;
2560+ case SIGCONT:
2561+ tcsetattr(fileno(stdin), TCSANOW, &temp_term);
2562+ break;
2563+ }
2564+}
2565+
2566+void end_app()
2567+{
2568+ tcsetattr(fileno(stdin), TCSANOW, &save_term);
2569+}
2570+#endif
2571+
--- hijk/hijk/tags/1.7/_gasfunc.s (nonexistent)
+++ hijk/hijk/tags/1.7/_gasfunc.s (revision 204)
@@ -0,0 +1,28 @@
1+ .file "_gasfunc.s"
2+ .text
3+
4+.globl _start_app
5+_start_app:
6+ movl 4(%esp),%esi
7+ movl 20(%esi),%eax
8+ leal -4(%eax),%esp
9+ movl 4(%esi),%eax
10+ movl 8(%esi),%ecx
11+ movl 12(%esi),%edx
12+ movl 16(%esi),%ebx
13+ movl 24(%esi),%ebp
14+ movl 32(%esi),%edi
15+ ret
16+
17+.globl _asm_api
18+_asm_api:
19+ pushal
20+ call _c_api
21+ popal
22+ ret
23+
24+.globl _asm_end
25+_asm_end:
26+ xorl %edi,%edi
27+ call _asm_api
28+ .byte 0x40
--- hijk/hijk/tags/1.7/gasfunc.s (nonexistent)
+++ hijk/hijk/tags/1.7/gasfunc.s (revision 204)
@@ -0,0 +1,28 @@
1+ .file "gasfunc.s"
2+ .text
3+
4+.globl start_app
5+start_app:
6+ movl 4(%esp),%esi
7+ movl 20(%esi),%eax
8+ leal -4(%eax),%esp
9+ movl 4(%esi),%eax
10+ movl 8(%esi),%ecx
11+ movl 12(%esi),%edx
12+ movl 16(%esi),%ebx
13+ movl 24(%esi),%ebp
14+ movl 32(%esi),%edi
15+ ret
16+
17+.globl asm_api
18+asm_api:
19+ pushal
20+ call c_api
21+ popal
22+ ret
23+
24+.globl asm_end
25+asm_end:
26+ xorl %edi,%edi
27+ call asm_api
28+ .byte 0x40
--- hijk/hijk/tags/1.7/!cons.bat (nonexistent)
+++ hijk/hijk/tags/1.7/!cons.bat (revision 204)
@@ -0,0 +1 @@
1+%ComSpec%
\ No newline at end of file
--- hijk/hijk/tags/1.7/Makefile (nonexistent)
+++ hijk/hijk/tags/1.7/Makefile (revision 204)
@@ -0,0 +1,31 @@
1+TARGET = hijk
2+MODE = exe
3+OBJS = $(TARGET).obj tek.obj naskfunc.obj
4+#STACKSIZE = 0x1c00000 # 28MB (default + 12MB)
5+
6+#INCPATH = ../z_tools/win32/
7+#CC1OPT = -Dmain=main0
8+#include ../z_tools/com_mak.txt
9+
10+
11+all :
12+ make $(TARGET).$(MODE)
13+
14+
15+$(TARGET).$(MODE) : $(TARGET).c tek.c _gasfunc.s
16+ gcc -Os -DUSE_WIN32 -o $(TARGET) $(TARGET).c tek.c _gasfunc.s
17+ strip $(TARGET).$(MODE)
18+# upx -9 $(TARGET).$(MODE)
19+
20+
21+#Linuxなどの場合:
22+# (gccの3.x系で)
23+# >gcc -Os -DUSE_POSIX -o efg01 efg01.c tek.c gasfunc.s
24+# >strip efg01
25+# >upx -9 efg01
26+
27+
28+
29+
30+clean :
31+ $(RM) $(TARGET).$(MODE)
\ No newline at end of file
Show on old repository browser