From bf8632c0637fa16dc6743a90a7d8a30752afe3e6 Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Sun, 26 Jun 2016 05:54:21 +0200 Subject: [PATCH 01/12] LessQt, part 1 - added wrappers over Qt classes for seamless replacement if Qt is not available - added bstrlib as submodule - only UEFIExtract works with this changes for now, others will followa bit later --- .gitmodules | 3 + UEFIExtract/ffsdumper.cpp | 45 +- UEFIExtract/ffsdumper.h | 13 +- UEFIExtract/uefiextract.pro | 16 +- UEFIExtract/uefiextract_main.cpp | 33 +- common/LZMA/LzmaDecompress.c | 6 +- common/basetypes.h | 98 +- common/ffs.cpp | 104 +- common/ffs.h | 64 +- common/ffsparser.cpp | 2484 +++++++++++++++--------------- common/ffsparser.h | 139 +- common/ffsreport.cpp | 40 +- common/ffsreport.h | 12 +- common/fitparser.cpp | 84 +- common/fitparser.h | 27 +- common/me.h | 4 +- common/nvram.cpp | 117 +- common/nvram.h | 58 +- common/peimage.cpp | 34 +- common/peimage.h | 1476 +++++++++--------- common/treeitem.cpp | 21 +- common/treeitem.h | 48 +- common/treemodel.cpp | 112 +- common/treemodel.h | 84 +- common/types.cpp | 200 ++- common/types.h | 12 +- common/ubytearray.h | 67 + common/umodelindex.h | 23 + common/ustring.cpp | 25 + common/ustring.h | 34 + common/utility.cpp | 166 +- common/utility.h | 16 +- 32 files changed, 2891 insertions(+), 2774 deletions(-) create mode 100644 .gitmodules create mode 100644 common/ubytearray.h create mode 100644 common/umodelindex.h create mode 100644 common/ustring.cpp create mode 100644 common/ustring.h diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..2f51807 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "bstrlib"] + path = bstrlib + url = https://github.com/websnarf/bstrlib diff --git a/UEFIExtract/ffsdumper.cpp b/UEFIExtract/ffsdumper.cpp index 05db4a8..7e97448 100644 --- a/UEFIExtract/ffsdumper.cpp +++ b/UEFIExtract/ffsdumper.cpp @@ -22,48 +22,48 @@ FfsDumper::~FfsDumper() { } -STATUS FfsDumper::dump(const QModelIndex & root, const QString & path, const bool dumpAll, const QString & guid) +USTATUS FfsDumper::dump(const UModelIndex & root, const UString & path, const bool dumpAll, const UString & guid) { dumped = false; UINT8 result = recursiveDump(root, path, dumpAll, guid); if (result) return result; else if (!dumped) - return ERR_ITEM_NOT_FOUND; - return ERR_SUCCESS; + return U_ITEM_NOT_FOUND; + return U_SUCCESS; } -STATUS FfsDumper::recursiveDump(const QModelIndex & index, const QString & path, const bool dumpAll, const QString & guid) +USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path, const bool dumpAll, const UString & guid) { if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; QDir dir; if (guid.isEmpty() || - guidToQString(*(const EFI_GUID*)model->header(index).constData()) == guid || - guidToQString(*(const EFI_GUID*)model->header(model->findParentOfType(index, Types::File)).constData()) == guid) { + guidToUString(*(const EFI_GUID*)model->header(index).constData()) == guid || + guidToUString(*(const EFI_GUID*)model->header(model->findParentOfType(index, Types::File)).constData()) == guid) { if (dir.cd(path)) - return ERR_DIR_ALREADY_EXIST; + return U_DIR_ALREADY_EXIST; if (!dir.mkpath(path)) - return ERR_DIR_CREATE; + return U_DIR_CREATE; QFile file; if (dumpAll || model->rowCount(index) == 0) { // Dump if leaf item or dumpAll is true if (!model->header(index).isEmpty()) { - file.setFileName(QObject::tr("%1/header.bin").arg(path)); + file.setFileName(path + UString("/header.bin")); if (!file.open(QFile::WriteOnly)) - return ERR_FILE_OPEN; + return U_FILE_OPEN; file.write(model->header(index)); file.close(); } if (!model->body(index).isEmpty()) { - file.setFileName(QObject::tr("%1/body.bin").arg(path)); + file.setFileName(path + UString("/body.bin")); if (!file.open(QFile::WriteOnly)) - return ERR_FILE_OPEN; + return U_FILE_OPEN; file.write(model->body(index)); file.close(); @@ -71,14 +71,13 @@ STATUS FfsDumper::recursiveDump(const QModelIndex & index, const QString & path, } // Always dump info - QString info = QObject::tr("Type: %1\nSubtype: %2\n%3%4\n") - .arg(itemTypeToQString(model->type(index))) - .arg(itemSubtypeToQString(model->type(index), model->subtype(index))) - .arg(model->text(index).isEmpty() ? QObject::tr("") : QObject::tr("Text: %1\n").arg(model->text(index))) - .arg(model->info(index)); - file.setFileName(QObject::tr("%1/info.txt").arg(path)); + UString info = UString("Type: ") + itemTypeToUString(model->type(index)) + UString("\n") + + UString("Subtype: ") + itemSubtypeToUString(model->type(index), model->subtype(index)) + UString("\n") + + (model->text(index).isEmpty() ? UString("") : UString("Text: ") + model->text(index) + UString("\n")) + + model->info(index) + UString("\n"); + file.setFileName(path + UString("/info.txt")); if (!file.open(QFile::Text | QFile::WriteOnly)) - return ERR_FILE_OPEN; + return U_FILE_OPEN; file.write(info.toLatin1()); file.close(); @@ -87,16 +86,16 @@ STATUS FfsDumper::recursiveDump(const QModelIndex & index, const QString & path, UINT8 result; for (int i = 0; i < model->rowCount(index); i++) { - QModelIndex childIndex = index.child(i, 0); + UModelIndex childIndex = index.child(i, 0); bool useText = FALSE; if (model->type(childIndex) != Types::Volume) useText = !model->text(childIndex).isEmpty(); - QString childPath = QString("%1/%2 %3").arg(path).arg(i).arg(useText ? model->text(childIndex) : model->name(childIndex)); + UString childPath = path + usprintf("/%u ", i) + (useText ? model->text(childIndex) : model->name(childIndex)); result = recursiveDump(childIndex, childPath, dumpAll, guid); if (result) return result; } - return ERR_SUCCESS; + return U_SUCCESS; } diff --git a/UEFIExtract/ffsdumper.h b/UEFIExtract/ffsdumper.h index 11ef4e8..23481e3 100644 --- a/UEFIExtract/ffsdumper.h +++ b/UEFIExtract/ffsdumper.h @@ -14,12 +14,11 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef FFSDUMPER_H #define FFSDUMPER_H -#include -#include -#include -#include -#include +#include +#include "../common/ubytearray.h" +#include "../common/ustring.h" +#include "../common/umodelindex.h" #include "../common/basetypes.h" #include "../common/treemodel.h" #include "../common/ffs.h" @@ -30,10 +29,10 @@ public: explicit FfsDumper(TreeModel * treeModel); ~FfsDumper(); - STATUS dump(const QModelIndex & root, const QString & path, const bool dumpAll = false, const QString & guid = QString()); + USTATUS dump(const UModelIndex & root, const UString & path, const bool dumpAll = false, const UString & guid = UString()); private: - STATUS recursiveDump(const QModelIndex & root, const QString & path, const bool dumpAll, const QString & guid); + USTATUS recursiveDump(const UModelIndex & root, const UString & path, const bool dumpAll, const UString & guid); TreeModel* model; bool dumped; }; diff --git a/UEFIExtract/uefiextract.pro b/UEFIExtract/uefiextract.pro index 4cc84a8..63297d9 100644 --- a/UEFIExtract/uefiextract.pro +++ b/UEFIExtract/uefiextract.pro @@ -6,7 +6,8 @@ TEMPLATE = app CONFIG += console CONFIG -= app_bundle -SOURCES += uefiextract_main.cpp \ +SOURCES += \ + uefiextract_main.cpp \ ffsdumper.cpp \ ../common/types.cpp \ ../common/descriptor.cpp \ @@ -22,8 +23,12 @@ SOURCES += uefiextract_main.cpp \ ../common/LZMA/LzmaDecompress.c \ ../common/LZMA/SDK/C/LzmaDec.c \ ../common/Tiano/EfiTianoDecompress.c \ + ../common/ustring.cpp \ + ../bstrlib/bstrlib.c \ + ../bstrlib/bstrwrap.cpp -HEADERS += ffsdumper.h \ +HEADERS += \ + ffsdumper.h \ ../common/basetypes.h \ ../common/descriptor.h \ ../common/gbe.h \ @@ -39,5 +44,10 @@ HEADERS += ffsdumper.h \ ../common/treemodel.h \ ../common/utility.h \ ../common/LZMA/LzmaDecompress.h \ - ../common/Tiano/EfiTianoDecompress.h + ../common/Tiano/EfiTianoDecompress.h \ + ../common/umodelindex.h \ + ../common/ubytearray.h \ + ../common/ustring.h \ + ../bstrlib/bstrlib.h \ + ../bstrlib/bstrwrap.cpp diff --git a/UEFIExtract/uefiextract_main.cpp b/UEFIExtract/uefiextract_main.cpp index 6cbf3ec..37a7978 100644 --- a/UEFIExtract/uefiextract_main.cpp +++ b/UEFIExtract/uefiextract_main.cpp @@ -11,11 +11,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. */ #include -#include #include - #include +#include <../common/ustring.h> #include "../common/ffsparser.h" #include "../common/ffsreport.h" #include "../common/fitparser.h" @@ -35,51 +34,51 @@ int main(int argc, char *argv[]) if (a.arguments().length() > 1) { // Check that input file exists - QString path = a.arguments().at(1); + UString path = a.arguments().at(1); QFileInfo fileInfo(path); if (!fileInfo.exists()) - return ERR_FILE_OPEN; + return U_FILE_OPEN; // Open the input file QFile inputFile; inputFile.setFileName(path); if (!inputFile.open(QFile::ReadOnly)) - return ERR_FILE_OPEN; + return U_FILE_OPEN; // Read and close the file - QByteArray buffer = inputFile.readAll(); + UByteArray buffer = inputFile.readAll(); inputFile.close(); // Create model and ffsParser TreeModel model; FfsParser ffsParser(&model); // Parse input buffer - STATUS result = ffsParser.parse(buffer); + USTATUS result = ffsParser.parse(buffer); if (result) return result; // Show ffsParser's messages - std::vector > messages = ffsParser.getMessages(); + std::vector > messages = ffsParser.getMessages(); for (size_t i = 0; i < messages.size(); i++) { std::cout << messages[i].first.toLatin1().constData() << std::endl; } // Get last VTF - QModelIndex lastVtf = ffsParser.getLastVtf(); + UModelIndex lastVtf = ffsParser.getLastVtf(); if (lastVtf.isValid()) { // Create fitParser FitParser fitParser(&model); // Find and parse FIT table result = fitParser.parse(model.index(0, 0), lastVtf); - if (ERR_SUCCESS == result) { + if (U_SUCCESS == result) { // Show fitParser's messages - std::vector > fitMessages = fitParser.getMessages(); + std::vector > fitMessages = fitParser.getMessages(); for (size_t i = 0; i < fitMessages.size(); i++) { std::cout << fitMessages[i].first.toLatin1().constData() << std::endl; } // Show FIT table - std::vector > fitTable = fitParser.getFitTable(); + std::vector > fitTable = fitParser.getFitTable(); if (fitTable.size()) { std::cout << "-------------------------------------------------------------------" << std::endl; std::cout << " Address | Size | Ver | Type | CS " << std::endl; @@ -97,7 +96,7 @@ int main(int argc, char *argv[]) // Create ffsReport FfsReport ffsReport(&model); - std::vector report = ffsReport.generate(); + std::vector report = ffsReport.generate(); if (report.size()) { QFile file; file.setFileName(fileInfo.fileName().append(".report.txt")); @@ -114,12 +113,12 @@ int main(int argc, char *argv[]) // Dump all non-leaf elements if (a.arguments().length() == 2) { - return (ffsDumper.dump(model.index(0, 0), fileInfo.fileName().append(".dump")) != ERR_SUCCESS); + return (ffsDumper.dump(model.index(0, 0), fileInfo.fileName().append(".dump")) != U_SUCCESS); } - else if (a.arguments().length() == 3 && a.arguments().at(2) == QString("all")) { // Dump everything - return (ffsDumper.dump(model.index(0, 0), fileInfo.fileName().append(".dump"), true) != ERR_SUCCESS); + else if (a.arguments().length() == 3 && a.arguments().at(2) == UString("all")) { // Dump everything + return (ffsDumper.dump(model.index(0, 0), fileInfo.fileName().append(".dump"), true) != U_SUCCESS); } - else if (a.arguments().length() == 3 && a.arguments().at(2) == QString("none")) { // Skip dumping + else if (a.arguments().length() == 3 && a.arguments().at(2) == UString("none")) { // Skip dumping return 0; } else { // Dump specific files diff --git a/common/LZMA/LzmaDecompress.c b/common/LZMA/LzmaDecompress.c index 34e176d..1bac9d7 100644 --- a/common/LZMA/LzmaDecompress.c +++ b/common/LZMA/LzmaDecompress.c @@ -100,7 +100,7 @@ UINT32 *DestinationSize DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source); *DestinationSize = (UINT32)DecodedSize; - return ERR_SUCCESS; + return U_SUCCESS; } /* @@ -151,9 +151,9 @@ VOID *Destination ); if (LzmaResult == SZ_OK) { - return ERR_SUCCESS; + return U_SUCCESS; } else { - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; } } diff --git a/common/basetypes.h b/common/basetypes.h index 92356f3..9adac9c 100644 --- a/common/basetypes.h +++ b/common/basetypes.h @@ -17,6 +17,48 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include +typedef uint8_t USTATUS; +#define U_SUCCESS 0 +#define U_INVALID_PARAMETER 1 +#define U_BUFFER_TOO_SMALL 2 +#define U_OUT_OF_RESOURCES 3 +#define U_OUT_OF_MEMORY 4 +#define U_FILE_OPEN 5 +#define U_FILE_READ 6 +#define U_FILE_WRITE 7 +#define U_ITEM_NOT_FOUND 8 +#define U_UNKNOWN_ITEM_TYPE 9 +#define U_INVALID_FLASH_DESCRIPTOR 10 +#define U_INVALID_REGION 11 +#define U_EMPTY_REGION 12 +#define U_BIOS_REGION_NOT_FOUND 13 +#define U_VOLUMES_NOT_FOUND 14 +#define U_INVALID_VOLUME 15 +#define U_VOLUME_REVISION_NOT_SUPPORTED 16 +#define U_COMPLEX_BLOCK_MAP 17 +#define U_UNKNOWN_FFS 18 +#define U_INVALID_FILE 19 +#define U_INVALID_SECTION 20 +#define U_UNKNOWN_SECTION 21 +#define U_STANDARD_COMPRESSION_FAILED 22 +#define U_CUSTOMIZED_COMPRESSION_FAILED 23 +#define U_STANDARD_DECOMPRESSION_FAILED 24 +#define U_CUSTOMIZED_DECOMPRESSION_FAILED 25 +#define U_UNKNOWN_COMPRESSION_TYPE 26 +#define U_DEPEX_PARSE_FAILED 27 +#define U_UNKNOWN_EXTRACT_MODE 28 +#define U_UNKNOWN_REPLACE_MODE 29 +#define U_UNKNOWN_IMAGE_TYPE 30 +#define U_UNKNOWN_PE_OPTIONAL_HEADER_TYPE 31 +#define U_UNKNOWN_RELOCATION_TYPE 32 +#define U_DIR_ALREADY_EXIST 33 +#define U_DIR_CREATE 34 +#define U_TRUNCATED_IMAGE 35 +#define U_INVALID_CAPSULE 36 +#define U_STORES_NOT_FOUND 37 +#define U_NOT_IMPLEMENTED 0xFF + +// UDK porting definitions typedef uint8_t BOOLEAN; typedef int8_t INT8; typedef uint8_t UINT8; @@ -28,7 +70,7 @@ typedef int64_t INT64; typedef uint64_t UINT64; typedef char CHAR8; typedef uint16_t CHAR16; -typedef unsigned int UINTN; +typedef unsigned int UINTN; #define CONST const #define VOID void @@ -42,56 +84,14 @@ typedef unsigned int UINTN; #define FALSE ((BOOLEAN)(0==1)) #endif -typedef UINT8 STATUS; -#define ERR_SUCCESS 0 -#define ERR_INVALID_PARAMETER 1 -#define ERR_BUFFER_TOO_SMALL 2 -#define ERR_OUT_OF_RESOURCES 3 -#define ERR_OUT_OF_MEMORY 4 -#define ERR_FILE_OPEN 5 -#define ERR_FILE_READ 6 -#define ERR_FILE_WRITE 7 -#define ERR_ITEM_NOT_FOUND 8 -#define ERR_UNKNOWN_ITEM_TYPE 9 -#define ERR_INVALID_FLASH_DESCRIPTOR 10 -#define ERR_INVALID_REGION 11 -#define ERR_EMPTY_REGION 12 -#define ERR_BIOS_REGION_NOT_FOUND 13 -#define ERR_VOLUMES_NOT_FOUND 14 -#define ERR_INVALID_VOLUME 15 -#define ERR_VOLUME_REVISION_NOT_SUPPORTED 16 -#define ERR_COMPLEX_BLOCK_MAP 17 -#define ERR_UNKNOWN_FFS 18 -#define ERR_INVALID_FILE 19 -#define ERR_INVALID_SECTION 20 -#define ERR_UNKNOWN_SECTION 21 -#define ERR_STANDARD_COMPRESSION_FAILED 22 -#define ERR_CUSTOMIZED_COMPRESSION_FAILED 23 -#define ERR_STANDARD_DECOMPRESSION_FAILED 24 -#define ERR_CUSTOMIZED_DECOMPRESSION_FAILED 25 -#define ERR_UNKNOWN_COMPRESSION_TYPE 26 -#define ERR_DEPEX_PARSE_FAILED 27 -#define ERR_UNKNOWN_EXTRACT_MODE 28 -#define ERR_UNKNOWN_REPLACE_MODE 29 -#define ERR_UNKNOWN_IMAGE_TYPE 30 -#define ERR_UNKNOWN_PE_OPTIONAL_HEADER_TYPE 31 -#define ERR_UNKNOWN_RELOCATION_TYPE 32 -#define ERR_DIR_ALREADY_EXIST 33 -#define ERR_DIR_CREATE 34 -#define ERR_TRUNCATED_IMAGE 35 -#define ERR_INVALID_CAPSULE 36 -#define ERR_STORES_NOT_FOUND 37 -#define ERR_NOT_IMPLEMENTED 0xFF - -// UDK porting definitions #define IN #define OUT #define EFIAPI #define EFI_STATUS UINTN -#define EFI_SUCCESS ERR_SUCCESS -#define EFI_INVALID_PARAMETER ERR_INVALID_PARAMETER -#define EFI_OUT_OF_RESOURCES ERR_OUT_OF_RESOURCES -#define EFI_BUFFER_TOO_SMALL ERR_BUFFER_TOO_SMALL +#define EFI_SUCCESS U_SUCCESS +#define EFI_INVALID_PARAMETER U_INVALID_PARAMETER +#define EFI_OUT_OF_RESOURCES U_OUT_OF_RESOURCES +#define EFI_BUFFER_TOO_SMALL U_BUFFER_TOO_SMALL #define EFI_ERROR(X) (X) // Compression algorithms @@ -162,10 +162,6 @@ typedef struct EFI_TIME_ { #include #define ASSERT(x) assert(x) -//Hexarg macros -#define hexarg(X) arg(QString("%1").arg((X),0,16).toUpper()) -#define hexarg2(X, Y) arg(QString("%1").arg((X),(Y),16,QLatin1Char('0')).toUpper()) - // SHA256 hash size in bytes #define SHA256_HASH_SIZE 0x20 diff --git a/common/ffs.cpp b/common/ffs.cpp index a7e8ce8..29ed276 100644 --- a/common/ffs.cpp +++ b/common/ffs.cpp @@ -10,11 +10,11 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. */ -#include +#include "ustring.h" #include "ffs.h" // This is a workaround for the lack of static std::vector initializer before C++11 -const QByteArray FFSv2VolumesInt[] = { +const UByteArray FFSv2VolumesInt[] = { EFI_FIRMWARE_FILE_SYSTEM_GUID, EFI_FIRMWARE_FILE_SYSTEM2_GUID, EFI_APPLE_BOOT_VOLUME_FILE_SYSTEM_GUID, @@ -25,9 +25,9 @@ const QByteArray FFSv2VolumesInt[] = { }; // This number must be updated if the array above is grown #define FFSv2VolumesIntSize 7 -const std::vector FFSv2Volumes(FFSv2VolumesInt, FFSv2VolumesInt + FFSv2VolumesIntSize); +const std::vector FFSv2Volumes(FFSv2VolumesInt, FFSv2VolumesInt + FFSv2VolumesIntSize); // Luckily, FFSv3Volumes now only has 1 element -const std::vector FFSv3Volumes(1, EFI_FIRMWARE_FILE_SYSTEM3_GUID); +const std::vector FFSv3Volumes(1, EFI_FIRMWARE_FILE_SYSTEM3_GUID); const UINT8 ffsAlignmentTable[] = { 0, 4, 7, 9, 10, 12, 15, 16 }; @@ -44,66 +44,66 @@ UINT32 uint24ToUint32(const UINT8* ffsSize) return *(UINT32*)ffsSize & 0x00FFFFFF; } -QString guidToQString(const EFI_GUID & guid) +UString guidToUString(const EFI_GUID & guid) { - return QString("%1-%2-%3-%4%5-%6%7%8%9%10%11") - .arg(*(const UINT32*)&guid.Data[0], 8, 16, QChar('0')) - .arg(*(const UINT16*)&guid.Data[4], 4, 16, QChar('0')) - .arg(*(const UINT16*)&guid.Data[6], 4, 16, QChar('0')) - .arg(guid.Data[8], 2, 16, QChar('0')) - .arg(guid.Data[9], 2, 16, QChar('0')) - .arg(guid.Data[10], 2, 16, QChar('0')) - .arg(guid.Data[11], 2, 16, QChar('0')) - .arg(guid.Data[12], 2, 16, QChar('0')) - .arg(guid.Data[13], 2, 16, QChar('0')) - .arg(guid.Data[14], 2, 16, QChar('0')) - .arg(guid.Data[15], 2, 16, QChar('0')).toUpper(); + return usprintf("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", + *(const UINT32*)&guid.Data[0], + *(const UINT16*)&guid.Data[4], + *(const UINT16*)&guid.Data[6], + guid.Data[8], + guid.Data[9], + guid.Data[10], + guid.Data[11], + guid.Data[12], + guid.Data[13], + guid.Data[14], + guid.Data[15]); } -QString fileTypeToQString(const UINT8 type) +UString fileTypeToUString(const UINT8 type) { switch (type) { - case EFI_FV_FILETYPE_RAW: return QObject::tr("Raw"); - case EFI_FV_FILETYPE_FREEFORM: return QObject::tr("Freeform"); - case EFI_FV_FILETYPE_SECURITY_CORE: return QObject::tr("SEC core"); - case EFI_FV_FILETYPE_PEI_CORE: return QObject::tr("PEI core"); - case EFI_FV_FILETYPE_DXE_CORE: return QObject::tr("DXE core"); - case EFI_FV_FILETYPE_PEIM: return QObject::tr("PEI module"); - case EFI_FV_FILETYPE_DRIVER: return QObject::tr("DXE driver"); - case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER: return QObject::tr("Combined PEI/DXE"); - case EFI_FV_FILETYPE_APPLICATION: return QObject::tr("Application"); - case EFI_FV_FILETYPE_SMM: return QObject::tr("SMM module"); - case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE: return QObject::tr("Volume image"); - case EFI_FV_FILETYPE_COMBINED_SMM_DXE: return QObject::tr("Combined SMM/DXE"); - case EFI_FV_FILETYPE_SMM_CORE: return QObject::tr("SMM core"); - case EFI_FV_FILETYPE_PAD: return QObject::tr("Pad"); - default: return QObject::tr("Unknown"); + case EFI_FV_FILETYPE_RAW: return UString("Raw"); + case EFI_FV_FILETYPE_FREEFORM: return UString("Freeform"); + case EFI_FV_FILETYPE_SECURITY_CORE: return UString("SEC core"); + case EFI_FV_FILETYPE_PEI_CORE: return UString("PEI core"); + case EFI_FV_FILETYPE_DXE_CORE: return UString("DXE core"); + case EFI_FV_FILETYPE_PEIM: return UString("PEI module"); + case EFI_FV_FILETYPE_DRIVER: return UString("DXE driver"); + case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER: return UString("Combined PEI/DXE"); + case EFI_FV_FILETYPE_APPLICATION: return UString("Application"); + case EFI_FV_FILETYPE_SMM: return UString("SMM module"); + case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE: return UString("Volume image"); + case EFI_FV_FILETYPE_COMBINED_SMM_DXE: return UString("Combined SMM/DXE"); + case EFI_FV_FILETYPE_SMM_CORE: return UString("SMM core"); + case EFI_FV_FILETYPE_PAD: return UString("Pad"); + default: return UString("Unknown"); }; } -QString sectionTypeToQString(const UINT8 type) +UString sectionTypeToUString(const UINT8 type) { switch (type) { - case EFI_SECTION_COMPRESSION: return QObject::tr("Compressed"); - case EFI_SECTION_GUID_DEFINED: return QObject::tr("GUID defined"); - case EFI_SECTION_DISPOSABLE: return QObject::tr("Disposable"); - case EFI_SECTION_PE32: return QObject::tr("PE32 image"); - case EFI_SECTION_PIC: return QObject::tr("PIC image"); - case EFI_SECTION_TE: return QObject::tr("TE image"); - case EFI_SECTION_DXE_DEPEX: return QObject::tr("DXE dependency"); - case EFI_SECTION_VERSION: return QObject::tr("Version"); - case EFI_SECTION_USER_INTERFACE: return QObject::tr("UI"); - case EFI_SECTION_COMPATIBILITY16: return QObject::tr("16-bit image"); - case EFI_SECTION_FIRMWARE_VOLUME_IMAGE: return QObject::tr("Volume image"); - case EFI_SECTION_FREEFORM_SUBTYPE_GUID: return QObject::tr("Freeform subtype GUID"); - case EFI_SECTION_RAW: return QObject::tr("Raw"); - case EFI_SECTION_PEI_DEPEX: return QObject::tr("PEI dependency"); - case EFI_SECTION_SMM_DEPEX: return QObject::tr("SMM dependency"); - case INSYDE_SECTION_POSTCODE: return QObject::tr("Insyde postcode"); - case PHOENIX_SECTION_POSTCODE: return QObject::tr("Phoenix postcode"); - default: return QObject::tr("Unknown"); + case EFI_SECTION_COMPRESSION: return UString("Compressed"); + case EFI_SECTION_GUID_DEFINED: return UString("GUID defined"); + case EFI_SECTION_DISPOSABLE: return UString("Disposable"); + case EFI_SECTION_PE32: return UString("PE32 image"); + case EFI_SECTION_PIC: return UString("PIC image"); + case EFI_SECTION_TE: return UString("TE image"); + case EFI_SECTION_DXE_DEPEX: return UString("DXE dependency"); + case EFI_SECTION_VERSION: return UString("Version"); + case EFI_SECTION_USER_INTERFACE: return UString("UI"); + case EFI_SECTION_COMPATIBILITY16: return UString("16-bit image"); + case EFI_SECTION_FIRMWARE_VOLUME_IMAGE: return UString("Volume image"); + case EFI_SECTION_FREEFORM_SUBTYPE_GUID: return UString("Freeform subtype GUID"); + case EFI_SECTION_RAW: return UString("Raw"); + case EFI_SECTION_PEI_DEPEX: return UString("PEI dependency"); + case EFI_SECTION_SMM_DEPEX: return UString("SMM dependency"); + case INSYDE_SECTION_POSTCODE: return UString("Insyde postcode"); + case PHOENIX_SECTION_POSTCODE: return UString("Phoenix postcode"); + default: return UString("Unknown"); } } diff --git a/common/ffs.h b/common/ffs.h index ade17b3..1c12ce6 100644 --- a/common/ffs.h +++ b/common/ffs.h @@ -15,16 +15,16 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include -#include -#include +#include "ubytearray.h" +#include "ustring.h" #include "basetypes.h" // Make sure we use right packing rules #pragma pack(push,1) -extern QString guidToQString(const EFI_GUID& guid); -extern QString fileTypeToQString(const UINT8 type); -extern QString sectionTypeToQString(const UINT8 type); +extern UString guidToUString(const EFI_GUID& guid); +extern UString fileTypeToUString(const UINT8 type); +extern UString sectionTypeToUString(const UINT8 type); //***************************************************************************** // EFI Capsule @@ -43,19 +43,19 @@ typedef struct EFI_CAPSULE_HEADER_ { #define EFI_CAPSULE_HEADER_FLAG_POPULATE_SYSTEM_TABLE 0x00020000 // Standard EFI capsule GUID -const QByteArray EFI_CAPSULE_GUID +const UByteArray EFI_CAPSULE_GUID ("\xBD\x86\x66\x3B\x76\x0D\x30\x40\xB7\x0E\xB5\x51\x9E\x2F\xC5\xA0", 16); // Intel capsule GUID -const QByteArray INTEL_CAPSULE_GUID +const UByteArray INTEL_CAPSULE_GUID ("\xB9\x82\x91\x53\xB5\xAB\x91\x43\xB6\x9A\xE3\xA9\x43\xF7\x2F\xCC", 16); // Lenovo capsule GUID -const QByteArray LENOVO_CAPSULE_GUID +const UByteArray LENOVO_CAPSULE_GUID ("\xD3\xAF\x0B\xE2\x14\x99\x4F\x4F\x95\x37\x31\x29\xE0\x90\xEB\x3C", 16); // Another Lenovo capsule GUID -const QByteArray LENOVO2_CAPSULE_GUID +const UByteArray LENOVO2_CAPSULE_GUID ("\x76\xFE\xB5\x25\x43\x82\x5C\x4A\xA9\xBD\x7E\xE3\x24\x61\x98\xB5", 16); // Toshiba EFI Capsule header @@ -67,7 +67,7 @@ typedef struct TOSHIBA_CAPSULE_HEADER_ { } TOSHIBA_CAPSULE_HEADER; // Toshiba capsule GUID -const QByteArray TOSHIBA_CAPSULE_GUID +const UByteArray TOSHIBA_CAPSULE_GUID ("\x62\x70\xE0\x3B\x51\x1D\xD2\x45\x83\x2B\xF0\x93\x25\x7E\xD4\x61", 16); // AMI Aptio extended capsule header @@ -80,11 +80,11 @@ typedef struct APTIO_CAPSULE_HEADER_ { } APTIO_CAPSULE_HEADER; // AMI Aptio signed extended capsule GUID -const QByteArray APTIO_SIGNED_CAPSULE_GUID +const UByteArray APTIO_SIGNED_CAPSULE_GUID ("\x8B\xA6\x3C\x4A\x23\x77\xFB\x48\x80\x3D\x57\x8C\xC1\xFE\xC4\x4D", 16); // AMI Aptio unsigned extended capsule GUID -const QByteArray APTIO_UNSIGNED_CAPSULE_GUID +const UByteArray APTIO_UNSIGNED_CAPSULE_GUID ("\x90\xBB\xEE\x14\x0A\x89\xDB\x43\xAE\xD1\x5D\x3C\x45\x88\xA4\x18", 16); //***************************************************************************** @@ -113,38 +113,38 @@ typedef struct EFI_FIRMWARE_VOLUME_HEADER_ { } EFI_FIRMWARE_VOLUME_HEADER; // Standard file system GUIDs -const QByteArray EFI_FIRMWARE_FILE_SYSTEM_GUID +const UByteArray EFI_FIRMWARE_FILE_SYSTEM_GUID ("\xD9\x54\x93\x7A\x68\x04\x4A\x44\x81\xCE\x0B\xF6\x17\xD8\x90\xDF", 16); -const QByteArray EFI_FIRMWARE_FILE_SYSTEM2_GUID +const UByteArray EFI_FIRMWARE_FILE_SYSTEM2_GUID ("\x78\xE5\x8C\x8C\x3D\x8A\x1C\x4F\x99\x35\x89\x61\x85\xC3\x2D\xD3", 16); // Vendor-specific file system GUIDs -const QByteArray EFI_APPLE_BOOT_VOLUME_FILE_SYSTEM_GUID +const UByteArray EFI_APPLE_BOOT_VOLUME_FILE_SYSTEM_GUID ("\xAD\xEE\xAD\x04\xFF\x61\x31\x4D\xB6\xBA\x64\xF8\xBF\x90\x1F\x5A", 16); -const QByteArray EFI_APPLE_BOOT_VOLUME_FILE_SYSTEM2_GUID +const UByteArray EFI_APPLE_BOOT_VOLUME_FILE_SYSTEM2_GUID ("\x8C\x1B\x00\xBD\x71\x6A\x7B\x48\xA1\x4F\x0C\x2A\x2D\xCF\x7A\x5D", 16); // AD3FFFFF-D28B-44C4-9F13-9EA98A97F9F0 // Intel 1 -const QByteArray EFI_INTEL_FILE_SYSTEM_GUID +const UByteArray EFI_INTEL_FILE_SYSTEM_GUID ("\xFF\xFF\x3F\xAD\x8B\xD2\xC4\x44\x9F\x13\x9E\xA9\x8A\x97\xF9\xF0", 16); // D6A1CD70-4B33-4994-A6EA-375F2CCC5437 // Intel 2 -const QByteArray EFI_INTEL_FILE_SYSTEM2_GUID +const UByteArray EFI_INTEL_FILE_SYSTEM2_GUID ("\x70\xCD\xA1\xD6\x33\x4B\x94\x49\xA6\xEA\x37\x5F\x2C\xCC\x54\x37", 16); // 4F494156-AED6-4D64-A537-B8A5557BCEEC // Sony 1 -const QByteArray EFI_SONY_FILE_SYSTEM_GUID +const UByteArray EFI_SONY_FILE_SYSTEM_GUID ("\x56\x41\x49\x4F\xD6\xAE\x64\x4D\xA5\x37\xB8\xA5\x55\x7B\xCE\xEC", 16); // Vector of volume GUIDs with FFSv2-compatible files -extern const std::vector FFSv2Volumes; +extern const std::vector FFSv2Volumes; -const QByteArray EFI_FIRMWARE_FILE_SYSTEM3_GUID // 5473C07A-3DCB-4DCA-BD6F-1E9689E7349A +const UByteArray EFI_FIRMWARE_FILE_SYSTEM3_GUID // 5473C07A-3DCB-4DCA-BD6F-1E9689E7349A ("\x7A\xC0\x73\x54\xCB\x3D\xCA\x4D\xBD\x6F\x1E\x96\x89\xE7\x34\x9A", 16); // Vector of volume GUIDs with FFSv3-compatible files -extern const std::vector FFSv3Volumes; +extern const std::vector FFSv3Volumes; // Firmware volume signature -const QByteArray EFI_FV_SIGNATURE("_FVH", 4); +const UByteArray EFI_FV_SIGNATURE("_FVH", 4); #define EFI_FV_SIGNATURE_OFFSET 0x28 // Firmware volume attributes @@ -338,19 +338,19 @@ extern const UINT8 ffsAlignmentTable[]; #define EFI_FILE_HEADER_INVALID 0x20 // PEI apriori file -const QByteArray EFI_PEI_APRIORI_FILE_GUID +const UByteArray EFI_PEI_APRIORI_FILE_GUID ("\x0A\xCC\x45\x1B\x6A\x15\x8A\x42\xAF\x62\x49\x86\x4D\xA0\xE6\xE6", 16); // DXE apriori file -const QByteArray EFI_DXE_APRIORI_FILE_GUID +const UByteArray EFI_DXE_APRIORI_FILE_GUID ("\xE7\x0E\x51\xFC\xDC\xFF\xD4\x11\xBD\x41\x00\x80\xC7\x3C\x88\x81", 16); // Volume top file -const QByteArray EFI_FFS_VOLUME_TOP_FILE_GUID +const UByteArray EFI_FFS_VOLUME_TOP_FILE_GUID ("\x2E\x06\xA0\x1B\x79\xC7\x82\x45\x85\x66\x33\x6A\xE8\xF7\x8F\x09", 16); // Pad file GUID -const QByteArray EFI_FFS_PAD_FILE_GUID +const UByteArray EFI_FFS_PAD_FILE_GUID ("\x85\x65\x53\xE4\x09\x79\x60\x4A\xB5\xC6\xEC\xDE\xA6\xEB\xFB\x54", 16); // FFS size conversion routines @@ -445,16 +445,16 @@ typedef struct EFI_GUID_DEFINED_SECTION_APPLE_ { #define EFI_GUIDED_SECTION_AUTH_STATUS_VALID 0x02 // GUIDs of GUID-defined sections -const QByteArray EFI_GUIDED_SECTION_CRC32 // FC1BCDB0-7D31-49AA-936A-A4600D9DD083 +const UByteArray EFI_GUIDED_SECTION_CRC32 // FC1BCDB0-7D31-49AA-936A-A4600D9DD083 ("\xB0\xCD\x1B\xFC\x31\x7D\xAA\x49\x93\x6A\xA4\x60\x0D\x9D\xD0\x83", 16); -const QByteArray EFI_GUIDED_SECTION_TIANO // A31280AD-481E-41B6-95E8-127F4C984779 +const UByteArray EFI_GUIDED_SECTION_TIANO // A31280AD-481E-41B6-95E8-127F4C984779 ("\xAD\x80\x12\xA3\x1E\x48\xB6\x41\x95\xE8\x12\x7F\x4C\x98\x47\x79", 16); -const QByteArray EFI_GUIDED_SECTION_LZMA // EE4E5898-3914-4259-9D6E-DC7BD79403CF +const UByteArray EFI_GUIDED_SECTION_LZMA // EE4E5898-3914-4259-9D6E-DC7BD79403CF ("\x98\x58\x4E\xEE\x14\x39\x59\x42\x9D\x6E\xDC\x7B\xD7\x94\x03\xCF", 16); -const QByteArray EFI_FIRMWARE_CONTENTS_SIGNED_GUID // 0F9D89E8-9259-4F76-A5AF-0C89E34023DF +const UByteArray EFI_FIRMWARE_CONTENTS_SIGNED_GUID // 0F9D89E8-9259-4F76-A5AF-0C89E34023DF ("\xE8\x89\x9D\x0F\x59\x92\x76\x4F\xA5\xAF\x0C\x89\xE3\x40\x23\xDF", 16); //#define WIN_CERT_TYPE_PKCS_SIGNED_DATA 0x0002 @@ -474,7 +474,7 @@ typedef struct WIN_CERTIFICATE_UEFI_GUID_ { } WIN_CERTIFICATE_UEFI_GUID; // WIN_CERTIFICATE_UEFI_GUID.CertType -const QByteArray EFI_CERT_TYPE_RSA2048_SHA256_GUID +const UByteArray EFI_CERT_TYPE_RSA2048_SHA256_GUID ("\x14\x74\x71\xA7\x16\xC6\x77\x49\x94\x20\x84\x47\x12\xA7\x35\xBF"); // WIN_CERTIFICATE_UEFI_GUID.CertData diff --git a/common/ffsparser.cpp b/common/ffsparser.cpp index 88810bd..f3880e7 100644 --- a/common/ffsparser.cpp +++ b/common/ffsparser.cpp @@ -20,15 +20,15 @@ struct REGION_INFO { UINT32 offset; UINT32 length; UINT8 type; - QByteArray data; + UByteArray data; friend bool operator< (const REGION_INFO & lhs, const REGION_INFO & rhs){ return lhs.offset < rhs.offset; } }; // Firmware image parsing functions -STATUS FfsParser::parse(const QByteArray & buffer) +USTATUS FfsParser::parse(const UByteArray & buffer) { - QModelIndex root; - STATUS result = performFirstPass(buffer, root); + UModelIndex root; + USTATUS result = performFirstPass(buffer, root); addOffsetsRecursive(root); if (result) return result; @@ -36,20 +36,20 @@ STATUS FfsParser::parse(const QByteArray & buffer) if (lastVtf.isValid()) result = performSecondPass(root); else - msg(QObject::tr("parse: not a single Volume Top File is found, the image may be corrupted")); + msg(("parse: not a single Volume Top File is found, the image may be corrupted")); return result; } -STATUS FfsParser::performFirstPass(const QByteArray & buffer, QModelIndex & index) +USTATUS FfsParser::performFirstPass(const UByteArray & buffer, UModelIndex & index) { // Reset capsule offset fixup value capsuleOffsetFixup = 0; // Check buffer size to be more than or equal to size of EFI_CAPSULE_HEADER if ((UINT32)buffer.size() <= sizeof(EFI_CAPSULE_HEADER)) { - msg(QObject::tr("performFirstPass: image file is smaller than minimum size of %1h (%2) bytes").hexarg(sizeof(EFI_CAPSULE_HEADER)).arg(sizeof(EFI_CAPSULE_HEADER))); - return ERR_INVALID_PARAMETER; + msg(UString("performFirstPass: image file is smaller than minimum size of 1Ch (28) bytes")); + return U_INVALID_PARAMETER; } UINT32 capsuleHeaderSize = 0; @@ -63,32 +63,34 @@ STATUS FfsParser::performFirstPass(const QByteArray & buffer, QModelIndex & inde // Check sanity of HeaderSize and CapsuleImageSize values if (capsuleHeader->HeaderSize == 0 || capsuleHeader->HeaderSize > (UINT32)buffer.size() || capsuleHeader->HeaderSize > capsuleHeader->CapsuleImageSize) { - msg(QObject::tr("performFirstPass: UEFI capsule header size of %1h (%2) bytes is invalid") - .hexarg(capsuleHeader->HeaderSize).arg(capsuleHeader->HeaderSize)); - return ERR_INVALID_CAPSULE; + msg(usprintf("performFirstPass: UEFI capsule header size of %Xh (%u) bytes is invalid", + capsuleHeader->HeaderSize, + capsuleHeader->HeaderSize)); + return U_INVALID_CAPSULE; } if (capsuleHeader->CapsuleImageSize == 0 || capsuleHeader->CapsuleImageSize > (UINT32)buffer.size()) { - msg(QObject::tr("performFirstPass: UEFI capsule image size of %1h (%2) bytes is invalid") - .hexarg(capsuleHeader->CapsuleImageSize).arg(capsuleHeader->CapsuleImageSize)); - return ERR_INVALID_CAPSULE; + msg(usprintf("performFirstPass: UEFI capsule image size of %Xh (%u) bytes is invalid", + capsuleHeader->CapsuleImageSize, + capsuleHeader->CapsuleImageSize)); + return U_INVALID_CAPSULE; } capsuleHeaderSize = capsuleHeader->HeaderSize; - QByteArray header = buffer.left(capsuleHeaderSize); - QByteArray body = buffer.mid(capsuleHeaderSize); - QString name = QObject::tr("UEFI capsule"); - QString info = QObject::tr("Capsule GUID: %1\nFull size: %2h (%3)\nHeader size: %4h (%5)\nImage size: %6h (%7)\nFlags: %8h") - .arg(guidToQString(capsuleHeader->CapsuleGuid)) - .hexarg(buffer.size()).arg(buffer.size()) - .hexarg(capsuleHeaderSize).arg(capsuleHeaderSize) - .hexarg(capsuleHeader->CapsuleImageSize - capsuleHeaderSize).arg(capsuleHeader->CapsuleImageSize - capsuleHeaderSize) - .hexarg2(capsuleHeader->Flags, 8); + UByteArray header = buffer.left(capsuleHeaderSize); + UByteArray body = buffer.mid(capsuleHeaderSize); + UString name("UEFI capsule"); + UString info = UString("Capsule GUID: ") + guidToUString(capsuleHeader->CapsuleGuid) + + usprintf("\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nImage size: %Xh (%u)\nFlags: %08Xh", + buffer.size(), buffer.size(), + capsuleHeaderSize, capsuleHeaderSize, + capsuleHeader->CapsuleImageSize - capsuleHeaderSize, capsuleHeader->CapsuleImageSize - capsuleHeaderSize, + capsuleHeader->Flags); // Set capsule offset fixup for correct volume allignment warnings capsuleOffsetFixup = capsuleHeaderSize; // Add tree item - index = model->addItem(Types::Capsule, Subtypes::UefiCapsule, name, QString(), info, header, body, QByteArray(), true); + index = model->addItem(Types::Capsule, Subtypes::UefiCapsule, name, UString(), info, header, body, UByteArray(), true); } // Check buffer for being Toshiba capsule header else if (buffer.startsWith(TOSHIBA_CAPSULE_GUID)) { @@ -97,40 +99,40 @@ STATUS FfsParser::performFirstPass(const QByteArray & buffer, QModelIndex & inde // Check sanity of HeaderSize and FullSize values if (capsuleHeader->HeaderSize == 0 || capsuleHeader->HeaderSize > (UINT32)buffer.size() || capsuleHeader->HeaderSize > capsuleHeader->FullSize) { - msg(QObject::tr("performFirstPass: Toshiba capsule header size of %1h (%2) bytes is invalid") - .hexarg(capsuleHeader->HeaderSize).arg(capsuleHeader->HeaderSize)); - return ERR_INVALID_CAPSULE; + msg(usprintf("performFirstPass: Toshiba capsule header size of %Xh (%u) bytes is invalid", + capsuleHeader->HeaderSize, capsuleHeader->HeaderSize)); + return U_INVALID_CAPSULE; } if (capsuleHeader->FullSize == 0 || capsuleHeader->FullSize > (UINT32)buffer.size()) { - msg(QObject::tr("performFirstPass: Toshiba capsule full size of %1h (%2) bytes is invalid") - .hexarg(capsuleHeader->FullSize).arg(capsuleHeader->FullSize)); - return ERR_INVALID_CAPSULE; + msg(usprintf("performFirstPass: Toshiba capsule full size of %Xh (%u) bytes is invalid", + capsuleHeader->FullSize, capsuleHeader->FullSize)); + return U_INVALID_CAPSULE; } capsuleHeaderSize = capsuleHeader->HeaderSize; - QByteArray header = buffer.left(capsuleHeaderSize); - QByteArray body = buffer.right(buffer.size() - capsuleHeaderSize); - QString name = QObject::tr("Toshiba capsule"); - QString info = QObject::tr("Capsule GUID: %1\nFull size: %2h (%3)\nHeader size: %4h (%5)\nImage size: %6h (%7)\nFlags: %8h") - .arg(guidToQString(capsuleHeader->CapsuleGuid)) - .hexarg(buffer.size()).arg(buffer.size()) - .hexarg(capsuleHeaderSize).arg(capsuleHeaderSize) - .hexarg(capsuleHeader->FullSize - capsuleHeaderSize).arg(capsuleHeader->FullSize - capsuleHeaderSize) - .hexarg2(capsuleHeader->Flags, 8); + UByteArray header = buffer.left(capsuleHeaderSize); + UByteArray body = buffer.right(buffer.size() - capsuleHeaderSize); + UString name("Toshiba capsule"); + UString info = UString("Capsule GUID: ") + guidToUString(capsuleHeader->CapsuleGuid) + + usprintf("\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nImage size: %Xh (%u)\nFlags: %08Xh", + buffer.size(), buffer.size(), + capsuleHeaderSize, capsuleHeaderSize, + capsuleHeader->FullSize - capsuleHeaderSize, capsuleHeader->FullSize - capsuleHeaderSize, + capsuleHeader->Flags); // Set capsule offset fixup for correct volume allignment warnings capsuleOffsetFixup = capsuleHeaderSize; // Add tree item - index = model->addItem(Types::Capsule, Subtypes::ToshibaCapsule, name, QString(), info, header, body, QByteArray(), true); + index = model->addItem(Types::Capsule, Subtypes::ToshibaCapsule, name, UString(), info, header, body, UByteArray(), true); } // Check buffer for being extended Aptio capsule header else if (buffer.startsWith(APTIO_SIGNED_CAPSULE_GUID) || buffer.startsWith(APTIO_UNSIGNED_CAPSULE_GUID)) { bool signedCapsule = buffer.startsWith(APTIO_SIGNED_CAPSULE_GUID); if ((UINT32)buffer.size() <= sizeof(APTIO_CAPSULE_HEADER)) { - msg(QObject::tr("performFirstPass: AMI capsule image file is smaller than minimum size of %1h (%2) bytes").hexarg(sizeof(APTIO_CAPSULE_HEADER)).arg(sizeof(APTIO_CAPSULE_HEADER))); - return ERR_INVALID_PARAMETER; + msg(UString("performFirstPass: AMI capsule image file is smaller than minimum size of 20h (32) bytes")); + return U_INVALID_PARAMETER; } // Get info @@ -138,50 +140,53 @@ STATUS FfsParser::performFirstPass(const QByteArray & buffer, QModelIndex & inde // Check sanity of RomImageOffset and CapsuleImageSize values if (capsuleHeader->RomImageOffset == 0 || capsuleHeader->RomImageOffset > (UINT32)buffer.size() || capsuleHeader->RomImageOffset > capsuleHeader->CapsuleHeader.CapsuleImageSize) { - msg(QObject::tr("performFirstPass: AMI capsule image offset of %1h (%2) bytes is invalid").hexarg(capsuleHeader->RomImageOffset).arg(capsuleHeader->RomImageOffset)); - return ERR_INVALID_CAPSULE; + msg(usprintf("performFirstPass: AMI capsule image offset of %Xh (%u) bytes is invalid", + capsuleHeader->RomImageOffset, capsuleHeader->RomImageOffset)); + return U_INVALID_CAPSULE; } if (capsuleHeader->CapsuleHeader.CapsuleImageSize == 0 || capsuleHeader->CapsuleHeader.CapsuleImageSize > (UINT32)buffer.size()) { - msg(QObject::tr("performFirstPass: AMI capsule image size of %1h (%2) bytes is invalid").hexarg(capsuleHeader->CapsuleHeader.CapsuleImageSize).arg(capsuleHeader->CapsuleHeader.CapsuleImageSize)); - return ERR_INVALID_CAPSULE; + msg(usprintf("performFirstPass: AMI capsule image size of %Xh (%u) bytes is invalid", + capsuleHeader->CapsuleHeader.CapsuleImageSize, + capsuleHeader->CapsuleHeader.CapsuleImageSize)); + return U_INVALID_CAPSULE; } capsuleHeaderSize = capsuleHeader->RomImageOffset; - QByteArray header = buffer.left(capsuleHeaderSize); - QByteArray body = buffer.mid(capsuleHeaderSize); - QString name = QObject::tr("AMI Aptio capsule"); - QString info = QObject::tr("Capsule GUID: %1\nFull size: %2h (%3)\nHeader size: %4h (%5)\nImage size: %6h (%7)\nFlags: %8h") - .arg(guidToQString(capsuleHeader->CapsuleHeader.CapsuleGuid)) - .hexarg(buffer.size()).arg(buffer.size()) - .hexarg(capsuleHeaderSize).arg(capsuleHeaderSize) - .hexarg(capsuleHeader->CapsuleHeader.CapsuleImageSize - capsuleHeaderSize).arg(capsuleHeader->CapsuleHeader.CapsuleImageSize - capsuleHeaderSize) - .hexarg2(capsuleHeader->CapsuleHeader.Flags, 8); + UByteArray header = buffer.left(capsuleHeaderSize); + UByteArray body = buffer.mid(capsuleHeaderSize); + UString name("AMI Aptio capsule"); + UString info = UString("Capsule GUID: ") + guidToUString(capsuleHeader->CapsuleHeader.CapsuleGuid) + + usprintf("\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nImage size: %Xh (%u)\nFlags: %08Xh", + buffer.size(), buffer.size(), + capsuleHeaderSize, capsuleHeaderSize, + capsuleHeader->CapsuleHeader.CapsuleImageSize - capsuleHeaderSize, capsuleHeader->CapsuleHeader.CapsuleImageSize - capsuleHeaderSize, + capsuleHeader->CapsuleHeader.Flags); // Set capsule offset fixup for correct volume allignment warnings capsuleOffsetFixup = capsuleHeaderSize; // Add tree item - index = model->addItem(Types::Capsule, signedCapsule ? Subtypes::AptioSignedCapsule : Subtypes::AptioUnsignedCapsule, name, QString(), info, header, body, QByteArray(), true); + index = model->addItem(Types::Capsule, signedCapsule ? Subtypes::AptioSignedCapsule : Subtypes::AptioUnsignedCapsule, name, UString(), info, header, body, UByteArray(), true); // Show message about possible Aptio signature break if (signedCapsule) { - msg(QObject::tr("performFirstPass: Aptio capsule signature may become invalid after image modifications"), index); + msg(UString("performFirstPass: Aptio capsule signature may become invalid after image modifications"), index); } } // Skip capsule header to have flash chip image - QByteArray flashImage = buffer.mid(capsuleHeaderSize); + UByteArray flashImage = buffer.mid(capsuleHeaderSize); // Check for Intel flash descriptor presence const FLASH_DESCRIPTOR_HEADER* descriptorHeader = (const FLASH_DESCRIPTOR_HEADER*)flashImage.constData(); // Check descriptor signature - STATUS result; + USTATUS result; if (descriptorHeader->Signature == FLASH_DESCRIPTOR_SIGNATURE) { // Parse as Intel image - QModelIndex imageIndex; + UModelIndex imageIndex; result = parseIntelImage(flashImage, capsuleHeaderSize, index, imageIndex); - if (result != ERR_INVALID_FLASH_DESCRIPTOR) { + if (result != U_INVALID_FLASH_DESCRIPTOR) { if (!index.isValid()) index = imageIndex; return result; @@ -189,15 +194,15 @@ STATUS FfsParser::performFirstPass(const QByteArray & buffer, QModelIndex & inde } // Get info - QString name = QObject::tr("UEFI image"); - QString info = QObject::tr("Full size: %1h (%2)").hexarg(flashImage.size()).arg(flashImage.size()); + UString name("UEFI image"); + UString info = usprintf("Full size: %Xh (%u)", flashImage.size(), flashImage.size()); // Construct parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); pdata.offset = capsuleHeaderSize; // Add tree item - QModelIndex biosIndex = model->addItem(Types::Image, Subtypes::UefiImage, name, QString(), info, QByteArray(), flashImage, QByteArray(), true, parsingDataToQByteArray(pdata), index); + UModelIndex biosIndex = model->addItem(Types::Image, Subtypes::UefiImage, name, UString(), info, UByteArray(), flashImage, UByteArray(), true, parsingDataToUByteArray(pdata), index); // Parse the image result = parseRawArea(biosIndex); @@ -206,22 +211,22 @@ STATUS FfsParser::performFirstPass(const QByteArray & buffer, QModelIndex & inde return result; } -STATUS FfsParser::parseIntelImage(const QByteArray & intelImage, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseIntelImage(const UByteArray & intelImage, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { // Sanity check if (intelImage.isEmpty()) return EFI_INVALID_PARAMETER; // Get parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Store the beginning of descriptor as descriptor base address const UINT8* descriptor = (const UINT8*)intelImage.constData(); // Check for buffer size to be greater or equal to descriptor region size if (intelImage.size() < FLASH_DESCRIPTOR_SIZE) { - msg(QObject::tr("parseIntelImage: input file is smaller than minimum descriptor size of %1h (%2) bytes").hexarg(FLASH_DESCRIPTOR_SIZE).arg(FLASH_DESCRIPTOR_SIZE)); - return ERR_INVALID_FLASH_DESCRIPTOR; + msg(usprintf("parseIntelImage: input file is smaller than minimum descriptor size of %X (%u) bytes", FLASH_DESCRIPTOR_SIZE, FLASH_DESCRIPTOR_SIZE)); + return U_INVALID_FLASH_DESCRIPTOR; } // Parse descriptor map @@ -232,17 +237,17 @@ STATUS FfsParser::parseIntelImage(const QByteArray & intelImage, const UINT32 pa if (descriptorMap->MasterBase > FLASH_DESCRIPTOR_MAX_BASE || descriptorMap->MasterBase == descriptorMap->RegionBase || descriptorMap->MasterBase == descriptorMap->ComponentBase) { - msg(QObject::tr("parseIntelImage: invalid descriptor master base %1h").hexarg2(descriptorMap->MasterBase, 2)); - return ERR_INVALID_FLASH_DESCRIPTOR; + msg(usprintf("parseIntelImage: invalid descriptor master base %02Xh", descriptorMap->MasterBase)); + return U_INVALID_FLASH_DESCRIPTOR; } if (descriptorMap->RegionBase > FLASH_DESCRIPTOR_MAX_BASE || descriptorMap->RegionBase == descriptorMap->ComponentBase) { - msg(QObject::tr("parseIntelImage: invalid descriptor region base %1h").hexarg2(descriptorMap->RegionBase, 2)); - return ERR_INVALID_FLASH_DESCRIPTOR; + msg(usprintf("parseIntelImage: invalid descriptor region base %02Xh", descriptorMap->RegionBase)); + return U_INVALID_FLASH_DESCRIPTOR; } if (descriptorMap->ComponentBase > FLASH_DESCRIPTOR_MAX_BASE) { - msg(QObject::tr("parseIntelImage: invalid descriptor component base %1h").hexarg2(descriptorMap->ComponentBase, 2)); - return ERR_INVALID_FLASH_DESCRIPTOR; + msg(usprintf("parseIntelImage: invalid descriptor component base %02Xh", descriptorMap->ComponentBase)); + return U_INVALID_FLASH_DESCRIPTOR; } const FLASH_DESCRIPTOR_REGION_SECTION* regionSection = (const FLASH_DESCRIPTOR_REGION_SECTION*)calculateAddress8(descriptor, descriptorMap->RegionBase); @@ -255,8 +260,8 @@ STATUS FfsParser::parseIntelImage(const QByteArray & intelImage, const UINT32 pa else if (componentSection->FlashParameters.ReadClockFrequency == FLASH_FREQUENCY_17MHZ) // Skylake+ descriptor descriptorVersion = 2; else { - msg(QObject::tr("parseIntelImage: unknown descriptor version with ReadClockFrequency %1h").hexarg(componentSection->FlashParameters.ReadClockFrequency)); - return ERR_INVALID_FLASH_DESCRIPTOR; + msg(usprintf("parseIntelImage: unknown descriptor version with ReadClockFrequency %02Xh", componentSection->FlashParameters.ReadClockFrequency)); + return U_INVALID_FLASH_DESCRIPTOR; } // Regions @@ -286,8 +291,8 @@ STATUS FfsParser::parseIntelImage(const QByteArray & intelImage, const UINT32 pa // Check for Gigabyte specific descriptor map if (bios.length == (UINT32)intelImage.size()) { if (!me.offset) { - msg(QObject::tr("parseIntelImage: can't determine BIOS region start from Gigabyte-specific descriptor")); - return ERR_INVALID_FLASH_DESCRIPTOR; + msg(("parseIntelImage: can't determine BIOS region start from Gigabyte-specific descriptor")); + return U_INVALID_FLASH_DESCRIPTOR; } // Use ME region end as BIOS region offset bios.offset = me.offset + me.length; @@ -302,8 +307,8 @@ STATUS FfsParser::parseIntelImage(const QByteArray & intelImage, const UINT32 pa regions.push_back(bios); } else { - msg(QObject::tr("parseIntelImage: descriptor parsing failed, BIOS region not found in descriptor")); - return ERR_INVALID_FLASH_DESCRIPTOR; + msg(("parseIntelImage: descriptor parsing failed, BIOS region not found in descriptor")); + return U_INVALID_FLASH_DESCRIPTOR; } // GbE region @@ -403,8 +408,10 @@ STATUS FfsParser::parseIntelImage(const QByteArray & intelImage, const UINT32 pa REGION_INFO region; // Check intersection with the descriptor if (regions.front().offset < FLASH_DESCRIPTOR_SIZE) { - msg(QObject::tr("parseIntelImage: %1 region has intersection with flash descriptor").arg(itemSubtypeToQString(Types::Region, regions.front().type)), index); - return ERR_INVALID_FLASH_DESCRIPTOR; + msg(UString("parseIntelImage: ") + itemSubtypeToUString(Types::Region, regions.front().type) + + UString(" region has intersection with flash descriptor"), + index); + return U_INVALID_FLASH_DESCRIPTOR; } // Check for padding between descriptor and the first region else if (regions.front().offset > FLASH_DESCRIPTOR_SIZE) { @@ -419,17 +426,18 @@ STATUS FfsParser::parseIntelImage(const QByteArray & intelImage, const UINT32 pa UINT32 previousRegionEnd = regions[i-1].offset + regions[i-1].length; // Check that current region is fully present in the image if (regions[i].offset + regions[i].length > (UINT32)intelImage.size()) { - msg(QObject::tr("parseIntelImage: %1 region is located outside of opened image, if your system uses dual-chip storage, please append another part to the opened image") - .arg(itemSubtypeToQString(Types::Region, regions[i].type)), index); - return ERR_TRUNCATED_IMAGE; + msg(UString("parseIntelImage: ") + itemSubtypeToUString(Types::Region, regions[i].type) + + UString(" region is located outside of opened image, if your system uses dual-chip storage, please append another part to the opened image"), + index); + return U_TRUNCATED_IMAGE; } // Check for intersection with previous region if (regions[i].offset < previousRegionEnd) { - msg(QObject::tr("parseIntelImage: %1 region has intersection with %2 region") - .arg(itemSubtypeToQString(Types::Region, regions[i].type)) - .arg(itemSubtypeToQString(Types::Region, regions[i-1].type)), index); - return ERR_INVALID_FLASH_DESCRIPTOR; + msg(UString("parseIntelImage: ") + itemSubtypeToUString(Types::Region, regions[i].type) + + UString(" region has intersection with ") + itemSubtypeToUString(Types::Region, regions[i - 1].type) +UString(" region"), + index); + return U_INVALID_FLASH_DESCRIPTOR; } // Check for padding between current and previous regions else if (regions[i].offset > previousRegionEnd) { @@ -454,114 +462,109 @@ STATUS FfsParser::parseIntelImage(const QByteArray & intelImage, const UINT32 pa // Region map is consistent // Intel image - QString name = QObject::tr("Intel image"); - QString info = QObject::tr("Full size: %1h (%2)\nFlash chips: %3\nRegions: %4\nMasters: %5\nPCH straps: %6\nPROC straps: %7") - .hexarg(intelImage.size()).arg(intelImage.size()) - .arg(descriptorMap->NumberOfFlashChips + 1) // - .arg(descriptorMap->NumberOfRegions + 1) // Zero-based numbers in storage - .arg(descriptorMap->NumberOfMasters + 1) // - .arg(descriptorMap->NumberOfPchStraps) - .arg(descriptorMap->NumberOfProcStraps); + UString name("Intel image"); + UString info = usprintf("Full size: %Xh (%u)\nFlash chips: %u\nRegions: %u\nMasters: %u\nPCH straps: %u\nPROC straps: %u", + intelImage.size(), intelImage.size(), + descriptorMap->NumberOfFlashChips + 1, // + descriptorMap->NumberOfRegions + 1, // Zero-based numbers in storage + descriptorMap->NumberOfMasters + 1, // + descriptorMap->NumberOfPchStraps, + descriptorMap->NumberOfProcStraps); // Construct parsing data pdata.offset = parentOffset; // Add Intel image tree item - index = model->addItem(Types::Image, Subtypes::IntelImage, name, QString(), info, QByteArray(), intelImage, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Image, Subtypes::IntelImage, name, UString(), info, UByteArray(), intelImage, UByteArray(), true, parsingDataToUByteArray(pdata), parent); // Descriptor // Get descriptor info - QByteArray body = intelImage.left(FLASH_DESCRIPTOR_SIZE); - name = QObject::tr("Descriptor region"); - info = QObject::tr("Full size: %1h (%2)").hexarg(FLASH_DESCRIPTOR_SIZE).arg(FLASH_DESCRIPTOR_SIZE); + UByteArray body = intelImage.left(FLASH_DESCRIPTOR_SIZE); + name = UString("Descriptor region"); + info = usprintf("Full size: %Xh (%u)", FLASH_DESCRIPTOR_SIZE, FLASH_DESCRIPTOR_SIZE); // Add offsets of actual regions for (size_t i = 0; i < regions.size(); i++) { if (regions[i].type != Subtypes::ZeroPadding && regions[i].type != Subtypes::OnePadding && regions[i].type != Subtypes::DataPadding) - info += QObject::tr("\n%1 region offset: %2h").arg(itemSubtypeToQString(Types::Region, regions[i].type)).hexarg(regions[i].offset + parentOffset); + info += itemSubtypeToUString(Types::Region, regions[i].type).prepend("\n") + + usprintf(" region offset: %Xh", itemSubtypeToUString(Types::Region, regions[i].type), regions[i].offset + parentOffset); } // Region access settings if (descriptorVersion == 1) { const FLASH_DESCRIPTOR_MASTER_SECTION* masterSection = (const FLASH_DESCRIPTOR_MASTER_SECTION*)calculateAddress8(descriptor, descriptorMap->MasterBase); - info += QObject::tr("\nRegion access settings:"); - info += QObject::tr("\nBIOS: %1h %2h ME: %3h %4h\nGbE: %5h %6h") - .hexarg2(masterSection->BiosRead, 2) - .hexarg2(masterSection->BiosWrite, 2) - .hexarg2(masterSection->MeRead, 2) - .hexarg2(masterSection->MeWrite, 2) - .hexarg2(masterSection->GbeRead, 2) - .hexarg2(masterSection->GbeWrite, 2); + info += UString("\nRegion access settings:"); + info += usprintf("\nBIOS: %02Xh %02Xh ME: %02Xh %02Xh\nGbE: %02Xh %02Xh", + masterSection->BiosRead, + masterSection->BiosWrite, + masterSection->MeRead, + masterSection->MeWrite, + masterSection->GbeRead, + masterSection->GbeWrite); // BIOS access table - info += QObject::tr("\nBIOS access table:"); - info += QObject::tr("\n Read Write"); - info += QObject::tr("\nDesc %1 %2") - .arg(masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_DESC ? "Yes " : "No ") - .arg(masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_DESC ? "Yes " : "No "); - info += QObject::tr("\nBIOS Yes Yes"); - info += QObject::tr("\nME %1 %2") - .arg(masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_ME ? "Yes " : "No ") - .arg(masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_ME ? "Yes " : "No "); - info += QObject::tr("\nGbE %1 %2") - .arg(masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_GBE ? "Yes " : "No ") - .arg(masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_GBE ? "Yes " : "No "); - info += QObject::tr("\nPDR %1 %2") - .arg(masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_PDR ? "Yes " : "No ") - .arg(masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_PDR ? "Yes " : "No "); + info += UString("\nBIOS access table:") + + UString("\n Read Write") + + usprintf("\nDesc %s %s", masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_DESC ? "Yes " : "No ", + masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_DESC ? "Yes " : "No "); + info += UString("\nBIOS Yes Yes") + + usprintf("\nME %s %s", masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_ME ? "Yes " : "No ", + masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_ME ? "Yes " : "No "); + info += usprintf("\nGbE %s %s", masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_GBE ? "Yes " : "No ", + masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_GBE ? "Yes " : "No "); + info += usprintf("\nPDR %s %s", masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_PDR ? "Yes " : "No ", + masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_PDR ? "Yes " : "No "); } else if (descriptorVersion == 2) { const FLASH_DESCRIPTOR_MASTER_SECTION_V2* masterSection = (const FLASH_DESCRIPTOR_MASTER_SECTION_V2*)calculateAddress8(descriptor, descriptorMap->MasterBase); - info += QObject::tr("\nRegion access settings:"); - info += QObject::tr("\nBIOS: %1h %2h ME: %3h %4h\nGbE: %5h %6h EC: %7h %8h") - .hexarg2(masterSection->BiosRead, 3) - .hexarg2(masterSection->BiosWrite, 3) - .hexarg2(masterSection->MeRead, 3) - .hexarg2(masterSection->MeWrite, 3) - .hexarg2(masterSection->GbeRead, 3) - .hexarg2(masterSection->GbeWrite, 3) - .hexarg2(masterSection->EcRead, 3) - .hexarg2(masterSection->EcWrite, 3); + info += ("\nRegion access settings:"); + info += ("\nBIOS: %03Xh %03Xh ME: %03Xh %03Xh\nGbE: %03Xh %03Xh EC: %03Xh %03Xh", + masterSection->BiosRead, + masterSection->BiosWrite, + masterSection->MeRead, + masterSection->MeWrite, + masterSection->GbeRead, + masterSection->GbeWrite, + masterSection->EcRead, + masterSection->EcWrite); // BIOS access table - info += QObject::tr("\nBIOS access table:"); - info += QObject::tr("\n Read Write"); - info += QObject::tr("\nDesc %1 %2") - .arg(masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_DESC ? "Yes " : "No ") - .arg(masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_DESC ? "Yes " : "No "); - info += QObject::tr("\nBIOS Yes Yes"); - info += QObject::tr("\nME %1 %2") - .arg(masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_ME ? "Yes " : "No ") - .arg(masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_ME ? "Yes " : "No "); - info += QObject::tr("\nGbE %1 %2") - .arg(masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_GBE ? "Yes " : "No ") - .arg(masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_GBE ? "Yes " : "No "); - info += QObject::tr("\nPDR %1 %2") - .arg(masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_PDR ? "Yes " : "No ") - .arg(masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_PDR ? "Yes " : "No "); - info += QObject::tr("\nEC %1 %2") - .arg(masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_EC ? "Yes " : "No ") - .arg(masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_EC ? "Yes " : "No "); + info += UString("\nBIOS access table:") + + UString("\n Read Write") + + usprintf("\nDesc %s %s", + masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_DESC ? "Yes " : "No ", + masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_DESC ? "Yes " : "No "); + info += UString("\nBIOS Yes Yes") + + usprintf("\nME %s %s", + masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_ME ? "Yes " : "No ", + masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_ME ? "Yes " : "No "); + info += usprintf("\nGbE %s %s", + masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_GBE ? "Yes " : "No ", + masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_GBE ? "Yes " : "No "); + info += usprintf("\nPDR %s %s", + masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_PDR ? "Yes " : "No ", + masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_PDR ? "Yes " : "No "); + info += usprintf("\nEC %s %s", + masterSection->BiosRead & FLASH_DESCRIPTOR_REGION_ACCESS_EC ? "Yes " : "No ", + masterSection->BiosWrite & FLASH_DESCRIPTOR_REGION_ACCESS_EC ? "Yes " : "No "); } // VSCC table const VSCC_TABLE_ENTRY* vsccTableEntry = (const VSCC_TABLE_ENTRY*)(descriptor + ((UINT16)upperMap->VsccTableBase << 4)); - info += QObject::tr("\nFlash chips in VSCC table:"); + info += UString("\nFlash chips in VSCC table:"); UINT8 vsscTableSize = upperMap->VsccTableSize * sizeof(UINT32) / sizeof(VSCC_TABLE_ENTRY); for (int i = 0; i < vsscTableSize; i++) { - info += QObject::tr("\n%1%2%3h") - .hexarg2(vsccTableEntry->VendorId, 2) - .hexarg2(vsccTableEntry->DeviceId0, 2) - .hexarg2(vsccTableEntry->DeviceId1, 2); + info += usprintf("\n02X%02X%02Xh", + vsccTableEntry->VendorId, vsccTableEntry->DeviceId0, vsccTableEntry->DeviceId1); vsccTableEntry++; } // Add descriptor tree item - QModelIndex regionIndex = model->addItem(Types::Region, Subtypes::DescriptorRegion, name, QString(), info, QByteArray(), body, QByteArray(), true, parsingDataToQByteArray(pdata), index); + UModelIndex regionIndex = model->addItem(Types::Region, Subtypes::DescriptorRegion, name, UString(), info, UByteArray(), body, UByteArray(), true, parsingDataToUByteArray(pdata), index); // Parse regions - UINT8 result = ERR_SUCCESS; - UINT8 parseResult = ERR_SUCCESS; + UINT8 result = U_SUCCESS; + UINT8 parseResult = U_SUCCESS; for (size_t i = 0; i < regions.size(); i++) { region = regions[i]; switch (region.type) { @@ -588,26 +591,26 @@ STATUS FfsParser::parseIntelImage(const QByteArray & intelImage, const UINT32 pa case Subtypes::OnePadding: case Subtypes::DataPadding: { // Add padding between regions - QByteArray padding = intelImage.mid(region.offset, region.length); + UByteArray padding = intelImage.mid(region.offset, region.length); // Get parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); // Get info - name = QObject::tr("Padding"); - info = QObject::tr("Full size: %1h (%2)") - .hexarg(padding.size()).arg(padding.size()); + name = UString("Padding"); + info = usprintf("Full size: %X (%u)", + padding.size(), padding.size()); // Construct parsing data pdata.offset = parentOffset + region.offset; // Add tree item - regionIndex = model->addItem(Types::Padding, getPaddingType(padding), name, QString(), info, QByteArray(), padding, QByteArray(), true, parsingDataToQByteArray(pdata), index); - result = ERR_SUCCESS; + regionIndex = model->addItem(Types::Padding, getPaddingType(padding), name, UString(), info, UByteArray(), padding, UByteArray(), true, parsingDataToUByteArray(pdata), index); + result = U_SUCCESS; } break; default: - msg(QObject::tr("parseIntelImage: region of unknown type found"), index); - result = ERR_INVALID_FLASH_DESCRIPTOR; + msg(("parseIntelImage: region of unknown type found"), index); + result = U_INVALID_FLASH_DESCRIPTOR; } // Store the first failed result as a final result if (!parseResult && result) @@ -617,54 +620,49 @@ STATUS FfsParser::parseIntelImage(const QByteArray & intelImage, const UINT32 pa return parseResult; } -STATUS FfsParser::parseGbeRegion(const QByteArray & gbe, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseGbeRegion(const UByteArray & gbe, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { // Check sanity if (gbe.isEmpty()) - return ERR_EMPTY_REGION; + return U_EMPTY_REGION; if ((UINT32)gbe.size() < GBE_VERSION_OFFSET + sizeof(GBE_VERSION)) - return ERR_INVALID_REGION; + return U_INVALID_REGION; // Get parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Get info - QString name = QObject::tr("GbE region"); + UString name("GbE region"); const GBE_MAC_ADDRESS* mac = (const GBE_MAC_ADDRESS*)gbe.constData(); const GBE_VERSION* version = (const GBE_VERSION*)(gbe.constData() + GBE_VERSION_OFFSET); - QString info = QObject::tr("Full size: %1h (%2)\nMAC: %3:%4:%5:%6:%7:%8\nVersion: %9.%10") - .hexarg(gbe.size()).arg(gbe.size()) - .hexarg2(mac->vendor[0], 2) - .hexarg2(mac->vendor[1], 2) - .hexarg2(mac->vendor[2], 2) - .hexarg2(mac->device[0], 2) - .hexarg2(mac->device[1], 2) - .hexarg2(mac->device[2], 2) - .arg(version->major) - .arg(version->minor); + UString info = usprintf("Full size: %Xh (%u)\nMAC: %02X:%02X:%02X:%02X:%02X:%02X\nVersion: %u.%u", + gbe.size(), gbe.size(), + mac->vendor[0], mac->vendor[1], mac->vendor[2], + mac->device[0], mac->device[1], mac->device[2], + version->major, + version->minor); // Construct parsing data pdata.offset += parentOffset; // Add tree item - index = model->addItem(Types::Region, Subtypes::GbeRegion, name, QString(), info, QByteArray(), gbe, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Region, Subtypes::GbeRegion, name, UString(), info, UByteArray(), gbe, UByteArray(), true, parsingDataToUByteArray(pdata), parent); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseMeRegion(const QByteArray & me, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseMeRegion(const UByteArray & me, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { // Check sanity if (me.isEmpty()) - return ERR_EMPTY_REGION; + return U_EMPTY_REGION; // Get parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Get info - QString name = QObject::tr("ME region"); - QString info = QObject::tr("Full size: %1h (%2)"). - hexarg(me.size()).arg(me.size()); + UString name("ME region"); + UString info = usprintf("Full size: %Xh (%u)", me.size(), me.size()); // Parse region bool versionFound = true; @@ -673,7 +671,7 @@ STATUS FfsParser::parseMeRegion(const QByteArray & me, const UINT32 parentOffset if (me.count() == me.count('\xFF') || me.count() == me.count('\x00')) { // Further parsing not needed emptyRegion = true; - info += QObject::tr("\nState: empty"); + info += ("\nState: empty"); } else { // Search for new signature @@ -682,23 +680,23 @@ STATUS FfsParser::parseMeRegion(const QByteArray & me, const UINT32 parentOffset // Search for old signature versionOffset = me.indexOf(ME_VERSION_SIGNATURE); if (versionOffset < 0){ - info += QObject::tr("\nVersion: unknown"); + info += ("\nVersion: unknown"); versionFound = false; } } // Check sanity if ((UINT32)me.size() < (UINT32)versionOffset + sizeof(ME_VERSION)) - return ERR_INVALID_REGION; + return U_INVALID_REGION; // Add version information if (versionFound) { const ME_VERSION* version = (const ME_VERSION*)(me.constData() + versionOffset); - info += QObject::tr("\nVersion: %1.%2.%3.%4") - .arg(version->major) - .arg(version->minor) - .arg(version->bugfix) - .arg(version->build); + info += usprintf("\nVersion: %u.%u.%u.%u", + version->major, + version->minor, + version->bugfix, + version->build); } } @@ -706,94 +704,91 @@ STATUS FfsParser::parseMeRegion(const QByteArray & me, const UINT32 parentOffset pdata.offset += parentOffset; // Add tree item - index = model->addItem(Types::Region, Subtypes::MeRegion, name, QString(), info, QByteArray(), me, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Region, Subtypes::MeRegion, name, UString(), info, UByteArray(), me, UByteArray(), true, parsingDataToUByteArray(pdata), parent); // Show messages if (emptyRegion) { - msg(QObject::tr("parseMeRegion: ME region is empty"), index); + msg(UString("parseMeRegion: ME region is empty"), index); } else if (!versionFound) { - msg(QObject::tr("parseMeRegion: ME version is unknown, it can be damaged"), index); + msg(UString("parseMeRegion: ME version is unknown, it can be damaged"), index); } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parsePdrRegion(const QByteArray & pdr, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parsePdrRegion(const UByteArray & pdr, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { // Check sanity if (pdr.isEmpty()) - return ERR_EMPTY_REGION; + return U_EMPTY_REGION; // Get parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Get info - QString name = QObject::tr("PDR region"); - QString info = QObject::tr("Full size: %1h (%2)"). - hexarg(pdr.size()).arg(pdr.size()); + UString name("PDR region"); + UString info = usprintf("Full size: %Xh (%u)", pdr.size(), pdr.size()); // Construct parsing data pdata.offset += parentOffset; // Add tree item - index = model->addItem(Types::Region, Subtypes::PdrRegion, name, QString(), info, QByteArray(), pdr, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Region, Subtypes::PdrRegion, name, UString(), info, UByteArray(), pdr, UByteArray(), true, parsingDataToUByteArray(pdata), parent); // Parse PDR region as BIOS space UINT8 result = parseRawArea(index); - if (result && result != ERR_VOLUMES_NOT_FOUND && result != ERR_INVALID_VOLUME) + if (result && result != U_VOLUMES_NOT_FOUND && result != U_INVALID_VOLUME) return result; - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseGeneralRegion(const UINT8 subtype, const QByteArray & region, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseGeneralRegion(const UINT8 subtype, const UByteArray & region, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { // Check sanity if (region.isEmpty()) - return ERR_EMPTY_REGION; + return U_EMPTY_REGION; // Get parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Get info - QString name = QObject::tr("%1 region").arg(itemSubtypeToQString(Types::Region, subtype)); - QString info = QObject::tr("Full size: %1h (%2)"). - hexarg(region.size()).arg(region.size()); + UString name = itemSubtypeToUString(Types::Region, subtype) + UString(" region"); + UString info = usprintf("Full size: %Xh (%u)", region.size(), region.size()); // Construct parsing data pdata.offset += parentOffset; // Add tree item - index = model->addItem(Types::Region, subtype, name, QString(), info, QByteArray(), region, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Region, subtype, name, UString(), info, UByteArray(), region, UByteArray(), true, parsingDataToUByteArray(pdata), parent); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseBiosRegion(const QByteArray & bios, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseBiosRegion(const UByteArray & bios, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { // Sanity check if (bios.isEmpty()) - return ERR_EMPTY_REGION; + return U_EMPTY_REGION; // Get parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Get info - QString name = QObject::tr("BIOS region"); - QString info = QObject::tr("Full size: %1h (%2)"). - hexarg(bios.size()).arg(bios.size()); + UString name("BIOS region"); + UString info = usprintf("Full size: %Xh (%u)", bios.size(), bios.size()); // Construct parsing data pdata.offset += parentOffset; // Add tree item - index = model->addItem(Types::Region, Subtypes::BiosRegion, name, QString(), info, QByteArray(), bios, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Region, Subtypes::BiosRegion, name, UString(), info, UByteArray(), bios, UByteArray(), true, parsingDataToUByteArray(pdata), parent); return parseRawArea(index); } -UINT8 FfsParser::getPaddingType(const QByteArray & padding) +UINT8 FfsParser::getPaddingType(const UByteArray & padding) { if (padding.count('\x00') == padding.count()) return Subtypes::ZeroPadding; @@ -802,22 +797,22 @@ UINT8 FfsParser::getPaddingType(const QByteArray & padding) return Subtypes::DataPadding; } -STATUS FfsParser::parseRawArea(const QModelIndex & index) +USTATUS FfsParser::parseRawArea(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); UINT32 headerSize = model->header(index).size(); UINT32 offset = pdata.offset + headerSize; // Get item data - QByteArray data = model->body(index); + UByteArray data = model->body(index); // Search for first volume - STATUS result; + USTATUS result; UINT32 prevVolumeOffset; result = findNextVolume(index, data, offset, 0, prevVolumeOffset); @@ -825,20 +820,19 @@ STATUS FfsParser::parseRawArea(const QModelIndex & index) return result; // First volume is not at the beginning of RAW area - QString name; - QString info; + UString name; + UString info; if (prevVolumeOffset > 0) { // Get info - QByteArray padding = data.left(prevVolumeOffset); - name = QObject::tr("Padding"); - info = QObject::tr("Full size: %1h (%2)") - .hexarg(padding.size()).arg(padding.size()); + UByteArray padding = data.left(prevVolumeOffset); + name = UString("Padding"); + info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Construct parsing data pdata.offset = offset; // Add tree item - model->addItem(Types::Padding, getPaddingType(padding), name, QString(), info, QByteArray(), padding, QByteArray(), true, parsingDataToQByteArray(pdata), index); + model->addItem(Types::Padding, getPaddingType(padding), name, UString(), info, UByteArray(), padding, UByteArray(), true, parsingDataToUByteArray(pdata), index); } // Search for and parse all volumes @@ -851,18 +845,17 @@ STATUS FfsParser::parseRawArea(const QModelIndex & index) if (volumeOffset > prevVolumeOffset + prevVolumeSize) { UINT32 paddingOffset = prevVolumeOffset + prevVolumeSize; UINT32 paddingSize = volumeOffset - paddingOffset; - QByteArray padding = data.mid(paddingOffset, paddingSize); + UByteArray padding = data.mid(paddingOffset, paddingSize); // Get info - name = QObject::tr("Padding"); - info = QObject::tr("Full size: %1h (%2)") - .hexarg(padding.size()).arg(padding.size()); + name = UString("Padding"); + info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Construct parsing data pdata.offset = offset + paddingOffset; // Add tree item - model->addItem(Types::Padding, getPaddingType(padding), name, QString(), info, QByteArray(), padding, QByteArray(), true, parsingDataToQByteArray(pdata), index); + model->addItem(Types::Padding, getPaddingType(padding), name, UString(), info, UByteArray(), padding, UByteArray(), true, parsingDataToUByteArray(pdata), index); } // Get volume size @@ -870,32 +863,31 @@ STATUS FfsParser::parseRawArea(const QModelIndex & index) UINT32 bmVolumeSize = 0; result = getVolumeSize(data, volumeOffset, volumeSize, bmVolumeSize); if (result) { - msg(QObject::tr("parseRawArea: getVolumeSize failed with error \"%1\"").arg(errorCodeToQString(result)), index); + msg(UString("parseRawArea: getVolumeSize failed with error ") + errorCodeToUString(result), index); return result; } // Check that volume is fully present in input if (volumeSize > (UINT32)data.size() || volumeOffset + volumeSize > (UINT32)data.size()) { - msg(QObject::tr("parseRawArea: one of volumes inside overlaps the end of data"), index); - return ERR_INVALID_VOLUME; + msg(UString("parseRawArea: one of volumes inside overlaps the end of data"), index); + return U_INVALID_VOLUME; } - QByteArray volume = data.mid(volumeOffset, volumeSize); + UByteArray volume = data.mid(volumeOffset, volumeSize); if (volumeSize > (UINT32)volume.size()) { // Mark the rest as padding and finish the parsing - QByteArray padding = data.right(volume.size()); + UByteArray padding = data.right(volume.size()); // Get info - name = QObject::tr("Padding"); - info = QObject::tr("Full size: %1h (%2)") - .hexarg(padding.size()).arg(padding.size()); + name = UString("Padding"); + info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Construct parsing data pdata.offset = offset + volumeOffset; // Add tree item - QModelIndex paddingIndex = model->addItem(Types::Padding, getPaddingType(padding), name, QString(), info, QByteArray(), padding, QByteArray(), true, parsingDataToQByteArray(pdata), index); - msg(QObject::tr("parseRawArea: one of volumes inside overlaps the end of data"), paddingIndex); + UModelIndex paddingIndex = model->addItem(Types::Padding, getPaddingType(padding), name, UString(), info, UByteArray(), padding, UByteArray(), true, parsingDataToUByteArray(pdata), index); + msg(UString("parseRawArea: one of volumes inside overlaps the end of data"), paddingIndex); // Update variables prevVolumeOffset = volumeOffset; @@ -904,16 +896,16 @@ STATUS FfsParser::parseRawArea(const QModelIndex & index) } // Parse current volume's header - QModelIndex volumeIndex; + UModelIndex volumeIndex; result = parseVolumeHeader(volume, headerSize + volumeOffset, index, volumeIndex); if (result) - msg(QObject::tr("parseRawArea: volume header parsing failed with error \"%1\"").arg(errorCodeToQString(result)), index); + msg(UString("parseRawArea: volume header parsing failed with error ") + errorCodeToUString(result), index); else { // Show messages if (volumeSize != bmVolumeSize) - msg(QObject::tr("parseRawArea: volume size stored in header %1h (%2) differs from calculated using block map %3h (%4)") - .hexarg(volumeSize).arg(volumeSize) - .hexarg(bmVolumeSize).arg(bmVolumeSize), + msg(usprintf("parseRawArea: volume size stored in header %Xh (%u) differs from calculated using block map %Xh (%u)", + volumeSize, volumeSize, + bmVolumeSize, bmVolumeSize), volumeIndex); } @@ -926,23 +918,22 @@ STATUS FfsParser::parseRawArea(const QModelIndex & index) // Padding at the end of RAW area volumeOffset = prevVolumeOffset + prevVolumeSize; if ((UINT32)data.size() > volumeOffset) { - QByteArray padding = data.mid(volumeOffset); + UByteArray padding = data.mid(volumeOffset); // Get info - name = QObject::tr("Padding"); - info = QObject::tr("Full size: %1h (%2)") - .hexarg(padding.size()).arg(padding.size()); + name = UString("Padding"); + info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Construct parsing data pdata.offset = offset + headerSize + volumeOffset; // Add tree item - model->addItem(Types::Padding, getPaddingType(padding), name, QString(), info, QByteArray(), padding, QByteArray(), true, parsingDataToQByteArray(pdata), index); + model->addItem(Types::Padding, getPaddingType(padding), name, UString(), info, UByteArray(), padding, UByteArray(), true, parsingDataToUByteArray(pdata), index); } // Parse bodies for (int i = 0; i < model->rowCount(index); i++) { - QModelIndex current = index.child(i, 0); + UModelIndex current = index.child(i, 0); switch (model->type(current)) { case Types::Volume: parseVolumeBody(current); @@ -951,26 +942,26 @@ STATUS FfsParser::parseRawArea(const QModelIndex & index) // No parsing required break; default: - return ERR_UNKNOWN_ITEM_TYPE; + return U_UNKNOWN_ITEM_TYPE; } } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseVolumeHeader(const QByteArray & volume, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseVolumeHeader(const UByteArray & volume, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { // Sanity check if (volume.isEmpty()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Check that there is space for the volume header if ((UINT32)volume.size() < sizeof(EFI_FIRMWARE_VOLUME_HEADER)) { - msg(QObject::tr("parseVolumeHeader: input volume size %1h (%2) is smaller than volume header size 40h (64)").hexarg(volume.size()).arg(volume.size())); - return ERR_INVALID_VOLUME; + msg(usprintf("parseVolumeHeader: input volume size %Xh (%u) is smaller than volume header size 40h (64)", volume.size(), volume.size())); + return U_INVALID_VOLUME; } // Populate volume header @@ -978,14 +969,14 @@ STATUS FfsParser::parseVolumeHeader(const QByteArray & volume, const UINT32 pare // Check sanity of HeaderLength value if ((UINT32)ALIGN8(volumeHeader->HeaderLength) > (UINT32)volume.size()) { - msg(QObject::tr("parseVolumeHeader: volume header overlaps the end of data")); - return ERR_INVALID_VOLUME; + msg(UString("parseVolumeHeader: volume header overlaps the end of data")); + return U_INVALID_VOLUME; } // Check sanity of ExtHeaderOffset value if (volumeHeader->Revision > 1 && volumeHeader->ExtHeaderOffset && (UINT32)ALIGN8(volumeHeader->ExtHeaderOffset + sizeof(EFI_FIRMWARE_VOLUME_EXT_HEADER)) > (UINT32)volume.size()) { - msg(QObject::tr("parseVolumeHeader: extended volume header overlaps the end of data")); - return ERR_INVALID_VOLUME; + msg(UString("parseVolumeHeader: extended volume header overlaps the end of data")); + return U_INVALID_VOLUME; } // Calculate volume header size @@ -1010,7 +1001,7 @@ STATUS FfsParser::parseVolumeHeader(const QByteArray & volume, const UINT32 pare UINT8 ffsVersion = 0; // Check for FFS v2 volume - QByteArray guid = QByteArray((const char*)volumeHeader->FileSystemGuid.Data, sizeof(EFI_GUID)); + UByteArray guid = UByteArray((const char*)volumeHeader->FileSystemGuid.Data, sizeof(EFI_GUID)); if (std::find(FFSv2Volumes.begin(), FFSv2Volumes.end(), guid) != FFSv2Volumes.end()) { isUnknown = false; ffsVersion = 2; @@ -1096,38 +1087,38 @@ STATUS FfsParser::parseVolumeHeader(const QByteArray & volume, const UINT32 pare // Check header checksum by recalculating it bool msgInvalidChecksum = false; - QByteArray tempHeader((const char*)volumeHeader, volumeHeader->HeaderLength); + UByteArray tempHeader((const char*)volumeHeader, volumeHeader->HeaderLength); ((EFI_FIRMWARE_VOLUME_HEADER*)tempHeader.data())->Checksum = 0; UINT16 calculated = calculateChecksum16((const UINT16*)tempHeader.constData(), volumeHeader->HeaderLength); if (volumeHeader->Checksum != calculated) msgInvalidChecksum = true; // Get info - QByteArray header = volume.left(headerSize); - QByteArray body = volume.mid(headerSize); - QString name = guidToQString(volumeHeader->FileSystemGuid); - QString info = QObject::tr("Signature: _FVH\nZeroVector:\n%1 %2 %3 %4 %5 %6 %7 %8\n%9 %10 %11 %12 %13 %14 %15 %16\nFileSystem GUID: %17\nFull size: %18h (%19)\n" - "Header size: %20h (%21)\nBody size: %22h (%23)\nRevision: %24\nAttributes: %25h\nErase polarity: %26\nChecksum: %27h, %28") - .hexarg2(volumeHeader->ZeroVector[0], 2).hexarg2(volumeHeader->ZeroVector[1], 2).hexarg2(volumeHeader->ZeroVector[2], 2).hexarg2(volumeHeader->ZeroVector[3], 2) - .hexarg2(volumeHeader->ZeroVector[4], 2).hexarg2(volumeHeader->ZeroVector[5], 2).hexarg2(volumeHeader->ZeroVector[6], 2).hexarg2(volumeHeader->ZeroVector[7], 2) - .hexarg2(volumeHeader->ZeroVector[8], 2).hexarg2(volumeHeader->ZeroVector[9], 2).hexarg2(volumeHeader->ZeroVector[10], 2).hexarg2(volumeHeader->ZeroVector[11], 2) - .hexarg2(volumeHeader->ZeroVector[12], 2).hexarg2(volumeHeader->ZeroVector[13], 2).hexarg2(volumeHeader->ZeroVector[14], 2).hexarg2(volumeHeader->ZeroVector[15], 2) - .arg(guidToQString(volumeHeader->FileSystemGuid)) - .hexarg(volumeSize).arg(volumeSize) - .hexarg(headerSize).arg(headerSize) - .hexarg(volumeSize - headerSize).arg(volumeSize - headerSize) - .arg(volumeHeader->Revision) - .hexarg2(volumeHeader->Attributes, 8) - .arg(emptyByte ? "1" : "0") - .hexarg2(volumeHeader->Checksum, 4) - .arg(msgInvalidChecksum ? QObject::tr("invalid, should be %1h").hexarg2(calculated, 4) : QObject::tr("valid")); + UByteArray header = volume.left(headerSize); + UByteArray body = volume.mid(headerSize); + UString name = guidToUString(volumeHeader->FileSystemGuid); + UString info = usprintf("Signature: _FVH\nZeroVector:\n%02X %02X %02X %02X %02X %02X %02X %02X\n" + "%02X %02X %02X %02X %02X %02X %02X %02X\nFileSystem GUID: ", + volumeHeader->ZeroVector[0], volumeHeader->ZeroVector[1], volumeHeader->ZeroVector[2], volumeHeader->ZeroVector[3], + volumeHeader->ZeroVector[4], volumeHeader->ZeroVector[5], volumeHeader->ZeroVector[6], volumeHeader->ZeroVector[7], + volumeHeader->ZeroVector[8], volumeHeader->ZeroVector[9], volumeHeader->ZeroVector[10], volumeHeader->ZeroVector[11], + volumeHeader->ZeroVector[12], volumeHeader->ZeroVector[13], volumeHeader->ZeroVector[14], volumeHeader->ZeroVector[15]) + + guidToUString(volumeHeader->FileSystemGuid) \ + + usprintf("\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nRevision: %u\nAttributes: %08Xh\nErase polarity: %u\nChecksum: %02Xh", + volumeSize, volumeSize, + headerSize, headerSize, + volumeSize - headerSize, volumeSize - headerSize, + volumeHeader->Revision, + volumeHeader->Attributes, 8, + emptyByte ? 1 : 0, + volumeHeader->Checksum) + + (msgInvalidChecksum ? usprintf("invalid, should be %04Xh", calculated) : UString("valid")); // Extended header present if (volumeHeader->Revision > 1 && volumeHeader->ExtHeaderOffset) { const EFI_FIRMWARE_VOLUME_EXT_HEADER* extendedHeader = (const EFI_FIRMWARE_VOLUME_EXT_HEADER*)(volume.constData() + volumeHeader->ExtHeaderOffset); - info += QObject::tr("\nExtended header size: %1h (%2)\nVolume GUID: %3") - .hexarg(extendedHeader->ExtHeaderSize).arg(extendedHeader->ExtHeaderSize) - .arg(guidToQString(extendedHeader->FvName)); + info += usprintf("\nExtended header size: %Xh (%u)\nVolume GUID: ", + extendedHeader->ExtHeaderSize, extendedHeader->ExtHeaderSize) + guidToUString(extendedHeader->FvName); } // Construct parsing data @@ -1143,11 +1134,11 @@ STATUS FfsParser::parseVolumeHeader(const QByteArray & volume, const UINT32 pare pdata.volume.isWeakAligned = (volumeHeader->Revision > 1 && (volumeHeader->Attributes & EFI_FVB2_WEAK_ALIGNMENT)); // Add text - QString text; + UString text; if (hasAppleCrc32) - text += QObject::tr("AppleCRC32 "); + text += UString("AppleCRC32 "); if (hasAppleFSO) - text += QObject::tr("AppleFSO "); + text += UString("AppleFSO "); // Add tree item UINT8 subtype = Subtypes::UnknownVolume; @@ -1159,42 +1150,48 @@ STATUS FfsParser::parseVolumeHeader(const QByteArray & volume, const UINT32 pare else if (isNvramVolume) subtype = Subtypes::NvramVolume; } - index = model->addItem(Types::Volume, subtype, name, text, info, header, body, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Volume, subtype, name, text, info, header, body, UByteArray(), true, parsingDataToUByteArray(pdata), parent); // Show messages if (isUnknown) - msg(QObject::tr("parseVolumeHeader: unknown file system %1").arg(guidToQString(volumeHeader->FileSystemGuid)), index); + msg(UString("parseVolumeHeader: unknown file system ") + guidToUString(volumeHeader->FileSystemGuid), index); if (msgInvalidChecksum) - msg(QObject::tr("parseVolumeHeader: volume header checksum is invalid"), index); + msg(UString("parseVolumeHeader: volume header checksum is invalid"), index); if (msgAlignmentBitsSet) - msg(QObject::tr("parseVolumeHeader: alignment bits set on volume without alignment capability"), index); + msg(UString("parseVolumeHeader: alignment bits set on volume without alignment capability"), index); if (msgUnaligned) - msg(QObject::tr("parseVolumeHeader: unaligned volume"), index); + msg(UString("parseVolumeHeader: unaligned volume"), index); if (msgUnknownRevision) - msg(QObject::tr("parseVolumeHeader: unknown volume revision %1").arg(volumeHeader->Revision), index); + msg(usprintf("parseVolumeHeader: unknown volume revision %u", volumeHeader->Revision), index); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::findNextVolume(const QModelIndex & index, const QByteArray & bios, const UINT32 parentOffset, const UINT32 volumeOffset, UINT32 & nextVolumeOffset) +USTATUS FfsParser::findNextVolume(const UModelIndex & index, const UByteArray & bios, const UINT32 parentOffset, const UINT32 volumeOffset, UINT32 & nextVolumeOffset) { int nextIndex = bios.indexOf(EFI_FV_SIGNATURE, volumeOffset); if (nextIndex < EFI_FV_SIGNATURE_OFFSET) - return ERR_VOLUMES_NOT_FOUND; + return U_VOLUMES_NOT_FOUND; // Check volume header to be sane for (; nextIndex > 0; nextIndex = bios.indexOf(EFI_FV_SIGNATURE, nextIndex + 1)) { const EFI_FIRMWARE_VOLUME_HEADER* volumeHeader = (const EFI_FIRMWARE_VOLUME_HEADER*)(bios.constData() + nextIndex - EFI_FV_SIGNATURE_OFFSET); if (volumeHeader->FvLength < sizeof(EFI_FIRMWARE_VOLUME_HEADER) + 2 * sizeof(EFI_FV_BLOCK_MAP_ENTRY) || volumeHeader->FvLength >= 0xFFFFFFFFUL) { - msg(QObject::tr("findNextVolume: volume candidate at offset %1h skipped, has invalid FvLength %2h").hexarg(parentOffset + (nextIndex - EFI_FV_SIGNATURE_OFFSET)).hexarg2(volumeHeader->FvLength, 16), index); + msg(usprintf("findNextVolume: volume candidate at offset %Xh skipped, has invalid FvLength %Xh", + parentOffset + (nextIndex - EFI_FV_SIGNATURE_OFFSET), + volumeHeader->FvLength), index); continue; } if (volumeHeader->Reserved != 0xFF && volumeHeader->Reserved != 0x00) { - msg(QObject::tr("findNextVolume: volume candidate at offset %1h skipped, has invalid Reserved byte value %2").hexarg(parentOffset + (nextIndex - EFI_FV_SIGNATURE_OFFSET)).hexarg2(volumeHeader->Reserved, 2), index); + msg(usprintf("findNextVolume: volume candidate at offset %Xh skipped, has invalid Reserved byte value %02X", + parentOffset + (nextIndex - EFI_FV_SIGNATURE_OFFSET), + volumeHeader->Reserved), index); continue; } if (volumeHeader->Revision != 1 && volumeHeader->Revision != 2) { - msg(QObject::tr("findNextVolume: volume candidate at offset %1h skipped, has invalid Revision byte value %2").hexarg(parentOffset + (nextIndex - EFI_FV_SIGNATURE_OFFSET)).hexarg2(volumeHeader->Revision, 2), index); + msg(usprintf("findNextVolume: volume candidate at offset %Xh skipped, has invalid Revision byte value %02X", + parentOffset + (nextIndex - EFI_FV_SIGNATURE_OFFSET) + ,volumeHeader->Revision), index); continue; } // All checks passed, volume found @@ -1202,31 +1199,31 @@ STATUS FfsParser::findNextVolume(const QModelIndex & index, const QByteArray & b } // No more volumes found if (nextIndex < EFI_FV_SIGNATURE_OFFSET) - return ERR_VOLUMES_NOT_FOUND; + return U_VOLUMES_NOT_FOUND; nextVolumeOffset = nextIndex - EFI_FV_SIGNATURE_OFFSET; - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::getVolumeSize(const QByteArray & bios, UINT32 volumeOffset, UINT32 & volumeSize, UINT32 & bmVolumeSize) +USTATUS FfsParser::getVolumeSize(const UByteArray & bios, UINT32 volumeOffset, UINT32 & volumeSize, UINT32 & bmVolumeSize) { // Check that there is space for the volume header and at least two block map entries. if ((UINT32)bios.size() < volumeOffset + sizeof(EFI_FIRMWARE_VOLUME_HEADER) + 2 * sizeof(EFI_FV_BLOCK_MAP_ENTRY)) - return ERR_INVALID_VOLUME; + return U_INVALID_VOLUME; // Populate volume header const EFI_FIRMWARE_VOLUME_HEADER* volumeHeader = (const EFI_FIRMWARE_VOLUME_HEADER*)(bios.constData() + volumeOffset); // Check volume signature - if (QByteArray((const char*)&volumeHeader->Signature, sizeof(volumeHeader->Signature)) != EFI_FV_SIGNATURE) - return ERR_INVALID_VOLUME; + if (UByteArray((const char*)&volumeHeader->Signature, sizeof(volumeHeader->Signature)) != EFI_FV_SIGNATURE) + return U_INVALID_VOLUME; // Calculate volume size using BlockMap const EFI_FV_BLOCK_MAP_ENTRY* entry = (const EFI_FV_BLOCK_MAP_ENTRY*)(bios.constData() + volumeOffset + sizeof(EFI_FIRMWARE_VOLUME_HEADER)); UINT32 calcVolumeSize = 0; while (entry->NumBlocks != 0 && entry->Length != 0) { if ((void*)entry > bios.constData() + bios.size()) - return ERR_INVALID_VOLUME; + return U_INVALID_VOLUME; calcVolumeSize += entry->NumBlocks * entry->Length; entry += 1; @@ -1236,26 +1233,26 @@ STATUS FfsParser::getVolumeSize(const QByteArray & bios, UINT32 volumeOffset, UI bmVolumeSize = calcVolumeSize; if (volumeSize == 0) - return ERR_INVALID_VOLUME; + return U_INVALID_VOLUME; - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseVolumeNonUefiData(const QByteArray & data, const UINT32 parentOffset, const QModelIndex & index) +USTATUS FfsParser::parseVolumeNonUefiData(const UByteArray & data, const UINT32 parentOffset, const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); // Modify it pdata.offset += parentOffset; // Search for VTF GUID backwards in received data - QByteArray padding = data; - QByteArray vtf; + UByteArray padding = data; + UByteArray vtf; INT32 vtfIndex = data.lastIndexOf(EFI_FFS_VOLUME_TOP_FILE_GUID); if (vtfIndex >= 0) { // VTF candidate found inside non-UEFI data padding = data.left(vtfIndex); @@ -1273,52 +1270,52 @@ STATUS FfsParser::parseVolumeNonUefiData(const QByteArray & data, const UINT32 p // Add non-UEFI data first // Get info - QString info = QObject::tr("Full size: %1h (%2)").hexarg(padding.size()).arg(padding.size()); + UString info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Add padding tree item - QModelIndex paddingIndex = model->addItem(Types::Padding, Subtypes::DataPadding, QObject::tr("Non-UEFI data"), "", info, QByteArray(), padding, QByteArray(), true, parsingDataToQByteArray(pdata), index); - msg(QObject::tr("parseVolumeNonUefiData: non-UEFI data found in volume's free space"), paddingIndex); + UModelIndex paddingIndex = model->addItem(Types::Padding, Subtypes::DataPadding, UString("Non-UEFI data"), "", info, UByteArray(), padding, UByteArray(), true, parsingDataToUByteArray(pdata), index); + msg(UString("parseVolumeNonUefiData: non-UEFI data found in volume's free space"), paddingIndex); if (vtfIndex >= 0) { // Get VTF file header - QByteArray header = vtf.left(sizeof(EFI_FFS_FILE_HEADER)); + UByteArray header = vtf.left(sizeof(EFI_FFS_FILE_HEADER)); const EFI_FFS_FILE_HEADER* fileHeader = (const EFI_FFS_FILE_HEADER*)header.constData(); if (pdata.ffsVersion == 3 && (fileHeader->Attributes & FFS_ATTRIB_LARGE_FILE)) { header = vtf.left(sizeof(EFI_FFS_FILE_HEADER2)); } //Parse VTF file header - QModelIndex fileIndex; - STATUS result = parseFileHeader(vtf, parentOffset + vtfIndex, index, fileIndex); + UModelIndex fileIndex; + USTATUS result = parseFileHeader(vtf, parentOffset + vtfIndex, index, fileIndex); if (result) { - msg(QObject::tr("parseVolumeNonUefiData: VTF file header parsing failed with error \"%1\"").arg(errorCodeToQString(result)), index); + msg(UString("parseVolumeNonUefiData: VTF file header parsing failed with error ") + errorCodeToUString(result), index); // Add the rest as non-UEFI data too pdata.offset += vtfIndex; // Get info - QString info = QObject::tr("Full size: %1h (%2)").hexarg(vtf.size()).arg(vtf.size()); + UString info = usprintf("Full size: %Xh (%u)", vtf.size(), vtf.size()); // Add padding tree item - QModelIndex paddingIndex = model->addItem(Types::Padding, Subtypes::DataPadding, QObject::tr("Non-UEFI data"), "", info, QByteArray(), vtf, QByteArray(), true, parsingDataToQByteArray(pdata), index); - msg(QObject::tr("parseVolumeNonUefiData: non-UEFI data found in volume's free space"), paddingIndex); + UModelIndex paddingIndex = model->addItem(Types::Padding, Subtypes::DataPadding, UString("Non-UEFI data"), "", info, UByteArray(), vtf, UByteArray(), true, parsingDataToUByteArray(pdata), index); + msg(("parseVolumeNonUefiData: non-UEFI data found in volume's free space"), paddingIndex); } } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseVolumeBody(const QModelIndex & index) +USTATUS FfsParser::parseVolumeBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get volume header size and body - QByteArray volumeBody = model->body(index); + UByteArray volumeBody = model->body(index); UINT32 volumeHeaderSize = model->header(index).size(); // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); UINT32 offset = pdata.offset; // Parse VSS NVRAM volumes with a dedicated function @@ -1326,7 +1323,7 @@ STATUS FfsParser::parseVolumeBody(const QModelIndex & index) return parseNvramVolumeBody(index); if (pdata.ffsVersion != 2 && pdata.ffsVersion != 3) // Don't parse unknown volumes - return ERR_SUCCESS; + return U_SUCCESS; // Search for and parse all files UINT32 volumeBodySize = volumeBody.size(); @@ -1337,10 +1334,10 @@ STATUS FfsParser::parseVolumeBody(const QModelIndex & index) // Check file size if (fileSize < sizeof(EFI_FFS_FILE_HEADER) || fileSize > volumeBodySize - fileOffset) { // Check that we are at the empty space - QByteArray header = volumeBody.mid(fileOffset, sizeof(EFI_FFS_FILE_HEADER)); + UByteArray header = volumeBody.mid(fileOffset, sizeof(EFI_FFS_FILE_HEADER)); if (header.count(pdata.emptyByte) == header.size()) { //Empty space // Check free space to be actually free - QByteArray freeSpace = volumeBody.mid(fileOffset); + UByteArray freeSpace = volumeBody.mid(fileOffset); if (freeSpace.count(pdata.emptyByte) != freeSpace.count()) { // Search for the first non-empty byte UINT32 i; @@ -1361,13 +1358,13 @@ STATUS FfsParser::parseVolumeBody(const QModelIndex & index) // Add all bytes before as free space if (i > 0) { - QByteArray free = freeSpace.left(i); + UByteArray free = freeSpace.left(i); // Get info - QString info = QObject::tr("Full size: %1h (%2)").hexarg(free.size()).arg(free.size()); + UString info = usprintf("Full size: %Xh (%u)", free.size(), free.size()); // Add free space item - model->addItem(Types::FreeSpace, 0, QObject::tr("Volume free space"), "", info, QByteArray(), free, QByteArray(), false, parsingDataToQByteArray(pdata), index); + model->addItem(Types::FreeSpace, 0, UString("Volume free space"), "", info, UByteArray(), free, UByteArray(), false, parsingDataToUByteArray(pdata), index); } // Parse non-UEFI data @@ -1378,10 +1375,10 @@ STATUS FfsParser::parseVolumeBody(const QModelIndex & index) pdata.offset = offset + volumeHeaderSize + fileOffset; // Get info - QString info = QObject::tr("Full size: %1h (%2)").hexarg(freeSpace.size()).arg(freeSpace.size()); + UString info = usprintf("Full size: %Xh (%u)", freeSpace.size(), freeSpace.size()); // Add free space item - model->addItem(Types::FreeSpace, 0, QObject::tr("Volume free space"), "", info, QByteArray(), freeSpace, QByteArray(), false, parsingDataToQByteArray(pdata), index); + model->addItem(Types::FreeSpace, 0, UString("Volume free space"), "", info, UByteArray(), freeSpace, UByteArray(), false, parsingDataToUByteArray(pdata), index); } break; // Exit from parsing loop } @@ -1393,18 +1390,18 @@ STATUS FfsParser::parseVolumeBody(const QModelIndex & index) } // Get file header - QByteArray file = volumeBody.mid(fileOffset, fileSize); - QByteArray header = file.left(sizeof(EFI_FFS_FILE_HEADER)); + UByteArray file = volumeBody.mid(fileOffset, fileSize); + UByteArray header = file.left(sizeof(EFI_FFS_FILE_HEADER)); const EFI_FFS_FILE_HEADER* fileHeader = (const EFI_FFS_FILE_HEADER*)header.constData(); if (pdata.ffsVersion == 3 && (fileHeader->Attributes & FFS_ATTRIB_LARGE_FILE)) { header = file.left(sizeof(EFI_FFS_FILE_HEADER2)); } //Parse current file's header - QModelIndex fileIndex; - STATUS result = parseFileHeader(file, volumeHeaderSize + fileOffset, index, fileIndex); + UModelIndex fileIndex; + USTATUS result = parseFileHeader(file, volumeHeaderSize + fileOffset, index, fileIndex); if (result) - msg(QObject::tr("parseVolumeBody: file header parsing failed with error \"%1\"").arg(errorCodeToQString(result)), index); + msg(UString("parseVolumeBody: file header parsing failed with error ") + errorCodeToUString(result), index); // Move to next file fileOffset += fileSize; @@ -1413,37 +1410,37 @@ STATUS FfsParser::parseVolumeBody(const QModelIndex & index) // Check for duplicate GUIDs for (int i = 0; i < model->rowCount(index); i++) { - QModelIndex current = index.child(i, 0); + UModelIndex current = index.child(i, 0); // Skip non-file entries and pad files if (model->type(current) != Types::File || model->subtype(current) == EFI_FV_FILETYPE_PAD) continue; // Get current file parsing data - PARSING_DATA currentPdata = parsingDataFromQModelIndex(current); - QByteArray currentGuid((const char*)¤tPdata.file.guid, sizeof(EFI_GUID)); + PARSING_DATA currentPdata = parsingDataFromUModelIndex(current); + UByteArray currentGuid((const char*)¤tPdata.file.guid, sizeof(EFI_GUID)); // Check files after current for having an equal GUID for (int j = i + 1; j < model->rowCount(index); j++) { - QModelIndex another = index.child(j, 0); + UModelIndex another = index.child(j, 0); // Skip non-file entries if (model->type(another) != Types::File) continue; // Get another file parsing data - PARSING_DATA anotherPdata = parsingDataFromQModelIndex(another); - QByteArray anotherGuid((const char*)&anotherPdata.file.guid, sizeof(EFI_GUID)); + PARSING_DATA anotherPdata = parsingDataFromUModelIndex(another); + UByteArray anotherGuid((const char*)&anotherPdata.file.guid, sizeof(EFI_GUID)); // Check GUIDs for being equal if (currentGuid == anotherGuid) { - msg(QObject::tr("parseVolumeBody: file with duplicate GUID %1").arg(guidToQString(anotherPdata.file.guid)), another); + msg(UString("parseVolumeBody: file with duplicate GUID ") + guidToUString(anotherPdata.file.guid), another); } } } //Parse bodies for (int i = 0; i < model->rowCount(index); i++) { - QModelIndex current = index.child(i, 0); + UModelIndex current = index.child(i, 0); switch (model->type(current)) { case Types::File: parseFileBody(current); @@ -1453,14 +1450,14 @@ STATUS FfsParser::parseVolumeBody(const QModelIndex & index) // No parsing required break; default: - return ERR_UNKNOWN_ITEM_TYPE; + return U_UNKNOWN_ITEM_TYPE; } } - return ERR_SUCCESS; + return U_SUCCESS; } -UINT32 FfsParser::getFileSize(const QByteArray & volume, const UINT32 fileOffset, const UINT8 ffsVersion) +UINT32 FfsParser::getFileSize(const UByteArray & volume, const UINT32 fileOffset, const UINT8 ffsVersion) { if (ffsVersion == 2) { if ((UINT32)volume.size() < fileOffset + sizeof(EFI_FFS_FILE_HEADER)) @@ -1481,24 +1478,24 @@ UINT32 FfsParser::getFileSize(const QByteArray & volume, const UINT32 fileOffset return 0; } -STATUS FfsParser::parseFileHeader(const QByteArray & file, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseFileHeader(const UByteArray & file, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { // Sanity check if (file.isEmpty()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; if ((UINT32)file.size() < sizeof(EFI_FFS_FILE_HEADER)) - return ERR_INVALID_FILE; + return U_INVALID_FILE; // Get parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Get file header - QByteArray header = file.left(sizeof(EFI_FFS_FILE_HEADER)); + UByteArray header = file.left(sizeof(EFI_FFS_FILE_HEADER)); const EFI_FFS_FILE_HEADER* fileHeader = (const EFI_FFS_FILE_HEADER*)header.constData(); if (pdata.ffsVersion == 3 && (fileHeader->Attributes & FFS_ATTRIB_LARGE_FILE)) { if ((UINT32)file.size() < sizeof(EFI_FFS_FILE_HEADER2)) - return ERR_INVALID_FILE; + return U_INVALID_FILE; header = file.left(sizeof(EFI_FFS_FILE_HEADER2)); } @@ -1515,7 +1512,7 @@ STATUS FfsParser::parseFileHeader(const QByteArray & file, const UINT32 parentOf msgFileAlignmentIsGreaterThanVolumes = true; // Check header checksum - QByteArray tempHeader = header; + UByteArray tempHeader = header; EFI_FFS_FILE_HEADER* tempFileHeader = (EFI_FFS_FILE_HEADER*)(tempHeader.data()); tempFileHeader->IntegrityCheck.Checksum.Header = 0; tempFileHeader->IntegrityCheck.Checksum.File = 0; @@ -1554,10 +1551,10 @@ STATUS FfsParser::parseFileHeader(const QByteArray & file, const UINT32 parentOf }; // Get file body - QByteArray body = file.mid(header.size()); + UByteArray body = file.mid(header.size()); // Check for file tail presence - QByteArray tail; + UByteArray tail; bool msgInvalidTailValue = false; if (pdata.volume.revision == 1 && (fileHeader->Attributes & FFS_ATTRIB_TAIL_PRESENT)) { @@ -1572,40 +1569,37 @@ STATUS FfsParser::parseFileHeader(const QByteArray & file, const UINT32 parentOf } // Get info - QString name; - QString info; + UString name; + UString info; if (fileHeader->Type != EFI_FV_FILETYPE_PAD) - name = guidToQString(fileHeader->Name); + name = guidToUString(fileHeader->Name); else - name = QObject::tr("Pad-file"); + name = UString("Pad-file"); - info = QObject::tr("File GUID: %1\nType: %2h\nAttributes: %3h\nFull size: %4h (%5)\nHeader size: %6h (%7)\nBody size: %8h (%9)\nTail size: %10h (%11)\n" - "State: %12h\nHeader checksum: %13h, %14\nData checksum: %15h, %16") - .arg(guidToQString(fileHeader->Name)) - .hexarg2(fileHeader->Type, 2) - .hexarg2(fileHeader->Attributes, 2) - .hexarg(header.size() + body.size() + tail.size()).arg(header.size() + body.size() + tail.size()) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()) - .hexarg(tail.size()).arg(tail.size()) - .hexarg2(fileHeader->State, 2) - .hexarg2(fileHeader->IntegrityCheck.Checksum.Header, 2) - .arg(msgInvalidHeaderChecksum ? QObject::tr("invalid, should be %1h").hexarg2(calculatedHeader, 2) : QObject::tr("valid")) - .hexarg2(fileHeader->IntegrityCheck.Checksum.File, 2) - .arg(msgInvalidDataChecksum ? QObject::tr("invalid, should be %1h").hexarg2(calculatedData, 2) : QObject::tr("valid")); + info = guidToUString(fileHeader->Name).prepend("File GUID : ") + + usprintf("\nType: %02Xh\nAttributes: %02Xh\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nTail size: %Xh (%u)\nState: %02Xh\n", + fileHeader->Type, + fileHeader->Attributes, + header.size() + body.size() + tail.size(), header.size() + body.size() + tail.size(), + header.size(), header.size(), + body.size(), body.size(), + tail.size(), tail.size(), + fileHeader->State) + + (msgInvalidHeaderChecksum ? usprintf("%02Xh, invalid, should be %02Xh", fileHeader->IntegrityCheck.Checksum.Header, calculatedHeader) : usprintf("%02X, valid", calculatedHeader)) + + (msgInvalidDataChecksum ? usprintf("%02Xh, invalid, should be %02Xh", fileHeader->IntegrityCheck.Checksum.File, calculatedData) : usprintf("%02X, valid", calculatedData)); // Add file GUID to parsing data pdata.file.guid = fileHeader->Name; - QString text; + UString text; bool isVtf = false; // Check if the file is a Volume Top File - if (QByteArray((const char*)&fileHeader->Name, sizeof(EFI_GUID)) == EFI_FFS_VOLUME_TOP_FILE_GUID) { + if (UByteArray((const char*)&fileHeader->Name, sizeof(EFI_GUID)) == EFI_FFS_VOLUME_TOP_FILE_GUID) { // Mark it as the last VTF // This information will later be used to determine memory addresses of uncompressed image elements // Because the last byte of the last VFT is mapped to 0xFFFFFFFF physical memory address isVtf = true; - text = QObject::tr("Volume Top File"); + text = UString("Volume Top File"); } // Construct parsing data @@ -1614,7 +1608,7 @@ STATUS FfsParser::parseFileHeader(const QByteArray & file, const UINT32 parentOf // Add tree item - index = model->addItem(Types::File, fileHeader->Type, name, text, info, header, body, tail, fixed, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::File, fileHeader->Type, name, text, info, header, body, tail, fixed, parsingDataToUByteArray(pdata), parent); // Overwrite lastVtf, if needed if (isVtf) { @@ -1623,22 +1617,22 @@ STATUS FfsParser::parseFileHeader(const QByteArray & file, const UINT32 parentOf // Show messages if (msgUnalignedFile) - msg(QObject::tr("parseFileHeader: unaligned file"), index); + msg(UString("parseFileHeader: unaligned file"), index); if (msgFileAlignmentIsGreaterThanVolumes) - msg(QObject::tr("parseFileHeader: file alignment %1h is greater than parent volume alignment %2h").hexarg(alignment).hexarg(pdata.volume.alignment), index); + msg(usprintf("parseFileHeader: file alignment %Xh is greater than parent volume alignment %Xh", alignment, pdata.volume.alignment), index); if (msgInvalidHeaderChecksum) - msg(QObject::tr("parseFileHeader: invalid header checksum"), index); + msg(UString("parseFileHeader: invalid header checksum"), index); if (msgInvalidDataChecksum) - msg(QObject::tr("parseFileHeader: invalid data checksum"), index); + msg(UString("parseFileHeader: invalid data checksum"), index); if (msgInvalidTailValue) - msg(QObject::tr("parseFileHeader: invalid tail value"), index); + msg(UString("parseFileHeader: invalid tail value"), index); if (msgUnknownType) - msg(QObject::tr("parseFileHeader: unknown file type %1h").hexarg2(fileHeader->Type, 2), index); + msg(usprintf("parseFileHeader: unknown file type %02Xh", fileHeader->Type), index); - return ERR_SUCCESS; + return U_SUCCESS; } -UINT32 FfsParser::getSectionSize(const QByteArray & file, const UINT32 sectionOffset, const UINT8 ffsVersion) +UINT32 FfsParser::getSectionSize(const UByteArray & file, const UINT32 sectionOffset, const UINT8 ffsVersion) { if (ffsVersion == 2) { if ((UINT32)file.size() < sectionOffset + sizeof(EFI_COMMON_SECTION_HEADER)) @@ -1660,15 +1654,15 @@ UINT32 FfsParser::getSectionSize(const QByteArray & file, const UINT32 sectionOf return 0; } -STATUS FfsParser::parseFileBody(const QModelIndex & index) +USTATUS FfsParser::parseFileBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Do not parse non-file bodies if (model->type(index) != Types::File) - return ERR_SUCCESS; + return U_SUCCESS; // Parse pad-file body if (model->subtype(index) == EFI_FV_FILETYPE_PAD) @@ -1677,10 +1671,10 @@ STATUS FfsParser::parseFileBody(const QModelIndex & index) // Parse raw files as raw areas if (model->subtype(index) == EFI_FV_FILETYPE_RAW || model->subtype(index) == EFI_FV_FILETYPE_ALL) { // Get data from parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); // Parse NVAR store - if (QByteArray((const char*)&pdata.file.guid, sizeof(EFI_GUID)) == NVRAM_NVAR_STORE_FILE_GUID) + if (UByteArray((const char*)&pdata.file.guid, sizeof(EFI_GUID)) == NVRAM_NVAR_STORE_FILE_GUID) return parseNvarStore(index); return parseRawArea(index); @@ -1690,19 +1684,19 @@ STATUS FfsParser::parseFileBody(const QModelIndex & index) return parseSections(model->body(index), index); } -STATUS FfsParser::parsePadFileBody(const QModelIndex & index) +USTATUS FfsParser::parsePadFileBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get data from parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); // Check if all bytes of the file are empty - QByteArray body = model->body(index); + UByteArray body = model->body(index); if (body.size() == body.count(pdata.emptyByte)) - return ERR_SUCCESS; + return U_SUCCESS; // Search for the first non-empty byte UINT32 i; @@ -1719,56 +1713,56 @@ STATUS FfsParser::parsePadFileBody(const QModelIndex & index) if (i != ALIGN8(i)) i = ALIGN8(i) - 8; - QByteArray free = body.left(i); + UByteArray free = body.left(i); // Get info - QString info = QObject::tr("Full size: %1h (%2)").hexarg(free.size()).arg(free.size()); + UString info = usprintf("Full size: %Xh (%u)", free.size(), free.size()); // Constuct parsing data pdata.offset += model->header(index).size(); // Add tree item - model->addItem(Types::FreeSpace, 0, QObject::tr("Free space"), QString(), info, QByteArray(), free, QByteArray(), false, parsingDataToQByteArray(pdata), index); + model->addItem(Types::FreeSpace, 0, UString("Free space"), UString(), info, UByteArray(), free, UByteArray(), false, parsingDataToUByteArray(pdata), index); } else i = 0; // ... and all bytes after as a padding - QByteArray padding = body.mid(i); + UByteArray padding = body.mid(i); // Get info - QString info = QObject::tr("Full size: %1h (%2)").hexarg(padding.size()).arg(padding.size()); + UString info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Constuct parsing data pdata.offset += i; // Add tree item - QModelIndex dataIndex = model->addItem(Types::Padding, Subtypes::DataPadding, QObject::tr("Non-UEFI data"), "", info, QByteArray(), padding, QByteArray(), true, parsingDataToQByteArray(pdata), index); + UModelIndex dataIndex = model->addItem(Types::Padding, Subtypes::DataPadding, UString("Non-UEFI data"), "", info, UByteArray(), padding, UByteArray(), true, parsingDataToUByteArray(pdata), index); // Show message - msg(QObject::tr("parsePadFileBody: non-UEFI data found in pad-file"), dataIndex); + msg(UString("parsePadFileBody: non-UEFI data found in pad-file"), dataIndex); // Rename the file - model->setName(index, QObject::tr("Non-empty pad-file")); + model->setName(index, UString("Non-empty pad-file")); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseSections(const QByteArray & sections, const QModelIndex & index, const bool preparse) +USTATUS FfsParser::parseSections(const UByteArray & sections, const UModelIndex & index, const bool preparse) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get data from parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); // Search for and parse all sections UINT32 bodySize = sections.size(); UINT32 headerSize = model->header(index).size(); UINT32 sectionOffset = 0; - STATUS result = ERR_SUCCESS; + USTATUS result = U_SUCCESS; while (sectionOffset < bodySize) { // Get section size UINT32 sectionSize = getSectionSize(sections, sectionOffset, pdata.ffsVersion); @@ -1776,9 +1770,9 @@ STATUS FfsParser::parseSections(const QByteArray & sections, const QModelIndex & // Check section size if (sectionSize < sizeof(EFI_COMMON_SECTION_HEADER) || sectionSize > (bodySize - sectionOffset)) { // Add padding to fill the rest of sections - QByteArray padding = sections.mid(sectionOffset); + UByteArray padding = sections.mid(sectionOffset); // Get info - QString info = QObject::tr("Full size: %1h (%2)").hexarg(padding.size()).arg(padding.size()); + UString info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Constuct parsing data pdata.offset += headerSize + sectionOffset; @@ -1786,26 +1780,26 @@ STATUS FfsParser::parseSections(const QByteArray & sections, const QModelIndex & // Final parsing if (!preparse) { // Add tree item - QModelIndex dataIndex = model->addItem(Types::Padding, Subtypes::DataPadding, QObject::tr("Non-UEFI data"), "", info, QByteArray(), padding, QByteArray(), true, parsingDataToQByteArray(pdata), index); + UModelIndex dataIndex = model->addItem(Types::Padding, Subtypes::DataPadding, UString("Non-UEFI data"), "", info, UByteArray(), padding, UByteArray(), true, parsingDataToUByteArray(pdata), index); // Show message - msg(QObject::tr("parseSections: non-UEFI data found in sections area"), dataIndex); + msg(UString("parseSections: non-UEFI data found in sections area"), dataIndex); } // Preparsing else { - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; } break; // Exit from parsing loop } // Parse section header - QModelIndex sectionIndex; + UModelIndex sectionIndex; result = parseSectionHeader(sections.mid(sectionOffset, sectionSize), headerSize + sectionOffset, index, sectionIndex, preparse); if (result) { if (!preparse) - msg(QObject::tr("parseSections: section header parsing failed with error \"%1\"").arg(errorCodeToQString(result)), index); + msg(UString("parseSections: section header parsing failed with error ") + errorCodeToUString(result), index); else - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; } // Move to next section sectionOffset += sectionSize; @@ -1814,7 +1808,7 @@ STATUS FfsParser::parseSections(const QByteArray & sections, const QModelIndex & //Parse bodies, will be skipped on preparse phase for (int i = 0; i < model->rowCount(index); i++) { - QModelIndex current = index.child(i, 0); + UModelIndex current = index.child(i, 0); switch (model->type(current)) { case Types::Section: parseSectionBody(current); @@ -1823,18 +1817,18 @@ STATUS FfsParser::parseSections(const QByteArray & sections, const QModelIndex & // No parsing required break; default: - return ERR_UNKNOWN_ITEM_TYPE; + return U_UNKNOWN_ITEM_TYPE; } } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse) +USTATUS FfsParser::parseSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse) { // Check sanity if ((UINT32)section.size() < sizeof(EFI_COMMON_SECTION_HEADER)) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; const EFI_COMMON_SECTION_HEADER* sectionHeader = (const EFI_COMMON_SECTION_HEADER*)(section.constData()); switch (sectionHeader->Type) { @@ -1859,20 +1853,20 @@ STATUS FfsParser::parseSectionHeader(const QByteArray & section, const UINT32 pa case EFI_SECTION_RAW: return parseCommonSectionHeader(section, parentOffset, parent, index, preparse); // Unknown default: - STATUS result = parseCommonSectionHeader(section, parentOffset, parent, index, preparse); - msg(QObject::tr("parseSectionHeader: section with unknown type %1h").hexarg2(sectionHeader->Type, 2), index); + USTATUS result = parseCommonSectionHeader(section, parentOffset, parent, index, preparse); + msg(usprintf("parseSectionHeader: section with unknown type %02Xh", sectionHeader->Type), index); return result; } } -STATUS FfsParser::parseCommonSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse) +USTATUS FfsParser::parseCommonSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse) { // Check sanity if ((UINT32)section.size() < sizeof(EFI_COMMON_SECTION_HEADER)) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; // Get data from parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Obtain header fields UINT32 headerSize; @@ -1892,37 +1886,37 @@ STATUS FfsParser::parseCommonSectionHeader(const QByteArray & section, const UIN // Check sanity again if ((UINT32)section.size() < headerSize) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; - QByteArray header = section.left(headerSize); - QByteArray body = section.mid(headerSize); + UByteArray header = section.left(headerSize); + UByteArray body = section.mid(headerSize); // Get info - QString name = sectionTypeToQString(type) + QObject::tr(" section"); - QString info = QObject::tr("Type: %1h\nFull size: %2h (%3)\nHeader size: %4h (%5)\nBody size: %6h (%7)") - .hexarg2(type, 2) - .hexarg(section.size()).arg(section.size()) - .hexarg(headerSize).arg(headerSize) - .hexarg(body.size()).arg(body.size()); + UString name = sectionTypeToUString(type) + UString(" section"); + UString info = usprintf("Type: %02Xh\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)", + type, + section.size(), section.size(), + headerSize, headerSize, + body.size(), body.size()); // Construct parsing data pdata.offset += parentOffset; // Add tree item if (!preparse) { - index = model->addItem(Types::Section, type, name, QString(), info, header, body, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Section, type, name, UString(), info, header, body, UByteArray(), true, parsingDataToUByteArray(pdata), parent); } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseCompressedSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse) +USTATUS FfsParser::parseCompressedSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse) { // Check sanity if ((UINT32)section.size() < sizeof(EFI_COMMON_SECTION_HEADER)) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; // Get data from parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Obtain header fields UINT32 headerSize; @@ -1941,7 +1935,7 @@ STATUS FfsParser::parseCompressedSectionHeader(const QByteArray & section, const else if (pdata.ffsVersion == 3 && uint24ToUint32(sectionHeader->Size) == EFI_SECTION2_IS_USED) { // Check for extended header section const EFI_COMPRESSION_SECTION* compressedSectionHeader = (const EFI_COMPRESSION_SECTION*)(section2Header + 1); if ((UINT32)section.size() < sizeof(EFI_COMMON_SECTION_HEADER2) + sizeof(EFI_COMPRESSION_SECTION)) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; headerSize = sizeof(EFI_COMMON_SECTION_HEADER2) + sizeof(EFI_COMPRESSION_SECTION); compressionType = compressedSectionHeader->CompressionType; uncompressedLength = compressedSectionHeader->UncompressedLength; @@ -1955,20 +1949,20 @@ STATUS FfsParser::parseCompressedSectionHeader(const QByteArray & section, const // Check sanity again if ((UINT32)section.size() < headerSize) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; - QByteArray header = section.left(headerSize); - QByteArray body = section.mid(headerSize); + UByteArray header = section.left(headerSize); + UByteArray body = section.mid(headerSize); // Get info - QString name = sectionTypeToQString(sectionHeader->Type) + QObject::tr(" section"); - QString info = QObject::tr("Type: %1h\nFull size: %2h (%3)\nHeader size: %4h (%5)\nBody size: %6h (%7)\nCompression type: %8h\nDecompressed size: %9h (%10)") - .hexarg2(sectionHeader->Type, 2) - .hexarg(section.size()).arg(section.size()) - .hexarg(headerSize).arg(headerSize) - .hexarg(body.size()).arg(body.size()) - .hexarg2(compressionType, 2) - .hexarg(uncompressedLength).arg(uncompressedLength); + UString name = sectionTypeToUString(sectionHeader->Type) + UString(" section"); + UString info = usprintf("Type: %02Xh\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nCompression type: %02Xh\nDecompressed size: %Xh (%u)", + sectionHeader->Type, + section.size(), section.size(), + headerSize, headerSize, + body.size(), body.size(), + compressionType, + uncompressedLength, uncompressedLength); // Construct parsing data pdata.offset += parentOffset; @@ -1977,19 +1971,19 @@ STATUS FfsParser::parseCompressedSectionHeader(const QByteArray & section, const // Add tree item if (!preparse) { - index = model->addItem(Types::Section, sectionHeader->Type, name, QString(), info, header, body, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Section, sectionHeader->Type, name, UString(), info, header, body, UByteArray(), true, parsingDataToUByteArray(pdata), parent); } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseGuidedSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse) +USTATUS FfsParser::parseGuidedSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse) { // Check sanity if ((UINT32)section.size() < sizeof(EFI_COMMON_SECTION_HEADER)) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; // Get data from parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Obtain header fields UINT32 headerSize; @@ -2004,7 +1998,7 @@ STATUS FfsParser::parseGuidedSectionHeader(const QByteArray & section, const UIN const EFI_GUID_DEFINED_SECTION_APPLE* appleSectionHeader = (const EFI_GUID_DEFINED_SECTION_APPLE*)(appleHeader + 1); headerSize = sizeof(EFI_COMMON_SECTION_HEADER_APPLE) + sizeof(EFI_GUID_DEFINED_SECTION_APPLE); if ((UINT32)section.size() < headerSize) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; guid = appleSectionHeader->SectionDefinitionGuid; dataOffset = appleSectionHeader->DataOffset; attributes = appleSectionHeader->Attributes; @@ -2012,7 +2006,7 @@ STATUS FfsParser::parseGuidedSectionHeader(const QByteArray & section, const UIN else if (pdata.ffsVersion == 3 && uint24ToUint32(sectionHeader->Size) == EFI_SECTION2_IS_USED) { // Check for extended header section const EFI_GUID_DEFINED_SECTION* guidDefinedSectionHeader = (const EFI_GUID_DEFINED_SECTION*)(section2Header + 1); if ((UINT32)section.size() < sizeof(EFI_COMMON_SECTION_HEADER2) + sizeof(EFI_GUID_DEFINED_SECTION)) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; headerSize = sizeof(EFI_COMMON_SECTION_HEADER2) + sizeof(EFI_GUID_DEFINED_SECTION); guid = guidDefinedSectionHeader->SectionDefinitionGuid; dataOffset = guidDefinedSectionHeader->DataOffset; @@ -2027,11 +2021,11 @@ STATUS FfsParser::parseGuidedSectionHeader(const QByteArray & section, const UIN } // Check sanity again if ((UINT32)section.size() < headerSize) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; // Check for special GUIDed sections - QByteArray additionalInfo; - QByteArray baGuid((const char*)&guid, sizeof(EFI_GUID)); + UByteArray additionalInfo; + UByteArray baGuid((const char*)&guid, sizeof(EFI_GUID)); bool msgSignedSectionFound = false; bool msgNoAuthStatusAttribute = false; bool msgNoProcessingRequiredAttributeCompressed = false; @@ -2045,17 +2039,17 @@ STATUS FfsParser::parseGuidedSectionHeader(const QByteArray & section, const UIN } if ((UINT32)section.size() < headerSize + sizeof(UINT32)) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; UINT32 crc = *(UINT32*)(section.constData() + headerSize); - additionalInfo += QObject::tr("\nChecksum type: CRC32"); + additionalInfo += UString("\nChecksum type: CRC32"); // Calculate CRC32 of section data UINT32 calculated = crc32(0, (const UINT8*)section.constData() + dataOffset, section.size() - dataOffset); if (crc == calculated) { - additionalInfo += QObject::tr("\nChecksum: %1h, valid").hexarg2(crc, 8); + additionalInfo += usprintf("\nChecksum: %08Xh, valid", crc); } else { - additionalInfo += QObject::tr("\nChecksum: %1h, invalid, should be %2h").hexarg2(crc, 8).hexarg2(calculated, 8); + additionalInfo += usprintf("\nChecksum: %02Xh, invalid, should be %02Xh", crc, calculated); msgInvalidCrc = true; } // No need to change dataOffset here @@ -2073,7 +2067,7 @@ STATUS FfsParser::parseGuidedSectionHeader(const QByteArray & section, const UIN // Get certificate type and length if ((UINT32)section.size() < headerSize + sizeof(WIN_CERTIFICATE)) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; const WIN_CERTIFICATE* winCertificate = (const WIN_CERTIFICATE*)(section.constData() + headerSize); UINT32 certLength = winCertificate->Length; @@ -2084,44 +2078,44 @@ STATUS FfsParser::parseGuidedSectionHeader(const QByteArray & section, const UIN // Check section size once again if ((UINT32)section.size() < dataOffset) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; // Check certificate type if (certType == WIN_CERT_TYPE_EFI_GUID) { - additionalInfo += QObject::tr("\nCertificate type: UEFI"); + additionalInfo += UString("\nCertificate type: UEFI"); // Get certificate GUID const WIN_CERTIFICATE_UEFI_GUID* winCertificateUefiGuid = (const WIN_CERTIFICATE_UEFI_GUID*)(section.constData() + headerSize); - QByteArray certTypeGuid((const char*)&winCertificateUefiGuid->CertType, sizeof(EFI_GUID)); + UByteArray certTypeGuid((const char*)&winCertificateUefiGuid->CertType, sizeof(EFI_GUID)); if (certTypeGuid == EFI_CERT_TYPE_RSA2048_SHA256_GUID) { - additionalInfo += QObject::tr("\nCertificate subtype: RSA2048/SHA256"); + additionalInfo += UString("\nCertificate subtype: RSA2048/SHA256"); } else { - additionalInfo += QObject::tr("\nCertificate subtype: unknown, GUID %1").arg(guidToQString(winCertificateUefiGuid->CertType)); + additionalInfo += UString("\nCertificate subtype: unknown, GUID ") + guidToUString(winCertificateUefiGuid->CertType); msgUnknownCertSubtype = true; } } else { - additionalInfo += QObject::tr("\nCertificate type: unknown (%1h)").hexarg2(certType, 4); + additionalInfo += usprintf("\nCertificate type: unknown (%04Xh)", certType); msgUnknownCertType = true; } msgSignedSectionFound = true; } - QByteArray header = section.left(dataOffset); - QByteArray body = section.mid(dataOffset); + UByteArray header = section.left(dataOffset); + UByteArray body = section.mid(dataOffset); // Get info - QString name = guidToQString(guid); - QString info = QObject::tr("Section GUID: %1\nType: %2h\nFull size: %3h (%4)\nHeader size: %5h (%6)\nBody size: %7h (%8)\nData offset: %9h\nAttributes: %10h") - .arg(name) - .hexarg2(sectionHeader->Type, 2) - .hexarg(section.size()).arg(section.size()) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()) - .hexarg(dataOffset) - .hexarg2(attributes, 4); + UString name = guidToUString(guid); + UString info = UString("Section GUID: ") + name + + usprintf("\nType: %02Xh\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nData offset: %Xh\nAttributes: %04Xh", + sectionHeader->Type, + section.size(), section.size(), + header.size(), header.size(), + body.size(), body.size(), + dataOffset, + attributes); // Append additional info info.append(additionalInfo); @@ -2132,36 +2126,36 @@ STATUS FfsParser::parseGuidedSectionHeader(const QByteArray & section, const UIN // Add tree item if (!preparse) { - index = model->addItem(Types::Section, sectionHeader->Type, name, QString(), info, header, body, QByteArray(), false, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Section, sectionHeader->Type, name, UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), parent); // Show messages if (msgSignedSectionFound) - msg(QObject::tr("parseGuidedSectionHeader: section signature may become invalid after any modification"), index); + msg(UString("parseGuidedSectionHeader: section signature may become invalid after any modification"), index); if (msgNoAuthStatusAttribute) - msg(QObject::tr("parseGuidedSectionHeader: CRC32 GUIDed section without AuthStatusValid attribute"), index); + msg(UString("parseGuidedSectionHeader: CRC32 GUIDed section without AuthStatusValid attribute"), index); if (msgNoProcessingRequiredAttributeCompressed) - msg(QObject::tr("parseGuidedSectionHeader: compressed GUIDed section without ProcessingRequired attribute"), index); + msg(UString("parseGuidedSectionHeader: compressed GUIDed section without ProcessingRequired attribute"), index); if (msgNoProcessingRequiredAttributeSigned) - msg(QObject::tr("parseGuidedSectionHeader: signed GUIDed section without ProcessingRequired attribute"), index); + msg(UString("parseGuidedSectionHeader: signed GUIDed section without ProcessingRequired attribute"), index); if (msgInvalidCrc) - msg(QObject::tr("parseGuidedSectionHeader: GUID defined section with invalid CRC32"), index); + msg(UString("parseGuidedSectionHeader: GUID defined section with invalid CRC32"), index); if (msgUnknownCertType) - msg(QObject::tr("parseGuidedSectionHeader: signed GUIDed section with unknown type"), index); + msg(UString("parseGuidedSectionHeader: signed GUIDed section with unknown type"), index); if (msgUnknownCertSubtype) - msg(QObject::tr("parseGuidedSectionHeader: signed GUIDed section with unknown subtype"), index); + msg(UString("parseGuidedSectionHeader: signed GUIDed section with unknown subtype"), index); } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseFreeformGuidedSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse) +USTATUS FfsParser::parseFreeformGuidedSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse) { // Check sanity if ((UINT32)section.size() < sizeof(EFI_COMMON_SECTION_HEADER)) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; // Get data from parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Obtain header fields UINT32 headerSize; @@ -2180,7 +2174,7 @@ STATUS FfsParser::parseFreeformGuidedSectionHeader(const QByteArray & section, c else if (pdata.ffsVersion == 3 && uint24ToUint32(sectionHeader->Size) == EFI_SECTION2_IS_USED) { // Check for extended header section const EFI_FREEFORM_SUBTYPE_GUID_SECTION* fsgSectionHeader = (const EFI_FREEFORM_SUBTYPE_GUID_SECTION*)(section2Header + 1); if ((UINT32)section.size() < sizeof(EFI_COMMON_SECTION_HEADER2) + sizeof(EFI_FREEFORM_SUBTYPE_GUID_SECTION)) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; headerSize = sizeof(EFI_COMMON_SECTION_HEADER2) + sizeof(EFI_FREEFORM_SUBTYPE_GUID_SECTION); guid = fsgSectionHeader->SubTypeGuid; type = section2Header->Type; @@ -2194,19 +2188,19 @@ STATUS FfsParser::parseFreeformGuidedSectionHeader(const QByteArray & section, c // Check sanity again if ((UINT32)section.size() < headerSize) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; - QByteArray header = section.left(headerSize); - QByteArray body = section.mid(headerSize); + UByteArray header = section.left(headerSize); + UByteArray body = section.mid(headerSize); // Get info - QString name = sectionTypeToQString(type) + QObject::tr(" section"); - QString info = QObject::tr("Type: %1h\nFull size: %2h (%3)\nHeader size: %4h (%5)\nBody size: %6h (%7)\nSubtype GUID: %8") - .hexarg2(type, 2) - .hexarg(section.size()).arg(section.size()) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()) - .arg(guidToQString(guid)); + UString name = sectionTypeToUString(type) + (" section"); + UString info = usprintf("Type: %02Xh\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nSubtype GUID: ", + type, + section.size(), section.size(), + header.size(), header.size(), + body.size(), body.size()) + + guidToUString(guid); // Construct parsing data pdata.offset += parentOffset; @@ -2214,22 +2208,22 @@ STATUS FfsParser::parseFreeformGuidedSectionHeader(const QByteArray & section, c // Add tree item if (!preparse) { - index = model->addItem(Types::Section, type, name, QString(), info, header, body, QByteArray(), false, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Section, type, name, UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), parent); // Rename section - model->setName(index, guidToQString(guid)); + model->setName(index, guidToUString(guid)); } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseVersionSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse) +USTATUS FfsParser::parseVersionSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse) { // Check sanity if ((UINT32)section.size() < sizeof(EFI_COMMON_SECTION_HEADER)) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; // Get data from parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Obtain header fields UINT32 headerSize; @@ -2260,38 +2254,38 @@ STATUS FfsParser::parseVersionSectionHeader(const QByteArray & section, const UI // Check sanity again if ((UINT32)section.size() < headerSize) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; - QByteArray header = section.left(headerSize); - QByteArray body = section.mid(headerSize); + UByteArray header = section.left(headerSize); + UByteArray body = section.mid(headerSize); // Get info - QString name = sectionTypeToQString(type) + QObject::tr(" section"); - QString info = QObject::tr("Type: %1h\nFull size: %2h (%3)\nHeader size: %4h (%5)\nBody size: %6h (%7)\nBuild number: %8") - .hexarg2(type, 2) - .hexarg(section.size()).arg(section.size()) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()) - .arg(buildNumber); + UString name = sectionTypeToUString(type) + (" section"); + UString info = ("Type: %02Xh\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nBuild number: %u", + type, + section.size(), section.size(), + header.size(), header.size(), + body.size(), body.size(), + buildNumber); // Construct parsing data pdata.offset += parentOffset; // Add tree item if (!preparse) { - index = model->addItem(Types::Section, type, name, QString(), info, header, body, QByteArray(), false, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Section, type, name, UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), parent); } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parsePostcodeSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse) +USTATUS FfsParser::parsePostcodeSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse) { // Check sanity if ((UINT32)section.size() < sizeof(EFI_COMMON_SECTION_HEADER)) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; // Get data from parent's parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Obtain header fields UINT32 headerSize; @@ -2322,39 +2316,39 @@ STATUS FfsParser::parsePostcodeSectionHeader(const QByteArray & section, const U // Check sanity again if ((UINT32)section.size() < headerSize) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; - QByteArray header = section.left(headerSize); - QByteArray body = section.mid(headerSize); + UByteArray header = section.left(headerSize); + UByteArray body = section.mid(headerSize); // Get info - QString name = sectionTypeToQString(type) + QObject::tr(" section"); - QString info = QObject::tr("Type: %1h\nFull size: %2h (%3)\nHeader size: %4h (%5)\nBody size: %6h (%7)\nPostcode: %8h") - .hexarg2(type, 2) - .hexarg(section.size()).arg(section.size()) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()) - .hexarg(postCode); + UString name = sectionTypeToUString(type) + (" section"); + UString info = ("Type: %02Xh\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nPostcode: %Xh", + type, 2, + section.size(), section.size(), + header.size(), header.size(), + body.size(), body.size(), + postCode); // Construct parsing data pdata.offset += parentOffset; // Add tree item if (!preparse) { - index = model->addItem(Types::Section, sectionHeader->Type, name, QString(), info, header, body, QByteArray(), false, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Section, sectionHeader->Type, name, UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), parent); } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseSectionBody(const QModelIndex & index) +USTATUS FfsParser::parseSectionBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; - QByteArray header = model->header(index); + return U_INVALID_PARAMETER; + UByteArray header = model->header(index); if ((UINT32)header.size() < sizeof(EFI_COMMON_SECTION_HEADER)) - return ERR_INVALID_SECTION; + return U_INVALID_SECTION; const EFI_COMMON_SECTION_HEADER* sectionHeader = (const EFI_COMMON_SECTION_HEADER*)(header.constData()); @@ -2380,130 +2374,130 @@ STATUS FfsParser::parseSectionBody(const QModelIndex & index) case PHOENIX_SECTION_POSTCODE: case INSYDE_SECTION_POSTCODE: default: - return ERR_SUCCESS; + return U_SUCCESS; } } -STATUS FfsParser::parseCompressedSectionBody(const QModelIndex & index) +USTATUS FfsParser::parseCompressedSectionBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get data from parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); UINT8 algorithm = pdata.section.compressed.compressionType; // Decompress section - QByteArray decompressed; - QByteArray efiDecompressed; - STATUS result = decompress(model->body(index), algorithm, decompressed, efiDecompressed); + UByteArray decompressed; + UByteArray efiDecompressed; + USTATUS result = decompress(model->body(index), algorithm, decompressed, efiDecompressed); if (result) { - msg(QObject::tr("parseCompressedSectionBody: decompression failed with error \"%1\"").arg(errorCodeToQString(result)), index); - return ERR_SUCCESS; + msg(UString("parseCompressedSectionBody: decompression failed with error ") + errorCodeToUString(result), index); + return U_SUCCESS; } // Check reported uncompressed size if (pdata.section.compressed.uncompressedSize != (UINT32)decompressed.size()) { - msg(QObject::tr("parseCompressedSectionBody: decompressed size stored in header %1h (%2) differs from actual %3h (%4)") - .hexarg(pdata.section.compressed.uncompressedSize) - .arg(pdata.section.compressed.uncompressedSize) - .hexarg(decompressed.size()) - .arg(decompressed.size()), index); - model->addInfo(index, QObject::tr("\nActual decompressed size: %1h (%2)").hexarg(decompressed.size()).arg(decompressed.size())); + msg(usprintf("parseCompressedSectionBody: decompressed size stored in header %Xh (%u) differs from actual %Xh (%u)", + pdata.section.compressed.uncompressedSize, + pdata.section.compressed.uncompressedSize, + decompressed.size(), + decompressed.size()), index); + model->addInfo(index, usprintf("\nActual decompressed size: %Xh (%u)", decompressed.size(), decompressed.size())); } // Check for undecided compression algorithm, this is a special case if (algorithm == COMPRESSION_ALGORITHM_UNDECIDED) { // Try preparse of sections decompressed with Tiano algorithm - if (ERR_SUCCESS == parseSections(decompressed, index, true)) { + if (U_SUCCESS == parseSections(decompressed, index, true)) { algorithm = COMPRESSION_ALGORITHM_TIANO; } // Try preparse of sections decompressed with EFI 1.1 algorithm - else if (ERR_SUCCESS == parseSections(efiDecompressed, index, true)) { + else if (U_SUCCESS == parseSections(efiDecompressed, index, true)) { algorithm = COMPRESSION_ALGORITHM_EFI11; decompressed = efiDecompressed; } else { - msg(QObject::tr("parseCompressedSectionBody: can't guess the correct decompression algorithm, both preparse steps are failed"), index); + msg(UString("parseCompressedSectionBody: can't guess the correct decompression algorithm, both preparse steps are failed"), index); } } // Add info - model->addInfo(index, QObject::tr("\nCompression algorithm: %1").arg(compressionTypeToQString(algorithm))); + model->addInfo(index, UString("\nCompression algorithm: ") + compressionTypeToUString(algorithm)); // Update data pdata.section.compressed.algorithm = algorithm; if (algorithm != COMPRESSION_ALGORITHM_NONE) model->setCompressed(index, true); - model->setParsingData(index, parsingDataToQByteArray(pdata)); + model->setParsingData(index, parsingDataToUByteArray(pdata)); // Parse decompressed data return parseSections(decompressed, index); } -STATUS FfsParser::parseGuidedSectionBody(const QModelIndex & index) +USTATUS FfsParser::parseGuidedSectionBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get data from parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); EFI_GUID guid = pdata.section.guidDefined.guid; // Check if section requires processing - QByteArray processed = model->body(index); - QByteArray efiDecompressed; - QString info; + UByteArray processed = model->body(index); + UByteArray efiDecompressed; + UString info; bool parseCurrentSection = true; UINT8 algorithm = COMPRESSION_ALGORITHM_NONE; // Tiano compressed section - if (QByteArray((const char*)&guid, sizeof(EFI_GUID)) == EFI_GUIDED_SECTION_TIANO) { + if (UByteArray((const char*)&guid, sizeof(EFI_GUID)) == EFI_GUIDED_SECTION_TIANO) { algorithm = EFI_STANDARD_COMPRESSION; - STATUS result = decompress(model->body(index), algorithm, processed, efiDecompressed); + USTATUS result = decompress(model->body(index), algorithm, processed, efiDecompressed); if (result) { parseCurrentSection = false; - msg(QObject::tr("parseGuidedSectionBody: decompression failed with error \"%1\"").arg(errorCodeToQString(result)), index); - return ERR_SUCCESS; + msg(UString("parseGuidedSectionBody: decompression failed with error ") + errorCodeToUString(result), index); + return U_SUCCESS; } // Check for undecided compression algorithm, this is a special case if (algorithm == COMPRESSION_ALGORITHM_UNDECIDED) { // Try preparse of sections decompressed with Tiano algorithm - if (ERR_SUCCESS == parseSections(processed, index, true)) { + if (U_SUCCESS == parseSections(processed, index, true)) { algorithm = COMPRESSION_ALGORITHM_TIANO; } // Try preparse of sections decompressed with EFI 1.1 algorithm - else if (ERR_SUCCESS == parseSections(efiDecompressed, index, true)) { + else if (U_SUCCESS == parseSections(efiDecompressed, index, true)) { algorithm = COMPRESSION_ALGORITHM_EFI11; processed = efiDecompressed; } else { - msg(QObject::tr("parseGuidedSectionBody: can't guess the correct decompression algorithm, both preparse steps are failed"), index); + msg(UString("parseGuidedSectionBody: can't guess the correct decompression algorithm, both preparse steps are failed"), index); parseCurrentSection = false; } } - info += QObject::tr("\nCompression algorithm: %1").arg(compressionTypeToQString(algorithm)); - info += QObject::tr("\nDecompressed size: %1h (%2)").hexarg(processed.length()).arg(processed.length()); + info += UString("\nCompression algorithm: ") + compressionTypeToUString(algorithm); + info += usprintf("\nDecompressed size: %Xh (%u)", processed.length(), processed.length()); } // LZMA compressed section - else if (QByteArray((const char*)&guid, sizeof(EFI_GUID)) == EFI_GUIDED_SECTION_LZMA) { + else if (UByteArray((const char*)&guid, sizeof(EFI_GUID)) == EFI_GUIDED_SECTION_LZMA) { algorithm = EFI_CUSTOMIZED_COMPRESSION; - STATUS result = decompress(model->body(index), algorithm, processed, efiDecompressed); + USTATUS result = decompress(model->body(index), algorithm, processed, efiDecompressed); if (result) { parseCurrentSection = false; - msg(QObject::tr("parseGuidedSectionBody: decompression failed with error \"%1\"").arg(errorCodeToQString(result)), index); - return ERR_SUCCESS; + msg(UString("parseGuidedSectionBody: decompression failed with error ") + errorCodeToUString(result), index); + return U_SUCCESS; } if (algorithm == COMPRESSION_ALGORITHM_LZMA) { - info += QObject::tr("\nCompression algorithm: LZMA"); - info += QObject::tr("\nDecompressed size: %1h (%2)").hexarg(processed.length()).arg(processed.length()); + info += UString("\nCompression algorithm: LZMA"); + info += usprintf("\nDecompressed size: %Xh (%u)", processed.length(), processed.length()); } else { - info += QObject::tr("\nCompression algorithm: unknown"); + info += UString("\nCompression algorithm: unknown"); parseCurrentSection = false; } } @@ -2514,41 +2508,41 @@ STATUS FfsParser::parseGuidedSectionBody(const QModelIndex & index) // Update data if (algorithm != COMPRESSION_ALGORITHM_NONE) model->setCompressed(index, true); - model->setParsingData(index, parsingDataToQByteArray(pdata)); + model->setParsingData(index, parsingDataToUByteArray(pdata)); if (!parseCurrentSection) { - msg(QObject::tr("parseGuidedSectionBody: GUID defined section can not be processed"), index); - return ERR_SUCCESS; + msg(UString("parseGuidedSectionBody: GUID defined section can not be processed"), index); + return U_SUCCESS; } return parseSections(processed, index); } -STATUS FfsParser::parseVersionSectionBody(const QModelIndex & index) +USTATUS FfsParser::parseVersionSectionBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Add info - model->addInfo(index, QObject::tr("\nVersion string: %1").arg(QString::fromUtf16((const CHAR16*)model->body(index).constData()))); + model->addInfo(index, UString("\nVersion string: ") + UString::fromUtf16((const CHAR16*)model->body(index).constData())); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseDepexSectionBody(const QModelIndex & index) +USTATUS FfsParser::parseDepexSectionBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; - QByteArray body = model->body(index); - QString parsed; + UByteArray body = model->body(index); + UString parsed; // Check data to be present if (body.size() < 2) { // 2 is a minimal sane value, i.e TRUE + END - msg(QObject::tr("parseDepexSectionBody: DEPEX section too short"), index); - return ERR_DEPEX_PARSE_FAILED; + msg(UString("parseDepexSectionBody: DEPEX section too short"), index); + return U_DEPEX_PARSE_FAILED; } const EFI_GUID * guid; @@ -2558,36 +2552,36 @@ STATUS FfsParser::parseDepexSectionBody(const QModelIndex & index) switch (*current) { case EFI_DEP_BEFORE: if (body.size() != 2 * EFI_DEP_OPCODE_SIZE + sizeof(EFI_GUID)) { - msg(QObject::tr("parseDepexSectionBody: DEPEX section too long for a section starting with BEFORE opcode"), index); - return ERR_SUCCESS; + msg(UString("parseDepexSectionBody: DEPEX section too long for a section starting with BEFORE opcode"), index); + return U_SUCCESS; } guid = (const EFI_GUID*)(current + EFI_DEP_OPCODE_SIZE); - parsed += QObject::tr("\nBEFORE %1").arg(guidToQString(*guid)); + parsed += UString("\nBEFORE ") + guidToUString(*guid); current += EFI_DEP_OPCODE_SIZE + sizeof(EFI_GUID); if (*current != EFI_DEP_END){ - msg(QObject::tr("parseDepexSectionBody: DEPEX section ends with non-END opcode"), index); - return ERR_SUCCESS; + msg(UString("parseDepexSectionBody: DEPEX section ends with non-END opcode"), index); + return U_SUCCESS; } - return ERR_SUCCESS; + return U_SUCCESS; case EFI_DEP_AFTER: if (body.size() != 2 * EFI_DEP_OPCODE_SIZE + sizeof(EFI_GUID)){ - msg(QObject::tr("parseDepexSectionBody: DEPEX section too long for a section starting with AFTER opcode"), index); - return ERR_SUCCESS; + msg(UString("parseDepexSectionBody: DEPEX section too long for a section starting with AFTER opcode"), index); + return U_SUCCESS; } guid = (const EFI_GUID*)(current + EFI_DEP_OPCODE_SIZE); - parsed += QObject::tr("\nAFTER %1").arg(guidToQString(*guid)); + parsed += UString("\nAFTER ") + guidToUString(*guid); current += EFI_DEP_OPCODE_SIZE + sizeof(EFI_GUID); if (*current != EFI_DEP_END) { - msg(QObject::tr("parseDepexSectionBody: DEPEX section ends with non-END opcode"), index); - return ERR_SUCCESS; + msg(UString("parseDepexSectionBody: DEPEX section ends with non-END opcode"), index); + return U_SUCCESS; } - return ERR_SUCCESS; + return U_SUCCESS; case EFI_DEP_SOR: if (body.size() <= 2 * EFI_DEP_OPCODE_SIZE) { - msg(QObject::tr("parseDepexSectionBody: DEPEX section too short for a section starting with SOR opcode"), index); - return ERR_SUCCESS; + msg(UString("parseDepexSectionBody: DEPEX section too short for a section starting with SOR opcode"), index); + return U_SUCCESS; } - parsed += QObject::tr("\nSOR"); + parsed += UString("\nSOR"); current += EFI_DEP_OPCODE_SIZE; break; } @@ -2596,148 +2590,148 @@ STATUS FfsParser::parseDepexSectionBody(const QModelIndex & index) while (current - (const UINT8*)body.constData() < body.size()) { switch (*current) { case EFI_DEP_BEFORE: { - msg(QObject::tr("parseDepexSectionBody: misplaced BEFORE opcode"), index); - return ERR_SUCCESS; + msg(UString("parseDepexSectionBody: misplaced BEFORE opcode"), index); + return U_SUCCESS; } case EFI_DEP_AFTER: { - msg(QObject::tr("parseDepexSectionBody: misplaced AFTER opcode"), index); - return ERR_SUCCESS; + msg(UString("parseDepexSectionBody: misplaced AFTER opcode"), index); + return U_SUCCESS; } case EFI_DEP_SOR: { - msg(QObject::tr("parseDepexSectionBody: misplaced SOR opcode"), index); - return ERR_SUCCESS; + msg(UString("parseDepexSectionBody: misplaced SOR opcode"), index); + return U_SUCCESS; } case EFI_DEP_PUSH: // Check that the rest of depex has correct size if ((UINT32)body.size() - (UINT32)(current - (const UINT8*)body.constData()) <= EFI_DEP_OPCODE_SIZE + sizeof(EFI_GUID)) { parsed.clear(); - msg(QObject::tr("parseDepexSectionBody: remains of DEPEX section too short for PUSH opcode"), index); - return ERR_SUCCESS; + msg(UString("parseDepexSectionBody: remains of DEPEX section too short for PUSH opcode"), index); + return U_SUCCESS; } guid = (const EFI_GUID*)(current + EFI_DEP_OPCODE_SIZE); - parsed += QObject::tr("\nPUSH %1").arg(guidToQString(*guid)); + parsed += UString("\nPUSH ") + guidToUString(*guid); current += EFI_DEP_OPCODE_SIZE + sizeof(EFI_GUID); break; case EFI_DEP_AND: - parsed += QObject::tr("\nAND"); + parsed += UString("\nAND"); current += EFI_DEP_OPCODE_SIZE; break; case EFI_DEP_OR: - parsed += QObject::tr("\nOR"); + parsed += UString("\nOR"); current += EFI_DEP_OPCODE_SIZE; break; case EFI_DEP_NOT: - parsed += QObject::tr("\nNOT"); + parsed += UString("\nNOT"); current += EFI_DEP_OPCODE_SIZE; break; case EFI_DEP_TRUE: - parsed += QObject::tr("\nTRUE"); + parsed += UString("\nTRUE"); current += EFI_DEP_OPCODE_SIZE; break; case EFI_DEP_FALSE: - parsed += QObject::tr("\nFALSE"); + parsed += UString("\nFALSE"); current += EFI_DEP_OPCODE_SIZE; break; case EFI_DEP_END: - parsed += QObject::tr("\nEND"); + parsed += UString("\nEND"); current += EFI_DEP_OPCODE_SIZE; // Check that END is the last opcode if (current - (const UINT8*)body.constData() < body.size()) { parsed.clear(); - msg(QObject::tr("parseDepexSectionBody: DEPEX section ends with non-END opcode"), index); + msg(UString("parseDepexSectionBody: DEPEX section ends with non-END opcode"), index); } break; default: - msg(QObject::tr("parseDepexSectionBody: unknown opcode"), index); - return ERR_SUCCESS; + msg(UString("parseDepexSectionBody: unknown opcode"), index); + return U_SUCCESS; break; } } // Add info - model->addInfo(index, QObject::tr("\nParsed expression:%1").arg(parsed)); + model->addInfo(index, UString("\nParsed expression:") + parsed); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseUiSectionBody(const QModelIndex & index) +USTATUS FfsParser::parseUiSectionBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; - QString text = QString::fromUtf16((const CHAR16*)model->body(index).constData()); + UString text = UString::fromUtf16((const CHAR16*)model->body(index).constData()); // Add info - model->addInfo(index, QObject::tr("\nText: %1").arg(text)); + model->addInfo(index, UString("\nText: ") + text); // Rename parent file model->setText(model->findParentOfType(index, Types::File), text); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseAprioriRawSection(const QByteArray & body, QString & parsed) +USTATUS FfsParser::parseAprioriRawSection(const UByteArray & body, UString & parsed) { // Sanity check if (body.size() % sizeof(EFI_GUID)) { - msg(QObject::tr("parseAprioriRawSection: apriori file has size is not a multiple of 16")); + msg(UString("parseAprioriRawSection: apriori file has size is not a multiple of 16")); } parsed.clear(); UINT32 count = body.size() / sizeof(EFI_GUID); if (count > 0) { for (UINT32 i = 0; i < count; i++) { const EFI_GUID* guid = (const EFI_GUID*)body.constData() + i; - parsed += QObject::tr("\n%1").arg(guidToQString(*guid)); + parsed += guidToUString(*guid).prepend("\n"); } } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseRawSectionBody(const QModelIndex & index) +USTATUS FfsParser::parseRawSectionBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Check for apriori file - QModelIndex parentFile = model->findParentOfType(index, Types::File); + UModelIndex parentFile = model->findParentOfType(index, Types::File); // Get parent file parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parentFile); - QByteArray parentFileGuid((const char*)&pdata.file.guid, sizeof(EFI_GUID)); + PARSING_DATA pdata = parsingDataFromUModelIndex(parentFile); + UByteArray parentFileGuid((const char*)&pdata.file.guid, sizeof(EFI_GUID)); if (parentFileGuid == EFI_PEI_APRIORI_FILE_GUID) { // PEI apriori file // Parse apriori file list - QString str; - STATUS result = parseAprioriRawSection(model->body(index), str); + UString str; + USTATUS result = parseAprioriRawSection(model->body(index), str); if (!result && !str.isEmpty()) - model->addInfo(index, QObject::tr("\nFile list:%1").arg(str)); + model->addInfo(index, UString("\nFile list:") + str); // Set parent file text - model->setText(parentFile, QObject::tr("PEI apriori file")); + model->setText(parentFile, UString("PEI apriori file")); - return ERR_SUCCESS; + return U_SUCCESS; } else if (parentFileGuid == EFI_DXE_APRIORI_FILE_GUID) { // DXE apriori file // Parse apriori file list - QString str; - STATUS result = parseAprioriRawSection(model->body(index), str); + UString str; + USTATUS result = parseAprioriRawSection(model->body(index), str); if (!result && !str.isEmpty()) - model->addInfo(index, QObject::tr("\nFile list:%1").arg(str)); + model->addInfo(index, UString("\nFile list:") + str); // Set parent file text - model->setText(parentFile, QObject::tr("DXE apriori file")); + model->setText(parentFile, UString("DXE apriori file")); - return ERR_SUCCESS; + return U_SUCCESS; } else if (parentFileGuid == NVRAM_NVAR_EXTERNAL_DEFAULTS_FILE_GUID) { // Parse NVAR area parseNvarStore(index); // Set parent file text - model->setText(parentFile, QObject::tr("NVRAM external defaults")); + model->setText(parentFile, UString("NVRAM external defaults")); } // Parse as raw area @@ -2745,154 +2739,156 @@ STATUS FfsParser::parseRawSectionBody(const QModelIndex & index) } -STATUS FfsParser::parsePeImageSectionBody(const QModelIndex & index) +USTATUS FfsParser::parsePeImageSectionBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get section body - QByteArray body = model->body(index); + UByteArray body = model->body(index); if ((UINT32)body.size() < sizeof(EFI_IMAGE_DOS_HEADER)) { - msg(QObject::tr("parsePeImageSectionBody: section body size is smaller than DOS header size"), index); - return ERR_SUCCESS; + msg(UString("parsePeImageSectionBody: section body size is smaller than DOS header size"), index); + return U_SUCCESS; } - QByteArray info; + UByteArray info; const EFI_IMAGE_DOS_HEADER* dosHeader = (const EFI_IMAGE_DOS_HEADER*)body.constData(); if (dosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) { - info += QObject::tr("\nDOS signature: %1h, invalid").hexarg2(dosHeader->e_magic, 4); - msg(QObject::tr("parsePeImageSectionBody: PE32 image with invalid DOS signature"), index); + info += usprintf("\nDOS signature: %04Xh, invalid", dosHeader->e_magic); + msg(UString("parsePeImageSectionBody: PE32 image with invalid DOS signature"), index); model->addInfo(index, info); - return ERR_SUCCESS; + return U_SUCCESS; } const EFI_IMAGE_PE_HEADER* peHeader = (EFI_IMAGE_PE_HEADER*)(body.constData() + dosHeader->e_lfanew); if (body.size() < (UINT8*)peHeader - (UINT8*)dosHeader) { - info += QObject::tr("\nDOS header: invalid"); - msg(QObject::tr("parsePeImageSectionBody: PE32 image with invalid DOS header"), index); + info += UString("\nDOS header: invalid"); + msg(UString("parsePeImageSectionBody: PE32 image with invalid DOS header"), index); model->addInfo(index, info); - return ERR_SUCCESS; + return U_SUCCESS; } if (peHeader->Signature != EFI_IMAGE_PE_SIGNATURE) { - info += QObject::tr("\nPE signature: %1h, invalid").hexarg2(peHeader->Signature, 8); - msg(QObject::tr("parsePeImageSectionBody: PE32 image with invalid PE signature"), index); + info += usprintf("\nPE signature: %08Xh, invalid", peHeader->Signature); + msg(UString("parsePeImageSectionBody: PE32 image with invalid PE signature"), index); model->addInfo(index, info); - return ERR_SUCCESS; + return U_SUCCESS; } const EFI_IMAGE_FILE_HEADER* imageFileHeader = (const EFI_IMAGE_FILE_HEADER*)(peHeader + 1); if (body.size() < (UINT8*)imageFileHeader - (UINT8*)dosHeader) { - info += QObject::tr("\nPE header: invalid"); - msg(QObject::tr("parsePeImageSectionBody: PE32 image with invalid PE header"), index); + info += UString("\nPE header: invalid"); + msg(UString("parsePeImageSectionBody: PE32 image with invalid PE header"), index); model->addInfo(index, info); - return ERR_SUCCESS; + return U_SUCCESS; } - info += QObject::tr("\nDOS signature: %1h\nPE signature: %2h\nMachine type: %3\nNumber of sections: %4\nCharacteristics: %5h") - .hexarg2(dosHeader->e_magic, 4) - .hexarg2(peHeader->Signature, 8) - .arg(machineTypeToQString(imageFileHeader->Machine)) - .arg(imageFileHeader->NumberOfSections) - .hexarg2(imageFileHeader->Characteristics, 4); + info += usprintf("\nDOS signature: %04Xh\nPE signature: %08Xh", + dosHeader->e_magic, + peHeader->Signature) + + machineTypeToUString(imageFileHeader->Machine).prepend("\nMachine type: ") + + usprintf("\nNumber of sections: %u\nCharacteristics: %04Xh", + imageFileHeader->NumberOfSections, + imageFileHeader->Characteristics); EFI_IMAGE_OPTIONAL_HEADER_POINTERS_UNION optionalHeader; optionalHeader.H32 = (const EFI_IMAGE_OPTIONAL_HEADER32*)(imageFileHeader + 1); if (body.size() < (UINT8*)optionalHeader.H32 - (UINT8*)dosHeader) { - info += QObject::tr("\nPE optional header: invalid"); - msg(QObject::tr("parsePeImageSectionBody: PE32 image with invalid PE optional header"), index); + info += UString("\nPE optional header: invalid"); + msg(UString("parsePeImageSectionBody: PE32 image with invalid PE optional header"), index); model->addInfo(index, info); - return ERR_SUCCESS; + return U_SUCCESS; } if (optionalHeader.H32->Magic == EFI_IMAGE_PE_OPTIONAL_HDR32_MAGIC) { - info += QObject::tr("\nOptional header signature: %1h\nSubsystem: %2h\nAddress of entry point: %3h\nBase of code: %4h\nImage base: %5h") - .hexarg2(optionalHeader.H32->Magic, 4) - .hexarg2(optionalHeader.H32->Subsystem, 4) - .hexarg(optionalHeader.H32->AddressOfEntryPoint) - .hexarg(optionalHeader.H32->BaseOfCode) - .hexarg(optionalHeader.H32->ImageBase); + info += usprintf("\nOptional header signature: %04Xh\nSubsystem: %04Xh\nAddress of entry point: %Xh\nBase of code: %Xh\nImage base: %Xh", + optionalHeader.H32->Magic, + optionalHeader.H32->Subsystem, + optionalHeader.H32->AddressOfEntryPoint, + optionalHeader.H32->BaseOfCode, + optionalHeader.H32->ImageBase); } else if (optionalHeader.H32->Magic == EFI_IMAGE_PE_OPTIONAL_HDR64_MAGIC) { - info += QObject::tr("\nOptional header signature: %1h\nSubsystem: %2h\nAddress of entry point: %3h\nBase of code: %4h\nImage base: %5h") - .hexarg2(optionalHeader.H64->Magic, 4) - .hexarg2(optionalHeader.H64->Subsystem, 4) - .hexarg(optionalHeader.H64->AddressOfEntryPoint) - .hexarg(optionalHeader.H64->BaseOfCode) - .hexarg(optionalHeader.H64->ImageBase); + info += usprintf("\nOptional header signature: %04Xh\nSubsystem: %04Xh\nAddress of entry point: %Xh\nBase of code: %Xh\nImage base: %Xh", + optionalHeader.H64->Magic, + optionalHeader.H64->Subsystem, + optionalHeader.H64->AddressOfEntryPoint, + optionalHeader.H64->BaseOfCode, + optionalHeader.H64->ImageBase); } else { - info += QObject::tr("\nOptional header signature: %1h, unknown").hexarg2(optionalHeader.H32->Magic, 4); - msg(QObject::tr("parsePeImageSectionBody: PE32 image with invalid optional PE header signature"), index); + info += usprintf("\nOptional header signature: %04Xh, unknown", optionalHeader.H32->Magic); + msg(UString("parsePeImageSectionBody: PE32 image with invalid optional PE header signature"), index); } model->addInfo(index, info); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseTeImageSectionBody(const QModelIndex & index) +USTATUS FfsParser::parseTeImageSectionBody(const UModelIndex & index) { // Check sanity if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get section body - QByteArray body = model->body(index); + UByteArray body = model->body(index); if ((UINT32)body.size() < sizeof(EFI_IMAGE_TE_HEADER)) { - msg(QObject::tr("parsePeImageSectionBody: section body size is smaller than TE header size"), index); - return ERR_SUCCESS; + msg(("parsePeImageSectionBody: section body size is smaller than TE header size"), index); + return U_SUCCESS; } - QByteArray info; + UByteArray info; const EFI_IMAGE_TE_HEADER* teHeader = (const EFI_IMAGE_TE_HEADER*)body.constData(); if (teHeader->Signature != EFI_IMAGE_TE_SIGNATURE) { - info += QObject::tr("\nSignature: %1h, invalid").hexarg2(teHeader->Signature, 4); - msg(QObject::tr("parseTeImageSectionBody: TE image with invalid TE signature"), index); + info += usprintf("\nSignature: %04Xh, invalid", teHeader->Signature); + msg(UString("parseTeImageSectionBody: TE image with invalid TE signature"), index); } else { - info += QObject::tr("\nSignature: %1h\nMachine type: %2\nNumber of sections: %3\nSubsystem: %4h\nStripped size: %5h (%6)\nBase of code: %7h\nAddress of entry point: %8h\nImage base: %9h\nAdjusted image base: %10h") - .hexarg2(teHeader->Signature, 4) - .arg(machineTypeToQString(teHeader->Machine)) - .arg(teHeader->NumberOfSections) - .hexarg2(teHeader->Subsystem, 2) - .hexarg(teHeader->StrippedSize).arg(teHeader->StrippedSize) - .hexarg(teHeader->BaseOfCode) - .hexarg(teHeader->AddressOfEntryPoint) - .hexarg(teHeader->ImageBase) - .hexarg(teHeader->ImageBase + teHeader->StrippedSize - sizeof(EFI_IMAGE_TE_HEADER)); + info += usprintf("\nSignature: %04Xh", teHeader->Signature) + + machineTypeToUString(teHeader->Machine).prepend("\nMachine type: ") + + usprintf("\nNumber of sections: %u\nSubsystem: %02Xh\nStripped size: %Xh (%u)\n" + "Base of code: %Xh\nAddress of entry point: %Xh\nImage base: %Xh\nAdjusted image base: %Xh", + teHeader->NumberOfSections, + teHeader->Subsystem, + teHeader->StrippedSize, teHeader->StrippedSize, + teHeader->BaseOfCode, + teHeader->AddressOfEntryPoint, + teHeader->ImageBase, + teHeader->ImageBase + teHeader->StrippedSize - sizeof(EFI_IMAGE_TE_HEADER)); } // Get data from parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); pdata.section.teImage.imageBase = teHeader->ImageBase; pdata.section.teImage.adjustedImageBase = teHeader->ImageBase + teHeader->StrippedSize - sizeof(EFI_IMAGE_TE_HEADER); // Update parsing data - model->setParsingData(index, parsingDataToQByteArray(pdata)); + model->setParsingData(index, parsingDataToUByteArray(pdata)); // Add TE info model->addInfo(index, info); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::performSecondPass(const QModelIndex & index) +USTATUS FfsParser::performSecondPass(const UModelIndex & index) { // Sanity check if (!index.isValid() || !lastVtf.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Check for compressed lastVtf if (model->compressed(lastVtf)) { - msg(QObject::tr("performSecondPass: the last VTF appears inside compressed item, the image may be damaged"), lastVtf); - return ERR_SUCCESS; + msg(UString("performSecondPass: the last VTF appears inside compressed item, the image may be damaged"), lastVtf); + return U_SUCCESS; } // Get parsing data for the last VTF - PARSING_DATA pdata = parsingDataFromQModelIndex(lastVtf); + PARSING_DATA pdata = parsingDataFromUModelIndex(lastVtf); // Calculate address difference const UINT32 vtfSize = model->header(lastVtf).size() + model->body(lastVtf).size() + model->tail(lastVtf).size(); @@ -2901,19 +2897,19 @@ STATUS FfsParser::performSecondPass(const QModelIndex & index) // Apply address information to index and all it's child items addMemoryAddressesRecursive(index, diff); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::addMemoryAddressesRecursive(const QModelIndex & index, const UINT32 diff) +USTATUS FfsParser::addMemoryAddressesRecursive(const UModelIndex & index, const UINT32 diff) { // Sanity check if (!index.isValid()) - return ERR_SUCCESS; + return U_SUCCESS; // Set address value for non-compressed data if (!model->compressed(index)) { // Get parsing data for the current item - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); // Check address sanity if ((const UINT64)diff + pdata.offset <= 0xFFFFFFFFUL) { @@ -2921,11 +2917,11 @@ STATUS FfsParser::addMemoryAddressesRecursive(const QModelIndex & index, const U pdata.address = diff + pdata.offset; UINT32 headerSize = model->header(index).size(); if (headerSize) { - model->addInfo(index, QObject::tr("\nHeader memory address: %1h").hexarg2(pdata.address, 8)); - model->addInfo(index, QObject::tr("\nData memory address: %1h").hexarg2(pdata.address + headerSize, 8)); + model->addInfo(index, usprintf("\nHeader memory address: %08Xh", pdata.address)); + model->addInfo(index, usprintf("\nData memory address: %08Xh", pdata.address + headerSize)); } else { - model->addInfo(index, QObject::tr("\nMemory address: %1h").hexarg2(pdata.address, 8)); + model->addInfo(index, usprintf("\nMemory address: %08Xh", pdata.address)); } // Special case of uncompressed TE image sections @@ -2938,13 +2934,13 @@ STATUS FfsParser::addMemoryAddressesRecursive(const QModelIndex & index, const U pdata.section.teImage.revision = 2; } else { - msg(QObject::tr("addMemoryAddressesRecursive: image base is neither original nor adjusted, it's likely a part of backup PEI volume or DXE volume, but can also be damaged"), index); + msg(UString("addMemoryAddressesRecursive: image base is neither original nor adjusted, it's likely a part of backup PEI volume or DXE volume, but can also be damaged"), index); pdata.section.teImage.revision = 0; } } // Set modified parsing data - model->setParsingData(index, parsingDataToQByteArray(pdata)); + model->setParsingData(index, parsingDataToUByteArray(pdata)); } } @@ -2953,51 +2949,51 @@ STATUS FfsParser::addMemoryAddressesRecursive(const QModelIndex & index, const U addMemoryAddressesRecursive(index.child(i, 0), diff); } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::addOffsetsRecursive(const QModelIndex & index) +USTATUS FfsParser::addOffsetsRecursive(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get parsing data for the current item - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); // Add current offset if the element is not compressed // or it's compressed, but it's parent isn't if ((!model->compressed(index)) || (index.parent().isValid() && !model->compressed(index.parent()))) { - model->addInfo(index, QObject::tr("Offset: %1h\n").hexarg(pdata.offset), false); + model->addInfo(index, usprintf("Offset: %Xh\n", pdata.offset), false); } //TODO: show FIT file fixed attribute correctly - model->addInfo(index, QObject::tr("\nCompressed: %1").arg(model->compressed(index) ? QObject::tr("Yes") : QObject::tr("No"))); - model->addInfo(index, QObject::tr("\nFixed: %1").arg(model->fixed(index) ? QObject::tr("Yes") : QObject::tr("No"))); + model->addInfo(index, usprintf("\nCompressed: %s", model->compressed(index) ? "Yes" : "No")); + model->addInfo(index, usprintf("\nFixed: %s", model->fixed(index) ? "Yes" : "No")); // Process child items for (int i = 0; i < model->rowCount(index); i++) { addOffsetsRecursive(index.child(i, 0)); } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseNvarStore(const QModelIndex & index) +USTATUS FfsParser::parseNvarStore(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get parsing data for the current item - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); UINT32 parentOffset = pdata.offset + model->header(index).size(); // Get item data - const QByteArray data = model->body(index); + const UByteArray data = model->body(index); // Rename parent file - model->setText(model->findParentOfType(index, Types::File), QObject::tr("NVAR store")); + model->setText(model->findParentOfType(index, Types::File), UString("NVAR store")); UINT32 offset = 0; UINT32 guidsInStore = 0; @@ -3022,14 +3018,14 @@ STATUS FfsParser::parseNvarStore(const QModelIndex & index) UINT32 extendedHeaderSize = 0; UINT8 extendedAttributes = 0; UINT64 timestamp = 0; - QByteArray hash; + UByteArray hash; UINT8 subtype = Subtypes::FullNvarEntry; - QString name; - QString text; - QByteArray header; - QByteArray body; - QByteArray tail; + UString name; + UString text; + UByteArray header; + UByteArray body; + UByteArray tail; UINT32 guidAreaSize = guidsInStore * sizeof(EFI_GUID); UINT32 unparsedSize = (UINT32)data.size() - offset - guidAreaSize; @@ -3043,48 +3039,47 @@ STATUS FfsParser::parseNvarStore(const QModelIndex & index) unparsedSize < entryHeader->Size) { // Check if the data left is a free space or a padding - QByteArray padding = data.mid(offset, unparsedSize); + UByteArray padding = data.mid(offset, unparsedSize); UINT8 type; if ((UINT32)padding.count(emptyByte) == unparsedSize) { // It's a free space - name = QObject::tr("Free space"); + name = ("Free space"); type = Types::FreeSpace; subtype = 0; } else { // Nothing is parsed yet, but the file is not empty if (!offset) { - msg(QObject::tr("parseNvarStore: file can't be parsed as NVAR variables store"), index); - return ERR_SUCCESS; + msg(UString("parseNvarStore: file can't be parsed as NVAR variables store"), index); + return U_SUCCESS; } // It's a padding - name = QObject::tr("Padding"); + name = UString("Padding"); type = Types::Padding; subtype = getPaddingType(padding); } // Get info - QString info = QObject::tr("Full size: %1h (%2)") - .hexarg(padding.size()).arg(padding.size()); + UString info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Construct parsing data pdata.offset = parentOffset + offset; // Add tree item - model->addItem(type, subtype, name, QString(), info, QByteArray(), padding, QByteArray(), false, parsingDataToQByteArray(pdata), index); + model->addItem(type, subtype, name, UString(), info, UByteArray(), padding, UByteArray(), false, parsingDataToUByteArray(pdata), index); // Add GUID store area - QByteArray guidArea = data.right(guidAreaSize); + UByteArray guidArea = data.right(guidAreaSize); // Get info - name = QObject::tr("GUID store area"); - info = QObject::tr("Full size: %1h (%2)\nGUIDs in store: %3") - .hexarg(guidArea.size()).arg(guidArea.size()) - .arg(guidsInStore); + name = UString("GUID store area"); + info = usprintf("Full size: %Xh (%u)\nGUIDs in store: %u", + guidArea.size(), guidArea.size(), + guidsInStore); // Construct parsing data pdata.offset = parentOffset + offset + padding.size(); // Add tree item - model->addItem(Types::Padding, getPaddingType(guidArea), name, QString(), info, QByteArray(), guidArea, QByteArray(), false, parsingDataToQByteArray(pdata), index); + model->addItem(Types::Padding, getPaddingType(guidArea), name, UString(), info, UByteArray(), guidArea, UByteArray(), false, parsingDataToUByteArray(pdata), index); - return ERR_SUCCESS; + return U_SUCCESS; } // Contruct generic header and body @@ -3170,11 +3165,11 @@ STATUS FfsParser::parseNvarStore(const QModelIndex & index) // Entry is data-only (nameless and GUIDless entry or link) if (entryHeader->Attributes & NVRAM_NVAR_ENTRY_DATA_ONLY) { // Data-only attribute is set isInvalidLink = true; - QModelIndex nvarIndex; + UModelIndex nvarIndex; // Search prevously added entries for a link to this variable //TODO:replace with linked lists for (int i = 0; i < model->rowCount(index); i++) { nvarIndex = index.child(i, 0); - PARSING_DATA nvarPdata = parsingDataFromQModelIndex(nvarIndex); + PARSING_DATA nvarPdata = parsingDataFromUModelIndex(nvarIndex); if (nvarPdata.nvar.isValid && nvarPdata.nvar.next + nvarPdata.offset - parentOffset == offset) { // Previous link is present and valid isInvalidLink = false; break; @@ -3201,17 +3196,17 @@ STATUS FfsParser::parseNvarStore(const QModelIndex & index) CHAR8* namePtr = (CHAR8*)(entryHeader + 1) + nameOffset; UINT32 nameSize = 0; if (entryHeader->Attributes & NVRAM_NVAR_ENTRY_ASCII_NAME) { // Name is stored as ASCII string of CHAR8s - text = QString(namePtr); + text = UString(namePtr); nameSize = text.length() + 1; } else { // Name is stored as UCS2 string of CHAR16s - text = QString::fromUtf16((CHAR16*)namePtr); + text = UString::fromUtf16((CHAR16*)namePtr); nameSize = (text.length() + 1) * 2; } // Get entry GUID if (entryHeader->Attributes & NVRAM_NVAR_ENTRY_GUID) { // GUID is strored in the variable itself - name = guidToQString(*(EFI_GUID*)(entryHeader + 1)); + name = guidToUString(*(EFI_GUID*)(entryHeader + 1)); } // GUID is stored in GUID list at the end of the store else { @@ -3221,7 +3216,7 @@ STATUS FfsParser::parseNvarStore(const QModelIndex & index) // The list begins at the end of the store and goes backwards const EFI_GUID* guidPtr = (const EFI_GUID*)(data.constData() + data.size()) - 1 - guidIndex; - name = guidToQString(*guidPtr); + name = guidToUString(*guidPtr); hasGuidIndex = true; } @@ -3230,59 +3225,58 @@ STATUS FfsParser::parseNvarStore(const QModelIndex & index) body = body.mid(nameOffset + nameSize); } parsing_done: - QString info; + UString info; // Rename invalid entries according to their types pdata.nvar.isValid = TRUE; if (isInvalid) { - name = QObject::tr("Invalid"); + name = UString("Invalid"); subtype = Subtypes::InvalidNvarEntry; pdata.nvar.isValid = FALSE; } else if (isInvalidLink) { - name = QObject::tr("Invalid link"); + name = UString("Invalid link"); subtype = Subtypes::InvalidLinkNvarEntry; pdata.nvar.isValid = FALSE; } else // Add GUID info for valid entries - info += QObject::tr("Variable GUID: %1\n").arg(name); + info += UString("Variable GUID: ") + name + UString("\n"); // Add GUID index information if (hasGuidIndex) - info += QObject::tr("GUID index: %1\n").arg(guidIndex); + info += usprintf("GUID index: %u\n", guidIndex); // Add header, body and extended data info - info += QObject::tr("Full size: %1h (%2)\nHeader size: %3h (%4)\nBody size: %5h (%6)") - .hexarg(entryHeader->Size).arg(entryHeader->Size) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()); + info += usprintf("Full size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)", + entryHeader->Size, entryHeader->Size, + header.size(), header.size(), + body.size(), body.size()); // Add attributes info - info += QObject::tr("\nAttributes: %1h").hexarg2(entryHeader->Attributes, 2); + info += usprintf("\nAttributes: %02Xh", entryHeader->Attributes); // Translate attributes to text if (entryHeader->Attributes && entryHeader->Attributes != 0xFF) - info += QObject::tr(" (%1)").arg(nvarAttributesToQString(entryHeader->Attributes)); + info += nvarAttributesToUString(entryHeader->Attributes).prepend(" (").append(")"); // Add next node info if (!isInvalid && entryHeader->Next != lastVariableFlag) - info += QObject::tr("\nNext node at offset: %1h").hexarg(parentOffset + offset + entryHeader->Next); + info += usprintf("\nNext node at offset: %Xh", parentOffset + offset + entryHeader->Next); // Add extended header info if (hasExtendedHeader) { - info += QObject::tr("\nExtended header size: %1h (%2)\nExtended attributes: %3h (%4)") - .hexarg(extendedHeaderSize).arg(extendedHeaderSize) - .hexarg2(extendedAttributes, 2) - .arg(nvarExtendedAttributesToQString(extendedAttributes)); + info += usprintf("\nExtended header size: %Xh (%u)\nExtended attributes: %Xh (", + extendedHeaderSize, extendedHeaderSize, + extendedAttributes) + nvarExtendedAttributesToUString(extendedAttributes).append(")"); // Checksum if (hasChecksum) - info += QObject::tr("\nChecksum: %1h%2").hexarg2(storedChecksum, 2) - .arg(calculatedChecksum ? QObject::tr(", invalid, should be %1h").hexarg2(0x100 - calculatedChecksum, 2) : QObject::tr(", valid")); + info += usprintf("\nChecksum: %02Xh", storedChecksum) + + (calculatedChecksum ? usprintf(", invalid, should be %02Xh", 0x100 - calculatedChecksum) : UString(", valid")); // Authentication data if (hasTimestampAndHash) { - info += QObject::tr("\nTimestamp: %1h\nHash: %2") - .hexarg(timestamp).arg(QString(hash.toHex().toUpper())); + info += usprintf("\nTimestamp: %Xh\nHash: ", + timestamp) + hash.toHex().toUpper(); } } @@ -3290,14 +3284,14 @@ parsing_done: pdata.offset = parentOffset + offset; // Add tree item - QModelIndex varIndex = model->addItem(Types::NvarEntry, subtype, name, text, info, header, body, tail, false, parsingDataToQByteArray(pdata), index); + UModelIndex varIndex = model->addItem(Types::NvarEntry, subtype, name, text, info, header, body, tail, false, parsingDataToUByteArray(pdata), index); // Show messages - if (msgUnknownExtDataFormat) msg(QObject::tr("parseNvarStore: unknown extended data format"), varIndex); - if (msgExtHeaderTooLong) msg(QObject::tr("parseNvarStore: extended header size (%1h) is greater than body size (%2h)") - .hexarg(extendedHeaderSize).hexarg(body.size()), varIndex); - if (msgExtDataTooShort) msg(QObject::tr("parseNvarStore: extended header size (%1h) is too small for timestamp and hash") - .hexarg(tail.size()), varIndex); + if (msgUnknownExtDataFormat) msg(UString("parseNvarStore: unknown extended data format"), varIndex); + if (msgExtHeaderTooLong) msg(usprintf("parseNvarStore: extended header size (%Xh) is greater than body size (%Xh)", + extendedHeaderSize, body.size()), varIndex); + if (msgExtDataTooShort) msg(usprintf("parseNvarStore: extended header size (%Xh) is too small for timestamp and hash", + tail.size()), varIndex); // Try parsing the entry data as NVAR storage if it begins with NVAR signature if ((subtype == Subtypes::DataNvarEntry || subtype == Subtypes::FullNvarEntry) @@ -3308,44 +3302,43 @@ parsing_done: offset += entryHeader->Size; } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseNvramVolumeBody(const QModelIndex & index) +USTATUS FfsParser::parseNvramVolumeBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); UINT32 parentOffset = pdata.offset + model->header(index).size(); // Get item data - QByteArray data = model->body(index); + UByteArray data = model->body(index); // Search for first store - STATUS result; + USTATUS result; UINT32 prevStoreOffset; result = findNextStore(index, data, parentOffset, 0, prevStoreOffset); if (result) return result; // First store is not at the beginning of volume body - QString name; - QString info; + UString name; + UString info; if (prevStoreOffset > 0) { // Get info - QByteArray padding = data.left(prevStoreOffset); - name = QObject::tr("Padding"); - info = QObject::tr("Full size: %1h (%2)") - .hexarg(padding.size()).arg(padding.size()); + UByteArray padding = data.left(prevStoreOffset); + name = UString("Padding"); + info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Construct parsing data pdata.offset = parentOffset; // Add tree item - model->addItem(Types::Padding, getPaddingType(padding), name, QString(), info, QByteArray(), padding, QByteArray(), true, parsingDataToQByteArray(pdata), index); + model->addItem(Types::Padding, getPaddingType(padding), name, UString(), info, UByteArray(), padding, UByteArray(), true, parsingDataToUByteArray(pdata), index); } // Search for and parse all stores @@ -3358,44 +3351,42 @@ STATUS FfsParser::parseNvramVolumeBody(const QModelIndex & index) if (storeOffset > prevStoreOffset + prevStoreSize) { UINT32 paddingOffset = prevStoreOffset + prevStoreSize; UINT32 paddingSize = storeOffset - paddingOffset; - QByteArray padding = data.mid(paddingOffset, paddingSize); + UByteArray padding = data.mid(paddingOffset, paddingSize); // Get info - name = QObject::tr("Padding"); - info = QObject::tr("Full size: %1h (%2)") - .hexarg(padding.size()).arg(padding.size()); + name = UString("Padding"); + info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Construct parsing data pdata.offset = parentOffset + paddingOffset; // Add tree item - model->addItem(Types::Padding, getPaddingType(padding), name, QString(), info, QByteArray(), padding, QByteArray(), true, parsingDataToQByteArray(pdata), index); + model->addItem(Types::Padding, getPaddingType(padding), name, UString(), info, UByteArray(), padding, UByteArray(), true, parsingDataToUByteArray(pdata), index); } // Get store size UINT32 storeSize = 0; result = getStoreSize(data, storeOffset, storeSize); if (result) { - msg(QObject::tr("parseNvramVolumeBody: getStoreSize failed with error \"%1\"").arg(errorCodeToQString(result)), index); + msg(UString("parseNvramVolumeBody: getStoreSize failed with error ") + errorCodeToUString(result), index); return result; } // Check that current store is fully present in input if (storeSize > (UINT32)data.size() || storeOffset + storeSize > (UINT32)data.size()) { // Mark the rest as padding and finish parsing - QByteArray padding = data.mid(storeOffset); + UByteArray padding = data.mid(storeOffset); // Get info - name = QObject::tr("Padding"); - info = QObject::tr("Full size: %1h (%2)") - .hexarg(padding.size()).arg(padding.size()); + name = ("Padding"); + info = ("Full size: %Xh (%u)", padding.size(), padding.size()); // Construct parsing data pdata.offset = parentOffset + storeOffset; // Add tree item - QModelIndex paddingIndex = model->addItem(Types::Padding, getPaddingType(padding), name, QString(), info, QByteArray(), padding, QByteArray(), true, parsingDataToQByteArray(pdata), index); - msg(QObject::tr("parseNvramVolumeBody: one of stores inside overlaps the end of data"), paddingIndex); + UModelIndex paddingIndex = model->addItem(Types::Padding, getPaddingType(padding), name, UString(), info, UByteArray(), padding, UByteArray(), true, parsingDataToUByteArray(pdata), index); + msg(UString("parseNvramVolumeBody: one of stores inside overlaps the end of data"), paddingIndex); // Update variables prevStoreOffset = storeOffset; @@ -3403,12 +3394,12 @@ STATUS FfsParser::parseNvramVolumeBody(const QModelIndex & index) break; } - QByteArray store = data.mid(storeOffset, storeSize); + UByteArray store = data.mid(storeOffset, storeSize); // Parse current store header - QModelIndex storeIndex; + UModelIndex storeIndex; result = parseStoreHeader(store, parentOffset + storeOffset, index, storeIndex); if (result) - msg(QObject::tr("parseNvramVolumeBody: store header parsing failed with error \"%1\"").arg(errorCodeToQString(result)), index); + msg(UString("parseNvramVolumeBody: store header parsing failed with error ") + errorCodeToUString(result), index); // Go to next store prevStoreOffset = storeOffset; @@ -3419,42 +3410,41 @@ STATUS FfsParser::parseNvramVolumeBody(const QModelIndex & index) // Padding/free space at the end storeOffset = prevStoreOffset + prevStoreSize; if ((UINT32)data.size() > storeOffset) { - QByteArray padding = data.mid(storeOffset); + UByteArray padding = data.mid(storeOffset); UINT8 type; UINT8 subtype; if (padding.count(pdata.emptyByte) == padding.size()) { // It's a free space - name = QObject::tr("Free space"); + name = UString("Free space"); type = Types::FreeSpace; subtype = 0; } else { // Nothing is parsed yet, but the file is not empty if (!storeOffset) { - msg(QObject::tr("parseNvramVolumeBody: can't be parsed as NVRAM volume"), index); - return ERR_SUCCESS; + msg(UString("parseNvramVolumeBody: can't be parsed as NVRAM volume"), index); + return U_SUCCESS; } // It's a padding - name = QObject::tr("Padding"); + name = UString("Padding"); type = Types::Padding; subtype = getPaddingType(padding); } // Add info - info = QObject::tr("Full size: %1h (%2)") - .hexarg(padding.size()).arg(padding.size()); + info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Construct parsing data pdata.offset = parentOffset + storeOffset; // Add tree item - model->addItem(type, subtype, name, QString(), info, QByteArray(), padding, QByteArray(), true, parsingDataToQByteArray(pdata), index); + model->addItem(type, subtype, name, UString(), info, UByteArray(), padding, UByteArray(), true, parsingDataToUByteArray(pdata), index); } // Parse bodies for (int i = 0; i < model->rowCount(index); i++) { - QModelIndex current = index.child(i, 0); + UModelIndex current = index.child(i, 0); switch (model->type(current)) { case Types::VssStore: case Types::FdcStore: parseVssStoreBody(current); break; @@ -3464,15 +3454,15 @@ STATUS FfsParser::parseNvramVolumeBody(const QModelIndex & index) } } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::findNextStore(const QModelIndex & index, const QByteArray & volume, const UINT32 parentOffset, const UINT32 storeOffset, UINT32 & nextStoreOffset) +USTATUS FfsParser::findNextStore(const UModelIndex & index, const UByteArray & volume, const UINT32 parentOffset, const UINT32 storeOffset, UINT32 & nextStoreOffset) { UINT32 dataSize = volume.size(); if (dataSize < sizeof(UINT32)) - return ERR_STORES_NOT_FOUND; + return U_STORES_NOT_FOUND; UINT32 offset = storeOffset; for (; offset < dataSize - sizeof(UINT32); offset++) { @@ -3480,11 +3470,11 @@ STATUS FfsParser::findNextStore(const QModelIndex & index, const QByteArray & vo if (*currentPos == NVRAM_VSS_STORE_SIGNATURE || *currentPos == NVRAM_APPLE_SVS_STORE_SIGNATURE) { //$VSS or $SVS signatures found, perform checks const VSS_VARIABLE_STORE_HEADER* vssHeader = (const VSS_VARIABLE_STORE_HEADER*)currentPos; if (vssHeader->Format != NVRAM_VSS_VARIABLE_STORE_FORMATTED) { - msg(QObject::tr("findNextStore: VSS store candidate at offset %1h skipped, has invalid format %2h").hexarg(parentOffset + offset).hexarg2(vssHeader->Format, 2), index); + msg(usprintf("findNextStore: VSS store candidate at offset %Xh skipped, has invalid format %02Xh", parentOffset + offset, vssHeader->Format), index); continue; } if (vssHeader->Size == 0 || vssHeader->Size == 0xFFFFFFFF) { - msg(QObject::tr("findNextStore: VSS store candidate at offset %1h skipped, has invalid size %2h").hexarg(parentOffset + offset).hexarg2(vssHeader->Size, 8), index); + msg(usprintf("findNextStore: VSS store candidate at offset %Xh skipped, has invalid size %Xh", parentOffset + offset, vssHeader->Size), index); continue; } // All checks passed, store found @@ -3493,7 +3483,7 @@ STATUS FfsParser::findNextStore(const QModelIndex & index, const QByteArray & vo else if (*currentPos == NVRAM_FDC_VOLUME_SIGNATURE) { //FDC signature found const FDC_VOLUME_HEADER* fdcHeader = (const FDC_VOLUME_HEADER*)currentPos; if (fdcHeader->Size == 0 || fdcHeader->Size == 0xFFFFFFFF) { - msg(QObject::tr("findNextStore: FDC store candidate at offset %1h skipped, has invalid size %2h").hexarg(parentOffset + offset).hexarg2(fdcHeader->Size, 8), index); + msg(usprintf("findNextStore: FDC store candidate at offset %Xh skipped, has invalid size %Xh", parentOffset + offset, fdcHeader->Size), index); continue; } // All checks passed, store found @@ -3502,7 +3492,7 @@ STATUS FfsParser::findNextStore(const QModelIndex & index, const QByteArray & vo else if (*currentPos == NVRAM_APPLE_FSYS_STORE_SIGNATURE || *currentPos == NVRAM_APPLE_GAID_STORE_SIGNATURE) { //Fsys or Gaid signature found const APPLE_FSYS_STORE_HEADER* fsysHeader = (const APPLE_FSYS_STORE_HEADER*)currentPos; if (fsysHeader->Size == 0 || fsysHeader->Size == 0xFFFF) { - msg(QObject::tr("findNextStore: Fsys store candidate at offset %1h skipped, has invalid size %2h").hexarg(parentOffset + offset).hexarg2(fsysHeader->Size, 4), index); + msg(usprintf("findNextStore: Fsys store candidate at offset %Xh skipped, has invalid size %Xh", parentOffset + offset, fsysHeader->Size), index); continue; } // All checks passed, store found @@ -3514,11 +3504,11 @@ STATUS FfsParser::findNextStore(const QModelIndex & index, const QByteArray & vo const EVSA_STORE_ENTRY* evsaHeader = (const EVSA_STORE_ENTRY*)(currentPos - 1); if (evsaHeader->Header.Type != NVRAM_EVSA_ENTRY_TYPE_STORE) { - msg(QObject::tr("findNextStore: EVSA store candidate at offset %1h skipped, has invalid type %2h").hexarg(parentOffset + offset - 4).hexarg2(evsaHeader->Header.Type, 2), index); + msg(usprintf("findNextStore: EVSA store candidate at offset %Xh skipped, has invalid type %02Xh", parentOffset + offset - 4, evsaHeader->Header.Type), index); continue; } if (evsaHeader->StoreSize == 0 || evsaHeader->StoreSize == 0xFFFFFFFF) { - msg(QObject::tr("findNextStore: EVSA store candidate at offset %1h skipped, has invalid size %2h").hexarg(parentOffset + offset).hexarg2(evsaHeader->StoreSize, 8), index); + msg(usprintf("findNextStore: EVSA store candidate at offset %Xh skipped, has invalid size %Xh", parentOffset + offset, evsaHeader->StoreSize), index); continue; } // All checks passed, store found @@ -3526,7 +3516,7 @@ STATUS FfsParser::findNextStore(const QModelIndex & index, const QByteArray & vo break; } else if (*currentPos == NVRAM_MAIN_STORE_VOLUME_GUID_DATA1 || *currentPos == EDKII_WORKING_BLOCK_SIGNATURE_GUID_DATA1) { //Possible FTW block signature found - QByteArray guid = QByteArray(volume.constData() + offset, sizeof(EFI_GUID)); + UByteArray guid = UByteArray(volume.constData() + offset, sizeof(EFI_GUID)); if (guid != NVRAM_MAIN_STORE_VOLUME_GUID && guid != EDKII_WORKING_BLOCK_SIGNATURE_GUID) // Check the whole signature continue; @@ -3534,14 +3524,14 @@ STATUS FfsParser::findNextStore(const QModelIndex & index, const QByteArray & vo const EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER32* ftwHeader = (const EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER32*)currentPos; if (ftwHeader->WriteQueueSize % 0x10 == 0x04) { // Header with 32 bit WriteQueueSize if (ftwHeader->WriteQueueSize == 0 || ftwHeader->WriteQueueSize == 0xFFFFFFFF) { - msg(QObject::tr("findNextStore: FTW block candidate at offset %1h skipped, has invalid body size %2h").hexarg(parentOffset + offset).hexarg2(ftwHeader->WriteQueueSize, 8), index); + msg(usprintf("findNextStore: FTW block candidate at offset %Xh skipped, has invalid body size %Xh", parentOffset + offset, ftwHeader->WriteQueueSize), index); continue; } } else if (ftwHeader->WriteQueueSize % 0x10 == 0x00) { // Header with 64 bit WriteQueueSize const EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64* ftw64Header = (const EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64*)currentPos; if (ftw64Header->WriteQueueSize == 0 || ftw64Header->WriteQueueSize >= 0xFFFFFFFF) { - msg(QObject::tr("findNextStore: FTW block candidate at offset %1h skipped, has invalid body size %2h").hexarg(parentOffset + offset).hexarg2(ftw64Header->WriteQueueSize, 16), index); + msg(usprintf("findNextStore: FTW block candidate at offset %Xh skipped, has invalid body size %Xh", parentOffset + offset, ftw64Header->WriteQueueSize), index); continue; } } @@ -3552,7 +3542,7 @@ STATUS FfsParser::findNextStore(const QModelIndex & index, const QByteArray & vo break; } else if (*currentPos == NVRAM_PHOENIX_FLASH_MAP_SIGNATURE_PART1) {// Phoenix SCT flash map - QByteArray signature = QByteArray(volume.constData() + offset, NVRAM_PHOENIX_FLASH_MAP_SIGNATURE_LENGTH); + UByteArray signature = UByteArray(volume.constData() + offset, NVRAM_PHOENIX_FLASH_MAP_SIGNATURE_LENGTH); if (signature != NVRAM_PHOENIX_FLASH_MAP_SIGNATURE) // Check the whole signature continue; @@ -3624,14 +3614,14 @@ STATUS FfsParser::findNextStore(const QModelIndex & index, const QByteArray & vo } // No more stores found if (offset >= dataSize - sizeof(UINT32)) - return ERR_STORES_NOT_FOUND; + return U_STORES_NOT_FOUND; nextStoreOffset = offset; - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::getStoreSize(const QByteArray & data, const UINT32 storeOffset, UINT32 & storeSize) +USTATUS FfsParser::getStoreSize(const UByteArray & data, const UINT32 storeOffset, UINT32 & storeSize) { const UINT32* signature = (const UINT32*)(data.constData() + storeOffset); if (*signature == NVRAM_VSS_STORE_SIGNATURE || *signature == NVRAM_APPLE_SVS_STORE_SIGNATURE) { @@ -3679,17 +3669,17 @@ STATUS FfsParser::getStoreSize(const QByteArray & data, const UINT32 storeOffset const INTEL_MICROCODE_HEADER* ucodeHeader = (const INTEL_MICROCODE_HEADER*)signature; storeSize = ucodeHeader->TotalSize; } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseVssStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseVssStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { const UINT32 dataSize = (const UINT32)store.size(); // Check store size if (dataSize < sizeof(VSS_VARIABLE_STORE_HEADER)) { - msg(QObject::tr("parseVssStoreHeader: volume body is too small even for VSS store header"), parent); - return ERR_SUCCESS; + msg(UString("parseVssStoreHeader: volume body is too small even for VSS store header"), parent); + return U_SUCCESS; } // Get VSS store header @@ -3697,48 +3687,48 @@ STATUS FfsParser::parseVssStoreHeader(const QByteArray & store, const UINT32 par // Check store size if (dataSize < vssStoreHeader->Size) { - msg(QObject::tr("parseVssStoreHeader: VSS store size %1h (%2) is greater than volume body size %3h (%4)") - .hexarg(vssStoreHeader->Size).arg(vssStoreHeader->Size) - .hexarg(dataSize).arg(dataSize), parent); - return ERR_SUCCESS; + msg(usprintf("parseVssStoreHeader: VSS store size %Xh (%u) is greater than volume body size %Xh (%u)", + vssStoreHeader->Size, vssStoreHeader->Size, + dataSize, dataSize), parent); + return U_SUCCESS; } // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Construct header and body - QByteArray header = store.left(sizeof(VSS_VARIABLE_STORE_HEADER)); - QByteArray body = store.mid(sizeof(VSS_VARIABLE_STORE_HEADER), vssStoreHeader->Size - sizeof(VSS_VARIABLE_STORE_HEADER)); + UByteArray header = store.left(sizeof(VSS_VARIABLE_STORE_HEADER)); + UByteArray body = store.mid(sizeof(VSS_VARIABLE_STORE_HEADER), vssStoreHeader->Size - sizeof(VSS_VARIABLE_STORE_HEADER)); // Add info bool isSvsStore = (vssStoreHeader->Signature == NVRAM_APPLE_SVS_STORE_SIGNATURE); - QString name = isSvsStore ? QObject::tr("SVS store") : QObject::tr("VSS store"); - QString info = QObject::tr("Signature: %1\nFull size: %2h (%3)\nHeader size: %4h (%5)\nBody size: %6h (%7)\nFormat: %8h\nState: %9h\nUnknown: %10h") - .arg(isSvsStore ? QObject::tr("$SVS") : QObject::tr("$VSS")) - .hexarg(vssStoreHeader->Size).arg(vssStoreHeader->Size) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()) - .hexarg2(vssStoreHeader->Format, 2) - .hexarg2(vssStoreHeader->State, 2) - .hexarg2(vssStoreHeader->Unknown, 4); + UString name = isSvsStore ? UString("SVS store") : UString("VSS store"); + UString info = usprintf("Signature: %s\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nFormat: %02Xh\nState: %02Xh\nUnknown: %04Xh", + isSvsStore ? "$SVS" : "$VSS", + vssStoreHeader->Size, vssStoreHeader->Size, + header.size(), header.size(), + body.size(), body.size(), + vssStoreHeader->Format, + vssStoreHeader->State, + vssStoreHeader->Unknown); // Add correct offset pdata.offset = parentOffset; // Add tree item - index = model->addItem(Types::VssStore, 0, name, QString(), info, header, body, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::VssStore, 0, name, UString(), info, header, body, UByteArray(), true, parsingDataToUByteArray(pdata), parent); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseFtwStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseFtwStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { const UINT32 dataSize = (const UINT32)store.size(); // Check store size if (dataSize < sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64)) { - msg(QObject::tr("parseFtwStoreHeader: volume body is too small even for FTW store header"), parent); - return ERR_SUCCESS; + msg(UString("parseFtwStoreHeader: volume body is too small even for FTW store header"), parent); + return U_SUCCESS; } // Get FTW block headers @@ -3757,56 +3747,55 @@ STATUS FfsParser::parseFtwStoreHeader(const QByteArray & store, const UINT32 par has32bitHeader = false; } if (dataSize < ftwBlockSize) { - msg(QObject::tr("parseFtwStoreHeader: FTW store size %1h (%2) is greater than volume body size %3h (%4)") - .hexarg(ftwBlockSize).arg(ftwBlockSize) - .hexarg(dataSize).arg(dataSize), parent); - return ERR_SUCCESS; + msg(usprintf("parseFtwStoreHeader: FTW store size %Xh (%u) is greater than volume body size %Xh (%u)", + ftwBlockSize, ftwBlockSize, + dataSize, dataSize), parent); + return U_SUCCESS; } // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Construct header and body UINT32 headerSize = has32bitHeader ? sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER32) : sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64); - QByteArray header = store.left(headerSize); - QByteArray body = store.mid(headerSize, ftwBlockSize - headerSize); + UByteArray header = store.left(headerSize); + UByteArray body = store.mid(headerSize, ftwBlockSize - headerSize); // Check block header checksum - QByteArray crcHeader = header; + UByteArray crcHeader = header; EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER32* crcFtwBlockHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER32*)header.data(); crcFtwBlockHeader->Crc = pdata.emptyByte ? 0xFFFFFFFF : 0; crcFtwBlockHeader->State = pdata.emptyByte ? 0xFF : 0; UINT32 calculatedCrc = crc32(0, (const UINT8*)crcFtwBlockHeader, headerSize); // Add info - QString name = QObject::tr("FTW store"); - QString info = QObject::tr("Signature: %1\nFull size: %2h (%3)\nHeader size: %4h (%5)\nBody size: %6h (%7)\nState: %8h\nHeader CRC32: %9") - .arg(guidToQString(ftw32BlockHeader->Signature)) - .hexarg(ftwBlockSize).arg(ftwBlockSize) - .hexarg(headerSize).arg(headerSize) - .hexarg(body.size()).arg(body.size()) - .hexarg2(ftw32BlockHeader->State, 2) - .arg(ftw32BlockHeader->Crc == calculatedCrc ? - QObject::tr("%1h, valid").hexarg2(ftw32BlockHeader->Crc, 8) : - QObject::tr("%1h, invalid, should be %2h").hexarg2(ftw32BlockHeader->Crc, 8).hexarg2(calculatedCrc, 8)); + UString name("FTW store"); + UString info = guidToUString(ftw32BlockHeader->Signature).prepend("Signature:") + + usprintf("\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nState: %02Xh\nHeader CRC32: ", + ftwBlockSize, ftwBlockSize, + headerSize, headerSize, + body.size(), body.size(), + ftw32BlockHeader->State) + + (ftw32BlockHeader->Crc == calculatedCrc ? usprintf("%08Xh, valid", ftw32BlockHeader->Crc) : + usprintf("%08Xh, invalid, should be %08Xh", ftw32BlockHeader->Crc, calculatedCrc)); // Add correct offset pdata.offset = parentOffset; // Add tree item - index = model->addItem(Types::FtwStore, 0, name, QString(), info, header, body, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::FtwStore, 0, name, UString(), info, header, body, UByteArray(), true, parsingDataToUByteArray(pdata), parent); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseFdcStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseFdcStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { const UINT32 dataSize = (const UINT32)store.size(); // Check store size if (dataSize < sizeof(FDC_VOLUME_HEADER)) { - msg(QObject::tr("parseFdcStoreHeader: volume body is too small even for FDC store header"), parent); - return ERR_SUCCESS; + msg(UString("parseFdcStoreHeader: volume body is too small even for FDC store header"), parent); + return U_SUCCESS; } // Get Fdc store header @@ -3814,10 +3803,10 @@ STATUS FfsParser::parseFdcStoreHeader(const QByteArray & store, const UINT32 par // Check store size if (dataSize < fdcStoreHeader->Size) { - msg(QObject::tr("parseFdcStoreHeader: FDC store size %1h (%2) is greater than volume body size %3h (%4)") - .hexarg(fdcStoreHeader->Size).arg(fdcStoreHeader->Size) - .hexarg(dataSize).arg(dataSize), parent); - return ERR_SUCCESS; + msg(usprintf("parseFdcStoreHeader: FDC store size %Xh (%u) is greater than volume body size %Xh (%u)", + fdcStoreHeader->Size, fdcStoreHeader->Size, + dataSize, dataSize), parent); + return U_SUCCESS; } // Determine internal volume header size @@ -3841,25 +3830,25 @@ STATUS FfsParser::parseFdcStoreHeader(const QByteArray & store, const UINT32 par // Check sanity of combined header size if (dataSize < headerSize) { - msg(QObject::tr("parseFdcStoreHeader: FDC store header size %1h (%2) is greater than volume body size %3h (%4)") - .hexarg2(fdcStoreHeader->Size, 8).arg(fdcStoreHeader->Size) - .hexarg2(dataSize, 8).arg(dataSize), parent); - return ERR_SUCCESS; + msg(usprintf("parseFdcStoreHeader: FDC store header size %Xh (%u) is greater than volume body size %Xh (%u)", + fdcStoreHeader->Size,fdcStoreHeader->Size, + dataSize, dataSize), parent); + return U_SUCCESS; } // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Construct header and body - QByteArray header = store.left(headerSize); - QByteArray body = store.mid(headerSize, fdcStoreHeader->Size - headerSize); + UByteArray header = store.left(headerSize); + UByteArray body = store.mid(headerSize, fdcStoreHeader->Size - headerSize); // Add info - QString name = QObject::tr("FDC store"); - QString info = QObject::tr("Signature: _FDC\nFull size: %1h (%2)\nHeader size: %3h (%4)\nBody size: %5h (%6)") - .hexarg(fdcStoreHeader->Size).arg(fdcStoreHeader->Size) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()); + UString name("FDC store"); + UString info = usprintf("Signature: _FDC\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)", + fdcStoreHeader->Size, fdcStoreHeader->Size, + header.size(), header.size(), + body.size(), body.size()); // TODO: add internal headers info @@ -3867,19 +3856,19 @@ STATUS FfsParser::parseFdcStoreHeader(const QByteArray & store, const UINT32 par pdata.offset = parentOffset; // Add tree item - index = model->addItem(Types::FdcStore, 0, name, QString(), info, header, body, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::FdcStore, 0, name, UString(), info, header, body, UByteArray(), true, parsingDataToUByteArray(pdata), parent); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseFsysStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseFsysStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { const UINT32 dataSize = (const UINT32)store.size(); // Check store size if (dataSize < sizeof(APPLE_FSYS_STORE_HEADER)) { - msg(QObject::tr("parseFsysStoreHeader: volume body is too small even for Fsys store header"), parent); - return ERR_SUCCESS; + msg(UString("parseFsysStoreHeader: volume body is too small even for Fsys store header"), parent); + return U_SUCCESS; } // Get Fsys store header @@ -3887,18 +3876,18 @@ STATUS FfsParser::parseFsysStoreHeader(const QByteArray & store, const UINT32 pa // Check store size if (dataSize < fsysStoreHeader->Size) { - msg(QObject::tr("parseFsysStoreHeader: Fsys store size %1h (%2) is greater than volume body size %3h (%4)") - .hexarg(fsysStoreHeader->Size).arg(fsysStoreHeader->Size) - .hexarg(dataSize).arg(dataSize), parent); - return ERR_SUCCESS; + msg(usprintf("parseFsysStoreHeader: Fsys store size %Xh (%u) is greater than volume body size %Xh (%u)", + fsysStoreHeader->Size, fsysStoreHeader->Size, + dataSize, dataSize), parent); + return U_SUCCESS; } // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Construct header and body - QByteArray header = store.left(sizeof(APPLE_FSYS_STORE_HEADER)); - QByteArray body = store.mid(sizeof(APPLE_FSYS_STORE_HEADER), fsysStoreHeader->Size - sizeof(APPLE_FSYS_STORE_HEADER) - sizeof(UINT32)); + UByteArray header = store.left(sizeof(APPLE_FSYS_STORE_HEADER)); + UByteArray body = store.mid(sizeof(APPLE_FSYS_STORE_HEADER), fsysStoreHeader->Size - sizeof(APPLE_FSYS_STORE_HEADER) - sizeof(UINT32)); // Check store checksum UINT32 storedCrc = *(UINT32*)store.right(sizeof(UINT32)).constBegin(); @@ -3906,33 +3895,33 @@ STATUS FfsParser::parseFsysStoreHeader(const QByteArray & store, const UINT32 pa // Add info bool isGaidStore = (fsysStoreHeader->Signature == NVRAM_APPLE_GAID_STORE_SIGNATURE); - QString name = isGaidStore ? QObject::tr("Gaid store") : QObject::tr("Fsys store"); - QString info = QObject::tr("Signature: %1\nFull size: %2h (%3)\nHeader size: %4h (%5)\nBody size: %6h (%7)\nUnknown0: %9h\nUnknown1: %10h\nCRC32: %11") - .arg(isGaidStore ? QObject::tr("Gaid") : QObject::tr("Fsys")) - .hexarg(fsysStoreHeader->Size).arg(fsysStoreHeader->Size) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()) - .hexarg2(fsysStoreHeader->Unknown0, 2) - .hexarg2(fsysStoreHeader->Unknown1, 8) - .arg(storedCrc == calculatedCrc ? QObject::tr("%1h, valid").hexarg2(storedCrc, 8) : QObject::tr("%1h, invalid, should be %2h").hexarg2(storedCrc, 8).hexarg2(calculatedCrc, 8)); + UString name = isGaidStore ? UString("Gaid store") : UString("Fsys store"); + UString info = usprintf("Signature: %s\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nUnknown0: %02Xh\nUnknown1: %08Xh\nCRC32: ", + isGaidStore ? "Gaid" : "Fsys", + fsysStoreHeader->Size, fsysStoreHeader->Size, + header.size(), header.size(), + body.size(), body.size(), + fsysStoreHeader->Unknown0, + fsysStoreHeader->Unknown1) + + (storedCrc == calculatedCrc ? usprintf("%08Xh, valid", storedCrc) : usprintf("%08Xh, invalid, should be %08Xh", storedCrc, calculatedCrc)); // Add correct offset pdata.offset = parentOffset; // Add tree item - index = model->addItem(Types::FsysStore, 0, name, QString(), info, header, body, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::FsysStore, 0, name, UString(), info, header, body, UByteArray(), true, parsingDataToUByteArray(pdata), parent); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseEvsaStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseEvsaStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { const UINT32 dataSize = (const UINT32)store.size(); // Check dataSize if (dataSize < sizeof(EVSA_STORE_ENTRY)) { - msg(QObject::tr("parseEvsaStoreHeader: volume body is too small even for EVSA store header"), parent); - return ERR_SUCCESS; + msg(UString("parseEvsaStoreHeader: volume body is too small even for EVSA store header"), parent); + return U_SUCCESS; } // Get EVSA store header @@ -3940,51 +3929,49 @@ STATUS FfsParser::parseEvsaStoreHeader(const QByteArray & store, const UINT32 pa // Check store size if (dataSize < evsaStoreHeader->StoreSize) { - msg(QObject::tr("parseEvsaStoreHeader: EVSA store size %1h (%2) is greater than volume body size %3h (%4)") - .hexarg(evsaStoreHeader->StoreSize).arg(evsaStoreHeader->StoreSize) - .hexarg(dataSize).arg(dataSize), parent); - return ERR_SUCCESS; + msg(usprintf("parseEvsaStoreHeader: EVSA store size %Xh (%u) is greater than volume body size %Xh (%u)", + evsaStoreHeader->StoreSize, evsaStoreHeader->StoreSize, + dataSize, dataSize), parent); + return U_SUCCESS; } // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Construct header and body - QByteArray header = store.left(evsaStoreHeader->Header.Size); - QByteArray body = store.mid(evsaStoreHeader->Header.Size, evsaStoreHeader->StoreSize - evsaStoreHeader->Header.Size); + UByteArray header = store.left(evsaStoreHeader->Header.Size); + UByteArray body = store.mid(evsaStoreHeader->Header.Size, evsaStoreHeader->StoreSize - evsaStoreHeader->Header.Size); // Recalculate checksum UINT8 calculated = calculateChecksum8(((const UINT8*)evsaStoreHeader) + 2, evsaStoreHeader->Header.Size - 2); // Add info - QString name = QObject::tr("EVSA store"); - QString info = QObject::tr("Signature: EVSA\nFull size: %1h (%2)\nHeader size: %3h (%4)\nBody size: %5h (%6)\nType: %7h\nChecksum: %8\nAttributes: %9h") - .hexarg(evsaStoreHeader->StoreSize).arg(evsaStoreHeader->StoreSize) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()) - .hexarg2(evsaStoreHeader->Header.Type, 2) - .arg(evsaStoreHeader->Header.Checksum == calculated ? - QObject::tr("%1h, valid").hexarg2(calculated, 2) : - QObject::tr("%1h, invalid, should be %2h").hexarg2(evsaStoreHeader->Header.Checksum, 2).hexarg2(calculated, 2)) - .hexarg2(evsaStoreHeader->Attributes, 8); + UString name("EVSA store"); + UString info = usprintf("Signature: EVSA\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nType: %02Xh\nAttributes: %08Xh\nChecksum: ", + evsaStoreHeader->StoreSize, evsaStoreHeader->StoreSize, + header.size(), header.size(), + body.size(), body.size(), + evsaStoreHeader->Header.Type, + evsaStoreHeader->Attributes) + + (evsaStoreHeader->Header.Checksum == calculated ? usprintf("%02Xh, valid", calculated) : usprintf("%02Xh, invalid, should be %02Xh", evsaStoreHeader->Header.Checksum, calculated)); // Add correct offset pdata.offset = parentOffset; // Add tree item - index = model->addItem(Types::EvsaStore, 0, name, QString(), info, header, body, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::EvsaStore, 0, name, UString(), info, header, body, UByteArray(), true, parsingDataToUByteArray(pdata), parent); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseFlashMapStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseFlashMapStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { const UINT32 dataSize = (const UINT32)store.size(); // Check data size if (dataSize < sizeof(PHOENIX_FLASH_MAP_HEADER)) { - msg(QObject::tr("parseFlashMapStoreHeader: volume body is too small even for FlashMap block header"), parent); - return ERR_SUCCESS; + msg(UString("parseFlashMapStoreHeader: volume body is too small even for FlashMap block header"), parent); + return U_SUCCESS; } // Get FlashMap block header @@ -3993,88 +3980,88 @@ STATUS FfsParser::parseFlashMapStoreHeader(const QByteArray & store, const UINT3 // Check store size UINT32 flashMapSize = sizeof(PHOENIX_FLASH_MAP_HEADER) + flashMapHeader->NumEntries * sizeof(PHOENIX_FLASH_MAP_ENTRY); if (dataSize < flashMapSize) { - msg(QObject::tr("parseFlashMapStoreHeader: FlashMap block size %1h (%2) is greater than volume body size %3h (%4)") - .hexarg(flashMapSize).arg(flashMapSize) - .hexarg(dataSize).arg(dataSize), parent); - return ERR_SUCCESS; + msg(usprintf("parseFlashMapStoreHeader: FlashMap block size %Xh (%u) is greater than volume body size %Xh (%u)", + flashMapSize, flashMapSize, + dataSize, dataSize), parent); + return U_SUCCESS; } // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Construct header and body - QByteArray header = store.left(sizeof(PHOENIX_FLASH_MAP_HEADER)); - QByteArray body = store.mid(sizeof(PHOENIX_FLASH_MAP_HEADER), flashMapSize - sizeof(PHOENIX_FLASH_MAP_HEADER)); + UByteArray header = store.left(sizeof(PHOENIX_FLASH_MAP_HEADER)); + UByteArray body = store.mid(sizeof(PHOENIX_FLASH_MAP_HEADER), flashMapSize - sizeof(PHOENIX_FLASH_MAP_HEADER)); // Add info - QString name = QObject::tr("Phoenix SCT flash map"); - QString info = QObject::tr("Signature: _FLASH_MAP\nFull size: %1h (%2)\nHeader size: %3h (%4)\nBody size: %5h (%6)\nNumber of entries: %7") - .hexarg(flashMapSize).arg(flashMapSize) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()) - .arg(flashMapHeader->NumEntries); + UString name("Phoenix SCT flash map"); + UString info = usprintf("Signature: _FLASH_MAP\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nNumber of entries: %u", + flashMapSize, flashMapSize, + header.size(), header.size(), + body.size(), body.size(), + flashMapHeader->NumEntries); // Add correct offset pdata.offset = parentOffset; // Add tree item - index = model->addItem(Types::FlashMapStore, 0, name, QString(), info, header, body, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::FlashMapStore, 0, name, UString(), info, header, body, UByteArray(), true, parsingDataToUByteArray(pdata), parent); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseCmdbStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseCmdbStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { const UINT32 dataSize = (const UINT32)store.size(); // Check store size if (dataSize < sizeof(PHOENIX_CMDB_HEADER)) { - msg(QObject::tr("parseCmdbStoreHeader: volume body is too small even for CMDB store header"), parent); - return ERR_SUCCESS; + msg(UString("parseCmdbStoreHeader: volume body is too small even for CMDB store header"), parent); + return U_SUCCESS; } UINT32 cmdbSize = NVRAM_PHOENIX_CMDB_SIZE; if (dataSize < cmdbSize) { - msg(QObject::tr("parseCmdbStoreHeader: CMDB store size %1h (%2) is greater than volume body size %3h (%4)") - .hexarg(cmdbSize).arg(cmdbSize) - .hexarg(dataSize).arg(dataSize), parent); - return ERR_SUCCESS; + msg(usprintf("parseCmdbStoreHeader: CMDB store size %Xh (%u) is greater than volume body size %Xh (%u)", + cmdbSize, cmdbSize, + dataSize, dataSize), parent); + return U_SUCCESS; } // Get store header const PHOENIX_CMDB_HEADER* cmdbHeader = (const PHOENIX_CMDB_HEADER*)store.constData(); // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Construct header and body - QByteArray header = store.left(cmdbHeader->TotalSize); - QByteArray body = store.mid(cmdbHeader->TotalSize, cmdbSize - cmdbHeader->TotalSize); + UByteArray header = store.left(cmdbHeader->TotalSize); + UByteArray body = store.mid(cmdbHeader->TotalSize, cmdbSize - cmdbHeader->TotalSize); // Add info - QString name = QObject::tr("CMDB store"); - QString info = QObject::tr("Signature: CMDB\nFull size: %1h (%2)\nHeader size: %3h (%4)\nBody size: %5h (%6)") - .hexarg(cmdbSize).arg(cmdbSize) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()); + UString name("CMDB store"); + UString info = usprintf("Signature: CMDB\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)", + cmdbSize, cmdbSize, + header.size(), header.size(), + body.size(), body.size()); // Add correct offset pdata.offset = parentOffset; // Add tree item - index = model->addItem(Types::CmdbStore, 0, name, QString(), info, header, body, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::CmdbStore, 0, name, UString(), info, header, body, UByteArray(), true, parsingDataToUByteArray(pdata), parent); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseSlicPubkeyHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseSlicPubkeyHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { const UINT32 dataSize = (const UINT32)store.size(); // Check data size if (dataSize < sizeof(OEM_ACTIVATION_PUBKEY)) { - msg(QObject::tr("parseSlicPubkeyHeader: volume body is too small even for SLIC pubkey header"), parent); - return ERR_SUCCESS; + msg(UString("parseSlicPubkeyHeader: volume body is too small even for SLIC pubkey header"), parent); + return U_SUCCESS; } // Get SLIC pubkey header @@ -4082,47 +4069,47 @@ STATUS FfsParser::parseSlicPubkeyHeader(const QByteArray & store, const UINT32 p // Check store size if (dataSize < pubkeyHeader->Size) { - msg(QObject::tr("parseSlicPubkeyHeader: SLIC pubkey size %1h (%2) is greater than volume body size %3h (%4)") - .hexarg(pubkeyHeader->Size).arg(pubkeyHeader->Size) - .hexarg(dataSize).arg(dataSize), parent); - return ERR_SUCCESS; + msg(usprintf("parseSlicPubkeyHeader: SLIC pubkey size %Xh (%u) is greater than volume body size %Xh (%u)", + pubkeyHeader->Size, pubkeyHeader->Size, + dataSize, dataSize), parent); + return U_SUCCESS; } // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Construct header and body - QByteArray header = store.left(sizeof(OEM_ACTIVATION_PUBKEY)); + UByteArray header = store.left(sizeof(OEM_ACTIVATION_PUBKEY)); // Add info - QString name = QObject::tr("SLIC pubkey"); - QString info = QObject::tr("Type: 0h\nFull size: %1h (%2)\nHeader size: %3h (%4)\nBody size: 0h (0)\n" - "Key type :%5h\nVersion: %6h\nAlgorithm: %7h\nMagic: RSA1\nBit length: %8h\nExponent:%9h") - .hexarg(pubkeyHeader->Size).arg(pubkeyHeader->Size) - .hexarg(header.size()).arg(header.size()) - .hexarg2(pubkeyHeader->KeyType, 2) - .hexarg2(pubkeyHeader->Version, 2) - .hexarg2(pubkeyHeader->Algorithm, 8) - .hexarg2(pubkeyHeader->BitLength, 8) - .hexarg2(pubkeyHeader->Exponent, 8); + UString name("SLIC pubkey"); + UString info = usprintf("Type: 0h\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: 0h (0)\n" + "Key type :%02Xh\nVersion: %02Xh\nAlgorithm: %08Xh\nMagic: RSA1\nBit length: %08Xh\nExponent:%08Xh", + pubkeyHeader->Size, pubkeyHeader->Size, + header.size(), header.size(), + pubkeyHeader->KeyType, + pubkeyHeader->Version, + pubkeyHeader->Algorithm, + pubkeyHeader->BitLength, + pubkeyHeader->Exponent); // Add correct offset pdata.offset = parentOffset; // Add tree item - index = model->addItem(Types::SlicData, Subtypes::PubkeySlicData, name, QString(), info, header, QByteArray(), QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::SlicData, Subtypes::PubkeySlicData, name, UString(), info, header, UByteArray(), UByteArray(), true, parsingDataToUByteArray(pdata), parent); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseSlicMarkerHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseSlicMarkerHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { const UINT32 dataSize = (const UINT32)store.size(); // Check data size if (dataSize < sizeof(OEM_ACTIVATION_MARKER)) { - msg(QObject::tr("parseSlicMarkerHeader: volume body is too small even for SLIC marker header"), parent); - return ERR_SUCCESS; + msg(UString("parseSlicMarkerHeader: volume body is too small even for SLIC marker header"), parent); + return U_SUCCESS; } // Get SLIC marker header @@ -4130,46 +4117,46 @@ STATUS FfsParser::parseSlicMarkerHeader(const QByteArray & store, const UINT32 p // Check store size if (dataSize < markerHeader->Size) { - msg(QObject::tr("parseSlicMarkerHeader: SLIC marker size %1h (%2) is greater than volume body size %3h (%4)") - .hexarg(markerHeader->Size).arg(markerHeader->Size) - .hexarg(dataSize).arg(dataSize), parent); - return ERR_SUCCESS; + msg(usprintf("parseSlicMarkerHeader: SLIC marker size %Xh (%u) is greater than volume body size %Xh (%u)", + markerHeader->Size, markerHeader->Size, + dataSize, dataSize), parent); + return U_SUCCESS; } // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Construct header and body - QByteArray header = store.left(sizeof(OEM_ACTIVATION_MARKER)); + UByteArray header = store.left(sizeof(OEM_ACTIVATION_MARKER)); // Add info - QString name = QObject::tr("SLIC marker"); - QString info = QObject::tr("Type: 1h\nFull size: %1h (%2)\nHeader size: %3h (%4)\nBody size: 0h (0)\n" - "Version :%5h\nOEM ID: %6\nOEM table ID: %7\nWindows flag: WINDOWS\nSLIC version: %8h") - .hexarg(markerHeader->Size).arg(markerHeader->Size) - .hexarg(header.size()).arg(header.size()) - .hexarg2(markerHeader->Version, 8) - .arg(QString::fromLatin1((const char*)&(markerHeader->OemId), sizeof(markerHeader->OemId))) - .arg(QString::fromLatin1((const char*)&(markerHeader->OemTableId), sizeof(markerHeader->OemTableId))) - .hexarg2(markerHeader->SlicVersion, 8); + UString name("SLIC marker"); + UString info = usprintf("Type: 1h\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: 0h (0)\n" + "Version :%08Xh\nOEM ID: %s\nOEM table ID: %s\nWindows flag: WINDOWS\nSLIC version: %08Xh", + markerHeader->Size, markerHeader->Size, + header.size(), header.size(), + markerHeader->Version, + (const char*)&(markerHeader->OemId), + (const char*)&(markerHeader->OemTableId), + markerHeader->SlicVersion); // Add correct offset pdata.offset = parentOffset; // Add tree item - index = model->addItem(Types::SlicData, Subtypes::MarkerSlicData, name, QString(), info, header, QByteArray(), QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::SlicData, Subtypes::MarkerSlicData, name, UString(), info, header, UByteArray(), UByteArray(), true, parsingDataToUByteArray(pdata), parent); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseIntelMicrocodeHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseIntelMicrocodeHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { const UINT32 dataSize = (const UINT32)store.size(); // Check data size if (dataSize < sizeof(INTEL_MICROCODE_HEADER)) { - msg(QObject::tr("parseIntelMicrocodeHeader: volume body is too small even for Intel microcode header"), parent); - return ERR_SUCCESS; + msg(UString("parseIntelMicrocodeHeader: volume body is too small even for Intel microcode header"), parent); + return U_SUCCESS; } // Get Intel microcode header @@ -4177,51 +4164,51 @@ STATUS FfsParser::parseIntelMicrocodeHeader(const QByteArray & store, const UINT // Check store size if (dataSize < ucodeHeader->TotalSize) { - msg(QObject::tr("parseIntelMicrocodeHeader: Intel microcode size %1h (%2) is greater than volume body size %3h (%4)") - .hexarg(ucodeHeader->TotalSize).arg(ucodeHeader->TotalSize) - .hexarg(dataSize).arg(dataSize), parent); - return ERR_SUCCESS; + msg(usprintf("parseIntelMicrocodeHeader: Intel microcode size %Xh (%u) is greater than volume body size %Xh (%u)", + ucodeHeader->TotalSize, ucodeHeader->TotalSize, + dataSize, dataSize), parent); + return U_SUCCESS; } // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(parent); + PARSING_DATA pdata = parsingDataFromUModelIndex(parent); // Construct header and body - QByteArray header = store.left(sizeof(INTEL_MICROCODE_HEADER)); - QByteArray body = store.mid(sizeof(INTEL_MICROCODE_HEADER), ucodeHeader->DataSize); + UByteArray header = store.left(sizeof(INTEL_MICROCODE_HEADER)); + UByteArray body = store.mid(sizeof(INTEL_MICROCODE_HEADER), ucodeHeader->DataSize); //TODO: recalculate checksum // Add info - QString name = QObject::tr("Intel microcode"); - QString info = QObject::tr("Revision: 1h\nFull size: %1h (%2)\nHeader size: %3h (%4)\nBody size: %5h (%6)\n" - "Date: %7h\nCPU signature: %8h\nChecksum: %9h\nLoader revision: %10h\nCPU flags: %11h") - .hexarg(ucodeHeader->TotalSize).arg(ucodeHeader->TotalSize) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()) - .hexarg2(ucodeHeader->Date, 8) - .hexarg2(ucodeHeader->CpuSignature, 8) - .hexarg2(ucodeHeader->Checksum, 8) - .hexarg2(ucodeHeader->LoaderRevision, 8) - .hexarg2(ucodeHeader->CpuFlags, 8); + UString name("Intel microcode"); + UString info = usprintf("Revision: 1h\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\n" + "Date: %08Xh\nCPU signature: %08Xh\nChecksum: %08Xh\nLoader revision: %08Xh\nCPU flags: %08Xh", + ucodeHeader->TotalSize, ucodeHeader->TotalSize, + header.size(), header.size(), + body.size(), body.size(), + ucodeHeader->Date, + ucodeHeader->CpuSignature, + ucodeHeader->Checksum, + ucodeHeader->LoaderRevision, + ucodeHeader->CpuFlags); // Add correct offset pdata.offset = parentOffset; // Add tree item - index = model->addItem(Types::Microcode, Subtypes::IntelMicrocode, name, QString(), info, header, body, QByteArray(), true, parsingDataToQByteArray(pdata), parent); + index = model->addItem(Types::Microcode, Subtypes::IntelMicrocode, name, UString(), info, header, body, UByteArray(), true, parsingDataToUByteArray(pdata), parent); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index) +USTATUS FfsParser::parseStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index) { const UINT32 dataSize = (const UINT32)store.size(); const UINT32* signature = (const UINT32*)store.constData(); // Check store size if (dataSize < sizeof(UINT32)) { - msg(QObject::tr("parseStoreHeader: volume body is too small even for store signature"), parent); - return ERR_SUCCESS; + msg(UString("parseStoreHeader: volume body is too small even for store signature"), parent); + return U_SUCCESS; } // Check signature and run parser function needed @@ -4257,26 +4244,26 @@ STATUS FfsParser::parseStoreHeader(const QByteArray & store, const UINT32 parent else if (*signature == INTEL_MICROCODE_HEADER_VERSION) return parseIntelMicrocodeHeader(store, parentOffset, parent, index); - msg(QObject::tr("parseStoreHeader: don't know how to parse a header with signature %1h").hexarg2(*signature, 8), parent); - return ERR_SUCCESS; + msg(usprintf("parseStoreHeader: don't know how to parse a header with signature %08Xh", *signature), parent); + return U_SUCCESS; } -STATUS FfsParser::parseVssStoreBody(const QModelIndex & index) +USTATUS FfsParser::parseVssStoreBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get parsing data for the current item - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); UINT32 parentOffset = pdata.offset + model->header(index).size(); - const QByteArray data = model->body(index); + const UByteArray data = model->body(index); // Check that the is enough space for variable header const UINT32 dataSize = (UINT32)data.size(); if (dataSize < sizeof(VSS_VARIABLE_HEADER)) { - msg(QObject::tr("parseVssStoreBody: store body is too small even for VSS variable header"), index); - return ERR_SUCCESS; + msg(UString("parseVssStoreBody: store body is too small even for VSS variable header"), index); + return U_SUCCESS; } UINT32 offset = 0; @@ -4294,12 +4281,12 @@ STATUS FfsParser::parseVssStoreBody(const QModelIndex & index) UINT32 pubKeyIndex = 0; UINT8 subtype = 0; - QString name; - QString text; + UString name; + UString text; EFI_GUID* variableGuid; CHAR16* variableName; - QByteArray header; - QByteArray body; + UByteArray header; + UByteArray body; UINT32 unparsedSize = dataSize - offset; @@ -4377,78 +4364,72 @@ STATUS FfsParser::parseVssStoreBody(const QModelIndex & index) // Can't parse further, add the last element and break the loop if (!variableSize) { // Check if the data left is a free space or a padding - QByteArray padding = data.mid(offset, unparsedSize); + UByteArray padding = data.mid(offset, unparsedSize); UINT8 type; if (padding.count(pdata.emptyByte) == padding.size()) { // It's a free space - name = QObject::tr("Free space"); + name = UString("Free space"); type = Types::FreeSpace; subtype = 0; } else { // Nothing is parsed yet, but the store is not empty if (!offset) { - msg(QObject::tr("parseVssStoreBody: store can't be parsed as VSS store"), index); - return ERR_SUCCESS; + msg(UString("parseVssStoreBody: store can't be parsed as VSS store"), index); + return U_SUCCESS; } // It's a padding - name = QObject::tr("Padding"); + name = UString("Padding"); type = Types::Padding; subtype = getPaddingType(padding); } // Get info - QString info = QObject::tr("Full size: %1h (%2)") - .hexarg(padding.size()).arg(padding.size()); + UString info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Construct parsing data pdata.offset = parentOffset + offset; // Add tree item - model->addItem(type, subtype, name, QString(), info, QByteArray(), padding, QByteArray(), false, parsingDataToQByteArray(pdata), index); + model->addItem(type, subtype, name, UString(), info, UByteArray(), padding, UByteArray(), false, parsingDataToUByteArray(pdata), index); - return ERR_SUCCESS; + return U_SUCCESS; } - QString info; + UString info; // Rename invalid variables if (isInvalid) { - name = QObject::tr("Invalid"); + name = UString("Invalid"); } else { // Add GUID and text for valid variables - name = guidToQString(*variableGuid); - info += QObject::tr("Variable GUID: %1\n").arg(name); - text = QString::fromUtf16(variableName); + name = guidToUString(*variableGuid); + info += UString("Variable GUID: ") + name + UString("\n"); + text = UString::fromUtf16(variableName); } - // Add header, body and extended data info - info += QObject::tr("Full size: %1h (%2)\nHeader size %3h (%4)\nBody size: %5h (%6)") - .hexarg(variableSize).arg(variableSize) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()); - - // Add state info - info += QObject::tr("\nState: %1h").hexarg2(variableHeader->State, 2); - - // Add attributes info - info += QObject::tr("\nAttributes: %1h (%2)").hexarg2(variableHeader->Attributes, 8).arg(vssAttributesToQString(variableHeader->Attributes)); + // Add info + info += usprintf("Full size: %Xh (%u)\nHeader size %Xh (%u)\nBody size: %Xh (%u)\nState: %02Xh\nAttributes: %08Xh (", + variableSize, variableSize, + header.size(), header.size(), + body.size(), body.size(), + variableHeader->State, + variableHeader->Attributes) + vssAttributesToUString(variableHeader->Attributes).append(")"); // Set subtype and add related info if (isInvalid) subtype = Subtypes::InvalidVssEntry; else if (isAuthenticated) { subtype = Subtypes::AuthVssEntry; - info += QObject::tr("\nMonotonic counter: %1h\nTimestamp: %2\nPubKey index: %3") - .hexarg(monotonicCounter).arg(efiTimeToQString(timestamp)).arg(pubKeyIndex); - + info += usprintf("\nMonotonic counter: %Xh\nTimestamp:", monotonicCounter) + efiTimeToUString(timestamp) + + usprintf("\nPubKey index: %u", pubKeyIndex); } else if (isAppleCrc32) { subtype = Subtypes::AppleVssEntry; - info += QObject::tr("\nData checksum: %1h%2").hexarg2(storedCrc32, 8) - .arg(storedCrc32 == calculatedCrc32 ? QObject::tr(", valid") : QObject::tr(", invalid, should be %1h").hexarg2(calculatedCrc32,8)); + info += usprintf("\nData checksum: %08X", storedCrc32) + + (storedCrc32 == calculatedCrc32 ? UString(", valid") : usprintf(", invalid, should be %08Xh", calculatedCrc32)); } else subtype = Subtypes::StandardVssEntry; @@ -4457,25 +4438,25 @@ STATUS FfsParser::parseVssStoreBody(const QModelIndex & index) pdata.offset = parentOffset + offset; // Add tree item - model->addItem(Types::VssEntry, subtype, name, text, info, header, body, QByteArray(), false, parsingDataToQByteArray(pdata), index); + model->addItem(Types::VssEntry, subtype, name, text, info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), index); // Move to next variable offset += variableSize; } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseFsysStoreBody(const QModelIndex & index) +USTATUS FfsParser::parseFsysStoreBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get parsing data for the current item - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); UINT32 parentOffset = pdata.offset + model->header(index).size(); - const QByteArray data = model->body(index); + const UByteArray data = model->body(index); // Check that the is enough space for variable header const UINT32 dataSize = (UINT32)data.size(); @@ -4493,36 +4474,34 @@ STATUS FfsParser::parseFsysStoreBody(const QModelIndex & index) variableSize = nameSize + sizeof(UINT8); } - QByteArray name; + UByteArray name; if (variableSize) { name = data.mid(offset + sizeof(UINT8), nameSize); // Check for EOF variable if (nameSize == 3 && name[0] == 'E' && name[1] == 'O' && name[2] == 'F') { // There is no data afterward, add EOF variable and free space and return - QByteArray header = data.mid(offset, sizeof(UINT8) + nameSize); - QString info = QObject::tr("Full size: %1h (%2)") - .hexarg(header.size()).arg(header.size()); + UByteArray header = data.mid(offset, sizeof(UINT8) + nameSize); + UString info = usprintf("Full size: %Xh (%u)", header.size(), header.size()); // Add correct offset to parsing data pdata.offset = parentOffset + offset; // Add EOF tree item - model->addItem(Types::FsysEntry, 0, name, QString(), info, header, QByteArray(), QByteArray(), false, parsingDataToQByteArray(pdata), index); + model->addItem(Types::FsysEntry, 0, name, UString(), info, header, UByteArray(), UByteArray(), false, parsingDataToUByteArray(pdata), index); // Add free space offset += header.size(); unparsedSize = dataSize - offset; - QByteArray body = data.mid(offset); - info = QObject::tr("Full size: %1h (%2)") - .hexarg(body.size()).arg(body.size()); + UByteArray body = data.mid(offset); + info = usprintf("Full size: %Xh (%u)", body.size(), body.size()); // Add correct offset to parsing data pdata.offset = parentOffset + offset; // Add free space tree item - model->addItem(Types::FreeSpace, 0, QObject::tr("Free space"), QString(), info, QByteArray(), body, QByteArray(), false, parsingDataToQByteArray(pdata), index); + model->addItem(Types::FreeSpace, 0, UString("Free space"), UString(), info, UByteArray(), body, UByteArray(), false, parsingDataToUByteArray(pdata), index); - return ERR_SUCCESS; + return U_SUCCESS; } } @@ -4533,71 +4512,70 @@ STATUS FfsParser::parseFsysStoreBody(const QModelIndex & index) } else { // Last variable is bad, add the rest as padding and return - QByteArray body = data.mid(offset); - QString info = QObject::tr("Full size: %1h (%2)") - .hexarg(body.size()).arg(body.size()); + UByteArray body = data.mid(offset); + UString info = usprintf("Full size: %Xh (%u)", body.size(), body.size()); // Add correct offset to parsing data pdata.offset = parentOffset + offset; // Add free space tree item - model->addItem(Types::Padding, getPaddingType(body), QObject::tr("Padding"), QString(), info, QByteArray(), body, QByteArray(), false, parsingDataToQByteArray(pdata), index); + model->addItem(Types::Padding, getPaddingType(body), UString("Padding"), UString(), info, UByteArray(), body, UByteArray(), false, parsingDataToUByteArray(pdata), index); // Show message - msg(QObject::tr("parseFsysStoreBody: next variable appears too big, added as padding"), index); + msg(UString("parseFsysStoreBody: next variable appears too big, added as padding"), index); - return ERR_SUCCESS; + return U_SUCCESS; } // Construct header and body - QByteArray header = data.mid(offset, sizeof(UINT8) + nameSize + sizeof(UINT16)); - QByteArray body = data.mid(offset + sizeof(UINT8) + nameSize + sizeof(UINT16), dataSize); + UByteArray header = data.mid(offset, sizeof(UINT8) + nameSize + sizeof(UINT16)); + UByteArray body = data.mid(offset + sizeof(UINT8) + nameSize + sizeof(UINT16), dataSize); // Add info - QString info = QObject::tr("Full size: %1h (%2)\nHeader size %3h (%4)\nBody size: %5h (%6)") - .hexarg(variableSize).arg(variableSize) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()); + UString info = usprintf("Full size: %Xh (%u)\nHeader size %Xh (%u)\nBody size: %Xh (%u)", + variableSize, variableSize, + header.size(), header.size(), + body.size(), body.size()); // Add correct offset to parsing data pdata.offset = parentOffset + offset; // Add tree item - model->addItem(Types::FsysEntry, 0, name, QString(), info, header, body, QByteArray(), false, parsingDataToQByteArray(pdata), index); + model->addItem(Types::FsysEntry, 0, name, UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), index); // Move to next variable offset += variableSize; } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseEvsaStoreBody(const QModelIndex & index) +USTATUS FfsParser::parseEvsaStoreBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get parsing data for the current item - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); UINT32 parentOffset = pdata.offset + model->header(index).size(); - const QByteArray data = model->body(index); + const UByteArray data = model->body(index); // Check that the is enough space for entry header const UINT32 dataSize = (UINT32)data.size(); UINT32 offset = 0; std::map guidMap; - std::map nameMap; + std::map nameMap; // Parse all entries UINT32 unparsedSize = dataSize; while (unparsedSize) { UINT32 variableSize = 0; - QString name; - QString info; - QByteArray header; - QByteArray body; + UString name; + UString info; + UByteArray header; + UByteArray body; UINT8 subtype; UINT8 calculated; @@ -4606,16 +4584,15 @@ STATUS FfsParser::parseEvsaStoreBody(const QModelIndex & index) // Check entry size variableSize = sizeof(EVSA_ENTRY_HEADER); if (unparsedSize < variableSize || unparsedSize < entryHeader->Size) { - QByteArray body = data.mid(offset); - QString info = QObject::tr("Full size: %1h (%2)") - .hexarg(body.size()).arg(body.size()); + UByteArray body = data.mid(offset); + UString info = usprintf("Full size: %Xh (%u)", body.size(), body.size()); // Checke type - QString name = QObject::tr("Free space"); + UString name("Free space"); UINT8 type = Types::FreeSpace; UINT8 subtype = 0; if (getPaddingType(body) == Subtypes::DataPadding) { - name = QObject::tr("Padding"); + name = UString("Padding"); type = Types::Padding; subtype = Subtypes::DataPadding; } @@ -4624,11 +4601,11 @@ STATUS FfsParser::parseEvsaStoreBody(const QModelIndex & index) pdata.offset = parentOffset + offset; // Add free space tree item - QModelIndex itemIndex = model->addItem(type, subtype, name, QString(), info, QByteArray(), body, QByteArray(), false, parsingDataToQByteArray(pdata), index); + UModelIndex itemIndex = model->addItem(type, subtype, name, UString(), info, UByteArray(), body, UByteArray(), false, parsingDataToUByteArray(pdata), index); // Show message if (type == Types::Padding) - msg(QObject::tr("parseEvsaStoreBody: variable parsing failed, rest of unparsed store added as padding"), itemIndex); + msg(UString("parseEvsaStoreBody: variable parsing failed, rest of unparsed store added as padding"), itemIndex); break; } @@ -4644,17 +4621,15 @@ STATUS FfsParser::parseEvsaStoreBody(const QModelIndex & index) header = data.mid(offset, sizeof(EVSA_GUID_ENTRY)); body = data.mid(offset + sizeof(EVSA_GUID_ENTRY), guidHeader->Header.Size - sizeof(EVSA_GUID_ENTRY)); EFI_GUID guid = *(EFI_GUID*)body.constData(); - name = guidToQString(guid); - info = QObject::tr("GUID: %1\nFull size: %2h (%3)\nHeader size %4h (%5)\nBody size: %6h (%7)\nType: %8h\nChecksum: %9\nGuidId: %10h") - .arg(name) - .hexarg(variableSize).arg(variableSize) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()) - .hexarg2(guidHeader->Header.Type, 2) - .arg(guidHeader->Header.Checksum == calculated ? - QObject::tr("%1h, valid").hexarg2(calculated, 2) : - QObject::tr("%1h, invalid, should be %2h").hexarg2(guidHeader->Header.Checksum, 2).hexarg2(calculated, 2)) - .hexarg2(guidHeader->GuidId, 4); + name = guidToUString(guid); + info = UString("GUID: ") + name + usprintf("\nFull size: %Xh (%u)\nHeader size %Xh (%u)\nBody size: %Xh (%u)\nType: %02Xh\nChecksum: %02Xh", + variableSize, variableSize, + header.size(), header.size(), + body.size(), body.size(), + guidHeader->Header.Type, + guidHeader->Header.Checksum) + + (guidHeader->Header.Checksum == calculated ? UString(", valid") : usprintf(", invalid, should be %02Xh", calculated)) + + usprintf("\nGuidId: %04Xh", guidHeader->GuidId); subtype = Subtypes::GuidEvsaEntry; guidMap.insert(std::pair(guidHeader->GuidId, guid)); } @@ -4664,19 +4639,17 @@ STATUS FfsParser::parseEvsaStoreBody(const QModelIndex & index) const EVSA_NAME_ENTRY* nameHeader = (const EVSA_NAME_ENTRY*)entryHeader; header = data.mid(offset, sizeof(EVSA_NAME_ENTRY)); body = data.mid(offset + sizeof(EVSA_NAME_ENTRY), nameHeader->Header.Size - sizeof(EVSA_NAME_ENTRY)); - name = QString::fromUtf16((const CHAR16*)body.constData()); - info = QObject::tr("Name: %1\nFull size: %2h (%3)\nHeader size %4h (%5)\nBody size: %6h (%7)\nType: %8h\nChecksum: %9\nVarId: %10h") - .arg(name) - .hexarg(variableSize).arg(variableSize) - .hexarg(header.size()).arg(header.size()) - .hexarg(body.size()).arg(body.size()) - .hexarg2(nameHeader->Header.Type, 2) - .arg(nameHeader->Header.Checksum == calculated ? - QObject::tr("%1h, valid").hexarg2(calculated, 2) : - QObject::tr("%1h, invalid, should be %2h").hexarg2(nameHeader->Header.Checksum, 2).hexarg2(calculated, 2)) - .hexarg2(nameHeader->VarId, 4); + name = UString::fromUtf16((const CHAR16*)body.constData()); + info = UString("GUID: ") + name + usprintf("\nFull size: %Xh (%u)\nHeader size %Xh (%u)\nBody size: %Xh (%u)\nType: %02Xh\nChecksum: %02Xh", + variableSize, variableSize, + header.size(), header.size(), + body.size(), body.size(), + nameHeader->Header.Type, + nameHeader->Header.Checksum) + + (nameHeader->Header.Checksum == calculated ? UString(", valid") : usprintf(", invalid, should be %02Xh", calculated)) + + usprintf("\nVarId: %04Xh", nameHeader->VarId); subtype = Subtypes::NameEvsaEntry; - nameMap.insert(std::pair(nameHeader->VarId, name)); + nameMap.insert(std::pair(nameHeader->VarId, name)); } // Data entry else if (entryHeader->Type == NVRAM_EVSA_ENTRY_TYPE_DATA1 || @@ -4695,32 +4668,32 @@ STATUS FfsParser::parseEvsaStoreBody(const QModelIndex & index) header = data.mid(offset, headerSize); body = data.mid(offset + headerSize, dataSize); - name = QObject::tr("Data"); - info = QObject::tr("Full size: %1h (%2)\nHeader size %3h (%4)\nBody size: %5h (%6)\nType: %7h\nChecksum: %8\nVarId: %9h\nGuidId: %10h\nAttributes: %11h (%12)") - .hexarg(variableSize).arg(variableSize) - .hexarg(headerSize).arg(headerSize) - .hexarg(dataSize).arg(dataSize) - .hexarg2(dataHeader->Header.Type, 2) - .arg(dataHeader->Header.Checksum == calculated ? - QObject::tr("%1h, valid").hexarg2(calculated, 2) : - QObject::tr("%1h, invalid, should be %2h").hexarg2(dataHeader->Header.Checksum, 2).hexarg2(calculated, 2)) - .hexarg2(dataHeader->VarId, 4) - .hexarg2(dataHeader->GuidId, 4) - .hexarg2(dataHeader->Attributes, 8).arg(evsaAttributesToQString(dataHeader->Attributes)); + name = UString("Data"); + info = usprintf("Full size: %Xh (%u)\nHeader size %Xh (%u)\nBody size: %Xh (%u)\nType: %02Xh\nChecksum: %02Xh", + variableSize, variableSize, + headerSize, headerSize, + dataSize, dataSize, + dataHeader->Header.Type, + dataHeader->Header.Checksum) + + (dataHeader->Header.Checksum == calculated ? UString("valid") : usprintf(", invalid, should be %02Xh", calculated)) + + usprintf("\nVarId: %04Xh\nGuidId: %04Xh\nAttributes: %08Xh (", + dataHeader->VarId, + dataHeader->GuidId, + dataHeader->Attributes) + + evsaAttributesToUString(dataHeader->Attributes).append(")"); subtype = Subtypes::DataEvsaEntry; } // Unknown entry or free space else { - QByteArray body = data.mid(offset); - QString info = QObject::tr("Full size: %1h (%2)") - .hexarg(body.size()).arg(body.size()); + UByteArray body = data.mid(offset); + UString info = usprintf("Full size: %Xh (%u)", body.size(), body.size()); // Check type - QString name = QObject::tr("Free space"); + UString name("Free space"); UINT8 type = Types::FreeSpace; UINT8 subtype = 0; if (getPaddingType(body) == Subtypes::DataPadding) { - name = QObject::tr("Padding"); + name = UString("Padding"); type = Types::Padding; subtype = Subtypes::DataPadding; } @@ -4729,11 +4702,11 @@ STATUS FfsParser::parseEvsaStoreBody(const QModelIndex & index) pdata.offset = parentOffset + offset; // Add free space tree item - QModelIndex itemIndex = model->addItem(type, subtype, name, QString(), info, QByteArray(), body, QByteArray(), false, parsingDataToQByteArray(pdata), index); + UModelIndex itemIndex = model->addItem(type, subtype, name, UString(), info, UByteArray(), body, UByteArray(), false, parsingDataToUByteArray(pdata), index); // Show message if (type == Types::Padding) - msg(QObject::tr("parseEvsaStoreBody: unknown variable of type %1h found at offset %2h, the rest of unparsed store added as padding").hexarg2(entryHeader->Type, 2).hexarg(offset), itemIndex); + msg(usprintf("parseEvsaStoreBody: unknown variable of type %02Xh found at offset %Xh, the rest of unparsed store added as padding",entryHeader->Type, offset), itemIndex); break; } @@ -4741,7 +4714,7 @@ STATUS FfsParser::parseEvsaStoreBody(const QModelIndex & index) pdata.offset = parentOffset + offset; // Add tree item - model->addItem(Types::EvsaEntry, subtype, name, QString(), info, header, body, QByteArray(), false, parsingDataToQByteArray(pdata), index); + model->addItem(Types::EvsaEntry, subtype, name, UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), index); // Move to next variable offset += variableSize; @@ -4750,61 +4723,61 @@ STATUS FfsParser::parseEvsaStoreBody(const QModelIndex & index) // Reparse all data variables to detect invalid ones and assign name and test to valid ones for (int i = 0; i < model->rowCount(index); i++) { - QModelIndex current = index.child(i, 0); + UModelIndex current = index.child(i, 0); if (model->subtype(current) == Subtypes::DataEvsaEntry) { - QByteArray header = model->header(current); + UByteArray header = model->header(current); const EVSA_DATA_ENTRY* dataHeader = (const EVSA_DATA_ENTRY*)header.constData(); - QString guid; + UString guid; if (guidMap.count(dataHeader->GuidId)) - guid = guidToQString(guidMap[dataHeader->GuidId]); - QString name; + guid = guidToUString(guidMap[dataHeader->GuidId]); + UString name; if (nameMap.count(dataHeader->VarId)) name = nameMap[dataHeader->VarId]; // Check for variable validity if (guid.isEmpty() && name.isEmpty()) { // Both name and guid aren't found model->setSubtype(current, Subtypes::InvalidEvsaEntry); - model->setName(current, QObject::tr("Invalid")); - msg(QObject::tr("parseEvsaStoreBody: data variable with invalid GuidId and invalid VarId"), current); + model->setName(current, UString("Invalid")); + msg(UString("parseEvsaStoreBody: data variable with invalid GuidId and invalid VarId"), current); } else if (guid.isEmpty()) { // Guid not found model->setSubtype(current, Subtypes::InvalidEvsaEntry); - model->setName(current, QObject::tr("Invalid")); - msg(QObject::tr("parseEvsaStoreBody: data variable with invalid GuidId"), current); + model->setName(current, UString("Invalid")); + msg(UString("parseEvsaStoreBody: data variable with invalid GuidId"), current); } else if (name.isEmpty()) { // Name not found model->setSubtype(current, Subtypes::InvalidEvsaEntry); - model->setName(current, QObject::tr("Invalid")); - msg(QObject::tr("parseEvsaStoreBody: data variable with invalid VarId"), current); + model->setName(current, UString("Invalid")); + msg(UString("parseEvsaStoreBody: data variable with invalid VarId"), current); } else { // Variable is OK, rename it if (dataHeader->Header.Type == NVRAM_EVSA_ENTRY_TYPE_DATA_INVALID) { model->setSubtype(current, Subtypes::InvalidEvsaEntry); - model->setName(current, QObject::tr("Invalid")); + model->setName(current, UString("Invalid")); } else { model->setName(current, guid); } model->setText(current, name); - model->addInfo(current, QObject::tr("GUID: %1\nName: %2\n").arg(guid).arg(name), false); + model->addInfo(current, guid.prepend("GUID: ") + name.prepend("\nName: "), false); } } } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsParser::parseFlashMapBody(const QModelIndex & index) +USTATUS FfsParser::parseFlashMapBody(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get parsing data for the current item - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); UINT32 parentOffset = pdata.offset + model->header(index).size(); - const QByteArray data = model->body(index); + const UByteArray data = model->body(index); const UINT32 dataSize = (UINT32)data.size(); @@ -4817,36 +4790,35 @@ STATUS FfsParser::parseFlashMapBody(const QModelIndex & index) // Check entry size if (unparsedSize < sizeof(PHOENIX_FLASH_MAP_ENTRY)) { // Last variable is bad, add the rest as padding and return - QByteArray body = data.mid(offset); - QString info = QObject::tr("Full size: %1h (%2)") - .hexarg(body.size()).arg(body.size()); + UByteArray body = data.mid(offset); + UString info = usprintf("Full size: %Xh (%u)", body.size(), body.size()); // Add correct offset to parsing data pdata.offset = parentOffset + offset; // Add free space tree item - model->addItem(Types::Padding, getPaddingType(body), QObject::tr("Padding"), QString(), info, QByteArray(), body, QByteArray(), false, parsingDataToQByteArray(pdata), index); + model->addItem(Types::Padding, getPaddingType(body), UString("Padding"), UString(), info, UByteArray(), body, UByteArray(), false, parsingDataToUByteArray(pdata), index); // Show message if (unparsedSize < entryHeader->Size) - msg(QObject::tr("parseFlashMapBody: next entry appears too big, added as padding"), index); + msg(UString("parseFlashMapBody: next entry appears too big, added as padding"), index); break; } - QString name = guidToQString(entryHeader->Guid); + UString name = guidToUString(entryHeader->Guid); // Construct header - QByteArray header = data.mid(offset, sizeof(PHOENIX_FLASH_MAP_ENTRY)); + UByteArray header = data.mid(offset, sizeof(PHOENIX_FLASH_MAP_ENTRY)); // Add info - QString info = QObject::tr("Entry GUID: %1\nFull size: 24h (36)\nHeader size: 24h (36)\nBody size: 0h (0)\nEntry type: %2h\nData type: %3h\nMemory address: %4h\nSize: %5h\nOffset: %6h") - .arg(name) - .hexarg2(entryHeader->EntryType, 4) - .hexarg2(entryHeader->DataType, 4) - .hexarg2(entryHeader->PhysicalAddress, 8) - .hexarg2(entryHeader->Size, 8) - .hexarg2(entryHeader->Offset, 8); + UString info = UString("Entry GUID: ") + name + usprintf("\nFull size: 24h (36)\nHeader size: 24h (36)\nBody size: 0h (0)\n" + "Entry type: %04Xh\nData type: %04Xh\nMemory address: %08Xh\nSize: %08Xh\nOffset: %08Xh", + entryHeader->EntryType, + entryHeader->DataType, + entryHeader->PhysicalAddress, + entryHeader->Size, + entryHeader->Offset); // Add correct offset to parsing data pdata.offset = parentOffset + offset; @@ -4863,12 +4835,12 @@ STATUS FfsParser::parseFlashMapBody(const QModelIndex & index) } // Add tree item - model->addItem(Types::FlashMapEntry, subtype, name, flashMapGuidToQString(entryHeader->Guid), info, header, QByteArray(), QByteArray(), true, parsingDataToQByteArray(pdata), index); + model->addItem(Types::FlashMapEntry, subtype, name, flashMapGuidToUString(entryHeader->Guid), info, header, UByteArray(), UByteArray(), true, parsingDataToUByteArray(pdata), index); // Move to next variable offset += sizeof(PHOENIX_FLASH_MAP_ENTRY); unparsedSize = dataSize - offset; } - return ERR_SUCCESS; + return U_SUCCESS; } diff --git a/common/ffsparser.h b/common/ffsparser.h index e160e82..5e4be11 100644 --- a/common/ffsparser.h +++ b/common/ffsparser.h @@ -15,10 +15,9 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include -#include -#include -#include - +#include "ustring.h" +#include "ubytearray.h" +#include "umodelindex.h" #include "basetypes.h" #include "treemodel.h" #include "utility.h" @@ -43,95 +42,95 @@ public: ~FfsParser() {} // Returns messages - std::vector > getMessages() const { return messagesVector; } + std::vector > getMessages() const { return messagesVector; } // Clears messages void clearMessages() { messagesVector.clear(); } // Firmware image parsing - STATUS parse(const QByteArray &buffer); + USTATUS parse(const UByteArray &buffer); // Retuns index of the last VTF after parsing is done - const QModelIndex getLastVtf() {return lastVtf;}; + const UModelIndex getLastVtf() {return lastVtf;}; private: TreeModel *model; - std::vector > messagesVector; - void msg(const QString & message, const QModelIndex &index = QModelIndex()) { - messagesVector.push_back(std::pair(message, index)); + std::vector > messagesVector; + void msg(const UString message, const UModelIndex index = UModelIndex()) { + messagesVector.push_back(std::pair(message, index)); }; - QModelIndex lastVtf; + UModelIndex lastVtf; UINT32 capsuleOffsetFixup; - STATUS parseRawArea(const QModelIndex & index); - STATUS parseVolumeHeader(const QByteArray & volume, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseVolumeBody(const QModelIndex & index); - STATUS parseFileHeader(const QByteArray & file, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseFileBody(const QModelIndex & index); - STATUS parseSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse = false); - STATUS parseSectionBody(const QModelIndex & index); + USTATUS parseRawArea(const UModelIndex & index); + USTATUS parseVolumeHeader(const UByteArray & volume, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseVolumeBody(const UModelIndex & index); + USTATUS parseFileHeader(const UByteArray & file, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseFileBody(const UModelIndex & index); + USTATUS parseSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse = false); + USTATUS parseSectionBody(const UModelIndex & index); - STATUS parseIntelImage(const QByteArray & intelImage, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & root); - STATUS parseGbeRegion(const QByteArray & gbe, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseMeRegion(const QByteArray & me, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseBiosRegion(const QByteArray & bios, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parsePdrRegion(const QByteArray & pdr, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseGeneralRegion(const UINT8 subtype, const QByteArray & region, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); + USTATUS parseIntelImage(const UByteArray & intelImage, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & root); + USTATUS parseGbeRegion(const UByteArray & gbe, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseMeRegion(const UByteArray & me, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseBiosRegion(const UByteArray & bios, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parsePdrRegion(const UByteArray & pdr, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseGeneralRegion(const UINT8 subtype, const UByteArray & region, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); - STATUS parsePadFileBody(const QModelIndex & index); - STATUS parseVolumeNonUefiData(const QByteArray & data, const UINT32 parentOffset, const QModelIndex & index); + USTATUS parsePadFileBody(const UModelIndex & index); + USTATUS parseVolumeNonUefiData(const UByteArray & data, const UINT32 parentOffset, const UModelIndex & index); - STATUS parseSections(const QByteArray & sections, const QModelIndex & index, const bool preparse = false); - STATUS parseCommonSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse); - STATUS parseCompressedSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse); - STATUS parseGuidedSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse); - STATUS parseFreeformGuidedSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse); - STATUS parseVersionSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse); - STATUS parsePostcodeSectionHeader(const QByteArray & section, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index, const bool preparse); + USTATUS parseSections(const UByteArray & sections, const UModelIndex & index, const bool preparse = false); + USTATUS parseCommonSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse); + USTATUS parseCompressedSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse); + USTATUS parseGuidedSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse); + USTATUS parseFreeformGuidedSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse); + USTATUS parseVersionSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse); + USTATUS parsePostcodeSectionHeader(const UByteArray & section, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index, const bool preparse); - STATUS parseCompressedSectionBody(const QModelIndex & index); - STATUS parseGuidedSectionBody(const QModelIndex & index); - STATUS parseVersionSectionBody(const QModelIndex & index); - STATUS parseDepexSectionBody(const QModelIndex & index); - STATUS parseUiSectionBody(const QModelIndex & index); - STATUS parseRawSectionBody(const QModelIndex & index); - STATUS parsePeImageSectionBody(const QModelIndex & index); - STATUS parseTeImageSectionBody(const QModelIndex & index); + USTATUS parseCompressedSectionBody(const UModelIndex & index); + USTATUS parseGuidedSectionBody(const UModelIndex & index); + USTATUS parseVersionSectionBody(const UModelIndex & index); + USTATUS parseDepexSectionBody(const UModelIndex & index); + USTATUS parseUiSectionBody(const UModelIndex & index); + USTATUS parseRawSectionBody(const UModelIndex & index); + USTATUS parsePeImageSectionBody(const UModelIndex & index); + USTATUS parseTeImageSectionBody(const UModelIndex & index); - UINT8 getPaddingType(const QByteArray & padding); - STATUS parseAprioriRawSection(const QByteArray & body, QString & parsed); - STATUS findNextVolume(const QModelIndex & index, const QByteArray & bios, const UINT32 parentOffset, const UINT32 volumeOffset, UINT32 & nextVolumeOffset); - STATUS getVolumeSize(const QByteArray & bios, const UINT32 volumeOffset, UINT32 & volumeSize, UINT32 & bmVolumeSize); - UINT32 getFileSize(const QByteArray & volume, const UINT32 fileOffset, const UINT8 ffsVersion); - UINT32 getSectionSize(const QByteArray & file, const UINT32 sectionOffset, const UINT8 ffsVersion); + UINT8 getPaddingType(const UByteArray & padding); + USTATUS parseAprioriRawSection(const UByteArray & body, UString & parsed); + USTATUS findNextVolume(const UModelIndex & index, const UByteArray & bios, const UINT32 parentOffset, const UINT32 volumeOffset, UINT32 & nextVolumeOffset); + USTATUS getVolumeSize(const UByteArray & bios, const UINT32 volumeOffset, UINT32 & volumeSize, UINT32 & bmVolumeSize); + UINT32 getFileSize(const UByteArray & volume, const UINT32 fileOffset, const UINT8 ffsVersion); + UINT32 getSectionSize(const UByteArray & file, const UINT32 sectionOffset, const UINT8 ffsVersion); - STATUS performFirstPass(const QByteArray & imageFile, QModelIndex & index); - STATUS performSecondPass(const QModelIndex & index); - STATUS addOffsetsRecursive(const QModelIndex & index); - STATUS addMemoryAddressesRecursive(const QModelIndex & index, const UINT32 diff); + USTATUS performFirstPass(const UByteArray & imageFile, UModelIndex & index); + USTATUS performSecondPass(const UModelIndex & index); + USTATUS addOffsetsRecursive(const UModelIndex & index); + USTATUS addMemoryAddressesRecursive(const UModelIndex & index, const UINT32 diff); // NVRAM parsing - STATUS parseNvramVolumeBody(const QModelIndex & index); - STATUS findNextStore(const QModelIndex & index, const QByteArray & volume, const UINT32 parentOffset, const UINT32 storeOffset, UINT32 & nextStoreOffset); - STATUS getStoreSize(const QByteArray & data, const UINT32 storeOffset, UINT32 & storeSize); - STATUS parseStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); + USTATUS parseNvramVolumeBody(const UModelIndex & index); + USTATUS findNextStore(const UModelIndex & index, const UByteArray & volume, const UINT32 parentOffset, const UINT32 storeOffset, UINT32 & nextStoreOffset); + USTATUS getStoreSize(const UByteArray & data, const UINT32 storeOffset, UINT32 & storeSize); + USTATUS parseStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); - STATUS parseNvarStore(const QModelIndex & index); - STATUS parseVssStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseFtwStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseFdcStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseFsysStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseEvsaStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseFlashMapStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseCmdbStoreHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseSlicPubkeyHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseSlicMarkerHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); - STATUS parseIntelMicrocodeHeader(const QByteArray & store, const UINT32 parentOffset, const QModelIndex & parent, QModelIndex & index); + USTATUS parseNvarStore(const UModelIndex & index); + USTATUS parseVssStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseFtwStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseFdcStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseFsysStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseEvsaStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseFlashMapStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseCmdbStoreHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseSlicPubkeyHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseSlicMarkerHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); + USTATUS parseIntelMicrocodeHeader(const UByteArray & store, const UINT32 parentOffset, const UModelIndex & parent, UModelIndex & index); - STATUS parseVssStoreBody(const QModelIndex & index); - STATUS parseFsysStoreBody(const QModelIndex & index); - STATUS parseEvsaStoreBody(const QModelIndex & index); - STATUS parseFlashMapBody(const QModelIndex & index); + USTATUS parseVssStoreBody(const UModelIndex & index); + USTATUS parseFsysStoreBody(const UModelIndex & index); + USTATUS parseEvsaStoreBody(const UModelIndex & index); + USTATUS parseFlashMapBody(const UModelIndex & index); }; #endif // FFSPARSER_H diff --git a/common/ffsreport.cpp b/common/ffsreport.cpp index b1ca61c..3f9638c 100644 --- a/common/ffsreport.cpp +++ b/common/ffsreport.cpp @@ -13,53 +13,49 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "ffsreport.h" -std::vector FfsReport::generate() +std::vector FfsReport::generate() { - std::vector report; + std::vector report; // Check model pointer if (!model) { - report.push_back(QObject::tr("ERROR: Invalid model pointer provided.")); + report.push_back(UString("ERROR: Invalid model pointer provided")); return report; } // Check root index to be valid - QModelIndex root = model->index(0,0); + UModelIndex root = model->index(0,0); if (!root.isValid()) { - report.push_back(QObject::tr("ERROR: Model root index is invalid.")); + report.push_back(UString("ERROR: Model root index is invalid")); return report; } // Generate report recursive - report.push_back(QObject::tr(" Type | Subtype | Size | CRC32 | Name ")); - STATUS result = generateRecursive(report, root); + report.push_back(UString(" Type | Subtype | Size | CRC32 | Name ")); + USTATUS result = generateRecursive(report, root); if (result) { - report.push_back(QObject::tr("ERROR: generateRecursive returned %1.") - .arg(errorCodeToQString(result))); + report.push_back(UString("ERROR: generateRecursive returned ") + errorCodeToUString(result)); } return report; } -STATUS FfsReport::generateRecursive(std::vector & report, QModelIndex index, UINT32 level) +USTATUS FfsReport::generateRecursive(std::vector & report, UModelIndex index, UINT32 level) { if (!index.isValid()) - return ERR_SUCCESS; //Nothing to report for invalid index + return U_SUCCESS; //Nothing to report for invalid index // Calculate item CRC32 - QByteArray data = model->header(index) + model->body(index) + model->tail(index); + UByteArray data = model->header(index) + model->body(index) + model->tail(index); UINT32 crc = crc32(0, (const UINT8*)data.constData(), data.size()); // Information on current item - QString text = model->text(index); - report.push_back(QObject::tr("%1 | %2 | %3 | %4 | %5 %6 %7") - .arg(itemTypeToQString(model->type(index)), 16, QChar(' ')) - .arg(itemSubtypeToQString(model->type(index), model->subtype(index)), 24, QChar(' ')) - .hexarg2(data.size(), 8) - .hexarg2(crc, 8) - .arg(' ', level, QChar('-')) - .arg(model->name(index)) - .arg(text.isEmpty() ? "" : text.prepend("| ")) + UString text = model->text(index); + report.push_back( + UString(" ") + itemTypeToUString(model->type(index)).leftJustified(16) + + UString("| ") + itemSubtypeToUString(model->type(index), model->subtype(index)).leftJustified(22) + + usprintf("| %08X | %08X | ", data.size(), crc) + + UString(level, '-') + UString(" ") + model->name(index) + (text.isEmpty() ? UString("") : UString(" | ") + text) ); // Information on child items @@ -67,5 +63,5 @@ STATUS FfsReport::generateRecursive(std::vector & report, QModelIndex i generateRecursive(report, index.child(i,0), level + 1); } - return ERR_SUCCESS; + return U_SUCCESS; } diff --git a/common/ffsreport.h b/common/ffsreport.h index 9432fda..5d671c6 100644 --- a/common/ffsreport.h +++ b/common/ffsreport.h @@ -16,11 +16,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include -#include -#include -#include -#include - +#include "../common/ubytearray.h" +#include "../common/ustring.h" +#include "../common/umodelindex.h" #include "basetypes.h" #include "treemodel.h" #include "ffs.h" @@ -33,12 +31,12 @@ public: FfsReport(TreeModel * treeModel) : model(treeModel) {} ~FfsReport() {}; - std::vector generate(); + std::vector generate(); private: TreeModel* model; - STATUS generateRecursive(std::vector & report, QModelIndex index, UINT32 level = 0); + USTATUS generateRecursive(std::vector & report, UModelIndex index, UINT32 level = 0); }; diff --git a/common/fitparser.cpp b/common/fitparser.cpp index 8ec00c5..a01c1dc 100644 --- a/common/fitparser.cpp +++ b/common/fitparser.cpp @@ -11,7 +11,7 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. */ #include "fitparser.h" -STATUS FitParser::parse(const QModelIndex & index, const QModelIndex & lastVtfIndex) +USTATUS FitParser::parse(const UModelIndex & index, const UModelIndex & lastVtfIndex) { // Check sanity if (!index.isValid() || !lastVtfIndex.isValid()) @@ -21,15 +21,15 @@ STATUS FitParser::parse(const QModelIndex & index, const QModelIndex & lastVtfIn lastVtf = lastVtfIndex; // Search for FIT - QModelIndex fitIndex; + UModelIndex fitIndex; UINT32 fitOffset; - STATUS result = findFitRecursive(index, fitIndex, fitOffset); + USTATUS result = findFitRecursive(index, fitIndex, fitOffset); if (result) return result; // FIT not found if (!fitIndex.isValid()) - return ERR_SUCCESS; + return U_SUCCESS; // Explicitly set the item as fixed model->setFixed(index, true); @@ -41,27 +41,27 @@ STATUS FitParser::parse(const QModelIndex & index, const QModelIndex & lastVtfIn UINT32 fitSize = (fitHeader->Size & 0xFFFFFF) << 4; if (fitHeader->Type & 0x80) { // Calculate FIT entry checksum - QByteArray tempFIT = model->body(fitIndex).mid(fitOffset, fitSize); + UByteArray tempFIT = model->body(fitIndex).mid(fitOffset, fitSize); FIT_ENTRY* tempFitHeader = (FIT_ENTRY*)tempFIT.data(); tempFitHeader->Checksum = 0; UINT8 calculated = calculateChecksum8((const UINT8*)tempFitHeader, fitSize); if (calculated != fitHeader->Checksum) { - msg(QObject::tr("Invalid FIT table checksum %1h, should be %2h").hexarg2(fitHeader->Checksum, 2).hexarg2(calculated, 2), fitIndex); + msg(usprintf("Invalid FIT table checksum %02Xh, should be %02Xh", fitHeader->Checksum, calculated), fitIndex); } } // Check fit header type if ((fitHeader->Type & 0x7F) != FIT_TYPE_HEADER) { - msg(QObject::tr("Invalid FIT header type"), fitIndex); + msg(("Invalid FIT header type"), fitIndex); } // Add FIT header to fitTable - std::vector currentStrings; - currentStrings.push_back(QObject::tr("_FIT_ ")); - currentStrings.push_back(QObject::tr("%1").hexarg2(fitSize, 8)); - currentStrings.push_back(QObject::tr("%1").hexarg2(fitHeader->Version, 4)); - currentStrings.push_back(fitEntryTypeToQString(fitHeader->Type)); - currentStrings.push_back(QObject::tr("%1").hexarg2(fitHeader->Checksum, 2)); + std::vector currentStrings; + currentStrings.push_back(UString("_FIT_ ")); + currentStrings.push_back(usprintf("%08X",fitSize)); + currentStrings.push_back(usprintf("%04X",fitHeader->Version)); + currentStrings.push_back(fitEntryTypeToUString(fitHeader->Type)); + currentStrings.push_back(usprintf("%02X",fitHeader->Checksum)); fitTable.push_back(currentStrings); // Process all other entries @@ -73,7 +73,7 @@ STATUS FitParser::parse(const QModelIndex & index, const QModelIndex & lastVtfIn // Check entry type switch (currentEntry->Type & 0x7F) { case FIT_TYPE_HEADER: - msg(QObject::tr("Second FIT header found, the table is damaged"), fitIndex); + msg(UString("Second FIT header found, the table is damaged"), fitIndex); break; case FIT_TYPE_EMPTY: @@ -93,52 +93,52 @@ STATUS FitParser::parse(const QModelIndex & index, const QModelIndex & lastVtfIn } // Add entry to fitTable - currentStrings.push_back(QObject::tr("%1").hexarg2(currentEntry->Address, 16)); - currentStrings.push_back(QObject::tr("%1").hexarg2(currentEntry->Size, 8)); - currentStrings.push_back(QObject::tr("%1").hexarg2(currentEntry->Version, 4)); - currentStrings.push_back(fitEntryTypeToQString(currentEntry->Type)); - currentStrings.push_back(QObject::tr("%1").hexarg2(currentEntry->Checksum, 2)); + currentStrings.push_back(usprintf("%016X",currentEntry->Address)); + currentStrings.push_back(usprintf("%08X", currentEntry->Size)); + currentStrings.push_back(usprintf("%04X", currentEntry->Version)); + currentStrings.push_back(fitEntryTypeToUString(currentEntry->Type)); + currentStrings.push_back(usprintf("%02X", currentEntry->Checksum)); fitTable.push_back(currentStrings); } if (msgModifiedImageMayNotWork) - msg(QObject::tr("Opened image may not work after any modification")); + msg(("Opened image may not work after any modification")); - return ERR_SUCCESS; + return U_SUCCESS; } -QString FitParser::fitEntryTypeToQString(UINT8 type) +UString FitParser::fitEntryTypeToUString(UINT8 type) { switch (type & 0x7F) { - case FIT_TYPE_HEADER: return QObject::tr("Header "); - case FIT_TYPE_MICROCODE: return QObject::tr("Microcode "); - case FIT_TYPE_BIOS_AC_MODULE: return QObject::tr("BIOS ACM "); - case FIT_TYPE_BIOS_INIT_MODULE: return QObject::tr("BIOS Init "); - case FIT_TYPE_TPM_POLICY: return QObject::tr("TPM Policy "); - case FIT_TYPE_BIOS_POLICY_DATA: return QObject::tr("BIOS Policy Data "); - case FIT_TYPE_TXT_CONF_POLICY: return QObject::tr("TXT Configuration Policy"); - case FIT_TYPE_AC_KEY_MANIFEST: return QObject::tr("BootGuard Key Manifest "); - case FIT_TYPE_AC_BOOT_POLICY: return QObject::tr("BootGuard Boot Policy "); - case FIT_TYPE_EMPTY: return QObject::tr("Empty "); - default: return QObject::tr("Unknown "); + case FIT_TYPE_HEADER: return ("Header "); + case FIT_TYPE_MICROCODE: return ("Microcode "); + case FIT_TYPE_BIOS_AC_MODULE: return ("BIOS ACM "); + case FIT_TYPE_BIOS_INIT_MODULE: return ("BIOS Init "); + case FIT_TYPE_TPM_POLICY: return ("TPM Policy "); + case FIT_TYPE_BIOS_POLICY_DATA: return ("BIOS Policy Data "); + case FIT_TYPE_TXT_CONF_POLICY: return ("TXT Configuration Policy"); + case FIT_TYPE_AC_KEY_MANIFEST: return ("BootGuard Key Manifest "); + case FIT_TYPE_AC_BOOT_POLICY: return ("BootGuard Boot Policy "); + case FIT_TYPE_EMPTY: return ("Empty "); + default: return ("Unknown "); } } -STATUS FitParser::findFitRecursive(const QModelIndex & index, QModelIndex & found, UINT32 & fitOffset) +USTATUS FitParser::findFitRecursive(const UModelIndex & index, UModelIndex & found, UINT32 & fitOffset) { // Sanity check if (!index.isValid()) - return ERR_SUCCESS; + return U_SUCCESS; // Process child items for (int i = 0; i < model->rowCount(index); i++) { findFitRecursive(index.child(i, 0), found, fitOffset); if (found.isValid()) - return ERR_SUCCESS; + return U_SUCCESS; } // Get parsing data for the current item - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); // Check for all FIT signatures in item's body for (INT32 offset = model->body(index).indexOf(FIT_SIGNATURE); @@ -148,16 +148,16 @@ STATUS FitParser::findFitRecursive(const QModelIndex & index, QModelIndex & foun UINT32 fitAddress = pdata.address + model->header(index).size() + (UINT32)offset; // Check FIT address to be in the last VTF - QByteArray lastVtfBody = model->body(lastVtf); + UByteArray lastVtfBody = model->body(lastVtf); if (*(const UINT32*)(lastVtfBody.constData() + lastVtfBody.size() - FIT_POINTER_OFFSET) == fitAddress) { found = index; fitOffset = offset; - msg(QObject::tr("Real FIT table found at physical address %1h").hexarg(fitAddress), found); - return ERR_SUCCESS; + msg(usprintf("Real FIT table found at physical address %Xh", fitAddress), found); + return U_SUCCESS; } else if (model->rowCount(index) == 0) // Show messages only to leaf items - msg(QObject::tr("FIT table candidate found, but not referenced from the last VTF"), index); + msg(("FIT table candidate found, but not referenced from the last VTF"), index); } - return ERR_SUCCESS; + return U_SUCCESS; } \ No newline at end of file diff --git a/common/fitparser.h b/common/fitparser.h index f9cbad7..4ebf663 100644 --- a/common/fitparser.h +++ b/common/fitparser.h @@ -15,10 +15,9 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include -#include -#include -#include - +#include "ustring.h" +#include "ubytearray.h" +#include "umodelindex.h" #include "treemodel.h" #include "utility.h" #include "parsingdata.h" @@ -36,25 +35,25 @@ public: ~FitParser() {} // Returns messages - std::vector > getMessages() const { return messagesVector; }; + std::vector > getMessages() const { return messagesVector; }; // Clears messages void clearMessages() { messagesVector.clear(); } - STATUS parse(const QModelIndex & index, const QModelIndex & lastVtf); - std::vector > getFitTable() const { return fitTable; } + USTATUS parse(const UModelIndex & index, const UModelIndex & lastVtf); + std::vector > getFitTable() const { return fitTable; } private: TreeModel *model; - std::vector > messagesVector; - QModelIndex lastVtf; - std::vector > fitTable; + std::vector > messagesVector; + UModelIndex lastVtf; + std::vector > fitTable; - STATUS findFitRecursive(const QModelIndex & index, QModelIndex & found, UINT32 & fitOffset); - QString fitEntryTypeToQString(UINT8 type); + USTATUS findFitRecursive(const UModelIndex & index, UModelIndex & found, UINT32 & fitOffset); + UString fitEntryTypeToUString(UINT8 type); // Message helper - void msg(const QString & message, const QModelIndex &index = QModelIndex()) { - messagesVector.push_back(std::pair(message, index)); + void msg(const UString & message, const UModelIndex &index = UModelIndex()) { + messagesVector.push_back(std::pair(message, index)); } }; diff --git a/common/me.h b/common/me.h index 7197065..60b8245 100644 --- a/common/me.h +++ b/common/me.h @@ -18,8 +18,8 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. // Make sure we use right packing rules #pragma pack(push, 1) -const QByteArray ME_VERSION_SIGNATURE("\x24\x4D\x41\x4E", 4); //$MAN -const QByteArray ME_VERSION_SIGNATURE2("\x24\x4D\x4E\x32", 4); //$MN2 +const UByteArray ME_VERSION_SIGNATURE("\x24\x4D\x41\x4E", 4); //$MAN +const UByteArray ME_VERSION_SIGNATURE2("\x24\x4D\x4E\x32", 4); //$MN2 typedef struct ME_VERSION_ { UINT32 signature; diff --git a/common/nvram.cpp b/common/nvram.cpp index cb3540d..c279d56 100644 --- a/common/nvram.cpp +++ b/common/nvram.cpp @@ -11,100 +11,99 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. */ -#include #include "nvram.h" -QString nvarAttributesToQString(const UINT8 attributes) +UString nvarAttributesToUString(const UINT8 attributes) { if (attributes == 0x00 || attributes == 0xFF) - return QString(); + return UString(); - QString str; - if (attributes & NVRAM_NVAR_ENTRY_RUNTIME) str += QObject::tr(", Runtime"); - if (attributes & NVRAM_NVAR_ENTRY_ASCII_NAME) str += QObject::tr(", AsciiName"); - if (attributes & NVRAM_NVAR_ENTRY_GUID) str += QObject::tr(", Guid"); - if (attributes & NVRAM_NVAR_ENTRY_DATA_ONLY) str += QObject::tr(", DataOnly"); - if (attributes & NVRAM_NVAR_ENTRY_EXT_HEADER) str += QObject::tr(", ExtHeader"); - if (attributes & NVRAM_NVAR_ENTRY_HW_ERROR_RECORD) str += QObject::tr(", HwErrorRecord"); - if (attributes & NVRAM_NVAR_ENTRY_AUTH_WRITE) str += QObject::tr(", AuthWrite"); - if (attributes & NVRAM_NVAR_ENTRY_VALID) str += QObject::tr(", Valid"); + UString str; + if (attributes & NVRAM_NVAR_ENTRY_RUNTIME) str += UString(", Runtime"); + if (attributes & NVRAM_NVAR_ENTRY_ASCII_NAME) str += UString(", AsciiName"); + if (attributes & NVRAM_NVAR_ENTRY_GUID) str += UString(", Guid"); + if (attributes & NVRAM_NVAR_ENTRY_DATA_ONLY) str += UString(", DataOnly"); + if (attributes & NVRAM_NVAR_ENTRY_EXT_HEADER) str += UString(", ExtHeader"); + if (attributes & NVRAM_NVAR_ENTRY_HW_ERROR_RECORD) str += UString(", HwErrorRecord"); + if (attributes & NVRAM_NVAR_ENTRY_AUTH_WRITE) str += UString(", AuthWrite"); + if (attributes & NVRAM_NVAR_ENTRY_VALID) str += UString(", Valid"); return str.mid(2); // Remove first comma and space } -QString nvarExtendedAttributesToQString(const UINT8 attributes) +UString nvarExtendedAttributesToUString(const UINT8 attributes) { - QString str; - if (attributes & NVRAM_NVAR_ENTRY_EXT_CHECKSUM) str += QObject::tr(", Checksum"); - if (attributes & NVRAM_NVAR_ENTRY_EXT_AUTH_WRITE) str += QObject::tr(", AuthWrite"); - if (attributes & NVRAM_NVAR_ENTRY_EXT_TIME_BASED) str += QObject::tr(", TimeBasedAuthWrite"); - if (attributes & NVRAM_NVAR_ENTRY_EXT_UNKNOWN_MASK) str += QObject::tr(", Unknown"); + UString str; + if (attributes & NVRAM_NVAR_ENTRY_EXT_CHECKSUM) str += UString(", Checksum"); + if (attributes & NVRAM_NVAR_ENTRY_EXT_AUTH_WRITE) str += UString(", AuthWrite"); + if (attributes & NVRAM_NVAR_ENTRY_EXT_TIME_BASED) str += UString(", TimeBasedAuthWrite"); + if (attributes & NVRAM_NVAR_ENTRY_EXT_UNKNOWN_MASK) str += UString(", Unknown"); return str.mid(2); // Remove first comma and space } -extern QString vssAttributesToQString(const UINT32 attributes) +extern UString vssAttributesToUString(const UINT32 attributes) { - QString str; - if (attributes & NVRAM_VSS_VARIABLE_NON_VOLATILE) str += QObject::tr(", NonVolatile"); - if (attributes & NVRAM_VSS_VARIABLE_BOOTSERVICE_ACCESS) str += QObject::tr(", BootService"); - if (attributes & NVRAM_VSS_VARIABLE_RUNTIME_ACCESS) str += QObject::tr(", Runtime"); - if (attributes & NVRAM_VSS_VARIABLE_HARDWARE_ERROR_RECORD) str += QObject::tr(", HwErrorRecord"); - if (attributes & NVRAM_VSS_VARIABLE_AUTHENTICATED_WRITE_ACCESS) str += QObject::tr(", AuthWrite"); - if (attributes & NVRAM_VSS_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) str += QObject::tr(", TimeBasedAuthWrite"); - if (attributes & NVRAM_VSS_VARIABLE_APPEND_WRITE) str += QObject::tr(", AppendWrite"); - if (attributes & NVRAM_VSS_VARIABLE_APPLE_DATA_CHECKSUM) str += QObject::tr(", AppleChecksum"); - if (attributes & NVRAM_VSS_VARIABLE_UNKNOWN_MASK) str += QObject::tr(", Unknown"); + UString str; + if (attributes & NVRAM_VSS_VARIABLE_NON_VOLATILE) str += UString(", NonVolatile"); + if (attributes & NVRAM_VSS_VARIABLE_BOOTSERVICE_ACCESS) str += UString(", BootService"); + if (attributes & NVRAM_VSS_VARIABLE_RUNTIME_ACCESS) str += UString(", Runtime"); + if (attributes & NVRAM_VSS_VARIABLE_HARDWARE_ERROR_RECORD) str += UString(", HwErrorRecord"); + if (attributes & NVRAM_VSS_VARIABLE_AUTHENTICATED_WRITE_ACCESS) str += UString(", AuthWrite"); + if (attributes & NVRAM_VSS_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) str += UString(", TimeBasedAuthWrite"); + if (attributes & NVRAM_VSS_VARIABLE_APPEND_WRITE) str += UString(", AppendWrite"); + if (attributes & NVRAM_VSS_VARIABLE_APPLE_DATA_CHECKSUM) str += UString(", AppleChecksum"); + if (attributes & NVRAM_VSS_VARIABLE_UNKNOWN_MASK) str += UString(", Unknown"); return str.mid(2); // Remove first comma and space } -QString evsaAttributesToQString(const UINT32 attributes) +UString evsaAttributesToUString(const UINT32 attributes) { - QString str; - if (attributes & NVRAM_EVSA_DATA_NON_VOLATILE) str += QObject::tr(", NonVolatile"); - if (attributes & NVRAM_EVSA_DATA_BOOTSERVICE_ACCESS) str += QObject::tr(", BootService"); - if (attributes & NVRAM_EVSA_DATA_RUNTIME_ACCESS) str += QObject::tr(", Runtime"); - if (attributes & NVRAM_EVSA_DATA_HARDWARE_ERROR_RECORD) str += QObject::tr(", HwErrorRecord"); - if (attributes & NVRAM_EVSA_DATA_AUTHENTICATED_WRITE_ACCESS) str += QObject::tr(", AuthWrite"); - if (attributes & NVRAM_EVSA_DATA_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) str += QObject::tr(", TimeBasedAuthWrite"); - if (attributes & NVRAM_EVSA_DATA_APPEND_WRITE) str += QObject::tr(", AppendWrite"); - if (attributes & NVRAM_EVSA_DATA_EXTENDED_HEADER) str += QObject::tr(", ExtendedHeader"); - if (attributes & NVRAM_EVSA_DATA_UNKNOWN_MASK) str += QObject::tr(", Unknown"); + UString str; + if (attributes & NVRAM_EVSA_DATA_NON_VOLATILE) str += UString(", NonVolatile"); + if (attributes & NVRAM_EVSA_DATA_BOOTSERVICE_ACCESS) str += UString(", BootService"); + if (attributes & NVRAM_EVSA_DATA_RUNTIME_ACCESS) str += UString(", Runtime"); + if (attributes & NVRAM_EVSA_DATA_HARDWARE_ERROR_RECORD) str += UString(", HwErrorRecord"); + if (attributes & NVRAM_EVSA_DATA_AUTHENTICATED_WRITE_ACCESS) str += UString(", AuthWrite"); + if (attributes & NVRAM_EVSA_DATA_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) str += UString(", TimeBasedAuthWrite"); + if (attributes & NVRAM_EVSA_DATA_APPEND_WRITE) str += UString(", AppendWrite"); + if (attributes & NVRAM_EVSA_DATA_EXTENDED_HEADER) str += UString(", ExtendedHeader"); + if (attributes & NVRAM_EVSA_DATA_UNKNOWN_MASK) str += UString(", Unknown"); return str.mid(2); // Remove first comma and space } -QString efiTimeToQString(const EFI_TIME & time) +UString efiTimeToUString(const EFI_TIME & time) { - return QObject::tr("%1-%2-%3T%4:%5:%6.%7") - .arg(time.Year, 4, 10, QLatin1Char('0')) - .arg(time.Month, 2, 10, QLatin1Char('0')) - .arg(time.Day, 2, 10, QLatin1Char('0')) - .arg(time.Hour, 2, 10, QLatin1Char('0')) - .arg(time.Minute, 2, 10, QLatin1Char('0')) - .arg(time.Second, 2, 10, QLatin1Char('0')) - .arg(time.Nanosecond); + return usprintf("%04u-%02u-%02uT%02u:%02u:%02u.%u", + time.Year, + time.Month, + time.Day, + time.Hour, + time.Minute, + time.Second, + time.Nanosecond); } -QString flashMapGuidToQString(const EFI_GUID & guid) +UString flashMapGuidToUString(const EFI_GUID & guid) { - const QByteArray baGuid((const char*)&guid, sizeof(EFI_GUID)); - if (baGuid == NVRAM_PHOENIX_FLASH_MAP_VOLUME_HEADER) return QObject::tr("Volume header"); - if (baGuid == NVRAM_PHOENIX_FLASH_MAP_MICROCODES_GUID) return QObject::tr("Microcodes"); - if (baGuid == NVRAM_PHOENIX_FLASH_MAP_CMDB_GUID) return QObject::tr("CMDB"); + const UByteArray baGuid((const char*)&guid, sizeof(EFI_GUID)); + if (baGuid == NVRAM_PHOENIX_FLASH_MAP_VOLUME_HEADER) return UString("Volume header"); + if (baGuid == NVRAM_PHOENIX_FLASH_MAP_MICROCODES_GUID) return UString("Microcodes"); + if (baGuid == NVRAM_PHOENIX_FLASH_MAP_CMDB_GUID) return UString("CMDB"); if (baGuid == NVRAM_PHOENIX_FLASH_MAP_PUBKEY1_GUID - || baGuid == NVRAM_PHOENIX_FLASH_MAP_PUBKEY2_GUID) return QObject::tr("SLIC pubkey"); + || baGuid == NVRAM_PHOENIX_FLASH_MAP_PUBKEY2_GUID) return UString("SLIC pubkey"); if (baGuid == NVRAM_PHOENIX_FLASH_MAP_MARKER1_GUID - || baGuid == NVRAM_PHOENIX_FLASH_MAP_MARKER2_GUID) return QObject::tr("SLIC marker"); + || baGuid == NVRAM_PHOENIX_FLASH_MAP_MARKER2_GUID) return UString("SLIC marker"); if (baGuid == NVRAM_PHOENIX_FLASH_MAP_EVSA1_GUID || baGuid == NVRAM_PHOENIX_FLASH_MAP_EVSA2_GUID || baGuid == NVRAM_PHOENIX_FLASH_MAP_EVSA3_GUID || baGuid == NVRAM_PHOENIX_FLASH_MAP_EVSA4_GUID || baGuid == NVRAM_PHOENIX_FLASH_MAP_EVSA5_GUID || baGuid == NVRAM_PHOENIX_FLASH_MAP_EVSA6_GUID - || baGuid == NVRAM_PHOENIX_FLASH_MAP_EVSA7_GUID) return QObject::tr("EVSA store"); - if (baGuid == NVRAM_PHOENIX_FLASH_MAP_SELF_GUID) return QObject::tr("Flash map"); - return QString(); + || baGuid == NVRAM_PHOENIX_FLASH_MAP_EVSA7_GUID) return UString("EVSA store"); + if (baGuid == NVRAM_PHOENIX_FLASH_MAP_SELF_GUID) return UString("Flash map"); + return UString(); } diff --git a/common/nvram.h b/common/nvram.h index 322e73c..7a26f74 100644 --- a/common/nvram.h +++ b/common/nvram.h @@ -15,8 +15,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef NVRAM_H #define NVRAM_H -#include -#include +#include "ubytearray.h" +#include "ustring.h" #include "basetypes.h" // Make sure we use right packing rules @@ -27,16 +27,16 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. // // CEF5B9A3-476D-497F-9FDC-E98143E0422C -const QByteArray NVRAM_NVAR_STORE_FILE_GUID +const UByteArray NVRAM_NVAR_STORE_FILE_GUID ("\xA3\xB9\xF5\xCE\x6D\x47\x7F\x49\x9F\xDC\xE9\x81\x43\xE0\x42\x2C", 16); // 9221315B-30BB-46B5-813E-1B1BF4712BD3 -const QByteArray NVRAM_NVAR_EXTERNAL_DEFAULTS_FILE_GUID +const UByteArray NVRAM_NVAR_EXTERNAL_DEFAULTS_FILE_GUID ("\x5B\x31\x21\x92\xBB\x30\xB5\x46\x81\x3E\x1B\x1B\xF4\x71\x2B\xD3", 16); -extern QString nvarAttributesToQString(const UINT8 attributes); -extern QString nvarExtendedAttributesToQString(const UINT8 attributes); -extern QString efiTimeToQString(const EFI_TIME & time); +extern UString nvarAttributesToUString(const UINT8 attributes); +extern UString nvarExtendedAttributesToUString(const UINT8 attributes); +extern UString efiTimeToUString(const EFI_TIME & time); typedef struct NVAR_ENTRY_HEADER_ { UINT32 Signature; // NVAR @@ -68,11 +68,11 @@ typedef struct NVAR_ENTRY_HEADER_ { // // FFF12B8D-7696-4C8B-A985-2747075B4F50 -const QByteArray NVRAM_MAIN_STORE_VOLUME_GUID +const UByteArray NVRAM_MAIN_STORE_VOLUME_GUID ("\x8D\x2B\xF1\xFF\x96\x76\x8B\x4C\xA9\x85\x27\x47\x07\x5B\x4F\x50", 16); // 00504624-8A59-4EEB-BD0F-6B36E96128E0 -const QByteArray NVRAM_ADDITIONAL_STORE_VOLUME_GUID +const UByteArray NVRAM_ADDITIONAL_STORE_VOLUME_GUID ("\x24\x46\x50\x00\x59\x8A\xEB\x4E\xBD\x0F\x6B\x36\xE9\x61\x28\xE0", 16); #define NVRAM_VSS_STORE_SIGNATURE 0x53535624 // $VSS @@ -156,7 +156,7 @@ typedef struct VSS_AUTH_VARIABLE_HEADER_ { #define NVRAM_VSS_VARIABLE_APPLE_DATA_CHECKSUM 0x80000000 #define NVRAM_VSS_VARIABLE_UNKNOWN_MASK 0x7FFFFF80 -extern QString vssAttributesToQString(const UINT32 attributes); +extern UString vssAttributesToUString(const UINT32 attributes); // // _FDC region @@ -179,7 +179,7 @@ typedef struct FDC_VOLUME_HEADER_ { #define EFI_FAULT_TOLERANT_WORKING_BLOCK_INVALID 0x2 // 9E58292B-7C68-497D-0ACE6500FD9F1B95 -const QByteArray EDKII_WORKING_BLOCK_SIGNATURE_GUID +const UByteArray EDKII_WORKING_BLOCK_SIGNATURE_GUID ("\x2B\x29\x58\x9E\x68\x7C\x7D\x49\x0A\xCE\x65\x00\xFD\x9F\x1B\x95", 16); #define NVRAM_MAIN_STORE_VOLUME_GUID_DATA1 0xFFF12B8D @@ -292,7 +292,7 @@ typedef struct EVSA_DATA_ENTRY_EXTENDED_ { //UINT8 Data[]; } EVSA_DATA_ENTRY_EXTENDED; -extern QString evsaAttributesToQString(const UINT32 attributes); +extern UString evsaAttributesToUString(const UINT32 attributes); // // Phoenix SCT Flash Map @@ -302,7 +302,7 @@ extern QString evsaAttributesToQString(const UINT32 attributes); #define NVRAM_PHOENIX_FLASH_MAP_SIGNATURE_LENGTH 10 // _FLASH_MAP -const QByteArray NVRAM_PHOENIX_FLASH_MAP_SIGNATURE +const UByteArray NVRAM_PHOENIX_FLASH_MAP_SIGNATURE ("\x5F\x46\x4C\x41\x53\x48\x5F\x4D\x41\x50", 10); typedef struct PHOENIX_FLASH_MAP_HEADER_ { @@ -323,66 +323,66 @@ typedef struct PHOENIX_FLASH_MAP_ENTRY_ { #define NVRAM_PHOENIX_FLASH_MAP_ENTRY_TYPE_VOLUME 0x0000 #define NVRAM_PHOENIX_FLASH_MAP_ENTRY_TYPE_DATA_BLOCK 0x0001 -extern QString flashMapGuidToQString(const EFI_GUID & guid); +extern UString flashMapGuidToUString(const EFI_GUID & guid); // B091E7D2-05A0-4198-94F0-74B7B8C55459 -const QByteArray NVRAM_PHOENIX_FLASH_MAP_VOLUME_HEADER +const UByteArray NVRAM_PHOENIX_FLASH_MAP_VOLUME_HEADER ("\xD2\xE7\x91\xB0\xA0\x05\x98\x41\x94\xF0\x74\xB7\xB8\xC5\x54\x59", 16); // FD3F690E-B4B0-4D68-89DB-19A1A3318F90 -const QByteArray NVRAM_PHOENIX_FLASH_MAP_MICROCODES_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_MICROCODES_GUID ("\x0E\x69\x3F\xFD\xB0\xB4\x68\x4D\x89\xDB\x19\xA1\xA3\x31\x8F\x90", 16); // 46310243-7B03-4132-BE44-2243FACA7CDD -const QByteArray NVRAM_PHOENIX_FLASH_MAP_CMDB_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_CMDB_GUID ("\x43\x02\x31\x46\x03\x7B\x32\x41\xBE\x44\x22\x43\xFA\xCA\x7C\xDD", 16); // 1B2C4952-D778-4B64-BDA1-15A36F5FA545 -const QByteArray NVRAM_PHOENIX_FLASH_MAP_PUBKEY1_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_PUBKEY1_GUID ("\x52\x49\x2C\x1B\x78\xD7\x64\x4B\xBD\xA1\x15\xA3\x6F\x5F\xA5\x45", 16); // 127C1C4E-9135-46E3-B006-F9808B0559A5 -const QByteArray NVRAM_PHOENIX_FLASH_MAP_MARKER1_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_MARKER1_GUID ("\x4E\x1C\x7C\x12\x35\x91\xE3\x46\xB0\x06\xF9\x80\x8B\x05\x59\xA5", 16); // 7CE75114-8272-45AF-B536-761BD38852CE -const QByteArray NVRAM_PHOENIX_FLASH_MAP_PUBKEY2_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_PUBKEY2_GUID ("\x14\x51\xE7\x7C\x72\x82\xAF\x45\xB5\x36\x76\x1B\xD3\x88\x52\xCE", 16); // 071A3DBE-CFF4-4B73-83F0-598C13DCFDD5 -const QByteArray NVRAM_PHOENIX_FLASH_MAP_MARKER2_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_MARKER2_GUID ("\xBE\x3D\x1A\x07\xF4\xCF\x73\x4B\x83\xF0\x59\x8C\x13\xDC\xFD\xD5", 16); // FACFB110-7BFD-4EFB-873E-88B6B23B97EA -const QByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA1_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA1_GUID ("\x10\xB1\xCF\xFA\xFD\x7B\xFB\x4E\x87\x3E\x88\xB6\xB2\x3B\x97\xEA", 16); // E68DC11A-A5F4-4AC3-AA2E-29E298BFF645 -const QByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA2_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA2_GUID ("\x1A\xC1\x8D\xE6\xF4\xA5\xC3\x4A\xAA\x2E\x29\xE2\x98\xBF\xF6\x45", 16); // 4B3828AE-0ACE-45B6-8CDB-DAFC28BBF8C5 -const QByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA3_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA3_GUID ("\xAE\x28\x38\x4B\xCE\x0A\xB6\x45\x8C\xDB\xDA\xFC\x28\xBB\xF8\xC5", 16); // C22E6B8A-8159-49A3-B353-E84B79DF19C0 -const QByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA4_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA4_GUID ("\x8A\x6B\x2E\xC2\x59\x81\xA3\x49\xB3\x53\xE8\x4B\x79\xDF\x19\xC0", 16); // B6B5FAB9-75C4-4AAE-8314-7FFFA7156EAA -const QByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA5_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA5_GUID ("\xB9\xFA\xB5\xB6\xC4\x75\xAE\x4A\x83\x14\x7F\xFF\xA7\x15\x6E\xAA", 16); // 919B9699-8DD0-4376-AA0B-0E54CCA47D8F -const QByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA6_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA6_GUID ("\x99\x96\x9B\x91\xD0\x8D\x76\x43\xAA\x0B\x0E\x54\xCC\xA4\x7D\x8F", 16); // 58A90A52-929F-44F8-AC35-A7E1AB18AC91 -const QByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA7_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_EVSA7_GUID ("\x52\x0A\xA9\x58\x9F\x92\xF8\x44\xAC\x35\xA7\xE1\xAB\x18\xAC\x91", 16); // 8CB71915-531F-4AF5-82BF-A09140817BAA -const QByteArray NVRAM_PHOENIX_FLASH_MAP_SELF_GUID +const UByteArray NVRAM_PHOENIX_FLASH_MAP_SELF_GUID ("\x15\x19\xB7\x8C\x1F\x53\xF5\x4A\x82\xBF\xA0\x91\x40\x81\x7B\xAA", 16); // diff --git a/common/peimage.cpp b/common/peimage.cpp index 47f3d70..534d0c8 100644 --- a/common/peimage.cpp +++ b/common/peimage.cpp @@ -11,27 +11,25 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. */ -#include - #include "peimage.h" -QString machineTypeToQString(UINT16 machineType) +UString machineTypeToUString(UINT16 machineType) { switch (machineType) { - case EFI_IMAGE_FILE_MACHINE_AMD64: return QObject::tr("x86-64"); - case EFI_IMAGE_FILE_MACHINE_ARM: return QObject::tr("ARM"); - case EFI_IMAGE_FILE_MACHINE_ARMNT: return QObject::tr("ARMv7"); - case EFI_IMAGE_FILE_MACHINE_APPLE_ARM: return QObject::tr("Apple ARM"); - case EFI_IMAGE_FILE_MACHINE_AARCH64: return QObject::tr("AARCH64"); - case EFI_IMAGE_FILE_MACHINE_EBC: return QObject::tr("EBC"); - case EFI_IMAGE_FILE_MACHINE_I386: return QObject::tr("x86"); - case EFI_IMAGE_FILE_MACHINE_IA64: return QObject::tr("IA64"); - case EFI_IMAGE_FILE_MACHINE_POWERPC: return QObject::tr("PowerPC"); - case EFI_IMAGE_FILE_MACHINE_POWERPCFP: return QObject::tr("PowerPC FP"); - case EFI_IMAGE_FILE_MACHINE_THUMB: return QObject::tr("ARM Thumb"); - case EFI_IMAGE_FILE_MACHINE_RISCV32: return QObject::tr("RISC-V 32-bit"); - case EFI_IMAGE_FILE_MACHINE_RISCV64: return QObject::tr("RISC-V 64-bit"); - case EFI_IMAGE_FILE_MACHINE_RISCV128: return QObject::tr("RISC-V 128-bit"); - default: return QObject::tr("Unknown %1h").hexarg2(machineType, 4); + case EFI_IMAGE_FILE_MACHINE_AMD64: return UString("x86-64"); + case EFI_IMAGE_FILE_MACHINE_ARM: return UString("ARM"); + case EFI_IMAGE_FILE_MACHINE_ARMNT: return UString("ARMv7"); + case EFI_IMAGE_FILE_MACHINE_APPLE_ARM: return UString("Apple ARM"); + case EFI_IMAGE_FILE_MACHINE_AARCH64: return UString("AARCH64"); + case EFI_IMAGE_FILE_MACHINE_EBC: return UString("EBC"); + case EFI_IMAGE_FILE_MACHINE_I386: return UString("x86"); + case EFI_IMAGE_FILE_MACHINE_IA64: return UString("IA64"); + case EFI_IMAGE_FILE_MACHINE_POWERPC: return UString("PowerPC"); + case EFI_IMAGE_FILE_MACHINE_POWERPCFP: return UString("PowerPC FP"); + case EFI_IMAGE_FILE_MACHINE_THUMB: return UString("ARM Thumb"); + case EFI_IMAGE_FILE_MACHINE_RISCV32: return UString("RISC-V 32-bit"); + case EFI_IMAGE_FILE_MACHINE_RISCV64: return UString("RISC-V 64-bit"); + case EFI_IMAGE_FILE_MACHINE_RISCV128: return UString("RISC-V 128-bit"); + default: return usprintf("Unknown (%04Xh)", machineType); } } \ No newline at end of file diff --git a/common/peimage.h b/common/peimage.h index 5eaa44a..0d5b1e7 100644 --- a/common/peimage.h +++ b/common/peimage.h @@ -1,737 +1,739 @@ -/* peimage.h - -Copyright (c) 2015, Nikolaj Schlej. All rights reserved. -Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved. -Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved. -This program and the accompanying materials -are licensed and made available under the terms and conditions of the BSD License -which accompanies this distribution. The full text of the license may be found at -http://opensource.org/licenses/bsd-license.php. - -THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - -*/ - -#ifndef PEIMAGE_H -#define PEIMAGE_H - -#include -#include "basetypes.h" - -extern QString machineTypeToQString(UINT16 machineType); - -// Make sure we use right packing rules -#pragma pack(push, 1) - -// -// PE32+ Subsystem type for EFI images -// -#define EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION 10 -#define EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER 11 -#define EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER 12 -#define EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER 13 - -// -// PE32+ Machine type for EFI images -// -#define EFI_IMAGE_FILE_MACHINE_I386 0x014c // x86 -#define EFI_IMAGE_FILE_MACHINE_ARM 0x01c0 // ARM little endian -#define EFI_IMAGE_FILE_MACHINE_THUMB 0x01c2 // ARM or Thumb (interworking) -#define EFI_IMAGE_FILE_MACHINE_ARMNT 0x01c4 // ARMv7 (or higher) Thumb mode only -#define EFI_IMAGE_FILE_MACHINE_APPLE_ARM 0x01c6 // Apple ARM -#define EFI_IMAGE_FILE_MACHINE_POWERPC 0x01f0 // Power PC little endian -#define EFI_IMAGE_FILE_MACHINE_POWERPCFP 0x01f1 // Power PC with floating point support -#define EFI_IMAGE_FILE_MACHINE_IA64 0x0200 // Itanium -#define EFI_IMAGE_FILE_MACHINE_EBC 0x0ebc // EFI Byte Code -#define EFI_IMAGE_FILE_MACHINE_AMD64 0x8664 // x86-64 -#define EFI_IMAGE_FILE_MACHINE_AARCH64 0xaa64 // ARMv8 in 64-bit mode -#define EFI_IMAGE_FILE_MACHINE_RISCV32 0x5032 // RISC-V 32-bit -#define EFI_IMAGE_FILE_MACHINE_RISCV64 0x5064 // RISC-V 64-bit -#define EFI_IMAGE_FILE_MACHINE_RISCV128 0x5128 // RISC-V 128-bit -// -// EXE file formats -// -#define EFI_IMAGE_DOS_SIGNATURE 0x5A4D // MZ -#define EFI_IMAGE_PE_SIGNATURE 0x00004550 // PE - -// -// PE images can start with an optional DOS header, so if an image is run -// under DOS it can print an error message. -// -typedef struct { - UINT16 e_magic; // Magic number - UINT16 e_cblp; // Bytes on last page of file - UINT16 e_cp; // Pages in file - UINT16 e_crlc; // Relocations - UINT16 e_cparhdr; // Size of header in paragraphs - UINT16 e_minalloc; // Minimum extra paragraphs needed - UINT16 e_maxalloc; // Maximum extra paragraphs needed - UINT16 e_ss; // Initial (relative) SS value - UINT16 e_sp; // Initial SP value - UINT16 e_csum; // Checksum - UINT16 e_ip; // Initial IP value - UINT16 e_cs; // Initial (relative) CS value - UINT16 e_lfarlc; // File address of relocation table - UINT16 e_ovno; // Overlay number - UINT16 e_res[4]; // Reserved words - UINT16 e_oemid; // OEM identifier (for e_oeminfo) - UINT16 e_oeminfo; // OEM information; e_oemid specific - UINT16 e_res2[10]; // Reserved words - UINT32 e_lfanew; // File address of new header -} EFI_IMAGE_DOS_HEADER; - -// -// COFF File Header (Object and Image) -// -typedef struct { - UINT16 Machine; - UINT16 NumberOfSections; - UINT32 TimeDateStamp; - UINT32 PointerToSymbolTable; - UINT32 NumberOfSymbols; - UINT16 SizeOfOptionalHeader; - UINT16 Characteristics; -} EFI_IMAGE_FILE_HEADER; - -// -// Size of EFI_IMAGE_FILE_HEADER. -// -#define EFI_IMAGE_SIZEOF_FILE_HEADER 20 - -// -// Characteristics -// -#define EFI_IMAGE_FILE_RELOCS_STRIPPED 0x0001 // Relocation info stripped from file -#define EFI_IMAGE_FILE_EXECUTABLE_IMAGE 0x0002 // File is executable (i.e. no unresolved external references) -#define EFI_IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004 // Line numbers stripped from file -#define EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008 // Local symbols stripped from file -#define EFI_IMAGE_FILE_BYTES_REVERSED_LO 0x0080 // Bytes of machine word are reversed -#define EFI_IMAGE_FILE_32BIT_MACHINE 0x0100 // 32 bit word machine -#define EFI_IMAGE_FILE_DEBUG_STRIPPED 0x0200 // Debugging info stripped from file in .DBG file -#define EFI_IMAGE_FILE_SYSTEM 0x1000 // System File -#define EFI_IMAGE_FILE_DLL 0x2000 // File is a DLL -#define EFI_IMAGE_FILE_BYTES_REVERSED_HI 0x8000 // Bytes of machine word are reversed - -// -// Header Data Directories. -// -typedef struct { - UINT32 VirtualAddress; - UINT32 Size; -} EFI_IMAGE_DATA_DIRECTORY; - -// -// Directory Entries -// -#define EFI_IMAGE_DIRECTORY_ENTRY_EXPORT 0 -#define EFI_IMAGE_DIRECTORY_ENTRY_IMPORT 1 -#define EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE 2 -#define EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 -#define EFI_IMAGE_DIRECTORY_ENTRY_SECURITY 4 -#define EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC 5 -#define EFI_IMAGE_DIRECTORY_ENTRY_DEBUG 6 -#define EFI_IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 -#define EFI_IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 -#define EFI_IMAGE_DIRECTORY_ENTRY_TLS 9 -#define EFI_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 - -#define EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES 16 - -// -// EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC means PE32 and -// EFI_IMAGE_OPTIONAL_HEADER32 must be used. -// The data structures only vary after NT additional fields -// -#define EFI_IMAGE_PE_OPTIONAL_HDR32_MAGIC 0x10b - -// -// Optional Header Standard Fields for PE32 -// -typedef struct { - // - // Standard fields. - // - UINT16 Magic; - UINT8 MajorLinkerVersion; - UINT8 MinorLinkerVersion; - UINT32 SizeOfCode; - UINT32 SizeOfInitializedData; - UINT32 SizeOfUninitializedData; - UINT32 AddressOfEntryPoint; - UINT32 BaseOfCode; - UINT32 BaseOfData; // PE32 contains this additional field, which is absent in PE32+. - // - // Optional Header Windows-Specific Fields. - // - UINT32 ImageBase; - UINT32 SectionAlignment; - UINT32 FileAlignment; - UINT16 MajorOperatingSystemVersion; - UINT16 MinorOperatingSystemVersion; - UINT16 MajorImageVersion; - UINT16 MinorImageVersion; - UINT16 MajorSubsystemVersion; - UINT16 MinorSubsystemVersion; - UINT32 Win32VersionValue; - UINT32 SizeOfImage; - UINT32 SizeOfHeaders; - UINT32 CheckSum; - UINT16 Subsystem; - UINT16 DllCharacteristics; - UINT32 SizeOfStackReserve; - UINT32 SizeOfStackCommit; - UINT32 SizeOfHeapReserve; - UINT32 SizeOfHeapCommit; - UINT32 LoaderFlags; - UINT32 NumberOfRvaAndSizes; - EFI_IMAGE_DATA_DIRECTORY DataDirectory[EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES]; -} EFI_IMAGE_OPTIONAL_HEADER32; - -// -// EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC means PE32+ and -// EFI_IMAGE_OPTIONAL_HEADER64 must be used. -// The data structures only vary after NT additional fields -// -#define EFI_IMAGE_PE_OPTIONAL_HDR64_MAGIC 0x20b - -// -// Optional Header Standard Fields for PE32+. -// -typedef struct { - // - // Standard fields. - // - UINT16 Magic; - UINT8 MajorLinkerVersion; - UINT8 MinorLinkerVersion; - UINT32 SizeOfCode; - UINT32 SizeOfInitializedData; - UINT32 SizeOfUninitializedData; - UINT32 AddressOfEntryPoint; - UINT32 BaseOfCode; - // - // Optional Header Windows-Specific Fields. - // - UINT64 ImageBase; - UINT32 SectionAlignment; - UINT32 FileAlignment; - UINT16 MajorOperatingSystemVersion; - UINT16 MinorOperatingSystemVersion; - UINT16 MajorImageVersion; - UINT16 MinorImageVersion; - UINT16 MajorSubsystemVersion; - UINT16 MinorSubsystemVersion; - UINT32 Win32VersionValue; - UINT32 SizeOfImage; - UINT32 SizeOfHeaders; - UINT32 CheckSum; - UINT16 Subsystem; - UINT16 DllCharacteristics; - UINT64 SizeOfStackReserve; - UINT64 SizeOfStackCommit; - UINT64 SizeOfHeapReserve; - UINT64 SizeOfHeapCommit; - UINT32 LoaderFlags; - UINT32 NumberOfRvaAndSizes; - EFI_IMAGE_DATA_DIRECTORY DataDirectory[EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES]; -} EFI_IMAGE_OPTIONAL_HEADER64; - -// Union for pointers to either PE32 or PE32+ headers -typedef union _EFI_IMAGE_OPTIONAL_HEADER_POINTERS_UNION { - const EFI_IMAGE_OPTIONAL_HEADER32* H32; - const EFI_IMAGE_OPTIONAL_HEADER64* H64; -} EFI_IMAGE_OPTIONAL_HEADER_POINTERS_UNION; - -typedef struct -{ - UINT32 Signature; - //EFI_IMAGE_FILE_HEADER FileHeader; - //EFI_IMAGE_OPTIONAL_HEADER OptionalHeader; -} EFI_IMAGE_PE_HEADER; - -// -// Other Windows Subsystem Values -// -#define EFI_IMAGE_SUBSYSTEM_UNKNOWN 0 -#define EFI_IMAGE_SUBSYSTEM_NATIVE 1 -#define EFI_IMAGE_SUBSYSTEM_WINDOWS_GUI 2 -#define EFI_IMAGE_SUBSYSTEM_WINDOWS_CUI 3 -#define EFI_IMAGE_SUBSYSTEM_OS2_CUI 5 -#define EFI_IMAGE_SUBSYSTEM_POSIX_CUI 7 - -// -// Length of ShortName -// -#define EFI_IMAGE_SIZEOF_SHORT_NAME 8 - -// -// Section Table. This table immediately follows the optional header. -// -typedef struct { - UINT8 Name[EFI_IMAGE_SIZEOF_SHORT_NAME]; - union { - UINT32 PhysicalAddress; - UINT32 VirtualSize; - } Misc; - UINT32 VirtualAddress; - UINT32 SizeOfRawData; - UINT32 PointerToRawData; - UINT32 PointerToRelocations; - UINT32 PointerToLinenumbers; - UINT16 NumberOfRelocations; - UINT16 NumberOfLinenumbers; - UINT32 Characteristics; -} EFI_IMAGE_SECTION_HEADER; - -// -// Size of EFI_IMAGE_SECTION_HEADER -// -#define EFI_IMAGE_SIZEOF_SECTION_HEADER 40 - -// -// Section Flags Values -// -#define EFI_IMAGE_SCN_TYPE_NO_PAD 0x00000008 // Reserved -#define EFI_IMAGE_SCN_CNT_CODE 0x00000020 -#define EFI_IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 -#define EFI_IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080 - -#define EFI_IMAGE_SCN_LNK_OTHER 0x00000100 // Reserved -#define EFI_IMAGE_SCN_LNK_INFO 0x00000200 // Section contains comments or some other type of information -#define EFI_IMAGE_SCN_LNK_REMOVE 0x00000800 // Section contents will not become part of image -#define EFI_IMAGE_SCN_LNK_COMDAT 0x00001000 - -#define EFI_IMAGE_SCN_ALIGN_1BYTES 0x00100000 -#define EFI_IMAGE_SCN_ALIGN_2BYTES 0x00200000 -#define EFI_IMAGE_SCN_ALIGN_4BYTES 0x00300000 -#define EFI_IMAGE_SCN_ALIGN_8BYTES 0x00400000 -#define EFI_IMAGE_SCN_ALIGN_16BYTES 0x00500000 -#define EFI_IMAGE_SCN_ALIGN_32BYTES 0x00600000 -#define EFI_IMAGE_SCN_ALIGN_64BYTES 0x00700000 - -#define EFI_IMAGE_SCN_MEM_DISCARDABLE 0x02000000 -#define EFI_IMAGE_SCN_MEM_NOT_CACHED 0x04000000 -#define EFI_IMAGE_SCN_MEM_NOT_PAGED 0x08000000 -#define EFI_IMAGE_SCN_MEM_SHARED 0x10000000 -#define EFI_IMAGE_SCN_MEM_EXECUTE 0x20000000 -#define EFI_IMAGE_SCN_MEM_READ 0x40000000 -#define EFI_IMAGE_SCN_MEM_WRITE 0x80000000 - -// -// Size of a Symbol Table Record -// -#define EFI_IMAGE_SIZEOF_SYMBOL 18 - -// -// Symbols have a section number of the section in which they are -// defined. Otherwise, section numbers have the following meanings: -// -#define EFI_IMAGE_SYM_UNDEFINED (UINT16) 0 ///< Symbol is undefined or is common -#define EFI_IMAGE_SYM_ABSOLUTE (UINT16) -1 ///< Symbol is an absolute value -#define EFI_IMAGE_SYM_DEBUG (UINT16) -2 ///< Symbol is a special debug item - -// -// Symbol Type (fundamental) values. -// -#define EFI_IMAGE_SYM_TYPE_NULL 0 // no type -#define EFI_IMAGE_SYM_TYPE_VOID 1 // no valid type -#define EFI_IMAGE_SYM_TYPE_CHAR 2 // type character -#define EFI_IMAGE_SYM_TYPE_SHORT 3 // type short integer -#define EFI_IMAGE_SYM_TYPE_INT 4 -#define EFI_IMAGE_SYM_TYPE_LONG 5 -#define EFI_IMAGE_SYM_TYPE_FLOAT 6 -#define EFI_IMAGE_SYM_TYPE_DOUBLE 7 -#define EFI_IMAGE_SYM_TYPE_STRUCT 8 -#define EFI_IMAGE_SYM_TYPE_UNION 9 -#define EFI_IMAGE_SYM_TYPE_ENUM 10 // enumeration -#define EFI_IMAGE_SYM_TYPE_MOE 11 // member of enumeration -#define EFI_IMAGE_SYM_TYPE_BYTE 12 -#define EFI_IMAGE_SYM_TYPE_WORD 13 -#define EFI_IMAGE_SYM_TYPE_UINT 14 -#define EFI_IMAGE_SYM_TYPE_DWORD 15 - -// -// Symbol Type (derived) values -// -#define EFI_IMAGE_SYM_DTYPE_NULL 0 // no derived type -#define EFI_IMAGE_SYM_DTYPE_POINTER 1 -#define EFI_IMAGE_SYM_DTYPE_FUNCTION 2 -#define EFI_IMAGE_SYM_DTYPE_ARRAY 3 - -// -// Storage classes -// -#define EFI_IMAGE_SYM_CLASS_END_OF_FUNCTION ((UINT8) -1) -#define EFI_IMAGE_SYM_CLASS_NULL 0 -#define EFI_IMAGE_SYM_CLASS_AUTOMATIC 1 -#define EFI_IMAGE_SYM_CLASS_EXTERNAL 2 -#define EFI_IMAGE_SYM_CLASS_STATIC 3 -#define EFI_IMAGE_SYM_CLASS_REGISTER 4 -#define EFI_IMAGE_SYM_CLASS_EXTERNAL_DEF 5 -#define EFI_IMAGE_SYM_CLASS_LABEL 6 -#define EFI_IMAGE_SYM_CLASS_UNDEFINED_LABEL 7 -#define EFI_IMAGE_SYM_CLASS_MEMBER_OF_STRUCT 8 -#define EFI_IMAGE_SYM_CLASS_ARGUMENT 9 -#define EFI_IMAGE_SYM_CLASS_STRUCT_TAG 10 -#define EFI_IMAGE_SYM_CLASS_MEMBER_OF_UNION 11 -#define EFI_IMAGE_SYM_CLASS_UNION_TAG 12 -#define EFI_IMAGE_SYM_CLASS_TYPE_DEFINITION 13 -#define EFI_IMAGE_SYM_CLASS_UNDEFINED_STATIC 14 -#define EFI_IMAGE_SYM_CLASS_ENUM_TAG 15 -#define EFI_IMAGE_SYM_CLASS_MEMBER_OF_ENUM 16 -#define EFI_IMAGE_SYM_CLASS_REGISTER_PARAM 17 -#define EFI_IMAGE_SYM_CLASS_BIT_FIELD 18 -#define EFI_IMAGE_SYM_CLASS_BLOCK 100 -#define EFI_IMAGE_SYM_CLASS_FUNCTION 101 -#define EFI_IMAGE_SYM_CLASS_END_OF_STRUCT 102 -#define EFI_IMAGE_SYM_CLASS_FILE 103 -#define EFI_IMAGE_SYM_CLASS_SECTION 104 -#define EFI_IMAGE_SYM_CLASS_WEAK_EXTERNAL 105 - -// -// Type packing constants -// -#define EFI_IMAGE_N_BTMASK 017 -#define EFI_IMAGE_N_TMASK 060 -#define EFI_IMAGE_N_TMASK1 0300 -#define EFI_IMAGE_N_TMASK2 0360 -#define EFI_IMAGE_N_BTSHFT 4 -#define EFI_IMAGE_N_TSHIFT 2 - -// -// Communal selection types -// -#define EFI_IMAGE_COMDAT_SELECT_NODUPLICATES 1 -#define EFI_IMAGE_COMDAT_SELECT_ANY 2 -#define EFI_IMAGE_COMDAT_SELECT_SAME_SIZE 3 -#define EFI_IMAGE_COMDAT_SELECT_EXACT_MATCH 4 -#define EFI_IMAGE_COMDAT_SELECT_ASSOCIATIVE 5 - -// -// The following values only be referred in PeCoff, not defined in PECOFF -// -#define EFI_IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY 1 -#define EFI_IMAGE_WEAK_EXTERN_SEARCH_LIBRARY 2 -#define EFI_IMAGE_WEAK_EXTERN_SEARCH_ALIAS 3 - -// -// Relocation format -// -typedef struct { - UINT32 VirtualAddress; - UINT32 SymbolTableIndex; - UINT16 Type; -} EFI_IMAGE_RELOCATION; - -// -// Size of EFI_IMAGE_RELOCATION -// -#define EFI_IMAGE_SIZEOF_RELOCATION 10 - -// -// I386 relocation types -// -#define EFI_IMAGE_REL_I386_ABSOLUTE 0x0000 // Reference is absolute, no relocation is necessary -#define EFI_IMAGE_REL_I386_DIR16 0x0001 // Direct 16-bit reference to the symbols virtual address -#define EFI_IMAGE_REL_I386_REL16 0x0002 // PC-relative 16-bit reference to the symbols virtual address -#define EFI_IMAGE_REL_I386_DIR32 0x0006 // Direct 32-bit reference to the symbols virtual address -#define EFI_IMAGE_REL_I386_DIR32NB 0x0007 // Direct 32-bit reference to the symbols virtual address, base not included -#define EFI_IMAGE_REL_I386_SEG12 0x0009 // Direct 16-bit reference to the segment-selector bits of a 32-bit virtual address -#define EFI_IMAGE_REL_I386_SECTION 0x000A -#define EFI_IMAGE_REL_I386_SECREL 0x000B -#define EFI_IMAGE_REL_I386_REL32 0x0014 // PC-relative 32-bit reference to the symbols virtual address - - -// -// x64 relocation types -// -#define EFI_IMAGE_REL_AMD64_ABSOLUTE 0x0000 -#define EFI_IMAGE_REL_AMD64_ADDR64 0x0001 -#define EFI_IMAGE_REL_AMD64_ADDR32 0x0002 -#define EFI_IMAGE_REL_AMD64_ADDR32NB 0x0003 -#define EFI_IMAGE_REL_AMD64_REL32 0x0004 -#define EFI_IMAGE_REL_AMD64_REL32_1 0x0005 -#define EFI_IMAGE_REL_AMD64_REL32_2 0x0006 -#define EFI_IMAGE_REL_AMD64_REL32_3 0x0007 -#define EFI_IMAGE_REL_AMD64_REL32_4 0x0008 -#define EFI_IMAGE_REL_AMD64_REL32_5 0x0009 -#define EFI_IMAGE_REL_AMD64_SECTION 0x000A -#define EFI_IMAGE_REL_AMD64_SECREL 0x000B -#define EFI_IMAGE_REL_AMD64_SECREL7 0x000C -#define EFI_IMAGE_REL_AMD64_TOKEN 0x000D -#define EFI_IMAGE_REL_AMD64_SREL32 0x000E -#define EFI_IMAGE_REL_AMD64_PAIR 0x000F -#define EFI_IMAGE_REL_AMD64_SSPAN32 0x0010 - -// -// RISC-V relocation types -// -#define EFI_IMAGE_REL_BASED_RISCV_HI20 0x0005 -#define EFI_IMAGE_REL_BASED_RISCV_LO12I 0x0007 -#define EFI_IMAGE_REL_BASED_RISCV_LO12S 0x0008 - -// -// Based relocation format -// -typedef struct { - UINT32 VirtualAddress; - UINT32 SizeOfBlock; -} EFI_IMAGE_BASE_RELOCATION; - -// -// Size of EFI_IMAGE_BASE_RELOCATION -// -#define EFI_IMAGE_SIZEOF_BASE_RELOCATION 8 - -// -// Based relocation types -// -#define EFI_IMAGE_REL_BASED_ABSOLUTE 0 -#define EFI_IMAGE_REL_BASED_HIGH 1 -#define EFI_IMAGE_REL_BASED_LOW 2 -#define EFI_IMAGE_REL_BASED_HIGHLOW 3 -#define EFI_IMAGE_REL_BASED_HIGHADJ 4 -#define EFI_IMAGE_REL_BASED_MIPS_JMPADDR 5 -#define EFI_IMAGE_REL_BASED_ARM_MOV32A 5 -#define EFI_IMAGE_REL_BASED_ARM_MOV32T 7 -#define EFI_IMAGE_REL_BASED_IA64_IMM64 9 -#define EFI_IMAGE_REL_BASED_MIPS_JMPADDR16 9 -#define EFI_IMAGE_REL_BASED_DIR64 10 - -// -// Line number format -// -typedef struct { - union { - UINT32 SymbolTableIndex; // Symbol table index of function name if line number is 0 - UINT32 VirtualAddress; // Virtual address of line number - } Type; - UINT16 Linenumber; // Line number -} EFI_IMAGE_LINENUMBER; - -// -// Size of EFI_IMAGE_LINENUMBER -// -#define EFI_IMAGE_SIZEOF_LINENUMBER 6 - -// -// Archive format -// -#define EFI_IMAGE_ARCHIVE_START_SIZE 8 -#define EFI_IMAGE_ARCHIVE_START "!\n" -#define EFI_IMAGE_ARCHIVE_END "`\n" -#define EFI_IMAGE_ARCHIVE_PAD "\n" -#define EFI_IMAGE_ARCHIVE_LINKER_MEMBER "/ " -#define EFI_IMAGE_ARCHIVE_LONGNAMES_MEMBER "// " - -// -// Archive Member Headers -// -typedef struct { - UINT8 Name[16]; // File member name - `/' terminated - UINT8 Date[12]; // File member date - decimal - UINT8 UserID[6]; // File member user id - decimal - UINT8 GroupID[6]; // File member group id - decimal - UINT8 Mode[8]; // File member mode - octal - UINT8 Size[10]; // File member size - decimal - UINT8 EndHeader[2]; // String to end header. (0x60 0x0A) -} EFI_IMAGE_ARCHIVE_MEMBER_HEADER; - -// -// Size of EFI_IMAGE_ARCHIVE_MEMBER_HEADER -// -#define EFI_IMAGE_SIZEOF_ARCHIVE_MEMBER_HDR 60 - -// -// DLL Support -// - -// -// Export Directory Table -// -typedef struct { - UINT32 Characteristics; - UINT32 TimeDateStamp; - UINT16 MajorVersion; - UINT16 MinorVersion; - UINT32 Name; - UINT32 Base; - UINT32 NumberOfFunctions; - UINT32 NumberOfNames; - UINT32 AddressOfFunctions; - UINT32 AddressOfNames; - UINT32 AddressOfNameOrdinals; -} EFI_IMAGE_EXPORT_DIRECTORY; - -// -// Hint/Name Table -// -typedef struct { - UINT16 Hint; - UINT8 Name[1]; -} EFI_IMAGE_IMPORT_BY_NAME; - -// -// Import Address Table RVA (Thunk Table) -// -typedef struct { - union { - UINT32 Function; - UINT32 Ordinal; - EFI_IMAGE_IMPORT_BY_NAME *AddressOfData; - } u1; -} EFI_IMAGE_THUNK_DATA; - -#define EFI_IMAGE_ORDINAL_FLAG 0x80000000 // Flag for PE32. -#define EFI_IMAGE_SNAP_BY_ORDINAL(Ordinal) ((Ordinal & EFI_IMAGE_ORDINAL_FLAG) != 0) -#define EFI_IMAGE_ORDINAL(Ordinal) (Ordinal & 0xffff) - -// -// Import Directory Table -// -typedef struct { - UINT32 Characteristics; - UINT32 TimeDateStamp; - UINT32 ForwarderChain; - UINT32 Name; - EFI_IMAGE_THUNK_DATA *FirstThunk; -} EFI_IMAGE_IMPORT_DESCRIPTOR; - -// -// Debug Directory Format -// -typedef struct { - UINT32 Characteristics; - UINT32 TimeDateStamp; - UINT16 MajorVersion; - UINT16 MinorVersion; - UINT32 Type; - UINT32 SizeOfData; - UINT32 RVA; // The address of the debug data when loaded, relative to the image base - UINT32 FileOffset; // The file pointer to the debug data -} EFI_IMAGE_DEBUG_DIRECTORY_ENTRY; - -#define EFI_IMAGE_DEBUG_TYPE_CODEVIEW 2 // The Visual C++ debug information. - -// -// Debug Data Structure defined in Microsoft C++ -// -#define CODEVIEW_SIGNATURE_NB10 0x3031424E // NB10 -typedef struct { - UINT32 Signature; - UINT32 Unknown; - UINT32 Unknown2; - UINT32 Unknown3; - // - // Filename of .PDB goes here - // -} EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY; - -// -// Debug Data Structure defined in Microsoft C++ -// -#define CODEVIEW_SIGNATURE_RSDS 0x53445352 // RSDS -typedef struct { - UINT32 Signature; - UINT32 Unknown; - UINT32 Unknown2; - UINT32 Unknown3; - UINT32 Unknown4; - UINT32 Unknown5; - // - // Filename of .PDB goes here - // -} EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY; - -// -// Debug Data Structure defined by Apple Mach-O to COFF utility. -// -#define CODEVIEW_SIGNATURE_MTOC 0x434F544D // MTOC -typedef struct { - UINT32 Signature; - UINT8 MachOUuid[16]; - // - // Filename of .DLL (Mach-O with debug info) goes here - // -} EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY; - -// -// Resource format -// -typedef struct { - UINT32 Characteristics; - UINT32 TimeDateStamp; - UINT16 MajorVersion; - UINT16 MinorVersion; - UINT16 NumberOfNamedEntries; - UINT16 NumberOfIdEntries; - // - // Array of EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY entries goes here - // -} EFI_IMAGE_RESOURCE_DIRECTORY; - -// -// Resource directory entry format -// -typedef struct { - union { - struct { - UINT32 NameOffset : 31; - UINT32 NameIsString : 1; - } s; - UINT32 Id; - } u1; - union { - UINT32 OffsetToData; - struct { - UINT32 OffsetToDirectory : 31; - UINT32 DataIsDirectory : 1; - } s; - } u2; -} EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY; - -// -// Resource directory entry for string -// -typedef struct { - UINT16 Length; - CHAR16 String[1]; -} EFI_IMAGE_RESOURCE_DIRECTORY_STRING; - -// -// Resource directory entry for data array -// -typedef struct { - UINT32 OffsetToData; - UINT32 Size; - UINT32 CodePage; - UINT32 Reserved; -} EFI_IMAGE_RESOURCE_DATA_ENTRY; - -// -// Header format for TE images, defined in the PI Specification 1.0. -// -typedef struct { - UINT16 Signature; // The signature for TE format = "VZ" - UINT16 Machine; // From original file header - UINT8 NumberOfSections; // From original file header - UINT8 Subsystem; // From original optional header - UINT16 StrippedSize; // Number of bytes we removed from header - UINT32 AddressOfEntryPoint; // Offset to entry point -- from original optional header - UINT32 BaseOfCode; // From original image -- required for ITP debug - UINT64 ImageBase; // From original file header - EFI_IMAGE_DATA_DIRECTORY DataDirectory[2]; // Only base relocation and debug directory -} EFI_IMAGE_TE_HEADER; - -#define EFI_IMAGE_TE_SIGNATURE 0x5A56 // VZ - -// -// Data directory indexes in our TE image header -// -#define EFI_IMAGE_TE_DIRECTORY_ENTRY_BASERELOC 0 -#define EFI_IMAGE_TE_DIRECTORY_ENTRY_DEBUG 1 - -// Restore previous packing rules -#pragma pack(pop) -#endif // PEIMAGE_H +/* peimage.h + +Copyright (c) 2015, Nikolaj Schlej. All rights reserved. +Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved. +Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved. +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php. + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +*/ + +#ifndef PEIMAGE_H +#define PEIMAGE_H + +#include "ustring.h" +#include "basetypes.h" + +extern UString machineTypeToUString(UINT16 machineType); + +// Make sure we use right packing rules +#pragma pack(push, 1) + +// +// PE32+ Subsystem type for EFI images +// +#define EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION 10 +#define EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER 11 +#define EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER 12 +#define EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER 13 + +// +// PE32+ Machine type for EFI images +// +#define EFI_IMAGE_FILE_MACHINE_I386 0x014c // x86 +#define EFI_IMAGE_FILE_MACHINE_ARM 0x01c0 // ARM little endian +#define EFI_IMAGE_FILE_MACHINE_THUMB 0x01c2 // ARM or Thumb (interworking) +#define EFI_IMAGE_FILE_MACHINE_ARMNT 0x01c4 // ARMv7 (or higher) Thumb mode only +#define EFI_IMAGE_FILE_MACHINE_APPLE_ARM 0x01c6 // Apple ARM +#define EFI_IMAGE_FILE_MACHINE_POWERPC 0x01f0 // Power PC little endian +#define EFI_IMAGE_FILE_MACHINE_POWERPCFP 0x01f1 // Power PC with floating point support +#define EFI_IMAGE_FILE_MACHINE_IA64 0x0200 // Itanium +#define EFI_IMAGE_FILE_MACHINE_EBC 0x0ebc // EFI Byte Code +#define EFI_IMAGE_FILE_MACHINE_AMD64 0x8664 // x86-64 +#define EFI_IMAGE_FILE_MACHINE_AARCH64 0xaa64 // ARMv8 in 64-bit mode +#define EFI_IMAGE_FILE_MACHINE_RISCV32 0x5032 // RISC-V 32-bit +#define EFI_IMAGE_FILE_MACHINE_RISCV64 0x5064 // RISC-V 64-bit +#define EFI_IMAGE_FILE_MACHINE_RISCV128 0x5128 // RISC-V 128-bit + +// +// EXE file formats +// +#define EFI_IMAGE_DOS_SIGNATURE 0x5A4D // MZ +#define EFI_IMAGE_PE_SIGNATURE 0x00004550 // PE + +// +// PE images can start with an optional DOS header, so if an image is run +// under DOS it can print an error message. +// +typedef struct { + UINT16 e_magic; // Magic number + UINT16 e_cblp; // Bytes on last page of file + UINT16 e_cp; // Pages in file + UINT16 e_crlc; // Relocations + UINT16 e_cparhdr; // Size of header in paragraphs + UINT16 e_minalloc; // Minimum extra paragraphs needed + UINT16 e_maxalloc; // Maximum extra paragraphs needed + UINT16 e_ss; // Initial (relative) SS value + UINT16 e_sp; // Initial SP value + UINT16 e_csum; // Checksum + UINT16 e_ip; // Initial IP value + UINT16 e_cs; // Initial (relative) CS value + UINT16 e_lfarlc; // File address of relocation table + UINT16 e_ovno; // Overlay number + UINT16 e_res[4]; // Reserved words + UINT16 e_oemid; // OEM identifier (for e_oeminfo) + UINT16 e_oeminfo; // OEM information; e_oemid specific + UINT16 e_res2[10]; // Reserved words + UINT32 e_lfanew; // File address of new header +} EFI_IMAGE_DOS_HEADER; + +// +// COFF File Header (Object and Image) +// +typedef struct { + UINT16 Machine; + UINT16 NumberOfSections; + UINT32 TimeDateStamp; + UINT32 PointerToSymbolTable; + UINT32 NumberOfSymbols; + UINT16 SizeOfOptionalHeader; + UINT16 Characteristics; +} EFI_IMAGE_FILE_HEADER; + +// +// Size of EFI_IMAGE_FILE_HEADER. +// +#define EFI_IMAGE_SIZEOF_FILE_HEADER 20 + +// +// Characteristics +// +#define EFI_IMAGE_FILE_RELOCS_STRIPPED 0x0001 // Relocation info stripped from file +#define EFI_IMAGE_FILE_EXECUTABLE_IMAGE 0x0002 // File is executable (i.e. no unresolved external references) +#define EFI_IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004 // Line numbers stripped from file +#define EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008 // Local symbols stripped from file +#define EFI_IMAGE_FILE_BYTES_REVERSED_LO 0x0080 // Bytes of machine word are reversed +#define EFI_IMAGE_FILE_32BIT_MACHINE 0x0100 // 32 bit word machine +#define EFI_IMAGE_FILE_DEBUG_STRIPPED 0x0200 // Debugging info stripped from file in .DBG file +#define EFI_IMAGE_FILE_SYSTEM 0x1000 // System File +#define EFI_IMAGE_FILE_DLL 0x2000 // File is a DLL +#define EFI_IMAGE_FILE_BYTES_REVERSED_HI 0x8000 // Bytes of machine word are reversed + +// +// Header Data Directories. +// +typedef struct { + UINT32 VirtualAddress; + UINT32 Size; +} EFI_IMAGE_DATA_DIRECTORY; + +// +// Directory Entries +// +#define EFI_IMAGE_DIRECTORY_ENTRY_EXPORT 0 +#define EFI_IMAGE_DIRECTORY_ENTRY_IMPORT 1 +#define EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE 2 +#define EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 +#define EFI_IMAGE_DIRECTORY_ENTRY_SECURITY 4 +#define EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC 5 +#define EFI_IMAGE_DIRECTORY_ENTRY_DEBUG 6 +#define EFI_IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 +#define EFI_IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 +#define EFI_IMAGE_DIRECTORY_ENTRY_TLS 9 +#define EFI_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 + +#define EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES 16 + +// +// EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC means PE32 and +// EFI_IMAGE_OPTIONAL_HEADER32 must be used. +// The data structures only vary after NT additional fields +// +#define EFI_IMAGE_PE_OPTIONAL_HDR32_MAGIC 0x10b + +// +// Optional Header Standard Fields for PE32 +// +typedef struct { + // + // Standard fields. + // + UINT16 Magic; + UINT8 MajorLinkerVersion; + UINT8 MinorLinkerVersion; + UINT32 SizeOfCode; + UINT32 SizeOfInitializedData; + UINT32 SizeOfUninitializedData; + UINT32 AddressOfEntryPoint; + UINT32 BaseOfCode; + UINT32 BaseOfData; // PE32 contains this additional field, which is absent in PE32+. + // + // Optional Header Windows-Specific Fields. + // + UINT32 ImageBase; + UINT32 SectionAlignment; + UINT32 FileAlignment; + UINT16 MajorOperatingSystemVersion; + UINT16 MinorOperatingSystemVersion; + UINT16 MajorImageVersion; + UINT16 MinorImageVersion; + UINT16 MajorSubsystemVersion; + UINT16 MinorSubsystemVersion; + UINT32 Win32VersionValue; + UINT32 SizeOfImage; + UINT32 SizeOfHeaders; + UINT32 CheckSum; + UINT16 Subsystem; + UINT16 DllCharacteristics; + UINT32 SizeOfStackReserve; + UINT32 SizeOfStackCommit; + UINT32 SizeOfHeapReserve; + UINT32 SizeOfHeapCommit; + UINT32 LoaderFlags; + UINT32 NumberOfRvaAndSizes; + EFI_IMAGE_DATA_DIRECTORY DataDirectory[EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES]; +} EFI_IMAGE_OPTIONAL_HEADER32; + +// +// EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC means PE32+ and +// EFI_IMAGE_OPTIONAL_HEADER64 must be used. +// The data structures only vary after NT additional fields +// +#define EFI_IMAGE_PE_OPTIONAL_HDR64_MAGIC 0x20b + +// +// Optional Header Standard Fields for PE32+. +// +typedef struct { + // + // Standard fields. + // + UINT16 Magic; + UINT8 MajorLinkerVersion; + UINT8 MinorLinkerVersion; + UINT32 SizeOfCode; + UINT32 SizeOfInitializedData; + UINT32 SizeOfUninitializedData; + UINT32 AddressOfEntryPoint; + UINT32 BaseOfCode; + // + // Optional Header Windows-Specific Fields. + // + UINT64 ImageBase; + UINT32 SectionAlignment; + UINT32 FileAlignment; + UINT16 MajorOperatingSystemVersion; + UINT16 MinorOperatingSystemVersion; + UINT16 MajorImageVersion; + UINT16 MinorImageVersion; + UINT16 MajorSubsystemVersion; + UINT16 MinorSubsystemVersion; + UINT32 Win32VersionValue; + UINT32 SizeOfImage; + UINT32 SizeOfHeaders; + UINT32 CheckSum; + UINT16 Subsystem; + UINT16 DllCharacteristics; + UINT64 SizeOfStackReserve; + UINT64 SizeOfStackCommit; + UINT64 SizeOfHeapReserve; + UINT64 SizeOfHeapCommit; + UINT32 LoaderFlags; + UINT32 NumberOfRvaAndSizes; + EFI_IMAGE_DATA_DIRECTORY DataDirectory[EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES]; +} EFI_IMAGE_OPTIONAL_HEADER64; + +// Union for pointers to either PE32 or PE32+ headers +typedef union _EFI_IMAGE_OPTIONAL_HEADER_POINTERS_UNION { + const EFI_IMAGE_OPTIONAL_HEADER32* H32; + const EFI_IMAGE_OPTIONAL_HEADER64* H64; +} EFI_IMAGE_OPTIONAL_HEADER_POINTERS_UNION; + +typedef struct +{ + UINT32 Signature; + //EFI_IMAGE_FILE_HEADER FileHeader; + //EFI_IMAGE_OPTIONAL_HEADER OptionalHeader; +} EFI_IMAGE_PE_HEADER; + +// +// Other Windows Subsystem Values +// +#define EFI_IMAGE_SUBSYSTEM_UNKNOWN 0 +#define EFI_IMAGE_SUBSYSTEM_NATIVE 1 +#define EFI_IMAGE_SUBSYSTEM_WINDOWS_GUI 2 +#define EFI_IMAGE_SUBSYSTEM_WINDOWS_CUI 3 +#define EFI_IMAGE_SUBSYSTEM_OS2_CUI 5 +#define EFI_IMAGE_SUBSYSTEM_POSIX_CUI 7 + +// +// Length of ShortName +// +#define EFI_IMAGE_SIZEOF_SHORT_NAME 8 + +// +// Section Table. This table immediately follows the optional header. +// +typedef struct { + UINT8 Name[EFI_IMAGE_SIZEOF_SHORT_NAME]; + union { + UINT32 PhysicalAddress; + UINT32 VirtualSize; + } Misc; + UINT32 VirtualAddress; + UINT32 SizeOfRawData; + UINT32 PointerToRawData; + UINT32 PointerToRelocations; + UINT32 PointerToLinenumbers; + UINT16 NumberOfRelocations; + UINT16 NumberOfLinenumbers; + UINT32 Characteristics; +} EFI_IMAGE_SECTION_HEADER; + +// +// Size of EFI_IMAGE_SECTION_HEADER +// +#define EFI_IMAGE_SIZEOF_SECTION_HEADER 40 + +// +// Section Flags Values +// +#define EFI_IMAGE_SCN_TYPE_NO_PAD 0x00000008 // Reserved +#define EFI_IMAGE_SCN_CNT_CODE 0x00000020 +#define EFI_IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 +#define EFI_IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080 + +#define EFI_IMAGE_SCN_LNK_OTHER 0x00000100 // Reserved +#define EFI_IMAGE_SCN_LNK_INFO 0x00000200 // Section contains comments or some other type of information +#define EFI_IMAGE_SCN_LNK_REMOVE 0x00000800 // Section contents will not become part of image +#define EFI_IMAGE_SCN_LNK_COMDAT 0x00001000 + +#define EFI_IMAGE_SCN_ALIGN_1BYTES 0x00100000 +#define EFI_IMAGE_SCN_ALIGN_2BYTES 0x00200000 +#define EFI_IMAGE_SCN_ALIGN_4BYTES 0x00300000 +#define EFI_IMAGE_SCN_ALIGN_8BYTES 0x00400000 +#define EFI_IMAGE_SCN_ALIGN_16BYTES 0x00500000 +#define EFI_IMAGE_SCN_ALIGN_32BYTES 0x00600000 +#define EFI_IMAGE_SCN_ALIGN_64BYTES 0x00700000 + +#define EFI_IMAGE_SCN_MEM_DISCARDABLE 0x02000000 +#define EFI_IMAGE_SCN_MEM_NOT_CACHED 0x04000000 +#define EFI_IMAGE_SCN_MEM_NOT_PAGED 0x08000000 +#define EFI_IMAGE_SCN_MEM_SHARED 0x10000000 +#define EFI_IMAGE_SCN_MEM_EXECUTE 0x20000000 +#define EFI_IMAGE_SCN_MEM_READ 0x40000000 +#define EFI_IMAGE_SCN_MEM_WRITE 0x80000000 + +// +// Size of a Symbol Table Record +// +#define EFI_IMAGE_SIZEOF_SYMBOL 18 + +// +// Symbols have a section number of the section in which they are +// defined. Otherwise, section numbers have the following meanings: +// +#define EFI_IMAGE_SYM_UNDEFINED (UINT16) 0 ///< Symbol is undefined or is common +#define EFI_IMAGE_SYM_ABSOLUTE (UINT16) -1 ///< Symbol is an absolute value +#define EFI_IMAGE_SYM_DEBUG (UINT16) -2 ///< Symbol is a special debug item + +// +// Symbol Type (fundamental) values. +// +#define EFI_IMAGE_SYM_TYPE_NULL 0 // no type +#define EFI_IMAGE_SYM_TYPE_VOID 1 // no valid type +#define EFI_IMAGE_SYM_TYPE_CHAR 2 // type character +#define EFI_IMAGE_SYM_TYPE_SHORT 3 // type short integer +#define EFI_IMAGE_SYM_TYPE_INT 4 +#define EFI_IMAGE_SYM_TYPE_LONG 5 +#define EFI_IMAGE_SYM_TYPE_FLOAT 6 +#define EFI_IMAGE_SYM_TYPE_DOUBLE 7 +#define EFI_IMAGE_SYM_TYPE_STRUCT 8 +#define EFI_IMAGE_SYM_TYPE_UNION 9 +#define EFI_IMAGE_SYM_TYPE_ENUM 10 // enumeration +#define EFI_IMAGE_SYM_TYPE_MOE 11 // member of enumeration +#define EFI_IMAGE_SYM_TYPE_BYTE 12 +#define EFI_IMAGE_SYM_TYPE_WORD 13 +#define EFI_IMAGE_SYM_TYPE_UINT 14 +#define EFI_IMAGE_SYM_TYPE_DWORD 15 + +// +// Symbol Type (derived) values +// +#define EFI_IMAGE_SYM_DTYPE_NULL 0 // no derived type +#define EFI_IMAGE_SYM_DTYPE_POINTER 1 +#define EFI_IMAGE_SYM_DTYPE_FUNCTION 2 +#define EFI_IMAGE_SYM_DTYPE_ARRAY 3 + +// +// Storage classes +// +#define EFI_IMAGE_SYM_CLASS_END_OF_FUNCTION ((UINT8) -1) +#define EFI_IMAGE_SYM_CLASS_NULL 0 +#define EFI_IMAGE_SYM_CLASS_AUTOMATIC 1 +#define EFI_IMAGE_SYM_CLASS_EXTERNAL 2 +#define EFI_IMAGE_SYM_CLASS_STATIC 3 +#define EFI_IMAGE_SYM_CLASS_REGISTER 4 +#define EFI_IMAGE_SYM_CLASS_EXTERNAL_DEF 5 +#define EFI_IMAGE_SYM_CLASS_LABEL 6 +#define EFI_IMAGE_SYM_CLASS_UNDEFINED_LABEL 7 +#define EFI_IMAGE_SYM_CLASS_MEMBER_OF_STRUCT 8 +#define EFI_IMAGE_SYM_CLASS_ARGUMENT 9 +#define EFI_IMAGE_SYM_CLASS_STRUCT_TAG 10 +#define EFI_IMAGE_SYM_CLASS_MEMBER_OF_UNION 11 +#define EFI_IMAGE_SYM_CLASS_UNION_TAG 12 +#define EFI_IMAGE_SYM_CLASS_TYPE_DEFINITION 13 +#define EFI_IMAGE_SYM_CLASS_UNDEFINED_STATIC 14 +#define EFI_IMAGE_SYM_CLASS_ENUM_TAG 15 +#define EFI_IMAGE_SYM_CLASS_MEMBER_OF_ENUM 16 +#define EFI_IMAGE_SYM_CLASS_REGISTER_PARAM 17 +#define EFI_IMAGE_SYM_CLASS_BIT_FIELD 18 +#define EFI_IMAGE_SYM_CLASS_BLOCK 100 +#define EFI_IMAGE_SYM_CLASS_FUNCTION 101 +#define EFI_IMAGE_SYM_CLASS_END_OF_STRUCT 102 +#define EFI_IMAGE_SYM_CLASS_FILE 103 +#define EFI_IMAGE_SYM_CLASS_SECTION 104 +#define EFI_IMAGE_SYM_CLASS_WEAK_EXTERNAL 105 + +// +// Type packing constants +// +#define EFI_IMAGE_N_BTMASK 017 +#define EFI_IMAGE_N_TMASK 060 +#define EFI_IMAGE_N_TMASK1 0300 +#define EFI_IMAGE_N_TMASK2 0360 +#define EFI_IMAGE_N_BTSHFT 4 +#define EFI_IMAGE_N_TSHIFT 2 + +// +// Communal selection types +// +#define EFI_IMAGE_COMDAT_SELECT_NODUPLICATES 1 +#define EFI_IMAGE_COMDAT_SELECT_ANY 2 +#define EFI_IMAGE_COMDAT_SELECT_SAME_SIZE 3 +#define EFI_IMAGE_COMDAT_SELECT_EXACT_MATCH 4 +#define EFI_IMAGE_COMDAT_SELECT_ASSOCIATIVE 5 + +// +// The following values only be referred in PeCoff, not defined in PECOFF +// +#define EFI_IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY 1 +#define EFI_IMAGE_WEAK_EXTERN_SEARCH_LIBRARY 2 +#define EFI_IMAGE_WEAK_EXTERN_SEARCH_ALIAS 3 + +// +// Relocation format +// +typedef struct { + UINT32 VirtualAddress; + UINT32 SymbolTableIndex; + UINT16 Type; +} EFI_IMAGE_RELOCATION; + +// +// Size of EFI_IMAGE_RELOCATION +// +#define EFI_IMAGE_SIZEOF_RELOCATION 10 + +// +// I386 relocation types +// +#define EFI_IMAGE_REL_I386_ABSOLUTE 0x0000 // Reference is absolute, no relocation is necessary +#define EFI_IMAGE_REL_I386_DIR16 0x0001 // Direct 16-bit reference to the symbols virtual address +#define EFI_IMAGE_REL_I386_REL16 0x0002 // PC-relative 16-bit reference to the symbols virtual address +#define EFI_IMAGE_REL_I386_DIR32 0x0006 // Direct 32-bit reference to the symbols virtual address +#define EFI_IMAGE_REL_I386_DIR32NB 0x0007 // Direct 32-bit reference to the symbols virtual address, base not included +#define EFI_IMAGE_REL_I386_SEG12 0x0009 // Direct 16-bit reference to the segment-selector bits of a 32-bit virtual address +#define EFI_IMAGE_REL_I386_SECTION 0x000A +#define EFI_IMAGE_REL_I386_SECREL 0x000B +#define EFI_IMAGE_REL_I386_REL32 0x0014 // PC-relative 32-bit reference to the symbols virtual address + + +// +// x64 relocation types +// +#define EFI_IMAGE_REL_AMD64_ABSOLUTE 0x0000 +#define EFI_IMAGE_REL_AMD64_ADDR64 0x0001 +#define EFI_IMAGE_REL_AMD64_ADDR32 0x0002 +#define EFI_IMAGE_REL_AMD64_ADDR32NB 0x0003 +#define EFI_IMAGE_REL_AMD64_REL32 0x0004 +#define EFI_IMAGE_REL_AMD64_REL32_1 0x0005 +#define EFI_IMAGE_REL_AMD64_REL32_2 0x0006 +#define EFI_IMAGE_REL_AMD64_REL32_3 0x0007 +#define EFI_IMAGE_REL_AMD64_REL32_4 0x0008 +#define EFI_IMAGE_REL_AMD64_REL32_5 0x0009 +#define EFI_IMAGE_REL_AMD64_SECTION 0x000A +#define EFI_IMAGE_REL_AMD64_SECREL 0x000B +#define EFI_IMAGE_REL_AMD64_SECREL7 0x000C +#define EFI_IMAGE_REL_AMD64_TOKEN 0x000D +#define EFI_IMAGE_REL_AMD64_SREL32 0x000E +#define EFI_IMAGE_REL_AMD64_PAIR 0x000F +#define EFI_IMAGE_REL_AMD64_SSPAN32 0x0010 + +// +// RISC-V relocation types +// +#define EFI_IMAGE_REL_BASED_RISCV_HI20 0x0005 +#define EFI_IMAGE_REL_BASED_RISCV_LO12I 0x0007 +#define EFI_IMAGE_REL_BASED_RISCV_LO12S 0x0008 + + +// +// Based relocation format +// +typedef struct { + UINT32 VirtualAddress; + UINT32 SizeOfBlock; +} EFI_IMAGE_BASE_RELOCATION; + +// +// Size of EFI_IMAGE_BASE_RELOCATION +// +#define EFI_IMAGE_SIZEOF_BASE_RELOCATION 8 + +// +// Based relocation types +// +#define EFI_IMAGE_REL_BASED_ABSOLUTE 0 +#define EFI_IMAGE_REL_BASED_HIGH 1 +#define EFI_IMAGE_REL_BASED_LOW 2 +#define EFI_IMAGE_REL_BASED_HIGHLOW 3 +#define EFI_IMAGE_REL_BASED_HIGHADJ 4 +#define EFI_IMAGE_REL_BASED_MIPS_JMPADDR 5 +#define EFI_IMAGE_REL_BASED_ARM_MOV32A 5 +#define EFI_IMAGE_REL_BASED_ARM_MOV32T 7 +#define EFI_IMAGE_REL_BASED_IA64_IMM64 9 +#define EFI_IMAGE_REL_BASED_MIPS_JMPADDR16 9 +#define EFI_IMAGE_REL_BASED_DIR64 10 + +// +// Line number format +// +typedef struct { + union { + UINT32 SymbolTableIndex; // Symbol table index of function name if line number is 0 + UINT32 VirtualAddress; // Virtual address of line number + } Type; + UINT16 Linenumber; // Line number +} EFI_IMAGE_LINENUMBER; + +// +// Size of EFI_IMAGE_LINENUMBER +// +#define EFI_IMAGE_SIZEOF_LINENUMBER 6 + +// +// Archive format +// +#define EFI_IMAGE_ARCHIVE_START_SIZE 8 +#define EFI_IMAGE_ARCHIVE_START "!\n" +#define EFI_IMAGE_ARCHIVE_END "`\n" +#define EFI_IMAGE_ARCHIVE_PAD "\n" +#define EFI_IMAGE_ARCHIVE_LINKER_MEMBER "/ " +#define EFI_IMAGE_ARCHIVE_LONGNAMES_MEMBER "// " + +// +// Archive Member Headers +// +typedef struct { + UINT8 Name[16]; // File member name - `/' terminated + UINT8 Date[12]; // File member date - decimal + UINT8 UserID[6]; // File member user id - decimal + UINT8 GroupID[6]; // File member group id - decimal + UINT8 Mode[8]; // File member mode - octal + UINT8 Size[10]; // File member size - decimal + UINT8 EndHeader[2]; // String to end header. (0x60 0x0A) +} EFI_IMAGE_ARCHIVE_MEMBER_HEADER; + +// +// Size of EFI_IMAGE_ARCHIVE_MEMBER_HEADER +// +#define EFI_IMAGE_SIZEOF_ARCHIVE_MEMBER_HDR 60 + +// +// DLL Support +// + +// +// Export Directory Table +// +typedef struct { + UINT32 Characteristics; + UINT32 TimeDateStamp; + UINT16 MajorVersion; + UINT16 MinorVersion; + UINT32 Name; + UINT32 Base; + UINT32 NumberOfFunctions; + UINT32 NumberOfNames; + UINT32 AddressOfFunctions; + UINT32 AddressOfNames; + UINT32 AddressOfNameOrdinals; +} EFI_IMAGE_EXPORT_DIRECTORY; + +// +// Hint/Name Table +// +typedef struct { + UINT16 Hint; + UINT8 Name[1]; +} EFI_IMAGE_IMPORT_BY_NAME; + +// +// Import Address Table RVA (Thunk Table) +// +typedef struct { + union { + UINT32 Function; + UINT32 Ordinal; + EFI_IMAGE_IMPORT_BY_NAME *AddressOfData; + } u1; +} EFI_IMAGE_THUNK_DATA; + +#define EFI_IMAGE_ORDINAL_FLAG 0x80000000 // Flag for PE32. +#define EFI_IMAGE_SNAP_BY_ORDINAL(Ordinal) ((Ordinal & EFI_IMAGE_ORDINAL_FLAG) != 0) +#define EFI_IMAGE_ORDINAL(Ordinal) (Ordinal & 0xffff) + +// +// Import Directory Table +// +typedef struct { + UINT32 Characteristics; + UINT32 TimeDateStamp; + UINT32 ForwarderChain; + UINT32 Name; + EFI_IMAGE_THUNK_DATA *FirstThunk; +} EFI_IMAGE_IMPORT_DESCRIPTOR; + +// +// Debug Directory Format +// +typedef struct { + UINT32 Characteristics; + UINT32 TimeDateStamp; + UINT16 MajorVersion; + UINT16 MinorVersion; + UINT32 Type; + UINT32 SizeOfData; + UINT32 RVA; // The address of the debug data when loaded, relative to the image base + UINT32 FileOffset; // The file pointer to the debug data +} EFI_IMAGE_DEBUG_DIRECTORY_ENTRY; + +#define EFI_IMAGE_DEBUG_TYPE_CODEVIEW 2 // The Visual C++ debug information. + +// +// Debug Data Structure defined in Microsoft C++ +// +#define CODEVIEW_SIGNATURE_NB10 0x3031424E // NB10 +typedef struct { + UINT32 Signature; + UINT32 Unknown; + UINT32 Unknown2; + UINT32 Unknown3; + // + // Filename of .PDB goes here + // +} EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY; + +// +// Debug Data Structure defined in Microsoft C++ +// +#define CODEVIEW_SIGNATURE_RSDS 0x53445352 // RSDS +typedef struct { + UINT32 Signature; + UINT32 Unknown; + UINT32 Unknown2; + UINT32 Unknown3; + UINT32 Unknown4; + UINT32 Unknown5; + // + // Filename of .PDB goes here + // +} EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY; + +// +// Debug Data Structure defined by Apple Mach-O to COFF utility. +// +#define CODEVIEW_SIGNATURE_MTOC 0x434F544D // MTOC +typedef struct { + UINT32 Signature; + UINT8 MachOUuid[16]; + // + // Filename of .DLL (Mach-O with debug info) goes here + // +} EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY; + +// +// Resource format +// +typedef struct { + UINT32 Characteristics; + UINT32 TimeDateStamp; + UINT16 MajorVersion; + UINT16 MinorVersion; + UINT16 NumberOfNamedEntries; + UINT16 NumberOfIdEntries; + // + // Array of EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY entries goes here + // +} EFI_IMAGE_RESOURCE_DIRECTORY; + +// +// Resource directory entry format +// +typedef struct { + union { + struct { + UINT32 NameOffset : 31; + UINT32 NameIsString : 1; + } s; + UINT32 Id; + } u1; + union { + UINT32 OffsetToData; + struct { + UINT32 OffsetToDirectory : 31; + UINT32 DataIsDirectory : 1; + } s; + } u2; +} EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY; + +// +// Resource directory entry for string +// +typedef struct { + UINT16 Length; + CHAR16 String[1]; +} EFI_IMAGE_RESOURCE_DIRECTORY_STRING; + +// +// Resource directory entry for data array +// +typedef struct { + UINT32 OffsetToData; + UINT32 Size; + UINT32 CodePage; + UINT32 Reserved; +} EFI_IMAGE_RESOURCE_DATA_ENTRY; + +// +// Header format for TE images, defined in the PI Specification 1.0. +// +typedef struct { + UINT16 Signature; // The signature for TE format = "VZ" + UINT16 Machine; // From original file header + UINT8 NumberOfSections; // From original file header + UINT8 Subsystem; // From original optional header + UINT16 StrippedSize; // Number of bytes we removed from header + UINT32 AddressOfEntryPoint; // Offset to entry point -- from original optional header + UINT32 BaseOfCode; // From original image -- required for ITP debug + UINT64 ImageBase; // From original file header + EFI_IMAGE_DATA_DIRECTORY DataDirectory[2]; // Only base relocation and debug directory +} EFI_IMAGE_TE_HEADER; + +#define EFI_IMAGE_TE_SIGNATURE 0x5A56 // VZ + +// +// Data directory indexes in our TE image header +// +#define EFI_IMAGE_TE_DIRECTORY_ENTRY_BASERELOC 0 +#define EFI_IMAGE_TE_DIRECTORY_ENTRY_DEBUG 1 + +// Restore previous packing rules +#pragma pack(pop) +#endif // PEIMAGE_H diff --git a/common/treeitem.cpp b/common/treeitem.cpp index 0997c85..a0fefe0 100644 --- a/common/treeitem.cpp +++ b/common/treeitem.cpp @@ -11,14 +11,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. */ -#include #include "treeitem.h" #include "types.h" TreeItem::TreeItem(const UINT8 type, const UINT8 subtype, - const QString & name, const QString & text, const QString & info, - const QByteArray & header, const QByteArray & body, const QByteArray & tail, - const BOOLEAN fixed, const BOOLEAN compressed, const QByteArray & parsingData, + const UString & name, const UString & text, const UString & info, + const UByteArray & header, const UByteArray & body, const UByteArray & tail, + const BOOLEAN fixed, const BOOLEAN compressed, const UByteArray & parsingData, TreeItem *parent) : itemAction(Actions::NoAction), itemType(type), @@ -41,18 +40,18 @@ UINT8 TreeItem::insertChildBefore(TreeItem *item, TreeItem *newItem) { int index = childItems.indexOf(item); if (index == -1) - return ERR_ITEM_NOT_FOUND; + return U_ITEM_NOT_FOUND; childItems.insert(index, newItem); - return ERR_SUCCESS; + return U_SUCCESS; } UINT8 TreeItem::insertChildAfter(TreeItem *item, TreeItem *newItem) { int index = childItems.indexOf(item); if (index == -1) - return ERR_ITEM_NOT_FOUND; + return U_ITEM_NOT_FOUND; childItems.insert(index + 1, newItem); - return ERR_SUCCESS; + return U_SUCCESS; } QVariant TreeItem::data(int column) const @@ -62,11 +61,11 @@ QVariant TreeItem::data(int column) const case 0: // Name return itemName; case 1: // Action - return actionTypeToQString(itemAction); + return actionTypeToUString(itemAction); case 2: // Type - return itemTypeToQString(itemType); + return itemTypeToUString(itemType); case 3: // Subtype - return itemSubtypeToQString(itemType, itemSubtype); + return itemSubtypeToUString(itemType, itemSubtype); case 4: // Text return itemText; default: diff --git a/common/treeitem.h b/common/treeitem.h index 9fc4c66..265d507 100644 --- a/common/treeitem.h +++ b/common/treeitem.h @@ -14,19 +14,19 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef TREEITEM_H #define TREEITEM_H -#include #include -#include #include +#include "ubytearray.h" +#include "ustring.h" #include "basetypes.h" class TreeItem { public: - TreeItem(const UINT8 type, const UINT8 subtype, const QString &name, const QString &text, const QString &info, - const QByteArray & header, const QByteArray & body, const QByteArray & tail, - const BOOLEAN fixed, const BOOLEAN compressed, const QByteArray & parsingData, + TreeItem(const UINT8 type, const UINT8 subtype, const UString &name, const UString &text, const UString &info, + const UByteArray & header, const UByteArray & body, const UByteArray & tail, + const BOOLEAN fixed, const BOOLEAN compressed, const UByteArray & parsingData, TreeItem *parent = 0); ~TreeItem() { qDeleteAll(childItems); } @@ -45,8 +45,8 @@ public: TreeItem *parent() { return parentItem; } // Reading operations for item parameters - QString name() const { return itemName; } - void setName(const QString &text) { itemName = text; } + UString name() const { return itemName; } + void setName(const UString &text) { itemName = text; } UINT8 type() const { return itemType; } void setType(const UINT8 type) { itemType = type; } @@ -54,25 +54,25 @@ public: UINT8 subtype() const { return itemSubtype; } void setSubtype(const UINT8 subtype) { itemSubtype = subtype; } - QString text() const { return itemText; } - void setText(const QString &text) { itemText = text; } + UString text() const { return itemText; } + void setText(const UString &text) { itemText = text; } - QByteArray header() const { return itemHeader; } + UByteArray header() const { return itemHeader; } bool hasEmptyHeader() const { return itemHeader.isEmpty(); } - QByteArray body() const { return itemBody; }; + UByteArray body() const { return itemBody; }; bool hasEmptyBody() const { return itemBody.isEmpty(); } - QByteArray tail() const { return itemTail; }; + UByteArray tail() const { return itemTail; }; bool hasEmptyTail() const { return itemTail.isEmpty(); } - QByteArray parsingData() const { return itemParsingData; } + UByteArray parsingData() const { return itemParsingData; } bool hasEmptyParsingData() const { return itemParsingData.isEmpty(); } - void setParsingData(const QByteArray & data) { itemParsingData = data; } + void setParsingData(const UByteArray & data) { itemParsingData = data; } - QString info() const { return itemInfo; } - void addInfo(const QString &info, const BOOLEAN append) { if (append) itemInfo.append(info); else itemInfo.prepend(info); } - void setInfo(const QString &info) { itemInfo = info; } + UString info() const { return itemInfo; } + void addInfo(const UString &info, const BOOLEAN append) { if (append) itemInfo.append(info); else itemInfo.prepend(info); } + void setInfo(const UString &info) { itemInfo = info; } UINT8 action() const {return itemAction; } void setAction(const UINT8 action) { itemAction = action; } @@ -88,13 +88,13 @@ private: UINT8 itemAction; UINT8 itemType; UINT8 itemSubtype; - QString itemName; - QString itemText; - QString itemInfo; - QByteArray itemHeader; - QByteArray itemBody; - QByteArray itemTail; - QByteArray itemParsingData; + UString itemName; + UString itemText; + UString itemInfo; + UByteArray itemHeader; + UByteArray itemBody; + UByteArray itemTail; + UByteArray itemParsingData; bool itemFixed; bool itemCompressed; TreeItem* parentItem; diff --git a/common/treemodel.cpp b/common/treemodel.cpp index 8046c20..f4222f7 100644 --- a/common/treemodel.cpp +++ b/common/treemodel.cpp @@ -17,7 +17,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. TreeModel::TreeModel(QObject *parent) : QAbstractItemModel(parent) { - rootItem = new TreeItem(Types::Root, 0, QString(), QString(), QString(), QByteArray(), QByteArray(), QByteArray(), TRUE, FALSE, QByteArray()); + rootItem = new TreeItem(Types::Root, 0, UString(), UString(), UString(), UByteArray(), UByteArray(), UByteArray(), TRUE, FALSE, UByteArray()); } TreeModel::~TreeModel() @@ -25,7 +25,7 @@ TreeModel::~TreeModel() delete rootItem; } -int TreeModel::columnCount(const QModelIndex &parent) const +int TreeModel::columnCount(const UModelIndex &parent) const { if (parent.isValid()) return static_cast(parent.internalPointer())->columnCount(); @@ -33,7 +33,7 @@ int TreeModel::columnCount(const QModelIndex &parent) const return rootItem->columnCount(); } -QVariant TreeModel::data(const QModelIndex &index, int role) const +QVariant TreeModel::data(const UModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); @@ -49,7 +49,7 @@ QVariant TreeModel::data(const QModelIndex &index, int role) const return item->info(); } -Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const +Qt::ItemFlags TreeModel::flags(const UModelIndex &index) const { if (!index.isValid()) return 0; @@ -79,10 +79,10 @@ QVariant TreeModel::headerData(int section, Qt::Orientation orientation, return QVariant(); } -QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const +UModelIndex TreeModel::index(int row, int column, const UModelIndex &parent) const { if (!hasIndex(row, column, parent)) - return QModelIndex(); + return UModelIndex(); TreeItem *parentItem; @@ -95,27 +95,27 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) con if (childItem) return createIndex(row, column, childItem); else - return QModelIndex(); + return UModelIndex(); } -QModelIndex TreeModel::parent(const QModelIndex &index) const +UModelIndex TreeModel::parent(const UModelIndex &index) const { if (!index.isValid()) - return QModelIndex(); + return UModelIndex(); TreeItem *childItem = static_cast(index.internalPointer()); if (childItem == rootItem) - return QModelIndex(); + return UModelIndex(); TreeItem *parentItem = childItem->parent(); if (parentItem == rootItem) - return QModelIndex(); + return UModelIndex(); return createIndex(parentItem->row(), 0, parentItem); } -int TreeModel::rowCount(const QModelIndex &parent) const +int TreeModel::rowCount(const UModelIndex &parent) const { TreeItem *parentItem; if (parent.column() > 0) @@ -129,7 +129,7 @@ int TreeModel::rowCount(const QModelIndex &parent) const return parentItem->childCount(); } -UINT8 TreeModel::type(const QModelIndex &index) const +UINT8 TreeModel::type(const UModelIndex &index) const { if (!index.isValid()) return 0; @@ -137,7 +137,7 @@ UINT8 TreeModel::type(const QModelIndex &index) const return item->type(); } -UINT8 TreeModel::subtype(const QModelIndex &index) const +UINT8 TreeModel::subtype(const UModelIndex &index) const { if (!index.isValid()) return 0; @@ -145,15 +145,15 @@ UINT8 TreeModel::subtype(const QModelIndex &index) const return item->subtype(); } -QByteArray TreeModel::header(const QModelIndex &index) const +UByteArray TreeModel::header(const UModelIndex &index) const { if (!index.isValid()) - return QByteArray(); + return UByteArray(); TreeItem *item = static_cast(index.internalPointer()); return item->header(); } -bool TreeModel::hasEmptyHeader(const QModelIndex &index) const +bool TreeModel::hasEmptyHeader(const UModelIndex &index) const { if (!index.isValid()) return true; @@ -161,15 +161,15 @@ bool TreeModel::hasEmptyHeader(const QModelIndex &index) const return item->hasEmptyHeader(); } -QByteArray TreeModel::body(const QModelIndex &index) const +UByteArray TreeModel::body(const UModelIndex &index) const { if (!index.isValid()) - return QByteArray(); + return UByteArray(); TreeItem *item = static_cast(index.internalPointer()); return item->body(); } -bool TreeModel::hasEmptyBody(const QModelIndex &index) const +bool TreeModel::hasEmptyBody(const UModelIndex &index) const { if (!index.isValid()) return true; @@ -177,15 +177,15 @@ bool TreeModel::hasEmptyBody(const QModelIndex &index) const return item->hasEmptyBody(); } -QByteArray TreeModel::tail(const QModelIndex &index) const +UByteArray TreeModel::tail(const UModelIndex &index) const { if (!index.isValid()) - return QByteArray(); + return UByteArray(); TreeItem *item = static_cast(index.internalPointer()); return item->tail(); } -bool TreeModel::hasEmptyTail(const QModelIndex &index) const +bool TreeModel::hasEmptyTail(const UModelIndex &index) const { if (!index.isValid()) return true; @@ -193,15 +193,15 @@ bool TreeModel::hasEmptyTail(const QModelIndex &index) const return item->hasEmptyTail(); } -QByteArray TreeModel::parsingData(const QModelIndex &index) const +UByteArray TreeModel::parsingData(const UModelIndex &index) const { if (!index.isValid()) - return QByteArray(); + return UByteArray(); TreeItem *item = static_cast(index.internalPointer()); return item->parsingData(); } -bool TreeModel::hasEmptyParsingData(const QModelIndex &index) const +bool TreeModel::hasEmptyParsingData(const UModelIndex &index) const { if (!index.isValid()) return true; @@ -209,31 +209,31 @@ bool TreeModel::hasEmptyParsingData(const QModelIndex &index) const return item->hasEmptyParsingData(); } -QString TreeModel::name(const QModelIndex &index) const +UString TreeModel::name(const UModelIndex &index) const { if (!index.isValid()) - return QString(); + return UString(); TreeItem *item = static_cast(index.internalPointer()); return item->name(); } -QString TreeModel::text(const QModelIndex &index) const +UString TreeModel::text(const UModelIndex &index) const { if (!index.isValid()) - return QString(); + return UString(); TreeItem *item = static_cast(index.internalPointer()); return item->text(); } -QString TreeModel::info(const QModelIndex &index) const +UString TreeModel::info(const UModelIndex &index) const { if (!index.isValid()) - return QString(); + return UString(); TreeItem *item = static_cast(index.internalPointer()); return item->info(); } -UINT8 TreeModel::action(const QModelIndex &index) const +UINT8 TreeModel::action(const UModelIndex &index) const { if (!index.isValid()) return Actions::NoAction; @@ -241,7 +241,7 @@ UINT8 TreeModel::action(const QModelIndex &index) const return item->action(); } -bool TreeModel::fixed(const QModelIndex &index) const +bool TreeModel::fixed(const UModelIndex &index) const { if (!index.isValid()) return false; @@ -249,7 +249,7 @@ bool TreeModel::fixed(const QModelIndex &index) const return item->fixed(); } -bool TreeModel::compressed(const QModelIndex &index) const +bool TreeModel::compressed(const UModelIndex &index) const { if (!index.isValid()) return false; @@ -257,7 +257,7 @@ bool TreeModel::compressed(const QModelIndex &index) const return item->compressed(); } -void TreeModel::setFixed(const QModelIndex &index, const bool fixed) +void TreeModel::setFixed(const UModelIndex &index, const bool fixed) { if (!index.isValid()) return; @@ -281,7 +281,7 @@ void TreeModel::setFixed(const QModelIndex &index, const bool fixed) emit dataChanged(index, index); } -void TreeModel::setCompressed(const QModelIndex &index, const bool compressed) +void TreeModel::setCompressed(const UModelIndex &index, const bool compressed) { if (!index.isValid()) return; @@ -293,7 +293,7 @@ void TreeModel::setCompressed(const QModelIndex &index, const bool compressed) } -void TreeModel::setSubtype(const QModelIndex & index, const UINT8 subtype) +void TreeModel::setSubtype(const UModelIndex & index, const UINT8 subtype) { if (!index.isValid()) return; @@ -303,7 +303,7 @@ void TreeModel::setSubtype(const QModelIndex & index, const UINT8 subtype) emit dataChanged(index, index); } -void TreeModel::setName(const QModelIndex &index, const QString &data) +void TreeModel::setName(const UModelIndex &index, const UString &data) { if (!index.isValid()) return; @@ -313,7 +313,7 @@ void TreeModel::setName(const QModelIndex &index, const QString &data) emit dataChanged(index, index); } -void TreeModel::setType(const QModelIndex &index, const UINT8 data) +void TreeModel::setType(const UModelIndex &index, const UINT8 data) { if (!index.isValid()) return; @@ -323,7 +323,7 @@ void TreeModel::setType(const QModelIndex &index, const UINT8 data) emit dataChanged(index, index); } -void TreeModel::setText(const QModelIndex &index, const QString &data) +void TreeModel::setText(const UModelIndex &index, const UString &data) { if (!index.isValid()) return; @@ -333,7 +333,7 @@ void TreeModel::setText(const QModelIndex &index, const QString &data) emit dataChanged(index, index); } -void TreeModel::setInfo(const QModelIndex &index, const QString &data) +void TreeModel::setInfo(const UModelIndex &index, const UString &data) { if (!index.isValid()) return; @@ -343,7 +343,7 @@ void TreeModel::setInfo(const QModelIndex &index, const QString &data) emit dataChanged(index, index); } -void TreeModel::addInfo(const QModelIndex &index, const QString &data, const bool append) +void TreeModel::addInfo(const UModelIndex &index, const UString &data, const bool append) { if (!index.isValid()) return; @@ -353,7 +353,7 @@ void TreeModel::addInfo(const QModelIndex &index, const QString &data, const boo emit dataChanged(index, index); } -void TreeModel::setAction(const QModelIndex &index, const UINT8 action) +void TreeModel::setAction(const UModelIndex &index, const UINT8 action) { if (!index.isValid()) return; @@ -363,7 +363,7 @@ void TreeModel::setAction(const QModelIndex &index, const UINT8 action) emit dataChanged(index, index); } -void TreeModel::setParsingData(const QModelIndex &index, const QByteArray &data) +void TreeModel::setParsingData(const UModelIndex &index, const UByteArray &data) { if (!index.isValid()) return; @@ -373,11 +373,11 @@ void TreeModel::setParsingData(const QModelIndex &index, const QByteArray &data) emit dataChanged(this->index(0, 0), index); } -QModelIndex TreeModel::addItem(const UINT8 type, const UINT8 subtype, - const QString & name, const QString & text, const QString & info, - const QByteArray & header, const QByteArray & body, const QByteArray & tail, - const bool fixed, const QByteArray & parsingData, - const QModelIndex & parent, const UINT8 mode) +UModelIndex TreeModel::addItem(const UINT8 type, const UINT8 subtype, + const UString & name, const UString & text, const UString & info, + const UByteArray & header, const UByteArray & body, const UByteArray & tail, + const bool fixed, const UByteArray & parsingData, + const UModelIndex & parent, const UINT8 mode) { TreeItem *item = 0; TreeItem *parentItem = 0; @@ -418,23 +418,23 @@ QModelIndex TreeModel::addItem(const UINT8 type, const UINT8 subtype, } else { delete newItem; - return QModelIndex(); + return UModelIndex(); } emit layoutChanged(); - QModelIndex created = createIndex(newItem->row(), parentColumn, newItem); + UModelIndex created = createIndex(newItem->row(), parentColumn, newItem); setFixed(created, fixed); // Non-trivial logic requires additional call return created; } -QModelIndex TreeModel::findParentOfType(const QModelIndex& index, UINT8 type) const +UModelIndex TreeModel::findParentOfType(const UModelIndex& index, UINT8 type) const { if (!index.isValid()) - return QModelIndex(); + return UModelIndex(); TreeItem *item; - QModelIndex parent = index; + UModelIndex parent = index; for (item = static_cast(parent.internalPointer()); item != NULL && item != rootItem && item->type() != type; @@ -443,5 +443,5 @@ QModelIndex TreeModel::findParentOfType(const QModelIndex& index, UINT8 type) co if (item != NULL && item != rootItem) return parent; - return QModelIndex(); + return UModelIndex(); } \ No newline at end of file diff --git a/common/treemodel.h b/common/treemodel.h index 8448177..58ed9d2 100644 --- a/common/treemodel.h +++ b/common/treemodel.h @@ -15,12 +15,14 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #define TREEMODEL_H #include -#include -#include #include +#include +#include "ustring.h" +#include "ubytearray.h" #include "basetypes.h" #include "types.h" +#include "umodelindex.h" class TreeItem; @@ -32,52 +34,52 @@ public: TreeModel(QObject *parent = 0); ~TreeModel(); - QVariant data(const QModelIndex &index, int role) const; - Qt::ItemFlags flags(const QModelIndex &index) const; + QVariant data(const UModelIndex &index, int role) const; + Qt::ItemFlags flags(const UModelIndex &index) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; - QModelIndex index(int row, int column, - const QModelIndex &parent = QModelIndex()) const; - QModelIndex parent(const QModelIndex &index) const; - int rowCount(const QModelIndex &parent = QModelIndex()) const; - int columnCount(const QModelIndex &parent = QModelIndex()) const; + UModelIndex index(int row, int column, + const UModelIndex &parent = UModelIndex()) const; + UModelIndex parent(const UModelIndex &index) const; + int rowCount(const UModelIndex &parent = UModelIndex()) const; + int columnCount(const UModelIndex &parent = UModelIndex()) const; - void setAction(const QModelIndex &index, const UINT8 action); - void setType(const QModelIndex &index, const UINT8 type); - void setSubtype(const QModelIndex &index, const UINT8 subtype); - void setName(const QModelIndex &index, const QString &name); - void setText(const QModelIndex &index, const QString &text); - void setInfo(const QModelIndex &index, const QString &info); - void addInfo(const QModelIndex &index, const QString &info, const bool append = TRUE); - void setParsingData(const QModelIndex &index, const QByteArray &data); - void setFixed(const QModelIndex &index, const bool fixed); - void setCompressed(const QModelIndex &index, const bool compressed); + void setAction(const UModelIndex &index, const UINT8 action); + void setType(const UModelIndex &index, const UINT8 type); + void setSubtype(const UModelIndex &index, const UINT8 subtype); + void setName(const UModelIndex &index, const UString &name); + void setText(const UModelIndex &index, const UString &text); + void setInfo(const UModelIndex &index, const UString &info); + void addInfo(const UModelIndex &index, const UString &info, const bool append = TRUE); + void setParsingData(const UModelIndex &index, const UByteArray &data); + void setFixed(const UModelIndex &index, const bool fixed); + void setCompressed(const UModelIndex &index, const bool compressed); - QString name(const QModelIndex &index) const; - QString text(const QModelIndex &index) const; - QString info(const QModelIndex &index) const; - UINT8 type(const QModelIndex &index) const; - UINT8 subtype(const QModelIndex &index) const; - QByteArray header(const QModelIndex &index) const; - bool hasEmptyHeader(const QModelIndex &index) const; - QByteArray body(const QModelIndex &index) const; - bool hasEmptyBody(const QModelIndex &index) const; - QByteArray tail(const QModelIndex &index) const; - bool hasEmptyTail(const QModelIndex &index) const; - QByteArray parsingData(const QModelIndex &index) const; - bool hasEmptyParsingData(const QModelIndex &index) const; - UINT8 action(const QModelIndex &index) const; - bool fixed(const QModelIndex &index) const; + UString name(const UModelIndex &index) const; + UString text(const UModelIndex &index) const; + UString info(const UModelIndex &index) const; + UINT8 type(const UModelIndex &index) const; + UINT8 subtype(const UModelIndex &index) const; + UByteArray header(const UModelIndex &index) const; + bool hasEmptyHeader(const UModelIndex &index) const; + UByteArray body(const UModelIndex &index) const; + bool hasEmptyBody(const UModelIndex &index) const; + UByteArray tail(const UModelIndex &index) const; + bool hasEmptyTail(const UModelIndex &index) const; + UByteArray parsingData(const UModelIndex &index) const; + bool hasEmptyParsingData(const UModelIndex &index) const; + UINT8 action(const UModelIndex &index) const; + bool fixed(const UModelIndex &index) const; - bool compressed(const QModelIndex &index) const; + bool compressed(const UModelIndex &index) const; - QModelIndex addItem(const UINT8 type, const UINT8 subtype, - const QString & name, const QString & text, const QString & info, - const QByteArray & header, const QByteArray & body, const QByteArray & tail, - const bool fixed, const QByteArray & parsingData = QByteArray(), - const QModelIndex & parent = QModelIndex(), const UINT8 mode = CREATE_MODE_APPEND); + UModelIndex addItem(const UINT8 type, const UINT8 subtype, + const UString & name, const UString & text, const UString & info, + const UByteArray & header, const UByteArray & body, const UByteArray & tail, + const bool fixed, const UByteArray & parsingData = UByteArray(), + const UModelIndex & parent = UModelIndex(), const UINT8 mode = CREATE_MODE_APPEND); - QModelIndex findParentOfType(const QModelIndex & index, UINT8 type) const; + UModelIndex findParentOfType(const UModelIndex & index, UINT8 type) const; private: TreeItem *rootItem; diff --git a/common/types.cpp b/common/types.cpp index 613e787..ac90a58 100644 --- a/common/types.cpp +++ b/common/types.cpp @@ -10,62 +10,60 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. */ -#include -#include +#include "ustring.h" #include "types.h" #include "ffs.h" -QString regionTypeToQString(const UINT8 type) -{ - switch (type) - { - case Subtypes::DescriptorRegion: return QObject::tr("Descriptor"); - case Subtypes::BiosRegion: return QObject::tr("BIOS"); - case Subtypes::MeRegion: return QObject::tr("ME"); - case Subtypes::GbeRegion: return QObject::tr("GbE"); - case Subtypes::PdrRegion: return QObject::tr("PDR"); - case Subtypes::Reserved1Region: return QObject::tr("Reserved1"); - case Subtypes::Reserved2Region: return QObject::tr("Reserved2"); - case Subtypes::Reserved3Region: return QObject::tr("Reserved3"); - case Subtypes::EcRegion: return QObject::tr("EC"); - case Subtypes::Reserved4Region: return QObject::tr("Reserved4"); - }; - - return QObject::tr("Unknown"); -} - -QString itemTypeToQString(const UINT8 type) +UString regionTypeToUString(const UINT8 type) { switch (type) { - case Types::Root: return QObject::tr("Root"); - case Types::Image: return QObject::tr("Image"); - case Types::Capsule: return QObject::tr("Capsule"); - case Types::Region: return QObject::tr("Region"); - case Types::Volume: return QObject::tr("Volume"); - case Types::Padding: return QObject::tr("Padding"); - case Types::File: return QObject::tr("File"); - case Types::Section: return QObject::tr("Section"); - case Types::FreeSpace: return QObject::tr("Free space"); - case Types::VssStore: return QObject::tr("VSS store"); - case Types::FtwStore: return QObject::tr("FTW store"); - case Types::FdcStore: return QObject::tr("FDC store"); - case Types::FsysStore: return QObject::tr("Fsys store"); - case Types::EvsaStore: return QObject::tr("EVSA store"); - case Types::CmdbStore: return QObject::tr("CMDB store"); - case Types::FlashMapStore: return QObject::tr("FlashMap store"); - case Types::NvarEntry: return QObject::tr("NVAR entry"); - case Types::VssEntry: return QObject::tr("VSS entry"); - case Types::FsysEntry: return QObject::tr("Fsys entry"); - case Types::EvsaEntry: return QObject::tr("EVSA entry"); - case Types::FlashMapEntry: return QObject::tr("FlashMap entry"); - case Types::Microcode: return QObject::tr("Microcode"); - case Types::SlicData: return QObject::tr("SLIC data"); - } + case Subtypes::DescriptorRegion: return UString("Descriptor"); + case Subtypes::BiosRegion: return UString("BIOS"); + case Subtypes::MeRegion: return UString("ME"); + case Subtypes::GbeRegion: return UString("GbE"); + case Subtypes::PdrRegion: return UString("PDR"); + case Subtypes::Reserved1Region: return UString("Reserved1"); + case Subtypes::Reserved2Region: return UString("Reserved2"); + case Subtypes::Reserved3Region: return UString("Reserved3"); + case Subtypes::EcRegion: return UString("EC"); + case Subtypes::Reserved4Region: return UString("Reserved4"); + }; - return QObject::tr("Unknown"); + return UString("Unknown"); } -QString itemSubtypeToQString(const UINT8 type, const UINT8 subtype) +UString itemTypeToUString(const UINT8 type) +{ + switch (type) { + case Types::Root: return UString("Root"); + case Types::Image: return UString("Image"); + case Types::Capsule: return UString("Capsule"); + case Types::Region: return UString("Region"); + case Types::Volume: return UString("Volume"); + case Types::Padding: return UString("Padding"); + case Types::File: return UString("File"); + case Types::Section: return UString("Section"); + case Types::FreeSpace: return UString("Free space"); + case Types::VssStore: return UString("VSS store"); + case Types::FtwStore: return UString("FTW store"); + case Types::FdcStore: return UString("FDC store"); + case Types::FsysStore: return UString("Fsys store"); + case Types::EvsaStore: return UString("EVSA store"); + case Types::CmdbStore: return UString("CMDB store"); + case Types::FlashMapStore: return UString("FlashMap store"); + case Types::NvarEntry: return UString("NVAR entry"); + case Types::VssEntry: return UString("VSS entry"); + case Types::FsysEntry: return UString("Fsys entry"); + case Types::EvsaEntry: return UString("EVSA entry"); + case Types::FlashMapEntry: return UString("FlashMap entry"); + case Types::Microcode: return UString("Microcode"); + case Types::SlicData: return UString("SLIC data"); + } + + return UString("Unknown"); +} + +UString itemSubtypeToUString(const UINT8 type, const UINT8 subtype) { switch (type) { case Types::Root: @@ -78,89 +76,89 @@ QString itemSubtypeToQString(const UINT8 type, const UINT8 subtype) case Types::FlashMapStore: case Types::CmdbStore: case Types::FsysEntry: - case Types::SlicData: return QString(); + case Types::SlicData: return UString(); case Types::Image: - if (subtype == Subtypes::IntelImage) return QObject::tr("Intel"); - if (subtype == Subtypes::UefiImage) return QObject::tr("UEFI"); + if (subtype == Subtypes::IntelImage) return UString("Intel"); + if (subtype == Subtypes::UefiImage) return UString("UEFI"); break; case Types::Padding: - if (subtype == Subtypes::ZeroPadding) return QObject::tr("Empty (0x00)"); - if (subtype == Subtypes::OnePadding) return QObject::tr("Empty (0xFF)"); - if (subtype == Subtypes::DataPadding) return QObject::tr("Non-empty"); + if (subtype == Subtypes::ZeroPadding) return UString("Empty (0x00)"); + if (subtype == Subtypes::OnePadding) return UString("Empty (0xFF)"); + if (subtype == Subtypes::DataPadding) return UString("Non-empty"); break; case Types::Volume: - if (subtype == Subtypes::UnknownVolume) return QObject::tr("Unknown"); - if (subtype == Subtypes::Ffs2Volume) return QObject::tr("FFSv2"); - if (subtype == Subtypes::Ffs3Volume) return QObject::tr("FFSv3"); - if (subtype == Subtypes::NvramVolume) return QObject::tr("NVRAM"); + if (subtype == Subtypes::UnknownVolume) return UString("Unknown"); + if (subtype == Subtypes::Ffs2Volume) return UString("FFSv2"); + if (subtype == Subtypes::Ffs3Volume) return UString("FFSv3"); + if (subtype == Subtypes::NvramVolume) return UString("NVRAM"); break; case Types::Capsule: - if (subtype == Subtypes::AptioSignedCapsule) return QObject::tr("Aptio signed"); - if (subtype == Subtypes::AptioUnsignedCapsule) return QObject::tr("Aptio unsigned"); - if (subtype == Subtypes::UefiCapsule) return QObject::tr("UEFI 2.0"); - if (subtype == Subtypes::ToshibaCapsule) return QObject::tr("Toshiba"); + if (subtype == Subtypes::AptioSignedCapsule) return UString("Aptio signed"); + if (subtype == Subtypes::AptioUnsignedCapsule) return UString("Aptio unsigned"); + if (subtype == Subtypes::UefiCapsule) return UString("UEFI 2.0"); + if (subtype == Subtypes::ToshibaCapsule) return UString("Toshiba"); break; - case Types::Region: return regionTypeToQString(subtype); - case Types::File: return fileTypeToQString(subtype); - case Types::Section: return sectionTypeToQString(subtype); + case Types::Region: return regionTypeToUString(subtype); + case Types::File: return fileTypeToUString(subtype); + case Types::Section: return sectionTypeToUString(subtype); case Types::NvarEntry: - if (subtype == Subtypes::InvalidNvarEntry) return QObject::tr("Invalid"); - if (subtype == Subtypes::InvalidLinkNvarEntry) return QObject::tr("Invalid link"); - if (subtype == Subtypes::LinkNvarEntry) return QObject::tr("Link"); - if (subtype == Subtypes::DataNvarEntry) return QObject::tr("Data"); - if (subtype == Subtypes::FullNvarEntry) return QObject::tr("Full"); + if (subtype == Subtypes::InvalidNvarEntry) return UString("Invalid"); + if (subtype == Subtypes::InvalidLinkNvarEntry) return UString("Invalid link"); + if (subtype == Subtypes::LinkNvarEntry) return UString("Link"); + if (subtype == Subtypes::DataNvarEntry) return UString("Data"); + if (subtype == Subtypes::FullNvarEntry) return UString("Full"); break; case Types::VssEntry: - if (subtype == Subtypes::InvalidVssEntry) return QObject::tr("Invalid"); - if (subtype == Subtypes::StandardVssEntry) return QObject::tr("Standard"); - if (subtype == Subtypes::AppleVssEntry) return QObject::tr("Apple"); - if (subtype == Subtypes::AuthVssEntry) return QObject::tr("Auth"); + if (subtype == Subtypes::InvalidVssEntry) return UString("Invalid"); + if (subtype == Subtypes::StandardVssEntry) return UString("Standard"); + if (subtype == Subtypes::AppleVssEntry) return UString("Apple"); + if (subtype == Subtypes::AuthVssEntry) return UString("Auth"); break; case Types::EvsaEntry: - if (subtype == Subtypes::InvalidEvsaEntry) return QObject::tr("Invalid"); - if (subtype == Subtypes::UnknownEvsaEntry) return QObject::tr("Unknown"); - if (subtype == Subtypes::GuidEvsaEntry) return QObject::tr("GUID"); - if (subtype == Subtypes::NameEvsaEntry) return QObject::tr("Name"); - if (subtype == Subtypes::DataEvsaEntry) return QObject::tr("Data"); + if (subtype == Subtypes::InvalidEvsaEntry) return UString("Invalid"); + if (subtype == Subtypes::UnknownEvsaEntry) return UString("Unknown"); + if (subtype == Subtypes::GuidEvsaEntry) return UString("GUID"); + if (subtype == Subtypes::NameEvsaEntry) return UString("Name"); + if (subtype == Subtypes::DataEvsaEntry) return UString("Data"); break; case Types::FlashMapEntry: - if (subtype == Subtypes::VolumeFlashMapEntry) return QObject::tr("Volume"); - if (subtype == Subtypes::DataFlashMapEntry) return QObject::tr("Data"); + if (subtype == Subtypes::VolumeFlashMapEntry) return UString("Volume"); + if (subtype == Subtypes::DataFlashMapEntry) return UString("Data"); break; case Types::Microcode: - if (subtype == Subtypes::IntelMicrocode) return QObject::tr("Intel"); - if (subtype == Subtypes::AmdMicrocode) return QObject::tr("AMD"); + if (subtype == Subtypes::IntelMicrocode) return UString("Intel"); + if (subtype == Subtypes::AmdMicrocode) return UString("AMD"); break; } - return QObject::tr("Unknown"); + return UString("Unknown"); } -QString compressionTypeToQString(const UINT8 algorithm) +UString compressionTypeToUString(const UINT8 algorithm) { switch (algorithm) { - case COMPRESSION_ALGORITHM_NONE: return QObject::tr("None"); - case COMPRESSION_ALGORITHM_EFI11: return QObject::tr("EFI 1.1"); - case COMPRESSION_ALGORITHM_TIANO: return QObject::tr("Tiano"); - case COMPRESSION_ALGORITHM_UNDECIDED: return QObject::tr("Undecided Tiano/EFI 1.1"); - case COMPRESSION_ALGORITHM_LZMA: return QObject::tr("LZMA"); - case COMPRESSION_ALGORITHM_IMLZMA: return QObject::tr("Intel modified LZMA"); + case COMPRESSION_ALGORITHM_NONE: return UString("None"); + case COMPRESSION_ALGORITHM_EFI11: return UString("EFI 1.1"); + case COMPRESSION_ALGORITHM_TIANO: return UString("Tiano"); + case COMPRESSION_ALGORITHM_UNDECIDED: return UString("Undecided Tiano/EFI 1.1"); + case COMPRESSION_ALGORITHM_LZMA: return UString("LZMA"); + case COMPRESSION_ALGORITHM_IMLZMA: return UString("Intel modified LZMA"); } - return QObject::tr("Unknown"); + return UString("Unknown"); } -QString actionTypeToQString(const UINT8 action) +UString actionTypeToUString(const UINT8 action) { switch (action) { - case Actions::NoAction: return QString(); - case Actions::Create: return QObject::tr("Create"); - case Actions::Insert: return QObject::tr("Insert"); - case Actions::Replace: return QObject::tr("Replace"); - case Actions::Remove: return QObject::tr("Remove"); - case Actions::Rebuild: return QObject::tr("Rebuild"); - case Actions::Rebase: return QObject::tr("Rebase"); + case Actions::NoAction: return UString(); + case Actions::Create: return UString("Create"); + case Actions::Insert: return UString("Insert"); + case Actions::Replace: return UString("Replace"); + case Actions::Remove: return UString("Remove"); + case Actions::Rebuild: return UString("Rebuild"); + case Actions::Rebase: return UString("Rebase"); } - return QObject::tr("Unknown"); + return UString("Unknown"); } \ No newline at end of file diff --git a/common/types.h b/common/types.h index e232da3..b114f3a 100644 --- a/common/types.h +++ b/common/types.h @@ -138,11 +138,11 @@ namespace Subtypes { }; }; -// *ToQString conversion routines -extern QString actionTypeToQString(const UINT8 action); -extern QString itemTypeToQString(const UINT8 type); -extern QString itemSubtypeToQString(const UINT8 type, const UINT8 subtype); -extern QString compressionTypeToQString(const UINT8 algorithm); -extern QString regionTypeToQString(const UINT8 type); +// *ToUString conversion routines +extern UString actionTypeToUString(const UINT8 action); +extern UString itemTypeToUString(const UINT8 type); +extern UString itemSubtypeToUString(const UINT8 type, const UINT8 subtype); +extern UString compressionTypeToUString(const UINT8 algorithm); +extern UString regionTypeToUString(const UINT8 type); #endif // TYPES_H diff --git a/common/ubytearray.h b/common/ubytearray.h new file mode 100644 index 0000000..ad81bb6 --- /dev/null +++ b/common/ubytearray.h @@ -0,0 +1,67 @@ +/* ubytearray.h + +Copyright (c) 2016, Nikolaj Schlej. All rights reserved. +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +*/ + +#ifndef UBYTEARRAY_H +#define UBYTEARRAY_H + +#ifdef QT_CORE_LIB +// Use Qt class, if Qt is available +#include +#define UByteArray QByteArray +#else +// Use own implementation +#include +#include +#include + +class UByteArray +{ +public: + UByteArray() : d() {} + UByteArray(const UByteArray & ba) : d(ba.d) {} + UByteArray(const std::basic_string & bs) : d(bs) {} + UByteArray(const std::vector & bc) : d(bc.data(), bc.size()) {} + UByteArray(const char* bytes, int32_t size) : d(bytes, size) {} + ~UByteArray() {} + + bool isEmpty() const { return d.length() == 0; } + + char* data() { return &(d.front()); /* Feels dirty, but works for all basic_string implementations I know, is fully OK in C++11 and later*/ } + const char* data() const { return d.c_str(); } + const char* constData() const { return d.c_str(); } + void clear() { d.clear(); } + + int32_t size() const { return d.size(); } + int32_t count(char ch) const { return std::count(d.cbegin(), d.cend(), ch); } + char at(uint32_t i) const { return d.at(i); } + char operator[](uint32_t i) const { return d[i]; } + char& operator[](uint32_t i) { return d[i]; } + + bool startsWith(const UByteArray & ba) const { return 0 == d.find(ba.d, 0); } + int indexOf(const UByteArray & ba, int from = 0) const { return d.find(ba.d, from); } + int lastIndexOf(const UByteArray & ba, int from = 0) const { return d.rfind(ba.d, from); } + + UByteArray left(int32_t len) const { return d.substr(0, len); } + UByteArray right(int32_t len) const { return d.substr(d.size() - 1 - len, len); }; + UByteArray mid(int32_t pos, int32_t len = -1) const { return d.substr(pos, len); }; + + UByteArray &operator=(const UByteArray & ba) { d = ba.d; return *this; } + bool operator== (const UByteArray & ba) const { return d == ba.d; } + bool operator!= (const UByteArray & ba) const { return d != ba.d; } + inline void swap(UByteArray &other) { std::swap(d, other.d); } + +private: + std::basic_string d; +}; + +#endif // QT_CORE_LIB +#endif // UBYTEARRAY_H \ No newline at end of file diff --git a/common/umodelindex.h b/common/umodelindex.h new file mode 100644 index 0000000..0e860a5 --- /dev/null +++ b/common/umodelindex.h @@ -0,0 +1,23 @@ +/* umodelindex.h + +Copyright (c) 2016, Nikolaj Schlej. All rights reserved. +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +*/ + +#ifndef UMODELINDEX_H +#define UMODELINDEX_H + +#ifdef QT_CORE_LIB +// Use Qt class, if Qt is available +#include +#define UModelIndex QModelIndex +#else +// Use own implementation +#endif // QT_CORE_LIB +#endif // UMODELINDEX_H diff --git a/common/ustring.cpp b/common/ustring.cpp new file mode 100644 index 0000000..ccc0284 --- /dev/null +++ b/common/ustring.cpp @@ -0,0 +1,25 @@ +/* ustring.cpp + +Copyright (c) 2016, Nikolaj Schlej. All rights reserved. +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +*/ + +#include "ustring.h" +#include + +UString usprintf(const char* fmt, ...) +{ + UString msg; + va_list vl; + va_start(vl, fmt); + msg = msg.vsprintf(fmt, vl); + va_end(vl); + return msg; +}; + diff --git a/common/ustring.h b/common/ustring.h new file mode 100644 index 0000000..635bf62 --- /dev/null +++ b/common/ustring.h @@ -0,0 +1,34 @@ +/* ustring.h + +Copyright (c) 2016, Nikolaj Schlej. All rights reserved. +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +*/ + +#ifndef USTRING_H +#define USTRING_H + +//TODO: modify properly + +#ifdef QT_CORE_LIB +// Use Qt class, if Qt is available +#include +#define UString QString +#else +// Use own implementation +#include "../bstrlib/bstrwrap.h" +class UString : public CBString { +}; + +#endif // QT_CORE_LIB + +UString usprintf(const char* fmt, ...); + + + +#endif // USTRING_H diff --git a/common/utility.cpp b/common/utility.cpp index 3b9e44e..4425f5c 100644 --- a/common/utility.cpp +++ b/common/utility.cpp @@ -10,7 +10,7 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. */ -#include + #include "treemodel.h" #include "utility.h" #include "ffs.h" @@ -20,7 +20,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "LZMA/LzmaDecompress.h" // Returns either new parsing data instance or obtains it from index -PARSING_DATA parsingDataFromQModelIndex(const QModelIndex & index) +PARSING_DATA parsingDataFromUModelIndex(const UModelIndex & index) { if (index.isValid()) { TreeModel* model = (TreeModel*)index.model(); @@ -39,64 +39,64 @@ PARSING_DATA parsingDataFromQModelIndex(const QModelIndex & index) } // Converts parsing data to byte array -QByteArray parsingDataToQByteArray(const PARSING_DATA & pdata) +UByteArray parsingDataToUByteArray(const PARSING_DATA & pdata) { - return QByteArray((const char*)&pdata, sizeof(PARSING_DATA)); + return UByteArray((const char*)&pdata, sizeof(PARSING_DATA)); } // Returns text representation of error code -QString errorCodeToQString(UINT8 errorCode) +UString errorCodeToUString(UINT8 errorCode) { switch (errorCode) { - case ERR_SUCCESS: return QObject::tr("Success"); - case ERR_NOT_IMPLEMENTED: return QObject::tr("Not implemented"); - case ERR_INVALID_PARAMETER: return QObject::tr("Function called with invalid parameter"); - case ERR_BUFFER_TOO_SMALL: return QObject::tr("Buffer too small"); - case ERR_OUT_OF_RESOURCES: return QObject::tr("Out of resources"); - case ERR_OUT_OF_MEMORY: return QObject::tr("Out of memory"); - case ERR_FILE_OPEN: return QObject::tr("File can't be opened"); - case ERR_FILE_READ: return QObject::tr("File can't be read"); - case ERR_FILE_WRITE: return QObject::tr("File can't be written"); - case ERR_ITEM_NOT_FOUND: return QObject::tr("Item not found"); - case ERR_UNKNOWN_ITEM_TYPE: return QObject::tr("Unknown item type"); - case ERR_INVALID_FLASH_DESCRIPTOR: return QObject::tr("Invalid flash descriptor"); - case ERR_INVALID_REGION: return QObject::tr("Invalid region"); - case ERR_EMPTY_REGION: return QObject::tr("Empty region"); - case ERR_BIOS_REGION_NOT_FOUND: return QObject::tr("BIOS region not found"); - case ERR_VOLUMES_NOT_FOUND: return QObject::tr("UEFI volumes not found"); - case ERR_INVALID_VOLUME: return QObject::tr("Invalid UEFI volume"); - case ERR_VOLUME_REVISION_NOT_SUPPORTED: return QObject::tr("Volume revision not supported"); - //case ERR_VOLUME_GROW_FAILED: return QObject::tr("Volume grow failed"); - case ERR_UNKNOWN_FFS: return QObject::tr("Unknown file system"); - case ERR_INVALID_FILE: return QObject::tr("Invalid file"); - case ERR_INVALID_SECTION: return QObject::tr("Invalid section"); - case ERR_UNKNOWN_SECTION: return QObject::tr("Unknown section"); - case ERR_STANDARD_COMPRESSION_FAILED: return QObject::tr("Standard compression failed"); - case ERR_CUSTOMIZED_COMPRESSION_FAILED: return QObject::tr("Customized compression failed"); - case ERR_STANDARD_DECOMPRESSION_FAILED: return QObject::tr("Standard decompression failed"); - case ERR_CUSTOMIZED_DECOMPRESSION_FAILED: return QObject::tr("Customized decompression failed"); - case ERR_UNKNOWN_COMPRESSION_TYPE: return QObject::tr("Unknown compression type"); - case ERR_UNKNOWN_EXTRACT_MODE: return QObject::tr("Unknown extract mode"); - case ERR_UNKNOWN_REPLACE_MODE: return QObject::tr("Unknown replace mode"); - //case ERR_UNKNOWN_INSERT_MODE: return QObject::tr("Unknown insert mode"); - case ERR_UNKNOWN_IMAGE_TYPE: return QObject::tr("Unknown executable image type"); - case ERR_UNKNOWN_PE_OPTIONAL_HEADER_TYPE: return QObject::tr("Unknown PE optional header type"); - case ERR_UNKNOWN_RELOCATION_TYPE: return QObject::tr("Unknown relocation type"); - //case ERR_GENERIC_CALL_NOT_SUPPORTED: return QObject::tr("Generic call not supported"); - //case ERR_VOLUME_BASE_NOT_FOUND: return QObject::tr("Volume base address not found"); - //case ERR_PEI_CORE_ENTRY_POINT_NOT_FOUND: return QObject::tr("PEI core entry point not found"); - case ERR_COMPLEX_BLOCK_MAP: return QObject::tr("Block map structure too complex for correct analysis"); - case ERR_DIR_ALREADY_EXIST: return QObject::tr("Directory already exists"); - case ERR_DIR_CREATE: return QObject::tr("Directory can't be created"); - //case ERR_UNKNOWN_PATCH_TYPE: return QObject::tr("Unknown patch type"); - //case ERR_PATCH_OFFSET_OUT_OF_BOUNDS: return QObject::tr("Patch offset out of bounds"); - //case ERR_INVALID_SYMBOL: return QObject::tr("Invalid symbol"); - //case ERR_NOTHING_TO_PATCH: return QObject::tr("Nothing to patch"); - case ERR_DEPEX_PARSE_FAILED: return QObject::tr("Dependency expression parsing failed"); - case ERR_TRUNCATED_IMAGE: return QObject::tr("Image is truncated"); - case ERR_INVALID_CAPSULE: return QObject::tr("Invalid capsule"); - case ERR_STORES_NOT_FOUND: return QObject::tr("Stores not found"); - default: return QObject::tr("Unknown error %1").arg(errorCode); + case U_SUCCESS: return UString("Success"); + case U_NOT_IMPLEMENTED: return UString("Not implemented"); + case U_INVALID_PARAMETER: return UString("Function called with invalid parameter"); + case U_BUFFER_TOO_SMALL: return UString("Buffer too small"); + case U_OUT_OF_RESOURCES: return UString("Out of resources"); + case U_OUT_OF_MEMORY: return UString("Out of memory"); + case U_FILE_OPEN: return UString("File can't be opened"); + case U_FILE_READ: return UString("File can't be read"); + case U_FILE_WRITE: return UString("File can't be written"); + case U_ITEM_NOT_FOUND: return UString("Item not found"); + case U_UNKNOWN_ITEM_TYPE: return UString("Unknown item type"); + case U_INVALID_FLASH_DESCRIPTOR: return UString("Invalid flash descriptor"); + case U_INVALID_REGION: return UString("Invalid region"); + case U_EMPTY_REGION: return UString("Empty region"); + case U_BIOS_REGION_NOT_FOUND: return UString("BIOS region not found"); + case U_VOLUMES_NOT_FOUND: return UString("UEFI volumes not found"); + case U_INVALID_VOLUME: return UString("Invalid UEFI volume"); + case U_VOLUME_REVISION_NOT_SUPPORTED: return UString("Volume revision not supported"); + //case U_VOLUME_GROW_FAILED: return UString("Volume grow failed"); + case U_UNKNOWN_FFS: return UString("Unknown file system"); + case U_INVALID_FILE: return UString("Invalid file"); + case U_INVALID_SECTION: return UString("Invalid section"); + case U_UNKNOWN_SECTION: return UString("Unknown section"); + case U_STANDARD_COMPRESSION_FAILED: return UString("Standard compression failed"); + case U_CUSTOMIZED_COMPRESSION_FAILED: return UString("Customized compression failed"); + case U_STANDARD_DECOMPRESSION_FAILED: return UString("Standard decompression failed"); + case U_CUSTOMIZED_DECOMPRESSION_FAILED: return UString("Customized decompression failed"); + case U_UNKNOWN_COMPRESSION_TYPE: return UString("Unknown compression type"); + case U_UNKNOWN_EXTRACT_MODE: return UString("Unknown extract mode"); + case U_UNKNOWN_REPLACE_MODE: return UString("Unknown replace mode"); + //case U_UNKNOWN_INSERT_MODE: return UString("Unknown insert mode"); + case U_UNKNOWN_IMAGE_TYPE: return UString("Unknown executable image type"); + case U_UNKNOWN_PE_OPTIONAL_HEADER_TYPE: return UString("Unknown PE optional header type"); + case U_UNKNOWN_RELOCATION_TYPE: return UString("Unknown relocation type"); + //case U_GENERIC_CALL_NOT_SUPPORTED: return UString("Generic call not supported"); + //case U_VOLUME_BASE_NOT_FOUND: return UString("Volume base address not found"); + //case U_PEI_CORE_ENTRY_POINT_NOT_FOUND: return UString("PEI core entry point not found"); + case U_COMPLEX_BLOCK_MAP: return UString("Block map structure too complex for correct analysis"); + case U_DIR_ALREADY_EXIST: return UString("Directory already exists"); + case U_DIR_CREATE: return UString("Directory can't be created"); + //case U_UNKNOWN_PATCH_TYPE: return UString("Unknown patch type"); + //case U_PATCH_OFFSET_OUT_OF_BOUNDS: return UString("Patch offset out of bounds"); + //case U_INVALID_SYMBOL: return UString("Invalid symbol"); + //case U_NOTHING_TO_PATCH: return UString("Nothing to patch"); + case U_DEPEX_PARSE_FAILED: return UString("Dependency expression parsing failed"); + case U_TRUNCATED_IMAGE: return UString("Image is truncated"); + case U_INVALID_CAPSULE: return UString("Invalid capsule"); + case U_STORES_NOT_FOUND: return UString("Stores not found"); + default: return usprintf("Unknown error %02X", errorCode); } } @@ -152,7 +152,7 @@ UINT32 crc32(UINT32 initial, const UINT8* buffer, const UINT32 length) } // Compression routines -STATUS decompress(const QByteArray & compressedData, UINT8 & algorithm, QByteArray & decompressedData, QByteArray & efiDecompressedData) +USTATUS decompress(const UByteArray & compressedData, UINT8 & algorithm, UByteArray & decompressedData, UByteArray & efiDecompressedData) { const UINT8* data; UINT32 dataSize; @@ -168,7 +168,7 @@ STATUS decompress(const QByteArray & compressedData, UINT8 & algorithm, QByteArr case EFI_NOT_COMPRESSED: decompressedData = compressedData; algorithm = COMPRESSION_ALGORITHM_NONE; - return ERR_SUCCESS; + return U_SUCCESS; case EFI_STANDARD_COMPRESSION: { // Set default algorithm to unknown algorithm = COMPRESSION_ALGORITHM_UNKNOWN; @@ -180,11 +180,11 @@ STATUS decompress(const QByteArray & compressedData, UINT8 & algorithm, QByteArr // Check header to be valid header = (const EFI_TIANO_HEADER*)data; if (header->CompSize + sizeof(EFI_TIANO_HEADER) != dataSize) - return ERR_STANDARD_DECOMPRESSION_FAILED; + return U_STANDARD_DECOMPRESSION_FAILED; // Get info function is the same for both algorithms - if (ERR_SUCCESS != EfiTianoGetInfo(data, dataSize, &decompressedSize, &scratchSize)) - return ERR_STANDARD_DECOMPRESSION_FAILED; + if (U_SUCCESS != EfiTianoGetInfo(data, dataSize, &decompressedSize, &scratchSize)) + return U_STANDARD_DECOMPRESSION_FAILED; // Allocate memory decompressed = (UINT8*)malloc(decompressedSize); @@ -194,31 +194,31 @@ STATUS decompress(const QByteArray & compressedData, UINT8 & algorithm, QByteArr if (decompressed) free(decompressed); if (efiDecompressed) free(efiDecompressed); if (scratch) free(scratch); - return ERR_STANDARD_DECOMPRESSION_FAILED; + return U_STANDARD_DECOMPRESSION_FAILED; } // Decompress section data using both algorithms - STATUS result = ERR_SUCCESS; + USTATUS result = U_SUCCESS; // Try Tiano - STATUS TianoResult = TianoDecompress(data, dataSize, decompressed, decompressedSize, scratch, scratchSize); + USTATUS TianoResult = TianoDecompress(data, dataSize, decompressed, decompressedSize, scratch, scratchSize); // Try EFI 1.1 - STATUS EfiResult = EfiDecompress(data, dataSize, efiDecompressed, decompressedSize, scratch, scratchSize); + USTATUS EfiResult = EfiDecompress(data, dataSize, efiDecompressed, decompressedSize, scratch, scratchSize); - if (EfiResult == ERR_SUCCESS && TianoResult == ERR_SUCCESS) { // Both decompressions are OK + if (EfiResult == U_SUCCESS && TianoResult == U_SUCCESS) { // Both decompressions are OK algorithm = COMPRESSION_ALGORITHM_UNDECIDED; - decompressedData = QByteArray((const char*)decompressed, decompressedSize); - efiDecompressedData = QByteArray((const char*)efiDecompressed, decompressedSize); + decompressedData = UByteArray((const char*)decompressed, decompressedSize); + efiDecompressedData = UByteArray((const char*)efiDecompressed, decompressedSize); } - else if (TianoResult == ERR_SUCCESS) { // Only Tiano is OK + else if (TianoResult == U_SUCCESS) { // Only Tiano is OK algorithm = COMPRESSION_ALGORITHM_TIANO; - decompressedData = QByteArray((const char*)decompressed, decompressedSize); + decompressedData = UByteArray((const char*)decompressed, decompressedSize); } - else if (EfiResult == ERR_SUCCESS) { // Only EFI 1.1 is OK + else if (EfiResult == U_SUCCESS) { // Only EFI 1.1 is OK algorithm = COMPRESSION_ALGORITHM_EFI11; - decompressedData = QByteArray((const char*)efiDecompressed, decompressedSize); + decompressedData = UByteArray((const char*)efiDecompressed, decompressedSize); } else { // Both decompressions failed - result = ERR_STANDARD_DECOMPRESSION_FAILED; + result = U_STANDARD_DECOMPRESSION_FAILED; } free(decompressed); @@ -235,47 +235,47 @@ STATUS decompress(const QByteArray & compressedData, UINT8 & algorithm, QByteArr dataSize = compressedData.size(); // Get info - if (ERR_SUCCESS != LzmaGetInfo(data, dataSize, &decompressedSize)) - return ERR_CUSTOMIZED_DECOMPRESSION_FAILED; + if (U_SUCCESS != LzmaGetInfo(data, dataSize, &decompressedSize)) + return U_CUSTOMIZED_DECOMPRESSION_FAILED; // Allocate memory decompressed = (UINT8*)malloc(decompressedSize); if (!decompressed) { - return ERR_STANDARD_DECOMPRESSION_FAILED; + return U_STANDARD_DECOMPRESSION_FAILED; } // Decompress section data - if (ERR_SUCCESS != LzmaDecompress(data, dataSize, decompressed)) { + if (U_SUCCESS != LzmaDecompress(data, dataSize, decompressed)) { // Intel modified LZMA workaround // Decompress section data once again data += sizeof(UINT32); // Get info again - if (ERR_SUCCESS != LzmaGetInfo(data, dataSize, &decompressedSize)) { + if (U_SUCCESS != LzmaGetInfo(data, dataSize, &decompressedSize)) { free(decompressed); - return ERR_CUSTOMIZED_DECOMPRESSION_FAILED; + return U_CUSTOMIZED_DECOMPRESSION_FAILED; } // Decompress section data again - if (ERR_SUCCESS != LzmaDecompress(data, dataSize, decompressed)) { + if (U_SUCCESS != LzmaDecompress(data, dataSize, decompressed)) { free(decompressed); - return ERR_CUSTOMIZED_DECOMPRESSION_FAILED; + return U_CUSTOMIZED_DECOMPRESSION_FAILED; } else { algorithm = COMPRESSION_ALGORITHM_IMLZMA; - decompressedData = QByteArray((const char*)decompressed, decompressedSize); + decompressedData = UByteArray((const char*)decompressed, decompressedSize); } } else { algorithm = COMPRESSION_ALGORITHM_LZMA; - decompressedData = QByteArray((const char*)decompressed, decompressedSize); + decompressedData = UByteArray((const char*)decompressed, decompressedSize); } free(decompressed); - return ERR_SUCCESS; + return U_SUCCESS; default: algorithm = COMPRESSION_ALGORITHM_UNKNOWN; - return ERR_UNKNOWN_COMPRESSION_TYPE; + return U_UNKNOWN_COMPRESSION_TYPE; } } diff --git a/common/utility.h b/common/utility.h index f594311..7378924 100644 --- a/common/utility.h +++ b/common/utility.h @@ -14,25 +14,25 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef UTILITY_H #define UTILITY_H -#include -#include +#include "ustring.h" +#include "umodelindex.h" #include "basetypes.h" #include "parsingdata.h" // Returns either new parsing data instance or obtains it from index -PARSING_DATA parsingDataFromQModelIndex(const QModelIndex & index); +PARSING_DATA parsingDataFromUModelIndex(const UModelIndex & index); // Converts parsing data to byte array -QByteArray parsingDataToQByteArray(const PARSING_DATA & pdata); +UByteArray parsingDataToUByteArray(const PARSING_DATA & pdata); -// Converts error code to QString -extern QString errorCodeToQString(UINT8 errorCode); +// Converts error code to UString +extern UString errorCodeToUString(UINT8 errorCode); // Decompression routine -extern STATUS decompress(const QByteArray & compressed, UINT8 & algorithm, QByteArray & decompressed, QByteArray & efiDecompressed); +extern USTATUS decompress(const UByteArray & compressed, UINT8 & algorithm, UByteArray & decompressed, UByteArray & efiDecompressed); // Compression routine -//STATUS compress(const QByteArray & decompressed, QByteArray & compressed, const UINT8 & algorithm); +//USTATUS compress(const UByteArray & decompressed, UByteArray & compressed, const UINT8 & algorithm); // CRC32 calculation routine extern UINT32 crc32(UINT32 initial, const UINT8* buffer, const UINT32 length); From d549840eedadf4dab77df00fb21d831aee0a82b6 Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Sun, 26 Jun 2016 06:03:03 +0200 Subject: [PATCH 02/12] Typo fix --- UEFIExtract/uefiextract.pro | 2 +- bstrlib | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 160000 bstrlib diff --git a/UEFIExtract/uefiextract.pro b/UEFIExtract/uefiextract.pro index 63297d9..3deb59b 100644 --- a/UEFIExtract/uefiextract.pro +++ b/UEFIExtract/uefiextract.pro @@ -49,5 +49,5 @@ HEADERS += \ ../common/ubytearray.h \ ../common/ustring.h \ ../bstrlib/bstrlib.h \ - ../bstrlib/bstrwrap.cpp + ../bstrlib/bstrwrap.h diff --git a/bstrlib b/bstrlib new file mode 160000 index 0000000..208b1f2 --- /dev/null +++ b/bstrlib @@ -0,0 +1 @@ +Subproject commit 208b1f2a4dfc96b806ed499bd1909e87ec15981d From a2484fdb5f09653410eb86f0156887aeb19dabbb Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Sun, 26 Jun 2016 10:05:45 +0200 Subject: [PATCH 03/12] CBString modified and integrated - CBString is used instead of QString, as PoC - removed submodule --- .gitmodules | 3 - UEFIExtract/ffsdumper.cpp | 13 ++-- UEFIExtract/uefiextract_main.cpp | 42 +++++----- common/ffsparser.cpp | 129 ++++++++++++++++--------------- common/ffsreport.cpp | 4 +- common/fitparser.cpp | 6 +- common/nvram.cpp | 12 ++- common/treeitem.cpp | 35 ++++++--- common/treeitem.h | 19 +++-- common/treemodel.cpp | 4 +- common/ustring.cpp | 73 ++++++++++++++++- common/ustring.h | 11 +-- 12 files changed, 217 insertions(+), 134 deletions(-) delete mode 100644 .gitmodules diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 2f51807..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "bstrlib"] - path = bstrlib - url = https://github.com/websnarf/bstrlib diff --git a/UEFIExtract/ffsdumper.cpp b/UEFIExtract/ffsdumper.cpp index 7e97448..ceaf719 100644 --- a/UEFIExtract/ffsdumper.cpp +++ b/UEFIExtract/ffsdumper.cpp @@ -43,16 +43,16 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path guidToUString(*(const EFI_GUID*)model->header(index).constData()) == guid || guidToUString(*(const EFI_GUID*)model->header(model->findParentOfType(index, Types::File)).constData()) == guid) { - if (dir.cd(path)) + if (dir.cd(QString(path))) return U_DIR_ALREADY_EXIST; - if (!dir.mkpath(path)) + if (!dir.mkpath(QString(path))) return U_DIR_CREATE; QFile file; if (dumpAll || model->rowCount(index) == 0) { // Dump if leaf item or dumpAll is true if (!model->header(index).isEmpty()) { - file.setFileName(path + UString("/header.bin")); + file.setFileName(QString(path) + UString("/header.bin")); if (!file.open(QFile::WriteOnly)) return U_FILE_OPEN; @@ -61,7 +61,7 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path } if (!model->body(index).isEmpty()) { - file.setFileName(path + UString("/body.bin")); + file.setFileName(QString(path) + UString("/body.bin")); if (!file.open(QFile::WriteOnly)) return U_FILE_OPEN; @@ -75,11 +75,11 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path + UString("Subtype: ") + itemSubtypeToUString(model->type(index), model->subtype(index)) + UString("\n") + (model->text(index).isEmpty() ? UString("") : UString("Text: ") + model->text(index) + UString("\n")) + model->info(index) + UString("\n"); - file.setFileName(path + UString("/info.txt")); + file.setFileName(QString(path) + UString("/info.txt")); if (!file.open(QFile::Text | QFile::WriteOnly)) return U_FILE_OPEN; - file.write(info.toLatin1()); + file.write(info); file.close(); dumped = true; } @@ -98,4 +98,5 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path } return U_SUCCESS; + } diff --git a/UEFIExtract/uefiextract_main.cpp b/UEFIExtract/uefiextract_main.cpp index 37a7978..3fa8d08 100644 --- a/UEFIExtract/uefiextract_main.cpp +++ b/UEFIExtract/uefiextract_main.cpp @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include +#include #include <../common/ustring.h> #include "../common/ffsparser.h" @@ -34,14 +35,15 @@ int main(int argc, char *argv[]) if (a.arguments().length() > 1) { // Check that input file exists - UString path = a.arguments().at(1); - QFileInfo fileInfo(path); + UString path(a.arguments().at(1).toLatin1()); + QString qpath = QString(path); + QFileInfo fileInfo(qpath); if (!fileInfo.exists()) return U_FILE_OPEN; // Open the input file QFile inputFile; - inputFile.setFileName(path); + inputFile.setFileName(qpath); if (!inputFile.open(QFile::ReadOnly)) return U_FILE_OPEN; @@ -60,7 +62,7 @@ int main(int argc, char *argv[]) // Show ffsParser's messages std::vector > messages = ffsParser.getMessages(); for (size_t i = 0; i < messages.size(); i++) { - std::cout << messages[i].first.toLatin1().constData() << std::endl; + std::cout << messages[i].first << std::endl; } // Get last VTF @@ -74,7 +76,7 @@ int main(int argc, char *argv[]) // Show fitParser's messages std::vector > fitMessages = fitParser.getMessages(); for (size_t i = 0; i < fitMessages.size(); i++) { - std::cout << fitMessages[i].first.toLatin1().constData() << std::endl; + std::cout << fitMessages[i].first << std::endl; } // Show FIT table @@ -84,11 +86,11 @@ int main(int argc, char *argv[]) std::cout << " Address | Size | Ver | Type | CS " << std::endl; std::cout << "-------------------------------------------------------------------" << std::endl; for (size_t i = 0; i < fitTable.size(); i++) { - std::cout << fitTable[i][0].toLatin1().constData() << " | " - << fitTable[i][1].toLatin1().constData() << " | " - << fitTable[i][2].toLatin1().constData() << " | " - << fitTable[i][3].toLatin1().constData() << " | " - << fitTable[i][4].toLatin1().constData() << std::endl; + std::cout << fitTable[i][0] << " | " + << fitTable[i][1] << " | " + << fitTable[i][2] << " | " + << fitTable[i][3] << " | " + << fitTable[i][4] << std::endl; } } } @@ -98,14 +100,12 @@ int main(int argc, char *argv[]) FfsReport ffsReport(&model); std::vector report = ffsReport.generate(); if (report.size()) { - QFile file; - file.setFileName(fileInfo.fileName().append(".report.txt")); - if (file.open(QFile::Text | QFile::WriteOnly)) { - for (size_t i = 0; i < report.size(); i++) { - file.write(report[i].toLatin1().append('\n')); - } - file.close(); + std::ofstream ofs; + ofs.open("report.txt", std::ofstream::out); + for (size_t i = 0; i < report.size(); i++) { + ofs << report[i] << std::endl; } + ofs.close(); } // Create ffsDumper @@ -113,10 +113,10 @@ int main(int argc, char *argv[]) // Dump all non-leaf elements if (a.arguments().length() == 2) { - return (ffsDumper.dump(model.index(0, 0), fileInfo.fileName().append(".dump")) != U_SUCCESS); + return (ffsDumper.dump(model.index(0, 0), fileInfo.fileName().append(".dump2").toLatin1().constData()) != U_SUCCESS); } else if (a.arguments().length() == 3 && a.arguments().at(2) == UString("all")) { // Dump everything - return (ffsDumper.dump(model.index(0, 0), fileInfo.fileName().append(".dump"), true) != U_SUCCESS); + return (ffsDumper.dump(model.index(0, 0), fileInfo.fileName().append(".dump2").toLatin1().constData(), true) != U_SUCCESS); } else if (a.arguments().length() == 3 && a.arguments().at(2) == UString("none")) { // Skip dumping return 0; @@ -124,7 +124,7 @@ int main(int argc, char *argv[]) else { // Dump specific files UINT32 returned = 0; for (int i = 2; i < a.arguments().length(); i++) { - result = ffsDumper.dump(model.index(0, 0), fileInfo.fileName().append(".dump"), true, a.arguments().at(i)); + result = ffsDumper.dump(model.index(0, 0), fileInfo.fileName().append(".dump2").toLatin1().constData(), true, a.arguments().at(i).toLatin1().constData()); if (result) returned |= (1 << (i - 1)); } @@ -142,4 +142,6 @@ int main(int argc, char *argv[]) << "Return value is a bit mask where 0 at position N means that file with GUID_N was found and unpacked, 1 otherwise" << std::endl; return 1; } + +return 0; } diff --git a/common/ffsparser.cpp b/common/ffsparser.cpp index f3880e7..0c2cf52 100644 --- a/common/ffsparser.cpp +++ b/common/ffsparser.cpp @@ -14,6 +14,7 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include +#include // Region info structure definition struct REGION_INFO { @@ -225,7 +226,7 @@ USTATUS FfsParser::parseIntelImage(const UByteArray & intelImage, const UINT32 p // Check for buffer size to be greater or equal to descriptor region size if (intelImage.size() < FLASH_DESCRIPTOR_SIZE) { - msg(usprintf("parseIntelImage: input file is smaller than minimum descriptor size of %X (%u) bytes", FLASH_DESCRIPTOR_SIZE, FLASH_DESCRIPTOR_SIZE)); + msg(usprintf("parseIntelImage: input file is smaller than minimum descriptor size of %Xh (%u) bytes", FLASH_DESCRIPTOR_SIZE, FLASH_DESCRIPTOR_SIZE)); return U_INVALID_FLASH_DESCRIPTOR; } @@ -486,8 +487,8 @@ USTATUS FfsParser::parseIntelImage(const UByteArray & intelImage, const UINT32 p // Add offsets of actual regions for (size_t i = 0; i < regions.size(); i++) { if (regions[i].type != Subtypes::ZeroPadding && regions[i].type != Subtypes::OnePadding && regions[i].type != Subtypes::DataPadding) - info += itemSubtypeToUString(Types::Region, regions[i].type).prepend("\n") - + usprintf(" region offset: %Xh", itemSubtypeToUString(Types::Region, regions[i].type), regions[i].offset + parentOffset); + info += UString("\n") + itemSubtypeToUString(Types::Region, regions[i].type) + + usprintf(" region offset: %Xh", regions[i].offset + parentOffset); } // Region access settings @@ -517,8 +518,8 @@ USTATUS FfsParser::parseIntelImage(const UByteArray & intelImage, const UINT32 p } else if (descriptorVersion == 2) { const FLASH_DESCRIPTOR_MASTER_SECTION_V2* masterSection = (const FLASH_DESCRIPTOR_MASTER_SECTION_V2*)calculateAddress8(descriptor, descriptorMap->MasterBase); - info += ("\nRegion access settings:"); - info += ("\nBIOS: %03Xh %03Xh ME: %03Xh %03Xh\nGbE: %03Xh %03Xh EC: %03Xh %03Xh", + info += UString("\nRegion access settings:"); + info += usprintf("\nBIOS: %03Xh %03Xh ME: %03Xh %03Xh\nGbE: %03Xh %03Xh EC: %03Xh %03Xh", masterSection->BiosRead, masterSection->BiosWrite, masterSection->MeRead, @@ -554,7 +555,7 @@ USTATUS FfsParser::parseIntelImage(const UByteArray & intelImage, const UINT32 p info += UString("\nFlash chips in VSCC table:"); UINT8 vsscTableSize = upperMap->VsccTableSize * sizeof(UINT32) / sizeof(VSCC_TABLE_ENTRY); for (int i = 0; i < vsscTableSize; i++) { - info += usprintf("\n02X%02X%02Xh", + info += usprintf("\n%02X%02X%02Xh", vsccTableEntry->VendorId, vsccTableEntry->DeviceId0, vsccTableEntry->DeviceId1); vsccTableEntry++; } @@ -598,7 +599,7 @@ USTATUS FfsParser::parseIntelImage(const UByteArray & intelImage, const UINT32 p // Get info name = UString("Padding"); - info = usprintf("Full size: %X (%u)", + info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Construct parsing data @@ -1104,15 +1105,15 @@ USTATUS FfsParser::parseVolumeHeader(const UByteArray & volume, const UINT32 par volumeHeader->ZeroVector[8], volumeHeader->ZeroVector[9], volumeHeader->ZeroVector[10], volumeHeader->ZeroVector[11], volumeHeader->ZeroVector[12], volumeHeader->ZeroVector[13], volumeHeader->ZeroVector[14], volumeHeader->ZeroVector[15]) + guidToUString(volumeHeader->FileSystemGuid) \ - + usprintf("\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nRevision: %u\nAttributes: %08Xh\nErase polarity: %u\nChecksum: %02Xh", + + usprintf("\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nRevision: %u\nAttributes: %08Xh\nErase polarity: %u\nChecksum: %04Xh", volumeSize, volumeSize, headerSize, headerSize, volumeSize - headerSize, volumeSize - headerSize, volumeHeader->Revision, - volumeHeader->Attributes, 8, - emptyByte ? 1 : 0, + volumeHeader->Attributes, + (emptyByte ? 1 : 0), volumeHeader->Checksum) + - (msgInvalidChecksum ? usprintf("invalid, should be %04Xh", calculated) : UString("valid")); + (msgInvalidChecksum ? usprintf(", invalid, should be %04Xh", calculated) : UString(", valid")); // Extended header present if (volumeHeader->Revision > 1 && volumeHeader->ExtHeaderOffset) { @@ -1177,19 +1178,19 @@ USTATUS FfsParser::findNextVolume(const UModelIndex & index, const UByteArray & for (; nextIndex > 0; nextIndex = bios.indexOf(EFI_FV_SIGNATURE, nextIndex + 1)) { const EFI_FIRMWARE_VOLUME_HEADER* volumeHeader = (const EFI_FIRMWARE_VOLUME_HEADER*)(bios.constData() + nextIndex - EFI_FV_SIGNATURE_OFFSET); if (volumeHeader->FvLength < sizeof(EFI_FIRMWARE_VOLUME_HEADER) + 2 * sizeof(EFI_FV_BLOCK_MAP_ENTRY) || volumeHeader->FvLength >= 0xFFFFFFFFUL) { - msg(usprintf("findNextVolume: volume candidate at offset %Xh skipped, has invalid FvLength %Xh", + msg(usprintf("findNextVolume: volume candidate at offset %Xh skipped, has invalid FvLength %"PRIX64"h", parentOffset + (nextIndex - EFI_FV_SIGNATURE_OFFSET), volumeHeader->FvLength), index); continue; } if (volumeHeader->Reserved != 0xFF && volumeHeader->Reserved != 0x00) { - msg(usprintf("findNextVolume: volume candidate at offset %Xh skipped, has invalid Reserved byte value %02X", + msg(usprintf("findNextVolume: volume candidate at offset %Xh skipped, has invalid Reserved byte value %02Xh", parentOffset + (nextIndex - EFI_FV_SIGNATURE_OFFSET), volumeHeader->Reserved), index); continue; } if (volumeHeader->Revision != 1 && volumeHeader->Revision != 2) { - msg(usprintf("findNextVolume: volume candidate at offset %Xh skipped, has invalid Revision byte value %02X", + msg(usprintf("findNextVolume: volume candidate at offset %Xh skipped, has invalid Revision byte value %02Xh", parentOffset + (nextIndex - EFI_FV_SIGNATURE_OFFSET) ,volumeHeader->Revision), index); continue; @@ -1576,17 +1577,17 @@ USTATUS FfsParser::parseFileHeader(const UByteArray & file, const UINT32 parentO else name = UString("Pad-file"); - info = guidToUString(fileHeader->Name).prepend("File GUID : ") + - usprintf("\nType: %02Xh\nAttributes: %02Xh\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nTail size: %Xh (%u)\nState: %02Xh\n", + info = UString("File GUID: ") + guidToUString(fileHeader->Name) + + usprintf("\nType: %02Xh\nAttributes: %02Xh\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nTail size: %Xh (%u)\nState: %02Xh", fileHeader->Type, fileHeader->Attributes, header.size() + body.size() + tail.size(), header.size() + body.size() + tail.size(), header.size(), header.size(), body.size(), body.size(), tail.size(), tail.size(), - fileHeader->State) - + (msgInvalidHeaderChecksum ? usprintf("%02Xh, invalid, should be %02Xh", fileHeader->IntegrityCheck.Checksum.Header, calculatedHeader) : usprintf("%02X, valid", calculatedHeader)) - + (msgInvalidDataChecksum ? usprintf("%02Xh, invalid, should be %02Xh", fileHeader->IntegrityCheck.Checksum.File, calculatedData) : usprintf("%02X, valid", calculatedData)); + fileHeader->State) + + usprintf("\nHeader checksum: %02Xh", fileHeader->IntegrityCheck.Checksum.Header) + (msgInvalidHeaderChecksum ? usprintf(", invalid, should be %02Xh", calculatedHeader) : UString(", valid")) + + usprintf("\nData checksum: %02Xh", fileHeader->IntegrityCheck.Checksum.File) + (msgInvalidDataChecksum ? usprintf(", invalid, should be %02Xh", calculatedData) : UString(", valid")); // Add file GUID to parsing data pdata.file.guid = fileHeader->Name; @@ -2024,7 +2025,7 @@ USTATUS FfsParser::parseGuidedSectionHeader(const UByteArray & section, const UI return U_INVALID_SECTION; // Check for special GUIDed sections - UByteArray additionalInfo; + UString additionalInfo; UByteArray baGuid((const char*)&guid, sizeof(EFI_GUID)); bool msgSignedSectionFound = false; bool msgNoAuthStatusAttribute = false; @@ -2049,7 +2050,7 @@ USTATUS FfsParser::parseGuidedSectionHeader(const UByteArray & section, const UI additionalInfo += usprintf("\nChecksum: %08Xh, valid", crc); } else { - additionalInfo += usprintf("\nChecksum: %02Xh, invalid, should be %02Xh", crc, calculated); + additionalInfo += usprintf("\nChecksum: %08Xh, invalid, should be %08Xh", crc, calculated); msgInvalidCrc = true; } // No need to change dataOffset here @@ -2118,7 +2119,7 @@ USTATUS FfsParser::parseGuidedSectionHeader(const UByteArray & section, const UI attributes); // Append additional info - info.append(additionalInfo); + info += additionalInfo; // Construct parsing data pdata.offset += parentOffset; @@ -2261,7 +2262,7 @@ USTATUS FfsParser::parseVersionSectionHeader(const UByteArray & section, const U // Get info UString name = sectionTypeToUString(type) + (" section"); - UString info = ("Type: %02Xh\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nBuild number: %u", + UString info = usprintf("Type: %02Xh\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nBuild number: %u", type, section.size(), section.size(), header.size(), header.size(), @@ -2323,8 +2324,8 @@ USTATUS FfsParser::parsePostcodeSectionHeader(const UByteArray & section, const // Get info UString name = sectionTypeToUString(type) + (" section"); - UString info = ("Type: %02Xh\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nPostcode: %Xh", - type, 2, + UString info = usprintf("Type: %02Xh\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nPostcode: %Xh", + type, section.size(), section.size(), header.size(), header.size(), body.size(), body.size(), @@ -2682,7 +2683,7 @@ USTATUS FfsParser::parseAprioriRawSection(const UByteArray & body, UString & par if (count > 0) { for (UINT32 i = 0; i < count; i++) { const EFI_GUID* guid = (const EFI_GUID*)body.constData() + i; - parsed += guidToUString(*guid).prepend("\n"); + parsed += UString("\n") + guidToUString(*guid); } } @@ -2752,7 +2753,7 @@ USTATUS FfsParser::parsePeImageSectionBody(const UModelIndex & index) return U_SUCCESS; } - UByteArray info; + UString info; const EFI_IMAGE_DOS_HEADER* dosHeader = (const EFI_IMAGE_DOS_HEADER*)body.constData(); if (dosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) { info += usprintf("\nDOS signature: %04Xh, invalid", dosHeader->e_magic); @@ -2787,7 +2788,7 @@ USTATUS FfsParser::parsePeImageSectionBody(const UModelIndex & index) info += usprintf("\nDOS signature: %04Xh\nPE signature: %08Xh", dosHeader->e_magic, peHeader->Signature) + - machineTypeToUString(imageFileHeader->Machine).prepend("\nMachine type: ") + + UString("\nMachine type: ") + machineTypeToUString(imageFileHeader->Machine) + usprintf("\nNumber of sections: %u\nCharacteristics: %04Xh", imageFileHeader->NumberOfSections, imageFileHeader->Characteristics); @@ -2810,7 +2811,7 @@ USTATUS FfsParser::parsePeImageSectionBody(const UModelIndex & index) optionalHeader.H32->ImageBase); } else if (optionalHeader.H32->Magic == EFI_IMAGE_PE_OPTIONAL_HDR64_MAGIC) { - info += usprintf("\nOptional header signature: %04Xh\nSubsystem: %04Xh\nAddress of entry point: %Xh\nBase of code: %Xh\nImage base: %Xh", + info += usprintf("\nOptional header signature: %04Xh\nSubsystem: %04Xh\nAddress of entry point: %Xh\nBase of code: %Xh\nImage base: %"PRIX64"h", optionalHeader.H64->Magic, optionalHeader.H64->Subsystem, optionalHeader.H64->AddressOfEntryPoint, @@ -2840,7 +2841,7 @@ USTATUS FfsParser::parseTeImageSectionBody(const UModelIndex & index) return U_SUCCESS; } - UByteArray info; + UString info; const EFI_IMAGE_TE_HEADER* teHeader = (const EFI_IMAGE_TE_HEADER*)body.constData(); if (teHeader->Signature != EFI_IMAGE_TE_SIGNATURE) { info += usprintf("\nSignature: %04Xh, invalid", teHeader->Signature); @@ -2848,9 +2849,9 @@ USTATUS FfsParser::parseTeImageSectionBody(const UModelIndex & index) } else { info += usprintf("\nSignature: %04Xh", teHeader->Signature) + - machineTypeToUString(teHeader->Machine).prepend("\nMachine type: ") + + UString("\nMachine type: ") + machineTypeToUString(teHeader->Machine) + usprintf("\nNumber of sections: %u\nSubsystem: %02Xh\nStripped size: %Xh (%u)\n" - "Base of code: %Xh\nAddress of entry point: %Xh\nImage base: %Xh\nAdjusted image base: %Xh", + "Base of code: %Xh\nAddress of entry point: %Xh\nImage base: %"PRIX64"h\nAdjusted image base: %"PRIX64"h", teHeader->NumberOfSections, teHeader->Subsystem, teHeader->StrippedSize, teHeader->StrippedSize, @@ -3256,9 +3257,8 @@ parsing_done: info += usprintf("\nAttributes: %02Xh", entryHeader->Attributes); // Translate attributes to text if (entryHeader->Attributes && entryHeader->Attributes != 0xFF) - info += nvarAttributesToUString(entryHeader->Attributes).prepend(" (").append(")"); - - + info += UString(" (") + nvarAttributesToUString(entryHeader->Attributes) + UString(")"); + // Add next node info if (!isInvalid && entryHeader->Next != lastVariableFlag) info += usprintf("\nNext node at offset: %Xh", parentOffset + offset + entryHeader->Next); @@ -3267,7 +3267,7 @@ parsing_done: if (hasExtendedHeader) { info += usprintf("\nExtended header size: %Xh (%u)\nExtended attributes: %Xh (", extendedHeaderSize, extendedHeaderSize, - extendedAttributes) + nvarExtendedAttributesToUString(extendedAttributes).append(")"); + extendedAttributes) + nvarExtendedAttributesToUString(extendedAttributes) + UString(")"); // Checksum if (hasChecksum) @@ -3275,8 +3275,8 @@ parsing_done: (calculatedChecksum ? usprintf(", invalid, should be %02Xh", 0x100 - calculatedChecksum) : UString(", valid")); // Authentication data if (hasTimestampAndHash) { - info += usprintf("\nTimestamp: %Xh\nHash: ", - timestamp) + hash.toHex().toUpper(); + info += usprintf("\nTimestamp: %"PRIX64"h\nHash: ", + timestamp) + UString(hash.toHex().toUpper()); } } @@ -3378,8 +3378,8 @@ USTATUS FfsParser::parseNvramVolumeBody(const UModelIndex & index) UByteArray padding = data.mid(storeOffset); // Get info - name = ("Padding"); - info = ("Full size: %Xh (%u)", padding.size(), padding.size()); + name = UString("Padding"); + info = usprintf("Full size: %Xh (%u)", padding.size(), padding.size()); // Construct parsing data pdata.offset = parentOffset + storeOffset; @@ -3770,14 +3770,14 @@ USTATUS FfsParser::parseFtwStoreHeader(const UByteArray & store, const UINT32 pa // Add info UString name("FTW store"); - UString info = guidToUString(ftw32BlockHeader->Signature).prepend("Signature:") + - usprintf("\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nState: %02Xh\nHeader CRC32: ", + UString info = UString("Signature: ") + guidToUString(ftw32BlockHeader->Signature) + + usprintf("\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nState: %02Xh\nHeader CRC32: %08Xh", ftwBlockSize, ftwBlockSize, headerSize, headerSize, body.size(), body.size(), - ftw32BlockHeader->State) + - (ftw32BlockHeader->Crc == calculatedCrc ? usprintf("%08Xh, valid", ftw32BlockHeader->Crc) : - usprintf("%08Xh, invalid, should be %08Xh", ftw32BlockHeader->Crc, calculatedCrc)); + ftw32BlockHeader->State, + ftw32BlockHeader->Crc) + + (ftw32BlockHeader->Crc != calculatedCrc ? usprintf(", invalid, should be %08Xh", calculatedCrc) : UString(", valid")); // Add correct offset pdata.offset = parentOffset; @@ -3896,14 +3896,14 @@ USTATUS FfsParser::parseFsysStoreHeader(const UByteArray & store, const UINT32 p // Add info bool isGaidStore = (fsysStoreHeader->Signature == NVRAM_APPLE_GAID_STORE_SIGNATURE); UString name = isGaidStore ? UString("Gaid store") : UString("Fsys store"); - UString info = usprintf("Signature: %s\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nUnknown0: %02Xh\nUnknown1: %08Xh\nCRC32: ", + UString info = usprintf("Signature: %s\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nUnknown0: %02Xh\nUnknown1: %08Xh\nCRC32: %08Xh", isGaidStore ? "Gaid" : "Fsys", fsysStoreHeader->Size, fsysStoreHeader->Size, header.size(), header.size(), body.size(), body.size(), fsysStoreHeader->Unknown0, fsysStoreHeader->Unknown1) - + (storedCrc == calculatedCrc ? usprintf("%08Xh, valid", storedCrc) : usprintf("%08Xh, invalid, should be %08Xh", storedCrc, calculatedCrc)); + + (storedCrc != calculatedCrc ? usprintf(", invalid, should be %08Xh", calculatedCrc) : UString(", valid")); // Add correct offset pdata.offset = parentOffset; @@ -3947,13 +3947,14 @@ USTATUS FfsParser::parseEvsaStoreHeader(const UByteArray & store, const UINT32 p // Add info UString name("EVSA store"); - UString info = usprintf("Signature: EVSA\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nType: %02Xh\nAttributes: %08Xh\nChecksum: ", + UString info = usprintf("Signature: EVSA\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: %Xh (%u)\nType: %02Xh\nAttributes: %08Xh\nChecksum: %02Xh", evsaStoreHeader->StoreSize, evsaStoreHeader->StoreSize, header.size(), header.size(), body.size(), body.size(), evsaStoreHeader->Header.Type, - evsaStoreHeader->Attributes) + - (evsaStoreHeader->Header.Checksum == calculated ? usprintf("%02Xh, valid", calculated) : usprintf("%02Xh, invalid, should be %02Xh", evsaStoreHeader->Header.Checksum, calculated)); + evsaStoreHeader->Attributes, + evsaStoreHeader->Header.Checksum) + + (evsaStoreHeader->Header.Checksum != calculated ? usprintf("%, invalid, should be %02Xh", calculated) : UString(", valid")); // Add correct offset pdata.offset = parentOffset; @@ -4084,7 +4085,7 @@ USTATUS FfsParser::parseSlicPubkeyHeader(const UByteArray & store, const UINT32 // Add info UString name("SLIC pubkey"); UString info = usprintf("Type: 0h\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: 0h (0)\n" - "Key type :%02Xh\nVersion: %02Xh\nAlgorithm: %08Xh\nMagic: RSA1\nBit length: %08Xh\nExponent:%08Xh", + "Key type :%02Xh\nVersion: %02Xh\nAlgorithm: %08Xh\nMagic: RSA1\nBit length: %08Xh\nExponent: %08Xh", pubkeyHeader->Size, pubkeyHeader->Size, header.size(), header.size(), pubkeyHeader->KeyType, @@ -4136,8 +4137,8 @@ USTATUS FfsParser::parseSlicMarkerHeader(const UByteArray & store, const UINT32 markerHeader->Size, markerHeader->Size, header.size(), header.size(), markerHeader->Version, - (const char*)&(markerHeader->OemId), - (const char*)&(markerHeader->OemTableId), + (const char*)UString((const char*)&(markerHeader->OemId), 6), + (const char*)UString((const char*)&(markerHeader->OemTableId), 8), markerHeader->SlicVersion); // Add correct offset @@ -4416,20 +4417,20 @@ USTATUS FfsParser::parseVssStoreBody(const UModelIndex & index) header.size(), header.size(), body.size(), body.size(), variableHeader->State, - variableHeader->Attributes) + vssAttributesToUString(variableHeader->Attributes).append(")"); + variableHeader->Attributes) + vssAttributesToUString(variableHeader->Attributes) + UString(")"); // Set subtype and add related info if (isInvalid) subtype = Subtypes::InvalidVssEntry; else if (isAuthenticated) { subtype = Subtypes::AuthVssEntry; - info += usprintf("\nMonotonic counter: %Xh\nTimestamp:", monotonicCounter) + efiTimeToUString(timestamp) + info += usprintf("\nMonotonic counter: %"PRIX64"h\nTimestamp: ", monotonicCounter) + efiTimeToUString(timestamp) + usprintf("\nPubKey index: %u", pubKeyIndex); } else if (isAppleCrc32) { subtype = Subtypes::AppleVssEntry; - info += usprintf("\nData checksum: %08X", storedCrc32) + - (storedCrc32 == calculatedCrc32 ? UString(", valid") : usprintf(", invalid, should be %08Xh", calculatedCrc32)); + info += usprintf("\nData checksum: %08Xh", storedCrc32) + + (storedCrc32 != calculatedCrc32 ? usprintf(", invalid, should be %08Xh", calculatedCrc32) : UString(", valid")); } else subtype = Subtypes::StandardVssEntry; @@ -4487,7 +4488,7 @@ USTATUS FfsParser::parseFsysStoreBody(const UModelIndex & index) pdata.offset = parentOffset + offset; // Add EOF tree item - model->addItem(Types::FsysEntry, 0, name, UString(), info, header, UByteArray(), UByteArray(), false, parsingDataToUByteArray(pdata), index); + model->addItem(Types::FsysEntry, 0, UString(name), UString(), info, header, UByteArray(), UByteArray(), false, parsingDataToUByteArray(pdata), index); // Add free space offset += header.size(); @@ -4541,7 +4542,7 @@ USTATUS FfsParser::parseFsysStoreBody(const UModelIndex & index) pdata.offset = parentOffset + offset; // Add tree item - model->addItem(Types::FsysEntry, 0, name, UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), index); + model->addItem(Types::FsysEntry, 0, UString(name), UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), index); // Move to next variable offset += variableSize; @@ -4628,7 +4629,7 @@ USTATUS FfsParser::parseEvsaStoreBody(const UModelIndex & index) body.size(), body.size(), guidHeader->Header.Type, guidHeader->Header.Checksum) - + (guidHeader->Header.Checksum == calculated ? UString(", valid") : usprintf(", invalid, should be %02Xh", calculated)) + + (guidHeader->Header.Checksum != calculated ? usprintf(", invalid, should be %02Xh", calculated) : UString(", valid")) + usprintf("\nGuidId: %04Xh", guidHeader->GuidId); subtype = Subtypes::GuidEvsaEntry; guidMap.insert(std::pair(guidHeader->GuidId, guid)); @@ -4646,7 +4647,7 @@ USTATUS FfsParser::parseEvsaStoreBody(const UModelIndex & index) body.size(), body.size(), nameHeader->Header.Type, nameHeader->Header.Checksum) - + (nameHeader->Header.Checksum == calculated ? UString(", valid") : usprintf(", invalid, should be %02Xh", calculated)) + + (nameHeader->Header.Checksum != calculated ? usprintf(", invalid, should be %02Xh", calculated) : UString(", valid")) + usprintf("\nVarId: %04Xh", nameHeader->VarId); subtype = Subtypes::NameEvsaEntry; nameMap.insert(std::pair(nameHeader->VarId, name)); @@ -4675,12 +4676,12 @@ USTATUS FfsParser::parseEvsaStoreBody(const UModelIndex & index) dataSize, dataSize, dataHeader->Header.Type, dataHeader->Header.Checksum) - + (dataHeader->Header.Checksum == calculated ? UString("valid") : usprintf(", invalid, should be %02Xh", calculated)) + + (dataHeader->Header.Checksum != calculated ? usprintf(", invalid, should be %02Xh", calculated) : UString(", valid")) + usprintf("\nVarId: %04Xh\nGuidId: %04Xh\nAttributes: %08Xh (", dataHeader->VarId, dataHeader->GuidId, dataHeader->Attributes) - + evsaAttributesToUString(dataHeader->Attributes).append(")"); + + evsaAttributesToUString(dataHeader->Attributes) + UString(")"); subtype = Subtypes::DataEvsaEntry; } // Unknown entry or free space @@ -4759,7 +4760,7 @@ USTATUS FfsParser::parseEvsaStoreBody(const UModelIndex & index) model->setName(current, guid); } model->setText(current, name); - model->addInfo(current, guid.prepend("GUID: ") + name.prepend("\nName: "), false); + model->addInfo(current, UString("GUID: ") + guid + UString("\nName: ") + name + UString("\n"), false); } } } diff --git a/common/ffsreport.cpp b/common/ffsreport.cpp index 3f9638c..a16c592 100644 --- a/common/ffsreport.cpp +++ b/common/ffsreport.cpp @@ -53,9 +53,9 @@ USTATUS FfsReport::generateRecursive(std::vector & report, UModelIndex UString text = model->text(index); report.push_back( UString(" ") + itemTypeToUString(model->type(index)).leftJustified(16) - + UString("| ") + itemSubtypeToUString(model->type(index), model->subtype(index)).leftJustified(22) + + UString("| ") + itemSubtypeToUString(model->type(index), model->subtype(index)).leftJustified(22) + usprintf("| %08X | %08X | ", data.size(), crc) - + UString(level, '-') + UString(" ") + model->name(index) + (text.isEmpty() ? UString("") : UString(" | ") + text) + + UString('-', level) + UString(" ") + model->name(index) + (text.isEmpty() ? UString("") : UString(" | ") + text) ); // Information on child items diff --git a/common/fitparser.cpp b/common/fitparser.cpp index a01c1dc..21e7ceb 100644 --- a/common/fitparser.cpp +++ b/common/fitparser.cpp @@ -58,10 +58,10 @@ USTATUS FitParser::parse(const UModelIndex & index, const UModelIndex & lastVtfI // Add FIT header to fitTable std::vector currentStrings; currentStrings.push_back(UString("_FIT_ ")); - currentStrings.push_back(usprintf("%08X",fitSize)); - currentStrings.push_back(usprintf("%04X",fitHeader->Version)); + currentStrings.push_back(usprintf("%08X", fitSize)); + currentStrings.push_back(usprintf("%04X", fitHeader->Version)); currentStrings.push_back(fitEntryTypeToUString(fitHeader->Type)); - currentStrings.push_back(usprintf("%02X",fitHeader->Checksum)); + currentStrings.push_back(usprintf("%02X", fitHeader->Checksum)); fitTable.push_back(currentStrings); // Process all other entries diff --git a/common/nvram.cpp b/common/nvram.cpp index c279d56..d34af8b 100644 --- a/common/nvram.cpp +++ b/common/nvram.cpp @@ -28,7 +28,8 @@ UString nvarAttributesToUString(const UINT8 attributes) if (attributes & NVRAM_NVAR_ENTRY_AUTH_WRITE) str += UString(", AuthWrite"); if (attributes & NVRAM_NVAR_ENTRY_VALID) str += UString(", Valid"); - return str.mid(2); // Remove first comma and space + str.remove(0, 2); // Remove first comma and space + return str; } UString nvarExtendedAttributesToUString(const UINT8 attributes) @@ -39,7 +40,8 @@ UString nvarExtendedAttributesToUString(const UINT8 attributes) if (attributes & NVRAM_NVAR_ENTRY_EXT_TIME_BASED) str += UString(", TimeBasedAuthWrite"); if (attributes & NVRAM_NVAR_ENTRY_EXT_UNKNOWN_MASK) str += UString(", Unknown"); - return str.mid(2); // Remove first comma and space + str.remove(0, 2); // Remove first comma and space + return str; } extern UString vssAttributesToUString(const UINT32 attributes) @@ -55,7 +57,8 @@ extern UString vssAttributesToUString(const UINT32 attributes) if (attributes & NVRAM_VSS_VARIABLE_APPLE_DATA_CHECKSUM) str += UString(", AppleChecksum"); if (attributes & NVRAM_VSS_VARIABLE_UNKNOWN_MASK) str += UString(", Unknown"); - return str.mid(2); // Remove first comma and space + str.remove(0, 2); // Remove first comma and space + return str; } UString evsaAttributesToUString(const UINT32 attributes) @@ -71,7 +74,8 @@ UString evsaAttributesToUString(const UINT32 attributes) if (attributes & NVRAM_EVSA_DATA_EXTENDED_HEADER) str += UString(", ExtendedHeader"); if (attributes & NVRAM_EVSA_DATA_UNKNOWN_MASK) str += UString(", Unknown"); - return str.mid(2); // Remove first comma and space + str.remove(0, 2); // Remove first comma and space + return str; } UString efiTimeToUString(const EFI_TIME & time) diff --git a/common/treeitem.cpp b/common/treeitem.cpp index a0fefe0..c8c3531 100644 --- a/common/treeitem.cpp +++ b/common/treeitem.cpp @@ -33,28 +33,35 @@ TreeItem::TreeItem(const UINT8 type, const UINT8 subtype, itemCompressed(compressed), parentItem(parent) { - setFixed(fixed); +} + +TreeItem::~TreeItem() { + std::list::iterator begin = childItems.begin(); + while (begin != childItems.end()) { + delete *begin; + ++begin; + } } UINT8 TreeItem::insertChildBefore(TreeItem *item, TreeItem *newItem) { - int index = childItems.indexOf(item); - if (index == -1) + std::list::iterator found = std::find(std::begin(childItems), std::end(childItems), item); + if (found == std::end(childItems)) return U_ITEM_NOT_FOUND; - childItems.insert(index, newItem); + childItems.insert(found, newItem); return U_SUCCESS; } UINT8 TreeItem::insertChildAfter(TreeItem *item, TreeItem *newItem) { - int index = childItems.indexOf(item); - if (index == -1) + std::list::iterator found = std::find(std::begin(childItems), std::end(childItems), item); + if (found == std::end(childItems)) return U_ITEM_NOT_FOUND; - childItems.insert(index + 1, newItem); + childItems.insert(++found, newItem); return U_SUCCESS; } -QVariant TreeItem::data(int column) const +UString TreeItem::data(int column) const { switch (column) { @@ -69,14 +76,18 @@ QVariant TreeItem::data(int column) const case 4: // Text return itemText; default: - return QVariant(); + return UString(); } } int TreeItem::row() const { - if (parentItem) - return parentItem->childItems.indexOf(const_cast(this)); - + if (parentItem) { + std::list::const_iterator iter = parentItem->childItems.cbegin(); + for (int i = 0; i < (int)parentItem->childItems.size(); ++i, ++iter) { + if (const_cast(this) == *iter) + return i; + } + } return 0; } diff --git a/common/treeitem.h b/common/treeitem.h index 265d507..08b92af 100644 --- a/common/treeitem.h +++ b/common/treeitem.h @@ -14,8 +14,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef TREEITEM_H #define TREEITEM_H -#include -#include +#include #include "ubytearray.h" #include "ustring.h" @@ -28,19 +27,19 @@ public: const UByteArray & header, const UByteArray & body, const UByteArray & tail, const BOOLEAN fixed, const BOOLEAN compressed, const UByteArray & parsingData, TreeItem *parent = 0); - ~TreeItem() { qDeleteAll(childItems); } + ~TreeItem(); // Non-trivial implementation in CPP file // Operations with items - void appendChild(TreeItem *item) { childItems.append(item); } - void prependChild(TreeItem *item) { childItems.prepend(item); }; + void appendChild(TreeItem *item) { childItems.push_back(item); } + void prependChild(TreeItem *item) { childItems.push_front(item); }; UINT8 insertChildBefore(TreeItem *item, TreeItem *newItem); // Non-trivial implementation in CPP file UINT8 insertChildAfter(TreeItem *item, TreeItem *newItem); // Non-trivial implementation in CPP file // Model support operations - TreeItem *child(int row) { return childItems.value(row, NULL); } - int childCount() const {return childItems.count(); } + TreeItem *child(int row) { return *std::next(childItems.begin(), row); } + int childCount() const {return childItems.size(); } int columnCount() const { return 5; } - QVariant data(int column) const; // Non-trivial implementation in CPP file + UString data(int column) const; // Non-trivial implementation in CPP file int row() const; // Non-trivial implementation in CPP file TreeItem *parent() { return parentItem; } @@ -71,7 +70,7 @@ public: void setParsingData(const UByteArray & data) { itemParsingData = data; } UString info() const { return itemInfo; } - void addInfo(const UString &info, const BOOLEAN append) { if (append) itemInfo.append(info); else itemInfo.prepend(info); } + void addInfo(const UString &info, const BOOLEAN append) { if (append) itemInfo += info; else itemInfo = info + itemInfo; } void setInfo(const UString &info) { itemInfo = info; } UINT8 action() const {return itemAction; } @@ -84,7 +83,7 @@ public: void setCompressed(const bool compressed) { itemCompressed = compressed; } private: - QList childItems; + std::list childItems; UINT8 itemAction; UINT8 itemType; UINT8 itemSubtype; diff --git a/common/treemodel.cpp b/common/treemodel.cpp index f4222f7..df0e03f 100644 --- a/common/treemodel.cpp +++ b/common/treemodel.cpp @@ -44,9 +44,9 @@ QVariant TreeModel::data(const UModelIndex &index, int role) const TreeItem *item = static_cast(index.internalPointer()); if (role == Qt::DisplayRole) - return item->data(index.column()); + return (const char*)item->data(index.column()); else - return item->info(); + return (const char*)item->info(); } Qt::ItemFlags TreeModel::flags(const UModelIndex &index) const diff --git a/common/ustring.cpp b/common/ustring.cpp index ccc0284..94b75fe 100644 --- a/common/ustring.cpp +++ b/common/ustring.cpp @@ -13,13 +13,84 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "ustring.h" #include +//TODO: modify properly + +#ifndef QT_CORE_LIB UString usprintf(const char* fmt, ...) { UString msg; va_list vl; va_start(vl, fmt); - msg = msg.vsprintf(fmt, vl); + msg.vsprintf(fmt, vl); va_end(vl); return msg; }; +#else +/* Give WATCOM C/C++, MSVC some latitude for their non-support of vsnprintf */ +#if defined(__WATCOMC__) || defined(_MSC_VER) +#define exvsnprintf(r,b,n,f,a) {r = _vsnprintf (b,n,f,a);} +#else +#ifdef BSTRLIB_NOVSNP +/* This is just a hack. If you are using a system without a vsnprintf, it is +not recommended that bformat be used at all. */ +#define exvsnprintf(r,b,n,f,a) {vsprintf (b,f,a); r = -1;} +#define START_VSNBUFF (256) +#else +#if defined (__GNUC__) && !defined (__PPC__) +/* Something is making gcc complain about this prototype not being here, so +I've just gone ahead and put it in. */ +extern "C" { + extern int vsnprintf(char *buf, size_t count, const char *format, va_list arg); +} +#endif + +#define exvsnprintf(r,b,n,f,a) {r = vsnprintf (b,n,f,a);} +#endif +#endif + +#ifndef START_VSNBUFF +#define START_VSNBUFF (16) +#endif + +UString usprintf(const char* fmt, ...) +{ + UString msg; + bstring b; + va_list arglist; + int r, n; + + if (fmt == NULL) { + msg = ""; + } + else { + + if ((b = bfromcstr("")) == NULL) { + msg = ""; + } + else { + if ((n = (int)(2 * (strlen)(fmt))) < START_VSNBUFF) n = START_VSNBUFF; + for (;;) { + if (BSTR_OK != balloc(b, n + 2)) { + b = bformat(""); + break; + } + + va_start(arglist, fmt); + exvsnprintf(r, (char *)b->data, n + 1, fmt, arglist); + va_end(arglist); + + b->data[n] = '\0'; + b->slen = (int)(strlen)((char *)b->data); + + if (b->slen < n) break; + if (r > n) n = r; else n += n; + } + msg = *b; + bdestroy(b); + } + } + + return msg; +} +#endif diff --git a/common/ustring.h b/common/ustring.h index 635bf62..345917a 100644 --- a/common/ustring.h +++ b/common/ustring.h @@ -15,20 +15,17 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. //TODO: modify properly -#ifdef QT_CORE_LIB +#ifndef QT_CORE_LIB // Use Qt class, if Qt is available #include #define UString QString #else -// Use own implementation +// Use Bstrlib +#define BSTRLIB_DOESNT_THROW_EXCEPTIONS #include "../bstrlib/bstrwrap.h" -class UString : public CBString { -}; - +#define UString CBString #endif // QT_CORE_LIB UString usprintf(const char* fmt, ...); - - #endif // USTRING_H From 9bd71281b9d5c7139de6d4623b89e167ef369324 Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Sun, 26 Jun 2016 10:14:44 +0200 Subject: [PATCH 04/12] remove submodule --- bstrlib | 1 - bstrlib/LICENSE | 28 + bstrlib/README.md | 34 + bstrlib/bsafe.c | 82 + bstrlib/bsafe.h | 43 + bstrlib/bstest.c | 3689 ++++++++++++++++++++++++++++++++++++++++++ bstrlib/bstraux.c | 1161 +++++++++++++ bstrlib/bstraux.h | 115 ++ bstrlib/bstrlib.c | 3153 ++++++++++++++++++++++++++++++++++++ bstrlib/bstrlib.h | 316 ++++ bstrlib/bstrlib.txt | 3512 ++++++++++++++++++++++++++++++++++++++++ bstrlib/bstrwrap.cpp | 1721 ++++++++++++++++++++ bstrlib/bstrwrap.h | 446 +++++ bstrlib/buniutil.c | 274 ++++ bstrlib/buniutil.h | 37 + bstrlib/gpl.txt | 339 ++++ bstrlib/porting.txt | 172 ++ bstrlib/security.txt | 217 +++ bstrlib/test.cpp | 1725 ++++++++++++++++++++ bstrlib/testaux.c | 423 +++++ bstrlib/utf8util.c | 249 +++ bstrlib/utf8util.h | 62 + 22 files changed, 17798 insertions(+), 1 deletion(-) delete mode 160000 bstrlib create mode 100644 bstrlib/LICENSE create mode 100644 bstrlib/README.md create mode 100644 bstrlib/bsafe.c create mode 100644 bstrlib/bsafe.h create mode 100644 bstrlib/bstest.c create mode 100644 bstrlib/bstraux.c create mode 100644 bstrlib/bstraux.h create mode 100644 bstrlib/bstrlib.c create mode 100644 bstrlib/bstrlib.h create mode 100644 bstrlib/bstrlib.txt create mode 100644 bstrlib/bstrwrap.cpp create mode 100644 bstrlib/bstrwrap.h create mode 100644 bstrlib/buniutil.c create mode 100644 bstrlib/buniutil.h create mode 100644 bstrlib/gpl.txt create mode 100644 bstrlib/porting.txt create mode 100644 bstrlib/security.txt create mode 100644 bstrlib/test.cpp create mode 100644 bstrlib/testaux.c create mode 100644 bstrlib/utf8util.c create mode 100644 bstrlib/utf8util.h diff --git a/bstrlib b/bstrlib deleted file mode 160000 index 208b1f2..0000000 --- a/bstrlib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 208b1f2a4dfc96b806ed499bd1909e87ec15981d diff --git a/bstrlib/LICENSE b/bstrlib/LICENSE new file mode 100644 index 0000000..28ab228 --- /dev/null +++ b/bstrlib/LICENSE @@ -0,0 +1,28 @@ +Copyright (c) 2014, Paul Hsieh +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of bstrlib nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/bstrlib/README.md b/bstrlib/README.md new file mode 100644 index 0000000..367650b --- /dev/null +++ b/bstrlib/README.md @@ -0,0 +1,34 @@ +The Better String Library + +The Better String Library is an abstraction of a string data type which is +superior to the C library char buffer string type, or C++'s std::string. +Among the features achieved are: + + - Substantial mitigation of buffer overflow/overrun problems and other + failures that result from erroneous usage of the common C string + library functions + + - Significantly simplified string manipulation + + - High performance interoperability with other source/libraries which + expect '\0' terminated char buffers + + - Improved overall performance of common string operations + + - Functional equivalency with other more modern languages + +The library is totally stand alone, portable (known to work with gcc/g++, +MSVC++, Intel C++, WATCOM C/C++, Turbo C, Borland C++, IBM's native CC +compiler on Windows, Linux and Mac OS X), high performance, easy to use and +is not part of some other collection of data structures. Even the file I/O +functions are totally abstracted (so that other stream-like mechanisms, like +sockets, can be used.) Nevertheless, it is adequate as a complete +replacement of the C string library for string manipulation in any C program. + +The library includes a robust C++ wrapper that uses overloaded operators, +rich constructors, exceptions, stream I/O and STL to make the CBString +struct a natural and powerful string abstraction with more functionality and +higher performance than std::string. + +Bstrlib is stable, well tested and suitable for any software production +environment. diff --git a/bstrlib/bsafe.c b/bstrlib/bsafe.c new file mode 100644 index 0000000..dc32e90 --- /dev/null +++ b/bstrlib/bsafe.c @@ -0,0 +1,82 @@ +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2015, and is covered by the BSD open source + * license. Refer to the accompanying documentation for details on usage and + * license. + */ + +/* + * bsafe.c + * + * This is an optional module that can be used to help enforce a safety + * standard based on pervasive usage of bstrlib. This file is not necessarily + * portable, however, it has been tested to work correctly with Intel's C/C++ + * compiler, WATCOM C/C++ v11.x and Microsoft Visual C++. + */ + +#include +#include +#include "bsafe.h" + +static int bsafeShouldExit = 1; + +char * strcpy (char *dst, const char *src); +char * strcat (char *dst, const char *src); + +char * strcpy (char *dst, const char *src) { + (void) dst; + (void) src; + fprintf (stderr, "bsafe error: strcpy() is not safe, use bstrcpy instead.\n"); + if (bsafeShouldExit) exit (-1); + return NULL; +} + +char * strcat (char *dst, const char *src) { + (void) dst; + (void) src; + fprintf (stderr, "bsafe error: strcat() is not safe, use bstrcat instead.\n"); + if (bsafeShouldExit) exit (-1); + return NULL; +} + +#if !defined (__GNUC__) && (!defined(_MSC_VER) || (_MSC_VER <= 1310)) +char * (gets) (char * buf) { + (void) buf; + fprintf (stderr, "bsafe error: gets() is not safe, use bgets.\n"); + if (bsafeShouldExit) exit (-1); + return NULL; +} +#endif + +char * (strncpy) (char *dst, const char *src, size_t n) { + (void) dst; + (void) src; + (void) n; + fprintf (stderr, "bsafe error: strncpy() is not safe, use bmidstr instead.\n"); + if (bsafeShouldExit) exit (-1); + return NULL; +} + +char * (strncat) (char *dst, const char *src, size_t n) { + (void) dst; + (void) src; + (void) n; + fprintf (stderr, "bsafe error: strncat() is not safe, use bstrcat then btrunc\n\tor cstr2tbstr, btrunc then bstrcat instead.\n"); + if (bsafeShouldExit) exit (-1); + return NULL; +} + +char * (strtok) (char *s1, const char *s2) { + (void) s1; + (void) s2; + fprintf (stderr, "bsafe error: strtok() is not safe, use bsplit or bsplits instead.\n"); + if (bsafeShouldExit) exit (-1); + return NULL; +} + +char * (strdup) (const char *s) { + (void) s; + fprintf (stderr, "bsafe error: strdup() is not safe, use bstrcpy.\n"); + if (bsafeShouldExit) exit (-1); + return NULL; +} diff --git a/bstrlib/bsafe.h b/bstrlib/bsafe.h new file mode 100644 index 0000000..6ee49cf --- /dev/null +++ b/bstrlib/bsafe.h @@ -0,0 +1,43 @@ +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2004, and is covered by the BSD open source + * license. Refer to the accompanying documentation for details on usage and + * license. + */ + +/* + * bsafe.h + * + * This is an optional module that can be used to help enforce a safety + * standard based on pervasive usage of bstrlib. This file is not necessarily + * portable, however, it has been tested to work correctly with Intel's C/C++ + * compiler, WATCOM C/C++ v11.x and Microsoft Visual C++. + */ + +#ifndef BSTRLIB_BSAFE_INCLUDE +#define BSTRLIB_BSAFE_INCLUDE + +#ifdef __cplusplus +extern "C" { +#endif + +#if !defined (__GNUC__) && (!defined(_MSC_VER) || (_MSC_VER <= 1310)) +/* This is caught in the linker, so its not necessary for gcc. */ +extern char * (gets) (char * buf); +#endif + +extern char * (strncpy) (char *dst, const char *src, size_t n); +extern char * (strncat) (char *dst, const char *src, size_t n); +extern char * (strtok) (char *s1, const char *s2); +extern char * (strdup) (const char *s); + +#undef strcpy +#undef strcat +#define strcpy(a,b) bsafe_strcpy(a,b) +#define strcat(a,b) bsafe_strcat(a,b) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/bstrlib/bstest.c b/bstrlib/bstest.c new file mode 100644 index 0000000..773768d --- /dev/null +++ b/bstrlib/bstest.c @@ -0,0 +1,3689 @@ +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2015, and is covered by the BSD open source + * license. Refer to the accompanying documentation for details on usage and + * license. + */ + +/* + * bstest.c + * + * This file is the C unit test for Bstrlib. + */ + +#include +#include +#include +#include +#include +#include "bstrlib.h" +#include "bstraux.h" + +static bstring dumpOut[16]; +static int rot = 0; + +static int incorrectBstring (const struct tagbstring * b) { + if (NULL == b) return 1; + if (NULL == b->data) return 1; + if (b->slen < 0) return 1; + if (b->mlen > 0 && b->slen > b->mlen) return 1; + if (b->data[b->slen] != '\0') return 1; + return 0; +} + +static char * dumpBstring (const struct tagbstring * b) { + rot = (rot + 1) % (unsigned)16; + if (dumpOut[rot] == NULL) { + dumpOut[rot] = bfromcstr (""); + if (dumpOut[rot] == NULL) return "FATAL INTERNAL ERROR"; + } + dumpOut[rot]->slen = 0; + if (b == NULL) { + bcatcstr (dumpOut[rot], "NULL"); + } else { + static char msg[256]; + sprintf (msg, "%p", (void *)b); + bcatcstr (dumpOut[rot], msg); + + if (b->slen < 0) { + sprintf (msg, ":[err:slen=%d<0]", b->slen); + bcatcstr (dumpOut[rot], msg); + } else { + if (b->mlen > 0 && b->mlen < b->slen) { + sprintf (msg, ":[err:mlen=%dmlen, b->slen); + bcatcstr (dumpOut[rot], msg); + } else { + if (b->mlen == -1) { + bcatcstr (dumpOut[rot], "[p]"); + } else if (b->mlen < 0) { + bcatcstr (dumpOut[rot], "[c]"); + } + bcatcstr (dumpOut[rot], ":"); + if (b->data == NULL) { + bcatcstr (dumpOut[rot], "[err:data=NULL]"); + } else { + bcatcstr (dumpOut[rot], "\""); + bcatcstr (dumpOut[rot], (const char *) b->data); + bcatcstr (dumpOut[rot], "\""); + } + } + } + } + return (char *) dumpOut[rot]->data; +} + +static char* dumpCstring (const char* s) { + rot = (rot + 1) % (unsigned)16; + if (dumpOut[rot] == NULL) { + dumpOut[rot] = bfromcstr (""); + if (dumpOut[rot] == NULL) return "FATAL INTERNAL ERROR"; + } + dumpOut[rot]->slen = 0; + if (s == NULL) { + bcatcstr (dumpOut[rot], "NULL"); + } else { + static char msg[64]; + int i; + + sprintf (msg, "cstr[%p] -> ", (void *)s); + bcatcstr (dumpOut[rot], msg); + + bcatStatic (dumpOut[rot], "\""); + for (i = 0; s[i]; i++) { + if (i > 1024) { + bcatStatic (dumpOut[rot], " ..."); + break; + } + bconchar (dumpOut[rot], s[i]); + } + bcatStatic (dumpOut[rot], "\""); + } + + return (char *) dumpOut[rot]->data; +} + +static int test0_0 (const char * s, const char * res) { +bstring b0 = bfromcstr (s); +int ret = 0; + + if (s == NULL) { + if (res != NULL) ret++; + printf (".\tbfromcstr (NULL) = %s\n", dumpBstring (b0)); + return ret; + } + + ret += (res == NULL) || ((int) strlen (res) != b0->slen) + || (0 != memcmp (res, b0->data, b0->slen)); + ret += b0->data[b0->slen] != '\0'; + + printf (".\tbfromcstr (\"%s\") = %s\n", s, dumpBstring (b0)); + bdestroy (b0); + return ret; +} + +static int test0_1 (const char * s, int len, const char * res) { +bstring b0 = bfromcstralloc (len, s); +int ret = 0; + + if (s == NULL) { + if (res != NULL) ret++; + printf (".\tbfromcstralloc (*, NULL) = %s\n", dumpBstring (b0)); + return ret; + } + + ret += (res == NULL) || ((int) strlen (res) != b0->slen) + || (0 != memcmp (res, b0->data, b0->slen)); + ret += b0->data[b0->slen] != '\0'; + ret += len > b0->mlen; + + printf (".\tbfromcstralloc (%d, \"%s\") = %s\n", len, s, dumpBstring (b0)); + bdestroy (b0); + return ret; +} + +#define EMPTY_STRING "" +#define SHORT_STRING "bogus" +#define EIGHT_CHAR_STRING "Waterloo" +#define LONG_STRING "This is a bogus but reasonably long string. Just long enough to cause some mallocing." + +static int test0_2 (char* s) { +int l = s?strlen(s):2; +int i, j, k; +int ret = 0; + + for (i = 0; i < l*2; i++) { + for (j = 0; j < l*2; j++) { + for (k = 0; k <= l; k++) { + char* t = s ? (s + k) : NULL; + bstring b = bfromcstrrangealloc (i, j, t); + if (NULL == b) { + if (i < j && t != NULL) { + printf ("[%d] i = %d, j = %d, l = %d, k = %d\n", __LINE__, i, j, l, k); + } + ret += (i < j && t != NULL); + continue; + } + if (NULL == t) { + printf ("[%d] i = %d, j = %d, l = %d, k = %d\n", __LINE__, i, j, l, k); + ret++; + bdestroy (b); + continue; + } + if (b->data == NULL) { + printf ("[%d] i = %d, j = %d, l = %d, k = %d\n", __LINE__, i, j, l, k); + ret++; + continue; + } + if (b->slen != l-k || b->data[l-k] != '\0' || b->mlen <= b->slen) { + printf ("[%d] i = %d, j = %d, l = %d, k = %d, b->slen = %d\n", __LINE__, i, j, l, k, b->slen); + ret++; + } else if (0 != memcmp (t, b->data, l-k+1)) { + printf ("[%d] \"%s\" != \"%s\"\n", b->data, t); + ret++; + } + bdestroy (b); + continue; + } + } + } + + printf (".\tbfromcstrrangealloc (*,*,%s) correct\n", dumpCstring(s)); + return ret; +} + +static int test0 (void) { +int ret = 0; + + printf ("TEST: bstring bfromcstr (const char * str);\n"); + + /* tests with NULL */ + ret += test0_0 (NULL, NULL); + + /* normal operation tests */ + ret += test0_0 (EMPTY_STRING, EMPTY_STRING); + ret += test0_0 (SHORT_STRING, SHORT_STRING); + ret += test0_0 (LONG_STRING, LONG_STRING); + printf ("\t# failures: %d\n", ret); + + printf ("TEST: bstring bfromcstralloc (int len, const char * str);\n"); + + /* tests with NULL */ + ret += test0_1 (NULL, 0, NULL); + ret += test0_1 (NULL, 30, NULL); + + /* normal operation tests */ + ret += test0_1 (EMPTY_STRING, 0, EMPTY_STRING); + ret += test0_1 (EMPTY_STRING, 30, EMPTY_STRING); + ret += test0_1 (SHORT_STRING, 0, SHORT_STRING); + ret += test0_1 (SHORT_STRING, 30, SHORT_STRING); + ret += test0_1 ( LONG_STRING, 0, LONG_STRING); + ret += test0_1 ( LONG_STRING, 30, LONG_STRING); + + printf ("TEST: bstring bfromcstrrangealloc (int minl, int maxl, const char * str);\n"); + + ret += test0_2 (NULL); + ret += test0_2 (EMPTY_STRING); + ret += test0_2 ( LONG_STRING); + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +static int test1_0 (const void * blk, int len, const char * res) { +bstring b0 = blk2bstr (blk, len); +int ret = 0; + if (b0 == NULL) { + if (res != NULL) ret++; + printf (".\tblk2bstr (NULL, len=%d) = %s\n", len, dumpBstring (b0)); + } else { + ret += (res == NULL) || (len != b0->slen) + || (0 != memcmp (res, b0->data, len)); + ret += b0->data[b0->slen] != '\0'; + printf (".\tblk2bstr (blk=%p, len=%d) = %s\n", blk, len, dumpBstring (b0)); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + bdestroy (b0); + return ret; +} + +static int test1 (void) { +int ret = 0; + + printf ("TEST: bstring blk2bstr (const void * blk, int len);\n"); + + /* tests with NULL */ + ret += test1_0 (NULL, 10, NULL); + ret += test1_0 (NULL, 0, NULL); + ret += test1_0 (NULL, -1, NULL); + + /* normal operation tests */ + ret += test1_0 (SHORT_STRING, sizeof (SHORT_STRING)-1, SHORT_STRING); + ret += test1_0 (LONG_STRING, sizeof (LONG_STRING)-1, LONG_STRING); + ret += test1_0 (LONG_STRING, 5, "This "); + ret += test1_0 (LONG_STRING, 0, ""); + ret += test1_0 (LONG_STRING, -1, NULL); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test2_0 (const_bstring b, char z, const unsigned char * res) { +char * s = bstr2cstr (b, z); +int ret = 0; + if (s == NULL) { + if (res != NULL) ret++; + printf (".\tbstr2cstr (%s, %02X) = NULL\n", dumpBstring (b), z); + free(s); + return ret; + } + + if (res == NULL) ret++; + else { + if (z != '\0') if ((int) strlen (s) != b->slen) ret++; + if (!ret) { + ret += (0 != memcmp (res, b->data, b->slen)); + } + } + + printf (".\tbstr2cstr (%s, %02X) = \"%s\"\n", dumpBstring (b), z, s); + free (s); + return ret; +} + +struct tagbstring emptyBstring = bsStatic (""); +struct tagbstring shortBstring = bsStatic ("bogus"); +struct tagbstring longBstring = bsStatic ("This is a bogus but reasonably long string. Just long enough to cause some mallocing."); + +struct tagbstring badBstring1 = {8, 4, NULL}; +struct tagbstring badBstring2 = {2, -5, (unsigned char *) "bogus"}; +struct tagbstring badBstring3 = {2, 5, (unsigned char *) "bogus"}; + +struct tagbstring xxxxxBstring = bsStatic ("xxxxx"); + +static int test2 (void) { +int ret = 0; + + printf ("TEST: char * bstr2cstr (const_bstring s, char z);\n"); + + /* tests with NULL */ + ret += test2_0 (NULL, (char) '?', NULL); + + /* normal operation tests */ + ret += test2_0 (&emptyBstring, (char) '?', emptyBstring.data); + ret += test2_0 (&shortBstring, (char) '?', shortBstring.data); + ret += test2_0 (&longBstring, (char) '?', longBstring.data); + ret += test2_0 (&badBstring1, (char) '?', NULL); + ret += test2_0 (&badBstring2, (char) '?', NULL); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test3_0 (const_bstring b) { +bstring b0 = bstrcpy (b); +int ret = 0; + printf (".\tbstrcpy (%s) = %s\n", dumpBstring (b), dumpBstring (b0)); + if (b0 == NULL) { + if (b != NULL && b->data != NULL && b->slen >= 0) ret++; + } else { + ret += (b == NULL) || (b->slen != b0->slen) + || (0 != memcmp (b->data, b0->data, b->slen)); + ret += b0->data[b0->slen] != '\0'; + } + bdestroy (b0); + return ret; +} + +static int test3 (void) { +int ret = 0; + + printf ("TEST: bstring bstrcpy (const_bstring b1);\n"); + + /* tests with NULL to make sure that there is NULL propogation */ + ret += test3_0 (NULL); + ret += test3_0 (&badBstring1); + ret += test3_0 (&badBstring2); + + /* normal operation tests */ + ret += test3_0 (&emptyBstring); + ret += test3_0 (&shortBstring); + ret += test3_0 (&longBstring); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test4_0 (const_bstring b, int left, int len, const char * res) { +bstring b0 = bmidstr (b, left, len); +int ret = 0; + printf (".\tbmidstr (%s, %d, %d) = %s\n", dumpBstring (b), left, len, dumpBstring (b0)); + if (b0 == NULL) { + if (b != NULL && b->data != NULL && b->slen >= 0 && len >= 0) ret++; + } else { + ret += (b == NULL) || (res == NULL) || (b0->slen > len && len >= 0) + || (b0->slen != (int) strlen (res)) + || (b0->slen > 0 && 0 != memcmp (res, b0->data, b0->slen)); + ret += b0->data[b0->slen] != '\0'; + } + if (ret) { + printf ("(b == NULL) = %d\n", (b == NULL)); + printf ("(res == NULL) = %d\n", (res == NULL)); + printf ("(b0->slen > len && len >= 0) = %d\n", (b0->slen > len && len >= 0)); + if (res) printf ("(b0->slen != strlen (res)) = %d\n", (b0->slen != (int) strlen (res))); + printf ("(b0->slen > 0 && 0 != memcmp (res, b0->data, b0->slen) = %d\n", (b0->slen > 0 && 0 != memcmp (res, b0->data, b0->slen))); + + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + bdestroy (b0); + return ret; +} + +static int test4 (void) { +int ret = 0; + + printf ("TEST: bstring bmidstr (const_bstring b, int left, int len);\n"); + + /* tests with NULL to make sure that there is NULL propogation */ + ret += test4_0 (NULL, 0, 0, NULL); + ret += test4_0 (NULL, 0, 2, NULL); + ret += test4_0 (NULL, 0, -2, NULL); + ret += test4_0 (NULL, -5, 2, NULL); + ret += test4_0 (NULL, -5, -2, NULL); + ret += test4_0 (&badBstring1, 1, 3, NULL); + ret += test4_0 (&badBstring2, 1, 3, NULL); + + /* normal operation tests on all sorts of subranges */ + ret += test4_0 (&emptyBstring, 0, 0, ""); + ret += test4_0 (&emptyBstring, 0, -1, ""); + ret += test4_0 (&emptyBstring, 1, 3, ""); + ret += test4_0 (&shortBstring, 0, 0, ""); + ret += test4_0 (&shortBstring, 0, -1, ""); + ret += test4_0 (&shortBstring, 1, 3, "ogu"); + ret += test4_0 (&shortBstring, -1, 3, "bo"); + ret += test4_0 (&shortBstring, -1, 9, "bogus"); + ret += test4_0 (&shortBstring, 3, -1, ""); + ret += test4_0 (&shortBstring, 9, 3, ""); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test5_0 (bstring b0, const_bstring b1, const char * res) { +bstring b2; +int rv, ret = 0; + + if (b0 != NULL && b0->data != NULL && b0->slen >= 0 && + b1 != NULL && b1->data != NULL && b1->slen >= 0 ) { + b2 = bstrcpy (b0); + bwriteprotect (*b2); + + printf (".\tbconcat (%s, ", dumpBstring (b2)); + + rv = bconcat (b2, b1); + ret += (rv == 0); + if (!biseq (b0, b2)) ret++; + + printf ("%s) = %s\n", dumpBstring (b1), dumpBstring (b2)); + + bwriteallow (*b2); + + printf (".\tbconcat (%s, ", dumpBstring (b2)); + + rv = bconcat (b2, b1); + + printf ("%s) = %s\n", dumpBstring (b1), dumpBstring (b2)); + + if (b1) ret += (b2->slen != b0->slen + b1->slen); + ret += ((0 != rv) && (b1 != NULL)) || ((0 == rv) && (b1 == NULL)); + ret += (res == NULL) || ((int) strlen (res) > b2->slen) + || (0 != memcmp (b2->data, res, b2->slen)); + ret += b2->data[b2->slen] != '\0'; + bdestroy (b2); + } else { + ret += (BSTR_ERR != (rv = bconcat (b0, b1))); + printf (".\tbconcat (%s, %s) = %d\n", dumpBstring (b0), dumpBstring (b1), rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test5_1 (void) { +bstring b, c; +struct tagbstring t; +int i, ret; + + printf ("TEST: bconcat aliasing\n"); + for (ret=i=0; i < longBstring.slen; i++) { + b = bstrcpy (&longBstring); + c = bstrcpy (&longBstring); + bmid2tbstr (t, b, i, longBstring.slen); + ret += 0 != bconcat (c, &t); + ret += 0 != bconcat (b, &t); + ret += !biseq (b, c); + bdestroy (b); + bdestroy (c); + } + + b = bfromcstr ("abcd"); + c = bfromcstr ("abcd"); + + for (ret=i=0; i < 100; i++) { + bmid2tbstr (t, b, 0, 3); + ret += 0 != bcatcstr (c, "abc"); + ret += 0 != bconcat (b, &t); + ret += !biseq (b, c); + } + + bdestroy (b); + bdestroy (c); + + if (ret) { + printf ("\t\talias failures(%d) = %d\n", __LINE__, ret); + } + + return ret; +} + +static int test5 (void) { +int ret = 0; + + printf ("TEST: int bconcat (bstring b0, const_bstring b1);\n"); + + /* tests with NULL */ + ret += test5_0 (NULL, NULL, NULL); + ret += test5_0 (NULL, &emptyBstring, NULL); + ret += test5_0 (&emptyBstring, NULL, ""); + ret += test5_0 (&emptyBstring, &badBstring1, NULL); + ret += test5_0 (&emptyBstring, &badBstring2, NULL); + ret += test5_0 (&badBstring1, &emptyBstring, NULL); + ret += test5_0 (&badBstring2, &emptyBstring, NULL); + + /* normal operation tests on all sorts of subranges */ + ret += test5_0 (&emptyBstring, &emptyBstring, ""); + ret += test5_0 (&emptyBstring, &shortBstring, "bogus"); + ret += test5_0 (&shortBstring, &emptyBstring, "bogus"); + ret += test5_0 (&shortBstring, &shortBstring, "bogusbogus"); + + ret += test5_1 (); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test6_0 (bstring b, char c, const char * res) { +bstring b0; +int rv, ret = 0; + + if (b != NULL && b->data != NULL && b->slen >= 0) { + b0 = bstrcpy (b); + bwriteprotect (*b0); + rv = bconchar (b0, c); + ret += (rv == 0); + if (!biseq (b0, b)) ret++; + + printf (".\tbconchar (%s, %c) = %s\n", dumpBstring (b), c, dumpBstring (b0)); + + bwriteallow (*b0); + rv = bconchar (b0, c); + ret += (0 != rv); + ret += (b0->slen != b->slen + 1); + ret += (res == NULL) || ((int) strlen (res) > b0->slen) + || (0 != memcmp (b0->data, res, b0->slen)); + ret += b0->data[b0->slen] != '\0'; + printf (".\tbconchar (%s, %c) = %s\n", dumpBstring (b), c, dumpBstring (b0)); + + bdestroy (b0); + } else { + ret += (BSTR_ERR != (rv = bconchar (b, c))); + printf (".\tbconchar (%s, %c) = %d\n", dumpBstring (b), c, rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test6 (void) { +int ret = 0; + + printf ("TEST: int bconchar (bstring b, char c);\n"); + + /* tests with NULL */ + ret += test6_0 (NULL, (char) 'x', NULL); + ret += test6_0 (&badBstring1, (char) 'x', NULL); + ret += test6_0 (&badBstring2, (char) 'x', NULL); + + /* normal operation tests on all sorts of subranges */ + ret += test6_0 (&emptyBstring, (char) 'x', "x"); + ret += test6_0 (&shortBstring, (char) 'x', "bogusx"); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test7x8_0 (char * fnname, int (* fnptr) (const struct tagbstring *, const struct tagbstring *), const struct tagbstring * b0, const struct tagbstring * b1, int res) { +int rv, ret = 0; + + ret += (res != (rv = fnptr (b0, b1))); + printf (".\t%s (%s, %s) = %d\n", fnname, dumpBstring (b0), dumpBstring (b1), rv); + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %d)\n", __LINE__, ret, res); + } + return ret; +} + +static int test7x8 (char * fnname, int (* fnptr) (const struct tagbstring *, const struct tagbstring *), + int retFail, int retLT, int retGT, int retEQ) { +int ret = 0; + + printf ("TEST: int %s (const_bstring b0, const_bstring b1);\n", fnname); + + /* tests with NULL */ + ret += test7x8_0 (fnname, fnptr, NULL, NULL, retFail); + ret += test7x8_0 (fnname, fnptr, &emptyBstring, NULL, retFail); + ret += test7x8_0 (fnname, fnptr, NULL, &emptyBstring, retFail); + ret += test7x8_0 (fnname, fnptr, &shortBstring, NULL, retFail); + ret += test7x8_0 (fnname, fnptr, NULL, &shortBstring, retFail); + ret += test7x8_0 (fnname, fnptr, &badBstring1, &badBstring1, retFail); + ret += test7x8_0 (fnname, fnptr, &badBstring2, &badBstring2, retFail); + ret += test7x8_0 (fnname, fnptr, &shortBstring, &badBstring2, retFail); + ret += test7x8_0 (fnname, fnptr, &badBstring2, &shortBstring, retFail); + + /* normal operation tests on all sorts of subranges */ + ret += test7x8_0 (fnname, fnptr, &emptyBstring, &emptyBstring, retEQ); + ret += test7x8_0 (fnname, fnptr, &shortBstring, &emptyBstring, retGT); + ret += test7x8_0 (fnname, fnptr, &emptyBstring, &shortBstring, retLT); + ret += test7x8_0 (fnname, fnptr, &shortBstring, &shortBstring, retEQ); + + { + bstring b = bstrcpy (&shortBstring); + b->data[1]++; + ret += test7x8_0 (fnname, fnptr, b, &shortBstring, retGT); + bdestroy (b); + } + + if (fnptr == biseq) { + ret += test7x8_0 (fnname, fnptr, &shortBstring, &longBstring, retGT); + ret += test7x8_0 (fnname, fnptr, &longBstring, &shortBstring, retLT); + } else { + ret += test7x8_0 (fnname, fnptr, &shortBstring, &longBstring, 'b'-'T'); + ret += test7x8_0 (fnname, fnptr, &longBstring, &shortBstring, 'T'-'b'); + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +#define test7() test7x8 ("biseq", biseq, -1, 0, 0, 1) +#define test8() test7x8 ("bstrcmp", bstrcmp, SHRT_MIN, -1, 1, 0) + +static int test47_0 (const struct tagbstring* b, const unsigned char* blk, int len, int res) { +int rv, ret = 0; + + ret += (res != (rv = biseqblk (b, blk, len))); + printf (".\tbiseqblk (%s, %s) = %d\n", dumpBstring (b), dumpCstring (blk), rv); + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %d)\n", __LINE__, ret, res); + } + return ret; +} + +static int test47 (void) { +int ret = 0; + + printf ("TEST: int biseqblk (const_bstring b, const void * blk, int len);\n"); + + /* tests with NULL */ + ret += test47_0 (NULL, NULL, 0, -1); + ret += test47_0 (&emptyBstring, NULL, 0, -1); + ret += test47_0 (NULL, emptyBstring.data, 0, -1); + ret += test47_0 (&shortBstring, NULL, shortBstring.slen, -1); + ret += test47_0 (NULL, shortBstring.data, 0, -1); + ret += test47_0 (&badBstring1, badBstring1.data, badBstring1.slen, -1); + ret += test47_0 (&badBstring2, badBstring2.data, badBstring2.slen, -1); + ret += test47_0 (&shortBstring, badBstring2.data, badBstring2.slen, -1); + ret += test47_0 (&badBstring2, shortBstring.data, shortBstring.slen, -1); + + /* normal operation tests on all sorts of subranges */ + ret += test47_0 (&emptyBstring, emptyBstring.data, emptyBstring.slen, 1); + ret += test47_0 (&shortBstring, emptyBstring.data, emptyBstring.slen, 0); + ret += test47_0 (&emptyBstring, shortBstring.data, shortBstring.slen, 0); + ret += test47_0 (&shortBstring, shortBstring.data, shortBstring.slen, 1); + + { + bstring b = bstrcpy (&shortBstring); + b->data[1]++; + ret += test47_0 (b, shortBstring.data, shortBstring.slen, 0); + bdestroy (b); + } + ret += test47_0 (&shortBstring, longBstring.data, longBstring.slen, 0); + ret += test47_0 (&longBstring, shortBstring.data, shortBstring.slen, 0); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test9_0 (const_bstring b0, const_bstring b1, int n, int res) { +int rv, ret = 0; + + ret += (res != (rv = bstrncmp (b0, b1, n))); + printf (".\tbstrncmp (%s, %s, %d) = %d\n", dumpBstring (b0), dumpBstring (b1), n, rv); + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %d)\n", __LINE__, ret, res); + } + return ret; +} + +static int test9 (void) { +int ret = 0; + + printf ("TEST: int bstrncmp (const_bstring b0, const_bstring b1, int n);\n"); + + /* tests with NULL */ + ret += test9_0 (NULL, NULL, 0, SHRT_MIN); + ret += test9_0 (NULL, NULL, -1, SHRT_MIN); + ret += test9_0 (NULL, NULL, 1, SHRT_MIN); + ret += test9_0 (&emptyBstring, NULL, 0, SHRT_MIN); + ret += test9_0 (NULL, &emptyBstring, 0, SHRT_MIN); + ret += test9_0 (&emptyBstring, NULL, 1, SHRT_MIN); + ret += test9_0 (NULL, &emptyBstring, 1, SHRT_MIN); + ret += test9_0 (&badBstring1, &badBstring1, 1, SHRT_MIN); + ret += test9_0 (&badBstring2, &badBstring2, 1, SHRT_MIN); + ret += test9_0 (&emptyBstring, &badBstring1, 1, SHRT_MIN); + ret += test9_0 (&emptyBstring, &badBstring2, 1, SHRT_MIN); + ret += test9_0 (&badBstring1, &emptyBstring, 1, SHRT_MIN); + ret += test9_0 (&badBstring2, &emptyBstring, 1, SHRT_MIN); + + /* normal operation tests on all sorts of subranges */ + ret += test9_0 (&emptyBstring, &emptyBstring, -1, 0); + ret += test9_0 (&emptyBstring, &emptyBstring, 0, 0); + ret += test9_0 (&emptyBstring, &emptyBstring, 1, 0); + ret += test9_0 (&shortBstring, &shortBstring, -1, 0); + ret += test9_0 (&shortBstring, &shortBstring, 0, 0); + ret += test9_0 (&shortBstring, &shortBstring, 1, 0); + ret += test9_0 (&shortBstring, &shortBstring, 9, 0); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test10_0 (bstring b, int res, int nochange) { +struct tagbstring sb = bsStatic (""); +int rv, x, ret = 0; + + if (b) sb = *b; + printf (".\tbdestroy (%s) = ", dumpBstring (b)); + rv = bdestroy (b); + printf ("%d\n", rv); + + if (b != NULL) { + if (rv >= 0) + /* If the bdestroy was successful we have to assume + the contents were "changed" */ + x = 1; + else + x = memcmp (&sb, b, sizeof sb); + } else x = !nochange; + ret += (rv != res); + ret += (!nochange) == (!x); + if (ret) { + printf ("\t\tfailure(%d) res = %d nochange = %d, x = %d, sb.slen = %d, sb.mlen = %d, sb.data = %p\n", __LINE__, res, nochange, x, sb.slen, sb.mlen, sb.data); + } + return ret; +} + +static int test10 (void) { +bstring c = bstrcpy (&shortBstring); +bstring b = bstrcpy (&emptyBstring); +int ret = 0; + + printf ("TEST: int bdestroy (const_bstring b);\n"); + /* tests with NULL */ + ret += test10_0 (NULL, BSTR_ERR, 1); + + /* protected, constant and regular instantiations on empty or not */ + bwriteprotect (*b); + bwriteprotect (*c); + ret += test10_0 (b, BSTR_ERR, 1); + ret += test10_0 (c, BSTR_ERR, 1); + bwriteallow (*b); + bwriteallow (*c); + ret += test10_0 (b, BSTR_OK, 0); + ret += test10_0 (c, BSTR_OK, 0); + ret += test10_0 (&emptyBstring, BSTR_ERR, 1); + bwriteallow (emptyBstring); + ret += test10_0 (&emptyBstring, BSTR_ERR, 1); + ret += test10_0 (&shortBstring, BSTR_ERR, 1); + bwriteallow (emptyBstring); + ret += test10_0 (&shortBstring, BSTR_ERR, 1); + ret += test10_0 (&badBstring1, BSTR_ERR, 1); + ret += test10_0 (&badBstring2, BSTR_ERR, 1); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test11_0 (bstring s1, int pos, const_bstring s2, int res) { +int rv, ret = 0; + + printf (".\tbinstr (%s, %d, %s) = ", dumpBstring (s1), pos, dumpBstring (s2)); + rv = binstr (s1, pos, s2); + printf ("%d\n", rv); + ret += (rv != res); + if (ret) { + printf ("\t\tfailure(%d) res = %d\n", __LINE__, res); + } + return ret; +} + +static int test11_1 (bstring s1, int pos, const_bstring s2, int res) { +int rv, ret = 0; + + printf (".\tbinstrcaseless (%s, %d, %s) = ", dumpBstring (s1), pos, dumpBstring (s2)); + rv = binstrcaseless (s1, pos, s2); + printf ("%d\n", rv); + ret += (rv != res); + if (ret) { + printf ("\t\tfailure(%d) res = %d\n", __LINE__, res); + } + return ret; +} + +static int test11 (void) { +bstring b, c; +int ret = 0; + + printf ("TEST: int binstr (const_bstring s1, int pos, const_bstring s2);\n"); + ret += test11_0 (NULL, 0, NULL, BSTR_ERR); + ret += test11_0 (&emptyBstring, 0, NULL, BSTR_ERR); + ret += test11_0 (NULL, 0, &emptyBstring, BSTR_ERR); + ret += test11_0 (&emptyBstring, 0, &badBstring1, BSTR_ERR); + ret += test11_0 (&emptyBstring, 0, &badBstring2, BSTR_ERR); + ret += test11_0 (&badBstring1, 0, &emptyBstring, BSTR_ERR); + ret += test11_0 (&badBstring2, 0, &emptyBstring, BSTR_ERR); + ret += test11_0 (&badBstring1, 0, &badBstring2, BSTR_ERR); + ret += test11_0 (&badBstring2, 0, &badBstring1, BSTR_ERR); + + ret += test11_0 (&emptyBstring, 0, &emptyBstring, 0); + ret += test11_0 (&emptyBstring, 1, &emptyBstring, BSTR_ERR); + ret += test11_0 (&shortBstring, 1, &shortBstring, BSTR_ERR); + ret += test11_0 (&shortBstring, 5, &emptyBstring, 5); + ret += test11_0 (&shortBstring, -1, &shortBstring, BSTR_ERR); + ret += test11_0 (&shortBstring, 0, &shortBstring, 0); + ret += test11_0 (&shortBstring, 0, b = bstrcpy (&shortBstring), 0); + bdestroy (b); + ret += test11_0 (&shortBstring, 0, b = bfromcstr ("BOGUS"), BSTR_ERR); + bdestroy (b); + ret += test11_0 (&longBstring, 0, &shortBstring, 10); + ret += test11_0 (&longBstring, 20, &shortBstring, BSTR_ERR); + + ret += test11_0 (c = bfromcstr ("sssssssssap"), 0, b = bfromcstr ("sap"), 8); + bdestroy (c); + bdestroy (b); + ret += test11_0 (c = bfromcstr ("sssssssssap"), 3, b = bfromcstr ("sap"), 8); + bdestroy (c); + bdestroy (b); + ret += test11_0 (c = bfromcstr ("ssssssssssap"), 3, b = bfromcstr ("sap"), 9); + bdestroy (c); + bdestroy (b); + ret += test11_0 (c = bfromcstr ("sssssssssap"), 0, b = bfromcstr ("s"), 0); + bdestroy (c); + bdestroy (b); + ret += test11_0 (c = bfromcstr ("sssssssssap"), 3, b = bfromcstr ("s"), 3); + bdestroy (c); + bdestroy (b); + ret += test11_0 (c = bfromcstr ("sssssssssap"), 0, b = bfromcstr ("a"), 9); + bdestroy (c); + bdestroy (b); + ret += test11_0 (c = bfromcstr ("sssssssssap"), 5, b = bfromcstr ("a"), 9); + bdestroy (c); + bdestroy (b); + ret += test11_0 (c = bfromcstr ("sasasasasap"), 0, b = bfromcstr ("sap"), 8); + bdestroy (c); + bdestroy (b); + ret += test11_0 (c = bfromcstr ("ssasasasasap"), 0, b = bfromcstr ("sap"), 9); + bdestroy (c); + bdestroy (b); + + printf ("TEST: int binstrcaseless (const_bstring s1, int pos, const_bstring s2);\n"); + ret += test11_1 (NULL, 0, NULL, BSTR_ERR); + ret += test11_1 (&emptyBstring, 0, NULL, BSTR_ERR); + ret += test11_1 (NULL, 0, &emptyBstring, BSTR_ERR); + ret += test11_1 (&emptyBstring, 0, &badBstring1, BSTR_ERR); + ret += test11_1 (&emptyBstring, 0, &badBstring2, BSTR_ERR); + ret += test11_1 (&badBstring1, 0, &emptyBstring, BSTR_ERR); + ret += test11_1 (&badBstring2, 0, &emptyBstring, BSTR_ERR); + ret += test11_1 (&badBstring1, 0, &badBstring2, BSTR_ERR); + ret += test11_1 (&badBstring2, 0, &badBstring1, BSTR_ERR); + + ret += test11_1 (&emptyBstring, 0, &emptyBstring, 0); + ret += test11_1 (&emptyBstring, 1, &emptyBstring, BSTR_ERR); + ret += test11_1 (&shortBstring, 1, &shortBstring, BSTR_ERR); + ret += test11_1 (&shortBstring, 5, &emptyBstring, 5); + ret += test11_1 (&shortBstring, -1, &shortBstring, BSTR_ERR); + ret += test11_1 (&shortBstring, 0, &shortBstring, 0); + ret += test11_1 (&shortBstring, 0, b = bstrcpy (&shortBstring), 0); + bdestroy (b); + ret += test11_1 (&shortBstring, 0, b = bfromcstr ("BOGUS"), 0); + bdestroy (b); + ret += test11_1 (&longBstring, 0, &shortBstring, 10); + ret += test11_1 (&longBstring, 20, &shortBstring, BSTR_ERR); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test12_0 (bstring s1, int pos, const_bstring s2, int res) { +int rv, ret = 0; + + printf (".\tbinstrr (%s, %d, %s) = ", dumpBstring (s1), pos, dumpBstring (s2)); + rv = binstrr (s1, pos, s2); + printf ("%d\n", rv); + ret += (rv != res); + if (ret) { + printf ("\t\tfailure(%d) res = %d\n", __LINE__, res); + } + return ret; +} + +static int test12_1 (bstring s1, int pos, const_bstring s2, int res) { +int rv, ret = 0; + + printf (".\tbinstrrcaseless (%s, %d, %s) = ", dumpBstring (s1), pos, dumpBstring (s2)); + rv = binstrrcaseless (s1, pos, s2); + printf ("%d\n", rv); + ret += (rv != res); + if (ret) { + printf ("\t\tfailure(%d) res = %d\n", __LINE__, res); + } + return ret; +} + +static int test12 (void) { +bstring b; +int ret = 0; + + printf ("TEST: int binstrr (const_bstring s1, int pos, const_bstring s2);\n"); + ret += test12_0 (NULL, 0, NULL, BSTR_ERR); + ret += test12_0 (&emptyBstring, 0, NULL, BSTR_ERR); + ret += test12_0 (NULL, 0, &emptyBstring, BSTR_ERR); + ret += test12_0 (&emptyBstring, 0, &badBstring1, BSTR_ERR); + ret += test12_0 (&emptyBstring, 0, &badBstring2, BSTR_ERR); + ret += test12_0 (&badBstring1, 0, &emptyBstring, BSTR_ERR); + ret += test12_0 (&badBstring2, 0, &emptyBstring, BSTR_ERR); + ret += test12_0 (&badBstring1, 0, &badBstring2, BSTR_ERR); + ret += test12_0 (&badBstring2, 0, &badBstring1, BSTR_ERR); + + ret += test12_0 (&emptyBstring, 0, &emptyBstring, 0); + ret += test12_0 (&emptyBstring, 1, &emptyBstring, BSTR_ERR); + ret += test12_0 (&shortBstring, 1, &shortBstring, 0); + ret += test12_0 (&shortBstring, 5, &emptyBstring, 5); + ret += test12_0 (&shortBstring, -1, &shortBstring, BSTR_ERR); + ret += test12_0 (&shortBstring, 0, &shortBstring, 0); + ret += test12_0 (&shortBstring, 0, b = bstrcpy (&shortBstring), 0); + bdestroy (b); + ret += test12_0 (&shortBstring, 0, b = bfromcstr ("BOGUS"), BSTR_ERR); + bdestroy (b); + ret += test12_0 (&longBstring, 0, &shortBstring, BSTR_ERR); + ret += test12_0 (&longBstring, 20, &shortBstring, 10); + + printf ("TEST: int binstrrcaseless (const_bstring s1, int pos, const_bstring s2);\n"); + ret += test12_1 (NULL, 0, NULL, BSTR_ERR); + ret += test12_1 (&emptyBstring, 0, NULL, BSTR_ERR); + ret += test12_1 (NULL, 0, &emptyBstring, BSTR_ERR); + ret += test12_1 (&emptyBstring, 0, &badBstring1, BSTR_ERR); + ret += test12_1 (&emptyBstring, 0, &badBstring2, BSTR_ERR); + ret += test12_1 (&badBstring1, 0, &emptyBstring, BSTR_ERR); + ret += test12_1 (&badBstring2, 0, &emptyBstring, BSTR_ERR); + ret += test12_1 (&badBstring1, 0, &badBstring2, BSTR_ERR); + ret += test12_1 (&badBstring2, 0, &badBstring1, BSTR_ERR); + + ret += test12_1 (&emptyBstring, 0, &emptyBstring, 0); + ret += test12_1 (&emptyBstring, 1, &emptyBstring, BSTR_ERR); + ret += test12_1 (&shortBstring, 1, &shortBstring, 0); + ret += test12_1 (&shortBstring, 5, &emptyBstring, 5); + ret += test12_1 (&shortBstring, -1, &shortBstring, BSTR_ERR); + ret += test12_1 (&shortBstring, 0, &shortBstring, 0); + ret += test12_1 (&shortBstring, 0, b = bstrcpy (&shortBstring), 0); + bdestroy (b); + ret += test12_1 (&shortBstring, 0, b = bfromcstr ("BOGUS"), 0); + bdestroy (b); + ret += test12_1 (&longBstring, 0, &shortBstring, BSTR_ERR); + ret += test12_1 (&longBstring, 20, &shortBstring, 10); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test13_0 (bstring s1, int pos, const_bstring s2, int res) { +int rv, ret = 0; + + printf (".\tbinchr (%s, %d, %s) = ", dumpBstring (s1), pos, dumpBstring (s2)); + rv = binchr (s1, pos, s2); + printf ("%d\n", rv); + ret += (rv != res); + if (ret) { + printf ("\t\tfailure(%d) res = %d\n", __LINE__, res); + } + return ret; +} + +static int test13 (void) { +bstring b; +int ret = 0; +struct tagbstring multipleOs = bsStatic ("ooooo"); + + printf ("TEST: int binchr (const_bstring s1, int pos, const_bstring s2);\n"); + ret += test13_0 (NULL, 0, NULL, BSTR_ERR); + ret += test13_0 (&emptyBstring, 0, NULL, BSTR_ERR); + ret += test13_0 (NULL, 0, &emptyBstring, BSTR_ERR); + ret += test13_0 (&emptyBstring, 0, &badBstring1, BSTR_ERR); + ret += test13_0 (&emptyBstring, 0, &badBstring2, BSTR_ERR); + ret += test13_0 (&badBstring1, 0, &emptyBstring, BSTR_ERR); + ret += test13_0 (&badBstring2, 0, &emptyBstring, BSTR_ERR); + ret += test13_0 (&badBstring2, 0, &badBstring1, BSTR_ERR); + ret += test13_0 (&badBstring1, 0, &badBstring2, BSTR_ERR); + + ret += test13_0 (&emptyBstring, 0, &emptyBstring, BSTR_ERR); + ret += test13_0 (&shortBstring, 0, &emptyBstring, BSTR_ERR); + ret += test13_0 (&shortBstring, 0, &shortBstring, 0); + ret += test13_0 (&shortBstring, 0, &multipleOs, 1); + ret += test13_0 (&shortBstring, 0, b = bstrcpy (&shortBstring), 0); + bdestroy (b); + ret += test13_0 (&shortBstring, -1, &shortBstring, BSTR_ERR); + ret += test13_0 (&shortBstring, 10, &shortBstring, BSTR_ERR); + ret += test13_0 (&shortBstring, 1, &shortBstring, 1); + ret += test13_0 (&emptyBstring, 0, &shortBstring, BSTR_ERR); + ret += test13_0 (&xxxxxBstring, 0, &shortBstring, BSTR_ERR); + ret += test13_0 (&longBstring, 0, &shortBstring, 3); + ret += test13_0 (&longBstring, 10, &shortBstring, 10); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test14_0 (bstring s1, int pos, const_bstring s2, int res) { +int rv, ret = 0; + + printf (".\tbinchrr (%s, %d, %s) = ", dumpBstring (s1), pos, dumpBstring (s2)); + rv = binchrr (s1, pos, s2); + printf ("%d\n", rv); + ret += (rv != res); + if (ret) { + printf ("\t\tfailure(%d) res = %d\n", __LINE__, res); + } + return ret; +} + +static int test14 (void) { +bstring b; +int ret = 0; + + printf ("TEST: int binchrr (const_bstring s1, int pos, const_bstring s2);\n"); + ret += test14_0 (NULL, 0, NULL, BSTR_ERR); + ret += test14_0 (&emptyBstring, 0, NULL, BSTR_ERR); + ret += test14_0 (NULL, 0, &emptyBstring, BSTR_ERR); + ret += test14_0 (&emptyBstring, 0, &emptyBstring, BSTR_ERR); + ret += test14_0 (&shortBstring, 0, &emptyBstring, BSTR_ERR); + ret += test14_0 (&emptyBstring, 0, &badBstring1, BSTR_ERR); + ret += test14_0 (&emptyBstring, 0, &badBstring2, BSTR_ERR); + ret += test14_0 (&badBstring1, 0, &emptyBstring, BSTR_ERR); + ret += test14_0 (&badBstring2, 0, &emptyBstring, BSTR_ERR); + ret += test14_0 (&badBstring2, 0, &badBstring1, BSTR_ERR); + ret += test14_0 (&badBstring1, 0, &badBstring2, BSTR_ERR); + + ret += test14_0 (&shortBstring, 0, &shortBstring, 0); + ret += test14_0 (&shortBstring, 0, b = bstrcpy (&shortBstring), 0); + bdestroy (b); + ret += test14_0 (&shortBstring, -1, &shortBstring, BSTR_ERR); + ret += test14_0 (&shortBstring, 5, &shortBstring, 4); + ret += test14_0 (&shortBstring, 4, &shortBstring, 4); + ret += test14_0 (&shortBstring, 1, &shortBstring, 1); + ret += test14_0 (&emptyBstring, 0, &shortBstring, BSTR_ERR); + ret += test14_0 (&xxxxxBstring, 4, &shortBstring, BSTR_ERR); + ret += test14_0 (&longBstring, 0, &shortBstring, BSTR_ERR); + ret += test14_0 (&longBstring, 10, &shortBstring, 10); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test15_0 (bstring b0, int pos, const_bstring b1, unsigned char fill, char * res) { +bstring b2; +int rv, ret = 0, linenum = 0; + + if (b0 != NULL && b0->data != NULL && b0->slen >= 0 && + b1 != NULL && b1->data != NULL && b1->slen >= 0) { + b2 = bstrcpy (b0); + bwriteprotect (*b2); + + printf (".\tbsetstr (%s, ", dumpBstring (b2)); + + rv = bsetstr (b2, pos, b1, fill); + ret += (rv == 0); if (ret && 0 == linenum) linenum = __LINE__; + if (!biseq (b0, b2)) ret++; if (ret && 0 == linenum) linenum = __LINE__; + + printf ("%d, %s, %02X) = %s\n", pos, dumpBstring (b1), fill, dumpBstring (b2)); + + bwriteallow (*b2); + + printf (".\tbsetstr (%s, ", dumpBstring (b2)); + + rv = bsetstr (b2, pos, b1, fill); + if (b1) { + ret += (pos >= 0) && (b2->slen != b0->slen + b1->slen) && (b2->slen != pos + b1->slen); if (ret && 0 == linenum) linenum = __LINE__; + ret += (pos < 0) && (b2->slen != b0->slen); if (ret && 0 == linenum) linenum = __LINE__; + } + + ret += ((rv == 0) != (pos >= 0)); if (ret && 0 == linenum) linenum = __LINE__; + ret += (res == NULL); if (ret && 0 == linenum) linenum = __LINE__; + ret += ((int) strlen (res) > b2->slen); if (ret && 0 == linenum) linenum = __LINE__; + ret += (0 != memcmp (b2->data, res, b2->slen)); if (ret && 0 == linenum) linenum = __LINE__; + ret += b2->data[b2->slen] != '\0'; if (ret && 0 == linenum) linenum = __LINE__; + + printf ("%d, %s, %02X) = %s\n", pos, dumpBstring (b1), fill, dumpBstring (b2)); + + bdestroy (b2); + } else { + ret += (BSTR_ERR != (rv = bsetstr (b0, pos, b1, fill))); if (ret && 0 == linenum) linenum = __LINE__; + printf (".\tbsetstr (%s, %d, %s, %02X) = %d\n", dumpBstring (b0), pos, dumpBstring (b1), fill, rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", linenum, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test15 (void) { +int ret = 0; + printf ("TEST: int bsetstr (bstring b0, int pos, const_bstring b1, unsigned char fill);\n"); + /* tests with NULL */ + ret += test15_0 (NULL, 0, NULL, (unsigned char) '?', NULL); + ret += test15_0 (NULL, 0, &emptyBstring, (unsigned char) '?', NULL); + ret += test15_0 (&badBstring1, 0, NULL, (unsigned char) '?', NULL); + ret += test15_0 (&badBstring1, 0, &badBstring1, (unsigned char) '?', NULL); + ret += test15_0 (&emptyBstring, 0, &badBstring1, (unsigned char) '?', NULL); + ret += test15_0 (&badBstring1, 0, &emptyBstring, (unsigned char) '?', NULL); + ret += test15_0 (&badBstring2, 0, NULL, (unsigned char) '?', NULL); + ret += test15_0 (&badBstring2, 0, &badBstring2, (unsigned char) '?', NULL); + ret += test15_0 (&emptyBstring, 0, &badBstring2, (unsigned char) '?', NULL); + ret += test15_0 (&badBstring2, 0, &emptyBstring, (unsigned char) '?', NULL); + + /* normal operation tests */ + ret += test15_0 (&emptyBstring, 0, &emptyBstring, (unsigned char) '?', ""); + ret += test15_0 (&emptyBstring, 5, &emptyBstring, (unsigned char) '?', "?????"); + ret += test15_0 (&emptyBstring, 5, &shortBstring, (unsigned char) '?', "?????bogus"); + ret += test15_0 (&shortBstring, 0, &emptyBstring, (unsigned char) '?', "bogus"); + ret += test15_0 (&emptyBstring, 0, &shortBstring, (unsigned char) '?', "bogus"); + ret += test15_0 (&shortBstring, 0, &shortBstring, (unsigned char) '?', "bogus"); + ret += test15_0 (&shortBstring, -1, &shortBstring, (unsigned char) '?', "bogus"); + ret += test15_0 (&shortBstring, 2, &shortBstring, (unsigned char) '?', "bobogus"); + ret += test15_0 (&shortBstring, 6, &shortBstring, (unsigned char) '?', "bogus?bogus"); + ret += test15_0 (&shortBstring, 6, NULL, (unsigned char) '?', "bogus?"); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test16_0 (bstring b0, int pos, const_bstring b1, unsigned char fill, char * res) { +bstring b2; +int rv, ret = 0; + + if (b0 != NULL && b0->data != NULL && b0->slen >= 0 && + b1 != NULL && b1->data != NULL && b1->slen >= 0) { + b2 = bstrcpy (b0); + bwriteprotect (*b2); + + printf (".\tbinsert (%s, ", dumpBstring (b2)); + + rv = binsert (b2, pos, b1, fill); + ret += (rv == 0); + if (!biseq (b0, b2)) ret++; + + printf ("%d, %s, %02X) = %s\n", pos, dumpBstring (b1), fill, dumpBstring (b2)); + + bwriteallow (*b2); + + printf (".\tbinsert (%s, ", dumpBstring (b2)); + + rv = binsert (b2, pos, b1, fill); + if (b1) { + ret += (pos >= 0) && (b2->slen != b0->slen + b1->slen) && (b2->slen != pos + b1->slen); + ret += (pos < 0) && (b2->slen != b0->slen); + ret += ((rv == 0) != (pos >= 0 && pos <= b2->slen)); + } + + ret += (res == NULL) || ((int) strlen (res) > b2->slen) + || (0 != memcmp (b2->data, res, b2->slen)); + ret += b2->data[b2->slen] != '\0'; + + printf ("%d, %s, %02X) = %s\n", pos, dumpBstring (b1), fill, dumpBstring (b2)); + + bdestroy (b2); + } else { + ret += (BSTR_ERR != (rv = binsert (b0, pos, b1, fill))); + printf (".\tbinsert (%s, %d, %s, %02X) = %d\n", dumpBstring (b0), pos, dumpBstring (b1), fill, rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test16_1 (void) { +bstring b0 = bfromStatic ("aaaaabbbbb"); +struct tagbstring b1; +int res, ret = 0; + + bmid2tbstr (b1, b0, 4, 4); + b0->slen = 6; + + printf (".\tbinsert (%s, 2, %s, '?') = ", dumpBstring (b0), dumpBstring (&b1)); + res = binsert (b0, 2, &b1, '?'); + printf ("%s (Alias test)\n", dumpBstring (b0)); + + ret += (res != 0); + ret += !biseqStatic(b0, "aaabbbaaab"); + + return ret; +} + +static int test16 (void) { +int ret = 0; + printf ("TEST: int binsert (bstring b0, int pos, const_bstring b1, unsigned char fill);\n"); + /* tests with NULL */ + ret += test16_0 (NULL, 0, NULL, (unsigned char) '?', NULL); + ret += test16_0 (NULL, 0, &emptyBstring, (unsigned char) '?', NULL); + ret += test16_0 (&badBstring1, 0, NULL, (unsigned char) '?', NULL); + ret += test16_0 (&badBstring1, 0, &badBstring1, (unsigned char) '?', NULL); + ret += test16_0 (&emptyBstring, 0, &badBstring1, (unsigned char) '?', NULL); + ret += test16_0 (&badBstring1, 0, &emptyBstring, (unsigned char) '?', NULL); + ret += test16_0 (&badBstring2, 0, NULL, (unsigned char) '?', NULL); + ret += test16_0 (&badBstring2, 0, &badBstring2, (unsigned char) '?', NULL); + ret += test16_0 (&emptyBstring, 0, &badBstring2, (unsigned char) '?', NULL); + ret += test16_0 (&badBstring2, 0, &emptyBstring, (unsigned char) '?', NULL); + + /* normal operation tests */ + ret += test16_0 (&emptyBstring, 0, &emptyBstring, (unsigned char) '?', ""); + ret += test16_0 (&emptyBstring, 5, &emptyBstring, (unsigned char) '?', "?????"); + ret += test16_0 (&emptyBstring, 5, &shortBstring, (unsigned char) '?', "?????bogus"); + ret += test16_0 (&shortBstring, 0, &emptyBstring, (unsigned char) '?', "bogus"); + ret += test16_0 (&emptyBstring, 0, &shortBstring, (unsigned char) '?', "bogus"); + ret += test16_0 (&shortBstring, 0, &shortBstring, (unsigned char) '?', "bogusbogus"); + ret += test16_0 (&shortBstring, -1, &shortBstring, (unsigned char) '?', "bogus"); + ret += test16_0 (&shortBstring, 2, &shortBstring, (unsigned char) '?', "bobogusgus"); + ret += test16_0 (&shortBstring, 6, &shortBstring, (unsigned char) '?', "bogus?bogus"); + ret += test16_0 (&shortBstring, 6, NULL, (unsigned char) '?', "bogus"); + + /* Alias testing */ + ret += test16_1 (); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test17_0 (bstring s1, int pos, int len, char * res) { +bstring b2; +int rv, ret = 0; + + if (s1 != NULL && s1->data != NULL && s1->slen >= 0) { + b2 = bstrcpy (s1); + bwriteprotect (*b2); + + printf (".\tbdelete (%s, ", dumpBstring (b2)); + + rv = bdelete (b2, pos, len); + ret += (rv == 0); + if (!biseq (s1, b2)) ret++; + + printf ("%d, %d) = %s\n", pos, len, dumpBstring (b2)); + + bwriteallow (*b2); + + printf (".\tbdelete (%s, ", dumpBstring (b2)); + + rv = bdelete (b2, pos, len); + ret += (len >= 0) != (rv == 0); + ret += (b2->slen > s1->slen) || (b2->slen < pos && s1->slen >= pos); + + ret += (res == NULL) || ((int) strlen (res) > b2->slen) + || (0 != memcmp (b2->data, res, b2->slen)); + ret += b2->data[b2->slen] != '\0'; + + printf ("%d, %d) = %s\n", pos, len, dumpBstring (b2)); + + bdestroy (b2); + } else { + ret += (BSTR_ERR != (rv = bdelete (s1, pos, len))); + printf (".\tbdelete (%s, %d, %d) = %d\n", dumpBstring (s1), pos, len, rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test17 (void) { +int ret = 0; + printf ("TEST: int bdelete (bstring s1, int pos, int len);\n"); + /* tests with NULL */ + ret += test17_0 (NULL, 0, 0, NULL); + ret += test17_0 (&badBstring1, 0, 0, NULL); + ret += test17_0 (&badBstring2, 0, 0, NULL); + + /* normal operation tests */ + ret += test17_0 (&emptyBstring, 0, 0, ""); + ret += test17_0 (&shortBstring, 1, 3, "bs"); + ret += test17_0 (&shortBstring, -1, 3, "gus"); + ret += test17_0 (&shortBstring, 1, -3, "bogus"); + ret += test17_0 (&shortBstring, 3, 9, "bog"); + ret += test17_0 (&shortBstring, 3, 1, "bogs"); + ret += test17_0 (&longBstring, 4, 300, "This"); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test18_0 (bstring b, int len, int res, int mlen) { +int ret = 0; +int rv; +int ol = 0; + + printf (".\tballoc (%s, %d) = ", dumpBstring (b), len); + if (b) ol = b->mlen; + rv = balloc (b, len); + printf ("%d\n", rv); + + if (b != NULL && b->data != NULL && b->slen >=0 && ol > b->mlen) { + printf ("\t\tfailure(%d) oldmlen = %d, newmlen %d\n", __LINE__, ol, b->mlen); + ret++; + } + + if (rv != res) { + printf ("\t\tfailure(%d) res = %d\n", __LINE__, res); + ret++; + } + if (b != NULL && (mlen > b->mlen || b->mlen == 0)) { + printf ("\t\tfailure(%d) b->mlen = %d mlen = %d\n", __LINE__, b->mlen, mlen); + ret++; + } + return ret; +} + +static int test18_1_int (bstring b, int len, int res, int mlen, int __line__) { +int ret = 0; +int rv; +int ol = 0; + + printf (".\tballocmin (%s, %d) = ", dumpBstring (b), len); + if (b) ol = b->mlen; + + rv = ballocmin (b, len); + printf ("[%d] %d\n", __LINE__, rv); + + if (b != NULL && b->data != NULL && b->mlen != mlen) { + printf ("\t\t[%d] failure(%d) oldmlen = %d, newmlen = %d, mlen = %d len = %d\n", __line__, __LINE__, ol, b->mlen, mlen, b->slen); + ret++; + } + + if (rv != res) { + printf ("\t\t[%d] failure(%d) res = %d\n", __line__, __LINE__, res); + ret++; + } + + return ret; +} + +#define test18_1(b, len, res, mlen) test18_1_int (b, len, res, mlen, __LINE__) + +static int test18 (void) { +int ret = 0, reto; +bstring b = bfromcstr ("test"); + + printf ("TEST: int balloc (bstring s, int len);\n"); + /* tests with NULL */ + ret += test18_0 (NULL, 2, BSTR_ERR, 0); + ret += test18_0 (&badBstring1, 2, BSTR_ERR, 0); + ret += test18_0 (&badBstring2, 2, BSTR_ERR, 0); + + /* normal operation tests */ + ret += test18_0 (b, 2, 0, b->mlen); + ret += test18_0 (b, -1, BSTR_ERR, b->mlen); + ret += test18_0 (b, 9, 0, 9); + ret += test18_0 (b, 2, 0, 9); + bwriteprotect (*b); + ret += test18_0 (b, 4, BSTR_ERR, b->mlen); + bwriteallow (*b); + ret += test18_0 (b, 2, 0, b->mlen); + ret += test18_0 (&emptyBstring, 9, BSTR_ERR, emptyBstring.mlen); + + bdestroy (b); + printf ("\t# failures: %d\n", ret); + + reto = ret; + ret = 0; + + b = bfromcstr ("test"); + + printf ("TEST: int ballocmin (bstring s, int len);\n"); + /* tests with NULL */ + ret += test18_1 (NULL, 2, BSTR_ERR, 0); + ret += test18_1 (&badBstring1, 2, BSTR_ERR, 0); + ret += test18_1 (&badBstring2, 2, BSTR_ERR, 2); + + /* normal operation tests */ + ret += test18_1 (b, 2, 0, b->slen + 1); + ret += test18_1 (b, -1, BSTR_ERR, b->mlen); + ret += test18_1 (b, 9, 0, 9); + ret += test18_1 (b, 2, 0, b->slen + 1); + ret += test18_1 (b, 9, 0, 9); + bwriteprotect (*b); + ret += test18_1 (b, 4, BSTR_ERR, -1); + bwriteallow (*b); + ret += test18_1 (b, 2, 0, b->slen + 1); + ret += test18_1 (&emptyBstring, 9, BSTR_ERR, emptyBstring.mlen); + + bdestroy (b); + printf ("\t# failures: %d\n", ret); + + return reto + ret; +} + +static int test19_0 (bstring b, int len, const char * res, int erv) { +int rv, ret = 0; +bstring b1; + + if (b != NULL && b->data != NULL && b->slen >= 0) { + b1 = bstrcpy (b); + bwriteprotect (*b1); + ret += bpattern (b1, len) != BSTR_ERR; + ret += !biseq (b1, b); + bwriteallow (*b1); + + printf (".\tbpattern (%s, %d) = ", dumpBstring (b1), len); + + rv = bpattern (b1, len); + + printf ("%s\n", dumpBstring (b1)); + + ret += (rv != erv); + ret += (res == NULL) || ((int) strlen (res) > b1->slen) + || (0 != memcmp (b1->data, res, b1->slen)); + ret += b1->data[b1->slen] != '\0'; + } else { + ret += BSTR_ERR != (rv = bpattern (b, len)); + printf (".\tbpattern (%s, %d) = %d\n", dumpBstring (b), len, rv); + } + + if (ret) { + printf ("\t\tfailure(%d) rv = %d erv = %d (res = %p", __LINE__, rv, erv, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test19 (void) { +int ret = 0; + + printf ("TEST: int bpattern (bstring b, int len);\n"); + /* tests with NULL */ + ret += test19_0 (NULL, 0, NULL, BSTR_ERR); + ret += test19_0 (NULL, 5, NULL, BSTR_ERR); + ret += test19_0 (NULL, -5, NULL, BSTR_ERR); + ret += test19_0 (&badBstring1, 5, NULL, BSTR_ERR); + ret += test19_0 (&badBstring2, 5, NULL, BSTR_ERR); + + /* normal operation tests */ + ret += test19_0 (&emptyBstring, 0, "", BSTR_ERR); + ret += test19_0 (&emptyBstring, 10, "", BSTR_ERR); + ret += test19_0 (&emptyBstring, -1, "", BSTR_ERR); + ret += test19_0 (&shortBstring, 0, "", 0); + ret += test19_0 (&shortBstring, 12, "bogusbogusbo", 0); + ret += test19_0 (&shortBstring, -1, "bogus", BSTR_ERR); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test20 (void) { +int ret = 0; + +#if !defined (BSTRLIB_NOVSNP) +int rv; +bstring b, c; + + printf ("TEST: bstring bformat (const char * fmt, ...);\n"); + /* tests with NULL */ + printf (".\tbformat (NULL, 1, 2) = "); + b = bformat (NULL, 1, 2); + printf ("%s\n", dumpBstring (b)); + ret += b != NULL; + + /* normal operation tests */ + printf (".\tbformat (\"%%d %%s\", 1, \"xy\") = "); + b = bformat ("%d %s", 1, "xy"); + printf ("%s\n", dumpBstring (b)); + ret += !biseq (c = bfromcstr ("1 xy"), b); + bdestroy (b); + + printf (".\tbformat (\"%%d %%s(%%s)\", 6, %s, %s) = ", dumpBstring (c), dumpBstring (&shortBstring)); + b = bformat ("%d %s(%s)", 6, c->data, shortBstring.data); + printf ("%s\n", dumpBstring (b)); + bdestroy (c); + ret += !biseq (c = bfromcstr ("6 1 xy(bogus)"), b); + bdestroy (c); + bdestroy (b); + + printf (".\tbformat (\"%%s%%s%%s%%s%%s%%s%%s%%s\", ...) ...\n"); + b = bformat ("%s%s%s%s%s%s%s%s", longBstring.data, longBstring.data + , longBstring.data, longBstring.data + , longBstring.data, longBstring.data + , longBstring.data, longBstring.data); + c = bstrcpy (&longBstring); + bconcat (c, c); + bconcat (c, c); + bconcat (c, c); + ret += !biseq (c, b); + bdestroy (c); + bdestroy (b); + + printf ("\t# failures: %d\n", ret); + + b = bfromcstr (""); + printf ("TEST: int bformata (bstring b, const char * fmt, ...);\n"); + /* tests with NULL */ + printf (".\tbformata (%s, NULL, 1, 2) = ", dumpBstring (b)); + rv = bformata (b, NULL, 1, 2); + printf ("%d\n", rv); + ret += rv != BSTR_ERR; + printf (".\tbformata (%s, \"%%d %%d\", 1, 2) = ", dumpBstring (&badBstring1)); + rv = bformata (&badBstring1, "%d %d", 1, 2); + printf ("%d\n", rv); + ret += rv != BSTR_ERR; + printf (".\tbformata (%s, \"%%d %%d\", 1, 2) = ", dumpBstring (b)); + rv = bformata (b, "%d %d", 1, 2); + printf ("%s\n", dumpBstring (b)); + ret += !biseq (c = bfromcstr ("1 2"), b); + bdestroy (c); + bdestroy (b); + + printf (".\tbformata (\"x\", \"%%s%%s%%s%%s%%s%%s%%s%%s\", ...) ...\n"); + rv = bformata (b = bfromcstr ("x"), "%s%s%s%s%s%s%s%s", + longBstring.data, longBstring.data, + longBstring.data, longBstring.data, + longBstring.data, longBstring.data, + longBstring.data, longBstring.data); + ret += rv == BSTR_ERR; + c = bstrcpy (&longBstring); + bconcat (c, c); + bconcat (c, c); + bconcat (c, c); + binsertch (c, 0, 1, (char) 'x'); + ret += !biseq (c, b); + bdestroy (c); + bdestroy (b); + + printf ("\t# failures: %d\n", ret); + + b = bfromcstr ("Initial"); + printf ("TEST: int bassignformat (bstring b, const char * fmt, ...);\n"); + /* tests with NULL */ + printf (".\tbassignformat (%s, NULL, 1, 2) = ", dumpBstring (b)); + rv = bassignformat (b, NULL, 1, 2); + printf ("%d\n", rv); + ret += rv != BSTR_ERR; + printf (".\tbassignformat (%s, \"%%d %%d\", 1, 2) = ", dumpBstring (&badBstring1)); + rv = bassignformat (&badBstring1, "%d %d", 1, 2); + printf ("%d\n", rv); + ret += rv != BSTR_ERR; + printf (".\tbassignformat (%s, \"%%d %%d\", 1, 2) = ", dumpBstring (b)); + rv = bassignformat (b, "%d %d", 1, 2); + printf ("%s\n", dumpBstring (b)); + ret += !biseq (c = bfromcstr ("1 2"), b); + bdestroy (c); + bdestroy (b); + + printf (".\tbassignformat (\"x\", \"%%s%%s%%s%%s%%s%%s%%s%%s\", ...) ...\n"); + rv = bassignformat (b = bfromcstr ("x"), "%s%s%s%s%s%s%s%s", + longBstring.data, longBstring.data, + longBstring.data, longBstring.data, + longBstring.data, longBstring.data, + longBstring.data, longBstring.data); + ret += rv == BSTR_ERR; + c = bstrcpy (&longBstring); + bconcat (c, c); + bconcat (c, c); + bconcat (c, c); + ret += !biseq (c, b); + bdestroy (c); + bdestroy (b); + + printf ("\t# failures: %d\n", ret); +#endif + + return ret; +} + +static int test21_0 (bstring b, char sc, int ns) { +struct bstrList * l; +int ret = 0; + + printf (".\tbsplit (%s, '%c') = ", dumpBstring (b), sc); + + if (b != NULL && b->data != NULL && b->slen >= 0) { + bstring c; + struct tagbstring t; + + blk2tbstr(t,&sc,1); + + printf ("{"); + + l = bsplit (b, sc); + + if (l) { + int i; + for (i=0; i < l->qty; i++) { + if (i != 0) printf (", "); + printf ("%s", dumpBstring (l->entry[i])); + } + printf (":<%d>", l->qty); + if (ns != l->qty) ret++; + } else { + printf ("NULL"); + ret ++; + } + + printf ("}\n"); + + c = bjoin (l, &t); + ret += !biseq (c, b); + ret += incorrectBstring (c); + bdestroy (c); + ret += 0 != bstrListDestroy (l); + } else { + l = bsplit (b, sc); + ret += (l != NULL); + printf ("%p\n", (void *) l); + } + + if (ret) { + printf ("\t\tfailure(%d) ns = %d\n", __LINE__, ns); + } + + return ret; +} + +static int test21_1 (bstring b, const_bstring sc, int ns) { +struct bstrList * l; +int ret = 0; + + printf (".\tbsplitstr (%s, %s) = ", dumpBstring (b), dumpBstring (sc)); + + if (b != NULL && b->data != NULL && b->slen >= 0) { + bstring c; + + printf ("{"); + + l = bsplitstr (b, sc); + + if (l) { + int i; + for (i=0; i < l->qty; i++) { + if (i != 0) printf (", "); + printf ("%s", dumpBstring (l->entry[i])); + } + printf (":<%d>", l->qty); + if (ns != l->qty) ret++; + } else { + printf ("NULL"); + ret ++; + } + + printf ("}\n"); + + c = bjoin (l, sc); + ret += !biseq (c, b); + ret += incorrectBstring (c); + bdestroy (c); + ret += 0 != bstrListDestroy (l); + } else { + l = bsplitstr (b, sc); + ret += (l != NULL); + printf ("%p\n", (void *) l); + } + + if (ret) { + printf ("\t\tfailure(%d) ns = %d\n", __LINE__, ns); + } + + return ret; +} + +static int test21 (void) { +struct tagbstring is = bsStatic ("is"); +struct tagbstring ng = bsStatic ("ng"); +struct tagbstring commas = bsStatic (",,,,"); +int ret = 0; + + printf ("TEST: struct bstrList * bsplit (const_bstring str, unsigned char splitChar);\n"); + /* tests with NULL */ + ret += test21_0 (NULL, (char) '?', 0); + ret += test21_0 (&badBstring1, (char) '?', 0); + ret += test21_0 (&badBstring2, (char) '?', 0); + + /* normal operation tests */ + ret += test21_0 (&emptyBstring, (char) '?', 1); + ret += test21_0 (&shortBstring, (char) 'o', 2); + ret += test21_0 (&shortBstring, (char) 's', 2); + ret += test21_0 (&shortBstring, (char) 'b', 2); + ret += test21_0 (&longBstring, (char) 'o', 9); + ret += test21_0 (&commas, (char) ',', 5); + + printf ("TEST: struct bstrList * bsplitstr (bstring str, const_bstring splitStr);\n"); + + ret += test21_1 (NULL, NULL, 0); + ret += test21_1 (&badBstring1, &emptyBstring, 0); + ret += test21_1 (&badBstring2, &emptyBstring, 0); + + /* normal operation tests */ + ret += test21_1 (&shortBstring, &emptyBstring, 5); + ret += test21_1 (&longBstring, &is, 3); + ret += test21_1 (&longBstring, &ng, 5); + + if (0 == ret) { + struct bstrList * l; + unsigned char c; + struct tagbstring t; + bstring b; + bstring list[3] = { &emptyBstring, &shortBstring, &longBstring }; + int i; + + blk2tbstr (t, &c, 1); + + for (i=0; i < 3; i++) { + c = (unsigned char) '\0'; + for (;;) { + b = bjoin (l = bsplit (list[i], c), &t); + if (!biseq (b, list[i])) { + printf ("\t\tfailure(%d) ", __LINE__); + printf ("join (bsplit (%s, x%02X), {x%02X}) = %s\n", dumpBstring (list[i]), c, c, dumpBstring (b)); + ret++; + } + bdestroy (b); + bstrListDestroy (l); + if (ret) break; + + b = bjoin (l = bsplitstr (list[i], &t), &t); + if (!biseq (b, list[i])) { + printf ("\t\tfailure(%d) ", __LINE__); + printf ("join (bsplitstr (%s, {x%02X}), {x%02X}) = %s\n", dumpBstring (list[i]), c, c, dumpBstring (b)); + ret++; + } + bdestroy (b); + bstrListDestroy (l); + if (ret) break; + + if (UCHAR_MAX == c) break; + c++; + } + if (ret) break; + } + + l = bsplit (&emptyBstring, 'x'); + bdestroy (l->entry[0]); + l->qty--; + b = bjoin (l, &longBstring); + ret += incorrectBstring (b); + bstrListDestroy (l); + if (b->slen) { + printf ("\t\tfailure(%d) ", __LINE__); + ret++; + } + bdestroy (b); + + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test22_0 (const_bstring b, const_bstring sep, int ns, ...) { +va_list arglist; +struct bstrList * l; +int ret = 0; + + printf (".\tbsplits (%s, %s)", dumpBstring (b), dumpBstring (sep)); + if ( b != NULL && b->data != NULL && b->slen >= 0 && + sep != NULL && sep->data != NULL && sep->slen >= 0) { + printf (" {"); + + l = bsplits (b, sep); + + if (l) { + int i; + va_start (arglist, ns); + + for (i=0; i < l->qty; i++) { + char * res; + + res = va_arg (arglist, char *); + + if (i != 0) printf (", "); + printf ("%s", dumpBstring (l->entry[i])); + + ret += (res == NULL) || ((int) strlen (res) > l->entry[i]->slen) + || (0 != memcmp (l->entry[i]->data, res, l->entry[i]->slen)); + ret += l->entry[i]->data[l->entry[i]->slen] != '\0'; + } + + va_end (arglist); + + printf (":<%d>", l->qty); + if (ns != l->qty) ret++; + } else { + printf ("NULL"); + ret += (ns != 0); + } + + printf ("}\n"); + + ret += (0 != bstrListDestroy (l) && l != NULL); + } else { + l = bsplits (b, sep); + ret += (l != NULL); + printf (" = %p\n", (void *) l); + } + + if (ret) { + printf ("\t\tfailure(%d) ns = %d\n", __LINE__, ns); + } + + return ret; +} + +static int test22 (void) { +int ret = 0; +struct tagbstring o=bsStatic("o"); +struct tagbstring s=bsStatic("s"); +struct tagbstring b=bsStatic("b"); +struct tagbstring bs=bsStatic("bs"); +struct tagbstring uo=bsStatic("uo"); + + printf ("TEST: extern struct bstrList * bsplits (const_bstring str, const_bstring splitStr);\n"); + /* tests with NULL */ + ret += test22_0 (NULL, &o, 0); + ret += test22_0 (&o, NULL, 0); + + /* normal operation tests */ + ret += test22_0 (&emptyBstring, &o, 1, ""); + ret += test22_0 (&emptyBstring, &uo, 1, ""); + ret += test22_0 (&shortBstring, &emptyBstring, 1, "bogus"); + ret += test22_0 (&shortBstring, &o, 2, "b", "gus"); + ret += test22_0 (&shortBstring, &s, 2, "bogu", ""); + ret += test22_0 (&shortBstring, &b, 2, "" , "ogus"); + ret += test22_0 (&shortBstring, &bs, 3, "" , "ogu", ""); + ret += test22_0 (&longBstring, &o, 9, "This is a b", "gus but reas", "nably l", "ng string. Just l", "ng en", "ugh t", " cause s", "me mall", "cing."); + ret += test22_0 (&shortBstring, &uo, 3, "b", "g", "s"); + + if (0 == ret) { + struct bstrList * l; + unsigned char c; + struct tagbstring t; + bstring bb; + bstring list[3] = { &emptyBstring, &shortBstring, &longBstring }; + int i; + + blk2tbstr (t, &c, 1); + + for (i=0; i < 3; i++) { + c = (unsigned char) '\0'; + for (;;) { + bb = bjoin (l = bsplits (list[i], &t), &t); + if (!biseq (bb, list[i])) { + printf ("\t\tfailure(%d) ", __LINE__); + printf ("join (bsplits (%s, {x%02X}), {x%02X}) = %s\n", dumpBstring (list[i]), c, c, dumpBstring (bb)); + ret++; + } + bdestroy (bb); + bstrListDestroy (l); + if (ret) break; + if (UCHAR_MAX == c) break; + c++; + } + if (ret) break; + } + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +struct sbstr { + int ofs; + bstring b; +}; + +static size_t test23_aux_read (void *buff, size_t elsize, size_t nelem, void *parm) { +struct sbstr * sb = (struct sbstr *)parm; +int els, len; + + if (parm == NULL || elsize == 0 || nelem == 0) return 0; + len = (int) (nelem * elsize); if (len <= 0) return 0; + if (len + sb->ofs > sb->b->slen) len = sb->b->slen - sb->ofs; + els = (int) (len / elsize); + len = (int) (els * elsize); + if (len > 0) { + memcpy (buff, sb->b->data + sb->ofs, len); + sb->ofs += len; + } + return els; +} + +static int test23_aux_open (struct sbstr * sb, bstring b) { + if (!sb || b == NULL || b->data == NULL) return -__LINE__; + sb->ofs = 0; + sb->b = b; + return 0; +} + +static int test23_aux_splitcb (void * parm, int ofs, const struct tagbstring * entry) { +bstring b = (bstring) parm; + + ofs = ofs; + if (b->slen > 0) bconchar (b, (char) '|'); + bconcat (b, entry); + return 0; +} + +struct tagBss { + int first; + unsigned char sc; + bstring b; +}; + +static int test23_aux_splitcbx (void * parm, int ofs, const struct tagbstring * entry) { +struct tagBss * p = (struct tagBss *) parm; + + ofs = ofs; + if (!p->first) { + bconchar (p->b, (char) p->sc); + } else p->first = 0; + + bconcat (p->b, entry); + return 0; +} + +static int test23 (void) { +struct tagbstring space = bsStatic (" "); +struct sbstr sb; +struct bStream * bs; +bstring b; +int l, ret = 0; + + printf ("TEST: bstream integrated test\n"); + test23_aux_open (&sb, &longBstring); + ret += NULL != (bs = bsopen ((bNread) NULL, &sb)); + ret += NULL == (bs = bsopen ((bNread) test23_aux_read, &sb)); + ret += (bseof (bs) != 0); + ret += BSTR_ERR != bsbufflength (NULL, -1); + ret += BSTR_ERR != bsbufflength (NULL, 1); + ret += BSTR_ERR != bsbufflength (bs, -1); + printf (".\tbsbufflength (bs, 0) -> %d\n", bsbufflength (bs, 0)); + ret += BSTR_ERR == bsbufflength (bs, 1); + ret += BSTR_ERR != bspeek (NULL, bs); + ret += BSTR_ERR != bsreadln (NULL, bs, (char) '?'); + ret += BSTR_ERR != bsreadln (&emptyBstring, bs, (char) '?'); + ret += BSTR_ERR != bspeek (&emptyBstring, bs); + + ret += BSTR_ERR == bspeek (b = bfromcstr (""), bs); + + printf (".\tbspeek () -> %s\n", dumpBstring (b)); + ret += BSTR_ERR != bsreadln (b, NULL, (char) '?'); + b->slen = 0; + ret += BSTR_ERR == bsreadln (b, bs, (char) '?'); + ret += (bseof (bs) <= 0); + ret += biseq (b, &longBstring) < 0; + printf (".\tbsreadln ('?') -> %s\n", dumpBstring (b)); + ret += BSTR_ERR == bsunread (bs, b); + ret += (bseof (bs) != 0); + printf (".\tbsunread (%s)\n", dumpBstring (b)); + b->slen = 0; + ret += BSTR_ERR == bspeek (b, bs); + ret += biseq (b, &longBstring) < 0; + printf (".\tbspeek () -> %s\n", dumpBstring (b)); + b->slen = 0; + ret += BSTR_ERR == bsreadln (b, bs, (char) '?'); + ret += (bseof (bs) <= 0); + ret += biseq (b, &longBstring) < 0; + printf (".\tbsreadln ('?') -> %s\n", dumpBstring (b)); + ret += NULL == bsclose (bs); + sb.ofs = 0; + + ret += NULL == (bs = bsopen ((bNread) test23_aux_read, &sb)); + b->slen = 0; + ret += BSTR_ERR == bsreadln (b, bs, (char) '.'); + l = b->slen; + ret += (0 != bstrncmp (b, &longBstring, l)) || (longBstring.data[l-1] != '.'); + printf (".\tbsreadln ('.') -> %s\n", dumpBstring (b)); + ret += BSTR_ERR == bsunread (bs, b); + + printf (".\tbsunread (%s)\n", dumpBstring (b)); + b->slen = 0; + ret += BSTR_ERR == bspeek (b, bs); + ret += biseq (b, &longBstring) < 0; + printf (".\tbspeek () -> %s\n", dumpBstring (b)); + b->slen = 0; + ret += BSTR_ERR == bsreadln (b, bs, (char) '.'); + + ret += b->slen != l || (0 != bstrncmp (b, &longBstring, l)) || (longBstring.data[l-1] != '.'); + printf (".\tbsreadln ('.') -> %s\n", dumpBstring (b)); + ret += NULL == bsclose (bs); + + test23_aux_open (&sb, &longBstring); + ret += NULL == (bs = bsopen ((bNread) test23_aux_read, &sb)); + ret += (bseof (bs) != 0); + b->slen = 0; + l = bssplitscb (bs, &space, test23_aux_splitcb, b); + ret += (bseof (bs) <= 0); + ret += NULL == bsclose (bs); + printf (".\tbssplitscb (' ') -> %s\n", dumpBstring (b)); + + for (l=1; l < 4; l++) { + char * str; + for (str = (char *) longBstring.data; *str; str++) { + test23_aux_open (&sb, &longBstring); + ret += NULL == (bs = bsopen ((bNread) test23_aux_read, &sb)); + ret += bseof (bs) != 0; + ret += 0 > bsbufflength (bs, l); + b->slen = 0; + while (0 == bsreadlna (b, bs, *str)) ; + ret += 0 == biseq (b, &longBstring); + ret += bseof (bs) <= 0; + ret += NULL == bsclose (bs); + if (ret) break; + } + if (ret) break; + } + + bdestroy (b); + + if (0 == ret) { + unsigned char c; + struct tagbstring t; + bstring list[3] = { &emptyBstring, &shortBstring, &longBstring }; + int i; + + blk2tbstr (t, &c, 1); + + for (i=0; i < 3; i++) { + c = (unsigned char) '\0'; + for (;;) { + struct tagBss bss; + + bss.sc = c; + bss.b = bfromcstr (""); + bss.first = 1; + + test23_aux_open (&sb, list[i]); + bs = bsopen ((bNread) test23_aux_read, &sb); + bssplitscb (bs, &t, test23_aux_splitcbx, &bss); + bsclose (bs); + + if (!biseq (bss.b, list[i])) { + printf ("\t\tfailure(%d) ", __LINE__); + printf ("join (bssplitscb (%s, {x%02X}), {x%02X}) = %s\n", dumpBstring (list[i]), c, c, dumpBstring (bss.b)); + ret++; + } + bdestroy (bss.b); + if (ret) break; + if (UCHAR_MAX == c) break; + c++; + } + if (ret) break; + + for (;;) { + struct tagBss bss; + + bss.sc = c; + bss.b = bfromcstr (""); + bss.first = 1; + + test23_aux_open (&sb, list[i]); + bs = bsopen ((bNread) test23_aux_read, &sb); + bssplitstrcb (bs, &t, test23_aux_splitcbx, &bss); + bsclose (bs); + + if (!biseq (bss.b, list[i])) { + printf ("\t\tfailure(%d) ", __LINE__); + printf ("join (bssplitstrcb (%s, {x%02X}), {x%02X}) = %s\n", dumpBstring (list[i]), c, c, dumpBstring (bss.b)); + ret++; + } + bdestroy (bss.b); + if (ret) break; + if (UCHAR_MAX == c) break; + c++; + } + if (ret) break; + } + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test24_0 (bstring s1, int pos, const_bstring s2, int res) { +int rv, ret = 0; + + printf (".\tbninchr (%s, %d, %s) = ", dumpBstring (s1), pos, dumpBstring (s2)); + rv = bninchr (s1, pos, s2); + printf ("%d\n", rv); + ret += (rv != res); + if (ret) { + printf ("\t\tfailure(%d) res = %d\n", __LINE__, res); + } + return ret; +} + +static int test24 (void) { +bstring b; +int ret = 0; + + printf ("TEST: int bninchr (const_bstring s1, int pos, const_bstring s2);\n"); + ret += test24_0 (NULL, 0, NULL, BSTR_ERR); + ret += test24_0 (&emptyBstring, 0, NULL, BSTR_ERR); + ret += test24_0 (NULL, 0, &emptyBstring, BSTR_ERR); + ret += test24_0 (&shortBstring, 3, &badBstring1, BSTR_ERR); + ret += test24_0 (&badBstring1, 3, &shortBstring, BSTR_ERR); + + ret += test24_0 (&emptyBstring, 0, &emptyBstring, BSTR_ERR); + ret += test24_0 (&shortBstring, 0, &emptyBstring, BSTR_ERR); + ret += test24_0 (&shortBstring, 0, &shortBstring, BSTR_ERR); + ret += test24_0 (&shortBstring, 1, &shortBstring, BSTR_ERR); + ret += test24_0 (&longBstring, 3, &shortBstring, 4); + ret += test24_0 (&longBstring, 3, b = bstrcpy (&shortBstring), 4); + bdestroy (b); + ret += test24_0 (&longBstring, -1, &shortBstring, BSTR_ERR); + ret += test24_0 (&longBstring, 1000, &shortBstring, BSTR_ERR); + ret += test24_0 (&xxxxxBstring, 0, &shortBstring, 0); + ret += test24_0 (&xxxxxBstring, 1, &shortBstring, 1); + ret += test24_0 (&emptyBstring, 0, &shortBstring, BSTR_ERR); + + ret += test24_0 (&longBstring, 0, &shortBstring, 0); + ret += test24_0 (&longBstring, 10, &shortBstring, 15); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test25_0 (bstring s1, int pos, const_bstring s2, int res) { +int rv, ret = 0; + + printf (".\tbninchrr (%s, %d, %s) = ", dumpBstring (s1), pos, dumpBstring (s2)); + rv = bninchrr (s1, pos, s2); + printf ("%d\n", rv); + ret += (rv != res); + if (ret) { + printf ("\t\tfailure(%d) res = %d\n", __LINE__, res); + } + return ret; +} + +static int test25 (void) { +bstring b; +int ret = 0; + + printf ("TEST: int bninchrr (const_bstring s1, int pos, const_bstring s2);\n"); + ret += test25_0 (NULL, 0, NULL, BSTR_ERR); + ret += test25_0 (&emptyBstring, 0, NULL, BSTR_ERR); + ret += test25_0 (NULL, 0, &emptyBstring, BSTR_ERR); + ret += test25_0 (&emptyBstring, 0, &emptyBstring, BSTR_ERR); + ret += test25_0 (&shortBstring, 0, &emptyBstring, BSTR_ERR); + ret += test25_0 (&shortBstring, 0, &badBstring1, BSTR_ERR); + ret += test25_0 (&badBstring1, 0, &shortBstring, BSTR_ERR); + + ret += test25_0 (&shortBstring, 0, &shortBstring, BSTR_ERR); + ret += test25_0 (&shortBstring, 4, &shortBstring, BSTR_ERR); + ret += test25_0 (&longBstring, 10, &shortBstring, 9); + ret += test25_0 (&longBstring, 10, b = bstrcpy (&shortBstring), 9); + bdestroy (b); + ret += test25_0 (&xxxxxBstring, 4, &shortBstring, 4); + ret += test25_0 (&emptyBstring, 0, &shortBstring, BSTR_ERR); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test26_0 (bstring b0, int pos, int len, const_bstring b1, unsigned char fill, char * res) { +bstring b2; +int rv, ret = 0; + + if (b0 != NULL && b0->data != NULL && b0->slen >= 0 && + b1 != NULL && b1->data != NULL && b1->slen >= 0) { + b2 = bstrcpy (b0); + bwriteprotect (*b2); + + printf (".\tbreplace (%s, ", dumpBstring (b2)); + + rv = breplace (b2, pos, len, b1, fill); + ret += (rv == 0); + if (!biseq (b0, b2)) ret++; + + printf ("%d, %d, %s, %02X) = %s\n", pos, len, dumpBstring (b1), fill, dumpBstring (b2)); + + bwriteallow (*b2); + + printf (".\tbreplace (%s, ", dumpBstring (b2)); + + rv = breplace (b2, pos, len, b1, fill); + if (b1) { + ret += (pos < 0) && (b2->slen != b0->slen); + ret += ((rv == 0) != (pos >= 0 && pos <= b2->slen)); + } + + ret += (res == NULL) || ((int) strlen (res) > b2->slen) + || (0 != memcmp (b2->data, res, b2->slen)); + ret += b2->data[b2->slen] != '\0'; + + printf ("%d, %d, %s, %02X) = %s\n", pos, len, dumpBstring (b1), fill, dumpBstring (b2)); + + bdestroy (b2); + } else { + ret += (BSTR_ERR != (rv = breplace (b0, pos, len, b1, fill))); + printf (".\tbreplace (%s, %d, %d, %s, %02X) = %d\n", dumpBstring (b0), pos, len, dumpBstring (b1), fill, rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test26 (void) { +int ret = 0; + printf ("TEST: int breplace (bstring b0, int pos, int len, const_bstring b1, unsigned char fill);\n"); + /* tests with NULL */ + ret += test26_0 (NULL, 0, 0, NULL, (unsigned char) '?', NULL); + ret += test26_0 (NULL, 0, 0, &emptyBstring, (unsigned char) '?', NULL); + ret += test26_0 (&badBstring1, 1, 3, &shortBstring, (unsigned char) '?', NULL); + ret += test26_0 (&shortBstring, 1, 3, &badBstring1, (unsigned char) '?', NULL); + + /* normal operation tests */ + ret += test26_0 (&emptyBstring, 0, 0, &emptyBstring, (unsigned char) '?', ""); + ret += test26_0 (&emptyBstring, 5, 0, &emptyBstring, (unsigned char) '?', "?????"); + ret += test26_0 (&emptyBstring, 5, 0, &shortBstring, (unsigned char) '?', "?????bogus"); + ret += test26_0 (&shortBstring, 0, 0, &emptyBstring, (unsigned char) '?', "bogus"); + ret += test26_0 (&emptyBstring, 0, 0, &shortBstring, (unsigned char) '?', "bogus"); + ret += test26_0 (&shortBstring, 0, 0, &shortBstring, (unsigned char) '?', "bogusbogus"); + ret += test26_0 (&shortBstring, 1, 3, &shortBstring, (unsigned char) '?', "bboguss"); + ret += test26_0 (&shortBstring, 3, 8, &shortBstring, (unsigned char) '?', "bogbogus"); + ret += test26_0 (&shortBstring, -1, 0, &shortBstring, (unsigned char) '?', "bogus"); + ret += test26_0 (&shortBstring, 2, 0, &shortBstring, (unsigned char) '?', "bobogusgus"); + ret += test26_0 (&shortBstring, 6, 0, &shortBstring, (unsigned char) '?', "bogus?bogus"); + ret += test26_0 (&shortBstring, 6, 0, NULL, (unsigned char) '?', "bogus"); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test27_0 (bstring b0, const_bstring b1, const char * res) { +bstring b2; +int rv, ret = 0; + + if (b0 != NULL && b0->data != NULL && b0->slen >= 0 && + b1 != NULL && b1->data != NULL && b1->slen >= 0) { + b2 = bstrcpy (b0); + bwriteprotect (*b2); + + printf (".\tbassign (%s, ", dumpBstring (b2)); + + rv = bassign (b2, b1); + ret += (rv == 0); + if (!biseq (b0, b2)) ret++; + + printf ("%s) = %s\n", dumpBstring (b1), dumpBstring (b2)); + + bwriteallow (*b2); + + printf (".\tbassign (%s, ", dumpBstring (b2)); + + rv = bassign (b2, b1); + + printf ("%s) = %s\n", dumpBstring (b1), dumpBstring (b2)); + + if (b1) ret += (b2->slen != b1->slen); + ret += ((0 != rv) && (b1 != NULL)) || ((0 == rv) && (b1 == NULL)); + ret += (res == NULL) || ((int) strlen (res) != b2->slen) + || (0 != memcmp (b2->data, res, b2->slen)); + ret += b2->data[b2->slen] != '\0'; + bdestroy (b2); + } else { + ret += (BSTR_ERR != (rv = bassign (b0, b1))); + printf (".\tbassign (%s, %s) = %d\n", dumpBstring (b0), dumpBstring (b1), rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test27 (void) { +int ret = 0; + + printf ("TEST: int bassign (bstring b0, const_bstring b1);\n"); + + /* tests with NULL */ + ret += test27_0 (NULL, NULL, NULL); + ret += test27_0 (NULL, &emptyBstring, NULL); + ret += test27_0 (&emptyBstring, NULL, ""); + ret += test27_0 (&badBstring1, &emptyBstring, NULL); + ret += test27_0 (&badBstring2, &emptyBstring, NULL); + ret += test27_0 (&emptyBstring, &badBstring1, NULL); + ret += test27_0 (&emptyBstring, &badBstring2, NULL); + + /* normal operation tests on all sorts of subranges */ + ret += test27_0 (&emptyBstring, &emptyBstring, ""); + ret += test27_0 (&emptyBstring, &shortBstring, "bogus"); + ret += test27_0 (&shortBstring, &emptyBstring, ""); + ret += test27_0 (&shortBstring, &shortBstring, "bogus"); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test28_0 (bstring s1, int c, int res) { +int rv, ret = 0; + + printf (".\tbstrchr (%s, %d) = ", dumpBstring (s1), c); + rv = bstrchr (s1, c); + printf ("%d\n", rv); + ret += (rv != res); + if (ret) { + printf ("\t\tfailure(%d) res = %d\n", __LINE__, res); + } + return ret; +} + +static int test28_1 (bstring s1, int c, int res) { +int rv, ret = 0; + + printf (".\tbstrrchr (%s, %d) = ", dumpBstring (s1), c); + rv = bstrrchr (s1, c); + printf ("%d\n", rv); + ret += (rv != res); + if (ret) { + printf ("\t\tfailure(%d) res = %d rv = %d\n", __LINE__, res, rv); + } + return ret; +} + +static int test28_2 (bstring s1, int c, int pos, int res) { +int rv, ret = 0; + + printf (".\tbstrchrp (%s, %d, %d) = ", dumpBstring (s1), c, pos); + rv = bstrchrp (s1, c, pos); + printf ("%d\n", rv); + ret += (rv != res); + if (ret) { + printf ("\t\tfailure(%d) res = %d\n", __LINE__, res); + } + return ret; +} + +static int test28_3 (bstring s1, int c, int pos, int res) { +int rv, ret = 0; + + printf (".\tbstrrchrp (%s, %d, %d) = ", dumpBstring (s1), c, pos); + rv = bstrrchrp (s1, c, pos); + printf ("%d\n", rv); + ret += (rv != res); + if (ret) { + printf ("\t\tfailure(%d) res = %d rv = %d\n", __LINE__, res, rv); + } + return ret; +} + +static int test28 (void) { +int ret = 0; + + printf ("TEST: int bstrchr (const_bstring s1, int c);\n"); + ret += test28_0 (NULL, 0, BSTR_ERR); + ret += test28_0 (&badBstring1, 'b', BSTR_ERR); + ret += test28_0 (&badBstring2, 's', BSTR_ERR); + + ret += test28_0 (&emptyBstring, 0, BSTR_ERR); + ret += test28_0 (&shortBstring, 0, BSTR_ERR); + ret += test28_0 (&shortBstring, 'b', 0); + ret += test28_0 (&shortBstring, 's', 4); + ret += test28_0 (&shortBstring, 'q', BSTR_ERR); + ret += test28_0 (&xxxxxBstring, 0, BSTR_ERR); + ret += test28_0 (&xxxxxBstring, 'b', BSTR_ERR); + ret += test28_0 (&longBstring, 'i', 2); + + printf ("TEST: int bstrrchr (const_bstring s1, int c);\n"); + ret += test28_1 (NULL, 0, BSTR_ERR); + ret += test28_1 (&badBstring1, 'b', BSTR_ERR); + ret += test28_1 (&badBstring2, 's', BSTR_ERR); + + ret += test28_1 (&emptyBstring, 0, BSTR_ERR); + ret += test28_1 (&shortBstring, 0, BSTR_ERR); + ret += test28_1 (&shortBstring, 'b', 0); + ret += test28_1 (&shortBstring, 's', 4); + ret += test28_1 (&shortBstring, 'q', BSTR_ERR); + ret += test28_1 (&xxxxxBstring, 0, BSTR_ERR); + ret += test28_1 (&xxxxxBstring, 'b', BSTR_ERR); + ret += test28_1 (&longBstring, 'i', 82); + + printf ("TEST: int bstrchrp (const_bstring s1, int c, int pos);\n"); + ret += test28_2 (NULL, 0, 0, BSTR_ERR); + ret += test28_2 (&badBstring1, 'b', 0, BSTR_ERR); + ret += test28_2 (&badBstring2, 's', 0, BSTR_ERR); + ret += test28_2 (&shortBstring, 'b', -1, BSTR_ERR); + ret += test28_2 (&shortBstring, 'b', shortBstring.slen, BSTR_ERR); + + ret += test28_2 (&emptyBstring, 0, 0, BSTR_ERR); + ret += test28_2 (&shortBstring, 0, 0, BSTR_ERR); + ret += test28_2 (&shortBstring, 'b', 0, 0); + ret += test28_2 (&shortBstring, 'b', 1, BSTR_ERR); + ret += test28_2 (&shortBstring, 's', 0, 4); + ret += test28_2 (&shortBstring, 'q', 0, BSTR_ERR); + + printf ("TEST: int bstrrchrp (const_bstring s1, int c, int pos);\n"); + ret += test28_3 (NULL, 0, 0, BSTR_ERR); + ret += test28_3 (&badBstring1, 'b', 0, BSTR_ERR); + ret += test28_3 (&badBstring2, 's', 0, BSTR_ERR); + ret += test28_3 (&shortBstring, 'b', -1, BSTR_ERR); + ret += test28_3 (&shortBstring, 'b', shortBstring.slen, BSTR_ERR); + + ret += test28_3 (&emptyBstring, 0, 0, BSTR_ERR); + ret += test28_3 (&shortBstring, 0, 0, BSTR_ERR); + ret += test28_3 (&shortBstring, 'b', 0, 0); + ret += test28_3 (&shortBstring, 'b', shortBstring.slen - 1, 0); + ret += test28_3 (&shortBstring, 's', shortBstring.slen - 1, 4); + ret += test28_3 (&shortBstring, 's', 0, BSTR_ERR); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test29_0 (bstring b0, char * s, const char * res) { +bstring b2; +int rv, ret = 0; + + if (b0 != NULL && b0->data != NULL && b0->slen >= 0) { + b2 = bstrcpy (b0); + bwriteprotect (*b2); + + printf (".\tbcatcstr (%s, ", dumpBstring (b2)); + + rv = bcatcstr (b2, s); + ret += (rv == 0); + if (!biseq (b0, b2)) ret++; + + printf ("%p) = %s\n", s, dumpBstring (b2)); + + bwriteallow (*b2); + + printf (".\tbcatcstr (%s, ", dumpBstring (b2)); + + rv = bcatcstr (b2, s); + + printf ("%p) = %s\n", s, dumpBstring (b2)); + + if (s) ret += (b2->slen != b0->slen + (int) strlen (s)); + ret += ((0 != rv) && (s != NULL)) || ((0 == rv) && (s == NULL)); + ret += (res == NULL) || ((int) strlen (res) != b2->slen) + || (0 != memcmp (b2->data, res, b2->slen)); + ret += b2->data[b2->slen] != '\0'; + bdestroy (b2); + } else { + ret += (BSTR_ERR != (rv = bcatcstr (b0, s))); + printf (".\tbcatcstr (%s, %p) = %d\n", dumpBstring (b0), s, rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test29 (void) { +int ret = 0; + + printf ("TEST: int bcatcstr (bstring b0, const char * s);\n"); + + /* tests with NULL */ + ret += test29_0 (NULL, NULL, NULL); + ret += test29_0 (NULL, "", NULL); + ret += test29_0 (&emptyBstring, NULL, ""); + ret += test29_0 (&badBstring1, "bogus", NULL); + ret += test29_0 (&badBstring2, "bogus", NULL); + + /* normal operation tests on all sorts of subranges */ + ret += test29_0 (&emptyBstring, "", ""); + ret += test29_0 (&emptyBstring, "bogus", "bogus"); + ret += test29_0 (&shortBstring, "", "bogus"); + ret += test29_0 (&shortBstring, "bogus", "bogusbogus"); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test30_0 (bstring b0, const unsigned char * s, int len, const char * res) { +bstring b2; +int rv, ret = 0; + + if (b0 != NULL && b0->data != NULL && b0->slen >= 0) { + b2 = bstrcpy (b0); + bwriteprotect (*b2); + + printf (".\tbcatblk (%s, ", dumpBstring (b2)); + + rv = bcatblk (b2, s, len); + ret += (rv == 0); + if (!biseq (b0, b2)) ret++; + + printf ("%p) = %s\n", s, dumpBstring (b2)); + + bwriteallow (*b2); + + printf (".\tbcatblk (%s, ", dumpBstring (b2)); + + rv = bcatblk (b2, s, len); + + printf ("%p) = %s\n", s, dumpBstring (b2)); + + if (s) { + if (len >= 0) ret += (b2->slen != b0->slen + len); + else ret += (b2->slen != b0->slen); + } + ret += ((0 != rv) && (s != NULL && len >= 0)) || ((0 == rv) && (s == NULL || len < 0)); + ret += (res == NULL) || ((int) strlen (res) != b2->slen) + || (0 != memcmp (b2->data, res, b2->slen)); + ret += b2->data[b2->slen] != '\0'; + bdestroy (b2); + } else { + ret += (BSTR_ERR != (rv = bcatblk (b0, s, len))); + printf (".\tbcatblk (%s, %p, %d) = %d\n", dumpBstring (b0), s, len, rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test30 (void) { +int ret = 0; + + printf ("TEST: int bcatblk (bstring b0, const char * s);\n"); + + /* tests with NULL */ + ret += test30_0 (NULL, NULL, 0, NULL); + ret += test30_0 (NULL, (unsigned char *) "", 0, NULL); + ret += test30_0 (&emptyBstring, NULL, 0, ""); + ret += test30_0 (&emptyBstring, NULL, -1, ""); + ret += test30_0 (&badBstring1, NULL, 0, NULL); + ret += test30_0 (&badBstring2, NULL, 0, NULL); + + /* normal operation tests on all sorts of subranges */ + ret += test30_0 (&emptyBstring, (unsigned char *) "", -1, ""); + ret += test30_0 (&emptyBstring, (unsigned char *) "", 0, ""); + ret += test30_0 (&emptyBstring, (unsigned char *) "bogus", 5, "bogus"); + ret += test30_0 (&shortBstring, (unsigned char *) "", 0, "bogus"); + ret += test30_0 (&shortBstring, (unsigned char *) "bogus", 5, "bogusbogus"); + ret += test30_0 (&shortBstring, (unsigned char *) "bogus", -1, "bogus"); + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test31_0 (bstring b0, const_bstring find, const_bstring replace, int pos, char * res) { +bstring b2; +int rv, ret = 0; + + if (b0 != NULL && b0->data != NULL && b0->slen >= 0 && + find != NULL && find->data != NULL && find->slen >= 0 && + replace != NULL && replace->data != NULL && replace->slen >= 0) { + b2 = bstrcpy (b0); + bwriteprotect (*b2); + + printf (".\tbfindreplace (%s, %s, %s, %d) = ", dumpBstring (b2), dumpBstring (find), dumpBstring (replace), pos); + + rv = bfindreplace (b2, find, replace, pos); + ret += (rv == 0); + if (!biseq (b0, b2)) ret++; + + printf ("%d\n", rv); + + bwriteallow (*b2); + + printf (".\tbfindreplace (%s, %s, %s, %d)", dumpBstring (b2), dumpBstring (find), dumpBstring (replace), pos); + + rv = bfindreplace (b2, find, replace, pos); + + ret += (res == NULL) || ((int) strlen (res) > b2->slen) + || (0 != memcmp (b2->data, res, b2->slen)); + ret += b2->data[b2->slen] != '\0'; + + printf (" -> %s\n", dumpBstring (b2)); + + bdestroy (b2); + } else { + ret += (BSTR_ERR != (rv = bfindreplace (b0, find, replace, pos))); + printf (".\tbfindreplace (%s, %s, %s, %d) = %d\n", dumpBstring (b0), dumpBstring (find), dumpBstring (replace), pos, rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test31_1 (bstring b0, const_bstring find, const_bstring replace, int pos, char * res) { +bstring b2; +int rv, ret = 0; + + if (b0 != NULL && b0->data != NULL && b0->slen >= 0 && + find != NULL && find->data != NULL && find->slen >= 0 && + replace != NULL && replace->data != NULL && replace->slen >= 0) { + b2 = bstrcpy (b0); + bwriteprotect (*b2); + + printf (".\tbfindreplacecaseless (%s, %s, %s, %d) = ", dumpBstring (b2), dumpBstring (find), dumpBstring (replace), pos); + + rv = bfindreplacecaseless (b2, find, replace, pos); + ret += (rv == 0); + if (!biseq (b0, b2)) ret++; + + printf ("%d\n", rv); + + bwriteallow (*b2); + + printf (".\tbfindreplacecaseless (%s, %s, %s, %d)", dumpBstring (b2), dumpBstring (find), dumpBstring (replace), pos); + + rv = bfindreplacecaseless (b2, find, replace, pos); + + ret += (res == NULL) || ((int) strlen (res) > b2->slen) + || (0 != memcmp (b2->data, res, b2->slen)); + ret += b2->data[b2->slen] != '\0'; + + printf (" -> %s\n", dumpBstring (b2)); + + bdestroy (b2); + } else { + ret += (BSTR_ERR != (rv = bfindreplacecaseless (b0, find, replace, pos))); + printf (".\tbfindreplacecaseless (%s, %s, %s, %d) = %d\n", dumpBstring (b0), dumpBstring (find), dumpBstring (replace), pos, rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +#define LOTS_OF_S "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" + +static int test31 (void) { +int ret = 0; +struct tagbstring t0 = bsStatic ("funny"); +struct tagbstring t1 = bsStatic ("weird"); +struct tagbstring t2 = bsStatic ("s"); +struct tagbstring t3 = bsStatic ("long"); +struct tagbstring t4 = bsStatic ("big"); +struct tagbstring t5 = bsStatic ("ss"); +struct tagbstring t6 = bsStatic ("sstsst"); +struct tagbstring t7 = bsStatic ("xx" LOTS_OF_S "xx"); +struct tagbstring t8 = bsStatic ("S"); +struct tagbstring t9 = bsStatic ("LONG"); + + printf ("TEST: int bfindreplace (bstring b, const_bstring f, const_bstring r, int pos);\n"); + /* tests with NULL */ + ret += test31_0 (NULL, NULL, NULL, 0, NULL); + ret += test31_0 (&shortBstring, NULL, &t1, 0, (char *) shortBstring.data); + ret += test31_0 (&shortBstring, &t2, NULL, 0, (char *) shortBstring.data); + ret += test31_0 (&badBstring1, &t2, &t1, 0, NULL); + ret += test31_0 (&badBstring2, &t2, &t1, 0, NULL); + + /* normal operation tests */ + ret += test31_0 (&longBstring, &shortBstring, &t0, 0, "This is a funny but reasonably long string. Just long enough to cause some mallocing."); + ret += test31_0 (&longBstring, &t2, &t1, 0, "Thiweird iweird a boguweird but reaweirdonably long weirdtring. Juweirdt long enough to cauweirde weirdome mallocing."); + ret += test31_0 (&shortBstring, &t2, &t1, 0, "boguweird"); + ret += test31_0 (&shortBstring, &t8, &t1, 0, "bogus"); + ret += test31_0 (&longBstring, &t2, &t1, 27, "This is a bogus but reasonably long weirdtring. Juweirdt long enough to cauweirde weirdome mallocing."); + ret += test31_0 (&longBstring, &t3, &t4, 0, "This is a bogus but reasonably big string. Just big enough to cause some mallocing."); + ret += test31_0 (&longBstring, &t9, &t4, 0, "This is a bogus but reasonably long string. Just long enough to cause some mallocing."); + ret += test31_0 (&t6, &t2, &t5, 0, "sssstsssst"); + ret += test31_0 (&t7, &t2, &t5, 0, "xx" LOTS_OF_S LOTS_OF_S "xx"); + + printf ("TEST: int bfindreplacecaseless (bstring b, const_bstring f, const_bstring r, int pos);\n"); + /* tests with NULL */ + ret += test31_1 (NULL, NULL, NULL, 0, NULL); + ret += test31_1 (&shortBstring, NULL, &t1, 0, (char *) shortBstring.data); + ret += test31_1 (&shortBstring, &t2, NULL, 0, (char *) shortBstring.data); + ret += test31_1 (&badBstring1, &t2, &t1, 0, NULL); + ret += test31_1 (&badBstring2, &t2, &t1, 0, NULL); + + /* normal operation tests */ + ret += test31_1 (&longBstring, &shortBstring, &t0, 0, "This is a funny but reasonably long string. Just long enough to cause some mallocing."); + ret += test31_1 (&longBstring, &t2, &t1, 0, "Thiweird iweird a boguweird but reaweirdonably long weirdtring. Juweirdt long enough to cauweirde weirdome mallocing."); + ret += test31_1 (&shortBstring, &t2, &t1, 0, "boguweird"); + ret += test31_1 (&shortBstring, &t8, &t1, 0, "boguweird"); + ret += test31_1 (&longBstring, &t2, &t1, 27, "This is a bogus but reasonably long weirdtring. Juweirdt long enough to cauweirde weirdome mallocing."); + ret += test31_1 (&longBstring, &t3, &t4, 0, "This is a bogus but reasonably big string. Just big enough to cause some mallocing."); + ret += test31_1 (&longBstring, &t9, &t4, 0, "This is a bogus but reasonably big string. Just big enough to cause some mallocing."); + ret += test31_1 (&t6, &t2, &t5, 0, "sssstsssst"); + ret += test31_1 (&t6, &t8, &t5, 0, "sssstsssst"); + ret += test31_1 (&t7, &t2, &t5, 0, "xx" LOTS_OF_S LOTS_OF_S "xx"); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test32_0 (const_bstring b, const char * s, int res) { +int rv, ret = 0; + + ret += (res != (rv = biseqcstr (b, s))); + printf (".\tbiseqcstr (%s, %p:<%s>) = %d\n", dumpBstring (b), s, (s ? s : NULL), rv); + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %d)\n", __LINE__, ret, res); + } + return ret; +} + +static int test32_1 (const_bstring b, const char * s, int res) { +int rv, ret = 0; + + ret += (res != (rv = biseqcstrcaseless (b, s))); + printf (".\tbiseqcstrcaseless (%s, %p:<%s>) = %d\n", dumpBstring (b), s, (s ? s : NULL), rv); + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %d)\n", __LINE__, ret, res); + } + return ret; +} + + +static int test32 (void) { +int ret = 0; + + printf ("TEST: int biseqcstr (const_bstring b, const char * s);\n"); + + /* tests with NULL */ + ret += test32_0 (NULL, NULL, BSTR_ERR); + ret += test32_0 (&emptyBstring, NULL, BSTR_ERR); + ret += test32_0 (NULL, "", BSTR_ERR); + ret += test32_0 (&badBstring1, "", BSTR_ERR); + ret += test32_0 (&badBstring2, "bogus", BSTR_ERR); + + /* normal operation tests on all sorts of subranges */ + ret += test32_0 (&emptyBstring, "", 1); + ret += test32_0 (&shortBstring, "bogus", 1); + ret += test32_0 (&emptyBstring, "bogus", 0); + ret += test32_0 (&shortBstring, "", 0); + + { + bstring b = bstrcpy (&shortBstring); + b->data[1]++; + ret += test32_0 (b, (char *) shortBstring.data, 0); + bdestroy (b); + } + + printf ("TEST: int biseqcstrcaseless (const_bstring b, const char * s);\n"); + + /* tests with NULL */ + ret += test32_1 (NULL, NULL, BSTR_ERR); + ret += test32_1 (&emptyBstring, NULL, BSTR_ERR); + ret += test32_1 (NULL, "", BSTR_ERR); + ret += test32_1 (&badBstring1, "", BSTR_ERR); + ret += test32_1 (&badBstring2, "bogus", BSTR_ERR); + + /* normal operation tests on all sorts of subranges */ + ret += test32_1 (&emptyBstring, "", 1); + ret += test32_1 (&shortBstring, "bogus", 1); + ret += test32_1 (&shortBstring, "BOGUS", 1); + ret += test32_1 (&emptyBstring, "bogus", 0); + ret += test32_1 (&shortBstring, "", 0); + + { + bstring b = bstrcpy (&shortBstring); + b->data[1]++; + ret += test32_1 (b, (char *) shortBstring.data, 0); + bdestroy (b); + } + + if (ret) printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test33_0 (bstring b0, const char * res) { +bstring b2; +int rv, ret = 0; + + if (b0 != NULL && b0->data != NULL && b0->slen >= 0) { + b2 = bstrcpy (b0); + bwriteprotect (*b2); + + printf (".\tbtoupper (%s)", dumpBstring (b2)); + + rv = btoupper (b2); + ret += (rv == 0); + if (!biseq (b0, b2)) ret++; + + printf (" = %s\n", dumpBstring (b2)); + + bwriteallow (*b2); + + printf (".\tbtoupper (%s)", dumpBstring (b2)); + + rv = btoupper (b2); + + printf (" = %s\n", dumpBstring (b2)); + + ret += (b2->slen != b0->slen); + ret += (0 != rv); + ret += (res == NULL) || ((int) strlen (res) != b2->slen) + || (0 != memcmp (b2->data, res, b2->slen)); + ret += b2->data[b2->slen] != '\0'; + bdestroy (b2); + } else { + ret += (BSTR_ERR != (rv = btoupper (b0))); + printf (".\tbtoupper (%s) = %d\n", dumpBstring (b0), rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test33 (void) { +int ret = 0; + + printf ("TEST: int btoupper (bstring b);\n"); + + /* tests with NULL */ + ret += test33_0 (NULL, NULL); + ret += test33_0 (&badBstring1, NULL); + ret += test33_0 (&badBstring2, NULL); + + /* normal operation tests on all sorts of subranges */ + ret += test33_0 (&emptyBstring, ""); + ret += test33_0 (&shortBstring, "BOGUS"); + ret += test33_0 (&longBstring, "THIS IS A BOGUS BUT REASONABLY LONG STRING. JUST LONG ENOUGH TO CAUSE SOME MALLOCING."); + + if (ret) printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test34_0 (bstring b0, const char * res) { +bstring b2; +int rv, ret = 0; + + if (b0 != NULL && b0->data != NULL && b0->slen >= 0) { + b2 = bstrcpy (b0); + bwriteprotect (*b2); + + printf (".\tbtolower (%s)", dumpBstring (b2)); + + rv = btolower (b2); + ret += (rv == 0); + if (!biseq (b0, b2)) ret++; + + printf (" = %s\n", dumpBstring (b2)); + + bwriteallow (*b2); + + printf (".\tbtolower (%s)", dumpBstring (b2)); + + rv = btolower (b2); + + printf (" = %s\n", dumpBstring (b2)); + + ret += (b2->slen != b0->slen); + ret += (0 != rv); + ret += (res == NULL) || ((int) strlen (res) != b2->slen) + || (0 != memcmp (b2->data, res, b2->slen)); + ret += b2->data[b2->slen] != '\0'; + bdestroy (b2); + } else { + ret += (BSTR_ERR != (rv = btolower (b0))); + printf (".\tbtolower (%s) = %d\n", dumpBstring (b0), rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test34 (void) { +int ret = 0; + + printf ("TEST: int btolower (bstring b);\n"); + + /* tests with NULL */ + ret += test34_0 (NULL, NULL); + ret += test34_0 (&badBstring1, NULL); + ret += test34_0 (&badBstring2, NULL); + + /* normal operation tests on all sorts of subranges */ + ret += test34_0 (&emptyBstring, ""); + ret += test34_0 (&shortBstring, "bogus"); + ret += test34_0 (&longBstring, "this is a bogus but reasonably long string. just long enough to cause some mallocing."); + + if (ret) printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test35_0 (const_bstring b0, const_bstring b1, int res) { +int rv, ret = 0; + + ret += (res != (rv = bstricmp (b0, b1))); + printf (".\tbstricmp (%s, %s) = %d\n", dumpBstring (b0), dumpBstring (b1), rv); + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %d)\n", __LINE__, ret, res); + } + return ret; +} + +static int test35 (void) { +int ret = 0; +struct tagbstring t0 = bsStatic ("bOgUs"); +struct tagbstring t1 = bsStatic ("bOgUR"); +struct tagbstring t2 = bsStatic ("bOgUt"); + + printf ("TEST: int bstricmp (const_bstring b0, const_bstring b1);\n"); + + /* tests with NULL */ + ret += test35_0 (NULL, NULL, SHRT_MIN); + ret += test35_0 (&emptyBstring, NULL, SHRT_MIN); + ret += test35_0 (NULL, &emptyBstring, SHRT_MIN); + ret += test35_0 (&emptyBstring, &badBstring1, SHRT_MIN); + ret += test35_0 (&badBstring1, &emptyBstring, SHRT_MIN); + ret += test35_0 (&shortBstring, &badBstring2, SHRT_MIN); + ret += test35_0 (&badBstring2, &shortBstring, SHRT_MIN); + + /* normal operation tests on all sorts of subranges */ + ret += test35_0 (&emptyBstring, &emptyBstring, 0); + ret += test35_0 (&shortBstring, &t0, 0); + ret += test35_0 (&shortBstring, &t1, tolower (shortBstring.data[4]) - tolower (t1.data[4])); + ret += test35_0 (&shortBstring, &t2, tolower (shortBstring.data[4]) - tolower (t2.data[4])); + + t0.slen++; + ret += test35_0 (&shortBstring, &t0, -(UCHAR_MAX+1)); + ret += test35_0 (&t0, &shortBstring, (UCHAR_MAX+1)); + + if (ret) printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test36_0 (const_bstring b0, const_bstring b1, int n, int res) { +int rv, ret = 0; + + ret += (res != (rv = bstrnicmp (b0, b1, n))); + printf (".\tbstrnicmp (%s, %s, %d) = %d\n", dumpBstring (b0), dumpBstring (b1), n, rv); + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %d)\n", __LINE__, ret, res); + } + return ret; +} + +static int test36 (void) { +int ret = 0; +struct tagbstring t0 = bsStatic ("bOgUs"); +struct tagbstring t1 = bsStatic ("bOgUR"); +struct tagbstring t2 = bsStatic ("bOgUt"); + + printf ("TEST: int bstrnicmp (const_bstring b0, const_bstring b1);\n"); + + /* tests with NULL */ + ret += test36_0 (NULL, NULL, 0, SHRT_MIN); + ret += test36_0 (&emptyBstring, NULL, 0, SHRT_MIN); + ret += test36_0 (NULL, &emptyBstring, 0, SHRT_MIN); + ret += test36_0 (&emptyBstring, &badBstring1, 0, SHRT_MIN); + ret += test36_0 (&badBstring1, &emptyBstring, 0, SHRT_MIN); + ret += test36_0 (&shortBstring, &badBstring2, 5, SHRT_MIN); + ret += test36_0 (&badBstring2, &shortBstring, 5, SHRT_MIN); + + /* normal operation tests on all sorts of subranges */ + ret += test36_0 (&emptyBstring, &emptyBstring, 0, 0); + ret += test36_0 (&shortBstring, &t0, 0, 0); + ret += test36_0 (&shortBstring, &t0, 5, 0); + ret += test36_0 (&shortBstring, &t0, 4, 0); + ret += test36_0 (&shortBstring, &t0, 6, 0); + ret += test36_0 (&shortBstring, &t1, 5, shortBstring.data[4] - t1.data[4]); + ret += test36_0 (&shortBstring, &t1, 4, 0); + ret += test36_0 (&shortBstring, &t1, 6, shortBstring.data[4] - t1.data[4]); + ret += test36_0 (&shortBstring, &t2, 5, shortBstring.data[4] - t2.data[4]); + ret += test36_0 (&shortBstring, &t2, 4, 0); + ret += test36_0 (&shortBstring, &t2, 6, shortBstring.data[4] - t2.data[4]); + + t0.slen++; + ret += test36_0 (&shortBstring, &t0, 5, 0); + ret += test36_0 (&shortBstring, &t0, 6, -(UCHAR_MAX+1)); + ret += test36_0 (&t0, &shortBstring, 6, (UCHAR_MAX+1)); + + if (ret) printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test37_0 (const_bstring b0, const_bstring b1, int res) { +int rv, ret = 0; + + ret += (res != (rv = biseqcaseless (b0, b1))); + printf (".\tbiseqcaseless (%s, %s) = %d\n", dumpBstring (b0), dumpBstring (b1), rv); + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %d)\n", __LINE__, ret, res); + } + return ret; +} + +static int test37 (void) { +int ret = 0; +struct tagbstring t0 = bsStatic ("bOgUs"); +struct tagbstring t1 = bsStatic ("bOgUR"); +struct tagbstring t2 = bsStatic ("bOgUt"); + + printf ("TEST: int biseqcaseless (const_bstring b0, const_bstring b1);\n"); + + /* tests with NULL */ + ret += test37_0 (NULL, NULL, BSTR_ERR); + ret += test37_0 (&emptyBstring, NULL, BSTR_ERR); + ret += test37_0 (NULL, &emptyBstring, BSTR_ERR); + ret += test37_0 (&emptyBstring, &badBstring1, BSTR_ERR); + ret += test37_0 (&badBstring1, &emptyBstring, BSTR_ERR); + ret += test37_0 (&shortBstring, &badBstring2, BSTR_ERR); + ret += test37_0 (&badBstring2, &shortBstring, BSTR_ERR); + + /* normal operation tests on all sorts of subranges */ + ret += test37_0 (&emptyBstring, &emptyBstring, 1); + ret += test37_0 (&shortBstring, &t0, 1); + ret += test37_0 (&shortBstring, &t1, 0); + ret += test37_0 (&shortBstring, &t2, 0); + + if (ret) printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test48_0 (const_bstring b, const unsigned char * blk, int len, int res) { +int rv, ret = 0; + + ret += (res != (rv = biseqcaselessblk (b, blk, len))); + printf (".\tbiseqcaselessblk (%s, %s, %d) = %d\n", dumpBstring (b), dumpCstring (blk), len, rv); + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %d)\n", __LINE__, ret, res); + } + return ret; +} + +static int test48 (void) { +int ret = 0; +struct tagbstring t0 = bsStatic ("bOgUs"); +struct tagbstring t1 = bsStatic ("bOgUR"); +struct tagbstring t2 = bsStatic ("bOgUt"); + + printf ("TEST: int biseqcaselessblk (const_bstring b, const void * blk, int len);\n"); + + /* tests with NULL */ + ret += test48_0 (NULL, NULL, 0, BSTR_ERR); + ret += test48_0 (&emptyBstring, NULL, 0, BSTR_ERR); + ret += test48_0 (NULL, emptyBstring.data, 0, BSTR_ERR); + ret += test48_0 (&emptyBstring, badBstring1.data, emptyBstring.slen, BSTR_ERR); + ret += test48_0 (&badBstring1, emptyBstring.data, badBstring1.slen, BSTR_ERR); + ret += test48_0 (&shortBstring, badBstring2.data, badBstring2.slen, BSTR_ERR); + ret += test48_0 (&badBstring2, shortBstring.data, badBstring2.slen, BSTR_ERR); + + /* normal operation tests on all sorts of subranges */ + ret += test48_0 (&emptyBstring, emptyBstring.data, emptyBstring.slen, 1); + ret += test48_0 (&shortBstring, t0.data, t0.slen, 1); + ret += test48_0 (&shortBstring, t1.data, t1.slen, 0); + ret += test48_0 (&shortBstring, t2.data, t2.slen, 0); + + if (ret) printf ("\t# failures: %d\n", ret); + return ret; +} + +struct emuFile { + int ofs; + bstring contents; +}; + +static int test38_aux_bNgetc (struct emuFile * f) { +int v = EOF; + if (NULL != f && EOF != (v = bchare (f->contents, f->ofs, EOF))) f->ofs++; + return v; +} + +static size_t test38_aux_bNread (void *buff, size_t elsize, size_t nelem, struct emuFile * f) { +char * b = (char *) buff; +int v; +size_t i, j, c = 0; + + if (NULL == f || NULL == b) return c; + for (i=0; i < nelem; i++) for (j=0; j < elsize; j++) { + v = test38_aux_bNgetc (f); + if (EOF == v) { + *b = (char) '\0'; + return c; + } else { + *b = (char) v; + b++; + c++; + } + } + + return c; +} + +static int test38_aux_bNopen (struct emuFile * f, bstring b) { + if (NULL == f || NULL == b) return -__LINE__; + f->ofs = 0; + f->contents = b; + return 0; +} + +static int test38 (void) { +struct emuFile f; +bstring b0, b1, b2, b3; +int ret = 0; + + printf ("TEST: bgets/breads test\n"); + + test38_aux_bNopen (&f, &shortBstring); + + /* Creation/reads */ + + b0 = bgets ((bNgetc) test38_aux_bNgetc, &f, (char) 'b'); + b1 = bread ((bNread) test38_aux_bNread, &f); + b2 = bgets ((bNgetc) test38_aux_bNgetc, &f, (char) '\0'); + b3 = bread ((bNread) test38_aux_bNread, &f); + + ret += 1 != biseqcstr (b0, "b"); + ret += 1 != biseqcstr (b1, "ogus"); + ret += NULL != b2; + ret += 1 != biseqcstr (b3, ""); + + /* Bogus accumulations */ + + f.ofs = 0; + + ret += 0 <= bgetsa (NULL, (bNgetc) test38_aux_bNgetc, &f, (char) 'o'); + ret += 0 <= breada (NULL, (bNread) test38_aux_bNread, &f); + ret += 0 <= bgetsa (&shortBstring, (bNgetc) test38_aux_bNgetc, &f, (char) 'o'); + ret += 0 <= breada (&shortBstring, (bNread) test38_aux_bNread, &f); + + /* Normal accumulations */ + + ret += 0 > bgetsa (b0, (bNgetc) test38_aux_bNgetc, &f, (char) 'o'); + ret += 0 > breada (b1, (bNread) test38_aux_bNread, &f); + + ret += 1 != biseqcstr (b0, "bbo"); + ret += 1 != biseqcstr (b1, "ogusgus"); + + /* Attempt to append past end should do nothing */ + + ret += 0 > bgetsa (b0, (bNgetc) test38_aux_bNgetc, &f, (char) 'o'); + ret += 0 > breada (b1, (bNread) test38_aux_bNread, &f); + + ret += 1 != biseqcstr (b0, "bbo"); + ret += 1 != biseqcstr (b1, "ogusgus"); + + bdestroy (b0); + bdestroy (b1); + bdestroy (b2); + bdestroy (b3); + + if (ret) printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test39_0 (const_bstring b, const_bstring lt, const_bstring rt, const_bstring t) { +bstring r; +int ret = 0; + + ret += 0 <= bltrimws (NULL); + ret += 0 <= brtrimws (NULL); + ret += 0 <= btrimws (NULL); + + r = bstrcpy (b); + bwriteprotect (*r); + ret += 0 <= bltrimws (r); + ret += 0 <= brtrimws (r); + ret += 0 <= btrimws (r); + bwriteallow(*r); + ret += 0 != bltrimws (r); + printf (".\tbltrim (%s) = %s\n", dumpBstring (b), dumpBstring (r)); + ret += !biseq (r, lt); + bdestroy (r); + + r = bstrcpy (b); + ret += 0 != brtrimws (r); + printf (".\tbrtrim (%s) = %s\n", dumpBstring (b), dumpBstring (r)); + ret += !biseq (r, rt); + bdestroy (r); + + r = bstrcpy (b); + ret += 0 != btrimws (r); + printf (".\tbtrim (%s) = %s\n", dumpBstring (b), dumpBstring (r)); + ret += !biseq (r, t); + bdestroy (r); + + return ret; +} + +static int test39 (void) { +int ret = 0; +struct tagbstring t0 = bsStatic (" bogus string "); +struct tagbstring t1 = bsStatic ("bogus string "); +struct tagbstring t2 = bsStatic (" bogus string"); +struct tagbstring t3 = bsStatic ("bogus string"); +struct tagbstring t4 = bsStatic (" "); +struct tagbstring t5 = bsStatic (""); + + printf ("TEST: trim functions\n"); + + ret += test39_0 (&t0, &t1, &t2, &t3); + ret += test39_0 (&t1, &t1, &t3, &t3); + ret += test39_0 (&t2, &t3, &t2, &t3); + ret += test39_0 (&t3, &t3, &t3, &t3); + ret += test39_0 (&t4, &t5, &t5, &t5); + ret += test39_0 (&t5, &t5, &t5, &t5); + + if (ret) printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test40_0 (bstring b0, const_bstring b1, int left, int len, const char * res) { +bstring b2; +int rv, ret = 0; + + if (b0 != NULL && b0->data != NULL && b0->slen >= 0 && + b1 != NULL && b1->data != NULL && b1->slen >= 0) { + b2 = bstrcpy (b0); + bwriteprotect (*b2); + + printf (".\tbassignmidstr (%s, ", dumpBstring (b2)); + + rv = bassignmidstr (b2, b1, left, len); + ret += (rv == 0); + if (!biseq (b0, b2)) ret++; + + printf ("%s, %d, %d) = %s\n", dumpBstring (b1), left, len, dumpBstring (b2)); + + bwriteallow (*b2); + + printf (".\tbassignmidstr (%s, ", dumpBstring (b2)); + + rv = bassignmidstr (b2, b1, left, len); + + printf ("%s, %d, %d) = %s\n", dumpBstring (b1), left, len, dumpBstring (b2)); + + if (b1) ret += (b2->slen > len) | (b2->slen < 0); + ret += ((0 != rv) && (b1 != NULL)) || ((0 == rv) && (b1 == NULL)); + ret += (res == NULL) || ((int) strlen (res) != b2->slen) + || (0 != memcmp (b2->data, res, b2->slen)); + ret += b2->data[b2->slen] != '\0'; + bdestroy (b2); + } else { + ret += (BSTR_ERR != (rv = bassignmidstr (b0, b1, left, len))); + printf (".\tbassignmidstr (%s, %s, %d, %d) = %d\n", dumpBstring (b0), dumpBstring (b1), left, len, rv); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test40 (void) { +int ret = 0; + + printf ("TEST: int bassignmidstr (bstring b0, const_bstring b1, int left, int len);\n"); + + /* tests with NULL */ + ret += test40_0 (NULL, NULL, 0, 1, NULL); + ret += test40_0 (NULL, &emptyBstring, 0, 1, NULL); + ret += test40_0 (&emptyBstring, NULL, 0, 1, ""); + ret += test40_0 (&badBstring1, &emptyBstring, 0, 1, NULL); + ret += test40_0 (&badBstring2, &emptyBstring, 0, 1, NULL); + ret += test40_0 (&emptyBstring, &badBstring1, 0, 1, NULL); + ret += test40_0 (&emptyBstring, &badBstring2, 0, 1, NULL); + + /* normal operation tests on all sorts of subranges */ + ret += test40_0 (&emptyBstring, &emptyBstring, 0, 1, ""); + ret += test40_0 (&emptyBstring, &shortBstring, 1, 3, "ogu"); + ret += test40_0 (&shortBstring, &emptyBstring, 0, 1, ""); + ret += test40_0 (&shortBstring, &shortBstring, 1, 3, "ogu"); + ret += test40_0 (&shortBstring, &shortBstring, -1, 4, "bog"); + ret += test40_0 (&shortBstring, &shortBstring, 1, 9, "ogus"); + ret += test40_0 (&shortBstring, &shortBstring, 9, 1, ""); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test41_0 (bstring b1, int left, int len, const char * res) { +struct tagbstring t; +bstring b2, b3; +int ret = 0; + + if (b1 != NULL && b1->data != NULL && b1->slen >= 0) { + b2 = bfromcstr (""); + + bassignmidstr (b2, b1, left, len); + + bmid2tbstr (t, b1, left, len); + b3 = bstrcpy (&t); + + printf (".\tbmid2tbstr (%s, %d, %d) = %s\n", dumpBstring (b1), left, len, dumpBstring (b3)); + + ret += !biseq (&t, b2); + + bdestroy (b2); + bdestroy (b3); + } else { + bmid2tbstr (t, b1, left, len); + b3 = bstrcpy (&t); + ret += t.slen != 0; + + printf (".\tbmid2tbstr (%s, %d, %d) = %s\n", dumpBstring (b1), left, len, dumpBstring (b3)); + bdestroy (b3); + } + + if (ret) { + printf ("\t\tfailure(%d) = %d (res = %p", __LINE__, ret, res); + if (res) printf (" = \"%s\"", res); + printf (")\n"); + } + return ret; +} + +static int test41 (void) { +int ret = 0; + + printf ("TEST: int bmid2tbstr (struct tagbstring &t, const_bstring b1, int left, int len);\n"); + + /* tests with NULL */ + ret += test41_0 (NULL, 0, 1, NULL); + ret += test41_0 (&emptyBstring, 0, 1, NULL); + ret += test41_0 (NULL, 0, 1, ""); + ret += test41_0 (&emptyBstring, 0, 1, NULL); + ret += test41_0 (&emptyBstring, 0, 1, NULL); + ret += test41_0 (&badBstring1, 0, 1, NULL); + ret += test41_0 (&badBstring2, 0, 1, NULL); + + /* normal operation tests on all sorts of subranges */ + ret += test41_0 (&emptyBstring, 0, 1, ""); + ret += test41_0 (&shortBstring, 1, 3, "ogu"); + ret += test41_0 (&emptyBstring, 0, 1, ""); + ret += test41_0 (&shortBstring, 1, 3, "ogu"); + ret += test41_0 (&shortBstring, -1, 4, "bog"); + ret += test41_0 (&shortBstring, 1, 9, "ogus"); + ret += test41_0 (&shortBstring, 9, 1, ""); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test42_0 (const_bstring bi, int len, const char * res) { +bstring b; +int rv, ret = 0; + + rv = btrunc (b = bstrcpy (bi), len); + ret += (len >= 0) ? (rv < 0) : (rv >= 0); + if (res) ret += (0 == biseqcstr (b, res)); + printf (".\tbtrunc (%s, %d) = %s\n", dumpBstring (bi), len, dumpBstring (b)); + bdestroy (b); + return ret; +} + +static int test42 (void) { +int ret = 0; + + printf ("TEST: int btrunc (bstring b, int n);\n"); + + /* tests with NULL */ + ret += 0 <= btrunc (NULL, 2); + ret += 0 <= btrunc (NULL, 0); + ret += 0 <= btrunc (NULL, -1); + + /* write protected */ + ret += 0 <= btrunc (&shortBstring, 2); + ret += 0 <= btrunc (&shortBstring, 0); + ret += 0 <= btrunc (&shortBstring, -1); + + ret += test42_0 (&emptyBstring, 10, ""); + ret += test42_0 (&emptyBstring, 0, ""); + ret += test42_0 (&emptyBstring, -1, NULL); + + ret += test42_0 (&shortBstring, 10, "bogus"); + ret += test42_0 (&shortBstring, 3, "bog"); + ret += test42_0 (&shortBstring, 0, ""); + ret += test42_0 (&shortBstring, -1, NULL); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test43 (void) { +static struct tagbstring ts0 = bsStatic (""); +static struct tagbstring ts1 = bsStatic (" "); +static struct tagbstring ts2 = bsStatic (" abc"); +static struct tagbstring ts3 = bsStatic ("abc "); +static struct tagbstring ts4 = bsStatic (" abc "); +static struct tagbstring ts5 = bsStatic ("abc"); +bstring tstrs[6] = { &ts0, &ts1, &ts2, &ts3, &ts4, &ts5 }; +int ret = 0; +int i; + + printf ("TEST: int btfromblk*trim (struct tagbstring t, void * s, int l);\n"); + + for (i=0; i < 6; i++) { + struct tagbstring t; + bstring b; + + btfromblkltrimws (t, tstrs[i]->data, tstrs[i]->slen); + bltrimws (b = bstrcpy (tstrs[i])); + if (!biseq (b, &t)) { + ret++; + bassign (b, &t); + printf ("btfromblkltrimws failure: <%s> -> <%s>\n", tstrs[i]->data, b->data); + } + printf (".\tbtfromblkltrimws (\"%s\", \"%s\", %d)\n", (char *) bdatae (b, NULL), tstrs[i]->data, tstrs[i]->slen); + bdestroy (b); + + btfromblkrtrimws (t, tstrs[i]->data, tstrs[i]->slen); + brtrimws (b = bstrcpy (tstrs[i])); + if (!biseq (b, &t)) { + ret++; + bassign (b, &t); + printf ("btfromblkrtrimws failure: <%s> -> <%s>\n", tstrs[i]->data, b->data); + } + printf (".\tbtfromblkrtrimws (\"%s\", \"%s\", %d)\n", (char *) bdatae (b, NULL), tstrs[i]->data, tstrs[i]->slen); + bdestroy (b); + + btfromblktrimws (t, tstrs[i]->data, tstrs[i]->slen); + btrimws (b = bstrcpy (tstrs[i])); + if (!biseq (b, &t)) { + ret++; + bassign (b, &t); + printf ("btfromblktrimws failure: <%s> -> <%s>\n", tstrs[i]->data, b->data); + } + printf (".\tbtfromblktrimws (\"%s\", \"%s\", %d)\n", (char *) bdatae (b, NULL), tstrs[i]->data, tstrs[i]->slen); + bdestroy (b); + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test44_0 (const char * str) { +int ret = 0, v; +bstring b; + if (NULL == str) { + ret += 0 <= bassigncstr (NULL, "test"); + printf (".\tbassigncstr (b = %s, NULL)", dumpBstring (b = bfromcstr (""))); + ret += 0 <= (v = bassigncstr (b, NULL)); + printf (" = %d; b -> %s\n", v, dumpBstring (b)); + ret += 0 <= bassigncstr (&shortBstring, NULL); + bdestroy (b); + return ret; + } + + ret += 0 <= bassigncstr (NULL, str); + printf (".\tbassigncstr (b = %s, \"%s\")", dumpBstring (b = bfromcstr ("")), str); + ret += 0 > (v = bassigncstr (b, str)); + printf (" = %d; b -> %s\n", v, dumpBstring (b)); + ret += 0 != strcmp (bdatae (b, ""), str); + ret += ((size_t) b->slen) != strlen (str); + ret += 0 > bassigncstr (b, "xxxxx"); + bwriteprotect(*b) + printf (".\tbassigncstr (b = %s, \"%s\")", dumpBstring (b), str); + ret += 0 <= (v = bassigncstr (b, str)); + printf (" = %d; b -> %s\n", v, dumpBstring (b)); + ret += 0 != strcmp (bdatae (b, ""), "xxxxx"); + ret += ((size_t) b->slen) != strlen ("xxxxx"); + bwriteallow(*b) + ret += 0 <= bassigncstr (&shortBstring, str); + bdestroy (b); + printf (".\tbassigncstr (a = %s, \"%s\")", dumpBstring (&shortBstring), str); + ret += 0 <= (v = bassigncstr (&shortBstring, str)); + printf (" = %d; a -> %s\n", v, dumpBstring (&shortBstring)); + return ret; +} + +static int test44 (void) { +int ret = 0; + + printf ("TEST: int bassigncstr (bstring a, char * str);\n"); + + /* tests with NULL */ + ret += test44_0 (NULL); + + ret += test44_0 (EMPTY_STRING); + ret += test44_0 (SHORT_STRING); + ret += test44_0 (LONG_STRING); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test45_0 (const char * str) { +int ret = 0, v, len; +bstring b; + if (NULL == str) { + ret += 0 <= bassignblk (NULL, "test", 4); + printf (".\tbassignblk (b = %s, NULL, 1)", dumpBstring (b = bfromcstr (""))); + ret += 0 <= (v = bassignblk (b, NULL, 1)); + printf (" = %d; b -> %s\n", v, dumpBstring (b)); + ret += 0 <= bassignblk (&shortBstring, NULL, 1); + bdestroy (b); + return ret; + } + + len = (int) strlen (str); + ret += 0 <= bassignblk (NULL, str, len); + printf (".\tbassignblk (b = %s, \"%s\", %d)", dumpBstring (b = bfromcstr ("")), str, len); + ret += 0 > (v = bassignblk (b, str, len)); + printf (" = %d; b -> %s\n", v, dumpBstring (b)); + ret += 0 != strcmp (bdatae (b, ""), str); + ret += b->slen != len; + ret += 0 > bassigncstr (b, "xxxxx"); + bwriteprotect(*b) + printf (".\tbassignblk (b = %s, \"%s\", %d)", dumpBstring (b), str, len); + ret += 0 <= (v = bassignblk (b, str, len)); + printf (" = %d; b -> %s\n", v, dumpBstring (b)); + ret += 0 != strcmp (bdatae (b, ""), "xxxxx"); + ret += ((size_t) b->slen) != strlen ("xxxxx"); + bwriteallow(*b) + ret += 0 <= bassignblk (&shortBstring, str, len); + bdestroy (b); + printf (".\tbassignblk (a = %s, \"%s\", %d)", dumpBstring (&shortBstring), str, len); + ret += 0 <= (v = bassignblk (&shortBstring, str, len)); + printf (" = %d; a -> %s\n", v, dumpBstring (&shortBstring)); + return ret; +} + +static int test45 (void) { +int ret = 0; + + printf ("TEST: int bassignblk (bstring a, const void * s, int len);\n"); + + /* tests with NULL */ + ret += test45_0 (NULL); + + ret += test45_0 (EMPTY_STRING); + ret += test45_0 (SHORT_STRING); + ret += test45_0 (LONG_STRING); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test46_0 (const_bstring r, bstring b, int count, const char * fmt, ...) { +int ret; +va_list arglist; + + printf (".\tbvcformata (%s, %d, \"%s\", ...) -> ", dumpBstring (b), count, fmt); + va_start (arglist, fmt); + ret = bvcformata (b, count, fmt, arglist); + va_end (arglist); + printf ("%d, %s (%s)\n", ret, dumpBstring (b), dumpBstring (r)); + if (ret < 0) return (NULL != r); + ret += 1 != biseq (r, b); + if (0 != ret) printf ("\t->failed\n"); + return ret; +} + +static int test46_1 (bstring b, const char * fmt, const_bstring r, ...) { +int ret; + + printf (".\tbvformata (&, %s, \"%s\", ...) -> ", dumpBstring (b), fmt); + bvformata (ret, b, fmt, r); + printf ("%d, %s (%s)\n", ret, dumpBstring (b), dumpBstring (r)); + if (ret < 0) return (NULL != r); + ret += 1 != biseq (r, b); + if (0 != ret) printf ("\t->failed\n"); + return ret; +} + +static int test46 (void) { +bstring b, b2; +int ret = 0; + + printf ("TEST: int bvcformata (bstring b, int count, const char * fmt, va_list arg);\n"); + + ret += test46_0 (NULL, NULL, 8, "[%d]", 15); + ret += test46_0 (NULL, &shortBstring, 8, "[%d]", 15); + ret += test46_0 (NULL, &badBstring1, 8, "[%d]", 15); + ret += test46_0 (NULL, &badBstring2, 8, "[%d]", 15); + ret += test46_0 (NULL, &badBstring3, 8, "[%d]", 15); + + b = bfromcstr (""); + ret += test46_0 (&shortBstring, b, shortBstring.slen, "%s", (char *) shortBstring.data); + b->slen = 0; + ret += test46_0 (&shortBstring, b, shortBstring.slen + 1, "%s", (char *) shortBstring.data); + b->slen = 0; + ret += test46_0 (NULL, b, shortBstring.slen-1, "%s", (char *) shortBstring.data); + + printf ("TEST: bvformata (int &ret, bstring b, const char * fmt, lastarg);\n"); + + ret += test46_1 (NULL, "[%d]", NULL, 15); + ret += test46_1 (&shortBstring, "[%d]", NULL, 15); + ret += test46_1 (&badBstring1, "[%d]", NULL, 15); + ret += test46_1 (&badBstring2, "[%d]", NULL, 15); + ret += test46_1 (&badBstring3, "[%d]", NULL, 15); + + b->slen = 0; + ret += test46_1 (b, "%s", &shortBstring, (char *) shortBstring.data); + + b->slen = 0; + ret += test46_1 (b, "%s", &longBstring, (char *) longBstring.data); + + b->slen = 0; + b2 = bfromcstr (EIGHT_CHAR_STRING); + bconcat (b2, b2); + bconcat (b2, b2); + bconcat (b2, b2); + ret += test46_1 (b, "%s%s%s%s%s%s%s%s", b2, + EIGHT_CHAR_STRING, EIGHT_CHAR_STRING, EIGHT_CHAR_STRING, EIGHT_CHAR_STRING, + EIGHT_CHAR_STRING, EIGHT_CHAR_STRING, EIGHT_CHAR_STRING, EIGHT_CHAR_STRING); + bdestroy (b2); + + bdestroy (b); + printf ("\t# failures: %d\n", ret); + return ret; +} + +int main (int argc, char * argv[]) { +int ret = 0; + + argc = argc; + argv = argv; + + printf ("Direct case testing of bstring core functions\n"); + + ret += test0 (); + ret += test1 (); + ret += test2 (); + ret += test3 (); + ret += test4 (); + ret += test5 (); + ret += test6 (); + ret += test7 (); + ret += test8 (); + ret += test9 (); + ret += test10 (); + ret += test11 (); + ret += test12 (); + ret += test13 (); + ret += test14 (); + ret += test15 (); + ret += test16 (); + ret += test17 (); + ret += test18 (); + ret += test19 (); + ret += test20 (); + ret += test21 (); + ret += test22 (); + ret += test23 (); + ret += test24 (); + ret += test25 (); + ret += test26 (); + ret += test27 (); + ret += test28 (); + ret += test29 (); + ret += test30 (); + ret += test31 (); + ret += test32 (); + ret += test33 (); + ret += test34 (); + ret += test35 (); + ret += test36 (); + ret += test37 (); + ret += test38 (); + ret += test39 (); + ret += test40 (); + ret += test41 (); + ret += test42 (); + ret += test43 (); + ret += test44 (); + ret += test45 (); + ret += test46 (); + ret += test47 (); + ret += test48 (); + + printf ("# test failures: %d\n", ret); + + return 0; +} diff --git a/bstrlib/bstraux.c b/bstrlib/bstraux.c new file mode 100644 index 0000000..ac97836 --- /dev/null +++ b/bstrlib/bstraux.c @@ -0,0 +1,1161 @@ + +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2015, and is covered by the BSD open source + * license and the GPL. Refer to the accompanying documentation for details + * on usage and license. + */ + +/* + * bstraux.c + * + * This file is not necessarily part of the core bstring library itself, but + * is just an auxilliary module which includes miscellaneous or trivial + * functions. + */ + +#if defined (_MSC_VER) +# define _CRT_SECURE_NO_WARNINGS +#endif + +#include +#include +#include +#include +#include +#include "bstrlib.h" +#include "bstraux.h" + +#ifndef UNUSED +#define UNUSED(x) (void)(x) +#endif + +/* bstring bTail (bstring b, int n) + * + * Return with a string of the last n characters of b. + */ +bstring bTail (bstring b, int n) { + if (b == NULL || n < 0 || (b->mlen < b->slen && b->mlen > 0)) return NULL; + if (n >= b->slen) return bstrcpy (b); + return bmidstr (b, b->slen - n, n); +} + +/* bstring bHead (bstring b, int n) + * + * Return with a string of the first n characters of b. + */ +bstring bHead (bstring b, int n) { + if (b == NULL || n < 0 || (b->mlen < b->slen && b->mlen > 0)) return NULL; + if (n >= b->slen) return bstrcpy (b); + return bmidstr (b, 0, n); +} + +/* int bFill (bstring a, char c, int len) + * + * Fill a given bstring with the character in parameter c, for a length n. + */ +int bFill (bstring b, char c, int len) { + if (b == NULL || len < 0 || (b->mlen < b->slen && b->mlen > 0)) return -__LINE__; + b->slen = 0; + return bsetstr (b, len, NULL, c); +} + +/* int bReplicate (bstring b, int n) + * + * Replicate the contents of b end to end n times and replace it in b. + */ +int bReplicate (bstring b, int n) { + return bpattern (b, n * b->slen); +} + +/* int bReverse (bstring b) + * + * Reverse the contents of b in place. + */ +int bReverse (bstring b) { +int i, n, m; +unsigned char t; + + if (b == NULL || b->slen < 0 || b->mlen < b->slen) return -__LINE__; + n = b->slen; + if (2 <= n) { + m = ((unsigned)n) >> 1; + n--; + for (i=0; i < m; i++) { + t = b->data[n - i]; + b->data[n - i] = b->data[i]; + b->data[i] = t; + } + } + return 0; +} + +/* int bInsertChrs (bstring b, int pos, int len, unsigned char c, unsigned char fill) + * + * Insert a repeated sequence of a given character into the string at + * position pos for a length len. + */ +int bInsertChrs (bstring b, int pos, int len, unsigned char c, unsigned char fill) { + if (b == NULL || b->slen < 0 || b->mlen < b->slen || pos < 0 || len <= 0) return -__LINE__; + + if (pos > b->slen + && 0 > bsetstr (b, pos, NULL, fill)) return -__LINE__; + + if (0 > balloc (b, b->slen + len)) return -__LINE__; + if (pos < b->slen) memmove (b->data + pos + len, b->data + pos, b->slen - pos); + memset (b->data + pos, c, len); + b->slen += len; + b->data[b->slen] = (unsigned char) '\0'; + return BSTR_OK; +} + +/* int bJustifyLeft (bstring b, int space) + * + * Left justify a string. + */ +int bJustifyLeft (bstring b, int space) { +int j, i, s, t; +unsigned char c = (unsigned char) space; + + if (b == NULL || b->slen < 0 || b->mlen < b->slen) return -__LINE__; + if (space != (int) c) return BSTR_OK; + + for (s=j=i=0; i < b->slen; i++) { + t = s; + s = c != (b->data[j] = b->data[i]); + j += (t|s); + } + if (j > 0 && b->data[j-1] == c) j--; + + b->data[j] = (unsigned char) '\0'; + b->slen = j; + return BSTR_OK; +} + +/* int bJustifyRight (bstring b, int width, int space) + * + * Right justify a string to within a given width. + */ +int bJustifyRight (bstring b, int width, int space) { +int ret; + if (width <= 0) return -__LINE__; + if (0 > (ret = bJustifyLeft (b, space))) return ret; + if (b->slen <= width) + return bInsertChrs (b, 0, width - b->slen, (unsigned char) space, (unsigned char) space); + return BSTR_OK; +} + +/* int bJustifyCenter (bstring b, int width, int space) + * + * Center a string's non-white space characters to within a given width by + * inserting whitespaces at the beginning. + */ +int bJustifyCenter (bstring b, int width, int space) { +int ret; + if (width <= 0) return -__LINE__; + if (0 > (ret = bJustifyLeft (b, space))) return ret; + if (b->slen <= width) + return bInsertChrs (b, 0, (width - b->slen + 1) >> 1, (unsigned char) space, (unsigned char) space); + return BSTR_OK; +} + +/* int bJustifyMargin (bstring b, int width, int space) + * + * Stretch a string to flush against left and right margins by evenly + * distributing additional white space between words. If the line is too + * long to be margin justified, it is left justified. + */ +int bJustifyMargin (bstring b, int width, int space) { +struct bstrList * sl; +int i, l, c; + + if (b == NULL || b->slen < 0 || b->mlen == 0 || b->mlen < b->slen) return -__LINE__; + if (NULL == (sl = bsplit (b, (unsigned char) space))) return -__LINE__; + for (l=c=i=0; i < sl->qty; i++) { + if (sl->entry[i]->slen > 0) { + c ++; + l += sl->entry[i]->slen; + } + } + + if (l + c >= width || c < 2) { + bstrListDestroy (sl); + return bJustifyLeft (b, space); + } + + b->slen = 0; + for (i=0; i < sl->qty; i++) { + if (sl->entry[i]->slen > 0) { + if (b->slen > 0) { + int s = (width - l + (c / 2)) / c; + bInsertChrs (b, b->slen, s, (unsigned char) space, (unsigned char) space); + l += s; + } + bconcat (b, sl->entry[i]); + c--; + if (c <= 0) break; + } + } + + bstrListDestroy (sl); + return BSTR_OK; +} + +static size_t readNothing (void *buff, size_t elsize, size_t nelem, void *parm) { + UNUSED(buff); + UNUSED(elsize); + UNUSED(nelem); + UNUSED(parm); + return 0; /* Immediately indicate EOF. */ +} + +/* struct bStream * bsFromBstr (const_bstring b); + * + * Create a bStream whose contents are a copy of the bstring passed in. + * This allows the use of all the bStream APIs with bstrings. + */ +struct bStream * bsFromBstr (const_bstring b) { +struct bStream * s = bsopen ((bNread) readNothing, NULL); + bsunread (s, b); /* Push the bstring data into the empty bStream. */ + return s; +} + +static size_t readRef (void *buff, size_t elsize, size_t nelem, void *parm) { +struct tagbstring * t = (struct tagbstring *) parm; +size_t tsz = elsize * nelem; + + if (tsz > (size_t) t->slen) tsz = (size_t) t->slen; + if (tsz > 0) { + memcpy (buff, t->data, tsz); + t->slen -= (int) tsz; + t->data += tsz; + return tsz / elsize; + } + return 0; +} + +/* The "by reference" version of the above function. This function puts + * a number of restrictions on the call site (the passed in struct + * tagbstring *will* be modified by this function, and the source data + * must remain alive and constant for the lifetime of the bStream). + * Hence it is not presented as an extern. + */ +static struct bStream * bsFromBstrRef (struct tagbstring * t) { + if (!t) return NULL; + return bsopen ((bNread) readRef, t); +} + +/* char * bStr2NetStr (const_bstring b) + * + * Convert a bstring to a netstring. See + * http://cr.yp.to/proto/netstrings.txt for a description of netstrings. + * Note: 1) The value returned should be freed with a call to bcstrfree() at + * the point when it will no longer be referenced to avoid a memory + * leak. + * 2) If the returned value is non-NULL, then it also '\0' terminated + * in the character position one past the "," terminator. + */ +char * bStr2NetStr (const_bstring b) { +char strnum[sizeof (b->slen) * 3 + 1]; +bstring s; +unsigned char * buff; + + if (b == NULL || b->data == NULL || b->slen < 0) return NULL; + sprintf (strnum, "%d:", b->slen); + if (NULL == (s = bfromcstr (strnum)) + || bconcat (s, b) == BSTR_ERR || bconchar (s, (char) ',') == BSTR_ERR) { + bdestroy (s); + return NULL; + } + buff = s->data; + bcstrfree ((char *) s); + return (char *) buff; +} + +/* bstring bNetStr2Bstr (const char * buf) + * + * Convert a netstring to a bstring. See + * http://cr.yp.to/proto/netstrings.txt for a description of netstrings. + * Note that the terminating "," *must* be present, however a following '\0' + * is *not* required. + */ +bstring bNetStr2Bstr (const char * buff) { +int i, x; +bstring b; + if (buff == NULL) return NULL; + x = 0; + for (i=0; buff[i] != ':'; i++) { + unsigned int v = buff[i] - '0'; + if (v > 9 || x > ((INT_MAX - (signed int)v) / 10)) return NULL; + x = (x * 10) + v; + } + + /* This thing has to be properly terminated */ + if (buff[i + 1 + x] != ',') return NULL; + + if (NULL == (b = bfromcstr (""))) return NULL; + if (balloc (b, x + 1) != BSTR_OK) { + bdestroy (b); + return NULL; + } + memcpy (b->data, buff + i + 1, x); + b->data[x] = (unsigned char) '\0'; + b->slen = x; + return b; +} + +static char b64ETable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/* bstring bBase64Encode (const_bstring b) + * + * Generate a base64 encoding. See: RFC1341 + */ +bstring bBase64Encode (const_bstring b) { +int i, c0, c1, c2, c3; +bstring out; + + if (b == NULL || b->slen < 0 || b->data == NULL) return NULL; + + out = bfromcstr (""); + for (i=0; i + 2 < b->slen; i += 3) { + if (i && ((i % 57) == 0)) { + if (bconchar (out, (char) '\015') < 0 || bconchar (out, (char) '\012') < 0) { + bdestroy (out); + return NULL; + } + } + c0 = b->data[i] >> 2; + c1 = ((b->data[i] << 4) | + (b->data[i+1] >> 4)) & 0x3F; + c2 = ((b->data[i+1] << 2) | + (b->data[i+2] >> 6)) & 0x3F; + c3 = b->data[i+2] & 0x3F; + if (bconchar (out, b64ETable[c0]) < 0 || + bconchar (out, b64ETable[c1]) < 0 || + bconchar (out, b64ETable[c2]) < 0 || + bconchar (out, b64ETable[c3]) < 0) { + bdestroy (out); + return NULL; + } + } + + if (i && ((i % 57) == 0)) { + if (bconchar (out, (char) '\015') < 0 || bconchar (out, (char) '\012') < 0) { + bdestroy (out); + return NULL; + } + } + + switch (i + 2 - b->slen) { + case 0: c0 = b->data[i] >> 2; + c1 = ((b->data[i] << 4) | + (b->data[i+1] >> 4)) & 0x3F; + c2 = (b->data[i+1] << 2) & 0x3F; + if (bconchar (out, b64ETable[c0]) < 0 || + bconchar (out, b64ETable[c1]) < 0 || + bconchar (out, b64ETable[c2]) < 0 || + bconchar (out, (char) '=') < 0) { + bdestroy (out); + return NULL; + } + break; + case 1: c0 = b->data[i] >> 2; + c1 = (b->data[i] << 4) & 0x3F; + if (bconchar (out, b64ETable[c0]) < 0 || + bconchar (out, b64ETable[c1]) < 0 || + bconchar (out, (char) '=') < 0 || + bconchar (out, (char) '=') < 0) { + bdestroy (out); + return NULL; + } + break; + case 2: break; + } + + return out; +} + +#define B64_PAD (-2) +#define B64_ERR (-1) + +static int base64DecodeSymbol (unsigned char alpha) { + if ((alpha >= 'A') && (alpha <= 'Z')) return (int)(alpha - 'A'); + else if ((alpha >= 'a') && (alpha <= 'z')) + return 26 + (int)(alpha - 'a'); + else if ((alpha >= '0') && (alpha <= '9')) + return 52 + (int)(alpha - '0'); + else if (alpha == '+') return 62; + else if (alpha == '/') return 63; + else if (alpha == '=') return B64_PAD; + else return B64_ERR; +} + +/* bstring bBase64DecodeEx (const_bstring b, int * boolTruncError) + * + * Decode a base64 block of data. All MIME headers are assumed to have been + * removed. See: RFC1341 + */ +bstring bBase64DecodeEx (const_bstring b, int * boolTruncError) { +int i, v; +unsigned char c0, c1, c2; +bstring out; + + if (b == NULL || b->slen < 0 || b->data == NULL) return NULL; + if (boolTruncError) *boolTruncError = 0; + out = bfromcstr (""); + i = 0; + for (;;) { + do { + if (i >= b->slen) return out; + if (b->data[i] == '=') { /* Bad "too early" truncation */ + if (boolTruncError) { + *boolTruncError = 1; + return out; + } + bdestroy (out); + return NULL; + } + v = base64DecodeSymbol (b->data[i]); + i++; + } while (v < 0); + c0 = (unsigned char) (v << 2); + do { + if (i >= b->slen || b->data[i] == '=') { /* Bad "too early" truncation */ + if (boolTruncError) { + *boolTruncError = 1; + return out; + } + bdestroy (out); + return NULL; + } + v = base64DecodeSymbol (b->data[i]); + i++; + } while (v < 0); + c0 |= (unsigned char) (v >> 4); + c1 = (unsigned char) (v << 4); + do { + if (i >= b->slen) { + if (boolTruncError) { + *boolTruncError = 1; + return out; + } + bdestroy (out); + return NULL; + } + if (b->data[i] == '=') { + i++; + if (i >= b->slen || b->data[i] != '=' || bconchar (out, c0) < 0) { + if (boolTruncError) { + *boolTruncError = 1; + return out; + } + bdestroy (out); /* Missing "=" at the end. */ + return NULL; + } + return out; + } + v = base64DecodeSymbol (b->data[i]); + i++; + } while (v < 0); + c1 |= (unsigned char) (v >> 2); + c2 = (unsigned char) (v << 6); + do { + if (i >= b->slen) { + if (boolTruncError) { + *boolTruncError = 1; + return out; + } + bdestroy (out); + return NULL; + } + if (b->data[i] == '=') { + if (bconchar (out, c0) < 0 || bconchar (out, c1) < 0) { + if (boolTruncError) { + *boolTruncError = 1; + return out; + } + bdestroy (out); + return NULL; + } + if (boolTruncError) *boolTruncError = 0; + return out; + } + v = base64DecodeSymbol (b->data[i]); + i++; + } while (v < 0); + c2 |= (unsigned char) (v); + if (bconchar (out, c0) < 0 || + bconchar (out, c1) < 0 || + bconchar (out, c2) < 0) { + if (boolTruncError) { + *boolTruncError = -1; + return out; + } + bdestroy (out); + return NULL; + } + } +} + +#define UU_DECODE_BYTE(b) (((b) == (signed int)'`') ? 0 : (b) - (signed int)' ') + +struct bUuInOut { + bstring src, dst; + int * badlines; +}; + +#define UU_MAX_LINELEN 45 + +static int bUuDecLine (void * parm, int ofs, int len) { +struct bUuInOut * io = (struct bUuInOut *) parm; +bstring s = io->src; +bstring t = io->dst; +int i, llen, otlen, ret, c0, c1, c2, c3, d0, d1, d2, d3; + + if (len == 0) return 0; + llen = UU_DECODE_BYTE (s->data[ofs]); + ret = 0; + + otlen = t->slen; + + if (((unsigned) llen) > UU_MAX_LINELEN) { ret = -__LINE__; + goto bl; + } + + llen += t->slen; + + for (i=1; i < s->slen && t->slen < llen;i += 4) { + unsigned char outoctet[3]; + c0 = UU_DECODE_BYTE (d0 = (int) bchare (s, i+ofs+0, ' ' - 1)); + c1 = UU_DECODE_BYTE (d1 = (int) bchare (s, i+ofs+1, ' ' - 1)); + c2 = UU_DECODE_BYTE (d2 = (int) bchare (s, i+ofs+2, ' ' - 1)); + c3 = UU_DECODE_BYTE (d3 = (int) bchare (s, i+ofs+3, ' ' - 1)); + + if (((unsigned) (c0|c1) >= 0x40)) { if (!ret) ret = -__LINE__; + if (d0 > 0x60 || (d0 < (' ' - 1) && !isspace (d0)) || + d1 > 0x60 || (d1 < (' ' - 1) && !isspace (d1))) { + t->slen = otlen; + goto bl; + } + c0 = c1 = 0; + } + outoctet[0] = (unsigned char) ((c0 << 2) | ((unsigned) c1 >> 4)); + if (t->slen+1 >= llen) { + if (0 > bconchar (t, (char) outoctet[0])) return -__LINE__; + break; + } + if ((unsigned) c2 >= 0x40) { if (!ret) ret = -__LINE__; + if (d2 > 0x60 || (d2 < (' ' - 1) && !isspace (d2))) { + t->slen = otlen; + goto bl; + } + c2 = 0; + } + outoctet[1] = (unsigned char) ((c1 << 4) | ((unsigned) c2 >> 2)); + if (t->slen+2 >= llen) { + if (0 > bcatblk (t, outoctet, 2)) return -__LINE__; + break; + } + if ((unsigned) c3 >= 0x40) { if (!ret) ret = -__LINE__; + if (d3 > 0x60 || (d3 < (' ' - 1) && !isspace (d3))) { + t->slen = otlen; + goto bl; + } + c3 = 0; + } + outoctet[2] = (unsigned char) ((c2 << 6) | ((unsigned) c3)); + if (0 > bcatblk (t, outoctet, 3)) return -__LINE__; + } + if (t->slen < llen) { if (0 == ret) ret = -__LINE__; + t->slen = otlen; + } + bl:; + if (ret && io->badlines) { + (*io->badlines)++; + return 0; + } + return ret; +} + +/* bstring bUuDecodeEx (const_bstring src, int * badlines) + * + * Performs a UUDecode of a block of data. If there are errors in the + * decoding, they are counted up and returned in "badlines", if badlines is + * not NULL. It is assumed that the "begin" and "end" lines have already + * been stripped off. The potential security problem of writing the + * filename in the begin line is something that is beyond the scope of a + * portable library. + */ + +#ifdef _MSC_VER +#pragma warning(disable:4204) +#endif + +bstring bUuDecodeEx (const_bstring src, int * badlines) { +struct tagbstring t; +struct bStream * s; +struct bStream * d; +bstring b; + + if (!src) return NULL; + t = *src; /* Short lifetime alias to header of src */ + s = bsFromBstrRef (&t); /* t is undefined after this */ + if (!s) return NULL; + d = bsUuDecode (s, badlines); + b = bfromcstralloc (256, ""); + if (NULL == b || 0 > bsread (b, d, INT_MAX)) { + bdestroy (b); + b = NULL; + } + bsclose (d); + bsclose (s); + return b; +} + +struct bsUuCtx { + struct bUuInOut io; + struct bStream * sInp; +}; + +static size_t bsUuDecodePart (void *buff, size_t elsize, size_t nelem, void *parm) { +static struct tagbstring eol = bsStatic ("\r\n"); +struct bsUuCtx * luuCtx = (struct bsUuCtx *) parm; +size_t tsz; +int l, lret; + + if (NULL == buff || NULL == parm) return 0; + tsz = elsize * nelem; + + CheckInternalBuffer:; + /* If internal buffer has sufficient data, just output it */ + if (((size_t) luuCtx->io.dst->slen) > tsz) { + memcpy (buff, luuCtx->io.dst->data, tsz); + bdelete (luuCtx->io.dst, 0, (int) tsz); + return nelem; + } + + DecodeMore:; + if (0 <= (l = binchr (luuCtx->io.src, 0, &eol))) { + int ol = 0; + struct tagbstring t; + bstring s = luuCtx->io.src; + luuCtx->io.src = &t; + + do { + if (l > ol) { + bmid2tbstr (t, s, ol, l - ol); + lret = bUuDecLine (&luuCtx->io, 0, t.slen); + if (0 > lret) { + luuCtx->io.src = s; + goto Done; + } + } + ol = l + 1; + if (((size_t) luuCtx->io.dst->slen) > tsz) break; + l = binchr (s, ol, &eol); + } while (BSTR_ERR != l); + bdelete (s, 0, ol); + luuCtx->io.src = s; + goto CheckInternalBuffer; + } + + if (BSTR_ERR != bsreada (luuCtx->io.src, luuCtx->sInp, bsbufflength (luuCtx->sInp, BSTR_BS_BUFF_LENGTH_GET))) { + goto DecodeMore; + } + + bUuDecLine (&luuCtx->io, 0, luuCtx->io.src->slen); + + Done:; + /* Output any lingering data that has been translated */ + if (((size_t) luuCtx->io.dst->slen) > 0) { + if (((size_t) luuCtx->io.dst->slen) > tsz) goto CheckInternalBuffer; + memcpy (buff, luuCtx->io.dst->data, luuCtx->io.dst->slen); + tsz = luuCtx->io.dst->slen / elsize; + luuCtx->io.dst->slen = 0; + if (tsz > 0) return tsz; + } + + /* Deallocate once EOF becomes triggered */ + bdestroy (luuCtx->io.dst); + bdestroy (luuCtx->io.src); + free (luuCtx); + return 0; +} + +/* bStream * bsUuDecode (struct bStream * sInp, int * badlines) + * + * Creates a bStream which performs the UUDecode of an an input stream. If + * there are errors in the decoding, they are counted up and returned in + * "badlines", if badlines is not NULL. It is assumed that the "begin" and + * "end" lines have already been stripped off. The potential security + * problem of writing the filename in the begin line is something that is + * beyond the scope of a portable library. + */ + +struct bStream * bsUuDecode (struct bStream * sInp, int * badlines) { +struct bsUuCtx * luuCtx = (struct bsUuCtx *) malloc (sizeof (struct bsUuCtx)); +struct bStream * sOut; + + if (NULL == luuCtx) return NULL; + + luuCtx->io.src = bfromcstr (""); + luuCtx->io.dst = bfromcstr (""); + if (NULL == luuCtx->io.dst || NULL == luuCtx->io.src) { + CleanUpFailureToAllocate:; + bdestroy (luuCtx->io.dst); + bdestroy (luuCtx->io.src); + free (luuCtx); + return NULL; + } + luuCtx->io.badlines = badlines; + if (badlines) *badlines = 0; + + luuCtx->sInp = sInp; + + sOut = bsopen ((bNread) bsUuDecodePart, luuCtx); + if (NULL == sOut) goto CleanUpFailureToAllocate; + return sOut; +} + +#define UU_ENCODE_BYTE(b) (char) (((b) == 0) ? '`' : ((b) + ' ')) + +/* bstring bUuEncode (const_bstring src) + * + * Performs a UUEncode of a block of data. The "begin" and "end" lines are + * not appended. + */ +bstring bUuEncode (const_bstring src) { +bstring out; +int i, j, jm; +unsigned int c0, c1, c2; + if (src == NULL || src->slen < 0 || src->data == NULL) return NULL; + if ((out = bfromcstr ("")) == NULL) return NULL; + for (i=0; i < src->slen; i += UU_MAX_LINELEN) { + if ((jm = i + UU_MAX_LINELEN) > src->slen) jm = src->slen; + if (bconchar (out, UU_ENCODE_BYTE (jm - i)) < 0) { + bstrFree (out); + break; + } + for (j = i; j < jm; j += 3) { + c0 = (unsigned int) bchar (src, j ); + c1 = (unsigned int) bchar (src, j + 1); + c2 = (unsigned int) bchar (src, j + 2); + if (bconchar (out, UU_ENCODE_BYTE ( (c0 & 0xFC) >> 2)) < 0 || + bconchar (out, UU_ENCODE_BYTE (((c0 & 0x03) << 4) | ((c1 & 0xF0) >> 4))) < 0 || + bconchar (out, UU_ENCODE_BYTE (((c1 & 0x0F) << 2) | ((c2 & 0xC0) >> 6))) < 0 || + bconchar (out, UU_ENCODE_BYTE ( (c2 & 0x3F))) < 0) { + bstrFree (out); + goto End; + } + } + if (bconchar (out, (char) '\r') < 0 || bconchar (out, (char) '\n') < 0) { + bstrFree (out); + break; + } + } + End:; + return out; +} + +/* bstring bYEncode (const_bstring src) + * + * Performs a YEncode of a block of data. No header or tail info is + * appended. See: http://www.yenc.org/whatis.htm and + * http://www.yenc.org/yenc-draft.1.3.txt + */ +bstring bYEncode (const_bstring src) { +int i; +bstring out; +unsigned char c; + + if (src == NULL || src->slen < 0 || src->data == NULL) return NULL; + if ((out = bfromcstr ("")) == NULL) return NULL; + for (i=0; i < src->slen; i++) { + c = (unsigned char)(src->data[i] + 42); + if (c == '=' || c == '\0' || c == '\r' || c == '\n') { + if (0 > bconchar (out, (char) '=')) { + bdestroy (out); + return NULL; + } + c += (unsigned char) 64; + } + if (0 > bconchar (out, c)) { + bdestroy (out); + return NULL; + } + } + return out; +} + +/* bstring bYDecode (const_bstring src) + * + * Performs a YDecode of a block of data. See: + * http://www.yenc.org/whatis.htm and http://www.yenc.org/yenc-draft.1.3.txt + */ +#define MAX_OB_LEN (64) + +bstring bYDecode (const_bstring src) { +int i; +bstring out; +unsigned char c; +unsigned char octetbuff[MAX_OB_LEN]; +int obl; + + if (src == NULL || src->slen < 0 || src->data == NULL) return NULL; + if ((out = bfromcstr ("")) == NULL) return NULL; + + obl = 0; + + for (i=0; i < src->slen; i++) { + if ('=' == (c = src->data[i])) { /* The = escape mode */ + i++; + if (i >= src->slen) { + bdestroy (out); + return NULL; + } + c = (unsigned char) (src->data[i] - 64); + } else { + if ('\0' == c) { + bdestroy (out); + return NULL; + } + + /* Extraneous CR/LFs are to be ignored. */ + if (c == '\r' || c == '\n') continue; + } + + octetbuff[obl] = (unsigned char) ((int) c - 42); + obl++; + + if (obl >= MAX_OB_LEN) { + if (0 > bcatblk (out, octetbuff, obl)) { + bdestroy (out); + return NULL; + } + obl = 0; + } + } + + if (0 > bcatblk (out, octetbuff, obl)) { + bdestroy (out); + out = NULL; + } + return out; +} + +/* int bSGMLEncode (bstring b) + * + * Change the string into a version that is quotable in SGML (HTML, XML). + */ +int bSGMLEncode (bstring b) { +static struct tagbstring fr[4][2] = { + { bsStatic("&"), bsStatic("&") }, + { bsStatic("\""), bsStatic(""") }, + { bsStatic("<"), bsStatic("<") }, + { bsStatic(">"), bsStatic(">") } }; +int i; + for (i = 0; i < 4; i++) { + int ret = bfindreplace (b, &fr[i][0], &fr[i][1], 0); + if (0 > ret) return ret; + } + return 0; +} + +/* bstring bStrfTime (const char * fmt, const struct tm * timeptr) + * + * Takes a format string that is compatible with strftime and a struct tm + * pointer, formats the time according to the format string and outputs + * the bstring as a result. Note that if there is an early generation of a + * '\0' character, the bstring will be truncated to this end point. + */ +bstring bStrfTime (const char * fmt, const struct tm * timeptr) { +#if defined (__TURBOC__) && !defined (__BORLANDC__) +static struct tagbstring ns = bsStatic ("bStrfTime Not supported"); + fmt = fmt; + timeptr = timeptr; + return &ns; +#else +bstring buff; +int n; +size_t r; + + if (fmt == NULL) return NULL; + + /* Since the length is not determinable beforehand, a search is + performed using the truncating "strftime" call on increasing + potential sizes for the output result. */ + + if ((n = (int) (2*strlen (fmt))) < 16) n = 16; + buff = bfromcstralloc (n+2, ""); + + for (;;) { + if (BSTR_OK != balloc (buff, n + 2)) { + bdestroy (buff); + return NULL; + } + + r = strftime ((char *) buff->data, n + 1, fmt, timeptr); + + if (r > 0) { + buff->slen = (int) r; + break; + } + + n += n; + } + + return buff; +#endif +} + +/* int bSetCstrChar (bstring a, int pos, char c) + * + * Sets the character at position pos to the character c in the bstring a. + * If the character c is NUL ('\0') then the string is truncated at this + * point. Note: this does not enable any other '\0' character in the bstring + * as terminator indicator for the string. pos must be in the position + * between 0 and b->slen inclusive, otherwise BSTR_ERR will be returned. + */ +int bSetCstrChar (bstring b, int pos, char c) { + if (NULL == b || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen) + return BSTR_ERR; + if (pos < 0 || pos > b->slen) return BSTR_ERR; + + if (pos == b->slen) { + if ('\0' != c) return bconchar (b, c); + return 0; + } + + b->data[pos] = (unsigned char) c; + if ('\0' == c) b->slen = pos; + + return 0; +} + +/* int bSetChar (bstring b, int pos, char c) + * + * Sets the character at position pos to the character c in the bstring a. + * The string is not truncated if the character c is NUL ('\0'). pos must + * be in the position between 0 and b->slen inclusive, otherwise BSTR_ERR + * will be returned. + */ +int bSetChar (bstring b, int pos, char c) { + if (NULL == b || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen) + return BSTR_ERR; + if (pos < 0 || pos > b->slen) return BSTR_ERR; + + if (pos == b->slen) { + return bconchar (b, c); + } + + b->data[pos] = (unsigned char) c; + return 0; +} + +#define INIT_SECURE_INPUT_LENGTH (256) + +/* bstring bSecureInput (int maxlen, int termchar, + * bNgetc vgetchar, void * vgcCtx) + * + * Read input from an abstracted input interface, for a length of at most + * maxlen characters. If maxlen <= 0, then there is no length limit put + * on the input. The result is terminated early if vgetchar() return EOF + * or the user specified value termchar. + * + */ +bstring bSecureInput (int maxlen, int termchar, bNgetc vgetchar, void * vgcCtx) { +int i, m, c; +bstring b, t; + + if (!vgetchar) return NULL; + + b = bfromcstralloc (INIT_SECURE_INPUT_LENGTH, ""); + if ((c = UCHAR_MAX + 1) == termchar) c++; + + for (i=0; ; i++) { + if (termchar == c || (maxlen > 0 && i >= maxlen)) break; + c = vgetchar (vgcCtx); + if (EOF == c) break; + + if (i+1 >= b->mlen) { + + /* Double size, and deal with numeric overflows */ + + if (b->mlen <= INT_MAX / 2) m = b->mlen << 1; + else if (b->mlen <= INT_MAX - 1024) m = b->mlen + 1024; + else if (b->mlen <= INT_MAX - 16) m = b->mlen + 16; + else if (b->mlen <= INT_MAX - 1) m = b->mlen + 1; + else { + bSecureDestroy (b); /* Cleanse partial buffer */ + return NULL; + } + + t = bfromcstrrangealloc (b->mlen + 1, m, ""); + if (t) memcpy (t->data, b->data, i); + bSecureDestroy (b); /* Cleanse previous buffer */ + b = t; + if (!b) return b; + } + + b->data[i] = (unsigned char) c; + } + + b->slen = i; + b->data[i] = (unsigned char) '\0'; + return b; +} + +#define BWS_BUFF_SZ (1024) + +struct bwriteStream { + bstring buff; /* Buffer for underwrites */ + void * parm; /* The stream handle for core stream */ + bNwrite writeFn; /* fwrite work-a-like fnptr for core stream */ + int isEOF; /* track stream's EOF state */ + int minBuffSz; +}; + +/* struct bwriteStream * bwsOpen (bNwrite writeFn, void * parm) + * + * Wrap a given open stream (described by a fwrite work-a-like function + * pointer and stream handle) into an open bwriteStream suitable for write + * streaming functions. + */ +struct bwriteStream * bwsOpen (bNwrite writeFn, void * parm) { +struct bwriteStream * ws; + + if (NULL == writeFn) return NULL; + ws = (struct bwriteStream *) malloc (sizeof (struct bwriteStream)); + if (ws) { + if (NULL == (ws->buff = bfromcstr (""))) { + free (ws); + ws = NULL; + } else { + ws->parm = parm; + ws->writeFn = writeFn; + ws->isEOF = 0; + ws->minBuffSz = BWS_BUFF_SZ; + } + } + return ws; +} + +#define internal_bwswriteout(ws,b) { \ + if ((b)->slen > 0) { \ + if (1 != (ws->writeFn ((b)->data, (b)->slen, 1, ws->parm))) { \ + ws->isEOF = 1; \ + return BSTR_ERR; \ + } \ + } \ +} + +/* int bwsWriteFlush (struct bwriteStream * ws) + * + * Force any pending data to be written to the core stream. + */ +int bwsWriteFlush (struct bwriteStream * ws) { + if (NULL == ws || ws->isEOF || 0 >= ws->minBuffSz || + NULL == ws->writeFn || NULL == ws->buff) return BSTR_ERR; + internal_bwswriteout (ws, ws->buff); + ws->buff->slen = 0; + return 0; +} + +/* int bwsWriteBstr (struct bwriteStream * ws, const_bstring b) + * + * Send a bstring to a bwriteStream. If the stream is at EOF BSTR_ERR is + * returned. Note that there is no deterministic way to determine the exact + * cut off point where the core stream stopped accepting data. + */ +int bwsWriteBstr (struct bwriteStream * ws, const_bstring b) { +struct tagbstring t; +int l; + + if (NULL == ws || NULL == b || NULL == ws->buff || + ws->isEOF || 0 >= ws->minBuffSz || NULL == ws->writeFn) + return BSTR_ERR; + + /* Buffer prepacking optimization */ + if (b->slen > 0 && ws->buff->mlen - ws->buff->slen > b->slen) { + static struct tagbstring empty = bsStatic (""); + if (0 > bconcat (ws->buff, b)) return BSTR_ERR; + return bwsWriteBstr (ws, &empty); + } + + if (0 > (l = ws->minBuffSz - ws->buff->slen)) { + internal_bwswriteout (ws, ws->buff); + ws->buff->slen = 0; + l = ws->minBuffSz; + } + + if (b->slen < l) return bconcat (ws->buff, b); + + if (0 > bcatblk (ws->buff, b->data, l)) return BSTR_ERR; + internal_bwswriteout (ws, ws->buff); + ws->buff->slen = 0; + + bmid2tbstr (t, (bstring) b, l, b->slen); + + if (t.slen >= ws->minBuffSz) { + internal_bwswriteout (ws, &t); + return 0; + } + + return bassign (ws->buff, &t); +} + +/* int bwsWriteBlk (struct bwriteStream * ws, void * blk, int len) + * + * Send a block of data a bwriteStream. If the stream is at EOF BSTR_ERR is + * returned. + */ +int bwsWriteBlk (struct bwriteStream * ws, void * blk, int len) { +struct tagbstring t; + if (NULL == blk || len < 0) return BSTR_ERR; + blk2tbstr (t, blk, len); + return bwsWriteBstr (ws, &t); +} + +/* int bwsIsEOF (const struct bwriteStream * ws) + * + * Returns 0 if the stream is currently writable, 1 if the core stream has + * responded by not accepting the previous attempted write. + */ +int bwsIsEOF (const struct bwriteStream * ws) { + if (NULL == ws || NULL == ws->buff || 0 > ws->minBuffSz || + NULL == ws->writeFn) return BSTR_ERR; + return ws->isEOF; +} + +/* int bwsBuffLength (struct bwriteStream * ws, int sz) + * + * Set the length of the buffer used by the bwsStream. If sz is zero, the + * length is not set. This function returns with the previous length. + */ +int bwsBuffLength (struct bwriteStream * ws, int sz) { +int oldSz; + if (ws == NULL || sz < 0) return BSTR_ERR; + oldSz = ws->minBuffSz; + if (sz > 0) ws->minBuffSz = sz; + return oldSz; +} + +/* void * bwsClose (struct bwriteStream * s) + * + * Close the bwriteStream, and return the handle to the stream that was + * originally used to open the given stream. Note that even if the stream + * is at EOF it still needs to be closed with a call to bwsClose. + */ +void * bwsClose (struct bwriteStream * ws) { +void * parm; + if (NULL == ws || NULL == ws->buff || 0 >= ws->minBuffSz || + NULL == ws->writeFn) return NULL; + bwsWriteFlush (ws); + parm = ws->parm; + ws->parm = NULL; + ws->minBuffSz = -1; + ws->writeFn = NULL; + bstrFree (ws->buff); + free (ws); + return parm; +} diff --git a/bstrlib/bstraux.h b/bstrlib/bstraux.h new file mode 100644 index 0000000..9f30e3c --- /dev/null +++ b/bstrlib/bstraux.h @@ -0,0 +1,115 @@ +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2015, and is covered by the BSD open source + * license and the GPL. Refer to the accompanying documentation for details + * on usage and license. + */ + +/* + * bstraux.h + * + * This file is not a necessary part of the core bstring library itself, but + * is just an auxilliary module which includes miscellaneous or trivial + * functions. + */ + +#ifndef BSTRAUX_INCLUDE +#define BSTRAUX_INCLUDE + +#include +#include "bstrlib.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Safety mechanisms */ +#define bstrDeclare(b) bstring (b) = NULL; +#define bstrFree(b) {if ((b) != NULL && (b)->slen >= 0 && (b)->mlen >= (b)->slen) { bdestroy (b); (b) = NULL; }} + +/* Backward compatibilty with previous versions of Bstrlib */ +#if !defined(BSTRLIB_REDUCE_NAMESPACE_POLLUTION) +#define bAssign(a,b) ((bassign)((a), (b))) +#define bSubs(b,pos,len,a,c) ((breplace)((b),(pos),(len),(a),(unsigned char)(c))) +#define bStrchr(b,c) ((bstrchr)((b), (c))) +#define bStrchrFast(b,c) ((bstrchr)((b), (c))) +#define bCatCstr(b,s) ((bcatcstr)((b), (s))) +#define bCatBlk(b,s,len) ((bcatblk)((b),(s),(len))) +#define bCatStatic(b,s) bcatStatic(b,s) +#define bTrunc(b,n) ((btrunc)((b), (n))) +#define bReplaceAll(b,find,repl,pos) ((bfindreplace)((b),(find),(repl),(pos))) +#define bUppercase(b) ((btoupper)(b)) +#define bLowercase(b) ((btolower)(b)) +#define bCaselessCmp(a,b) ((bstricmp)((a), (b))) +#define bCaselessNCmp(a,b,n) ((bstrnicmp)((a), (b), (n))) +#define bBase64Decode(b) (bBase64DecodeEx ((b), NULL)) +#define bUuDecode(b) (bUuDecodeEx ((b), NULL)) +#endif + +/* Unusual functions */ +extern struct bStream * bsFromBstr (const_bstring b); +extern bstring bTail (bstring b, int n); +extern bstring bHead (bstring b, int n); +extern int bSetCstrChar (bstring a, int pos, char c); +extern int bSetChar (bstring b, int pos, char c); +extern int bFill (bstring a, char c, int len); +extern int bReplicate (bstring b, int n); +extern int bReverse (bstring b); +extern int bInsertChrs (bstring b, int pos, int len, unsigned char c, unsigned char fill); +extern bstring bStrfTime (const char * fmt, const struct tm * timeptr); +#define bAscTime(t) (bStrfTime ("%c\n", (t))) +#define bCTime(t) ((t) ? bAscTime (localtime (t)) : NULL) + +/* Spacing formatting */ +extern int bJustifyLeft (bstring b, int space); +extern int bJustifyRight (bstring b, int width, int space); +extern int bJustifyMargin (bstring b, int width, int space); +extern int bJustifyCenter (bstring b, int width, int space); + +/* Esoteric standards specific functions */ +extern char * bStr2NetStr (const_bstring b); +extern bstring bNetStr2Bstr (const char * buf); +extern bstring bBase64Encode (const_bstring b); +extern bstring bBase64DecodeEx (const_bstring b, int * boolTruncError); +extern struct bStream * bsUuDecode (struct bStream * sInp, int * badlines); +extern bstring bUuDecodeEx (const_bstring src, int * badlines); +extern bstring bUuEncode (const_bstring src); +extern bstring bYEncode (const_bstring src); +extern bstring bYDecode (const_bstring src); +extern int bSGMLEncode (bstring b); + +/* Writable stream */ +typedef int (* bNwrite) (const void * buf, size_t elsize, size_t nelem, void * parm); + +struct bwriteStream * bwsOpen (bNwrite writeFn, void * parm); +int bwsWriteBstr (struct bwriteStream * stream, const_bstring b); +int bwsWriteBlk (struct bwriteStream * stream, void * blk, int len); +int bwsWriteFlush (struct bwriteStream * stream); +int bwsIsEOF (const struct bwriteStream * stream); +int bwsBuffLength (struct bwriteStream * stream, int sz); +void * bwsClose (struct bwriteStream * stream); + +/* Security functions */ +#define bSecureDestroy(b) { \ +bstring bstr__tmp = (b); \ + if (bstr__tmp && bstr__tmp->mlen > 0 && bstr__tmp->data) { \ + (void) memset (bstr__tmp->data, 0, (size_t) bstr__tmp->mlen); \ + bdestroy (bstr__tmp); \ + } \ +} +#define bSecureWriteProtect(t) { \ + if ((t).mlen >= 0) { \ + if ((t).mlen > (t).slen)) { \ + (void) memset ((t).data + (t).slen, 0, (size_t) (t).mlen - (t).slen); \ + } \ + (t).mlen = -1; \ + } \ +} +extern bstring bSecureInput (int maxlen, int termchar, + bNgetc vgetchar, void * vgcCtx); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/bstrlib/bstrlib.c b/bstrlib/bstrlib.c new file mode 100644 index 0000000..aa2c000 --- /dev/null +++ b/bstrlib/bstrlib.c @@ -0,0 +1,3153 @@ +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2015, and is covered by the BSD open source + * license and the GPL. Refer to the accompanying documentation for details + * on usage and license. + */ + +/* + * bstrlib.c + * + * This file is the core module for implementing the bstring functions. + */ + +#if defined (_MSC_VER) +# define _CRT_SECURE_NO_WARNINGS +#endif + +#include +#include +#include +#include +#include +#include +#include +#include "bstrlib.h" + +/* Optionally include a mechanism for debugging memory */ + +#if defined(MEMORY_DEBUG) || defined(BSTRLIB_MEMORY_DEBUG) +#include "memdbg.h" +#endif + +#ifndef bstr__alloc +#if defined (BSTRLIB_TEST_CANARY) +void* bstr__alloc (size_t sz) { + char* p = (char *) malloc (sz); + memset (p, 'X', sz); + return p; +} +#else +#define bstr__alloc(x) malloc (x) +#endif +#endif + +#ifndef bstr__free +#define bstr__free(p) free (p) +#endif + +#ifndef bstr__realloc +#define bstr__realloc(p,x) realloc ((p), (x)) +#endif + +#ifndef bstr__memcpy +#define bstr__memcpy(d,s,l) memcpy ((d), (s), (l)) +#endif + +#ifndef bstr__memmove +#define bstr__memmove(d,s,l) memmove ((d), (s), (l)) +#endif + +#ifndef bstr__memset +#define bstr__memset(d,c,l) memset ((d), (c), (l)) +#endif + +#ifndef bstr__memcmp +#define bstr__memcmp(d,c,l) memcmp ((d), (c), (l)) +#endif + +#ifndef bstr__memchr +#define bstr__memchr(s,c,l) memchr ((s), (c), (l)) +#endif + +/* Just a length safe wrapper for memmove. */ + +#define bBlockCopy(D,S,L) { if ((L) > 0) bstr__memmove ((D),(S),(L)); } + +/* Compute the snapped size for a given requested size. By snapping to powers + of 2 like this, repeated reallocations are avoided. */ +static int snapUpSize (int i) { + if (i < 8) { + i = 8; + } else { + unsigned int j; + j = (unsigned int) i; + + j |= (j >> 1); + j |= (j >> 2); + j |= (j >> 4); + j |= (j >> 8); /* Ok, since int >= 16 bits */ +#if (UINT_MAX != 0xffff) + j |= (j >> 16); /* For 32 bit int systems */ +#if (UINT_MAX > 0xffffffffUL) + j |= (j >> 32); /* For 64 bit int systems */ +#endif +#endif + /* Least power of two greater than i */ + j++; + if ((int) j >= i) i = (int) j; + } + return i; +} + +/* int balloc (bstring b, int len) + * + * Increase the size of the memory backing the bstring b to at least len. + */ +int balloc (bstring b, int olen) { + int len; + if (b == NULL || b->data == NULL || b->slen < 0 || b->mlen <= 0 || + b->mlen < b->slen || olen <= 0) { + return BSTR_ERR; + } + + if (olen >= b->mlen) { + unsigned char * x; + + if ((len = snapUpSize (olen)) <= b->mlen) return BSTR_OK; + + /* Assume probability of a non-moving realloc is 0.125 */ + if (7 * b->mlen < 8 * b->slen) { + + /* If slen is close to mlen in size then use realloc to reduce + the memory defragmentation */ + + reallocStrategy:; + + x = (unsigned char *) bstr__realloc (b->data, (size_t) len); + if (x == NULL) { + + /* Since we failed, try allocating the tighest possible + allocation */ + + len = olen; + x = (unsigned char *) bstr__realloc (b->data, (size_t) olen); + if (NULL == x) { + return BSTR_ERR; + } + } + } else { + + /* If slen is not close to mlen then avoid the penalty of copying + the extra bytes that are allocated, but not considered part of + the string */ + + if (NULL == (x = (unsigned char *) bstr__alloc ((size_t) len))) { + + /* Perhaps there is no available memory for the two + allocations to be in memory at once */ + + goto reallocStrategy; + + } else { + if (b->slen) bstr__memcpy ((char *) x, (char *) b->data, + (size_t) b->slen); + bstr__free (b->data); + } + } + b->data = x; + b->mlen = len; + b->data[b->slen] = (unsigned char) '\0'; + +#if defined (BSTRLIB_TEST_CANARY) + if (len > b->slen + 1) { + memchr (b->data + b->slen + 1, 'X', len - (b->slen + 1)); + } +#endif + } + + return BSTR_OK; +} + +/* int ballocmin (bstring b, int len) + * + * Set the size of the memory backing the bstring b to len or b->slen+1, + * whichever is larger. Note that repeated use of this function can degrade + * performance. + */ +int ballocmin (bstring b, int len) { + unsigned char * s; + + if (b == NULL || b->data == NULL) return BSTR_ERR; + if (b->slen >= INT_MAX || b->slen < 0) return BSTR_ERR; + if (b->mlen <= 0 || b->mlen < b->slen || len <= 0) { + return BSTR_ERR; + } + + if (len < b->slen + 1) len = b->slen + 1; + + if (len != b->mlen) { + s = (unsigned char *) bstr__realloc (b->data, (size_t) len); + if (NULL == s) return BSTR_ERR; + s[b->slen] = (unsigned char) '\0'; + b->data = s; + b->mlen = len; + } + + return BSTR_OK; +} + +/* bstring bfromcstr (const char * str) + * + * Create a bstring which contains the contents of the '\0' terminated char * + * buffer str. + */ +bstring bfromcstr (const char * str) { +bstring b; +int i; +size_t j; + + if (str == NULL) return NULL; + j = (strlen) (str); + i = snapUpSize ((int) (j + (2 - (j != 0)))); + if (i <= (int) j) return NULL; + + b = (bstring) bstr__alloc (sizeof (struct tagbstring)); + if (NULL == b) return NULL; + b->slen = (int) j; + if (NULL == (b->data = (unsigned char *) bstr__alloc (b->mlen = i))) { + bstr__free (b); + return NULL; + } + + bstr__memcpy (b->data, str, j+1); + return b; +} + +/* bstring bfromcstrrangealloc (int minl, int maxl, const char* str) + * + * Create a bstring which contains the contents of the '\0' terminated + * char* buffer str. The memory buffer backing the string is at least + * minl characters in length, but an attempt is made to allocate up to + * maxl characters. + */ +bstring bfromcstrrangealloc (int minl, int maxl, const char* str) { +bstring b; +int i; +size_t j; + + /* Bad parameters? */ + if (str == NULL) return NULL; + if (maxl < minl || minl < 0) return NULL; + + /* Adjust lengths */ + j = (strlen) (str); + if ((size_t) minl < (j+1)) minl = (int) (j+1); + if (maxl < minl) maxl = minl; + i = maxl; + + b = (bstring) bstr__alloc (sizeof (struct tagbstring)); + if (b == NULL) return NULL; + b->slen = (int) j; + + while (NULL == (b->data = (unsigned char *) bstr__alloc (b->mlen = i))) { + int k = (i >> 1) + (minl >> 1); + if (i == k || i < minl) { + bstr__free (b); + return NULL; + } + i = k; + } + + bstr__memcpy (b->data, str, j+1); + return b; +} + +/* bstring bfromcstralloc (int mlen, const char * str) + * + * Create a bstring which contains the contents of the '\0' terminated + * char* buffer str. The memory buffer backing the string is at least + * mlen characters in length. + */ +bstring bfromcstralloc (int mlen, const char * str) { + return bfromcstrrangealloc (mlen, mlen, str); +} + +/* bstring blk2bstr (const void * blk, int len) + * + * Create a bstring which contains the content of the block blk of length + * len. + */ +bstring blk2bstr (const void * blk, int len) { +bstring b; +int i; + + if (blk == NULL || len < 0) return NULL; + b = (bstring) bstr__alloc (sizeof (struct tagbstring)); + if (b == NULL) return NULL; + b->slen = len; + + i = len + (2 - (len != 0)); + i = snapUpSize (i); + + b->mlen = i; + + b->data = (unsigned char *) bstr__alloc ((size_t) b->mlen); + if (b->data == NULL) { + bstr__free (b); + return NULL; + } + + if (len > 0) bstr__memcpy (b->data, blk, (size_t) len); + b->data[len] = (unsigned char) '\0'; + + return b; +} + +/* char * bstr2cstr (const_bstring s, char z) + * + * Create a '\0' terminated char * buffer which is equal to the contents of + * the bstring s, except that any contained '\0' characters are converted + * to the character in z. This returned value should be freed with a + * bcstrfree () call, by the calling application. + */ +char * bstr2cstr (const_bstring b, char z) { +int i, l; +char * r; + + if (b == NULL || b->slen < 0 || b->data == NULL) return NULL; + l = b->slen; + r = (char *) bstr__alloc ((size_t) (l + 1)); + if (r == NULL) return r; + + for (i=0; i < l; i ++) { + r[i] = (char) ((b->data[i] == '\0') ? z : (char) (b->data[i])); + } + + r[l] = (unsigned char) '\0'; + + return r; +} + +/* int bcstrfree (char * s) + * + * Frees a C-string generated by bstr2cstr (). This is normally unnecessary + * since it just wraps a call to bstr__free (), however, if bstr__alloc () + * and bstr__free () have been redefined as a macros within the bstrlib + * module (via defining them in memdbg.h after defining + * BSTRLIB_MEMORY_DEBUG) with some difference in behaviour from the std + * library functions, then this allows a correct way of freeing the memory + * that allows higher level code to be independent from these macro + * redefinitions. + */ +int bcstrfree (char * s) { + if (s) { + bstr__free (s); + return BSTR_OK; + } + return BSTR_ERR; +} + +/* int bconcat (bstring b0, const_bstring b1) + * + * Concatenate the bstring b1 to the bstring b0. + */ +int bconcat (bstring b0, const_bstring b1) { +int len, d; +bstring aux = (bstring) b1; + + if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL) + return BSTR_ERR; + + d = b0->slen; + len = b1->slen; + if ((d | (b0->mlen - d) | len | (d + len)) < 0) return BSTR_ERR; + + if (b0->mlen <= d + len + 1) { + ptrdiff_t pd = b1->data - b0->data; + if (0 <= pd && pd < b0->mlen) { + if (NULL == (aux = bstrcpy (b1))) return BSTR_ERR; + } + if (balloc (b0, d + len + 1) != BSTR_OK) { + if (aux != b1) bdestroy (aux); + return BSTR_ERR; + } + } + + bBlockCopy (&b0->data[d], &aux->data[0], (size_t) len); + b0->data[d + len] = (unsigned char) '\0'; + b0->slen = d + len; + if (aux != b1) bdestroy (aux); + return BSTR_OK; +} + +/* int bconchar (bstring b, char c) + * + * Concatenate the single character c to the bstring b. + */ +int bconchar (bstring b, char c) { +int d; + + if (b == NULL) return BSTR_ERR; + d = b->slen; + if ((d | (b->mlen - d)) < 0 || balloc (b, d + 2) != BSTR_OK) + return BSTR_ERR; + b->data[d] = (unsigned char) c; + b->data[d + 1] = (unsigned char) '\0'; + b->slen++; + return BSTR_OK; +} + +/* int bcatcstr (bstring b, const char * s) + * + * Concatenate a char * string to a bstring. + */ +int bcatcstr (bstring b, const char * s) { +char * d; +int i, l; + + if (b == NULL || b->data == NULL || b->slen < 0 || b->mlen < b->slen + || b->mlen <= 0 || s == NULL) return BSTR_ERR; + + /* Optimistically concatenate directly */ + l = b->mlen - b->slen; + d = (char *) &b->data[b->slen]; + for (i=0; i < l; i++) { + if ((*d++ = *s++) == '\0') { + b->slen += i; + return BSTR_OK; + } + } + b->slen += i; + + /* Need to explicitely resize and concatenate tail */ + return bcatblk (b, (const void *) s, (int) strlen (s)); +} + +/* int bcatblk (bstring b, const void * s, int len) + * + * Concatenate a fixed length buffer to a bstring. + */ +int bcatblk (bstring b, const void * s, int len) { +int nl; + + if (b == NULL || b->data == NULL || b->slen < 0 || b->mlen < b->slen + || b->mlen <= 0 || s == NULL || len < 0) return BSTR_ERR; + + if (0 > (nl = b->slen + len)) return BSTR_ERR; /* Overflow? */ + if (b->mlen <= nl && 0 > balloc (b, nl + 1)) return BSTR_ERR; + + bBlockCopy (&b->data[b->slen], s, (size_t) len); + b->slen = nl; + b->data[nl] = (unsigned char) '\0'; + return BSTR_OK; +} + +/* bstring bstrcpy (const_bstring b) + * + * Create a copy of the bstring b. + */ +bstring bstrcpy (const_bstring b) { +bstring b0; +int i,j; + + /* Attempted to copy an invalid string? */ + if (b == NULL || b->slen < 0 || b->data == NULL) return NULL; + + b0 = (bstring) bstr__alloc (sizeof (struct tagbstring)); + if (b0 == NULL) { + /* Unable to allocate memory for string header */ + return NULL; + } + + i = b->slen; + j = snapUpSize (i + 1); + + b0->data = (unsigned char *) bstr__alloc (j); + if (b0->data == NULL) { + j = i + 1; + b0->data = (unsigned char *) bstr__alloc (j); + if (b0->data == NULL) { + /* Unable to allocate memory for string data */ + bstr__free (b0); + return NULL; + } + } + + b0->mlen = j; + b0->slen = i; + + if (i) bstr__memcpy ((char *) b0->data, (char *) b->data, i); + b0->data[b0->slen] = (unsigned char) '\0'; + + return b0; +} + +/* int bassign (bstring a, const_bstring b) + * + * Overwrite the string a with the contents of string b. + */ +int bassign (bstring a, const_bstring b) { + if (b == NULL || b->data == NULL || b->slen < 0) + return BSTR_ERR; + if (b->slen != 0) { + if (balloc (a, b->slen) != BSTR_OK) return BSTR_ERR; + bstr__memmove (a->data, b->data, b->slen); + } else { + if (a == NULL || a->data == NULL || a->mlen < a->slen || + a->slen < 0 || a->mlen == 0) + return BSTR_ERR; + } + a->data[b->slen] = (unsigned char) '\0'; + a->slen = b->slen; + return BSTR_OK; +} + +/* int bassignmidstr (bstring a, const_bstring b, int left, int len) + * + * Overwrite the string a with the middle of contents of string b + * starting from position left and running for a length len. left and + * len are clamped to the ends of b as with the function bmidstr. + */ +int bassignmidstr (bstring a, const_bstring b, int left, int len) { + if (b == NULL || b->data == NULL || b->slen < 0) + return BSTR_ERR; + + if (left < 0) { + len += left; + left = 0; + } + + if (len > b->slen - left) len = b->slen - left; + + if (a == NULL || a->data == NULL || a->mlen < a->slen || + a->slen < 0 || a->mlen == 0) + return BSTR_ERR; + + if (len > 0) { + if (balloc (a, len) != BSTR_OK) return BSTR_ERR; + bstr__memmove (a->data, b->data + left, len); + a->slen = len; + } else { + a->slen = 0; + } + a->data[a->slen] = (unsigned char) '\0'; + return BSTR_OK; +} + +/* int bassigncstr (bstring a, const char * str) + * + * Overwrite the string a with the contents of char * string str. Note that + * the bstring a must be a well defined and writable bstring. If an error + * occurs BSTR_ERR is returned however a may be partially overwritten. + */ +int bassigncstr (bstring a, const char * str) { +int i; +size_t len; + if (a == NULL || a->data == NULL || a->mlen < a->slen || + a->slen < 0 || a->mlen == 0 || NULL == str) + return BSTR_ERR; + + for (i=0; i < a->mlen; i++) { + if ('\0' == (a->data[i] = str[i])) { + a->slen = i; + return BSTR_OK; + } + } + + a->slen = i; + len = strlen (str + i); + if (len + 1 > INT_MAX - i || + 0 > balloc (a, (int) (i + len + 1))) return BSTR_ERR; + bBlockCopy (a->data + i, str + i, (size_t) len + 1); + a->slen += (int) len; + return BSTR_OK; +} + +/* int bassignblk (bstring a, const void * s, int len) + * + * Overwrite the string a with the contents of the block (s, len). Note that + * the bstring a must be a well defined and writable bstring. If an error + * occurs BSTR_ERR is returned and a is not overwritten. + */ +int bassignblk (bstring a, const void * s, int len) { + if (a == NULL || a->data == NULL || a->mlen < a->slen || + a->slen < 0 || a->mlen == 0 || NULL == s || len < 0 || len >= INT_MAX) + return BSTR_ERR; + if (len + 1 > a->mlen && 0 > balloc (a, len + 1)) return BSTR_ERR; + bBlockCopy (a->data, s, (size_t) len); + a->data[len] = (unsigned char) '\0'; + a->slen = len; + return BSTR_OK; +} + +/* int btrunc (bstring b, int n) + * + * Truncate the bstring to at most n characters. + */ +int btrunc (bstring b, int n) { + if (n < 0 || b == NULL || b->data == NULL || b->mlen < b->slen || + b->slen < 0 || b->mlen <= 0) return BSTR_ERR; + if (b->slen > n) { + b->slen = n; + b->data[n] = (unsigned char) '\0'; + } + return BSTR_OK; +} + +#define upcase(c) (toupper ((unsigned char) c)) +#define downcase(c) (tolower ((unsigned char) c)) +#define wspace(c) (isspace ((unsigned char) c)) + +/* int btoupper (bstring b) + * + * Convert contents of bstring to upper case. + */ +int btoupper (bstring b) { +int i, len; + if (b == NULL || b->data == NULL || b->mlen < b->slen || + b->slen < 0 || b->mlen <= 0) return BSTR_ERR; + for (i=0, len = b->slen; i < len; i++) { + b->data[i] = (unsigned char) upcase (b->data[i]); + } + return BSTR_OK; +} + +/* int btolower (bstring b) + * + * Convert contents of bstring to lower case. + */ +int btolower (bstring b) { +int i, len; + if (b == NULL || b->data == NULL || b->mlen < b->slen || + b->slen < 0 || b->mlen <= 0) return BSTR_ERR; + for (i=0, len = b->slen; i < len; i++) { + b->data[i] = (unsigned char) downcase (b->data[i]); + } + return BSTR_OK; +} + +/* int bstricmp (const_bstring b0, const_bstring b1) + * + * Compare two strings without differentiating between case. The return + * value is the difference of the values of the characters where the two + * strings first differ after lower case transformation, otherwise 0 is + * returned indicating that the strings are equal. If the lengths are + * different, then a difference from 0 is given, but if the first extra + * character is '\0', then it is taken to be the value UCHAR_MAX+1. + */ +int bstricmp (const_bstring b0, const_bstring b1) { +int i, v, n; + + if (bdata (b0) == NULL || b0->slen < 0 || + bdata (b1) == NULL || b1->slen < 0) return SHRT_MIN; + if ((n = b0->slen) > b1->slen) n = b1->slen; + else if (b0->slen == b1->slen && b0->data == b1->data) return BSTR_OK; + + for (i = 0; i < n; i ++) { + v = (char) downcase (b0->data[i]) + - (char) downcase (b1->data[i]); + if (0 != v) return v; + } + + if (b0->slen > n) { + v = (char) downcase (b0->data[n]); + if (v) return v; + return UCHAR_MAX + 1; + } + if (b1->slen > n) { + v = - (char) downcase (b1->data[n]); + if (v) return v; + return - (int) (UCHAR_MAX + 1); + } + return BSTR_OK; +} + +/* int bstrnicmp (const_bstring b0, const_bstring b1, int n) + * + * Compare two strings without differentiating between case for at most n + * characters. If the position where the two strings first differ is + * before the nth position, the return value is the difference of the values + * of the characters, otherwise 0 is returned. If the lengths are different + * and less than n characters, then a difference from 0 is given, but if the + * first extra character is '\0', then it is taken to be the value + * UCHAR_MAX+1. + */ +int bstrnicmp (const_bstring b0, const_bstring b1, int n) { +int i, v, m; + + if (bdata (b0) == NULL || b0->slen < 0 || + bdata (b1) == NULL || b1->slen < 0 || n < 0) return SHRT_MIN; + m = n; + if (m > b0->slen) m = b0->slen; + if (m > b1->slen) m = b1->slen; + + if (b0->data != b1->data) { + for (i = 0; i < m; i ++) { + v = (char) downcase (b0->data[i]); + v -= (char) downcase (b1->data[i]); + if (v != 0) return b0->data[i] - b1->data[i]; + } + } + + if (n == m || b0->slen == b1->slen) return BSTR_OK; + + if (b0->slen > m) { + v = (char) downcase (b0->data[m]); + if (v) return v; + return UCHAR_MAX + 1; + } + + v = - (char) downcase (b1->data[m]); + if (v) return v; + return - (int) (UCHAR_MAX + 1); +} + +/* int biseqcaselessblk (const_bstring b, const void * blk, int len) + * + * Compare content of b and the array of bytes in blk for length len for + * equality without differentiating between character case. If the content + * differs other than in case, 0 is returned, if, ignoring case, the content + * is the same, 1 is returned, if there is an error, -1 is returned. If the + * length of the strings are different, this function is O(1). '\0' + * characters are not treated in any special way. + */ +int biseqcaselessblk (const_bstring b, const void * blk, int len) { +int i; + + if (bdata (b) == NULL || b->slen < 0 || + blk == NULL || len < 0) return BSTR_ERR; + if (b->slen != len) return 0; + if (len == 0 || b->data == blk) return 1; + for (i=0; i < len; i++) { + if (b->data[i] != ((unsigned char*)blk)[i]) { + unsigned char c = (unsigned char) downcase (b->data[i]); + if (c != (unsigned char) downcase (((unsigned char*)blk)[i])) + return 0; + } + } + return 1; +} + + +/* int biseqcaseless (const_bstring b0, const_bstring b1) + * + * Compare two strings for equality without differentiating between case. + * If the strings differ other than in case, 0 is returned, if the strings + * are the same, 1 is returned, if there is an error, -1 is returned. If + * the length of the strings are different, this function is O(1). '\0' + * termination characters are not treated in any special way. + */ +int biseqcaseless (const_bstring b0, const_bstring b1) { +#if 0 +int i, n; + + if (bdata (b0) == NULL || b0->slen < 0 || + bdata (b1) == NULL || b1->slen < 0) return BSTR_ERR; + if (b0->slen != b1->slen) return BSTR_OK; + if (b0->data == b1->data || b0->slen == 0) return 1; + for (i=0, n=b0->slen; i < n; i++) { + if (b0->data[i] != b1->data[i]) { + unsigned char c = (unsigned char) downcase (b0->data[i]); + if (c != (unsigned char) downcase (b1->data[i])) return 0; + } + } + return 1; +#else + if (NULL == b1) return BSTR_ERR; + return biseqcaselessblk (b0, b1->data, b1->slen); +#endif +} + +/* int bisstemeqcaselessblk (const_bstring b0, const void * blk, int len) + * + * Compare beginning of string b0 with a block of memory of length len + * without differentiating between case for equality. If the beginning of b0 + * differs from the memory block other than in case (or if b0 is too short), + * 0 is returned, if the strings are the same, 1 is returned, if there is an + * error, -1 is returned. '\0' characters are not treated in any special + * way. + */ +int bisstemeqcaselessblk (const_bstring b0, const void * blk, int len) { +int i; + + if (bdata (b0) == NULL || b0->slen < 0 || NULL == blk || len < 0) + return BSTR_ERR; + if (b0->slen < len) return BSTR_OK; + if (b0->data == (const unsigned char *) blk || len == 0) return 1; + + for (i = 0; i < len; i ++) { + if (b0->data[i] != ((const unsigned char *) blk)[i]) { + if (downcase (b0->data[i]) != + downcase (((const unsigned char *) blk)[i])) return 0; + } + } + return 1; +} + +/* + * int bltrimws (bstring b) + * + * Delete whitespace contiguous from the left end of the string. + */ +int bltrimws (bstring b) { +int i, len; + + if (b == NULL || b->data == NULL || b->mlen < b->slen || + b->slen < 0 || b->mlen <= 0) return BSTR_ERR; + + for (len = b->slen, i = 0; i < len; i++) { + if (!wspace (b->data[i])) { + return bdelete (b, 0, i); + } + } + + b->data[0] = (unsigned char) '\0'; + b->slen = 0; + return BSTR_OK; +} + +/* + * int brtrimws (bstring b) + * + * Delete whitespace contiguous from the right end of the string. + */ +int brtrimws (bstring b) { +int i; + + if (b == NULL || b->data == NULL || b->mlen < b->slen || + b->slen < 0 || b->mlen <= 0) return BSTR_ERR; + + for (i = b->slen - 1; i >= 0; i--) { + if (!wspace (b->data[i])) { + if (b->mlen > i) b->data[i+1] = (unsigned char) '\0'; + b->slen = i + 1; + return BSTR_OK; + } + } + + b->data[0] = (unsigned char) '\0'; + b->slen = 0; + return BSTR_OK; +} + +/* + * int btrimws (bstring b) + * + * Delete whitespace contiguous from both ends of the string. + */ +int btrimws (bstring b) { +int i, j; + + if (b == NULL || b->data == NULL || b->mlen < b->slen || + b->slen < 0 || b->mlen <= 0) return BSTR_ERR; + + for (i = b->slen - 1; i >= 0; i--) { + if (!wspace (b->data[i])) { + if (b->mlen > i) b->data[i+1] = (unsigned char) '\0'; + b->slen = i + 1; + for (j = 0; wspace (b->data[j]); j++) {} + return bdelete (b, 0, j); + } + } + + b->data[0] = (unsigned char) '\0'; + b->slen = 0; + return BSTR_OK; +} + +/* int biseqblk (const_bstring b, const void * blk, int len) + * + * Compare the string b with the character block blk of length len. If the + * content differs, 0 is returned, if the content is the same, 1 is returned, + * if there is an error, -1 is returned. If the length of the strings are + * different, this function is O(1). '\0' characters are not treated in any + * special way. + */ +int biseqblk (const_bstring b, const void * blk, int len) { + if (len < 0 || b == NULL || blk == NULL || b->data == NULL || b->slen < 0) + return BSTR_ERR; + if (b->slen != len) return 0; + if (len == 0 || b->data == blk) return 1; + return !bstr__memcmp (b->data, blk, len); +} + +/* int biseq (const_bstring b0, const_bstring b1) + * + * Compare the string b0 and b1. If the strings differ, 0 is returned, if + * the strings are the same, 1 is returned, if there is an error, -1 is + * returned. If the length of the strings are different, this function is + * O(1). '\0' termination characters are not treated in any special way. + */ +int biseq (const_bstring b0, const_bstring b1) { + if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL || + b0->slen < 0 || b1->slen < 0) return BSTR_ERR; + if (b0->slen != b1->slen) return BSTR_OK; + if (b0->data == b1->data || b0->slen == 0) return 1; + return !bstr__memcmp (b0->data, b1->data, b0->slen); +} + +/* int bisstemeqblk (const_bstring b0, const void * blk, int len) + * + * Compare beginning of string b0 with a block of memory of length len for + * equality. If the beginning of b0 differs from the memory block (or if b0 + * is too short), 0 is returned, if the strings are the same, 1 is returned, + * if there is an error, -1 is returned. '\0' characters are not treated in + * any special way. + */ +int bisstemeqblk (const_bstring b0, const void * blk, int len) { +int i; + + if (bdata (b0) == NULL || b0->slen < 0 || NULL == blk || len < 0) + return BSTR_ERR; + if (b0->slen < len) return BSTR_OK; + if (b0->data == (const unsigned char *) blk || len == 0) return 1; + + for (i = 0; i < len; i ++) { + if (b0->data[i] != ((const unsigned char *) blk)[i]) return BSTR_OK; + } + return 1; +} + +/* int biseqcstr (const_bstring b, const char *s) + * + * Compare the bstring b and char * string s. The C string s must be '\0' + * terminated at exactly the length of the bstring b, and the contents + * between the two must be identical with the bstring b with no '\0' + * characters for the two contents to be considered equal. This is + * equivalent to the condition that their current contents will be always be + * equal when comparing them in the same format after converting one or the + * other. If the strings are equal 1 is returned, if they are unequal 0 is + * returned and if there is a detectable error BSTR_ERR is returned. + */ +int biseqcstr (const_bstring b, const char * s) { +int i; + if (b == NULL || s == NULL || b->data == NULL || b->slen < 0) + return BSTR_ERR; + for (i=0; i < b->slen; i++) { + if (s[i] == '\0' || b->data[i] != (unsigned char) s[i]) + return BSTR_OK; + } + return s[i] == '\0'; +} + +/* int biseqcstrcaseless (const_bstring b, const char *s) + * + * Compare the bstring b and char * string s. The C string s must be '\0' + * terminated at exactly the length of the bstring b, and the contents + * between the two must be identical except for case with the bstring b with + * no '\0' characters for the two contents to be considered equal. This is + * equivalent to the condition that their current contents will be always be + * equal ignoring case when comparing them in the same format after + * converting one or the other. If the strings are equal, except for case, + * 1 is returned, if they are unequal regardless of case 0 is returned and + * if there is a detectable error BSTR_ERR is returned. + */ +int biseqcstrcaseless (const_bstring b, const char * s) { +int i; + if (b == NULL || s == NULL || b->data == NULL || b->slen < 0) + return BSTR_ERR; + for (i=0; i < b->slen; i++) { + if (s[i] == '\0' || + (b->data[i] != (unsigned char) s[i] && + downcase (b->data[i]) != (unsigned char) downcase (s[i]))) + return BSTR_OK; + } + return s[i] == '\0'; +} + +/* int bstrcmp (const_bstring b0, const_bstring b1) + * + * Compare the string b0 and b1. If there is an error, SHRT_MIN is returned, + * otherwise a value less than or greater than zero, indicating that the + * string pointed to by b0 is lexicographically less than or greater than + * the string pointed to by b1 is returned. If the the string lengths are + * unequal but the characters up until the length of the shorter are equal + * then a value less than, or greater than zero, indicating that the string + * pointed to by b0 is shorter or longer than the string pointed to by b1 is + * returned. 0 is returned if and only if the two strings are the same. If + * the length of the strings are different, this function is O(n). Like its + * standard C library counter part strcmp, the comparison does not proceed + * past any '\0' termination characters encountered. + */ +int bstrcmp (const_bstring b0, const_bstring b1) { +int i, v, n; + + if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL || + b0->slen < 0 || b1->slen < 0) return SHRT_MIN; + n = b0->slen; if (n > b1->slen) n = b1->slen; + if (b0->slen == b1->slen && (b0->data == b1->data || b0->slen == 0)) + return BSTR_OK; + + for (i = 0; i < n; i ++) { + v = ((char) b0->data[i]) - ((char) b1->data[i]); + if (v != 0) return v; + if (b0->data[i] == (unsigned char) '\0') return BSTR_OK; + } + + if (b0->slen > n) return 1; + if (b1->slen > n) return -1; + return BSTR_OK; +} + +/* int bstrncmp (const_bstring b0, const_bstring b1, int n) + * + * Compare the string b0 and b1 for at most n characters. If there is an + * error, SHRT_MIN is returned, otherwise a value is returned as if b0 and + * b1 were first truncated to at most n characters then bstrcmp was called + * with these new strings are paremeters. If the length of the strings are + * different, this function is O(n). Like its standard C library counter + * part strcmp, the comparison does not proceed past any '\0' termination + * characters encountered. + */ +int bstrncmp (const_bstring b0, const_bstring b1, int n) { +int i, v, m; + + if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL || + b0->slen < 0 || b1->slen < 0) return SHRT_MIN; + m = n; + if (m > b0->slen) m = b0->slen; + if (m > b1->slen) m = b1->slen; + + if (b0->data != b1->data) { + for (i = 0; i < m; i ++) { + v = ((char) b0->data[i]) - ((char) b1->data[i]); + if (v != 0) return v; + if (b0->data[i] == (unsigned char) '\0') return BSTR_OK; + } + } + + if (n == m || b0->slen == b1->slen) return BSTR_OK; + + if (b0->slen > m) return 1; + return -1; +} + +/* bstring bmidstr (const_bstring b, int left, int len) + * + * Create a bstring which is the substring of b starting from position left + * and running for a length len (clamped by the end of the bstring b.) If + * b is detectably invalid, then NULL is returned. The section described + * by (left, len) is clamped to the boundaries of b. + */ +bstring bmidstr (const_bstring b, int left, int len) { + + if (b == NULL || b->slen < 0 || b->data == NULL) return NULL; + + if (left < 0) { + len += left; + left = 0; + } + + if (len > b->slen - left) len = b->slen - left; + + if (len <= 0) return bfromcstr (""); + return blk2bstr (b->data + left, len); +} + +/* int bdelete (bstring b, int pos, int len) + * + * Removes characters from pos to pos+len-1 inclusive and shifts the tail of + * the bstring starting from pos+len to pos. len must be positive for this + * call to have any effect. The section of the string described by (pos, + * len) is clamped to boundaries of the bstring b. + */ +int bdelete (bstring b, int pos, int len) { + /* Clamp to left side of bstring */ + if (pos < 0) { + len += pos; + pos = 0; + } + + if (len < 0 || b == NULL || b->data == NULL || b->slen < 0 || + b->mlen < b->slen || b->mlen <= 0) + return BSTR_ERR; + if (len > 0 && pos < b->slen) { + if (pos + len >= b->slen) { + b->slen = pos; + } else { + bBlockCopy ((char *) (b->data + pos), + (char *) (b->data + pos + len), + b->slen - (pos+len)); + b->slen -= len; + } + b->data[b->slen] = (unsigned char) '\0'; + } + return BSTR_OK; +} + +/* int bdestroy (bstring b) + * + * Free up the bstring. Note that if b is detectably invalid or not writable + * then no action is performed and BSTR_ERR is returned. Like a freed memory + * allocation, dereferences, writes or any other action on b after it has + * been bdestroyed is undefined. + */ +int bdestroy (bstring b) { + if (b == NULL || b->slen < 0 || b->mlen <= 0 || b->mlen < b->slen || + b->data == NULL) + return BSTR_ERR; + + bstr__free (b->data); + + /* In case there is any stale usage, there is one more chance to + notice this error. */ + + b->slen = -1; + b->mlen = -__LINE__; + b->data = NULL; + + bstr__free (b); + return BSTR_OK; +} + +/* int binstr (const_bstring b1, int pos, const_bstring b2) + * + * Search for the bstring b2 in b1 starting from position pos, and searching + * forward. If it is found then return with the first position where it is + * found, otherwise return BSTR_ERR. Note that this is just a brute force + * string searcher that does not attempt clever things like the Boyer-Moore + * search algorithm. Because of this there are many degenerate cases where + * this can take much longer than it needs to. + */ +int binstr (const_bstring b1, int pos, const_bstring b2) { +int j, ii, ll, lf; +unsigned char * d0; +unsigned char c0; +register unsigned char * d1; +register unsigned char c1; +register int i; + + if (b1 == NULL || b1->data == NULL || b1->slen < 0 || + b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR; + if (b1->slen == pos) return (b2->slen == 0)?pos:BSTR_ERR; + if (b1->slen < pos || pos < 0) return BSTR_ERR; + if (b2->slen == 0) return pos; + + /* No space to find such a string? */ + if ((lf = b1->slen - b2->slen + 1) <= pos) return BSTR_ERR; + + /* An obvious alias case */ + if (b1->data == b2->data && pos == 0) return 0; + + i = pos; + + d0 = b2->data; + d1 = b1->data; + ll = b2->slen; + + /* Peel off the b2->slen == 1 case */ + c0 = d0[0]; + if (1 == ll) { + for (;i < lf; i++) if (c0 == d1[i]) return i; + return BSTR_ERR; + } + + c1 = c0; + j = 0; + lf = b1->slen - 1; + + ii = -1; + if (i < lf) do { + /* Unrolled current character test */ + if (c1 != d1[i]) { + if (c1 != d1[1+i]) { + i += 2; + continue; + } + i++; + } + + /* Take note if this is the start of a potential match */ + if (0 == j) ii = i; + + /* Shift the test character down by one */ + j++; + i++; + + /* If this isn't past the last character continue */ + if (j < ll) { + c1 = d0[j]; + continue; + } + + N0:; + + /* If no characters mismatched, then we matched */ + if (i == ii+j) return ii; + + /* Shift back to the beginning */ + i -= j; + j = 0; + c1 = c0; + } while (i < lf); + + /* Deal with last case if unrolling caused a misalignment */ + if (i == lf && ll == j+1 && c1 == d1[i]) goto N0; + + return BSTR_ERR; +} + +/* int binstrr (const_bstring b1, int pos, const_bstring b2) + * + * Search for the bstring b2 in b1 starting from position pos, and searching + * backward. If it is found then return with the first position where it is + * found, otherwise return BSTR_ERR. Note that this is just a brute force + * string searcher that does not attempt clever things like the Boyer-Moore + * search algorithm. Because of this there are many degenerate cases where + * this can take much longer than it needs to. + */ +int binstrr (const_bstring b1, int pos, const_bstring b2) { +int j, i, l; +unsigned char * d0, * d1; + + if (b1 == NULL || b1->data == NULL || b1->slen < 0 || + b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR; + if (b1->slen == pos && b2->slen == 0) return pos; + if (b1->slen < pos || pos < 0) return BSTR_ERR; + if (b2->slen == 0) return pos; + + /* Obvious alias case */ + if (b1->data == b2->data && pos == 0 && b2->slen <= b1->slen) return 0; + + i = pos; + if ((l = b1->slen - b2->slen) < 0) return BSTR_ERR; + + /* If no space to find such a string then snap back */ + if (l + 1 <= i) i = l; + j = 0; + + d0 = b2->data; + d1 = b1->data; + l = b2->slen; + + for (;;) { + if (d0[j] == d1[i + j]) { + j ++; + if (j >= l) return i; + } else { + i --; + if (i < 0) break; + j=0; + } + } + + return BSTR_ERR; +} + +/* int binstrcaseless (const_bstring b1, int pos, const_bstring b2) + * + * Search for the bstring b2 in b1 starting from position pos, and searching + * forward but without regard to case. If it is found then return with the + * first position where it is found, otherwise return BSTR_ERR. Note that + * this is just a brute force string searcher that does not attempt clever + * things like the Boyer-Moore search algorithm. Because of this there are + * many degenerate cases where this can take much longer than it needs to. + */ +int binstrcaseless (const_bstring b1, int pos, const_bstring b2) { +int j, i, l, ll; +unsigned char * d0, * d1; + + if (b1 == NULL || b1->data == NULL || b1->slen < 0 || + b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR; + if (b1->slen == pos) return (b2->slen == 0)?pos:BSTR_ERR; + if (b1->slen < pos || pos < 0) return BSTR_ERR; + if (b2->slen == 0) return pos; + + l = b1->slen - b2->slen + 1; + + /* No space to find such a string? */ + if (l <= pos) return BSTR_ERR; + + /* An obvious alias case */ + if (b1->data == b2->data && pos == 0) return BSTR_OK; + + i = pos; + j = 0; + + d0 = b2->data; + d1 = b1->data; + ll = b2->slen; + + for (;;) { + if (d0[j] == d1[i + j] || downcase (d0[j]) == downcase (d1[i + j])) { + j ++; + if (j >= ll) return i; + } else { + i ++; + if (i >= l) break; + j=0; + } + } + + return BSTR_ERR; +} + +/* int binstrrcaseless (const_bstring b1, int pos, const_bstring b2) + * + * Search for the bstring b2 in b1 starting from position pos, and searching + * backward but without regard to case. If it is found then return with the + * first position where it is found, otherwise return BSTR_ERR. Note that + * this is just a brute force string searcher that does not attempt clever + * things like the Boyer-Moore search algorithm. Because of this there are + * many degenerate cases where this can take much longer than it needs to. + */ +int binstrrcaseless (const_bstring b1, int pos, const_bstring b2) { +int j, i, l; +unsigned char * d0, * d1; + + if (b1 == NULL || b1->data == NULL || b1->slen < 0 || + b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR; + if (b1->slen == pos && b2->slen == 0) return pos; + if (b1->slen < pos || pos < 0) return BSTR_ERR; + if (b2->slen == 0) return pos; + + /* Obvious alias case */ + if (b1->data == b2->data && pos == 0 && b2->slen <= b1->slen) + return BSTR_OK; + + i = pos; + if ((l = b1->slen - b2->slen) < 0) return BSTR_ERR; + + /* If no space to find such a string then snap back */ + if (l + 1 <= i) i = l; + j = 0; + + d0 = b2->data; + d1 = b1->data; + l = b2->slen; + + for (;;) { + if (d0[j] == d1[i + j] || downcase (d0[j]) == downcase (d1[i + j])) { + j ++; + if (j >= l) return i; + } else { + i --; + if (i < 0) break; + j=0; + } + } + + return BSTR_ERR; +} + + +/* int bstrchrp (const_bstring b, int c, int pos) + * + * Search for the character c in b forwards from the position pos + * (inclusive). + */ +int bstrchrp (const_bstring b, int c, int pos) { +unsigned char * p; + + if (b == NULL || b->data == NULL || b->slen <= pos || pos < 0) + return BSTR_ERR; + p = (unsigned char *) bstr__memchr ((b->data + pos), (unsigned char) c, + (b->slen - pos)); + if (p) return (int) (p - b->data); + return BSTR_ERR; +} + +/* int bstrrchrp (const_bstring b, int c, int pos) + * + * Search for the character c in b backwards from the position pos in string + * (inclusive). + */ +int bstrrchrp (const_bstring b, int c, int pos) { +int i; + + if (b == NULL || b->data == NULL || b->slen <= pos || pos < 0) + return BSTR_ERR; + for (i=pos; i >= 0; i--) { + if (b->data[i] == (unsigned char) c) return i; + } + return BSTR_ERR; +} + +#if !defined (BSTRLIB_AGGRESSIVE_MEMORY_FOR_SPEED_TRADEOFF) +#define LONG_LOG_BITS_QTY (3) +#define LONG_BITS_QTY (1 << LONG_LOG_BITS_QTY) +#define LONG_TYPE unsigned char + +#define CFCLEN ((1 << CHAR_BIT) / LONG_BITS_QTY) +struct charField { LONG_TYPE content[CFCLEN]; }; +#define testInCharField(cf,c) ((cf)->content[(c) >> LONG_LOG_BITS_QTY] & \ + (((long)1) << ((c) & (LONG_BITS_QTY-1)))) +#define setInCharField(cf,idx) { \ + unsigned int c = (unsigned int) (idx); \ + (cf)->content[c >> LONG_LOG_BITS_QTY] |= \ + (LONG_TYPE) (1ul << (c & (LONG_BITS_QTY-1))); \ +} + +#else + +#define CFCLEN (1 << CHAR_BIT) +struct charField { unsigned char content[CFCLEN]; }; +#define testInCharField(cf,c) ((cf)->content[(unsigned char) (c)]) +#define setInCharField(cf,idx) (cf)->content[(unsigned int) (idx)] = ~0 + +#endif + +/* Convert a bstring to charField */ +static int buildCharField (struct charField * cf, const_bstring b) { +int i; + if (b == NULL || b->data == NULL || b->slen <= 0) return BSTR_ERR; + memset ((void *) cf->content, 0, sizeof (struct charField)); + for (i=0; i < b->slen; i++) { + setInCharField (cf, b->data[i]); + } + return BSTR_OK; +} + +static void invertCharField (struct charField * cf) { +int i; + for (i=0; i < CFCLEN; i++) cf->content[i] = ~cf->content[i]; +} + +/* Inner engine for binchr */ +static int binchrCF (const unsigned char * data, int len, int pos, + const struct charField * cf) { +int i; + for (i=pos; i < len; i++) { + unsigned char c = (unsigned char) data[i]; + if (testInCharField (cf, c)) return i; + } + return BSTR_ERR; +} + +/* int binchr (const_bstring b0, int pos, const_bstring b1); + * + * Search for the first position in b0 starting from pos or after, in which + * one of the characters in b1 is found and return it. If such a position + * does not exist in b0, then BSTR_ERR is returned. + */ +int binchr (const_bstring b0, int pos, const_bstring b1) { +struct charField chrs; + if (pos < 0 || b0 == NULL || b0->data == NULL || + b0->slen <= pos) return BSTR_ERR; + if (1 == b1->slen) return bstrchrp (b0, b1->data[0], pos); + if (0 > buildCharField (&chrs, b1)) return BSTR_ERR; + return binchrCF (b0->data, b0->slen, pos, &chrs); +} + +/* Inner engine for binchrr */ +static int binchrrCF (const unsigned char * data, int pos, + const struct charField * cf) { +int i; + for (i=pos; i >= 0; i--) { + unsigned int c = (unsigned int) data[i]; + if (testInCharField (cf, c)) return i; + } + return BSTR_ERR; +} + +/* int binchrr (const_bstring b0, int pos, const_bstring b1); + * + * Search for the last position in b0 no greater than pos, in which one of + * the characters in b1 is found and return it. If such a position does not + * exist in b0, then BSTR_ERR is returned. + */ +int binchrr (const_bstring b0, int pos, const_bstring b1) { +struct charField chrs; + if (pos < 0 || b0 == NULL || b0->data == NULL || b1 == NULL || + b0->slen < pos) return BSTR_ERR; + if (pos == b0->slen) pos--; + if (1 == b1->slen) return bstrrchrp (b0, b1->data[0], pos); + if (0 > buildCharField (&chrs, b1)) return BSTR_ERR; + return binchrrCF (b0->data, pos, &chrs); +} + +/* int bninchr (const_bstring b0, int pos, const_bstring b1); + * + * Search for the first position in b0 starting from pos or after, in which + * none of the characters in b1 is found and return it. If such a position + * does not exist in b0, then BSTR_ERR is returned. + */ +int bninchr (const_bstring b0, int pos, const_bstring b1) { +struct charField chrs; + if (pos < 0 || b0 == NULL || b0->data == NULL || + b0->slen <= pos) return BSTR_ERR; + if (buildCharField (&chrs, b1) < 0) return BSTR_ERR; + invertCharField (&chrs); + return binchrCF (b0->data, b0->slen, pos, &chrs); +} + +/* int bninchrr (const_bstring b0, int pos, const_bstring b1); + * + * Search for the last position in b0 no greater than pos, in which none of + * the characters in b1 is found and return it. If such a position does not + * exist in b0, then BSTR_ERR is returned. + */ +int bninchrr (const_bstring b0, int pos, const_bstring b1) { +struct charField chrs; + if (pos < 0 || b0 == NULL || b0->data == NULL || + b0->slen < pos) return BSTR_ERR; + if (pos == b0->slen) pos--; + if (buildCharField (&chrs, b1) < 0) return BSTR_ERR; + invertCharField (&chrs); + return binchrrCF (b0->data, pos, &chrs); +} + +/* int bsetstr (bstring b0, int pos, bstring b1, unsigned char fill) + * + * Overwrite the string b0 starting at position pos with the string b1. If + * the position pos is past the end of b0, then the character "fill" is + * appended as necessary to make up the gap between the end of b0 and pos. + * If b1 is NULL, it behaves as if it were a 0-length string. + */ +int bsetstr (bstring b0, int pos, const_bstring b1, unsigned char fill) { +int d, newlen; +ptrdiff_t pd; +bstring aux = (bstring) b1; + + if (pos < 0 || b0 == NULL || b0->slen < 0 || NULL == b0->data || + b0->mlen < b0->slen || b0->mlen <= 0) return BSTR_ERR; + if (b1 != NULL && (b1->slen < 0 || b1->data == NULL)) return BSTR_ERR; + + d = pos; + + /* Aliasing case */ + if (NULL != aux) { + if ((pd = (ptrdiff_t) (b1->data - b0->data)) >= 0 && + pd < (ptrdiff_t) b0->mlen) { + if (NULL == (aux = bstrcpy (b1))) return BSTR_ERR; + } + d += aux->slen; + } + + /* Increase memory size if necessary */ + if (balloc (b0, d + 1) != BSTR_OK) { + if (aux != b1) bdestroy (aux); + return BSTR_ERR; + } + + newlen = b0->slen; + + /* Fill in "fill" character as necessary */ + if (pos > newlen) { + bstr__memset (b0->data + b0->slen, (int) fill, + (size_t) (pos - b0->slen)); + newlen = pos; + } + + /* Copy b1 to position pos in b0. */ + if (aux != NULL) { + bBlockCopy ((char *) (b0->data + pos), (char *) aux->data, aux->slen); + if (aux != b1) bdestroy (aux); + } + + /* Indicate the potentially increased size of b0 */ + if (d > newlen) newlen = d; + + b0->slen = newlen; + b0->data[newlen] = (unsigned char) '\0'; + + return BSTR_OK; +} + +/* int binsertblk (bstring b, int pos, const void * blk, int len, + * unsigned char fill) + * + * Inserts the block of characters at blk with length len into b at position + * pos. If the position pos is past the end of b, then the character "fill" + * is appended as necessary to make up the gap between the end of b1 and pos. + * Unlike bsetstr, binsert does not allow b2 to be NULL. + */ +int binsertblk (bstring b, int pos, const void * blk, int len, + unsigned char fill) { +int d, l; +unsigned char* aux = (unsigned char*) blk; + + if (b == NULL || blk == NULL || pos < 0 || len < 0 || b->slen < 0 || + b->mlen <= 0 || b->mlen < b->slen) return BSTR_ERR; + + /* Compute the two possible end pointers */ + d = b->slen + len; + l = pos + len; + if ((d|l) < 0) return BSTR_ERR; /* Integer wrap around. */ + + /* Aliasing case */ + if (((size_t) ((unsigned char*) blk + len)) >= ((size_t) b->data) && + ((size_t) blk) < ((size_t) (b->data + b->mlen))) { + if (NULL == (aux = (unsigned char*) bstr__alloc (len))) + return BSTR_ERR; + bstr__memcpy (aux, blk, len); + } + + if (l > d) { + /* Inserting past the end of the string */ + if (balloc (b, l + 1) != BSTR_OK) { + if (aux != (unsigned char*) blk) bstr__free (aux); + return BSTR_ERR; + } + bstr__memset (b->data + b->slen, (int) fill, + (size_t) (pos - b->slen)); + b->slen = l; + } else { + /* Inserting in the middle of the string */ + if (balloc (b, d + 1) != BSTR_OK) { + if (aux != (unsigned char*) blk) bstr__free (aux); + return BSTR_ERR; + } + bBlockCopy (b->data + l, b->data + pos, d - l); + b->slen = d; + } + bBlockCopy (b->data + pos, aux, len); + b->data[b->slen] = (unsigned char) '\0'; + if (aux != (unsigned char*) blk) bstr__free (aux); + return BSTR_OK; +} + +/* int binsert (bstring b1, int pos, const_bstring b2, unsigned char fill) + * + * Inserts the string b2 into b1 at position pos. If the position pos is + * past the end of b1, then the character "fill" is appended as necessary to + * make up the gap between the end of b1 and pos. Unlike bsetstr, binsert + * does not allow b2 to be NULL. + */ +int binsert (bstring b1, int pos, const_bstring b2, unsigned char fill) { + if (NULL == b2 || (b2->mlen > 0 && b2->slen > b2->mlen)) return BSTR_ERR; + return binsertblk (b1, pos, b2->data, b2->slen, fill); +} + +/* int breplace (bstring b1, int pos, int len, bstring b2, + * unsigned char fill) + * + * Replace a section of a string from pos for a length len with the string + * b2. fill is used is pos > b1->slen. + */ +int breplace (bstring b1, int pos, int len, const_bstring b2, + unsigned char fill) { +int pl, ret; +ptrdiff_t pd; +bstring aux = (bstring) b2; + + if (pos < 0 || len < 0) return BSTR_ERR; + if (pos > INT_MAX - len) return BSTR_ERR; /* Overflow */ + pl = pos + len; + if (b1 == NULL || b2 == NULL || b1->data == NULL || b2->data == NULL || + b1->slen < 0 || b2->slen < 0 || b1->mlen < b1->slen || + b1->mlen <= 0) return BSTR_ERR; + + /* Straddles the end? */ + if (pl >= b1->slen) { + if ((ret = bsetstr (b1, pos, b2, fill)) < 0) return ret; + if (pos + b2->slen < b1->slen) { + b1->slen = pos + b2->slen; + b1->data[b1->slen] = (unsigned char) '\0'; + } + return ret; + } + + /* Aliasing case */ + if ((pd = (ptrdiff_t) (b2->data - b1->data)) >= 0 && + pd < (ptrdiff_t) b1->slen) { + if (NULL == (aux = bstrcpy (b2))) return BSTR_ERR; + } + + if (aux->slen > len) { + if (balloc (b1, b1->slen + aux->slen - len) != BSTR_OK) { + if (aux != b2) bdestroy (aux); + return BSTR_ERR; + } + } + + if (aux->slen != len) bstr__memmove (b1->data + pos + aux->slen, + b1->data + pos + len, + b1->slen - (pos + len)); + bstr__memcpy (b1->data + pos, aux->data, aux->slen); + b1->slen += aux->slen - len; + b1->data[b1->slen] = (unsigned char) '\0'; + if (aux != b2) bdestroy (aux); + return BSTR_OK; +} + +/* + * findreplaceengine is used to implement bfindreplace and + * bfindreplacecaseless. It works by breaking the three cases of + * expansion, reduction and replacement, and solving each of these + * in the most efficient way possible. + */ + +typedef int (*instr_fnptr) (const_bstring s1, int pos, const_bstring s2); + +#define INITIAL_STATIC_FIND_INDEX_COUNT 32 + +static int findreplaceengine (bstring b, const_bstring find, + const_bstring repl, int pos, + instr_fnptr instr) { +int i, ret, slen, mlen, delta, acc; +int * d; +int static_d[INITIAL_STATIC_FIND_INDEX_COUNT+1]; /* This +1 is for LINT. */ +ptrdiff_t pd; +bstring auxf = (bstring) find; +bstring auxr = (bstring) repl; + + if (b == NULL || b->data == NULL || find == NULL || + find->data == NULL || repl == NULL || repl->data == NULL || + pos < 0 || find->slen <= 0 || b->mlen <= 0 || b->slen > b->mlen || + b->slen < 0 || repl->slen < 0) return BSTR_ERR; + if (pos > b->slen - find->slen) return BSTR_OK; + + /* Alias with find string */ + pd = (ptrdiff_t) (find->data - b->data); + if ((ptrdiff_t) (pos - find->slen) < pd && pd < (ptrdiff_t) b->slen) { + if (NULL == (auxf = bstrcpy (find))) return BSTR_ERR; + } + + /* Alias with repl string */ + pd = (ptrdiff_t) (repl->data - b->data); + if ((ptrdiff_t) (pos - repl->slen) < pd && pd < (ptrdiff_t) b->slen) { + if (NULL == (auxr = bstrcpy (repl))) { + if (auxf != find) bdestroy (auxf); + return BSTR_ERR; + } + } + + delta = auxf->slen - auxr->slen; + + /* in-place replacement since find and replace strings are of equal + length */ + if (delta == 0) { + while ((pos = instr (b, pos, auxf)) >= 0) { + bstr__memcpy (b->data + pos, auxr->data, auxr->slen); + pos += auxf->slen; + } + if (auxf != find) bdestroy (auxf); + if (auxr != repl) bdestroy (auxr); + return BSTR_OK; + } + + /* shrinking replacement since auxf->slen > auxr->slen */ + if (delta > 0) { + acc = 0; + + while ((i = instr (b, pos, auxf)) >= 0) { + if (acc && i > pos) + bstr__memmove (b->data + pos - acc, b->data + pos, i - pos); + if (auxr->slen) + bstr__memcpy (b->data + i - acc, auxr->data, auxr->slen); + acc += delta; + pos = i + auxf->slen; + } + + if (acc) { + i = b->slen; + if (i > pos) + bstr__memmove (b->data + pos - acc, b->data + pos, i - pos); + b->slen -= acc; + b->data[b->slen] = (unsigned char) '\0'; + } + + if (auxf != find) bdestroy (auxf); + if (auxr != repl) bdestroy (auxr); + return BSTR_OK; + } + + /* expanding replacement since find->slen < repl->slen. Its a lot + more complicated. This works by first finding all the matches and + storing them to a growable array, then doing at most one resize of + the destination bstring and then performing the direct memory transfers + of the string segment pieces to form the final result. The growable + array of matches uses a deferred doubling reallocing strategy. What + this means is that it starts as a reasonably fixed sized auto array in + the hopes that many if not most cases will never need to grow this + array. But it switches as soon as the bounds of the array will be + exceeded. An extra find result is always appended to this array that + corresponds to the end of the destination string, so slen is checked + against mlen - 1 rather than mlen before resizing. + */ + + mlen = INITIAL_STATIC_FIND_INDEX_COUNT; + d = (int *) static_d; /* Avoid malloc for trivial/initial cases */ + acc = slen = 0; + + while ((pos = instr (b, pos, auxf)) >= 0) { + if (slen >= mlen - 1) { + int *t; + int sl; + /* Overflow */ + if (mlen > (INT_MAX / sizeof(int *)) / 2) { + ret = BSTR_ERR; + goto done; + } + mlen += mlen; + sl = sizeof (int *) * mlen; + if (static_d == d) d = NULL; /* static_d cannot be realloced */ + if (NULL == (t = (int *) bstr__realloc (d, sl))) { + ret = BSTR_ERR; + goto done; + } + if (NULL == d) bstr__memcpy (t, static_d, sizeof (static_d)); + d = t; + } + d[slen] = pos; + slen++; + acc -= delta; + pos += auxf->slen; + if (pos < 0 || acc < 0) { + ret = BSTR_ERR; + goto done; + } + } + + /* slen <= INITIAL_STATIC_INDEX_COUNT-1 or mlen-1 here. */ + d[slen] = b->slen; + + if (BSTR_OK == (ret = balloc (b, b->slen + acc + 1))) { + b->slen += acc; + for (i = slen-1; i >= 0; i--) { + int s, l; + s = d[i] + auxf->slen; + l = d[i+1] - s; /* d[slen] may be accessed here. */ + if (l) { + bstr__memmove (b->data + s + acc, b->data + s, l); + } + if (auxr->slen) { + bstr__memmove (b->data + s + acc - auxr->slen, + auxr->data, auxr->slen); + } + acc += delta; + } + b->data[b->slen] = (unsigned char) '\0'; + } + + done:; + if (static_d != d) bstr__free (d); + if (auxf != find) bdestroy (auxf); + if (auxr != repl) bdestroy (auxr); + return ret; +} + +/* int bfindreplace (bstring b, const_bstring find, const_bstring repl, + * int pos) + * + * Replace all occurrences of a find string with a replace string after a + * given point in a bstring. + */ +int bfindreplace (bstring b, const_bstring find, const_bstring repl, + int pos) { + return findreplaceengine (b, find, repl, pos, binstr); +} + +/* int bfindreplacecaseless (bstring b, const_bstring find, + * const_bstring repl, int pos) + * + * Replace all occurrences of a find string, ignoring case, with a replace + * string after a given point in a bstring. + */ +int bfindreplacecaseless (bstring b, const_bstring find, const_bstring repl, + int pos) { + return findreplaceengine (b, find, repl, pos, binstrcaseless); +} + +/* int binsertch (bstring b, int pos, int len, unsigned char fill) + * + * Inserts the character fill repeatedly into b at position pos for a + * length len. If the position pos is past the end of b, then the + * character "fill" is appended as necessary to make up the gap between the + * end of b and the position pos + len. + */ +int binsertch (bstring b, int pos, int len, unsigned char fill) { +int d, l, i; + + if (pos < 0 || b == NULL || b->slen < 0 || b->mlen < b->slen || + b->mlen <= 0 || len < 0) return BSTR_ERR; + + /* Compute the two possible end pointers */ + d = b->slen + len; + l = pos + len; + if ((d|l) < 0) return BSTR_ERR; + + if (l > d) { + /* Inserting past the end of the string */ + if (balloc (b, l + 1) != BSTR_OK) return BSTR_ERR; + pos = b->slen; + b->slen = l; + } else { + /* Inserting in the middle of the string */ + if (balloc (b, d + 1) != BSTR_OK) return BSTR_ERR; + for (i = d - 1; i >= l; i--) { + b->data[i] = b->data[i - len]; + } + b->slen = d; + } + + for (i=pos; i < l; i++) b->data[i] = fill; + b->data[b->slen] = (unsigned char) '\0'; + return BSTR_OK; +} + +/* int bpattern (bstring b, int len) + * + * Replicate the bstring, b in place, end to end repeatedly until it + * surpasses len characters, then chop the result to exactly len characters. + * This function operates in-place. The function will return with BSTR_ERR + * if b is NULL or of length 0, otherwise BSTR_OK is returned. + */ +int bpattern (bstring b, int len) { +int i, d; + + d = blength (b); + if (d <= 0 || len < 0 || balloc (b, len + 1) != BSTR_OK) return BSTR_ERR; + if (len > 0) { + if (d == 1) return bsetstr (b, len, NULL, b->data[0]); + for (i = d; i < len; i++) b->data[i] = b->data[i - d]; + } + b->data[len] = (unsigned char) '\0'; + b->slen = len; + return BSTR_OK; +} + +#define BS_BUFF_SZ (1024) + +/* int breada (bstring b, bNread readPtr, void * parm) + * + * Use a finite buffer fread-like function readPtr to concatenate to the + * bstring b the entire contents of file-like source data in a roughly + * efficient way. + */ +int breada (bstring b, bNread readPtr, void * parm) { +int i, l, n; + + if (b == NULL || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen || + readPtr == NULL) return BSTR_ERR; + + i = b->slen; + for (n=i+16; ; n += ((n < BS_BUFF_SZ) ? n : BS_BUFF_SZ)) { + if (BSTR_OK != balloc (b, n + 1)) return BSTR_ERR; + l = (int) readPtr ((void *) (b->data + i), 1, n - i, parm); + i += l; + b->slen = i; + if (i < n) break; + } + + b->data[i] = (unsigned char) '\0'; + return BSTR_OK; +} + +/* bstring bread (bNread readPtr, void * parm) + * + * Use a finite buffer fread-like function readPtr to create a bstring + * filled with the entire contents of file-like source data in a roughly + * efficient way. + */ +bstring bread (bNread readPtr, void * parm) { +bstring buff; + + if (0 > breada (buff = bfromcstr (""), readPtr, parm)) { + bdestroy (buff); + return NULL; + } + return buff; +} + +/* int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator) + * + * Use an fgetc-like single character stream reading function (getcPtr) to + * obtain a sequence of characters which are concatenated to the end of the + * bstring b. The stream read is terminated by the passed in terminator + * parameter. + * + * If getcPtr returns with a negative number, or the terminator character + * (which is appended) is read, then the stream reading is halted and the + * function returns with a partial result in b. If there is an empty partial + * result, 1 is returned. If no characters are read, or there is some other + * detectable error, BSTR_ERR is returned. + */ +int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator) { +int c, d, e; + + if (b == NULL || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen || + getcPtr == NULL) return BSTR_ERR; + d = 0; + e = b->mlen - 2; + + while ((c = getcPtr (parm)) >= 0) { + if (d > e) { + b->slen = d; + if (balloc (b, d + 2) != BSTR_OK) return BSTR_ERR; + e = b->mlen - 2; + } + b->data[d] = (unsigned char) c; + d++; + if (c == terminator) break; + } + + b->data[d] = (unsigned char) '\0'; + b->slen = d; + + return d == 0 && c < 0; +} + +/* int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator) + * + * Use an fgetc-like single character stream reading function (getcPtr) to + * obtain a sequence of characters which are concatenated to the end of the + * bstring b. The stream read is terminated by the passed in terminator + * parameter. + * + * If getcPtr returns with a negative number, or the terminator character + * (which is appended) is read, then the stream reading is halted and the + * function returns with a partial result concatentated to b. If there is + * an empty partial result, 1 is returned. If no characters are read, or + * there is some other detectable error, BSTR_ERR is returned. + */ +int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator) { +int c, d, e; + + if (b == NULL || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen || + getcPtr == NULL) return BSTR_ERR; + d = b->slen; + e = b->mlen - 2; + + while ((c = getcPtr (parm)) >= 0) { + if (d > e) { + b->slen = d; + if (balloc (b, d + 2) != BSTR_OK) return BSTR_ERR; + e = b->mlen - 2; + } + b->data[d] = (unsigned char) c; + d++; + if (c == terminator) break; + } + + b->data[d] = (unsigned char) '\0'; + b->slen = d; + + return d == 0 && c < 0; +} + +/* bstring bgets (bNgetc getcPtr, void * parm, char terminator) + * + * Use an fgetc-like single character stream reading function (getcPtr) to + * obtain a sequence of characters which are concatenated into a bstring. + * The stream read is terminated by the passed in terminator function. + * + * If getcPtr returns with a negative number, or the terminator character + * (which is appended) is read, then the stream reading is halted and the + * result obtained thus far is returned. If no characters are read, or + * there is some other detectable error, NULL is returned. + */ +bstring bgets (bNgetc getcPtr, void * parm, char terminator) { +bstring buff; + + if (0 > bgetsa (buff = bfromcstr (""), getcPtr, parm, terminator) || + 0 >= buff->slen) { + bdestroy (buff); + buff = NULL; + } + return buff; +} + +struct bStream { + bstring buff; /* Buffer for over-reads */ + void * parm; /* The stream handle for core stream */ + bNread readFnPtr; /* fread compatible fnptr for core stream */ + int isEOF; /* track file's EOF state */ + int maxBuffSz; +}; + +/* struct bStream * bsopen (bNread readPtr, void * parm) + * + * Wrap a given open stream (described by a fread compatible function + * pointer and stream handle) into an open bStream suitable for the bstring + * library streaming functions. + */ +struct bStream * bsopen (bNread readPtr, void * parm) { +struct bStream * s; + + if (readPtr == NULL) return NULL; + s = (struct bStream *) bstr__alloc (sizeof (struct bStream)); + if (s == NULL) return NULL; + s->parm = parm; + s->buff = bfromcstr (""); + s->readFnPtr = readPtr; + s->maxBuffSz = BS_BUFF_SZ; + s->isEOF = 0; + return s; +} + +/* int bsbufflength (struct bStream * s, int sz) + * + * Set the length of the buffer used by the bStream. If sz is zero, the + * length is not set. This function returns with the previous length. + */ +int bsbufflength (struct bStream * s, int sz) { +int oldSz; + if (s == NULL || sz < 0) return BSTR_ERR; + oldSz = s->maxBuffSz; + if (sz > 0) s->maxBuffSz = sz; + return oldSz; +} + +int bseof (const struct bStream * s) { + if (s == NULL || s->readFnPtr == NULL) return BSTR_ERR; + return s->isEOF && (s->buff->slen == 0); +} + +/* void * bsclose (struct bStream * s) + * + * Close the bStream, and return the handle to the stream that was originally + * used to open the given stream. + */ +void * bsclose (struct bStream * s) { +void * parm; + if (s == NULL) return NULL; + s->readFnPtr = NULL; + if (s->buff) bdestroy (s->buff); + s->buff = NULL; + parm = s->parm; + s->parm = NULL; + s->isEOF = 1; + bstr__free (s); + return parm; +} + +/* int bsreadlna (bstring r, struct bStream * s, char terminator) + * + * Read a bstring terminated by the terminator character or the end of the + * stream from the bStream (s) and return it into the parameter r. This + * function may read additional characters from the core stream that are not + * returned, but will be retained for subsequent read operations. + */ +int bsreadlna (bstring r, struct bStream * s, char terminator) { +int i, l, ret, rlo; +char * b; +struct tagbstring x; + + if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0 || + r->slen < 0 || r->mlen < r->slen) return BSTR_ERR; + l = s->buff->slen; + if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR; + b = (char *) s->buff->data; + x.data = (unsigned char *) b; + + /* First check if the current buffer holds the terminator */ + b[l] = terminator; /* Set sentinel */ + for (i=0; b[i] != terminator; i++) ; + if (i < l) { + x.slen = i + 1; + ret = bconcat (r, &x); + s->buff->slen = l; + if (BSTR_OK == ret) bdelete (s->buff, 0, i + 1); + return BSTR_OK; + } + + rlo = r->slen; + + /* If not then just concatenate the entire buffer to the output */ + x.slen = l; + if (BSTR_OK != bconcat (r, &x)) return BSTR_ERR; + + /* Perform direct in-place reads into the destination to allow for + the minimum of data-copies */ + for (;;) { + if (BSTR_OK != balloc (r, r->slen + s->maxBuffSz + 1)) + return BSTR_ERR; + b = (char *) (r->data + r->slen); + l = (int) s->readFnPtr (b, 1, s->maxBuffSz, s->parm); + if (l <= 0) { + r->data[r->slen] = (unsigned char) '\0'; + s->buff->slen = 0; + s->isEOF = 1; + /* If nothing was read return with an error message */ + return BSTR_ERR & -(r->slen == rlo); + } + b[l] = terminator; /* Set sentinel */ + for (i=0; b[i] != terminator; i++) ; + if (i < l) break; + r->slen += l; + } + + /* Terminator found, push over-read back to buffer */ + i++; + r->slen += i; + s->buff->slen = l - i; + bstr__memcpy (s->buff->data, b + i, l - i); + r->data[r->slen] = (unsigned char) '\0'; + return BSTR_OK; +} + +/* int bsreadlnsa (bstring r, struct bStream * s, bstring term) + * + * Read a bstring terminated by any character in the term string or the end + * of the stream from the bStream (s) and return it into the parameter r. + * This function may read additional characters from the core stream that + * are not returned, but will be retained for subsequent read operations. + */ +int bsreadlnsa (bstring r, struct bStream * s, const_bstring term) { +int i, l, ret, rlo; +unsigned char * b; +struct tagbstring x; +struct charField cf; + + if (s == NULL || s->buff == NULL || r == NULL || term == NULL || + term->data == NULL || r->mlen <= 0 || r->slen < 0 || + r->mlen < r->slen) return BSTR_ERR; + if (term->slen == 1) return bsreadlna (r, s, term->data[0]); + if (term->slen < 1 || buildCharField (&cf, term)) return BSTR_ERR; + + l = s->buff->slen; + if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR; + b = (unsigned char *) s->buff->data; + x.data = b; + + /* First check if the current buffer holds the terminator */ + b[l] = term->data[0]; /* Set sentinel */ + for (i=0; !testInCharField (&cf, b[i]); i++) ; + if (i < l) { + x.slen = i + 1; + ret = bconcat (r, &x); + s->buff->slen = l; + if (BSTR_OK == ret) bdelete (s->buff, 0, i + 1); + return BSTR_OK; + } + + rlo = r->slen; + + /* If not then just concatenate the entire buffer to the output */ + x.slen = l; + if (BSTR_OK != bconcat (r, &x)) return BSTR_ERR; + + /* Perform direct in-place reads into the destination to allow for + the minimum of data-copies */ + for (;;) { + if (BSTR_OK != balloc (r, r->slen + s->maxBuffSz + 1)) + return BSTR_ERR; + b = (unsigned char *) (r->data + r->slen); + l = (int) s->readFnPtr (b, 1, s->maxBuffSz, s->parm); + if (l <= 0) { + r->data[r->slen] = (unsigned char) '\0'; + s->buff->slen = 0; + s->isEOF = 1; + /* If nothing was read return with an error message */ + return BSTR_ERR & -(r->slen == rlo); + } + + b[l] = term->data[0]; /* Set sentinel */ + for (i=0; !testInCharField (&cf, b[i]); i++) ; + if (i < l) break; + r->slen += l; + } + + /* Terminator found, push over-read back to buffer */ + i++; + r->slen += i; + s->buff->slen = l - i; + bstr__memcpy (s->buff->data, b + i, l - i); + r->data[r->slen] = (unsigned char) '\0'; + return BSTR_OK; +} + +/* int bsreada (bstring r, struct bStream * s, int n) + * + * Read a bstring of length n (or, if it is fewer, as many bytes as is + * remaining) from the bStream. This function may read additional + * characters from the core stream that are not returned, but will be + * retained for subsequent read operations. This function will not read + * additional characters from the core stream beyond virtual stream pointer. + */ +int bsreada (bstring r, struct bStream * s, int n) { +int l, ret, orslen; +char * b; +struct tagbstring x; + + if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0 + || r->slen < 0 || r->mlen < r->slen || n <= 0) return BSTR_ERR; + + if (n > INT_MAX - r->slen) return BSTR_ERR; + n += r->slen; + + l = s->buff->slen; + + orslen = r->slen; + + if (0 == l) { + if (s->isEOF) return BSTR_ERR; + if (r->mlen > n) { + l = (int) s->readFnPtr (r->data + r->slen, 1, n - r->slen, + s->parm); + if (0 >= l || l > n - r->slen) { + s->isEOF = 1; + return BSTR_ERR; + } + r->slen += l; + r->data[r->slen] = (unsigned char) '\0'; + return 0; + } + } + + if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR; + b = (char *) s->buff->data; + x.data = (unsigned char *) b; + + do { + if (l + r->slen >= n) { + x.slen = n - r->slen; + ret = bconcat (r, &x); + s->buff->slen = l; + if (BSTR_OK == ret) bdelete (s->buff, 0, x.slen); + return BSTR_ERR & -(r->slen == orslen); + } + + x.slen = l; + if (BSTR_OK != bconcat (r, &x)) break; + + l = n - r->slen; + if (l > s->maxBuffSz) l = s->maxBuffSz; + + l = (int) s->readFnPtr (b, 1, l, s->parm); + + } while (l > 0); + if (l < 0) l = 0; + if (l == 0) s->isEOF = 1; + s->buff->slen = l; + return BSTR_ERR & -(r->slen == orslen); +} + +/* int bsreadln (bstring r, struct bStream * s, char terminator) + * + * Read a bstring terminated by the terminator character or the end of the + * stream from the bStream (s) and return it into the parameter r. This + * function may read additional characters from the core stream that are not + * returned, but will be retained for subsequent read operations. + */ +int bsreadln (bstring r, struct bStream * s, char terminator) { + if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0) + return BSTR_ERR; + if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR; + r->slen = 0; + return bsreadlna (r, s, terminator); +} + +/* int bsreadlns (bstring r, struct bStream * s, bstring term) + * + * Read a bstring terminated by any character in the term string or the end + * of the stream from the bStream (s) and return it into the parameter r. + * This function may read additional characters from the core stream that + * are not returned, but will be retained for subsequent read operations. + */ +int bsreadlns (bstring r, struct bStream * s, const_bstring term) { + if (s == NULL || s->buff == NULL || r == NULL || term == NULL + || term->data == NULL || r->mlen <= 0) return BSTR_ERR; + if (term->slen == 1) return bsreadln (r, s, term->data[0]); + if (term->slen < 1) return BSTR_ERR; + if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR; + r->slen = 0; + return bsreadlnsa (r, s, term); +} + +/* int bsread (bstring r, struct bStream * s, int n) + * + * Read a bstring of length n (or, if it is fewer, as many bytes as is + * remaining) from the bStream. This function may read additional + * characters from the core stream that are not returned, but will be + * retained for subsequent read operations. This function will not read + * additional characters from the core stream beyond virtual stream pointer. + */ +int bsread (bstring r, struct bStream * s, int n) { + if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0 + || n <= 0) return BSTR_ERR; + if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR; + r->slen = 0; + return bsreada (r, s, n); +} + +/* int bsunread (struct bStream * s, const_bstring b) + * + * Insert a bstring into the bStream at the current position. These + * characters will be read prior to those that actually come from the core + * stream. + */ +int bsunread (struct bStream * s, const_bstring b) { + if (s == NULL || s->buff == NULL) return BSTR_ERR; + return binsert (s->buff, 0, b, (unsigned char) '?'); +} + +/* int bspeek (bstring r, const struct bStream * s) + * + * Return the currently buffered characters from the bStream that will be + * read prior to reads from the core stream. + */ +int bspeek (bstring r, const struct bStream * s) { + if (s == NULL || s->buff == NULL) return BSTR_ERR; + return bassign (r, s->buff); +} + +/* bstring bjoinblk (const struct bstrList * bl, void * blk, int len); + * + * Join the entries of a bstrList into one bstring by sequentially + * concatenating them with the content from blk for length len in between. + * If there is an error NULL is returned, otherwise a bstring with the + * correct result is returned. + */ +bstring bjoinblk (const struct bstrList * bl, const void * blk, int len) { +bstring b; +unsigned char * p; +int i, c, v; + + if (bl == NULL || bl->qty < 0) return NULL; + if (len < 0) return NULL; + if (len > 0 && blk == NULL) return NULL; + if (bl->qty < 1) return bfromStatic (""); + + for (i = 0, c = 1; i < bl->qty; i++) { + v = bl->entry[i]->slen; + if (v < 0) return NULL; /* Invalid input */ + if (v > INT_MAX - c) return NULL; /* Overflow */ + c += v; + } + + b = (bstring) bstr__alloc (sizeof (struct tagbstring)); + if (len == 0) { + p = b->data = (unsigned char *) bstr__alloc (c); + if (p == NULL) { + bstr__free (b); + return NULL; + } + for (i = 0; i < bl->qty; i++) { + v = bl->entry[i]->slen; + bstr__memcpy (p, bl->entry[i]->data, v); + p += v; + } + } else { + v = (bl->qty - 1) * len; + if ((bl->qty > 512 || len > 127) && + v / len != bl->qty - 1) return NULL; /* Overflow */ + if (v > INT_MAX - c) return NULL; /* Overflow */ + c += v; + p = b->data = (unsigned char *) bstr__alloc (c); + if (p == NULL) { + bstr__free (b); + return NULL; + } + v = bl->entry[0]->slen; + bstr__memcpy (p, bl->entry[0]->data, v); + p += v; + for (i = 1; i < bl->qty; i++) { + bstr__memcpy (p, blk, len); + p += len; + v = bl->entry[i]->slen; + if (v) { + bstr__memcpy (p, bl->entry[i]->data, v); + p += v; + } + } + } + b->mlen = c; + b->slen = c-1; + b->data[c-1] = (unsigned char) '\0'; + return b; +} + +/* bstring bjoin (const struct bstrList * bl, const_bstring sep); + * + * Join the entries of a bstrList into one bstring by sequentially + * concatenating them with the sep string in between. If there is an error + * NULL is returned, otherwise a bstring with the correct result is returned. + */ +bstring bjoin (const struct bstrList * bl, const_bstring sep) { + if (sep != NULL && (sep->slen < 0 || sep->data == NULL)) return NULL; + return bjoinblk (bl, sep->data, sep->slen); +} + +#define BSSSC_BUFF_LEN (256) + +/* int bssplitscb (struct bStream * s, const_bstring splitStr, + * int (* cb) (void * parm, int ofs, const_bstring entry), + * void * parm) + * + * Iterate the set of disjoint sequential substrings read from a stream + * divided by any of the characters in splitStr. An empty splitStr causes + * the whole stream to be iterated once. + * + * Note: At the point of calling the cb function, the bStream pointer is + * pointed exactly at the position right after having read the split + * character. The cb function can act on the stream by causing the bStream + * pointer to move, and bssplitscb will continue by starting the next split + * at the position of the pointer after the return from cb. + * + * However, if the cb causes the bStream s to be destroyed then the cb must + * return with a negative value, otherwise bssplitscb will continue in an + * undefined manner. + */ +int bssplitscb (struct bStream * s, const_bstring splitStr, + int (* cb) (void * parm, int ofs, const_bstring entry), void * parm) { +struct charField chrs; +bstring buff; +int i, p, ret; + + if (cb == NULL || s == NULL || s->readFnPtr == NULL || + splitStr == NULL || splitStr->slen < 0) return BSTR_ERR; + + if (NULL == (buff = bfromcstr (""))) return BSTR_ERR; + + if (splitStr->slen == 0) { + while (bsreada (buff, s, BSSSC_BUFF_LEN) >= 0) ; + if ((ret = cb (parm, 0, buff)) > 0) + ret = 0; + } else { + buildCharField (&chrs, splitStr); + ret = p = i = 0; + for (;;) { + if (i >= buff->slen) { + bsreada (buff, s, BSSSC_BUFF_LEN); + if (i >= buff->slen) { + if (0 < (ret = cb (parm, p, buff))) ret = 0; + break; + } + } + if (testInCharField (&chrs, buff->data[i])) { + struct tagbstring t; + unsigned char c; + + blk2tbstr (t, buff->data + i + 1, buff->slen - (i + 1)); + if ((ret = bsunread (s, &t)) < 0) break; + buff->slen = i; + c = buff->data[i]; + buff->data[i] = (unsigned char) '\0'; + if ((ret = cb (parm, p, buff)) < 0) break; + buff->data[i] = c; + buff->slen = 0; + p += i + 1; + i = -1; + } + i++; + } + } + + bdestroy (buff); + return ret; +} + +/* int bssplitstrcb (struct bStream * s, const_bstring splitStr, + * int (* cb) (void * parm, int ofs, const_bstring entry), + * void * parm) + * + * Iterate the set of disjoint sequential substrings read from a stream + * divided by the entire substring splitStr. An empty splitStr causes + * each character of the stream to be iterated. + * + * Note: At the point of calling the cb function, the bStream pointer is + * pointed exactly at the position right after having read the split + * character. The cb function can act on the stream by causing the bStream + * pointer to move, and bssplitscb will continue by starting the next split + * at the position of the pointer after the return from cb. + * + * However, if the cb causes the bStream s to be destroyed then the cb must + * return with a negative value, otherwise bssplitscb will continue in an + * undefined manner. + */ +int bssplitstrcb (struct bStream * s, const_bstring splitStr, + int (* cb) (void * parm, int ofs, const_bstring entry), void * parm) { +bstring buff; +int i, p, ret; + + if (cb == NULL || s == NULL || s->readFnPtr == NULL + || splitStr == NULL || splitStr->slen < 0) return BSTR_ERR; + + if (splitStr->slen == 1) return bssplitscb (s, splitStr, cb, parm); + + if (NULL == (buff = bfromcstr (""))) return BSTR_ERR; + + if (splitStr->slen == 0) { + for (i=0; bsreada (buff, s, BSSSC_BUFF_LEN) >= 0; i++) { + if ((ret = cb (parm, 0, buff)) < 0) { + bdestroy (buff); + return ret; + } + buff->slen = 0; + } + return BSTR_OK; + } else { + ret = p = i = 0; + for (i=p=0;;) { + if ((ret = binstr (buff, 0, splitStr)) >= 0) { + struct tagbstring t; + blk2tbstr (t, buff->data, ret); + i = ret + splitStr->slen; + if ((ret = cb (parm, p, &t)) < 0) break; + p += i; + bdelete (buff, 0, i); + } else { + bsreada (buff, s, BSSSC_BUFF_LEN); + if (bseof (s)) { + if ((ret = cb (parm, p, buff)) > 0) ret = 0; + break; + } + } + } + } + + bdestroy (buff); + return ret; +} + +/* int bstrListCreate (void) + * + * Create a bstrList. + */ +struct bstrList * bstrListCreate (void) { +struct bstrList * sl = + (struct bstrList *) bstr__alloc (sizeof (struct bstrList)); + if (sl) { + sl->entry = (bstring *) bstr__alloc (1*sizeof (bstring)); + if (!sl->entry) { + bstr__free (sl); + sl = NULL; + } else { + sl->qty = 0; + sl->mlen = 1; + } + } + return sl; +} + +/* int bstrListDestroy (struct bstrList * sl) + * + * Destroy a bstrList that has been created by bsplit, bsplits or + * bstrListCreate. + */ +int bstrListDestroy (struct bstrList * sl) { +int i; + if (sl == NULL || sl->qty < 0) return BSTR_ERR; + for (i=0; i < sl->qty; i++) { + if (sl->entry[i]) { + bdestroy (sl->entry[i]); + sl->entry[i] = NULL; + } + } + sl->qty = -1; + sl->mlen = -1; + bstr__free (sl->entry); + sl->entry = NULL; + bstr__free (sl); + return BSTR_OK; +} + +/* int bstrListAlloc (struct bstrList * sl, int msz) + * + * Ensure that there is memory for at least msz number of entries for the + * list. + */ +int bstrListAlloc (struct bstrList * sl, int msz) { +bstring * l; +int smsz; +size_t nsz; + if (!sl || msz <= 0 || !sl->entry || sl->qty < 0 || sl->mlen <= 0 || + sl->qty > sl->mlen) return BSTR_ERR; + if (sl->mlen >= msz) return BSTR_OK; + smsz = snapUpSize (msz); + nsz = ((size_t) smsz) * sizeof (bstring); + if (nsz < (size_t) smsz) return BSTR_ERR; + l = (bstring *) bstr__realloc (sl->entry, nsz); + if (!l) { + smsz = msz; + nsz = ((size_t) smsz) * sizeof (bstring); + l = (bstring *) bstr__realloc (sl->entry, nsz); + if (!l) return BSTR_ERR; + } + sl->mlen = smsz; + sl->entry = l; + return BSTR_OK; +} + +/* int bstrListAllocMin (struct bstrList * sl, int msz) + * + * Try to allocate the minimum amount of memory for the list to include at + * least msz entries or sl->qty whichever is greater. + */ +int bstrListAllocMin (struct bstrList * sl, int msz) { +bstring * l; +size_t nsz; + if (!sl || msz <= 0 || !sl->entry || sl->qty < 0 || sl->mlen <= 0 || + sl->qty > sl->mlen) return BSTR_ERR; + if (msz < sl->qty) msz = sl->qty; + if (sl->mlen == msz) return BSTR_OK; + nsz = ((size_t) msz) * sizeof (bstring); + if (nsz < (size_t) msz) return BSTR_ERR; + l = (bstring *) bstr__realloc (sl->entry, nsz); + if (!l) return BSTR_ERR; + sl->mlen = msz; + sl->entry = l; + return BSTR_OK; +} + +/* int bsplitcb (const_bstring str, unsigned char splitChar, int pos, + * int (* cb) (void * parm, int ofs, int len), void * parm) + * + * Iterate the set of disjoint sequential substrings over str divided by the + * character in splitChar. + * + * Note: Non-destructive modification of str from within the cb function + * while performing this split is not undefined. bsplitcb behaves in + * sequential lock step with calls to cb. I.e., after returning from a cb + * that return a non-negative integer, bsplitcb continues from the position + * 1 character after the last detected split character and it will halt + * immediately if the length of str falls below this point. However, if the + * cb function destroys str, then it *must* return with a negative value, + * otherwise bsplitcb will continue in an undefined manner. + */ +int bsplitcb (const_bstring str, unsigned char splitChar, int pos, + int (* cb) (void * parm, int ofs, int len), void * parm) { +int i, p, ret; + + if (cb == NULL || str == NULL || pos < 0 || pos > str->slen) + return BSTR_ERR; + + p = pos; + do { + for (i=p; i < str->slen; i++) { + if (str->data[i] == splitChar) break; + } + if ((ret = cb (parm, p, i - p)) < 0) return ret; + p = i + 1; + } while (p <= str->slen); + return BSTR_OK; +} + +/* int bsplitscb (const_bstring str, const_bstring splitStr, int pos, + * int (* cb) (void * parm, int ofs, int len), void * parm) + * + * Iterate the set of disjoint sequential substrings over str divided by any + * of the characters in splitStr. An empty splitStr causes the whole str to + * be iterated once. + * + * Note: Non-destructive modification of str from within the cb function + * while performing this split is not undefined. bsplitscb behaves in + * sequential lock step with calls to cb. I.e., after returning from a cb + * that return a non-negative integer, bsplitscb continues from the position + * 1 character after the last detected split character and it will halt + * immediately if the length of str falls below this point. However, if the + * cb function destroys str, then it *must* return with a negative value, + * otherwise bsplitscb will continue in an undefined manner. + */ +int bsplitscb (const_bstring str, const_bstring splitStr, int pos, + int (* cb) (void * parm, int ofs, int len), void * parm) { +struct charField chrs; +int i, p, ret; + + if (cb == NULL || str == NULL || pos < 0 || pos > str->slen + || splitStr == NULL || splitStr->slen < 0) return BSTR_ERR; + if (splitStr->slen == 0) { + if ((ret = cb (parm, 0, str->slen)) > 0) ret = 0; + return ret; + } + + if (splitStr->slen == 1) + return bsplitcb (str, splitStr->data[0], pos, cb, parm); + + buildCharField (&chrs, splitStr); + + p = pos; + do { + for (i=p; i < str->slen; i++) { + if (testInCharField (&chrs, str->data[i])) break; + } + if ((ret = cb (parm, p, i - p)) < 0) return ret; + p = i + 1; + } while (p <= str->slen); + return BSTR_OK; +} + +/* int bsplitstrcb (const_bstring str, const_bstring splitStr, int pos, + * int (* cb) (void * parm, int ofs, int len), void * parm) + * + * Iterate the set of disjoint sequential substrings over str divided by the + * substring splitStr. An empty splitStr causes the whole str to be + * iterated once. + * + * Note: Non-destructive modification of str from within the cb function + * while performing this split is not undefined. bsplitstrcb behaves in + * sequential lock step with calls to cb. I.e., after returning from a cb + * that return a non-negative integer, bsplitscb continues from the position + * 1 character after the last detected split character and it will halt + * immediately if the length of str falls below this point. However, if the + * cb function destroys str, then it *must* return with a negative value, + * otherwise bsplitscb will continue in an undefined manner. + */ +int bsplitstrcb (const_bstring str, const_bstring splitStr, int pos, + int (* cb) (void * parm, int ofs, int len), void * parm) { +int i, p, ret; + + if (cb == NULL || str == NULL || pos < 0 || pos > str->slen + || splitStr == NULL || splitStr->slen < 0) return BSTR_ERR; + + if (0 == splitStr->slen) { + for (i=pos; i < str->slen; i++) { + if ((ret = cb (parm, i, 1)) < 0) return ret; + } + return BSTR_OK; + } + + if (splitStr->slen == 1) + return bsplitcb (str, splitStr->data[0], pos, cb, parm); + + for (i=p=pos; i <= str->slen - splitStr->slen; i++) { + if (0 == bstr__memcmp (splitStr->data, str->data + i, + splitStr->slen)) { + if ((ret = cb (parm, p, i - p)) < 0) return ret; + i += splitStr->slen; + p = i; + } + } + if ((ret = cb (parm, p, str->slen - p)) < 0) return ret; + return BSTR_OK; +} + +struct genBstrList { + bstring b; + struct bstrList * bl; +}; + +static int bscb (void * parm, int ofs, int len) { +struct genBstrList * g = (struct genBstrList *) parm; + if (g->bl->qty >= g->bl->mlen) { + int mlen = g->bl->mlen * 2; + bstring * tbl; + + while (g->bl->qty >= mlen) { + if (mlen < g->bl->mlen) return BSTR_ERR; + mlen += mlen; + } + + tbl = (bstring *) bstr__realloc (g->bl->entry, + sizeof (bstring) * mlen); + if (tbl == NULL) return BSTR_ERR; + + g->bl->entry = tbl; + g->bl->mlen = mlen; + } + + g->bl->entry[g->bl->qty] = bmidstr (g->b, ofs, len); + g->bl->qty++; + return BSTR_OK; +} + +/* struct bstrList * bsplit (const_bstring str, unsigned char splitChar) + * + * Create an array of sequential substrings from str divided by the character + * splitChar. + */ +struct bstrList * bsplit (const_bstring str, unsigned char splitChar) { +struct genBstrList g; + + if (str == NULL || str->data == NULL || str->slen < 0) return NULL; + + g.bl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList)); + if (g.bl == NULL) return NULL; + g.bl->mlen = 4; + g.bl->entry = (bstring *) bstr__alloc (g.bl->mlen * sizeof (bstring)); + if (NULL == g.bl->entry) { + bstr__free (g.bl); + return NULL; + } + + g.b = (bstring) str; + g.bl->qty = 0; + if (bsplitcb (str, splitChar, 0, bscb, &g) < 0) { + bstrListDestroy (g.bl); + return NULL; + } + return g.bl; +} + +/* struct bstrList * bsplitstr (const_bstring str, const_bstring splitStr) + * + * Create an array of sequential substrings from str divided by the entire + * substring splitStr. + */ +struct bstrList * bsplitstr (const_bstring str, const_bstring splitStr) { +struct genBstrList g; + + if (str == NULL || str->data == NULL || str->slen < 0) return NULL; + + g.bl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList)); + if (g.bl == NULL) return NULL; + g.bl->mlen = 4; + g.bl->entry = (bstring *) bstr__alloc (g.bl->mlen * sizeof (bstring)); + if (NULL == g.bl->entry) { + bstr__free (g.bl); + return NULL; + } + + g.b = (bstring) str; + g.bl->qty = 0; + if (bsplitstrcb (str, splitStr, 0, bscb, &g) < 0) { + bstrListDestroy (g.bl); + return NULL; + } + return g.bl; +} + +/* struct bstrList * bsplits (const_bstring str, bstring splitStr) + * + * Create an array of sequential substrings from str divided by any of the + * characters in splitStr. An empty splitStr causes a single entry bstrList + * containing a copy of str to be returned. + */ +struct bstrList * bsplits (const_bstring str, const_bstring splitStr) { +struct genBstrList g; + + if ( str == NULL || str->slen < 0 || str->data == NULL || + splitStr == NULL || splitStr->slen < 0 || splitStr->data == NULL) + return NULL; + + g.bl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList)); + if (g.bl == NULL) return NULL; + g.bl->mlen = 4; + g.bl->entry = (bstring *) bstr__alloc (g.bl->mlen * sizeof (bstring)); + if (NULL == g.bl->entry) { + bstr__free (g.bl); + return NULL; + } + g.b = (bstring) str; + g.bl->qty = 0; + + if (bsplitscb (str, splitStr, 0, bscb, &g) < 0) { + bstrListDestroy (g.bl); + return NULL; + } + return g.bl; +} + +#if defined (__TURBOC__) && !defined (__BORLANDC__) +# ifndef BSTRLIB_NOVSNP +# define BSTRLIB_NOVSNP +# endif +#endif + +/* Give WATCOM C/C++, MSVC some latitude for their non-support of vsnprintf */ +#if defined(__WATCOMC__) || defined(_MSC_VER) +#define exvsnprintf(r,b,n,f,a) {r = _vsnprintf (b,n,f,a);} +#else +#ifdef BSTRLIB_NOVSNP +/* This is just a hack. If you are using a system without a vsnprintf, it is + not recommended that bformat be used at all. */ +#define exvsnprintf(r,b,n,f,a) {vsprintf (b,f,a); r = -1;} +#define START_VSNBUFF (256) +#else + +#if defined(__GNUC__) && !defined(__APPLE__) +/* Something is making gcc complain about this prototype not being here, so + I've just gone ahead and put it in. */ +extern int vsnprintf (char *buf, size_t count, const char *format, va_list arg); +#endif + +#define exvsnprintf(r,b,n,f,a) {r = vsnprintf (b,n,f,a);} +#endif +#endif + +#if !defined (BSTRLIB_NOVSNP) + +#ifndef START_VSNBUFF +#define START_VSNBUFF (16) +#endif + +/* On IRIX vsnprintf returns n-1 when the operation would overflow the target + buffer, WATCOM and MSVC both return -1, while C99 requires that the + returned value be exactly what the length would be if the buffer would be + large enough. This leads to the idea that if the return value is larger + than n, then changing n to the return value will reduce the number of + iterations required. */ + +/* int bformata (bstring b, const char * fmt, ...) + * + * After the first parameter, it takes the same parameters as printf (), but + * rather than outputting results to stdio, it appends the results to + * a bstring which contains what would have been output. Note that if there + * is an early generation of a '\0' character, the bstring will be truncated + * to this end point. + */ +int bformata (bstring b, const char * fmt, ...) { +va_list arglist; +bstring buff; +int n, r; + + if (b == NULL || fmt == NULL || b->data == NULL || b->mlen <= 0 + || b->slen < 0 || b->slen > b->mlen) return BSTR_ERR; + + /* Since the length is not determinable beforehand, a search is + performed using the truncating "vsnprintf" call (to avoid buffer + overflows) on increasing potential sizes for the output result. */ + + if ((n = (int) (2*strlen (fmt))) < START_VSNBUFF) n = START_VSNBUFF; + if (NULL == (buff = bfromcstralloc (n + 2, ""))) { + n = 1; + if (NULL == (buff = bfromcstralloc (n + 2, ""))) return BSTR_ERR; + } + + for (;;) { + va_start (arglist, fmt); + exvsnprintf (r, (char *) buff->data, n + 1, fmt, arglist); + va_end (arglist); + + buff->data[n] = (unsigned char) '\0'; + buff->slen = (int) (strlen) ((char *) buff->data); + + if (buff->slen < n) break; + + if (r > n) n = r; else n += n; + + if (BSTR_OK != balloc (buff, n + 2)) { + bdestroy (buff); + return BSTR_ERR; + } + } + + r = bconcat (b, buff); + bdestroy (buff); + return r; +} + +/* int bassignformat (bstring b, const char * fmt, ...) + * + * After the first parameter, it takes the same parameters as printf (), but + * rather than outputting results to stdio, it outputs the results to + * the bstring parameter b. Note that if there is an early generation of a + * '\0' character, the bstring will be truncated to this end point. + */ +int bassignformat (bstring b, const char * fmt, ...) { +va_list arglist; +bstring buff; +int n, r; + + if (b == NULL || fmt == NULL || b->data == NULL || b->mlen <= 0 + || b->slen < 0 || b->slen > b->mlen) return BSTR_ERR; + + /* Since the length is not determinable beforehand, a search is + performed using the truncating "vsnprintf" call (to avoid buffer + overflows) on increasing potential sizes for the output result. */ + + if ((n = (int) (2*strlen (fmt))) < START_VSNBUFF) n = START_VSNBUFF; + if (NULL == (buff = bfromcstralloc (n + 2, ""))) { + n = 1; + if (NULL == (buff = bfromcstralloc (n + 2, ""))) return BSTR_ERR; + } + + for (;;) { + va_start (arglist, fmt); + exvsnprintf (r, (char *) buff->data, n + 1, fmt, arglist); + va_end (arglist); + + buff->data[n] = (unsigned char) '\0'; + buff->slen = (int) (strlen) ((char *) buff->data); + + if (buff->slen < n) break; + + if (r > n) n = r; else n += n; + + if (BSTR_OK != balloc (buff, n + 2)) { + bdestroy (buff); + return BSTR_ERR; + } + } + + r = bassign (b, buff); + bdestroy (buff); + return r; +} + +/* bstring bformat (const char * fmt, ...) + * + * Takes the same parameters as printf (), but rather than outputting results + * to stdio, it forms a bstring which contains what would have been output. + * Note that if there is an early generation of a '\0' character, the + * bstring will be truncated to this end point. + */ +bstring bformat (const char * fmt, ...) { +va_list arglist; +bstring buff; +int n, r; + + if (fmt == NULL) return NULL; + + /* Since the length is not determinable beforehand, a search is + performed using the truncating "vsnprintf" call (to avoid buffer + overflows) on increasing potential sizes for the output result. */ + + if ((n = (int) (2*strlen (fmt))) < START_VSNBUFF) n = START_VSNBUFF; + if (NULL == (buff = bfromcstralloc (n + 2, ""))) { + n = 1; + if (NULL == (buff = bfromcstralloc (n + 2, ""))) return NULL; + } + + for (;;) { + va_start (arglist, fmt); + exvsnprintf (r, (char *) buff->data, n + 1, fmt, arglist); + va_end (arglist); + + buff->data[n] = (unsigned char) '\0'; + buff->slen = (int) (strlen) ((char *) buff->data); + + if (buff->slen < n) break; + + if (r > n) n = r; else n += n; + + if (BSTR_OK != balloc (buff, n + 2)) { + bdestroy (buff); + return NULL; + } + } + + return buff; +} + +/* int bvcformata (bstring b, int count, const char * fmt, va_list arglist) + * + * The bvcformata function formats data under control of the format control + * string fmt and attempts to append the result to b. The fmt parameter is + * the same as that of the printf function. The variable argument list is + * replaced with arglist, which has been initialized by the va_start macro. + * The size of the output is upper bounded by count. If the required output + * exceeds count, the string b is not augmented with any contents and a value + * below BSTR_ERR is returned. If a value below -count is returned then it + * is recommended that the negative of this value be used as an update to the + * count in a subsequent pass. On other errors, such as running out of + * memory, parameter errors or numeric wrap around BSTR_ERR is returned. + * BSTR_OK is returned when the output is successfully generated and + * appended to b. + * + * Note: There is no sanity checking of arglist, and this function is + * destructive of the contents of b from the b->slen point onward. If there + * is an early generation of a '\0' character, the bstring will be truncated + * to this end point. + */ +int bvcformata (bstring b, int count, const char * fmt, va_list arg) { +int n, r, l; + + if (b == NULL || fmt == NULL || count <= 0 || b->data == NULL + || b->mlen <= 0 || b->slen < 0 || b->slen > b->mlen) return BSTR_ERR; + + if (count > (n = b->slen + count) + 2) return BSTR_ERR; + if (BSTR_OK != balloc (b, n + 2)) return BSTR_ERR; + + exvsnprintf (r, (char *) b->data + b->slen, count + 2, fmt, arg); + b->data[b->slen + count + 2] = '\0'; + + /* Did the operation complete successfully within bounds? */ + + if (n >= (l = b->slen + (int) (strlen) ((char *) b->data + b->slen))) { + b->slen = l; + return BSTR_OK; + } + + /* Abort, since the buffer was not large enough. The return value + tries to help set what the retry length should be. */ + + b->data[b->slen] = '\0'; + if (r > count+1) { + l = r; + } else { + if (count > INT_MAX / 2) + l = INT_MAX; + else + l = count + count; + } + n = -l; + if (n > BSTR_ERR-1) n = BSTR_ERR-1; + return n; +} + +#endif diff --git a/bstrlib/bstrlib.h b/bstrlib/bstrlib.h new file mode 100644 index 0000000..fd0874c --- /dev/null +++ b/bstrlib/bstrlib.h @@ -0,0 +1,316 @@ +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2015, and is covered by the BSD open source + * license and the GPL. Refer to the accompanying documentation for details + * on usage and license. + */ + +/* + * bstrlib.h + * + * This file is the interface for the core bstring functions. + */ + +#ifndef BSTRLIB_INCLUDE +#define BSTRLIB_INCLUDE + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include + +#if !defined (BSTRLIB_VSNP_OK) && !defined (BSTRLIB_NOVSNP) +# if defined (__TURBOC__) && !defined (__BORLANDC__) +# define BSTRLIB_NOVSNP +# endif +#endif + +#define BSTR_ERR (-1) +#define BSTR_OK (0) +#define BSTR_BS_BUFF_LENGTH_GET (0) + +typedef struct tagbstring * bstring; +typedef const struct tagbstring * const_bstring; + +/* Copy functions */ +#define cstr2bstr bfromcstr +extern bstring bfromcstr (const char * str); +extern bstring bfromcstralloc (int mlen, const char * str); +extern bstring bfromcstrrangealloc (int minl, int maxl, const char* str); +extern bstring blk2bstr (const void * blk, int len); +extern char * bstr2cstr (const_bstring s, char z); +extern int bcstrfree (char * s); +extern bstring bstrcpy (const_bstring b1); +extern int bassign (bstring a, const_bstring b); +extern int bassignmidstr (bstring a, const_bstring b, int left, int len); +extern int bassigncstr (bstring a, const char * str); +extern int bassignblk (bstring a, const void * s, int len); + +/* Destroy function */ +extern int bdestroy (bstring b); + +/* Space allocation hinting functions */ +extern int balloc (bstring s, int len); +extern int ballocmin (bstring b, int len); + +/* Substring extraction */ +extern bstring bmidstr (const_bstring b, int left, int len); + +/* Various standard manipulations */ +extern int bconcat (bstring b0, const_bstring b1); +extern int bconchar (bstring b0, char c); +extern int bcatcstr (bstring b, const char * s); +extern int bcatblk (bstring b, const void * s, int len); +extern int binsert (bstring s1, int pos, const_bstring s2, unsigned char fill); +extern int binsertblk (bstring s1, int pos, const void * s2, int len, unsigned char fill); +extern int binsertch (bstring s1, int pos, int len, unsigned char fill); +extern int breplace (bstring b1, int pos, int len, const_bstring b2, unsigned char fill); +extern int bdelete (bstring s1, int pos, int len); +extern int bsetstr (bstring b0, int pos, const_bstring b1, unsigned char fill); +extern int btrunc (bstring b, int n); + +/* Scan/search functions */ +extern int bstricmp (const_bstring b0, const_bstring b1); +extern int bstrnicmp (const_bstring b0, const_bstring b1, int n); +extern int biseqcaseless (const_bstring b0, const_bstring b1); +extern int biseqcaselessblk (const_bstring b, const void * blk, int len); +extern int bisstemeqcaselessblk (const_bstring b0, const void * blk, int len); +extern int biseq (const_bstring b0, const_bstring b1); +extern int biseqblk (const_bstring b, const void * blk, int len); +extern int bisstemeqblk (const_bstring b0, const void * blk, int len); +extern int biseqcstr (const_bstring b, const char * s); +extern int biseqcstrcaseless (const_bstring b, const char * s); +extern int bstrcmp (const_bstring b0, const_bstring b1); +extern int bstrncmp (const_bstring b0, const_bstring b1, int n); +extern int binstr (const_bstring s1, int pos, const_bstring s2); +extern int binstrr (const_bstring s1, int pos, const_bstring s2); +extern int binstrcaseless (const_bstring s1, int pos, const_bstring s2); +extern int binstrrcaseless (const_bstring s1, int pos, const_bstring s2); +extern int bstrchrp (const_bstring b, int c, int pos); +extern int bstrrchrp (const_bstring b, int c, int pos); +#define bstrchr(b,c) bstrchrp ((b), (c), 0) +#define bstrrchr(b,c) bstrrchrp ((b), (c), blength(b)-1) +extern int binchr (const_bstring b0, int pos, const_bstring b1); +extern int binchrr (const_bstring b0, int pos, const_bstring b1); +extern int bninchr (const_bstring b0, int pos, const_bstring b1); +extern int bninchrr (const_bstring b0, int pos, const_bstring b1); +extern int bfindreplace (bstring b, const_bstring find, const_bstring repl, int pos); +extern int bfindreplacecaseless (bstring b, const_bstring find, const_bstring repl, int pos); + +/* List of string container functions */ +struct bstrList { + int qty, mlen; + bstring * entry; +}; +extern struct bstrList * bstrListCreate (void); +extern int bstrListDestroy (struct bstrList * sl); +extern int bstrListAlloc (struct bstrList * sl, int msz); +extern int bstrListAllocMin (struct bstrList * sl, int msz); + +/* String split and join functions */ +extern struct bstrList * bsplit (const_bstring str, unsigned char splitChar); +extern struct bstrList * bsplits (const_bstring str, const_bstring splitStr); +extern struct bstrList * bsplitstr (const_bstring str, const_bstring splitStr); +extern bstring bjoin (const struct bstrList * bl, const_bstring sep); +extern bstring bjoinblk (const struct bstrList * bl, const void * s, int len); +extern int bsplitcb (const_bstring str, unsigned char splitChar, int pos, + int (* cb) (void * parm, int ofs, int len), void * parm); +extern int bsplitscb (const_bstring str, const_bstring splitStr, int pos, + int (* cb) (void * parm, int ofs, int len), void * parm); +extern int bsplitstrcb (const_bstring str, const_bstring splitStr, int pos, + int (* cb) (void * parm, int ofs, int len), void * parm); + +/* Miscellaneous functions */ +extern int bpattern (bstring b, int len); +extern int btoupper (bstring b); +extern int btolower (bstring b); +extern int bltrimws (bstring b); +extern int brtrimws (bstring b); +extern int btrimws (bstring b); + +#if !defined (BSTRLIB_NOVSNP) +extern bstring bformat (const char * fmt, ...); +extern int bformata (bstring b, const char * fmt, ...); +extern int bassignformat (bstring b, const char * fmt, ...); +extern int bvcformata (bstring b, int count, const char * fmt, va_list arglist); + +#define bvformata(ret, b, fmt, lastarg) { \ +bstring bstrtmp_b = (b); \ +const char * bstrtmp_fmt = (fmt); \ +int bstrtmp_r = BSTR_ERR, bstrtmp_sz = 16; \ + for (;;) { \ + va_list bstrtmp_arglist; \ + va_start (bstrtmp_arglist, lastarg); \ + bstrtmp_r = bvcformata (bstrtmp_b, bstrtmp_sz, bstrtmp_fmt, bstrtmp_arglist); \ + va_end (bstrtmp_arglist); \ + if (bstrtmp_r >= 0) { /* Everything went ok */ \ + bstrtmp_r = BSTR_OK; \ + break; \ + } else if (-bstrtmp_r <= bstrtmp_sz) { /* A real error? */ \ + bstrtmp_r = BSTR_ERR; \ + break; \ + } \ + bstrtmp_sz = -bstrtmp_r; /* Doubled or target size */ \ + } \ + ret = bstrtmp_r; \ +} + +#endif + +typedef int (*bNgetc) (void *parm); +typedef size_t (* bNread) (void *buff, size_t elsize, size_t nelem, void *parm); + +/* Input functions */ +extern bstring bgets (bNgetc getcPtr, void * parm, char terminator); +extern bstring bread (bNread readPtr, void * parm); +extern int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator); +extern int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator); +extern int breada (bstring b, bNread readPtr, void * parm); + +/* Stream functions */ +extern struct bStream * bsopen (bNread readPtr, void * parm); +extern void * bsclose (struct bStream * s); +extern int bsbufflength (struct bStream * s, int sz); +extern int bsreadln (bstring b, struct bStream * s, char terminator); +extern int bsreadlns (bstring r, struct bStream * s, const_bstring term); +extern int bsread (bstring b, struct bStream * s, int n); +extern int bsreadlna (bstring b, struct bStream * s, char terminator); +extern int bsreadlnsa (bstring r, struct bStream * s, const_bstring term); +extern int bsreada (bstring b, struct bStream * s, int n); +extern int bsunread (struct bStream * s, const_bstring b); +extern int bspeek (bstring r, const struct bStream * s); +extern int bssplitscb (struct bStream * s, const_bstring splitStr, + int (* cb) (void * parm, int ofs, const_bstring entry), void * parm); +extern int bssplitstrcb (struct bStream * s, const_bstring splitStr, + int (* cb) (void * parm, int ofs, const_bstring entry), void * parm); +extern int bseof (const struct bStream * s); + +struct tagbstring { + int mlen; + int slen; + unsigned char * data; +}; + +/* Accessor macros */ +#define blengthe(b, e) (((b) == (void *)0 || (b)->slen < 0) ? (int)(e) : ((b)->slen)) +#define blength(b) (blengthe ((b), 0)) +#define bdataofse(b, o, e) (((b) == (void *)0 || (b)->data == (void*)0) ? (char *)(e) : ((char *)(b)->data) + (o)) +#define bdataofs(b, o) (bdataofse ((b), (o), (void *)0)) +#define bdatae(b, e) (bdataofse (b, 0, e)) +#define bdata(b) (bdataofs (b, 0)) +#define bchare(b, p, e) ((((unsigned)(p)) < (unsigned)blength(b)) ? ((b)->data[(p)]) : (e)) +#define bchar(b, p) bchare ((b), (p), '\0') + +/* Static constant string initialization macro */ +#define bsStaticMlen(q,m) {(m), (int) sizeof(q)-1, (unsigned char *) ("" q "")} +#if defined(_MSC_VER) +# define bsStatic(q) bsStaticMlen(q,-32) +#endif +#ifndef bsStatic +# define bsStatic(q) bsStaticMlen(q,-__LINE__) +#endif + +/* Static constant block parameter pair */ +#define bsStaticBlkParms(q) ((void *)("" q "")), ((int) sizeof(q)-1) + +#define bcatStatic(b,s) ((bcatblk)((b), bsStaticBlkParms(s))) +#define bfromStatic(s) ((blk2bstr)(bsStaticBlkParms(s))) +#define bassignStatic(b,s) ((bassignblk)((b), bsStaticBlkParms(s))) +#define binsertStatic(b,p,s,f) ((binsertblk)((b), (p), bsStaticBlkParms(s), (f))) +#define bjoinStatic(b,s) ((bjoinblk)((b), bsStaticBlkParms(s))) +#define biseqStatic(b,s) ((biseqblk)((b), bsStaticBlkParms(s))) +#define bisstemeqStatic(b,s) ((bisstemeqblk)((b), bsStaticBlkParms(s))) +#define biseqcaselessStatic(b,s) ((biseqcaselessblk)((b), bsStaticBlkParms(s))) +#define bisstemeqcaselessStatic(b,s) ((bisstemeqcaselessblk)((b), bsStaticBlkParms(s))) + +/* Reference building macros */ +#define cstr2tbstr btfromcstr +#define btfromcstr(t,s) { \ + (t).data = (unsigned char *) (s); \ + (t).slen = ((t).data) ? ((int) (strlen) ((char *)(t).data)) : 0; \ + (t).mlen = -1; \ +} +#define blk2tbstr(t,s,l) { \ + (t).data = (unsigned char *) (s); \ + (t).slen = l; \ + (t).mlen = -1; \ +} +#define btfromblk(t,s,l) blk2tbstr(t,s,l) +#define bmid2tbstr(t,b,p,l) { \ + const_bstring bstrtmp_s = (b); \ + if (bstrtmp_s && bstrtmp_s->data && bstrtmp_s->slen >= 0) { \ + int bstrtmp_left = (p); \ + int bstrtmp_len = (l); \ + if (bstrtmp_left < 0) { \ + bstrtmp_len += bstrtmp_left; \ + bstrtmp_left = 0; \ + } \ + if (bstrtmp_len > bstrtmp_s->slen - bstrtmp_left) \ + bstrtmp_len = bstrtmp_s->slen - bstrtmp_left; \ + if (bstrtmp_len <= 0) { \ + (t).data = (unsigned char *)""; \ + (t).slen = 0; \ + } else { \ + (t).data = bstrtmp_s->data + bstrtmp_left; \ + (t).slen = bstrtmp_len; \ + } \ + } else { \ + (t).data = (unsigned char *)""; \ + (t).slen = 0; \ + } \ + (t).mlen = -__LINE__; \ +} +#define btfromblkltrimws(t,s,l) { \ + int bstrtmp_idx = 0, bstrtmp_len = (l); \ + unsigned char * bstrtmp_s = (s); \ + if (bstrtmp_s && bstrtmp_len >= 0) { \ + for (; bstrtmp_idx < bstrtmp_len; bstrtmp_idx++) { \ + if (!isspace (bstrtmp_s[bstrtmp_idx])) break; \ + } \ + } \ + (t).data = bstrtmp_s + bstrtmp_idx; \ + (t).slen = bstrtmp_len - bstrtmp_idx; \ + (t).mlen = -__LINE__; \ +} +#define btfromblkrtrimws(t,s,l) { \ + int bstrtmp_len = (l) - 1; \ + unsigned char * bstrtmp_s = (s); \ + if (bstrtmp_s && bstrtmp_len >= 0) { \ + for (; bstrtmp_len >= 0; bstrtmp_len--) { \ + if (!isspace (bstrtmp_s[bstrtmp_len])) break; \ + } \ + } \ + (t).data = bstrtmp_s; \ + (t).slen = bstrtmp_len + 1; \ + (t).mlen = -__LINE__; \ +} +#define btfromblktrimws(t,s,l) { \ + int bstrtmp_idx = 0, bstrtmp_len = (l) - 1; \ + unsigned char * bstrtmp_s = (s); \ + if (bstrtmp_s && bstrtmp_len >= 0) { \ + for (; bstrtmp_idx <= bstrtmp_len; bstrtmp_idx++) { \ + if (!isspace (bstrtmp_s[bstrtmp_idx])) break; \ + } \ + for (; bstrtmp_len >= bstrtmp_idx; bstrtmp_len--) { \ + if (!isspace (bstrtmp_s[bstrtmp_len])) break; \ + } \ + } \ + (t).data = bstrtmp_s + bstrtmp_idx; \ + (t).slen = bstrtmp_len + 1 - bstrtmp_idx; \ + (t).mlen = -__LINE__; \ +} + +/* Write protection macros */ +#define bwriteprotect(t) { if ((t).mlen >= 0) (t).mlen = -1; } +#define bwriteallow(t) { if ((t).mlen == -1) (t).mlen = (t).slen + ((t).slen == 0); } +#define biswriteprotected(t) ((t).mlen <= 0) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/bstrlib/bstrlib.txt b/bstrlib/bstrlib.txt new file mode 100644 index 0000000..9d2231b --- /dev/null +++ b/bstrlib/bstrlib.txt @@ -0,0 +1,3512 @@ +Better String library +--------------------- + +by Paul Hsieh + +The bstring library is an attempt to provide improved string processing +functionality to the C and C++ language. At the heart of the bstring library +(Bstrlib for short) is the management of "bstring"s which are a significant +improvement over '\0' terminated char buffers. + +=============================================================================== + +Motivation +---------- + +The standard C string library has serious problems: + + 1) Its use of '\0' to denote the end of the string means knowing a + string's length is O(n) when it could be O(1). + 2) It imposes an interpretation for the character value '\0'. + 3) gets() always exposes the application to a buffer overflow. + 4) strtok() modifies the string its parsing and thus may not be usable in + programs which are re-entrant or multithreaded. + 5) fgets has the unusual semantic of ignoring '\0's that occur before + '\n's are consumed. + 6) There is no memory management, and actions performed such as strcpy, + strcat and sprintf are common places for buffer overflows. + 7) strncpy() doesn't '\0' terminate the destination in some cases. + 8) Passing NULL to C library string functions causes an undefined NULL + pointer access. + 9) Parameter aliasing (overlapping, or self-referencing parameters) + within most C library functions has undefined behavior. + 10) Many C library string function calls take integer parameters with + restricted legal ranges. Parameters passed outside these ranges are + not typically detected and cause undefined behavior. + +So the desire is to create an alternative string library that does not suffer +from the above problems and adds in the following functionality: + + 1) Incorporate string functionality seen from other languages. + a) MID$() - from BASIC + b) split()/join() - from Python + c) string/char x n - from Perl + 2) Implement analogs to functions that combine stream IO and char buffers + without creating a dependency on stream IO functionality. + 3) Implement the basic text editor-style functions insert, delete, find, + and replace. + 4) Implement reference based sub-string access (as a generalization of + pointer arithmetic.) + 5) Implement runtime write protection for strings. + +There is also a desire to avoid "API-bloat". So functionality that can be +implemented trivially in other functionality is omitted. So there is no +left$() or right$() or reverse() or anything like that as part of the core +functionality. + +Explaining Bstrings +------------------- + +A bstring is basically a header which wraps a pointer to a char buffer. Lets +start with the declaration of a struct tagbstring: + + struct tagbstring { + int mlen; + int slen; + unsigned char * data; + }; + +This definition is considered exposed, not opaque (though it is neither +necessary nor recommended that low level maintenance of bstrings be performed +whenever the abstract interfaces are sufficient). The mlen field (usually) +describes a lower bound for the memory allocated for the data field. The +slen field describes the exact length for the bstring. The data field is a +single contiguous buffer of unsigned chars. Note that the existence of a '\0' +character in the unsigned char buffer pointed to by the data field does not +necessarily denote the end of the bstring. + +To be a well formed modifiable bstring the mlen field must be at least the +length of the slen field, and slen must be non-negative. Furthermore, the +data field must point to a valid buffer in which access to the first mlen +characters has been acquired. So the minimal check for correctness is: + + (slen >= 0 && mlen >= slen && data != NULL) + +bstrings returned by bstring functions can be assumed to be either NULL or +satisfy the above property. (When bstrings are only readable, the mlen >= +slen restriction is not required; this is discussed later in this section.) +A bstring itself is just a pointer to a struct tagbstring: + + typedef struct tagbstring * bstring; + +Note that use of the prefix "tag" in struct tagbstring is required to work +around the inconsistency between C and C++'s struct namespace usage. This +definition is also considered exposed. + +Bstrlib basically manages bstrings allocated as a header and an associated +data-buffer. Since the implementation is exposed, they can also be +constructed manually. Functions which mutate bstrings assume that the header +and data buffer have been malloced; the bstring library may perform free() or +realloc() on both the header and data buffer of any bstring parameter. +Functions which return bstring's create new bstrings. The string memory is +freed by a bdestroy() call (or using the bstrFree macro). + +The following related typedef is also provided: + + typedef const struct tagbstring * const_bstring; + +which is also considered exposed. These are directly bstring compatible (no +casting required) but are just used for parameters which are meant to be +non-mutable. So in general, bstring parameters which are read as input but +not meant to be modified will be declared as const_bstring, and bstring +parameters which may be modified will be declared as bstring. This convention +is recommended for user written functions as well. + +Since bstrings maintain interoperability with C library char-buffer style +strings, all functions which modify, update or create bstrings also append a +'\0' character into the position slen + 1. This trailing '\0' character is +not required for bstrings input to the bstring functions; this is provided +solely as a convenience for interoperability with standard C char-buffer +functionality. + +Analogs for the ANSI C string library functions have been created when they +are necessary, but have also been left out when they are not. In particular +there are no functions analogous to fwrite, or puts just for the purposes of +bstring. The ->data member of any string is exposed, and therefore can be +used just as easily as char buffers for C functions which read strings. + +For those that wish to hand construct bstrings, the following should be kept +in mind: + + 1) While bstrlib can accept constructed bstrings without terminating + '\0' characters, the rest of the C language string library will not + function properly on such non-terminated strings. This is obvious + but must be kept in mind. + 2) If it is intended that a constructed bstring be written to by the + bstring library functions then the data portion should be allocated + by the malloc function and the slen and mlen fields should be entered + properly. The struct tagbstring header is not reallocated, and only + freed by bdestroy. + 3) Writing arbitrary '\0' characters at various places in the string + will not modify its length as perceived by the bstring library + functions. In fact, '\0' is a legitimate non-terminating character + for a bstring to contain. + 4) For read only parameters, bstring functions do not check the mlen. + I.e., the minimal correctness requirements are reduced to: + + (slen >= 0 && data != NULL) + +Better pointer arithmetic +------------------------- + +One built-in feature of '\0' terminated char * strings, is that its very easy +and fast to obtain a reference to the tail of any string using pointer +arithmetic. Bstrlib does one better by providing a way to get a reference to +any substring of a bstring (or any other length delimited block of memory.) +So rather than just having pointer arithmetic, with bstrlib one essentially +has segment arithmetic. This is achieved using the macro blk2tbstr() which +builds a reference to a block of memory and the macro bmid2tbstr() which +builds a reference to a segment of a bstring. Bstrlib also includes +functions for direct consumption of memory blocks into bstrings, namely +bcatblk () and blk2bstr (). + +One scenario where this can be extremely useful is when string contains many +substrings which one would like to pass as read-only reference parameters to +some string consuming function without the need to allocate entire new +containers for the string data. More concretely, imagine parsing a command +line string whose parameters are space delimited. This can only be done for +tails of the string with '\0' terminated char * strings. + +Improved NULL semantics and error handling +------------------------------------------ + +Unless otherwise noted, if a NULL pointer is passed as a bstring or any other +detectably illegal parameter, the called function will return with an error +indicator (either NULL or BSTR_ERR) rather than simply performing a NULL +pointer access, or having undefined behavior. + +To illustrate the value of this, consider the following example: + + strcpy (p = malloc (13 * sizeof (char)), "Hello,"); + strcat (p, " World"); + +This is not correct because malloc may return NULL (due to an out of memory +condition), and the behaviour of strcpy is undefined if either of its +parameters are NULL. However: + + bstrcat (p = bfromcstr ("Hello,"), q = bfromcstr (" World")); + bdestroy (q); + +is well defined, because if either p or q are assigned NULL (indicating a +failure to allocate memory) both bstrcat and bdestroy will recognize it and +perform no detrimental action. + +Note that it is not necessary to check any of the members of a returned +bstring for internal correctness (in particular the data member does not need +to be checked against NULL when the header is non-NULL), since this is +assured by the bstring library itself. + +bStreams +-------- + +In addition to the bgets and bread functions, bstrlib can abstract streams +with a high performance read only stream called a bStream. In general, the +idea is to open a core stream (with something like fopen) then pass its +handle as well as a bNread function pointer (like fread) to the bsopen +function which will return a handle to an open bStream. Then the functions +bsread, bsreadln or bsreadlns can be called to read portions of the stream. +Finally, the bsclose function is called to close the bStream -- it will +return a handle to the original (core) stream. So bStreams, essentially, +wrap other streams. + +The bStreams have two main advantages over the bgets and bread (as well as +fgets/ungetc) paradigms: + +1) Improved functionality via the bunread function which allows a stream to + unread characters, giving the bStream stack-like functionality if so + desired. +2) A very high performance bsreadln function. The C library function fgets() + (and the bgets function) can typically be written as a loop on top of + fgetc(), thus paying all of the overhead costs of calling fgetc on a per + character basis. bsreadln will read blocks at a time, thus amortizing the + overhead of fread calls over many characters at once. + +However, clearly bStreams are suboptimal or unusable for certain kinds of +streams (stdin) or certain usage patterns (a few spotty, or non-sequential +reads from a slow stream.) For those situations, using bgets will be more +appropriate. + +The semantics of bStreams allows practical construction of layerable data +streams. What this means is that by writing a bNread compatible function on +top of a bStream, one can construct a new bStream on top of it. This can be +useful for writing multi-pass parsers that don't actually read the entire +input more than once and don't require the use of intermediate storage. + +Aliasing +-------- + +Aliasing occurs when a function is given two parameters which point to data +structures which overlap in the memory they occupy. While this does not +disturb read only functions, for many libraries this can make functions that +write to these memory locations malfunction. This is a common problem of the +C standard library and especially the string functions in the C standard +library. + +The C standard string library is entirely char by char oriented (as is +bstring) which makes conforming implementations alias safe for some +scenarios. However no actual detection of aliasing is typically performed, +so it is easy to find cases where the aliasing will cause anomolous or +undesirable behaviour (consider: strcat (p, p).) The C99 standard includes +the "restrict" pointer modifier which allows the compiler to document and +assume a no-alias condition on usage. However, only the most trivial cases +can be caught (if at all) by the compiler at compile time, and thus there is +no actual enforcement of non-aliasing. + +Bstrlib, by contrast, permits aliasing and is completely aliasing safe, in +the C99 sense of aliasing. That is to say, under the assumption that +pointers of incompatible types from distinct objects can never alias, bstrlib +is completely aliasing safe. (In practice this means that the data buffer +portion of any bstring and header of any bstring are assumed to never alias.) +With the exception of the reference building macros, the library behaves as +if all read-only parameters are first copied and replaced by temporary +non-aliased parameters before any writing to any output bstring is performed +(though actual copying is extremely rarely ever done.) + +Besides being a useful safety feature, bstring searching/comparison +functions can improve to O(1) execution when aliasing is detected. + +Note that aliasing detection and handling code in Bstrlib is generally +extremely cheap. There is almost never any appreciable performance penalty +for using aliased parameters. + +Reenterancy +----------- + +Nearly every function in Bstrlib is a leaf function, and is completely +reenterable with the exception of writing to common bstrings. The split +functions which use a callback mechanism requires only that the source string +not be destroyed by the callback function unless the callback function returns +with an error status (note that Bstrlib functions which return an error do +not modify the string in any way.) The string can in fact be modified by the +callback and the behaviour is deterministic. See the documentation of the +various split functions for more details. + +Undefined scenarios +------------------- + +One of the basic important premises for Bstrlib is to not to increase the +propogation of undefined situations from parameters that are otherwise legal +in of themselves. In particular, except for extremely marginal cases, usages +of bstrings that use the bstring library functions alone cannot lead to any +undefined action. But due to C/C++ language and library limitations, there +is no way to define a non-trivial library that is completely without +undefined operations. All such possible undefined operations are described +below: + +1) bstrings or struct tagbstrings that are not explicitely initialized cannot + be passed as a parameter to any bstring function. +2) The members of the NULL bstring cannot be accessed directly. (Though all + APIs and macros detect the NULL bstring.) +3) A bstring whose data member has not been obtained from a malloc or + compatible call and which is write accessible passed as a writable + parameter will lead to undefined results. (i.e., do not writeAllow any + constructed bstrings unless the data portion has been obtained from the + heap.) +4) If the headers of two strings alias but are not identical (which can only + happen via a defective manual construction), then passing them to a + bstring function in which one is writable is not defined. +5) If the mlen member is larger than the actual accessible length of the data + member for a writable bstring, or if the slen member is larger than the + readable length of the data member for a readable bstring, then the + corresponding bstring operations are undefined. +6) Any bstring definition whose header or accessible data portion has been + assigned to inaccessible or otherwise illegal memory clearly cannot be + acted upon by the bstring library in any way. +7) Destroying the source of an incremental split from within the callback + and not returning with a negative value (indicating that it should abort) + will lead to undefined behaviour. (Though *modifying* or adjusting the + state of the source data, even if those modification fail within the + bstrlib API, has well defined behavior.) +8) Modifying a bstring which is write protected by direct access has + undefined behavior. + +While this may seem like a long list, with the exception of invalid uses of +the writeAllow macro, and source destruction during an iterative split +without an accompanying abort, no usage of the bstring API alone can cause +any undefined scenario to occurr. I.e., the policy of restricting usage of +bstrings to the bstring API can significantly reduce the risk of runtime +errors (in practice it should eliminate them) related to string manipulation +due to undefined action. + +C++ wrapper +----------- + +A C++ wrapper has been created to enable bstring functionality for C++ in the +most natural (for C++ programers) way possible. The mandate for the C++ +wrapper is different from the base C bstring library. Since the C++ language +has far more abstracting capabilities, the CBString structure is considered +fully abstracted -- i.e., hand generated CBStrings are not supported (though +conversion from a struct tagbstring is allowed) and all detectable errors are +manifest as thrown exceptions. + +- The C++ class definitions are all under the namespace Bstrlib. bstrwrap.h + enables this namespace (with a using namespace Bstrlib; directive at the + end) unless the macro BSTRLIB_DONT_ASSUME_NAMESPACE has been defined before + it is included. + +- Erroneous accesses results in an exception being thrown. The exception + parameter is of type "struct CBStringException" which is derived from + std::exception if STL is used. A verbose description of the error message + can be obtained from the what() method. + +- CBString is a C++ structure derived from a struct tagbstring. An address + of a CBString cast to a bstring must not be passed to bdestroy. The bstring + C API has been made C++ safe and can be used directly in a C++ project. + +- It includes constructors which can take a char, '\0' terminated char + buffer, tagbstring, (char, repeat-value), a length delimited buffer or a + CBStringList to initialize it. + +- Concatenation is performed with the + and += operators. Comparisons are + done with the ==, !=, <, >, <= and >= operators. Note that == and != use + the biseq call, while <, >, <= and >= use bstrcmp. + +- CBString's can be directly cast to const character buffers. + +- CBString's can be directly cast to double, float, int or unsigned int so + long as the CBString are decimal representations of those types (otherwise + an exception will be thrown). Converting the other way should be done with + the format(a) method(s). + +- CBString contains the length, character and [] accessor methods. The + character and [] accessors are aliases of each other. If the bounds for + the string are exceeded, an exception is thrown. To avoid the overhead for + this check, first cast the CBString to a (const char *) and use [] to + dereference the array as normal. Note that the character and [] accessor + methods allows both reading and writing of individual characters. + +- The methods: format, formata, find, reversefind, findcaseless, + reversefindcaseless, midstr, insert, insertchrs, replace, findreplace, + findreplacecaseless, remove, findchr, nfindchr, alloc, toupper, tolower, + gets, read are analogous to the functions that can be found in the C API. + +- The caselessEqual and caselessCmp methods are analogous to biseqcaseless + and bstricmp functions respectively. + +- Note that just like the bformat function, the format and formata methods do + not automatically cast CBStrings into char * strings for "%s"-type + substitutions: + + CBString w("world"); + CBString h("Hello"); + CBString hw; + + /* The casts are necessary */ + hw.format ("%s, %s", (const char *)h, (const char *)w); + +- The methods trunc and repeat have been added instead of using pattern. + +- ltrim, rtrim and trim methods have been added. These remove characters + from a given character string set (defaulting to the whitespace characters) + from either the left, right or both ends of the CBString, respectively. + +- The method setsubstr is also analogous in functionality to bsetstr, except + that it cannot be passed NULL. Instead the method fill and the fill-style + constructor have been supplied to enable this functionality. + +- The writeprotect(), writeallow() and iswriteprotected() methods are + analogous to the bwriteprotect(), bwriteallow() and biswriteprotected() + macros in the C API. Write protection semantics in CBString are stronger + than with the C API in that indexed character assignment is checked for + write protection. However, unlike with the C API, a write protected + CBString can be destroyed by the destructor. + +- CBStream is a C++ structure which wraps a struct bStream (its not derived + from it, since destruction is slightly different). It is constructed by + passing in a bNread function pointer and a stream parameter cast to void *. + This structure includes methods for detecting eof, setting the buffer + length, reading the whole stream or reading entries line by line or block + by block, an unread function, and a peek function. + +- If STL is available, the CBStringList structure is derived from a vector of + CBString with various split methods. The split method has been overloaded + to accept either a character or CBString as the second parameter (when the + split parameter is a CBString any character in that CBString is used as a + seperator). The splitstr method takes a CBString as a substring seperator. + Joins can be performed via a CBString constructor which takes a + CBStringList as a parameter, or just using the CBString::join() method. + +- If there is proper support for std::iostreams, then the >> and << operators + and the getline() function have been added (with semantics the same as + those for std::string). + +Multithreading +-------------- + +A mutable bstring is kind of analogous to a small (two entry) linked list +allocated by malloc, with all aliasing completely under programmer control. +I.e., manipulation of one bstring will never affect any other distinct +bstring unless explicitely constructed to do so by the programmer via hand +construction or via building a reference. Bstrlib also does not use any +static or global storage, so there are no hidden unremovable race conditions. +Bstrings are also clearly not inherently thread local. So just like +char *'s, bstrings can be passed around from thread to thread and shared and +so on, so long as modifications to a bstring correspond to some kind of +exclusive access lock as should be expected (or if the bstring is read-only, +which can be enforced by bstring write protection) for any sort of shared +object in a multithreaded environment. + +Bsafe module +------------ + +For convenience, a bsafe module has been included. The idea is that if this +module is included, inadvertant usage of the most dangerous C functions will +be overridden and lead to an immediate run time abort. Of course, it should +be emphasized that usage of this module is completely optional. The +intention is essentially to provide an option for creating project safety +rules which can be enforced mechanically rather than socially. This is +useful for larger, or open development projects where its more difficult to +enforce social rules or "coding conventions". + +Problems not solved +------------------- + +Bstrlib is written for the C and C++ languages, which have inherent weaknesses +that cannot be easily solved: + +1. Memory leaks: Forgetting to call bdestroy on a bstring that is about to be + unreferenced, just as forgetting to call free on a heap buffer that is + about to be dereferenced. Though bstrlib itself is leak free. +2. Read before write usage: In C, declaring an auto bstring does not + automatically fill it with legal/valid contents. This problem has been + somewhat mitigated in C++. (The bstrDeclare and bstrFree macros from + bstraux can be used to help mitigate this problem.) + +Other problems not addressed: + +3. Built-in mutex usage to automatically avoid all bstring internal race + conditions in multitasking environments: The problem with trying to + implement such things at this low a level is that it is typically more + efficient to use locks in higher level primitives. There is also no + platform independent way to implement locks or mutexes. + +Note that except for spotty support of wide characters, the default C +standard library does not address any of these problems either. + +Configurable compilation options +-------------------------------- + +The Better String Library is not an application, it is a library. To compile +it, you need to compile bstrlib.c to an object file that is linked to your +application. A Makefile might contain entries such as the following to +accomplish this: + +BSTRDIR = $(CDIR)/bstrlib +INCLUDES = -I$(BSTRDIR) +BSTROBJS = $(ODIR)/bstrlib.o +DEFINES = +CFLAGS = -O3 -Wall -pedantic -ansi -s $(DEFINES) + +application: $(ODIR)/main.o $(BSTROBJS) + echo Linking: $@ + $(CC) $< $(BSTROBJS) -o $@ + +$(ODIR)/%.o : $(BSTRDIR)/%.c + echo Compiling: $< + $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ + +$(ODIR)/%.o : %.c + echo Compiling: $< + $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ + +You can configure bstrlib using with the standard macro defines passed to +the compiler. All configuration options are meant solely for the purpose of +compiler compatibility. Configuration options are not meant to change the +semantics or capabilities of the library, except where it is unavoidable. + +Since some C++ compilers don't include the Standard Template Library and some +have the options of disabling exception handling, a number of macros can be +used to conditionally compile support for each of this: + +BSTRLIB_CAN_USE_STL + + - defining this will enable the used of the Standard Template Library. + Defining BSTRLIB_CAN_USE_STL overrides the BSTRLIB_CANNOT_USE_STL macro. + +BSTRLIB_CANNOT_USE_STL + + - defining this will disable the use of the Standard Template Library. + Defining BSTRLIB_CAN_USE_STL overrides the BSTRLIB_CANNOT_USE_STL macro. + +BSTRLIB_CAN_USE_IOSTREAM + + - defining this will enable the used of streams from class std. Defining + BSTRLIB_CAN_USE_IOSTREAM overrides the BSTRLIB_CANNOT_USE_IOSTREAM macro. + +BSTRLIB_CANNOT_USE_IOSTREAM + + - defining this will disable the use of streams from class std. Defining + BSTRLIB_CAN_USE_IOSTREAM overrides the BSTRLIB_CANNOT_USE_IOSTREAM macro. + +BSTRLIB_THROWS_EXCEPTIONS + + - defining this will enable the exception handling within bstring. + Defining BSTRLIB_THROWS_EXCEPTIONS overrides the + BSTRLIB_DOESNT_THROWS_EXCEPTIONS macro. + +BSTRLIB_DOESNT_THROW_EXCEPTIONS + + - defining this will disable the exception handling within bstring. + Defining BSTRLIB_THROWS_EXCEPTIONS overrides the + BSTRLIB_DOESNT_THROW_EXCEPTIONS macro. + +Note that these macros must be defined consistently throughout all modules +that use CBStrings including bstrwrap.cpp. + +Some older C compilers do not support functions such as vsnprintf. This is +handled by the following macro variables: + +BSTRLIB_NOVSNP + + - defining this indicates that the compiler does not support vsnprintf. + This will cause bformat and bformata to not be declared. Note that + for some compilers, such as Turbo C, this is set automatically. + Defining BSTRLIB_NOVSNP overrides the BSTRLIB_VSNP_OK macro. + +BSTRLIB_VSNP_OK + + - defining this will disable the autodetection of compilers that do not + vsnprintf. + Defining BSTRLIB_NOVSNP overrides the BSTRLIB_VSNP_OK macro. + +Semantic compilation options +---------------------------- + +Bstrlib comes with very few compilation options for changing the semantics of +of the library. These are described below. + +BSTRLIB_DONT_ASSUME_NAMESPACE + + - Defining this before including bstrwrap.h will disable the automatic + enabling of the Bstrlib namespace for the C++ declarations. + +BSTRLIB_DONT_USE_VIRTUAL_DESTRUCTOR + + - Defining this will make the CBString destructor non-virtual. + +BSTRLIB_MEMORY_DEBUG + + - Defining this will cause the bstrlib modules bstrlib.c and bstrwrap.cpp + to invoke a #include "memdbg.h". memdbg.h has to be supplied by the user. + +Note that these macros must be defined consistently throughout all modules +that use bstrings or CBStrings including bstrlib.c, bstraux.c and +bstrwrap.cpp. + +=============================================================================== + +Files +----- + +Core C files (required for C and C++): +bstrlib.c - C implementaion of bstring functions. +bstrlib.h - C header file for bstring functions. + +Core C++ files (required for C++): +bstrwrap.cpp - C++ implementation of CBString. +bstrwrap.h - C++ header file for CBString. + +Base Unicode support: +utf8util.c - C implemention of generic utf8 parsing functions. +utf8util.h - C head file for generic utf8 parsing functions. +buniutil.c - C implemention utf8 bstring packing and unpacking functions. +buniutil.c - C header file for utf8 bstring functions. + +Extra utility functions: +bstraux.c - C example that implements trivial additional functions. +bstraux.h - C header for bstraux.c + +Miscellaneous: +bstest.c - C unit/regression test for bstrlib.c +test.cpp - C++ unit/regression test for bstrwrap.cpp +bsafe.c - C runtime stubs to abort usage of unsafe C functions. +bsafe.h - C header file for bsafe.c functions. + +C modules need only include bstrlib.h and compile/link bstrlib.c to use the +basic bstring library. C++ projects need to additionally include bstrwrap.h +and compile/link bstrwrap.cpp. For both, there may be a need to make choices +about feature configuration as described in the "Configurable compilation +options" in the section above. + +Other files that are included in this archive are: + +license.txt - The BSD license for Bstrlib +gpl.txt - The GPL version 2 +security.txt - A security statement useful for auditting Bstrlib +porting.txt - A guide to porting Bstrlib +bstrlib.txt - This file + +=============================================================================== + +The functions +------------- + + extern bstring bfromcstr (const char * str); + + Take a standard C library style '\0' terminated char buffer and generate + a bstring with the same contents as the char buffer. If an error occurs + NULL is returned. + + So for example: + + bstring b = bfromcstr ("Hello"); + if (!b) { + fprintf (stderr, "Out of memory"); + } else { + puts ((char *) b->data); + } + + .......................................................................... + + extern bstring bfromcstralloc (int mlen, const char * str); + + Create a bstring which contains the contents of the '\0' terminated + char * buffer str. The memory buffer backing the bstring is at least + mlen characters in length. The buffer is also at least size required + to hold the string with the '\0' terminator. If an error occurs NULL + is returned. + + So for example: + + bstring b = bfromcstralloc (64, someCstr); + if (b) b->data[63] = 'x'; + + The idea is that this will set the 64th character of b to 'x' if it is at + least 64 characters long otherwise do nothing. And we know this is well + defined so long as b was successfully created, since it will have been + allocated with at least 64 characters. + + .......................................................................... + + extern bstring bfromcstrrangealloc (int minl, int maxl, const char* str); + + Create a bstring which contains the contents of the '\0' terminated + char * buffer str. The memory buffer backing the string is at least + minl characters in length, but an attempt is made to allocate up to + maxl characters. The buffer is also at least size required to hold + the string with the '\0' terminator. If an error occurs NULL is + returned. + + So for example: + + bstring b = bfromcstrrangealloc (0, 128, "Hello."); + if (b) b->data[5] = '!'; + + The idea is that this will set the 6th character of b to '!' if it was + allocated otherwise do nothing. And we know this is well defined so + long as b was successfully created, since it will have been allocated + with at least 7 (strlen("Hello.")) characters. + + .......................................................................... + + extern bstring blk2bstr (const void * blk, int len); + + Create a bstring whose contents are described by the contiguous buffer + pointing to by blk with a length of len bytes. Note that this function + creates a copy of the data in blk, rather than simply referencing it. + Compare with the blk2tbstr macro. If an error occurs NULL is returned. + + .......................................................................... + + extern char * bstr2cstr (const_bstring s, char z); + + Create a '\0' terminated char buffer which contains the contents of the + bstring s, except that any contained '\0' characters are converted to the + character in z. This returned value should be freed with bcstrfree(), by + the caller. If an error occurs NULL is returned. + + .......................................................................... + + extern int bcstrfree (char * s); + + Frees a C-string generated by bstr2cstr (). This is normally unnecessary + since it just wraps a call to free (), however, if malloc () and free () + have been redefined as a macros within the bstrlib module (via macros in + the memdbg.h backdoor) with some difference in behaviour from the std + library functions, then this allows a correct way of freeing the memory + that allows higher level code to be independent from these macro + redefinitions. + + .......................................................................... + + extern bstring bstrcpy (const_bstring b1); + + Make a copy of the passed in bstring. The copied bstring is returned if + there is no error, otherwise NULL is returned. + + .......................................................................... + + extern int bassign (bstring a, const_bstring b); + + Overwrite the bstring a with the contents of bstring b. Note that the + bstring a must be a well defined and writable bstring. If an error + occurs BSTR_ERR is returned and a is not overwritten. + + .......................................................................... + + int bassigncstr (bstring a, const char * str); + + Overwrite the string a with the contents of char * string str. Note that + the bstring a must be a well defined and writable bstring. If an error + occurs BSTR_ERR is returned and a may be partially overwritten. + + .......................................................................... + + int bassignblk (bstring a, const void * s, int len); + + Overwrite the string a with the contents of the block (s, len). Note that + the bstring a must be a well defined and writable bstring. If an error + occurs BSTR_ERR is returned and a is not overwritten. + + .......................................................................... + + extern int bassignmidstr (bstring a, const_bstring b, int left, int len); + + Overwrite the bstring a with the middle of contents of bstring b + starting from position left and running for a length len. left and + len are clamped to the ends of b as with the function bmidstr. Note that + the bstring a must be a well defined and writable bstring. If an error + occurs BSTR_ERR is returned and a is not overwritten. + + .......................................................................... + + extern bstring bmidstr (const_bstring b, int left, int len); + + Create a bstring which is the substring of b starting from position left + and running for a length len (clamped by the end of the bstring b.) If + there was no error, the value of this constructed bstring is returned + otherwise NULL is returned. + + .......................................................................... + + extern int bdelete (bstring s1, int pos, int len); + + Removes characters from pos to pos+len-1 and shifts the tail of the + bstring starting from pos+len to pos. len must be positive for this call + to have any effect. The section of the bstring described by (pos, len) + is clamped to boundaries of the bstring b. The value BSTR_OK is returned + if the operation is successful, otherwise BSTR_ERR is returned. + + .......................................................................... + + extern int bconcat (bstring b0, const_bstring b1); + + Concatenate the bstring b1 to the end of bstring b0. The value BSTR_OK + is returned if the operation is successful, otherwise BSTR_ERR is + returned. + + .......................................................................... + + extern int bconchar (bstring b, char c); + + Concatenate the character c to the end of bstring b. The value BSTR_OK + is returned if the operation is successful, otherwise BSTR_ERR is + returned. + + .......................................................................... + + extern int bcatcstr (bstring b, const char * s); + + Concatenate the char * string s to the end of bstring b. The value + BSTR_OK is returned if the operation is successful, otherwise BSTR_ERR is + returned. + + .......................................................................... + + extern int bcatblk (bstring b, const void * s, int len); + + Concatenate a fixed length buffer (s, len) to the end of bstring b. The + value BSTR_OK is returned if the operation is successful, otherwise + BSTR_ERR is returned. + + .......................................................................... + + extern int biseq (const_bstring b0, const_bstring b1); + + Compare the bstring b0 and b1 for equality. If the bstrings differ, 0 + is returned, if the bstrings are the same, 1 is returned, if there is an + error, -1 is returned. If the length of the bstrings are different, this + function has O(1) complexity. Contained '\0' characters are not treated + as a termination character. + + Note that the semantics of biseq are not completely compatible with + bstrcmp because of its different treatment of the '\0' character. + + .......................................................................... + + extern int bisstemeqblk (const_bstring b, const void * blk, int len); + + Compare beginning of bstring b0 with a block of memory of length len for + equality. If the beginning of b0 differs from the memory block (or if b0 + is too short), 0 is returned, if the bstrings are the same, 1 is returned, + if there is an error, -1 is returned. + + .......................................................................... + + extern int biseqcaseless (const_bstring b0, const_bstring b1); + + Compare two bstrings for equality without differentiating between case. + If the bstrings differ other than in case, 0 is returned, if the bstrings + are the same, 1 is returned, if there is an error, -1 is returned. If + the length of the bstrings are different, this function is O(1). '\0' + termination characters are not treated in any special way. + + .......................................................................... + + extern int biseqcaselessblk (const_bstring b, const void * blk, int len); + + Compare content of b and the array of bytes in blk for length len for + equality without differentiating between character case. If the content + differs other than in case, 0 is returned, if, ignoring case, the content + is the same, 1 is returned, if there is an error, -1 is returned. If the + length of the strings are different, this function is O(1). '\0' + termination characters are not treated in any special way. + + .......................................................................... + + extern int bisstemeqcaselessblk (const_bstring b0, const void * blk, int len); + + Compare beginning of bstring b0 with a block of memory of length len + without differentiating between case for equality. If the beginning of b0 + differs from the memory block other than in case (or if b0 is too short), + 0 is returned, if the bstrings are the same, 1 is returned, if there is an + error, -1 is returned. + + .......................................................................... + + int biseqblk (const_bstring b, const void * blk, int len) + + Compare the string b with the character block blk of length len. If the + content differs, 0 is returned, if the content is the same, 1 is returned, + if there is an error, -1 is returned. If the length of the strings are + different, this function is O(1). '\0' characters are not treated in + any special way. + + .......................................................................... + + extern int biseqcstr (const_bstring b, const char *s); + + Compare the bstring b and char * bstring s. The C string s must be '\0' + terminated at exactly the length of the bstring b, and the contents + between the two must be identical with the bstring b with no '\0' + characters for the two contents to be considered equal. This is + equivalent to the condition that their current contents will be always be + equal when comparing them in the same format after converting one or the + other. If they are equal 1 is returned, if they are unequal 0 is + returned and if there is a detectable error BSTR_ERR is returned. + + .......................................................................... + + extern int biseqcstrcaseless (const_bstring b, const char *s); + + Compare the bstring b and char * string s. The C string s must be '\0' + terminated at exactly the length of the bstring b, and the contents + between the two must be identical except for case with the bstring b with + no '\0' characters for the two contents to be considered equal. This is + equivalent to the condition that their current contents will be always be + equal ignoring case when comparing them in the same format after + converting one or the other. If they are equal, except for case, 1 is + returned, if they are unequal regardless of case 0 is returned and if + there is a detectable error BSTR_ERR is returned. + + .......................................................................... + + extern int bstrcmp (const_bstring b0, const_bstring b1); + + Compare the bstrings b0 and b1 for ordering. If there is an error, + SHRT_MIN is returned, otherwise a value less than or greater than zero, + indicating that the bstring pointed to by b0 is lexicographically less + than or greater than the bstring pointed to by b1 is returned. If the + bstring lengths are unequal but the characters up until the length of the + shorter are equal then a value less than, or greater than zero, + indicating that the bstring pointed to by b0 is shorter or longer than the + bstring pointed to by b1 is returned. 0 is returned if and only if the + two bstrings are the same. If the length of the bstrings are different, + this function is O(n). Like its standard C library counter part, the + comparison does not proceed past any '\0' termination characters + encountered. + + The seemingly odd error return value, merely provides slightly more + granularity than the undefined situation given in the C library function + strcmp. The function otherwise behaves very much like strcmp(). + + Note that the semantics of bstrcmp are not completely compatible with + biseq because of its different treatment of the '\0' termination + character. + + .......................................................................... + + extern int bstrncmp (const_bstring b0, const_bstring b1, int n); + + Compare the bstrings b0 and b1 for ordering for at most n characters. If + there is an error, SHRT_MIN is returned, otherwise a value is returned as + if b0 and b1 were first truncated to at most n characters then bstrcmp + was called with these new bstrings are paremeters. If the length of the + bstrings are different, this function is O(n). Like its standard C + library counter part, the comparison does not proceed past any '\0' + termination characters encountered. + + The seemingly odd error return value, merely provides slightly more + granularity than the undefined situation given in the C library function + strncmp. The function otherwise behaves very much like strncmp(). + + .......................................................................... + + extern int bstricmp (const_bstring b0, const_bstring b1); + + Compare two bstrings without differentiating between case. The return + value is the difference of the values of the characters where the two + bstrings first differ, otherwise 0 is returned indicating that the + bstrings are equal. If the lengths are different, then a difference from + 0 is given, but if the first extra character is '\0', then it is taken to + be the value UCHAR_MAX+1. + + .......................................................................... + + extern int bstrnicmp (const_bstring b0, const_bstring b1, int n); + + Compare two bstrings without differentiating between case for at most n + characters. If the position where the two bstrings first differ is + before the nth position, the return value is the difference of the values + of the characters, otherwise 0 is returned. If the lengths are different + and less than n characters, then a difference from 0 is given, but if the + first extra character is '\0', then it is taken to be the value + UCHAR_MAX+1. + + .......................................................................... + + extern int bdestroy (bstring b); + + Deallocate the bstring passed. Passing NULL in as a parameter will have + no effect. Note that both the header and the data portion of the bstring + will be freed. No other bstring function which modifies one of its + parameters will free or reallocate the header. Because of this, in + general, bdestroy cannot be called on any declared struct tagbstring even + if it is not write protected. A bstring which is write protected cannot + be destroyed via the bdestroy call. Any attempt to do so will result in + no action taken, and BSTR_ERR will be returned. + + Note to C++ users: Passing in a CBString cast to a bstring will lead to + undefined behavior (free will be called on the header, rather than the + CBString destructor.) Instead just use the ordinary C++ language + facilities to dealloc a CBString. + + .......................................................................... + + extern int binstr (const_bstring s1, int pos, const_bstring s2); + + Search for the bstring s2 in s1 starting at position pos and looking in a + forward (increasing) direction. If it is found then it returns with the + first position after pos where it is found, otherwise it returns BSTR_ERR. + The algorithm used is brute force; O(m*n). + + .......................................................................... + + extern int binstrr (const_bstring s1, int pos, const_bstring s2); + + Search for the bstring s2 in s1 starting at position pos and looking in a + backward (decreasing) direction. If it is found then it returns with the + first position after pos where it is found, otherwise return BSTR_ERR. + Note that the current position at pos is tested as well -- so to be + disjoint from a previous forward search it is recommended that the + position be backed up (decremented) by one position. The algorithm used + is brute force; O(m*n). + + .......................................................................... + + extern int binstrcaseless (const_bstring s1, int pos, const_bstring s2); + + Search for the bstring s2 in s1 starting at position pos and looking in a + forward (increasing) direction but without regard to case. If it is + found then it returns with the first position after pos where it is + found, otherwise it returns BSTR_ERR. The algorithm used is brute force; + O(m*n). + + .......................................................................... + + extern int binstrrcaseless (const_bstring s1, int pos, const_bstring s2); + + Search for the bstring s2 in s1 starting at position pos and looking in a + backward (decreasing) direction but without regard to case. If it is + found then it returns with the first position after pos where it is + found, otherwise return BSTR_ERR. Note that the current position at pos + is tested as well -- so to be disjoint from a previous forward search it + is recommended that the position be backed up (decremented) by one + position. The algorithm used is brute force; O(m*n). + + .......................................................................... + + extern int binchr (const_bstring b0, int pos, const_bstring b1); + + Search for the first position in b0 starting from pos or after, in which + one of the characters in b1 is found. This function has an execution + time of O(b0->slen + b1->slen). If such a position does not exist in b0, + then BSTR_ERR is returned. + + .......................................................................... + + extern int binchrr (const_bstring b0, int pos, const_bstring b1); + + Search for the last position in b0 no greater than pos, in which one of + the characters in b1 is found. This function has an execution time + of O(b0->slen + b1->slen). If such a position does not exist in b0, + then BSTR_ERR is returned. + + .......................................................................... + + extern int bninchr (const_bstring b0, int pos, const_bstring b1); + + Search for the first position in b0 starting from pos or after, in which + none of the characters in b1 is found and return it. This function has + an execution time of O(b0->slen + b1->slen). If such a position does + not exist in b0, then BSTR_ERR is returned. + + .......................................................................... + + extern int bninchrr (const_bstring b0, int pos, const_bstring b1); + + Search for the last position in b0 no greater than pos, in which none of + the characters in b1 is found and return it. This function has an + execution time of O(b0->slen + b1->slen). If such a position does not + exist in b0, then BSTR_ERR is returned. + + .......................................................................... + + extern int bstrchr (const_bstring b, int c); + + Search for the character c in the bstring b forwards from the start of + the bstring. Returns the position of the found character or BSTR_ERR if + it is not found. + + NOTE: This has been implemented as a macro on top of bstrchrp (). + + .......................................................................... + + extern int bstrrchr (const_bstring b, int c); + + Search for the character c in the bstring b backwards from the end of the + bstring. Returns the position of the found character or BSTR_ERR if it is + not found. + + NOTE: This has been implemented as a macro on top of bstrrchrp (). + + .......................................................................... + + extern int bstrchrp (const_bstring b, int c, int pos); + + Search for the character c in b forwards from the position pos + (inclusive). Returns the position of the found character or BSTR_ERR if + it is not found. + + .......................................................................... + + extern int bstrrchrp (const_bstring b, int c, int pos); + + Search for the character c in b backwards from the position pos in bstring + (inclusive). Returns the position of the found character or BSTR_ERR if + it is not found. + + .......................................................................... + + extern int bsetstr (bstring b0, int pos, const_bstring b1, unsigned char fill); + + Overwrite the bstring b0 starting at position pos with the bstring b1. If + the position pos is past the end of b0, then the character "fill" is + appended as necessary to make up the gap between the end of b0 and pos. + If b1 is NULL, it behaves as if it were a 0-length bstring. The value + BSTR_OK is returned if the operation is successful, otherwise BSTR_ERR is + returned. + + .......................................................................... + + extern int binsert (bstring s1, int pos, const_bstring s2, unsigned char fill); + + Inserts the bstring s2 into s1 at position pos. If the position pos is + past the end of s1, then the character "fill" is appended as necessary to + make up the gap between the end of s1 and pos. The value BSTR_OK is + returned if the operation is successful, otherwise BSTR_ERR is returned. + + .......................................................................... + + int binsertblk (bstring b, int pos, const void * blk, int len, + unsigned char fill) + + Inserts the block of characters at blk with length len into b at position + pos. If the position pos is past the end of b, then the character "fill" + is appended as necessary to make up the gap between the end of b1 and pos. + Unlike bsetstr, binsert does not allow b2 to be NULL. + + .......................................................................... + + extern int binsertch (bstring s1, int pos, int len, unsigned char fill); + + Inserts the character fill repeatedly into s1 at position pos for a + length len. If the position pos is past the end of s1, then the + character "fill" is appended as necessary to make up the gap between the + end of s1 and the position pos + len (exclusive). The value BSTR_OK is + returned if the operation is successful, otherwise BSTR_ERR is returned. + + .......................................................................... + + extern int breplace (bstring b1, int pos, int len, const_bstring b2, + unsigned char fill); + + Replace a section of a bstring from pos for a length len with the bstring + b2. If the position pos is past the end of b1 then the character "fill" + is appended as necessary to make up the gap between the end of b1 and + pos. + + .......................................................................... + + extern int bfindreplace (bstring b, const_bstring find, + const_bstring replace, int position); + + Replace all occurrences of the find substring with a replace bstring + after a given position in the bstring b. The find bstring must have a + length > 0 otherwise BSTR_ERR is returned. This function does not + perform recursive per character replacement; that is to say successive + searches resume at the position after the last replace. + + So for example: + + bfindreplace (a0 = bfromcstr("aabaAb"), a1 = bfromcstr("a"), + a2 = bfromcstr("aa"), 0); + + Should result in changing a0 to "aaaabaaAb". + + This function performs exactly (b->slen - position) bstring comparisons, + and data movement is bounded above by character volume equivalent to size + of the output bstring. + + .......................................................................... + + extern int bfindreplacecaseless (bstring b, const_bstring find, + const_bstring replace, int position); + + Replace all occurrences of the find substring, ignoring case, with a + replace bstring after a given position in the bstring b. The find bstring + must have a length > 0 otherwise BSTR_ERR is returned. This function + does not perform recursive per character replacement; that is to say + successive searches resume at the position after the last replace. + + So for example: + + bfindreplacecaseless (a0 = bfromcstr("AAbaAb"), a1 = bfromcstr("a"), + a2 = bfromcstr("aa"), 0); + + Should result in changing a0 to "aaaabaaaab". + + This function performs exactly (b->slen - position) bstring comparisons, + and data movement is bounded above by character volume equivalent to size + of the output bstring. + + .......................................................................... + + extern int balloc (bstring b, int length); + + Increase the allocated memory backing the data buffer for the bstring b + to a length of at least length. If the memory backing the bstring b is + already large enough, not action is performed. This has no effect on the + bstring b that is visible to the bstring API. Usually this function will + only be used when a minimum buffer size is required coupled with a direct + access to the ->data member of the bstring structure. + + Be warned that like any other bstring function, the bstring must be well + defined upon entry to this function. I.e., doing something like: + + b->slen *= 2; /* ?? Most likely incorrect */ + balloc (b, b->slen); + + is invalid, and should be implemented as: + + int t; + if (BSTR_OK == balloc (b, t = (b->slen * 2))) b->slen = t; + + This function will return with BSTR_ERR if b is not detected as a valid + bstring or length is not greater than 0, otherwise BSTR_OK is returned. + + .......................................................................... + + extern int ballocmin (bstring b, int length); + + Change the amount of memory backing the bstring b to at least length. + This operation will never truncate the bstring data including the + extra terminating '\0' and thus will not decrease the length to less than + b->slen + 1. Note that repeated use of this function may cause + performance problems (realloc may be called on the bstring more than + the O(log(INT_MAX)) times). This function will return with BSTR_ERR if b + is not detected as a valid bstring or length is not greater than 0, + otherwise BSTR_OK is returned. + + So for example: + + if (BSTR_OK == ballocmin (b, 64)) b->data[63] = 'x'; + + The idea is that this will set the 64th character of b to 'x' if it is at + least 64 characters long otherwise do nothing. And we know this is well + defined so long as the ballocmin call was successfully, since it will + ensure that b has been allocated with at least 64 characters. + + .......................................................................... + + int btrunc (bstring b, int n); + + Truncate the bstring to at most n characters. This function will return + with BSTR_ERR if b is not detected as a valid bstring or n is less than + 0, otherwise BSTR_OK is returned. + + .......................................................................... + + extern int bpattern (bstring b, int len); + + Replicate the starting bstring, b, end to end repeatedly until it + surpasses len characters, then chop the result to exactly len characters. + This function operates in-place. This function will return with BSTR_ERR + if b is NULL or of length 0, otherwise BSTR_OK is returned. + + .......................................................................... + + extern int btoupper (bstring b); + + Convert contents of bstring to upper case. This function will return with + BSTR_ERR if b is NULL or of length 0, otherwise BSTR_OK is returned. + + .......................................................................... + + extern int btolower (bstring b); + + Convert contents of bstring to lower case. This function will return with + BSTR_ERR if b is NULL or of length 0, otherwise BSTR_OK is returned. + + .......................................................................... + + extern int bltrimws (bstring b); + + Delete whitespace contiguous from the left end of the bstring. This + function will return with BSTR_ERR if b is NULL or of length 0, otherwise + BSTR_OK is returned. + + .......................................................................... + + extern int brtrimws (bstring b); + + Delete whitespace contiguous from the right end of the bstring. This + function will return with BSTR_ERR if b is NULL or of length 0, otherwise + BSTR_OK is returned. + + .......................................................................... + + extern int btrimws (bstring b); + + Delete whitespace contiguous from both ends of the bstring. This function + will return with BSTR_ERR if b is NULL or of length 0, otherwise BSTR_OK + is returned. + + .......................................................................... + + extern struct bstrList* bstrListCreate (void); + + Create an empty struct bstrList. The struct bstrList output structure is + declared as follows: + + struct bstrList { + int qty, mlen; + bstring * entry; + }; + + The entry field actually is an array with qty number entries. The mlen + record counts the maximum number of bstring's for which there is memory + in the entry record. + + The Bstrlib API does *NOT* include a comprehensive set of functions for + full management of struct bstrList in an abstracted way. The reason for + this is because aliasing semantics of the list are best left to the user + of this function, and performance varies wildly depending on the + assumptions made. For a complete list of bstring data type it is + recommended that the C++ public std::vector be used, since its + semantics are usage are more standard. + + .......................................................................... + + extern int bstrListDestroy (struct bstrList * sl); + + Destroy a struct bstrList structure that was returned by the bsplit + function. Note that this will destroy each bstring in the ->entry array + as well. See bstrListCreate() above for structure of struct bstrList. + + .......................................................................... + + extern int bstrListAlloc (struct bstrList * sl, int msz); + + Ensure that there is memory for at least msz number of entries for the + list. + + .......................................................................... + + extern int bstrListAllocMin (struct bstrList * sl, int msz); + + Try to allocate the minimum amount of memory for the list to include at + least msz entries or sl->qty whichever is greater. + + .......................................................................... + + extern struct bstrList * bsplit (bstring str, unsigned char splitChar); + + Create an array of sequential substrings from str divided by the + character splitChar. Successive occurrences of the splitChar will be + divided by empty bstring entries, following the semantics from the Python + programming language. To reclaim the memory from this output structure, + bstrListDestroy () should be called. See bstrListCreate() above for + structure of struct bstrList. + + .......................................................................... + + extern struct bstrList * bsplits (bstring str, const_bstring splitStr); + + Create an array of sequential substrings from str divided by any + character contained in splitStr. An empty splitStr causes a single entry + bstrList containing a copy of str to be returned. See bstrListCreate() + above for structure of struct bstrList. + + .......................................................................... + + extern struct bstrList * bsplitstr (bstring str, const_bstring splitStr); + + Create an array of sequential substrings from str divided by the entire + substring splitStr. An empty splitStr causes a single entry bstrList + containing a copy of str to be returned. See bstrListCreate() above for + structure of struct bstrList. + + .......................................................................... + + extern bstring bjoin (const struct bstrList * bl, const_bstring sep); + + Join the entries of a bstrList into one bstring by sequentially + concatenating them with the sep bstring in between. If sep is NULL, it + is treated as if it were the empty bstring. Note that: + + bjoin (l = bsplit (b, s->data[0]), s); + + should result in a copy of b, if s->slen is 1. If there is an error NULL + is returned, otherwise a bstring with the correct result is returned. + See bstrListCreate() above for structure of struct bstrList. + + .......................................................................... + + bstring bjoinblk (const struct bstrList * bl, void * blk, int len); + + Join the entries of a bstrList into one bstring by sequentially + concatenating them with the content from blk for length len in between. + If there is an error NULL is returned, otherwise a bstring with the + correct result is returned. + + .......................................................................... + + extern int bsplitcb (const_bstring str, unsigned char splitChar, int pos, + int (* cb) (void * parm, int ofs, int len), void * parm); + + Iterate the set of disjoint sequential substrings over str starting at + position pos divided by the character splitChar. The parm passed to + bsplitcb is passed on to cb. If the function cb returns a value < 0, + then further iterating is halted and this value is returned by bsplitcb. + + Note: Non-destructive modification of str from within the cb function + while performing this split is not undefined. bsplitcb behaves in + sequential lock step with calls to cb. I.e., after returning from a cb + that return a non-negative integer, bsplitcb continues from the position + 1 character after the last detected split character and it will halt + immediately if the length of str falls below this point. However, if the + cb function destroys str, then it *must* return with a negative value, + otherwise bsplitcb will continue in an undefined manner. + + This function is provided as an incremental alternative to bsplit that is + abortable and which does not impose additional memory allocation. + + .......................................................................... + + extern int bsplitscb (const_bstring str, const_bstring splitStr, int pos, + int (* cb) (void * parm, int ofs, int len), void * parm); + + Iterate the set of disjoint sequential substrings over str starting at + position pos divided by any of the characters in splitStr. An empty + splitStr causes the whole str to be iterated once. The parm passed to + bsplitcb is passed on to cb. If the function cb returns a value < 0, + then further iterating is halted and this value is returned by bsplitcb. + + Note: Non-destructive modification of str from within the cb function + while performing this split is not undefined. bsplitscb behaves in + sequential lock step with calls to cb. I.e., after returning from a cb + that return a non-negative integer, bsplitscb continues from the position + 1 character after the last detected split character and it will halt + immediately if the length of str falls below this point. However, if the + cb function destroys str, then it *must* return with a negative value, + otherwise bsplitscb will continue in an undefined manner. + + This function is provided as an incremental alternative to bsplits that + is abortable and which does not impose additional memory allocation. + + .......................................................................... + + extern int bsplitstrcb (const_bstring str, const_bstring splitStr, int pos, + int (* cb) (void * parm, int ofs, int len), void * parm); + + Iterate the set of disjoint sequential substrings over str starting at + position pos divided by the entire substring splitStr. An empty splitStr + causes each character of str to be iterated. The parm passed to bsplitcb + is passed on to cb. If the function cb returns a value < 0, then further + iterating is halted and this value is returned by bsplitcb. + + Note: Non-destructive modification of str from within the cb function + while performing this split is not undefined. bsplitstrcb behaves in + sequential lock step with calls to cb. I.e., after returning from a cb + that return a non-negative integer, bsplitstrcb continues from the position + 1 character after the last detected split character and it will halt + immediately if the length of str falls below this point. However, if the + cb function destroys str, then it *must* return with a negative value, + otherwise bsplitscb will continue in an undefined manner. + + This function is provided as an incremental alternative to bsplitstr that + is abortable and which does not impose additional memory allocation. + + .......................................................................... + + extern bstring bformat (const char * fmt, ...); + + Takes the same parameters as printf (), but rather than outputting + results to stdio, it forms a bstring which contains what would have been + output. Note that if there is an early generation of a '\0' character, + the bstring will be truncated to this end point. + + Note that %s format tokens correspond to '\0' terminated char * buffers, + not bstrings. To print a bstring, first dereference data element of the + the bstring: + + /* b1->data needs to be '\0' terminated, so tagbstrings generated + by blk2tbstr () might not be suitable. */ + b0 = bformat ("Hello, %s", b1->data); + + Note that if the BSTRLIB_NOVSNP macro has been set when bstrlib has been + compiled the bformat function is not present. + + .......................................................................... + + extern int bformata (bstring b, const char * fmt, ...); + + In addition to the initial output buffer b, bformata takes the same + parameters as printf (), but rather than outputting results to stdio, it + appends the results to the initial bstring parameter. Note that if + there is an early generation of a '\0' character, the bstring will be + truncated to this end point. + + Note that %s format tokens correspond to '\0' terminated char * buffers, + not bstrings. To print a bstring, first dereference data element of the + the bstring: + + /* b1->data needs to be '\0' terminated, so tagbstrings generated + by blk2tbstr () might not be suitable. */ + bformata (b0 = bfromcstr ("Hello"), ", %s", b1->data); + + Note that if the BSTRLIB_NOVSNP macro has been set when bstrlib has been + compiled the bformata function is not present. + + .......................................................................... + + extern int bassignformat (bstring b, const char * fmt, ...); + + After the first parameter, it takes the same parameters as printf (), but + rather than outputting results to stdio, it outputs the results to + the bstring parameter b. Note that if there is an early generation of a + '\0' character, the bstring will be truncated to this end point. + + Note that %s format tokens correspond to '\0' terminated char * buffers, + not bstrings. To print a bstring, first dereference data element of the + the bstring: + + /* b1->data needs to be '\0' terminated, so tagbstrings generated + by blk2tbstr () might not be suitable. */ + bassignformat (b0 = bfromcstr ("Hello"), ", %s", b1->data); + + Note that if the BSTRLIB_NOVSNP macro has been set when bstrlib has been + compiled the bassignformat function is not present. + + .......................................................................... + + extern int bvcformata (bstring b, int count, const char * fmt, va_list arglist); + + The bvcformata function formats data under control of the format control + string fmt and attempts to append the result to b. The fmt parameter is + the same as that of the printf function. The variable argument list is + replaced with arglist, which has been initialized by the va_start macro. + The size of the output is upper bounded by count. If the required output + exceeds count, the string b is not augmented with any contents and a value + below BSTR_ERR is returned. If a value below -count is returned then it + is recommended that the negative of this value be used as an update to the + count in a subsequent pass. On other errors, such as running out of + memory, parameter errors or numeric wrap around BSTR_ERR is returned. + BSTR_OK is returned when the output is successfully generated and + appended to b. + + Note: There is no sanity checking of arglist, and this function is + destructive of the contents of b from the b->slen point onward. If there + is an early generation of a '\0' character, the bstring will be truncated + to this end point. + + Although this function is part of the external API for Bstrlib, the + interface and semantics (length limitations, and unusual return codes) + are fairly atypical. The real purpose for this function is to provide an + engine for the bvformata macro. + + Note that if the BSTRLIB_NOVSNP macro has been set when bstrlib has been + compiled the bvcformata function is not present. + + .......................................................................... + + extern bstring bread (bNread readPtr, void * parm); + typedef size_t (* bNread) (void *buff, size_t elsize, size_t nelem, + void *parm); + + Read an entire stream into a bstring, verbatum. The readPtr function + pointer is compatible with fread sematics, except that it need not obtain + the stream data from a file. The intention is that parm would contain + the stream data context/state required (similar to the role of the FILE* + I/O stream parameter of fread.) + + Abstracting the block read function allows for block devices other than + file streams to be read if desired. Note that there is an ANSI + compatibility issue if "fread" is used directly; see the ANSI issues + section below. + + .......................................................................... + + extern int breada (bstring b, bNread readPtr, void * parm); + + Read an entire stream and append it to a bstring, verbatum. Behaves + like bread, except that it appends it results to the bstring b. + BSTR_ERR is returned on error, otherwise 0 is returned. + + .......................................................................... + + extern bstring bgets (bNgetc getcPtr, void * parm, char terminator); + typedef int (* bNgetc) (void * parm); + + Read a bstring from a stream. As many bytes as is necessary are read + until the terminator is consumed or no more characters are available from + the stream. If read from the stream, the terminator character will be + appended to the end of the returned bstring. The getcPtr function must + have the same semantics as the fgetc C library function (i.e., returning + an integer whose value is negative when there are no more characters + available, otherwise the value of the next available unsigned character + from the stream.) The intention is that parm would contain the stream + data context/state required (similar to the role of the FILE* I/O stream + parameter of fgets.) If no characters are read, or there is some other + detectable error, NULL is returned. + + bgets will never call the getcPtr function more often than necessary to + construct its output (including a single call, if required, to determine + that the stream contains no more characters.) + + Abstracting the character stream function and terminator character allows + for different stream devices and string formats other than '\n' + terminated lines in a file if desired (consider \032 terminated email + messages, in a UNIX mailbox for example.) + + For files, this function can be used analogously as fgets as follows: + + fp = fopen ( ... ); + if (fp) b = bgets ((bNgetc) fgetc, fp, '\n'); + + (Note that only one terminator character can be used, and that '\0' is + not assumed to terminate the stream in addition to the terminator + character. This is consistent with the semantics of fgets.) + + .......................................................................... + + extern int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator); + + Read from a stream and concatenate to a bstring. Behaves like bgets, + except that it appends it results to the bstring b. The value 1 is + returned if no characters are read before a negative result is returned + from getcPtr. Otherwise BSTR_ERR is returned on error, and 0 is returned + in other normal cases. + + .......................................................................... + + extern int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator); + + Read from a stream and concatenate to a bstring. Behaves like bgets, + except that it assigns the results to the bstring b. The value 1 is + returned if no characters are read before a negative result is returned + from getcPtr. Otherwise BSTR_ERR is returned on error, and 0 is returned + in other normal cases. + + .......................................................................... + + extern struct bStream * bsopen (bNread readPtr, void * parm); + + Wrap a given open stream (described by a fread compatible function + pointer and stream handle) into an open bStream suitable for the bstring + library streaming functions. + + .......................................................................... + + extern void * bsclose (struct bStream * s); + + Close the bStream, and return the handle to the stream that was + originally used to open the given stream. If s is NULL or detectably + invalid, NULL will be returned. + + .......................................................................... + + extern int bsbufflength (struct bStream * s, int sz); + + Set the length of the buffer used by the bStream. If sz is the macro + BSTR_BS_BUFF_LENGTH_GET (which is 0), the length is not set. If s is + NULL or sz is negative, the function will return with BSTR_ERR, otherwise + this function returns with the previous length. + + .......................................................................... + + extern int bsreadln (bstring r, struct bStream * s, char terminator); + + Read a bstring terminated by the terminator character or the end of the + stream from the bStream (s) and return it into the parameter r. The + matched terminator, if found, appears at the end of the line read. If + the stream has been exhausted of all available data, before any can be + read, BSTR_ERR is returned. This function may read additional characters + into the stream buffer from the core stream that are not returned, but + will be retained for subsequent read operations. When reading from high + speed streams, this function can perform significantly faster than bgets. + + .......................................................................... + + extern int bsreadlna (bstring r, struct bStream * s, char terminator); + + Read a bstring terminated by the terminator character or the end of the + stream from the bStream (s) and concatenate it to the parameter r. The + matched terminator, if found, appears at the end of the line read. If + the stream has been exhausted of all available data, before any can be + read, BSTR_ERR is returned. This function may read additional characters + into the stream buffer from the core stream that are not returned, but + will be retained for subsequent read operations. When reading from high + speed streams, this function can perform significantly faster than bgets. + + .......................................................................... + + extern int bsreadlns (bstring r, struct bStream * s, bstring terminators); + + Read a bstring terminated by any character in the terminators bstring or + the end of the stream from the bStream (s) and return it into the + parameter r. This function may read additional characters from the core + stream that are not returned, but will be retained for subsequent read + operations. + + .......................................................................... + + extern int bsreadlnsa (bstring r, struct bStream * s, bstring terminators); + + Read a bstring terminated by any character in the terminators bstring or + the end of the stream from the bStream (s) and concatenate it to the + parameter r. If the stream has been exhausted of all available data, + before any can be read, BSTR_ERR is returned. This function may read + additional characters from the core stream that are not returned, but + will be retained for subsequent read operations. + + .......................................................................... + + extern int bsread (bstring r, struct bStream * s, int n); + + Read a bstring of length n (or, if it is fewer, as many bytes as is + remaining) from the bStream. This function will read the minimum + required number of additional characters from the core stream. When the + stream is at the end of the file BSTR_ERR is returned, otherwise BSTR_OK + is returned. + + .......................................................................... + + extern int bsreada (bstring r, struct bStream * s, int n); + + Read a bstring of length n (or, if it is fewer, as many bytes as is + remaining) from the bStream and concatenate it to the parameter r. This + function will read the minimum required number of additional characters + from the core stream. When the stream is at the end of the file BSTR_ERR + is returned, otherwise BSTR_OK is returned. + + .......................................................................... + + extern int bsunread (struct bStream * s, const_bstring b); + + Insert a bstring into the bStream at the current position. These + characters will be read prior to those that actually come from the core + stream. + + .......................................................................... + + extern int bspeek (bstring r, const struct bStream * s); + + Return the number of currently buffered characters from the bStream that + will be read prior to reads from the core stream, and append it to the + the parameter r. + + .......................................................................... + + extern int bssplitscb (struct bStream * s, const_bstring splitStr, + int (* cb) (void * parm, int ofs, const_bstring entry), void * parm); + + Iterate the set of disjoint sequential substrings over the stream s + divided by any character from the bstring splitStr. The parm passed to + bssplitscb is passed on to cb. If the function cb returns a value < 0, + then further iterating is halted and this return value is returned by + bssplitscb. + + Note: At the point of calling the cb function, the bStream pointer is + pointed exactly at the position right after having read the split + character. The cb function can act on the stream by causing the bStream + pointer to move, and bssplitscb will continue by starting the next split + at the position of the pointer after the return from cb. + + However, if the cb causes the bStream s to be destroyed then the cb must + return with a negative value, otherwise bssplitscb will continue in an + undefined manner. + + This function is provided as way to incrementally parse through a file + or other generic stream that in total size may otherwise exceed the + practical or desired memory available. As with the other split callback + based functions this is abortable and does not impose additional memory + allocation. + + .......................................................................... + + extern int bssplitstrcb (struct bStream * s, const_bstring splitStr, + int (* cb) (void * parm, int ofs, const_bstring entry), void * parm); + + Iterate the set of disjoint sequential substrings over the stream s + divided by the entire substring splitStr. The parm passed to + bssplitstrcb is passed on to cb. If the function cb returns a + value < 0, then further iterating is halted and this return value is + returned by bssplitstrcb. + + Note: At the point of calling the cb function, the bStream pointer is + pointed exactly at the position right after having read the split + character. The cb function can act on the stream by causing the bStream + pointer to move, and bssplitstrcb will continue by starting the next + split at the position of the pointer after the return from cb. + + However, if the cb causes the bStream s to be destroyed then the cb must + return with a negative value, otherwise bssplitscb will continue in an + undefined manner. + + This function is provided as way to incrementally parse through a file + or other generic stream that in total size may otherwise exceed the + practical or desired memory available. As with the other split callback + based functions this is abortable and does not impose additional memory + allocation. + + .......................................................................... + + extern int bseof (const struct bStream * s); + + Return the defacto "EOF" (end of file) state of a stream (1 if the + bStream is in an EOF state, 0 if not, and BSTR_ERR if stream is closed or + detectably erroneous.) When the readPtr callback returns a value <= 0 + the stream reaches its "EOF" state. Note that bunread with non-empty + content will essentially turn off this state, and the stream will not be + in its "EOF" state so long as its possible to read more data out of it. + + Also note that the semantics of bseof() are slightly different from + something like feof(). I.e., reaching the end of the stream does not + necessarily guarantee that bseof() will return with a value indicating + that this has happened. bseof() will only return indicating that it has + reached the "EOF" and an attempt has been made to read past the end of + the bStream. + +The macros +---------- + + The macros described below are shown in a prototype form indicating their + intended usage. Note that the parameters passed to these macros will be + referenced multiple times. As with all macros, programmer care is + required to guard against unintended side effects. + + int blengthe (const_bstring b, int err); + + Returns the length of the bstring. If the bstring is NULL err is + returned. + + .......................................................................... + + int blength (const_bstring b); + + Returns the length of the bstring. If the bstring is NULL, the length + returned is 0. + + .......................................................................... + + int bchare (const_bstring b, int p, int c); + + Returns the p'th character of the bstring b. If the position p refers to + a position that does not exist in the bstring or the bstring is NULL, + then c is returned. + + .......................................................................... + + char bchar (const_bstring b, int p); + + Returns the p'th character of the bstring b. If the position p refers to + a position that does not exist in the bstring or the bstring is NULL, + then '\0' is returned. + + .......................................................................... + + char * bdatae (bstring b, char * err); + + Returns the char * data portion of the bstring b. If b is NULL, err is + returned. + + .......................................................................... + + char * bdata (bstring b); + + Returns the char * data portion of the bstring b. If b is NULL, NULL is + returned. + + .......................................................................... + + char * bdataofse (bstring b, int ofs, char * err); + + Returns the char * data portion of the bstring b offset by ofs. If b is + NULL, err is returned. + + .......................................................................... + + char * bdataofs (bstring b, int ofs); + + Returns the char * data portion of the bstring b offset by ofs. If b is + NULL, NULL is returned. + + .......................................................................... + + struct tagbstring var = bsStatic ("..."); + + The bsStatic macro allows for static declarations of literal string + constants as struct tagbstring structures. The resulting tagbstring does + not need to be freed or destroyed. Note that this macro is only well + defined for string literal arguments. For more general string pointers, + use the btfromcstr macro. + + The resulting struct tagbstring is permanently write protected. Attempts + to write to this struct tagbstring from any bstrlib function will lead to + BSTR_ERR being returned. Invoking the bwriteallow macro onto this struct + tagbstring has no effect. + + .......................................................................... + + <- bsStaticBlkParms ("...") + + The bsStaticBlkParms macro emits a pair of comma seperated parameters + corresponding to the block parameters for the block functions in Bstrlib + (i.e., blk2bstr, bcatblk, blk2tbstr, bisstemeqblk, bisstemeqcaselessblk.) + Note that this macro is only well defined for string literal arguments. + + Examples: + + bstring b = blk2bstr (bsStaticBlkParms ("Fast init. ")); + bcatblk (b, bsStaticBlkParms ("No frills fast concatenation.")); + + These are faster than using bfromcstr() and bcatcstr() respectively + because the length of the inline string is known as a compile time + constant. Also note that seperate struct tagbstring declarations for + holding the output of a bsStatic() macro are not required. + + .......................................................................... + + void btfromcstr (struct tagbstring& t, const char * s); + + Fill in the tagbstring t with the '\0' terminated char buffer s. This + action is purely reference oriented; no memory management is done. The + data member is just assigned s, and slen is assigned the strlen of s. + The s parameter is accessed exactly once in this macro. + + The resulting struct tagbstring is initially write protected. Attempts + to write to this struct tagbstring in a write protected state from any + bstrlib function will lead to BSTR_ERR being returned. Invoke the + bwriteallow on this struct tagbstring to make it writeable (though this + requires that s be obtained from a function compatible with malloc.) + + .......................................................................... + + void btfromblk (struct tagbstring& t, void * s, int len); + + Fill in the tagbstring t with the data buffer s with length len. This + action is purely reference oriented; no memory management is done. The + data member of t is just assigned s, and slen is assigned len. Note that + the buffer is not appended with a '\0' character. The s and len + parameters are accessed exactly once each in this macro. + + The resulting struct tagbstring is initially write protected. Attempts + to write to this struct tagbstring in a write protected state from any + bstrlib function will lead to BSTR_ERR being returned. Invoke the + bwriteallow on this struct tagbstring to make it writeable (though this + requires that s be obtained from a function compatible with malloc.) + + .......................................................................... + + void btfromblkltrimws (struct tagbstring& t, void * s, int len); + + Fill in the tagbstring t with the data buffer s with length len after it + has been left trimmed. This action is purely reference oriented; no + memory management is done. The data member of t is just assigned to a + pointer inside the buffer s. Note that the buffer is not appended with a + '\0' character. The s and len parameters are accessed exactly once each + in this macro. + + The resulting struct tagbstring is permanently write protected. Attempts + to write to this struct tagbstring from any bstrlib function will lead to + BSTR_ERR being returned. Invoking the bwriteallow macro onto this struct + tagbstring has no effect. + + .......................................................................... + + void btfromblkrtrimws (struct tagbstring& t, void * s, int len); + + Fill in the tagbstring t with the data buffer s with length len after it + has been right trimmed. This action is purely reference oriented; no + memory management is done. The data member of t is just assigned to a + pointer inside the buffer s. Note that the buffer is not appended with a + '\0' character. The s and len parameters are accessed exactly once each + in this macro. + + The resulting struct tagbstring is permanently write protected. Attempts + to write to this struct tagbstring from any bstrlib function will lead to + BSTR_ERR being returned. Invoking the bwriteallow macro onto this struct + tagbstring has no effect. + + .......................................................................... + + void btfromblktrimws (struct tagbstring& t, void * s, int len); + + Fill in the tagbstring t with the data buffer s with length len after it + has been left and right trimmed. This action is purely reference + oriented; no memory management is done. The data member of t is just + assigned to a pointer inside the buffer s. Note that the buffer is not + appended with a '\0' character. The s and len parameters are accessed + exactly once each in this macro. + + The resulting struct tagbstring is permanently write protected. Attempts + to write to this struct tagbstring from any bstrlib function will lead to + BSTR_ERR being returned. Invoking the bwriteallow macro onto this struct + tagbstring has no effect. + + .......................................................................... + + void bmid2tbstr (struct tagbstring& t, bstring b, int pos, int len); + + Fill the tagbstring t with the substring from b, starting from position + pos with a length len. The segment is clamped by the boundaries of + the bstring b. This action is purely reference oriented; no memory + management is done. Note that the buffer is not appended with a '\0' + character. Note that the t parameter to this macro may be accessed + multiple times. Note that the contents of t will become undefined + if the contents of b change or are destroyed. + + The resulting struct tagbstring is permanently write protected. Attempts + to write to this struct tagbstring in a write protected state from any + bstrlib function will lead to BSTR_ERR being returned. Invoking the + bwriteallow macro on this struct tagbstring will have no effect. + + .......................................................................... + + bstring bfromStatic("..."); + + Allocate a bstring with the contents of a string literal. Returns + NULL if an error has occurred (ran out of memory). The string literal + parameter is enforced as literal at compile time. + + .......................................................................... + + int bcatStatic (bstring b, "..."); + + Append a string literal to bstring b. Returns 0 if successful, or + BSTR_ERR if some error has occurred. The string literal parameter is + enforced as literal at compile time. + + .......................................................................... + + int binsertStatic (bstring s1, int pos, " ... ", char fill); + + Inserts the string literal into s1 at position pos. If the position pos + is past the end of s1, then the character "fill" is appended as necessary + to make up the gap between the end of s1 and pos. The value BSTR_OK is + returned if the operation is successful, otherwise BSTR_ERR is returned. + + .......................................................................... + + int bassignStatic (bstring b, " ... "); + + Assign the contents of a string literal to the bstring b. The string + literal parameter is enforced as literal at compile time. + + .......................................................................... + + int biseqStatic (const_bstring b, " ... "); + + Compare the string b with the string literal. If the content differs, 0 + is returned, if the content is the same, 1 is returned, if there is an + error, -1 is returned. If the length of the strings are different, this + function is O(1). '\0' characters are not treated in any special way. + + .......................................................................... + + int biseqcaselessStatic (const_bstring b, " ... "); + + Compare content of b and the string literal for equality without + differentiating between character case. If the content differs other + than in case, 0 is returned, if, ignoring case, the content is the same, + 1 is returned, if there is an error, -1 is returned. If the length of + the strings are different, this function is O(1). '\0' characters are + not treated in any special way. + + .......................................................................... + + int bisstemeqStatic (bstring b, " ... "); + + Compare beginning of bstring b with a string literal for equality. If + the beginning of b differs from the memory block (or if b is too short), + 0 is returned, if the bstrings are the same, 1 is returned, if there is + an error, -1 is returned. The string literal parameter is enforced as + literal at compile time. + + .......................................................................... + + int bisstemeqcaselessStatic (bstring b, " ... "); + + Compare beginning of bstring b with a string literal without + differentiating between case for equality. If the beginning of b differs + from the memory block other than in case (or if b is too short), 0 is + returned, if the bstrings are the same, 1 is returned, if there is an + error, -1 is returned. The string literal parameter is enforced as + literal at compile time. + + .......................................................................... + + bstring bjoinStatic (const struct bstrList * bl, " ... "); + + Join the entries of a bstrList into one bstring by sequentially + concatenating them with the string literal in between. If there is an + error NULL is returned, otherwise a bstring with the correct result is + returned. See bstrListCreate() above for structure of struct bstrList. + + .......................................................................... + + void bvformata (int& ret, bstring b, const char * format, lastarg); + + Append the bstring b with printf like formatting with the format control + string, and the arguments taken from the ... list of arguments after + lastarg passed to the containing function. If the containing function + does not have ... parameters or lastarg is not the last named parameter + before the ... then the results are undefined. If successful, the + results are appended to b and BSTR_OK is assigned to ret. Otherwise + BSTR_ERR is assigned to ret. + + Example: + + void dbgerror (FILE * fp, const char * fmt, ...) { + int ret; + bstring b; + bvformata (ret, b = bfromcstr ("DBG: "), fmt, fmt); + if (BSTR_OK == ret) fputs ((char *) bdata (b), fp); + bdestroy (b); + } + + Note that if the BSTRLIB_NOVSNP macro was set when bstrlib had been + compiled the bvformata macro will not link properly. If the + BSTRLIB_NOVSNP macro has been set, the bvformata macro will not be + available. + + .......................................................................... + + void bwriteprotect (struct tagbstring& t); + + Disallow bstring from being written to via the bstrlib API. Attempts to + write to the resulting tagbstring from any bstrlib function will lead to + BSTR_ERR being returned. + + Note: bstrings which are write protected cannot be destroyed via bdestroy. + + Note to C++ users: Setting a CBString as write protected will not prevent + it from being destroyed by the destructor. + + .......................................................................... + + void bwriteallow (struct tagbstring& t); + + Allow bstring to be written to via the bstrlib API. Note that such an + action makes the bstring both writable and destroyable. If the bstring is + not legitimately writable (as is the case for struct tagbstrings + initialized with a bsStatic value), the results of this are undefined. + + Note that invoking the bwriteallow macro may increase the number of + reallocs by one more than necessary for every call to bwriteallow + interleaved with any bstring API which writes to this bstring. + + .......................................................................... + + int biswriteprotected (struct tagbstring& t); + + Returns 1 if the bstring is write protected, otherwise 0 is returned. + +=============================================================================== + +Unicode functions +----------------- + + The two modules utf8util.c and buniutil.c implement basic functions for + parsing and collecting Unicode data in the UTF8 format. Unicode is + described by a sequence of "code points" which are values between 0 and + 1114111 inclusive mapped to symbol content corresponding to nearly all + the standardized scripts of the world. + + The semantics of Unicode code points is varied and complicated. The + base support of the better string library does not attempt to perform + any interpretation of these code points. The better string library + solely provides support for iterating through unicode code points, + appending and extracting code points to and from bstrings, and parsing + UTF8 and UTF16 from raw data. + + The types cpUcs4 and cpUcs2 respectively are defined as 4 byte and 2 byte + encoding formats corresponding to UCS4 and UCS2 respectively. To test + if a raw code point is valid, the macro isLegalUnicodeCodePoint() has + been defined. The utf8 iterator is defined by struct utf8Iterator. To + test if the iterator has more code points to walk through the macro + utf8IteratorNoMore() has been defined. + + To use these functions compile and link utf8util.c and buniutil.c + + .......................................................................... + + extern void utf8IteratorInit (struct utf8Iterator* iter, + unsigned char* data, int slen); + + Initialize a unicode utf8 iterator to traverse an array of utf8 encoded + code points pointed to by data, with length slen from the start. The + iterator iter is only valid for as long as the array it is pointed to + is valid and not modified. + + .......................................................................... + + extern void utf8IteratorUninit (struct utf8Iterator* iter); + + Invalidate utf8 iterator. After calling this the iterator iter, should + yield false when passed to the utf8IteratorNoMore() macro. + + .......................................................................... + + extern cpUcs4 utf8IteratorGetNextCodePoint (struct utf8Iterator* iter, + cpUcs4 errCh); + + Parse code point the iterator is pointing at and advance the iterator to + the next code point. If the iterator was pointing at a valid code point + the code point is returned, otherwise, errCh will be returned. + + .......................................................................... + + extern cpUcs4 utf8IteratorGetCurrCodePoint (struct utf8Iterator* iter, + cpUcs4 errCh); + + Parse code point the iterator is pointing at. If the iterator was + pointing at a valid code point the code point is returned, otherwise, + errCh will be returned. + + .......................................................................... + + extern int utf8ScanBackwardsForCodePoint (unsigned char* msg, int len, + int pos, cpUcs4* out); + + From the position "pos" in the array msg of length len, search for the + last position before or at pos where from which a valid Unicode code + point can be parsed. If such an offset is found it is returned otherwise + a negative value is returned. The code point parsed is put into *out if + it is not NULL. + + .......................................................................... + + extern int buIsUTF8Content (const_bstring bu); + + Scan a bstring and determine if it is made entirely of unicode code + valid points. If it is, 1 is returned, otherwise 0 is returned. + + .......................................................................... + + extern int buAppendBlkUcs4 (bstring b, const cpUcs4* bu, int len, + cpUcs4 errCh); + + Append the code points passed in the UCS4 format (raw numbers) in the + array bu of length len. Any unparsable characters are replaced by errCh. + If errCh is not a valid Unicode code point, then parsing errors will cause + BSTR_ERR to be returned. + + .......................................................................... + + extern int buGetBlkUTF16 (cpUcs2* ucs2, int len, cpUcs4 errCh, + const_bstring bu, int pos); + + Convert a string of UTF8 codepoints (bu), skipping the first pos, into a + sequence of UTF16 encoded code points. Returns the number of UCS2 16-bit + words written to the output. No more than len words are written to the + target array ucs2. If any code point in bu is unparsable, it will be + translated to errCh. + + .......................................................................... + + extern int buAppendBlkUTF16 (bstring bu, const cpUcs2* utf16, int len, + cpUcs2* bom, cpUcs4 errCh); + + Append an array of UCS2 code points (utf16) to UTF8 codepoints (bu). Any + invalid code point is replaced by errCh. If errCh is itself not a + valid code point, then this translation will halt upon the first error + and return BSTR_ERR. Otherwise BSTR_OK is returned. If a byte order mark + has been previously read, it may be passed in as bom, otherwise if *bom is + set to 0, it will be filled in with the BOM as read from the first + character if it is a BOM. + +=============================================================================== + +The bstest module +----------------- + +The bstest module is just a unit test for the bstrlib module. For correct +implementations of bstrlib, it should execute with 0 failures being reported. +This test should be utilized if modifications/customizations to bstrlib have +been performed. It tests each core bstrlib function with bstrings of every +mode (read-only, NULL, static and mutable) and ensures that the expected +semantics are observed (including results that should indicate an error). It +also tests for aliasing support. Passing bstest is a necessary but not a +sufficient condition for ensuring the correctness of the bstrlib module. + + +The test module +--------------- + +The test module is just a unit test for the bstrwrap module. For correct +implementations of bstrwrap, it should execute with 0 failures being +reported. This test should be utilized if modifications/customizations to +bstrwrap have been performed. It tests each core bstrwrap function with +CBStrings write protected or not and ensures that the expected semantics are +observed (including expected exceptions.) Note that exceptions cannot be +disabled to run this test. Passing test is a necessary but not a sufficient +condition for ensuring the correctness of the bstrwrap module. + +=============================================================================== + +Using Bstring and CBString as an alternative to the C library +------------------------------------------------------------- + +First let us give a table of C library functions and the alternative bstring +functions and CBString methods that should be used instead of them. + +C-library Bstring alternative CBString alternative +--------- ------------------- -------------------- +gets bgets ::gets +strcpy bassign = operator +strncpy bassignmidstr ::midstr +strcat bconcat += operator +strncat bconcat + btrunc += operator + ::trunc +strtok bsplit, bsplits ::split +sprintf b(assign)format ::format +snprintf b(assign)format + btrunc ::format + ::trunc +vsprintf bvformata bvformata + +vsnprintf bvformata + btrunc bvformata + btrunc +vfprintf bvformata + fputs use bvformata + fputs +strcmp biseq, bstrcmp comparison operators. +strncmp bstrncmp, memcmp bstrncmp, memcmp +strlen ->slen, blength ::length +strdup bstrcpy constructor +strset bpattern ::fill +strstr binstr ::find +strpbrk binchr ::findchr +stricmp bstricmp cast & use bstricmp +strlwr btolower cast & use btolower +strupr btoupper cast & use btoupper +strrev bReverse (aux module) cast & use bReverse +strchr bstrchr cast & use bstrchr +strspnp use strspn use strspn +ungetc bsunread bsunread + +The top 9 C functions listed here are troublesome in that they impose memory +management in the calling function. The Bstring and CBstring interfaces have +built-in memory management, so there is far less code with far less potential +for buffer overrun problems. strtok can only be reliably called as a "leaf" +calculation, since it (quite bizarrely) maintains hidden internal state. And +gets is well known to be broken no matter what. The Bstrlib alternatives do +not suffer from those sorts of problems. + +The substitute for strncat can be performed with higher performance by using +the blk2tbstr macro to create a presized second operand for bconcat. + +C-library Bstring alternative CBString alternative +--------- ------------------- -------------------- +strspn strspn acceptable strspn acceptable +strcspn strcspn acceptable strcspn acceptable +strnset strnset acceptable strnset acceptable +printf printf acceptable printf acceptable +puts puts acceptable puts acceptable +fprintf fprintf acceptable fprintf acceptable +fputs fputs acceptable fputs acceptable +memcmp memcmp acceptable memcmp acceptable + +Remember that Bstring (and CBstring) functions will automatically append the +'\0' character to the character data buffer. So by simply accessing the data +buffer directly, ordinary C string library functions can be called directly +on them. Note that bstrcmp is not the same as memcmp in exactly the same way +that strcmp is not the same as memcmp. + +C-library Bstring alternative CBString alternative +--------- ------------------- -------------------- +fread balloc + fread ::alloc + fread +fgets balloc + fgets ::alloc + fgets + +These are odd ones because of the exact sizing of the buffer required. The +Bstring and CBString alternatives requires that the buffers are forced to +hold at least the prescribed length, then just use fread or fgets directly. +However, typically the automatic memory management of Bstring and CBstring +will make the typical use of fgets and fread to read specifically sized +strings unnecessary. + +Implementation Choices +---------------------- + +Overhead: +......... + +The bstring library has more overhead versus straight char buffers for most +functions. This overhead is essentially just the memory management and +string header allocation. This overhead usually only shows up for small +string manipulations. The performance loss has to be considered in +light of the following: + +1) What would be the performance loss of trying to write this management + code in one's own application? +2) Since the bstring library source code is given, a sufficiently powerful + modern inlining globally optimizing compiler can remove function call + overhead. + +Since the data type is exposed, a developer can replace any unsatisfactory +function with their own inline implementation. And that is besides the main +point of what the better string library is mainly meant to provide. Any +overhead lost has to be compared against the value of the safe abstraction +for coupling memory management and string functionality. + +Performance of the C interface: +............................... + +The algorithms used have performance advantages versus the analogous C +library functions. For example: + +1. bfromcstr/blk2str/bstrcpy versus strcpy/strdup. By using memmove instead + of strcpy, the break condition of the copy loop is based on an independent + counter (that should be allocated in a register) rather than having to + check the results of the load. Modern out-of-order executing CPUs can + parallelize the final branch mis-predict penality with the loading of the + source string. Some CPUs will also tend to have better built-in hardware + support for counted memory moves than load-compare-store. (This is a + minor, but non-zero gain.) +2. biseq versus strcmp. If the strings are unequal in length, bsiseq will + return in O(1) time. If the strings are aliased, or have aliased data + buffers, biseq will return in O(1) time. strcmp will always be O(k), + where k is the length of the common prefix or the whole string if they are + identical. +3. ->slen versus strlen. ->slen is obviously always O(1), while strlen is + always O(n) where n is the length of the string. +4. bconcat versus strcat. Both rely on precomputing the length of the + destination string argument, which will favor the bstring library. On + iterated concatenations the performance difference can be enormous. +5. bsreadln versus fgets. The bsreadln function reads large blocks at a time + from the given stream, then parses out lines from the buffers directly. + Some C libraries will implement fgets as a loop over single fgetc calls. + Testing indicates that the bsreadln approach can be several times faster + for fast stream devices (such as a file that has been entirely cached.) +6. bsplits/bsplitscb versus strspn. Accelerators for the set of match + characters are generated only once. +7. binstr versus strstr. The binstr implementation unrolls the loops to + help reduce loop overhead. This will matter if the target string is + long and source string is not found very early in the target string. + With strstr, while it is possible to unroll the source contents, it is + not possible to do so with the destination contents in a way that is + effective because every destination character must be tested against + '\0' before proceeding to the next character. +8. bReverse versus strrev. The C function must find the end of the string + first before swaping character pairs. +9. bstrrchr versus no comparable C function. Its not hard to write some C + code to search for a character from the end going backwards. But there + is no way to do this without computing the length of the string with + strlen. + +Practical testing indicates that in general Bstrlib is never signifcantly +slower than the C library for common operations, while very often having a +performance advantage that ranges from significant to massive. Even for +functions like b(n)inchr versus str(c)spn() (where, in theory, there is no +advantage for the Bstrlib architecture) the performance of Bstrlib is vastly +superior to most tested C library implementations. + +Some of Bstrlib's extra functionality also lead to inevitable performance +advantages over typical C solutions. For example, using the blk2tbstr macro, +one can (in O(1) time) generate an internal substring by reference while not +disturbing the original string. If disturbing the original string is not an +option, typically, a comparable char * solution would have to make a copy of +the substring to provide similar functionality. Another example is reverse +character set scanning -- the str(c)spn functions only scan in a forward +direction which can complicate some parsing algorithms. + +Where high performance char * based algorithms are available, Bstrlib can +still leverage them by accessing the ->data field on bstrings. So +realistically Bstrlib can never be significantly slower than any standard +'\0' terminated char * based solutions. + +Performance of the C++ interface: +................................. + +The C++ interface has been designed with an emphasis on abstraction and safety +first. However, since it is substantially a wrapper for the C bstring +functions, for longer strings the performance comments described in the +"Performance of the C interface" section above still apply. Note that the +(CBString *) type can be directly cast to a (bstring) type, and passed as +parameters to the C functions (though a CBString must never be passed to +bdestroy.) + +Probably the most controversial choice is performing full bounds checking on +the [] operator. This decision was made because 1) the fast alternative of +not bounds checking is still available by first casting the CBString to a +(const char *) buffer or to a (struct tagbstring) then derefencing .data and +2) because the lack of bounds checking is seen as one of the main weaknesses +of C/C++ versus other languages. This check being done on every access leads +to individual character extraction being actually slower than other languages +in this one respect (other language's compilers will normally dedicate more +resources on hoisting or removing bounds checking as necessary) but otherwise +bring C++ up to the level of other languages in terms of functionality. + +It is common for other C++ libraries to leverage the abstractions provided by +C++ to use reference counting and "copy on write" policies. While these +techniques can speed up some scenarios, they impose a problem with respect to +thread safety. bstrings and CBStrings can be properly protected with +"per-object" mutexes, meaning that two bstrlib calls can be made and execute +simultaneously, so long as the bstrings and CBstrings are distinct. With a +reference count and alias before copy on write policy, global mutexes are +required that prevent multiple calls to the strings library to execute +simultaneously regardless of whether or not the strings represent the same +string. + +One interesting trade off in CBString is that the default constructor is not +trivial. I.e., it always prepares a ready to use memory buffer. The purpose +is to ensure that there is a uniform internal composition for any functioning +CBString that is compatible with bstrings. It also means that the other +methods in the class are not forced to perform "late initialization" checks. +In the end it means that construction of CBStrings are slower than other +comparable C++ string classes. Initial testing, however, indicates that +CBString outperforms std::string and MFC's CString, for example, in all other +operations. So to work around this weakness it is recommended that CBString +declarations be pushed outside of inner loops. + +Practical testing indicates that with the exception of the caveats given +above (constructors and safe index character manipulations) the C++ API for +Bstrlib generally outperforms popular standard C++ string classes. Amongst +the standard libraries and compilers, the quality of concatenation operations +varies wildly and very little care has gone into search functions. Bstrlib +dominates those performance benchmarks. + +Memory management: +.................. + +The bstring functions which write and modify bstrings will automatically +reallocate the backing memory for the char buffer whenever it is required to +grow. The algorithm for resizing chosen is to snap up to sizes that are a +power of two which are sufficient to hold the intended new size. Memory +reallocation is not performed when the required size of the buffer is +decreased. This behavior can be relied on, and is necessary to make the +behaviour of balloc deterministic. This trades off additional memory usage +for decreasing the frequency for required reallocations: + +1. For any bstring whose size never exceeds n, its buffer is not ever + reallocated more than log_2(n) times for its lifetime. +2. For any bstring whose size never exceeds n, its buffer is never more than + 2*(n+1) in length. (The extra characters beyond 2*n are to allow for the + implicit '\0' which is always added by the bstring modifying functions.) + +Decreasing the buffer size when the string decreases in size would violate 1) +above and in real world case lead to pathological heap thrashing. Similarly, +allocating more tightly than "least power of 2 greater than necessary" would +lead to a violation of 1) and have the same potential for heap thrashing. + +Property 2) needs emphasizing. Although the memory allocated is always a +power of 2, for a bstring that grows linearly in size, its buffer memory also +grows linearly, not exponentially. The reason is that the amount of extra +space increases with each reallocation, which decreases the frequency of +future reallocations. + +Obviously, given that bstring writing functions may reallocate the data +buffer backing the target bstring, one should not attempt to cache the data +buffer address and use it after such bstring functions have been called. +This includes making reference struct tagbstrings which alias to a writable +bstring. + +balloc or bfromcstralloc can be used to preallocate the minimum amount of +space used for a given bstring. This will reduce even further the number of +times the data portion is reallocated. If the length of the string is never +more than one less than the memory length then there will be no further +reallocations. + +Note that invoking the bwriteallow macro may increase the number of reallocs +by one more than necessary for every call to bwriteallow interleaved with any +bstring API which writes to this bstring. + +The library does not use any mechanism for automatic clean up for the C API. +Thus explicit clean up via calls to bdestroy() are required to avoid memory +leaks. + +Constant and static tagbstrings: +................................ + +A struct tagbstring can be write protected from any bstrlib function using +the bwriteprotect macro. A write protected struct tagbstring can then be +reset to being writable via the bwriteallow macro. There is, of course, no +protection from attempts to directly access the bstring members. Modifying a +bstring which is write protected by direct access has undefined behavior. + +static struct tagbstrings can be declared via the bsStatic macro. They are +considered permanently unwritable. Such struct tagbstrings's are declared +such that attempts to write to it are not well defined. Invoking either +bwriteallow or bwriteprotect on static struct tagbstrings has no effect. + +struct tagbstring's initialized via btfromcstr or blk2tbstr are protected by +default but can be made writeable via the bwriteallow macro. If bwriteallow +is called on such struct tagbstring's, it is the programmer's responsibility +to ensure that: + +1) the buffer supplied was allocated from the heap. +2) bdestroy is not called on this tagbstring (unless the header itself has + also been allocated from the heap.) +3) free is called on the buffer to reclaim its memory. + +bwriteallow and bwriteprotect can be invoked on ordinary bstrings (they have +to be dereferenced with the (*) operator to get the levels of indirection +correct) to give them write protection. + +Buffer declaration: +................... + +The memory buffer is actually declared "unsigned char *" instead of "char *". +The reason for this is to trigger compiler warnings whenever uncasted char +buffers are assigned to the data portion of a bstring. This will draw more +diligent programmers into taking a second look at the code where they +have carelessly left off the typically required cast. (Research from +AT&T/Lucent indicates that additional programmer eyeballs is one of the most +effective mechanisms at ferreting out bugs.) + +Function pointers: +.................. + +The bgets, bread and bStream functions use function pointers to obtain +strings from data streams. The function pointer declarations have been +specifically chosen to be compatible with the fgetc and fread functions. +While this may seem to be a convoluted way of implementing fgets and fread +style functionality, it has been specifically designed this way to ensure +that there is no dependency on a single narrowly defined set of device +interfaces, such as just stream I/O. In the embedded world, its quite +possible to have environments where such interfaces may not exist in the +standard C library form. Furthermore, the generalization that this opens up +allows for more sophisticated uses for these functions (performing an fgets +like function on a socket, for example.) By using function pointers, it also +allows such abstract stream interfaces to be created using the bstring library +itself while not creating a circular dependency. + +Use of int's for sizes: +....................... + +This is just a recognition that 16bit platforms with requirements for strings +that are larger than 64K and 32bit+ platforms with requirements for strings +that are larger than 4GB are pretty marginal. The main focus is for 32bit +platforms, and emerging 64bit platforms with reasonable < 4GB string +requirements. Using ints allows for negative values which has meaning +internally to bstrlib. + +Semantic consideration: +....................... + +Certain care needs to be taken when copying and aliasing bstrings. A bstring +is essentially a pointer type which points to a multipart abstract data +structure. Thus usage, and lifetime of bstrings have semantics that follow +these considerations. For example: + + bstring a, b; + struct tagbstring t; + + a = bfromcstr("Hello"); /* Create new bstring and copy "Hello" into it. */ + b = a; /* Alias b to the contents of a. */ + t = *a; /* Create a current instance pseudo-alias of a. */ + bconcat (a, b); /* Double a and b, t is now undefined. */ + bdestroy (a); /* Destroy the contents of both a and b. */ + +Variables of type bstring are really just references that point to real +bstring objects. The equal operator (=) creates aliases, and the asterisk +dereference operator (*) creates a kind of alias to the current instance (which +is generally not useful for any purpose.) Using bstrcpy() is the correct way +of creating duplicate instances. The ampersand operator (&) is useful for +creating aliases to struct tagbstrings (remembering that constructed struct +tagbstrings are not writable by default.) + +CBStrings use complete copy semantics for the equal operator (=), and thus do +not have these sorts of issues. + +Debugging: +.......... + +Bstrings have a simple, exposed definition and construction, and the library +itself is open source. So most debugging is going to be fairly straight- +forward. But the memory for bstrings come from the heap, which can often be +corrupted indirectly, and it might not be obvious what has happened even from +direct examination of the contents in a debugger or a core dump. There are +some tools such as Purify, Insure++ and Electric Fence which can help solve +such problems, however another common approach is to directly instrument the +calls to malloc, realloc, calloc, free, memcpy, memmove and/or other calls +by overriding them with macro definitions. + +Although the user could hack on the Bstrlib sources directly as necessary to +perform such an instrumentation, Bstrlib comes with a built-in mechanism for +doing this. By defining the macro BSTRLIB_MEMORY_DEBUG and providing an +include file named memdbg.h this will force the core Bstrlib modules to +attempt to include this file. In such a file, macros could be defined which +overrides Bstrlib's useage of the C standard library. + +Rather than calling malloc, realloc, free, memcpy or memmove directly, Bstrlib +emits the macros bstr__alloc, bstr__realloc, bstr__free, bstr__memcpy and +bstr__memmove in their place respectively. By default these macros are simply +assigned to be equivalent to their corresponding C standard library function +call. However, if they are given earlier macro definitions (via the back +door include file) they will not be given their default definition. In this +way Bstrlib's interface to the standard library can be changed but without +having to directly redefine or link standard library symbols (both of which +are not strictly ANSI C compliant.) + +An example definition might include: + + #define bstr__alloc(sz) X_malloc ((sz), __LINE__, __FILE__) + +which might help contextualize heap entries in a debugging environment. + +The NULL parameter and sanity checking of bstrings is part of the Bstrlib +API, and thus Bstrlib itself does not present any different modes which would +correspond to "Debug" or "Release" modes. Bstrlib always contains mechanisms +which one might think of as debugging features, but retains the performance +and small memory footprint one would normally associate with release mode +code. + +Integration Microsoft's Visual Studio debugger: +............................................... + +Microsoft's Visual Studio debugger has a capability of customizable mouse +float over data type descriptions. This is accomplished by editting the +AUTOEXP.DAT file to include the following: + + ; new for CBString + tagbstring =slen= mlen= + Bstrlib::CBStringList =count= + +In Visual C++ 6.0 this file is located in the directory: + + C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin + +and in Visual Studio .NET 2003 its located here: + + C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Packages\Debugger + +This will improve the ability of debugging with Bstrlib under Visual Studio. + +Security +-------- + +Bstrlib does not come with explicit security features outside of its fairly +comprehensive error detection, coupled with its strict semantic support. +That is to say that certain common security problems, such as buffer overrun, +constant overwrite, arbitrary truncation etc, are far less likely to happen +inadvertently. Where it does help, Bstrlib maximizes its advantage by +providing developers a simple adoption path that lets them leave less secure +string mechanisms behind. The library will not leave developers wanting, so +they will be less likely to add new code using a less secure string library +to add functionality that might be missing from Bstrlib. + +That said there are a number of security ideas not addressed by Bstrlib: + +1. Race condition exploitation (i.e., verifying a string's contents, then +raising the privilege level and execute it as a shell command as two +non-atomic steps) is well beyond the scope of what Bstrlib can provide. It +should be noted that MFC's built-in string mutex actually does not solve this +problem either -- it just removes immediate data corruption as a possible +outcome of such exploit attempts (it can be argued that this is worse, since +it will leave no trace of the exploitation). In general race conditions have +to be dealt with by careful design and implementation; it cannot be assisted +by a string library. + +2. Any kind of access control or security attributes to prevent usage in +dangerous interfaces such as system(). Perl includes a "trust" attribute +which can be endowed upon strings that are intended to be passed to such +dangerous interfaces. However, Perl's solution reflects its own limitations +-- notably that it is not a strongly typed language. In the example code for +Bstrlib, there is a module called taint.cpp. It demonstrates how to write a +simple wrapper class for managing "untainted" or trusted strings using the +type system to prevent questionable mixing of ordinary untrusted strings with +untainted ones then passing them to dangerous interfaces. In this way the +security correctness of the code reduces to auditing the direct usages of +dangerous interfaces or promotions of tainted strings to untainted ones. + +3. Encryption of string contents is way beyond the scope of Bstrlib. +Maintaining encrypted string contents in the futile hopes of thwarting things +like using system-level debuggers to examine sensitive string data is likely +to be a wasted effort (imagine a debugger that runs at a higher level than a +virtual processor where the application runs). For more standard encryption +usages, since the bstring contents are simply binary blocks of data, this +should pose no problem for usage with other standard encryption libraries. + +Compatibility +------------- + +The Better String Library is known to compile and function correctly with the +following compilers: + + - Microsoft Visual C++ + - Watcom C/C++ + - Intel's C/C++ compiler (Windows) + - The GNU C/C++ compiler (cygwin and Linux on PPC64) + - Borland C + - Turbo C + +Setting of configuration options should be unnecessary for these compilers +(unless exceptions are being disabled or STLport has been added to WATCOM +C/C++). Bstrlib has been developed with an emphasis on portability. As such +porting it to other compilers should be straight forward. This package +includes a porting guide (called porting.txt) which explains what issues may +exist for porting Bstrlib to different compilers and environments. + +ANSI issues +----------- + +1. The function pointer types bNgetc and bNread have prototypes which are very +similar to, but not exactly the same as fgetc and fread respectively. +Basically the FILE * parameter is replaced by void *. The purpose of this +was to allow one to create other functions with fgetc and fread like +semantics without being tied to ANSI C's file streaming mechanism. I.e., one +could very easily adapt it to sockets, or simply reading a block of memory, +or procedurally generated strings (for fractal generation, for example.) + +The problem is that invoking the functions (bNgetc)fgetc and (bNread)fread is +not technically legal in ANSI C. The reason being that the compiler is only +able to coerce the function pointers themselves into the target type, however +are unable to perform any cast (implicit or otherwise) on the parameters +passed once invoked. I.e., if internally void * and FILE * need some kind of +mechanical coercion, the compiler will not properly perform this conversion +and thus lead to undefined behavior. + +Apparently a platform from Data General called "Eclipse" and another from +Tandem called "NonStop" have a different representation for pointers to bytes +and pointers to words, for example, where coercion via casting is necessary. +(Actual confirmation of the existence of such machines is hard to come by, so +it is prudent to be skeptical about this information.) However, this is not +an issue for any known contemporary platforms. One may conclude that such +platforms are effectively apocryphal even if they do exist. + +To correctly work around this problem to the satisfaction of the ANSI +limitations, one needs to create wrapper functions for fgets and/or +fread with the prototypes of bNgetc and/or bNread respectively which performs +no other action other than to explicitely cast the void * parameter to a +FILE *, and simply pass the remaining parameters straight to the function +pointer call. + +The wrappers themselves are trivial: + + size_t freadWrap (void * buff, size_t esz, size_t eqty, void * parm) { + return fread (buff, esz, eqty, (FILE *) parm); + } + + int fgetcWrap (void * parm) { + return fgetc ((FILE *) parm); + } + +These have not been supplied in bstrlib or bstraux to prevent unnecessary +linking with file I/O functions. + +2. vsnprintf is not available on all compilers. Because of this, the bformat +and bformata functions (and format and formata methods) are not guaranteed to +work properly. For those compilers that don't have vsnprintf, the +BSTRLIB_NOVSNP macro should be set before compiling bstrlib, and the format +functions/method will be disabled. + +The more recent ANSI C standards have specified the required inclusion of a +vsnprintf function. + +3. The bstrlib function names are not unique in the first 6 characters. This +is only an issue for older C compiler environments which do not store more +than 6 characters for function names. + +4. The bsafe module defines macros and function names which are part of the +C library. This simply overrides the definition as expected on all platforms +tested, however it is not sanctioned by the ANSI standard. This module is +clearly optional and should be omitted on platforms which disallow its +undefined semantics. + +In practice the real issue is that some compilers in some modes of operation +can/will inline these standard library functions on a module by module basis +as they appear in each. The linker will thus have no opportunity to override +the implementation of these functions for those cases. This can lead to +inconsistent behaviour of the bsafe module on different platforms and +compilers. + +=============================================================================== + +Comparison with Microsoft's CString class +----------------------------------------- + +Although developed independently, CBStrings have very similar functionality to +Microsoft's CString class. However, the bstring library has significant +advantages over CString: + +1. Bstrlib is a C-library as well as a C++ library (using the C++ wrapper). + + - Thus it is compatible with more programming environments and + available to a wider population of programmers. + +2. The internal structure of a bstring is considered exposed. + + - A single contiguous block of data can be cut into read-only pieces by + simply creating headers, without allocating additional memory to create + reference copies of each of these sub-strings. + - In this way, using bstrings in a totally abstracted way becomes a choice + rather than an imposition. Further this choice can be made differently + at different layers of applications that use it. + +3. Static declaration support precludes the need for constructor + invocation. + + - Allows for static declarations of constant strings that has no + additional constructor overhead. + +4. Bstrlib is not attached to another library. + + - Bstrlib is designed to be easily plugged into any other library + collection, without dependencies on other libraries or paradigms (such + as "MFC".) + +The bstring library also comes with a few additional functions that are not +available in the CString class: + + - bsetstr + - bsplit + - bread + - breplace (this is different from CString::Replace()) + - Writable indexed characters (for example a[i]='x') + +Interestingly, although Microsoft did implement mid$(), left$() and right$() +functional analogues (these are functions from GWBASIC) they seem to have +forgotten that mid$() could be also used to write into the middle of a string. +This functionality exists in Bstrlib with the bsetstr() and breplace() +functions. + +Among the disadvantages of Bstrlib is that there is no special support for +localization or wide characters. Such things are considered beyond the scope +of what bstrings are trying to deliver. CString essentially supports the +older UCS-2 version of Unicode via widechar_t as an application-wide compile +time switch. + +CString's also use built-in mechanisms for ensuring thread safety under all +situations. While this makes writing thread safe code that much easier, this +built-in safety feature has a price -- the inner loops of each CString method +runs in its own critical section (grabbing and releasing a light weight mutex +on every operation.) The usual way to decrease the impact of a critical +section performance penalty is to amortize more operations per critical +section. But since the implementation of CStrings is fixed as a one critical +section per-operation cost, there is no way to leverage this common +performance enhancing idea. + +The search facilities in Bstrlib are comparable to those in MFC's CString +class, though it is missing locale specific collation. But because Bstrlib +is interoperable with C's char buffers, it will allow programmers to write +their own string searching mechanism (such as Boyer-Moore), or be able to +choose from a variety of available existing string searching libraries (such +as those for regular expressions) without difficulty. + +Microsoft used a very non-ANSI conforming trick in its implementation to +allow printf() to use the "%s" specifier to output a CString correctly. This +can be convenient, but it is inherently not portable. CBString requires an +explicit cast, while bstring requires the data member to be dereferenced. +Microsoft's own documentation recommends casting, instead of relying on this +feature. + +Comparison with C++'s std::string +--------------------------------- + +This is the C++ language's standard STL based string class. + +1. There is no C implementation. +2. The [] operator is not bounds checked. +3. Missing a lot of useful functions like printf-like formatting. +4. Some sub-standard std::string implementations (SGI) are necessarily unsafe + to use with multithreading. +5. Limited by STL's std::iostream which in turn is limited by ifstream which + can only take input from files. (Compare to CBStream's API which can take + abstracted input.) +6. Extremely uneven performance across implementations. + +Comparison with ISO C TR 24731 proposal +--------------------------------------- + +Following the ISO C99 standard, Microsoft has proposed a group of C library +extensions which are supposedly "safer and more secure". This proposal is +expected to be adopted by the ISO C standard which follows C99. + +The proposal reveals itself to be very similar to Microsoft's "StrSafe" +library. The functions are basically the same as other standard C library +string functions except that destination parameters are paired with an +additional length parameter of type rsize_t. rsize_t is the same as size_t, +however, the range is checked to make sure its between 1 and RSIZE_MAX. Like +Bstrlib, the functions perform a "parameter check". Unlike Bstrlib, when a +parameter check fails, rather than simply outputing accumulatable error +statuses, they call a user settable global error function handler, and upon +return of control performs no (additional) detrimental action. The proposal +covers basic string functions as well as a few non-reenterable functions +(asctime, ctime, and strtok). + +1. Still based solely on char * buffers (and therefore strlen() and strcat() + is still O(n), and there are no faster streq() comparison functions.) +2. No growable string semantics. +3. Requires manual buffer length synchronization in the source code. +4. No attempt to enhance functionality of the C library. +5. Introduces a new error scenario (strings exceeding RSIZE_MAX length). + +The hope is that by exposing the buffer length requirements there will be +fewer buffer overrun errors. However, the error modes are really just +transformed, rather than removed. The real problem of buffer overflows is +that they all happen as a result of erroneous programming. So forcing +programmers to manually deal with buffer limits, will make them more aware of +the problem but doesn't remove the possibility of erroneous programming. So +a programmer that erroneously mixes up the rsize_t parameters is no better off +from a programmer that introduces potential buffer overflows through other +more typical lapses. So at best this may reduce the rate of erroneous +programming, rather than making any attempt at removing failure modes. + +The error handler can discriminate between types of failures, but does not +take into account any callsite context. So the problem is that the error is +going to be manifest in a piece of code, but there is no pointer to that +code. It would seem that passing in the call site __FILE__, __LINE__ as +parameters would be very useful, but the API clearly doesn't support such a +thing (it would increase code bloat even more than the extra length +parameter does, and would require macro tricks to implement). + +The Bstrlib C API takes the position that error handling needs to be done at +the callsite, and just tries to make it as painless as possible. Furthermore, +error modes are removed by supporting auto-growing strings and aliasing. For +capturing errors in more central code fragments, Bstrlib's C++ API uses +exception handling extensively, which is superior to the leaf-only error +handler approach. + +Comparison with Managed String Library CERT proposal +---------------------------------------------------- + +The main webpage for the managed string library: +http://www.cert.org/secure-coding/managedstring.html + +Robert Seacord at CERT has proposed a C string library that he calls the +"Managed String Library" for C. Like Bstrlib, it introduces a new type +which is called a managed string. The structure of a managed string +(string_m) is like a struct tagbstring but missing the length field. This +internal structure is considered opaque. The length is, like the C standard +library, always computed on the fly by searching for a terminating NUL on +every operation that requires it. So it suffers from every performance +problem that the C standard library suffers from. Interoperating with C +string APIs (like printf, fopen, or anything else that takes a string +parameter) requires copying to additionally allocating buffers that have to +be manually freed -- this makes this library probably slower and more +cumbersome than any other string library in existence. + +The library gives a fully populated error status as the return value of every +string function. The hope is to be able to diagnose all problems +specifically from the return code alone. Comparing this to Bstrlib, which +aways returns one consistent error message, might make it seem that Bstrlib +would be harder to debug; but this is not true. With Bstrlib, if an error +occurs there is always enough information from just knowing there was an error +and examining the parameters to deduce exactly what kind of error has +happened. The managed string library thus gives up nested function calls +while achieving little benefit, while Bstrlib does not. + +One interesting feature that "managed strings" has is the idea of data +sanitization via character set whitelisting. That is to say, a globally +definable filter that makes any attempt to put invalid characters into strings +lead to an error and not modify the string. The author gives the following +example: + + // create valid char set + if (retValue = strcreate_m(&str1, "abc") ) { + fprintf( + stderr, + "Error %d from strcreate_m.\n", + retValue + ); + } + if (retValue = setcharset(str1)) { + fprintf( + stderr, + "Error %d from setcharset().\n", + retValue + ); + } + if (retValue = strcreate_m(&str1, "aabbccabc")) { + fprintf( + stderr, + "Error %d from strcreate_m.\n", + retValue + ); + } + // create string with invalid char set + if (retValue = strcreate_m(&str1, "abbccdabc")) { + fprintf( + stderr, + "Error %d from strcreate_m.\n", + retValue + ); + } + +Which we can compare with a more Bstrlib way of doing things: + + bstring bCreateWithFilter (const char * cstr, const_bstring filter) { + bstring b = bfromcstr (cstr); + if (BSTR_ERR != bninchr (b, filter) && NULL != b) { + fprintf (stderr, "Filter violation.\n"); + bdestroy (b); + b = NULL; + } + return b; + } + + struct tagbstring charFilter = bsStatic ("abc"); + bstring str1 = bCreateWithFilter ("aabbccabc", &charFilter); + bstring str2 = bCreateWithFilter ("aabbccdabc", &charFilter); + +The first thing we should notice is that with the Bstrlib approach you can +have different filters for different strings if necessary. Furthermore, +selecting a charset filter in the Managed String Library is uni-contextual. +That is to say, there can only be one such filter active for the entire +program, which means its usage is not well defined for intermediate library +usage (a library that uses it will interfere with user code that uses it, and +vice versa.) It is also likely to be poorly defined in multi-threading +environments. + +There is also a question as to whether the data sanitization filter is checked +on every operation, or just on creation operations. Since the charset can be +set arbitrarily at run time, it might be set *after* some managed strings have +been created. This would seem to imply that all functions should run this +additional check every time if there is an attempt to enforce this. This +would make things tremendously slow. On the other hand, if it is assumed that +only creates and other operations that take char *'s as input need be checked +because the charset was only supposed to be called once at and before any +other managed string was created, then one can see that its easy to cover +Bstrlib with equivalent functionality via a few wrapper calls such as the +example given above. + +And finally we have to question the value of sanitation in the first place. +For example, for httpd servers, there is generally a requirement that the +URLs parsed have some form that avoids undesirable translation to local file +system filenames or resources. The problem is that the way URLs can be +encoded, it must be completely parsed and translated to know if it is using +certain invalid character combinations. That is to say, merely filtering +each character one at a time is not necessarily the right way to ensure that +a string has safe contents. + +In the article that describes this proposal, it is claimed that it fairly +closely approximates the existing C API semantics. On this point we should +compare this "closeness" with Bstrlib: + + Bstrlib Managed String Library + ------- ---------------------- + +Pointer arithmetic Segment arithmetic N/A + +Use in C Std lib ->data, or bdata{e} getstr_m(x,*) ... free(x) + +String literals bsStatic, bsStaticBlk strcreate_m() + +Transparency Complete None + +Its pretty clear that the semantic mapping from C strings to Bstrlib is fairly +straightforward, and that in general semantic capabilities are the same or +superior in Bstrlib. On the other hand the Managed String Library is either +missing semantics or changes things fairly significantly. + +Comparison with Annexia's c2lib library +--------------------------------------- + +This library is available at: +http://www.annexia.org/freeware/c2lib + +1. Still based solely on char * buffers (and therefore strlen() and strcat() + is still O(n), and there are no faster streq() comparison functions.) + Their suggestion that alternatives which wrap the string data type (such as + bstring does) imposes a difficulty in interoperating with the C langauge's + ordinary C string library is not founded. +2. Introduction of memory (and vector?) abstractions imposes a learning + curve, and some kind of memory usage policy that is outside of the strings + themselves (and therefore must be maintained by the developer.) +3. The API is massive, and filled with all sorts of trivial (pjoin) and + controvertial (pmatch -- regular expression are not sufficiently + standardized, and there is a very large difference in performance between + compiled and non-compiled, REs) functions. Bstrlib takes a decidely + minimal approach -- none of the functionality in c2lib is difficult or + challenging to implement on top of Bstrlib (except the regex stuff, which + is going to be difficult, and controvertial no matter what.) +4. Understanding why c2lib is the way it is pretty much requires a working + knowledge of Perl. bstrlib requires only knowledge of the C string library + while providing just a very select few worthwhile extras. +5. It is attached to a lot of cruft like a matrix math library (that doesn't + include any functions for getting the determinant, eigenvectors, + eigenvalues, the matrix inverse, test for singularity, test for + orthogonality, a grahm schmit orthogonlization, LU decomposition ... I + mean why bother?) + +Convincing a development house to use c2lib is likely quite difficult. It +introduces too much, while not being part of any kind of standards body. The +code must therefore be trusted, or maintained by those that use it. While +bstring offers nothing more on this front, since its so much smaller, covers +far less in terms of scope, and will typically improve string performance, +the barrier to usage should be much smaller. + +Comparison with stralloc/qmail +------------------------------ + +More information about this library can be found here: +http://www.canonical.org/~kragen/stralloc.html or here: +http://cr.yp.to/lib/stralloc.html + +1. Library is very very minimal. A little too minimal. +2. Untargetted source parameters are not declared const. +3. Slightly different expected emphasis (like _cats function which takes an + ordinary C string char buffer as a parameter.) Its clear that the + remainder of the C string library is still required to perform more + useful string operations. + +The struct declaration for their string header is essentially the same as that +for bstring. But its clear that this was a quickly written hack whose goals +are clearly a subset of what Bstrlib supplies. For anyone who is served by +stralloc, Bstrlib is complete substitute that just adds more functionality. + +stralloc actually uses the interesting policy that a NULL data pointer +indicates an empty string. In this way, non-static empty strings can be +declared without construction. This advantage is minimal, since static empty +bstrings can be declared inline without construction, and if the string needs +to be written to it should be constructed from an empty string (or its first +initializer) in any event. + +wxString class +-------------- + +This is the string class used in the wxWindows project. A description of +wxString can be found here: +http://www.wxwindows.org/manuals/2.4.2/wx368.htm#wxstring + +This C++ library is similar to CBString. However, it is littered with +trivial functions (IsAscii, UpperCase, RemoveLast etc.) + +1. There is no C implementation. +2. The memory management strategy is to allocate a bounded fixed amount of + additional space on each resize, meaning that it does not have the + log_2(n) property that Bstrlib has (it will thrash very easily, cause + massive fragmentation in common heap implementations, and can easily be a + common source of performance problems). +3. The library uses a "copy on write" strategy, meaning that it has to deal + with multithreading problems. + +Vstr +---- + +This is a highly orthogonal C string library with an emphasis on +networking/realtime programming. It can be found here: +http://www.and.org/vstr/ + +1. The convoluted internal structure does not contain a '\0' char * compatible + buffer, so interoperability with the C library a non-starter. +2. The API and implementation is very large (owing to its orthogonality) and + can lead to difficulty in understanding its exact functionality. +3. An obvious dependency on gnu tools (confusing make configure step) +4. Uses a reference counting system, meaning that it is not likely to be + thread safe. + +The implementation has an extreme emphasis on performance for nontrivial +actions (adds, inserts and deletes are all constant or roughly O(#operations) +time) following the "zero copy" principle. This trades off performance of +trivial functions (character access, char buffer access/coersion, alias +detection) which becomes significantly slower, as well as incremental +accumulative costs for its searching/parsing functions. Whether or not Vstr +wins any particular performance benchmark will depend a lot on the benchmark, +but it should handily win on some, while losing dreadfully on others. + +The learning curve for Vstr is very steep, and it doesn't come with any +obvious way to build for Windows or other platforms without gnu tools. At +least one mechanism (the iterator) introduces a new undefined scenario +(writing to a Vstr while iterating through it.) Vstr has a very large +footprint, and is very ambitious in its total functionality. Vstr has no C++ +API. + +Vstr usage requires context initialization via vstr_init() which must be run +in a thread-local context. Given the totally reference based architecture +this means that sharing Vstrings across threads is not well defined, or at +least not safe from race conditions. This API is clearly geared to the older +standard of fork() style multitasking in UNIX, and is not safely transportable +to modern shared memory multithreading available in Linux and Windows. There +is no portable external solution making the library thread safe (since it +requires a mutex around each Vstr context -- not each string.) + +In the documentation for this library, a big deal is made of its self hosted +s(n)printf-like function. This is an issue for older compilers that don't +include vsnprintf(), but also an issue because Vstr has a slow conversion to +'\0' terminated char * mechanism. That is to say, using "%s" to format data +that originates from Vstr would be slow without some sort of native function +to do so. Bstrlib sidesteps the issue by relying on what snprintf-like +functionality does exist and having a high performance conversion to a char * +compatible string so that "%s" can be used directly. + +Str Library +----------- + +This is a fairly extensive string library, that includes full unicode support +and targetted at the goal of out performing MFC and STL. The architecture, +similarly to MFC's CStrings, is a copy on write reference counting mechanism. + +http://www.utilitycode.com/str/default.aspx + +1. Commercial. +2. C++ only. + +This library, like Vstr, uses a ref counting system. There is only so deeply +I can analyze it, since I don't have a license for it. However, performance +improvements over MFC's and STL, doesn't seem like a sufficient reason to +move your source base to it. For example, in the future, Microsoft may +improve the performance CString. + +It should be pointed out that performance testing of Bstrlib has indicated +that its relative performance advantage versus MFC's CString and STL's +std::string is at least as high as that for the Str library. + +libmib astrings +--------------- + +A handful of functional extensions to the C library that add dynamic string +functionality. +http://www.mibsoftware.com/libmib/astring/ + +This package basically references strings through char ** pointers and assumes +they are pointing to the top of an allocated heap entry (or NULL, in which +case memory will be newly allocated from the heap.) So its still up to user +to mix and match the older C string functions with these functions whenever +pointer arithmetic is used (i.e., there is no leveraging of the type system +to assert semantic differences between references and base strings as Bstrlib +does since no new types are introduced.) Unlike Bstrlib, exact string length +meta data is not stored, thus requiring a strlen() call on *every* string +writing operation. The library is very small, covering only a handful of C's +functions. + +While this is better than nothing, it is clearly slower than even the +standard C library, less safe and less functional than Bstrlib. + +To explain the advantage of using libmib, their website shows an example of +how dangerous C code: + + char buf[256]; + char *pszExtraPath = ";/usr/local/bin"; + + strcpy(buf,getenv("PATH")); /* oops! could overrun! */ + strcat(buf,pszExtraPath); /* Could overrun as well! */ + + printf("Checking...%s\n",buf); /* Some printfs overrun too! */ + +is avoided using libmib: + + char *pasz = 0; /* Must initialize to 0 */ + char *paszOut = 0; + char *pszExtraPath = ";/usr/local/bin"; + + if (!astrcpy(&pasz,getenv("PATH"))) /* malloc error */ exit(-1); + if (!astrcat(&pasz,pszExtraPath)) /* malloc error */ exit(-1); + + /* Finally, a "limitless" printf! we can use */ + asprintf(&paszOut,"Checking...%s\n",pasz);fputs(paszOut,stdout); + + astrfree(&pasz); /* Can use free(pasz) also. */ + astrfree(&paszOut); + +However, compare this to Bstrlib: + + bstring b, out; + + bcatcstr (b = bfromcstr (getenv ("PATH")), ";/usr/local/bin"); + out = bformat ("Checking...%s\n", bdatae (b, "")); + /* if (out && b) */ fputs (bdatae (out, ""), stdout); + bdestroy (b); + bdestroy (out); + +Besides being shorter, we can see that error handling can be deferred right +to the very end. Also, unlike the above two versions, if getenv() returns +with NULL, the Bstrlib version will not exhibit undefined behavior. +Initialization starts with the relevant content rather than an extra +autoinitialization step. + +libclc +------ + +An attempt to add to the standard C library with a number of common useful +functions, including additional string functions. +http://libclc.sourceforge.net/ + +1. Uses standard char * buffer, and adopts C 99's usage of "restrict" to pass + the responsibility to guard against aliasing to the programmer. +2. Adds no safety or memory management whatsoever. +3. Most of the supplied string functions are completely trivial. + +The goals of libclc and Bstrlib are clearly quite different. + +fireString +---------- + +http://firestuff.org/ + +1. Uses standard char * buffer, and adopts C 99's usage of "restrict" to pass + the responsibility to guard against aliasing to the programmer. +2. Mixes char * and length wrapped buffers (estr) functions, doubling the API + size, with safety limited to only half of the functions. + +Firestring was originally just a wrapper of char * functionality with extra +length parameters. However, it has been augmented with the inclusion of the +estr type which has similar functionality to stralloc. But firestring does +not nearly cover the functional scope of Bstrlib. + +Safe C String Library +--------------------- + +A library written for the purpose of increasing safety and power to C's string +handling capabilities. +http://www.zork.org/safestr/safestr.html + +1. While the safestr_* functions are safe in of themselves, interoperating + with char * string has dangerous unsafe modes of operation. +2. The architecture of safestr's causes the base pointer to change. Thus, + its not practical/safe to store a safestr in multiple locations if any + single instance can be manipulated. +3. Dependent on an additional error handling library. +4. Uses reference counting, meaning that it is either not thread safe or + slow and not portable. + +I think the idea of reallocating (and hence potentially changing) the base +pointer is a serious design flaw that is fatal to this architecture. True +safety is obtained by having automatic handling of all common scenarios +without creating implicit constraints on the user. + +Because of its automatic temporary clean up system, it cannot use "const" +semantics on input arguments. Interesting anomolies such as: + + safestr_t s, t; + s = safestr_replace (t = SAFESTR_TEMP ("This is a test"), + SAFESTR_TEMP (" "), SAFESTR_TEMP (".")); + /* t is now undefined. */ + +are possible. If one defines a function which takes a safestr_t as a +parameter, then the function would not know whether or not the safestr_t is +defined after it passes it to a safestr library function. The author +recommended method for working around this problem is to examine the +attributes of the safestr_t within the function which is to modify any of +its parameters and play games with its reference count. I think, therefore, +that the whole SAFESTR_TEMP idea is also fatally broken. + +The library implements immutability, optional non-resizability, and a "trust" +flag. This trust flag is interesting, and suggests that applying any +arbitrary sequence of safestr_* function calls on any set of trusted strings +will result in a trusted string. It seems to me, however, that if one wanted +to implement a trusted string semantic, one might do so by actually creating +a different *type* and only implement the subset of string functions that are +deemed safe (i.e., user input would be excluded, for example.) This, in +essence, would allow the compiler to enforce trust propogation at compile +time rather than run time. Non-resizability is also interesting, however, +it seems marginal (i.e., to want a string that cannot be resized, yet can be +modified and yet where a fixed sized buffer is undesirable.) + +Libsrt +------ + +This is a length based string library based on a slightly different strategy. +The string contents are appended to the end of the header directly so strings +only require a single allocation. However, whenever a reallocation occurs, +the header is replicated and the base pointer for the string is changed. +That means references to the string are only valid so long as they are not +resized after any such reference is cached. The internal structure maintains +a lot some state used to accelerate unicode manipulation. This makes +sustainable usage of the library essentially opaque. This also creates a +bottleneck for whatever extensions to the library one desires (write all +extensions on top of the base library, put in a request to the author, or +dedicate an expert to learn the internals of the library). The library is +committed to Unicode representation of its string data, and therefore cannot +be used as a generic buffer library. + +=============================================================================== + +Examples +-------- + + Dumping a line numbered file: + + FILE * fp; + int i, ret; + struct bstrList * lines; + struct tagbstring prefix = bsStatic ("-> "); + + if (NULL != (fp = fopen ("bstrlib.txt", "rb"))) { + bstring b = bread ((bNread) fread, fp); + fclose (fp); + if (NULL != (lines = bsplit (b, '\n'))) { + for (i=0; i < lines->qty; i++) { + binsert (lines->entry[i], 0, &prefix, '?'); + printf ("%04d: %s\n", i, bdatae (lines->entry[i], "NULL")); + } + bstrListDestroy (lines); + } + bdestroy (b); + } + +For numerous other examples, see bstraux.c, bstraux.h and the example archive. + +=============================================================================== + +License +------- + +The Better String Library is available under either the BSD license (see the +accompanying license.txt) or the Gnu Public License version 2 (see the +accompanying gpl.txt) at the option of the user. + +=============================================================================== + +Acknowledgements +---------------- + +The following individuals have made significant contributions to the design +and testing of the Better String Library: + +Bjorn Augestad +Clint Olsen +Darryl Bleau +Fabian Cenedese +Graham Wideman +Ignacio Burgueno +International Business Machines Corporation +Ira Mica +John Kortink +Manuel Woelker +Marcel van Kervinck +Michael Hsieh +Richard A. Smith +Simon Ekstrom +Wayne Scott +Zed A. Shaw + +=============================================================================== diff --git a/bstrlib/bstrwrap.cpp b/bstrlib/bstrwrap.cpp new file mode 100644 index 0000000..ce77f17 --- /dev/null +++ b/bstrlib/bstrwrap.cpp @@ -0,0 +1,1721 @@ +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2015, and is covered by the BSD open source + * license and the GPL. Refer to the accompanying documentation for details + * on usage and license. + */ + +/* + * bstrwrap.c + * + * This file is the C++ wrapper for the bstring functions. + */ + +#if defined (_MSC_VER) +# define _CRT_SECURE_NO_WARNINGS +#endif + +#include +#include +#include +#include +#include +#include "bstrwrap.h" + +#if defined(MEMORY_DEBUG) || defined(BSTRLIB_MEMORY_DEBUG) +#include "memdbg.h" +#endif + +#ifndef bstr__alloc +#define bstr__alloc(x) malloc (x) +#endif + +#ifndef bstr__free +#define bstr__free(p) free (p) +#endif + +#ifndef bstr__realloc +#define bstr__realloc(p,x) realloc ((p), (x)) +#endif + +#ifndef bstr__memcpy +#define bstr__memcpy(d,s,l) memcpy ((d), (s), (l)) +#endif + +#ifndef bstr__memmove +#define bstr__memmove(d,s,l) memmove ((d), (s), (l)) +#endif + +#ifndef bstr__memset +#define bstr__memset(d,c,l) memset ((d), (c), (l)) +#endif + +#ifndef bstr__memcmp +#define bstr__memcmp(d,c,l) memcmp ((d), (c), (l)) +#endif + +#ifndef bstr__memchr +#define bstr__memchr(s,c,l) memchr ((s), (c), (l)) +#endif + +#if defined(BSTRLIB_CAN_USE_IOSTREAM) +#include +#endif + +namespace Bstrlib { + +// Constructors. + +CBString::CBString () { + slen = 0; + mlen = 8; + data = (unsigned char *) bstr__alloc (mlen); + if (!data) { + mlen = 0; + bstringThrow ("Failure in default constructor"); + } else { + data[0] = '\0'; + } +} + +CBString::CBString (const void * blk, int len) { + data = NULL; + if (len >= 0) { + mlen = len + 1; + slen = len; + data = (unsigned char *) bstr__alloc (mlen); + } + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in block constructor"); + } else { + if (slen > 0) bstr__memcpy (data, blk, slen); + data[slen] = '\0'; + } +} + +CBString::CBString (char c, int len) { + data = NULL; + if (len >= 0) { + mlen = len + 1; + slen = len; + data = (unsigned char *) bstr__alloc (mlen); + } + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in repeat(char) constructor"); + } else { + if (slen > 0) bstr__memset (data, c, slen); + data[slen] = '\0'; + } +} + +CBString::CBString (char c) { + mlen = 2; + slen = 1; + if (NULL == (data = (unsigned char *) bstr__alloc (mlen))) { + mlen = slen = 0; + bstringThrow ("Failure in (char) constructor"); + } else { + data[0] = (unsigned char) c; + data[1] = '\0'; + } +} + +CBString::CBString (unsigned char c) { + mlen = 2; + slen = 1; + if (NULL == (data = (unsigned char *) bstr__alloc (mlen))) { + mlen = slen = 0; + bstringThrow ("Failure in (char) constructor"); + } else { + data[0] = c; + data[1] = '\0'; + } +} + +CBString::CBString (const char *s) { + if (s) { + size_t sslen = strlen (s); + if (sslen >= INT_MAX) bstringThrow ("Failure in (char *) constructor, string too large") + slen = (int) sslen; + mlen = slen + 1; + if (NULL != (data = (unsigned char *) bstr__alloc (mlen))) { + bstr__memcpy (data, s, mlen); + return; + } + } + data = NULL; + bstringThrow ("Failure in (char *) constructor"); +} + +CBString::CBString (int len, const char *s) { + if (s) { + size_t sslen = strlen (s); + if (sslen >= INT_MAX) bstringThrow ("Failure in (char *) constructor, string too large") + slen = (int) sslen; + mlen = slen + 1; + if (mlen < len) mlen = len; + if (NULL != (data = (unsigned char *) bstr__alloc (mlen))) { + bstr__memcpy (data, s, slen + 1); + return; + } + } + data = NULL; + bstringThrow ("Failure in (int len, char *) constructor"); +} + +CBString::CBString (const CBString& b) { + slen = b.slen; + mlen = slen + 1; + data = NULL; + if (mlen > 0) data = (unsigned char *) bstr__alloc (mlen); + if (!data) { + bstringThrow ("Failure in (CBString) constructor"); + } else { + bstr__memcpy (data, b.data, slen); + data[slen] = '\0'; + } +} + +CBString::CBString (const tagbstring& x) { + slen = x.slen; + mlen = slen + 1; + data = NULL; + if (slen >= 0 && x.data != NULL) data = (unsigned char *) bstr__alloc (mlen); + if (!data) { + bstringThrow ("Failure in (tagbstring) constructor"); + } else { + bstr__memcpy (data, x.data, slen); + data[slen] = '\0'; + } +} + +// Destructor. + +CBString::~CBString () { + if (data != NULL) { + bstr__free (data); + data = NULL; + } + mlen = 0; + slen = -__LINE__; +} + +// = operator. + +const CBString& CBString::operator = (char c) { + if (mlen <= 0) bstringThrow ("Write protection error"); + if (2 >= mlen) alloc (2); + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in =(char) operator"); + } else { + slen = 1; + data[0] = (unsigned char) c; + data[1] = '\0'; + } + return *this; +} + +const CBString& CBString::operator = (unsigned char c) { + if (mlen <= 0) bstringThrow ("Write protection error"); + if (2 >= mlen) alloc (2); + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in =(char) operator"); + } else { + slen = 1; + data[0] = c; + data[1] = '\0'; + } + return *this; +} + +const CBString& CBString::operator = (const char *s) { +size_t tmpSlen; + + if (mlen <= 0) bstringThrow ("Write protection error"); + if (NULL == s) s = ""; + if ((tmpSlen = strlen (s)) >= (size_t) mlen) { + if (tmpSlen >= INT_MAX-1) bstringThrow ("Failure in =(const char *) operator, string too large"); + alloc ((int) tmpSlen); + } + + if (data) { + slen = (int) tmpSlen; + bstr__memcpy (data, s, tmpSlen + 1); + } else { + mlen = slen = 0; + bstringThrow ("Failure in =(const char *) operator"); + } + return *this; +} + +const CBString& CBString::operator = (const CBString& b) { + if (mlen <= 0) bstringThrow ("Write protection error"); + if (b.slen >= mlen) alloc (b.slen); + + slen = b.slen; + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in =(CBString) operator"); + } else { + bstr__memcpy (data, b.data, slen); + data[slen] = '\0'; + } + return *this; +} + +const CBString& CBString::operator = (const tagbstring& x) { + if (mlen <= 0) bstringThrow ("Write protection error"); + if (x.slen < 0) bstringThrow ("Failure in =(tagbstring) operator, badly formed tagbstring"); + if (x.slen >= mlen) alloc (x.slen); + + slen = x.slen; + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in =(tagbstring) operator"); + } else { + bstr__memcpy (data, x.data, slen); + data[slen] = '\0'; + } + return *this; +} + +const CBString& CBString::operator += (const CBString& b) { + if (BSTR_ERR == bconcat (this, (bstring) &b)) { + bstringThrow ("Failure in concatenate"); + } + return *this; +} + +const CBString& CBString::operator += (const char *s) { + char * d; + int i, l; + + if (mlen <= 0) bstringThrow ("Write protection error"); + + /* Optimistically concatenate directly */ + l = mlen - slen; + d = (char *) &data[slen]; + for (i=0; i < l; i++) { + if ((*d++ = *s++) == '\0') { + slen += i; + return *this; + } + } + slen += i; + + if (BSTR_ERR == bcatcstr (this, s)) { + bstringThrow ("Failure in concatenate"); + } + return *this; +} + +const CBString& CBString::operator += (char c) { + if (BSTR_ERR == bconchar (this, c)) { + bstringThrow ("Failure in concatenate"); + } + return *this; +} + +const CBString& CBString::operator += (unsigned char c) { + if (BSTR_ERR == bconchar (this, (char) c)) { + bstringThrow ("Failure in concatenate"); + } + return *this; +} + +const CBString& CBString::operator += (const tagbstring& x) { + if (mlen <= 0) bstringThrow ("Write protection error"); + if (x.slen < 0) bstringThrow ("Failure in +=(tagbstring) operator, badly formed tagbstring"); + alloc (x.slen + slen + 1); + + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in +=(tagbstring) operator"); + } else { + bstr__memcpy (data + slen, x.data, x.slen); + slen += x.slen; + data[slen] = '\0'; + } + return *this; +} + +const CBString CBString::operator + (char c) const { + CBString retval (*this); + retval += c; + return retval; +} + +const CBString CBString::operator + (unsigned char c) const { + CBString retval (*this); + retval += c; + return retval; +} + +const CBString CBString::operator + (const CBString& b) const { + CBString retval (*this); + retval += b; + return retval; +} + +const CBString CBString::operator + (const char *s) const { + if (s == NULL) bstringThrow ("Failure in + (char *) operator, NULL"); + CBString retval (*this); + retval += s; + return retval; +} + +const CBString CBString::operator + (const unsigned char *s) const { + if (s == NULL) bstringThrow ("Failure in + (unsigned char *) operator, NULL"); + CBString retval (*this); + retval += (const char *) s; + return retval; +} + +const CBString CBString::operator + (const tagbstring& x) const { + if (x.slen < 0) bstringThrow ("Failure in + (tagbstring) operator, badly formed tagbstring"); + CBString retval (*this); + retval += x; + return retval; +} + +bool CBString::operator == (const CBString& b) const { + int retval; + if (BSTR_ERR == (retval = biseq ((bstring)this, (bstring)&b))) { + bstringThrow ("Failure in compare (==)"); + } + return retval > 0; +} + +bool CBString::operator == (const char * s) const { + int retval; + if (NULL == s) { + bstringThrow ("Failure in compare (== NULL)"); + } + if (BSTR_ERR == (retval = biseqcstr ((bstring) this, s))) { + bstringThrow ("Failure in compare (==)"); + } + return retval > 0; +} + +bool CBString::operator == (const unsigned char * s) const { + int retval; + if (NULL == s) { + bstringThrow ("Failure in compare (== NULL)"); + } + if (BSTR_ERR == (retval = biseqcstr ((bstring) this, (const char *) s))) { + bstringThrow ("Failure in compare (==)"); + } + return retval > 0; +} + +bool CBString::operator != (const CBString& b) const { + return ! ((*this) == b); +} + +bool CBString::operator != (const char * s) const { + return ! ((*this) == s); +} + +bool CBString::operator != (const unsigned char * s) const { + return ! ((*this) == s); +} + +bool CBString::operator < (const CBString& b) const { + int retval; + if (SHRT_MIN == (retval = bstrcmp ((bstring) this, (bstring)&b))) { + bstringThrow ("Failure in compare (<)"); + } + return retval < 0; +} + +bool CBString::operator < (const char * s) const { + if (s == NULL) { + bstringThrow ("Failure in compare (<)"); + } + return strcmp ((const char *)this->data, s) < 0; +} + +bool CBString::operator < (const unsigned char * s) const { + if (s == NULL) { + bstringThrow ("Failure in compare (<)"); + } + return strcmp ((const char *)this->data, (const char *)s) < 0; +} + +bool CBString::operator <= (const CBString& b) const { + int retval; + if (SHRT_MIN == (retval = bstrcmp ((bstring) this, (bstring)&b))) { + bstringThrow ("Failure in compare (<=)"); + } + return retval <= 0; +} + +bool CBString::operator <= (const char * s) const { + if (s == NULL) { + bstringThrow ("Failure in compare (<=)"); + } + return strcmp ((const char *)this->data, s) <= 0; +} + +bool CBString::operator <= (const unsigned char * s) const { + if (s == NULL) { + bstringThrow ("Failure in compare (<=)"); + } + return strcmp ((const char *)this->data, (const char *)s) <= 0; +} + +bool CBString::operator > (const CBString& b) const { + return ! ((*this) <= b); +} + +bool CBString::operator > (const char * s) const { + return ! ((*this) <= s); +} + +bool CBString::operator > (const unsigned char * s) const { + return ! ((*this) <= s); +} + +bool CBString::operator >= (const CBString& b) const { + return ! ((*this) < b); +} + +bool CBString::operator >= (const char * s) const { + return ! ((*this) < s); +} + +bool CBString::operator >= (const unsigned char * s) const { + return ! ((*this) < s); +} + +CBString::operator double () const { +double d = 0; + if (1 != sscanf ((const char *)this->data, "%lf", &d)) { + bstringThrow ("Unable to convert to a double"); + } + return d; +} + +CBString::operator float () const { +float d = 0; + if (1 != sscanf ((const char *)this->data, "%f", &d)) { + bstringThrow ("Unable to convert to a float"); + } + return d; +} + +CBString::operator int () const { +int d = 0; + if (1 != sscanf ((const char *)this->data, "%d", &d)) { + bstringThrow ("Unable to convert to an int"); + } + return d; +} + +CBString::operator unsigned int () const { +unsigned int d = 0; + if (1 != sscanf ((const char *)this->data, "%u", &d)) { + bstringThrow ("Unable to convert to an unsigned int"); + } + return d; +} + +#ifdef __TURBOC__ +# ifndef BSTRLIB_NOVSNP +# define BSTRLIB_NOVSNP +# endif +#endif + +/* Give WATCOM C/C++, MSVC some latitude for their non-support of vsnprintf */ +#if defined(__WATCOMC__) || defined(_MSC_VER) +#define exvsnprintf(r,b,n,f,a) {r = _vsnprintf (b,n,f,a);} +#else +#ifdef BSTRLIB_NOVSNP +/* This is just a hack. If you are using a system without a vsnprintf, it is + not recommended that bformat be used at all. */ +#define exvsnprintf(r,b,n,f,a) {vsprintf (b,f,a); r = -1;} +#define START_VSNBUFF (256) +#else + +#if defined (__GNUC__) && !defined (__PPC__) +/* Something is making gcc complain about this prototype not being here, so + I've just gone ahead and put it in. */ +extern "C" { +extern int vsnprintf (char *buf, size_t count, const char *format, va_list arg); +} +#endif + +#define exvsnprintf(r,b,n,f,a) {r = vsnprintf (b,n,f,a);} +#endif +#endif + +#ifndef START_VSNBUFF +#define START_VSNBUFF (16) +#endif + +/* + * Yeah I'd like to just call a vformat function or something, but because of + * the ANSI specified brokeness of the va_* macros, it is actually not + * possible to do this correctly. + */ + +void CBString::format (const char * fmt, ...) { + bstring b; + va_list arglist; + int r, n; + + if (mlen <= 0) bstringThrow ("Write protection error"); + if (fmt == NULL) { + *this = ""; + bstringThrow ("CBString::format (NULL, ...) is erroneous."); + } else { + + if ((b = bfromcstr ("")) == NULL) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("CBString::format out of memory."); +#else + *this = ""; +#endif + } else { + if ((n = (int) (2 * (strlen) (fmt))) < START_VSNBUFF) n = START_VSNBUFF; + for (;;) { + if (BSTR_OK != balloc (b, n + 2)) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("CBString::format out of memory."); +#else + b = bformat (""); + break; +#endif + } + + va_start (arglist, fmt); + exvsnprintf (r, (char *) b->data, n + 1, fmt, arglist); + va_end (arglist); + + b->data[n] = '\0'; + b->slen = (int) (strlen) ((char *) b->data); + + if (b->slen < n) break; + if (r > n) n = r; else n += n; + } + *this = *b; + bdestroy (b); + } + } +} + +void CBString::formata (const char * fmt, ...) { + bstring b; + va_list arglist; + int r, n; + + if (mlen <= 0) bstringThrow ("Write protection error"); + if (fmt == NULL) { + *this += ""; + bstringThrow ("CBString::formata (NULL, ...) is erroneous."); + } else { + + if ((b = bfromcstr ("")) == NULL) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("CBString::format out of memory."); +#else + *this += ""; +#endif + } else { + if ((n = (int) (2 * (strlen) (fmt))) < START_VSNBUFF) n = START_VSNBUFF; + for (;;) { + if (BSTR_OK != balloc (b, n + 2)) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("CBString::format out of memory."); +#else + b = bformat (""); + break; +#endif + } + + va_start (arglist, fmt); + exvsnprintf (r, (char *) b->data, n + 1, fmt, arglist); + va_end (arglist); + + b->data[n] = '\0'; + b->slen = (int) (strlen) ((char *) b->data); + + if (b->slen < n) break; + if (r > n) n = r; else n += n; + } + *this += *b; + bdestroy (b); + } + } +} + +int CBString::caselessEqual (const CBString& b) const { +int ret; + if (BSTR_ERR == (ret = biseqcaseless ((bstring) this, (bstring) &b))) { + bstringThrow ("CBString::caselessEqual Unable to compare"); + } + return ret; +} + +int CBString::caselessCmp (const CBString& b) const { +int ret; + if (SHRT_MIN == (ret = bstricmp ((bstring) this, (bstring) &b))) { + bstringThrow ("CBString::caselessCmp Unable to compare"); + } + return ret; +} + +int CBString::find (const CBString& b, int pos) const { + return binstr ((bstring) this, pos, (bstring) &b); +} + +/* + int CBString::find (const char * b, int pos) const; + + Uses and unrolling and sliding paired indexes character matching. Since + the unrolling is the primary real world impact the true purpose of this + algorithm choice is maximize the effectiveness of the unrolling. The + idea is to scan until at least one match of the current indexed character + from each string, and then shift indexes of both down by and repeat until + the last character form b matches. When the last character from b + matches if the were no mismatches in previous strlen(b) characters then + we know we have a full match, otherwise shift both indexes back strlen(b) + characters and continue. + + In general, if there is any character in b that is not at all in this + CBString, then this algorithm is O(slen). The algorithm does not easily + degenerate into O(slen * strlen(b)) performance except in very uncommon + situations. Thus from a real world perspective, the overhead of + precomputing suffix shifts in the Boyer-Moore algorithm is avoided, while + delivering an unrolled matching inner loop most of the time. + */ + +int CBString::find (const char * b, int pos) const { +int ii, j; +unsigned char c0; +register int i, l; +register unsigned char cx; +register unsigned char * pdata; + + if (NULL == b) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("CBString::find NULL."); +#else + return BSTR_ERR; +#endif + } + + if ((unsigned int) pos > (unsigned int) slen) return BSTR_ERR; + if ('\0' == b[0]) return pos; + if (pos == slen) return BSTR_ERR; + if ('\0' == b[1]) return find (b[0], pos); + + cx = c0 = (unsigned char) b[0]; + l = slen - 1; + + pdata = data; + for (ii = -1, i = pos, j = 0; i < l;) { + /* Unrolled current character test */ + if (cx != pdata[i]) { + if (cx != pdata[1+i]) { + i += 2; + continue; + } + i++; + } + + /* Take note if this is the start of a potential match */ + if (0 == j) ii = i; + + /* Shift the test character down by one */ + j++; + i++; + + /* If this isn't past the last character continue */ + if ('\0' != (cx = b[j])) continue; + + N0:; + + /* If no characters mismatched, then we matched */ + if (i == ii+j) return ii; + + /* Shift back to the beginning */ + i -= j; + j = 0; + cx = c0; + } + + /* Deal with last case if unrolling caused a misalignment */ + if (i == l && cx == pdata[i] && '\0' == b[j+1]) goto N0; + + return BSTR_ERR; +} + +int CBString::caselessfind (const CBString& b, int pos) const { + return binstrcaseless ((bstring) this, pos, (bstring) &b); +} + +int CBString::caselessfind (const char * b, int pos) const { +struct tagbstring t; + + if (NULL == b) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("CBString::caselessfind NULL."); +#else + return BSTR_ERR; +#endif + } + + if ((unsigned int) pos > (unsigned int) slen) return BSTR_ERR; + if ('\0' == b[0]) return pos; + if (pos == slen) return BSTR_ERR; + + btfromcstr (t, b); + return binstrcaseless ((bstring) this, pos, (bstring) &t); +} + +int CBString::find (char c, int pos) const { + if (pos < 0) return BSTR_ERR; + for (;pos < slen; pos++) { + if (data[pos] == (unsigned char) c) return pos; + } + return BSTR_ERR; +} + +int CBString::reversefind (const CBString& b, int pos) const { + return binstrr ((bstring) this, pos, (bstring) &b); +} + +int CBString::reversefind (const char * b, int pos) const { +struct tagbstring t; + if (NULL == b) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("CBString::reversefind NULL."); +#else + return BSTR_ERR; +#endif + } + cstr2tbstr (t, b); + return binstrr ((bstring) this, pos, &t); +} + +int CBString::caselessreversefind (const CBString& b, int pos) const { + return binstrrcaseless ((bstring) this, pos, (bstring) &b); +} + +int CBString::caselessreversefind (const char * b, int pos) const { +struct tagbstring t; + + if (NULL == b) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("CBString::caselessreversefind NULL."); +#else + return BSTR_ERR; +#endif + } + + if ((unsigned int) pos > (unsigned int) slen) return BSTR_ERR; + if ('\0' == b[0]) return pos; + if (pos == slen) return BSTR_ERR; + + btfromcstr (t, b); + return binstrrcaseless ((bstring) this, pos, (bstring) &t); +} + +int CBString::reversefind (char c, int pos) const { + if (pos > slen) return BSTR_ERR; + if (pos == slen) pos--; + for (;pos >= 0; pos--) { + if (data[pos] == (unsigned char) c) return pos; + } + return BSTR_ERR; +} + +int CBString::findchr (const CBString& b, int pos) const { + return binchr ((bstring) this, pos, (bstring) &b); +} + +int CBString::findchr (const char * s, int pos) const { +struct tagbstring t; + if (NULL == s) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("CBString::findchr NULL."); +#else + return BSTR_ERR; +#endif + } + cstr2tbstr (t, s); + return binchr ((bstring) this, pos, (bstring) &t); +} + +int CBString::nfindchr (const CBString& b, int pos) const { + return bninchr ((bstring) this, pos, (bstring) &b); +} + +int CBString::nfindchr (const char * s, int pos) const { +struct tagbstring t; + if (NULL == s) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("CBString::nfindchr NULL."); +#else + return BSTR_ERR; +#endif + } + cstr2tbstr (t, s); + return bninchr ((bstring) this, pos, &t); +} + +int CBString::reversefindchr (const CBString& b, int pos) const { + return binchrr ((bstring) this, pos, (bstring) &b); +} + +int CBString::reversefindchr (const char * s, int pos) const { +struct tagbstring t; + if (NULL == s) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("CBString::reversefindchr NULL."); +#else + return BSTR_ERR; +#endif + } + cstr2tbstr (t, s); + return binchrr ((bstring) this, pos, &t); +} + +int CBString::nreversefindchr (const CBString& b, int pos) const { + return bninchrr ((bstring) this, pos, (bstring) &b); +} + +int CBString::nreversefindchr (const char * s, int pos) const { +struct tagbstring t; + if (NULL == s) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("CBString::nreversefindchr NULL."); +#else + return BSTR_ERR; +#endif + } + cstr2tbstr (t, s); + return bninchrr ((bstring) this, pos, &t); +} + +const CBString CBString::midstr (int left, int len) const { +struct tagbstring t; + if (left < 0) { + len += left; + left = 0; + } + if (len > slen - left) len = slen - left; + if (len <= 0) return CBString (""); + blk2tbstr (t, data + left, len); + return CBString (t); +} + +void CBString::alloc (int len) { + if (BSTR_ERR == balloc ((bstring)this, len)) { + bstringThrow ("Failure in alloc"); + } +} + +void CBString::fill (int len, unsigned char cfill) { + slen = 0; + if (BSTR_ERR == bsetstr (this, len, NULL, cfill)) { + bstringThrow ("Failure in fill"); + } +} + +void CBString::setsubstr (int pos, const CBString& b, unsigned char cfill) { + if (BSTR_ERR == bsetstr (this, pos, (bstring) &b, cfill)) { + bstringThrow ("Failure in setsubstr"); + } +} + +void CBString::setsubstr (int pos, const char * s, unsigned char cfill) { +struct tagbstring t; + if (NULL == s) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("setsubstr NULL."); +#else + return; +#endif + } + cstr2tbstr (t, s); + if (BSTR_ERR == bsetstr (this, pos, &t, cfill)) { + bstringThrow ("Failure in setsubstr"); + } +} + +void CBString::insert (int pos, const CBString& b, unsigned char cfill) { + if (BSTR_ERR == binsert (this, pos, (bstring) &b, cfill)) { + bstringThrow ("Failure in insert"); + } +} + +void CBString::insert (int pos, const char * s, unsigned char cfill) { +struct tagbstring t; + if (NULL == s) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("insert NULL."); +#else + return; +#endif + } + cstr2tbstr (t, s); + if (BSTR_ERR == binsert (this, pos, &t, cfill)) { + bstringThrow ("Failure in insert"); + } +} + +void CBString::insertchrs (int pos, int len, unsigned char cfill) { + if (BSTR_ERR == binsertch (this, pos, len, cfill)) { + bstringThrow ("Failure in insertchrs"); + } +} + +void CBString::replace (int pos, int len, const CBString& b, unsigned char cfill) { + if (BSTR_ERR == breplace (this, pos, len, (bstring) &b, cfill)) { + bstringThrow ("Failure in replace"); + } +} + +void CBString::replace (int pos, int len, const char * s, unsigned char cfill) { +struct tagbstring t; +size_t q; + + if (mlen <= 0) bstringThrow ("Write protection error"); + if (NULL == s || (pos|len) < 0) { + bstringThrow ("Failure in replace"); + } else { + if (pos + len >= slen) { + cstr2tbstr (t, s); + if (BSTR_ERR == bsetstr (this, pos, &t, cfill)) { + bstringThrow ("Failure in replace"); + } else if (pos + t.slen < slen) { + slen = pos + t.slen; + data[slen] = '\0'; + } + } else { + + /* Aliasing case */ + if ((unsigned int) (data - (unsigned char *) s) < (unsigned int) slen) { + replace (pos, len, CBString(s), cfill); + return; + } + + if ((q = strlen (s)) > (size_t) len || len < 0) { + if (slen + q - len >= INT_MAX) bstringThrow ("Failure in replace, result too long."); + alloc ((int) (slen + q - len)); + if (NULL == data) return; + } + if ((int) q != len) bstr__memmove (data + pos + q, data + pos + len, slen - (pos + len)); + bstr__memcpy (data + pos, s, q); + slen += ((int) q) - len; + data[slen] = '\0'; + } + } +} + +void CBString::findreplace (const CBString& sfind, const CBString& repl, int pos) { + if (BSTR_ERR == bfindreplace (this, (bstring) &sfind, (bstring) &repl, pos)) { + bstringThrow ("Failure in findreplace"); + } +} + +void CBString::findreplace (const CBString& sfind, const char * repl, int pos) { +struct tagbstring t; + if (NULL == repl) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("findreplace NULL."); +#else + return; +#endif + } + cstr2tbstr (t, repl); + if (BSTR_ERR == bfindreplace (this, (bstring) &sfind, (bstring) &t, pos)) { + bstringThrow ("Failure in findreplace"); + } +} + +void CBString::findreplace (const char * sfind, const CBString& repl, int pos) { +struct tagbstring t; + if (NULL == sfind) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("findreplace NULL."); +#else + return; +#endif + } + cstr2tbstr (t, sfind); + if (BSTR_ERR == bfindreplace (this, (bstring) &t, (bstring) &repl, pos)) { + bstringThrow ("Failure in findreplace"); + } +} + +void CBString::findreplace (const char * sfind, const char * repl, int pos) { +struct tagbstring t, u; + if (NULL == repl || NULL == sfind) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("findreplace NULL."); +#else + return; +#endif + } + cstr2tbstr (t, sfind); + cstr2tbstr (u, repl); + if (BSTR_ERR == bfindreplace (this, (bstring) &t, (bstring) &u, pos)) { + bstringThrow ("Failure in findreplace"); + } +} + +void CBString::findreplacecaseless (const CBString& sfind, const CBString& repl, int pos) { + if (BSTR_ERR == bfindreplacecaseless (this, (bstring) &sfind, (bstring) &repl, pos)) { + bstringThrow ("Failure in findreplacecaseless"); + } +} + +void CBString::findreplacecaseless (const CBString& sfind, const char * repl, int pos) { +struct tagbstring t; + if (NULL == repl) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("findreplacecaseless NULL."); +#else + return; +#endif + } + cstr2tbstr (t, repl); + if (BSTR_ERR == bfindreplacecaseless (this, (bstring) &sfind, (bstring) &t, pos)) { + bstringThrow ("Failure in findreplacecaseless"); + } +} + +void CBString::findreplacecaseless (const char * sfind, const CBString& repl, int pos) { +struct tagbstring t; + if (NULL == sfind) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("findreplacecaseless NULL."); +#else + return; +#endif + } + cstr2tbstr (t, sfind); + if (BSTR_ERR == bfindreplacecaseless (this, (bstring) &t, (bstring) &repl, pos)) { + bstringThrow ("Failure in findreplacecaseless"); + } +} + +void CBString::findreplacecaseless (const char * sfind, const char * repl, int pos) { +struct tagbstring t, u; + if (NULL == repl || NULL == sfind) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("findreplacecaseless NULL."); +#else + return; +#endif + } + cstr2tbstr (t, sfind); + cstr2tbstr (u, repl); + if (BSTR_ERR == bfindreplacecaseless (this, (bstring) &t, (bstring) &u, pos)) { + bstringThrow ("Failure in findreplacecaseless"); + } +} + +void CBString::remove (int pos, int len) { + if (BSTR_ERR == bdelete (this, pos, len)) { + bstringThrow ("Failure in remove"); + } +} + +void CBString::trunc (int len) { + if (len < 0) { + bstringThrow ("Failure in trunc"); + } + if (len < slen) { + slen = len; + data[len] = '\0'; + } +} + +void CBString::ltrim (const CBString& b) { + int l = nfindchr (b, 0); + if (l == BSTR_ERR) l = slen; + remove (0, l); +} + +void CBString::rtrim (const CBString& b) { + int l = nreversefindchr (b, slen - 1); +#if BSTR_ERR != -1 + if (l == BSTR_ERR) l = -1; +#endif + slen = l + 1; + if (mlen > slen) data[slen] = '\0'; +} + +void CBString::toupper () { + if (BSTR_ERR == btoupper ((bstring) this)) { + bstringThrow ("Failure in toupper"); + } +} + +void CBString::tolower () { + if (BSTR_ERR == btolower ((bstring) this)) { + bstringThrow ("Failure in tolower"); + } +} + +void CBString::repeat (int count) { + count *= slen; + if (count == 0) { + trunc (0); + return; + } + if (count < 0 || BSTR_ERR == bpattern (this, count)) { + bstringThrow ("Failure in repeat"); + } +} + +int CBString::gets (bNgetc getcPtr, void * parm, char terminator) { + if (mlen <= 0) bstringThrow ("Write protection error"); + bstring b = bgets (getcPtr, parm, terminator); + if (b == NULL) { + slen = 0; + return -1; + } + *this = *b; + bdestroy (b); + return 0; +} + +int CBString::read (bNread readPtr, void * parm) { + if (mlen <= 0) bstringThrow ("Write protection error"); + bstring b = bread (readPtr, parm); + if (b == NULL) { + slen = 0; + return -1; + } + *this = *b; + bdestroy (b); + return 0; +} + +const CBString operator + (const char *a, const CBString& b) { + return CBString(a) + b; +} + +const CBString operator + (const unsigned char *a, const CBString& b) { + return CBString((const char *)a) + b; +} + +const CBString operator + (char c, const CBString& b) { + return CBString(c) + b; +} + +const CBString operator + (unsigned char c, const CBString& b) { + return CBString(c) + b; +} + +const CBString operator + (const tagbstring& x, const CBString& b) { + return CBString(x) + b; +} + +void CBString::writeprotect () { + if (mlen >= 0) mlen = -1; +} + +void CBString::writeallow () { + if (mlen == -1) mlen = slen + (slen == 0); + else if (mlen < 0) { + bstringThrow ("Cannot unprotect a constant"); + } +} + +#if defined(BSTRLIB_CAN_USE_STL) + +// Constructors. + +CBString::CBString (const CBStringList& l) { +int c; +size_t i; + + for (c=1, i=0; i < l.size(); i++) { + c += l.at(i).slen; + } + + mlen = c; + slen = 0; + data = (unsigned char *) bstr__alloc (c); + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in (CBStringList) constructor"); + } else { + for (i=0; i < l.size(); i++) { + *this += l.at(i); + } + } +} + +CBString::CBString (const struct CBStringList& l, const CBString& sep) { +int c, sl = sep.length (); +size_t i; + + for (c=1, i=0; i < l.size(); i++) { + c += l.at(i).slen + sl; + } + + mlen = c; + slen = 0; + data = (unsigned char *) bstr__alloc (mlen); + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in (CBStringList) constructor"); + } else { + for (i=0; i < l.size(); i++) { + if (i > 0) *this += sep; + *this += l.at(i); + } + } +} + +CBString::CBString (const struct CBStringList& l, char sep) { +int c; +size_t i; + + for (c=1, i=0; i < l.size(); i++) { + c += l.at(i).slen + 1; + } + + mlen = c; + slen = 0; + data = (unsigned char *) bstr__alloc (mlen); + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in (CBStringList) constructor"); + } else { + for (i=0; i < l.size(); i++) { + if (i > 0) *this += sep; + *this += l.at(i); + } + } +} + +CBString::CBString (const struct CBStringList& l, unsigned char sep) { +int c; +size_t i; + + for (c=1, i=0; i < l.size(); i++) { + c += l.at(i).slen + 1; + } + + mlen = c; + slen = 0; + data = (unsigned char *) bstr__alloc (mlen); + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in (CBStringList) constructor"); + } else { + for (i=0; i < l.size(); i++) { + if (i > 0) *this += sep; + *this += l.at(i); + } + } +} + +void CBString::join (const struct CBStringList& l) { +int c; +size_t i; + + if (mlen <= 0) { + bstringThrow ("Write protection error"); + } + + for (c=1, i=0; i < l.size(); i++) { + c += l.at(i).slen; + if (c < 0) bstringThrow ("Failure in (CBStringList) constructor, too long"); + } + + alloc (c); + slen = 0; + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in (CBStringList) constructor"); + } else { + for (i=0; i < l.size(); i++) { + *this += l.at(i); + } + } +} + +void CBString::join (const struct CBStringList& l, const CBString& sep) { +int c, sl = sep.length(); +size_t i; + + if (mlen <= 0) { + bstringThrow ("Write protection error"); + } + + for (c=1, i=0; i < l.size(); i++) { + c += l.at(i).slen + sl; + if (c < sl) bstringThrow ("Failure in (CBStringList) constructor, too long"); + } + + alloc (c); + slen = 0; + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in (CBStringList) constructor"); + } else { + for (i=0; i < l.size(); i++) { + if (i > 0) *this += sep; + *this += l.at(i); + } + } +} + + +void CBString::join (const struct CBStringList& l, char sep) { +int c; +size_t i; + + if (mlen <= 0) { + bstringThrow ("Write protection error"); + } + + for (c=1, i=0; i < l.size(); i++) { + c += l.at(i).slen + 1; + if (c <= 0) bstringThrow ("Failure in (CBStringList) constructor, too long"); + } + + alloc (c); + slen = 0; + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in (CBStringList) constructor"); + } else { + for (i=0; i < l.size(); i++) { + if (i > 0) *this += sep; + *this += l.at(i); + } + } +} + +void CBString::join (const struct CBStringList& l, unsigned char sep) { +int c; +size_t i; + + if (mlen <= 0) { + bstringThrow ("Write protection error"); + } + + for (c=1, i=0; i < l.size(); i++) { + c += l.at(i).slen + 1; + if (c <= 0) bstringThrow ("Failure in (CBStringList) constructor, too long"); + } + + alloc (c); + slen = 0; + if (!data) { + mlen = slen = 0; + bstringThrow ("Failure in (CBStringList) constructor"); + } else { + for (i=0; i < l.size(); i++) { + if (i > 0) *this += sep; + *this += l.at(i); + } + } +} + +// Split functions. + +void CBStringList::split (const CBString& b, unsigned char splitChar) { +int p, i; + + p = 0; + do { + for (i = p; i < b.length (); i++) { + if (b.character (i) == splitChar) break; + } + if (i >= p) this->push_back (CBString (&(b.data[p]), i - p)); + p = i + 1; + } while (p <= b.length ()); +} + +void CBStringList::split (const CBString& b, const CBString& s) { +struct { unsigned long content[(1 << CHAR_BIT) / 32]; } chrs; +unsigned char c; +int p, i; + + if (s.length() == 0) bstringThrow ("Null splitstring failure"); + if (s.length() == 1) { + this->split (b, s.character (0)); + } else { + + for (i=0; i < ((1 << CHAR_BIT) / 32); i++) chrs.content[i] = 0x0; + for (i=0; i < s.length(); i++) { + c = s.character (i); + chrs.content[c >> 5] |= ((long)1) << (c & 31); + } + + p = 0; + do { + for (i = p; i < b.length (); i++) { + c = b.character (i); + if (chrs.content[c >> 5] & ((long)1) << (c & 31)) break; + } + if (i >= p) this->push_back (CBString (&(b.data[p]), i - p)); + p = i + 1; + } while (p <= b.length ()); + } +} + +void CBStringList::splitstr (const CBString& b, const CBString& s) { +int p, i; + + if (s.length() == 1) { + this->split (b, s.character (0)); + } else if (s.length() == 0) { + for (i=0; i < b.length (); i++) { + this->push_back (CBString (b.data[i])); + } + } else { + for (p=0; (i = b.find (s, p)) >= 0; p = i + s.length ()) { + this->push_back (b.midstr (p, i - p)); + } + if (p <= b.length ()) { + this->push_back (b.midstr (p, b.length () - p)); + } + } +} + +static int streamSplitCb (void * parm, int ofs, const_bstring entry) { +CBStringList * r = (CBStringList *) parm; + + ofs = ofs; + r->push_back (CBString (*entry)); + return 0; +} + +void CBStringList::split (const CBStream& b, const CBString& s) { + if (0 > bssplitscb (b.m_s, (bstring) &s, streamSplitCb, + (void *) this)) { + bstringThrow ("Split bstream failure"); + } +} + +void CBStringList::split (const CBStream& b, unsigned char splitChar) { +CBString sc (splitChar); + if (0 > bssplitscb (b.m_s, (bstring) &sc, + streamSplitCb, (void *) this)) { + bstringThrow ("Split bstream failure"); + } +} + +void CBStringList::splitstr (const CBStream& b, const CBString& s) { + if (0 > bssplitstrcb (b.m_s, (bstring) &s, streamSplitCb, + (void *) this)) { + bstringThrow ("Split bstream failure"); + } +} + +#endif + +#if defined(BSTRLIB_CAN_USE_IOSTREAM) + +std::ostream& operator << (std::ostream& sout, CBString b) { + return sout.write ((const char *)b, b.length()); +} + +#include + +static int istreamGets (void * parm) { + char c = '\n'; + ((std::istream *)parm)->get(c); + if (isspace (c)) c = '\n'; + return c; +} + +std::istream& operator >> (std::istream& sin, CBString& b) { + do { + b.gets ((bNgetc) istreamGets, &sin, '\n'); + if (b.slen > 0 && b.data[b.slen-1] == '\n') b.slen--; + } while (b.slen == 0 && !sin.eof ()); + return sin; +} + +struct sgetc { + std::istream * sin; + char terminator; +}; + +static int istreamGetc (void * parm) { + char c = ((struct sgetc *)parm)->terminator; + ((struct sgetc *)parm)->sin->get(c); + return c; +} + +std::istream& getline (std::istream& sin, CBString& b, char terminator) { +struct sgetc parm; + parm.sin = &sin; + parm.terminator = terminator; + b.gets ((bNgetc) istreamGetc, &parm, terminator); + if (b.slen > 0 && b.data[b.slen-1] == terminator) b.slen--; + return sin; +} + +#endif + +CBStream::CBStream (bNread readPtr, void * parm) { + m_s = bsopen (readPtr, parm); +} + +CBStream::~CBStream () { + bsclose (m_s); +} + +int CBStream::buffLengthSet (int sz) { + if (sz <= 0) { + bstringThrow ("buffLengthSet parameter failure"); + } + return bsbufflength (m_s, sz); +} + +int CBStream::buffLengthGet () { + return bsbufflength (m_s, 0); +} + +CBString CBStream::readLine (char terminator) { + CBString ret(""); + if (0 > bsreadln ((bstring) &ret, m_s, terminator) && eof () < 0) { + bstringThrow ("Failed readLine"); + } + return ret; +} + +CBString CBStream::readLine (const CBString& terminator) { + CBString ret(""); + if (0 > bsreadlns ((bstring) &ret, m_s, (bstring) &terminator) && eof () < 0) { + bstringThrow ("Failed readLine"); + } + return ret; +} + +void CBStream::readLine (CBString& s, char terminator) { + if (0 > bsreadln ((bstring) &s, m_s, terminator) && eof () < 0) { + bstringThrow ("Failed readLine"); + } +} + +void CBStream::readLine (CBString& s, const CBString& terminator) { + if (0 > bsreadlns ((bstring) &s, m_s, (bstring) &terminator) && eof () < 0) { + bstringThrow ("Failed readLine"); + } +} + +void CBStream::readLineAppend (CBString& s, char terminator) { + if (0 > bsreadlna ((bstring) &s, m_s, terminator) && eof () < 0) { + bstringThrow ("Failed readLineAppend"); + } +} + +void CBStream::readLineAppend (CBString& s, const CBString& terminator) { + if (0 > bsreadlnsa ((bstring) &s, m_s, (bstring) &terminator) && eof () < 0) { + bstringThrow ("Failed readLineAppend"); + } +} + +#define BS_BUFF_SZ (1024) + +CBString CBStream::read () { + CBString ret(""); + while (!bseof (m_s)) { + if (0 > bsreada ((bstring) &ret, m_s, BS_BUFF_SZ) && eof () < 0) { + bstringThrow ("Failed read"); + } + } + return ret; +} + +CBString& CBStream::operator >> (CBString& s) { + while (!bseof (m_s)) { + if (0 > bsreada ((bstring) &s, m_s, BS_BUFF_SZ) && eof () < 0) { + bstringThrow ("Failed read"); + } + } + return s; +} + +CBString CBStream::read (int n) { + CBString ret(""); + if (0 > bsread ((bstring) &ret, m_s, n) && eof () < 0) { + bstringThrow ("Failed read"); + } + return ret; +} + +void CBStream::read (CBString& s) { + s.slen = 0; + while (!bseof (m_s)) { + if (0 > bsreada ((bstring) &s, m_s, BS_BUFF_SZ)) { + bstringThrow ("Failed read"); + } + } +} + +void CBStream::read (CBString& s, int n) { + if (0 > bsread ((bstring) &s, m_s, n)) { + bstringThrow ("Failed read"); + } +} + +void CBStream::readAppend (CBString& s) { + while (!bseof (m_s)) { + if (0 > bsreada ((bstring) &s, m_s, BS_BUFF_SZ)) { + bstringThrow ("Failed readAppend"); + } + } +} + +void CBStream::readAppend (CBString& s, int n) { + if (0 > bsreada ((bstring) &s, m_s, n)) { + bstringThrow ("Failed readAppend"); + } +} + +void CBStream::unread (const CBString& s) { + if (0 > bsunread (m_s, (bstring) &s)) { + bstringThrow ("Failed unread"); + } +} + +CBString CBStream::peek () const { + CBString ret (""); + if (0 > bspeek ((bstring) &ret, m_s)) { + bstringThrow ("Failed peek"); + } + return ret; +} + +void CBStream::peek (CBString& s) const { + s.slen = 0; + if (0 > bspeek ((bstring) &s, m_s)) { + bstringThrow ("Failed peek"); + } +} + +void CBStream::peekAppend (CBString& s) const { + if (0 > bspeek ((bstring) &s, m_s)) { + bstringThrow ("Failed peekAppend"); + } +} + +int CBStream::eof () const { + int ret = bseof (m_s); + if (0 > ret) { + bstringThrow ("Failed eof"); + } + return ret; +} + +} // namespace Bstrlib diff --git a/bstrlib/bstrwrap.h b/bstrlib/bstrwrap.h new file mode 100644 index 0000000..dcb0ecc --- /dev/null +++ b/bstrlib/bstrwrap.h @@ -0,0 +1,446 @@ +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2015, and is covered by the BSD open source + * license and the GPL. Refer to the accompanying documentation for details + * on usage and license. + */ + +/* + * bstrwrap.h + * + * This file is the C++ wrapper for the bstring functions. + */ + +#ifndef BSTRWRAP_INCLUDE +#define BSTRWRAP_INCLUDE + +/////////////////// Configuration defines ////////////////////////////// + +// WATCOM C/C++ has broken STL and std::iostream support. If you have +// ported over STLport, then you can #define BSTRLIB_CAN_USE_STL to use +// the CBStringList class. +#if defined(__WATCOMC__) +# if !defined (BSTRLIB_CAN_USE_STL) && !defined (BSTRLIB_CANNOT_USE_STL) +# define BSTRLIB_CANNOT_USE_STL +# endif +# if !defined (BSTRLIB_CAN_USE_IOSTREAM) && !defined (BSTRLIB_CANNOT_USE_IOSTREAM) +# define BSTRLIB_CANNOT_USE_IOSTREAM +# endif +#endif + +// By default it assumed that STL has been installed and works for your +// compiler. If this is not the case, then #define BSTRLIB_CANNOT_USE_STL +#if !defined (BSTRLIB_CANNOT_USE_STL) && !defined (BSTRLIB_CAN_USE_STL) +#define BSTRLIB_CAN_USE_STL +#endif + +// By default it assumed that std::iostream works well with your compiler. +// If this is not the case, then #define BSTRLIB_CAN_USE_IOSTREAM +#if !defined (BSTRLIB_CANNOT_USE_IOSTREAM) && !defined (BSTRLIB_CAN_USE_IOSTREAM) +#define BSTRLIB_CAN_USE_IOSTREAM +#endif + +// By default it is assumed that your compiler can deal with and has enabled +// exception handlling. If this is not the case then you will need to +// #define BSTRLIB_DOESNT_THROW_EXCEPTIONS +#if !defined (BSTRLIB_THROWS_EXCEPTIONS) && !defined (BSTRLIB_DOESNT_THROW_EXCEPTIONS) +#define BSTRLIB_THROWS_EXCEPTIONS +#endif + +//////////////////////////////////////////////////////////////////////// + +#include +#include "bstrlib.h" +#include "../common/ubytearray.h" + +#ifdef __cplusplus + +#if defined(BSTRLIB_CAN_USE_STL) + +#if defined(__WATCOMC__) +#pragma warning 604 10 +#pragma warning 595 10 +#pragma warning 594 10 +#pragma warning 549 10 +#endif + +#include +#include + +#if defined(__WATCOMC__) +#pragma warning 604 9 +#pragma warning 595 9 +#pragma warning 594 9 +#endif + +#endif + +namespace Bstrlib { + +#ifdef BSTRLIB_THROWS_EXCEPTIONS +#if defined(BSTRLIB_CAN_USE_STL) +struct CBStringException : public std::exception { +private: + std::string msg; +public: + CBStringException (const std::string inmsg) : msg(inmsg) {} + virtual ~CBStringException () throw () {} + virtual const char *what () const throw () { return msg.c_str(); } +}; +#else +struct CBStringException { +private: + char * msg; + int needToFree; +public: + CBStringException (const char * inmsg) : needToFree(0) { + if (inmsg) { + msg = (char *) malloc (1 + strlen (inmsg)); + if (NULL == msg) msg = "Out of memory"; + else { + strcpy (msg, inmsg); + needToFree = 1; + } + } else { + msg = "NULL exception message"; + } + } + virtual ~CBStringException () throw () { + if (needToFree) { + free (msg); + needToFree = 0; + msg = NULL; + } + } + virtual const char *what () const throw () { return msg; } +}; +#endif +#define bstringThrow(er) {\ + CBStringException bstr__cppwrapper_exception ("CBString::" er "");\ + throw bstr__cppwrapper_exception;\ +} +#else +#define bstringThrow(er) {} +#endif + +struct CBString; + +#ifdef _MSC_VER +#pragma warning(disable:4512) +#endif + +class CBCharWriteProtected { +friend struct CBString; + private: + const struct tagbstring& s; + unsigned int idx; + CBCharWriteProtected (const struct tagbstring& c, int i) : s(c), idx((unsigned int)i) { + if (idx >= (unsigned) s.slen) { + bstringThrow ("character index out of bounds"); + } + } + + public: + + inline char operator = (char c) { + if (s.mlen <= 0) { + bstringThrow ("Write protection error"); + } else { +#ifndef BSTRLIB_THROWS_EXCEPTIONS + if (idx >= (unsigned) s.slen) return '\0'; +#endif + s.data[idx] = (unsigned char) c; + } + return (char) s.data[idx]; + } + inline unsigned char operator = (unsigned char c) { + if (s.mlen <= 0) { + bstringThrow ("Write protection error"); + } else { +#ifndef BSTRLIB_THROWS_EXCEPTIONS + if (idx >= (unsigned) s.slen) return '\0'; +#endif + s.data[idx] = c; + } + return s.data[idx]; + } + inline operator unsigned char () const { +#ifndef BSTRLIB_THROWS_EXCEPTIONS + if (idx >= (unsigned) s.slen) return (unsigned char) '\0'; +#endif + return s.data[idx]; + } +}; + +struct CBString : public tagbstring { + + // Constructors + CBString (); + CBString (char c); + CBString (unsigned char c); + CBString (const char *s); + CBString (int len, const char *s); + CBString (const CBString& b); + CBString (const tagbstring& x); + CBString (char c, int len); + CBString (const void * blk, int len); + +#if defined(BSTRLIB_CAN_USE_STL) + CBString (const struct CBStringList& l); + CBString (const struct CBStringList& l, const CBString& sep); + CBString (const struct CBStringList& l, char sep); + CBString (const struct CBStringList& l, unsigned char sep); +#endif + + // Destructor +#if !defined(BSTRLIB_DONT_USE_VIRTUAL_DESTRUCTOR) + virtual +#endif + ~CBString (); + + // = operator + const CBString& operator = (char c); + const CBString& operator = (unsigned char c); + const CBString& operator = (const char *s); + const CBString& operator = (const CBString& b); + const CBString& operator = (const tagbstring& x); + + // += operator + const CBString& operator += (char c); + const CBString& operator += (unsigned char c); + const CBString& operator += (const char *s); + const CBString& operator += (const CBString& b); + const CBString& operator += (const tagbstring& x); + + // *= operator + inline const CBString& operator *= (int count) { + this->repeat (count); + return *this; + } + + // + operator + const CBString operator + (char c) const; + const CBString operator + (unsigned char c) const; + const CBString operator + (const unsigned char *s) const; + const CBString operator + (const char *s) const; + const CBString operator + (const CBString& b) const; + const CBString operator + (const tagbstring& x) const; + + // * operator + inline const CBString operator * (int count) const { + CBString retval (*this); + retval.repeat (count); + return retval; + } + + // Comparison operators + bool operator == (const CBString& b) const; + bool operator == (const char * s) const; + bool operator == (const unsigned char * s) const; + bool operator != (const CBString& b) const; + bool operator != (const char * s) const; + bool operator != (const unsigned char * s) const; + bool operator < (const CBString& b) const; + bool operator < (const char * s) const; + bool operator < (const unsigned char * s) const; + bool operator <= (const CBString& b) const; + bool operator <= (const char * s) const; + bool operator <= (const unsigned char * s) const; + bool operator > (const CBString& b) const; + bool operator > (const char * s) const; + bool operator > (const unsigned char * s) const; + bool operator >= (const CBString& b) const; + bool operator >= (const char * s) const; + bool operator >= (const unsigned char * s) const; + + // Casts + inline operator const char* () const { return (const char *)data; } + inline operator const unsigned char* () const { return (const unsigned char *)data; } + operator double () const; + operator float () const; + operator int () const; + operator unsigned int () const; + + // Accessors + inline int length () const {return slen;} + + inline unsigned char character (int i) const { + if (((unsigned) i) >= (unsigned) slen) { +#ifdef BSTRLIB_THROWS_EXCEPTIONS + bstringThrow ("character idx out of bounds"); +#else + return '\0'; +#endif + } + return data[i]; + } + inline unsigned char operator [] (int i) const { return character(i); } + + inline CBCharWriteProtected character (int i) { + return CBCharWriteProtected (*this, i); + } + inline CBCharWriteProtected operator [] (int i) { return character(i); } + + // Space allocation hint method. + void alloc (int length); + + // Search methods. + int caselessEqual (const CBString& b) const; + int caselessCmp (const CBString& b) const; + int find (const CBString& b, int pos = 0) const; + int find (const char * b, int pos = 0) const; + int caselessfind (const CBString& b, int pos = 0) const; + int caselessfind (const char * b, int pos = 0) const; + int find (char c, int pos = 0) const; + int reversefind (const CBString& b, int pos) const; + int reversefind (const char * b, int pos) const; + int caselessreversefind (const CBString& b, int pos) const; + int caselessreversefind (const char * b, int pos) const; + int reversefind (char c, int pos) const; + int findchr (const CBString& b, int pos = 0) const; + int findchr (const char * s, int pos = 0) const; + int reversefindchr (const CBString& b, int pos) const; + int reversefindchr (const char * s, int pos) const; + int nfindchr (const CBString& b, int pos = 0) const; + int nfindchr (const char * b, int pos = 0) const; + int nreversefindchr (const CBString& b, int pos) const; + int nreversefindchr (const char * b, int pos) const; + + // Search and substitute methods. + void findreplace (const CBString& find, const CBString& repl, int pos = 0); + void findreplace (const CBString& find, const char * repl, int pos = 0); + void findreplace (const char * find, const CBString& repl, int pos = 0); + void findreplace (const char * find, const char * repl, int pos = 0); + void findreplacecaseless (const CBString& find, const CBString& repl, int pos = 0); + void findreplacecaseless (const CBString& find, const char * repl, int pos = 0); + void findreplacecaseless (const char * find, const CBString& repl, int pos = 0); + void findreplacecaseless (const char * find, const char * repl, int pos = 0); + + // Extraction method. + const CBString midstr (int left, int len) const; + + // Standard manipulation methods. + void setsubstr (int pos, const CBString& b, unsigned char fill = ' '); + void setsubstr (int pos, const char * b, unsigned char fill = ' '); + void insert (int pos, const CBString& b, unsigned char fill = ' '); + void insert (int pos, const char * b, unsigned char fill = ' '); + void insertchrs (int pos, int len, unsigned char fill = ' '); + void replace (int pos, int len, const CBString& b, unsigned char fill = ' '); + void replace (int pos, int len, const char * s, unsigned char fill = ' '); + void remove (int pos, int len); + void trunc (int len); + + // Miscellaneous methods. + void format (const char * fmt, ...); + void formata (const char * fmt, ...); + void fill (int length, unsigned char fill = ' '); + void repeat (int count); + void ltrim (const CBString& b = CBString (bsStaticBlkParms (" \t\v\f\r\n"))); + void rtrim (const CBString& b = CBString (bsStaticBlkParms (" \t\v\f\r\n"))); + inline void trim (const CBString& b = CBString (bsStaticBlkParms (" \t\v\f\r\n"))) { + rtrim (b); + ltrim (b); + } + void toupper (); + void tolower (); + + // Write protection methods. + void writeprotect (); + void writeallow (); + inline bool iswriteprotected () const { return mlen <= 0; } + + // Join methods. +#if defined(BSTRLIB_CAN_USE_STL) + void join (const struct CBStringList& l); + void join (const struct CBStringList& l, const CBString& sep); + void join (const struct CBStringList& l, char sep); + void join (const struct CBStringList& l, unsigned char sep); +#endif + + // CBStream methods + int gets (bNgetc getcPtr, void * parm, char terminator = '\n'); + int read (bNread readPtr, void * parm); + + // QString compatibility methods + bool isEmpty() const { return slen == 0; } + void clear() { *this = ""; } + static CBString fromUtf16(const ushort* str) { // Naive implementation assuming that only ASCII part of UCS2 is used + CBString msg; while (*str) { msg += *(char*)str; str++; } return msg; + } + CBString leftJustified(int length) { if (length > slen) { return *this + CBString(' ', length - slen); } return *this; } +}; +extern const CBString operator + (const char *a, const CBString& b); +extern const CBString operator + (const unsigned char *a, const CBString& b); +extern const CBString operator + (char c, const CBString& b); +extern const CBString operator + (unsigned char c, const CBString& b); +extern const CBString operator + (const tagbstring& x, const CBString& b); +inline const CBString operator * (int count, const CBString& b) { + CBString retval (b); + retval.repeat (count); + return retval; +} + +#if defined(BSTRLIB_CAN_USE_IOSTREAM) +extern std::ostream& operator << (std::ostream& sout, CBString b); +extern std::istream& operator >> (std::istream& sin, CBString& b); +extern std::istream& getline (std::istream& sin, CBString& b, char terminator='\n'); +#endif + +struct CBStream { +friend struct CBStringList; +private: + struct bStream * m_s; +public: + CBStream (bNread readPtr, void * parm); + ~CBStream (); + int buffLengthSet (int sz); + int buffLengthGet (); + int eof () const; + + CBString readLine (char terminator); + CBString readLine (const CBString& terminator); + void readLine (CBString& s, char terminator); + void readLine (CBString& s, const CBString& terminator); + void readLineAppend (CBString& s, char terminator); + void readLineAppend (CBString& s, const CBString& terminator); + + CBString read (); + CBString& operator >> (CBString& s); + + CBString read (int n); + void read (CBString& s); + void read (CBString& s, int n); + void readAppend (CBString& s); + void readAppend (CBString& s, int n); + + void unread (const CBString& s); + inline CBStream& operator << (const CBString& s) { + this->unread (s); + return *this; + } + + CBString peek () const; + void peek (CBString& s) const; + void peekAppend (CBString& s) const; +}; + +#if defined(BSTRLIB_CAN_USE_STL) +struct CBStringList : public std::vector { + // split a string into a vector of strings. + void split (const CBString& b, unsigned char splitChar); + void split (const CBString& b, const CBString& s); + void splitstr (const CBString& b, const CBString& s); + void split (const CBStream& b, unsigned char splitChar); + void split (const CBStream& b, const CBString& s); + void splitstr (const CBStream& b, const CBString& s); +}; +#endif + +} // namespace Bstrlib + +#if !defined (BSTRLIB_DONT_ASSUME_NAMESPACE) +using namespace Bstrlib; +#endif + +#endif +#endif diff --git a/bstrlib/buniutil.c b/bstrlib/buniutil.c new file mode 100644 index 0000000..f8d38e5 --- /dev/null +++ b/bstrlib/buniutil.c @@ -0,0 +1,274 @@ +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2015, and is covered by the BSD open source + * license and the GPL. Refer to the accompanying documentation for details + * on usage and license. + */ + +/* + * buniutil.c + * + * This file is not necessarily part of the core bstring library itself, but + * is just an implementation of basic utf8 processing for bstrlib. Note that + * this module is dependent upon bstrlib.c and utf8util.c + */ + +#include "bstrlib.h" +#include "buniutil.h" + +#define UNICODE__CODE_POINT__REPLACEMENT_CHARACTER (0xFFFDL) + +/* int buIsUTF8Content (const_bstring bu) + * + * Scan string and return 1 if its entire contents is entirely UTF8 code + * points. Otherwise return 0. + */ +int buIsUTF8Content (const_bstring bu) { +struct utf8Iterator iter; + + if (NULL == bdata (bu)) return 0; + for (utf8IteratorInit (&iter, bu->data, bu->slen); + iter.next < iter.slen;) { + if (0 >= utf8IteratorGetNextCodePoint (&iter, -1)) return 0; + } + return 1; +} + +/* int buGetBlkUTF16 (cpUcs2* ucs2, int len, cpUcs4 errCh, const_bstring bu, + * int pos) + * + * Convert a string of UTF8 codepoints (bu) skipping the first pos, into a + * sequence of UTF16 encoded code points. Returns the number of UCS2 16-bit + * words written to the output. No more than len words are written to the + * target array ucs2. If any code point in bu is unparsable, it will be + * translated to errCh. + */ +int buGetBlkUTF16 (/* @out */ cpUcs2* ucs2, int len, cpUcs4 errCh, const_bstring bu, int pos) { +struct tagbstring t; +struct utf8Iterator iter; +cpUcs4 ucs4; +int i, j; + + if (!isLegalUnicodeCodePoint (errCh)) errCh = UNICODE__CODE_POINT__REPLACEMENT_CHARACTER; + if (NULL == ucs2 || 0 >= len || NULL == bdata (bu) || 0 > pos) return BSTR_ERR; + + for (j=0, i=0; j < bu->slen; j++) { + if (0x80 != (0xC0 & bu->data[j])) { + if (i >= pos) break; + i++; + } + } + + t.mlen = -1; + t.data = bu->data + j; + t.slen = bu->slen - j; + + utf8IteratorInit (&iter, t.data, t.slen); + + ucs4 = BSTR_ERR; + for (i=0; 0 < len && iter.next < iter.slen && + 0 <= (ucs4 = utf8IteratorGetNextCodePoint (&iter, errCh)); i++) { + if (ucs4 < 0x10000) { + *ucs2++ = (cpUcs2) ucs4; + len--; + } else { + if (len < 2) { + *ucs2++ = UNICODE__CODE_POINT__REPLACEMENT_CHARACTER; + len--; + } else { + long y = ucs4 - 0x10000; + ucs2[0] = (cpUcs2) (0xD800 | (y >> 10)); + ucs2[1] = (cpUcs2) (0xDC00 | (y & 0x03FF)); + len -= 2; + ucs2 += 2; + i++; + } + } + } + while (0 < len) { + *ucs2++ = 0; + len--; + } + + utf8IteratorUninit (&iter); + if (0 > ucs4) return BSTR_ERR; + return i; +} + +/* + +Unicode UTF-8 +------- ----- +U-00000000 - U-0000007F: 0xxxxxxx +U-00000080 - U-000007FF: 110xxxxx 10xxxxxx +U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx +U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + +U-00200000 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx +U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx + +UTF-32: U-000000 - U-10FFFF + +*/ + +/* int buAppendBlkUcs4 (bstring b, const cpUcs4* bu, int len, cpUcs4 errCh) + * + * Convert an array of UCS4 code points (bu) to UTF8 codepoints b. Any + * invalid code point is replaced by errCh. If errCh is itself not a + * valid code point, then this translation will halt upon the first error + * and return BSTR_ERR. Otherwise BSTR_OK is returned. + */ +int buAppendBlkUcs4 (bstring b, const cpUcs4* bu, int len, cpUcs4 errCh) { +int i, oldSlen; + + if (NULL == bu || NULL == b || 0 > len || 0 > (oldSlen = blengthe (b, -1))) return BSTR_ERR; + if (!isLegalUnicodeCodePoint (errCh)) errCh = ~0; + + for (i=0; i < len; i++) { + unsigned char c[6]; + cpUcs4 v = bu[i]; + + if (!isLegalUnicodeCodePoint (v)) { + if (~0 == errCh) { + b->slen = oldSlen; + return BSTR_ERR; + } + v = errCh; + } + + if (v < 0x80) { + if (BSTR_OK != bconchar (b, (char) v)) { + b->slen = oldSlen; + return BSTR_ERR; + } + } else if (v < 0x800) { + c[0] = (unsigned char) ( (v >> 6) + 0xc0); + c[1] = (unsigned char) (( v & 0x3f) + 0x80); + if (BSTR_OK != bcatblk (b, c, 2)) { + b->slen = oldSlen; + return BSTR_ERR; + } + } else if (v < 0x10000) { + c[0] = (unsigned char) ( (v >> 12) + 0xe0); + c[1] = (unsigned char) (((v >> 6) & 0x3f) + 0x80); + c[2] = (unsigned char) (( v & 0x3f) + 0x80); + if (BSTR_OK != bcatblk (b, c, 3)) { + b->slen = oldSlen; + return BSTR_ERR; + } + } else +#if 0 + if (v < 0x200000) +#endif + { + c[0] = (unsigned char) ( (v >> 18) + 0xf0); + c[1] = (unsigned char) (((v >> 12) & 0x3f) + 0x80); + c[2] = (unsigned char) (((v >> 6) & 0x3f) + 0x80); + c[3] = (unsigned char) (( v & 0x3f) + 0x80); + if (BSTR_OK != bcatblk (b, c, 4)) { + b->slen = oldSlen; + return BSTR_ERR; + } + } +#if 0 + else if (v < 0x4000000) { + c[0] = (unsigned char) ( (v >> 24) + 0xf8); + c[1] = (unsigned char) (((v >> 18) & 0x3f) + 0x80); + c[2] = (unsigned char) (((v >> 12) & 0x3f) + 0x80); + c[3] = (unsigned char) (((v >> 6) & 0x3f) + 0x80); + c[4] = (unsigned char) (( v & 0x3f) + 0x80); + if (BSTR_OK != bcatblk (b, c, 5)) { + b->slen = oldSlen; + return BSTR_ERR; + } + } else { + c[0] = (unsigned char) ( (v >> 30) + 0xfc); + c[1] = (unsigned char) (((v >> 24) & 0x3f) + 0x80); + c[2] = (unsigned char) (((v >> 18) & 0x3f) + 0x80); + c[3] = (unsigned char) (((v >> 12) & 0x3f) + 0x80); + c[4] = (unsigned char) (((v >> 6) & 0x3f) + 0x80); + c[5] = (unsigned char) (( v & 0x3f) + 0x80); + if (BSTR_OK != bcatblk (b, c, 6)) { + b->slen = oldSlen; + return BSTR_ERR; + } + } +#endif + } + return BSTR_OK; +} + +#define endSwap(cs,mode) ((mode) ? ((((cs) & 0xFF) << 8) | (((cs) >> 8) & 0xFF)) : (cs)) +#define TEMP_UCS4_BUFFER_SIZE (64) + +/* int buAppendBlkUTF16 (bstring bu, const cpUcs2* utf16, int len, + * cpUcs2* bom, cpUcs4 errCh) + * + * Append an array of UCS2 code points (utf16) to UTF8 codepoints (bu). Any + * invalid code point is replaced by errCh. If errCh is itself not a + * valid code point, then this translation will halt upon the first error + * and return BSTR_ERR. Otherwise BSTR_OK is returned. If a byte order mark + * has been previously read, it may be passed in as bom, otherwise if *bom is + * set to 0, it will be filled in with the BOM as read from the first + * character if it is a BOM. + */ +int buAppendBlkUTF16 (bstring bu, const cpUcs2* utf16, int len, cpUcs2* bom, cpUcs4 errCh) { +cpUcs4 buff[TEMP_UCS4_BUFFER_SIZE]; +int cc, i, sm, oldSlen; + + if (NULL == bdata(bu) || NULL == utf16 || len < 0) return BSTR_ERR; + if (!isLegalUnicodeCodePoint (errCh)) errCh = ~0; + if (len == 0) return BSTR_OK; + + oldSlen = bu->slen; + i = 0; + + /* Check for BOM character and select endianess. Also remove the + BOM from the stream, since there is no need for it in a UTF-8 encoding. */ + if (bom && (cpUcs2) 0xFFFE == *bom) { + sm = 8; + } else if (bom && (cpUcs2) 0xFEFF == *bom) { + sm = 0; + } else if (utf16[i] == (cpUcs2) 0xFFFE) { + if (bom) *bom = utf16[i]; + sm = 8; + i++; + } else if (utf16[i] == (cpUcs2) 0xFEFF) { + if (bom) *bom = utf16[i]; + sm = 0; + i++; + } else { + sm = 0; /* Assume local endianness. */ + } + + cc = 0; + for (;i < len; i++) { + cpUcs4 c, v; + v = endSwap (utf16[i], sm); + + if ((v | 0x7FF) == 0xDFFF) { /* Deal with surrogate pairs */ + if (v >= 0xDC00 || i >= len) { + ErrMode:; + if (~0 == errCh) { + ErrReturn:; + bu->slen = oldSlen; + return BSTR_ERR; + } + v = errCh; + } else { + i++; + if ((c = endSwap (utf16[i], sm) - 0xDC00) > 0x3FF) goto ErrMode; + v = ((v - 0xD800) << 10) + c + 0x10000; + } + } + buff[cc] = v; + cc++; + if (cc >= TEMP_UCS4_BUFFER_SIZE) { + if (0 > buAppendBlkUcs4 (bu, buff, cc, errCh)) goto ErrReturn; + cc = 0; + } + } + if (cc > 0 && 0 > buAppendBlkUcs4 (bu, buff, cc, errCh)) goto ErrReturn; + + return BSTR_OK; +} diff --git a/bstrlib/buniutil.h b/bstrlib/buniutil.h new file mode 100644 index 0000000..1017212 --- /dev/null +++ b/bstrlib/buniutil.h @@ -0,0 +1,37 @@ +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2015, and is covered by the BSD open source + * license and the GPL. Refer to the accompanying documentation for details + * on usage and license. + */ + +/* + * buniutil.h + * + * This file is the interface for the buniutil basic "Unicode for bstrings" + * functions. Note that there are dependencies on bstrlib.h and utf8util.h . + */ + +#ifndef BSTRLIB_UNICODE_UTILITIES +#define BSTRLIB_UNICODE_UTILITIES + +#include "utf8util.h" +#include "bstrlib.h" + +#ifdef __cplusplus +extern "C" { +#endif + +extern int buIsUTF8Content (const_bstring bu); +extern int buAppendBlkUcs4 (bstring b, const cpUcs4* bu, int len, cpUcs4 errCh); + +/* For those unfortunate enough to be stuck supporting UTF16. */ +extern int buGetBlkUTF16 (/* @out */ cpUcs2* ucs2, int len, cpUcs4 errCh, const_bstring bu, int pos); +extern int buAppendBlkUTF16 (bstring bu, const cpUcs2* utf16, int len, cpUcs2* bom, cpUcs4 errCh); + +#ifdef __cplusplus +} +#endif + +#endif /* BSTRLIB_UNICODE_UTILITIES */ + diff --git a/bstrlib/gpl.txt b/bstrlib/gpl.txt new file mode 100644 index 0000000..d511905 --- /dev/null +++ b/bstrlib/gpl.txt @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/bstrlib/porting.txt b/bstrlib/porting.txt new file mode 100644 index 0000000..11d8d13 --- /dev/null +++ b/bstrlib/porting.txt @@ -0,0 +1,172 @@ +Better String library Porting Guide +----------------------------------- + +by Paul Hsieh + +The bstring library is an attempt to provide improved string processing +functionality to the C and C++ language. At the heart of the bstring library +is the management of "bstring"s which are a significant improvement over '\0' +terminated char buffers. See the accompanying documenation file bstrlib.txt +for more information. + +=============================================================================== + +Identifying the Compiler +------------------------ + +Bstrlib has been tested on the following compilers: + + Microsoft Visual C++ + Watcom C/C++ (32 bit flat) + Intel's C/C++ compiler (on Windows) + The GNU C/C++ compiler (on Windows/Linux on x86 and PPC64) + Borland C++ + Turbo C + +There are slight differences in these compilers which requires slight +differences in the implementation of Bstrlib. These are accomodated in the +same sources using #ifdef/#if defined() on compiler specific macros. To +port Bstrlib to a new compiler not listed above, it is recommended that the +same strategy be followed. If you are unaware of the compiler specific +identifying preprocessor macro for your compiler you might find it here: + +http://predef.sourceforge.net/precomp.html + +Note that Intel C/C++ on Windows sets the Microsoft identifier: _MSC_VER. + +16-bit vs. 32-bit vs. 64-bit Systems +------------------------------------ + +Bstrlib has been architected to deal with strings of length between 0 and +INT_MAX (inclusive). Since the values of int are never higher than size_t +there will be no issue here. Note that on most 64-bit systems int is 32-bit. + +Dependency on The C-Library +--------------------------- + +Bstrlib uses the functions memcpy, memmove, malloc, realloc, free and +vsnprintf. Many free standing C compiler implementations that have a mode in +which the C library is not available will typically not include these +functions which will make porting Bstrlib to it onerous. Bstrlib is not +designed for such bare bones compiler environments. This usually includes +compilers that target ROM environments. + +Porting Issues +-------------- + +Bstrlib has been written completely in ANSI/ISO C and ISO C++, however, there +are still a few porting issues. These are described below. + +1. The vsnprintf () function. + +Unfortunately, the earlier ANSI/ISO C standards did not include this function. +If the compiler of interest does not support this function then the +BSTRLIB_NOVSNP should be defined via something like: + + #if !defined (BSTRLIB_VSNP_OK) && !defined (BSTRLIB_NOVSNP) + # if defined (__TURBOC__) || defined (__COMPILERVENDORSPECIFICMACRO__) + # define BSTRLIB_NOVSNP + # endif + #endif + +which appears at the top of bstrlib.h. Note that the bformat(a) functions +will not be declared or implemented if the BSTRLIB_NOVSNP macro is set. If +the compiler has renamed vsnprintf() to some other named function, then +search for the definition of the exvsnprintf macro in bstrlib.c file and be +sure its defined appropriately: + + #if defined (__COMPILERVENDORSPECIFICMACRO__) + # define exvsnprintf(r,b,n,f,a) {r=__compiler_specific_vsnprintf(b,n,f,a);} + #else + # define exvsnprintf(r,b,n,f,a) {r=vsnprintf(b,n,f,a);} + #endif + +Take notice of the return value being captured in the variable r. It is +assumed that r exceeds n if and only if the underlying vsnprintf function has +determined what the true maximal output length would be for output if the +buffer were large enough to hold it. Non-modern implementations must output a +lesser number (the macro can and should be modified to ensure this). + +2. Weak C++ compiler. + +C++ is a much more complicated language to implement than C. This has lead +to varying quality of compiler implementations. The weaknesses isolated in +the initial ports are inclusion of the Standard Template Library, +std::iostream and exception handling. By default it is assumed that the C++ +compiler supports all of these things correctly. If your compiler does not +support one or more of these define the corresponding macro: + + BSTRLIB_CANNOT_USE_STL + BSTRLIB_CANNOT_USE_IOSTREAM + BSTRLIB_DOESNT_THROW_EXCEPTIONS + +The compiler specific detected macro should be defined at the top of +bstrwrap.h in the Configuration defines section. Note that these disabling +macros can be overrided with the associated enabling macro if a subsequent +version of the compiler gains support. (For example, its possible to rig +up STLport to provide STL support for WATCOM C/C++, so -DBSTRLIB_CAN_USE_STL +can be passed in as a compiler option.) + +3. The bsafe module, and reserved words. + +The bsafe module is in gross violation of the ANSI/ISO C standard in the +sense that it redefines what could be implemented as reserved words on a +given compiler. The typical problem is that a compiler may inline some of the +functions and thus not be properly overridden by the definitions in the bsafe +module. It is also possible that a compiler may prohibit the redefinitions in +the bsafe module. Compiler specific action will be required to deal with +these situations. + +Platform Specific Files +----------------------- + +The makefiles for the examples are basically setup of for particular +environments for each platform. In general these makefiles are not portable +and should be constructed as necessary from scratch for each platform. + +Testing a port +-------------- + +To test that a port compiles correctly do the following: + +1. Build a sample project that includes the bstrlib, bstraux, bstrwrap, and + bsafe modules. +2. Compile bstest against the bstrlib module. +3. Run bstest and ensure that 0 errors are reported. +4. Compile test against the bstrlib and bstrwrap modules. +5. Run test and ensure that 0 errors are reported. +6. Compile each of the examples (except for the "re" example, which may be + complicated and is not a real test of bstrlib and except for the mfcbench + example which is Windows specific.) +7. Run each of the examples. + +The builds must have 0 errors, and should have the absolute minimum number of +warnings (in most cases can be reduced to 0.) The result of execution should +be essentially identical on each platform. + +Performance +----------- + +Different CPU and compilers have different capabilities in terms of +performance. It is possible for Bstrlib to assume performance +characteristics that a platform doesn't have (since it was primarily +developed on just one platform). The goal of Bstrlib is to provide very good +performance on all platforms regardless of this but without resorting to +extreme measures (such as using assembly language, or non-portable intrinsics +or library extensions.) + +There are two performance benchmarks that can be found in the example/ +directory. They are: cbench.c and cppbench.cpp. These are variations and +expansions of a benchmark for another string library. They don't cover all +string functionality, but do include the most basic functions which will be +common in most string manipulation kernels. + +............................................................................... + +Feedback +-------- + +In all cases, you may email issues found to the primary author of Bstrlib at +the email address: websnarf@users.sourceforge.net + +=============================================================================== diff --git a/bstrlib/security.txt b/bstrlib/security.txt new file mode 100644 index 0000000..d3f5c90 --- /dev/null +++ b/bstrlib/security.txt @@ -0,0 +1,217 @@ +Better String library Security Statement +---------------------------------------- + +by Paul Hsieh + +=============================================================================== + +Introduction +------------ + +The Better String library (hereafter referred to as Bstrlib) is an attempt to +provide improved string processing functionality to the C and C++ languages. +At the heart of the Bstrlib is the management of "bstring"s which are a +significant improvement over '\0' terminated char buffers. See the +accompanying documenation file bstrlib.txt for more information. + +DISCLAIMER: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT +NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Like any software, there is always a possibility of failure due to a flawed +implementation. Nevertheless a good faith effort has been made to minimize +such flaws in Bstrlib. Use of Bstrlib by itself will not make an application +secure or free from implementation failures, however, it is the author's +conviction that use of Bstrlib can greatly facilitate the creation of +software meeting the highest possible standards of security. + +Part of the reason why this document has been created, is for the purpose of +security auditing, or the creation of further "Statements on Security" for +software that is created that uses Bstrlib. An auditor may check the claims +below against Bstrlib, and use this as a basis for analysis of software which +uses Bstrlib. + +=============================================================================== + +Statement on Security +--------------------- + +This is a document intended to give consumers of the Better String Library +who are interested in security an idea of where the Better String Library +stands on various security issues. Any deviation observed in the actual +library itself from the descriptions below should be considered an +implementation error, not a design flaw. + +This statement is not an analytical proof of correctness or an outline of one +but rather an assertion similar to a scientific claim or hypothesis. By use, +testing and open independent examination (otherwise known as scientific +falsifiability), the credibility of the claims made below can rise to the +level of an established theory. + +Common security issues: +....................... + +1. Buffer Overflows + +The Bstrlib API allows the programmer a way to deal with strings without +having to deal with the buffers containing them. Ordinary usage of the +Bstrlib API itself makes buffer overflows impossible. + +Furthermore, the Bstrlib API has a superset of basic string functionality as +compared to the C library's char * functions, C++'s std::string class and +Microsoft's MFC based CString class. It also has abstracted mechanisms for +dealing with IO. This is important as it gives developers a way of migrating +all their code from a functionality point of view. + +2. Memory size overflow/wrap around attack + +By design, Bstrlib is impervious to memory size overflow attacks. The +reason is that it detects length overflows and leads to a result error before +the operation attempts to proceed. Attempted conversions of char* strings +which may have lengths greater than INT_MAX are detected and the conversion +is aborted. If the memory to hold the string exceeds the available memory +for it, again, the result is aborted without changing the prior state of the +strings. + +3. Constant string protection + +Bstrlib implements runtime enforced constant and read-only string semantics. +I.e., bstrings which are declared as constant via the bsStatic() macro cannot +be modified or deallocated directly through the Bstrlib API, and this cannot +be subverted by casting or other type coercion. This is independent of the +use of the const_bstring data type. + +The Bstrlib C API uses the type const_bstring to specify bstring parameters +whose contents do not change. Although the C language cannot enforce this, +this is nevertheless guaranteed by the implementation of the Bstrlib library +of C functions. The C++ API enforces the const attribute on CBString types +correctly. + +4. Aliased bstring support + +Bstrlib detects and supports aliased parameter management throughout the API. +The kind of aliasing that is allowed is the one where pointers of the same +basic type may be pointing to overlapping objects (this is the assumption the +ANSI C99 specification makes.) Each function behaves as if all read-only +parameters were copied to temporaries which are used in their stead before +the function is enacted (it rarely actually does this). No function in the +Bstrlib uses the "restrict" parameter attribute from the ANSI C99 +specification. + +5. Information leaking + +In bstraux.h, using the semantically equivalent macros bSecureDestroy() and +bSecureWriteProtect() in place of bdestroy() and bwriteprotect() respectively +will ensure that stale data does not linger in the heap's free space after +strings have been released back to memory. Created bstrings or CBStrings +are not linked to anything external to themselves, and thus cannot expose +deterministic data leaking. If a bstring is resized, the preimage may exist +as a copy that is released to the heap. Thus for sensitive data, the bstring +should be sufficiently presized before manipulated so that it is not resized. +bSecureInput() has been supplied in bstraux.c, which can be used to obtain +input securely without any risk of leaving any part of the input image in the +heap except for the allocated bstring that is returned. + +6. Memory leaking + +Bstrlib can be built using memdbg.h enabled via the BSTRLIB_MEMORY_DEBUG +macro. User generated definitions for malloc, realloc and free can then be +supplied which can implement special strategies for memory corruption +detection or memory leaking. Otherwise, bstrlib does not do anything out of +the ordinary to attempt to deal with the standard problem of memory leaking +(i.e., losing references to allocated memory) when programming in the C and +C++ languages. However, it does not compound the problem any more than exists +either, as it doesn't have any intrinsic inescapable leaks in it. Bstrlib +does not preclude the use of automatic garbage collection mechanisms such as +the Boehm garbage collector. + +7. Encryption + +Bstrlib does not present any built-in encryption mechanism. However, it +supports full binary contents in its data buffers, so any standard block +based encryption mechanism can make direct use of bstrings/CBStrings for +buffer management. + +8. Double freeing + +Freeing a pointer that is already free is an extremely rare, but nevertheless +a potentially ruthlessly corrupting operation (its possible to cause Win 98 to +reboot, by calling free mulitiple times on already freed data using the WATCOM +CRT.) Bstrlib invalidates the bstring header data before freeing, so that in +many cases a double free will be detected and an error will be reported +(though this behaviour is not guaranteed and should not be relied on). + +Using bstrFree pervasively (instead of bdestroy) can lead to somewhat +improved invalid free avoidance (it is completely safe whenever bstring +instances are only stored in unique variables). For example: + + struct tagbstring hw = bsStatic ("Hello, world"); + bstring cpHw = bstrcpy (&hw); + + #ifdef NOT_QUITE_AS_SAFE + bdestroy (cpHw); /* Never fail */ + bdestroy (cpHw); /* Error sometimes detected at runtime */ + bdestroy (&hw); /* Error detected at run time */ + #else + bstrFree (cpHw); /* Never fail */ + bstrFree (cpHw); /* Will do nothing */ + bstrFree (&hw); /* Will lead to a compile time error */ + #endif + +9. Resource based denial of service + +bSecureInput() has been supplied in bstraux.c. It has an optional upper limit +for input length. But unlike fgets(), it is also easily determined if the +buffer has been truncated early. In this way, a program can set an upper +limit on input sizes while still allowing for implementing context specific +truncation semantics (i.e., does the program consume but dump the extra +input, or does it consume it in later inputs?) + +10. Mixing char *'s and bstrings + +The bstring and char * representations are not identical. So there is a risk +when converting back and forth that data may lost. Essentially bstrings can +contain '\0' as a valid non-terminating character, while char * strings +cannot and in fact must use the character as a terminator. The risk of data +loss is very low, since: + + A) the simple method of only using bstrings in a char * semantically + compatible way is both easy to achieve and pervasively supported. + B) obtaining '\0' content in a string is either deliberate or indicative + of another, likely more serious problem in the code. + C) the library comes with various functions which deal with this issue + (namely: bfromcstr(), bstr2cstr (), and bSetCstrChar ()) + +Marginal security issues: +......................... + +11. 8-bit versus 9-bit portability + +Bstrlib uses CHAR_BIT and other limits.h constants to the maximum extent +possible to avoid portability problems. However, Bstrlib has not been tested +on any system that does not represent char as 8-bits. So whether or not it +works on 9-bit systems is an open question. It is recommended that Bstrlib be +carefully auditted by anyone using a system in which CHAR_BIT is not 8. + +12. EBCDIC/ASCII/UTF-8 data representation attacks. + +Bstrlib uses ctype.h functions to ensure that it remains portable to non- +ASCII systems. It also checks range to make sure it is well defined even for +data that ANSI does not define for the ctype functions. + +Obscure issues: +............... + +13. Data attributes + +There is no support for a Perl-like "taint" attribute, although this is a +fairly straightforward exercise using C++'s type system. + diff --git a/bstrlib/test.cpp b/bstrlib/test.cpp new file mode 100644 index 0000000..9503342 --- /dev/null +++ b/bstrlib/test.cpp @@ -0,0 +1,1725 @@ +// +// This source file is part of the bstring string library. This code was +// written by Paul Hsieh in 2002-2015, and is covered by the BSD open source +// license. Refer to the accompanying documentation for details on usage and +// license. +// + +// +// test.cpp +// +// This file is the C++ unit test for Bstrlib +// + +#if defined (_MSC_VER) +# define _CRT_SECURE_NO_WARNINGS +#endif + +#include +#include +#include "bstrlib.h" +#include "bstrwrap.h" + +// Exceptions must be turned on in the compiler to successfully run +// this test. The compiler must also support STL. + +#define dumpOutQty (32) +static bstring dumpOut[dumpOutQty]; +static unsigned int rot = 0; + +const char * dumpBstring (const bstring b) { + rot = (rot + 1) % (unsigned) dumpOutQty; + if (dumpOut[rot] == NULL) { + dumpOut[rot] = bfromcstr (""); + if (dumpOut[rot] == NULL) return "FATAL INTERNAL ERROR"; + } + dumpOut[rot]->slen = 0; + if (b == NULL) { + bcatcstr (dumpOut[rot], "NULL"); + } else { + char msg[32]; + sprintf (msg, "%p", (void *)b); + bcatcstr (dumpOut[rot], msg); + + if (b->slen < 0) { + sprintf (msg, ":[err:slen=%d<0]", b->slen); + bcatcstr (dumpOut[rot], msg); + } else { + if (b->mlen > 0 && b->mlen < b->slen) { + sprintf (msg, ":[err:mlen=%dmlen, b->slen); + bcatcstr (dumpOut[rot], msg); + } else { + if (b->mlen == -1) { + bcatcstr (dumpOut[rot], "[p]"); + } else if (b->mlen < 0) { + bcatcstr (dumpOut[rot], "[c]"); + } + bcatcstr (dumpOut[rot], ":"); + if (b->data == NULL) { + bcatcstr (dumpOut[rot], "[err:data=NULL]"); + } else { + bcatcstr (dumpOut[rot], "\""); + bcatcstr (dumpOut[rot], (const char *) b->data); + bcatcstr (dumpOut[rot], "\""); + } + } + } + } + return (const char *) dumpOut[rot]->data; +} + +int test0 (void) { +int ret = 0; + + printf ("TEST: CBString constructor\n"); + + try { + printf ("\tCBString c;\n"); + CBString c0; + ret += (0 != c0.length()); + ret += '\0' != ((const char *)c0)[c0.length()]; + + printf ("\tCBString c(\"test\");\n"); + CBString c1 ("test"); + ret += (c1 != "test"); + ret += '\0' != ((const char *)c1)[c1.length()]; + + printf ("\tCBString c(25, \"test\");\n"); + CBString c8 (25, "test"); + ret += (c8 != "test"); + ret += c8.mlen < 25; + ret += '\0' != ((const char *)c8)[c8.length()]; + + printf ("\tCBString c('t');\n"); + CBString c2 ('t'); + ret += (c2 != "t"); + ret += '\0' != ((const char *)c2)[c2.length()]; + + printf ("\tCBString c('\\0');\n"); + CBString c3 ('\0'); + ret += (1 != c3.length()) || ('\0' != c3[0]); + ret += '\0' != ((const char *)c3)[c3.length()]; + + printf ("\tCBString c(bstr[\"test\"]);\n"); + struct tagbstring t = bsStatic ("test"); + CBString c4 (t); + ret += (c4 != t.data); + ret += '\0' != ((const char *)c4)[c4.length()]; + + printf ("\tCBString c(CBstr[\"test\"]);\n"); + CBString c5 (c1); + ret += (c1 != c5); + ret += '\0' != ((const char *)c5)[c5.length()]; + + printf ("\tCBString c('x',5);\n"); + CBString c6 ('x',5); + ret += (c6 != "xxxxx"); + ret += '\0' != ((const char *)c6)[c6.length()]; + + printf ("\tCBString c(\"123456\",4);\n"); + CBString c7 ((void *)"123456",4); + ret += (c7 != "1234"); + ret += '\0' != ((const char *)c7)[c7.length()]; + } + + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +#define EXCEPTION_EXPECTED(line) \ + try { \ + line; \ + ret++; \ + printf ("\tException was expected\n"); \ + } \ + catch (struct CBStringException) { } + +int test1 (void) { +int ret = 0; + + printf ("TEST: CBString = operator\n"); + + try { + CBString c0; + struct tagbstring t = bsStatic ("test"); + + ret += c0.iswriteprotected(); + c0.writeprotect (); + ret += 1 != c0.iswriteprotected(); + EXCEPTION_EXPECTED (c0 = 'x'); + EXCEPTION_EXPECTED (c0 = (unsigned char) 'x'); + EXCEPTION_EXPECTED (c0 = "test"); + EXCEPTION_EXPECTED (c0 = CBString ("test")); + EXCEPTION_EXPECTED (c0 = t); + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0, c1; + struct tagbstring t = bsStatic ("test"); + + printf ("\tc = 'x';\n"); + c0 = 'x'; + ret += (c0 != "x"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\tc = (unsigned char)'x';\n"); + c0 = (unsigned char) 'x'; + ret += (c0 != "x"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\tc = \"test\";\n"); + c0 = "test"; + ret += (c0 != "test"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\tc = CBStr[\"test\"];\n"); + c1 = c0; + ret += (c0 != c1); + ret += '\0' != ((const char *)c1)[c1.length()]; + printf ("\tc = tbstr[\"test\"];\n"); + c0 = t; + ret += (c0 != "test"); + ret += '\0' != ((const char *)c0)[c0.length()]; + } + + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test2 (void) { +int ret = 0; + + printf ("TEST: CBString += operator\n"); + + try { + CBString c0; + struct tagbstring t = bsStatic ("test"); + + c0.writeprotect (); + EXCEPTION_EXPECTED (c0 += 'x'); + EXCEPTION_EXPECTED (c0 += (unsigned char) 'x'); + EXCEPTION_EXPECTED (c0 += "test"); + EXCEPTION_EXPECTED (c0 += CBString ("test")); + EXCEPTION_EXPECTED (c0 += t); + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0; + struct tagbstring t = bsStatic ("extra"); + + c0 = "test"; + printf ("\tc += 'x';\n"); + c0 += 'x'; + ret += (c0 != "testx"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\tc += (unsigned char)'x';\n"); + c0 += (unsigned char) 'y'; + ret += (c0 != "testxy"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\tc += \"test\";\n"); + c0 += "test"; + ret += (c0 != "testxytest"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\tc += CBStr[\"test\"];\n"); + c0 += CBString (c0); + ret += (c0 != "testxytesttestxytest"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\tc += tbstr[\"test\"];\n"); + c0 += t; + ret += (c0 != "testxytesttestxytestextra"); + ret += '\0' != ((const char *)c0)[c0.length()]; + } + + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test3 (void) { +int ret = 0; + + try { + CBString c0, c1; + struct tagbstring t = bsStatic ("extra"); + + printf ("TEST: CBString + operator\n"); + + c1 = "test"; + printf ("\tc + 'x';\n"); + c0 = c1 + 'x'; + ret += (c0 != "testx"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\tc + (unsigned char)'x';\n"); + c0 = c1 + (unsigned char) 'y'; + ret += (c0 != "testy"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\tc + \"test\";\n"); + c0 = c1 + (const char *) "stuff"; + ret += (c0 != "teststuff"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\tc + (unsigned char *) \"test\";\n"); + c0 = c1 + (const unsigned char *) "stuff"; + ret += (c0 != "teststuff"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\tc + CBStr[\"test\"];\n"); + c0 = c1 + CBString ("other"); + ret += (c0 != "testother"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\tc + tbstr[\"test\"];\n"); + c0 = c1 + t; + ret += (c0 != "testextra"); + ret += '\0' != ((const char *)c0)[c0.length()]; + + printf ("TEST: + CBString operator\n"); + + printf ("\t'x' + c;\n"); + c0 = 'x' + c1; + ret += (c0 != "xtest"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\t(unsigned char)'y' + c;\n"); + c0 = (unsigned char) 'y' + c1; + ret += (c0 != "ytest"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\t\"test\" + c;\n"); + c0 = (const char *) "stuff" + c1; + ret += (c0 != "stufftest"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\t(unsigned char *) \"test\" + c;\n"); + c0 = (const unsigned char *) "stuff" + c1; + ret += (c0 != "stufftest"); + ret += '\0' != ((const char *)c0)[c0.length()]; + printf ("\ttbstr[\"extra\"] + c;\n"); + c0 = t + c1; + ret += (c0 != "extratest"); + ret += '\0' != ((const char *)c0)[c0.length()]; + } + + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test4 (void) { +int ret = 0; + + try { + printf ("TEST: CBString == operator\n"); + + CBString c0, c1, c2; + + c0 = c1 = "test"; + c2 = "other"; + + printf ("\tc == d;\n"); + ret += !(c0 == c1); + ret += (c0 == c2); + + printf ("\tc == \"test\";\n"); + ret += !(c0 == "test"); + ret += (c2 == "test"); + + printf ("\tc == (unsigned char *) \"test\";\n"); + ret += !(c0 == (unsigned char *) "test"); + ret += (c2 == (unsigned char *) "test"); + } + + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test5 (void) { +int ret = 0; + + try { + printf ("TEST: CBString != operator\n"); + + CBString c0, c1, c2; + + c0 = c1 = "test"; + c2 = "other"; + + printf ("\tc != d;\n"); + ret += (c0 != c1); + ret += !(c0 != c2); + + printf ("\tc != \"test\";\n"); + ret += (c0 != "test"); + ret += !(c2 != "test"); + + printf ("\tc != (unsigned char *) \"test\";\n"); + ret += (c0 != (unsigned char *) "test"); + ret += !(c2 != (unsigned char *) "test"); + } + + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test6 (void) { +int ret = 0; + + try { + printf ("TEST: CBString <, <= operators\n"); + + CBString c0, c1, c2; + + c0 = c1 = "test"; + c2 = "other"; + + printf ("\tc < d;\n"); + ret += (c0 < c1); + ret += (c0 < c2); + ret += (c1 < c0); + ret += !(c2 < c0); + + printf ("\tc <= d;\n"); + ret += !(c0 <= c1); + ret += (c0 <= c2); + ret += !(c1 <= c0); + ret += !(c2 <= c0); + + printf ("\tc < \"test\";\n"); + ret += (c0 < "test"); + ret += (c1 < "test"); + ret += !(c2 < "test"); + ret += (c0 < "other"); + ret += (c1 < "other"); + ret += (c2 < "other"); + + printf ("\tc <= \"test\";\n"); + ret += !(c0 <= "test"); + ret += !(c1 <= "test"); + ret += !(c2 <= "test"); + ret += (c0 <= "other"); + ret += (c1 <= "other"); + ret += !(c2 <= "other"); + + printf ("\tc < (unsigned char *) \"test\";\n"); + ret += (c0 < (const char *) "test"); + ret += (c1 < (const char *) "test"); + ret += !(c2 < (const char *) "test"); + ret += (c0 < (const char *) "other"); + ret += (c1 < (const char *) "other"); + ret += (c2 < (const char *) "other"); + + printf ("\tc <= (unsigned char *) \"test\";\n"); + ret += !(c0 <= (const char *) "test"); + ret += !(c1 <= (const char *) "test"); + ret += !(c2 <= (const char *) "test"); + ret += (c0 <= (const char *) "other"); + ret += (c1 <= (const char *) "other"); + ret += !(c2 <= (const char *) "other"); + } + + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test7 (void) { +int ret = 0; + + try { + printf ("TEST: CBString >, >= operators\n"); + + CBString c0, c1, c2; + + c0 = c1 = "test"; + c2 = "other"; + + printf ("\tc >= d;\n"); + ret += !(c0 >= c1); + ret += !(c0 >= c2); + ret += !(c1 >= c0); + ret += (c2 >= c0); + + printf ("\tc > d;\n"); + ret += (c0 > c1); + ret += !(c0 > c2); + ret += (c1 > c0); + ret += (c2 > c0); + + printf ("\tc >= \"test\";\n"); + ret += !(c0 >= "test"); + ret += !(c1 >= "test"); + ret += (c2 >= "test"); + ret += !(c0 >= "other"); + ret += !(c1 >= "other"); + ret += !(c2 >= "other"); + + printf ("\tc > \"test\";\n"); + ret += (c0 > "test"); + ret += (c1 > "test"); + ret += (c2 > "test"); + ret += !(c0 > "other"); + ret += !(c1 > "other"); + ret += (c2 > "other"); + + printf ("\tc >= (unsigned char *) \"test\";\n"); + ret += !(c0 >= (const char *) "test"); + ret += !(c1 >= (const char *) "test"); + ret += (c2 >= (const char *) "test"); + ret += !(c0 >= (const char *) "other"); + ret += !(c1 >= (const char *) "other"); + ret += !(c2 >= (const char *) "other"); + + printf ("\tc > (unsigned char *) \"test\";\n"); + ret += (c0 > (const char *) "test"); + ret += (c1 > (const char *) "test"); + ret += (c2 > (const char *) "test"); + ret += !(c0 > (const char *) "other"); + ret += !(c1 > (const char *) "other"); + ret += (c2 > (const char *) "other"); + } + + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test8 (void) { +int ret = 0; + + try { + printf ("TEST: (const char *) CBString operator\n"); + + CBString c0 ("test"), c1 ("other"); + + printf ("\t(const char *) CBString\n"); + ret += 0 != memcmp ((const char *) c0, "test", 5); + ret += 0 != memcmp ((const char *) c1, "other", 6); + + printf ("\t(const unsigned char *) CBString\n"); + ret += 0 != memcmp ((const unsigned char *) c0, "test", 5); + ret += 0 != memcmp ((const unsigned char *) c1, "other", 6); + } + + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test9 (void) { +int ret = 0; + + try { + printf ("TEST: (double), (float), (int) CBString operators\n"); + CBString c0 ("1.2e3"), c1("100"), c2("100.55"); + printf ("\t(double) \"%s\"\n", (const char *) c0); + ret += 1.2e3 != (double) c0; + printf ("\t(float) \"%s\"\n", (const char *) c0); + ret += 1.2e3 != (float) c0; + printf ("\t(int) \"%s\"\n", (const char *) c1); + ret += 100 != (float) c1; + printf ("\t(int) \"%s\"\n", (const char *) c2); + ret += 100 != (int) c2; + printf ("\t(unsigned int) \"%s\"\n", (const char *) c2); + ret += 100 != (unsigned int) c2; + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0 ("xxxxx"); + printf ("\t(double) \"%s\"\n", (const char *) c0); + ret += -1.2e3 != (double) c0; + } + catch (struct CBStringException err) { + printf ("\tException (%s) correctly thrown\n", err.what()); + } + + try { + CBString c0 ("xxxxx"); + printf ("\t(float) \"%s\"\n", (const char *) c0); + ret += -1.2e3 != (float) c0; + } + catch (struct CBStringException err) { + printf ("\tException (%s) correctly thrown\n", err.what()); + } + + try { + CBString c0 ("xxxxx"); + printf ("\t(int) \"%s\"\n", (const char *) c0); + ret += -100 != (int) c0; + } + catch (struct CBStringException err) { + printf ("\tException (%s) correctly thrown\n", err.what()); + } + + try { + CBString c0 ("xxxxx"); + printf ("\t(unsigned int) \"%s\"\n", (const char *) c0); + ret += 1000 != (unsigned int) c0; + } + catch (struct CBStringException err) { + printf ("\tException (%s) correctly thrown\n", err.what()); + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test10 (void) { +int ret = 0; + + try { + printf ("TEST: length() method\n"); + CBString c0, c1("Test"); + + printf ("\t\"%s\".length();\n", (const char *) c0); + ret += 0 != c0.length(); + printf ("\t\"%s\".length();\n", (const char *) c1); + ret += 4 != c1.length(); + } + + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test11 (void) { +int ret = 0; + + printf ("TEST: character() method, [] operator\n"); + + try { + CBString c0("test"); + c0.writeprotect (); + ret += c0[0] != 't'; + ret += (1 + c0[0]) != 'u'; + ret += ((unsigned char) c0[0] + 1) != 'u'; + ret += c0.character(0) != 't'; + EXCEPTION_EXPECTED (c0[0] = 'x'); + EXCEPTION_EXPECTED (c0.character(0) = 'x'); + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0("Test"); + + printf ("\t\"%s\".character ();\n", (const char *) c0); + ret += 's' != c0.character (2); + c0.character (2) = 'x'; + ret += c0 != "Text"; + + printf ("\t\"%s\"[];\n", (const char *) c0); + ret += 'T' != c0[0]; + c0[0] = 't'; + ret += c0 != "text"; + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0; + printf ("\t\"%s\".character ();\n", (const char *) c0); + ret += '?' != c0.character (0); + } + catch (struct CBStringException err) { + printf ("\tException (%s) correctly thrown\n", err.what()); + } + + try { + CBString c0; + printf ("\t\"%s\"[];\n", (const char *) c0); + ret += '?' != c0[0]; + } + catch (struct CBStringException err) { + printf ("\tException (%s) correctly thrown\n", err.what()); + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test12 (void) { +int ret = 0; + +#ifndef BSTRLIB_NOVSNP + printf ("TEST: format(), formata() methods\n"); + + try { + CBString c0; + + c0.writeprotect (); + EXCEPTION_EXPECTED (c0.format ("%s(%d)", "extra", 4)); + EXCEPTION_EXPECTED (c0.formata ("%s(%d)", "extra", 4)); + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0, c1("Test"), c2, c3; + + printf ("\tc.format (...);\n"); + c0.format ("%s(%d)", "extra", 4); + ret += c0 != "extra(4)"; + + c2 = c0 + c0 + c0 + c0; + c2 += c2; + c2.insert (0, "x"); + c3.format ("x%s%s%s%s%s%s%s%s", (const char *) c0, (const char *) c0 + , (const char *) c0, (const char *) c0 + , (const char *) c0, (const char *) c0 + , (const char *) c0, (const char *) c0); + ret += c2 != c3; + + printf ("\t\"%s\".formata (...);\n", (const char *) c1); + c1.formata ("%s(%d)", "extra", 4); + ret += c1 != "Testextra(4)"; + + c2 = c0 + c0 + c0 + c0; + c2 += c2; + c2.insert (0, "x"); + c3 = "x"; + c3.formata ("%s%s%s%s%s%s%s%s", (const char *) c0, (const char *) c0 + , (const char *) c0, (const char *) c0 + , (const char *) c0, (const char *) c0 + , (const char *) c0, (const char *) c0); + ret += c2 != c3; + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); +#endif + return ret; +} + +int test13 (void) { +int ret = 0; + + try { + printf ("TEST: find() method\n"); + CBString c0, c1("Test"); + + printf ("\t\"%s\".find (CBString());\n", (const char *) c0); + ret += -1 != c0.find (CBString("x")); + ret += 1 != c1.find (CBString("e")); + + printf ("\t\"%s\".find (char *);\n", (const char *) c0); + ret += -1 != c0.find ("x"); + ret += 1 != c1.find ("e"); + + ret += 8 != CBString ("sssssssssap").find ("sap"); + ret += 9 != CBString ("sssssssssap").find ("ap"); + ret += 9 != CBString ("sssssssssap").find ("ap", 3); + ret += 9 != CBString ("sssssssssap").find ("a"); + ret += 9 != CBString ("sssssssssap").find ("a", 3); + ret += -1 != CBString ("sssssssssap").find ("x"); + ret += -1 != CBString ("sssssssssap").find ("x", 3); + ret += -1 != CBString ("sssssssssap").find ("ax"); + ret += -1 != CBString ("sssssssssap").find ("ax", 3); + ret += -1 != CBString ("sssssssssap").find ("sax"); + ret += -1 != CBString ("sssssssssap").find ("sax", 1); + ret += 8 != CBString ("sssssssssap").find ("sap", 3); + ret += 9 != CBString ("ssssssssssap").find ("sap", 3); + ret += 0 != CBString ("sssssssssap").find ("s"); + ret += 3 != CBString ("sssssssssap").find ("s", 3); + ret += 9 != CBString ("sssssssssap").find ("a"); + ret += 9 != CBString ("sssssssssap").find ("a", 5); + ret += 8 != CBString ("sasasasasap").find ("sap"); + ret += 9 != CBString ("ssasasasasap").find ("sap"); + + printf ("\t\"%s\".find (char);\n", (const char *) c0); + ret += -1 != c0.find ('x'); + ret += 1 != c1.find ('e'); + + printf ("TEST: reversefind () method\n"); + printf ("\t\"%s\".reversefind (CBString());\n", (const char *) c0); + ret += -1 != c0.reversefind (CBString("x"), c0.length()); + ret += 1 != c1.reversefind (CBString("e"), c1.length()); + + printf ("\t\"%s\".reversefind (char *);\n", (const char *) c0); + ret += -1 != c0.reversefind ("x", c0.length()); + ret += 1 != c1.reversefind ("e", c1.length()); + + printf ("\t\"%s\".reversefind (char);\n", (const char *) c0); + ret += -1 != c0.reversefind ('x', c0.length()); + ret += 1 != c1.reversefind ('e', c1.length()); + + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test14 (void) { +int ret = 0; + + try { + printf ("TEST: findchr(), reversefindchr() methods\n"); + CBString c0, c1("Test"); + + printf ("\t\"%s\".findchr (CBString(\"abcdef\"));\n", (const char *) c0); + ret += -1 != c0.findchr (CBString ("abcdef")); + printf ("\t\"%s\".findchr (CBString(\"abcdef\"));\n", (const char *) c1); + ret += 1 != c1.findchr (CBString ("abcdef")); + printf ("\t\"%s\".findchr (\"abcdef\");\n", (const char *) c0); + ret += -1 != c0.findchr ("abcdef"); + printf ("\t\"%s\".findchr (\"abcdef\");\n", (const char *) c1); + ret += 1 != c1.findchr ("abcdef"); + + printf ("\t\"%s\".reversefindchr (CBString(\"abcdef\"));\n", (const char *) c0); + ret += -1 != c0.reversefindchr (CBString ("abcdef"), c0.length()); + printf ("\t\"%s\".reversefindchr (CBString(\"abcdef\"));\n", (const char *) c1); + ret += 1 != c1.reversefindchr (CBString ("abcdef"), c1.length()); + printf ("\t\"%s\".reversefindchr (\"abcdef\");\n", (const char *) c0); + ret += -1 != c0.reversefindchr ("abcdef", c0.length()); + printf ("\t\"%s\".reversefindchr (\"abcdef\");\n", (const char *) c1); + ret += 1 != c1.reversefindchr ("abcdef", c1.length()); + + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test15 (void) { +int ret = 0; + + try { + printf ("TEST: nfindchr(), nreversefindchr() methods\n"); + CBString c0, c1("Test"); + + printf ("\t\"%s\".nfindchr (CBString(\"abcdef\"));\n", (const char *) c0); + ret += -1 != c0.nfindchr (CBString ("abcdef")); + printf ("\t\"%s\".nfindchr (CBString(\"abcdef\"));\n", (const char *) c1); + ret += 0 != c1.nfindchr (CBString ("abcdef")); + printf ("\t\"%s\".nfindchr (\"abcdef\");\n", (const char *) c0); + ret += -1 != c0.nfindchr ("abcdef"); + printf ("\t\"%s\".nfindchr (\"abcdef\");\n", (const char *) c1); + ret += 0 != c1.nfindchr ("abcdef"); + + printf ("\t\"%s\".nreversefindchr (CBString(\"abcdef\"));\n", (const char *) c0); + ret += -1 != c0.nreversefindchr (CBString ("abcdef"), c0.length()); + printf ("\t\"%s\".nreversefindchr (CBString(\"abcdef\"));\n", (const char *) c1); + ret += 3 != c1.nreversefindchr (CBString ("abcdef"), c1.length()); + printf ("\t\"%s\".nreversefindchr (\"abcdef\");\n", (const char *) c0); + ret += -1 != c0.nreversefindchr ("abcdef", c0.length()); + printf ("\t\"%s\".nreversefindchr (\"abcdef\");\n", (const char *) c1); + ret += 3 != c1.nreversefindchr ("abcdef", c1.length()); + + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test16 (void) { +int ret = 0; + + printf ("TEST: midstr() method\n"); + + try { + CBString c0, c1("bogus"), c2; + + printf ("\t\"%s\".midstr (1,3)\n", (const char *) c0); + ret += (c2 = c0.midstr (1,3)) != ""; + ret += '\0' != ((const char *)c2)[c2.length ()]; + + printf ("\t\"%s\".midstr (1,3)\n", (const char *) c1); + ret += (c2 = c1.midstr (1,3)) != "ogu"; + ret += '\0' != ((const char *)c2)[c2.length ()]; + } + + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test17 (void) { +int ret = 0; + + printf ("TEST: fill() method\n"); + + try { + CBString c0; + + c0.writeprotect (); + EXCEPTION_EXPECTED (c0.fill (5, 'x')); + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0, c1("Test-test"); + + printf ("\t\"%s\".fill(5,'x')\n", (const char *) c0); + c0.fill (5, 'x'); + ret += c0 != "xxxxx"; + + printf ("\t\"%s\".fill(5,'x')\n", (const char *) c1); + c1.fill (5, 'x'); + ret += c1 != "xxxxx"; + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test18 (void) { +int ret = 0; + + printf ("TEST: alloc() method\n"); + + try { + CBString c0; + + c0.writeprotect (); + EXCEPTION_EXPECTED (c0.alloc (5)); + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0, c1("Test-test"); + + printf ("\t\"%s\".alloc(5)\n", (const char *) c0); + c0.alloc (5); + ret += c0 != ""; + + printf ("\t\"%s\".alloc(5)\n", (const char *) c1); + c1.alloc (5); + ret += c1 != "Test-test"; + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0; + + printf ("\t\"%s\".alloc(0)\n", (const char *) c0); + c0.alloc (0); + ret += c0 != "Error"; + } + catch (struct CBStringException err) { + printf ("\tException (%s) properly thrown\n", err.what()); + } + + try { + CBString c0; + + printf ("\t\"%s\".alloc(-1)\n", (const char *) c0); + c0.alloc (-1); + ret += c0 != "Error"; + } + catch (struct CBStringException err) { + printf ("\tException (%s) properly thrown\n", err.what()); + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test19 (void) { +int ret = 0; + + printf ("TEST: setsubstr() method\n"); + + try { + CBString c0("Test-test"); + + c0.writeprotect (); + EXCEPTION_EXPECTED (c0.setsubstr (4, "extra")); + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0, c1("Test-test"); + + printf ("\t\"%s\".setsubstr (4,\"extra\")\n", (const char *) c0); + c0.setsubstr (4, "extra"); + ret += c0 != " extra"; + printf ("\t\"%s\".setsubstr (4,\"extra\")\n", (const char *) c1); + c1.setsubstr (4, "extra"); + ret += c1 != "Testextra"; + + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0; + + printf ("\t\"%s\".setsubstr(-1,\"extra\")\n", (const char *) c0); + c0.setsubstr (-1, "extra"); + ret ++; + } + catch (struct CBStringException err) { + printf ("\tException (%s) properly thrown\n", err.what()); + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test20 (void) { +int ret = 0; + + printf ("TEST: insert() method\n"); + + try { + CBString c0("Test-test"); + + c0.writeprotect (); + EXCEPTION_EXPECTED (c0.insert (4, "extra")); + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0, c1("Test-test"); + + printf ("\t\"%s\".insert (4,\"extra\")\n", (const char *) c0); + c0.insert (4, "extra"); + ret += c0 != " extra"; + printf ("\t\"%s\".insert (4,\"extra\")\n", (const char *) c1); + c1.insert (4, "extra"); + ret += c1 != "Testextra-test"; + + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0; + + printf ("\t\"%s\".insert(-1,\"extra\")\n", (const char *) c0); + c0.insert (-1, "extra"); + ret ++; + } + catch (struct CBStringException err) { + printf ("\tException (%s) properly thrown\n", err.what()); + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test21 (void) { +int ret = 0; + + printf ("TEST: insertchrs() method\n"); + + try { + CBString c0("Test-test"); + + c0.writeprotect (); + EXCEPTION_EXPECTED (c0.insertchrs (4, 2, 'x')); + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0, c1("Test-test"); + + printf ("\t\"%s\".insertchrs (4,2,'x')\n", (const char *) c0); + c0.insertchrs (4, 2, 'x'); + ret += c0 != "xxxxxx"; + printf ("\t\"%s\".insertchrs (4,2,'x')\n", (const char *) c1); + c1.insertchrs (4, 2, 'x'); + ret += c1 != "Testxx-test"; + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0; + + printf ("\t\"%s\".insertchrs (-1,2,'x')\n", (const char *) c0); + c0.insertchrs (-1, 2, 'x'); + ret ++; + } + catch (struct CBStringException err) { + printf ("\tException (%s) properly thrown\n", err.what()); + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test22 (void) { +int ret = 0; + + printf ("TEST: replace() method\n"); + + try { + CBString c0("Test-test"); + + c0.writeprotect (); + EXCEPTION_EXPECTED (c0.replace (4, 2, "beef")); + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0, c1("Test-test"); + + printf ("\t\"%s\".replace (4,2,\"beef\")\n", (const char *) c0); + c0.replace (4, 2, CBString ("beef")); + ret += c0 != " beef"; + c0 = ""; + c0.replace (4, 2, "beef"); + ret += c0 != " beef"; + + printf ("\t\"%s\".replace (4,2,\"beef\")\n", (const char *) c1); + c1.replace (4, 2, CBString ("beef")); + ret += c1 != "Testbeefest"; + c1 = "Test-test"; + c1.replace (4, 2, "beef"); + ret += c1 != "Testbeefest"; + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test23 (void) { +int ret = 0; + + printf ("TEST: findreplace() method\n"); + + try { + CBString c0, c1("Test-test"); + + printf ("\t\"%s\".findreplace (\"est\",\"beef\")\n", (const char *) c0); + c0.findreplace ("est", "beef"); + ret += c0 != ""; + c0 = ""; + c0.findreplace (CBString ("est"), CBString ("beef")); + ret += c0 != ""; + + printf ("\t\"%s\".findreplace (\"est\",\"beef\")\n", (const char *) c1); + c1.findreplace ("est", "beef"); + ret += c1 != "Tbeef-tbeef"; + c1 = "Test-test"; + c1.findreplace (CBString ("est"), CBString ("beef")); + ret += c1 != "Tbeef-tbeef"; + + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0, c1("TeSt-tEsT"); + + printf ("\t\"%s\".findreplacecaseless (\"est\",\"beef\")\n", (const char *) c0); + c0.findreplacecaseless ("est", "beef"); + ret += c0 != ""; + c0 = ""; + c0.findreplacecaseless (CBString ("est"), CBString ("beef")); + ret += c0 != ""; + + printf ("\t\"%s\".findreplacecaseless (\"est\",\"beef\")\n", (const char *) c1); + c1.findreplacecaseless ("est", "beef"); + ret += c1 != "Tbeef-tbeef"; + c1 = "Test-test"; + c1.findreplacecaseless (CBString ("est"), CBString ("beef")); + ret += c1 != "Tbeef-tbeef"; + + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test24 (void) { +int ret = 0; + + printf ("TEST: remove() method\n"); + + try { + CBString c0, c1("Test-test"); + + printf ("\t\"%s\".remove (4,2)\n", (const char *) c0); + c0.remove (4, 2); + ret += c0 != ""; + + printf ("\t\"%s\".remove (4,2)\n", (const char *) c1); + c1.remove (4, 2); + ret += c1 != "Testest"; + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test25 (void) { +int ret = 0; + + printf ("TEST: trunc() method\n"); + + try { + CBString c0, c1("Test-test"); + + printf ("\t\"%s\".trunc (4)\n", (const char *) c0); + c0.trunc (4); + ret += c0 != ""; + + printf ("\t\"%s\".trunc (4)\n", (const char *) c1); + c1.trunc (4); + ret += c1 != "Test"; + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test26 (void) { +int ret = 0; + + printf ("TEST: repeat() method\n"); + + try { + CBString c0, c1("Test"); + + printf ("\t\"%s\".repeat (4)\n", (const char *) c0); + c0.repeat (4); + ret += c0 != ""; + + printf ("\t\"%s\".repeat (4)\n", (const char *) c1); + c1.repeat (4); + ret += c1 != "TestTestTestTest"; + c1 = "Test"; + c1.repeat (4); + ret += c1 != "TestTestTestTest"; + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test27 (void) { +int ret = 0; + + printf ("TEST: ltrim(), rtrim() methods\n"); + + try { + CBString c0, c1(" Test "), c2(" "); + + printf ("\t\"%s\".ltrim ()\n", (const char *) c0); + c0.ltrim (); + ret += c0 != ""; + c0 = ""; + c0.rtrim (); + ret += c0 != ""; + + printf ("\t\"%s\".ltrim ()\n", (const char *) c1); + c1.ltrim (); + ret += c1 != "Test "; + c1 = " Test "; + c1.rtrim (); + ret += c1 != " Test"; + + printf ("\t\"%s\".ltrim ()\n", (const char *) c2); + c2.ltrim (); + ret += c2 != ""; + c2 = " "; + c2.rtrim (); + ret += c2 != ""; + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +#if !defined(BSTRLIB_CANNOT_USE_STL) + +int test28 (void) { +int ret = 0; + + printf ("TEST: split(), join() mechanisms\n"); + + try { + CBString c0, c1("a b c d e f"); + struct CBStringList s; + s.split (c1, ' '); + + c0.writeprotect (); + EXCEPTION_EXPECTED (c0.join (s)); + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0, c1("a b c d e f"); + struct CBStringList s; + + printf ("\t\"%s\".split (' ')\n", (const char *) c1); + + s.split (c1, ' '); + CBString c2(s), c3(s, ','); + + printf ("\tc.join (<...>)\n"); + + ret += c2 != "abcdef"; + ret += c3 != "a,b,c,d,e,f"; + c0.join (s); + ret += c0 != "abcdef"; + c0.join (s, ','); + ret += c0 != "a,b,c,d,e,f"; + + CBString strPepe = "valor1@valor2@valor3@@@valor6"; + for (unsigned char c = (unsigned char) '\0';;c++) { + CBStringList sl; + CBString x; + + sl.split (strPepe, c); + x.join (sl, c); + if (x != strPepe) { + printf ("\tfailure[%d] split/join mismatch\n\t\t%s\n\t\t%s\n", __LINE__, (const char *) strPepe, (const char *) x); + ret++; + break; + } + if (UCHAR_MAX == c) break; + } + + { + CBStringList sl; + CBString x; + + sl.splitstr (strPepe, CBString ("or")); + x.join (sl, CBString ("or")); + if (x != strPepe) { + printf ("\tfailure[%d] splitstr/join mismatch\n\t\t%s\n\t\t%s\n", __LINE__, (const char *) strPepe, (const char *) x); + ret++; + } + } + + { + CBStringList sl; + CBString x; + + sl.splitstr (strPepe, CBString ("6")); + x.join (sl, CBString ("6")); + if (x != strPepe) { + printf ("\tfailure[%d] splitstr/join mismatch\n\t\t%s\n\t\t%s\n", __LINE__, (const char *) strPepe, (const char *) x); + ret++; + } + } + + { + CBStringList sl; + CBString x; + + sl.splitstr (strPepe, CBString ("val")); + x.join (sl, CBString ("val")); + if (x != strPepe) { + printf ("\tfailure[%d] splitstr/join mismatch\n\t\t%s\n\t\t%s\n", __LINE__, (const char *) strPepe, (const char *) x); + ret++; + } + } + + { + CBStringList sl; + CBString x; + + sl.splitstr (strPepe, CBString ("@@")); + x.join (sl, CBString ("@@")); + if (x != strPepe) { + printf ("\tfailure[%d] splitstr/join mismatch\n\t\t%s\n\t\t%s\n", __LINE__, (const char *) strPepe, (const char *) x); + ret++; + } + } + + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +#endif + +int test29 (void) { +int ret = 0; + + printf ("TEST: caselessEqual(), caselessCmp() mechanisms\n"); + + try { + CBString c0("Test"), c1("test"), c2("testy"); + + printf ("\t\"%s\".caselessEqual (\"%s\")\n", (const char *) c0, (const char *) c1); + ret += 1 != c0.caselessEqual (c1); + ret += 1 != c1.caselessEqual (c0); + printf ("\t\"%s\".caselessEqual (\"%s\")\n", (const char *) c0, (const char *) c2); + ret += 0 != c0.caselessEqual (c2); + ret += 0 != c2.caselessEqual (c0); + + printf ("\t\"%s\".caselessCmp (\"%s\")\n", (const char *) c0, (const char *) c1); + ret += 0 != c0.caselessCmp (c1); + ret += 0 != c1.caselessCmp (c0); + printf ("\t\"%s\".caselessCmp (\"%s\")\n", (const char *) c0, (const char *) c2); + ret += 0 == c0.caselessCmp (c2); + ret += 0 == c2.caselessCmp (c0); + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int test30 (void) { +int ret = 0; + + printf ("TEST: toupper(), tolower() mechanisms\n"); + + try { + CBString c0("Test-test"); + + c0.writeprotect (); + EXCEPTION_EXPECTED (c0.toupper()); + EXCEPTION_EXPECTED (c0.tolower()); + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + try { + CBString c0; + + c0 = "Test"; + printf ("\t\"%s\".toupper ()\n", (const char *) c0); + c0.toupper(); + ret += c0 != "TEST"; + + c0 = "Test"; + printf ("\t\"%s\".tolower ()\n", (const char *) c0); + c0.tolower (); + ret += c0 != "test"; + } + catch (struct CBStringException err) { + printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); + ret ++; + } + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static size_t test31_aux (void *buff, size_t elsize, size_t nelem, void *parm) { + buff = buff; + elsize = elsize; + nelem = nelem; + parm = parm; + return 0; +} + +int test31 (void) { +CBString c; +int ret = 0; + + printf ("TEST: CBStream test\n"); + + CBStream s((bNread) test31_aux, NULL); + s << CBString("Test"); + + ret += ((c = s.read ()) != CBString ("Test")); + ret += !s.eof(); + + printf ("\t\"%s\" through CBStream.read()\n", (const char *) c); + + s << CBString("Test"); + + c.trunc (0); + ret += ((s >> c) != CBString ("Test")); + ret += !s.eof(); + + printf ("\t\"%s\" through CBStream.>>\n", (const char *) c); + + return ret; +} + +/* int bMultiConcatNeedNULLAsLastArgument (bstring dst, ...) + * + * Concatenate a sequence of exactly n char * arguments to dst. + */ +int bMultiConcatNeedNULLAsLastArgument (bstring dst, ...) { +va_list arglist; +int ret = 0; + va_start (arglist, dst); + do { + bstring parm = va_arg (arglist, bstring); + if (NULL == parm) break; + if (NULL == parm->data || parm->slen > parm->mlen || + parm->mlen < 0 || parm->slen < 0) { + ret = BSTR_ERR; + break; + } + ret = bconcat (dst, parm); + } while (0 <= ret); + va_end (arglist); + return ret; +} + +/* int bMultiCatCstrNeedNULLAsLastArgument (bstring dst, ...) + * + * Concatenate a sequence of exactly n char * arguments to dst. + */ +int bMultiCatCstrNeedNULLAsLastArgument (bstring dst, ...) { +va_list arglist; +int ret = 0; + va_start (arglist, dst); + do { + char* parm = va_arg (arglist, char *); + if (NULL == parm) break; + ret = bcatcstr (dst, parm); + } while (0 <= ret); + va_end (arglist); + return ret; +} + +/* + * The following macros are only available on more recent compilers that + * support variable length macro arguments and __VA_ARGS__. These can also + * be dangerous because there is no compiler time type checking on the + * arguments. + */ + + +#define bMultiConcat(dst,...) bMultiConcatNeedNULLAsLastArgument((dst),##__VA_ARGS__,NULL) +#define bMultiCatCstr(dst,...) bMultiCatCstrNeedNULLAsLastArgument((dst),##__VA_ARGS__,NULL) + +#define bGlue3_aux(a,b,c) a ## b ## c +#define bGlue3(a,b,c) bGlue3_aux(a,b,c) + +#if defined(_MSC_VER) +#define _bDeclTbstrIdx(t,n,...) \ + static unsigned char bGlue3(_btmpuc_,t,n)[] = {__VA_ARGS__, '\0'}; \ + struct tagbstring t = { -32, sizeof(bGlue3(_btmpuc_,t,n))-1, bGlue3(_btmpuc_,t,n)} +#define bDeclTbstr(t,...) _bDeclTbstrIdx(t,__COUNTER__,__VA_ARGS__) +#else +#define bDeclTbstr(t,...) \ + static unsigned char bGlue3(_btmpuc_,t,__LINE__)[] = {__VA_ARGS__, '\0'}; \ + struct tagbstring t = { -__LINE__, sizeof(bGlue3(_btmpuc_,t,__LINE__))-1, bGlue3(_btmpuc_,t,__LINE__)} +#endif + +static int test32(void) { +bstring b1 = bfromStatic ("a"); +bstring b2 = bfromStatic ("e"); +bstring b3 = bfromStatic ("i"); +bstring b4 = bfromStatic (""); +int ret = 0; + + printf ("TEST: bMultiCatCstr, bMultiConcat\n"); + + bMultiCatCstr(b1, "b", "c", "d"); + bMultiCatCstr(b2, "f", "g", "h"); + bMultiCatCstr(b3, "j", "k", "l"); + bMultiConcat(b4, b1, b2, b3); + + ret += 1 != biseqStatic (b1, "abcd"); + ret += 1 != biseqStatic (b2, "efgh"); + ret += 1 != biseqStatic (b3, "ijkl"); + ret += 1 != biseqStatic (b4, "abcdefghijkl"); + + bdestroy (b1); + bdestroy (b2); + bdestroy (b3); + bdestroy (b4); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +static int test33(void) { + bDeclTbstr (t1, 'H','e','l','l','o'); + bDeclTbstr (t2, 32,'w','o','r','l','d'); + bstring b = bfromStatic("["); + int ret; + + printf ("TEST: bDeclTbstr\n"); + + bconcat (b, &t1); + bconcat (b, &t2); + bcatStatic (b, "]"); + ret = 1 != biseqStatic (b, "[Hello world]"); + bdestroy (b); + + printf ("\t# failures: %d\n", ret); + return ret; +} + +int main () { +int ret = 0; + + printf ("Direct case testing of CPP core functions\n"); + + ret += test0 (); + ret += test1 (); + ret += test2 (); + ret += test3 (); + ret += test4 (); + ret += test5 (); + ret += test6 (); + ret += test7 (); + ret += test8 (); + ret += test9 (); + ret += test10 (); + ret += test11 (); + ret += test12 (); + ret += test13 (); + ret += test14 (); + ret += test15 (); + ret += test16 (); + ret += test17 (); + ret += test18 (); + ret += test19 (); + ret += test20 (); + ret += test21 (); + ret += test22 (); + ret += test23 (); + ret += test24 (); + ret += test25 (); + ret += test26 (); + ret += test27 (); +#if !defined(BSTRLIB_CANNOT_USE_STL) + ret += test28 (); +#endif + ret += test29 (); + ret += test30 (); + ret += test31 (); + ret += test32 (); + ret += test33 (); + + printf ("# test failures: %d\n", ret); + + return 0; +} diff --git a/bstrlib/testaux.c b/bstrlib/testaux.c new file mode 100644 index 0000000..99a05df --- /dev/null +++ b/bstrlib/testaux.c @@ -0,0 +1,423 @@ +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2015, and is covered by the BSD open source + * license. Refer to the accompanying documentation for details on usage and + * license. + */ + +/* + * testaux.c + * + * This file is the C unit test for the bstraux module of Bstrlib. + */ + +#include +#include "bstrlib.h" +#include "bstraux.h" + +static int tWrite (const void * buf, size_t elsize, size_t nelem, void * parm) { +bstring b = (bstring) parm; +size_t i; + + if (NULL == b || NULL == buf || 0 == elsize || 0 == nelem) + return -__LINE__; + + for (i=0; i < nelem; i++) { + if (0 > bcatblk (b, buf, elsize)) break; + buf = (const void *) (elsize + (const char *) buf); + } + return (int) i; +} + +int test0 (void) { +struct bwriteStream * ws; +bstring s; +int ret = 0; + + printf ("TEST: struct bwriteStream functions.\n"); + + ws = bwsOpen ((bNwrite) tWrite, (s = bfromcstr (""))); + bwsBuffLength (ws, 8); + ret += 8 != bwsBuffLength (ws, 0); + bwsWriteBlk (ws, bsStaticBlkParms ("Hello ")); + ret += 0 == biseqcstr (s, ""); + bwsWriteBlk (ws, bsStaticBlkParms ("World\n")); + ret += 0 == biseqcstr (s, "Hello Wo"); + ret += s != bwsClose (ws); + ret += 0 == biseqcstr (s, "Hello World\n"); + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +int test1 (void) { +struct tagbstring t = bsStatic ("Hello world"); +bstring b, c, d; +int ret = 0; + + printf ("TEST: bTail and bHead functions.\n"); + b = bTail (&t, 5); + c = bHead (&t, 5); + ret += 0 >= biseqcstr (b, "world"); + ret += 0 >= biseqcstr (c, "Hello"); + bdestroy (b); + bdestroy (c); + + b = bTail (&t, 0); + c = bHead (&t, 0); + ret += 0 >= biseqcstr (b, ""); + ret += 0 >= biseqcstr (c, ""); + bdestroy (b); + bdestroy (c); + + d = bstrcpy (&t); + b = bTail (d, 5); + c = bHead (d, 5); + ret += 0 >= biseqcstr (b, "world"); + ret += 0 >= biseqcstr (c, "Hello"); + bdestroy (b); + bdestroy (c); + bdestroy (d); + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +int test2 (void) { +struct tagbstring t = bsStatic ("Hello world"); +bstring b; +int ret = 0, reto; + + printf ("TEST: bSetChar function.\n"); + ret += 0 <= bSetChar (&t, 4, ','); + ret += 0 > bSetChar (b = bstrcpy (&t), 4, ','); + ret += 0 >= biseqcstr (b, "Hell, world"); + ret += 0 <= bSetChar (b, -1, 'x'); + b->slen = 2; + ret += 0 > bSetChar (b, 1, 'i'); + ret += 0 >= biseqcstr (b, "Hi"); + ret += 0 > bSetChar (b, 2, 's'); + ret += 0 >= biseqcstr (b, "His"); + ret += 0 > bSetChar (b, 1, '\0'); + ret += blength (b) != 3; + ret += bchare (b, 0, '?') != 'H'; + ret += bchare (b, 1, '?') != '\0'; + ret += bchare (b, 2, '?') != 's'; + bdestroy (b); + + printf ("\t# failures: %d\n", ret); + + reto = ret; + ret = 0; + + printf ("TEST: bSetCstrChar function.\n"); + ret += 0 <= bSetCstrChar (&t, 4, ','); + ret += 0 > bSetCstrChar (b = bstrcpy (&t), 4, ','); + ret += 0 >= biseqcstr (b, "Hell, world"); + ret += 0 <= bSetCstrChar (b, -1, 'x'); + b->slen = 2; + ret += 0 > bSetCstrChar (b, 1, 'i'); + ret += 0 >= biseqcstr (b, "Hi"); + ret += 0 > bSetCstrChar (b, 2, 's'); + ret += 0 >= biseqcstr (b, "His"); + ret += 0 > bSetCstrChar (b, 1, '\0'); + ret += blength (b) != 1; + ret += bchare (b, 0, '?') != 'H'; + bdestroy (b); + + printf ("\t# failures: %d\n", ret); + + return reto + ret; +} + +int test3 (void) { +struct tagbstring t = bsStatic ("Hello world"); +bstring b; +int ret = 0; + + printf ("TEST: bFill function.\n"); + ret += 0 <= bFill (&t, 'x', 7); + ret += 0 > bFill (b = bstrcpy (&t), 'x', 7); + ret += 0 >= biseqcstr (b, "xxxxxxx"); + ret += 0 <= bFill (b, 'x', -1); + ret += 0 > bFill (b, 'x', 0); + ret += 0 >= biseqcstr (b, ""); + bdestroy (b); + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +int test4 (void) { +struct tagbstring t = bsStatic ("foo"); +bstring b; +int ret = 0; + + printf ("TEST: bReplicate function.\n"); + ret += 0 <= bReplicate (&t, 4); + ret += 0 <= bReplicate (b = bstrcpy (&t), -1); + ret += 0 > bReplicate (b, 4); + ret += 0 >= biseqcstr (b, "foofoofoofoo"); + ret += 0 > bReplicate (b, 0); + ret += 0 >= biseqcstr (b, ""); + bdestroy (b); + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +int test5 (void) { +struct tagbstring t = bsStatic ("Hello world"); +bstring b; +int ret = 0; + + printf ("TEST: bReverse function.\n"); + ret += 0 <= bReverse (&t); + ret += 0 > bReverse (b = bstrcpy (&t)); + ret += 0 >= biseqcstr (b, "dlrow olleH"); + b->slen = 0; + ret += 0 > bReverse (b); + ret += 0 >= biseqcstr (b, ""); + bdestroy (b); + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +int test6 (void) { +struct tagbstring t = bsStatic ("Hello world"); +bstring b; +int ret = 0; + + printf ("TEST: bInsertChrs function.\n"); + ret += 0 <= bInsertChrs (&t, 6, 4, 'x', '?'); + ret += 0 > bInsertChrs (b = bstrcpy (&t), 6, 4, 'x', '?'); + ret += 0 >= biseqcstr (b, "Hello xxxxworld"); + bdestroy (b); + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +int test7 (void) { +struct tagbstring t = bsStatic (" i am "); +bstring b; +int ret = 0; + + printf ("TEST: bJustify functions.\n"); + ret += 0 <= bJustifyLeft (&t, ' '); + ret += 0 <= bJustifyRight (&t, 8, ' '); + ret += 0 <= bJustifyMargin (&t, 8, ' '); + ret += 0 <= bJustifyCenter (&t, 8, ' '); + ret += 0 > bJustifyLeft (b = bstrcpy (&t), ' '); + ret += 0 >= biseqcstr (b, "i am"); + ret += 0 > bJustifyRight (b, 8, ' '); + ret += 0 >= biseqcstr (b, " i am"); + ret += 0 > bJustifyMargin (b, 8, ' '); + ret += 0 >= biseqcstr (b, "i am"); + ret += 0 > bJustifyCenter (b, 8, ' '); + ret += 0 >= biseqcstr (b, " i am"); + bdestroy (b); + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +int test8 (void) { +struct tagbstring t = bsStatic ("Hello world"); +bstring b; +char * c; +int ret = 0; + + printf ("TEST: NetStr functions.\n"); + c = bStr2NetStr (&t); + ret += 0 != strcmp (c, "11:Hello world,"); + b = bNetStr2Bstr (c); + ret += 0 >= biseq (b, &t); + bdestroy (b); + bcstrfree (c); + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +int test9 (void) { +struct tagbstring t = bsStatic ("Hello world"); +bstring b, c; +int err, ret = 0; + + printf ("TEST: Base 64 codec.\n"); + + b = bBase64Encode (&t); + ret += 0 >= biseqcstr (b, "SGVsbG8gd29ybGQ="); + c = bBase64DecodeEx (b, &err); + ret += 0 != err; + ret += 0 >= biseq (c, &t); + bdestroy (b); + bdestroy (c); + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +int test10 (void) { +struct tagbstring t = bsStatic ("Hello world"); +bstring b, c; +int err, ret = 0; + + printf ("TEST: UU codec.\n"); + + b = bUuEncode (&t); + ret += 0 >= biseqcstr (b, "+2&5L;&\\@=V]R;&0`\r\n"); + c = bUuDecodeEx (b, &err); + ret += 0 != err; + ret += 0 >= biseq (c, &t); + bdestroy (b); + bdestroy (c); + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +int test11 (void) { +struct tagbstring t = bsStatic ("Hello world"); +unsigned char Ytstr[] = {0x72, 0x8f, 0x96, 0x96, 0x99, 0x4a, 0xa1, 0x99, 0x9c, 0x96, 0x8e}; +bstring b, c; +int ret = 0; + + printf ("TEST: Y codec.\n"); + + b = bYEncode (&t); + ret += 11 != b->slen; + ret += 0 >= bisstemeqblk (b, Ytstr, 11); + c = bYDecode (b); + ret += 0 >= biseq (c, &t); + bdestroy (b); + bdestroy (c); + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +int test12 (void) { +struct tagbstring t = bsStatic ("Hello world"); +struct bStream * s; +bstring b; +int ret = 0; + + printf ("TEST: bsFromBstr.\n"); + + ret = bsread (b = bfromcstr (""), s = bsFromBstr (&t), 6); + ret += 1 != biseqcstr (b, "Hello "); + if (b) b->slen = 0; + ret = bsread (b, s, 6); + ret += 1 != biseqcstr (b, "world"); + + bdestroy (b); + bsclose (s); + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +struct vfgetc { + int ofs; + bstring base; +}; + +static int test13_fgetc (void * ctx) { +struct vfgetc * vctx = (struct vfgetc *) ctx; +int c; + + if (NULL == vctx || NULL == vctx->base) return EOF; + if (vctx->ofs >= blength (vctx->base)) return EOF; + c = bchare (vctx->base, vctx->ofs, EOF); + vctx->ofs++; + return c; +} + +int test13 (void) { +struct tagbstring t0 = bsStatic ("Random String, long enough to cause to reallocing"); +struct vfgetc vctx; +bstring b; +int ret = 0; +int i; + + printf ("TEST: bSecureInput, bSecureDestroy.\n"); + + for (i=0; i < 1000; i++) { + unsigned char * h; + + vctx.ofs = 0; + vctx.base = &t0; + + b = bSecureInput (INT_MAX, '\n', (bNgetc) test13_fgetc, &vctx); + ret += 1 != biseq (b, &t0); + h = b->data; + bSecureDestroy (b); + + /* WARNING! Technically undefined code follows (h has been freed): */ + ret += (0 == memcmp (h, t0.data, t0.slen)); + + if (ret) break; + } + + printf ("\t# failures: %d\n", ret); + + return ret; +} + +int test14_aux(bstring b, const char* chkVal) { +int ret = 0; + ret += 0 != bSGMLEncode (b); + ret += 1 != biseqcstr (b, chkVal); + return ret; +} + +int test14 (void) { +bstring b; +int ret = 0; + + printf ("TEST: bSGMLEncode.\n"); + ret += test14_aux (b = bfromStatic ("<\"Hello, you, me, & world\">"), "<"Hello, you, me, & world">"); + printf ("\t# failures: %d\n", ret); + return ret; +} + +int main () { +int ret = 0; + + printf ("Direct case testing of bstraux functions\n"); + + ret += test0 (); + ret += test1 (); + ret += test2 (); + ret += test3 (); + ret += test4 (); + ret += test5 (); + ret += test6 (); + ret += test7 (); + ret += test8 (); + ret += test9 (); + ret += test10 (); + ret += test11 (); + ret += test12 (); + ret += test13 (); + ret += test14 (); + + printf ("# test failures: %d\n", ret); + + return 0; +} diff --git a/bstrlib/utf8util.c b/bstrlib/utf8util.c new file mode 100644 index 0000000..b74606f --- /dev/null +++ b/bstrlib/utf8util.c @@ -0,0 +1,249 @@ +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2015, and is covered by the BSD open source + * license and the GPL. Refer to the accompanying documentation for details + * on usage and license. + */ + +/* + * utf8util.c + * + * This file is not necessarily part of the core bstring library itself, but + * is just an generic module for implementing utf8 utility functions. + */ + +#include "utf8util.h" + +#ifndef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void *)0) +#endif +#endif + +/* Surrogate range is wrong, there is a maximum, the BOM alias is illegal and 0xFFFF is illegal */ +#define isLegalUnicodeCodePoint(v) ((((v) < 0xD800L) || ((v) > 0xDFFFL)) && (((unsigned long)(v)) <= 0x0010FFFFL) && (((v)|0x1F0001) != 0x1FFFFFL)) + +void utf8IteratorInit (struct utf8Iterator* iter, unsigned char* data, int slen) { + if (iter) { + iter->data = data; + iter->slen = (iter->data && slen >= 0) ? slen : -1; + iter->start = -1; + iter->next = (iter->slen >= 0) ? 0 : -1; + iter->error = (iter->slen >= 0) ? 0 : 1; + } +} + +void utf8IteratorUninit (struct utf8Iterator* iter) { + if (iter) { + iter->data = NULL; + iter->slen = -1; + iter->start = iter->next = -1; + } +} + +int utf8ScanBackwardsForCodePoint (unsigned char* msg, int len, int pos, cpUcs4* out) { + cpUcs4 v1, v2, v3, v4, x; + int ret; + if (NULL == msg || len < 0 || (unsigned) pos >= (unsigned) len) { + return -__LINE__; + } + if (!out) out = &x; + ret = 0; + if (msg[pos] < 0x80) { + *out = msg[pos]; + return 0; + } else if (msg[pos] < 0xC0) { + if (0 == pos) return -__LINE__; + ret = -__LINE__; + if (msg[pos-1] >= 0xC1 && msg[pos-1] < 0xF8) { + pos--; + ret = 1; + } else { + if (1 == pos) return -__LINE__; + if ((msg[pos-1] | 0x3F) != 0xBF) return -__LINE__; + if (msg[pos-2] >= 0xE0 && msg[pos-2] < 0xF8) { + pos -= 2; + ret = 2; + } else { + if (2 == pos) return -__LINE__; + if ((msg[pos-2] | 0x3F) != 0xBF) return -__LINE__; + if ((msg[pos-3]|0x07) == 0xF7) { + pos -= 3; + ret = 3; + } else return -__LINE__; + } + } + } + if (msg[pos] < 0xE0) { + if (pos + 1 >= len) return -__LINE__; + v1 = msg[pos] & ~0xE0; + v2 = msg[pos+1] & ~0xC0; + v1 = (v1 << 6) + v2; + if (v1 < 0x80) return -__LINE__; + *out = v1; + return ret; + } + if (msg[pos] < 0xF0) { + if (pos + 2 >= len) return -__LINE__; + v1 = msg[pos] & ~0xF0; + v2 = msg[pos+1] & ~0xC0; + v3 = msg[pos+2] & ~0xC0; + v1 = (v1 << 12) + (v2 << 6) + v3; + if (v1 < 0x800) return -__LINE__; + if (!isLegalUnicodeCodePoint(v1)) return -__LINE__; + *out = v1; + return ret; + } + + if (msg[pos] >= 0xF8) return -__LINE__; + + if (pos + 3 >= len) return -__LINE__; + v1 = msg[pos] & ~0xF8; + v2 = msg[pos+1] & ~0xC0; + v3 = msg[pos+2] & ~0xC0; + v4 = msg[pos+3] & ~0xC0; + v1 = (v1 << 18) + (v2 << 12) + (v3 << 6) + v4; + if (v1 < 0x10000) return -__LINE__; + if (!isLegalUnicodeCodePoint(v1)) return -__LINE__; + *out = v1; + return ret; +} + +/* +Code point UTF-8 +---------- ----- +U-00000000 - U-0000007F: 0xxxxxxx +U-00000080 - U-000007FF: 110xxxxx 10xxxxxx +U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx +U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + +U-00200000 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx +U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx +*/ + +/* + * Returns next read code point for iterator. + * + * iter->data + iter->start points at the characters just read. + * + * iter->data + iter->next points at the characters that will be read next. + * + * iter->error is boolean indicating whether or not last read contained an error. + */ +cpUcs4 utf8IteratorGetNextCodePoint (struct utf8Iterator* iter, cpUcs4 errCh) { + unsigned char * chrs; + unsigned char c, d, e; + long v; + int i, ofs; + + if (NULL == iter || iter->next < 0) return errCh; + if (iter->next >= iter->slen) { + iter->start = iter->slen; + return errCh; + } + if (NULL == iter->data || iter->next < 0 || utf8IteratorNoMore(iter)) return errCh; + chrs = iter->data + iter->next; + + iter->error = 0; + c = chrs[0]; + ofs = 0; + + if (c < 0xC0 || c > 0xFD) { + if (c >= 0x80) goto ErrMode; + v = c; + ofs = 1; + } else if (c < 0xE0) { + if (iter->next >= iter->slen + 1) goto ErrMode; + v = (c << 6u) - (0x0C0 << 6u); + c = (unsigned char) ((unsigned) chrs[1] - 0x080); + v += c; + if (c >= 0x40 || v < 0x80) goto ErrMode; + ofs = 2; + } else if (c < 0xF0) { + if (iter->next >= iter->slen + 2) goto ErrMode; + v = (c << 12) - (0x0E0 << 12u); + c = (unsigned char) ((unsigned) chrs[1] - 0x080); + d = (unsigned char) ((unsigned) chrs[2] - 0x080); + v += (c << 6u) + d; + if ((c|d) >= 0x40 || v < 0x800 || !isLegalUnicodeCodePoint (v)) goto ErrMode; + ofs = 3; + } else if (c < 0xF8) { + if (iter->next >= iter->slen + 3) goto ErrMode; + v = (c << 18) - (0x0F0 << 18u); + c = (unsigned char) ((unsigned) chrs[1] - 0x080); + d = (unsigned char) ((unsigned) chrs[2] - 0x080); + e = (unsigned char) ((unsigned) chrs[3] - 0x080); + v += (c << 12u) + (d << 6u) + e; + if ((c|d|e) >= 0x40 || v < 0x10000 || !isLegalUnicodeCodePoint (v)) goto ErrMode; + ofs = 4; + } else { /* 5 and 6 byte encodings are invalid */ + ErrMode:; + iter->error = 1; + v = errCh; + for (i = iter->next+1; i < iter->slen; i++) if ((iter->data[i] & 0xC0) != 0x80) break; + ofs = i - iter->next; + } + + iter->start = iter->next; + iter->next += ofs; + return v; +} + +/* + * Returns next read code point for iterator. + * + * iter->data + iter->start points at the characters to be read. + * + * iter->data + iter->next points at the characters that will be read next. + * + * iter->error is boolean indicating whether or not last read contained an error. + */ +cpUcs4 utf8IteratorGetCurrCodePoint (struct utf8Iterator* iter, cpUcs4 errCh) { + unsigned char * chrs; + unsigned char c, d, e; + long v; + + if (NULL == iter || iter->next < 0) return errCh; + if (iter->next >= iter->slen) { + iter->start = iter->slen; + return errCh; + } + if (NULL == iter->data || iter->next < 0 || utf8IteratorNoMore(iter)) return errCh; + chrs = iter->data + iter->next; + + iter->error = 0; + c = chrs[0]; + + if (c < 0xC0 || c > 0xFD) { + if (c >= 0x80) goto ErrMode; + v = c; + } else if (c < 0xE0) { + if (iter->next >= iter->slen + 1) goto ErrMode; + v = (c << 6u) - (0x0C0 << 6u); + c = (unsigned char) ((unsigned) chrs[1] - 0x080); + v += c; + if (c >= 0x40 || v < 0x80) goto ErrMode; + } else if (c < 0xF0) { + if (iter->next >= iter->slen + 2) goto ErrMode; + v = (c << 12lu) - (0x0E0 << 12u); + c = (unsigned char) ((unsigned) chrs[1] - 0x080); + d = (unsigned char) ((unsigned) chrs[2] - 0x080); + v += (c << 6u) + d; + if ((c|d) >= 0x40 || v < 0x800 || !isLegalUnicodeCodePoint (v)) goto ErrMode; + } else if (c < 0xF8) { + if (iter->next >= iter->slen + 3) goto ErrMode; + v = (c << 18lu) - (0x0F0 << 18u); + c = (unsigned char) ((unsigned) chrs[1] - 0x080); + d = (unsigned char) ((unsigned) chrs[2] - 0x080); + e = (unsigned char) ((unsigned) chrs[3] - 0x080); + v += (c << 12lu) + (d << 6u) + e; + if ((c|d|e) >= 0x40 || v < 0x10000 || !isLegalUnicodeCodePoint (v)) goto ErrMode; + } else { /* 5 and 6 byte encodings are invalid */ + ErrMode:; + iter->error = 1; + v = errCh; + } + return v; +} diff --git a/bstrlib/utf8util.h b/bstrlib/utf8util.h new file mode 100644 index 0000000..5e615f7 --- /dev/null +++ b/bstrlib/utf8util.h @@ -0,0 +1,62 @@ +/* + * This source file is part of the bstring string library. This code was + * written by Paul Hsieh in 2002-2015, and is covered by the BSD open source + * license and the GPL. Refer to the accompanying documentation for details + * on usage and license. + */ + +/* + * utf8util.h + * + * This file defines the interface to the utf8 utility functions. + */ + +#ifndef UTF8_UNICODE_UTILITIES +#define UTF8_UNICODE_UTILITIES + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#if INT_MAX >= 0x7fffffffUL +typedef int cpUcs4; +#elif LONG_MAX >= 0x7fffffffUL +typedef long cpUcs4; +#else +#error This compiler is not supported +#endif + +#if UINT_MAX == 0xFFFF +typedef unsigned int cpUcs2; +#elif USHRT_MAX == 0xFFFF +typedef unsigned short cpUcs2; +#elif UCHAR_MAX == 0xFFFF +typedef unsigned char cpUcs2; +#else +#error This compiler is not supported +#endif + +#define isLegalUnicodeCodePoint(v) ((((v) < 0xD800L) || ((v) > 0xDFFFL)) && (((unsigned long)(v)) <= 0x0010FFFFL) && (((v)|0x1F0001) != 0x1FFFFFL)) + +struct utf8Iterator { + unsigned char* data; + int slen; + int start, next; + int error; +}; + +#define utf8IteratorNoMore(it) (!(it) || (it)->next >= (it)->slen) + +extern void utf8IteratorInit (struct utf8Iterator* iter, unsigned char* data, int slen); +extern void utf8IteratorUninit (struct utf8Iterator* iter); +extern cpUcs4 utf8IteratorGetNextCodePoint (struct utf8Iterator* iter, cpUcs4 errCh); +extern cpUcs4 utf8IteratorGetCurrCodePoint (struct utf8Iterator* iter, cpUcs4 errCh); +extern int utf8ScanBackwardsForCodePoint (unsigned char* msg, int len, int pos, cpUcs4* out); + +#ifdef __cplusplus +} +#endif + +#endif /* UTF8_UNICODE_UTILITIES */ From 804a55ba641d7be7f6d42763f65fb4a8601d6232 Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Tue, 5 Jul 2016 16:18:55 +0200 Subject: [PATCH 05/12] UString integration done - now UString works for both Qt (uses QString) and non-Qt (uses modified CBString) builds --- UEFIExtract/ffsdumper.cpp | 2 +- UEFIExtract/uefiextract_main.cpp | 16 ++++++++-------- bstrlib/bstrwrap.cpp | 2 +- bstrlib/bstrwrap.h | 5 ++++- common/ffsparser.cpp | 4 ++-- common/treemodel.cpp | 4 ++-- common/ustring.cpp | 2 +- common/ustring.h | 4 +--- 8 files changed, 20 insertions(+), 19 deletions(-) diff --git a/UEFIExtract/ffsdumper.cpp b/UEFIExtract/ffsdumper.cpp index ceaf719..7f60612 100644 --- a/UEFIExtract/ffsdumper.cpp +++ b/UEFIExtract/ffsdumper.cpp @@ -79,7 +79,7 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path if (!file.open(QFile::Text | QFile::WriteOnly)) return U_FILE_OPEN; - file.write(info); + file.write(info.toLocal8Bit()); file.close(); dumped = true; } diff --git a/UEFIExtract/uefiextract_main.cpp b/UEFIExtract/uefiextract_main.cpp index 3fa8d08..d80a3af 100644 --- a/UEFIExtract/uefiextract_main.cpp +++ b/UEFIExtract/uefiextract_main.cpp @@ -62,7 +62,7 @@ int main(int argc, char *argv[]) // Show ffsParser's messages std::vector > messages = ffsParser.getMessages(); for (size_t i = 0; i < messages.size(); i++) { - std::cout << messages[i].first << std::endl; + std::cout << (const char*)messages[i].first.toLocal8Bit() << std::endl; } // Get last VTF @@ -76,7 +76,7 @@ int main(int argc, char *argv[]) // Show fitParser's messages std::vector > fitMessages = fitParser.getMessages(); for (size_t i = 0; i < fitMessages.size(); i++) { - std::cout << fitMessages[i].first << std::endl; + std::cout << (const char*)fitMessages[i].first.toLocal8Bit() << std::endl; } // Show FIT table @@ -86,11 +86,11 @@ int main(int argc, char *argv[]) std::cout << " Address | Size | Ver | Type | CS " << std::endl; std::cout << "-------------------------------------------------------------------" << std::endl; for (size_t i = 0; i < fitTable.size(); i++) { - std::cout << fitTable[i][0] << " | " - << fitTable[i][1] << " | " - << fitTable[i][2] << " | " - << fitTable[i][3] << " | " - << fitTable[i][4] << std::endl; + std::cout << (const char*)fitTable[i][0].toLocal8Bit() << " | " + << (const char*)fitTable[i][1].toLocal8Bit() << " | " + << (const char*)fitTable[i][2].toLocal8Bit() << " | " + << (const char*)fitTable[i][3].toLocal8Bit() << " | " + << (const char*)fitTable[i][4].toLocal8Bit() << std::endl; } } } @@ -103,7 +103,7 @@ int main(int argc, char *argv[]) std::ofstream ofs; ofs.open("report.txt", std::ofstream::out); for (size_t i = 0; i < report.size(); i++) { - ofs << report[i] << std::endl; + ofs << (const char*)report[i].toLocal8Bit() << std::endl; } ofs.close(); } diff --git a/bstrlib/bstrwrap.cpp b/bstrlib/bstrwrap.cpp index ce77f17..2ec38c1 100644 --- a/bstrlib/bstrwrap.cpp +++ b/bstrlib/bstrwrap.cpp @@ -903,7 +903,7 @@ struct tagbstring t; return bninchrr ((bstring) this, pos, &t); } -const CBString CBString::midstr (int left, int len) const { +CBString CBString::midstr (int left, int len) const { struct tagbstring t; if (left < 0) { len += left; diff --git a/bstrlib/bstrwrap.h b/bstrlib/bstrwrap.h index dcb0ecc..4a785a9 100644 --- a/bstrlib/bstrwrap.h +++ b/bstrlib/bstrwrap.h @@ -317,7 +317,7 @@ struct CBString : public tagbstring { void findreplacecaseless (const char * find, const char * repl, int pos = 0); // Extraction method. - const CBString midstr (int left, int len) const; + CBString midstr (int left, int len) const; // Standard manipulation methods. void setsubstr (int pos, const CBString& b, unsigned char fill = ' '); @@ -362,8 +362,11 @@ struct CBString : public tagbstring { int read (bNread readPtr, void * parm); // QString compatibility methods + CBString toLocal8Bit() { return *this; } bool isEmpty() const { return slen == 0; } void clear() { *this = ""; } + CBString left(int len) { return midstr(0, len); } + CBString mid(int pos, int len) { return midstr(pos, len); } static CBString fromUtf16(const ushort* str) { // Naive implementation assuming that only ASCII part of UCS2 is used CBString msg; while (*str) { msg += *(char*)str; str++; } return msg; } diff --git a/common/ffsparser.cpp b/common/ffsparser.cpp index 0c2cf52..b91a09c 100644 --- a/common/ffsparser.cpp +++ b/common/ffsparser.cpp @@ -4137,8 +4137,8 @@ USTATUS FfsParser::parseSlicMarkerHeader(const UByteArray & store, const UINT32 markerHeader->Size, markerHeader->Size, header.size(), header.size(), markerHeader->Version, - (const char*)UString((const char*)&(markerHeader->OemId), 6), - (const char*)UString((const char*)&(markerHeader->OemTableId), 8), + (const char*)UString((const char*)&(markerHeader->OemId)).left(6).toLocal8Bit(), + (const char*)UString((const char*)&(markerHeader->OemTableId)).left(8).toLocal8Bit(), markerHeader->SlicVersion); // Add correct offset diff --git a/common/treemodel.cpp b/common/treemodel.cpp index df0e03f..00f695e 100644 --- a/common/treemodel.cpp +++ b/common/treemodel.cpp @@ -44,9 +44,9 @@ QVariant TreeModel::data(const UModelIndex &index, int role) const TreeItem *item = static_cast(index.internalPointer()); if (role == Qt::DisplayRole) - return (const char*)item->data(index.column()); + return (const char*)item->data(index.column()).toLocal8Bit(); else - return (const char*)item->info(); + return (const char*)item->info().toLocal8Bit(); } Qt::ItemFlags TreeModel::flags(const UModelIndex &index) const diff --git a/common/ustring.cpp b/common/ustring.cpp index 94b75fe..8c85c76 100644 --- a/common/ustring.cpp +++ b/common/ustring.cpp @@ -15,7 +15,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. //TODO: modify properly -#ifndef QT_CORE_LIB +#if defined(QT_CORE_LIB) && defined (U_USE_QSTRING) UString usprintf(const char* fmt, ...) { UString msg; diff --git a/common/ustring.h b/common/ustring.h index 345917a..de15135 100644 --- a/common/ustring.h +++ b/common/ustring.h @@ -13,9 +13,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef USTRING_H #define USTRING_H -//TODO: modify properly - -#ifndef QT_CORE_LIB +#if defined (QT_CORE_LIB) && defined(U_USE_QSTRING) // Use Qt class, if Qt is available #include #define UString QString From 71ba5fe582ed563c07111eab43cc205e514d9300 Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Tue, 5 Jul 2016 17:22:03 +0200 Subject: [PATCH 06/12] UByteArray integrated - another Qt class can be replaced for non-Qt builds --- UEFIExtract/ffsdumper.cpp | 6 ++++-- UEFIExtract/uefiextract_main.cpp | 4 +++- bstrlib/bstrwrap.h | 6 +++--- common/ffsparser.cpp | 22 +++++++++++----------- common/fit.h | 4 ++-- common/treeitem.cpp | 2 +- common/treeitem.h | 8 ++++---- common/treemodel.cpp | 2 +- common/ubytearray.h | 25 +++++++++++++++++++++++-- common/umodelindex.h | 5 ++++- 10 files changed, 56 insertions(+), 28 deletions(-) diff --git a/UEFIExtract/ffsdumper.cpp b/UEFIExtract/ffsdumper.cpp index 7f60612..3c86cab 100644 --- a/UEFIExtract/ffsdumper.cpp +++ b/UEFIExtract/ffsdumper.cpp @@ -56,7 +56,8 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path if (!file.open(QFile::WriteOnly)) return U_FILE_OPEN; - file.write(model->header(index)); + QByteArray ba(model->header(index).constData(), model->header(index).size()); + file.write(ba); file.close(); } @@ -65,7 +66,8 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path if (!file.open(QFile::WriteOnly)) return U_FILE_OPEN; - file.write(model->body(index)); + QByteArray ba(model->body(index).constData(), model->body(index).size()); + file.write(ba); file.close(); } } diff --git a/UEFIExtract/uefiextract_main.cpp b/UEFIExtract/uefiextract_main.cpp index d80a3af..1cc57ef 100644 --- a/UEFIExtract/uefiextract_main.cpp +++ b/UEFIExtract/uefiextract_main.cpp @@ -48,9 +48,11 @@ int main(int argc, char *argv[]) return U_FILE_OPEN; // Read and close the file - UByteArray buffer = inputFile.readAll(); + QByteArray b = inputFile.readAll(); inputFile.close(); + UByteArray buffer(b.constData(), b.size()); + // Create model and ffsParser TreeModel model; FfsParser ffsParser(&model); diff --git a/bstrlib/bstrwrap.h b/bstrlib/bstrwrap.h index 4a785a9..c3945de 100644 --- a/bstrlib/bstrwrap.h +++ b/bstrlib/bstrwrap.h @@ -365,9 +365,9 @@ struct CBString : public tagbstring { CBString toLocal8Bit() { return *this; } bool isEmpty() const { return slen == 0; } void clear() { *this = ""; } - CBString left(int len) { return midstr(0, len); } - CBString mid(int pos, int len) { return midstr(pos, len); } - static CBString fromUtf16(const ushort* str) { // Naive implementation assuming that only ASCII part of UCS2 is used + CBString left(int len) const { return midstr(0, len); } + CBString mid(int pos, int len) const { return midstr(pos, len); } + static CBString fromUtf16(const unsigned short* str) { // Naive implementation assuming that only ASCII part of UCS2 is used CBString msg; while (*str) { msg += *(char*)str; str++; } return msg; } CBString leftJustified(int length) { if (length > slen) { return *this + CBString(' ', length - slen); } return *this; } diff --git a/common/ffsparser.cpp b/common/ffsparser.cpp index b91a09c..35c936a 100644 --- a/common/ffsparser.cpp +++ b/common/ffsparser.cpp @@ -112,7 +112,7 @@ USTATUS FfsParser::performFirstPass(const UByteArray & buffer, UModelIndex & ind capsuleHeaderSize = capsuleHeader->HeaderSize; UByteArray header = buffer.left(capsuleHeaderSize); - UByteArray body = buffer.right(buffer.size() - capsuleHeaderSize); + UByteArray body = buffer.mid(capsuleHeaderSize); UString name("Toshiba capsule"); UString info = UString("Capsule GUID: ") + guidToUString(capsuleHeader->CapsuleGuid) + usprintf("\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nImage size: %Xh (%u)\nFlags: %08Xh", @@ -669,7 +669,7 @@ USTATUS FfsParser::parseMeRegion(const UByteArray & me, const UINT32 parentOffse bool versionFound = true; bool emptyRegion = false; // Check for empty region - if (me.count() == me.count('\xFF') || me.count() == me.count('\x00')) { + if (me.size() == me.count('\xFF') || me.size() == me.count('\x00')) { // Further parsing not needed emptyRegion = true; info += ("\nState: empty"); @@ -791,9 +791,9 @@ USTATUS FfsParser::parseBiosRegion(const UByteArray & bios, const UINT32 parentO UINT8 FfsParser::getPaddingType(const UByteArray & padding) { - if (padding.count('\x00') == padding.count()) + if (padding.count('\x00') == padding.size()) return Subtypes::ZeroPadding; - if (padding.count('\xFF') == padding.count()) + if (padding.count('\xFF') == padding.size()) return Subtypes::OnePadding; return Subtypes::DataPadding; } @@ -1339,7 +1339,7 @@ USTATUS FfsParser::parseVolumeBody(const UModelIndex & index) if (header.count(pdata.emptyByte) == header.size()) { //Empty space // Check free space to be actually free UByteArray freeSpace = volumeBody.mid(fileOffset); - if (freeSpace.count(pdata.emptyByte) != freeSpace.count()) { + if (freeSpace.count(pdata.emptyByte) != freeSpace.size()) { // Search for the first non-empty byte UINT32 i; UINT32 size = freeSpace.size(); @@ -2481,7 +2481,7 @@ USTATUS FfsParser::parseGuidedSectionBody(const UModelIndex & index) } info += UString("\nCompression algorithm: ") + compressionTypeToUString(algorithm); - info += usprintf("\nDecompressed size: %Xh (%u)", processed.length(), processed.length()); + info += usprintf("\nDecompressed size: %Xh (%u)", processed.size(), processed.size()); } // LZMA compressed section else if (UByteArray((const char*)&guid, sizeof(EFI_GUID)) == EFI_GUIDED_SECTION_LZMA) { @@ -2495,7 +2495,7 @@ USTATUS FfsParser::parseGuidedSectionBody(const UModelIndex & index) if (algorithm == COMPRESSION_ALGORITHM_LZMA) { info += UString("\nCompression algorithm: LZMA"); - info += usprintf("\nDecompressed size: %Xh (%u)", processed.length(), processed.length()); + info += usprintf("\nDecompressed size: %Xh (%u)", processed.size(), processed.size()); } else { info += UString("\nCompression algorithm: unknown"); @@ -3276,7 +3276,7 @@ parsing_done: // Authentication data if (hasTimestampAndHash) { info += usprintf("\nTimestamp: %"PRIX64"h\nHash: ", - timestamp) + UString(hash.toHex().toUpper()); + timestamp) + UString(hash.toHex().constData()); } } @@ -3890,7 +3890,7 @@ USTATUS FfsParser::parseFsysStoreHeader(const UByteArray & store, const UINT32 p UByteArray body = store.mid(sizeof(APPLE_FSYS_STORE_HEADER), fsysStoreHeader->Size - sizeof(APPLE_FSYS_STORE_HEADER) - sizeof(UINT32)); // Check store checksum - UINT32 storedCrc = *(UINT32*)store.right(sizeof(UINT32)).constBegin(); + UINT32 storedCrc = *(UINT32*)store.right(sizeof(UINT32)).constData(); UINT32 calculatedCrc = crc32(0, (const UINT8*)store.constData(), (const UINT32)store.size() - sizeof(UINT32)); // Add info @@ -4488,7 +4488,7 @@ USTATUS FfsParser::parseFsysStoreBody(const UModelIndex & index) pdata.offset = parentOffset + offset; // Add EOF tree item - model->addItem(Types::FsysEntry, 0, UString(name), UString(), info, header, UByteArray(), UByteArray(), false, parsingDataToUByteArray(pdata), index); + model->addItem(Types::FsysEntry, 0, UString("EOF"), UString(), info, header, UByteArray(), UByteArray(), false, parsingDataToUByteArray(pdata), index); // Add free space offset += header.size(); @@ -4542,7 +4542,7 @@ USTATUS FfsParser::parseFsysStoreBody(const UModelIndex & index) pdata.offset = parentOffset + offset; // Add tree item - model->addItem(Types::FsysEntry, 0, UString(name), UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), index); + model->addItem(Types::FsysEntry, 0, UString(name.constData(), name.size()), UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), index); // Move to next variable offset += variableSize; diff --git a/common/fit.h b/common/fit.h index 1be4815..81d73de 100644 --- a/common/fit.h +++ b/common/fit.h @@ -13,8 +13,8 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef FIT_H #define FIT_H -#include #include "basetypes.h" +#include "ubytearray.h" // Make sure we use right packing rules #pragma pack(push, 1) @@ -37,7 +37,7 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #define FIT_HEADER_VERSION 0x0100 #define FIT_MICROCODE_VERSION 0x0100 -const QByteArray FIT_SIGNATURE +const UByteArray FIT_SIGNATURE ("\x5F\x46\x49\x54\x5F\x20\x20\x20", 8); typedef struct FIT_ENTRY_ { diff --git a/common/treeitem.cpp b/common/treeitem.cpp index c8c3531..bf0006d 100644 --- a/common/treeitem.cpp +++ b/common/treeitem.cpp @@ -17,7 +17,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. TreeItem::TreeItem(const UINT8 type, const UINT8 subtype, const UString & name, const UString & text, const UString & info, const UByteArray & header, const UByteArray & body, const UByteArray & tail, - const BOOLEAN fixed, const BOOLEAN compressed, const UByteArray & parsingData, + const bool fixed, const bool compressed, const UByteArray & parsingData, TreeItem *parent) : itemAction(Actions::NoAction), itemType(type), diff --git a/common/treeitem.h b/common/treeitem.h index 08b92af..ec81b37 100644 --- a/common/treeitem.h +++ b/common/treeitem.h @@ -25,7 +25,7 @@ class TreeItem public: TreeItem(const UINT8 type, const UINT8 subtype, const UString &name, const UString &text, const UString &info, const UByteArray & header, const UByteArray & body, const UByteArray & tail, - const BOOLEAN fixed, const BOOLEAN compressed, const UByteArray & parsingData, + const bool fixed, const bool compressed, const UByteArray & parsingData, TreeItem *parent = 0); ~TreeItem(); // Non-trivial implementation in CPP file @@ -70,16 +70,16 @@ public: void setParsingData(const UByteArray & data) { itemParsingData = data; } UString info() const { return itemInfo; } - void addInfo(const UString &info, const BOOLEAN append) { if (append) itemInfo += info; else itemInfo = info + itemInfo; } + void addInfo(const UString &info, const bool append) { if (append) itemInfo += info; else itemInfo = info + itemInfo; } void setInfo(const UString &info) { itemInfo = info; } UINT8 action() const {return itemAction; } void setAction(const UINT8 action) { itemAction = action; } - BOOLEAN fixed() const { return itemFixed; } + bool fixed() const { return itemFixed; } void setFixed(const bool fixed) { itemFixed = fixed; } - BOOLEAN compressed() const { return itemCompressed; } + bool compressed() const { return itemCompressed; } void setCompressed(const bool compressed) { itemCompressed = compressed; } private: diff --git a/common/treemodel.cpp b/common/treemodel.cpp index 00f695e..486d8cc 100644 --- a/common/treemodel.cpp +++ b/common/treemodel.cpp @@ -349,7 +349,7 @@ void TreeModel::addInfo(const UModelIndex &index, const UString &data, const boo return; TreeItem *item = static_cast(index.internalPointer()); - item->addInfo(data, (BOOLEAN)append); + item->addInfo(data, append); emit dataChanged(index, index); } diff --git a/common/ubytearray.h b/common/ubytearray.h index ad81bb6..03cd42f 100644 --- a/common/ubytearray.h +++ b/common/ubytearray.h @@ -13,7 +13,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef UBYTEARRAY_H #define UBYTEARRAY_H -#ifdef QT_CORE_LIB +//#define U_USE_QBYTEARRAY + +#if defined(QT_CORE_LIB) && defined(U_USE_QBYTEARRAY) // Use Qt class, if Qt is available #include #define UByteArray QByteArray @@ -54,14 +56,33 @@ public: UByteArray right(int32_t len) const { return d.substr(d.size() - 1 - len, len); }; UByteArray mid(int32_t pos, int32_t len = -1) const { return d.substr(pos, len); }; - UByteArray &operator=(const UByteArray & ba) { d = ba.d; return *this; } + UByteArray & operator=(const UByteArray & ba) { d = ba.d; return *this; } + UByteArray & operator+=(const UByteArray & ba) { d += ba.d; return *this; } bool operator== (const UByteArray & ba) const { return d == ba.d; } bool operator!= (const UByteArray & ba) const { return d != ba.d; } inline void swap(UByteArray &other) { std::swap(d, other.d); } + UByteArray toHex() { + std::basic_string hex(size() * 2, '\x00'); + for (int32_t i = 0; i < size(); ++i) { + uint8_t low = d[i] & 0x0F; + uint8_t high = (d[i] & 0xF0) >> 4; + low += (low < 10 ? 'a' : '0'); + high += (high < 10 ? 'a' : '0'); + hex[i] = low; + hex[i + 1] = high; + } + std::reverse(hex.begin(), hex.end()); + return UByteArray(hex); + } private: std::basic_string d; }; +inline const UByteArray operator+(const UByteArray &a1, const UByteArray &a2) +{ + return UByteArray(a1) += a2; +} + #endif // QT_CORE_LIB #endif // UBYTEARRAY_H \ No newline at end of file diff --git a/common/umodelindex.h b/common/umodelindex.h index 0e860a5..4f4c705 100644 --- a/common/umodelindex.h +++ b/common/umodelindex.h @@ -13,11 +13,14 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef UMODELINDEX_H #define UMODELINDEX_H -#ifdef QT_CORE_LIB +#if defined(QT_CORE_LIB) // Use Qt class, if Qt is available #include #define UModelIndex QModelIndex #else // Use own implementation + + #endif // QT_CORE_LIB #endif // UMODELINDEX_H + From 12029c768c52af41f4c41fb811096c0686898dd3 Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Tue, 5 Jul 2016 18:19:04 +0200 Subject: [PATCH 07/12] UModelIndex integrated - can be used instead of QModelIndex for non-Qt builds --- UEFIExtract/ffsdumper.h | 1 - UEFIExtract/uefiextract.pro | 1 - common/ffsparser.cpp | 15 ++--- common/ffsparser.h | 1 - common/ffsreport.h | 1 - common/fitparser.h | 1 - common/treemodel.cpp | 67 ++++++++++++++++------- common/treemodel.h | 106 +++++++++++++++++++++++++++++++++--- common/ubytearray.h | 2 - common/umodelindex.h | 26 --------- common/utility.h | 1 - 11 files changed, 153 insertions(+), 69 deletions(-) delete mode 100644 common/umodelindex.h diff --git a/UEFIExtract/ffsdumper.h b/UEFIExtract/ffsdumper.h index 23481e3..007e9f0 100644 --- a/UEFIExtract/ffsdumper.h +++ b/UEFIExtract/ffsdumper.h @@ -18,7 +18,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include "../common/ubytearray.h" #include "../common/ustring.h" -#include "../common/umodelindex.h" #include "../common/basetypes.h" #include "../common/treemodel.h" #include "../common/ffs.h" diff --git a/UEFIExtract/uefiextract.pro b/UEFIExtract/uefiextract.pro index 3deb59b..4cc6273 100644 --- a/UEFIExtract/uefiextract.pro +++ b/UEFIExtract/uefiextract.pro @@ -45,7 +45,6 @@ HEADERS += \ ../common/utility.h \ ../common/LZMA/LzmaDecompress.h \ ../common/Tiano/EfiTianoDecompress.h \ - ../common/umodelindex.h \ ../common/ubytearray.h \ ../common/ustring.h \ ../bstrlib/bstrlib.h \ diff --git a/common/ffsparser.cpp b/common/ffsparser.cpp index 35c936a..faaa127 100644 --- a/common/ffsparser.cpp +++ b/common/ffsparser.cpp @@ -12,6 +12,7 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "ffsparser.h" +#include #include #include #include @@ -1027,7 +1028,7 @@ USTATUS FfsParser::parseVolumeHeader(const UByteArray & volume, const UINT32 par UINT32 alignment = 65536; // Default volume alignment is 64K if (volumeHeader->Revision == 1) { // Acquire alignment capability bit - bool alignmentCap = volumeHeader->Attributes & EFI_FVB_ALIGNMENT_CAP; + BOOLEAN alignmentCap = volumeHeader->Attributes & EFI_FVB_ALIGNMENT_CAP; if (!alignmentCap) { if ((volumeHeader->Attributes & 0xFFFF0000)) msgAlignmentBitsSet = true; @@ -1230,7 +1231,7 @@ USTATUS FfsParser::getVolumeSize(const UByteArray & bios, UINT32 volumeOffset, U entry += 1; } - volumeSize = volumeHeader->FvLength; + volumeSize = (UINT32)volumeHeader->FvLength; bmVolumeSize = calcVolumeSize; if (volumeSize == 0) @@ -1604,7 +1605,7 @@ USTATUS FfsParser::parseFileHeader(const UByteArray & file, const UINT32 parentO } // Construct parsing data - bool fixed = fileHeader->Attributes & FFS_ATTRIB_FIXED; + bool fixed(fileHeader->Attributes & FFS_ATTRIB_FIXED); pdata.offset += parentOffset; @@ -2863,8 +2864,8 @@ USTATUS FfsParser::parseTeImageSectionBody(const UModelIndex & index) // Get data from parsing data PARSING_DATA pdata = parsingDataFromUModelIndex(index); - pdata.section.teImage.imageBase = teHeader->ImageBase; - pdata.section.teImage.adjustedImageBase = teHeader->ImageBase + teHeader->StrippedSize - sizeof(EFI_IMAGE_TE_HEADER); + pdata.section.teImage.imageBase = (UINT32)teHeader->ImageBase; + pdata.section.teImage.adjustedImageBase = (UINT32)(teHeader->ImageBase + teHeader->StrippedSize - sizeof(EFI_IMAGE_TE_HEADER)); // Update parsing data model->setParsingData(index, parsingDataToUByteArray(pdata)); @@ -3647,7 +3648,7 @@ USTATUS FfsParser::getStoreSize(const UByteArray & data, const UINT32 storeOffse } else { // Header with 64 bit WriteQueueSize const EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64* ftw64Header = (const EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64*)signature; - storeSize = sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64) + ftw64Header->WriteQueueSize; + storeSize = sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64) + (UINT32)ftw64Header->WriteQueueSize; } } else if (*signature == NVRAM_PHOENIX_FLASH_MAP_SIGNATURE_PART1) { // Phoenix SCT flash map @@ -3743,7 +3744,7 @@ USTATUS FfsParser::parseFtwStoreHeader(const UByteArray & store, const UINT32 pa has32bitHeader = true; } else { // Header with 64 bit WriteQueueSize - ftwBlockSize = sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64) + ftw64BlockHeader->WriteQueueSize; + ftwBlockSize = sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64) + (UINT32)ftw64BlockHeader->WriteQueueSize; has32bitHeader = false; } if (dataSize < ftwBlockSize) { diff --git a/common/ffsparser.h b/common/ffsparser.h index 5e4be11..e809e2b 100644 --- a/common/ffsparser.h +++ b/common/ffsparser.h @@ -17,7 +17,6 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "ustring.h" #include "ubytearray.h" -#include "umodelindex.h" #include "basetypes.h" #include "treemodel.h" #include "utility.h" diff --git a/common/ffsreport.h b/common/ffsreport.h index 5d671c6..c7415f9 100644 --- a/common/ffsreport.h +++ b/common/ffsreport.h @@ -18,7 +18,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "../common/ubytearray.h" #include "../common/ustring.h" -#include "../common/umodelindex.h" #include "basetypes.h" #include "treemodel.h" #include "ffs.h" diff --git a/common/fitparser.h b/common/fitparser.h index 4ebf663..3b4dac8 100644 --- a/common/fitparser.h +++ b/common/fitparser.h @@ -17,7 +17,6 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "ustring.h" #include "ubytearray.h" -#include "umodelindex.h" #include "treemodel.h" #include "utility.h" #include "parsingdata.h" diff --git a/common/treemodel.cpp b/common/treemodel.cpp index 486d8cc..f7969ab 100644 --- a/common/treemodel.cpp +++ b/common/treemodel.cpp @@ -14,25 +14,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "treeitem.h" #include "treemodel.h" -TreeModel::TreeModel(QObject *parent) - : QAbstractItemModel(parent) -{ - rootItem = new TreeItem(Types::Root, 0, UString(), UString(), UString(), UByteArray(), UByteArray(), UByteArray(), TRUE, FALSE, UByteArray()); -} - -TreeModel::~TreeModel() -{ - delete rootItem; -} - -int TreeModel::columnCount(const UModelIndex &parent) const -{ - if (parent.isValid()) - return static_cast(parent.internalPointer())->columnCount(); - else - return rootItem->columnCount(); -} - +#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL) QVariant TreeModel::data(const UModelIndex &index, int role) const { if (!index.isValid()) @@ -78,6 +60,53 @@ QVariant TreeModel::headerData(int section, Qt::Orientation orientation, return QVariant(); } +#else +UString TreeModel::data(const UModelIndex &index, int role) const +{ + if (!index.isValid()) + return UString(); + + if (role != 0 && role != 0x0100) + return UString(); + + TreeItem *item = static_cast(index.internalPointer()); + + if (role == 0) + return item->data(index.column()); + else + return item->info(); +} + +UString TreeModel::headerData(int section, int orientation, + int role) const +{ + if (orientation == 1 && role == 0) { + switch (section) + { + case 0: + return UString("Name"); + case 1: + return UString("Action"); + case 2: + return UString("Type"); + case 3: + return UString("Subtype"); + case 4: + return UString("Text"); + } + } + + return UString(); +} +#endif + +int TreeModel::columnCount(const UModelIndex &parent) const +{ + if (parent.isValid()) + return static_cast(parent.internalPointer())->columnCount(); + else + return rootItem->columnCount(); +} UModelIndex TreeModel::index(int row, int column, const UModelIndex &parent) const { diff --git a/common/treemodel.h b/common/treemodel.h index 58ed9d2..747be23 100644 --- a/common/treemodel.h +++ b/common/treemodel.h @@ -14,7 +14,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef TREEMODEL_H #define TREEMODEL_H +#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL) +// Use Qt classes #include +#include #include #include @@ -22,22 +25,103 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "ubytearray.h" #include "basetypes.h" #include "types.h" -#include "umodelindex.h" +#include "treeitem.h" -class TreeItem; +#define UModelIndex QModelIndex +#else +// Use own implementation +#include "ustring.h" +#include "ubytearray.h" +#include "basetypes.h" +#include "types.h" +#include "treeitem.h" +class UModelIndex +{ + friend class TreeModel; + +public: + inline UModelIndex() : r(-1), c(-1), i(0), m(0) {} + // compiler-generated copy/move ctors/assignment operators are fine! + inline int row() const { return r; } + inline int column() const { return c; } + inline uint64_t internalId() const { return i; } + inline void *internalPointer() const { return reinterpret_cast(i); } + inline UModelIndex parent() const; + inline UModelIndex child(int row, int column) const; + inline CBString data(int role) const; + inline const TreeModel *model() const { return m; } + inline bool isValid() const { return (r >= 0) && (c >= 0) && (m != 0); } + inline bool operator==(const UModelIndex &other) const { return (other.r == r) && (other.i == i) && (other.c == c) && (other.m == m); } + inline bool operator!=(const UModelIndex &other) const { return !(*this == other); } + inline bool operator<(const UModelIndex &other) const + { + return r < other.r + || (r == other.r && (c < other.c + || (c == other.c && (i < other.i + || (i == other.i && m < other.m))))); + } + +private: + inline UModelIndex(int arow, int acolumn, void *ptr, const TreeModel *amodel) + : r(arow), c(acolumn), i(reinterpret_cast(ptr)), m(amodel) {} + inline UModelIndex(int arow, int acolumn, uint64_t id, const TreeModel *amodel) + : r(arow), c(acolumn), i(id), m(amodel) {} + int r, c; + uint64_t i; + const TreeModel *m; +}; +#endif + +#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL) class TreeModel : public QAbstractItemModel { Q_OBJECT +private: + TreeItem *rootItem; public: - TreeModel(QObject *parent = 0); - ~TreeModel(); - QVariant data(const UModelIndex &index, int role) const; Qt::ItemFlags flags(const UModelIndex &index) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + TreeModel(QObject *parent = 0) : QAbstractItemModel(parent) { + rootItem = new TreeItem(Types::Root, 0, UString(), UString(), UString(), UByteArray(), UByteArray(), UByteArray(), TRUE, FALSE, UByteArray()); + } + +#else +#define emit + +class TreeModel +{ +private: + TreeItem *rootItem; + void dataChanged(const UModelIndex &, const UModelIndex &) {} + void layoutAboutToBeChanged() {} + void layoutChanged() {} + +public: + UString data(const UModelIndex &index, int role) const; + UString headerData(int section, int orientation, int role = 0) const; + + TreeModel() { + rootItem = new TreeItem(Types::Root, 0, UString(), UString(), UString(), UByteArray(), UByteArray(), UByteArray(), TRUE, FALSE, UByteArray()); + } + + bool hasIndex(int row, int column, const UModelIndex &parent = UModelIndex()) const { + if (row < 0 || column < 0) + return false; + return row < rowCount(parent) && column < columnCount(parent); + } + + UModelIndex createIndex(int row, int column, void *data) const { return UModelIndex(row, column, data, this); } +#endif + + + ~TreeModel() { + delete rootItem; + } + UModelIndex index(int row, int column, const UModelIndex &parent = UModelIndex()) const; UModelIndex parent(const UModelIndex &index) const; @@ -70,7 +154,6 @@ public: bool hasEmptyParsingData(const UModelIndex &index) const; UINT8 action(const UModelIndex &index) const; bool fixed(const UModelIndex &index) const; - bool compressed(const UModelIndex &index) const; UModelIndex addItem(const UINT8 type, const UINT8 subtype, @@ -80,9 +163,14 @@ public: const UModelIndex & parent = UModelIndex(), const UINT8 mode = CREATE_MODE_APPEND); UModelIndex findParentOfType(const UModelIndex & index, UINT8 type) const; - -private: - TreeItem *rootItem; }; +#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL) +// Nothing required here +#else +inline UModelIndex UModelIndex::parent() const { return m ? m->parent(*this) : UModelIndex(); } +inline UModelIndex UModelIndex::child(int row, int column) const { return m ? m->index(row, column, *this) : UModelIndex(); } +inline UString UModelIndex::data(int role) const { return m ? m->data(*this, role) : UString(); } +#endif + #endif // TREEMODEL_H diff --git a/common/ubytearray.h b/common/ubytearray.h index 03cd42f..9c1631f 100644 --- a/common/ubytearray.h +++ b/common/ubytearray.h @@ -13,8 +13,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef UBYTEARRAY_H #define UBYTEARRAY_H -//#define U_USE_QBYTEARRAY - #if defined(QT_CORE_LIB) && defined(U_USE_QBYTEARRAY) // Use Qt class, if Qt is available #include diff --git a/common/umodelindex.h b/common/umodelindex.h deleted file mode 100644 index 4f4c705..0000000 --- a/common/umodelindex.h +++ /dev/null @@ -1,26 +0,0 @@ -/* umodelindex.h - -Copyright (c) 2016, Nikolaj Schlej. All rights reserved. -This program and the accompanying materials -are licensed and made available under the terms and conditions of the BSD License -which accompanies this distribution. The full text of the license may be found at -http://opensource.org/licenses/bsd-license.php - -THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -*/ - -#ifndef UMODELINDEX_H -#define UMODELINDEX_H - -#if defined(QT_CORE_LIB) -// Use Qt class, if Qt is available -#include -#define UModelIndex QModelIndex -#else -// Use own implementation - - -#endif // QT_CORE_LIB -#endif // UMODELINDEX_H - diff --git a/common/utility.h b/common/utility.h index 7378924..8e02035 100644 --- a/common/utility.h +++ b/common/utility.h @@ -15,7 +15,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #define UTILITY_H #include "ustring.h" -#include "umodelindex.h" #include "basetypes.h" #include "parsingdata.h" From 9045fc6cc0a7a0f08493cb98acdc0f01b5c51822 Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Thu, 7 Jul 2016 07:57:45 +0200 Subject: [PATCH 08/12] Merge Qt/non-Qt codebase - added UEFIDump tool, which is now Windows-only UEFIExtract with some limitations, made as PoC for non-Qt engine usage - ensured that Qt classes will be used, if available - checked build of UT and UE - porting of UEFIFind to non-Qt engine TBD --- UEFIDump/CMakeLists.txt | 49 ++++++++ UEFIDump/uefidump.cpp | 234 ++++++++++++++++++++++++++++++++++++ UEFIDump/uefidump.h | 50 ++++++++ UEFIDump/uefidump_main.cpp | 43 +++++++ UEFIExtract/uefiextract.pro | 8 +- UEFIFind/uefifind.pro | 5 +- UEFITool/ffsfinder.cpp | 84 ++++++------- UEFITool/ffsfinder.h | 21 ++-- UEFITool/uefitool.cpp | 26 ++-- UEFITool/uefitool.pro | 7 +- common/LZMA/LzmaCompress.c | 6 +- common/ffsbuilder.cpp | 182 ++++++++++++++-------------- common/ffsbuilder.h | 39 +++--- common/ffsops.cpp | 52 ++++---- common/ffsops.h | 23 ++-- common/ffsparser.cpp | 4 +- common/treemodel.cpp | 2 +- common/treemodel.h | 6 +- common/ubytearray.h | 14 +-- common/ustring.cpp | 4 +- common/ustring.h | 2 +- common/utility.h | 1 + 22 files changed, 612 insertions(+), 250 deletions(-) create mode 100644 UEFIDump/CMakeLists.txt create mode 100644 UEFIDump/uefidump.cpp create mode 100644 UEFIDump/uefidump.h create mode 100644 UEFIDump/uefidump_main.cpp diff --git a/UEFIDump/CMakeLists.txt b/UEFIDump/CMakeLists.txt new file mode 100644 index 0000000..e69b7e1 --- /dev/null +++ b/UEFIDump/CMakeLists.txt @@ -0,0 +1,49 @@ +PROJECT(UEFIDump) + +SET(PROJECT_SOURCES + uefidump_main.cpp + uefidump.cpp + ../common/types.cpp + ../common/descriptor.cpp + ../common/ffs.cpp + ../common/nvram.cpp + ../common/ffsparser.cpp + ../common/ffsreport.cpp + ../common/fitparser.cpp + ../common/peimage.cpp + ../common/treeitem.cpp + ../common/treemodel.cpp + ../common/utility.cpp + ../common/LZMA/LzmaDecompress.c + ../common/LZMA/SDK/C/LzmaDec.c + ../common/Tiano/EfiTianoDecompress.c + ../common/ustring.cpp + ../bstrlib/bstrlib.c + ../bstrlib/bstrwrap.cpp +) + +SET(PROJECT_HEADERS + uefidump.h + ../common/basetypes.h + ../common/descriptor.h + ../common/gbe.h + ../common/me.h + ../common/ffs.h + ../common/nvram.h + ../common/ffsparser.h + ../common/ffsreport.h + ../common/fitparser.h + ../common/peimage.h + ../common/types.h + ../common/treeitem.h + ../common/treemodel.h + ../common/utility.h + ../common/LZMA/LzmaDecompress.h + ../common/Tiano/EfiTianoDecompress.h + ../common/ubytearray.h + ../common/ustring.h + ../bstrlib/bstrlib.h + ../bstrlib/bstrwrap.h +) + +ADD_EXECUTABLE(UEFIDump ${PROJECT_SOURCES} ${PROJECT_HEADERS}) \ No newline at end of file diff --git a/UEFIDump/uefidump.cpp b/UEFIDump/uefidump.cpp new file mode 100644 index 0000000..08816e8 --- /dev/null +++ b/UEFIDump/uefidump.cpp @@ -0,0 +1,234 @@ +/* ffsdumper.cpp + +Copyright (c) 2015, Nikolaj Schlej. All rights reserved. +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +*/ + +#include + +#include "uefidump.h" +#include "../common/ffs.h" +#include +#include +#include +#include + +USTATUS UEFIDumper::dump(const UByteArray & buffer, const std::wstring & inPath, const std::wstring & guid) +{ + //TODO: rework to support relative paths + std::wstring path = std::wstring(inPath).append(L".dump2"); + path = L"\\\\?\\" + path; + std::wstring reportPath = std::wstring(inPath).append(L".report2.txt"); + reportPath = L"\\\\?\\" + reportPath; + std::wcout << L"Dump path: " << path << std::endl; + std::wcout << L"Report path: " << reportPath << std::endl; + + + if (initialized) { + // Check if called with a different buffer as before + if (buffer != currentBuffer) { + // Reinitalize if so + initialized = false; + } + } + + if (!initialized) { + // Fill currentBuffer + currentBuffer = buffer; + + // Parse FFS structure + USTATUS result = ffsParser.parse(buffer); + if (result) + return result; + // Show ffsParser messages + std::vector > messages = ffsParser.getMessages(); + for (size_t i = 0; i < messages.size(); i++) { + std::cout << messages[i].first << std::endl; + } + + // Get last VTF + UModelIndex lastVtf = ffsParser.getLastVtf(); + if (lastVtf.isValid()) { + // Create fitParser + FitParser fitParser(&model); + // Find and parse FIT table + result = fitParser.parse(model.index(0, 0), lastVtf); + if (U_SUCCESS == result) { + // Show fitParser's messages + std::vector > fitMessages = fitParser.getMessages(); + for (size_t i = 0; i < fitMessages.size(); i++) { + std::cout << (const char*)fitMessages[i].first.toLocal8Bit() << std::endl; + } + + // Show FIT table + std::vector > fitTable = fitParser.getFitTable(); + if (fitTable.size()) { + std::cout << "-------------------------------------------------------------------" << std::endl; + std::cout << " Address | Size | Ver | Type | CS " << std::endl; + std::cout << "-------------------------------------------------------------------" << std::endl; + for (size_t i = 0; i < fitTable.size(); i++) { + std::cout << (const char*)fitTable[i][0].toLocal8Bit() << " | " + << (const char*)fitTable[i][1].toLocal8Bit() << " | " + << (const char*)fitTable[i][2].toLocal8Bit() << " | " + << (const char*)fitTable[i][3].toLocal8Bit() << " | " + << (const char*)fitTable[i][4].toLocal8Bit() << std::endl; + } + } + } + } + + // Create ffsReport + FfsReport ffsReport(&model); + std::vector report = ffsReport.generate(); + if (report.size()) { + std::ofstream ofs; + ofs.open(reportPath, std::ofstream::out); + for (size_t i = 0; i < report.size(); i++) { + ofs << (const char*)report[i].toLocal8Bit() << std::endl; + } + ofs.close(); + } + + initialized = true; + } + + dumped = false; + UINT8 result = recursiveDump(model.index(0,0), path, guid); + if (result) + return result; + else if (!dumped) + return U_ITEM_NOT_FOUND; + + return U_SUCCESS; +} + +std::wstring UEFIDumper::guidToWstring(const EFI_GUID & guid) +{ + std::wstringstream ws; + + ws << std::hex << std::uppercase << std::setfill(L'0'); + ws << std::setw(8) << *(const UINT32*)&guid.Data[0] << L"-"; + ws << std::setw(4) << *(const UINT16*)&guid.Data[4] << L"-"; + ws << std::setw(4) << *(const UINT16*)&guid.Data[6] << L"-"; + ws << std::setw(2) << guid.Data[8]; + ws << std::setw(2) << guid.Data[9] << L"-"; + ws << std::setw(2) << guid.Data[10]; + ws << std::setw(2) << guid.Data[11]; + ws << std::setw(2) << guid.Data[12]; + ws << std::setw(2) << guid.Data[13]; + ws << std::setw(2) << guid.Data[14]; + ws << std::setw(2) << guid.Data[15]; + + return ws.str(); +} + +bool UEFIDumper::createFullPath(const std::wstring & path) { + + // Break the path into parent\current, assuming the path is already full and converted into Windows native "\\?\" format + size_t pos = path.find_last_of(L'\\'); + + // Slash is not found, it's a bug + if (pos == path.npos) + return FALSE; + + std::wstring parent = path.substr(0, pos); + std::wstring current = path.substr(pos + 1); + + // Check if first exist, if so, create last and return true + UINT32 attributes = GetFileAttributesW(parent.c_str()); + if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) { + // first is already exist, just create last + return CreateDirectoryW(path.c_str(), NULL) != 0; + } + + // Perform recursive call + if (createFullPath(parent)) + return CreateDirectoryW(path.c_str(), NULL) != 0; + + return FALSE; +} + +USTATUS UEFIDumper::recursiveDump(const UModelIndex & index, const std::wstring & path, const std::wstring & guid) +{ + if (!index.isValid()) + return U_INVALID_PARAMETER; + + UByteArray itemHeader = model.header(index); + UByteArray fileHeader = model.header(model.findParentOfType(index, Types::File)); + + if (guid.length() == 0 || + (itemHeader.size() >= sizeof (EFI_GUID) && guidToWstring(*(const EFI_GUID*)itemHeader.constData()) == guid) || + (fileHeader.size() >= sizeof(EFI_GUID) && guidToWstring(*(const EFI_GUID*)fileHeader.constData()) == guid)) { + + if (SetCurrentDirectoryW(path.c_str())) + return U_DIR_ALREADY_EXIST; + + if (!createFullPath(path)) + return U_DIR_CREATE; + + //if (model.rowCount(index) == 0) { + // Header + UByteArray data = model.header(index); + if (!data.isEmpty()) { + std::ofstream file; + std::wstring name = path + std::wstring(L"\\header.bin"); + file.open(name, std::ios::out | std::ios::binary); + file.write(data.constData(), data.size()); + file.close(); + } + + // Body + data = model.body(index); + if (!data.isEmpty()) { + std::ofstream file; + std::wstring name = path + std::wstring(L"\\body.bin"); + file.open(name, std::ios::out | std::ios::binary); + file.write(data.constData(), data.size()); + file.close(); + } + //} + // Info + UString info = "Type: " + itemTypeToUString(model.type(index)) + "\n" + + "Subtype: " + itemSubtypeToUString(model.type(index), model.subtype(index)) + "\n"; + if (model.text(index).length() > 0) + info += "Text: " + model.text(index) + "\n"; + info += model.info(index) + "\n"; + + std::ofstream file; + std::wstring name = path + std::wstring(L"\\info.txt"); + file.open(name, std::ios::out); + file.write((const char*)info, info.length()); + file.close(); + + dumped = true; + } + + UINT8 result; + for (int i = 0; i < model.rowCount(index); i++) { + UModelIndex childIndex = index.child(i, 0); + bool useText = false; + if (model.type(childIndex) != Types::Volume) + useText = (model.text(childIndex).length() > 0); + + UString name = useText ? (const char *)model.text(childIndex) : (const char *)model.name(childIndex); + std::string sName = std::string((const char*)name, name.length()); + std::wstring childPath = path + std::wstring(L"\\") + std::to_wstring(i) + std::wstring(L" ") + std::wstring(sName.begin(), sName.end()); + + // Workaround for paths with dot at the end, just add remove it + if (childPath.at(childPath.length() - 1) == L'.') + childPath.pop_back(); + + result = recursiveDump(childIndex, childPath, guid); + if (result) + return result; + } + + return U_SUCCESS; +} diff --git a/UEFIDump/uefidump.h b/UEFIDump/uefidump.h new file mode 100644 index 0000000..be456f4 --- /dev/null +++ b/UEFIDump/uefidump.h @@ -0,0 +1,50 @@ +/* uefidump.h + +Copyright (c) 2016, Nikolaj Schlej. All rights reserved. +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +*/ + +#ifndef UEFIDUMP_H +#define UEFIDUMP_H + +#include + +#include "../common/basetypes.h" +#include "../common/ustring.h" +#include "../common/treemodel.h" +#include "../common/ffsparser.h" +#include "../common/ffsreport.h" +#include "../common/fitparser.h" + + +class UEFIDumper +{ +public: + explicit UEFIDumper() : model(), ffsParser(&model), ffsReport(&model), fitParser(&model), currentBuffer(), initialized(false), dumped(false) {} + ~UEFIDumper() {} + + USTATUS dump(const UByteArray & buffer, const std::wstring & path, const std::wstring & guid = std::wstring()); + +private: + USTATUS recursiveDump(const UModelIndex & root, const std::wstring & path, const std::wstring & guid); + std::wstring guidToWstring(const EFI_GUID & guid); + bool createFullPath(const std::wstring & path); + + TreeModel model; + FfsParser ffsParser; + FfsReport ffsReport; + FitParser fitParser; + + UByteArray currentBuffer; + bool initialized; + bool dumped; +}; + +#endif diff --git a/UEFIDump/uefidump_main.cpp b/UEFIDump/uefidump_main.cpp new file mode 100644 index 0000000..af160a7 --- /dev/null +++ b/UEFIDump/uefidump_main.cpp @@ -0,0 +1,43 @@ +/* uefidump_main.cpp + +Copyright (c) 2016, Nikolaj Schlej. All rights reserved. +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +*/ +#include +#include + +#include "uefidump.h" + +int wmain(int argc, wchar_t *argv[]) +{ + if (argc > 32) { + std::cout << "Too many arguments" << std::endl; + return 1; + } + + if (argc > 1) { + std::ifstream inputFile; + inputFile.open(argv[1], std::ios::in | std::ios::binary); + std::vector buffer(std::istreambuf_iterator(inputFile), + (std::istreambuf_iterator())); + inputFile.close(); + + UEFIDumper uefidumper; + return (uefidumper.dump(buffer, std::wstring(argv[1])) != U_SUCCESS); + } + else { + std::cout << "UEFIDump 0.1.0" << std::endl << std::endl + << "Usage: UEFIExtract imagefile " << std::endl + << "Return value is a bit mask where 0 at position N means that file with GUID_N was found and unpacked, 1 otherwise" << std::endl; + return 1; + } + + return 1; +} diff --git a/UEFIExtract/uefiextract.pro b/UEFIExtract/uefiextract.pro index 4cc6273..32e7642 100644 --- a/UEFIExtract/uefiextract.pro +++ b/UEFIExtract/uefiextract.pro @@ -23,9 +23,7 @@ SOURCES += \ ../common/LZMA/LzmaDecompress.c \ ../common/LZMA/SDK/C/LzmaDec.c \ ../common/Tiano/EfiTianoDecompress.c \ - ../common/ustring.cpp \ - ../bstrlib/bstrlib.c \ - ../bstrlib/bstrwrap.cpp + ../common/ustring.cpp HEADERS += \ ffsdumper.h \ @@ -46,7 +44,5 @@ HEADERS += \ ../common/LZMA/LzmaDecompress.h \ ../common/Tiano/EfiTianoDecompress.h \ ../common/ubytearray.h \ - ../common/ustring.h \ - ../bstrlib/bstrlib.h \ - ../bstrlib/bstrwrap.h + ../common/ustring.h diff --git a/UEFIFind/uefifind.pro b/UEFIFind/uefifind.pro index ff6ffad..30b7374 100644 --- a/UEFIFind/uefifind.pro +++ b/UEFIFind/uefifind.pro @@ -20,6 +20,7 @@ SOURCES += uefifind_main.cpp \ ../common/LZMA/LzmaDecompress.c \ ../common/LZMA/SDK/C/LzmaDec.c \ ../common/Tiano/EfiTianoDecompress.c \ + ../common/ustring.cpp HEADERS += uefifind.h \ ../common/basetypes.h \ @@ -35,5 +36,7 @@ HEADERS += uefifind.h \ ../common/treemodel.h \ ../common/utility.h \ ../common/LZMA/LzmaDecompress.h \ - ../common/Tiano/EfiTianoDecompress.h + ../common/Tiano/EfiTianoDecompress.h \ + ../common/ustring.h \ + ../common/ubytearray.h diff --git a/UEFITool/ffsfinder.cpp b/UEFITool/ffsfinder.cpp index d53878d..20e6c8e 100644 --- a/UEFITool/ffsfinder.cpp +++ b/UEFITool/ffsfinder.cpp @@ -13,24 +13,24 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "ffsfinder.h" -STATUS FfsFinder::findHexPattern(const QModelIndex & index, const QByteArray & hexPattern, const UINT8 mode) +USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray & hexPattern, const UINT8 mode) { if (!index.isValid()) - return ERR_SUCCESS; + return U_SUCCESS; if (hexPattern.isEmpty()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Check for "all substrings" pattern if (hexPattern.count('.') == hexPattern.length()) - return ERR_SUCCESS; + return U_SUCCESS; bool hasChildren = (model->rowCount(index) > 0); for (int i = 0; i < model->rowCount(index); i++) { findHexPattern(index.child(i, index.column()), hexPattern, mode); } - QByteArray data; + UByteArray data; if (hasChildren) { if (mode != SEARCH_MODE_BODY) data = model->header(index); @@ -44,39 +44,37 @@ STATUS FfsFinder::findHexPattern(const QModelIndex & index, const QByteArray & h data.append(model->header(index)).append(model->body(index)); } - QString hexBody = QString(data.toHex()); - QRegExp regexp = QRegExp(QString(hexPattern), Qt::CaseInsensitive); + UString hexBody = UString(data.toHex()); + QRegExp regexp = QRegExp(UString(hexPattern), Qt::CaseInsensitive); INT32 offset = regexp.indexIn(hexBody); while (offset >= 0) { if (offset % 2 == 0) { - msg(QObject::tr("Hex pattern \"%1\" found as \"%2\" in %3 at %4-offset %5h") - .arg(QString(hexPattern)) - .arg(hexBody.mid(offset, hexPattern.length()).toUpper()) - .arg(model->name(index)) - .arg(mode == SEARCH_MODE_BODY ? QObject::tr("body") : QObject::tr("header")) - .hexarg(offset / 2), + msg(UString("Hex pattern \"") + UString(hexPattern) + + UString("\" found as \"") + hexBody.mid(offset, hexPattern.length()).toUpper() + + UString("\" in ") + model->name(index) + + usprintf(" at %s-offset %02Xh", mode == SEARCH_MODE_BODY ? "body" : "header", offset / 2), index); } offset = regexp.indexIn(hexBody, offset + 1); } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsFinder::findGuidPattern(const QModelIndex & index, const QByteArray & guidPattern, const UINT8 mode) +USTATUS FfsFinder::findGuidPattern(const UModelIndex & index, const UByteArray & guidPattern, const UINT8 mode) { if (guidPattern.isEmpty()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; if (!index.isValid()) - return ERR_SUCCESS; + return U_SUCCESS; bool hasChildren = (model->rowCount(index) > 0); for (int i = 0; i < model->rowCount(index); i++) { findGuidPattern(index.child(i, index.column()), guidPattern, mode); } - QByteArray data; + UByteArray data; if (hasChildren) { if (mode != SEARCH_MODE_BODY) data = model->header(index); @@ -90,12 +88,12 @@ STATUS FfsFinder::findGuidPattern(const QModelIndex & index, const QByteArray & data.append(model->header(index)).append(model->body(index)); } - QString hexBody = QString(data.toHex()); - QList list = guidPattern.split('-'); + UString hexBody = UString(data.toHex()); + QList list = guidPattern.split('-'); if (list.count() != 5) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; - QByteArray hexPattern; + UByteArray hexPattern; // Reverse first GUID block hexPattern.append(list.at(0).mid(6, 2)); hexPattern.append(list.at(0).mid(4, 2)); @@ -112,40 +110,38 @@ STATUS FfsFinder::findGuidPattern(const QModelIndex & index, const QByteArray & // Check for "all substrings" pattern if (hexPattern.count('.') == hexPattern.length()) - return ERR_SUCCESS; + return U_SUCCESS; - QRegExp regexp(QString(hexPattern), Qt::CaseInsensitive); + QRegExp regexp(UString(hexPattern), Qt::CaseInsensitive); INT32 offset = regexp.indexIn(hexBody); while (offset >= 0) { if (offset % 2 == 0) { - msg(QObject::tr("GUID pattern \"%1\" found as \"%2\" in %3 at %4-offset %5h") - .arg(QString(guidPattern)) - .arg(hexBody.mid(offset, hexPattern.length()).toUpper()) - .arg(model->name(index)) - .arg(mode == SEARCH_MODE_BODY ? QObject::tr("body") : QObject::tr("header")) - .hexarg(offset / 2), + msg(UString("GUID pattern \"") + UString(guidPattern) + + UString("\" found as \"") + hexBody.mid(offset, hexPattern.length()).toUpper() + + UString("\" in ") + model->name(index) + + usprintf(" at %s-offset %02Xh", mode == SEARCH_MODE_BODY ? "body" : "header", offset / 2), index); } offset = regexp.indexIn(hexBody, offset + 1); } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsFinder::findTextPattern(const QModelIndex & index, const QString & pattern, const UINT8 mode, const bool unicode, const Qt::CaseSensitivity caseSensitive) +USTATUS FfsFinder::findTextPattern(const UModelIndex & index, const UString & pattern, const UINT8 mode, const bool unicode, const Qt::CaseSensitivity caseSensitive) { if (pattern.isEmpty()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; if (!index.isValid()) - return ERR_SUCCESS; + return U_SUCCESS; bool hasChildren = (model->rowCount(index) > 0); for (int i = 0; i < model->rowCount(index); i++) { findTextPattern(index.child(i, index.column()), pattern, mode, unicode, caseSensitive); } - QByteArray body; + UByteArray body; if (hasChildren) { if (mode != SEARCH_MODE_BODY) body = model->header(index); @@ -159,22 +155,20 @@ STATUS FfsFinder::findTextPattern(const QModelIndex & index, const QString & pat body.append(model->header(index)).append(model->body(index)); } - QString data; + UString data; if (unicode) - data = QString::fromUtf16((const ushort*)body.constData(), body.length() / 2); + data = UString::fromUtf16((const ushort*)body.constData(), body.length() / 2); else - data = QString::fromLatin1((const char*)body.constData(), body.length()); + data = UString::fromLatin1((const char*)body.constData(), body.length()); int offset = -1; while ((offset = data.indexOf(pattern, offset + 1, caseSensitive)) >= 0) { - msg(QObject::tr("%1 text \"%2\" found in %3 at %4-offset %5h") - .arg(unicode ? "Unicode" : "ASCII") - .arg(pattern) - .arg(model->name(index)) - .arg(mode == SEARCH_MODE_BODY ? QObject::tr("body") : QObject::tr("header")) - .hexarg(unicode ? offset * 2 : offset), + + msg((unicode ? UString("Unicode") : UString("ASCII")) + UString(" text \"") + UString(pattern) + + UString("\" found in ") + model->name(index) + + usprintf(" at %s-offset %02Xh", mode == SEARCH_MODE_BODY ? "body" : "header", (unicode ? offset * 2 : offset)), index); } - return ERR_SUCCESS; + return U_SUCCESS; } diff --git a/UEFITool/ffsfinder.h b/UEFITool/ffsfinder.h index c341a1f..b88866b 100644 --- a/UEFITool/ffsfinder.h +++ b/UEFITool/ffsfinder.h @@ -15,13 +15,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #define FFSFINDER_H #include - -#include -#include -#include -#include #include +#include "../common/ubytearray.h" +#include "../common/ustring.h" #include "../common/basetypes.h" #include "../common/treemodel.h" @@ -31,19 +28,19 @@ public: FfsFinder(const TreeModel * treeModel) : model(treeModel) {} ~FfsFinder() {} - std::vector > getMessages() const { return messagesVector; } + std::vector > getMessages() const { return messagesVector; } void clearMessages() { messagesVector.clear(); } - STATUS findHexPattern(const QModelIndex & index, const QByteArray & hexPattern, const UINT8 mode); - STATUS findGuidPattern(const QModelIndex & index, const QByteArray & guidPattern, const UINT8 mode); - STATUS findTextPattern(const QModelIndex & index, const QString & pattern, const UINT8 mode, const bool unicode, const Qt::CaseSensitivity caseSensitive); + USTATUS findHexPattern(const UModelIndex & index, const UByteArray & hexPattern, const UINT8 mode); + USTATUS findGuidPattern(const UModelIndex & index, const UByteArray & guidPattern, const UINT8 mode); + USTATUS findTextPattern(const UModelIndex & index, const UString & pattern, const UINT8 mode, const bool unicode, const Qt::CaseSensitivity caseSensitive); private: const TreeModel* model; - std::vector > messagesVector; + std::vector > messagesVector; - void msg(const QString & message, const QModelIndex &index = QModelIndex()) { - messagesVector.push_back(std::pair(message, index)); + void msg(const UString & message, const UModelIndex &index = UModelIndex()) { + messagesVector.push_back(std::pair(message, index)); } }; diff --git a/UEFITool/uefitool.cpp b/UEFITool/uefitool.cpp index a7fee01..2b5e6d3 100644 --- a/UEFITool/uefitool.cpp +++ b/UEFITool/uefitool.cpp @@ -17,7 +17,7 @@ UEFITool::UEFITool(QWidget *parent) : QMainWindow(parent), ui(new Ui::UEFITool), -version(tr("0.30.0_alpha28")) +version(tr("0.30.0_alpha29")) { clipboard = QApplication::clipboard(); @@ -211,7 +211,7 @@ bool UEFITool::enableExtractBodyUncompressed(const QModelIndex ¤t) if (current.isValid() && model->type(current) == Types::Section && (model->subtype(current) == EFI_SECTION_COMPRESSION || model->subtype(current) == EFI_SECTION_GUID_DEFINED)) { // Get parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(current); + PARSING_DATA pdata = parsingDataFromUModelIndex(current); if (model->subtype(current) == EFI_SECTION_COMPRESSION && pdata.section.compressed.algorithm != COMPRESSION_ALGORITHM_NONE && @@ -297,7 +297,7 @@ void UEFITool::goToData() QModelIndex parent = model->parent(index); for (int i = index.row(); i < model->rowCount(parent); i++) { - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); UINT32 lastVariableFlag = pdata.emptyByte ? 0xFFFFFF : 0; if (pdata.nvar.next == lastVariableFlag) { ui->structureTreeView->scrollTo(index, QAbstractItemView::PositionAtCenter); @@ -306,7 +306,7 @@ void UEFITool::goToData() for (int j = i + 1; j < model->rowCount(parent); j++) { QModelIndex currentIndex = parent.child(j, 0); - PARSING_DATA currentPdata = parsingDataFromQModelIndex(currentIndex); + PARSING_DATA currentPdata = parsingDataFromUModelIndex(currentIndex); if (currentPdata.offset == pdata.offset + pdata.nvar.next) { index = currentIndex; break; @@ -481,7 +481,7 @@ void UEFITool::replace(const UINT8 mode) UINT8 result = ffsOps->replace(index, buffer, mode); if (result) { - QMessageBox::critical(this, tr("Replacing failed"), errorCodeToQString(result), QMessageBox::Ok); + QMessageBox::critical(this, tr("Replacing failed"), errorCodeToUString(result), QMessageBox::Ok); return; } ui->actionSaveImageFile->setEnabled(true); @@ -512,7 +512,7 @@ void UEFITool::extract(const UINT8 mode) QString name; UINT8 result = ffsOps->extract(index, name, extracted, mode); if (result) { - QMessageBox::critical(this, tr("Extraction failed"), errorCodeToQString(result), QMessageBox::Ok); + QMessageBox::critical(this, tr("Extraction failed"), errorCodeToUString(result), QMessageBox::Ok); return; } @@ -609,21 +609,21 @@ void UEFITool::extract(const UINT8 mode) void UEFITool::rebuild() { - QModelIndex index = ui->structureTreeView->selectionModel()->currentIndex(); + UModelIndex index = ui->structureTreeView->selectionModel()->currentIndex(); if (!index.isValid()) return; - if (ERR_SUCCESS == ffsOps->rebuild(index)) + if (U_SUCCESS == ffsOps->rebuild(index)) ui->actionSaveImageFile->setEnabled(true); } void UEFITool::remove() { - QModelIndex index = ui->structureTreeView->selectionModel()->currentIndex(); + UModelIndex index = ui->structureTreeView->selectionModel()->currentIndex(); if (!index.isValid()) return; - if (ERR_SUCCESS == ffsOps->remove(index)) + if (U_SUCCESS == ffsOps->remove(index)) ui->actionSaveImageFile->setEnabled(true); } @@ -661,10 +661,10 @@ void UEFITool::saveImageFile() // Create ffsBuilder delete ffsBuilder; ffsBuilder = new FfsBuilder(model); - STATUS result = ffsBuilder->build(model->index(0,0), reconstructed); + USTATUS result = ffsBuilder->build(model->index(0,0), reconstructed); showBuilderMessages(); if (result) { - QMessageBox::critical(this, tr("Image build failed"), errorCodeToQString(result), QMessageBox::Ok); + QMessageBox::critical(this, tr("Image build failed"), errorCodeToUString(result), QMessageBox::Ok); return; } @@ -725,7 +725,7 @@ void UEFITool::openImageFile(QString path) UINT8 result = ffsParser->parse(buffer); showParserMessages(); if (result) { - QMessageBox::critical(this, tr("Image parsing failed"), errorCodeToQString(result), QMessageBox::Ok); + QMessageBox::critical(this, tr("Image parsing failed"), errorCodeToUString(result), QMessageBox::Ok); return; } else diff --git a/UEFITool/uefitool.pro b/UEFITool/uefitool.pro index 106bf47..0921366 100644 --- a/UEFITool/uefitool.pro +++ b/UEFITool/uefitool.pro @@ -29,7 +29,8 @@ SOURCES += uefitool_main.cpp \ ../common/LZMA/SDK/C/LzmaEnc.c \ ../common/Tiano/EfiTianoDecompress.c \ ../common/Tiano/EfiTianoCompress.c \ - ../common/Tiano/EfiTianoCompressLegacy.c + ../common/Tiano/EfiTianoCompressLegacy.c \ + ../common/ustring.cpp \ HEADERS += uefitool.h \ searchdialog.h \ @@ -56,7 +57,9 @@ HEADERS += uefitool.h \ ../common/LZMA/LzmaCompress.h \ ../common/LZMA/LzmaDecompress.h \ ../common/Tiano/EfiTianoDecompress.h \ - ../common/Tiano/EfiTianoCompress.h + ../common/Tiano/EfiTianoCompress.h \ + ../common/ustring.h \ + ../common/ubytearray.h \ FORMS += uefitool.ui \ searchdialog.ui diff --git a/common/LZMA/LzmaCompress.c b/common/LZMA/LzmaCompress.c index 622ca86..8ae345c 100644 --- a/common/LZMA/LzmaCompress.c +++ b/common/LZMA/LzmaCompress.c @@ -75,7 +75,7 @@ UINT32 *DestinationSize if (*DestinationSize < destLen) { *DestinationSize = destLen; - return ERR_BUFFER_TOO_SMALL; + return EFI_BUFFER_TOO_SMALL; } LzmaEncProps_Init(&props); @@ -101,9 +101,9 @@ UINT32 *DestinationSize SetEncodedSizeOfBuf((UINT64)SourceSize, Destination); if (LzmaResult == SZ_OK) { - return ERR_SUCCESS; + return EFI_SUCCESS; } else { - return ERR_INVALID_PARAMETER; + return EFI_INVALID_PARAMETER; } } diff --git a/common/ffsbuilder.cpp b/common/ffsbuilder.cpp index 46d97c4..1b4b9a1 100644 --- a/common/ffsbuilder.cpp +++ b/common/ffsbuilder.cpp @@ -12,22 +12,22 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. */ #include "ffsbuilder.h" -STATUS FfsBuilder::erase(const QModelIndex & index, QByteArray & erased) +USTATUS FfsBuilder::erase(const UModelIndex & index, UByteArray & erased) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); erased.fill(pdata.emptyByte); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsBuilder::build(const QModelIndex & root, QByteArray & image) +USTATUS FfsBuilder::build(const UModelIndex & root, UByteArray & image) { // Sanity check if (!root.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; if (model->type(root) == Types::Capsule) { return buildCapsule(root, image); @@ -41,44 +41,44 @@ STATUS FfsBuilder::build(const QModelIndex & root, QByteArray & image) } } - return ERR_NOT_IMPLEMENTED; + return U_NOT_IMPLEMENTED; } -STATUS FfsBuilder::buildCapsule(const QModelIndex & index, QByteArray & capsule) +USTATUS FfsBuilder::buildCapsule(const UModelIndex & index, UByteArray & capsule) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // No action required if (model->action(index) == Actions::NoAction) { // Use original item data capsule = model->header(index).append(model->body(index)); - return ERR_SUCCESS; + return U_SUCCESS; } // Rebuild or Replace else if (model->action(index) == Actions::Rebuild || model->action(index) == Actions::Replace) { if (model->rowCount(index)) { - // Clear the supplied QByteArray + // Clear the supplied UByteArray capsule.clear(); // Right now there is only one capsule image element supported if (model->rowCount(index) != 1) { - msg(QObject::tr("buildCapsule: building of capsules with %1 elements are not supported, original item data is used").arg(model->rowCount(index)), index); + //msg(UString("buildCapsule: building of capsules with %1 elements are not supported, original item data is used").arg(model->rowCount(index)), index); // Use original item data capsule = model->header(index).append(model->body(index)); - return ERR_SUCCESS; + return U_SUCCESS; } // Build image - QModelIndex imageIndex = index.child(0, 0); - QByteArray imageData; + UModelIndex imageIndex = index.child(0, 0); + UByteArray imageData; // Check image type if (model->type(imageIndex) == Types::Image) { - STATUS result = ERR_SUCCESS; + USTATUS result = U_SUCCESS; if (model->subtype(imageIndex) == Subtypes::IntelImage) { result = buildIntelImage(imageIndex, imageData); } @@ -86,20 +86,20 @@ STATUS FfsBuilder::buildCapsule(const QModelIndex & index, QByteArray & capsule) result = buildRawArea(imageIndex, imageData); } else { - msg(QObject::tr("buildCapsule: unexpected item of subtype %1 can't be processed, original item data is used").arg(model->subtype(imageIndex)), imageIndex); + //msg(UString("buildCapsule: unexpected item of subtype %1 can't be processed, original item data is used").arg(model->subtype(imageIndex)), imageIndex); capsule.append(model->header(imageIndex)).append(model->body(imageIndex)); } // Check build result if (result) { - msg(QObject::tr("buildCapsule: building of \"%1\" failed with error \"%2\", original item data is used").arg(model->name(imageIndex)).arg(errorCodeToQString(result)), imageIndex); + //msg(UString("buildCapsule: building of \"%1\" failed with error \"%2\", original item data is used").arg(model->name(imageIndex)).arg(errorCodeToUString(result)), imageIndex); capsule.append(model->header(imageIndex)).append(model->body(imageIndex)); } else capsule.append(imageData); } else { - msg(QObject::tr("buildCapsule: unexpected item of type %1 can't be processed, original item data is used").arg(model->type(imageIndex)), imageIndex); + //msg(UString("buildCapsule: unexpected item of type %1 can't be processed, original item data is used").arg(model->type(imageIndex)), imageIndex); capsule.append(model->header(imageIndex)).append(model->body(imageIndex)); } @@ -107,14 +107,14 @@ STATUS FfsBuilder::buildCapsule(const QModelIndex & index, QByteArray & capsule) UINT32 newSize = capsule.size(); UINT32 oldSize = model->body(index).size(); if (newSize > oldSize) { - msg(QObject::tr("buildCapsule: new capsule body size %1h (%2) is bigger than the original %3h (%4)") - .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize),index); - return ERR_INVALID_PARAMETER; + //msg(UString("buildCapsule: new capsule body size %1h (%2) is bigger than the original %3h (%4)") + // .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize),index); + return U_INVALID_PARAMETER; } else if (newSize < oldSize) { - msg(QObject::tr("buildCapsule: new capsule body size %1h (%2) is smaller than the original %3h (%4)") - .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index); - return ERR_INVALID_PARAMETER; + //msg(UString("buildCapsule: new capsule body size %1h (%2) is smaller than the original %3h (%4)") + // .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index); + return U_INVALID_PARAMETER; } } else @@ -122,23 +122,23 @@ STATUS FfsBuilder::buildCapsule(const QModelIndex & index, QByteArray & capsule) // Build successful, append header capsule = model->header(index).append(capsule); - return ERR_SUCCESS; + return U_SUCCESS; } - msg(QObject::tr("buildCapsule: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index); - return ERR_NOT_IMPLEMENTED; + //msg(UString("buildCapsule: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index); + return U_NOT_IMPLEMENTED; } -STATUS FfsBuilder::buildIntelImage(const QModelIndex & index, QByteArray & intelImage) +USTATUS FfsBuilder::buildIntelImage(const UModelIndex & index, UByteArray & intelImage) { // Sanity check if (!index.isValid()) - return ERR_SUCCESS; + return U_SUCCESS; // No action if (model->action(index) == Actions::NoAction) { intelImage = model->header(index).append(model->body(index)); - return ERR_SUCCESS; + return U_SUCCESS; } // Rebuild @@ -150,7 +150,7 @@ STATUS FfsBuilder::buildIntelImage(const QModelIndex & index, QByteArray & intel // Process other regions for (int i = 1; i < model->rowCount(index); i++) { - QModelIndex currentRegion = index.child(i, 0); + UModelIndex currentRegion = index.child(i, 0); // Skip regions with Remove action if (model->action(currentRegion) == Actions::Remove) @@ -165,15 +165,15 @@ STATUS FfsBuilder::buildIntelImage(const QModelIndex & index, QByteArray & intel } // Check region subtype - STATUS result; - QByteArray region; + USTATUS result; + UByteArray region; UINT8 regionType = model->subtype(currentRegion); switch (regionType) { case Subtypes::BiosRegion: case Subtypes::PdrRegion: result = buildRawArea(currentRegion, region); if (result) { - msg(QObject::tr("buildIntelImage: building of %1 region failed with error \"%2\", original item data is used").arg(regionTypeToQString(regionType)).arg(errorCodeToQString(result)), currentRegion); + //msg(UString("buildIntelImage: building of %1 region failed with error \"%2\", original item data is used").arg(regionTypeToQString(regionType)).arg(errorCodeToQString(result)), currentRegion); region = model->header(currentRegion).append(model->body(currentRegion)); } break; @@ -188,8 +188,8 @@ STATUS FfsBuilder::buildIntelImage(const QModelIndex & index, QByteArray & intel region = model->header(currentRegion).append(model->body(currentRegion)); break; default: - msg(QObject::tr("buildIntelImage: don't know how to build region of unknown type"), index); - return ERR_UNKNOWN_ITEM_TYPE; + msg(UString("buildIntelImage: don't know how to build region of unknown type"), index); + return U_UNKNOWN_ITEM_TYPE; } // Append the resulting region @@ -200,48 +200,48 @@ STATUS FfsBuilder::buildIntelImage(const QModelIndex & index, QByteArray & intel UINT32 newSize = intelImage.size(); UINT32 oldSize = model->body(index).size(); if (newSize > oldSize) { - msg(QObject::tr("buildIntelImage: new image size %1h (%2) is bigger than the original %3h (%4)") - .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index); - return ERR_INVALID_PARAMETER; + //msg(UString("buildIntelImage: new image size %1h (%2) is bigger than the original %3h (%4)") + // .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index); + return U_INVALID_PARAMETER; } else if (newSize < oldSize) { - msg(QObject::tr("buildIntelImage: new image size %1h (%2) is smaller than the original %3h (%4)") - .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index); - return ERR_INVALID_PARAMETER; + //msg(UString("buildIntelImage: new image size %1h (%2) is smaller than the original %3h (%4)") + // .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index); + return U_INVALID_PARAMETER; } // Reconstruction successful - return ERR_SUCCESS; + return U_SUCCESS; } - msg(QObject::tr("buildIntelImage: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index); - return ERR_NOT_IMPLEMENTED; + //msg(UString("buildIntelImage: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index); + return U_NOT_IMPLEMENTED; } -STATUS FfsBuilder::buildRawArea(const QModelIndex & index, QByteArray & rawArea, bool addHeader) +USTATUS FfsBuilder::buildRawArea(const UModelIndex & index, UByteArray & rawArea, bool addHeader) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // No action required if (model->action(index) == Actions::NoAction) { rawArea = model->header(index).append(model->body(index)); - return ERR_SUCCESS; + return U_SUCCESS; } // Rebuild or Replace else if (model->action(index) == Actions::Rebuild || model->action(index) == Actions::Replace) { if (model->rowCount(index)) { - // Clear the supplied QByteArray + // Clear the supplied UByteArray rawArea.clear(); // Build children for (int i = 0; i < model->rowCount(index); i++) { - STATUS result = ERR_SUCCESS; - QModelIndex currentChild = index.child(i, 0); - QByteArray currentData; + USTATUS result = U_SUCCESS; + UModelIndex currentChild = index.child(i, 0); + UByteArray currentData; // Check child type if (model->type(currentChild) == Types::Volume) { result = buildVolume(currentChild, currentData); @@ -250,12 +250,12 @@ STATUS FfsBuilder::buildRawArea(const QModelIndex & index, QByteArray & rawArea, result = buildPadding(currentChild, currentData); } else { - msg(QObject::tr("buildRawArea: unexpected item of type %1 can't be processed, original item data is used").arg(model->type(currentChild)), currentChild); + //msg(UString("buildRawArea: unexpected item of type %1 can't be processed, original item data is used").arg(model->type(currentChild)), currentChild); currentData = model->header(currentChild).append(model->body(currentChild)); } // Check build result if (result) { - msg(QObject::tr("buildRawArea: building of %1 failed with error \"%2\", original item data is used").arg(model->name(currentChild)).arg(errorCodeToQString(result)), currentChild); + //msg(UString("buildRawArea: building of %1 failed with error \"%2\", original item data is used").arg(model->name(currentChild)).arg(errorCodeToQString(result)), currentChild); currentData = model->header(currentChild).append(model->body(currentChild)); } // Append current data @@ -266,14 +266,14 @@ STATUS FfsBuilder::buildRawArea(const QModelIndex & index, QByteArray & rawArea, UINT32 newSize = rawArea.size(); UINT32 oldSize = model->body(index).size(); if (newSize > oldSize) { - msg(QObject::tr("buildRawArea: new area size %1h (%2) is bigger than the original %3h (%4)") - .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index); - return ERR_INVALID_PARAMETER; + //msg(UString("buildRawArea: new area size %1h (%2) is bigger than the original %3h (%4)") + // .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index); + return U_INVALID_PARAMETER; } else if (newSize < oldSize) { - msg(QObject::tr("buildRawArea: new area size %1h (%2) is smaller than the original %3h (%4)") - .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index); - return ERR_INVALID_PARAMETER; + //msg(UString("buildRawArea: new area size %1h (%2) is smaller than the original %3h (%4)") + // .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index); + return U_INVALID_PARAMETER; } } else @@ -282,95 +282,95 @@ STATUS FfsBuilder::buildRawArea(const QModelIndex & index, QByteArray & rawArea, // Build successful, add header if needed if (addHeader) rawArea = model->header(index).append(rawArea); - return ERR_SUCCESS; + return U_SUCCESS; } - msg(QObject::tr("buildRawArea: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index); - return ERR_NOT_IMPLEMENTED; + //msg(UString("buildRawArea: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index); + return U_NOT_IMPLEMENTED; } -STATUS FfsBuilder::buildPadding(const QModelIndex & index, QByteArray & padding) +USTATUS FfsBuilder::buildPadding(const UModelIndex & index, UByteArray & padding) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // No action required if (model->action(index) == Actions::NoAction) { padding = model->header(index).append(model->body(index)); - return ERR_SUCCESS; + return U_SUCCESS; } // Erase else if (model->action(index) == Actions::Erase) { padding = model->header(index).append(model->body(index)); if(erase(index, padding)) - msg(QObject::tr("buildPadding: erase failed, original item data is used"), index); - return ERR_SUCCESS; + msg(UString("buildPadding: erase failed, original item data is used"), index); + return U_SUCCESS; } - msg(QObject::tr("buildPadding: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index); - return ERR_NOT_IMPLEMENTED; + //msg(UString("buildPadding: unexpected action \"%1\"").arg(actionTypeToUString(model->action(index))), index); + return U_NOT_IMPLEMENTED; } -STATUS FfsBuilder::buildNonUefiData(const QModelIndex & index, QByteArray & data) +USTATUS FfsBuilder::buildNonUefiData(const UModelIndex & index, UByteArray & data) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // No action required if (model->action(index) == Actions::NoAction) { data = model->header(index).append(model->body(index)); - return ERR_SUCCESS; + return U_SUCCESS; } // Erase else if (model->action(index) == Actions::Erase) { data = model->header(index).append(model->body(index)); if (erase(index, data)) - msg(QObject::tr("buildNonUefiData: erase failed, original item data is used"), index); - return ERR_SUCCESS; + msg(UString("buildNonUefiData: erase failed, original item data is used"), index); + return U_SUCCESS; } - msg(QObject::tr("buildNonUefiData: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index); - return ERR_NOT_IMPLEMENTED; + //msg(UString("buildNonUefiData: unexpected action \"%1\"").arg(actionTypeToUString(model->action(index))), index); + return U_NOT_IMPLEMENTED; } -STATUS FfsBuilder::buildFreeSpace(const QModelIndex & index, QByteArray & freeSpace) +USTATUS FfsBuilder::buildFreeSpace(const UModelIndex & index, UByteArray & freeSpace) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // No action required if (model->action(index) == Actions::NoAction) { freeSpace = model->header(index).append(model->body(index)); - return ERR_SUCCESS; + return U_SUCCESS; } - msg(QObject::tr("buildFreeSpace: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index); - return ERR_NOT_IMPLEMENTED; + //msg(UString("buildFreeSpace: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index); + return U_NOT_IMPLEMENTED; } -STATUS FfsBuilder::buildVolume(const QModelIndex & index, QByteArray & volume) +USTATUS FfsBuilder::buildVolume(const UModelIndex & index, UByteArray & volume) { - return ERR_NOT_IMPLEMENTED; + return U_NOT_IMPLEMENTED; } -STATUS FfsBuilder::buildPadFile(const QModelIndex & index, QByteArray & padFile) +USTATUS FfsBuilder::buildPadFile(const UModelIndex & index, UByteArray & padFile) { - return ERR_NOT_IMPLEMENTED; + return U_NOT_IMPLEMENTED; } -STATUS FfsBuilder::buildFile(const QModelIndex & index, QByteArray & file) +USTATUS FfsBuilder::buildFile(const UModelIndex & index, UByteArray & file) { - return ERR_NOT_IMPLEMENTED; + return U_NOT_IMPLEMENTED; } -STATUS FfsBuilder::buildSection(const QModelIndex & index, QByteArray & section) +USTATUS FfsBuilder::buildSection(const UModelIndex & index, UByteArray & section) { - return ERR_NOT_IMPLEMENTED; + return U_NOT_IMPLEMENTED; } diff --git a/common/ffsbuilder.h b/common/ffsbuilder.h index b7f8236..14bf3c0 100644 --- a/common/ffsbuilder.h +++ b/common/ffsbuilder.h @@ -16,11 +16,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include -#include -#include -#include -#include - +#include "ubytearray.h" +#include "ustring.h" #include "basetypes.h" #include "treemodel.h" #include "descriptor.h" @@ -33,31 +30,31 @@ public: FfsBuilder(const TreeModel * treeModel) : model(treeModel) {} ~FfsBuilder() {} - std::vector > getMessages() const { return messagesVector; } + std::vector > getMessages() const { return messagesVector; } void clearMessages() { messagesVector.clear(); } - STATUS build(const QModelIndex & root, QByteArray & image); + USTATUS build(const UModelIndex & root, UByteArray & image); private: const TreeModel* model; - std::vector > messagesVector; - void msg(const QString & message, const QModelIndex &index = QModelIndex()) { - messagesVector.push_back(std::pair(message, index)); + std::vector > messagesVector; + void msg(const UString & message, const UModelIndex &index = UModelIndex()) { + messagesVector.push_back(std::pair(message, index)); } - STATUS buildCapsule(const QModelIndex & index, QByteArray & capsule); - STATUS buildIntelImage(const QModelIndex & index, QByteArray & intelImage); - STATUS buildRawArea(const QModelIndex & index, QByteArray & rawArea, bool addHeader = true); - STATUS buildPadding(const QModelIndex & index, QByteArray & padding); - STATUS buildVolume(const QModelIndex & index, QByteArray & volume); - STATUS buildNonUefiData(const QModelIndex & index, QByteArray & data); - STATUS buildFreeSpace(const QModelIndex & index, QByteArray & freeSpace); - STATUS buildPadFile(const QModelIndex & index, QByteArray & padFile); - STATUS buildFile(const QModelIndex & index, QByteArray & file); - STATUS buildSection(const QModelIndex & index, QByteArray & section); + USTATUS buildCapsule(const UModelIndex & index, UByteArray & capsule); + USTATUS buildIntelImage(const UModelIndex & index, UByteArray & intelImage); + USTATUS buildRawArea(const UModelIndex & index, UByteArray & rawArea, bool addHeader = true); + USTATUS buildPadding(const UModelIndex & index, UByteArray & padding); + USTATUS buildVolume(const UModelIndex & index, UByteArray & volume); + USTATUS buildNonUefiData(const UModelIndex & index, UByteArray & data); + USTATUS buildFreeSpace(const UModelIndex & index, UByteArray & freeSpace); + USTATUS buildPadFile(const UModelIndex & index, UByteArray & padFile); + USTATUS buildFile(const UModelIndex & index, UByteArray & file); + USTATUS buildSection(const UModelIndex & index, UByteArray & section); // Utility functions - STATUS erase(const QModelIndex & index, QByteArray & erased); + USTATUS erase(const UModelIndex & index, UByteArray & erased); }; #endif // FFSBUILDER_H diff --git a/common/ffsops.cpp b/common/ffsops.cpp index a767747..27cd0f4 100644 --- a/common/ffsops.cpp +++ b/common/ffsops.cpp @@ -13,24 +13,24 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "ffsops.h" -STATUS FfsOperations::extract(const QModelIndex & index, QString & name, QByteArray & extracted, const UINT8 mode) +USTATUS FfsOperations::extract(const UModelIndex & index, UString & name, UByteArray & extracted, const UINT8 mode) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get data from parsing data - PARSING_DATA pdata = parsingDataFromQModelIndex(index); + PARSING_DATA pdata = parsingDataFromUModelIndex(index); // Construct a name for extracted data - QString itemName = model->name(index); - QString itemText = model->text(index); + UString itemName = model->name(index); + UString itemText = model->text(index); // Default name name = itemName.replace(' ', '_').replace('/', '_').replace('-', '_'); switch (model->type(index)) { - case Types::Volume: if (pdata.volume.hasExtendedHeader) name = guidToQString(pdata.volume.extendedHeaderGuid).replace('-', '_'); break; + case Types::Volume: if (pdata.volume.hasExtendedHeader) name = guidToUString(pdata.volume.extendedHeaderGuid).replace('-', '_'); break; case Types::NvarEntry: case Types::VssEntry: case Types::FsysEntry: @@ -39,11 +39,11 @@ STATUS FfsOperations::extract(const QModelIndex & index, QString & name, QByteAr case Types::File: name = itemText.isEmpty() ? itemName : itemText.replace(' ', '_').replace('-', '_'); break; case Types::Section: { // Get parent file name - QModelIndex fileIndex = model->findParentOfType(index, Types::File); - QString fileText = model->text(fileIndex); + UModelIndex fileIndex = model->findParentOfType(index, Types::File); + UString fileText = model->text(fileIndex); name = fileText.isEmpty() ? model->name(fileIndex) : fileText.replace(' ', '_').replace('-', '_'); // Append section subtype name - name += QChar('_') + itemName.replace(' ', '_'); + name += '_' + itemName.replace(' ', '_'); } break; } @@ -67,9 +67,9 @@ STATUS FfsOperations::extract(const QModelIndex & index, QString & name, QByteAr extracted.clear(); // There is no need to redo decompression, we can use child items for (int i = 0; i < model->rowCount(index); i++) { - QModelIndex childIndex = index.child(i, 0); + UModelIndex childIndex = index.child(i, 0); // Ensure 4-byte alignment of current section - extracted.append(QByteArray('\x00', ALIGN4((UINT32)extracted.size()) - (UINT32)extracted.size())); + extracted.append(UByteArray('\x00', ALIGN4((UINT32)extracted.size()) - (UINT32)extracted.size())); // Add current section header, body and tail extracted.append(model->header(childIndex)); extracted.append(model->body(childIndex)); @@ -77,49 +77,49 @@ STATUS FfsOperations::extract(const QModelIndex & index, QString & name, QByteAr } } else - return ERR_UNKNOWN_EXTRACT_MODE; + return U_UNKNOWN_EXTRACT_MODE; - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsOperations::replace(const QModelIndex & index, const QString & data, const UINT8 mode) +USTATUS FfsOperations::replace(const UModelIndex & index, const UString & data, const UINT8 mode) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get data from parsing data //PARSING_DATA pdata = parsingDataFromQModelIndex(index); if (mode == REPLACE_MODE_AS_IS) { - return ERR_NOT_IMPLEMENTED; + return U_NOT_IMPLEMENTED; } else if (mode == REPLACE_MODE_BODY) { - return ERR_NOT_IMPLEMENTED; + return U_NOT_IMPLEMENTED; } else - return ERR_UNKNOWN_REPLACE_MODE; + return U_UNKNOWN_REPLACE_MODE; - return ERR_NOT_IMPLEMENTED; + return U_NOT_IMPLEMENTED; } -STATUS FfsOperations::remove(const QModelIndex & index) +USTATUS FfsOperations::remove(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Set remove action model->setAction(index, Actions::Remove); - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS FfsOperations::rebuild(const QModelIndex & index) +USTATUS FfsOperations::rebuild(const UModelIndex & index) { // Sanity check if (!index.isValid()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // On insert action, set insert action for children //if (action == Actions::Insert) @@ -130,10 +130,10 @@ STATUS FfsOperations::rebuild(const QModelIndex & index) model->setAction(index, Actions::Rebuild); // Rebuild parent, if it has no action now - QModelIndex parent = index.parent(); + UModelIndex parent = index.parent(); if (parent.isValid() && model->type(parent) != Types::Root && model->action(parent) == Actions::NoAction) rebuild(parent); - return ERR_SUCCESS; + return U_SUCCESS; } diff --git a/common/ffsops.h b/common/ffsops.h index 4876411..b9a7862 100644 --- a/common/ffsops.h +++ b/common/ffsops.h @@ -16,11 +16,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include -#include -#include -#include -#include - +#include "ubytearray.h" +#include "ustring.h" #include "basetypes.h" #include "treemodel.h" #include "ffs.h" @@ -33,21 +30,21 @@ public: FfsOperations(TreeModel * treeModel) : model(treeModel) {} ~FfsOperations() {}; - std::vector > getMessages() const { return messagesVector; } + std::vector > getMessages() const { return messagesVector; } void clearMessages() { messagesVector.clear(); } - STATUS extract(const QModelIndex & index, QString & name, QByteArray & extracted, const UINT8 mode); - STATUS replace(const QModelIndex & index, const QString & data, const UINT8 mode); + USTATUS extract(const UModelIndex & index, UString & name, UByteArray & extracted, const UINT8 mode); + USTATUS replace(const UModelIndex & index, const UString & data, const UINT8 mode); - STATUS remove(const QModelIndex & index); - STATUS rebuild(const QModelIndex & index); + USTATUS remove(const UModelIndex & index); + USTATUS rebuild(const UModelIndex & index); private: TreeModel* model; - std::vector > messagesVector; + std::vector > messagesVector; - void msg(const QString & message, const QModelIndex &index = QModelIndex()) { - messagesVector.push_back(std::pair(message, index)); + void msg(const UString & message, const UModelIndex &index = UModelIndex()) { + messagesVector.push_back(std::pair(message, index)); } }; diff --git a/common/ffsparser.cpp b/common/ffsparser.cpp index faaa127..a584b53 100644 --- a/common/ffsparser.cpp +++ b/common/ffsparser.cpp @@ -1605,7 +1605,7 @@ USTATUS FfsParser::parseFileHeader(const UByteArray & file, const UINT32 parentO } // Construct parsing data - bool fixed(fileHeader->Attributes & FFS_ATTRIB_FIXED); + bool fixed = (fileHeader->Attributes & FFS_ATTRIB_FIXED) != 0; pdata.offset += parentOffset; @@ -4543,7 +4543,7 @@ USTATUS FfsParser::parseFsysStoreBody(const UModelIndex & index) pdata.offset = parentOffset + offset; // Add tree item - model->addItem(Types::FsysEntry, 0, UString(name.constData(), name.size()), UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), index); + model->addItem(Types::FsysEntry, 0, UString(name.constData()), UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), index); // Move to next variable offset += variableSize; diff --git a/common/treemodel.cpp b/common/treemodel.cpp index f7969ab..8ec8450 100644 --- a/common/treemodel.cpp +++ b/common/treemodel.cpp @@ -14,7 +14,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "treeitem.h" #include "treemodel.h" -#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL) +#if defined(QT_CORE_LIB) QVariant TreeModel::data(const UModelIndex &index, int role) const { if (!index.isValid()) diff --git a/common/treemodel.h b/common/treemodel.h index 747be23..f48fa39 100644 --- a/common/treemodel.h +++ b/common/treemodel.h @@ -14,7 +14,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef TREEMODEL_H #define TREEMODEL_H -#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL) +#if defined(QT_CORE_LIB) // Use Qt classes #include #include @@ -73,7 +73,7 @@ private: }; #endif -#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL) +#if defined(QT_CORE_LIB) class TreeModel : public QAbstractItemModel { Q_OBJECT @@ -165,7 +165,7 @@ public: UModelIndex findParentOfType(const UModelIndex & index, UINT8 type) const; }; -#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL) +#if defined(QT_CORE_LIB) // Nothing required here #else inline UModelIndex UModelIndex::parent() const { return m ? m->parent(*this) : UModelIndex(); } diff --git a/common/ubytearray.h b/common/ubytearray.h index 9c1631f..addfeae 100644 --- a/common/ubytearray.h +++ b/common/ubytearray.h @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef UBYTEARRAY_H #define UBYTEARRAY_H -#if defined(QT_CORE_LIB) && defined(U_USE_QBYTEARRAY) +#if defined(QT_CORE_LIB) // Use Qt class, if Qt is available #include #define UByteArray QByteArray @@ -60,14 +60,14 @@ public: bool operator!= (const UByteArray & ba) const { return d != ba.d; } inline void swap(UByteArray &other) { std::swap(d, other.d); } UByteArray toHex() { - std::basic_string hex(size() * 2, '\x00'); - for (int32_t i = 0; i < size(); ++i) { + std::basic_string hex(size() * 2, '\x00'); + for (int32_t i = 0; i < size(); i++) { uint8_t low = d[i] & 0x0F; uint8_t high = (d[i] & 0xF0) >> 4; - low += (low < 10 ? 'a' : '0'); - high += (high < 10 ? 'a' : '0'); - hex[i] = low; - hex[i + 1] = high; + low += (low < 10 ? '0' : 'a'); + high += (high < 10 ? '0' : 'a'); + hex[2*i] = low; + hex[2*i + 1] = high; } std::reverse(hex.begin(), hex.end()); return UByteArray(hex); diff --git a/common/ustring.cpp b/common/ustring.cpp index 8c85c76..8ff2533 100644 --- a/common/ustring.cpp +++ b/common/ustring.cpp @@ -13,9 +13,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "ustring.h" #include -//TODO: modify properly - -#if defined(QT_CORE_LIB) && defined (U_USE_QSTRING) +#if defined(QT_CORE_LIB) UString usprintf(const char* fmt, ...) { UString msg; diff --git a/common/ustring.h b/common/ustring.h index de15135..3c6afa4 100644 --- a/common/ustring.h +++ b/common/ustring.h @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef USTRING_H #define USTRING_H -#if defined (QT_CORE_LIB) && defined(U_USE_QSTRING) +#if defined (QT_CORE_LIB) // Use Qt class, if Qt is available #include #define UString QString diff --git a/common/utility.h b/common/utility.h index 8e02035..beacde3 100644 --- a/common/utility.h +++ b/common/utility.h @@ -15,6 +15,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #define UTILITY_H #include "ustring.h" +#include "treemodel.h" #include "basetypes.h" #include "parsingdata.h" From 7bae8e040c74387e20916c0cb40265f035c54b57 Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Sat, 9 Jul 2016 08:31:08 +0200 Subject: [PATCH 09/12] UEFIDump 0.1.0 for Windows - Linux and OSX will be done in next commit --- UEFIDump/uefidump.cpp | 161 ++++++++++++++++--------------------- UEFIDump/uefidump.h | 8 +- UEFIDump/uefidump_main.cpp | 7 +- bstrlib/bstrwrap.h | 2 +- common/basetypes.h | 7 +- common/utility.cpp | 49 +++++++++++ common/utility.h | 13 +-- 7 files changed, 135 insertions(+), 112 deletions(-) diff --git a/UEFIDump/uefidump.cpp b/UEFIDump/uefidump.cpp index 08816e8..3aca67b 100644 --- a/UEFIDump/uefidump.cpp +++ b/UEFIDump/uefidump.cpp @@ -11,8 +11,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. */ -#include - #include "uefidump.h" #include "../common/ffs.h" #include @@ -20,16 +18,29 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include -USTATUS UEFIDumper::dump(const UByteArray & buffer, const std::wstring & inPath, const std::wstring & guid) -{ - //TODO: rework to support relative paths - std::wstring path = std::wstring(inPath).append(L".dump2"); - path = L"\\\\?\\" + path; - std::wstring reportPath = std::wstring(inPath).append(L".report2.txt"); - reportPath = L"\\\\?\\" + reportPath; - std::wcout << L"Dump path: " << path << std::endl; - std::wcout << L"Report path: " << reportPath << std::endl; +#ifdef WIN32 +#include +#include +bool isExistOnFs(const UString & path) { + struct _stat buf; + return (_stat((const char*)path.toLocal8Bit(), &buf) == 0); +} + +bool makeDirectory(const UString & dir) { + return (_mkdir((const char*)dir.toLocal8Bit()) == 0); +} + +bool changeDirectory(const UString & dir) { + return (_chdir((const char*)dir.toLocal8Bit()) == 0); +} +#else +#endif + +USTATUS UEFIDumper::dump(const UByteArray & buffer, const UString & inPath, const UString & guid) +{ + UString path = UString(inPath) + UString(".dump"); + UString reportPath = UString(inPath) + UString(".report.txt"); if (initialized) { // Check if called with a different buffer as before @@ -99,8 +110,19 @@ USTATUS UEFIDumper::dump(const UByteArray & buffer, const std::wstring & inPath, initialized = true; } + // Check for dump directory existence + if (isExistOnFs(path)) + return U_DIR_ALREADY_EXIST; + + // Create dump directory and cd to it + if (!makeDirectory(path)) + return U_DIR_CREATE; + + if (!changeDirectory(path)) + return U_DIR_CHANGE; + dumped = false; - UINT8 result = recursiveDump(model.index(0,0), path, guid); + UINT8 result = recursiveDump(model.index(0,0)); if (result) return result; else if (!dumped) @@ -109,77 +131,42 @@ USTATUS UEFIDumper::dump(const UByteArray & buffer, const std::wstring & inPath, return U_SUCCESS; } -std::wstring UEFIDumper::guidToWstring(const EFI_GUID & guid) -{ - std::wstringstream ws; - - ws << std::hex << std::uppercase << std::setfill(L'0'); - ws << std::setw(8) << *(const UINT32*)&guid.Data[0] << L"-"; - ws << std::setw(4) << *(const UINT16*)&guid.Data[4] << L"-"; - ws << std::setw(4) << *(const UINT16*)&guid.Data[6] << L"-"; - ws << std::setw(2) << guid.Data[8]; - ws << std::setw(2) << guid.Data[9] << L"-"; - ws << std::setw(2) << guid.Data[10]; - ws << std::setw(2) << guid.Data[11]; - ws << std::setw(2) << guid.Data[12]; - ws << std::setw(2) << guid.Data[13]; - ws << std::setw(2) << guid.Data[14]; - ws << std::setw(2) << guid.Data[15]; - - return ws.str(); -} - -bool UEFIDumper::createFullPath(const std::wstring & path) { - - // Break the path into parent\current, assuming the path is already full and converted into Windows native "\\?\" format - size_t pos = path.find_last_of(L'\\'); - - // Slash is not found, it's a bug - if (pos == path.npos) - return FALSE; - - std::wstring parent = path.substr(0, pos); - std::wstring current = path.substr(pos + 1); - - // Check if first exist, if so, create last and return true - UINT32 attributes = GetFileAttributesW(parent.c_str()); - if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) { - // first is already exist, just create last - return CreateDirectoryW(path.c_str(), NULL) != 0; - } - - // Perform recursive call - if (createFullPath(parent)) - return CreateDirectoryW(path.c_str(), NULL) != 0; - - return FALSE; -} - -USTATUS UEFIDumper::recursiveDump(const UModelIndex & index, const std::wstring & path, const std::wstring & guid) +USTATUS UEFIDumper::recursiveDump(const UModelIndex & index) { if (!index.isValid()) return U_INVALID_PARAMETER; - UByteArray itemHeader = model.header(index); - UByteArray fileHeader = model.header(model.findParentOfType(index, Types::File)); + //UByteArray itemHeader = model.header(index); + //UByteArray fileHeader = model.header(model.findParentOfType(index, Types::File)); - if (guid.length() == 0 || - (itemHeader.size() >= sizeof (EFI_GUID) && guidToWstring(*(const EFI_GUID*)itemHeader.constData()) == guid) || - (fileHeader.size() >= sizeof(EFI_GUID) && guidToWstring(*(const EFI_GUID*)fileHeader.constData()) == guid)) { + //if (guid.length() == 0 || + // (itemHeader.size() >= sizeof (EFI_GUID) && guidToUString(*(const EFI_GUID*)itemHeader.constData()) == guid) || + // (fileHeader.size() >= sizeof(EFI_GUID) && guidToUString(*(const EFI_GUID*)fileHeader.constData()) == guid)) { - if (SetCurrentDirectoryW(path.c_str())) - return U_DIR_ALREADY_EXIST; + // Construct file name + UString orgName = uniqueItemName(index); + UString name = orgName; + bool nameFound = false; + for (int i = 0; i < 0x10000; ++i) { + if (isExistOnFs(name)) { + name = orgName + UString("_") + usprintf("%04X", i); + } + else { + nameFound = true; + break; + } + } + if (!nameFound) + return U_INVALID_PARAMETER; //TODO: replace with proper errorCode - if (!createFullPath(path)) - return U_DIR_CREATE; - - //if (model.rowCount(index) == 0) { + // Add header and body only for leaf sections + if (model.rowCount(index) == 0) { // Header UByteArray data = model.header(index); if (!data.isEmpty()) { std::ofstream file; - std::wstring name = path + std::wstring(L"\\header.bin"); - file.open(name, std::ios::out | std::ios::binary); + std::string str = std::string((const char*)name) + std::string("_header.bin"); + file.open(str, std::ios::out | std::ios::binary); file.write(data.constData(), data.size()); file.close(); } @@ -188,12 +175,12 @@ USTATUS UEFIDumper::recursiveDump(const UModelIndex & index, const std::wstring data = model.body(index); if (!data.isEmpty()) { std::ofstream file; - std::wstring name = path + std::wstring(L"\\body.bin"); - file.open(name, std::ios::out | std::ios::binary); + std::string str = std::string((const char*)name) + std::string("_body.bin"); + file.open(str, std::ios::out | std::ios::binary); file.write(data.constData(), data.size()); file.close(); } - //} + } // Info UString info = "Type: " + itemTypeToUString(model.type(index)) + "\n" + "Subtype: " + itemSubtypeToUString(model.type(index), model.subtype(index)) + "\n"; @@ -202,30 +189,18 @@ USTATUS UEFIDumper::recursiveDump(const UModelIndex & index, const std::wstring info += model.info(index) + "\n"; std::ofstream file; - std::wstring name = path + std::wstring(L"\\info.txt"); - file.open(name, std::ios::out); + std::string str = std::string((const char*)name) + std::string("_info.txt"); + file.open(str, std::ios::out); file.write((const char*)info, info.length()); file.close(); dumped = true; - } - + //} + + // Process child items UINT8 result; for (int i = 0; i < model.rowCount(index); i++) { - UModelIndex childIndex = index.child(i, 0); - bool useText = false; - if (model.type(childIndex) != Types::Volume) - useText = (model.text(childIndex).length() > 0); - - UString name = useText ? (const char *)model.text(childIndex) : (const char *)model.name(childIndex); - std::string sName = std::string((const char*)name, name.length()); - std::wstring childPath = path + std::wstring(L"\\") + std::to_wstring(i) + std::wstring(L" ") + std::wstring(sName.begin(), sName.end()); - - // Workaround for paths with dot at the end, just add remove it - if (childPath.at(childPath.length() - 1) == L'.') - childPath.pop_back(); - - result = recursiveDump(childIndex, childPath, guid); + result = recursiveDump(index.child(i, 0)); if (result) return result; } diff --git a/UEFIDump/uefidump.h b/UEFIDump/uefidump.h index be456f4..b61b909 100644 --- a/UEFIDump/uefidump.h +++ b/UEFIDump/uefidump.h @@ -14,8 +14,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef UEFIDUMP_H #define UEFIDUMP_H -#include - #include "../common/basetypes.h" #include "../common/ustring.h" #include "../common/treemodel.h" @@ -30,12 +28,10 @@ public: explicit UEFIDumper() : model(), ffsParser(&model), ffsReport(&model), fitParser(&model), currentBuffer(), initialized(false), dumped(false) {} ~UEFIDumper() {} - USTATUS dump(const UByteArray & buffer, const std::wstring & path, const std::wstring & guid = std::wstring()); + USTATUS dump(const UByteArray & buffer, const UString & path, const UString & guid = UString()); private: - USTATUS recursiveDump(const UModelIndex & root, const std::wstring & path, const std::wstring & guid); - std::wstring guidToWstring(const EFI_GUID & guid); - bool createFullPath(const std::wstring & path); + USTATUS recursiveDump(const UModelIndex & root); TreeModel model; FfsParser ffsParser; diff --git a/UEFIDump/uefidump_main.cpp b/UEFIDump/uefidump_main.cpp index af160a7..48a7a28 100644 --- a/UEFIDump/uefidump_main.cpp +++ b/UEFIDump/uefidump_main.cpp @@ -15,7 +15,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "uefidump.h" -int wmain(int argc, wchar_t *argv[]) +int main(int argc, char *argv[]) { if (argc > 32) { std::cout << "Too many arguments" << std::endl; @@ -30,12 +30,11 @@ int wmain(int argc, wchar_t *argv[]) inputFile.close(); UEFIDumper uefidumper; - return (uefidumper.dump(buffer, std::wstring(argv[1])) != U_SUCCESS); + return (uefidumper.dump(buffer, UString(argv[1])) != U_SUCCESS); } else { std::cout << "UEFIDump 0.1.0" << std::endl << std::endl - << "Usage: UEFIExtract imagefile " << std::endl - << "Return value is a bit mask where 0 at position N means that file with GUID_N was found and unpacked, 1 otherwise" << std::endl; + << "Usage: UEFIDump imagefile" << std::endl; return 1; } diff --git a/bstrlib/bstrwrap.h b/bstrlib/bstrwrap.h index c3945de..88e0f14 100644 --- a/bstrlib/bstrwrap.h +++ b/bstrlib/bstrwrap.h @@ -362,7 +362,7 @@ struct CBString : public tagbstring { int read (bNread readPtr, void * parm); // QString compatibility methods - CBString toLocal8Bit() { return *this; } + CBString toLocal8Bit() const { return *this; } bool isEmpty() const { return slen == 0; } void clear() { *this = ""; } CBString left(int len) const { return midstr(0, len); } diff --git a/common/basetypes.h b/common/basetypes.h index 9adac9c..b4d4bc1 100644 --- a/common/basetypes.h +++ b/common/basetypes.h @@ -53,9 +53,10 @@ typedef uint8_t USTATUS; #define U_UNKNOWN_RELOCATION_TYPE 32 #define U_DIR_ALREADY_EXIST 33 #define U_DIR_CREATE 34 -#define U_TRUNCATED_IMAGE 35 -#define U_INVALID_CAPSULE 36 -#define U_STORES_NOT_FOUND 37 +#define U_DIR_CHANGE 35 +#define U_TRUNCATED_IMAGE 36 +#define U_INVALID_CAPSULE 37 +#define U_STORES_NOT_FOUND 38 #define U_NOT_IMPLEMENTED 0xFF // UDK porting definitions diff --git a/common/utility.cpp b/common/utility.cpp index 4425f5c..49529be 100644 --- a/common/utility.cpp +++ b/common/utility.cpp @@ -44,6 +44,54 @@ UByteArray parsingDataToUByteArray(const PARSING_DATA & pdata) return UByteArray((const char*)&pdata, sizeof(PARSING_DATA)); } +// Returns unique name string based for tree item +UString uniqueItemName(const UModelIndex & index) +{ + // Sanity check + if (!index.isValid()) + return UString("Invalid index"); + + // Get model from index + const TreeModel* model = index.model(); + + // Get data from parsing data + PARSING_DATA pdata = parsingDataFromUModelIndex(index); + + // Construct the name + UString itemName = model->name(index); + UString itemText = model->text(index); + + // Default name + UString name = itemName; + switch (model->type(index)) { + case Types::Volume: + if (pdata.volume.hasExtendedHeader) name = guidToUString(pdata.volume.extendedHeaderGuid); + break; + case Types::NvarEntry: + case Types::VssEntry: + case Types::FsysEntry: + case Types::EvsaEntry: + case Types::FlashMapEntry: + case Types::File: + name = itemText.isEmpty() ? itemName : itemText; + break; + case Types::Section: { + // Get parent file name + UModelIndex fileIndex = model->findParentOfType(index, Types::File); + UString fileText = model->text(fileIndex); + name = fileText.isEmpty() ? model->name(fileIndex) : fileText; + // Append section subtype name + name += '_' + itemName; + } break; + } + + name.findreplace(' ', '_'); + name.findreplace('/', '_'); + name.findreplace('-', '_'); + + return name; +} + // Returns text representation of error code UString errorCodeToUString(UINT8 errorCode) { @@ -88,6 +136,7 @@ UString errorCodeToUString(UINT8 errorCode) case U_COMPLEX_BLOCK_MAP: return UString("Block map structure too complex for correct analysis"); case U_DIR_ALREADY_EXIST: return UString("Directory already exists"); case U_DIR_CREATE: return UString("Directory can't be created"); + case U_DIR_CHANGE: return UString("Change directory failed"); //case U_UNKNOWN_PATCH_TYPE: return UString("Unknown patch type"); //case U_PATCH_OFFSET_OUT_OF_BOUNDS: return UString("Patch offset out of bounds"); //case U_INVALID_SYMBOL: return UString("Invalid symbol"); diff --git a/common/utility.h b/common/utility.h index beacde3..dcde3e5 100644 --- a/common/utility.h +++ b/common/utility.h @@ -25,22 +25,25 @@ PARSING_DATA parsingDataFromUModelIndex(const UModelIndex & index); // Converts parsing data to byte array UByteArray parsingDataToUByteArray(const PARSING_DATA & pdata); +// Returns unique name string based for tree item +UString uniqueItemName(const UModelIndex & index); + // Converts error code to UString -extern UString errorCodeToUString(UINT8 errorCode); +UString errorCodeToUString(UINT8 errorCode); // Decompression routine -extern USTATUS decompress(const UByteArray & compressed, UINT8 & algorithm, UByteArray & decompressed, UByteArray & efiDecompressed); +USTATUS decompress(const UByteArray & compressed, UINT8 & algorithm, UByteArray & decompressed, UByteArray & efiDecompressed); // Compression routine //USTATUS compress(const UByteArray & decompressed, UByteArray & compressed, const UINT8 & algorithm); // CRC32 calculation routine -extern UINT32 crc32(UINT32 initial, const UINT8* buffer, const UINT32 length); +UINT32 crc32(UINT32 initial, const UINT8* buffer, const UINT32 length); // 8bit checksum calculation routine -extern UINT8 calculateChecksum8(const UINT8* buffer, UINT32 bufferSize); +UINT8 calculateChecksum8(const UINT8* buffer, UINT32 bufferSize); // 16bit checksum calculation routine -extern UINT16 calculateChecksum16(const UINT16* buffer, UINT32 bufferSize); +UINT16 calculateChecksum16(const UINT16* buffer, UINT32 bufferSize); #endif // UTILITY_H From 80b85cbf195ffa726f5c85ebfcf87fdc98cbc67f Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Sat, 9 Jul 2016 08:50:34 +0200 Subject: [PATCH 10/12] Unix compatibility for UD 0.1.0 - a few naming changes and then the release, yay --- UEFIDump/uefidump.cpp | 16 ++++++++++++++-- common/treemodel.h | 2 ++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/UEFIDump/uefidump.cpp b/UEFIDump/uefidump.cpp index 3aca67b..a66631d 100644 --- a/UEFIDump/uefidump.cpp +++ b/UEFIDump/uefidump.cpp @@ -17,11 +17,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include +#include #ifdef WIN32 #include -#include - bool isExistOnFs(const UString & path) { struct _stat buf; return (_stat((const char*)path.toLocal8Bit(), &buf) == 0); @@ -35,6 +34,19 @@ bool changeDirectory(const UString & dir) { return (_chdir((const char*)dir.toLocal8Bit()) == 0); } #else +#include +bool isExistOnFs(const UString & path) { + struct stat buf; + return (stat((const char*)path.toLocal8Bit(), &buf) == 0); +} + +bool makeDirectory(const UString & dir) { + return (mkdir((const char*)dir.toLocal8Bit(), ACCESSPERMS) == 0); +} + +bool changeDirectory(const UString & dir) { + return (chdir((const char*)dir.toLocal8Bit()) == 0); +} #endif USTATUS UEFIDumper::dump(const UByteArray & buffer, const UString & inPath, const UString & guid) diff --git a/common/treemodel.h b/common/treemodel.h index f48fa39..84ee141 100644 --- a/common/treemodel.h +++ b/common/treemodel.h @@ -36,6 +36,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "types.h" #include "treeitem.h" +class TreeModel; + class UModelIndex { friend class TreeModel; From 0114a72fa57d3b4830eca9014cae840e3df27c05 Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Sat, 9 Jul 2016 10:01:41 +0200 Subject: [PATCH 11/12] Build fixes for Windows builds - now to test in OSX and Linux --- UEFIDump/uefidump.cpp | 9 +++---- UEFIFind/uefifind.cpp | 50 ++++++++++++-------------------------- UEFIFind/uefifind.h | 6 ++--- UEFIFind/uefifind_main.cpp | 20 +++++++-------- common/ffsparser.cpp | 4 +-- common/ustring.h | 1 + common/utility.cpp | 17 +++++++------ 7 files changed, 46 insertions(+), 61 deletions(-) diff --git a/UEFIDump/uefidump.cpp b/UEFIDump/uefidump.cpp index a66631d..df26ede 100644 --- a/UEFIDump/uefidump.cpp +++ b/UEFIDump/uefidump.cpp @@ -159,15 +159,14 @@ USTATUS UEFIDumper::recursiveDump(const UModelIndex & index) UString orgName = uniqueItemName(index); UString name = orgName; bool nameFound = false; - for (int i = 0; i < 0x10000; ++i) { - if (isExistOnFs(name)) { - name = orgName + UString("_") + usprintf("%04X", i); - } - else { + for (int i = 1; i < 1000; ++i) { + if (!isExistOnFs(name + UString("_info.txt"))) { nameFound = true; break; } + name = orgName + UString("_") + usprintf("%03d", i); } + if (!nameFound) return U_INVALID_PARAMETER; //TODO: replace with proper errorCode diff --git a/UEFIFind/uefifind.cpp b/UEFIFind/uefifind.cpp index 91eec33..ba6a376 100644 --- a/UEFIFind/uefifind.cpp +++ b/UEFIFind/uefifind.cpp @@ -27,20 +27,20 @@ UEFIFind::~UEFIFind() model = NULL; } -STATUS UEFIFind::init(const QString & path) +USTATUS UEFIFind::init(const QString & path) { - STATUS result; + USTATUS result; fileInfo = QFileInfo(path); if (!fileInfo.exists()) - return ERR_FILE_OPEN; + return U_FILE_OPEN; QFile inputFile; inputFile.setFileName(path); if (!inputFile.open(QFile::ReadOnly)) - return ERR_FILE_OPEN; + return U_FILE_OPEN; QByteArray buffer = inputFile.readAll(); inputFile.close(); @@ -50,42 +50,24 @@ STATUS UEFIFind::init(const QString & path) return result; initDone = true; - return ERR_SUCCESS; + return U_SUCCESS; } -QString UEFIFind::guidToQString(const UINT8* guid) -{ - const UINT32 u32 = *(const UINT32*)guid; - const UINT16 u16_1 = *(const UINT16*)(guid + 4); - const UINT16 u16_2 = *(const UINT16*)(guid + 6); - const UINT8 u8_1 = *(const UINT8*)(guid + 8); - const UINT8 u8_2 = *(const UINT8*)(guid + 9); - const UINT8 u8_3 = *(const UINT8*)(guid + 10); - const UINT8 u8_4 = *(const UINT8*)(guid + 11); - const UINT8 u8_5 = *(const UINT8*)(guid + 12); - const UINT8 u8_6 = *(const UINT8*)(guid + 13); - const UINT8 u8_7 = *(const UINT8*)(guid + 14); - const UINT8 u8_8 = *(const UINT8*)(guid + 15); - - return QString("%1-%2-%3-%4%5-%6%7%8%9%10%11").hexarg2(u32, 8).hexarg2(u16_1, 4).hexarg2(u16_2, 4).hexarg2(u8_1, 2).hexarg2(u8_2, 2) - .hexarg2(u8_3, 2).hexarg2(u8_4, 2).hexarg2(u8_5, 2).hexarg2(u8_6, 2).hexarg2(u8_7, 2).hexarg2(u8_8, 2); -} - -STATUS UEFIFind::find(const UINT8 mode, const bool count, const QString & hexPattern, QString & result) +USTATUS UEFIFind::find(const UINT8 mode, const bool count, const QString & hexPattern, QString & result) { QModelIndex root = model->index(0, 0); std::set > files; result.clear(); - STATUS returned = findFileRecursive(root, hexPattern, mode, files); + USTATUS returned = findFileRecursive(root, hexPattern, mode, files); if (returned) return returned; if (count) { if (!files.empty()) result.append(QString("%1\n").arg(files.size())); - return ERR_SUCCESS; + return U_SUCCESS; } for (std::set >::const_iterator citer = files.cbegin(); citer != files.cend(); ++citer) { @@ -93,30 +75,30 @@ STATUS UEFIFind::find(const UINT8 mode, const bool count, const QString & hexPat std::pair indexes = *citer; if (!model->hasEmptyHeader(indexes.first)) data = model->header(indexes.first).left(16); - result.append(guidToQString((const UINT8*)data.constData())); + result.append(guidToUString(*(const EFI_GUID*)data.constData())); // Special case of freeform subtype GUID files if (indexes.second.isValid() && model->subtype(indexes.second) == EFI_SECTION_FREEFORM_SUBTYPE_GUID) { data = model->header(indexes.second).left(sizeof(EFI_FREEFORM_SUBTYPE_GUID_SECTION)); - result.append(" ").append(guidToQString((const UINT8*)data.constData() + sizeof(EFI_COMMON_SECTION_HEADER))); + result.append(" ").append(guidToUString(*(const EFI_GUID*)(data.constData() + sizeof(EFI_COMMON_SECTION_HEADER)))); } result.append("\n"); } - return ERR_SUCCESS; + return U_SUCCESS; } -STATUS UEFIFind::findFileRecursive(const QModelIndex index, const QString & hexPattern, const UINT8 mode, std::set > & files) +USTATUS UEFIFind::findFileRecursive(const QModelIndex index, const QString & hexPattern, const UINT8 mode, std::set > & files) { if (!index.isValid()) - return ERR_SUCCESS; + return U_SUCCESS; if (hexPattern.isEmpty()) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Check for "all substrings" pattern if (hexPattern.count('.') == hexPattern.length()) - return ERR_SUCCESS; + return U_SUCCESS; bool hasChildren = (model->rowCount(index) > 0); for (int i = 0; i < model->rowCount(index); i++) { @@ -157,5 +139,5 @@ STATUS UEFIFind::findFileRecursive(const QModelIndex index, const QString & hexP offset = regexp.indexIn(hexBody, offset + 1); } - return ERR_SUCCESS; + return U_SUCCESS; } \ No newline at end of file diff --git a/UEFIFind/uefifind.h b/UEFIFind/uefifind.h index 4b76a82..69cd1a5 100644 --- a/UEFIFind/uefifind.h +++ b/UEFIFind/uefifind.h @@ -34,11 +34,11 @@ public: explicit UEFIFind(); ~UEFIFind(); - STATUS init(const QString & path); - STATUS find(const UINT8 mode, const bool count, const QString & hexPattern, QString & result); + USTATUS init(const QString & path); + USTATUS find(const UINT8 mode, const bool count, const QString & hexPattern, QString & result); private: - STATUS findFileRecursive(const QModelIndex index, const QString & hexPattern, const UINT8 mode, std::set > & files); + USTATUS findFileRecursive(const QModelIndex index, const QString & hexPattern, const UINT8 mode, std::set > & files); QString guidToQString(const UINT8* guid); FfsParser* ffsParser; diff --git a/UEFIFind/uefifind_main.cpp b/UEFIFind/uefifind_main.cpp index 079e8d2..a6a991d 100644 --- a/UEFIFind/uefifind_main.cpp +++ b/UEFIFind/uefifind_main.cpp @@ -34,7 +34,7 @@ int main(int argc, char *argv[]) else if (a.arguments().at(1) == QString("all")) mode = SEARCH_MODE_ALL; else - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Get result type bool count; @@ -43,7 +43,7 @@ int main(int argc, char *argv[]) else if (a.arguments().at(2) == QString("count")) count = true; else - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Parse input file result = w.init(a.arguments().at(4)); @@ -58,26 +58,26 @@ int main(int argc, char *argv[]) // Nothing is found if (found.isEmpty()) - return ERR_ITEM_NOT_FOUND; + return U_ITEM_NOT_FOUND; // Print result std::cout << found.toStdString(); - return ERR_SUCCESS; + return U_SUCCESS; } else if (a.arguments().length() == 4) { // Get search mode if (a.arguments().at(1) != QString("file")) - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; // Open patterns file QFileInfo fileInfo(a.arguments().at(2)); if (!fileInfo.exists()) - return ERR_FILE_OPEN; + return U_FILE_OPEN; QFile patternsFile; patternsFile.setFileName(a.arguments().at(2)); if (!patternsFile.open(QFile::ReadOnly)) - return ERR_FILE_OPEN; + return U_FILE_OPEN; // Parse input file result = w.init(a.arguments().at(3)); @@ -143,15 +143,15 @@ int main(int argc, char *argv[]) // Nothing is found if (!somethingFound) - return ERR_ITEM_NOT_FOUND; + return U_ITEM_NOT_FOUND; - return ERR_SUCCESS; + return U_SUCCESS; } else { std::cout << "UEFIFind 0.10.6" << std::endl << std::endl << "Usage: UEFIFind {header | body | all} {list | count} pattern imagefile" << std::endl << " or UEFIFind file patternsfile imagefile" << std::endl; - return ERR_INVALID_PARAMETER; + return U_INVALID_PARAMETER; } } diff --git a/common/ffsparser.cpp b/common/ffsparser.cpp index a584b53..21ae946 100644 --- a/common/ffsparser.cpp +++ b/common/ffsparser.cpp @@ -4086,7 +4086,7 @@ USTATUS FfsParser::parseSlicPubkeyHeader(const UByteArray & store, const UINT32 // Add info UString name("SLIC pubkey"); UString info = usprintf("Type: 0h\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: 0h (0)\n" - "Key type :%02Xh\nVersion: %02Xh\nAlgorithm: %08Xh\nMagic: RSA1\nBit length: %08Xh\nExponent: %08Xh", + "Key type: %02Xh\nVersion: %02Xh\nAlgorithm: %08Xh\nMagic: RSA1\nBit length: %08Xh\nExponent: %08Xh", pubkeyHeader->Size, pubkeyHeader->Size, header.size(), header.size(), pubkeyHeader->KeyType, @@ -4134,7 +4134,7 @@ USTATUS FfsParser::parseSlicMarkerHeader(const UByteArray & store, const UINT32 // Add info UString name("SLIC marker"); UString info = usprintf("Type: 1h\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nBody size: 0h (0)\n" - "Version :%08Xh\nOEM ID: %s\nOEM table ID: %s\nWindows flag: WINDOWS\nSLIC version: %08Xh", + "Version: %08Xh\nOEM ID: %s\nOEM table ID: %s\nWindows flag: WINDOWS\nSLIC version: %08Xh", markerHeader->Size, markerHeader->Size, header.size(), header.size(), markerHeader->Version, diff --git a/common/ustring.h b/common/ustring.h index 3c6afa4..171c7ec 100644 --- a/common/ustring.h +++ b/common/ustring.h @@ -17,6 +17,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. // Use Qt class, if Qt is available #include #define UString QString +#define findreplace replace #else // Use Bstrlib #define BSTRLIB_DOESNT_THROW_EXCEPTIONS diff --git a/common/utility.cpp b/common/utility.cpp index 49529be..a678200 100644 --- a/common/utility.cpp +++ b/common/utility.cpp @@ -52,7 +52,7 @@ UString uniqueItemName(const UModelIndex & index) return UString("Invalid index"); // Get model from index - const TreeModel* model = index.model(); + const TreeModel* model = (const TreeModel*)index.model(); // Get data from parsing data PARSING_DATA pdata = parsingDataFromUModelIndex(index); @@ -64,7 +64,7 @@ UString uniqueItemName(const UModelIndex & index) // Default name UString name = itemName; switch (model->type(index)) { - case Types::Volume: + case Types::Volume: if (pdata.volume.hasExtendedHeader) name = guidToUString(pdata.volume.extendedHeaderGuid); break; case Types::NvarEntry: @@ -72,19 +72,22 @@ UString uniqueItemName(const UModelIndex & index) case Types::FsysEntry: case Types::EvsaEntry: case Types::FlashMapEntry: - case Types::File: - name = itemText.isEmpty() ? itemName : itemText; + case Types::File: + name = itemText.isEmpty() ? itemName : itemName + '_' + itemText; break; case Types::Section: { // Get parent file name UModelIndex fileIndex = model->findParentOfType(index, Types::File); UString fileText = model->text(fileIndex); - name = fileText.isEmpty() ? model->name(fileIndex) : fileText; - // Append section subtype name - name += '_' + itemName; + name = fileText.isEmpty() ? model->name(fileIndex) : model->name(fileIndex) + '_' + fileText; } break; } + UString subtypeString = itemSubtypeToUString(model->type(index), model->subtype(index)); + name = itemTypeToUString(model->type(index)) + + (subtypeString.length() ? ('_' + subtypeString) : UString()) + + '_' + name; + name.findreplace(' ', '_'); name.findreplace('/', '_'); name.findreplace('-', '_'); From 926c65dd14391bf201f290229f5225b93ec158e1 Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Sat, 9 Jul 2016 10:08:32 +0200 Subject: [PATCH 12/12] Add spaces around PRIX64 to please CLang - UEFIDump 0.1.0 is ready for release --- common/ffsparser.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/common/ffsparser.cpp b/common/ffsparser.cpp index 21ae946..aacc1d3 100644 --- a/common/ffsparser.cpp +++ b/common/ffsparser.cpp @@ -1,4 +1,4 @@ -/* ffsparser.cpp +/* ffsparser.cpp Copyright (c) 2016, Nikolaj Schlej. All rights reserved. This program and the accompanying materials @@ -1179,7 +1179,7 @@ USTATUS FfsParser::findNextVolume(const UModelIndex & index, const UByteArray & for (; nextIndex > 0; nextIndex = bios.indexOf(EFI_FV_SIGNATURE, nextIndex + 1)) { const EFI_FIRMWARE_VOLUME_HEADER* volumeHeader = (const EFI_FIRMWARE_VOLUME_HEADER*)(bios.constData() + nextIndex - EFI_FV_SIGNATURE_OFFSET); if (volumeHeader->FvLength < sizeof(EFI_FIRMWARE_VOLUME_HEADER) + 2 * sizeof(EFI_FV_BLOCK_MAP_ENTRY) || volumeHeader->FvLength >= 0xFFFFFFFFUL) { - msg(usprintf("findNextVolume: volume candidate at offset %Xh skipped, has invalid FvLength %"PRIX64"h", + msg(usprintf("findNextVolume: volume candidate at offset %Xh skipped, has invalid FvLength %" PRIX64 "h", parentOffset + (nextIndex - EFI_FV_SIGNATURE_OFFSET), volumeHeader->FvLength), index); continue; @@ -2812,7 +2812,7 @@ USTATUS FfsParser::parsePeImageSectionBody(const UModelIndex & index) optionalHeader.H32->ImageBase); } else if (optionalHeader.H32->Magic == EFI_IMAGE_PE_OPTIONAL_HDR64_MAGIC) { - info += usprintf("\nOptional header signature: %04Xh\nSubsystem: %04Xh\nAddress of entry point: %Xh\nBase of code: %Xh\nImage base: %"PRIX64"h", + info += usprintf("\nOptional header signature: %04Xh\nSubsystem: %04Xh\nAddress of entry point: %Xh\nBase of code: %Xh\nImage base: %" PRIX64 "h", optionalHeader.H64->Magic, optionalHeader.H64->Subsystem, optionalHeader.H64->AddressOfEntryPoint, @@ -2852,7 +2852,7 @@ USTATUS FfsParser::parseTeImageSectionBody(const UModelIndex & index) info += usprintf("\nSignature: %04Xh", teHeader->Signature) + UString("\nMachine type: ") + machineTypeToUString(teHeader->Machine) + usprintf("\nNumber of sections: %u\nSubsystem: %02Xh\nStripped size: %Xh (%u)\n" - "Base of code: %Xh\nAddress of entry point: %Xh\nImage base: %"PRIX64"h\nAdjusted image base: %"PRIX64"h", + "Base of code: %Xh\nAddress of entry point: %Xh\nImage base: %" PRIX64 "h\nAdjusted image base: %" PRIX64 "h", teHeader->NumberOfSections, teHeader->Subsystem, teHeader->StrippedSize, teHeader->StrippedSize, @@ -3276,7 +3276,7 @@ parsing_done: (calculatedChecksum ? usprintf(", invalid, should be %02Xh", 0x100 - calculatedChecksum) : UString(", valid")); // Authentication data if (hasTimestampAndHash) { - info += usprintf("\nTimestamp: %"PRIX64"h\nHash: ", + info += usprintf("\nTimestamp: %" PRIX64 "h\nHash: ", timestamp) + UString(hash.toHex().constData()); } } @@ -4425,7 +4425,7 @@ USTATUS FfsParser::parseVssStoreBody(const UModelIndex & index) subtype = Subtypes::InvalidVssEntry; else if (isAuthenticated) { subtype = Subtypes::AuthVssEntry; - info += usprintf("\nMonotonic counter: %"PRIX64"h\nTimestamp: ", monotonicCounter) + efiTimeToUString(timestamp) + info += usprintf("\nMonotonic counter: %" PRIX64 "h\nTimestamp: ", monotonicCounter) + efiTimeToUString(timestamp) + usprintf("\nPubKey index: %u", pubKeyIndex); } else if (isAppleCrc32) {