summaryrefslogtreecommitdiffstats
path: root/container.c
diff options
context:
space:
mode:
authorDave Heller <hellerda@us.ibm.com>2017-07-16 13:32:36 -0400
committerDave Heller <hellerda@us.ibm.com>2017-07-16 13:32:36 -0400
commita391a7347ca75a683f6a85ba139c50b48eb91714 (patch)
tree9d64110d897372041b4b6824aac3287521b62340 /container.c
parent4f6ed53e453efb5f57b6301c7f086478f71b757b (diff)
downloadsb-signing-utils-a391a7347ca75a683f6a85ba139c50b48eb91714.tar.gz
sb-signing-utils-a391a7347ca75a683f6a85ba139c50b48eb91714.zip
Refactor some code and add container.c
Diffstat (limited to 'container.c')
-rw-r--r--container.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/container.c b/container.c
new file mode 100644
index 0000000..ba42234
--- /dev/null
+++ b/container.c
@@ -0,0 +1,95 @@
+/* Copyright 2017 IBM Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+
+#include "container.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <string.h>
+#include <regex.h>
+#include <sysexits.h>
+
+extern char *progname;
+
+extern bool verbose;
+extern int wrap;
+
+#define die(status, msg, ...) \
+ { fprintf(stderr, "error: %s.%s() line %d: " msg "\n", progname, \
+ __func__, __LINE__, __VA_ARGS__); exit(status); }
+
+#define debug_msg(msg, ...) \
+ if (debug) fprintf(stderr, "--> %s.%s(): " msg "\n", progname, \
+ __func__, __VA_ARGS__);
+
+#define verbose_msg(msg, ...) \
+ if (verbose) fprintf(stdout, "--> %s: " msg "\n", progname, \
+ __VA_ARGS__);
+
+void verbose_print(char *lead, unsigned char *buffer, size_t buflen)
+{
+ if (!verbose)
+ return;
+ unsigned int i, indent = 4;
+ char prelead[100];
+ snprintf(prelead, 100, "--> %s: ", progname);
+
+ char *pad = (((strlen(prelead) + strlen(lead)) % 2) == 0) ? "" : " ";
+ wrap = ((wrap % 2) == 0) ? wrap : wrap - 1;
+ indent = ((indent % 2) == 0) ? indent : indent - 1;
+ int col = fprintf(stdout, "%s%s%s", prelead, lead, pad);
+ for (i = 1; i < buflen + 1; i++) {
+ fprintf(stdout, "%02x", buffer[i - 1]);
+ col = col + 2;
+ if (((col % wrap) == 0) && (i < buflen)) {
+ fprintf(stdout, "\n%*c", indent, ' ');
+ col = indent;
+ }
+ }
+ fprintf(stdout, "\n");
+}
+
+int isValidHex(char *input, int len) {
+ int r;
+ size_t maxlen = 512; // sane limit
+ regex_t regexpr;
+ char pattern[48];
+ char multiplier[8];
+ bool result = false;
+
+ if (strnlen(input, maxlen) >= maxlen)
+ die(EX_DATAERR, "input exceeded max length: %lu", maxlen);
+
+ if (len > 0)
+ sprintf(multiplier, "{%d}", len * 2);
+ else
+ sprintf(multiplier, "+");
+
+ sprintf(pattern, "^(0x|0X)?[a-fA-F0-9]%s$", multiplier);
+
+ if ((r = regcomp(&regexpr, pattern, REG_EXTENDED | REG_NOSUB)))
+ die(EX_SOFTWARE, "%s", "failure to compile regex");
+
+ if (!(r = regexec(&regexpr, input, 0, NULL, 0)))
+ result = true;
+
+ regfree(&regexpr);
+ return result;
+}
OpenPOWER on IntegriCloud