-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhqr.c
248 lines (213 loc) · 7.04 KB
/
hqr.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
/*
Copyright (C) 2002-2004 The TwinE team
Copyright (C) 2012 The LBADTA team
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
#include "hqr.h"
#include "ioconv.h"
/**
* internal routine to navigate through headers and offset tables in a HQR file
* It returns the valid data structure for the entry
* @param error pointer MUST be valid.
* @param file pointer MUST be valid.
* @param index
* @param dta[out] dta pointer MUST be valid.
* @return 0: okay, else: error
*/
int __fillItemDta(struct ErrorMemory *error, FILE *file, int index, struct HQREntry *dta)
{
uint32_t dta_headerSize;
if(fread(&dta_headerSize, 4, 1, file) != 1)
{
return returnError(error, 1, "cannot read header size");
}
int headerSize = fromLE_UDW_to_int(dta_headerSize);
if(index >= headerSize/4 || index < 0 )
{
return returnError(error, 1, "index %d out of bounds %d", index, headerSize/4);
}
if(fseek(file, index*4, SEEK_SET) != 0)
{
return returnError(error, 1, "cannot reach header entry. File could be truncated");
}
uint32_t offToData;
if(fread(&offToData, 4, 1, file) != 1)
{
return returnError(error, 1, "cannot read item offset. File could be truncated");
}
dta->offset = fromLE_UDW_to_int(offToData);
if(fseek(file, offToData, SEEK_SET) != 0)
{
return returnError(error, 1, "cannot reach item. File could be truncated");
}
uint32_t dataSize;
if(fread(&dataSize, 4, 1, file) != 1)
{
return returnError(error, 1, "cannot read item size. File could be truncated");
}
dta->uncompressed_size = fromLE_UDW_to_int(dataSize);
uint32_t compressedSize;
if(fread(&compressedSize, 4, 1, file) != 1)
{
return returnError(error, 1, "cannot read compressed item size. File could be truncated");
}
dta->compressed_size = fromLE_UDW_to_int(compressedSize);
const int DATA_COMPRESSED = 1;
uint16_t mode;
if(fread(&mode, 2, 1, file) != 1)
{
return returnError(error, 1, "cannot read item mode. File could be truncated");
}
dta->is_compressed = (mode== DATA_COMPRESSED ? 1 : 0);
return 0;
}
int hqr_uncompressData( struct ErrorMemory* error,
int uncompressed_size, unsigned char* uncompressed,
int compressed_size, unsigned char* compressed)
{
char loop;
unsigned char indic;
unsigned char *readback;
int size;
uint16_t temp;
int i;
do
{
loop = 8;
indic = *(compressed++); ///Read in one byte
do
{
if (!(indic & 1)) /// if (first bit is not set)
{
temp = (uint16_t)((*(compressed+1)<<8)+(*compressed));//*((short*)compressed);
compressed += 2;
size = temp & 0x0F; ///transfer lowest 8 bit to size
size += 2;
uncompressed_size -= size;
readback = (uncompressed - (temp >> 4)) - 1; ///Now write back from output to output
for (i = 0; i < size; i++)
{
*(uncompressed++) = *(readback++);
}
if (uncompressed_size <= 0)
return returnError(error, 1, "decompression ran out of output buffer");
loop--;
}
else
{
*(uncompressed++) = *(compressed++);
loop--;
uncompressed_size--;
if (uncompressed_size <= 0)
return returnError(error, 1, "decompression ran out of output buffer");
}
indic >>= 1;
}while (loop);
}while (uncompressed_size); ///work through the whole buffer!
return 0;
}
int hqr_readItem( struct ErrorMemory* error, FILE *file, short int index,
unsigned char *compressed, int compr_bufsize,
unsigned char *uncompressed, int uncompr_bufsize)
{
//Argument Checks
checkErrorMemory(error);
if(file == 0)
{
return returnError(error, 1, "file pointer is NULL");
}
//Remember stream position
long streampos = ftell(file);
struct HQREntry dta;
int rgw = __fillItemDta(error, file, index, &dta);
if(rgw != 0)
{
//Reset stream position
fseek(file, streampos, SEEK_SET); //if fseek fails, we don't report it, because the primary error is more important
return rgw;
}
if(uncompr_bufsize < dta.uncompressed_size)
{
//Reset stream position
fseek(file, streampos, SEEK_SET); //if fseek fails, we don't report it, because the primary error is more important
return returnError(error, 1, "require %d bytes to unpack data, but buffer has only %d bytes", dta.uncompressed_size, uncompr_bufsize);
}
if(dta.is_compressed)
{
if(compr_bufsize < dta.compressed_size)
{
//Reset stream position
fseek(file, streampos, SEEK_SET); //if fseek fails, we don't report it, because the primary error is more important
return returnError(error, 1, "buffer for compressed data requires size >= %d but only provides %d", dta.compressed_size, compr_bufsize);
}
if(fread(compressed, 1, (size_t)dta.compressed_size, file) != dta.compressed_size)
{
//Reset stream position
fseek(file, streampos, SEEK_SET); //if fseek fails, we don't report it, because the primary error is more important
return returnError(error, 1, "could not read %d bytes of compressed data. File could be truncated", dta.compressed_size);
}
int rgw = hqr_uncompressData(error, dta.uncompressed_size, uncompressed, dta.compressed_size, compressed);
if(rgw != 0)
{
//Reset stream position
fseek(file, streampos, SEEK_SET); //if fseek fails, we don't report it, because the primary error is more important
return rgw;
}
}
else //uncompressed
{
if(fread(uncompressed, 1, (size_t)dta.uncompressed_size, file) != dta.uncompressed_size)
{
//Reset stream position
fseek(file, streampos, SEEK_SET); //if fseek fails, we don't report it, because the primary error is more important
return returnError(error, 1, "could not read %d bytes of uncompressed data. File could be truncated", dta.uncompressed_size);
}
}
//Reset stream position
if(fseek(file, streampos, SEEK_SET) != 0)
{
return returnError(error, 1, "could not reset stream position");
}
return(0);
}
int hqr_getItemInformation(struct ErrorMemory* error, FILE *file, int index, struct HQREntry *dta)
{
//Argument Checks
checkErrorMemory(error);
if(dta == 0)
{
return returnError(error, 1, "dta pointer is NULL");
}
if(file == 0)
{
return returnError(error, 1, "file pointer is NULL");
}
//Remember stream position
long streampos = ftell(file);
int rgw = __fillItemDta(error, file, index, dta);
if(rgw != 0)
{
//Reset stream position
fseek(file, streampos, SEEK_SET); //if fseek fails, we don't report it, because the primary error is more important
return rgw;
}
//Reset stream position
if(fseek(file, streampos, SEEK_SET) != 0)
{
return returnError(error, 1, "could not reset stream position");
}
return (0);
}