Files
skiboot/libstb/container-utils.c
Stewart Smith 49496485fe Re-license IBM written files as Apache 2.0 OR GPLv2+
SPDX makes it a simpler diff.

I have audited the commit history of each file to ensure that they are
exclusively authored by IBM and thus we have the right to relicense.

The motivation behind this is twofold:
1) We want to enable experiments with coreboot, which is GPLv2 licensed
2) An upcoming firmware component wants to incorporate code from skiboot
   and code from the Linux kernel, which is GPLv2 licensed.

I have gone through the IBM internal way of gaining approval for this.

The following files are not exclusively authored by IBM, so are *not*
included in this update (I will be seeking approval from contributors):

core/direct-controls.c
core/flash.c
core/pcie-slot.c
external/common/arch_flash_unknown.c
external/common/rules.mk
external/gard/Makefile
external/gard/rules.mk
external/opal-prd/Makefile
external/pflash/Makefile
external/xscom-utils/Makefile
hdata/vpd.c
hw/dts.c
hw/ipmi/ipmi-watchdog.c
hw/phb4.c
include/cpu.h
include/phb4.h
include/platform.h
libflash/libffs.c
libstb/mbedtls/sha512.c
libstb/mbedtls/sha512.h
platforms/astbmc/barreleye.c
platforms/astbmc/garrison.c
platforms/astbmc/mihawk.c
platforms/astbmc/nicole.c
platforms/astbmc/p8dnu.c
platforms/astbmc/p8dtu.c
platforms/astbmc/p9dsu.c
platforms/astbmc/vesnin.c
platforms/rhesus/ec/config.h
platforms/rhesus/ec/gpio.h
platforms/rhesus/gpio.c
platforms/rhesus/rhesus.c
platforms/astbmc/talos.c
platforms/astbmc/romulus.c

Signed-off-by: Stewart Smith <stewart@linux.ibm.com>
[oliver: fixed up the drift]
Signed-off-by: Oliver O'Halloran <oohall@gmail.com>
2020-03-12 20:33:18 +11:00

125 lines
3.0 KiB
C

// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
/* Copyright 2017 IBM Corp. */
#include "config.h"
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include "ccan/short_types/short_types.h"
#include "container-utils.h"
#include "container.h"
extern char *progname;
extern bool verbose, debug;
extern int wrap;
void hex_print(char *lead, unsigned char *buffer, size_t buflen)
{
unsigned int i, indent = 4;
char prelead[100];
const char *pad;
int col;
snprintf(prelead, 100, "--> %s: ", progname);
pad = (((strlen(prelead) + strlen(lead)) % 2) == 0) ? "" : " ";
wrap = ((wrap % 2) == 0) ? wrap : wrap - 1;
indent = ((indent % 2) == 0) ? indent : indent - 1;
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");
}
void verbose_print(char *lead, unsigned char *buffer, size_t buflen)
{
if (verbose)
hex_print(lead, buffer, buflen);
}
void debug_print(char *lead, unsigned char *buffer, size_t buflen)
{
if (debug)
hex_print(lead, buffer, buflen);
}
/**
* Validate hexadecimal ASCII input of a given length.
* - len is the byte len of the resulting value, not the len of the hexascii.
* - len = 0 means validate input of arbitrary length.
*/
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 * 2) || (len > (int) maxlen))
die(EX_DATAERR, "input exceeded max length: %lu", maxlen);
if (len > 0)
sprintf(multiplier, "{%d}", len * 2); // allow this (byte) len only
else
sprintf(multiplier, "+"); // unlimited
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;
}
/**
* Validate ASCII input up to a given length.
* - len is the expected len of the ascii input.
* - len = 0 means validate input of arbitrary length.
* - NOTE: not all ascii chars are allowed here.
*/
int isValidAscii(char *input, int len) {
int r;
size_t maxlen = 256; // sane limit
regex_t regexpr;
char pattern[48];
char multiplier[8];
bool result = false;
if ((strnlen(input, maxlen) > maxlen) || (len > (int) maxlen))
die(EX_DATAERR, "input exceeded max length: %lu", maxlen);
if (len > 0)
sprintf(multiplier, "{,%d}", len); // allow *up to* this len
else
sprintf(multiplier, "+"); // unlimited
sprintf(pattern, "^[a-zA-Z0-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;
}