/* OpenIDA - Outil d'analyse de fichiers binaires
* endianness.h - prototypes pour la manipulation abstraite des nombres
*
* Copyright (C) 2009-2010 Cyrille Bagard
*
* This file is part of OpenIDA.
*
* OpenIDA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* OpenIDA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see .
*/
#ifndef _COMMON_ENDIANNESS_H
#define _COMMON_ENDIANNESS_H
#include
#include "../arch/archbase.h"
/* Type de boutismes existants */
typedef enum _SourceEndian
{
SRE_LITTLE, /* Petits boutistes */
SRE_MIDDLE, /* Moyens boutistes */
SRE_BIG /* Gros boutistes */
} SourceEndian;
/* Lit un nombre non signé sur 4 bits. */
bool read_u4(uint8_t *, const bin_t *, off_t *, off_t, bool *, SourceEndian);
/* Lit un nombre non signé sur un octet. */
bool read_u8(uint8_t *, const bin_t *, off_t *, off_t, SourceEndian);
/* Lit un nombre non signé sur deux octets. */
bool read_u16(uint16_t *, const bin_t *, off_t *, off_t, SourceEndian);
/* Lit un nombre non signé sur quatre octets. */
bool read_u32(uint32_t *, const bin_t *, off_t *, off_t, SourceEndian);
/* Lit un nombre non signé sur huit octets. */
bool read_u64(uint64_t *, const bin_t *, off_t *, off_t, SourceEndian);
#define read_s4(target, data, pos, len, low, endian) read_u4((uint8_t *)target, data, pos, len, low, endian)
#define read_s8(target, data, pos, len, endian) read_u8((uint8_t *)target, data, pos, len, endian)
#define read_s16(target, data, pos, len, endian) read_u16((uint16_t *)target, data, pos, len, endian)
#define read_s32(target, data, pos, len, endian) read_u32((uint32_t *)target, data, pos, len, endian)
#define read_s64(target, data, pos, len, endian) read_u64((uint64_t *)target, data, pos, len, endian)
/* Ecrit un nombre non signé sur n octets. */
bool _write_un(const bin_t *, size_t, bin_t *, off_t *, off_t, SourceEndian);
#define write_un(value, data, pos, len, endian, type) \
({ \
type __tmp; \
(void)(value == &__tmp); \
_write_un((bin_t *)value, sizeof(type), data, pos, len, endian); \
})
#define write_u8(value, data, pos, len, endian) write_un(value, data, pos, len, endian, uint8_t)
#define write_u16(value, data, pos, len, endian) write_un(value, data, pos, len, endian, uint16_t)
#define write_u32(value, data, pos, len, endian) write_un(value, data, pos, len, endian, uint32_t)
#define write_u64(value, data, pos, len, endian) write_un(value, data, pos, len, endian, uint64_t)
#define write_s8(value, data, pos, len, endian) write_un(value, data, pos, len, endian, sint8_t)
#define write_s16(value, data, pos, len, endian) write_un(value, data, pos, len, endian, sint16_t)
#define write_s32(value, data, pos, len, endian) write_un(value, data, pos, len, endian, sint32_t)
#define write_s64(value, data, pos, len, endian) write_un(value, data, pos, len, endian, sint64_t)
/* Lit un nombre hexadécimal non signé sur deux octets. */
bool strtou8(uint8_t *, const char *, size_t *, size_t, SourceEndian);
/* Lit un nombre hexadécimal non signé sur n octets. */
bool _strtoun(uint8_t, const char *, size_t *, size_t, SourceEndian, ...);
#define strtou32(target, data, pos, len, endian) _strtoun(4, data, pos, len, endian, target)
#endif /* _COMMON_ENDIANNESS_H */