From 7b8f6b0644e16614ebb07983ff2e8809581f8350 Mon Sep 17 00:00:00 2001 From: Amlal Date: Wed, 17 Jul 2024 16:31:17 +0200 Subject: [IMP] Move support header to BootKit. Signed-off-by: Amlal --- Boot/BootKit/BootKit.hxx | 2 +- Boot/BootKit/Support.hxx | 142 ++++++++++++++++++++++++++++++++++++++++ Boot/BootKit/Vendor/Qr.hxx | 10 +-- Boot/BootKit/Vendor/Support.hxx | 142 ---------------------------------------- 4 files changed, 149 insertions(+), 147 deletions(-) create mode 100644 Boot/BootKit/Support.hxx delete mode 100644 Boot/BootKit/Vendor/Support.hxx (limited to 'Boot/BootKit') diff --git a/Boot/BootKit/BootKit.hxx b/Boot/BootKit/BootKit.hxx index af727da1..16f81696 100644 --- a/Boot/BootKit/BootKit.hxx +++ b/Boot/BootKit/BootKit.hxx @@ -19,7 +19,7 @@ #include #include -#include +#include /***********************************************************************************/ /// Include other APIs. diff --git a/Boot/BootKit/Support.hxx b/Boot/BootKit/Support.hxx new file mode 100644 index 00000000..a12148a1 --- /dev/null +++ b/Boot/BootKit/Support.hxx @@ -0,0 +1,142 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies + +------------------------------------------- */ + +#pragma once + +/// @file Support.hxx +/// @brief Purpose of this file is to help port libs into the bootloader. + +#define cLongMax ((long)(~0UL >> 1)) +#define cLongMin (~cLongMax) + +#define SetMem(dst, c, sz) memset(dst, c, sz) +#define MoveMem(dst, src, sz) memcpy(dst, src, sz) +#define CopyMem(dst, src, sz) memcpy(dst, src, sz) +#define StrLen(src) strlen(src) + +inline int IsSpace(int c) +{ + return c == ' '; +} + +inline int StringNCompare(const char* destination, const char* source, long length) +{ + long err = 0; + + for (long i = 0UL; i < length; ++i) + { + if (source[i] != destination[i]) + ++err; + } + + return err; +} + +inline long StringToLong(const char* nptr, char** endptr, int base) +{ + const char *p = nptr, *endp; + bool is_neg = 0, overflow = 0; + + /* Need unsigned so (-cLongMin) can fit in these: */ + unsigned long n = 0UL, cutoff; + int cutlim; + + if (base < 0 || base == 1 || base > 36) + { + return 0L; + } + + endp = nptr; + + while (IsSpace(*p)) + p++; + + if (*p == '+') + { + p++; + } + else if (*p == '-') + { + is_neg = 1, p++; + } + if (*p == '0') + { + p++; + /* For strtol(" 0xZ", &endptr, 16), endptr should point to 'x'; + * pointing to ' ' or '0' is non-compliant. + * (Many implementations do this wrong.) */ + endp = p; + if (base == 16 && (*p == 'X' || *p == 'x')) + { + p++; + } + else if (base == 2 && (*p == 'B' || *p == 'b')) + { + /* C23 standard supports "0B" and "0b" prefixes. */ + p++; + } + else if (base == 0) + { + if (*p == 'X' || *p == 'x') + { + base = 16, p++; + } + else if (*p == 'B' || *p == 'b') + { + base = 2, p++; + } + else + { + base = 8; + } + } + } + else if (base == 0) + { + base = 10; + } + + cutoff = (is_neg) ? -(cLongMin / base) : cLongMax / base; + cutlim = (is_neg) ? -(cLongMin % base) : cLongMax % base; + + while (1) + { + int c; + if (*p >= 'A') + c = ((*p - 'A') & (~('a' ^ 'A'))) + 10; + else if (*p <= '9') + c = *p - '0'; + else + break; + if (c < 0 || c >= base) + break; + endp = ++p; + if (overflow) + { + /* endptr should go forward and point to the non-digit character + * (of the given base); required by ANSI standard. */ + if (endptr) + continue; + break; + } + if (n > cutoff || (n == cutoff && c > cutlim)) + { + overflow = 1; + continue; + } + n = n * base + c; + } + + if (endptr) + *endptr = (char*)endp; + + if (overflow) + { + return ((is_neg) ? cLongMin : cLongMax); + } + + return (long)((is_neg) ? -n : n); +} diff --git a/Boot/BootKit/Vendor/Qr.hxx b/Boot/BootKit/Vendor/Qr.hxx index 9f32430d..3616aaac 100644 --- a/Boot/BootKit/Vendor/Qr.hxx +++ b/Boot/BootKit/Vendor/Qr.hxx @@ -1,16 +1,18 @@ -#ifndef QR_H -#define QR_H +#ifndef QR_HXX +#define QR_HXX +extern "C" { #include #include #include +} #include #include #include #include -#include +#include #include /// @note the QR code is still code 128, it utilizes the same concept of having it's own character set. @@ -970,4 +972,4 @@ namespace Kernel::Qr using namespace qr; } // namespace Kernel::Qr -#endif +#endif // QR_HXX diff --git a/Boot/BootKit/Vendor/Support.hxx b/Boot/BootKit/Vendor/Support.hxx deleted file mode 100644 index a12148a1..00000000 --- a/Boot/BootKit/Vendor/Support.hxx +++ /dev/null @@ -1,142 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies - -------------------------------------------- */ - -#pragma once - -/// @file Support.hxx -/// @brief Purpose of this file is to help port libs into the bootloader. - -#define cLongMax ((long)(~0UL >> 1)) -#define cLongMin (~cLongMax) - -#define SetMem(dst, c, sz) memset(dst, c, sz) -#define MoveMem(dst, src, sz) memcpy(dst, src, sz) -#define CopyMem(dst, src, sz) memcpy(dst, src, sz) -#define StrLen(src) strlen(src) - -inline int IsSpace(int c) -{ - return c == ' '; -} - -inline int StringNCompare(const char* destination, const char* source, long length) -{ - long err = 0; - - for (long i = 0UL; i < length; ++i) - { - if (source[i] != destination[i]) - ++err; - } - - return err; -} - -inline long StringToLong(const char* nptr, char** endptr, int base) -{ - const char *p = nptr, *endp; - bool is_neg = 0, overflow = 0; - - /* Need unsigned so (-cLongMin) can fit in these: */ - unsigned long n = 0UL, cutoff; - int cutlim; - - if (base < 0 || base == 1 || base > 36) - { - return 0L; - } - - endp = nptr; - - while (IsSpace(*p)) - p++; - - if (*p == '+') - { - p++; - } - else if (*p == '-') - { - is_neg = 1, p++; - } - if (*p == '0') - { - p++; - /* For strtol(" 0xZ", &endptr, 16), endptr should point to 'x'; - * pointing to ' ' or '0' is non-compliant. - * (Many implementations do this wrong.) */ - endp = p; - if (base == 16 && (*p == 'X' || *p == 'x')) - { - p++; - } - else if (base == 2 && (*p == 'B' || *p == 'b')) - { - /* C23 standard supports "0B" and "0b" prefixes. */ - p++; - } - else if (base == 0) - { - if (*p == 'X' || *p == 'x') - { - base = 16, p++; - } - else if (*p == 'B' || *p == 'b') - { - base = 2, p++; - } - else - { - base = 8; - } - } - } - else if (base == 0) - { - base = 10; - } - - cutoff = (is_neg) ? -(cLongMin / base) : cLongMax / base; - cutlim = (is_neg) ? -(cLongMin % base) : cLongMax % base; - - while (1) - { - int c; - if (*p >= 'A') - c = ((*p - 'A') & (~('a' ^ 'A'))) + 10; - else if (*p <= '9') - c = *p - '0'; - else - break; - if (c < 0 || c >= base) - break; - endp = ++p; - if (overflow) - { - /* endptr should go forward and point to the non-digit character - * (of the given base); required by ANSI standard. */ - if (endptr) - continue; - break; - } - if (n > cutoff || (n == cutoff && c > cutlim)) - { - overflow = 1; - continue; - } - n = n * base + c; - } - - if (endptr) - *endptr = (char*)endp; - - if (overflow) - { - return ((is_neg) ? cLongMin : cLongMax); - } - - return (long)((is_neg) ? -n : n); -} -- cgit v1.2.3