1335640Shselasky/* 2335640Shselasky * Copyright (c) 1992, 1993, 1994, 1995, 1996 3335640Shselasky * The Regents of the University of California. All rights reserved. 4335640Shselasky * 5335640Shselasky * Redistribution and use in source and binary forms, with or without 6335640Shselasky * modification, are permitted provided that: (1) source code distributions 7335640Shselasky * retain the above copyright notice and this paragraph in its entirety, (2) 8335640Shselasky * distributions including binary code include the above copyright notice and 9335640Shselasky * this paragraph in its entirety in the documentation or other materials 10335640Shselasky * provided with the distribution, and (3) all advertising materials mentioning 11335640Shselasky * features or use of this software display the following acknowledgement: 12335640Shselasky * ``This product includes software developed by the University of California, 13335640Shselasky * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14335640Shselasky * the University nor the names of its contributors may be used to endorse 15335640Shselasky * or promote products derived from this software without specific prior 16335640Shselasky * written permission. 17335640Shselasky * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18335640Shselasky * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19335640Shselasky * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20335640Shselasky */ 21335640Shselasky 22335640Shselasky#ifndef _WIN32 23335640Shselasky#include <arpa/inet.h> 24335640Shselasky#endif 25335640Shselasky 26335640Shselasky#include <pcap/pcap-inttypes.h> 27335640Shselasky#include <pcap/compiler-tests.h> 28335640Shselasky 29335640Shselasky/* 30335640Shselasky * Macros to extract possibly-unaligned big-endian integral values. 31335640Shselasky */ 32335640Shselasky#ifdef LBL_ALIGN 33335640Shselasky/* 34335640Shselasky * The processor doesn't natively handle unaligned loads. 35335640Shselasky */ 36335640Shselasky#if PCAP_IS_AT_LEAST_GNUC_VERSION(2,0) && \ 37335640Shselasky (defined(__alpha) || defined(__alpha__) || \ 38335640Shselasky defined(__mips) || defined(__mips__)) 39335640Shselasky/* 40335640Shselasky * This is MIPS or Alpha, which don't natively handle unaligned loads, 41335640Shselasky * but which have instructions that can help when doing unaligned 42335640Shselasky * loads, and this is GCC 2.0 or later or a compiler that claims to 43335640Shselasky * be GCC 2.0 or later, which we assume that mean we have 44335640Shselasky * __attribute__((packed)), which we can use to convince the compiler 45335640Shselasky * to generate those instructions. 46335640Shselasky * 47335640Shselasky * Declare packed structures containing a uint16_t and a uint32_t, 48335640Shselasky * cast the pointer to point to one of those, and fetch through it; 49335640Shselasky * the GCC manual doesn't appear to explicitly say that 50335640Shselasky * __attribute__((packed)) causes the compiler to generate unaligned-safe 51335640Shselasky * code, but it apppears to do so. 52335640Shselasky * 53335640Shselasky * We do this in case the compiler can generate code using those 54335640Shselasky * instructions to do an unaligned load and pass stuff to "ntohs()" or 55335640Shselasky * "ntohl()", which might be better than than the code to fetch the 56335640Shselasky * bytes one at a time and assemble them. (That might not be the 57335640Shselasky * case on a little-endian platform, such as DEC's MIPS machines and 58335640Shselasky * Alpha machines, where "ntohs()" and "ntohl()" might not be done 59335640Shselasky * inline.) 60335640Shselasky * 61335640Shselasky * We do this only for specific architectures because, for example, 62335640Shselasky * at least some versions of GCC, when compiling for 64-bit SPARC, 63335640Shselasky * generate code that assumes alignment if we do this. 64335640Shselasky * 65335640Shselasky * XXX - add other architectures and compilers as possible and 66335640Shselasky * appropriate. 67335640Shselasky * 68335640Shselasky * HP's C compiler, indicated by __HP_cc being defined, supports 69335640Shselasky * "#pragma unaligned N" in version A.05.50 and later, where "N" 70335640Shselasky * specifies a number of bytes at which the typedef on the next 71335640Shselasky * line is aligned, e.g. 72335640Shselasky * 73335640Shselasky * #pragma unalign 1 74335640Shselasky * typedef uint16_t unaligned_uint16_t; 75335640Shselasky * 76335640Shselasky * to define unaligned_uint16_t as a 16-bit unaligned data type. 77335640Shselasky * This could be presumably used, in sufficiently recent versions of 78335640Shselasky * the compiler, with macros similar to those below. This would be 79335640Shselasky * useful only if that compiler could generate better code for PA-RISC 80335640Shselasky * or Itanium than would be generated by a bunch of shifts-and-ORs. 81335640Shselasky * 82335640Shselasky * DEC C, indicated by __DECC being defined, has, at least on Alpha, 83335640Shselasky * an __unaligned qualifier that can be applied to pointers to get the 84335640Shselasky * compiler to generate code that does unaligned loads and stores when 85335640Shselasky * dereferencing the pointer in question. 86335640Shselasky * 87335640Shselasky * XXX - what if the native C compiler doesn't support 88335640Shselasky * __attribute__((packed))? How can we get it to generate unaligned 89335640Shselasky * accesses for *specific* items? 90335640Shselasky */ 91335640Shselaskytypedef struct { 92335640Shselasky uint16_t val; 93335640Shselasky} __attribute__((packed)) unaligned_uint16_t; 94335640Shselasky 95335640Shselaskytypedef struct { 96335640Shselasky uint32_t val; 97335640Shselasky} __attribute__((packed)) unaligned_uint32_t; 98335640Shselasky 99335640Shselaskystatic inline uint16_t 100335640ShselaskyEXTRACT_16BITS(const void *p) 101335640Shselasky{ 102335640Shselasky return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val)); 103335640Shselasky} 104335640Shselasky 105335640Shselaskystatic inline uint32_t 106335640ShselaskyEXTRACT_32BITS(const void *p) 107335640Shselasky{ 108335640Shselasky return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val)); 109335640Shselasky} 110335640Shselasky 111335640Shselaskystatic inline uint64_t 112335640ShselaskyEXTRACT_64BITS(const void *p) 113335640Shselasky{ 114335640Shselasky return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 | \ 115335640Shselasky ((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0)); 116335640Shselasky} 117335640Shselasky 118335640Shselasky#else /* have to do it a byte at a time */ 119335640Shselasky/* 120335640Shselasky * This isn't a GCC-compatible compiler, we don't have __attribute__, 121335640Shselasky * or we do but we don't know of any better way with this instruction 122335640Shselasky * set to do unaligned loads, so do unaligned loads of big-endian 123335640Shselasky * quantities the hard way - fetch the bytes one at a time and 124335640Shselasky * assemble them. 125335640Shselasky */ 126335640Shselasky#define EXTRACT_16BITS(p) \ 127335640Shselasky ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \ 128335640Shselasky ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0))) 129335640Shselasky#define EXTRACT_32BITS(p) \ 130335640Shselasky ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \ 131335640Shselasky ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \ 132335640Shselasky ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \ 133335640Shselasky ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0))) 134335640Shselasky#define EXTRACT_64BITS(p) \ 135335640Shselasky ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \ 136335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \ 137335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \ 138335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \ 139335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \ 140335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \ 141335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \ 142335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0))) 143335640Shselasky#endif /* must special-case unaligned accesses */ 144335640Shselasky#else /* LBL_ALIGN */ 145335640Shselasky/* 146335640Shselasky * The processor natively handles unaligned loads, so we can just 147335640Shselasky * cast the pointer and fetch through it. 148335640Shselasky */ 149335640Shselaskystatic inline uint16_t 150335640ShselaskyEXTRACT_16BITS(const void *p) 151335640Shselasky{ 152335640Shselasky return ((uint16_t)ntohs(*(const uint16_t *)(p))); 153335640Shselasky} 154335640Shselasky 155335640Shselaskystatic inline uint32_t 156335640ShselaskyEXTRACT_32BITS(const void *p) 157335640Shselasky{ 158335640Shselasky return ((uint32_t)ntohl(*(const uint32_t *)(p))); 159335640Shselasky} 160335640Shselasky 161335640Shselaskystatic inline uint64_t 162335640ShselaskyEXTRACT_64BITS(const void *p) 163335640Shselasky{ 164335640Shselasky return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 | \ 165335640Shselasky ((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0)); 166335640Shselasky 167335640Shselasky} 168335640Shselasky 169335640Shselasky#endif /* LBL_ALIGN */ 170335640Shselasky 171335640Shselasky#define EXTRACT_24BITS(p) \ 172335640Shselasky ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \ 173335640Shselasky ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 174335640Shselasky ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0))) 175335640Shselasky 176335640Shselasky#define EXTRACT_40BITS(p) \ 177335640Shselasky ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \ 178335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \ 179335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 180335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \ 181335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0))) 182335640Shselasky 183335640Shselasky#define EXTRACT_48BITS(p) \ 184335640Shselasky ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \ 185335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \ 186335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \ 187335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \ 188335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \ 189335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0))) 190335640Shselasky 191335640Shselasky#define EXTRACT_56BITS(p) \ 192335640Shselasky ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \ 193335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \ 194335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \ 195335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \ 196335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \ 197335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \ 198335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0))) 199335640Shselasky 200335640Shselasky/* 201335640Shselasky * Macros to extract possibly-unaligned little-endian integral values. 202335640Shselasky * XXX - do loads on little-endian machines that support unaligned loads? 203335640Shselasky */ 204335640Shselasky#define EXTRACT_LE_8BITS(p) (*(p)) 205335640Shselasky#define EXTRACT_LE_16BITS(p) \ 206335640Shselasky ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 207335640Shselasky ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0))) 208335640Shselasky#define EXTRACT_LE_32BITS(p) \ 209335640Shselasky ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \ 210335640Shselasky ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 211335640Shselasky ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 212335640Shselasky ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0))) 213335640Shselasky#define EXTRACT_LE_24BITS(p) \ 214335640Shselasky ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 215335640Shselasky ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 216335640Shselasky ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0))) 217335640Shselasky#define EXTRACT_LE_64BITS(p) \ 218335640Shselasky ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \ 219335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \ 220335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \ 221335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \ 222335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \ 223335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 224335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 225335640Shselasky ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0))) 226