1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/modules/libmar/src/mar_read.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,570 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include <sys/types.h> 1.11 +#include <fcntl.h> 1.12 +#include <stdlib.h> 1.13 +#include <string.h> 1.14 +#include "mar_private.h" 1.15 +#include "mar.h" 1.16 + 1.17 +#ifdef XP_WIN 1.18 +#include <winsock2.h> 1.19 +#else 1.20 +#include <netinet/in.h> 1.21 +#endif 1.22 + 1.23 + 1.24 +/* this is the same hash algorithm used by nsZipArchive.cpp */ 1.25 +static uint32_t mar_hash_name(const char *name) { 1.26 + uint32_t val = 0; 1.27 + unsigned char* c; 1.28 + 1.29 + for (c = (unsigned char *) name; *c; ++c) 1.30 + val = val*37 + *c; 1.31 + 1.32 + return val % TABLESIZE; 1.33 +} 1.34 + 1.35 +static int mar_insert_item(MarFile *mar, const char *name, int namelen, 1.36 + uint32_t offset, uint32_t length, uint32_t flags) { 1.37 + MarItem *item, *root; 1.38 + uint32_t hash; 1.39 + 1.40 + item = (MarItem *) malloc(sizeof(MarItem) + namelen); 1.41 + if (!item) 1.42 + return -1; 1.43 + item->next = NULL; 1.44 + item->offset = offset; 1.45 + item->length = length; 1.46 + item->flags = flags; 1.47 + memcpy(item->name, name, namelen + 1); 1.48 + 1.49 + hash = mar_hash_name(name); 1.50 + 1.51 + root = mar->item_table[hash]; 1.52 + if (!root) { 1.53 + mar->item_table[hash] = item; 1.54 + } else { 1.55 + /* append item */ 1.56 + while (root->next) 1.57 + root = root->next; 1.58 + root->next = item; 1.59 + } 1.60 + return 0; 1.61 +} 1.62 + 1.63 +static int mar_consume_index(MarFile *mar, char **buf, const char *buf_end) { 1.64 + /* 1.65 + * Each item has the following structure: 1.66 + * uint32_t offset (network byte order) 1.67 + * uint32_t length (network byte order) 1.68 + * uint32_t flags (network byte order) 1.69 + * char name[N] (where N >= 1) 1.70 + * char null_byte; 1.71 + */ 1.72 + uint32_t offset; 1.73 + uint32_t length; 1.74 + uint32_t flags; 1.75 + const char *name; 1.76 + int namelen; 1.77 + 1.78 + if ((buf_end - *buf) < (int)(3*sizeof(uint32_t) + 2)) 1.79 + return -1; 1.80 + 1.81 + memcpy(&offset, *buf, sizeof(offset)); 1.82 + *buf += sizeof(offset); 1.83 + 1.84 + memcpy(&length, *buf, sizeof(length)); 1.85 + *buf += sizeof(length); 1.86 + 1.87 + memcpy(&flags, *buf, sizeof(flags)); 1.88 + *buf += sizeof(flags); 1.89 + 1.90 + offset = ntohl(offset); 1.91 + length = ntohl(length); 1.92 + flags = ntohl(flags); 1.93 + 1.94 + name = *buf; 1.95 + /* find namelen; must take care not to read beyond buf_end */ 1.96 + while (**buf) { 1.97 + if (*buf == buf_end) 1.98 + return -1; 1.99 + ++(*buf); 1.100 + } 1.101 + namelen = (*buf - name); 1.102 + /* consume null byte */ 1.103 + if (*buf == buf_end) 1.104 + return -1; 1.105 + ++(*buf); 1.106 + 1.107 + return mar_insert_item(mar, name, namelen, offset, length, flags); 1.108 +} 1.109 + 1.110 +static int mar_read_index(MarFile *mar) { 1.111 + char id[MAR_ID_SIZE], *buf, *bufptr, *bufend; 1.112 + uint32_t offset_to_index, size_of_index; 1.113 + 1.114 + /* verify MAR ID */ 1.115 + if (fread(id, MAR_ID_SIZE, 1, mar->fp) != 1) 1.116 + return -1; 1.117 + if (memcmp(id, MAR_ID, MAR_ID_SIZE) != 0) 1.118 + return -1; 1.119 + 1.120 + if (fread(&offset_to_index, sizeof(uint32_t), 1, mar->fp) != 1) 1.121 + return -1; 1.122 + offset_to_index = ntohl(offset_to_index); 1.123 + 1.124 + if (fseek(mar->fp, offset_to_index, SEEK_SET)) 1.125 + return -1; 1.126 + if (fread(&size_of_index, sizeof(uint32_t), 1, mar->fp) != 1) 1.127 + return -1; 1.128 + size_of_index = ntohl(size_of_index); 1.129 + 1.130 + buf = (char *) malloc(size_of_index); 1.131 + if (!buf) 1.132 + return -1; 1.133 + if (fread(buf, size_of_index, 1, mar->fp) != 1) { 1.134 + free(buf); 1.135 + return -1; 1.136 + } 1.137 + 1.138 + bufptr = buf; 1.139 + bufend = buf + size_of_index; 1.140 + while (bufptr < bufend && mar_consume_index(mar, &bufptr, bufend) == 0); 1.141 + 1.142 + free(buf); 1.143 + return (bufptr == bufend) ? 0 : -1; 1.144 +} 1.145 + 1.146 +/** 1.147 + * Internal shared code for mar_open and mar_wopen. 1.148 + * On failure, will fclose(fp). 1.149 + */ 1.150 +static MarFile *mar_fpopen(FILE *fp) 1.151 +{ 1.152 + MarFile *mar; 1.153 + 1.154 + mar = (MarFile *) malloc(sizeof(*mar)); 1.155 + if (!mar) { 1.156 + fclose(fp); 1.157 + return NULL; 1.158 + } 1.159 + 1.160 + mar->fp = fp; 1.161 + memset(mar->item_table, 0, sizeof(mar->item_table)); 1.162 + if (mar_read_index(mar)) { 1.163 + mar_close(mar); 1.164 + return NULL; 1.165 + } 1.166 + 1.167 + return mar; 1.168 +} 1.169 + 1.170 +MarFile *mar_open(const char *path) { 1.171 + FILE *fp; 1.172 + 1.173 + fp = fopen(path, "rb"); 1.174 + if (!fp) { 1.175 + fprintf(stderr, "ERROR: could not open file in mar_open()\n"); 1.176 + perror(path); 1.177 + return NULL; 1.178 + } 1.179 + 1.180 + return mar_fpopen(fp); 1.181 +} 1.182 + 1.183 +#ifdef XP_WIN 1.184 +MarFile *mar_wopen(const wchar_t *path) { 1.185 + FILE *fp; 1.186 + 1.187 + _wfopen_s(&fp, path, L"rb"); 1.188 + if (!fp) { 1.189 + fprintf(stderr, "ERROR: could not open file in mar_wopen()\n"); 1.190 + _wperror(path); 1.191 + return NULL; 1.192 + } 1.193 + 1.194 + return mar_fpopen(fp); 1.195 +} 1.196 +#endif 1.197 + 1.198 +void mar_close(MarFile *mar) { 1.199 + MarItem *item; 1.200 + int i; 1.201 + 1.202 + fclose(mar->fp); 1.203 + 1.204 + for (i = 0; i < TABLESIZE; ++i) { 1.205 + item = mar->item_table[i]; 1.206 + while (item) { 1.207 + MarItem *temp = item; 1.208 + item = item->next; 1.209 + free(temp); 1.210 + } 1.211 + } 1.212 + 1.213 + free(mar); 1.214 +} 1.215 + 1.216 +/** 1.217 + * Determines the MAR file information. 1.218 + * 1.219 + * @param fp An opened MAR file in read mode. 1.220 + * @param hasSignatureBlock Optional out parameter specifying if the MAR 1.221 + * file has a signature block or not. 1.222 + * @param numSignatures Optional out parameter for storing the number 1.223 + * of signatures in the MAR file. 1.224 + * @param hasAdditionalBlocks Optional out parameter specifying if the MAR 1.225 + * file has additional blocks or not. 1.226 + * @param offsetAdditionalBlocks Optional out parameter for the offset to the 1.227 + * first additional block. Value is only valid if 1.228 + * hasAdditionalBlocks is not equal to 0. 1.229 + * @param numAdditionalBlocks Optional out parameter for the number of 1.230 + * additional blocks. Value is only valid if 1.231 + * hasAdditionalBlocks is not equal to 0. 1.232 + * @return 0 on success and non-zero on failure. 1.233 + */ 1.234 +int get_mar_file_info_fp(FILE *fp, 1.235 + int *hasSignatureBlock, 1.236 + uint32_t *numSignatures, 1.237 + int *hasAdditionalBlocks, 1.238 + uint32_t *offsetAdditionalBlocks, 1.239 + uint32_t *numAdditionalBlocks) 1.240 +{ 1.241 + uint32_t offsetToIndex, offsetToContent, signatureCount, signatureLen, i; 1.242 + 1.243 + /* One of hasSignatureBlock or hasAdditionalBlocks must be non NULL */ 1.244 + if (!hasSignatureBlock && !hasAdditionalBlocks) { 1.245 + return -1; 1.246 + } 1.247 + 1.248 + 1.249 + /* Skip to the start of the offset index */ 1.250 + if (fseek(fp, MAR_ID_SIZE, SEEK_SET)) { 1.251 + return -1; 1.252 + } 1.253 + 1.254 + /* Read the offset to the index. */ 1.255 + if (fread(&offsetToIndex, sizeof(offsetToIndex), 1, fp) != 1) { 1.256 + return -1; 1.257 + } 1.258 + offsetToIndex = ntohl(offsetToIndex); 1.259 + 1.260 + if (numSignatures) { 1.261 + /* Skip past the MAR file size field */ 1.262 + if (fseek(fp, sizeof(uint64_t), SEEK_CUR)) { 1.263 + return -1; 1.264 + } 1.265 + 1.266 + /* Read the number of signatures field */ 1.267 + if (fread(numSignatures, sizeof(*numSignatures), 1, fp) != 1) { 1.268 + return -1; 1.269 + } 1.270 + *numSignatures = ntohl(*numSignatures); 1.271 + } 1.272 + 1.273 + /* Skip to the first index entry past the index size field 1.274 + We do it in 2 calls because offsetToIndex + sizeof(uint32_t) 1.275 + could oerflow in theory. */ 1.276 + if (fseek(fp, offsetToIndex, SEEK_SET)) { 1.277 + return -1; 1.278 + } 1.279 + 1.280 + if (fseek(fp, sizeof(uint32_t), SEEK_CUR)) { 1.281 + return -1; 1.282 + } 1.283 + 1.284 + /* Read the first offset to content field. */ 1.285 + if (fread(&offsetToContent, sizeof(offsetToContent), 1, fp) != 1) { 1.286 + return -1; 1.287 + } 1.288 + offsetToContent = ntohl(offsetToContent); 1.289 + 1.290 + /* Check if we have a new or old MAR file */ 1.291 + if (hasSignatureBlock) { 1.292 + if (offsetToContent == MAR_ID_SIZE + sizeof(uint32_t)) { 1.293 + *hasSignatureBlock = 0; 1.294 + } else { 1.295 + *hasSignatureBlock = 1; 1.296 + } 1.297 + } 1.298 + 1.299 + /* If the caller doesn't care about the product info block 1.300 + value, then just return */ 1.301 + if (!hasAdditionalBlocks) { 1.302 + return 0; 1.303 + } 1.304 + 1.305 + /* Skip to the start of the signature block */ 1.306 + if (fseeko(fp, SIGNATURE_BLOCK_OFFSET, SEEK_SET)) { 1.307 + return -1; 1.308 + } 1.309 + 1.310 + /* Get the number of signatures */ 1.311 + if (fread(&signatureCount, sizeof(signatureCount), 1, fp) != 1) { 1.312 + return -1; 1.313 + } 1.314 + signatureCount = ntohl(signatureCount); 1.315 + 1.316 + /* Check that we have less than the max amount of signatures so we don't 1.317 + waste too much of either updater's or signmar's time. */ 1.318 + if (signatureCount > MAX_SIGNATURES) { 1.319 + return -1; 1.320 + } 1.321 + 1.322 + /* Skip past the whole signature block */ 1.323 + for (i = 0; i < signatureCount; i++) { 1.324 + /* Skip past the signature algorithm ID */ 1.325 + if (fseek(fp, sizeof(uint32_t), SEEK_CUR)) { 1.326 + return -1; 1.327 + } 1.328 + 1.329 + /* Read the signature length and skip past the signature */ 1.330 + if (fread(&signatureLen, sizeof(uint32_t), 1, fp) != 1) { 1.331 + return -1; 1.332 + } 1.333 + signatureLen = ntohl(signatureLen); 1.334 + if (fseek(fp, signatureLen, SEEK_CUR)) { 1.335 + return -1; 1.336 + } 1.337 + } 1.338 + 1.339 + if (ftell(fp) == offsetToContent) { 1.340 + *hasAdditionalBlocks = 0; 1.341 + } else { 1.342 + if (numAdditionalBlocks) { 1.343 + /* We have an additional block, so read in the number of additional blocks 1.344 + and set the offset. */ 1.345 + *hasAdditionalBlocks = 1; 1.346 + if (fread(numAdditionalBlocks, sizeof(uint32_t), 1, fp) != 1) { 1.347 + return -1; 1.348 + } 1.349 + *numAdditionalBlocks = ntohl(*numAdditionalBlocks); 1.350 + if (offsetAdditionalBlocks) { 1.351 + *offsetAdditionalBlocks = ftell(fp); 1.352 + } 1.353 + } else if (offsetAdditionalBlocks) { 1.354 + /* numAdditionalBlocks is not specified but offsetAdditionalBlocks 1.355 + is, so fill it! */ 1.356 + *offsetAdditionalBlocks = ftell(fp) + sizeof(uint32_t); 1.357 + } 1.358 + } 1.359 + 1.360 + return 0; 1.361 +} 1.362 + 1.363 +/** 1.364 + * Reads the product info block from the MAR file's additional block section. 1.365 + * The caller is responsible for freeing the fields in infoBlock 1.366 + * if the return is successful. 1.367 + * 1.368 + * @param infoBlock Out parameter for where to store the result to 1.369 + * @return 0 on success, -1 on failure 1.370 +*/ 1.371 +int 1.372 +read_product_info_block(char *path, 1.373 + struct ProductInformationBlock *infoBlock) 1.374 +{ 1.375 + int rv; 1.376 + MarFile mar; 1.377 + mar.fp = fopen(path, "rb"); 1.378 + if (!mar.fp) { 1.379 + fprintf(stderr, "ERROR: could not open file in read_product_info_block()\n"); 1.380 + perror(path); 1.381 + return -1; 1.382 + } 1.383 + rv = mar_read_product_info_block(&mar, infoBlock); 1.384 + fclose(mar.fp); 1.385 + return rv; 1.386 +} 1.387 + 1.388 +/** 1.389 + * Reads the product info block from the MAR file's additional block section. 1.390 + * The caller is responsible for freeing the fields in infoBlock 1.391 + * if the return is successful. 1.392 + * 1.393 + * @param infoBlock Out parameter for where to store the result to 1.394 + * @return 0 on success, -1 on failure 1.395 +*/ 1.396 +int 1.397 +mar_read_product_info_block(MarFile *mar, 1.398 + struct ProductInformationBlock *infoBlock) 1.399 +{ 1.400 + uint32_t i, offsetAdditionalBlocks, numAdditionalBlocks, 1.401 + additionalBlockSize, additionalBlockID; 1.402 + int hasAdditionalBlocks; 1.403 + 1.404 + /* The buffer size is 97 bytes because the MAR channel name < 64 bytes, and 1.405 + product version < 32 bytes + 3 NULL terminator bytes. */ 1.406 + char buf[97] = { '\0' }; 1.407 + int ret = get_mar_file_info_fp(mar->fp, NULL, NULL, 1.408 + &hasAdditionalBlocks, 1.409 + &offsetAdditionalBlocks, 1.410 + &numAdditionalBlocks); 1.411 + for (i = 0; i < numAdditionalBlocks; ++i) { 1.412 + /* Read the additional block size */ 1.413 + if (fread(&additionalBlockSize, 1.414 + sizeof(additionalBlockSize), 1.415 + 1, mar->fp) != 1) { 1.416 + return -1; 1.417 + } 1.418 + additionalBlockSize = ntohl(additionalBlockSize) - 1.419 + sizeof(additionalBlockSize) - 1.420 + sizeof(additionalBlockID); 1.421 + 1.422 + /* Read the additional block ID */ 1.423 + if (fread(&additionalBlockID, 1.424 + sizeof(additionalBlockID), 1.425 + 1, mar->fp) != 1) { 1.426 + return -1; 1.427 + } 1.428 + additionalBlockID = ntohl(additionalBlockID); 1.429 + 1.430 + if (PRODUCT_INFO_BLOCK_ID == additionalBlockID) { 1.431 + const char *location; 1.432 + int len; 1.433 + 1.434 + /* This block must be at most 104 bytes. 1.435 + MAR channel name < 64 bytes, and product version < 32 bytes + 3 NULL 1.436 + terminator bytes. We only check for 96 though because we remove 8 1.437 + bytes above from the additionalBlockSize: We subtract 1.438 + sizeof(additionalBlockSize) and sizeof(additionalBlockID) */ 1.439 + if (additionalBlockSize > 96) { 1.440 + return -1; 1.441 + } 1.442 + 1.443 + if (fread(buf, additionalBlockSize, 1, mar->fp) != 1) { 1.444 + return -1; 1.445 + } 1.446 + 1.447 + /* Extract the MAR channel name from the buffer. For now we 1.448 + point to the stack allocated buffer but we strdup this 1.449 + if we are within bounds of each field's max length. */ 1.450 + location = buf; 1.451 + len = strlen(location); 1.452 + infoBlock->MARChannelID = location; 1.453 + location += len + 1; 1.454 + if (len >= 64) { 1.455 + infoBlock->MARChannelID = NULL; 1.456 + return -1; 1.457 + } 1.458 + 1.459 + /* Extract the version from the buffer */ 1.460 + len = strlen(location); 1.461 + infoBlock->productVersion = location; 1.462 + location += len + 1; 1.463 + if (len >= 32) { 1.464 + infoBlock->MARChannelID = NULL; 1.465 + infoBlock->productVersion = NULL; 1.466 + return -1; 1.467 + } 1.468 + infoBlock->MARChannelID = 1.469 + strdup(infoBlock->MARChannelID); 1.470 + infoBlock->productVersion = 1.471 + strdup(infoBlock->productVersion); 1.472 + return 0; 1.473 + } else { 1.474 + /* This is not the additional block you're looking for. Move along. */ 1.475 + if (fseek(mar->fp, additionalBlockSize, SEEK_CUR)) { 1.476 + return -1; 1.477 + } 1.478 + } 1.479 + } 1.480 + 1.481 + /* If we had a product info block we would have already returned */ 1.482 + return -1; 1.483 +} 1.484 + 1.485 +const MarItem *mar_find_item(MarFile *mar, const char *name) { 1.486 + uint32_t hash; 1.487 + const MarItem *item; 1.488 + 1.489 + hash = mar_hash_name(name); 1.490 + 1.491 + item = mar->item_table[hash]; 1.492 + while (item && strcmp(item->name, name) != 0) 1.493 + item = item->next; 1.494 + 1.495 + return item; 1.496 +} 1.497 + 1.498 +int mar_enum_items(MarFile *mar, MarItemCallback callback, void *closure) { 1.499 + MarItem *item; 1.500 + int i; 1.501 + 1.502 + for (i = 0; i < TABLESIZE; ++i) { 1.503 + item = mar->item_table[i]; 1.504 + while (item) { 1.505 + int rv = callback(mar, item, closure); 1.506 + if (rv) 1.507 + return rv; 1.508 + item = item->next; 1.509 + } 1.510 + } 1.511 + 1.512 + return 0; 1.513 +} 1.514 + 1.515 +int mar_read(MarFile *mar, const MarItem *item, int offset, char *buf, 1.516 + int bufsize) { 1.517 + int nr; 1.518 + 1.519 + if (offset == (int) item->length) 1.520 + return 0; 1.521 + if (offset > (int) item->length) 1.522 + return -1; 1.523 + 1.524 + nr = item->length - offset; 1.525 + if (nr > bufsize) 1.526 + nr = bufsize; 1.527 + 1.528 + if (fseek(mar->fp, item->offset + offset, SEEK_SET)) 1.529 + return -1; 1.530 + 1.531 + return fread(buf, 1, nr, mar->fp); 1.532 +} 1.533 + 1.534 +/** 1.535 + * Determines the MAR file information. 1.536 + * 1.537 + * @param path The path of the MAR file to check. 1.538 + * @param hasSignatureBlock Optional out parameter specifying if the MAR 1.539 + * file has a signature block or not. 1.540 + * @param numSignatures Optional out parameter for storing the number 1.541 + * of signatures in the MAR file. 1.542 + * @param hasAdditionalBlocks Optional out parameter specifying if the MAR 1.543 + * file has additional blocks or not. 1.544 + * @param offsetAdditionalBlocks Optional out parameter for the offset to the 1.545 + * first additional block. Value is only valid if 1.546 + * hasAdditionalBlocks is not equal to 0. 1.547 + * @param numAdditionalBlocks Optional out parameter for the number of 1.548 + * additional blocks. Value is only valid if 1.549 + * has_additional_blocks is not equal to 0. 1.550 + * @return 0 on success and non-zero on failure. 1.551 + */ 1.552 +int get_mar_file_info(const char *path, 1.553 + int *hasSignatureBlock, 1.554 + uint32_t *numSignatures, 1.555 + int *hasAdditionalBlocks, 1.556 + uint32_t *offsetAdditionalBlocks, 1.557 + uint32_t *numAdditionalBlocks) 1.558 +{ 1.559 + int rv; 1.560 + FILE *fp = fopen(path, "rb"); 1.561 + if (!fp) { 1.562 + fprintf(stderr, "ERROR: could not open file in get_mar_file_info()\n"); 1.563 + perror(path); 1.564 + return -1; 1.565 + } 1.566 + 1.567 + rv = get_mar_file_info_fp(fp, hasSignatureBlock, 1.568 + numSignatures, hasAdditionalBlocks, 1.569 + offsetAdditionalBlocks, numAdditionalBlocks); 1.570 + 1.571 + fclose(fp); 1.572 + return rv; 1.573 +}