Skip to content

Commit

Permalink
to fix upstream bug with hexstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdruppe committed Oct 24, 2024
1 parent 23a23a1 commit 635f54d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compiler/src/dmd/dinterpret.d
Original file line number Diff line number Diff line change
Expand Up @@ -6142,6 +6142,8 @@ public:
return;
}

import dmd.utils : arrayCastBigEndian;

auto str = arrayCastBigEndian(se.peekData(), sz);
emplaceExp!(StringExp)(pue, e1.loc, str, se.len / sz, cast(ubyte) sz);
result = pue.exp();
Expand Down
33 changes: 33 additions & 0 deletions compiler/src/dmd/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,39 @@ unittest
assert(buf[] == expected);
}

/**
* Cast a `ubyte[]` to an array of larger integers as if we are on a big endian architecture
* Params:
* data = array with big endian data
* size = 1 for ubyte[], 2 for ushort[], 4 for uint[], 8 for ulong[]
* Returns: copy of `data`, with bytes shuffled if compiled for `version(LittleEndian)`
*/
ubyte[] arrayCastBigEndian(const ubyte[] data, size_t size)
{
ubyte[] impl(T)()
{
auto result = new T[](data.length / T.sizeof);
foreach (i; 0 .. result.length)
{
result[i] = 0;
foreach (j; 0 .. T.sizeof)
{
result[i] |= T(data[i * T.sizeof + j]) << ((T.sizeof - 1 - j) * 8);
}
}
return cast(ubyte[]) result;
}
switch (size)
{
case 1: return data.dup;
case 2: return impl!ushort;
case 4: return impl!uint;
case 8: return impl!ulong;
default: assert(0);
}
}


/**
* Convert string to integer.
*
Expand Down

0 comments on commit 635f54d

Please sign in to comment.