19e8ec64bSBrian Somers /*- 2*1de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*1de7b4b8SPedro F. Giffuni * 49e8ec64bSBrian Somers * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 59e8ec64bSBrian Somers * All rights reserved. 69e8ec64bSBrian Somers * 79e8ec64bSBrian Somers * Redistribution and use in source and binary forms, with or without 89e8ec64bSBrian Somers * modification, are permitted provided that the following conditions 99e8ec64bSBrian Somers * are met: 109e8ec64bSBrian Somers * 1. Redistributions of source code must retain the above copyright 119e8ec64bSBrian Somers * notice, this list of conditions and the following disclaimer. 129e8ec64bSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 139e8ec64bSBrian Somers * notice, this list of conditions and the following disclaimer in the 149e8ec64bSBrian Somers * documentation and/or other materials provided with the distribution. 159e8ec64bSBrian Somers * 169e8ec64bSBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 179e8ec64bSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 189e8ec64bSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 199e8ec64bSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 209e8ec64bSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 219e8ec64bSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 229e8ec64bSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 239e8ec64bSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 249e8ec64bSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 259e8ec64bSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 269e8ec64bSBrian Somers * SUCH DAMAGE. 279e8ec64bSBrian Somers * 2897d92980SPeter Wemm * $FreeBSD$ 299e8ec64bSBrian Somers */ 309e8ec64bSBrian Somers 319e8ec64bSBrian Somers #ifdef __i386__ /* Do any other archs not care about alignment ? */ 329e8ec64bSBrian Somers 339e8ec64bSBrian Somers # define ua_htonl(src, tgt) (*(u_int32_t *)(tgt) = htonl(*(u_int32_t *)(src))) 349e8ec64bSBrian Somers # define ua_ntohl(src, tgt) (*(u_int32_t *)(tgt) = ntohl(*(u_int32_t *)(src))) 359e8ec64bSBrian Somers # define ua_htons(src, tgt) (*(u_int16_t *)(tgt) = htons(*(u_int16_t *)(src))) 369e8ec64bSBrian Somers # define ua_ntohs(src, tgt) (*(u_int16_t *)(tgt) = ntohs(*(u_int16_t *)(src))) 379e8ec64bSBrian Somers 389e8ec64bSBrian Somers #else /* We care about alignment (or else drop a core !) */ 399e8ec64bSBrian Somers 409e8ec64bSBrian Somers # define ua_htonl(src, tgt) \ 419e8ec64bSBrian Somers do { \ 429e8ec64bSBrian Somers u_int32_t __oh; \ 439e8ec64bSBrian Somers memcpy(&__oh, (src), sizeof __oh); \ 449e8ec64bSBrian Somers *(u_char *)(tgt) = __oh >> 24; \ 459e8ec64bSBrian Somers *((u_char *)(tgt) + 1) = (__oh >> 16) & 0xff; \ 469e8ec64bSBrian Somers *((u_char *)(tgt) + 2) = (__oh >> 8) & 0xff; \ 479e8ec64bSBrian Somers *((u_char *)(tgt) + 3) = __oh & 0xff; \ 489e8ec64bSBrian Somers } while (0) 499e8ec64bSBrian Somers 509e8ec64bSBrian Somers # define ua_ntohl(src, tgt) \ 519e8ec64bSBrian Somers do { \ 529e8ec64bSBrian Somers u_int32_t __nh; \ 539e8ec64bSBrian Somers __nh = ((u_int32_t)*(u_char *)(src) << 24) | \ 549e8ec64bSBrian Somers ((u_int32_t)*((u_char *)(src) + 1) << 16) | \ 559e8ec64bSBrian Somers ((u_int32_t)*((u_char *)(src) + 2) << 8) | \ 569e8ec64bSBrian Somers (u_int32_t)*((u_char *)(src) + 3); \ 579e8ec64bSBrian Somers memcpy((tgt), &__nh, sizeof __nh); \ 589e8ec64bSBrian Somers } while (0) 599e8ec64bSBrian Somers 609e8ec64bSBrian Somers # define ua_htons(src, tgt) \ 619e8ec64bSBrian Somers do { \ 629e8ec64bSBrian Somers u_int16_t __oh; \ 639e8ec64bSBrian Somers memcpy(&__oh, (src), sizeof __oh); \ 649e8ec64bSBrian Somers *(u_char *)(tgt) = __oh >> 8; \ 659e8ec64bSBrian Somers *((u_char *)(tgt) + 1) = __oh & 0xff; \ 669e8ec64bSBrian Somers } while (0) 679e8ec64bSBrian Somers 689e8ec64bSBrian Somers # define ua_ntohs(src, tgt) \ 699e8ec64bSBrian Somers do { \ 7042628155SBrian Somers u_int16_t __nh; \ 7142628155SBrian Somers __nh = ((u_int16_t)*(u_char *)(src) << 8) | \ 7242628155SBrian Somers (u_int16_t)*((u_char *)(src) + 1); \ 739e8ec64bSBrian Somers memcpy((tgt), &__nh, sizeof __nh); \ 749e8ec64bSBrian Somers } while (0) 759e8ec64bSBrian Somers 769e8ec64bSBrian Somers #endif 77