1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * Copyright (c) 2024 rilysh <nightquick@proton.me> 4 */ 5 6 #include <unistd.h> 7 #include <sys/endian.h> 8 9 void 10 swab(const void * __restrict from, void * __restrict to, ssize_t len) 11 { 12 const uint16_t *f __aligned(1) = from; 13 uint16_t *t __aligned(1) = to; 14 15 /* 16 * POSIX says overlapping copy behavior is undefined, however many 17 * applications assume the old FreeBSD and current GNU libc behavior 18 * that will swap the bytes correctly when from == to. Reading both bytes 19 * and swapping them before writing them back accomplishes this. 20 */ 21 while (len > 1) { 22 *t++ = bswap16(*f++); 23 len -= 2; 24 } 25 } 26