-
Notifications
You must be signed in to change notification settings - Fork 173
/
soft.c
480 lines (379 loc) · 10.3 KB
/
soft.c
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/* Keen Dreams Source Code
* Copyright (C) 2014 Javier M. Chavez
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <alloc.h>
#include <fcntl.h>
#include <dos.h>
#include <io.h>
#include "kd_def.h"
//#include "gelib.h"
#include "soft.h"
#include "lzw.h"
#include "lzhuff.h"
#include "jam_io.h"
BufferedIO lzwBIO;
//===========================================================================
//
// SWITCHES
//
//===========================================================================
#define LZH_SUPPORT 1
#define LZW_SUPPORT 0
//=========================================================================
//
//
// GENERAL LOAD ROUTINES
//
//
//=========================================================================
//--------------------------------------------------------------------------
// BLoad() -- THIS HAS NOT BEEN FULLY TESTED!
//
// NOTICE : This version of BLOAD is compatable with JAMPak V3.0 and the
// new fileformat...
//--------------------------------------------------------------------------
unsigned long BLoad(char *SourceFile, memptr *DstPtr)
{
int handle;
memptr SrcPtr;
unsigned long i, j, k, r, c;
word flags;
byte Buffer[8];
unsigned long SrcLen,DstLen;
struct CMP1Header CompHeader;
boolean Compressed = false;
memset((void *)&CompHeader,0,sizeof(struct CMP1Header));
//
// Open file to load....
//
if ((handle = open(SourceFile, O_RDONLY|O_BINARY)) == -1)
return(0);
//
// Look for JAMPAK headers
//
read(handle,Buffer,4);
if (!strncmp(Buffer,COMP,4))
{
//
// Compressed under OLD file format
//
Compressed = true;
SrcLen = Verify(SourceFile);
read(handle,(void *)&CompHeader.OrginalLen,4);
CompHeader.CompType = ct_LZW;
MM_GetPtr(DstPtr,CompHeader.OrginalLen);
if (!*DstPtr)
return(0);
}
else
if (!strncmp(Buffer,CMP1,4))
{
//
// Compressed under new file format...
//
Compressed = true;
SrcLen = Verify(SourceFile);
read(handle,(void *)&CompHeader,sizeof(struct CMP1Header));
MM_GetPtr(DstPtr,CompHeader.OrginalLen);
if (!*DstPtr)
return(0);
}
else
DstLen = Verify(SourceFile);
//
// Load the file in memory...
//
if (Compressed)
{
DstLen = CompHeader.OrginalLen;
if ((MM_TotalFree() < SrcLen) && (CompHeader.CompType))
{
if (!InitBufferedIO(handle,&lzwBIO))
Quit("No memory for buffered I/O.");
switch (CompHeader.CompType)
{
#if LZW_SUPPORT
case ct_LZW:
lzwDecompress(&lzwBIO,MK_FP(*DstPtr,0),CompHeader.OrginalLen,(SRC_BFILE|DEST_MEM));
break;
#endif
#if LZH_SUPPORT
case ct_LZH:
lzhDecompress(&lzwBIO,MK_FP(*DstPtr,0),CompHeader.OrginalLen,CompHeader.CompressLen,(SRC_BFILE|DEST_MEM));
break;
#endif
default:
Quit("BLoad() - Unrecognized/Supported compression");
break;
}
FreeBufferedIO(&lzwBIO);
}
else
{
CA_LoadFile(SourceFile,&SrcPtr);
switch (CompHeader.CompType)
{
#if LZW_SUPPORT
case ct_LZW:
lzwDecompress(MK_FP(SrcPtr,8),MK_FP(*DstPtr,0),CompHeader.OrginalLen,(SRC_MEM|DEST_MEM));
break;
#endif
#if LZH_SUPPORT
case ct_LZH:
lzhDecompress(MK_FP(SrcPtr,8),MK_FP(*DstPtr,0),CompHeader.OrginalLen,CompHeader.CompressLen,(SRC_MEM|DEST_MEM));
break;
#endif
default:
Quit("BLoad() - Unrecognized/Supported compression");
break;
}
MM_FreePtr(&SrcPtr);
}
}
else
CA_LoadFile(SourceFile,DstPtr);
close(handle);
return(DstLen);
}
////////////////////////////////////////////////////////////////////////////
//
// LoadLIBShape()
//
int LoadLIBShape(char *SLIB_Filename, char *Filename,struct Shape *SHP)
{
#define CHUNK(Name) (*ptr == *Name) && \
(*(ptr+1) == *(Name+1)) && \
(*(ptr+2) == *(Name+2)) && \
(*(ptr+3) == *(Name+3))
int RT_CODE;
FILE *fp;
char CHUNK[5];
char far *ptr;
memptr IFFfile = NULL;
unsigned long FileLen, size, ChunkLen;
int loop;
RT_CODE = 1;
// Decompress to ram and return ptr to data and return len of data in
// passed variable...
if (!LoadLIBFile(SLIB_Filename,Filename,&IFFfile))
Quit("Error Loading Compressed lib shape!");
// Evaluate the file
//
ptr = MK_FP(IFFfile,0);
if (!CHUNK("FORM"))
goto EXIT_FUNC;
ptr += 4;
FileLen = *(long far *)ptr;
SwapLong((long far *)&FileLen);
ptr += 4;
if (!CHUNK("ILBM"))
goto EXIT_FUNC;
ptr += 4;
FileLen += 4;
while (FileLen)
{
ChunkLen = *(long far *)(ptr+4);
SwapLong((long far *)&ChunkLen);
ChunkLen = (ChunkLen+1) & 0xFFFFFFFE;
if (CHUNK("BMHD"))
{
ptr += 8;
SHP->bmHdr.w = ((struct BitMapHeader far *)ptr)->w;
SHP->bmHdr.h = ((struct BitMapHeader far *)ptr)->h;
SHP->bmHdr.x = ((struct BitMapHeader far *)ptr)->x;
SHP->bmHdr.y = ((struct BitMapHeader far *)ptr)->y;
SHP->bmHdr.d = ((struct BitMapHeader far *)ptr)->d;
SHP->bmHdr.trans = ((struct BitMapHeader far *)ptr)->trans;
SHP->bmHdr.comp = ((struct BitMapHeader far *)ptr)->comp;
SHP->bmHdr.pad = ((struct BitMapHeader far *)ptr)->pad;
SwapWord(&SHP->bmHdr.w);
SwapWord(&SHP->bmHdr.h);
SwapWord(&SHP->bmHdr.x);
SwapWord(&SHP->bmHdr.y);
ptr += ChunkLen;
}
else
if (CHUNK("BODY"))
{
ptr += 4;
size = *((long far *)ptr);
ptr += 4;
SwapLong((long far *)&size);
SHP->BPR = (SHP->bmHdr.w+7) >> 3;
MM_GetPtr(&SHP->Data,size);
if (!SHP->Data)
goto EXIT_FUNC;
movedata(FP_SEG(ptr),FP_OFF(ptr),FP_SEG(SHP->Data),0,size);
ptr += ChunkLen;
break;
}
else
ptr += ChunkLen+8;
FileLen -= ChunkLen+8;
}
RT_CODE = 0;
EXIT_FUNC:;
if (IFFfile)
{
// segptr = (memptr)FP_SEG(IFFfile);
MM_FreePtr(&IFFfile);
}
return (RT_CODE);
}
//----------------------------------------------------------------------------
// LoadLIBFile() -- Copies a file from an existing archive to dos.
//
// PARAMETERS :
//
// LibName - Name of lib file created with SoftLib V1.0
//
// FileName - Name of file to load from lib file.
//
// MemPtr - (IF !NULL) - Pointer to memory to load into ..
// (IF NULL) - Routine allocates necessary memory and
// returns a MEM(SEG) pointer to memory allocated.
//
// RETURN :
//
// (IF !NULL) - A pointer to the loaded data.
// (IF NULL) - Error!
//
//----------------------------------------------------------------------------
memptr LoadLIBFile(char *LibName,char *FileName,memptr *MemPtr)
{
int handle;
unsigned long header;
struct ChunkHeader Header;
unsigned long ChunkLen;
short x;
struct FileEntryHdr FileEntry; // Storage for file once found
struct FileEntryHdr FileEntryHeader; // Header used durring searching
struct SoftLibHdr LibraryHeader; // Library header - Version Checking
boolean FileFound = false;
unsigned long id_slib = ID_SLIB;
unsigned long id_chunk = ID_CHUNK;
//
// OPEN SOFTLIB FILE
//
if ((handle = open(LibName,O_RDONLY|O_BINARY, S_IREAD)) == -1)
return(NULL);
//
// VERIFY it is a SOFTLIB (SLIB) file
//
if (read(handle,&header,4) == -1)
{
close(handle);
return(NULL);
}
if (header != id_slib)
{
close(handle);
return(NULL);
}
//
// CHECK LIBRARY HEADER VERSION NUMBER
//
if (read(handle, &LibraryHeader,sizeof(struct SoftLibHdr)) == -1)
Quit("read error in LoadSLIBFile()\n");
if (LibraryHeader.Version > SOFTLIB_VER)
Quit("Unsupported file ver ");
//
// MANAGE FILE ENTRY HEADERS...
//
for (x = 1;x<=LibraryHeader.FileCount;x++)
{
if (read(handle, &FileEntryHeader,sizeof(struct FileEntryHdr)) == -1)
{
close(handle);
return(NULL);
}
if (!stricmp(FileEntryHeader.FileName,FileName))
{
FileEntry = FileEntryHeader;
FileFound = true;
}
}
//
// IF FILE HAS BEEN FOUND THEN SEEK TO POSITION AND EXTRACT
// ELSE RETURN WITH ERROR CODE...
//
if (FileFound)
{
if (lseek(handle,FileEntry.Offset,SEEK_CUR) == -1)
{
close(handle);
return(NULL);
}
//
// READ CHUNK HEADER - Verify we are at the beginning of a chunk..
//
if (read(handle,(char *)&Header,sizeof(struct ChunkHeader)) == -1)
Quit("LIB File - Unable to read Header!");
if (Header.HeaderID != id_chunk)
Quit("LIB File - BAD HeaderID!");
//
// Allocate memory if Necessary...
//
if (!*MemPtr)
MM_GetPtr(MemPtr,FileEntry.OrginalLength);
//
// Calculate the length of the data (without the chunk header).
//
ChunkLen = FileEntry.ChunkLen - sizeof(struct ChunkHeader);
//
// Extract Data from file
//
switch (Header.Compression)
{
#if LZW_SUPPORT
case ct_LZW:
if (!InitBufferedIO(handle,&lzwBIO))
Quit("No memory for buffered I/O.");
lzwDecompress(&lzwBIO,MK_FP(*MemPtr,0),FileEntry.OrginalLength,(SRC_BFILE|DEST_MEM));
FreeBufferedIO(&lzwBIO);
break;
#endif
#if LZH_SUPPORT
case ct_LZH:
if (!InitBufferedIO(handle,&lzwBIO))
Quit("No memory for buffered I/O.");
lzhDecompress(&lzwBIO, MK_FP(*MemPtr,0), FileEntry.OrginalLength, ChunkLen, (SRC_BFILE|DEST_MEM));
FreeBufferedIO(&lzwBIO);
break;
#endif
case ct_NONE:
if (!CA_FarRead(handle,MK_FP(*MemPtr,0),ChunkLen))
{
// close(handle);
*MemPtr = NULL;
}
break;
default:
close(handle);
Quit("Unknown Chunk.Compression Type!");
break;
}
}
else
*MemPtr = NULL;
close(handle);
return(*MemPtr);
}