Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make code endianness-agnostic #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ case "$host_os" in
AC_DEFINE([_DEFAULT_SOURCE], [], [Enable vsyslog().])
;;
esac
AC_C_BIGENDIAN(,, [AC_MSG_ERROR([Unknown endianness])])
AC_CONFIG_HEADERS([libexfat/config.h])
AC_CONFIG_FILES([
libexfat/Makefile
Expand Down
81 changes: 47 additions & 34 deletions libexfat/byteorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,59 @@
#ifndef BYTEORDER_H_INCLUDED
#define BYTEORDER_H_INCLUDED

#include "platform.h"
#include "config.h"
#include <stdint.h>
#include <stddef.h>

#ifdef WORDS_BIGENDIAN
typedef unsigned char bitmap_t;
#else
typedef size_t bitmap_t;
#endif

typedef struct { uint16_t __u16; } le16_t;
typedef struct { uint32_t __u32; } le32_t;
typedef struct { uint64_t __u64; } le64_t;

#if EXFAT_BYTE_ORDER == EXFAT_LITTLE_ENDIAN

static inline uint16_t le16_to_cpu(le16_t v) { return v.__u16; }
static inline uint32_t le32_to_cpu(le32_t v) { return v.__u32; }
static inline uint64_t le64_to_cpu(le64_t v) { return v.__u64; }

static inline le16_t cpu_to_le16(uint16_t v) { le16_t t = {v}; return t; }
static inline le32_t cpu_to_le32(uint32_t v) { le32_t t = {v}; return t; }
static inline le64_t cpu_to_le64(uint64_t v) { le64_t t = {v}; return t; }

typedef size_t bitmap_t;

#elif EXFAT_BYTE_ORDER == EXFAT_BIG_ENDIAN

static inline uint16_t le16_to_cpu(le16_t v)
{ return exfat_bswap16(v.__u16); }
static inline uint32_t le32_to_cpu(le32_t v)
{ return exfat_bswap32(v.__u32); }
static inline uint64_t le64_to_cpu(le64_t v)
{ return exfat_bswap64(v.__u64); }

static inline le16_t cpu_to_le16(uint16_t v)
{ le16_t t = {exfat_bswap16(v)}; return t; }
static inline le32_t cpu_to_le32(uint32_t v)
{ le32_t t = {exfat_bswap32(v)}; return t; }
static inline le64_t cpu_to_le64(uint64_t v)
{ le64_t t = {exfat_bswap64(v)}; return t; }

typedef unsigned char bitmap_t;

#else
#error Wow! You have a PDP machine?!
#endif
static inline uint16_t le16_to_cpu(le16_t v) {
unsigned char *p = (unsigned char *)&v;
return p[0] | p[1] << 8;
}

static inline uint32_t le32_to_cpu(le32_t v) {
unsigned char *p = (unsigned char *)&v;
return p[0] | p[1] << 8 | p[2] << 16 | (uint32_t)p[3] << 24;
}
static inline uint64_t le64_to_cpu(le64_t v) {
unsigned char *p = (unsigned char *)&v;
uint64_t rval = 0;

rval |= (uint64_t)p[0] | (uint64_t)p[1] << 8;
rval |= (uint64_t)p[2] << 16 | (uint64_t)p[3] << 24;
rval |= (uint64_t)p[4] << 32 | (uint64_t)p[5] << 40;
rval |= (uint64_t)p[6] << 48 | (uint64_t)p[7] << 56;
return rval;
}

static inline le16_t cpu_to_le16(uint16_t v) {
le16_t t;
unsigned char *p = (unsigned char *)&t;
p[0] = v ; p[1] = v >> 8;
return t;
}
static inline le32_t cpu_to_le32(uint32_t v) {
le32_t t;
unsigned char *p = (unsigned char *)&t;
p[0] = v ; p[1] = v >>= 8; p[2] = v >>= 8; p[3] = v >> 8;
return t;
}

static inline le64_t cpu_to_le64(uint64_t v) {
le64_t t;
unsigned char *p = (unsigned char *)&t;
p[0] = v ; p[1] = v >>= 8; p[2] = v >>= 8; p[3] = v >>= 8;
p[4] = v >>= 8; p[5] = v >>= 8; p[6] = v >>= 8; p[7] = v >> 8;
return t;
}

#endif /* ifndef BYTEORDER_H_INCLUDED */
73 changes: 0 additions & 73 deletions libexfat/platform.h

This file was deleted.