Niotso  git revision 558726a9f13d7c3423a683dd2f4323589b66c310
The New Implementation of The Sims Online
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
far.h
Go to the documentation of this file.
1 /*
2  FileHandler - General-purpose file handling library for Niotso
3  far.h - Copyright (c) 2011 Niotso Project <http://niotso.org/>
4  Author(s): Fatbag <X-Fi6@phppoll.org>
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 
19 /****
20 ** Constants
21 */
22 
23 /* libfarOptions array members */
24 #define FAR_CONFIG_DEFAULT_TO_1A 0
25 #define FAR_CONFIG_DBPF_COMPRESSED 1
26 #define FAR_CONFIG_MAX_FILE_NAME_LENGTH 2
27 #define FAR_CONFIG_REFPACK_HNSV 3
28 
29 /* Archive types */
30 #define FAR_TYPE_INVALID 0
31 #define FAR_TYPE_FAR 1
32 #define FAR_TYPE_DBPF 2
33 #define FAR_TYPE_PERSIST 3
34 
35 /* Numerical constants */
36 #define FAR_ARCHIVE_MINIMUM_SIZE 14
37 #define MINSIZE_FAR 20
38 #define MINSIZE_DBPF 64
39 #define MINSIZE_ENTRY_FAR_1A 16
40 #define MINSIZE_ENTRY_FAR_1B 14
41 #define MINSIZE_ENTRY_FAR_3 24
42 #define SIZEOF_ENTRY_DBPF 20
43 #define MAX_ENTRIES_FAR_1A 268435455
44 #define MAX_ENTRIES_FAR_1B 306783377
45 #define MAX_ENTRIES_FAR_3 178956970
46 #define MAX_ENTRIES_DBPF 214748364
47 #define MINSIZE_PERSIST 18
48 #define MAXSIZE_REFPACK_UNCOMPRESSED 16777215
49 #define MAXSIZE_REFPACK_COMPRESSED 16777215
50 
51 /* Header bytes */
52 static const uint8_t Header_FAR[] = {'F','A','R','!','b','y','A','Z'};
53 static const uint8_t Header_DBPF[] = {'D','B','P','F'};
54 
55 /****
56 ** Data structures
57 */
58 
59 typedef struct PersistFile_s
60 {
61  uint8_t BodyType;
62  uint32_t DecompressedSize;
63  uint32_t CompressedSize;
64  uint32_t StreamBodySize;
65  uint8_t Compressor;
66  uint8_t * Parameters;
67  uint8_t * CompressedData;
68  uint8_t * DecompressedData;
69 } PersistFile;
70 
71 typedef struct RefPackParameters_s
72 {
73  uint8_t hnsv;
74  uint32_t DecompressedSize;
76 
77 typedef struct FAREntry_s
78 {
79  uint32_t DecompressedSize;
80  uint32_t CompressedSize;
81  uint8_t DataType;
82  uint32_t DataOffset;
83  uint16_t HasFilename;
84  uint32_t FilenameLength;
85  uint32_t TypeID;
86  uint32_t GroupID;
87  uint32_t FileID;
88  char * Filename;
90  uint8_t * CompressedData;
91  uint8_t * DecompressedData;
92 } FAREntry;
93 
94 typedef struct FAREntryNode_s
95 {
99 } FAREntryNode;
100 
101 typedef struct FARFile_s
102 {
103  int32_t Type;
104 
105  /* Header */
106  uint32_t MajorVersion;
107  uint32_t MinorVersion;
108  uint32_t Revision;
109  uint32_t IndexOffset;
110  /* DBPF */
111  uint32_t DateCreated;
112  uint32_t DateModified;
114  uint32_t IndexSize;
115  uint32_t TrashCount;
116  uint32_t TrashOffset;
117  uint32_t TrashSize;
119 
120  /* Regular index */
121  uint32_t Files;
124  struct { /* DIR index for DBPF */
127  } Dir;
128  struct { /* Trash index for DBPF */
131  } Trash;
132 } FARFile;
133 
134 /****
135 ** Exported functions
136 */
137 
138 #ifdef __cplusplus
139 extern "C" {
140 #endif
141 
142 void libfar_set_option(int Option, int Value);
143 int libfar_get_option(int Option);
144 
145 int far_identify(const uint8_t * Buffer, unsigned FileSize);
146 
147 FARFile * far_create_archive(int Type);
149 
150 FAREntryNode * far_add_entry(FARFile * FARFileInfo, int Position);
151 
152 int far_read_header(FARFile * FARFileInfo, const uint8_t * Buffer, unsigned FileSize);
153 int far_read_entry(const FARFile * FARFileInfo, FAREntry * FAREntryInfo,
154  const uint8_t * Buffer, unsigned MaxEntrySize, unsigned ArchiveSize);
155 int far_read_persist_header(PersistFile * PersistData, const uint8_t * Buffer, unsigned FileSize);
156 int far_read_entry_data(const FARFile * FARFileInfo, FAREntry * FAREntryInfo, uint8_t * Buffer);
157 int far_read_persist_data(PersistFile * PersistData, uint8_t * CompressedData);
158 
159 int far_enumerate_entries(FARFile * FARFileInfo, const uint8_t * Index, unsigned IndexSize, unsigned ArchiveSize);
160 int far_enumerate_entry_data(const FARFile * FARFileInfo, uint8_t * Buffer);
161 
165 
166 void far_delete_entry(FARFile * FARFileInfo, int Position);
167 void far_delete_archive(FARFile * FARFileInfo);
168 void far_delete_persist(FARFile * FARFileInfo);
169 void libfar_free(void * ptr);
170 
171 int RefPackDecompress(const uint8_t * CompressedData, size_t CompressedSize,
172  uint8_t * DecompressedData, size_t DecompressedSize, unsigned HNSV);
173 
174 #ifdef __cplusplus
175 }
176 #endif