archive_endian.h revision 228761
1235783Skib/*- 2235783Skib * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org> 3235783Skib * All rights reserved. 4235783Skib * 5235783Skib * Redistribution and use in source and binary forms, with or without 6235783Skib * modification, are permitted provided that the following conditions 7235783Skib * are met: 8235783Skib * 1. Redistributions of source code must retain the above copyright 9235783Skib * notice, this list of conditions and the following disclaimer. 10235783Skib * 2. Redistributions in binary form must reproduce the above copyright 11235783Skib * notice, this list of conditions and the following disclaimer in the 12235783Skib * documentation and/or other materials provided with the distribution. 13235783Skib * 14235783Skib * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15235783Skib * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16235783Skib * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17235783Skib * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18235783Skib * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19235783Skib * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20235783Skib * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21235783Skib * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22235783Skib * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23235783Skib * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24235783Skib * SUCH DAMAGE. 25235783Skib * 26235783Skib * $FreeBSD: head/lib/libarchive/archive_endian.h 201085 2009-12-28 02:17:15Z kientzle $ 27235783Skib * 28235783Skib * Borrowed from FreeBSD's <sys/endian.h> 29235783Skib */ 30235783Skib 31235783Skib#ifndef __LIBARCHIVE_BUILD 32235783Skib#error This header is only to be used internally to libarchive. 33235783Skib#endif 34282199Sdumbbell 35282199Sdumbbell/* Note: This is a purely internal header! */ 36235783Skib/* Do not use this outside of libarchive internal code! */ 37235783Skib 38235783Skib#ifndef ARCHIVE_ENDIAN_H_INCLUDED 39235783Skib#define ARCHIVE_ENDIAN_H_INCLUDED 40235783Skib 41282199Sdumbbell 42282199Sdumbbell/* 43282199Sdumbbell * Disabling inline keyword for compilers known to choke on it: 44271769Sdumbbell * - Watcom C++ in C code. (For any version?) 45282199Sdumbbell * - SGI MIPSpro 46282199Sdumbbell * - Microsoft Visual C++ 6.0 (supposedly newer versions too) 47271769Sdumbbell */ 48274865Sdumbbell#if defined(__WATCOMC__) || defined(__sgi) || defined(__hpux) || defined(__BORLANDC__) 49274865Sdumbbell#define inline 50271769Sdumbbell#elif defined(_MSC_VER) 51262861Sjhb#define inline __inline 52282199Sdumbbell#endif 53282199Sdumbbell 54262861Sjhb/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */ 55262861Sjhb 56262861Sjhbstatic inline uint16_t 57262861Sjhbarchive_be16dec(const void *pp) 58262861Sjhb{ 59262861Sjhb unsigned char const *p = (unsigned char const *)pp; 60262861Sjhb 61262861Sjhb return ((p[0] << 8) | p[1]); 62262861Sjhb} 63262861Sjhb 64262861Sjhbstatic inline uint32_t 65262861Sjhbarchive_be32dec(const void *pp) 66262861Sjhb{ 67262861Sjhb unsigned char const *p = (unsigned char const *)pp; 68262861Sjhb 69262861Sjhb return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); 70262861Sjhb} 71262861Sjhb 72262861Sjhbstatic inline uint64_t 73262861Sjhbarchive_be64dec(const void *pp) 74262861Sjhb{ 75262861Sjhb unsigned char const *p = (unsigned char const *)pp; 76262861Sjhb 77271769Sdumbbell return (((uint64_t)archive_be32dec(p) << 32) | archive_be32dec(p + 4)); 78271769Sdumbbell} 79271769Sdumbbell 80271769Sdumbbellstatic inline uint16_t 81271769Sdumbbellarchive_le16dec(const void *pp) 82262861Sjhb{ 83262861Sjhb unsigned char const *p = (unsigned char const *)pp; 84262861Sjhb 85282199Sdumbbell return ((p[1] << 8) | p[0]); 86282199Sdumbbell} 87235783Skib 88282199Sdumbbellstatic inline uint32_t 89282199Sdumbbellarchive_le32dec(const void *pp) 90235783Skib{ 91282199Sdumbbell unsigned char const *p = (unsigned char const *)pp; 92235783Skib 93282199Sdumbbell return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]); 94282199Sdumbbell} 95235783Skib 96282199Sdumbbellstatic inline uint64_t 97282199Sdumbbellarchive_le64dec(const void *pp) 98282199Sdumbbell{ 99282199Sdumbbell unsigned char const *p = (unsigned char const *)pp; 100235783Skib 101235783Skib return (((uint64_t)archive_le32dec(p + 4) << 32) | archive_le32dec(p)); 102282199Sdumbbell} 103282199Sdumbbell 104282199Sdumbbellstatic inline void 105282199Sdumbbellarchive_be16enc(void *pp, uint16_t u) 106282199Sdumbbell{ 107282199Sdumbbell unsigned char *p = (unsigned char *)pp; 108282199Sdumbbell 109282199Sdumbbell p[0] = (u >> 8) & 0xff; 110235783Skib p[1] = u & 0xff; 111235783Skib} 112235783Skib 113274865Sdumbbellstatic inline void 114235783Skibarchive_be32enc(void *pp, uint32_t u) 115262861Sjhb{ 116274865Sdumbbell unsigned char *p = (unsigned char *)pp; 117274865Sdumbbell 118274865Sdumbbell p[0] = (u >> 24) & 0xff; 119274865Sdumbbell p[1] = (u >> 16) & 0xff; 120274865Sdumbbell p[2] = (u >> 8) & 0xff; 121274865Sdumbbell p[3] = u & 0xff; 122274865Sdumbbell} 123274865Sdumbbell 124274865Sdumbbellstatic inline void 125274865Sdumbbellarchive_be64enc(void *pp, uint64_t u) 126274865Sdumbbell{ 127262861Sjhb unsigned char *p = (unsigned char *)pp; 128274865Sdumbbell 129274865Sdumbbell archive_be32enc(p, u >> 32); 130274865Sdumbbell archive_be32enc(p + 4, u & 0xffffffff); 131274865Sdumbbell} 132274865Sdumbbell 133274865Sdumbbellstatic inline void 134274865Sdumbbellarchive_le16enc(void *pp, uint16_t u) 135274865Sdumbbell{ 136274865Sdumbbell unsigned char *p = (unsigned char *)pp; 137282199Sdumbbell 138235783Skib p[0] = u & 0xff; 139235783Skib p[1] = (u >> 8) & 0xff; 140282199Sdumbbell} 141282199Sdumbbell 142282199Sdumbbellstatic inline void 143282199Sdumbbellarchive_le32enc(void *pp, uint32_t u) 144282199Sdumbbell{ 145282199Sdumbbell unsigned char *p = (unsigned char *)pp; 146282199Sdumbbell 147282199Sdumbbell p[0] = u & 0xff; 148282199Sdumbbell p[1] = (u >> 8) & 0xff; 149282199Sdumbbell p[2] = (u >> 16) & 0xff; 150282199Sdumbbell p[3] = (u >> 24) & 0xff; 151282199Sdumbbell} 152282199Sdumbbell 153282199Sdumbbellstatic inline void 154282199Sdumbbellarchive_le64enc(void *pp, uint64_t u) 155282199Sdumbbell{ 156282199Sdumbbell unsigned char *p = (unsigned char *)pp; 157282199Sdumbbell 158282199Sdumbbell archive_le32enc(p, u & 0xffffffff); 159282199Sdumbbell archive_le32enc(p + 4, u >> 32); 160282199Sdumbbell} 161282199Sdumbbell 162282199Sdumbbell#endif 163282199Sdumbbell