Rizin
unix-like reverse engineering framework and cli tools
cab.h
Go to the documentation of this file.
1 /* This file is part of libmspack.
2  * (C) 2003-2018 Stuart Caie.
3  *
4  * libmspack is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License (LGPL) version 2.1
6  *
7  * For further details, see the file COPYING.LIB distributed with libmspack
8  */
9 
10 #ifndef MSPACK_CAB_H
11 #define MSPACK_CAB_H 1
12 
13 /* generic CAB definitions */
14 
15 /* structure offsets */
16 #define cfhead_Signature (0x00)
17 #define cfhead_CabinetSize (0x08)
18 #define cfhead_FileOffset (0x10)
19 #define cfhead_MinorVersion (0x18)
20 #define cfhead_MajorVersion (0x19)
21 #define cfhead_NumFolders (0x1A)
22 #define cfhead_NumFiles (0x1C)
23 #define cfhead_Flags (0x1E)
24 #define cfhead_SetID (0x20)
25 #define cfhead_CabinetIndex (0x22)
26 #define cfhead_SIZEOF (0x24)
27 #define cfheadext_HeaderReserved (0x00)
28 #define cfheadext_FolderReserved (0x02)
29 #define cfheadext_DataReserved (0x03)
30 #define cfheadext_SIZEOF (0x04)
31 #define cffold_DataOffset (0x00)
32 #define cffold_NumBlocks (0x04)
33 #define cffold_CompType (0x06)
34 #define cffold_SIZEOF (0x08)
35 #define cffile_UncompressedSize (0x00)
36 #define cffile_FolderOffset (0x04)
37 #define cffile_FolderIndex (0x08)
38 #define cffile_Date (0x0A)
39 #define cffile_Time (0x0C)
40 #define cffile_Attribs (0x0E)
41 #define cffile_SIZEOF (0x10)
42 #define cfdata_CheckSum (0x00)
43 #define cfdata_CompressedSize (0x04)
44 #define cfdata_UncompressedSize (0x06)
45 #define cfdata_SIZEOF (0x08)
46 
47 /* flags */
48 #define cffoldCOMPTYPE_MASK (0x000f)
49 #define cffoldCOMPTYPE_NONE (0x0000)
50 #define cffoldCOMPTYPE_MSZIP (0x0001)
51 #define cffoldCOMPTYPE_QUANTUM (0x0002)
52 #define cffoldCOMPTYPE_LZX (0x0003)
53 #define cfheadPREV_CABINET (0x0001)
54 #define cfheadNEXT_CABINET (0x0002)
55 #define cfheadRESERVE_PRESENT (0x0004)
56 #define cffileCONTINUED_FROM_PREV (0xFFFD)
57 #define cffileCONTINUED_TO_NEXT (0xFFFE)
58 #define cffileCONTINUED_PREV_AND_NEXT (0xFFFF)
59 
60 /* CAB data blocks are <= 32768 bytes in uncompressed form. Uncompressed
61  * blocks have zero growth. MSZIP guarantees that it won't grow above
62  * uncompressed size by more than 12 bytes. LZX guarantees it won't grow
63  * more than 6144 bytes. Quantum has no documentation, but the largest
64  * block seen in the wild is 337 bytes above uncompressed size.
65  */
66 #define CAB_BLOCKMAX (32768)
67 #define CAB_INPUTMAX (CAB_BLOCKMAX+6144)
68 
69 /* input buffer needs to be CAB_INPUTMAX + 1 byte to allow for max-sized block
70  * plus 1 trailer byte added by cabd_sys_read_block() for Quantum alignment.
71  *
72  * When MSCABD_PARAM_SALVAGE is set, block size is not checked so can be
73  * up to 65535 bytes, so max input buffer size needed is 65535 + 1
74  */
75 #define CAB_INPUTMAX_SALVAGE (65535)
76 #define CAB_INPUTBUF (CAB_INPUTMAX_SALVAGE + 1)
77 
78 /* There are no more than 65535 data blocks per folder, so a folder cannot
79  * be more than 32768*65535 bytes in length. As files cannot span more than
80  * one folder, this is also their max offset, length and offset+length limit.
81  */
82 #define CAB_FOLDERMAX (65535)
83 #define CAB_LENGTHMAX (CAB_BLOCKMAX * CAB_FOLDERMAX)
84 
85 /* CAB compression definitions */
86 
88  struct mscab_compressor base;
90  /* todo */
91 };
92 
93 /* CAB decompression definitions */
94 
96  struct mscabd_folder_p *folder; /* current folder we're extracting from */
97  struct mscabd_folder_data *data; /* current folder split we're in */
98  unsigned int offset; /* uncompressed offset within folder */
99  unsigned int block; /* which block are we decompressing? */
100  off_t outlen; /* cumulative sum of block output sizes */
101  struct mspack_system sys; /* special I/O code for decompressor */
102  int comp_type; /* type of compression used by folder */
103  int (*decompress)(void *, off_t); /* decompressor code */
104  void *state; /* decompressor state */
105  struct mscabd_cabinet_p *incab; /* cabinet where input data comes from */
106  struct mspack_file *infh; /* input file handle */
107  struct mspack_file *outfh; /* output file handle */
108  unsigned char *i_ptr, *i_end; /* input data consumed, end */
109  unsigned char input[CAB_INPUTBUF]; /* one input block of data */
110 };
111 
113  struct mscab_decompressor base;
118 };
119 
121  struct mscabd_cabinet base;
122  off_t blocks_off; /* offset to data blocks */
123  int block_resv; /* reserved space in data blocks */
124 };
125 
126 /* there is one of these for every cabinet a folder spans */
129  struct mscabd_cabinet_p *cab; /* cabinet file of this folder span */
130  off_t offset; /* cabinet offset of first datablock */
131 };
132 
134  struct mscabd_folder base;
135  struct mscabd_folder_data data; /* where are the data blocks? */
136  struct mscabd_file *merge_prev; /* first file needing backwards merge */
137  struct mscabd_file *merge_next; /* first file needing forwards merge */
138 };
139 
140 #endif
#define CAB_INPUTBUF
Definition: cab.h:76
static int
Definition: sfsocketcall.h:114
int off_t
Definition: sftypes.h:41
struct mscab_compressor base
Definition: cab.h:88
struct mspack_system * system
Definition: cab.h:89
int searchbuf_size
Definition: cab.h:116
struct mscab_decompressor base
Definition: cab.h:113
struct mspack_system * system
Definition: cab.h:115
struct mscabd_decompress_state * d
Definition: cab.h:114
int block_resv
Definition: cab.h:123
off_t blocks_off
Definition: cab.h:122
struct mscabd_cabinet base
Definition: cab.h:121
struct mspack_system sys
Definition: cab.h:101
unsigned int block
Definition: cab.h:99
unsigned int offset
Definition: cab.h:98
struct mspack_file * infh
Definition: cab.h:106
unsigned char * i_ptr
Definition: cab.h:108
struct mscabd_cabinet_p * incab
Definition: cab.h:105
unsigned char * i_end
Definition: cab.h:108
unsigned char input[CAB_INPUTBUF]
Definition: cab.h:109
struct mscabd_folder_p * folder
Definition: cab.h:96
int(* decompress)(void *, off_t)
Definition: cab.h:103
struct mscabd_folder_data * data
Definition: cab.h:97
struct mspack_file * outfh
Definition: cab.h:107
off_t offset
Definition: cab.h:130
struct mscabd_cabinet_p * cab
Definition: cab.h:129
struct mscabd_folder_data * next
Definition: cab.h:128
struct mscabd_folder base
Definition: cab.h:134
struct mscabd_folder_data data
Definition: cab.h:135
struct mscabd_file * merge_next
Definition: cab.h:137
struct mscabd_file * merge_prev
Definition: cab.h:136