PageRenderTime 24ms CodeModel.GetById 12ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/src/zziplib/zzip/fetch.c

https://bitbucket.org/cabalistic/ogredeps/
C | 126 lines | 74 code | 9 blank | 43 comment | 0 complexity | 84f92a283eb04dd7ace25572125c98b6 MD5 | raw file
  1
  2/*
  3 * These routines are helpers - note that this file gets linked into all the
  4 * zip-access variants that refer to <zzip/format.h>. On the x86 platform the
  5 * actual definitions will be empty - fetching is done on native machine-level
  6 *
  7 * Author:
  8 *      Guido Draheim <guidod@gmx.de>
  9 *
 10 * Copyright (c) 2004,2005,2006 Guido Draheim
 11 *          All rights reserved,
 12 *          use under the restrictions of the
 13 *          Lesser GNU General Public License
 14 *          or alternatively the restrictions
 15 *          of the Mozilla Public License 1.1
 16 */
 17
 18#include <zzip/fetch.h>
 19
 20/* ------------------------- fetch helpers --------------------------------- */
 21
 22/**
 23 * Make 32 bit value in host byteorder from little-endian mapped octet-data
 24 * (works also on machines which SIGBUS on misaligned data access (eg. 68000))
 25 */
 26uint32_t
 27__zzip_get32(unsigned char *s)
 28{
 29#if defined __ZZIP_GET32
 30    return __ZZIP_GET32(s);
 31#else
 32    return ((uint32_t) s[3] << 24) | ((uint32_t) s[2] << 16)
 33        | ((uint32_t) s[1] << 8) | ((uint32_t) s[0]);
 34#endif
 35}
 36
 37/** => __zzip_get32
 38 * This function does the same for a 16 bit value.
 39 */
 40uint16_t
 41__zzip_get16(unsigned char *s)
 42{
 43#if defined __ZZIP_GET16
 44    return __ZZIP_GET16(s);
 45#else
 46    return ((uint16_t) s[1] << 8) | ((uint16_t) s[0]);
 47#endif
 48}
 49
 50/** => __zzip_get32
 51 * This function does the same for an off64_t value.
 52 */
 53uint64_t
 54__zzip_get64(unsigned char *s)
 55{
 56#ifdef __GNUC__
 57    /* *INDENT-OFF* */
 58    register uint64_t v
 59       = s[7]; v <<= 8;
 60    v |= s[6]; v <<= 8;
 61    v |= s[5]; v <<= 8;
 62    v |= s[4]; v <<= 8;
 63    v |= s[3]; v <<= 8;
 64    v |= s[2]; v <<= 8;
 65    v |= s[1]; v <<= 8;
 66    v |= s[0]; return v;
 67    /* *INDENT-ON* */
 68#else
 69    return ((uint64_t) s[7] << 56) | ((uint64_t) s[6] << 48)
 70        | ((uint64_t) s[5] << 40) | ((uint64_t) s[4] << 32)
 71        | ((uint64_t) s[3] << 24) | ((uint64_t) s[2] << 16)
 72        | ((uint64_t) s[1] << 8) | ((uint64_t) s[0]);
 73#endif
 74}
 75
 76/** => __zzip_get32
 77 * This function pushes a 32bit value at the specified address
 78 */
 79void
 80__zzip_set32(unsigned char *s, uint32_t v)
 81{
 82#if defined __ZZIP_SET32
 83    return __ZZIP_SET32(s, v);
 84#else
 85    /* *INDENT-OFF* */
 86    s[0] = (unsigned char) (v); v >>= 8;
 87    s[1] = (unsigned char) (v); v >>= 8;
 88    s[2] = (unsigned char) (v); v >>= 8;
 89    s[3] = (unsigned char) (v);
 90    /* *INDENT-ON* */
 91#endif
 92}
 93
 94/** => __zzip_get32
 95 * This function does the same for a 16 bit value.
 96 */
 97void
 98__zzip_set16(unsigned char *s, uint16_t v)
 99{
100#if defined __ZZIP_SET16
101    return __ZZIP_SET16(s, v);
102#else
103    /* *INDENT-OFF* */
104    s[0] = (unsigned char) (v); v >>= 8;
105    s[1] = (unsigned char) (v);
106    /* *INDENT-ON* */
107#endif
108}
109
110/** => __zzip_get32
111 * This function pushes a off64_t value at the specified address
112 */
113void
114__zzip_set64(unsigned char *s, uint64_t v)
115{
116    /* *INDENT-OFF* */
117    s[0] = (unsigned char) (v); v >>= 8;
118    s[1] = (unsigned char) (v); v >>= 8;
119    s[2] = (unsigned char) (v); v >>= 8;
120    s[3] = (unsigned char) (v); v >>= 8;
121    s[4] = (unsigned char) (v); v >>= 8;
122    s[5] = (unsigned char) (v); v >>= 8;
123    s[6] = (unsigned char) (v); v >>= 8;
124    s[7] = (unsigned char) (v);
125    /* *INDENT-ON* */
126}