clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -O3 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name mar_read.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/modules/libmar/src -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/modules/libmar/src -resource-dir /usr/lib/llvm-23/lib/clang/23 -D XP_UNIX -D DEBUG=1 -I /root/firefox-clang/modules/libmar/src -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/modules/libmar/src -I /root/firefox-clang/other-licenses/nsis/Contrib/CityHash/cityhash -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -fdwarf2-cfi-asm -o /tmp/scan-build-2026-09-01-224014-2642839-1 -x c /root/firefox-clang/modules/libmar/src/mar_read.c
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | #include <sys/types.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include "city.h" |
| 10 | #include "mar_private.h" |
| 11 | #include "mar.h" |
| 12 | #ifdef XP_WIN |
| 13 | # define strdup _strdup |
| 14 | #endif |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | #define MAXADDITIONALBLOCKSIZE 96 |
| 22 | |
| 23 | static uint32_t mar_hash_name(const char* name) { |
| 24 | return CityHash64(name, strlen(name)) % TABLESIZE; |
| 25 | } |
| 26 | |
| 27 | static int mar_insert_item(MarFile* mar, const char* name, uint32_t namelen, |
| 28 | uint32_t offset, uint32_t length, uint32_t flags) { |
| 29 | MarItem *item, *root; |
| 30 | uint32_t hash; |
| 31 | |
| 32 | item = (MarItem*)malloc(sizeof(MarItem) + namelen); |
| 33 | if (!item) { |
| 34 | return -1; |
| 35 | } |
| 36 | item->next = NULL; |
| 37 | item->offset = offset; |
| 38 | item->length = length; |
| 39 | item->flags = flags; |
| 40 | memcpy(item->name, name, namelen + 1); |
| 41 | |
| 42 | hash = mar_hash_name(name); |
| 43 | |
| 44 | root = mar->item_table[hash]; |
| 45 | if (!root) { |
| 46 | mar->item_table[hash] = item; |
| 47 | } else { |
| 48 | |
| 49 | while (root->next) root = root->next; |
| 50 | root->next = item; |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int mar_consume_index(MarFile* mar, char** buf, const char* buf_end) { |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | uint32_t offset; |
| 65 | uint32_t length; |
| 66 | uint32_t flags; |
| 67 | const char* name; |
| 68 | int namelen; |
| 69 | |
| 70 | if ((buf_end - *buf) < (int)(3 * sizeof(uint32_t) + 2)) { |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | memcpy(&offset, *buf, sizeof(offset)); |
| 75 | *buf += sizeof(offset); |
| 76 | |
| 77 | memcpy(&length, *buf, sizeof(length)); |
| 78 | *buf += sizeof(length); |
| 79 | |
| 80 | memcpy(&flags, *buf, sizeof(flags)); |
| 81 | *buf += sizeof(flags); |
| 82 | |
| 83 | offset = ntohl(offset); |
| 84 | length = ntohl(length); |
| 85 | flags = ntohl(flags); |
| 86 | |
| 87 | name = *buf; |
| 88 | |
| 89 | while (**buf) { |
| 90 | |
| 91 | if (*buf == (buf_end - 1)) { |
| 92 | return -1; |
| 93 | } |
| 94 | ++(*buf); |
| 95 | } |
| 96 | namelen = (*buf - name); |
| 97 | |
| 98 | if (namelen < 0) { |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | if (*buf == buf_end) { |
| 103 | return -1; |
| 104 | } |
| 105 | ++(*buf); |
| 106 | |
| 107 | return mar_insert_item(mar, name, namelen, offset, length, flags); |
| 108 | } |
| 109 | |
| 110 | static int mar_read_index(MarFile* mar) { |
| 111 | char id[MAR_ID_SIZE], *buf, *bufptr, *bufend; |
| 112 | uint32_t offset_to_index, size_of_index; |
| 113 | size_t mar_position = 0; |
| 114 | |
| 115 | |
| 116 | if (mar_read_buffer(mar, id, &mar_position, MAR_ID_SIZE) != 0) { |
| 117 | return -1; |
| 118 | } |
| 119 | if (memcmp(id, MAR_ID, MAR_ID_SIZE) != 0) { |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | if (mar_read_buffer(mar, &offset_to_index, &mar_position, sizeof(uint32_t)) != |
| 124 | 0) { |
| 125 | return -1; |
| 126 | } |
| 127 | offset_to_index = ntohl(offset_to_index); |
| 128 | |
| 129 | mar_position = 0; |
| 130 | if (mar_buffer_seek(mar, &mar_position, offset_to_index) != 0) { |
| 131 | return -1; |
| 132 | } |
| 133 | if (mar_read_buffer(mar, &size_of_index, &mar_position, sizeof(uint32_t)) != |
| 134 | 0) { |
| 135 | return -1; |
| 136 | } |
| 137 | size_of_index = ntohl(size_of_index); |
| 138 | |
| 139 | if (size_of_index > mar->data_len) { |
| 140 | return -1; |
| 141 | } |
| 142 | buf = (char*)malloc(size_of_index); |
| 143 | if (!buf) { |
| 144 | return -1; |
| 145 | } |
| 146 | if (mar_read_buffer(mar, buf, &mar_position, size_of_index) != 0) { |
| 147 | free(buf); |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | bufptr = buf; |
| 152 | bufend = buf + size_of_index; |
| 153 | while (bufptr < bufend && mar_consume_index(mar, &bufptr, bufend) == 0); |
| 154 | |
| 155 | free(buf); |
| 156 | return (bufptr == bufend) ? 0 : -1; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | static int mar_insert_offset(MarFile* mar, uint32_t offset, uint32_t length) { |
| 168 | |
| 169 | if (length == 0) { |
| 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | SeenIndex* index = (SeenIndex*)malloc(sizeof(SeenIndex)); |
| 174 | if (!index) { |
| 175 | return -1; |
| 176 | } |
| 177 | index->next = NULL; |
| 178 | index->offset = offset; |
| 179 | index->length = length; |
| 180 | uint32_t index_end = index->offset + index->length - 1; |
| 181 | |
| 182 | |
| 183 | if (mar->index_list == NULL) { |
| 184 | mar->index_list = index; |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | SeenIndex* previous; |
| 190 | SeenIndex* current = mar->index_list; |
| 191 | while (current != NULL) { |
| 192 | uint32_t current_end = current->offset + current->length - 1; |
| 193 | |
| 194 | |
| 195 | |
| 196 | if ((index->offset >= current->offset && index->offset <= current_end) || |
| 197 | (index_end >= current->offset && index_end <= current_end) || |
| 198 | (current->offset >= index->offset && current->offset <= index_end) || |
| 199 | (current_end >= index->offset && current_end <= index_end)) { |
| 200 | free(index); |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | previous = current; |
| 206 | current = current->next; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | previous->next = index; |
| 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | static MarReadResult mar_fpopen(FILE* fp, MarFile** out_mar) { |
| 220 | *out_mar = NULL; |
| 221 | MarFile* mar; |
| 222 | |
| 223 | mar = (MarFile*)malloc(sizeof(*mar)); |
| 224 | if (!mar) { |
| 4 | | Assuming 'mar' is non-null | |
|
| |
| 225 | return MAR_MEM_ERROR; |
| 226 | } |
| 227 | |
| 228 | off_t buffer_size = -1; |
| 229 | if (fseeko(fp, 0, SEEK_END) == 0) { |
| |
| 230 | buffer_size = ftello(fp); |
| 231 | } |
| 232 | rewind(fp); |
| 7 | | After calling 'rewind' reading 'errno' is required to find out if the call has failed | |
|
| 233 | if (buffer_size < 0) { |
| |
| 234 | fprintf(stderr, "Warning: MAR size could not be determined\n"); |
| 9 | | Value of 'errno' was not checked and may be overwritten by function 'fprintf' |
|
| 235 | buffer_size = MAX_SIZE_OF_MAR_FILE; |
| 236 | } |
| 237 | if (buffer_size > MAX_SIZE_OF_MAR_FILE) { |
| 238 | fprintf(stderr, "ERROR: MAR exceeds maximum size (%lli)\n", |
| 239 | (long long int)buffer_size); |
| 240 | free(mar); |
| 241 | return MAR_FILE_TOO_BIG_ERROR; |
| 242 | } |
| 243 | |
| 244 | mar->buffer = malloc(buffer_size); |
| 245 | if (!mar->buffer) { |
| 246 | fprintf(stderr, "ERROR: MAR buffer could not be allocated\n"); |
| 247 | free(mar); |
| 248 | return MAR_MEM_ERROR; |
| 249 | } |
| 250 | mar->data_len = fread(mar->buffer, 1, buffer_size, fp); |
| 251 | if (fgetc(fp) != EOF) { |
| 252 | fprintf(stderr, "ERROR: File is larger than buffer (%lli)\n", |
| 253 | (long long int)buffer_size); |
| 254 | free(mar->buffer); |
| 255 | free(mar); |
| 256 | return MAR_IO_ERROR; |
| 257 | } |
| 258 | if (ferror(fp)) { |
| 259 | fprintf(stderr, "ERROR: Failed to read MAR\n"); |
| 260 | free(mar->buffer); |
| 261 | free(mar); |
| 262 | return MAR_IO_ERROR; |
| 263 | } |
| 264 | |
| 265 | mar->item_table_is_valid = 0; |
| 266 | memset(mar->item_table, 0, sizeof(mar->item_table)); |
| 267 | mar->index_list = NULL; |
| 268 | |
| 269 | *out_mar = mar; |
| 270 | return MAR_READ_SUCCESS; |
| 271 | } |
| 272 | |
| 273 | MarReadResult mar_open(const char* path, MarFile** out_mar) { |
| 274 | *out_mar = NULL; |
| 275 | |
| 276 | FILE* fp; |
| 277 | |
| 278 | fp = fopen(path, "rb"); |
| 279 | if (!fp) { |
| |
| 280 | fprintf(stderr, "ERROR: could not open file in mar_open()\n"); |
| 281 | perror(path); |
| 282 | return MAR_IO_ERROR; |
| 283 | } |
| 284 | |
| 285 | MarReadResult result = mar_fpopen(fp, out_mar); |
| |
| 286 | fclose(fp); |
| 287 | return result; |
| 288 | } |
| 289 | |
| 290 | #ifdef XP_WIN |
| 291 | MarReadResult mar_wopen(const wchar_t* path, MarFile** out_mar) { |
| 292 | *out_mar = NULL; |
| 293 | |
| 294 | FILE* fp; |
| 295 | |
| 296 | _wfopen_s(&fp, path, L"rb"); |
| 297 | if (!fp) { |
| 298 | fprintf(stderr, "ERROR: could not open file in mar_wopen()\n"); |
| 299 | _wperror(path); |
| 300 | return MAR_IO_ERROR; |
| 301 | } |
| 302 | |
| 303 | MarReadResult result = mar_fpopen(fp, out_mar); |
| 304 | fclose(fp); |
| 305 | return result; |
| 306 | } |
| 307 | #endif |
| 308 | |
| 309 | void mar_close(MarFile* mar) { |
| 310 | MarItem* item; |
| 311 | SeenIndex* index; |
| 312 | int i; |
| 313 | |
| 314 | free(mar->buffer); |
| 315 | |
| 316 | for (i = 0; i < TABLESIZE; ++i) { |
| 317 | item = mar->item_table[i]; |
| 318 | while (item) { |
| 319 | MarItem* temp = item; |
| 320 | item = item->next; |
| 321 | free(temp); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | while (mar->index_list != NULL) { |
| 326 | index = mar->index_list; |
| 327 | mar->index_list = index->next; |
| 328 | free(index); |
| 329 | } |
| 330 | |
| 331 | free(mar); |
| 332 | } |
| 333 | |
| 334 | int mar_read_buffer(MarFile* mar, void* dest, size_t* position, size_t size) { |
| 335 | |
| 336 | |
| 337 | |
| 338 | if (size > mar->data_len) { |
| 339 | return -1; |
| 340 | } |
| 341 | if (*position > mar->data_len - size) { |
| 342 | return -1; |
| 343 | } |
| 344 | memcpy(dest, mar->buffer + *position, size); |
| 345 | *position += size; |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | int mar_read_buffer_max(MarFile* mar, void* dest, size_t* position, |
| 350 | size_t size) { |
| 351 | |
| 352 | |
| 353 | |
| 354 | if (mar->data_len <= *position) { |
| 355 | return 0; |
| 356 | } |
| 357 | size_t read_count = mar->data_len - *position; |
| 358 | if (read_count > size) { |
| 359 | read_count = size; |
| 360 | } |
| 361 | memcpy(dest, mar->buffer + *position, read_count); |
| 362 | *position += read_count; |
| 363 | return read_count; |
| 364 | } |
| 365 | |
| 366 | int mar_buffer_seek(MarFile* mar, size_t* position, size_t distance) { |
| 367 | |
| 368 | |
| 369 | |
| 370 | if (distance > mar->data_len) { |
| 371 | return -1; |
| 372 | } |
| 373 | if (*position > mar->data_len - distance) { |
| 374 | return -1; |
| 375 | } |
| 376 | *position += distance; |
| 377 | return 0; |
| 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 | int get_open_mar_file_info(MarFile* mar, size_t* mar_position, |
| 404 | int* hasSignatureBlock, uint32_t* numSignatures, |
| 405 | int* hasAdditionalBlocks, |
| 406 | uint32_t* offsetAdditionalBlocks, |
| 407 | uint32_t* numAdditionalBlocks) { |
| 408 | uint32_t offsetToIndex, offsetToContent, signatureCount, signatureLen, i; |
| 409 | |
| 410 | |
| 411 | if (!hasSignatureBlock && !hasAdditionalBlocks) { |
| 412 | return -1; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | *mar_position = 0; |
| 417 | if (mar_buffer_seek(mar, mar_position, MAR_ID_SIZE) != 0) { |
| 418 | return -1; |
| 419 | } |
| 420 | |
| 421 | |
| 422 | if (mar_read_buffer(mar, &offsetToIndex, mar_position, |
| 423 | sizeof(offsetToIndex)) != 0) { |
| 424 | return -1; |
| 425 | } |
| 426 | offsetToIndex = ntohl(offsetToIndex); |
| 427 | |
| 428 | if (numSignatures) { |
| 429 | |
| 430 | if (mar_buffer_seek(mar, mar_position, sizeof(uint64_t)) != 0) { |
| 431 | return -1; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | if (mar_read_buffer(mar, numSignatures, mar_position, |
| 436 | sizeof(*numSignatures)) != 0) { |
| 437 | return -1; |
| 438 | } |
| 439 | *numSignatures = ntohl(*numSignatures); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | *mar_position = 0; |
| 446 | if (mar_buffer_seek(mar, mar_position, offsetToIndex) != 0) { |
| 447 | return -1; |
| 448 | } |
| 449 | |
| 450 | if (mar_buffer_seek(mar, mar_position, sizeof(uint32_t)) != 0) { |
| 451 | return -1; |
| 452 | } |
| 453 | |
| 454 | |
| 455 | if (mar_read_buffer(mar, &offsetToContent, mar_position, |
| 456 | sizeof(offsetToContent)) != 0) { |
| 457 | return -1; |
| 458 | } |
| 459 | offsetToContent = ntohl(offsetToContent); |
| 460 | |
| 461 | |
| 462 | if (hasSignatureBlock) { |
| 463 | if (offsetToContent == MAR_ID_SIZE + sizeof(uint32_t)) { |
| 464 | *hasSignatureBlock = 0; |
| 465 | } else { |
| 466 | *hasSignatureBlock = 1; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | |
| 471 | |
| 472 | if (!hasAdditionalBlocks) { |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | |
| 477 | *mar_position = 0; |
| 478 | if (mar_buffer_seek(mar, mar_position, SIGNATURE_BLOCK_OFFSET) != 0) { |
| 479 | return -1; |
| 480 | } |
| 481 | |
| 482 | |
| 483 | if (mar_read_buffer(mar, &signatureCount, mar_position, |
| 484 | sizeof(signatureCount)) != 0) { |
| 485 | return -1; |
| 486 | } |
| 487 | signatureCount = ntohl(signatureCount); |
| 488 | |
| 489 | |
| 490 | |
| 491 | if (signatureCount > MAX_SIGNATURES) { |
| 492 | return -1; |
| 493 | } |
| 494 | |
| 495 | |
| 496 | for (i = 0; i < signatureCount; i++) { |
| 497 | |
| 498 | if (mar_buffer_seek(mar, mar_position, sizeof(uint32_t)) != 0) { |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | |
| 503 | if (mar_read_buffer(mar, &signatureLen, mar_position, sizeof(uint32_t)) != |
| 504 | 0) { |
| 505 | return -1; |
| 506 | } |
| 507 | signatureLen = ntohl(signatureLen); |
| 508 | if (mar_buffer_seek(mar, mar_position, signatureLen) != 0) { |
| 509 | return -1; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if (*mar_position <= (size_t)INT64_MAX && |
| 514 | (int64_t)mar_position == (int64_t)offsetToContent) { |
| 515 | *hasAdditionalBlocks = 0; |
| 516 | } else { |
| 517 | if (numAdditionalBlocks) { |
| 518 | |
| 519 | |
| 520 | *hasAdditionalBlocks = 1; |
| 521 | if (mar_read_buffer(mar, numAdditionalBlocks, mar_position, |
| 522 | sizeof(uint32_t)) != 0) { |
| 523 | return -1; |
| 524 | } |
| 525 | *numAdditionalBlocks = ntohl(*numAdditionalBlocks); |
| 526 | if (offsetAdditionalBlocks) { |
| 527 | if (*mar_position > (size_t)UINT32_MAX) { |
| 528 | return -1; |
| 529 | } |
| 530 | *offsetAdditionalBlocks = (uint32_t)*mar_position; |
| 531 | } |
| 532 | } else if (offsetAdditionalBlocks) { |
| 533 | |
| 534 | |
| 535 | if (mar_buffer_seek(mar, mar_position, sizeof(uint32_t)) != 0) { |
| 536 | return -1; |
| 537 | } |
| 538 | if (*mar_position > (size_t)UINT32_MAX) { |
| 539 | return -1; |
| 540 | } |
| 541 | *offsetAdditionalBlocks = (uint32_t)*mar_position; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | int read_product_info_block(char* path, |
| 557 | struct ProductInformationBlock* infoBlock) { |
| 558 | int rv; |
| 559 | MarFile* mar; |
| 560 | MarReadResult result = mar_open(path, &mar); |
| 561 | if (result != MAR_READ_SUCCESS) { |
| 562 | fprintf(stderr, |
| 563 | "ERROR: could not open file in read_product_info_block()\n"); |
| 564 | return -1; |
| 565 | } |
| 566 | rv = mar_read_product_info_block(mar, infoBlock); |
| 567 | mar_close(mar); |
| 568 | return rv; |
| 569 | } |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | int mar_read_product_info_block(MarFile* mar, |
| 580 | struct ProductInformationBlock* infoBlock) { |
| 581 | uint32_t offsetAdditionalBlocks, numAdditionalBlocks, additionalBlockSize, |
| 582 | additionalBlockID; |
| 583 | int hasAdditionalBlocks; |
| 584 | size_t mar_position = 0; |
| 585 | |
| 586 | |
| 587 | |
| 588 | char buf[MAXADDITIONALBLOCKSIZE + 1] = {'\0'}; |
| 589 | if (get_open_mar_file_info(mar, &mar_position, NULL, NULL, |
| 590 | &hasAdditionalBlocks, &offsetAdditionalBlocks, |
| 591 | &numAdditionalBlocks) != 0) { |
| 592 | return -1; |
| 593 | } |
| 594 | |
| 595 | |
| 596 | |
| 597 | if (numAdditionalBlocks > 0) { |
| 598 | |
| 599 | if (mar_read_buffer(mar, &additionalBlockSize, &mar_position, |
| 600 | sizeof(additionalBlockSize)) != 0) { |
| 601 | return -1; |
| 602 | } |
| 603 | additionalBlockSize = ntohl(additionalBlockSize) - |
| 604 | sizeof(additionalBlockSize) - |
| 605 | sizeof(additionalBlockID); |
| 606 | |
| 607 | |
| 608 | if (additionalBlockSize > MAXADDITIONALBLOCKSIZE) { |
| 609 | return -1; |
| 610 | } |
| 611 | |
| 612 | |
| 613 | if (mar_read_buffer(mar, &additionalBlockID, &mar_position, |
| 614 | sizeof(additionalBlockID)) != 0) { |
| 615 | return -1; |
| 616 | } |
| 617 | additionalBlockID = ntohl(additionalBlockID); |
| 618 | |
| 619 | if (PRODUCT_INFO_BLOCK_ID == additionalBlockID) { |
| 620 | const char* location; |
| 621 | int len; |
| 622 | |
| 623 | if (mar_read_buffer(mar, buf, &mar_position, additionalBlockSize) != 0) { |
| 624 | return -1; |
| 625 | } |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | location = buf; |
| 631 | len = strlen(location); |
| 632 | infoBlock->MARChannelID = location; |
| 633 | location += len + 1; |
| 634 | if (len >= 64) { |
| 635 | infoBlock->MARChannelID = NULL; |
| 636 | return -1; |
| 637 | } |
| 638 | |
| 639 | |
| 640 | len = strlen(location); |
| 641 | infoBlock->productVersion = location; |
| 642 | if (len >= 32) { |
| 643 | infoBlock->MARChannelID = NULL; |
| 644 | infoBlock->productVersion = NULL; |
| 645 | return -1; |
| 646 | } |
| 647 | infoBlock->MARChannelID = strdup(infoBlock->MARChannelID); |
| 648 | infoBlock->productVersion = strdup(infoBlock->productVersion); |
| 649 | return 0; |
| 650 | } else { |
| 651 | |
| 652 | if (mar_buffer_seek(mar, &mar_position, additionalBlockSize) != 0) { |
| 653 | return -1; |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | |
| 659 | return -1; |
| 660 | } |
| 661 | |
| 662 | const MarItem* mar_find_item(MarFile* mar, const char* name) { |
| 663 | uint32_t hash; |
| 664 | const MarItem* item; |
| 665 | |
| 666 | if (!mar->item_table_is_valid) { |
| 667 | if (mar_read_index(mar)) { |
| 668 | return NULL; |
| 669 | } else { |
| 670 | mar->item_table_is_valid = 1; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | hash = mar_hash_name(name); |
| 675 | |
| 676 | item = mar->item_table[hash]; |
| 677 | while (item && strcmp(item->name, name) != 0) { |
| 678 | item = item->next; |
| 679 | } |
| 680 | |
| 681 | |
| 682 | if (mar_insert_offset(mar, item->offset, item->length) == 1) { |
| 683 | return item; |
| 684 | } else { |
| 685 | fprintf(stderr, "ERROR: file content collision in mar_find_item()\n"); |
| 686 | return NULL; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | int mar_enum_items(MarFile* mar, MarItemCallback callback, void* closure) { |
| 691 | MarItem* item; |
| 692 | int i, rv; |
| 693 | |
| 694 | if (!mar->item_table_is_valid) { |
| 695 | if (mar_read_index(mar)) { |
| 696 | return -1; |
| 697 | } else { |
| 698 | mar->item_table_is_valid = 1; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | for (i = 0; i < TABLESIZE; ++i) { |
| 703 | item = mar->item_table[i]; |
| 704 | while (item) { |
| 705 | |
| 706 | if (mar_insert_offset(mar, item->offset, item->length) == 1) { |
| 707 | rv = callback(mar, item, closure); |
| 708 | if (rv) { |
| 709 | return rv; |
| 710 | } |
| 711 | } else { |
| 712 | fprintf(stderr, "ERROR: file content collision in mar_enum_items()\n"); |
| 713 | return 1; |
| 714 | } |
| 715 | item = item->next; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | int mar_read(MarFile* mar, const MarItem* item, int offset, uint8_t* buf, |
| 723 | int bufsize) { |
| 724 | int nr; |
| 725 | size_t mar_position = 0; |
| 726 | |
| 727 | if (offset == (int)item->length) { |
| 728 | return 0; |
| 729 | } |
| 730 | if (offset > (int)item->length) { |
| 731 | return -1; |
| 732 | } |
| 733 | |
| 734 | nr = item->length - offset; |
| 735 | if (nr > bufsize) { |
| 736 | nr = bufsize; |
| 737 | } |
| 738 | |
| 739 | |
| 740 | if (mar_buffer_seek(mar, &mar_position, item->offset)) { |
| 741 | return -1; |
| 742 | } |
| 743 | if (mar_buffer_seek(mar, &mar_position, offset)) { |
| 744 | return -1; |
| 745 | } |
| 746 | |
| 747 | return mar_read_buffer_max(mar, buf, &mar_position, nr); |
| 748 | } |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | int get_mar_file_info(const char* path, int* hasSignatureBlock, |
| 769 | uint32_t* numSignatures, int* hasAdditionalBlocks, |
| 770 | uint32_t* offsetAdditionalBlocks, |
| 771 | uint32_t* numAdditionalBlocks) { |
| 772 | int rv; |
| 773 | MarFile* mar; |
| 774 | size_t mar_position = 0; |
| 775 | MarReadResult result = mar_open(path, &mar); |
| |
| 776 | if (result != MAR_READ_SUCCESS) { |
| 777 | fprintf(stderr, "ERROR: could not read file in get_mar_file_info()\n"); |
| 778 | return -1; |
| 779 | } |
| 780 | |
| 781 | rv = get_open_mar_file_info(mar, &mar_position, hasSignatureBlock, |
| 782 | numSignatures, hasAdditionalBlocks, |
| 783 | offsetAdditionalBlocks, numAdditionalBlocks); |
| 784 | |
| 785 | mar_close(mar); |
| 786 | return rv; |
| 787 | } |