| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * mpq-endian.c -- little-endian 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 | ||
| 8 | * by the Free Software Foundation; either version 2.1 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "mpq-endian.h" | ||
| 13 | |||
| 14 | #include <stddef.h> | ||
| 15 | |||
| 16 | /* Load a 16-bit little-endian value without alignment or host-byte-order assumptions. */ | ||
| 17 | uint16_t | ||
| 18 | 15586 | libmpq__load_le16(const uint8_t *buffer) | |
| 19 | { | ||
| 20 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15585 times.
|
15586 | if (buffer == NULL) |
| 21 | 1 | return 0; | |
| 22 | 15585 | return (uint16_t)buffer[0] | ((uint16_t)buffer[1] << 8); | |
| 23 | } | ||
| 24 | |||
| 25 | /* Load a 32-bit little-endian value from four serialized bytes. */ | ||
| 26 | uint32_t | ||
| 27 | 684402 | libmpq__load_le32(const uint8_t *buffer) | |
| 28 | { | ||
| 29 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 684401 times.
|
684402 | if (buffer == NULL) |
| 30 | 1 | return 0; | |
| 31 | 684401 | return (uint32_t)buffer[0] | ((uint32_t)buffer[1] << 8) | ((uint32_t)buffer[2] << 16) | | |
| 32 | 684401 | ((uint32_t)buffer[3] << 24); | |
| 33 | } | ||
| 34 | |||
| 35 | /* Load a 64-bit value from two serialized little-endian 32-bit halves. */ | ||
| 36 | uint64_t | ||
| 37 | 27 | libmpq__load_le64(const uint8_t *buffer) | |
| 38 | { | ||
| 39 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26 times.
|
27 | if (buffer == NULL) |
| 40 | 1 | return 0; | |
| 41 | 26 | return (uint64_t)libmpq__load_le32(buffer) | ((uint64_t)libmpq__load_le32(buffer + 4) << 32); | |
| 42 | } | ||
| 43 | |||
| 44 | /* Store a 16-bit value as two little-endian bytes. */ | ||
| 45 | void | ||
| 46 | 18793 | libmpq__store_le16(uint8_t *buffer, uint16_t value) | |
| 47 | { | ||
| 48 | 18793 | buffer[0] = (uint8_t)value; | |
| 49 | 18793 | buffer[1] = (uint8_t)(value >> 8); | |
| 50 | 18793 | } | |
| 51 | |||
| 52 | /* Store a 32-bit value as four little-endian bytes. */ | ||
| 53 | void | ||
| 54 | 681910 | libmpq__store_le32(uint8_t *buffer, uint32_t value) | |
| 55 | { | ||
| 56 | 681910 | buffer[0] = (uint8_t)value; | |
| 57 | 681910 | buffer[1] = (uint8_t)(value >> 8); | |
| 58 | 681910 | buffer[2] = (uint8_t)(value >> 16); | |
| 59 | 681910 | buffer[3] = (uint8_t)(value >> 24); | |
| 60 | 681910 | } | |
| 61 | |||
| 62 | /* Store a 64-bit value as two little-endian 32-bit halves. */ | ||
| 63 | void | ||
| 64 | 27 | libmpq__store_le64(uint8_t *buffer, uint64_t value) | |
| 65 | { | ||
| 66 | 27 | libmpq__store_le32(buffer, (uint32_t)value); | |
| 67 | 27 | libmpq__store_le32(buffer + 4, (uint32_t)(value >> 32)); | |
| 68 | 27 | } | |
| 69 |