Path: blob/master/Utilities/cmlibarchive/libarchive/archive_endian.h
3153 views
/*-1* Copyright (c) 2002 Thomas Moestl <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25* Borrowed from FreeBSD's <sys/endian.h>26*/2728#ifndef ARCHIVE_ENDIAN_H_INCLUDED29#define ARCHIVE_ENDIAN_H_INCLUDED3031/* Note: This is a purely internal header! */32/* Do not use this outside of libarchive internal code! */3334#ifndef __LIBARCHIVE_BUILD35#error This header is only to be used internally to libarchive.36#endif3738/*39* Disabling inline keyword for compilers known to choke on it:40* - Watcom C++ in C code. (For any version?)41* - SGI MIPSpro42* - Microsoft Visual C++ 6.0 (supposedly newer versions too)43* - IBM VisualAge 6 (XL v6)44* - Sun WorkShop C (SunPro) before 5.945*/46#if defined(__WATCOMC__) || defined(__sgi) || defined(__hpux) || defined(__BORLANDC__)47#define inline48#elif defined(__IBMC__) && __IBMC__ < 70049#define inline50#elif defined(__SUNPRO_C) && __SUNPRO_C < 0x59051#define inline52#elif defined(_MSC_VER) || defined(__osf__)53#define inline __inline54#endif5556/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */5758static inline uint16_t59archive_be16dec(const void *pp)60{61unsigned char const *p = (unsigned char const *)pp;6263/* Store into unsigned temporaries before left shifting, to avoid64promotion to signed int and then left shifting into the sign bit,65which is undefined behaviour. */66unsigned int p1 = p[1];67unsigned int p0 = p[0];6869return ((p0 << 8) | p1);70}7172static inline uint32_t73archive_be32dec(const void *pp)74{75unsigned char const *p = (unsigned char const *)pp;7677/* Store into unsigned temporaries before left shifting, to avoid78promotion to signed int and then left shifting into the sign bit,79which is undefined behaviour. */80unsigned int p3 = p[3];81unsigned int p2 = p[2];82unsigned int p1 = p[1];83unsigned int p0 = p[0];8485return ((p0 << 24) | (p1 << 16) | (p2 << 8) | p3);86}8788static inline uint64_t89archive_be64dec(const void *pp)90{91unsigned char const *p = (unsigned char const *)pp;9293return (((uint64_t)archive_be32dec(p) << 32) | archive_be32dec(p + 4));94}9596static inline uint16_t97archive_le16dec(const void *pp)98{99unsigned char const *p = (unsigned char const *)pp;100101/* Store into unsigned temporaries before left shifting, to avoid102promotion to signed int and then left shifting into the sign bit,103which is undefined behaviour. */104unsigned int p1 = p[1];105unsigned int p0 = p[0];106107return ((p1 << 8) | p0);108}109110static inline uint32_t111archive_le32dec(const void *pp)112{113unsigned char const *p = (unsigned char const *)pp;114115/* Store into unsigned temporaries before left shifting, to avoid116promotion to signed int and then left shifting into the sign bit,117which is undefined behaviour. */118unsigned int p3 = p[3];119unsigned int p2 = p[2];120unsigned int p1 = p[1];121unsigned int p0 = p[0];122123return ((p3 << 24) | (p2 << 16) | (p1 << 8) | p0);124}125126static inline uint64_t127archive_le64dec(const void *pp)128{129unsigned char const *p = (unsigned char const *)pp;130131return (((uint64_t)archive_le32dec(p + 4) << 32) | archive_le32dec(p));132}133134static inline void135archive_be16enc(void *pp, uint16_t u)136{137unsigned char *p = (unsigned char *)pp;138139p[0] = (u >> 8) & 0xff;140p[1] = u & 0xff;141}142143static inline void144archive_be32enc(void *pp, uint32_t u)145{146unsigned char *p = (unsigned char *)pp;147148p[0] = (u >> 24) & 0xff;149p[1] = (u >> 16) & 0xff;150p[2] = (u >> 8) & 0xff;151p[3] = u & 0xff;152}153154static inline void155archive_be64enc(void *pp, uint64_t u)156{157unsigned char *p = (unsigned char *)pp;158159archive_be32enc(p, (uint32_t)(u >> 32));160archive_be32enc(p + 4, (uint32_t)(u & 0xffffffff));161}162163static inline void164archive_le16enc(void *pp, uint16_t u)165{166unsigned char *p = (unsigned char *)pp;167168p[0] = u & 0xff;169p[1] = (u >> 8) & 0xff;170}171172static inline void173archive_le32enc(void *pp, uint32_t u)174{175unsigned char *p = (unsigned char *)pp;176177p[0] = u & 0xff;178p[1] = (u >> 8) & 0xff;179p[2] = (u >> 16) & 0xff;180p[3] = (u >> 24) & 0xff;181}182183static inline void184archive_le64enc(void *pp, uint64_t u)185{186unsigned char *p = (unsigned char *)pp;187188archive_le32enc(p, (uint32_t)(u & 0xffffffff));189archive_le32enc(p + 4, (uint32_t)(u >> 32));190}191192#endif193194195