| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * mpq-endian.c -- byte-order serialization helpers for MPQ data. | ||
| 3 | * | ||
| 4 | * Copyright (c) 2003-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-endian.h" | ||
| 21 | |||
| 22 | #include <stddef.h> | ||
| 23 | |||
| 24 | /* Load a 16-bit little-endian value without alignment or host-byte-order assumptions. */ | ||
| 25 | uint16_t | ||
| 26 | 562031 | libmpq__load_le16(const uint8_t *buffer) | |
| 27 | { | ||
| 28 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 562030 times.
|
562031 | if (buffer == NULL) |
| 29 | 1 | return 0; | |
| 30 | 562030 | return (uint16_t)buffer[0] | ((uint16_t)buffer[1] << 8); | |
| 31 | } | ||
| 32 | |||
| 33 | /* Load a 32-bit little-endian value from four serialized bytes. */ | ||
| 34 | uint32_t | ||
| 35 | 7437672 | libmpq__load_le32(const uint8_t *buffer) | |
| 36 | { | ||
| 37 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7437671 times.
|
7437672 | if (buffer == NULL) |
| 38 | 1 | return 0; | |
| 39 | 7437671 | return (uint32_t)buffer[0] | ((uint32_t)buffer[1] << 8) | ((uint32_t)buffer[2] << 16) | | |
| 40 | 7437671 | ((uint32_t)buffer[3] << 24); | |
| 41 | } | ||
| 42 | |||
| 43 | /* Load a 64-bit value from two serialized little-endian 32-bit halves. */ | ||
| 44 | uint64_t | ||
| 45 | 6505 | libmpq__load_le64(const uint8_t *buffer) | |
| 46 | { | ||
| 47 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6504 times.
|
6505 | if (buffer == NULL) |
| 48 | 1 | return 0; | |
| 49 | 6504 | return (uint64_t)libmpq__load_le32(buffer) | ((uint64_t)libmpq__load_le32(buffer + 4) << 32); | |
| 50 | } | ||
| 51 | |||
| 52 | /* Store a 16-bit value as two little-endian bytes. */ | ||
| 53 | void | ||
| 54 | 559254 | libmpq__store_le16(uint8_t *buffer, uint16_t value) | |
| 55 | { | ||
| 56 | 559254 | buffer[0] = (uint8_t)value; | |
| 57 | 559254 | buffer[1] = (uint8_t)(value >> 8); | |
| 58 | 559254 | } | |
| 59 | |||
| 60 | /* Store a 32-bit value as four little-endian bytes. */ | ||
| 61 | void | ||
| 62 | 3646206 | libmpq__store_le32(uint8_t *buffer, uint32_t value) | |
| 63 | { | ||
| 64 | 3646206 | buffer[0] = (uint8_t)value; | |
| 65 | 3646206 | buffer[1] = (uint8_t)(value >> 8); | |
| 66 | 3646206 | buffer[2] = (uint8_t)(value >> 16); | |
| 67 | 3646206 | buffer[3] = (uint8_t)(value >> 24); | |
| 68 | 3646206 | } | |
| 69 | |||
| 70 | /* Store a 64-bit value as two little-endian 32-bit halves. */ | ||
| 71 | void | ||
| 72 | 10117 | libmpq__store_le64(uint8_t *buffer, uint64_t value) | |
| 73 | { | ||
| 74 | 10117 | libmpq__store_le32(buffer, (uint32_t)value); | |
| 75 | 10117 | libmpq__store_le32(buffer + 4, (uint32_t)(value >> 32)); | |
| 76 | 10117 | } | |
| 77 | |||
| 78 | /* Load a 32-bit big-endian value from four serialized bytes. */ | ||
| 79 | uint32_t | ||
| 80 | 173704 | libmpq__load_be32(const uint8_t *buffer) | |
| 81 | { | ||
| 82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 173704 times.
|
173704 | if (buffer == NULL) |
| 83 | ✗ | return 0; | |
| 84 | 173704 | return ((uint32_t)buffer[0] << 24) | ((uint32_t)buffer[1] << 16) | ((uint32_t)buffer[2] << 8) | | |
| 85 | 173704 | (uint32_t)buffer[3]; | |
| 86 | } | ||
| 87 | |||
| 88 | /* Store a 32-bit value as four big-endian bytes. */ | ||
| 89 | void | ||
| 90 | 8350 | libmpq__store_be32(uint8_t *buffer, uint32_t value) | |
| 91 | { | ||
| 92 | 8350 | buffer[0] = (uint8_t)(value >> 24); | |
| 93 | 8350 | buffer[1] = (uint8_t)(value >> 16); | |
| 94 | 8350 | buffer[2] = (uint8_t)(value >> 8); | |
| 95 | 8350 | buffer[3] = (uint8_t)value; | |
| 96 | 8350 | } | |
| 97 |