| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * mpq-hash.c -- MPQ hash-table wire-format encoding and decoding. | ||
| 3 | * | ||
| 4 | * Copyright (c) 2026-2026 Maik Broemme <mbroemme@libmpq.org> | ||
| 5 | * | ||
| 6 | * This file is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU Lesser General Public License as published by | ||
| 8 | * the Free Software Foundation; either version 2.1 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This file is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public License | ||
| 17 | * along with this file; if not, see <https://www.gnu.org/licenses/>. | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include "mpq-hash.h" | ||
| 21 | #include "mpq-endian.h" | ||
| 22 | #include <libmpq/mpq.h> | ||
| 23 | |||
| 24 | /* Decode fixed-size entries without interpreting slot or block-index semantics. */ | ||
| 25 | int32_t | ||
| 26 | 1629 | libmpq__hash_table_decode( | |
| 27 | const uint8_t *input, size_t input_size, mpq_hash_s *entries, size_t entry_count | ||
| 28 | ) | ||
| 29 | { | ||
| 30 | size_t i; | ||
| 31 | |||
| 32 |
2/2✓ Branch 0 taken 1628 times.
✓ Branch 1 taken 1 times.
|
1629 | if (entry_count > SIZE_MAX / LIBMPQ_HASH_ENTRY_WIRE_SIZE || |
| 33 |
4/4✓ Branch 0 taken 1627 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1624 times.
✓ Branch 3 taken 3 times.
|
1628 | input_size < entry_count * LIBMPQ_HASH_ENTRY_WIRE_SIZE || |
| 34 |
3/4✓ Branch 0 taken 1623 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1623 times.
|
1624 | (entry_count != 0 && (input == NULL || entries == NULL))) |
| 35 | 3 | return LIBMPQ_ERROR_SIZE; | |
| 36 | |||
| 37 |
2/2✓ Branch 0 taken 233151 times.
✓ Branch 1 taken 1626 times.
|
234777 | for (i = 0; i < entry_count; i++) { |
| 38 | 233151 | const uint8_t *wire = input + i * LIBMPQ_HASH_ENTRY_WIRE_SIZE; | |
| 39 | |||
| 40 | 233151 | entries[i].hash_a = libmpq__load_le32(wire); | |
| 41 | 233151 | entries[i].hash_b = libmpq__load_le32(wire + 4); | |
| 42 | 233151 | entries[i].locale = libmpq__load_le16(wire + 8); | |
| 43 | 233151 | entries[i].platform = libmpq__load_le16(wire + 10); | |
| 44 | 233151 | entries[i].block_table_index = libmpq__load_le32(wire + 12); | |
| 45 | } | ||
| 46 | 1626 | return 0; | |
| 47 | } | ||
| 48 | |||
| 49 | /* Encode native entries into caller-owned wire bytes before table encryption. */ | ||
| 50 | int32_t | ||
| 51 | 745 | libmpq__hash_table_encode( | |
| 52 | const mpq_hash_s *entries, size_t entry_count, uint8_t *output, size_t output_size | ||
| 53 | ) | ||
| 54 | { | ||
| 55 | size_t i; | ||
| 56 | |||
| 57 |
2/2✓ Branch 0 taken 744 times.
✓ Branch 1 taken 1 times.
|
745 | if (entry_count > SIZE_MAX / LIBMPQ_HASH_ENTRY_WIRE_SIZE || |
| 58 |
4/4✓ Branch 0 taken 743 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 742 times.
✓ Branch 3 taken 1 times.
|
744 | output_size < entry_count * LIBMPQ_HASH_ENTRY_WIRE_SIZE || |
| 59 |
3/4✓ Branch 0 taken 742 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 741 times.
|
742 | (entry_count != 0 && (entries == NULL || output == NULL))) |
| 60 | 3 | return LIBMPQ_ERROR_SIZE; | |
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 80098 times.
✓ Branch 1 taken 742 times.
|
80840 | for (i = 0; i < entry_count; i++) { |
| 63 | 80098 | uint8_t *wire = output + i * LIBMPQ_HASH_ENTRY_WIRE_SIZE; | |
| 64 | |||
| 65 | 80098 | libmpq__store_le32(wire, entries[i].hash_a); | |
| 66 | 80098 | libmpq__store_le32(wire + 4, entries[i].hash_b); | |
| 67 | 80098 | libmpq__store_le16(wire + 8, entries[i].locale); | |
| 68 | 80098 | libmpq__store_le16(wire + 10, entries[i].platform); | |
| 69 | 80098 | libmpq__store_le32(wire + 12, entries[i].block_table_index); | |
| 70 | } | ||
| 71 | 742 | return 0; | |
| 72 | } | ||
| 73 |