Skip to content

Instantly share code, notes, and snippets.

@animetosho
Created September 24, 2017 03:26
Show Gist options
  • Save animetosho/5012484c08204bd744be475e78131247 to your computer and use it in GitHub Desktop.
Save animetosho/5012484c08204bd744be475e78131247 to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
#include "decoder.h"
template<bool isRaw>
static size_t do_decode_scalar(const unsigned char* src, unsigned char* dest, size_t len, char* state);
template<bool isRaw, bool use_ssse3>
static size_t do_decode_sse(const unsigned char* src, unsigned char* dest, size_t len, char* state);
using decode_func = size_t(*)(const unsigned char* src, unsigned char* dest, size_t len, char* state);
typedef struct
{
const char* source;
const char* expected;
} test_pair;
std::vector<test_pair> tests = {
{"7c 8b 9c 4b 44 31 2a 84 98 9d 3b 2b 37 2a 2a 2a 2a 2a 2a 2a dd b5 9e 4c bb 78 2a b4 29 69 30 2a 2a 8a ed 2c 8a 04 14 98 1e 8c 2e 73 3e 5a 4b 2a 4a 4a 2a 2a 2a 2a 2a 2a 2b 2a 2a 2a 79 9a 8f 98 6b 7e 80 57 8c 9f 93 96 8e 57 8c 99 a2 57 76 93 98 9f a2 57 8e 93 9d 95 5b 58 a0 8e 93 2a 1a 7d e6 a2 66 66 66 4a 79 9c 8b 8d 96 8f 4a 80 77 4a 80 93 9c 9e 9f 8b 96 6c 99 a2 4a 6e 93 9d 95 4a ",
"52 61 72 21 1a 07 00 5a 6e 73 11 01 0d 00 00 00 00 00 00 00 b3 8b 74 22 91 4e 00 8a ff 3f 06 00 00 60 c3 02 60 da ea 6e f4 62 04 49 14 30 21 00 20 20 00 00 00 00 00 00 01 00 00 00 4f 70 65 6e 41 54 56 2d 62 75 69 6c 64 2d 62 6f 78 2d 4c 69 6e 75 78 2d 64 69 73 6b 31 2e 76 64 69 00 f0 53 bc 78 3c 3c 3c 20 4f 72 61 63 6c 65 20 56 4d 20 56 69 72 74 75 61 6c 42 6f 78 20 44 69 73 6b 20 "},
{"0f 1a b1 a4 0c d4 15 2a 61 47 c8 bf bf e4 d3 e9 e2 b2 1f e0 99 1d 79 9a 38 26 c0 8b 3d 40 42 cf c6 f9 85 34 8d 9c f2 55 ce 16 ec 4d 38 29 3d 4d 22 d8 bb cc ce 2a 91 c9 93 87 6f 0f fb 5b 2c d7 90 3c 22 4a ac a7 1a 57 1a bb 6b 64 23 e0 87 8f b2 3d 7d 94 30 c5 eb 2f cb e5 78 35 8e bc d0 0b 57 15 58 69 e3 9d fc f3 da 6b c1 07 3d 4d d2 6a 60 6f 43 a4 3d 4a 81 dc b7 ca 04 8a c1 f6 8d b8 ",
"e5 f0 87 7a e2 aa eb 00 37 1d 9e 95 95 ba a9 bf b8 88 f5 b6 6f f3 4f 70 0e fc 96 61 d6 18 a5 9c cf 5b 0a 63 72 c8 2b a4 ec c2 23 0e ff e3 f8 ae 91 a2 a4 00 67 9f 69 5d 45 e5 d1 31 02 ad 66 12 f8 20 82 7d f0 2d f0 91 41 3a f9 b6 5d 65 88 13 6a 06 9b c1 05 a1 bb 4e 0b 64 92 a6 e1 2d eb 2e 3f b9 73 d2 c9 b0 41 97 dd e3 a8 40 36 45 19 7a e0 57 b2 8d a0 da 60 97 cc 63 8e "}
};
std::vector<unsigned char> unhex(const char* hexstr)
{
std::vector<unsigned char> result;
for (const char* p = hexstr; *p; p += 3)
{
char *endptr;
long val = strtol(p, &endptr, 16);
result.push_back((unsigned char)val);
}
return result;
}
void print_vector(const char* title, std::vector<unsigned char> vec)
{
printf("%s:\n", title);
for (unsigned char ch : vec)
{
printf("%02x ", ch);
}
printf("\n");
}
void test(decode_func func)
{
int num = 0;
for (test_pair pair : tests)
{
std::vector<unsigned char> source = unhex(pair.source);
std::vector<unsigned char> expected = unhex(pair.expected);
std::vector<unsigned char> result;
result.resize(source.size());
size_t res_len = func(source.data(), result.data(), source.size(), nullptr);
result.resize(res_len);
bool ok = result == expected;
printf("\nTest %i: %s\n", ++num, ok ? "OK" : "FAILURE");
if (!ok)
{
print_vector("Source", source);
print_vector("Expected", expected);
print_vector("Result", result);
}
}
}
int main()
{
decoder_init();
printf("Testing scalar\n");
test((decode_func)do_decode_scalar<false>);
printf("\n");
printf("Testing sse\n");
test((decode_func)do_decode_sse<false, false>);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment