[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 2/2] pickles: add bincodec.pk
From: |
Mohammad-Reza Nabipoor |
Subject: |
[PATCH 2/2] pickles: add bincodec.pk |
Date: |
Thu, 26 Jan 2023 01:01:21 +0100 |
Add pickle to deal with different representation of binary data.
2023-01-26 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
* pickles/bincodec.pk: New pickle.
* pickles/Makefile.am (dist_pickles_DATA): Add new pickle.
* testsuite/poke.pickles/bincodec.pk: New tests.
* testsuite/Makefile.am (EXTRA_DIST): Update.
---
ChangeLog | 7 +++
pickles/Makefile.am | 2 +-
pickles/bincodec.pk | 63 +++++++++++++++++++++++++
testsuite/Makefile.am | 1 +
testsuite/poke.pickles/bincodec-test.pk | 53 +++++++++++++++++++++
5 files changed, 125 insertions(+), 1 deletion(-)
create mode 100644 pickles/bincodec.pk
create mode 100644 testsuite/poke.pickles/bincodec-test.pk
diff --git a/ChangeLog b/ChangeLog
index fc67576c..f216c20e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2023-01-26 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
+
+ * pickles/bincodec.pk: New pickle.
+ * pickles/Makefile.am (dist_pickles_DATA): Add new pickle.
+ * testsuite/poke.pickles/bincodec.pk: New tests.
+ * testsuite/Makefile.am (EXTRA_DIST): Update.
+
2023-01-26 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
* libpoke/std.pk (isdigit_p): New function.
diff --git a/pickles/Makefile.am b/pickles/Makefile.am
index cc4d9877..f8336b7c 100644
--- a/pickles/Makefile.am
+++ b/pickles/Makefile.am
@@ -26,4 +26,4 @@ dist_pickles_DATA = elf-common.pk elf-64.pk elf-32.pk elf.pk
ctf.pk ctf-dump.pk
openpgp.pk search.pk riscv.pk coff.pk coff-i386.pk
coff-aarch64.pk \
pe.pk pe-amd64.pk pe-arm.pk pe-arm64.pk pe-i386.pk
pe-ia64.pk \
pe-m32r.pk pe-mips.pk pe-ppc.pk pe-sh3.pk pe-riscv.pk
pe-debug.pk \
- uuid.pk redoxfs.pk pcap.pk ieee754.pk pdap.pk
+ uuid.pk redoxfs.pk pcap.pk ieee754.pk pdap.pk bincodec.pk
diff --git a/pickles/bincodec.pk b/pickles/bincodec.pk
new file mode 100644
index 00000000..787cba0a
--- /dev/null
+++ b/pickles/bincodec.pk
@@ -0,0 +1,63 @@
+/* bincodec.pk - Encoders/decoders for binary data representations. */
+
+/* Copyright (C) 2023 The poke authors. */
+
+/* This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Given an array of bytes, generate the corresponding hex string
+ representation. */
+
+fun hex_encode = (uint<8>[] data) string:
+{
+ var N = data'length,
+ chars = uint<8>[2*N] (),
+ tmp = 0UB;
+
+ for (var i = 0, o = 0; i < N; ++i, o += 2)
+ {
+ tmp = data[i] .>> 4;
+ chars[o] = tmp < 10UB ? tmp + '0' : tmp - 10UB + 'a';
+ tmp = data[i] & 0xfUB;
+ chars[o + 1] = tmp < 10UB ? tmp + '0' : tmp - 10UB + 'a';
+ }
+ return catos (chars);
+}
+
+/* Given a hex string (that is expected to match the following regular
+ expression: ([0-9a-fA-F][0-9a-fA-F])*), return the corresponding
+ byte array. */
+
+fun hex_decode = (string str) uint<8>[]:
+{
+ assert ((str'length & 1) == 0, "input string length should be even");
+
+ var N = str'length / 2,
+ data = uint<8>[N] ();
+
+ for (var i = 0, o = 0; o < N; i += 2, ++o)
+ {
+ var hi = str[i] as uint<8>,
+ lo = str[i + 1] as uint<8>;
+
+ assert (isxdigit_p (hi), format ("invalid digit at position %i32d", i));
+ assert (isxdigit_p (lo),
+ format ("invalid digit at position %i32d", i + 1));
+
+ hi = isdigit_p (hi) ? hi - '0' : (hi|0x20UB) - 'a' + 10UB;
+ lo = isdigit_p (lo) ? lo - '0' : (lo|0x20UB) - 'a' + 10UB;
+ data[o] = hi <<. 4 | lo;
+ }
+ return data;
+}
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index 885dcb0d..fbbc7b80 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -586,6 +586,7 @@ EXTRA_DIST = \
poke.pickles/pickles.exp \
poke.pickles/argp-test.pk \
poke.pickles/asn1-ber-test.pk \
+ poke.pickles/bincodec.pk \
poke.pickles/bmp-test.pk \
poke.pickles/bpf-test.pk \
poke.pickles/btf-ext-test.pk \
diff --git a/testsuite/poke.pickles/bincodec-test.pk
b/testsuite/poke.pickles/bincodec-test.pk
new file mode 100644
index 00000000..edb64473
--- /dev/null
+++ b/testsuite/poke.pickles/bincodec-test.pk
@@ -0,0 +1,53 @@
+/* bincodec-test.pk - Tests for the bincodec pickle. */
+
+/* Copyright (C) 2023 The poke authors. */
+
+/* This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+load pktest;
+load bincodec;
+
+var tests = [
+ PkTest {
+ name = "hex_encode",
+ func = lambda (string name) void:
+ {
+ assert (hex_encode (uint<8>[] ()) == "");
+ assert (hex_encode ([0UB]) == "00");
+ // assert (hex_encode ([0B]) == "00");
+ assert (hex_encode ([0UB, 1UB, 16UB, 31UB, 32UB, 128UB, 255UB])
+ == "0001101f2080ff");
+ assert (hex_encode ([0x12UB, 0x34UB, 0x56UB, 0x78UB, 0x90UB,
+ 0xabUB, 0xcdUB, 0xefUB])
+ == "1234567890abcdef");
+ },
+ },
+ PkTest {
+ name = "hex_decode",
+ func = lambda (string name) void:
+ {
+ assert (hex_decode ("") == uint<8>[] ());
+ assert (hex_decode ("00") == [0UB]);
+ assert (hex_decode ("0001101f2080ff")
+ == [0UB, 1UB, 16UB, 31UB, 32UB, 128UB, 255UB]);
+ assert (hex_decode ("1234567890abcdef")
+ == [0x12UB, 0x34UB, 0x56UB, 0x78UB, 0x90UB, 0xabUB, 0xcdUB,
+ 0xefUB]);
+ },
+ },
+];
+
+var ok = pktest_run (tests);
+exit (ok ? 0 : 1);
--
2.39.1