agl / jbig2enc

JBIG2 Encoder
Other
251 stars 86 forks source link

Wrong bit field order on ARM #63

Open LubosD opened 5 years ago

LubosD commented 5 years ago

I've just spent a few hours figuring out why jbig2enc compiled for Android/arm produces output that cannot be decoded...

In jbig2structs.h there's a comment saying:

// GCC packs bit fields in a different order on big endian machines

The problem is Android has its own definition of _BIG_ENDIAN coming from here:

/tmp/my-android-toolchain/bin/../sysroot/usr/include/sys/endian.h:33:9: note: previous definition is here
#define _BIG_ENDIAN     4321

Which makes your code assume it's being compiled for a big endian platform.

Mark-Joy commented 3 years ago

Thank you!! This is exactly what happened to my build! I use Termux on my android phone which uses clang as the default compiler. According to this answer https://stackoverflow.com/questions/2100331/c-macro-definition-to-determine-big-endian-or-little-endian-machine/2100391#2100391, I run on terminal:

λ [~] $ gcc -E -dM - < /dev/null |grep ENDIAN
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
#define __LITTLE_ENDIAN__ 1
#define __ORDER_BIG_ENDIAN__ 4321
#define __ORDER_LITTLE_ENDIAN__ 1234
#define __ORDER_PDP_ENDIAN__ 3412
λ [~] $

My ARM is little endian with __LITTLE_ENDIAN__ flag defined. There are BIG_ENDIAN _BIG_ENDIAN and __BIG_ENDIAN defined in /sysroot/usr/include/sys/endian.h, but there is no __BIG_ENDIAN__

So a quick fix in my case would be replacing all entries: #ifndef _BIG_ENDIAN with #ifndef __BIG_ENDIAN__ in src/jbig2structs.h and src/jbig2segments.h