From f12215b8d88a9c63ff1ecf2ec4233266871a0836 Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Fri, 24 Sep 2021 15:31:27 +0800 Subject: [PATCH] fix upper hex in user templates --- test_unishox2.c | 18 ++++++++++++++++++ unishox2.c | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/test_unishox2.c b/test_unishox2.c index 6887396..6be02b9 100644 --- a/test_unishox2.c +++ b/test_unishox2.c @@ -395,6 +395,24 @@ if (argv >= 2 && strcmp(args[1], "-t") == 0) { if (argv > 2) preset = atoi(args[2]); + // check template + { + char cbuf[128]; + char dbuf[128]; + char *hex = ":AAAAAA-bbbbbb"; + const int len = strlen(hex); + const int clen = unishox2_compress_lines(hex, len, cbuf, USX_HCODES_DFLT, USX_HCODE_LENS_DFLT, USX_FREQ_SEQ_DFLT, (const char *[]){":FFFFFF", "-ffffff", 0, 0, 0} , NULL); + const int dlen = unishox2_decompress_lines(cbuf, clen, dbuf, USX_HCODES_DFLT, USX_HCODE_LENS_DFLT, USX_FREQ_SEQ_DFLT, (const char *[]){":FFFFFF", "-ffffff", 0, 0, 0} , NULL); + if (dlen != len) { + printf("Fail len (template): %d, %d:\n%s\n%s\n", len, dlen, hex, dbuf); + return 1; + } + if (strncmp(hex, dbuf, len)) { + printf("Fail cmp (template):\n%s\n%s\n", hex, dbuf); + return 0; + } + } + // Basic if (!test_ushx_cd("Hello", preset)) return 1; if (!test_ushx_cd("Hello World", preset)) return 1; diff --git a/unishox2.c b/unishox2.c index 5d317db..765ef8c 100644 --- a/unishox2.c +++ b/unishox2.c @@ -891,7 +891,7 @@ int decodeRepeat(const char *in, int len, char *out, int ol, int *bit_no, struct char getHexChar(int nibble, int hex_type) { if (nibble >= 0 && nibble <= 9) return '0' + nibble; - else if (hex_type < 3) + else if (hex_type < USX_NIB_HEX_UPPER) return 'a' + nibble - 10; return 'A' + nibble - 10; } @@ -1061,7 +1061,7 @@ int unishox2_decompress_lines(const char *in, int len, char *out, const byte usx } do { int nibble = (int) getNumFromBits(in, len, bit_no, 4); - out[ol++] = getHexChar(nibble, idx); + out[ol++] = getHexChar(nibble, idx < 3 ? USX_NIB_HEX_LOWER : USX_NIB_HEX_UPPER); if ((idx == 2 || idx == 4) && (nibble_count == 25 || nibble_count == 21 || nibble_count == 17 || nibble_count == 13)) out[ol++] = '-'; bit_no += 4;