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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
|
//
// filesystem.c
// Duke3D
//
// Created by fabien sanglard on 12-12-19.
// Copyright (c) 2012 fabien sanglard. All rights reserved.
//
#include "filesystem.h"
#include "platform.h"
#include "cache.h"
#include "fixedPoint_math.h"
#include "../../Game/src/global.h"
extern char game_dir[512];
//The multiplayer module in game.dll needs direct access to the crc32 (sic).
int32_t groupefil_crc32[MAXGROUPFILES];
// A typical GRP index entry:
// - 12 bytes for filename
// - 4 for filesize
typedef uint8_t grpIndexEntry_t[16];
typedef struct grpArchive_s{
int32_t numFiles ;//Number of files in the archive.
grpIndexEntry_t *gfilelist ;//Array containing the filenames.
int32_t *fileOffsets ;//Array containing the file offsets.
int32_t *filesizes ;//Array containing the file offsets.
int fileDescriptor ;//The fd used for open,read operations.
uint32_t crc32 ;//Hash to recognize GRP: Duke Shareware, Duke plutonimum etc...
int cache_len; // valid if cached == 1
int cached; // 0 = not cached, 1 = partial, 2 = full
uint8_t *data; // entire file in RAM
} __attribute__((packed)) grpArchive_t;
//All GRP opened are in this structure
typedef struct grpSet_s{
grpArchive_t archives[MAXGROUPFILES];
int32_t num;
} __attribute__((packed)) grpSet_t;
// Marking it static gurantee not only invisility outside module
// but also that the content will be set to 0.
static grpSet_t grpSet;
int32_t filelength(int32_t fd);
int32_t initgroupfile(const char *filename)
{
uint8_t buf[16] ;
int32_t i, j, k ;
grpArchive_t* archive ;
printf("Loading %s ...\n", filename);
if (grpSet.num == MAXGROUPFILES){
printf("Error: Unable to open an extra GRP archive <= No more slot available.\n");
return(-1);
}
archive = &grpSet.archives[grpSet.num];
//Init the slot
memset(archive, 0, sizeof(grpArchive_t));
//groupfil_memory[numgroupfiles] = NULL; // addresses of raw GRP files in memory
//groupefil_crc32[numgroupfiles] = 0;
archive->fileDescriptor = open(filename,O_BINARY|O_RDONLY);
if (archive->fileDescriptor < 0){
printf("Error: Unable to open file %s.\n",filename);
exit(0);
}
read(archive->fileDescriptor,buf,16);
//FCS : The ".grp" file format is just a collection of a lot of files stored into 1 big one.
//KS doc: I tried to make the format as simple as possible: The first 12 bytes contains my name,
//"KenSilverman". The next 4 bytes is the number of files that were compacted into the
//group file. Then for each file, there is a 16 byte structure, where the first 12
//bytes are the filename, and the last 4 bytes are the file's size. The rest of the
//group file is just the raw data packed one after the other in the same order as the list
//of files. - ken
// Check the magic number (12 bytes header).
if ((buf[0] != 'K') || (buf[1] != 'e') || (buf[2] != 'n') ||
(buf[3] != 'S') || (buf[4] != 'i') || (buf[5] != 'l') ||
(buf[6] != 'v') || (buf[7] != 'e') || (buf[8] != 'r') ||
(buf[9] != 'm') || (buf[10] != 'a') || (buf[11] != 'n')){
printf("Error: File %s is not a GRP archive.\n",filename);
return(-1);
}
// The next 4 bytes of the header feature the number of files in the GRP archive.
archive->numFiles = BUILDSWAP_INTEL32(*((int32_t *)&buf[12]));
archive->gfilelist = kmalloc(archive->numFiles * sizeof(grpIndexEntry_t));
archive->fileOffsets = kmalloc(archive->numFiles * sizeof(int32_t));
archive->filesizes = kmalloc(archive->numFiles * sizeof(int32_t));
// Load the full index 16 bytes per file (12bytes for name + 4 bytes for the size).
read(archive->fileDescriptor,archive->gfilelist, archive->numFiles * 16);
//Initialize all file offset and pointers.
j = 12 + 4 + archive->numFiles * sizeof(grpIndexEntry_t);
for(i=0;i<archive->numFiles;i++){
k = BUILDSWAP_INTEL32(*((int32_t *)&archive->gfilelist[i][12])); // get size
// Now that the filesize has been read, we can replace it with '0' and hence have a
// valid, null terminated character string that will be usable.
archive->gfilelist[i][12] = '\0';
archive->filesizes[i] = k;
archive->fileOffsets[i] = j; // absolute offset list of all files.
j += k;
}
//archive->fileOffsets[archive->numFiles-1] = j;
// Compute CRC32 of the whole grp and implicitely caches the GRP in memory through windows caching service.
// Rewind the fileDescriptor
lseek(archive->fileDescriptor, 0, SEEK_SET);
//i = 1000000;
//groupfil_memory[numgroupfiles] = malloc(i);
//Load the full GRP in RAM.
int total = filelength(archive->fileDescriptor);
#define CRC_BUFLEN 1024*128
/* Atomic Edition won't fit */
if(total > MEMORYSIZE / 2 * 1024 * 1024)
{
#if MEMORYSIZE > 32
archive->cached = 1;
archive->cache_len = 24 * 1024 * 1024; /* arbitrary, but must be multiple of CRC_BUFLEN (currently 1024*128) */
assert(archive->cache_len % CRC_BUFLEN == 0);
archive->data = malloc(archive->cache_len);
#else
archive->cached = 0;
archive->data = 0;
#endif
}
else
{
archive->data = malloc(total);
archive->cached = archive->data != NULL ? 2 : 0;
}
switch(archive->cached)
{
case 2:
printf("Caching complete GRP in RAM (%d KB)\n", total / 1024);
break;
case 1:
printf("Partially caching GRP in RAM (%d KB/%d KB)\n", archive->cache_len / 1024, total / 1024);
break;
default:
printf("Not caching large GRP (%d KB)", total / 1024);
break;
}
uint8_t *crcBuffer = NULL;
/* we need a temporary buffer to calculate the CRC if it's not being cached */
if(!archive->cached)
crcBuffer = malloc(CRC_BUFLEN);
else
crcBuffer = archive->data;
int loop = 0;
int bytes = 0;
bool stop_inc = false;
while((j=read(archive->fileDescriptor, crcBuffer, CRC_BUFLEN))) {
archive->crc32 = crc32_update(crcBuffer,j,archive->crc32);
/* crcBuffer points to a permanent buffer */
if(archive->cached)
{
if(!stop_inc)
crcBuffer += j;
if(archive->cached == 1 && !stop_inc)
{
if(crcBuffer - archive->data >= archive->cache_len) /* we've read enough, further reads go into a temp buffer */
{
crcBuffer = malloc(CRC_BUFLEN);
stop_inc = true;
}
}
}
bytes += j;
loop++;
if(loop % 16 == 0)
printf("%d%% complete...\n", (int)(100.0 * bytes / (float)total)); // sorry
//rb->splashf(0, "point -%d", loop);
}
if(!archive->cached || stop_inc)
free(crcBuffer);
// The game layer seems to absolutely need to access an array int[4] groupefil_crc32
// so we need to store the crc32 in there too.
groupefil_crc32[grpSet.num] = archive->crc32;
//free(groupfil_memory[numgroupfiles]);
//groupfil_memory[numgroupfiles] = 0;
grpSet.num++;
return(grpSet.num-1);
}
void uninitgroupfile(void)
{
int i;
for( i=0 ; i < grpSet.num ;i++){
close(grpSet.archives[i].fileDescriptor);
if(grpSet.archives[i].cached)
free(grpSet.archives[i].data);
free(grpSet.archives[i].gfilelist);
free(grpSet.archives[i].fileOffsets);
free(grpSet.archives[i].filesizes);
memset(&grpSet.archives[i], 0, sizeof(grpArchive_t));
}
}
void crc32_table_gen(unsigned int* crc32_table) /* build CRC32 table */
{
unsigned int crc, poly;
int i, j;
poly = 0xEDB88320L;
for (i = 0; i < 256; i++)
{
crc = i;
for (j = 8; j > 0; j--)
{
if (crc & 1)
crc = (crc >> 1) ^ poly;
else
crc >>= 1;
}
crc32_table[i] = crc;
}
}
unsigned int crc32(uint8_t *buf, unsigned int length)
{
unsigned int initial_crc;
initial_crc = 0;
return(crc32_update(buf, length, initial_crc));
}
uint32_t crc32_update(uint8_t *buf, uint32_t length, uint32_t crc_to_update)
{
unsigned int crc32_table[256];
crc32_table_gen(crc32_table);
crc_to_update ^= 0xFFFFFFFF;
while (length--)
crc_to_update = crc32_table[(crc_to_update ^ *buf++) & 0xFF] ^ (crc_to_update >> 8);
return crc_to_update ^ 0xFFFFFFFF;
}
/*
* 16 12 5
* this is the CCITT CRC 16 polynomial X + X + X + 1.
* This is 0x1021 when x is 2, but the way the algorithm works
* we use 0x8408 (the reverse of the bit pattern). The high
* bit is always assumed to be set, thus we only use 16 bits to
* represent the 17 bit value.
*/
#define POLY 0x8408 /* 1021H bit reversed */
uint16_t crc16(uint8_t *data_p, uint16_t length)
{
uint8_t i;
unsigned int data;
unsigned int crc = 0xffff;
if (length == 0)
return (~crc);
do
{
for (i=0, data=(unsigned int)0xff & *data_p++;
i < 8;
i++, data >>= 1)
{
if ((crc & 0x0001) ^ (data & 0x0001))
crc = (crc >> 1) ^ POLY;
else crc >>= 1;
}
} while (--length);
crc = ~crc;
data = crc;
crc = (crc << 8) | (data >> 8 & 0xff);
return (crc);
}
// The engine can open files transparently on the filesystem or on the GRPsystem
enum fileType_e{ SYSTEM_FILE, GRP_FILE} ;
//An entry in the array tracking open files
typedef struct openFile_s{
enum fileType_e type ;
int fd ; //Either the fileDescriptor or the fileIndex in a GRP depending on the type.
int cursor ; //lseek cursor
int grpID ; //GRP id
int used ; //Marker 1=used
} openFile_t;
#define MAXOPENFILES 64
static openFile_t openFiles[MAXOPENFILES];
int32_t kopen4load(const char *filename, int openOnlyFromGRP){
int32_t i, k;
int32_t newhandle;
grpArchive_t* archive;
//Search a free slot
newhandle = MAXOPENFILES-1;
while (openFiles[newhandle].used && newhandle >= 0)
newhandle--;
if (newhandle < 0)
Error(EXIT_FAILURE, "Too Many files open!\n");
//Try to look in the filesystem first. In this case fd = filedescriptor.
if(!openOnlyFromGRP){
openFiles[newhandle].fd = open(filename,O_BINARY|O_RDONLY);
#if 0
if(openFiles[newhandle].fd < 0)
perror(filename);
#endif
if (openFiles[newhandle].fd >= 0){
openFiles[newhandle].type = SYSTEM_FILE;
openFiles[newhandle].cursor = 0;
openFiles[newhandle].used = 1;
return(newhandle);
}
}
//Try to look in the GRP archives. In this case fd = index of the file in the GRP.
for(k=grpSet.num-1;k>=0;k--)
{
archive = &grpSet.archives[k];
for(i=archive->numFiles-1;i>=0;i--){
if (!strncasecmp((char*)archive->gfilelist[i],filename,12)){
openFiles[newhandle].type = GRP_FILE;
openFiles[newhandle].used = 1;
openFiles[newhandle].cursor = 0;
openFiles[newhandle].fd = i;
openFiles[newhandle].grpID = k;
LOGF("found GRP %s", filename);
return(newhandle);
}
}
}
LOGF("kopen4load: couldn't find %s in GRP", filename);
return(-1);
}
int32_t kread(int32_t handle, void *buffer, int32_t leng){
openFile_t * openFile ;
grpArchive_t * archive ;
openFile = &openFiles[handle];
if (!openFile->used){
printf("Invalide handle. Unrecoverable error.\n");
getchar();
exit(0);
}
//FILESYSTEM ? OS takes care of it !
if (openFile->type == SYSTEM_FILE){
return(read(openFile->fd,buffer,leng));
}
//File is actually in the GRP
archive = & grpSet.archives[openFile->grpID];
//Adjust leng so we cannot read more than filesystem-cursor location.
leng = min(leng,archive->filesizes[openFile->fd]-openFile->cursor);
if(!archive->cached)
{
lseek(archive->fileDescriptor,
archive->fileOffsets[openFile->fd] + openFile->cursor,
SEEK_SET);
leng = read(archive->fileDescriptor,buffer,leng);
}
else
{
if(archive->cached == 2) /* completely in RAM */
memcpy(buffer, archive->data + archive->fileOffsets[openFile->fd] + openFile->cursor, leng);
else
{
/* 3 cases here: either it's completely outside the cached region, in which case we
* fall back to a disk read; it can be on a boundary; or completely in RAM */
int ofs = archive->fileOffsets[openFile->fd];
int start = ofs + openFile->cursor;
/* completely outside */
if(start >= archive->cache_len)
{
lseek(archive->fileDescriptor,
start,
SEEK_SET);
leng = read(archive->fileDescriptor, buffer, leng);
}
else
{
int end = start + leng - 1;
/* partial */
if(end >= archive->cache_len)
{
/* in-RAM portion */
memcpy(buffer, archive->data + start,
archive->cache_len - start);
/* disk */
lseek(archive->fileDescriptor, archive->cache_len, SEEK_SET);
leng = archive->cache_len - start +
read(archive->fileDescriptor, buffer + archive->cache_len - start,
end - archive->cache_len);
}
else
{
/* fully in RAM */
memcpy(buffer, archive->data + ofs + openFile->cursor,
leng);
}
}
}
}
openFile->cursor += leng;
return leng;
}
int32_t kread16(int32_t handle, int16_t *buffer){
if (kread(handle, buffer, 2) != 2)
return(0);
*buffer = BUILDSWAP_INTEL16(*buffer);
return(1);
}
int32_t kread32(int32_t handle, int32_t *buffer){
if (kread(handle, buffer, 4) != 4)
return(0);
*buffer = BUILDSWAP_INTEL32(*buffer);
return(1);
}
int32_t kread8(int32_t handle, uint8_t *buffer){
if (kread(handle, buffer, 1) != 1)
return(0);
return(1);
}
int32_t klseek(int32_t handle, int32_t offset, int whence){
grpArchive_t* archive;
if (!openFiles[handle].used){
printf("Invalide handle. Unrecoverable error.\n");
getchar();
exit(0);
}
// FILESYSTEM ? OS will take care of it.
if (openFiles[handle].type == SYSTEM_FILE){
return lseek(openFiles[handle].fd,offset,whence);
}
archive = & grpSet.archives [ openFiles[handle].grpID ];
switch(whence){
case SEEK_SET: openFiles[handle].cursor = offset; break;
case SEEK_END: openFiles[handle].cursor = archive->filesizes[openFiles[handle].fd]; break;
case SEEK_CUR: openFiles[handle].cursor += offset; break;
}
return(openFiles[handle].cursor);
}
int32_t filelength(int32_t fd)
{
off_t cur = lseek(fd, 0, SEEK_CUR);
off_t len = lseek(fd, 0, SEEK_END);
lseek(fd, cur, SEEK_SET);
return len;
}
int32_t kfilelength(int32_t handle)
{
openFile_t* openFile = &openFiles[handle];
if (!openFile->used){
printf("Invalide handle. Unrecoverable error.\n");
getchar();
exit(0);
}
if (openFile->type == SYSTEM_FILE){
LOGF("calling filelength");
return(filelength(openFile->fd));
}
else{
grpArchive_t* archive = &grpSet.archives[ openFile->grpID ];
return archive->filesizes[openFile->fd];
}
}
void kclose(int32_t handle)
{
openFile_t* openFile = &openFiles[handle];
//This is a typical handle for a non existing file.
if (handle == -1)
return;
if (!openFile->used){
printf("Invalide handle. Unrecoverable error.\n");
getchar();
exit(0);
}
if (openFile->type == SYSTEM_FILE){
close(openFile->fd);
}
memset(openFile, 0, sizeof(openFile_t));
}
/* Internal LZW variables */
#define LZWSIZE 16384 /* Watch out for shorts! */
static uint8_t *lzwbuf1, *lzwbuf4, *lzwbuf5;
static uint8_t lzwbuflock[5];
static short *lzwbuf2, *lzwbuf3;
int32_t compress(uint8_t *lzwinbuf, int32_t uncompleng, uint8_t *lzwoutbuf)
{
int32_t i, addr, newaddr, addrcnt, zx, *longptr;
int32_t bytecnt1, bitcnt, numbits, oneupnumbits;
short *shortptr;
for(i=255;i>=0;i--) { lzwbuf1[i] = (uint8_t ) i; lzwbuf3[i] = (short) ((i+1)&255); }
clearbuf((void *) (lzwbuf2),256>>1,0xffffffff);
clearbuf((void *) (lzwoutbuf),((uncompleng+15)+3)>>2,0L);
addrcnt = 256; bytecnt1 = 0; bitcnt = (4<<3);
numbits = 8; oneupnumbits = (1<<8);
do
{
addr = lzwinbuf[bytecnt1];
do
{
bytecnt1++;
if (bytecnt1 == uncompleng) break;
if (lzwbuf2[addr] < 0) {lzwbuf2[addr] = (short) addrcnt; break;}
newaddr = lzwbuf2[addr];
while (lzwbuf1[newaddr] != lzwinbuf[bytecnt1])
{
zx = lzwbuf3[newaddr];
if (zx < 0) {lzwbuf3[newaddr] = (short) addrcnt; break;}
newaddr = zx;
}
if (lzwbuf3[newaddr] == addrcnt) break;
addr = newaddr;
} while (addr >= 0);
lzwbuf1[addrcnt] = lzwinbuf[bytecnt1];
lzwbuf2[addrcnt] = -1;
lzwbuf3[addrcnt] = -1;
longptr = (int32_t *)&lzwoutbuf[bitcnt>>3];
longptr[0] |= (addr<<(bitcnt&7));
bitcnt += numbits;
if ((addr&((oneupnumbits>>1)-1)) > ((addrcnt-1)&((oneupnumbits>>1)-1)))
bitcnt--;
addrcnt++;
if (addrcnt > oneupnumbits) { numbits++; oneupnumbits <<= 1; }
} while ((bytecnt1 < uncompleng) && (bitcnt < (uncompleng<<3)));
longptr = (int32_t *)&lzwoutbuf[bitcnt>>3];
longptr[0] |= (addr<<(bitcnt&7));
bitcnt += numbits;
if ((addr&((oneupnumbits>>1)-1)) > ((addrcnt-1)&((oneupnumbits>>1)-1)))
bitcnt--;
shortptr = (short *)lzwoutbuf;
shortptr[0] = (short)uncompleng;
if (((bitcnt+7)>>3) < uncompleng)
{
shortptr[1] = (short)addrcnt;
return((bitcnt+7)>>3);
}
shortptr[1] = (short)0;
for(i=0;i<uncompleng;i++) lzwoutbuf[i+4] = lzwinbuf[i];
return(uncompleng+4);
}
int32_t uncompress(uint8_t *lzwinbuf, int32_t compleng, uint8_t *lzwoutbuf)
{
int32_t strtot, currstr, numbits, oneupnumbits;
int32_t i, dat, leng, bitcnt, outbytecnt, *longptr;
short *shortptr;
shortptr = (short *)lzwinbuf;
strtot = (int32_t )shortptr[1];
if (strtot == 0)
{
copybuf((void *)((lzwinbuf)+4),(void *)((lzwoutbuf)),((compleng-4)+3)>>2);
return((int32_t )shortptr[0]); /* uncompleng */
}
for(i=255;i>=0;i--) { lzwbuf2[i] = (short) i; lzwbuf3[i] = (short) i; }
currstr = 256; bitcnt = (4<<3); outbytecnt = 0;
numbits = 8; oneupnumbits = (1<<8);
do
{
longptr = (int32_t *)&lzwinbuf[bitcnt>>3];
dat = ((longptr[0]>>(bitcnt&7)) & (oneupnumbits-1));
bitcnt += numbits;
if ((dat&((oneupnumbits>>1)-1)) > ((currstr-1)&((oneupnumbits>>1)-1)))
{ dat &= ((oneupnumbits>>1)-1); bitcnt--; }
lzwbuf3[currstr] = (short) dat;
for(leng=0;dat>=256;leng++,dat=lzwbuf3[dat])
lzwbuf1[leng] = (uint8_t ) lzwbuf2[dat];
lzwoutbuf[outbytecnt++] = (uint8_t ) dat;
for(i=leng-1;i>=0;i--) lzwoutbuf[outbytecnt++] = lzwbuf1[i];
lzwbuf2[currstr-1] = (short) dat; lzwbuf2[currstr] = (short) dat;
currstr++;
if (currstr > oneupnumbits) { numbits++; oneupnumbits <<= 1; }
} while (currstr < strtot);
return((int32_t )shortptr[0]); /* uncompleng */
}
void kdfread(void *buffer, size_t dasizeof, size_t count, int32_t fil)
{
kread(fil, buffer, dasizeof * count);
#if 0
size_t i, j;
int32_t k, kgoal;
short leng;
uint8_t *ptr;
lzwbuflock[0] = lzwbuflock[1] = lzwbuflock[2] = lzwbuflock[3] = lzwbuflock[4] = 200;
if (lzwbuf1 == NULL) allocache(&lzwbuf1,LZWSIZE+(LZWSIZE>>4),&lzwbuflock[0]);
if (lzwbuf2 == NULL) allocache((uint8_t**)&lzwbuf2,(LZWSIZE+(LZWSIZE>>4))*2,&lzwbuflock[1]);
if (lzwbuf3 == NULL) allocache((uint8_t**)&lzwbuf3,(LZWSIZE+(LZWSIZE>>4))*2,&lzwbuflock[2]);
if (lzwbuf4 == NULL) allocache(&lzwbuf4,LZWSIZE,&lzwbuflock[3]);
if (lzwbuf5 == NULL) allocache(&lzwbuf5,LZWSIZE+(LZWSIZE>>4),&lzwbuflock[4]);
if (dasizeof > LZWSIZE) { count *= dasizeof; dasizeof = 1; }
ptr = (uint8_t *)buffer;
kread(fil,&leng,2); kread(fil,lzwbuf5,(int32_t )leng);
k = 0;
kgoal = uncompress(lzwbuf5,leng,lzwbuf4);
copybufbyte(lzwbuf4,ptr,(int32_t )dasizeof);
k += (int32_t )dasizeof;
for(i=1;i<count;i++)
{
if (k >= kgoal)
{
kread(fil,&leng,2); kread(fil,lzwbuf5,(int32_t )leng);
k = 0; kgoal = uncompress(lzwbuf5,(int32_t )leng,lzwbuf4);
}
for(j=0;j<dasizeof;j++) ptr[j+dasizeof] = (uint8_t ) ((ptr[j]+lzwbuf4[j+k])&255);
k += dasizeof;
ptr += dasizeof;
}
lzwbuflock[0] = lzwbuflock[1] = lzwbuflock[2] = lzwbuflock[3] = lzwbuflock[4] = 1;
#endif
}
void dfread(void *buffer, size_t dasizeof, size_t count, FILE *fil)
{
fread(buffer, dasizeof, count, fil);
#if 0
size_t i, j;
int32_t k, kgoal;
short leng;
uint8_t *ptr;
lzwbuflock[0] = lzwbuflock[1] = lzwbuflock[2] = lzwbuflock[3] = lzwbuflock[4] = 200;
if (lzwbuf1 == NULL) allocache(&lzwbuf1,LZWSIZE+(LZWSIZE>>4),&lzwbuflock[0]);
if (lzwbuf2 == NULL) allocache((uint8_t**)&lzwbuf2,(LZWSIZE+(LZWSIZE>>4))*2,&lzwbuflock[1]);
if (lzwbuf3 == NULL) allocache((uint8_t**)&lzwbuf3,(LZWSIZE+(LZWSIZE>>4))*2,&lzwbuflock[2]);
if (lzwbuf4 == NULL) allocache(&lzwbuf4,LZWSIZE,&lzwbuflock[3]);
if (lzwbuf5 == NULL) allocache(&lzwbuf5,LZWSIZE+(LZWSIZE>>4),&lzwbuflock[4]);
if (dasizeof > LZWSIZE) {
count *= dasizeof;
dasizeof = 1;
}
ptr = (uint8_t *)buffer;
fread(&leng,2,1,fil);
fread(lzwbuf5,(int32_t )leng,1,fil);
k = 0;
kgoal = uncompress(lzwbuf5,(int32_t )leng,lzwbuf4);
copybufbyte(lzwbuf4,ptr,(int32_t )dasizeof);
k += (int32_t )dasizeof;
for(i=1;i<count;i++)
{
if (k >= kgoal)
{
fread(&leng,2,1,fil); fread(lzwbuf5,(int32_t )leng,1,fil);
k = 0; kgoal = uncompress(lzwbuf5,(int32_t )leng,lzwbuf4);
}
for(j=0;j<dasizeof;j++) ptr[j+dasizeof] = (uint8_t ) ((ptr[j]+lzwbuf4[j+k])&255);
k += dasizeof;
ptr += dasizeof;
}
lzwbuflock[0] = lzwbuflock[1] = lzwbuflock[2] = lzwbuflock[3] = lzwbuflock[4] = 1;
#endif
}
void dfwrite(void *buffer, size_t dasizeof, size_t count, FILE *fil)
{
fwrite(buffer, dasizeof, count, fil);
#if 0
size_t i, j, k;
short leng;
uint8_t *ptr;
lzwbuflock[0] = lzwbuflock[1] = lzwbuflock[2] = lzwbuflock[3] = lzwbuflock[4] = 200;
if (lzwbuf1 == NULL) allocache(&lzwbuf1,LZWSIZE+(LZWSIZE>>4),&lzwbuflock[0]);
if (lzwbuf2 == NULL) allocache((uint8_t**)&lzwbuf2,(LZWSIZE+(LZWSIZE>>4))*2,&lzwbuflock[1]);
if (lzwbuf3 == NULL) allocache((uint8_t**)&lzwbuf3,(LZWSIZE+(LZWSIZE>>4))*2,&lzwbuflock[2]);
if (lzwbuf4 == NULL) allocache(&lzwbuf4,LZWSIZE,&lzwbuflock[3]);
if (lzwbuf5 == NULL) allocache(&lzwbuf5,LZWSIZE+(LZWSIZE>>4),&lzwbuflock[4]);
if (dasizeof > LZWSIZE) { count *= dasizeof; dasizeof = 1; }
ptr = (uint8_t *)buffer;
copybufbyte(ptr,lzwbuf4,(int32_t )dasizeof);
k = dasizeof;
if (k > LZWSIZE-dasizeof)
{
leng = (short)compress(lzwbuf4,k,lzwbuf5); k = 0;
fwrite(&leng,2,1,fil); fwrite(lzwbuf5,(int32_t )leng,1,fil);
}
for(i=1;i<count;i++)
{
for(j=0;j<dasizeof;j++) lzwbuf4[j+k] = (uint8_t ) ((ptr[j+dasizeof]-ptr[j])&255);
k += dasizeof;
if (k > LZWSIZE-dasizeof)
{
leng = (short)compress(lzwbuf4,k,lzwbuf5); k = 0;
fwrite(&leng,2,1,fil); fwrite(lzwbuf5,(int32_t )leng,1,fil);
}
ptr += dasizeof;
}
if (k > 0)
{
leng = (short)compress(lzwbuf4,k,lzwbuf5);
fwrite(&leng,2,1,fil); fwrite(lzwbuf5,(int32_t )leng,1,fil);
}
lzwbuflock[0] = lzwbuflock[1] = lzwbuflock[2] = lzwbuflock[3] = lzwbuflock[4] = 1;
#endif
}
int SafeFileExists ( const char * _filename );
int32_t TCkopen4load(const char *filename, int32_t readfromGRP)
{
char fullfilename[512] = "\0";
int32_t result = 0;
if(game_dir[0] != '\0' && !readfromGRP)
{
sprintf(fullfilename, "%s/%s", game_dir, filename);
if (!SafeFileExists(fullfilename)) // try root
sprintf(fullfilename, "%s", filename);
}
else
{
sprintf(fullfilename, "%s/%s", game_dir, filename);
}
result = kopen4load(fullfilename, readfromGRP);
return result;
}
void setGameDir(char* gameDir){
if (gameDir == NULL)
return;
strncpy(game_dir,gameDir,sizeof(game_dir));
}
char* getGameDir(void){
//return game_dir;
return "/.rockbox/duke3d";
}
int getGRPcrc32(int grpID){
return grpSet.archives[grpID].crc32;
}
|