aboutsummaryrefslogtreecommitdiff
path: root/include/flatcc/support/cdump.h
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2023-07-16 02:03:33 +0200
committerToni Uhlig <matzeton@googlemail.com>2023-07-16 02:03:33 +0200
commit5a40295c4cf0af5ea8da9ced04a4ce7d3621a080 (patch)
treecb21506e7b04d10b45d6066a0ee1655563d5d52b /include/flatcc/support/cdump.h
Squashed 'flatcc/' content from commit 473da2a
git-subtree-dir: flatcc git-subtree-split: 473da2afa5ca435363f8c5e6569167aee6bc31c5
Diffstat (limited to 'include/flatcc/support/cdump.h')
-rw-r--r--include/flatcc/support/cdump.h38
1 files changed, 38 insertions, 0 deletions
diff --git a/include/flatcc/support/cdump.h b/include/flatcc/support/cdump.h
new file mode 100644
index 0000000..b589362
--- /dev/null
+++ b/include/flatcc/support/cdump.h
@@ -0,0 +1,38 @@
+#ifndef CDUMP_H
+#define CDUMP_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdio.h>
+
+/* Generates a constant a C byte array. */
+static void cdump(const char *name, void *addr, size_t len, FILE *fp) {
+ unsigned int i;
+ unsigned char *pc = (unsigned char*)addr;
+
+ // Output description if given.
+ name = name ? name : "dump";
+ fprintf(fp, "const unsigned char %s[] = {", name);
+
+ // Process every byte in the data.
+ for (i = 0; i < (unsigned int)len; i++) {
+ // Multiple of 16 means new line (with line offset).
+
+ if ((i % 16) == 0) {
+ fprintf(fp, "\n ");
+ } else if ((i % 8) == 0) {
+ fprintf(fp, " ");
+ }
+
+ fprintf(fp, " 0x%02x,", pc[i]);
+ }
+ fprintf(fp, "\n};\n");
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CDUMP_H */