ByteOrderDecoder.cpp 1.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
/* Copyright 2015 the SumatraPDF project authors (see AUTHORS file).
   License: Simplified BSD (see COPYING.BSD) */

#include "BaseUtil.h"
#include "ByteOrderDecoder.h"

uint16 UInt16BE(const uint8* d)
{
    return d[1] | (d[0] << 8);
}

uint16 UInt16LE(const uint8* d)
{
    return d[0] | (d[1] << 8);
}

uint32 UInt32BE(const uint8* d)
{
    return d[3] | (d[2] << 8) | (d[1] << 16) | (d[0] << 24);
}

uint32 UInt32LE(const uint8* d)
{
    return d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);
}

ByteOrderDecoder::ByteOrderDecoder(const char *d, size_t len, ByteOrder order)
    : data((const uint8 *)d), left(len), byteOrder(order), curr(data) { }

ByteOrderDecoder::ByteOrderDecoder(const uint8 *d, size_t len, ByteOrder order)
    : data(d), left(len), byteOrder(order), curr(data) { }

uint8 ByteOrderDecoder::UInt8()
{
    CrashIf(left < 1);

    left--;
    return *curr++;
}

uint16 ByteOrderDecoder::UInt16()
{
    uint16 v;
    CrashIf(left < sizeof(v));

    if (LittleEndian == byteOrder)
        v = UInt16LE(curr);
    else
        v = UInt16BE(curr);
    left -= sizeof(v);
    curr += sizeof(v);
    return v;
}

uint32 ByteOrderDecoder::UInt32()
{
    uint32 v;
    CrashIf(left < sizeof(v));

    if (LittleEndian == byteOrder)
        v = UInt32LE(curr);
    else
        v = UInt32BE(curr);
    left -= sizeof(v);
    curr += sizeof(v);
    return v;
}

uint64 ByteOrderDecoder::UInt64()
{
    uint64 first = UInt32();
    if (LittleEndian == byteOrder)
        return first | ((uint64)UInt32() << 32);
    return (first << 32) | UInt32();
}

void ByteOrderDecoder::Bytes(char *dest, size_t len)
{
    CrashIf(left < len);
    memcpy(dest, curr, len);
    left -= len;
    curr += len;
}

void ByteOrderDecoder::Skip(size_t len)
{
    CrashIf(left < len);
    left -= len;
    curr += len;
}

void ByteOrderDecoder::Unskip(size_t len)
{
    CrashIf(curr < data + len);
    left += len;
    curr -= len;
}