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
|
/*
Copyright (c) 2011, 2012, Simon Howard
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include "ext_header.h"
#include "lha_endian.h"
//
// Extended header parsing.
//
// Extended headers were introduced with LHA v2 - various different
// tools support different extended headers. Some are operating system
// specific.
//
// Extended header types:
#define LHA_EXT_HEADER_COMMON 0x00
#define LHA_EXT_HEADER_FILENAME 0x01
#define LHA_EXT_HEADER_PATH 0x02
#define LHA_EXT_HEADER_MULTI_DISC 0x39
#define LHA_EXT_HEADER_COMMENT 0x3f
#define LHA_EXT_HEADER_WINDOWS_TIMESTAMPS 0x41
#define LHA_EXT_HEADER_FILE_SIZES 0x42
#define LHA_EXT_HEADER_UNIX_PERMISSION 0x50
#define LHA_EXT_HEADER_UNIX_UID_GID 0x51
#define LHA_EXT_HEADER_UNIX_GROUP 0x52
#define LHA_EXT_HEADER_UNIX_USER 0x53
#define LHA_EXT_HEADER_UNIX_TIMESTAMP 0x54
#define LHA_EXT_HEADER_OS9 0xcc
/**
* Structure representing an extended header type.
*/
typedef struct {
/**
* Header number.
*
* Each extended header type has a unique byte value that represents
* it.
*/
uint8_t num;
/**
* Callback function for parsing an extended header block.
*
* @param header The file header structure in which to store
* decoded data.
* @param data Pointer to the header data to decode.
* @param data_len Size of the header data, in bytes.
* @return Non-zero if successful, or zero for failure.
*/
int (*decoder)(LHAFileHeader *header, uint8_t *data, size_t data_len);
/** Minimum length for a header of this type. */
size_t min_len;
} LHAExtHeaderType;
// Common header (0x00).
//
// This contains a 16-bit CRC of the entire LHA header.
static int ext_header_common_decoder(LHAFileHeader *header,
uint8_t *data,
size_t data_len)
{
header->extra_flags |= LHA_FILE_COMMON_CRC;
header->common_crc = lha_decode_uint16(data);
// There is a catch-22 in calculating the CRC, because the field
// containing the CRC is part of the data being CRC'd. The solution
// is that the CRC is calculated with the CRC field set to zero.
// Therefore, now that the CRC has been read, set the field to
// zero in the raw_data array so that the CRC can be calculated
// correctly.
data[0] = 0x00;
data[1] = 0x00;
// TODO: Some platforms (OS/2, Unix) put extra data in the common
// header which might also be decoded.
return 1;
}
static const LHAExtHeaderType lha_ext_header_common = {
LHA_EXT_HEADER_COMMON,
ext_header_common_decoder,
2
};
// Filename header (0x01).
//
// This stores the filename for the file. This is essential on level 2/3
// headers, as the filename field is no longer part of the standard
// header.
static int ext_header_filename_decoder(LHAFileHeader *header,
uint8_t *data,
size_t data_len)
{
char *new_filename;
unsigned int i;
new_filename = malloc(data_len + 1);
if (new_filename == NULL) {
return 0;
}
memcpy(new_filename, data, data_len);
new_filename[data_len] = '\0';
// Sanitize the filename that was read. It is not allowed to
// contain a path separator, which could potentially be used
// to do something malicious.
for (i = 0; new_filename[i] != '\0'; ++i) {
if (new_filename[i] == '/') {
new_filename[i] = '_';
}
}
free(header->filename);
header->filename = new_filename;
return 1;
}
static const LHAExtHeaderType lha_ext_header_filename = {
LHA_EXT_HEADER_FILENAME,
ext_header_filename_decoder,
1
};
// Path header (0x02).
//
// This stores the directory path of the file. A value of 0xff is used
// as the path separator. It is supposed to include a terminating path
// separator as the last character.
static int ext_header_path_decoder(LHAFileHeader *header,
uint8_t *data,
size_t data_len)
{
unsigned int i;
uint8_t *new_path;
new_path = malloc(data_len + 2);
if (new_path == NULL) {
return 0;
}
memcpy(new_path, data, data_len);
new_path[data_len] = '\0';
// Amiga LHA v1.22 generates path headers without a path
// separator at the end of the string. This is broken (and
// was fixed in a later version), but handle it correctly.
if (new_path[data_len - 1] != 0xff) {
new_path[data_len] = 0xff;
new_path[data_len + 1] = '\0';
++data_len;
}
free(header->path);
header->path = (char *) new_path;
for (i = 0; i < data_len; ++i) {
if (new_path[i] == 0xff) {
new_path[i] = '/';
}
}
return 1;
}
static const LHAExtHeaderType lha_ext_header_path = {
LHA_EXT_HEADER_PATH,
ext_header_path_decoder,
1
};
// Windows timestamp header (0x41).
//
// This is a Windows-specific header that stores 64-bit timestamps in
// Windows FILETIME format. The timestamps have 100ns accuracy, which is
// much more accurate than the normal Unix time_t format.
static int ext_header_windows_timestamps(LHAFileHeader *header,
uint8_t *data,
size_t data_len)
{
header->extra_flags |= LHA_FILE_WINDOWS_TIMESTAMPS;
header->win_creation_time = lha_decode_uint64(data);
header->win_modification_time = lha_decode_uint64(data + 8);
header->win_access_time = lha_decode_uint64(data + 16);
return 1;
}
static const LHAExtHeaderType lha_ext_header_windows_timestamps = {
LHA_EXT_HEADER_WINDOWS_TIMESTAMPS,
ext_header_windows_timestamps,
24
};
// File sizes header (0x42).
//
// This contains 64-bit versions of the uncompressed/compressed file size
// header fields, for storing very long files.
static int ext_header_file_size_decoder(LHAFileHeader *header, uint8_t *data,
size_t data_len)
{
header->extra_flags |= LHA_FILE_64BIT_SIZES;
header->compressed_length = lha_decode_uint64(data);
header->length = lha_decode_uint64(data + 8);
// We populate the old ABI size fields, which used size_t. On some
// systems this is a 32-bit integer, so if the new values would
// overflow this, store the maximum value possible instead.
if (header->compressed_length > SIZE_MAX) {
header->_old_compressed_length = SIZE_MAX;
} else {
header->_old_compressed_length =
(size_t) header->compressed_length;
}
if (header->length > SIZE_MAX) {
header->_old_length = SIZE_MAX;
} else {
header->_old_length = (size_t) header->length;
}
return 1;
}
static const LHAExtHeaderType lha_ext_header_file_sizes = {
LHA_EXT_HEADER_FILE_SIZES,
ext_header_file_size_decoder,
16
};
// Unix permissions header (0x50).
static int ext_header_unix_perms_decoder(LHAFileHeader *header,
uint8_t *data,
size_t data_len)
{
header->extra_flags |= LHA_FILE_UNIX_PERMS;
header->unix_perms = lha_decode_uint16(data);
return 1;
}
static const LHAExtHeaderType lha_ext_header_unix_perms = {
LHA_EXT_HEADER_UNIX_PERMISSION,
ext_header_unix_perms_decoder,
2
};
// Unix UID/GID header (0x51).
static int ext_header_unix_uid_gid_decoder(LHAFileHeader *header,
uint8_t *data,
size_t data_len)
{
header->extra_flags |= LHA_FILE_UNIX_UID_GID;
header->unix_gid = lha_decode_uint16(data);
header->unix_uid = lha_decode_uint16(data + 2);
return 1;
}
static const LHAExtHeaderType lha_ext_header_unix_uid_gid = {
LHA_EXT_HEADER_UNIX_UID_GID,
ext_header_unix_uid_gid_decoder,
4
};
// Unix username header (0x53).
//
// This stores a string containing the username. There don't seem to be
// any tools that actually generate archives containing this header.
static int ext_header_unix_username_decoder(LHAFileHeader *header,
uint8_t *data,
size_t data_len)
{
char *username;
username = malloc(data_len + 1);
if (username == NULL) {
return 0;
}
memcpy(username, data, data_len);
username[data_len] = '\0';
free(header->unix_username);
header->unix_username = username;
return 1;
}
static const LHAExtHeaderType lha_ext_header_unix_username = {
LHA_EXT_HEADER_UNIX_USER,
ext_header_unix_username_decoder,
1
};
// Unix group header (0x52).
//
// This stores a string containing the Unix group name. As with the
// username header, there don't seem to be any tools that actually
// generate archives containing this header.
static int ext_header_unix_group_decoder(LHAFileHeader *header,
uint8_t *data,
size_t data_len)
{
char *group;
group = malloc(data_len + 1);
if (group == NULL) {
return 0;
}
memcpy(group, data, data_len);
group[data_len] = '\0';
free(header->unix_group);
header->unix_group = group;
return 1;
}
static const LHAExtHeaderType lha_ext_header_unix_group = {
LHA_EXT_HEADER_UNIX_GROUP,
ext_header_unix_group_decoder,
1
};
// Unix timestamp header (0x54).
//
// This stores a 32-bit Unix time_t timestamp representing the
// modification time of the file.
static int ext_header_unix_timestamp_decoder(LHAFileHeader *header,
uint8_t *data,
size_t data_len)
{
header->timestamp = lha_decode_uint32(data);
return 1;
}
static const LHAExtHeaderType lha_ext_header_unix_timestamp = {
LHA_EXT_HEADER_UNIX_TIMESTAMP,
ext_header_unix_timestamp_decoder,
4
};
// OS-9 (6809) header (0xcc)
//
// This stores OS-9 filesystem metadata.
static int ext_header_os9_decoder(LHAFileHeader *header,
uint8_t *data,
size_t data_len)
{
// TODO: The OS-9 extended header contains various data, but
// it's not clear what it's all for. Just extract the
// permissions for now.
header->os9_perms = lha_decode_uint16(data + 7);
header->extra_flags |= LHA_FILE_OS9_PERMS;
return 1;
}
static const LHAExtHeaderType lha_ext_header_os9 = {
LHA_EXT_HEADER_OS9,
ext_header_os9_decoder,
12
};
// Table of extended headers.
static const LHAExtHeaderType *const ext_header_types[] = {
&lha_ext_header_common,
&lha_ext_header_filename,
&lha_ext_header_path,
&lha_ext_header_unix_perms,
&lha_ext_header_unix_uid_gid,
&lha_ext_header_unix_username,
&lha_ext_header_unix_group,
&lha_ext_header_unix_timestamp,
&lha_ext_header_windows_timestamps,
&lha_ext_header_file_sizes,
&lha_ext_header_os9,
};
#define NUM_HEADER_TYPES (sizeof(ext_header_types) / sizeof(*ext_header_types))
/**
* Look up the extended header parser for the specified header code.
*
* @param num Extended header type.
* @return Matching @ref LHAExtHeaderType structure, or NULL if
* not found for this header type.
*/
static const LHAExtHeaderType *ext_header_for_num(uint8_t num)
{
unsigned int i;
for (i = 0; i < NUM_HEADER_TYPES; ++i) {
if (ext_header_types[i]->num == num) {
return ext_header_types[i];
}
}
return NULL;
}
int lha_ext_header_decode(LHAFileHeader *header,
uint8_t num,
uint8_t *data,
size_t data_len)
{
const LHAExtHeaderType *htype;
htype = ext_header_for_num(num);
if (htype == NULL) {
return 0;
}
if (data_len < htype->min_len) {
return 0;
}
return htype->decoder(header, data, data_len);
}
|