diff --git a/passes/bytes.cc b/passes/bytes.cc index 93fe028..43ff1cb 100644 --- a/passes/bytes.cc +++ b/passes/bytes.cc @@ -7,14 +7,14 @@ extern "C" { void ByteSwap(uint8_t *__restrict__ dst, const uint8_t *__restrict__ src, size_t len) { - static const size_t n = sizeof(uint64_t); - uint64_t *__restrict__ u64_dst = reinterpret_cast(dst); - const uint64_t *__restrict__ u64_src = - reinterpret_cast(src); + static constexpr size_t n = sizeof(uint64_t); + auto dst64 = reinterpret_cast(dst); + auto src64 = reinterpret_cast(src); const size_t m = len / n; + const size_t r = m * n; for (size_t i = 0; i < m; ++i) - u64_dst[i] = __builtin_bswap64(u64_src[m - 1 - i]); - for (size_t i = m * n; i < len; ++i) - dst[i] = src[i]; + dst64[i] = __builtin_bswap64(src64[m - 1 - i]); + for (size_t i = 0; i < len - r; ++i) + dst[r + i] = src[len - 1 - i]; } } diff --git a/passes/passes_test.go b/passes/passes_test.go index 8720d47..fa7743b 100644 --- a/passes/passes_test.go +++ b/passes/passes_test.go @@ -22,8 +22,9 @@ func TestCompress(t *testing.T) { pm := core.NewLegacyPassManager() pm.AddPass(&RandCompressor{}) pm.AddPass(&RandDecompressor{}) - r, err := pm.RunOnBytes([]byte("wtf")) - if string(r) != "wtf" || err != nil { + const s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + r, err := pm.RunOnBytes([]byte(s)) + if string(r) != s || err != nil { t.Log(err) t.Log(r) t.Fail() @@ -34,8 +35,9 @@ func TestLZ4Compress(t *testing.T) { pm := core.NewLegacyPassManager() pm.AddPass(&LZ4Compressor{}) pm.AddPass(&LZ4Decompressor{}) - r, err := pm.RunOnBytes([]byte("wtfwtfwtfwtf")) - if string(r) != "wtfwtfwtfwtf" || err != nil { + const s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + r, err := pm.RunOnBytes([]byte(s)) + if string(r) != s || err != nil { t.Log(err) t.Log(r) t.Fail() @@ -46,8 +48,9 @@ func TestSnappyCompress(t *testing.T) { pm := core.NewLegacyPassManager() pm.AddPass(&SnappyEncoder{}) pm.AddPass(&SnappyDecoder{}) - r, err := pm.RunOnBytes([]byte("wtfwtfwtfwtf")) - if string(r) != "wtfwtfwtfwtf" || err != nil { + const s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + r, err := pm.RunOnBytes([]byte(s)) + if string(r) != s || err != nil { t.Log(err) t.Log(r) t.Fail() @@ -58,8 +61,9 @@ func TestRandCompress(t *testing.T) { pm := core.NewLegacyPassManager() pm.AddPass(&RandCompressor{}) pm.AddPass(&RandDecompressor{}) - r, err := pm.RunOnBytes([]byte("wtfwtfwtfwtf")) - if string(r) != "wtfwtfwtfwtf" || err != nil { + const s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + r, err := pm.RunOnBytes([]byte(s)) + if string(r) != s || err != nil { t.Log(err) t.Log(r) t.Fail() @@ -106,8 +110,9 @@ func TestRC4(t *testing.T) { pm := core.NewLegacyPassManager() pm.AddPass(&RC4Enc{}) pm.AddPass(&RC4Dec{}) - r, err := pm.RunOnBytes([]byte("wtf")) - if string(r) != "wtf" || err != nil { + const s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + r, err := pm.RunOnBytes([]byte(s)) + if string(r) != s || err != nil { t.Log(err) t.Log(r) t.Fail() @@ -118,8 +123,9 @@ func TestOBFS(t *testing.T) { pm := core.NewLegacyPassManager() pm.AddPass(&OBFSEncoder{}) pm.AddPass(&OBFSDecoder{}) - r, err := pm.RunOnBytes([]byte("wtf")) - if string(r) != "wtf" || err != nil { + const s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + r, err := pm.RunOnBytes([]byte(s)) + if string(r) != s || err != nil { t.Log(err) t.Log(r) t.Fail() @@ -130,20 +136,34 @@ func TestRotateLeft(t *testing.T) { pm := core.NewLegacyPassManager() pm.AddPass(&RotateLeft{}) pm.AddPass(&DeRotateLeft{}) - r, err := pm.RunOnBytes([]byte("wtf")) - if string(r) != "wtf" || err != nil { + const s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + r, err := pm.RunOnBytes([]byte(s)) + if string(r) != s || err != nil { t.Log(err) t.Log(r) t.Fail() } } -func TestByteSwap(t *testing.T) { +func TestByteSwapTwice(t *testing.T) { pm := core.NewLegacyPassManager() pm.AddPass(&ByteSwap{}) pm.AddPass(&ByteSwap{}) - r, err := pm.RunOnBytes([]byte("wtfwtfwtfwtfwtfwtfwtf")) - if string(r) != "wtfwtfwtfwtfwtfwtfwtf" || err != nil { + const s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + r, err := pm.RunOnBytes([]byte(s)) + if string(r) != s || err != nil { + t.Log(err) + t.Log(r) + t.Fail() + } +} + +func TestByteSwap(t *testing.T) { + pm := core.NewLegacyPassManager() + pm.AddPass(&ByteSwap{}) + const s = "0123456789ABCDE" + r, err := pm.RunOnBytes([]byte(s)) + if string(r) != "76543210EDCBA98" || err != nil { t.Log(err) t.Log(r) t.Fail() @@ -154,8 +174,9 @@ func TestIntegration(t *testing.T) { pm := core.NewLegacyPassManager() pm.AddPass(&ByteSwap{}).AddPass(&OBFSEncoder{}).AddPass(&LZ4Compressor{}).AddPass(&Reverse{}).AddPass(&RotateLeft{}) pm.AddPass(&DeRotateLeft{}).AddPass(&Reverse{}).AddPass(&LZ4Decompressor{}).AddPass(&OBFSDecoder{}).AddPass(&ByteSwap{}) - r, err := pm.RunOnBytes([]byte("wtfwtfwtf")) - if string(r) != "wtfwtfwtf" || err != nil { + const s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + r, err := pm.RunOnBytes([]byte(s)) + if string(r) != s || err != nil { t.Log(err) t.Log(r) t.Fail()