aboutsummaryrefslogtreecommitdiff
path: root/testing/yup-comm/utils
diff options
context:
space:
mode:
Diffstat (limited to 'testing/yup-comm/utils')
-rw-r--r--testing/yup-comm/utils/byte_buffer.h31
-rw-r--r--testing/yup-comm/utils/crc32q.c64
-rw-r--r--testing/yup-comm/utils/crc32q.h12
3 files changed, 107 insertions, 0 deletions
diff --git a/testing/yup-comm/utils/byte_buffer.h b/testing/yup-comm/utils/byte_buffer.h
new file mode 100644
index 0000000..30746f2
--- /dev/null
+++ b/testing/yup-comm/utils/byte_buffer.h
@@ -0,0 +1,31 @@
+/* Author: Jan Sucan */
+
+#ifndef BYTE_BUFFER_H_
+#define BYTE_BUFFER_H_
+
+#include <stdint.h>
+#include <stdlib.h>
+
+#define GET_UINT8_T_FROM_BYTES(b, i) ((uint8_t) b[i])
+
+#define GET_UINT16_T_FROM_BYTES(b, i) ((uint16_t) ((b[i + 1] << 8) | b[i]))
+
+#define GET_UINT32_T_FROM_BYTES(b, i) ((uint32_t) ((b[i + 3] << 24) | (b[i + 2] << 16) | (b[i + 1] << 8) | b[i]))
+
+#define SET_BYTES_FROM_UINT8_T(b, i, v) {\
+ b[i + 0] = v;\
+}
+
+#define SET_BYTES_FROM_UINT16_T(b, i, v) { \
+ b[i + 1] = (v & 0xFF00) >> 8;\
+ b[i + 0] = (v & 0x00FF);\
+}
+
+#define SET_BYTES_FROM_UINT32_T(b, i, v) {\
+ b[i + 3] = (v & 0xFF000000) >> 24;\
+ b[i + 2] = (v & 0x00FF0000) >> 16;\
+ b[i + 1] = (v & 0x0000FF00) >> 8;\
+ b[i + 0] = (v & 0x000000FF);\
+}
+
+#endif /* BYTE_BUFFER_H_ */
diff --git a/testing/yup-comm/utils/crc32q.c b/testing/yup-comm/utils/crc32q.c
new file mode 100644
index 0000000..a0f0c46
--- /dev/null
+++ b/testing/yup-comm/utils/crc32q.c
@@ -0,0 +1,64 @@
+/* Author: Jan Sucan */
+
+#include "crc32q.h"
+
+#include <stdbool.h>
+
+#define CRC32_TABLE_SIZE 256
+
+#define CRC32_GENERATING_POLYNOME 0x814141ab
+
+static void crc32q_create_table(uint32_t * const tab, uint32_t gen_polynome);
+
+void
+crc32q_create_table(uint32_t * const tab, uint32_t gen_polynome)
+{
+ int i, j;
+ uint32_t c;
+ uint32_t msbMask = 1U << ((sizeof(c) * 8) - 1);
+
+ for (i = 0; i < CRC32_TABLE_SIZE; i++) {
+ c = i << 24;
+ for (j = 0; j < 8; j++) {
+ if (c & msbMask) {
+ c = (c << 1) ^ gen_polynome;
+ } else {
+ c = c << 1;
+ }
+ }
+ tab[i] = c;
+ }
+}
+
+static uint32_t crc32_tab[CRC32_TABLE_SIZE];
+
+static bool crc32q_table_created = false;
+
+void
+crc32q_init(void)
+{
+ crc32q_create_table(crc32_tab, CRC32_GENERATING_POLYNOME);
+ crc32q_table_created = true;
+}
+
+uint32_t
+crc32q(const void * const buf, size_t size)
+{
+ // Vytvorenie tabulky pred prvym pouzitim
+ if (!crc32q_table_created) {
+ crc32q_create_table(crc32_tab, CRC32_GENERATING_POLYNOME);
+ crc32q_table_created = true;
+ }
+
+ // Vypocet CRC-32
+ const uint8_t *p;
+
+ p = buf;
+ uint32_t crc = 0U; // Inicializacna CRC hodnota
+
+ while (size--) {
+ crc = (crc << 8) ^ crc32_tab[(crc >> 24) ^ *p++];
+ }
+
+ return crc ^ 0U;
+}
diff --git a/testing/yup-comm/utils/crc32q.h b/testing/yup-comm/utils/crc32q.h
new file mode 100644
index 0000000..54ea16d
--- /dev/null
+++ b/testing/yup-comm/utils/crc32q.h
@@ -0,0 +1,12 @@
+/* Author: Jan Sucan */
+
+#ifndef CRC32Q_H_
+#define CRC32Q_H_
+
+#include <stdint.h>
+#include <stdlib.h>
+
+void crc32q_init(void);
+uint32_t crc32q(const void * const buf, size_t size);
+
+#endif /* CRC32Q_H_ */ \ No newline at end of file