c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen/*
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen * Little-endian data access functions.
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen *
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen * Copyright (c) 2004 Andrey Panin <pazke@donpac.ru>
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen *
e074ffeaee1ce283bd42f167c6810e3d013f8218Timo Sirainen * This software is released under the MIT license.
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen */
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#ifndef NTLM_BYTEORDER_H
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#define NTLM_BYTEORDER_H
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
db14b5bccf7173161d2247efa7df503cd13a66e3Timo Sirainen#if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenstatic inline uint16_t read_le16(const void *addr)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen{
8d6cb44a0161d88743756733f83c4fb278485987Timo Sirainen return *((const uint16_t *) addr);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen}
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenstatic inline uint32_t read_le32(const void *addr)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen{
8d6cb44a0161d88743756733f83c4fb278485987Timo Sirainen return *((const uint32_t *) addr);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen}
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenstatic inline uint64_t read_le64(const void *addr)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen{
8d6cb44a0161d88743756733f83c4fb278485987Timo Sirainen return *((const uint64_t *) addr);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen}
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenstatic inline void write_le16(void *addr, const uint16_t value)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen{
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen *((uint16_t *) addr) = value;
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen}
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenstatic inline void write_le32(void *addr, const uint32_t value)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen{
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen *((uint32_t *) addr) = value;
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen}
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenstatic inline void write_le64(void *addr, const uint64_t value)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen{
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen *((uint64_t *) addr) = value;
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen}
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen#else
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen/*
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen * Dumb and slow, but byteorder and alignment independent code.
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen */
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen#define readb(addr, pos, type) ((type)(*(((uint8_t *) (addr)) + (pos))))
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenstatic inline uint16_t read_le16(const void *addr)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen{
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen return readb(addr, 0, uint16_t) | (readb(addr, 1, uint16_t) << 8);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen}
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenstatic inline uint32_t read_le32(const void *addr)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen{
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen return readb(addr, 0, uint32_t) |
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen (readb(addr, 1, uint32_t) << 8) |
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen (readb(addr, 2, uint32_t) << 16) |
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen (readb(addr, 3, uint32_t) << 24);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen}
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenstatic inline uint64_t read_le64(const void *addr)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen{
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen return readb(addr, 0, uint64_t) |
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen (readb(addr, 1, uint64_t) << 8) |
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen (readb(addr, 2, uint64_t) << 16) |
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen (readb(addr, 3, uint64_t) << 24) |
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen (readb(addr, 4, uint64_t) << 32) |
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen (readb(addr, 5, uint64_t) << 40) |
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen (readb(addr, 6, uint64_t) << 48) |
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen (readb(addr, 7, uint64_t) << 56);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen}
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen#define writeb(addr, pos, value) \
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen *(((uint8_t *)(addr)) + (pos)) = (uint8_t) (value)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenstatic inline void write_le16(void *addr, const uint16_t value)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen{
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 0, value & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 1, (value >> 8) & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen}
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenstatic inline void write_le32(void *addr, const uint32_t value)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen{
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 0, value & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 1, (value >> 8) & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 2, (value >> 16) & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 3, (value >> 24) & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen}
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenstatic inline void write_le64(void *addr, const uint64_t value)
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen{
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 0, value & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 1, (value >> 8) & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 2, (value >> 16) & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 3, (value >> 24) & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 4, (value >> 32) & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 5, (value >> 40) & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 6, (value >> 48) & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen writeb(addr, 7, (value >> 56) & 0xff);
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen}
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen#endif
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#endif