-
Notifications
You must be signed in to change notification settings - Fork 0
/
hunklib.pas
156 lines (130 loc) · 3.5 KB
/
hunklib.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
unit hunklib;
{$PACKRECORDS 1}
Interface
Const
HUNK_UNIT = $03E7; //lower word
HUNK_NAME = $03E8;
HUNK_DATA = $03EA;
HUNK_EXT = $03EF;
HUNK_END = $03F2;
CHIP_MEM = $40000000;
FAST_MEM = $80000000;
ANY_MEM = $00000000;
procedure WriteHunk(var F : File; hunk : longword);
Procedure WriteHunkUnit(var F : File);
Procedure WriteHunkName(var F : File; name : string);
Function WriteHunkData(var F : File;MemLoad : longword;Filename : string;IncludeSize : Boolean) : longword;
Procedure WriteHunkExtSize(var F : File;SymbolName : string; filesize : longword);
Procedure WriteHunkExt(var F : File;SymbolName : string);
Procedure WriteHunkEnd(var F : File);
Implementation
Function LongToLE(myLongWord : LongWord) : LongWord;
var
Temp : array[1..4] of Byte;
begin
Move(myLongWord,Temp,sizeof(Temp));
LongToLE := (Temp[1] shl 24) + (Temp[2] shl 16) + (Temp[3] shl 8) + Temp[4];
end;
Function WordToLE(myWord : Word) : Word; // if its little endian to will become big endian vice versa
begin
WordToLE:=LO(myWord) SHL 8 + HI(myWord);
end;
procedure WriteHunk(var F : File; hunk : longword);
begin
Blockwrite(F,LongToLE(hunk),sizeof(hunk));
end;
Procedure WriteHunkUnit(var F : File);
begin
WriteHunk(F,HUNK_UNIT);
WriteHunk(F,$00000000);
end;
Procedure WriteHunkName(var F : File; name : string);
var
qname : string;
qLen,c : longword;
SBuf : array[1..20] of CHAR;
begin
qname:=Copy(name,1,20);
qLen:=(Length(qname)+3) Div 4;
WriteHunk(F,HUNK_NAME);
WriteHunk(F,qLen);
FillChar(Sbuf,sizeof(SBuf),0);
for c:=1 to Length(qname) do
begin
SBuf[c]:=qname[c];
end;
Blockwrite(F,SBuf,qLen*4);
end;
Function WriteHunkData(var F : File;MemLoad : longword;Filename : string;IncludeSize : Boolean) : longword;
var
DataFile : File;
size,qsize : longword;
DataPtr : Pointer;
begin
WriteHunk(F,HUNK_DATA+MemLoad);
Assign(DataFile,Filename);
Reset(DataFile,1);
size:=FileSize(DataFile);
qsize:=((size+3) div 4)*4;
if IncludeSize then WriteHunk(F,qsize div 4+1) else WriteHunk(F,qsize div 4);
GetMem(DataPtr,qsize);
FillChar(DataPtr^,qsize,0);
If DataPtr<>NIL then
begin
Blockread(DataFile,DataPtr^,size);
Blockwrite(F,DataPtr^,qsize);
FreeMem(DataPtr,qsize);
end;
close(DataFile);
if IncludeSize then WriteHunk(F,size);
WriteHunkData:=qsize;
end;
Procedure WriteHunkExt(var F : File;SymbolName : string);
const
Symbol : longword = $01000000;
var
qLen,c : longword;
qSymbolName : string;
SBuf : array[1..20] of CHAR;
begin
qSymbolName:=Copy(SymbolName,1,20);
qLen:=(Length(qSymbolName)+3) Div 4;
WriteHunk(F,HUNK_EXT);
WriteHunk(F,Symbol+qLen);
// qfilesize:=((filesize+3) div 4)*4;
FillChar(Sbuf,sizeof(SBuf),0);
for c:=1 to Length(qSymbolName) do
begin
SBuf[c]:=qSymbolName[c];
end;
Blockwrite(F,SBuf,qLen*4);
WriteHunk(F,$00000000);
end;
Procedure WriteHunkExtSize(var F : File;SymbolName : string; filesize : longword);
const
Symbol : longword = $01000000;
var
qLen,qfilesize,c : longword;
qSymbolName : string;
SBuf : array[1..20] of CHAR;
begin
qSymbolName:=Copy(SymbolName,1,20);
qLen:=(Length(qSymbolName)+3) Div 4;
// WriteHunk(HUNK_EXT); //not needed if proceded by previous HUNK_EXT definition in WriteHunkExt
WriteHunk(F,Symbol+qLen);
qfilesize:=((filesize+3) div 4)*4;
FillChar(Sbuf,sizeof(SBuf),0);
for c:=1 to Length(qSymbolName) do
begin
SBuf[c]:=qSymbolName[c];
end;
Blockwrite(F,SBuf,qLen*4);
WriteHunk(F,qfilesize);
end;
Procedure WriteHunkEnd(var F : File);
begin
WriteHunk(F,$00000000);
WriteHunk(F,HUNK_END);
end;
begin
end.