Skip to content

Commit 80a9c01

Browse files
committed
Big endian support (Fixes #567)
1 parent 201ca6c commit 80a9c01

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/Decoder.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -746,29 +746,30 @@ static ZyanStatus ZydisReadDisplacement(ZydisDecoderState* state,
746746
{
747747
ZyanU16 value;
748748
ZYAN_CHECK(ZydisInputNextBytes(state, instruction, (ZyanU8*)&value, 2));
749+
ZYAN_LE16_TO_NATIVE(value);
749750
instruction->raw.disp.value = *(ZyanI16*)&value;
750751
break;
751752
}
752753
case 32:
753754
{
754755
ZyanU32 value;
755756
ZYAN_CHECK(ZydisInputNextBytes(state, instruction, (ZyanU8*)&value, 4));
757+
ZYAN_LE32_TO_NATIVE(value);
756758
instruction->raw.disp.value = *(ZyanI32*)&value;
757759
break;
758760
}
759761
case 64:
760762
{
761763
ZyanU64 value;
762764
ZYAN_CHECK(ZydisInputNextBytes(state, instruction, (ZyanU8*)&value, 8));
765+
ZYAN_LE64_TO_NATIVE(value);
763766
instruction->raw.disp.value = *(ZyanI64*)&value;
764767
break;
765768
}
766769
default:
767770
ZYAN_UNREACHABLE;
768771
}
769772

770-
// TODO: Fix endianess on big-endian systems
771-
772773
return ZYAN_STATUS_SUCCESS;
773774
}
774775

@@ -817,6 +818,7 @@ static ZyanStatus ZydisReadImmediate(ZydisDecoderState* state,
817818
{
818819
ZyanU16 value;
819820
ZYAN_CHECK(ZydisInputNextBytes(state, instruction, (ZyanU8*)&value, 2));
821+
ZYAN_LE16_TO_NATIVE(value);
820822
if (is_signed)
821823
{
822824
instruction->raw.imm[id].value.s = (ZyanI16)value;
@@ -830,6 +832,7 @@ static ZyanStatus ZydisReadImmediate(ZydisDecoderState* state,
830832
{
831833
ZyanU32 value;
832834
ZYAN_CHECK(ZydisInputNextBytes(state, instruction, (ZyanU8*)&value, 4));
835+
ZYAN_LE32_TO_NATIVE(value);
833836
if (is_signed)
834837
{
835838
instruction->raw.imm[id].value.s = (ZyanI32)value;
@@ -843,6 +846,7 @@ static ZyanStatus ZydisReadImmediate(ZydisDecoderState* state,
843846
{
844847
ZyanU64 value;
845848
ZYAN_CHECK(ZydisInputNextBytes(state, instruction, (ZyanU8*)&value, 8));
849+
ZYAN_LE64_TO_NATIVE(value);
846850
if (is_signed)
847851
{
848852
instruction->raw.imm[id].value.s = (ZyanI64)value;
@@ -856,8 +860,6 @@ static ZyanStatus ZydisReadImmediate(ZydisDecoderState* state,
856860
ZYAN_UNREACHABLE;
857861
}
858862

859-
// TODO: Fix endianess on big-endian systems
860-
861863
return ZYAN_STATUS_SUCCESS;
862864
}
863865

src/Encoder.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3172,13 +3172,13 @@ static ZyanStatus ZydisEmitUInt(ZyanU64 data, ZyanU8 size, ZydisEncoderBuffer *b
31723172
return ZYAN_STATUS_INSUFFICIENT_BUFFER_SIZE;
31733173
}
31743174

3175-
// TODO: fix for big-endian systems
31763175
// The size variable is not passed on purpose to allow the compiler
31773176
// to generate better code with a known size at compile time.
31783177
if (size == 1)
31793178
{
31803179
ZYAN_MEMCPY(buffer->buffer + buffer->offset, &data, 1);
31813180
}
3181+
#if ZYAN_ENDIAN == ZYAN_LITTLE_ENDIAN
31823182
else if (size == 2)
31833183
{
31843184
ZYAN_MEMCPY(buffer->buffer + buffer->offset, &data, 2);
@@ -3191,6 +3191,23 @@ static ZyanStatus ZydisEmitUInt(ZyanU64 data, ZyanU8 size, ZydisEncoderBuffer *b
31913191
{
31923192
ZYAN_MEMCPY(buffer->buffer + buffer->offset, &data, 8);
31933193
}
3194+
#else
3195+
else if (size == 2)
3196+
{
3197+
ZyanU16 value = ZYAN_BYTESWAP16((ZyanU16)data);
3198+
ZYAN_MEMCPY(buffer->buffer + buffer->offset, &value, 2);
3199+
}
3200+
else if (size == 4)
3201+
{
3202+
ZyanU32 value = ZYAN_BYTESWAP32((ZyanU32)data);
3203+
ZYAN_MEMCPY(buffer->buffer + buffer->offset, &value, 4);
3204+
}
3205+
else if (size == 8)
3206+
{
3207+
ZyanU64 value = ZYAN_BYTESWAP64((ZyanU64)data);
3208+
ZYAN_MEMCPY(buffer->buffer + buffer->offset, &value, 8);
3209+
}
3210+
#endif
31943211
else
31953212
{
31963213
ZYAN_UNREACHABLE;

0 commit comments

Comments
 (0)