-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunrarlib.c
2725 lines (2249 loc) · 75.3 KB
/
unrarlib.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
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ***************************************************************************
**
** This file is part of the UniquE RAR File Library.
**
** Copyright (C) 2000-2002 by Christian Scheurer (www.ChristianScheurer.ch)
** UNIX port copyright (c) 2000-2002 by Johannes Winkelmann (jw@tks6.net)
**
** The contents of this file are subject to the UniquE RAR File Library
** License (the "unrarlib-license.txt"). You may not use this file except
** in compliance with the License. You may obtain a copy of the License
** at http://www.unrarlib.org/license.html.
** Software distributed under the License is distributed on an "AS IS"
** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied warranty.
**
** Alternatively, the contents of this file may be used under the terms
** of the GNU General Public License Version 2 or later (the "GPL"), in
** which case the provisions of the GPL are applicable instead of those
** above. If you wish to allow use of your version of this file only
** under the terms of the GPL and not to allow others to use your version
** of this file under the terms of the UniquE RAR File Library License,
** indicate your decision by deleting the provisions above and replace
** them with the notice and other provisions required by the GPL. If you
** do not delete the provisions above, a recipient may use your version
** of this file under the terms of the GPL or the UniquE RAR File Library
** License.
**
************************************************************************** */
/* ***************************************************************************
**
** UniquE RAR File Library
** The free file lib for the demoscene
** multi-OS version (Win32, Linux and SunOS)
**
*****************************************************************************
**
** ==> Please configure the program in "unrarlib.h". <==
**
** RAR decompression code:
** (C) Eugene Roshal
** Modifications to a FileLib:
** (C) 2000-2002 Christian Scheurer aka. UniquE/Vantage (cs@unrarlib.org)
** Linux port:
** (C) 2000-2002 Johannes Winkelmann (jw@tks6.net)
**
** The UniquE RAR File Library gives you the ability to access RAR archives
** (any compression method supported in RAR v2.0 including Multimedia
** Compression and encryption) directly from your program with ease an by
** adding only 12kB (6kB UPX-compressed) additional code to your program.
** Both solid and normal (recommended for fast random access to the files!)
** archives are supported. This FileLib is made for the Demo scene, so it's
** designed for easy use within your demos and intros.
** Please read "licence.txt" to learn more about how you may use URARFileLib
** in your productions.
**
*****************************************************************************
**
** ==> see the "CHANGES" file to see what's new
**
************************************************************************** */
/* -- include files ------------------------------------------------------- */
#include "unrarlib.h" /* include global configuration */
/* ------------------------------------------------------------------------ */
/* -- global stuff -------------------------------------------------------- */
#ifdef _WIN_32
#include <windows.h> /* WIN32 definitions */
#include <stdio.h>
#include <string.h>
#define ENABLE_ACCESS
#define HOST_OS WIN_32
#define FM_NORMAL 0x00
#define FM_RDONLY 0x01
#define FM_HIDDEN 0x02
#define FM_SYSTEM 0x04
#define FM_LABEL 0x08
#define FM_DIREC 0x10
#define FM_ARCH 0x20
#define PATHDIVIDER "\\"
#define CPATHDIVIDER '\\'
#define MASKALL "*.*"
#define READBINARY "rb"
#define READTEXT "rt"
#define UPDATEBINARY "r+b"
#define CREATEBINARY "w+b"
#define CREATETEXT "w"
#define APPENDTEXT "at"
#endif
#ifdef _UNIX
#include <stdio.h> /* LINUX/UNIX definitions */
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define ENABLE_ACCESS
#define HOST_OS UNIX
#define FM_LABEL 0x0000
#define FM_DIREC 0x4000
#define PATHDIVIDER "/"
#define CPATHDIVIDER '/'
#define MASKALL "*.*"
#define READBINARY "r"
#define READTEXT "r"
#define UPDATEBINARY "r+"
#define CREATEBINARY "w+"
#define CREATETEXT "w"
#define APPENDTEXT "a"
/* emulation of the windows API and data types */
/* 20-08-2000 Johannes Winkelmann, jw@tks6.net */
typedef long DWORD;
typedef short BOOL;
#define TRUE 1
#define FALSE 0
#ifdef _DEBUG_LOG /* define macros for debugging */
#include <unistd.h>
#include <sys/time.h>
DWORD GetTickCount()
{
struct timeval tv;
gettimeofday( &tv, 0 );
return (tv.tv_usec / 1000);
}
#endif
#endif
#ifdef _DEBUG_LOG /* define macros for debugging */
BOOL debug_log_first_start = TRUE;
#define debug_log(a); debug_log_proc(a, __FILE__, __LINE__);
#define debug_init(a); debug_init_proc(a);
void debug_log_proc(char *text, char *sourcefile, int sourceline);
void debug_init_proc(char *file_name);
#else
#define debug_log(a); /* no debug this time */
#define debug_init(a); /* no debug this time */
#endif
#define MAXWINSIZE 0x100000
#define MAXWINMASK (MAXWINSIZE-1)
#define UNP_MEMORY MAXWINSIZE
#define Min(x,y) (((x)<(y)) ? (x):(y))
#define Max(x,y) (((x)>(y)) ? (x):(y))
#define NM 260
#define SIZEOF_MARKHEAD 7
#define SIZEOF_OLDMHD 7
#define SIZEOF_NEWMHD 13
#define SIZEOF_OLDLHD 21
#define SIZEOF_NEWLHD 32
#define SIZEOF_SHORTBLOCKHEAD 7
#define SIZEOF_LONGBLOCKHEAD 11
#define SIZEOF_COMMHEAD 13
#define SIZEOF_PROTECTHEAD 26
#define PACK_VER 20 /* version of decompression code*/
#define UNP_VER 20
#define PROTECT_VER 20
enum { M_DENYREAD,M_DENYWRITE,M_DENYNONE,M_DENYALL };
enum { FILE_EMPTY,FILE_ADD,FILE_UPDATE,FILE_COPYOLD,FILE_COPYBLOCK };
enum { SUCCESS,WARNING,FATAL_ERROR,CRC_ERROR,LOCK_ERROR,WRITE_ERROR,
OPEN_ERROR,USER_ERROR,MEMORY_ERROR,USER_BREAK=255,IMM_ABORT=0x8000 };
enum { EN_LOCK=1,EN_VOL=2 };
enum { SD_MEMORY=1,SD_FILES=2 };
enum { NAMES_DONTCHANGE };
enum { LOG_ARC=1,LOG_FILE=2 };
enum { OLD_DECODE=0,OLD_ENCODE=1,NEW_CRYPT=2 };
enum { OLD_UNPACK,NEW_UNPACK };
#define MHD_COMMENT 2
#define MHD_LOCK 4
#define MHD_PACK_COMMENT 16
#define MHD_AV 32
#define MHD_PROTECT 64
#define LHD_SPLIT_BEFORE 1
#define LHD_SPLIT_AFTER 2
#define LHD_PASSWORD 4
#define LHD_COMMENT 8
#define LHD_SOLID 16
#define LHD_WINDOWMASK 0x00e0
#define LHD_WINDOW64 0
#define LHD_WINDOW128 32
#define LHD_WINDOW256 64
#define LHD_WINDOW512 96
#define LHD_WINDOW1024 128
#define LHD_DIRECTORY 0x00e0
#define LONG_BLOCK 0x8000
#define READSUBBLOCK 0x8000
enum { ALL_HEAD=0,MARK_HEAD=0x72,MAIN_HEAD=0x73,FILE_HEAD=0x74,
COMM_HEAD=0x75,AV_HEAD=0x76,SUB_HEAD=0x77,PROTECT_HEAD=0x78};
enum { EA_HEAD=0x100 };
enum { MS_DOS=0,OS2=1,WIN_32=2,UNIX=3 };
struct MarkHeader
{
UBYTE Mark[7];
};
struct NewMainArchiveHeader
{
UWORD HeadCRC;
UBYTE HeadType;
UWORD Flags;
UWORD HeadSize;
UWORD Reserved;
UDWORD Reserved1;
};
struct NewFileHeader
{
UWORD HeadCRC;
UBYTE HeadType;
UWORD Flags;
UWORD HeadSize;
UDWORD PackSize;
UDWORD UnpSize;
UBYTE HostOS;
UDWORD FileCRC;
UDWORD FileTime;
UBYTE UnpVer;
UBYTE Method;
UWORD NameSize;
UDWORD FileAttr;
};
struct BlockHeader
{
UWORD HeadCRC;
UBYTE HeadType;
UWORD Flags;
UWORD HeadSize;
UDWORD DataSize;
};
struct Decode
{
unsigned int MaxNum;
unsigned int DecodeLen[16];
unsigned int DecodePos[16];
unsigned int DecodeNum[2];
};
struct MarkHeader MarkHead;
struct NewMainArchiveHeader NewMhd;
struct NewFileHeader NewLhd;
struct BlockHeader BlockHead;
static UBYTE *TempMemory = NULL; /* temporary unpack-buffer */
static char *CommMemory = NULL;
static UBYTE *UnpMemory = NULL;
static char* ArgName = NULL; /* current file in rar archive */
static char* ArcFileName = NULL; /* file to decompress */
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION /* mem-to-mem decompression */
static MemoryFile *MemRARFile; /* pointer to RAR file in memory*/
#else
static char* ArcName = NULL; /* RAR archive name */
static FILE *ArcPtr; /* input RAR file handler */
#endif
static char *Password = NULL; /* password to decrypt files */
static unsigned char *temp_output_buffer; /* extract files to this pointer*/
static unsigned long *temp_output_buffer_offset; /* size of temp. extract buffer */
static BOOL FileFound; /* TRUE=use current extracted */
/* data FALSE=throw data away, */
/* wrong file */
static int MainHeadSize;
static long CurBlockPos,NextBlockPos;
static unsigned long CurUnpRead, CurUnpWrite;
static long UnpPackedSize;
static long DestUnpSize;
static UDWORD HeaderCRC;
static int Encryption;
//static unsigned int UnpWrSize;
//static unsigned char *UnpWrAddr;
static unsigned int UnpPtr,WrPtr;
static unsigned char PN1,PN2,PN3;
static unsigned short OldKey[4];
/* function header definitions */
static int ReadHeader(int BlockType);
static BOOL ExtrFile(void);
//BOOL ListFile(void);
static int tread(void *stream,void *buf,unsigned len);
static int tseek(void *stream,long offset,int fromwhere);
static BOOL UnstoreFile(void);
static int IsArchive(void);
static int ReadBlock(int BlockType);
static unsigned int UnpRead(unsigned char *Addr,unsigned int Count);
static void UnpInitData(void);
static void Unpack(unsigned char *UnpAddr);
static UBYTE DecodeAudio(int Delta);
static void DecodeNumber(struct Decode *Dec);
static void UpdKeys(UBYTE *Buf);
static void SetCryptKeys(char *Password);
static void SetOldKeys(char *Password);
static void DecryptBlock(unsigned char *Buf);
static void InitCRC(void);
static UDWORD CalcCRC32(UDWORD StartCRC,UBYTE *Addr,UDWORD Size);
static void UnpReadBuf(int FirstBuf);
static void ReadTables(void);
static void ReadLastTables(void);
static void MakeDecodeTables(unsigned char *LenTab,
struct Decode *Dec,
int Size);
static int my_stricomp(char *Str1,char *Str2);
/* ------------------------------------------------------------------------ */
/* -- global functions ---------------------------------------------------- */
int urarlib_get(void *output,
unsigned long *size,
char *filename,
void *rarfile,
char *libpassword)
/* Get a file from a RAR file to the "output" buffer. The UniquE RAR FileLib
* does everything from allocating memory, decrypting and unpacking the file
* from the archive. TRUE is returned if the file could be successfully
* extracted, else a FALSE indicates a failure.
*/
{
BOOL retcode = FALSE;
#ifdef _DEBUG_LOG
int str_offs; /* used for debug-strings */
char DebugMsg[500]; /* used to compose debug msg */
if(debug_log_first_start)
{
debug_log_first_start=FALSE; /* only create a new log file */
debug_init(_DEBUG_LOG_FILE); /* on startup */
}
#endif
InitCRC(); /* init some vars */
if(ArgName) free(ArgName);
ArgName = strdup(filename); /* set file(s) to extract */
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
MemRARFile = rarfile; /* set pointer to mem-RAR file */
#else
if(ArcName) free(ArcName);
ArcName = strdup(rarfile); /* set RAR file name */
#endif
if(Password) free(Password);
if(libpassword != NULL)
Password = strdup(libpassword); /* init password */
else
Password = strdup("");
temp_output_buffer = NULL;
temp_output_buffer_offset=size; /* set size of the temp buffer */
#ifdef _DEBUG_LOG
sprintf(DebugMsg, "Extracting >%s< from >%s< (password is >%s<)...",
filename, (char*)rarfile, libpassword);
debug_log(DebugMsg);
#endif
retcode = ExtrFile(); /* unpack file now! */
//memset(Password,0,sizeof(Password)); /* clear password */
#ifndef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
if (ArcPtr!=NULL){
fclose(ArcPtr);
ArcPtr = NULL;
}
#endif
if(UnpMemory) free(UnpMemory); /* free memory */
if(TempMemory) free(TempMemory);
if(CommMemory) free(CommMemory);
UnpMemory=NULL;
TempMemory=NULL;
CommMemory=NULL;
if(retcode == FALSE)
{
if(temp_output_buffer) /* free memory and return NULL */
free(temp_output_buffer);
temp_output_buffer=NULL;
*(DWORD*)output=0; /* pointer on errors */
*size=0;
#ifdef _DEBUG_LOG
/* sorry for this ugly code, but older SunOS gcc compilers don't support */
/* white spaces within strings */
str_offs = sprintf(DebugMsg, "Error - couldn't extract ");
str_offs += sprintf(DebugMsg + str_offs, ">%s<", filename);
str_offs += sprintf(DebugMsg + str_offs, " and allocated ");
str_offs += sprintf(DebugMsg + str_offs, "%u Bytes", (unsigned int)*size);
str_offs += sprintf(DebugMsg + str_offs, " of unused memory!");
} else
{
sprintf(DebugMsg, "Extracted %u Bytes.", (unsigned int)*size);
}
debug_log(DebugMsg);
#else
}
#endif
*(DWORD*)output=(DWORD)temp_output_buffer;/* return pointer for unpacked*/
/* data */
return retcode;
}
int urarlib_list(void *rarfile, ArchiveList_struct *list)
{
ArchiveList_struct *tmp_List = NULL;
int NoOfFilesInArchive = 0; /* number of files in archive */
#ifdef _DEBUG_LOG
if(debug_log_first_start)
{
debug_log_first_start=FALSE; /* only create a new log file */
debug_init(_DEBUG_LOG_FILE); /* on startup */
}
#endif
InitCRC(); /* init some vars */
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
MemRARFile = rarfile; /* assign pointer to RAR file */
MemRARFile->offset = 0;
if (!IsArchive())
{
debug_log("Not a RAR file");
return NoOfFilesInArchive; /* error => exit! */
}
#else
/* open and identify archive */
if ((ArcPtr=fopen(rarfile,READBINARY))!=NULL)
{
if (!IsArchive())
{
debug_log("Not a RAR file");
fclose(ArcPtr);
ArcPtr = NULL;
return NoOfFilesInArchive; /* error => exit! */
}
}
else {
debug_log("Error opening file.");
return NoOfFilesInArchive;
}
#endif
if ((UnpMemory=malloc(UNP_MEMORY))==NULL)
{
debug_log("Can't allocate memory for decompression!");
return NoOfFilesInArchive;
}
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
MemRARFile->offset+=NewMhd.HeadSize-MainHeadSize;
#else
tseek(ArcPtr,NewMhd.HeadSize-MainHeadSize,SEEK_CUR);
#endif
(*(DWORD*)list) = (DWORD)NULL; /* init file list */
/* do while file is not extracted and there's no error */
while (TRUE)
{
if (ReadBlock(FILE_HEAD | READSUBBLOCK) <= 0) /* read name of the next */
{ /* file within the RAR archive */
debug_log("Couldn't read next filename from archive (I/O error).");
break; /* error, file not found in */
} /* archive or I/O error */
if (BlockHead.HeadType==SUB_HEAD)
{
debug_log("Sorry, sub-headers not supported.");
break; /* error => exit */
}
if((void*)(*(DWORD*)list) == NULL) /* first entry */
{
tmp_List = malloc(sizeof(ArchiveList_struct));
tmp_List->next = NULL;
(*(DWORD*)list) = (DWORD)tmp_List;
} else /* add entry */
{
tmp_List->next = malloc(sizeof(ArchiveList_struct));
tmp_List = (ArchiveList_struct*) tmp_List->next;
tmp_List->next = NULL;
}
tmp_List->item.Name = malloc(NewLhd.NameSize + 1);
strcpy(tmp_List->item.Name, ArcFileName);
tmp_List->item.NameSize = NewLhd.NameSize;
tmp_List->item.PackSize = NewLhd.PackSize;
tmp_List->item.UnpSize = NewLhd.UnpSize;
tmp_List->item.HostOS = NewLhd.HostOS;
tmp_List->item.FileCRC = NewLhd.FileCRC;
tmp_List->item.FileTime = NewLhd.FileTime;
tmp_List->item.UnpVer = NewLhd.UnpVer;
tmp_List->item.Method = NewLhd.Method;
tmp_List->item.FileAttr = NewLhd.FileAttr;
NoOfFilesInArchive++; /* count files */
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
MemRARFile->offset = NextBlockPos;
#else
if (ArcPtr!=NULL) tseek(ArcPtr,NextBlockPos,SEEK_SET);
#endif
};
/* free memory, clear password and close archive */
memset(Password,0,sizeof(Password)); /* clear password */
#ifndef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
if (ArcPtr!=NULL){
fclose(ArcPtr);
ArcPtr = NULL;
}
#endif
free(UnpMemory); /* free memory */
free(TempMemory);
free(CommMemory);
UnpMemory=NULL;
TempMemory=NULL;
CommMemory=NULL;
return NoOfFilesInArchive;
}
/* urarlib_freelist:
* (after the suggestion and code of Duy Nguyen, Sean O'Blarney
* and Johannes Winkelmann who independently wrote a patch)
* free the memory of a ArchiveList_struct created by urarlib_list.
*
* input: *list pointer to an ArchiveList_struct
* output: -
*/
void urarlib_freelist(ArchiveList_struct *list)
{
ArchiveList_struct* tmp = list;
while ( list ) {
tmp = list->next;
free( list->item.Name );
free( list );
list = tmp;
}
}
/* ------------------------------------------------------------------------ */
/****************************************************************************
****************************************************************************
****************************************************************************
****************************************************************************
******* *******
******* *******
******* *******
******* B L O C K I / O *******
******* *******
******* *******
******* *******
****************************************************************************
****************************************************************************
****************************************************************************
****************************************************************************/
#define GetHeaderByte(N) Header[N]
#define GetHeaderWord(N) (Header[N]+((UWORD)Header[N+1]<<8))
#define GetHeaderDword(N) (Header[N]+((UWORD)Header[N+1]<<8)+\
((UDWORD)Header[N+2]<<16)+\
((UDWORD)Header[N+3]<<24))
int ReadBlock(int BlockType)
{
struct NewFileHeader SaveFileHead;
int Size=0,ReadSubBlock=0;
static int LastBlock;
memcpy(&SaveFileHead,&NewLhd,sizeof(SaveFileHead));
if (BlockType & READSUBBLOCK)
ReadSubBlock=1;
BlockType &= 0xff;
{
while (1)
{
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
CurBlockPos=MemRARFile->offset; /* get offset of mem-file */
#else
CurBlockPos=ftell(ArcPtr);
#endif
Size=ReadHeader(FILE_HEAD);
if (Size!=0)
{
if (NewLhd.HeadSize<SIZEOF_SHORTBLOCKHEAD)
return(0);
NextBlockPos=CurBlockPos+NewLhd.HeadSize;
if (NewLhd.Flags & LONG_BLOCK)
NextBlockPos+=NewLhd.PackSize;
if (NextBlockPos<=CurBlockPos)
return(0);
}
if (Size > 0 && BlockType!=SUB_HEAD)
LastBlock=BlockType;
if (Size==0 || BlockType==ALL_HEAD || NewLhd.HeadType==BlockType ||
(NewLhd.HeadType==SUB_HEAD && ReadSubBlock && LastBlock==BlockType))
break;
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
MemRARFile->offset = NextBlockPos;
#else
tseek(ArcPtr, NextBlockPos, SEEK_SET);
#endif
}
}
BlockHead.HeadCRC=NewLhd.HeadCRC;
BlockHead.HeadType=NewLhd.HeadType;
BlockHead.Flags=NewLhd.Flags;
BlockHead.HeadSize=NewLhd.HeadSize;
BlockHead.DataSize=NewLhd.PackSize;
if (BlockType!=NewLhd.HeadType) BlockType=ALL_HEAD;
if((FILE_HEAD == BlockType) && (Size>0))
{
ArcFileName=realloc(ArcFileName,NewLhd.NameSize+1);
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
tread(MemRARFile, ArcFileName, NewLhd.NameSize);
#else
tread(ArcPtr,ArcFileName,NewLhd.NameSize);
#endif
ArcFileName[NewLhd.NameSize]=0;
#ifdef _DEBUG_LOG
if (NewLhd.HeadCRC!=(UWORD)~CalcCRC32(HeaderCRC,(UBYTE*)&ArcFileName[0],
NewLhd.NameSize))
{
debug_log("file header broken");
}
#endif
Size+=NewLhd.NameSize;
} else
{
memcpy(&NewLhd,&SaveFileHead,sizeof(NewLhd));
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
MemRARFile->offset = CurBlockPos;
#else
tseek(ArcPtr,CurBlockPos,SEEK_SET);
#endif
}
return(Size);
}
int ReadHeader(int BlockType)
{
int Size = 0;
unsigned char Header[64];
switch(BlockType)
{
case MAIN_HEAD:
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
Size=tread(MemRARFile, Header, SIZEOF_NEWMHD);
#else
Size=tread(ArcPtr,Header,SIZEOF_NEWMHD);
#endif
NewMhd.HeadCRC=(unsigned short)GetHeaderWord(0);
NewMhd.HeadType=GetHeaderByte(2);
NewMhd.Flags=(unsigned short)GetHeaderWord(3);
NewMhd.HeadSize=(unsigned short)GetHeaderWord(5);
NewMhd.Reserved=(unsigned short)GetHeaderWord(7);
NewMhd.Reserved1=GetHeaderDword(9);
HeaderCRC=CalcCRC32(0xFFFFFFFFL,&Header[2],SIZEOF_NEWMHD-2);
break;
case FILE_HEAD:
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
Size=tread(MemRARFile, Header, SIZEOF_NEWLHD);
#else
Size=tread(ArcPtr,Header,SIZEOF_NEWLHD);
#endif
NewLhd.HeadCRC=(unsigned short)GetHeaderWord(0);
NewLhd.HeadType=GetHeaderByte(2);
NewLhd.Flags=(unsigned short)GetHeaderWord(3);
NewLhd.HeadSize=(unsigned short)GetHeaderWord(5);
NewLhd.PackSize=GetHeaderDword(7);
NewLhd.UnpSize=GetHeaderDword(11);
NewLhd.HostOS=GetHeaderByte(15);
NewLhd.FileCRC=GetHeaderDword(16);
NewLhd.FileTime=GetHeaderDword(20);
NewLhd.UnpVer=GetHeaderByte(24);
NewLhd.Method=GetHeaderByte(25);
NewLhd.NameSize=(unsigned short)GetHeaderWord(26);
NewLhd.FileAttr=GetHeaderDword(28);
HeaderCRC=CalcCRC32(0xFFFFFFFFL,&Header[2],SIZEOF_NEWLHD-2);
break;
#ifdef _DEBUG_LOG
case COMM_HEAD: /* log errors in case of debug */
debug_log("Comment headers not supported! "\
"Please create archives without comments.");
break;
case PROTECT_HEAD:
debug_log("Protected headers not supported!");
break;
case ALL_HEAD:
debug_log("ShortBlockHeader not supported!");
break;
default:
debug_log("Unknown//unsupported !");
#else
default: /* else do nothing */
break;
#endif
}
return(Size);
}
/* **************************************************************************
****************************************************************************
****************************************************************************
************************************************************************** */
/* **************************************************************************
****************************************************************************
****************************************************************************
****************************************************************************
******* *******
******* *******
******* *******
******* E X T R A C T L O O P *******
******* *******
******* *******
******* *******
****************************************************************************
****************************************************************************
****************************************************************************
************************************************************************** */
int IsArchive(void)
{
#ifdef _DEBUG_LOG
int str_offs; /* used for debug-strings */
char DebugMsg[500]; /* used to compose debug msg */
#endif
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
if (tread(MemRARFile, MarkHead.Mark, SIZEOF_MARKHEAD) != SIZEOF_MARKHEAD)
return(FALSE);
#else
if (tread(ArcPtr,MarkHead.Mark,SIZEOF_MARKHEAD)!=SIZEOF_MARKHEAD)
return(FALSE);
#endif
/* Old archive => error */
if (MarkHead.Mark[0]==0x52 && MarkHead.Mark[1]==0x45 &&
MarkHead.Mark[2]==0x7e && MarkHead.Mark[3]==0x5e)
{
debug_log("Attention: format as OLD detected! Can't handel archive!");
}
else
/* original RAR v2.0 */
if ((MarkHead.Mark[0]==0x52 && MarkHead.Mark[1]==0x61 && /* original */
MarkHead.Mark[2]==0x72 && MarkHead.Mark[3]==0x21 && /* RAR header*/
MarkHead.Mark[4]==0x1a && MarkHead.Mark[5]==0x07 &&
MarkHead.Mark[6]==0x00) ||
/* "UniquE!" - header */
(MarkHead.Mark[0]=='U' && MarkHead.Mark[1]=='n' && /* "UniquE!" */
MarkHead.Mark[2]=='i' && MarkHead.Mark[3]=='q' && /* header */
MarkHead.Mark[4]=='u' && MarkHead.Mark[5]=='E' &&
MarkHead.Mark[6]=='!'))
{
if (ReadHeader(MAIN_HEAD)!=SIZEOF_NEWMHD)
return(FALSE);
} else
{
#ifdef _DEBUG_LOG
/* sorry for this ugly code, but older SunOS gcc compilers don't */
/* support white spaces within strings */
str_offs = sprintf(DebugMsg, "unknown archive type (only plain RAR ");
str_offs += sprintf(DebugMsg + str_offs, "supported (normal and solid ");
str_offs += sprintf(DebugMsg + str_offs, "archives), SFX and Volumes ");
str_offs += sprintf(DebugMsg + str_offs, "are NOT supported!)");
debug_log(DebugMsg);
#endif
}
MainHeadSize=SIZEOF_NEWMHD;
return(TRUE);
}
BOOL ExtrFile(void)
{
BOOL ReturnCode=TRUE;
FileFound=FALSE; /* no file found by default */
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
MemRARFile->offset = 0; /* start reading from offset 0 */
if (!IsArchive())
{
debug_log("Not a RAR file");
return FALSE; /* error => exit! */
}
#else
/* open and identify archive */
if ((ArcPtr=fopen(ArcName,READBINARY))!=NULL)
{
if (!IsArchive())
{
debug_log("Not a RAR file");
fclose(ArcPtr);
ArcPtr = NULL;
return FALSE; /* error => exit! */
}
} else
{
debug_log("Error opening file.");
return FALSE;
}
#endif
if ((UnpMemory=malloc(UNP_MEMORY))==NULL)
{
debug_log("Can't allocate memory for decompression!");
return FALSE;
}
#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
MemRARFile->offset+=NewMhd.HeadSize-MainHeadSize;
#else
tseek(ArcPtr,NewMhd.HeadSize-MainHeadSize,SEEK_CUR);
#endif
/* do while file is not extracted and there's no error */
do
{
if (ReadBlock(FILE_HEAD | READSUBBLOCK) <= 0) /* read name of the next */
{ /* file within the RAR archive */
/*
*
* 21.11.2000 UnQ There's a problem with some linux distros when a file
* can not be found in an archive.
*
* debug_log("Couldn't read next filename from archive (I/O error).");
*
*/
ReturnCode=FALSE;
break; /* error, file not found in */
} /* archive or I/O error */
if (BlockHead.HeadType==SUB_HEAD)
{
debug_log("Sorry, sub-headers not supported.");
ReturnCode=FALSE;
break; /* error => exit */
}
if(TRUE == (FileFound=(my_stricomp(ArgName, ArcFileName) == 0)))
/* *** file found! *** */
{
{
temp_output_buffer=malloc(NewLhd.UnpSize);/* allocate memory for the*/
}
*temp_output_buffer_offset=0; /* file. The default offset */
/* within the buffer is 0 */
if(temp_output_buffer == NULL)
{
debug_log("can't allocate memory for the file decompression");
ReturnCode=FALSE;
break; /* error, can't extract file! */
}
}
/* in case of a solid archive, we need to decompress any single file till
* we have found the one we are looking for. In case of normal archives
* (recommended!!), we skip the files until we are sure that it is the
* one we want.
*/
if((NewMhd.Flags & 0x08) || FileFound)
{
if (NewLhd.UnpVer<13 || NewLhd.UnpVer>UNP_VER)
{
debug_log("unknown compression method");