Compare commits

..

4 Commits

Author SHA1 Message Date
Yourtion
73b7421cf2 非矩形窗口 2016-05-19 14:08:59 +08:00
Yourtion
203dd63152 标准函数 2016-05-19 14:05:01 +08:00
Yourtion
9df2b90ad3 clean files 2016-05-19 13:07:37 +08:00
Yourtion
633089d320 文件压缩 2016-05-19 13:01:06 +08:00
30 changed files with 2148 additions and 160 deletions

View File

@@ -21,8 +21,7 @@ haribote.img : haribote/ipl20.bin haribote/haribote.sys Makefile \
lines/lines.hrb walk/walk.hrb noodle/noodle.hrb \
beepdown/beepdown.hrb color/color.hrb color2/color2.hrb \
sosu/sosu.hrb sosu2/sosu2.hrb sosu3/sosu3.hrb \
typeipl/typeipl.hrb type/type.hrb iroha/iroha.hrb \
chklang/chklang.hrb
type/type.hrb iroha/iroha.hrb chklang/chklang.hrb
$(EDIMG) imgin:../z_tools/fdimg0at.tek \
wbinimg src:haribote/ipl20.bin len:512 from:0 to:0 \
copy from:haribote/haribote.sys to:@: \
@@ -47,10 +46,10 @@ haribote.img : haribote/ipl20.bin haribote/haribote.sys Makefile \
copy from:sosu/sosu.hrb to:@: \
copy from:sosu2/sosu2.hrb to:@: \
copy from:sosu3/sosu3.hrb to:@: \
copy from:typeipl/typeipl.hrb to:@: \
copy from:type/type.hrb to:@: \
copy from:iroha/iroha.hrb to:@: \
copy from:chklang/chklang.hrb to:@: \
copy from:notrec/notrec.hrb to:@: \
copy from:euc.txt to:@: \
copy from:nihongo/nihongo.fnt to:@: \
imgout:haribote.img
@@ -69,6 +68,7 @@ install :
full :
$(MAKE) -C haribote
$(MAKE) -C apilib
$(MAKE) -C stdlib
$(MAKE) -C a
$(MAKE) -C hello3
$(MAKE) -C hello4
@@ -88,10 +88,10 @@ full :
$(MAKE) -C sosu
$(MAKE) -C sosu2
$(MAKE) -C sosu3
$(MAKE) -C typeipl
$(MAKE) -C type
$(MAKE) -C iroha
$(MAKE) -C chklang
$(MAKE) -C notrec
$(MAKE) haribote.img
run_full :
@@ -117,6 +117,7 @@ src_only :
clean_full :
$(MAKE) -C haribote clean
$(MAKE) -C apilib clean
$(MAKE) -C stdlib clean
$(MAKE) -C a clean
$(MAKE) -C hello3 clean
$(MAKE) -C hello4 clean
@@ -136,14 +137,15 @@ clean_full :
$(MAKE) -C sosu clean
$(MAKE) -C sosu2 clean
$(MAKE) -C sosu3 clean
$(MAKE) -C typeipl clean
$(MAKE) -C type clean
$(MAKE) -C iroha clean
$(MAKE) -C chklang clean
$(MAKE) -C notrec clean
src_only_full :
$(MAKE) -C haribote src_only
$(MAKE) -C apilib src_only
$(MAKE) -C stdlib src_only
$(MAKE) -C a src_only
$(MAKE) -C hello3 src_only
$(MAKE) -C hello4 src_only
@@ -163,10 +165,10 @@ src_only_full :
$(MAKE) -C sosu src_only
$(MAKE) -C sosu2 src_only
$(MAKE) -C sosu3 src_only
$(MAKE) -C typeipl src_only
$(MAKE) -C type src_only
$(MAKE) -C iroha src_only
$(MAKE) -C chklang src_only
$(MAKE) -C notrec src_only
-$(DEL) haribote.img
refresh :

View File

@@ -1,6 +1,7 @@
TOOLPATH = ../../z_tools/
INCPATH = ../../z_tools/haribote/
APILIBPATH = ../apilib/
STDLIBPATH = ../stdlib/
HARIBOTEPATH = ../haribote/
MAKE = $(TOOLPATH)make.exe -r
@@ -11,6 +12,7 @@ OBJ2BIM = $(TOOLPATH)obj2bim.exe
MAKEFONT = $(TOOLPATH)makefont.exe
BIN2OBJ = $(TOOLPATH)bin2obj.exe
BIM2HRB = $(TOOLPATH)bim2hrb.exe
BIM2BIN = $(TOOLPATH)bim2bin.exe
RULEFILE = ../haribote.rul
EDIMG = $(TOOLPATH)edimg.exe
IMGTOL = $(TOOLPATH)imgtol.com
@@ -25,24 +27,26 @@ default :
#文件生成规则
$(APP).bim : $(APP).obj $(APILIBPATH)apilib.lib Makefile ../app_make.txt
$(APP).bim : $(APP).obj $(APILIBPATH)apilib.lib $(STDLIBPATH)stdlib.lib Makefile ../app_make.txt
$(OBJ2BIM) @$(RULEFILE) out:$(APP).bim map:$(APP).map stack:$(STACK) \
$(APP).obj $(APILIBPATH)apilib.lib
$(APP).obj $(APILIBPATH)apilib.lib $(STDLIBPATH)stdlib.lib
$(APP).hrb : $(APP).bim Makefile ../app_make.txt
$(BIM2HRB) $(APP).bim $(APP).hrb $(MALLOC)
$(STDAPP).bim : $(APP).obj $(STDLIBPATH)stdlib.lib Makefile ../app_make.txt
$(OBJ2BIM) @$(RULEFILE) out:$(APP).bim map:$(APP).map stack:$(STACK) \
$(APP).obj $(STDLIBPATH)stdlib.lib
haribote.img : ../haribote/ipl10.bin ../haribote/haribote.sys $(APP).hrb \
haribote.img : ../haribote/ipl20.bin ../haribote/haribote.sys $(APP).hrb \
Makefile ../app_make.txt
$(EDIMG) imgin:../../z_tools/fdimg0at.tek \
wbinimg src:../haribote/ipl10.bin len:512 from:0 to:0 \
wbinimg src:../haribote/ipl20.bin len:512 from:0 to:0 \
copy from:../haribote/haribote.sys to:@: \
copy from:$(APP).hrb to:@: \
copy from:../nihongo/nihongo.fnt to:@: \
imgout:haribote.img
#一般规则
%.gas : %.c ../apilib.h Makefile ../app_make.txt
%.gas : %.c ../apilib.h ../stdlib.h Makefile ../app_make.txt
$(CC1) -o $*.gas $*.c
%.nas : %.gas Makefile ../app_make.txt
@@ -51,6 +55,12 @@ haribote.img : ../haribote/ipl10.bin ../haribote/haribote.sys $(APP).hrb \
%.obj : %.nas Makefile ../app_make.txt
$(NASK) $*.nas $*.obj $*.lst
%.org : %.bim Makefile ../app_make.txt
$(BIM2HRB) $*.bim $*.org $(MALLOC)
%.hrb : %.org Makefile ../app_make.txt
$(BIM2BIN) -osacmp in:$*.org out:$*.hrb
#命令
run :
@@ -60,10 +70,12 @@ run :
full :
$(MAKE) -C $(APILIBPATH)
$(MAKE) -C $(STDLIBPATH)
$(MAKE) $(APP).hrb
run_full :
$(MAKE) -C $(APILIBPATH)
$(MAKE) -C $(STDLIBPATH)
$(MAKE) -C ../haribote
$(MAKE) run
@@ -72,6 +84,7 @@ clean :
-$(DEL) *.obj
-$(DEL) *.map
-$(DEL) *.bim
-$(DEL) *.org
-$(DEL) haribote.img
src_only :

View File

@@ -1,6 +1,6 @@
OBJS_BOOTPACK = bootpack.obj naskfunc.obj hankaku.obj graphic.obj dsctbl.obj \
int.obj fifo.obj keyboard.obj mouse.obj memory.obj sheet.obj timer.obj \
mtask.obj window.obj console.obj file.obj
mtask.obj window.obj console.obj file.obj tek.obj
TOOLPATH = ../../z_tools/
INCPATH = ../../z_tools/haribote/
@@ -28,9 +28,6 @@ default :
# 镜像文件生成
ipl10.bin : ipl10.nas Makefile
$(NASK) ipl10.nas ipl10.bin ipl10.lst
ipl20.bin : ipl20.nas Makefile
$(NASK) ipl20.nas ipl20.bin ipl20.lst
@@ -78,6 +75,5 @@ clean :
src_only :
$(MAKE) clean
-$(DEL) ipl10.bin
-$(DEL) ipl20.bin
-$(DEL) haribote.sys

View File

@@ -106,15 +106,17 @@ void HariMain(void)
fifo32_put(&keycmd, key_leds);
/* 载入nihongo.fnt */
nihongo = (unsigned char *) memman_alloc_4k(memman, 16 * 256 + 32 * 94 * 47);
fat = (int *) memman_alloc_4k(memman, 4 * 2880);
file_readfat(fat, (unsigned char *) (ADR_DISKIMG + 0x000200));
finfo = file_search("nihongo.fnt", (struct FILEINFO *) (ADR_DISKIMG + 0x002600), 224);
if (finfo != 0) {
file_loadfile(finfo->clustno, finfo->size, nihongo, fat, (char *) (ADR_DISKIMG + 0x003e00));
i = finfo->size;
nihongo = file_loadfile2(finfo->clustno, &i, fat);
} else {
nihongo = (unsigned char *) memman_alloc_4k(memman, 16 * 256 + 32 * 94 * 47);
for (i = 0; i < 16 * 256; i++) {
nihongo[i] = hankaku[i]; /* 没有字库,半角部分直接复制英文字库 */
nihongo[i] = hankaku[i]; /*没有字库,半角部分直接复制英文字库*/
}
for (i = 16 * 256; i < 16 * 256 + 32 * 94 * 47; i++) {
nihongo[i] = 0xff; /* 没有字库全角部分以0xff填充 */

View File

@@ -286,7 +286,12 @@ struct FILEINFO {
void file_readfat(int *fat, unsigned char *img);
void file_loadfile(int clustno, int size, char *buf, int *fat, char *img);
struct FILEINFO *file_search(char *name, struct FILEINFO *finfo, int max);
char *file_loadfile2(int clustno, int *psize, int *fat);
/* bootpack.c */
struct TASK *open_constask(struct SHEET *sht, unsigned int memtotal);
struct SHEET *open_console(struct SHTCTL *shtctl, unsigned int memtotal);
/* tek.c */
int tek_getsize(unsigned char *p);
int tek_decomp(unsigned char *p, char *q, int size);

View File

@@ -350,7 +350,7 @@ int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline)
struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *) ADR_GDT;
char name[18], *p, *q;
struct TASK *task = task_now();
int i, segsiz, datsiz, esp, dathrb;
int i, segsiz, datsiz, esp, dathrb, appsiz;
struct SHTCTL *shtctl;
struct SHEET *sht;
@@ -376,10 +376,10 @@ int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline)
}
if (finfo != 0) {
/*找到文件的情况*/
p = (char *) memman_alloc_4k(memman, finfo->size);
file_loadfile(finfo->clustno, finfo->size, p, fat, (char *) (ADR_DISKIMG + 0x003e00));
if (finfo->size >= 36 && strncmp(p + 4, "Hari", 4) == 0 && *p == 0x00) {
/*如果找到文件*/
appsiz = finfo->size;
p = file_loadfile2(finfo->clustno, &appsiz, fat);
if (appsiz >= 36 && strncmp(p + 4, "Hari", 4) == 0 && *p == 0x00) {
segsiz = *((int *) (p + 0x0000));
esp = *((int *) (p + 0x000c));
datsiz = *((int *) (p + 0x0010));
@@ -412,7 +412,7 @@ int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline)
} else {
cons_putstr0(cons, ".hrb file format error.\n");
}
memman_free_4k(memman, (int) p, finfo->size);
memman_free_4k(memman, (int) p, appsiz);
cons_newline(cons);
return 1;
}
@@ -566,7 +566,7 @@ int *hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int
fh->buf = (char *) memman_alloc_4k(memman, finfo->size);
fh->size = finfo->size;
fh->pos = 0;
file_loadfile(finfo->clustno, finfo->size, fh->buf, task->fat, (char *) (ADR_DISKIMG + 0x003e00));
file_loadfile2(finfo->clustno, &fh->size, task->fat);
}
}
} else if (edx == 22) {

View File

@@ -72,3 +72,23 @@ next:
}
return 0; /*没有找到*/
}
char *file_loadfile2(int clustno, int *psize, int *fat)
{
int size = *psize, size2;
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
char *buf, *buf2;
buf = (char *) memman_alloc_4k(memman, size);
file_loadfile(clustno, size, buf, fat, (char *) (ADR_DISKIMG + 0x003e00));
if (size >= 17) {
size2 = tek_getsize(buf);
if (size2 > 0) { /*使用tek格式压缩的文件*/
buf2 = (char *) memman_alloc_4k(memman, size2);
tek_decomp(buf, buf2, size2);
memman_free_4k(memman, (int) buf, size);
buf = buf2;
*psize = size2;
}
}
return buf;
}

View File

@@ -1,109 +0,0 @@
; haribote-ipl
; TAB=4
CYLS EQU 10 ; 声明CYLS=10
ORG 0x7c00 ; 指明程序装载地址
; 标准FAT12格式软盘专用的代码 Stand FAT12 format floppy code
JMP entry
DB 0x90
DB "HARIBOTE" ; 启动扇区名称8字节
DW 512 ; 每个扇区sector大小必须512字节
DB 1 ; 簇cluster大小必须为1个扇区
DW 1 ; FAT起始位置一般为第一个扇区
DB 2 ; FAT个数必须为2
DW 224 ; 根目录大小一般为224项
DW 2880 ; 该磁盘大小必须为2880扇区1440*1024/512
DB 0xf0 ; 磁盘类型必须为0xf0
DW 9 ; FAT的长度必??9扇区
DW 18 ; 一个磁道track有几个扇区必须为18
DW 2 ; 磁头数(必??2
DD 0 ; 不使用分区必须是0
DD 2880 ; 重写一次磁盘大小
DB 0,0,0x29 ; 意义不明(固定)
DD 0xffffffff ; (可能是)卷标号码
DB "HARIBOTEOS " ; 磁盘的名称必须为11字?,不足填空格)
DB "FAT12 " ; 磁盘格式名称(必??8字?,不足填空格)
RESB 18 ; 先空出18字节
; 程序主体
entry:
MOV AX,0 ; 初始化寄存器
MOV SS,AX
MOV SP,0x7c00
MOV DS,AX
; 读取磁盘
MOV AX,0x0820
MOV ES,AX
MOV CH,0 ; 柱面0
MOV DH,0 ; 磁头0
MOV CL,2 ; 扇区2
readloop:
MOV SI,0 ; 记录失败次数寄存器
retry:
MOV AH,0x02 ; AH=0x02 : 读入磁盘
MOV AL,1 ; 1个扇区
MOV BX,0
MOV DL,0x00 ; A驱动器
INT 0x13 ; 调用磁盘BIOS
JNC next ; 没出错则跳转到fin
ADD SI,1 ; 往SI加1
CMP SI,5 ; 比较SI与5
JAE error ; SI >= 5 跳转到error
MOV AH,0x00
MOV DL,0x00 ; A驱动器
INT 0x13 ; 重置驱动器
JMP retry
next:
MOV AX,ES ; 把内存地址后移0x200512/16十六进制转换
ADD AX,0x0020
MOV ES,AX ; ADD ES,0x020因为没有ADD ES只能通过AX进行
ADD CL,1 ; 往CL里面加1
CMP CL,18 ; 比较CL与18
JBE readloop ; CL <= 18 跳转到readloop
MOV CL,1
ADD DH,1
CMP DH,2
JB readloop ; DH < 2 跳转到readloop
MOV DH,0
ADD CH,1
CMP CH,CYLS
JB readloop ; CH < CYLS 跳转到readloop
; 读取完毕跳转到haribote.sys执行
MOV [0x0ff0],CH ; IPLがどこまで読んだのかをメモ
JMP 0xc200
error:
MOV SI,msg
putloop:
MOV AL,[SI]
ADD SI,1 ; 给SI加1
CMP AL,0
JE fin
MOV AH,0x0e ; 显示一个文字
MOV BX,15 ; 指定字符颜色
INT 0x10 ; 调用显卡BIOS
JMP putloop
fin:
HLT ; 让CPU停止等待指令
JMP fin ; 无限循环
msg:
DB 0x0a, 0x0a ; 换行两次
DB "load error"
DB 0x0a ; 换行
DB 0
RESB 0x7dfe-$ ; 填写0x00直到0x001fe
DB 0x55, 0xaa

View File

@@ -1,7 +1,7 @@
; haribote-ipl
; TAB=4
CYLS EQU 10 ; どこまで読み込むか
CYLS EQU 20 ; どこまで読み込むか
ORG 0x7c00 ; このプログラムがどこに読み込まれるのか

646
29_day/haribote/tek.c Normal file
View File

@@ -0,0 +1,646 @@
#include "bootpack.h"
#include <setjmp.h>
#include <string.h>
#define NULL 0
typedef unsigned char UCHAR;
typedef unsigned int UINT32;
typedef UINT32 tek_TPRB;
static int tek_decode1(int siz, UCHAR *p, UCHAR *q);
static int tek_decode2(int siz, UCHAR *p, UCHAR *q);
static int tek_decode5(int siz, UCHAR *p, UCHAR *q);
static unsigned int tek_getnum_s7s(UCHAR **pp)
{
unsigned int s = 0;
UCHAR *p = *pp;
do {
s = s << 7 | *p++;
} while ((s & 1) == 0);
s >>= 1;
*pp = p;
return s;
}
int tek_getsize(unsigned char *p)
{
static char header[15] = {
0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x4f, 0x53, 0x41, 0x53, 0x4b, 0x43, 0x4d, 0x50
};
int size = -1;
if (memcmp(p + 1, header, 15) == 0 && (*p == 0x83 || *p == 0x85 || *p == 0x89)) {
p += 16;
size = tek_getnum_s7s(&p);
}
return size;
} /* memcmp和strncmp差不多这个函数忽略字符串中的0并一直比较到指定的15个字符为止*/
int tek_decomp(unsigned char *p, char *q, int size)
{
int err = -1;
if (*p == 0x83) {
err = tek_decode1(size, p, q);
} else if (*p == 0x85) {
err = tek_decode2(size, p, q);
} else if (*p == 0x89) {
err = tek_decode5(size, p, q);
}
if (err != 0) {
return -1; /*失败*/
}
return 0; /*成功*/
}
static int tek_lzrestore_stk1(int srcsiz, UCHAR *src, int outsiz, UCHAR *q)
{
int by, lz, cp, ds;
UCHAR *q1 = q + outsiz, *s7ptr = src, *q0 = q;
do {
if ((by = (lz = *s7ptr++) & 0x0f) == 0)
by = tek_getnum_s7s(&s7ptr);
if ((lz >>= 4) == 0)
lz = tek_getnum_s7s(&s7ptr);
do {
*q++ = *s7ptr++;
} while (--by);
if (q >= q1)
break;
do {
ds = (cp = *s7ptr++) & 0x0f;
if ((ds & 1) == 0) {
do {
ds = ds << 7 | *s7ptr++;
} while ((ds & 1) == 0);
}
ds = ~(ds >> 1);
if ((cp >>= 4) == 0) {
do {
cp = cp << 7 | *s7ptr++;
} while ((cp & 1) == 0);
cp >>= 1;
}
cp++;
if (q + ds < q0)
goto err;
if (q + cp > q1)
cp = q1 - q;
do {
*q = *(q + ds);
q++;
} while (--cp);
} while (--lz);
} while (q < q1);
return 0;
err:
return 1;
}
static int tek_decode1(int siz, UCHAR *p, UCHAR *q)
{
int dsiz, hed, bsiz;
UCHAR *p1 = p + siz;
p += 16;
if ((dsiz = tek_getnum_s7s(&p)) > 0) {
hed = tek_getnum_s7s(&p);
bsiz = 1 << (((hed >> 1) & 0x0f) + 8);
if (dsiz > bsiz || (hed & 0x21) != 0x01)
return 1;
if (hed & 0x40)
tek_getnum_s7s(&p);
if (tek_getnum_s7s(&p) != 0)
return 1;
return tek_lzrestore_stk1(p1 - p, p, dsiz, q);
}
return 0;
}
static unsigned int tek_getnum_s7(UCHAR **pp)
{
unsigned int s = 0, b = 0, a = 1;
UCHAR *p = *pp;
for (;;) {
s = s << 7 | *p++;
if (s & 1)
break;
a <<= 7;
b += a;
}
s >>= 1;
*pp = p;
return s + b;
}
static int tek_lzrestore_stk2(int srcsiz, UCHAR *src, int outsiz, UCHAR *q)
{
int cp, ds, repdis[4], i, j;
UCHAR *q1 = q + outsiz, *s7ptr = src, *q0 = q, bylz, cbylz;
for (j = 0; j < 4; j++)
repdis[j] = -1 - j;
bylz = cbylz = 0;
if (outsiz) {
if (tek_getnum_s7s(&s7ptr))
return 1;
do {
j = 0;
do {
j++;
if (j >= 17) {
j += tek_getnum_s7s(&s7ptr);
break;
}
if (cbylz == 0) {
cbylz = 8;
bylz = *s7ptr++;
}
cbylz--;
i = bylz & 1;
bylz >>= 1;
} while (i == 0);
do {
*q++ = *s7ptr++;
} while (--j);
if (q >= q1)
break;
j = 0;
do {
j++;
if (j >= 17) {
j += tek_getnum_s7s(&s7ptr);
break;
}
if (cbylz == 0) {
cbylz = 8;
bylz = *s7ptr++;
}
cbylz--;
i = bylz & 1;
bylz >>= 1;
} while (i == 0);
do {
i = *s7ptr++;
cp = i >> 4;
i &= 0x0f;
if ((i & 1) == 0)
i |= (tek_getnum_s7(&s7ptr) + 1) << 4;
i >>= 1;
ds = ~(i - 6);
if (i < 4)
ds = repdis[i];
if (i == 4)
ds = repdis[0] - tek_getnum_s7(&s7ptr) - 1;
if (i == 5)
ds = repdis[0] + tek_getnum_s7(&s7ptr) + 1;
if (cp == 0)
cp = tek_getnum_s7(&s7ptr) + 16;
cp++;
if (i > 0) {
if (i > 1) {
if (i > 2)
repdis[3] = repdis[2];
repdis[2] = repdis[1];
}
repdis[1] = repdis[0];
repdis[0] = ds;
}
if (q + ds < q0)
goto err;
if (q + cp > q1)
cp = q1 - q;
do {
*q = *(q + ds);
q++;
} while (--cp);
} while (--j);
} while (q < q1);
}
return 0;
err:
return 1;
}
static int tek_decode2(int siz, UCHAR *p, UCHAR *q)
{
UCHAR *p1 = p + siz;
int dsiz, hed, bsiz, st = 0;
p += 16;
if ((dsiz = tek_getnum_s7s(&p)) > 0) {
hed = tek_getnum_s7s(&p);
bsiz = 1 << (((hed >> 1) & 0x0f) + 8);
if (dsiz > bsiz || (hed & 0x21) != 0x01)
return 1;
if (hed & 0x40)
tek_getnum_s7s(&p);
st = tek_lzrestore_stk2(p1 - p, p, dsiz, q);
}
return st;
}
static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int pb, int lp, int flags);
static int tek_lzrestore_tek5(int srcsiz, UCHAR *src, int outsiz, UCHAR *outbuf)
{
int wrksiz, lc, lp, pb, flags, *work, prop0, fl;
if ((fl = (prop0 = *src) & 0x0f) == 0x01) /* 0001 */
flags |= -1;
else if (fl == 0x05)
flags = -2;
else if (fl == 0x09)
flags &= 0;
else
return 1;
src++;
prop0 >>= 4;
if (prop0 == 0)
prop0 = *src++;
else {
static UCHAR prop0_table[] = { 0x5d, 0x00 }, prop1_table[] = { 0x00 };
if (flags == -1) {
if (prop0 >= 3)
return 1;
prop0 = prop0_table[prop0 - 1];
} else {
if (prop0 >= 2)
return 1;
prop0 = prop1_table[prop0 - 1];
}
}
lp = prop0 / (9 * 5);
prop0 %= 9 * 5;
pb = prop0 / 9;
lc = prop0 % 9;
if (flags == 0) /* tek5:z2 */
flags = *src++;
if (flags == -1) { /* stk5 */
wrksiz = lp;
lp = pb;
pb = wrksiz;
}
wrksiz = 0x180 * sizeof (UINT32) + (0x840 + (0x300 << (lc + lp))) * sizeof (tek_TPRB); /* Åá15KB, lc+lp=3ÈçA36KB */
work = (int *) memman_alloc_4k((struct MEMMAN *) MEMMAN_ADDR, wrksiz);
if (work == NULL)
return -1;
flags = tek_decmain5(work, src, outsiz, outbuf, lc, pb, lp, flags);
memman_free_4k((struct MEMMAN *) MEMMAN_ADDR, (int) work, wrksiz);
return flags;
}
struct tek_STR_BITMODEL {
UCHAR t, m, s, dmy;
UINT32 prb0, prb1, tmsk, ntm, lt, lt0, dmy4;
};
struct tek_STR_PRB {
struct tek_STR_PRB_PB {
struct tek_STR_PRB_PBST {
tek_TPRB mch, rep0l1;
} st[12];
tek_TPRB lenlow[2][8], lenmid[2][8];
} pb[16];
struct tek_STR_PRB_ST {
tek_TPRB rep, repg0, repg1, repg2;
} st[12];
tek_TPRB lensel[2][2], lenhigh[2][256], pslot[4][64], algn[64];
tek_TPRB spdis[2][2+4+8+16+32], lenext[2+4+8+16+32];
tek_TPRB repg3, fchgprm[2 * 32], tbmt[16], tbmm[16], fchglt;
tek_TPRB lit[1];
};
struct tek_STR_RNGDEC {
UCHAR *p;
UINT32 range, code, rmsk;
jmp_buf errjmp;
struct tek_STR_BITMODEL bm[32], *ptbm[16];
struct tek_STR_PRB probs;
};
static void tek_setbm5(struct tek_STR_BITMODEL *bm, int t, int m)
{
bm->t = t;
bm->m = m;
bm->prb1 = -1 << (m + t);
bm->prb0 = ~bm->prb1;
bm->prb1 |= 1 << t;
bm->tmsk = (-1 << t) & 0xffff;
bm->prb0 &= bm->tmsk;
bm->prb1 &= bm->tmsk;
bm->ntm = ~bm->tmsk;
return;
}
static int tek_rdget0(struct tek_STR_RNGDEC *rd, int n, int i)
{
do {
while (rd->range < (UINT32) (1 << 24)) {
rd->range <<= 8;
rd->code = rd->code << 8 | *rd->p++;
}
rd->range >>= 1;
i += i;
if (rd->code >= rd->range) {
rd->code -= rd->range;
i |= 1;
}
} while (--n);
return ~i;
}
static int tek_rdget1(struct tek_STR_RNGDEC *rd, tek_TPRB *prob0, int n, int j, struct tek_STR_BITMODEL *bm)
{
UINT32 p, i, *prob, nm = n >> 4;
n &= 0x0f;
prob0 -= j;
do {
p = *(prob = prob0 + j);
if (bm->lt > 0) {
if (--bm->lt == 0) {
if (tek_rdget1(rd, &rd->probs.fchglt, 0x71, 0, &rd->bm[3]) == 0) {
err:
longjmp(rd->errjmp, 1);
}
i = bm - rd->bm;
if ((bm->s = tek_rdget1(rd, &rd->probs.fchgprm[i * 2 + bm->s], 0x71, 0, &rd->bm[1])) == 0) {
i = tek_rdget1(rd, rd->probs.tbmt, 0x74, 1, &rd->bm[2]) & 15;
if (i == 15)
goto err;
tek_setbm5(bm, i, ((tek_rdget1(rd, rd->probs.tbmm, 0x74, 1, &rd->bm[2]) - 1) & 15) + 1);
}
bm->lt = bm->lt0;
}
if (p < bm->prb0) {
p = bm->prb0;
goto fixprob;
}
if (p > bm->prb1) {
p = bm->prb1;
goto fixprob;
}
if (p & bm->ntm) {
p &= bm->tmsk;
fixprob:
*prob = p;
}
}
while (rd->range < (UINT32) (1 << 24)) {
rd->range <<= 8;
rd->code = rd->code << 8 | *rd->p++;
}
j += j;
i = ((unsigned long long) (rd->range & rd->rmsk) * p) >> 16;
if (rd->code < i) {
j |= 1;
rd->range = i;
*prob += ((0x10000 - p) >> bm->m) & bm->tmsk;
} else {
rd->range -= i;
rd->code -= i;
*prob -= (p >> bm->m) & bm->tmsk;
}
--n;
if ((n & nm) == 0)
bm++;
} while (n);
return j;
}
static UINT32 tek_revbit(UINT32 data, int len)
{
UINT32 rev = 0;
do {
rev += rev + (data & 1);
data >>= 1;
} while (--len);
return rev;
}
static int tek_getlen5(struct tek_STR_RNGDEC *rd, int m, int s_pos, int stk)
{
int i;
if (tek_rdget1(rd, &rd->probs.lensel[m][0], 0x71, 0, rd->ptbm[3]) ^ stk) /* low */
i = tek_rdget1(rd, rd->probs.pb[s_pos].lenlow[m], 0x73, 1, rd->ptbm[4]) & 7;
else if (tek_rdget1(rd, &rd->probs.lensel[m][1], 0x71, 0, rd->ptbm[3]) ^ stk) /* mid */
i = tek_rdget1(rd, rd->probs.pb[s_pos].lenmid[m], 0x73, 1, rd->ptbm[5]);
else {
/* high */
i = tek_rdget1(rd, rd->probs.lenhigh[m], 0x78, 1, rd->ptbm[6]) - (256 + 256 - 8);
if (i > 0) {
if (i < 6 && stk == 0)
i = tek_rdget1(rd, &rd->probs.lenext[(1 << i) - 2], i | 0x70, 1, rd->ptbm[7]) - 1;
else
i = tek_rdget0(rd, i, ~1) - 1;
i = tek_rdget0(rd, i, ~1) - 1;
}
i += 256 - 8 + 16;
}
return i;
}
static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int pb, int lp, int flags)
{
static int state_table[] = { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
int i, j, k, pmch, rep[4], s, pos, m_pos = (1 << pb) - 1, m_lp = (1 << lp) - 1;
int stk = (flags == -1), lcr = 8 - lc, s_pos, lit0cntmsk = 0x78;
UINT32 *lit1;
struct tek_STR_RNGDEC *rd = (struct tek_STR_RNGDEC *) work;
struct tek_STR_PRB *prb = &rd->probs;
rd->p = &src[4];
rd->range |= -1;
rd->code = src[0] << 24 | src[1] << 16 | src[2] << 8 | src[3];
for (i = 0; i < 4; i++)
rep[i] = ~i;
if (setjmp(rd->errjmp))
goto err;
for (i = sizeof (struct tek_STR_PRB) / sizeof (tek_TPRB) + (0x300 << (lc + lp)) - 2; i >= 0; i--)
((tek_TPRB *) prb)[i] = 1 << 15;
for (i = 0; i < 32; i++) {
rd->bm[i].lt = (i >= 4);
rd->bm[i].lt0 = (i < 24) ? 16 * 1024 : 8 * 1024;
rd->bm[i].s &= 0;
rd->bm[i].t = rd->bm[i].m = 5;
}
lit1 = prb->lit + ((256 << (lc + lp)) - 2);
if (stk) {
rd->rmsk = -1 << 11;
for (i = 0; i < 32; i++)
rd->bm[i].lt = 0;
for (i = 0; i < 14; i++)
rd->ptbm[i] = &rd->bm[0];
} else {
UCHAR pt[14];
static UCHAR pt1[14] = {
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 18, 18, 18, 8
};
static UCHAR pt2[14] = {
8, 8, 10, 11, 12, 12, 14, 15,
16, 16, 18, 18, 20, 21
};
/*
0- 7:mch, mch, lit1, lensel, lenlow, lenmid, lenhigh, lenext
8-15:pslot, pslot, sdis, sdis, align, rep-repg2
*/
rd->rmsk |= -1;
rd->bm[1].t = 5; rd->bm[1].m = 3; /* for fchgprm */
rd->bm[2].t = 9; rd->bm[2].m = 2; /* for tbmt, tbmm */
if (flags & 0x40) { /* lt-flag */
rd->bm[3].t = 0; rd->bm[3].m = 1;
prb->fchglt = 0xffff;
}
rd->bm[22].t = 0; rd->bm[22].m = 1;
prb->repg3 = 0xffff;
if (flags == -2) { /* z1 */
rd->bm[22].lt = 0;
for (i = 0; i < 14; i++)
pt[i] = pt1[i];
} else {
for (i = 0; i < 14; i++)
pt[i] = pt2[i];
lit0cntmsk = (7 >> (flags & 3)) << 4 | 8;
pt[ 1] = 8 + ((flags & 0x04) != 0); /* mch */
pt[ 5] = 12 + ((flags & 0x08) != 0); /* llm */
pt[ 9] = 16 + ((flags & 0x10) != 0); /* pst */
pt[11] = 18 + ((flags & 0x20) != 0); /* sds */
}
for (i = 0; i < 14; i++)
rd->ptbm[i] = &rd->bm[pt[i]];
}
for (i = 0; i < 32; i++)
tek_setbm5(&rd->bm[i], rd->bm[i].t, rd->bm[i].m);
if ((tek_rdget1(rd, &prb->pb[0].st[0].mch, 0x71, 0, rd->ptbm[0]) ^ stk) == 0)
goto err;
*q++ = tek_rdget1(rd, prb->lit, lit0cntmsk, 1, &rd->bm[24]) & 0xff;
pmch &= 0; s &= 0; pos = 1;
while (pos < osiz) {
s_pos = pos & m_pos;
if (tek_rdget1(rd, &prb->pb[s_pos].st[s].mch, 0x71, 0, rd->ptbm[s > 0]) ^ stk) {
i = (q[-1] >> lcr | (pos & m_lp) << lc) << 8;
s = state_table[s];
if (pmch == 0)
*q = tek_rdget1(rd, &prb->lit[i], lit0cntmsk, 1, &rd->bm[24]) & 0xff;
else {
struct tek_STR_BITMODEL *bm = &rd->bm[24];
j = 1;
k = 8;
pmch = q[rep[0]];
do {
j += j + tek_rdget1(rd, &lit1[(i + j) << 1 | pmch >> 7], 0x71, 0, rd->ptbm[2]);
k--;
if ((k & (lit0cntmsk >> 4)) == 0)
bm++;
if ((((pmch >> 7) ^ j) & 1) != 0 && k != 0) {
j = tek_rdget1(rd, &prb->lit[i + j - 1], k | (lit0cntmsk & 0x70), j, bm);
break;
}
pmch <<= 1;
} while (k);
*q = j & 0xff;
pmch &= 0;
}
pos++;
q++;
} else { /* lz */
pmch |= 1;
if (tek_rdget1(rd, &prb->st[s].rep, 0x71, 0, rd->ptbm[13]) ^ stk) { /* len/dis */
rep[3] = rep[2];
rep[2] = rep[1];
rep[1] = rep[0];
j = i = tek_getlen5(rd, 0, s_pos, stk);
s = s < 7 ? 7 : 10;
if (j >= 4)
j = 3;
rep[0] = j = tek_rdget1(rd, prb->pslot[j], 0x76, 1, rd->ptbm[8 + (j == 3)]) & 0x3f;
if (j >= 4) {
k = (j >> 1) - 1; /* k = [1, 30] */
rep[0] = (2 | (j & 1)) << k;
if (j < 14) /* k < 6 */
rep[0] |= tek_revbit(tek_rdget1(rd, &prb->spdis[j & 1][(1 << k) - 2], k | 0x70, 1, rd->ptbm[10 + (k >= 4)]), k);
else {
if (stk == 0) {
if (k -= 6)
rep[0] |= tek_rdget0(rd, k, ~0) << 6;
rep[0] |= tek_revbit(tek_rdget1(rd, prb->algn, 0x76, 1, rd->ptbm[12]), 6);
} else {
rep[0] |= tek_rdget0(rd, k - 4, ~0) << 4;
rep[0] |= tek_revbit(tek_rdget1(rd, prb->algn, 0x74, 1, rd->ptbm[12]), 4);
}
}
}
rep[0] = ~rep[0];
} else { /* repeat-dis */
if (tek_rdget1(rd, &prb->st[s].repg0, 0x71, 0, rd->ptbm[13]) ^ stk) { /* rep0 */
i |= -1;
if (tek_rdget1(rd, &prb->pb[s_pos].st[s].rep0l1, 0x71, 0, rd->ptbm[13]) == 0) {
s = s < 7 ? 9 : 11;
goto skip;
}
} else {
if (tek_rdget1(rd, &prb->st[s].repg1, 0x71, 0, rd->ptbm[13]) ^ stk) /* rep1 */
i = rep[1];
else {
if (tek_rdget1(rd, &prb->st[s].repg2, 0x71, 0, rd->ptbm[13]) ^ stk) /* rep2 */
i = rep[2];
else {
if (stk == 0) {
if (tek_rdget1(rd, &prb->repg3, 0x71, 0, &rd->bm[22]) == 0)
goto err;
}
i = rep[3]; /* rep3 */
rep[3] = rep[2];
}
rep[2] = rep[1];
}
rep[1] = rep[0];
rep[0] = i;
}
i = tek_getlen5(rd, 1, s_pos, stk);
s = s < 7 ? 8 : 11;
}
skip:
i += 2;
if (pos + rep[0] < 0)
goto err;
if (pos + i > osiz)
i = osiz - pos;
pos += i;
do {
*q = q[rep[0]];
q++;
} while (--i);
}
}
return 0;
err:
return 1;
}
static int tek_decode5(int siz, UCHAR *p, UCHAR *q)
{
UCHAR *p1 = p + siz;
int dsiz, hed, bsiz, st = 0;
p += 16;
if ((dsiz = tek_getnum_s7s(&p)) > 0) {
hed = tek_getnum_s7s(&p);
if ((hed & 1) == 0)
st = tek_lzrestore_tek5(p1 - p + 1, p - 1, dsiz, q);
else {
bsiz = 1 << (((hed >> 1) & 0x0f) + 8);
if (hed & 0x20)
return 1;
if (bsiz == 256)
st = tek_lzrestore_tek5(p1 - p, p, dsiz, q);
else {
if (dsiz > bsiz)
return 1;
if (hed & 0x40)
tek_getnum_s7s(&p);
st = tek_lzrestore_tek5(p1 - p, p, dsiz, q);
}
}
}
return st;
}

View File

@@ -3,3 +3,6 @@ STACK = 1k
MALLOC = 0k
include ../app_make.txt
$(STDAPP).hrb : $(APP).org Makefile
$(COPY) $(APP).org $(APP).hrb

View File

@@ -1,7 +1,7 @@
#include "apilib.h"
#include "stdlib.h"
void HariMain(void)
{
api_putstr0("hello, world\n");
api_end();
printf("hello, world : %s %d\n", "aaa", 10);
exit(0);
}

View File

@@ -3,3 +3,6 @@ STACK = 1k
MALLOC = 0k
include ../app_make.txt
$(APP).hrb : $(APP).org Makefile
$(COPY) $(APP).org $(APP).hrb

Binary file not shown.

BIN
29_day/nihongo/nihongo.org Normal file

Binary file not shown.

View File

@@ -1,5 +1,5 @@
APP = typeipl
STACK = 1k
APP = notrec
STACK = 11k
MALLOC = 0k
include ../app_make.txt

17
29_day/notrec/notrec.c Normal file
View File

@@ -0,0 +1,17 @@
#include "apilib.h"
void HariMain(void)
{
int win;
char buf[150 * 70];
win = api_openwin(buf, 150, 70, 255, "notrec");
api_boxfilwin(win, 0, 50, 34, 69, 255);
api_boxfilwin(win, 115, 50, 149, 69, 255);
api_boxfilwin(win, 50, 30, 99, 49, 255);
for (;;) {
if (api_getkey(1) == 0x0a) {
break; /*按下回车键则break; */
}
}
api_end();
}

4
29_day/stdlib.h Normal file
View File

@@ -0,0 +1,4 @@
int putchar(int c);
void exit(int status);
int printf(char *format, ...);
void *malloc(int size);

View File

@@ -0,0 +1 @@
command

View File

@@ -0,0 +1 @@
cmd.exe

50
29_day/stdlib/Makefile Normal file
View File

@@ -0,0 +1,50 @@
OBJS_API = stdlib.obj
TOOLPATH = ../../z_tools/
INCPATH = ../../z_tools/haribote/
MAKE = $(TOOLPATH)make.exe -r
NASK = $(TOOLPATH)nask.exe
CC1 = $(TOOLPATH)cc1.exe -I$(INCPATH) -Os -Wall -quiet
GAS2NASK = $(TOOLPATH)gas2nask.exe -a
OBJ2BIM = $(TOOLPATH)obj2bim.exe
MAKEFONT = $(TOOLPATH)makefont.exe
BIN2OBJ = $(TOOLPATH)bin2obj.exe
BIM2HRB = $(TOOLPATH)bim2hrb.exe
RULEFILE = ../haribote.rul
EDIMG = $(TOOLPATH)edimg.exe
IMGTOL = $(TOOLPATH)imgtol.com
GOLIB = $(TOOLPATH)golib00.exe
COPY = copy
DEL = del
#默认动作
default :
$(MAKE) stdlib.lib
#库生成规则
stdlib.lib : Makefile $(OBJS_API)
$(GOLIB) $(OBJS_API) out:stdlib.lib
#文件生成规则
%.gas : %.c ../stdlib.h Makefile
$(CC1) -o $*.gas $*.c
%.nas : %.gas Makefile
$(GAS2NASK) $*.gas $*.nas
%.obj : %.nas Makefile
$(NASK) $*.nas $*.obj $*.lst
#命令
clean :
-$(DEL) *.lst
-$(DEL) *.obj
src_only :
$(MAKE) clean
-$(DEL) stdlib.lib

1
29_day/stdlib/make.bat Normal file
View File

@@ -0,0 +1 @@
..\..\z_tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

51
29_day/stdlib/stdlib.c Normal file
View File

@@ -0,0 +1,51 @@
#include "../apilib.h"
#include "../stdlib.h"
#include <stdio.h>
#include <stdarg.h>
int putchar(int c)
{
api_putchar(c);
return c;
}
void exit(int status)
{
api_end();
}
int printf(char *format, ...)
{
va_list ap;
char s[1000];
int i;
va_start(ap, format);
i = vsprintf(s, format, ap);
api_putstr0(s);
va_end(ap);
return i;
}
void *malloc(int size)
{
char *p = api_malloc(size + 16);
if (p != 0) {
*((int *) p) = size;
p += 16;
}
return p;
}
void free(void *p)
{
char *q = p;
int size;
if (q != 0) {
q -= 16;
size = *((int *) q);
api_free(q, size + 16);
}
return;
}

649
29_day/tek/autodec_.c Normal file
View File

@@ -0,0 +1,649 @@
#include <stdio.h> /* NULL */
#include <stdlib.h> /* malloc, free */
#include <setjmp.h>
typedef unsigned char UCHAR;
typedef unsigned int UINT32;
typedef UINT32 tek_TPRB;
static int tek_decode1(int siz, UCHAR *p, UCHAR *q);
static int tek_decode2(int siz, UCHAR *p, UCHAR *q);
static int tek_decode5(int siz, UCHAR *p, UCHAR *q);
static unsigned int tek_getnum_s7s(UCHAR **pp)
{
unsigned int s = 0;
UCHAR *p = *pp;
do {
s = s << 7 | *p++;
} while ((s & 1) == 0);
s >>= 1;
*pp = p;
return s;
}
int autodecomp(int siz0, UCHAR *p0, int siz)
{
unsigned char *b = p0, *c, *c0;
int s, i, e = 0;
if ((*(int *) &b[0x08] == 0x5341534f) && (*(int *) &b[0x0c] == 0x504d434b)) {
if (*(int *) &b[0x04] == 0x00000001) {
unsigned int t = *(int *) &b[0x00];
e |= 1;
if (0xffffff83 <= t && t <= 0xffffff89) {
c = &b[0x10];
s = tek_getnum_s7s(&c);
if (s + siz - 0x10 <= siz0) {
c0 = c = b + siz0 - siz;
for (i = siz - 1; i >= 0x10; i--)
c[i] = b[i];
c += 0x10;
tek_getnum_s7s(&c);
if (t == 0xffffff83)
e = tek_decode1(siz, c0, b);
if (t == 0xffffff85)
e = tek_decode2(siz, c0, b);
if (t == 0xffffff89)
e = tek_decode5(siz, c0, b);
siz = s;
}
}
}
}
if (e)
siz |= -1;
return siz;
}
static int tek_lzrestore_stk1(int srcsiz, UCHAR *src, int outsiz, UCHAR *q)
{
int by, lz, cp, ds;
UCHAR *q1 = q + outsiz, *s7ptr = src, *q0 = q;
do {
if ((by = (lz = *s7ptr++) & 0x0f) == 0)
by = tek_getnum_s7s(&s7ptr);
if ((lz >>= 4) == 0)
lz = tek_getnum_s7s(&s7ptr);
do {
*q++ = *s7ptr++;
} while (--by);
if (q >= q1)
break;
do {
ds = (cp = *s7ptr++) & 0x0f;
if ((ds & 1) == 0) {
do {
ds = ds << 7 | *s7ptr++;
} while ((ds & 1) == 0);
}
ds = ~(ds >> 1);
if ((cp >>= 4) == 0) {
do {
cp = cp << 7 | *s7ptr++;
} while ((cp & 1) == 0);
cp >>= 1;
}
cp++;
if (q + ds < q0)
goto err;
if (q + cp > q1)
cp = q1 - q;
do {
*q = *(q + ds);
q++;
} while (--cp);
} while (--lz);
} while (q < q1);
return 0;
err:
return 1;
}
static int tek_decode1(int siz, UCHAR *p, UCHAR *q)
{
int dsiz, hed, bsiz;
UCHAR *p1 = p + siz;
p += 16;
if ((dsiz = tek_getnum_s7s(&p)) > 0) {
hed = tek_getnum_s7s(&p);
bsiz = 1 << (((hed >> 1) & 0x0f) + 8);
if (dsiz > bsiz || (hed & 0x21) != 0x01)
return 1;
if (hed & 0x40)
tek_getnum_s7s(&p);
if (tek_getnum_s7s(&p) != 0)
return 1;
return tek_lzrestore_stk1(p1 - p, p, dsiz, q);
}
return 0;
}
static unsigned int tek_getnum_s7(UCHAR **pp)
{
unsigned int s = 0, b = 0, a = 1;
UCHAR *p = *pp;
for (;;) {
s = s << 7 | *p++;
if (s & 1)
break;
a <<= 7;
b += a;
}
s >>= 1;
*pp = p;
return s + b;
}
static int tek_lzrestore_stk2(int srcsiz, UCHAR *src, int outsiz, UCHAR *q)
{
int cp, ds, repdis[4], i, j;
UCHAR *q1 = q + outsiz, *s7ptr = src, *q0 = q, bylz, cbylz;
for (j = 0; j < 4; j++)
repdis[j] = -1 - j;
bylz = cbylz = 0;
if (outsiz) {
if (tek_getnum_s7s(&s7ptr))
return 1;
do {
j = 0;
do {
j++;
if (j >= 17) {
j += tek_getnum_s7s(&s7ptr);
break;
}
if (cbylz == 0) {
cbylz = 8;
bylz = *s7ptr++;
}
cbylz--;
i = bylz & 1;
bylz >>= 1;
} while (i == 0);
do {
*q++ = *s7ptr++;
} while (--j);
if (q >= q1)
break;
j = 0;
do {
j++;
if (j >= 17) {
j += tek_getnum_s7s(&s7ptr);
break;
}
if (cbylz == 0) {
cbylz = 8;
bylz = *s7ptr++;
}
cbylz--;
i = bylz & 1;
bylz >>= 1;
} while (i == 0);
do {
i = *s7ptr++;
cp = i >> 4;
i &= 0x0f;
if ((i & 1) == 0)
i |= (tek_getnum_s7(&s7ptr) + 1) << 4;
i >>= 1;
ds = ~(i - 6);
if (i < 4)
ds = repdis[i];
if (i == 4)
ds = repdis[0] - tek_getnum_s7(&s7ptr) - 1;
if (i == 5)
ds = repdis[0] + tek_getnum_s7(&s7ptr) + 1;
if (cp == 0)
cp = tek_getnum_s7(&s7ptr) + 16;
cp++;
if (i > 0) {
if (i > 1) {
if (i > 2)
repdis[3] = repdis[2];
repdis[2] = repdis[1];
}
repdis[1] = repdis[0];
repdis[0] = ds;
}
if (q + ds < q0)
goto err;
if (q + cp > q1)
cp = q1 - q;
do {
*q = *(q + ds);
q++;
} while (--cp);
} while (--j);
} while (q < q1);
}
return 0;
err:
return 1;
}
static int tek_decode2(int siz, UCHAR *p, UCHAR *q)
{
UCHAR *p1 = p + siz;
int dsiz, hed, bsiz, st = 0;
p += 16;
if ((dsiz = tek_getnum_s7s(&p)) > 0) {
hed = tek_getnum_s7s(&p);
bsiz = 1 << (((hed >> 1) & 0x0f) + 8);
if (dsiz > bsiz || (hed & 0x21) != 0x01)
return 1;
if (hed & 0x40)
tek_getnum_s7s(&p);
st = tek_lzrestore_stk2(p1 - p, p, dsiz, q);
}
return st;
}
static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int pb, int lp, int flags);
static int tek_lzrestore_tek5(int srcsiz, UCHAR *src, int outsiz, UCHAR *outbuf)
{
int wrksiz, lc, lp, pb, flags, *work, prop0, fl;
if ((fl = (prop0 = *src) & 0x0f) == 0x01) /* 0001 */
flags |= -1;
else if (fl == 0x05)
flags = -2;
else if (fl == 0x09)
flags &= 0;
else
return 1;
src++;
prop0 >>= 4;
if (prop0 == 0)
prop0 = *src++;
else {
static UCHAR prop0_table[] = { 0x5d, 0x00 }, prop1_table[] = { 0x00 };
if (flags == -1) {
if (prop0 >= 3)
return 1;
prop0 = prop0_table[prop0 - 1];
} else {
if (prop0 >= 2)
return 1;
prop0 = prop1_table[prop0 - 1];
}
}
lp = prop0 / (9 * 5);
prop0 %= 9 * 5;
pb = prop0 / 9;
lc = prop0 % 9;
if (flags == 0) /* tek5:z2 */
flags = *src++;
if (flags == -1) { /* stk5 */
wrksiz = lp;
lp = pb;
pb = wrksiz;
}
wrksiz = 0x180 * sizeof (UINT32) + (0x840 + (0x300 << (lc + lp))) * sizeof (tek_TPRB);
work = malloc(wrksiz);
if (work == NULL)
return -1;
flags = tek_decmain5(work, src, outsiz, outbuf, lc, pb, lp, flags);
free(work);
return flags;
}
struct tek_STR_BITMODEL {
UCHAR t, m, s, dmy;
UINT32 prb0, prb1, tmsk, ntm, lt, lt0, dmy4;
};
struct tek_STR_PRB {
struct tek_STR_PRB_PB {
struct tek_STR_PRB_PBST {
tek_TPRB mch, rep0l1;
} st[12];
tek_TPRB lenlow[2][8], lenmid[2][8];
} pb[16];
struct tek_STR_PRB_ST {
tek_TPRB rep, repg0, repg1, repg2;
} st[12];
tek_TPRB lensel[2][2], lenhigh[2][256], pslot[4][64], algn[64];
tek_TPRB spdis[2][2+4+8+16+32], lenext[2+4+8+16+32];
tek_TPRB repg3, fchgprm[2 * 32], tbmt[16], tbmm[16], fchglt;
tek_TPRB lit[1];
};
struct tek_STR_RNGDEC {
UCHAR *p;
UINT32 range, code, rmsk;
jmp_buf errjmp;
struct tek_STR_BITMODEL bm[32], *ptbm[16];
struct tek_STR_PRB probs;
};
static void tek_setbm5(struct tek_STR_BITMODEL *bm, int t, int m)
{
bm->t = t;
bm->m = m;
bm->prb1 = -1 << (m + t);
bm->prb0 = ~bm->prb1;
bm->prb1 |= 1 << t;
bm->tmsk = (-1 << t) & 0xffff;
bm->prb0 &= bm->tmsk;
bm->prb1 &= bm->tmsk;
bm->ntm = ~bm->tmsk;
return;
}
static int tek_rdget0(struct tek_STR_RNGDEC *rd, int n, int i)
{
do {
while (rd->range < (UINT32) (1 << 24)) {
rd->range <<= 8;
rd->code = rd->code << 8 | *rd->p++;
}
rd->range >>= 1;
i += i;
if (rd->code >= rd->range) {
rd->code -= rd->range;
i |= 1;
}
} while (--n);
return ~i;
}
static int tek_rdget1(struct tek_STR_RNGDEC *rd, tek_TPRB *prob0, int n, int j, struct tek_STR_BITMODEL *bm)
{
UINT32 p, i, *prob, nm = n >> 4;
n &= 0x0f;
prob0 -= j;
do {
p = *(prob = prob0 + j);
if (bm->lt > 0) {
if (--bm->lt == 0) {
if (tek_rdget1(rd, &rd->probs.fchglt, 0x71, 0, &rd->bm[3]) == 0) {
err:
longjmp(rd->errjmp, 1);
}
i = bm - rd->bm;
if ((bm->s = tek_rdget1(rd, &rd->probs.fchgprm[i * 2 + bm->s], 0x71, 0, &rd->bm[1])) == 0) {
i = tek_rdget1(rd, rd->probs.tbmt, 0x74, 1, &rd->bm[2]) & 15;
if (i == 15)
goto err;
tek_setbm5(bm, i, ((tek_rdget1(rd, rd->probs.tbmm, 0x74, 1, &rd->bm[2]) - 1) & 15) + 1);
}
bm->lt = bm->lt0;
}
if (p < bm->prb0) {
p = bm->prb0;
goto fixprob;
}
if (p > bm->prb1) {
p = bm->prb1;
goto fixprob;
}
if (p & bm->ntm) {
p &= bm->tmsk;
fixprob:
*prob = p;
}
}
while (rd->range < (UINT32) (1 << 24)) {
rd->range <<= 8;
rd->code = rd->code << 8 | *rd->p++;
}
j += j;
i = ((unsigned long long) (rd->range & rd->rmsk) * p) >> 16;
if (rd->code < i) {
j |= 1;
rd->range = i;
*prob += ((0x10000 - p) >> bm->m) & bm->tmsk;
} else {
rd->range -= i;
rd->code -= i;
*prob -= (p >> bm->m) & bm->tmsk;
}
--n;
if ((n & nm) == 0)
bm++;
} while (n);
return j;
}
static UINT32 tek_revbit(UINT32 data, int len)
{
UINT32 rev = 0;
do {
rev += rev + (data & 1);
data >>= 1;
} while (--len);
return rev;
}
static int tek_getlen5(struct tek_STR_RNGDEC *rd, int m, int s_pos, int stk)
{
int i;
if (tek_rdget1(rd, &rd->probs.lensel[m][0], 0x71, 0, rd->ptbm[3]) ^ stk) /* low */
i = tek_rdget1(rd, rd->probs.pb[s_pos].lenlow[m], 0x73, 1, rd->ptbm[4]) & 7;
else if (tek_rdget1(rd, &rd->probs.lensel[m][1], 0x71, 0, rd->ptbm[3]) ^ stk) /* mid */
i = tek_rdget1(rd, rd->probs.pb[s_pos].lenmid[m], 0x73, 1, rd->ptbm[5]);
else {
/* high */
i = tek_rdget1(rd, rd->probs.lenhigh[m], 0x78, 1, rd->ptbm[6]) - (256 + 256 - 8);
if (i > 0) {
if (i < 6 && stk == 0)
i = tek_rdget1(rd, &rd->probs.lenext[(1 << i) - 2], i | 0x70, 1, rd->ptbm[7]) - 1;
else
i = tek_rdget0(rd, i, ~1) - 1;
i = tek_rdget0(rd, i, ~1) - 1;
}
i += 256 - 8 + 16;
}
return i;
}
static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int pb, int lp, int flags)
{
static int state_table[] = { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
int i, j, k, pmch, rep[4], s, pos, m_pos = (1 << pb) - 1, m_lp = (1 << lp) - 1;
int stk = (flags == -1), lcr = 8 - lc, s_pos, lit0cntmsk = 0x78;
UINT32 *lit1;
struct tek_STR_RNGDEC *rd = (struct tek_STR_RNGDEC *) work;
struct tek_STR_PRB *prb = &rd->probs;
rd->p = &src[4];
rd->range |= -1;
rd->code = src[0] << 24 | src[1] << 16 | src[2] << 8 | src[3];
for (i = 0; i < 4; i++)
rep[i] = ~i;
if (setjmp(rd->errjmp))
goto err;
for (i = sizeof (struct tek_STR_PRB) / sizeof (tek_TPRB) + (0x300 << (lc + lp)) - 2; i >= 0; i--)
((tek_TPRB *) prb)[i] = 1 << 15;
for (i = 0; i < 32; i++) {
rd->bm[i].lt = (i >= 4);
rd->bm[i].lt0 = (i < 24) ? 16 * 1024 : 8 * 1024;
rd->bm[i].s &= 0;
rd->bm[i].t = rd->bm[i].m = 5;
}
lit1 = prb->lit + ((256 << (lc + lp)) - 2);
if (stk) {
rd->rmsk = -1 << 11;
for (i = 0; i < 32; i++)
rd->bm[i].lt = 0;
for (i = 0; i < 14; i++)
rd->ptbm[i] = &rd->bm[0];
} else {
UCHAR pt[14];
static UCHAR pt1[14] = {
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 18, 18, 18, 8
};
static UCHAR pt2[14] = {
8, 8, 10, 11, 12, 12, 14, 15,
16, 16, 18, 18, 20, 21
};
/*
0- 7:mch, mch, lit1, lensel, lenlow, lenmid, lenhigh, lenext
8-15:pslot, pslot, sdis, sdis, align, rep-repg2
*/
rd->rmsk |= -1;
rd->bm[1].t = 5; rd->bm[1].m = 3; /* for fchgprm */
rd->bm[2].t = 9; rd->bm[2].m = 2; /* for tbmt, tbmm */
if (flags & 0x40) { /* lt-flag */
rd->bm[3].t = 0; rd->bm[3].m = 1;
prb->fchglt = 0xffff;
}
rd->bm[22].t = 0; rd->bm[22].m = 1;
prb->repg3 = 0xffff;
if (flags == -2) { /* z1 */
rd->bm[22].lt = 0;
for (i = 0; i < 14; i++)
pt[i] = pt1[i];
} else {
for (i = 0; i < 14; i++)
pt[i] = pt2[i];
lit0cntmsk = (7 >> (flags & 3)) << 4 | 8;
pt[ 1] = 8 + ((flags & 0x04) != 0); /* mch */
pt[ 5] = 12 + ((flags & 0x08) != 0); /* llm */
pt[ 9] = 16 + ((flags & 0x10) != 0); /* pst */
pt[11] = 18 + ((flags & 0x20) != 0); /* sds */
}
for (i = 0; i < 14; i++)
rd->ptbm[i] = &rd->bm[pt[i]];
}
for (i = 0; i < 32; i++)
tek_setbm5(&rd->bm[i], rd->bm[i].t, rd->bm[i].m);
if ((tek_rdget1(rd, &prb->pb[0].st[0].mch, 0x71, 0, rd->ptbm[0]) ^ stk) == 0)
goto err;
*q++ = tek_rdget1(rd, prb->lit, lit0cntmsk, 1, &rd->bm[24]) & 0xff;
pmch &= 0; s &= 0; pos = 1;
while (pos < osiz) {
s_pos = pos & m_pos;
if (tek_rdget1(rd, &prb->pb[s_pos].st[s].mch, 0x71, 0, rd->ptbm[s > 0]) ^ stk) {
i = (q[-1] >> lcr | (pos & m_lp) << lc) << 8;
s = state_table[s];
if (pmch == 0)
*q = tek_rdget1(rd, &prb->lit[i], lit0cntmsk, 1, &rd->bm[24]) & 0xff;
else {
struct tek_STR_BITMODEL *bm = &rd->bm[24];
j = 1; /* lit1͍ŏ©ç2ðŒ¸Ä é */
k = 8;
pmch = q[rep[0]];
do {
j += j + tek_rdget1(rd, &lit1[(i + j) << 1 | pmch >> 7], 0x71, 0, rd->ptbm[2]);
k--;
if ((k & (lit0cntmsk >> 4)) == 0)
bm++;
if ((((pmch >> 7) ^ j) & 1) != 0 && k != 0) {
j = tek_rdget1(rd, &prb->lit[i + j - 1], k | (lit0cntmsk & 0x70), j, bm);
break;
}
pmch <<= 1;
} while (k);
*q = j & 0xff;
pmch &= 0;
}
pos++;
q++;
} else { /* lz */
pmch |= 1;
if (tek_rdget1(rd, &prb->st[s].rep, 0x71, 0, rd->ptbm[13]) ^ stk) { /* len/dis */
rep[3] = rep[2];
rep[2] = rep[1];
rep[1] = rep[0];
j = i = tek_getlen5(rd, 0, s_pos, stk);
s = s < 7 ? 7 : 10;
if (j >= 4)
j = 3;
rep[0] = j = tek_rdget1(rd, prb->pslot[j], 0x76, 1, rd->ptbm[8 + (j == 3)]) & 0x3f;
if (j >= 4) {
k = (j >> 1) - 1; /* k = [1, 30] */
rep[0] = (2 | (j & 1)) << k;
if (j < 14) /* k < 6 */
rep[0] |= tek_revbit(tek_rdget1(rd, &prb->spdis[j & 1][(1 << k) - 2], k | 0x70, 1, rd->ptbm[10 + (k >= 4)]), k);
else {
if (stk == 0) {
if (k -= 6)
rep[0] |= tek_rdget0(rd, k, ~0) << 6;
rep[0] |= tek_revbit(tek_rdget1(rd, prb->algn, 0x76, 1, rd->ptbm[12]), 6);
} else {
rep[0] |= tek_rdget0(rd, k - 4, ~0) << 4;
rep[0] |= tek_revbit(tek_rdget1(rd, prb->algn, 0x74, 1, rd->ptbm[12]), 4);
}
}
}
rep[0] = ~rep[0];
} else { /* repeat-dis */
if (tek_rdget1(rd, &prb->st[s].repg0, 0x71, 0, rd->ptbm[13]) ^ stk) { /* rep0 */
i |= -1;
if (tek_rdget1(rd, &prb->pb[s_pos].st[s].rep0l1, 0x71, 0, rd->ptbm[13]) == 0) {
s = s < 7 ? 9 : 11;
goto skip;
}
} else {
if (tek_rdget1(rd, &prb->st[s].repg1, 0x71, 0, rd->ptbm[13]) ^ stk) /* rep1 */
i = rep[1];
else {
if (tek_rdget1(rd, &prb->st[s].repg2, 0x71, 0, rd->ptbm[13]) ^ stk) /* rep2 */
i = rep[2];
else {
if (stk == 0) {
if (tek_rdget1(rd, &prb->repg3, 0x71, 0, &rd->bm[22]) == 0)
goto err;
}
i = rep[3]; /* rep3 */
rep[3] = rep[2];
}
rep[2] = rep[1];
}
rep[1] = rep[0];
rep[0] = i;
}
i = tek_getlen5(rd, 1, s_pos, stk);
s = s < 7 ? 8 : 11;
}
skip:
i += 2;
if (pos + rep[0] < 0)
goto err;
if (pos + i > osiz)
i = osiz - pos;
pos += i;
do {
*q = q[rep[0]];
q++;
} while (--i);
}
}
return 0;
err:
return 1;
}
int tek_decode5(int siz, UCHAR *p, UCHAR *q)
{
UCHAR *p1 = p + siz;
int dsiz, hed, bsiz, st = 0;
p += 16;
if ((dsiz = tek_getnum_s7s(&p)) > 0) {
hed = tek_getnum_s7s(&p);
if ((hed & 1) == 0)
st = tek_lzrestore_tek5(p1 - p + 1, p - 1, dsiz, q);
else {
bsiz = 1 << (((hed >> 1) & 0x0f) + 8);
if (hed & 0x20)
return 1;
if (bsiz == 256)
st = tek_lzrestore_tek5(p1 - p, p, dsiz, q);
else {
if (dsiz > bsiz)
return 1;
if (hed & 0x40)
tek_getnum_s7s(&p);
st = tek_lzrestore_tek5(p1 - p, p, dsiz, q);
}
}
}
return st;
}

646
29_day/tek/tek.c Normal file
View File

@@ -0,0 +1,646 @@
#include "bootpack.h"
#include <setjmp.h>
#include <string.h>
#define NULL 0
typedef unsigned char UCHAR;
typedef unsigned int UINT32;
typedef UINT32 tek_TPRB;
static int tek_decode1(int siz, UCHAR *p, UCHAR *q);
static int tek_decode2(int siz, UCHAR *p, UCHAR *q);
static int tek_decode5(int siz, UCHAR *p, UCHAR *q);
static unsigned int tek_getnum_s7s(UCHAR **pp)
{
unsigned int s = 0;
UCHAR *p = *pp;
do {
s = s << 7 | *p++;
} while ((s & 1) == 0);
s >>= 1;
*pp = p;
return s;
}
int tek_getsize(unsigned char *p)
{
static char header[15] = {
0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x4f, 0x53, 0x41, 0x53, 0x4b, 0x43, 0x4d, 0x50
};
int size = -1;
if (memcmp(p + 1, header, 15) == 0 && (*p == 0x83 || *p == 0x85 || *p == 0x89)) {
p += 16;
size = tek_getnum_s7s(&p);
}
return size;
} /* memcmp和strncmp差不多这个函数忽略字符串中的0并一直比较到指定的15个字符为止*/
int tek_decomp(unsigned char *p, char *q, int size)
{
int err = -1;
if (*p == 0x83) {
err = tek_decode1(size, p, q);
} else if (*p == 0x85) {
err = tek_decode2(size, p, q);
} else if (*p == 0x89) {
err = tek_decode5(size, p, q);
}
if (err != 0) {
return -1; /*失败*/
}
return 0; /*成功*/
}
static int tek_lzrestore_stk1(int srcsiz, UCHAR *src, int outsiz, UCHAR *q)
{
int by, lz, cp, ds;
UCHAR *q1 = q + outsiz, *s7ptr = src, *q0 = q;
do {
if ((by = (lz = *s7ptr++) & 0x0f) == 0)
by = tek_getnum_s7s(&s7ptr);
if ((lz >>= 4) == 0)
lz = tek_getnum_s7s(&s7ptr);
do {
*q++ = *s7ptr++;
} while (--by);
if (q >= q1)
break;
do {
ds = (cp = *s7ptr++) & 0x0f;
if ((ds & 1) == 0) {
do {
ds = ds << 7 | *s7ptr++;
} while ((ds & 1) == 0);
}
ds = ~(ds >> 1);
if ((cp >>= 4) == 0) {
do {
cp = cp << 7 | *s7ptr++;
} while ((cp & 1) == 0);
cp >>= 1;
}
cp++;
if (q + ds < q0)
goto err;
if (q + cp > q1)
cp = q1 - q;
do {
*q = *(q + ds);
q++;
} while (--cp);
} while (--lz);
} while (q < q1);
return 0;
err:
return 1;
}
static int tek_decode1(int siz, UCHAR *p, UCHAR *q)
{
int dsiz, hed, bsiz;
UCHAR *p1 = p + siz;
p += 16;
if ((dsiz = tek_getnum_s7s(&p)) > 0) {
hed = tek_getnum_s7s(&p);
bsiz = 1 << (((hed >> 1) & 0x0f) + 8);
if (dsiz > bsiz || (hed & 0x21) != 0x01)
return 1;
if (hed & 0x40)
tek_getnum_s7s(&p);
if (tek_getnum_s7s(&p) != 0)
return 1;
return tek_lzrestore_stk1(p1 - p, p, dsiz, q);
}
return 0;
}
static unsigned int tek_getnum_s7(UCHAR **pp)
{
unsigned int s = 0, b = 0, a = 1;
UCHAR *p = *pp;
for (;;) {
s = s << 7 | *p++;
if (s & 1)
break;
a <<= 7;
b += a;
}
s >>= 1;
*pp = p;
return s + b;
}
static int tek_lzrestore_stk2(int srcsiz, UCHAR *src, int outsiz, UCHAR *q)
{
int cp, ds, repdis[4], i, j;
UCHAR *q1 = q + outsiz, *s7ptr = src, *q0 = q, bylz, cbylz;
for (j = 0; j < 4; j++)
repdis[j] = -1 - j;
bylz = cbylz = 0;
if (outsiz) {
if (tek_getnum_s7s(&s7ptr))
return 1;
do {
j = 0;
do {
j++;
if (j >= 17) {
j += tek_getnum_s7s(&s7ptr);
break;
}
if (cbylz == 0) {
cbylz = 8;
bylz = *s7ptr++;
}
cbylz--;
i = bylz & 1;
bylz >>= 1;
} while (i == 0);
do {
*q++ = *s7ptr++;
} while (--j);
if (q >= q1)
break;
j = 0;
do {
j++;
if (j >= 17) {
j += tek_getnum_s7s(&s7ptr);
break;
}
if (cbylz == 0) {
cbylz = 8;
bylz = *s7ptr++;
}
cbylz--;
i = bylz & 1;
bylz >>= 1;
} while (i == 0);
do {
i = *s7ptr++;
cp = i >> 4;
i &= 0x0f;
if ((i & 1) == 0)
i |= (tek_getnum_s7(&s7ptr) + 1) << 4;
i >>= 1;
ds = ~(i - 6);
if (i < 4)
ds = repdis[i];
if (i == 4)
ds = repdis[0] - tek_getnum_s7(&s7ptr) - 1;
if (i == 5)
ds = repdis[0] + tek_getnum_s7(&s7ptr) + 1;
if (cp == 0)
cp = tek_getnum_s7(&s7ptr) + 16;
cp++;
if (i > 0) {
if (i > 1) {
if (i > 2)
repdis[3] = repdis[2];
repdis[2] = repdis[1];
}
repdis[1] = repdis[0];
repdis[0] = ds;
}
if (q + ds < q0)
goto err;
if (q + cp > q1)
cp = q1 - q;
do {
*q = *(q + ds);
q++;
} while (--cp);
} while (--j);
} while (q < q1);
}
return 0;
err:
return 1;
}
static int tek_decode2(int siz, UCHAR *p, UCHAR *q)
{
UCHAR *p1 = p + siz;
int dsiz, hed, bsiz, st = 0;
p += 16;
if ((dsiz = tek_getnum_s7s(&p)) > 0) {
hed = tek_getnum_s7s(&p);
bsiz = 1 << (((hed >> 1) & 0x0f) + 8);
if (dsiz > bsiz || (hed & 0x21) != 0x01)
return 1;
if (hed & 0x40)
tek_getnum_s7s(&p);
st = tek_lzrestore_stk2(p1 - p, p, dsiz, q);
}
return st;
}
static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int pb, int lp, int flags);
static int tek_lzrestore_tek5(int srcsiz, UCHAR *src, int outsiz, UCHAR *outbuf)
{
int wrksiz, lc, lp, pb, flags, *work, prop0, fl;
if ((fl = (prop0 = *src) & 0x0f) == 0x01) /* 0001 */
flags |= -1;
else if (fl == 0x05)
flags = -2;
else if (fl == 0x09)
flags &= 0;
else
return 1;
src++;
prop0 >>= 4;
if (prop0 == 0)
prop0 = *src++;
else {
static UCHAR prop0_table[] = { 0x5d, 0x00 }, prop1_table[] = { 0x00 };
if (flags == -1) {
if (prop0 >= 3)
return 1;
prop0 = prop0_table[prop0 - 1];
} else {
if (prop0 >= 2)
return 1;
prop0 = prop1_table[prop0 - 1];
}
}
lp = prop0 / (9 * 5);
prop0 %= 9 * 5;
pb = prop0 / 9;
lc = prop0 % 9;
if (flags == 0) /* tek5:z2 */
flags = *src++;
if (flags == -1) { /* stk5 */
wrksiz = lp;
lp = pb;
pb = wrksiz;
}
wrksiz = 0x180 * sizeof (UINT32) + (0x840 + (0x300 << (lc + lp))) * sizeof (tek_TPRB); /* Åá15KB, lc+lp=3ÈçA36KB */
work = (int *) memman_alloc_4k((struct MEMMAN *) MEMMAN_ADDR, wrksiz);
if (work == NULL)
return -1;
flags = tek_decmain5(work, src, outsiz, outbuf, lc, pb, lp, flags);
memman_free_4k((struct MEMMAN *) MEMMAN_ADDR, (int) work, wrksiz);
return flags;
}
struct tek_STR_BITMODEL {
UCHAR t, m, s, dmy;
UINT32 prb0, prb1, tmsk, ntm, lt, lt0, dmy4;
};
struct tek_STR_PRB {
struct tek_STR_PRB_PB {
struct tek_STR_PRB_PBST {
tek_TPRB mch, rep0l1;
} st[12];
tek_TPRB lenlow[2][8], lenmid[2][8];
} pb[16];
struct tek_STR_PRB_ST {
tek_TPRB rep, repg0, repg1, repg2;
} st[12];
tek_TPRB lensel[2][2], lenhigh[2][256], pslot[4][64], algn[64];
tek_TPRB spdis[2][2+4+8+16+32], lenext[2+4+8+16+32];
tek_TPRB repg3, fchgprm[2 * 32], tbmt[16], tbmm[16], fchglt;
tek_TPRB lit[1];
};
struct tek_STR_RNGDEC {
UCHAR *p;
UINT32 range, code, rmsk;
jmp_buf errjmp;
struct tek_STR_BITMODEL bm[32], *ptbm[16];
struct tek_STR_PRB probs;
};
static void tek_setbm5(struct tek_STR_BITMODEL *bm, int t, int m)
{
bm->t = t;
bm->m = m;
bm->prb1 = -1 << (m + t);
bm->prb0 = ~bm->prb1;
bm->prb1 |= 1 << t;
bm->tmsk = (-1 << t) & 0xffff;
bm->prb0 &= bm->tmsk;
bm->prb1 &= bm->tmsk;
bm->ntm = ~bm->tmsk;
return;
}
static int tek_rdget0(struct tek_STR_RNGDEC *rd, int n, int i)
{
do {
while (rd->range < (UINT32) (1 << 24)) {
rd->range <<= 8;
rd->code = rd->code << 8 | *rd->p++;
}
rd->range >>= 1;
i += i;
if (rd->code >= rd->range) {
rd->code -= rd->range;
i |= 1;
}
} while (--n);
return ~i;
}
static int tek_rdget1(struct tek_STR_RNGDEC *rd, tek_TPRB *prob0, int n, int j, struct tek_STR_BITMODEL *bm)
{
UINT32 p, i, *prob, nm = n >> 4;
n &= 0x0f;
prob0 -= j;
do {
p = *(prob = prob0 + j);
if (bm->lt > 0) {
if (--bm->lt == 0) {
if (tek_rdget1(rd, &rd->probs.fchglt, 0x71, 0, &rd->bm[3]) == 0) {
err:
longjmp(rd->errjmp, 1);
}
i = bm - rd->bm;
if ((bm->s = tek_rdget1(rd, &rd->probs.fchgprm[i * 2 + bm->s], 0x71, 0, &rd->bm[1])) == 0) {
i = tek_rdget1(rd, rd->probs.tbmt, 0x74, 1, &rd->bm[2]) & 15;
if (i == 15)
goto err;
tek_setbm5(bm, i, ((tek_rdget1(rd, rd->probs.tbmm, 0x74, 1, &rd->bm[2]) - 1) & 15) + 1);
}
bm->lt = bm->lt0;
}
if (p < bm->prb0) {
p = bm->prb0;
goto fixprob;
}
if (p > bm->prb1) {
p = bm->prb1;
goto fixprob;
}
if (p & bm->ntm) {
p &= bm->tmsk;
fixprob:
*prob = p;
}
}
while (rd->range < (UINT32) (1 << 24)) {
rd->range <<= 8;
rd->code = rd->code << 8 | *rd->p++;
}
j += j;
i = ((unsigned long long) (rd->range & rd->rmsk) * p) >> 16;
if (rd->code < i) {
j |= 1;
rd->range = i;
*prob += ((0x10000 - p) >> bm->m) & bm->tmsk;
} else {
rd->range -= i;
rd->code -= i;
*prob -= (p >> bm->m) & bm->tmsk;
}
--n;
if ((n & nm) == 0)
bm++;
} while (n);
return j;
}
static UINT32 tek_revbit(UINT32 data, int len)
{
UINT32 rev = 0;
do {
rev += rev + (data & 1);
data >>= 1;
} while (--len);
return rev;
}
static int tek_getlen5(struct tek_STR_RNGDEC *rd, int m, int s_pos, int stk)
{
int i;
if (tek_rdget1(rd, &rd->probs.lensel[m][0], 0x71, 0, rd->ptbm[3]) ^ stk) /* low */
i = tek_rdget1(rd, rd->probs.pb[s_pos].lenlow[m], 0x73, 1, rd->ptbm[4]) & 7;
else if (tek_rdget1(rd, &rd->probs.lensel[m][1], 0x71, 0, rd->ptbm[3]) ^ stk) /* mid */
i = tek_rdget1(rd, rd->probs.pb[s_pos].lenmid[m], 0x73, 1, rd->ptbm[5]);
else {
/* high */
i = tek_rdget1(rd, rd->probs.lenhigh[m], 0x78, 1, rd->ptbm[6]) - (256 + 256 - 8);
if (i > 0) {
if (i < 6 && stk == 0)
i = tek_rdget1(rd, &rd->probs.lenext[(1 << i) - 2], i | 0x70, 1, rd->ptbm[7]) - 1;
else
i = tek_rdget0(rd, i, ~1) - 1;
i = tek_rdget0(rd, i, ~1) - 1;
}
i += 256 - 8 + 16;
}
return i;
}
static int tek_decmain5(int *work, UCHAR *src, int osiz, UCHAR *q, int lc, int pb, int lp, int flags)
{
static int state_table[] = { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
int i, j, k, pmch, rep[4], s, pos, m_pos = (1 << pb) - 1, m_lp = (1 << lp) - 1;
int stk = (flags == -1), lcr = 8 - lc, s_pos, lit0cntmsk = 0x78;
UINT32 *lit1;
struct tek_STR_RNGDEC *rd = (struct tek_STR_RNGDEC *) work;
struct tek_STR_PRB *prb = &rd->probs;
rd->p = &src[4];
rd->range |= -1;
rd->code = src[0] << 24 | src[1] << 16 | src[2] << 8 | src[3];
for (i = 0; i < 4; i++)
rep[i] = ~i;
if (setjmp(rd->errjmp))
goto err;
for (i = sizeof (struct tek_STR_PRB) / sizeof (tek_TPRB) + (0x300 << (lc + lp)) - 2; i >= 0; i--)
((tek_TPRB *) prb)[i] = 1 << 15;
for (i = 0; i < 32; i++) {
rd->bm[i].lt = (i >= 4);
rd->bm[i].lt0 = (i < 24) ? 16 * 1024 : 8 * 1024;
rd->bm[i].s &= 0;
rd->bm[i].t = rd->bm[i].m = 5;
}
lit1 = prb->lit + ((256 << (lc + lp)) - 2);
if (stk) {
rd->rmsk = -1 << 11;
for (i = 0; i < 32; i++)
rd->bm[i].lt = 0;
for (i = 0; i < 14; i++)
rd->ptbm[i] = &rd->bm[0];
} else {
UCHAR pt[14];
static UCHAR pt1[14] = {
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 18, 18, 18, 8
};
static UCHAR pt2[14] = {
8, 8, 10, 11, 12, 12, 14, 15,
16, 16, 18, 18, 20, 21
};
/*
0- 7:mch, mch, lit1, lensel, lenlow, lenmid, lenhigh, lenext
8-15:pslot, pslot, sdis, sdis, align, rep-repg2
*/
rd->rmsk |= -1;
rd->bm[1].t = 5; rd->bm[1].m = 3; /* for fchgprm */
rd->bm[2].t = 9; rd->bm[2].m = 2; /* for tbmt, tbmm */
if (flags & 0x40) { /* lt-flag */
rd->bm[3].t = 0; rd->bm[3].m = 1;
prb->fchglt = 0xffff;
}
rd->bm[22].t = 0; rd->bm[22].m = 1;
prb->repg3 = 0xffff;
if (flags == -2) { /* z1 */
rd->bm[22].lt = 0;
for (i = 0; i < 14; i++)
pt[i] = pt1[i];
} else {
for (i = 0; i < 14; i++)
pt[i] = pt2[i];
lit0cntmsk = (7 >> (flags & 3)) << 4 | 8;
pt[ 1] = 8 + ((flags & 0x04) != 0); /* mch */
pt[ 5] = 12 + ((flags & 0x08) != 0); /* llm */
pt[ 9] = 16 + ((flags & 0x10) != 0); /* pst */
pt[11] = 18 + ((flags & 0x20) != 0); /* sds */
}
for (i = 0; i < 14; i++)
rd->ptbm[i] = &rd->bm[pt[i]];
}
for (i = 0; i < 32; i++)
tek_setbm5(&rd->bm[i], rd->bm[i].t, rd->bm[i].m);
if ((tek_rdget1(rd, &prb->pb[0].st[0].mch, 0x71, 0, rd->ptbm[0]) ^ stk) == 0)
goto err;
*q++ = tek_rdget1(rd, prb->lit, lit0cntmsk, 1, &rd->bm[24]) & 0xff;
pmch &= 0; s &= 0; pos = 1;
while (pos < osiz) {
s_pos = pos & m_pos;
if (tek_rdget1(rd, &prb->pb[s_pos].st[s].mch, 0x71, 0, rd->ptbm[s > 0]) ^ stk) {
i = (q[-1] >> lcr | (pos & m_lp) << lc) << 8;
s = state_table[s];
if (pmch == 0)
*q = tek_rdget1(rd, &prb->lit[i], lit0cntmsk, 1, &rd->bm[24]) & 0xff;
else {
struct tek_STR_BITMODEL *bm = &rd->bm[24];
j = 1;
k = 8;
pmch = q[rep[0]];
do {
j += j + tek_rdget1(rd, &lit1[(i + j) << 1 | pmch >> 7], 0x71, 0, rd->ptbm[2]);
k--;
if ((k & (lit0cntmsk >> 4)) == 0)
bm++;
if ((((pmch >> 7) ^ j) & 1) != 0 && k != 0) {
j = tek_rdget1(rd, &prb->lit[i + j - 1], k | (lit0cntmsk & 0x70), j, bm);
break;
}
pmch <<= 1;
} while (k);
*q = j & 0xff;
pmch &= 0;
}
pos++;
q++;
} else { /* lz */
pmch |= 1;
if (tek_rdget1(rd, &prb->st[s].rep, 0x71, 0, rd->ptbm[13]) ^ stk) { /* len/dis */
rep[3] = rep[2];
rep[2] = rep[1];
rep[1] = rep[0];
j = i = tek_getlen5(rd, 0, s_pos, stk);
s = s < 7 ? 7 : 10;
if (j >= 4)
j = 3;
rep[0] = j = tek_rdget1(rd, prb->pslot[j], 0x76, 1, rd->ptbm[8 + (j == 3)]) & 0x3f;
if (j >= 4) {
k = (j >> 1) - 1; /* k = [1, 30] */
rep[0] = (2 | (j & 1)) << k;
if (j < 14) /* k < 6 */
rep[0] |= tek_revbit(tek_rdget1(rd, &prb->spdis[j & 1][(1 << k) - 2], k | 0x70, 1, rd->ptbm[10 + (k >= 4)]), k);
else {
if (stk == 0) {
if (k -= 6)
rep[0] |= tek_rdget0(rd, k, ~0) << 6;
rep[0] |= tek_revbit(tek_rdget1(rd, prb->algn, 0x76, 1, rd->ptbm[12]), 6);
} else {
rep[0] |= tek_rdget0(rd, k - 4, ~0) << 4;
rep[0] |= tek_revbit(tek_rdget1(rd, prb->algn, 0x74, 1, rd->ptbm[12]), 4);
}
}
}
rep[0] = ~rep[0];
} else { /* repeat-dis */
if (tek_rdget1(rd, &prb->st[s].repg0, 0x71, 0, rd->ptbm[13]) ^ stk) { /* rep0 */
i |= -1;
if (tek_rdget1(rd, &prb->pb[s_pos].st[s].rep0l1, 0x71, 0, rd->ptbm[13]) == 0) {
s = s < 7 ? 9 : 11;
goto skip;
}
} else {
if (tek_rdget1(rd, &prb->st[s].repg1, 0x71, 0, rd->ptbm[13]) ^ stk) /* rep1 */
i = rep[1];
else {
if (tek_rdget1(rd, &prb->st[s].repg2, 0x71, 0, rd->ptbm[13]) ^ stk) /* rep2 */
i = rep[2];
else {
if (stk == 0) {
if (tek_rdget1(rd, &prb->repg3, 0x71, 0, &rd->bm[22]) == 0)
goto err;
}
i = rep[3]; /* rep3 */
rep[3] = rep[2];
}
rep[2] = rep[1];
}
rep[1] = rep[0];
rep[0] = i;
}
i = tek_getlen5(rd, 1, s_pos, stk);
s = s < 7 ? 8 : 11;
}
skip:
i += 2;
if (pos + rep[0] < 0)
goto err;
if (pos + i > osiz)
i = osiz - pos;
pos += i;
do {
*q = q[rep[0]];
q++;
} while (--i);
}
}
return 0;
err:
return 1;
}
static int tek_decode5(int siz, UCHAR *p, UCHAR *q)
{
UCHAR *p1 = p + siz;
int dsiz, hed, bsiz, st = 0;
p += 16;
if ((dsiz = tek_getnum_s7s(&p)) > 0) {
hed = tek_getnum_s7s(&p);
if ((hed & 1) == 0)
st = tek_lzrestore_tek5(p1 - p + 1, p - 1, dsiz, q);
else {
bsiz = 1 << (((hed >> 1) & 0x0f) + 8);
if (hed & 0x20)
return 1;
if (bsiz == 256)
st = tek_lzrestore_tek5(p1 - p, p, dsiz, q);
else {
if (dsiz > bsiz)
return 1;
if (hed & 0x40)
tek_getnum_s7s(&p);
st = tek_lzrestore_tek5(p1 - p, p, dsiz, q);
}
}
}
return st;
}

View File

@@ -1,17 +0,0 @@
#include "apilib.h"
void HariMain(void)
{
int fh;
char c;
fh = api_fopen("ipl10.nas");
if (fh != 0) {
for (;;) {
if (api_fread(&c, 1, fh) == 0) {
break;
}
api_putchar(c);
}
}
api_end();
}

View File

@@ -3,3 +3,7 @@ STACK = 8k
MALLOC = 0k
include ../app_make.txt
$(APP).hrb : $(APP).org Makefile
$(COPY) $(APP).org $(APP).hrb