diff options
| author | Amlal El Mahrouss <113760121+Amlal-ElMahrouss@users.noreply.github.com> | 2024-04-14 10:48:10 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <113760121+Amlal-ElMahrouss@users.noreply.github.com> | 2024-04-14 10:48:10 +0200 |
| commit | 180b333debbd1e555fbab5191656e2146dff6e8a (patch) | |
| tree | d371c780283540df695a82384757d0b9f0d79bd2 | |
| parent | dd6d72026ac1c96bb85d5f108fa8ddfa2758f02b (diff) | |
see below.
- format code.
- fix xcoff header.
- add .dword, .long and .word for i64asm
Signed-off-by: Amlal El Mahrouss <113760121+Amlal-ElMahrouss@users.noreply.github.com>
| -rw-r--r-- | Examples/ExampleCDialect.c | 17 | ||||
| -rw-r--r-- | Examples/ExamplePowerPC.S | 3 | ||||
| -rw-r--r-- | Headers/AsmKit/AsmKit.hpp | 2 | ||||
| -rw-r--r-- | Headers/AsmKit/CPU/32x0.hpp | 12 | ||||
| -rw-r--r-- | Headers/AsmKit/CPU/64x0.hpp | 52 | ||||
| -rw-r--r-- | Headers/AsmKit/CPU/amd64.hpp | 25 | ||||
| -rw-r--r-- | Headers/AsmKit/CPU/ppc.hpp | 19 | ||||
| -rw-r--r-- | Headers/CompilerKit.hpp | 8 | ||||
| -rw-r--r-- | Headers/Defines.hpp | 10 | ||||
| -rw-r--r-- | Headers/StdKit/ELF.hpp | 520 | ||||
| -rw-r--r-- | Headers/StdKit/XCOFF.hxx | 35 | ||||
| -rw-r--r-- | Headers/UUID.hpp | 1560 | ||||
| -rw-r--r-- | SDK/PowerPC.inc | 40 | ||||
| -rw-r--r-- | Sources/32asm.cc | 7 | ||||
| -rw-r--r-- | Sources/64asm.cc | 20 | ||||
| -rw-r--r-- | Sources/64x0-cc.cc | 85 | ||||
| -rw-r--r-- | Sources/Detail/Readme.md | 1 | ||||
| -rw-r--r-- | Sources/Detail/asmutils.h | 6 | ||||
| -rw-r--r-- | Sources/bpp.cc | 14 | ||||
| -rw-r--r-- | Sources/ccplus.cc | 3 | ||||
| -rw-r--r-- | Sources/i64asm.cc | 37 | ||||
| -rw-r--r-- | Sources/link.cc | 49 | ||||
| -rw-r--r-- | Sources/ppc-cc.cc | 88 | ||||
| -rw-r--r-- | Sources/ppcasm.cc | 21 |
24 files changed, 1251 insertions, 1383 deletions
diff --git a/Examples/ExampleCDialect.c b/Examples/ExampleCDialect.c index c8c72d1..899062c 100644 --- a/Examples/ExampleCDialect.c +++ b/Examples/ExampleCDialect.c @@ -1,12 +1,11 @@ -int main(int argc, char const *argv[]) -{ - int* foo = 0x1000; +int main(int argc, char const* argv[]) { + int* foo = 0x1000; - if (foo == 57) { - foo = 0x2000; - *foo = 5; - return foo; - } + if (foo == 57) { + foo = 0x2000; + *foo = 5; + return foo; + } - return 57; + return 57; } diff --git a/Examples/ExamplePowerPC.S b/Examples/ExamplePowerPC.S index d25f7d2..51c8dbc 100644 --- a/Examples/ExamplePowerPC.S +++ b/Examples/ExamplePowerPC.S @@ -1,6 +1,5 @@ b 0x1000 mflr r21 mtlr r21 - -stwu r1, [r2 + 2] + nik li r3, 0
\ No newline at end of file diff --git a/Headers/AsmKit/AsmKit.hpp b/Headers/AsmKit/AsmKit.hpp index 86a5248..7fbe842 100644 --- a/Headers/AsmKit/AsmKit.hpp +++ b/Headers/AsmKit/AsmKit.hpp @@ -111,7 +111,7 @@ class EncoderInterface { class EncoderAMD64 final : public EncoderInterface { public: explicit EncoderAMD64() = default; - ~EncoderAMD64() override = default; + ~EncoderAMD64() override = default; MPCC_COPY_DEFAULT(EncoderAMD64); diff --git a/Headers/AsmKit/CPU/32x0.hpp b/Headers/AsmKit/CPU/32x0.hpp index 35dbc29..ed9c20f 100644 --- a/Headers/AsmKit/CPU/32x0.hpp +++ b/Headers/AsmKit/CPU/32x0.hpp @@ -40,13 +40,15 @@ struct CpuCode32x0 { #define kAsmByteStr ".byte" /* 8-bit */ inline std::vector<CpuCode32x0> kOpcodes32x0 = { - kAsmOpcodeDecl("nop", 0b0100011, 0b000, kAsmNoArgs) // nothing to do. + kAsmOpcodeDecl("nop", 0b0100011, 0b000, kAsmNoArgs) // nothing to do. kAsmOpcodeDecl("br", 0b1110011, 0b001, kAsmJump) // jump to branch - kAsmOpcodeDecl("mr", 0b0100011, 0b101, kAsmImmediate) // move registers + kAsmOpcodeDecl("mr", 0b0100011, 0b101, kAsmImmediate) // move registers kAsmOpcodeDecl("psh", 0b0111011, 0b000, kAsmImmediate) // push to sp - kAsmOpcodeDecl("pop", 0b0111011, 0b001, kAsmImmediate) // pop from sp. - kAsmOpcodeDecl("cls", 0b0111011, 0b010, kAsmImmediate) // setup stack and call, store address to CR. - kAsmOpcodeDecl("rts", 0b0111011, 0b110, kAsmImmediate) // pull stack and return form CR. + kAsmOpcodeDecl("pop", 0b0111011, 0b001, kAsmImmediate) // pop from sp. + kAsmOpcodeDecl("cls", 0b0111011, 0b010, + kAsmImmediate) // setup stack and call, store address to CR. + kAsmOpcodeDecl("rts", 0b0111011, 0b110, + kAsmImmediate) // pull stack and return form CR. kAsmOpcodeDecl("int", 0b0111111, 0b000, kAsmSyscall) // raise interrupt }; diff --git a/Headers/AsmKit/CPU/64x0.hpp b/Headers/AsmKit/CPU/64x0.hpp index c4c358e..b5db71f 100644 --- a/Headers/AsmKit/CPU/64x0.hpp +++ b/Headers/AsmKit/CPU/64x0.hpp @@ -35,31 +35,33 @@ struct CpuOpcode64x0 { }; inline std::vector<CpuOpcode64x0> kOpcodes64x0 = { - kAsmOpcodeDecl("nop", 0b0000000, 0b0000000, kAsmNoArgs) // no-operation. - kAsmOpcodeDecl("np", 0b0000000, 0b0000000, kAsmNoArgs) // no-operation. - kAsmOpcodeDecl("jlr", 0b1110011, 0b0000111, kAsmJump) // jump to linked return register - kAsmOpcodeDecl("jrl", 0b1110011, 0b0001111, kAsmJump) // jump from return register. - kAsmOpcodeDecl("mv", 0b0100011, 0b101, kAsmRegToReg) - kAsmOpcodeDecl("bg", 0b1100111, 0b111, kAsmRegToReg) - kAsmOpcodeDecl("bl", 0b1100111, 0b011, kAsmRegToReg) - kAsmOpcodeDecl("beq", 0b1100111, 0b000, kAsmRegToReg) - kAsmOpcodeDecl("bne", 0b1100111, 0b001,kAsmRegToReg) - kAsmOpcodeDecl("bge", 0b1100111, 0b101, kAsmRegToReg) - kAsmOpcodeDecl("ble", 0b1100111, 0b100, kAsmRegToReg) - kAsmOpcodeDecl("stw", 0b0001111, 0b100, kAsmImmediate) - kAsmOpcodeDecl("ldw", 0b0001111, 0b100, kAsmImmediate) - kAsmOpcodeDecl("lda", 0b0001111, 0b101, kAsmImmediate) - kAsmOpcodeDecl("sta", 0b0001111, 0b001, kAsmImmediate) - // add/sub without carry flag - kAsmOpcodeDecl("add", 0b0101011, 0b100, kAsmImmediate) - kAsmOpcodeDecl("dec", 0b0101011, 0b101, kAsmImmediate) - // add/sub with carry flag - kAsmOpcodeDecl("addc", 0b0101011, 0b110, kAsmImmediate) - kAsmOpcodeDecl("decc", 0b0101011, 0b111, kAsmImmediate) - kAsmOpcodeDecl("int", 0b1110011, 0b00, kAsmSyscall) - kAsmOpcodeDecl("pha", 0b1110011, 0b00, kAsmNoArgs) - kAsmOpcodeDecl("pla", 0b1110011, 0b01, kAsmNoArgs) -}; + kAsmOpcodeDecl("nop", 0b0000000, 0b0000000, kAsmNoArgs) // no-operation. + kAsmOpcodeDecl("np", 0b0000000, 0b0000000, kAsmNoArgs) // no-operation. + kAsmOpcodeDecl("jlr", 0b1110011, 0b0000111, + kAsmJump) // jump to linked return register + kAsmOpcodeDecl("jrl", 0b1110011, 0b0001111, + kAsmJump) // jump from return register. + kAsmOpcodeDecl("mv", 0b0100011, 0b101, kAsmRegToReg) kAsmOpcodeDecl( + "bg", 0b1100111, 0b111, + kAsmRegToReg) kAsmOpcodeDecl("bl", 0b1100111, 0b011, kAsmRegToReg) + kAsmOpcodeDecl("beq", 0b1100111, 0b000, kAsmRegToReg) kAsmOpcodeDecl( + "bne", 0b1100111, 0b001, + kAsmRegToReg) kAsmOpcodeDecl("bge", 0b1100111, 0b101, kAsmRegToReg) + kAsmOpcodeDecl("ble", 0b1100111, 0b100, kAsmRegToReg) + kAsmOpcodeDecl("stw", 0b0001111, 0b100, kAsmImmediate) + kAsmOpcodeDecl("ldw", 0b0001111, 0b100, kAsmImmediate) + kAsmOpcodeDecl("lda", 0b0001111, 0b101, kAsmImmediate) + kAsmOpcodeDecl("sta", 0b0001111, 0b001, + kAsmImmediate) + // add/sub without carry flag + kAsmOpcodeDecl("add", 0b0101011, 0b100, kAsmImmediate) + kAsmOpcodeDecl("dec", 0b0101011, 0b101, kAsmImmediate) + // add/sub with carry flag + kAsmOpcodeDecl("addc", 0b0101011, 0b110, kAsmImmediate) + kAsmOpcodeDecl("decc", 0b0101011, 0b111, kAsmImmediate) + kAsmOpcodeDecl("int", 0b1110011, 0b00, kAsmSyscall) + kAsmOpcodeDecl("pha", 0b1110011, 0b00, kAsmNoArgs) + kAsmOpcodeDecl("pla", 0b1110011, 0b01, kAsmNoArgs)}; // \brief 64x0 register prefix // example: r32, r0 diff --git a/Headers/AsmKit/CPU/amd64.hpp b/Headers/AsmKit/CPU/amd64.hpp index bddcda2..ca3c7dd 100644 --- a/Headers/AsmKit/CPU/amd64.hpp +++ b/Headers/AsmKit/CPU/amd64.hpp @@ -37,22 +37,13 @@ struct CpuOpcodeAMD64 { #define kJumpLimitStandardLimit 0xEB inline std::vector<CpuOpcodeAMD64> kOpcodesAMD64 = { - kAsmOpcodeDecl("int", 0xCD) - kAsmOpcodeDecl("into", 0xCE) - kAsmOpcodeDecl("intd", 0xF1) - kAsmOpcodeDecl("int3", 0xC3) - kAsmOpcodeDecl("iret", 0xCF) - kAsmOpcodeDecl("retf", 0xCB) - kAsmOpcodeDecl("retn", 0xC3) - kAsmOpcodeDecl("ret", 0xC3) - kAsmOpcodeDecl("sti", 0xfb) - kAsmOpcodeDecl("cli", 0xfa) - kAsmOpcodeDecl("hlt", 0xf4) - kAsmOpcodeDecl("nop", 0x90) - kAsmOpcodeDecl("mov", 0x48) - kAsmOpcodeDecl("call", 0xFF) - kAsmOpcodeDecl("mv", 0x48) - kAsmOpcodeDecl("br", 0xE9) -}; + kAsmOpcodeDecl("int", 0xCD) kAsmOpcodeDecl("into", 0xCE) kAsmOpcodeDecl( + "intd", 0xF1) kAsmOpcodeDecl("int3", 0xC3) kAsmOpcodeDecl("iret", 0xCF) + kAsmOpcodeDecl("retf", 0xCB) kAsmOpcodeDecl("retn", 0xC3) + kAsmOpcodeDecl("ret", 0xC3) kAsmOpcodeDecl("sti", 0xfb) + kAsmOpcodeDecl("cli", 0xfa) kAsmOpcodeDecl("hlt", 0xf4) + kAsmOpcodeDecl("nop", 0x90) kAsmOpcodeDecl("mov", 0x48) + kAsmOpcodeDecl("call", 0xFF) kAsmOpcodeDecl("mv", 0x48) + kAsmOpcodeDecl("br", 0xE9)}; #define kAsmRegisterLimit 15 diff --git a/Headers/AsmKit/CPU/ppc.hpp b/Headers/AsmKit/CPU/ppc.hpp index 1c8bba1..873552d 100644 --- a/Headers/AsmKit/CPU/ppc.hpp +++ b/Headers/AsmKit/CPU/ppc.hpp @@ -2,18 +2,19 @@ #include <cstdint> -/// @note Based of: https://opensource.apple.com/source/cctools/cctools-750/as/ppc-opcode.h.auto.html +/// @note Based of: +/// https://opensource.apple.com/source/cctools/cctools-750/as/ppc-opcode.h.auto.html /* * These defines are use in the cpus field of the instructions. If the field * is zero it can execute on all cpus. The defines are or'ed together. This * information is used to set the cpusubtype in the resulting object file. */ -#define CPU601 0x1 -#define IMPL64 0x2 -#define OPTIONAL 0x4 -#define VMX 0x8 -#define CPU970 0x10 /* added to OPTIONAL insts that the 970 has */ +#define CPU601 0x1 +#define IMPL64 0x2 +#define OPTIONAL 0x4 +#define VMX 0x8 +#define CPU970 0x10 /* added to OPTIONAL insts that the 970 has */ enum optype { NONE, /* no operand */ @@ -47,13 +48,13 @@ enum optype { struct op { uint32_t offset : 5; uint32_t width : 5; - enum optype type : 6; + enum optype type : 6; }; struct CpuOpcodePPC { uint32_t opcode; - const char *name; // c++ wants the string to be const, it makes sense here. - struct op ops[5]; + const char *name; // c++ wants the string to be const, it makes sense here. + struct op ops[5]; uint32_t cpus; }; diff --git a/Headers/CompilerKit.hpp b/Headers/CompilerKit.hpp index ea8ea4f..dba0a5e 100644 --- a/Headers/CompilerKit.hpp +++ b/Headers/CompilerKit.hpp @@ -9,19 +9,19 @@ #ifndef _CK_CL_HPP #define _CK_CL_HPP -#define MPCC_COPY_DELETE(KLASS) \ +#define MPCC_COPY_DELETE(KLASS) \ KLASS &operator=(const KLASS &) = delete; \ KLASS(const KLASS &) = delete; -#define MPCC_COPY_DEFAULT(KLASS) \ +#define MPCC_COPY_DEFAULT(KLASS) \ KLASS &operator=(const KLASS &) = default; \ KLASS(const KLASS &) = default; -#define MPCC_MOVE_DELETE(KLASS) \ +#define MPCC_MOVE_DELETE(KLASS) \ KLASS &operator=(KLASS &&) = delete; \ KLASS(KLASS &&) = delete; -#define MPCC_MOVE_DEFAULT(KLASS) \ +#define MPCC_MOVE_DEFAULT(KLASS) \ KLASS &operator=(KLASS &&) = default; \ KLASS(KLASS &&) = default; diff --git a/Headers/Defines.hpp b/Headers/Defines.hpp index 85bfd3f..51b0dec 100644 --- a/Headers/Defines.hpp +++ b/Headers/Defines.hpp @@ -61,19 +61,19 @@ #define rt_copy_memory(dst, src, len) memcpy(dst, src, len) #endif -#define MPCC_COPY_DELETE(KLASS) \ +#define MPCC_COPY_DELETE(KLASS) \ KLASS &operator=(const KLASS &) = delete; \ KLASS(const KLASS &) = delete; -#define MPCC_COPY_DEFAULT(KLASS) \ +#define MPCC_COPY_DEFAULT(KLASS) \ KLASS &operator=(const KLASS &) = default; \ KLASS(const KLASS &) = default; -#define MPCC_MOVE_DELETE(KLASS) \ +#define MPCC_MOVE_DELETE(KLASS) \ KLASS &operator=(KLASS &&) = delete; \ KLASS(KLASS &&) = delete; -#define MPCC_MOVE_DEFAULT(KLASS) \ +#define MPCC_MOVE_DEFAULT(KLASS) \ KLASS &operator=(KLASS &&) = default; \ KLASS(KLASS &&) = default; @@ -131,6 +131,6 @@ typedef char char_type; #define MPCC_MODULE(name) int main(int argc, char **argv) #endif /* ifdef __MODULE_NEED__ */ -#pragma scalar_storage_order big-endian +#pragma scalar_storage_order big - endian #endif /* ifndef __MPCC_DEFINES_HPP__ */ diff --git a/Headers/StdKit/ELF.hpp b/Headers/StdKit/ELF.hpp index 32b9ada..7fe2c30 100644 --- a/Headers/StdKit/ELF.hpp +++ b/Headers/StdKit/ELF.hpp @@ -6,130 +6,130 @@ struct file; #ifndef elf_read_implies_exec - /* Executables for which elf_read_implies_exec() returns TRUE will - have the READ_IMPLIES_EXEC personality flag set automatically. - Override in asm/elf.h as needed. */ -# define elf_read_implies_exec(ex, have_pt_gnu_stack) 0 +/* Executables for which elf_read_implies_exec() returns TRUE will + have the READ_IMPLIES_EXEC personality flag set automatically. + Override in asm/elf.h as needed. */ +#define elf_read_implies_exec(ex, have_pt_gnu_stack) 0 #endif /* 32-bit ELF base types. */ -typedef uint32_t Elf32_Addr; -typedef uint16_t Elf32_Half; -typedef uint32_t Elf32_Off; -typedef int32_t Elf32_Sword; -typedef uint32_t Elf32_Word; +typedef uint32_t Elf32_Addr; +typedef uint16_t Elf32_Half; +typedef uint32_t Elf32_Off; +typedef int32_t Elf32_Sword; +typedef uint32_t Elf32_Word; /* 64-bit ELF base types. */ -typedef uintptr_t Elf64_Addr; -typedef uint16_t Elf64_Half; -typedef int16_t Elf64_SHalf; -typedef uint64_t Elf64_Off; -typedef int32_t Elf64_Sword; -typedef uint32_t Elf64_Word; -typedef uint64_t Elf64_Xword; -typedef int64_t Elf64_Sxword; +typedef uintptr_t Elf64_Addr; +typedef uint16_t Elf64_Half; +typedef int16_t Elf64_SHalf; +typedef uint64_t Elf64_Off; +typedef int32_t Elf64_Sword; +typedef uint32_t Elf64_Word; +typedef uint64_t Elf64_Xword; +typedef int64_t Elf64_Sxword; /* These constants are for the segment types stored in the image headers */ -#define PT_NULL 0 -#define PT_LOAD 1 +#define PT_NULL 0 +#define PT_LOAD 1 #define PT_DYNAMIC 2 -#define PT_INTERP 3 -#define PT_NOTE 4 -#define PT_SHLIB 5 -#define PT_PHDR 6 -#define PT_TLS 7 /* Thread local storage segment */ -#define PT_LOOS 0x60000000 /* OS-specific */ -#define PT_HIOS 0x6fffffff /* OS-specific */ -#define PT_LOPROC 0x70000000 -#define PT_HIPROC 0x7fffffff -#define PT_GNU_EH_FRAME 0x6474e550 - -#define PT_GNU_STACK (PT_LOOS + 0x474e551) +#define PT_INTERP 3 +#define PT_NOTE 4 +#define PT_SHLIB 5 +#define PT_PHDR 6 +#define PT_TLS 7 /* Thread local storage segment */ +#define PT_LOOS 0x60000000 /* OS-specific */ +#define PT_HIOS 0x6fffffff /* OS-specific */ +#define PT_LOPROC 0x70000000 +#define PT_HIPROC 0x7fffffff +#define PT_GNU_EH_FRAME 0x6474e550 + +#define PT_GNU_STACK (PT_LOOS + 0x474e551) /* These constants define the different elf file types */ -#define ET_NONE 0 -#define ET_REL 1 -#define ET_EXEC 2 -#define ET_DYN 3 -#define ET_CORE 4 +#define ET_NONE 0 +#define ET_REL 1 +#define ET_EXEC 2 +#define ET_DYN 3 +#define ET_CORE 4 #define ET_LOPROC 0xff00 #define ET_HIPROC 0xffff /* This is the info that is needed to parse the dynamic section of the file */ -#define DT_NULL 0 -#define DT_NEEDED 1 -#define DT_PLTRELSZ 2 -#define DT_PLTGOT 3 -#define DT_HASH 4 -#define DT_STRTAB 5 -#define DT_SYMTAB 6 -#define DT_RELA 7 -#define DT_RELASZ 8 -#define DT_RELAENT 9 -#define DT_STRSZ 10 -#define DT_SYMENT 11 -#define DT_INIT 12 -#define DT_FINI 13 -#define DT_SONAME 14 -#define DT_RPATH 15 -#define DT_SYMBOLIC 16 -#define DT_REL 17 -#define DT_RELSZ 18 -#define DT_RELENT 19 -#define DT_PLTREL 20 -#define DT_DEBUG 21 -#define DT_TEXTREL 22 -#define DT_JMPREL 23 -#define DT_ENCODING 32 -#define OLD_DT_LOOS 0x60000000 -#define DT_LOOS 0x6000000d -#define DT_HIOS 0x6ffff000 -#define DT_VALRNGLO 0x6ffffd00 -#define DT_VALRNGHI 0x6ffffdff -#define DT_ADDRRNGLO 0x6ffffe00 -#define DT_ADDRRNGHI 0x6ffffeff -#define DT_VERSYM 0x6ffffff0 -#define DT_RELACOUNT 0x6ffffff9 -#define DT_RELCOUNT 0x6ffffffa -#define DT_FLAGS_1 0x6ffffffb -#define DT_VERDEF 0x6ffffffc -#define DT_VERDEFNUM 0x6ffffffd -#define DT_VERNEED 0x6ffffffe -#define DT_VERNEEDNUM 0x6fffffff -#define OLD_DT_HIOS 0x6fffffff -#define DT_LOPROC 0x70000000 -#define DT_HIPROC 0x7fffffff +#define DT_NULL 0 +#define DT_NEEDED 1 +#define DT_PLTRELSZ 2 +#define DT_PLTGOT 3 +#define DT_HASH 4 +#define DT_STRTAB 5 +#define DT_SYMTAB 6 +#define DT_RELA 7 +#define DT_RELASZ 8 +#define DT_RELAENT 9 +#define DT_STRSZ 10 +#define DT_SYMENT 11 +#define DT_INIT 12 +#define DT_FINI 13 +#define DT_SONAME 14 +#define DT_RPATH 15 +#define DT_SYMBOLIC 16 +#define DT_REL 17 +#define DT_RELSZ 18 +#define DT_RELENT 19 +#define DT_PLTREL 20 +#define DT_DEBUG 21 +#define DT_TEXTREL 22 +#define DT_JMPREL 23 +#define DT_ENCODING 32 +#define OLD_DT_LOOS 0x60000000 +#define DT_LOOS 0x6000000d +#define DT_HIOS 0x6ffff000 +#define DT_VALRNGLO 0x6ffffd00 +#define DT_VALRNGHI 0x6ffffdff +#define DT_ADDRRNGLO 0x6ffffe00 +#define DT_ADDRRNGHI 0x6ffffeff +#define DT_VERSYM 0x6ffffff0 +#define DT_RELACOUNT 0x6ffffff9 +#define DT_RELCOUNT 0x6ffffffa +#define DT_FLAGS_1 0x6ffffffb +#define DT_VERDEF 0x6ffffffc +#define DT_VERDEFNUM 0x6ffffffd +#define DT_VERNEED 0x6ffffffe +#define DT_VERNEEDNUM 0x6fffffff +#define OLD_DT_HIOS 0x6fffffff +#define DT_LOPROC 0x70000000 +#define DT_HIPROC 0x7fffffff /* This info is needed when parsing the symbol table */ -#define STB_LOCAL 0 +#define STB_LOCAL 0 #define STB_GLOBAL 1 -#define STB_WEAK 2 +#define STB_WEAK 2 -#define STT_NOTYPE 0 -#define STT_OBJECT 1 -#define STT_FUNC 2 +#define STT_NOTYPE 0 +#define STT_OBJECT 1 +#define STT_FUNC 2 #define STT_SECTION 3 -#define STT_FILE 4 -#define STT_COMMON 5 -#define STT_TLS 6 - -#define ELF_ST_BIND(x) ((x) >> 4) -#define ELF_ST_TYPE(x) (((unsigned int) x) & 0xf) -#define ELF32_ST_BIND(x) ELF_ST_BIND(x) -#define ELF32_ST_TYPE(x) ELF_ST_TYPE(x) -#define ELF64_ST_BIND(x) ELF_ST_BIND(x) -#define ELF64_ST_TYPE(x) ELF_ST_TYPE(x) - -typedef struct dynamic{ +#define STT_FILE 4 +#define STT_COMMON 5 +#define STT_TLS 6 + +#define ELF_ST_BIND(x) ((x) >> 4) +#define ELF_ST_TYPE(x) (((unsigned int)x) & 0xf) +#define ELF32_ST_BIND(x) ELF_ST_BIND(x) +#define ELF32_ST_TYPE(x) ELF_ST_TYPE(x) +#define ELF64_ST_BIND(x) ELF_ST_BIND(x) +#define ELF64_ST_TYPE(x) ELF_ST_TYPE(x) + +typedef struct dynamic { Elf32_Sword d_tag; - union{ - Elf32_Sword d_val; - Elf32_Addr d_ptr; + union { + Elf32_Sword d_val; + Elf32_Addr d_ptr; } d_un; } Elf32_Dyn; typedef struct { - Elf64_Sxword d_tag; /* entry tag value */ + Elf64_Sxword d_tag; /* entry tag value */ union { Elf64_Xword d_val; Elf64_Addr d_ptr; @@ -140,77 +140,76 @@ typedef struct { #define ELF32_R_SYM(x) ((x) >> 8) #define ELF32_R_TYPE(x) ((x) & 0xff) -#define ELF64_R_SYM(i) ((i) >> 32) -#define ELF64_R_TYPE(i) ((i) & 0xffffffff) +#define ELF64_R_SYM(i) ((i) >> 32) +#define ELF64_R_TYPE(i) ((i) & 0xffffffff) typedef struct elf32_rel { - Elf32_Addr r_offset; - Elf32_Word r_info; + Elf32_Addr r_offset; + Elf32_Word r_info; } Elf32_Rel; typedef struct elf64_rel { - Elf64_Addr r_offset; /* Location at which to apply the action */ - Elf64_Xword r_info; /* index and type of relocation */ + Elf64_Addr r_offset; /* Location at which to apply the action */ + Elf64_Xword r_info; /* index and type of relocation */ } Elf64_Rel; typedef struct elf32_rela { - Elf32_Addr r_offset; - Elf32_Word r_info; - Elf32_Sword r_addend; + Elf32_Addr r_offset; + Elf32_Word r_info; + Elf32_Sword r_addend; } Elf32_Rela; typedef struct elf64_rela { - Elf64_Addr r_offset; /* Location at which to apply the action */ - Elf64_Xword r_info; /* index and type of relocation */ - Elf64_Sxword r_addend; /* Constant addend used to compute value */ + Elf64_Addr r_offset; /* Location at which to apply the action */ + Elf64_Xword r_info; /* index and type of relocation */ + Elf64_Sxword r_addend; /* Constant addend used to compute value */ } Elf64_Rela; typedef struct elf32_sym { - Elf32_Word st_name; - Elf32_Addr st_value; - Elf32_Word st_size; - unsigned char st_info; - unsigned char st_other; - Elf32_Half st_shndx; + Elf32_Word st_name; + Elf32_Addr st_value; + Elf32_Word st_size; + unsigned char st_info; + unsigned char st_other; + Elf32_Half st_shndx; } Elf32_Sym; typedef struct elf64_sym { - Elf64_Word st_name; /* Symbol name, index in string tbl */ - unsigned char st_info; /* Type and binding attributes */ - unsigned char st_other; /* No defined meaning, 0 */ - Elf64_Half st_shndx; /* Associated section index */ - Elf64_Addr st_value; /* Value of the symbol */ - Elf64_Xword st_size; /* Associated symbol size */ + Elf64_Word st_name; /* Symbol name, index in string tbl */ + unsigned char st_info; /* Type and binding attributes */ + unsigned char st_other; /* No defined meaning, 0 */ + Elf64_Half st_shndx; /* Associated section index */ + Elf64_Addr st_value; /* Value of the symbol */ + Elf64_Xword st_size; /* Associated symbol size */ } Elf64_Sym; - -#define EI_NIDENT 16 +#define EI_NIDENT 16 typedef struct elf32_hdr { - unsigned char e_ident[EI_NIDENT]; - Elf32_Half e_type; - Elf32_Half e_machine; - Elf32_Word e_version; - Elf32_Addr e_entry; /* Entry point */ - Elf32_Off e_phoff; - Elf32_Off e_shoff; - Elf32_Word e_flags; - Elf32_Half e_ehsize; - Elf32_Half e_phentsize; - Elf32_Half e_phnum; - Elf32_Half e_shentsize; - Elf32_Half e_shnum; - Elf32_Half e_shstrndx; + unsigned char e_ident[EI_NIDENT]; + Elf32_Half e_type; + Elf32_Half e_machine; + Elf32_Word e_version; + Elf32_Addr e_entry; /* Entry point */ + Elf32_Off e_phoff; + Elf32_Off e_shoff; + Elf32_Word e_flags; + Elf32_Half e_ehsize; + Elf32_Half e_phentsize; + Elf32_Half e_phnum; + Elf32_Half e_shentsize; + Elf32_Half e_shnum; + Elf32_Half e_shstrndx; } Elf32_Ehdr; typedef struct elf64_hdr { - unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */ + unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */ Elf64_Half e_type; Elf64_Half e_machine; Elf64_Word e_version; - Elf64_Addr e_entry; /* Entry point virtual address */ - Elf64_Off e_phoff; /* Program header table file offset */ - Elf64_Off e_shoff; /* Section header table file offset */ + Elf64_Addr e_entry; /* Entry point virtual address */ + Elf64_Off e_phoff; /* Program header table file offset */ + Elf64_Off e_shoff; /* Section header table file offset */ Elf64_Word e_flags; Elf64_Half e_ehsize; Elf64_Half e_phentsize; @@ -222,154 +221,153 @@ typedef struct elf64_hdr { /* These constants define the permissions on sections in the program header, p_flags. */ -#define PF_R 0x4 -#define PF_W 0x2 -#define PF_X 0x1 +#define PF_R 0x4 +#define PF_W 0x2 +#define PF_X 0x1 typedef struct elf32_phdr { - Elf32_Word p_type; - Elf32_Off p_offset; - Elf32_Addr p_vaddr; - Elf32_Addr p_paddr; - Elf32_Word p_filesz; - Elf32_Word p_memsz; - Elf32_Word p_flags; - Elf32_Word p_align; + Elf32_Word p_type; + Elf32_Off p_offset; + Elf32_Addr p_vaddr; + Elf32_Addr p_paddr; + Elf32_Word p_filesz; + Elf32_Word p_memsz; + Elf32_Word p_flags; + Elf32_Word p_align; } Elf32_Phdr; typedef struct elf64_phdr { Elf64_Word p_type; Elf64_Word p_flags; - Elf64_Off p_offset; /* Segment file offset */ - Elf64_Addr p_vaddr; /* Segment virtual address */ - Elf64_Addr p_paddr; /* Segment physical address */ - Elf64_Xword p_filesz; /* Segment size in file */ - Elf64_Xword p_memsz; /* Segment size in memory */ - Elf64_Xword p_align; /* Segment alignment, file & memory */ + Elf64_Off p_offset; /* Segment file offset */ + Elf64_Addr p_vaddr; /* Segment virtual address */ + Elf64_Addr p_paddr; /* Segment physical address */ + Elf64_Xword p_filesz; /* Segment size in file */ + Elf64_Xword p_memsz; /* Segment size in memory */ + Elf64_Xword p_align; /* Segment alignment, file & memory */ } Elf64_Phdr; /* sh_type */ -#define SHT_NULL 0 -#define SHT_PROGBITS 1 -#define SHT_SYMTAB 2 -#define SHT_STRTAB 3 -#define SHT_RELA 4 -#define SHT_HASH 5 -#define SHT_DYNAMIC 6 -#define SHT_NOTE 7 -#define SHT_NOBITS 8 -#define SHT_REL 9 -#define SHT_SHLIB 10 -#define SHT_DYNSYM 11 -#define SHT_NUM 12 -#define SHT_LOPROC 0x70000000 -#define SHT_HIPROC 0x7fffffff -#define SHT_LOUSER 0x80000000 -#define SHT_HIUSER 0xffffffff +#define SHT_NULL 0 +#define SHT_PROGBITS 1 +#define SHT_SYMTAB 2 +#define SHT_STRTAB 3 +#define SHT_RELA 4 +#define SHT_HASH 5 +#define SHT_DYNAMIC 6 +#define SHT_NOTE 7 +#define SHT_NOBITS 8 +#define SHT_REL 9 +#define SHT_SHLIB 10 +#define SHT_DYNSYM 11 +#define SHT_NUM 12 +#define SHT_LOPROC 0x70000000 +#define SHT_HIPROC 0x7fffffff +#define SHT_LOUSER 0x80000000 +#define SHT_HIUSER 0xffffffff /* sh_flags */ -#define SHF_WRITE 0x1 -#define SHF_ALLOC 0x2 -#define SHF_EXECINSTR 0x4 -#define SHF_MASKPROC 0xf0000000 +#define SHF_WRITE 0x1 +#define SHF_ALLOC 0x2 +#define SHF_EXECINSTR 0x4 +#define SHF_MASKPROC 0xf0000000 /* special section indexes */ -#define SHN_UNDEF 0 -#define SHN_LORESERVE 0xff00 -#define SHN_LOPROC 0xff00 -#define SHN_HIPROC 0xff1f -#define SHN_ABS 0xfff1 -#define SHN_COMMON 0xfff2 -#define SHN_HIRESERVE 0xffff - +#define SHN_UNDEF 0 +#define SHN_LORESERVE 0xff00 +#define SHN_LOPROC 0xff00 +#define SHN_HIPROC 0xff1f +#define SHN_ABS 0xfff1 +#define SHN_COMMON 0xfff2 +#define SHN_HIRESERVE 0xffff + typedef struct { - Elf32_Word sh_name; - Elf32_Word sh_type; - Elf32_Word sh_flags; - Elf32_Addr sh_addr; - Elf32_Off sh_offset; - Elf32_Word sh_size; - Elf32_Word sh_link; - Elf32_Word sh_info; - Elf32_Word sh_addralign; - Elf32_Word sh_entsize; + Elf32_Word sh_name; + Elf32_Word sh_type; + Elf32_Word sh_flags; + Elf32_Addr sh_addr; + Elf32_Off sh_offset; + Elf32_Word sh_size; + Elf32_Word sh_link; + Elf32_Word sh_info; + Elf32_Word sh_addralign; + Elf32_Word sh_entsize; } Elf32_Shdr; typedef struct elf64_shdr { - Elf64_Word sh_name; /* Section name, index in string tbl */ - Elf64_Word sh_type; /* Type of section */ - Elf64_Xword sh_flags; /* Miscellaneous section attributes */ - Elf64_Addr sh_addr; /* Section virtual addr at execution */ - Elf64_Off sh_offset; /* Section file offset */ - Elf64_Xword sh_size; /* Size of section in bytes */ - Elf64_Word sh_link; /* Index of another section */ - Elf64_Word sh_info; /* Additional section information */ - Elf64_Xword sh_addralign; /* Section alignment */ - Elf64_Xword sh_entsize; /* Entry size if section holds table */ + Elf64_Word sh_name; /* Section name, index in string tbl */ + Elf64_Word sh_type; /* Type of section */ + Elf64_Xword sh_flags; /* Miscellaneous section attributes */ + Elf64_Addr sh_addr; /* Section virtual addr at execution */ + Elf64_Off sh_offset; /* Section file offset */ + Elf64_Xword sh_size; /* Size of section in bytes */ + Elf64_Word sh_link; /* Index of another section */ + Elf64_Word sh_info; /* Additional section information */ + Elf64_Xword sh_addralign; /* Section alignment */ + Elf64_Xword sh_entsize; /* Entry size if section holds table */ } Elf64_Shdr; -#define EI_MAG0 0 /* e_ident[] indexes */ -#define EI_MAG1 1 -#define EI_MAG2 2 -#define EI_MAG3 3 -#define EI_CLASS 4 -#define EI_DATA 5 -#define EI_VERSION 6 -#define EI_OSABI 7 -#define EI_PAD 8 - -#define ELFMAG0 0x7f /* EI_MAG */ -#define ELFMAG1 'E' -#define ELFMAG2 'L' -#define ELFMAG3 'F' -#define ELFMAG "\177ELF" -#define SELFMAG 4 - -#define ELFCLASSNONE 0 /* EI_CLASS */ -#define ELFCLASS32 1 -#define ELFCLASS64 2 -#define ELFCLASSNUM 3 - -#define ELFDATANONE 0 /* e_ident[EI_DATA] */ -#define ELFDATA2LSB 1 -#define ELFDATA2MSB 2 - -#define EV_NONE 0 /* e_version, EI_VERSION */ -#define EV_CURRENT 1 -#define EV_NUM 2 - -#define ELFOSABI_NONE 0 -#define ELFOSABI_LINUX 3 +#define EI_MAG0 0 /* e_ident[] indexes */ +#define EI_MAG1 1 +#define EI_MAG2 2 +#define EI_MAG3 3 +#define EI_CLASS 4 +#define EI_DATA 5 +#define EI_VERSION 6 +#define EI_OSABI 7 +#define EI_PAD 8 + +#define ELFMAG0 0x7f /* EI_MAG */ +#define ELFMAG1 'E' +#define ELFMAG2 'L' +#define ELFMAG3 'F' +#define ELFMAG "\177ELF" +#define SELFMAG 4 + +#define ELFCLASSNONE 0 /* EI_CLASS */ +#define ELFCLASS32 1 +#define ELFCLASS64 2 +#define ELFCLASSNUM 3 + +#define ELFDATANONE 0 /* e_ident[EI_DATA] */ +#define ELFDATA2LSB 1 +#define ELFDATA2MSB 2 + +#define EV_NONE 0 /* e_version, EI_VERSION */ +#define EV_CURRENT 1 +#define EV_NUM 2 + +#define ELFOSABI_NONE 0 +#define ELFOSABI_LINUX 3 #ifndef ELF_OSABI #define ELF_OSABI ELFOSABI_NONE #endif /* Notes used in ET_CORE */ -#define NT_PRSTATUS 1 -#define NT_PRFPREG 2 -#define NT_PRPSINFO 3 -#define NT_TASKSTRUCT 4 -#define NT_AUXV 6 -#define NT_PRXFPREG 0x46e62b7f /* copied from gdb5.1/include/elf/common.h */ -#define NT_PPC_VMX 0x100 /* PowerPC Altivec/VMX registers */ -#define NT_PPC_SPE 0x101 /* PowerPC SPE/EVR registers */ -#define NT_PPC_VSX 0x102 /* PowerPC VSX registers */ -#define NT_386_TLS 0x200 /* i386 TLS slots (struct user_desc) */ -#define NT_386_IOPERM 0x201 /* x86 io permission bitmap (1=deny) */ -#define NT_PRXSTATUS 0x300 /* s390 upper register halves */ - +#define NT_PRSTATUS 1 +#define NT_PRFPREG 2 +#define NT_PRPSINFO 3 +#define NT_TASKSTRUCT 4 +#define NT_AUXV 6 +#define NT_PRXFPREG 0x46e62b7f /* copied from gdb5.1/include/elf/common.h */ +#define NT_PPC_VMX 0x100 /* PowerPC Altivec/VMX registers */ +#define NT_PPC_SPE 0x101 /* PowerPC SPE/EVR registers */ +#define NT_PPC_VSX 0x102 /* PowerPC VSX registers */ +#define NT_386_TLS 0x200 /* i386 TLS slots (struct user_desc) */ +#define NT_386_IOPERM 0x201 /* x86 io permission bitmap (1=deny) */ +#define NT_PRXSTATUS 0x300 /* s390 upper register halves */ /* Note header in a PT_NOTE section */ typedef struct elf32_note { - Elf32_Word n_namesz; /* Name size */ - Elf32_Word n_descsz; /* Content size */ - Elf32_Word n_type; /* Content type */ + Elf32_Word n_namesz; /* Name size */ + Elf32_Word n_descsz; /* Content size */ + Elf32_Word n_type; /* Content type */ } Elf32_Nhdr; /* Note header in a PT_NOTE section */ typedef struct elf64_note { - Elf64_Word n_namesz; /* Name size */ - Elf64_Word n_descsz; /* Content size */ - Elf64_Word n_type; /* Content type */ + Elf64_Word n_namesz; /* Name size */ + Elf64_Word n_descsz; /* Content size */ + Elf64_Word n_type; /* Content type */ } Elf64_Nhdr; diff --git a/Headers/StdKit/XCOFF.hxx b/Headers/StdKit/XCOFF.hxx index dfe33b8..cb37c55 100644 --- a/Headers/StdKit/XCOFF.hxx +++ b/Headers/StdKit/XCOFF.hxx @@ -16,25 +16,24 @@ #include <Headers/Defines.hpp> -#define kXCOFF64Magic 0x01F7 +#define kXCOFF64Magic 0x01F7 -#define kXCOFFRelFlg 0x0001 +#define kXCOFFRelFlg 0x0001 #define kXCOFFExecutable 0x0002 -#define kXCOFFLnno 0x0004 -#define kXCOFFLSyms 0x0008 - -namespace COmpilerKit { -/// @brief XCoff file header, meant for PowerPC programs. -typedef struct XCoffFileHeader -{ - UInt16 fMagic; - UInt16 fTarget; - UInt16 fNumSecs; - UInt32 fTimeDat; - UIntPtr fSymPtr; - UInt32 fNumSyms; - UInt16 fOptHdr; // ?: Number of bytes in optional header +#define kXCOFFLnno 0x0004 +#define kXCOFFLSyms 0x0008 + +namespace CompilerKit { +/// @brief XCoff identification header. +typedef struct XCoffFileHeader { + UInt16 fMagic; + UInt16 fTarget; + UInt16 fNumSecs; + UInt32 fTimeDat; + UIntPtr fSymPtr; + UInt32 fNumSyms; + UInt16 fOptHdr; // ?: Number of bytes in optional header } XCoffFileHeader; -} // namespace CompilerKit +} // namespace CompilerKit -#endif // ifndef __XCOFF__
\ No newline at end of file +#endif // ifndef __XCOFF__
\ No newline at end of file diff --git a/Headers/UUID.hpp b/Headers/UUID.hpp index 3817168..e283571 100644 --- a/Headers/UUID.hpp +++ b/Headers/UUID.hpp @@ -1,30 +1,29 @@ #ifndef STDUUID_H #define STDUUID_H +#include <array> +#include <atomic> +#include <chrono> #include <cstring> -#include <string> -#include <sstream> +#include <functional> #include <iomanip> -#include <array> -#include <string_view> #include <iterator> -#include <random> #include <memory> -#include <functional> -#include <type_traits> -#include <optional> -#include <chrono> #include <numeric> -#include <atomic> +#include <optional> +#include <random> +#include <sstream> +#include <string> +#include <string_view> +#include <type_traits> #ifdef __cplusplus -# if (__cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) -# define LIBUUID_CPP20_OR_GREATER -# endif - +#if (__cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) +#define LIBUUID_CPP20_OR_GREATER #endif +#endif #ifdef LIBUUID_CPP20_OR_GREATER #include <span> @@ -46,7 +45,7 @@ #endif #ifdef UUID_TIME_GENERATOR -#include <iphlpapi.h> +#include <iphlpapi.h> #pragma comment(lib, "IPHLPAPI.lib") #endif @@ -64,904 +63,803 @@ #endif -namespace uuids -{ +namespace uuids { #ifdef __cpp_lib_span - template <class ElementType, std::size_t Extent> - using span = std::span<ElementType, Extent>; +template <class ElementType, std::size_t Extent> +using span = std::span<ElementType, Extent>; #else - template <class ElementType, std::ptrdiff_t Extent> - using span = gsl::span<ElementType, Extent>; +template <class ElementType, std::ptrdiff_t Extent> +using span = gsl::span<ElementType, Extent>; #endif - namespace detail - { - template <typename TChar> - [[nodiscard]] constexpr inline unsigned char hex2char(TChar const ch) noexcept - { - if (ch >= static_cast<TChar>('0') && ch <= static_cast<TChar>('9')) - return static_cast<unsigned char>(ch - static_cast<TChar>('0')); - if (ch >= static_cast<TChar>('a') && ch <= static_cast<TChar>('f')) - return static_cast<unsigned char>(10 + ch - static_cast<TChar>('a')); - if (ch >= static_cast<TChar>('A') && ch <= static_cast<TChar>('F')) - return static_cast<unsigned char>(10 + ch - static_cast<TChar>('A')); - return 0; - } +namespace detail { +template <typename TChar> +[[nodiscard]] constexpr inline unsigned char hex2char(TChar const ch) noexcept { + if (ch >= static_cast<TChar>('0') && ch <= static_cast<TChar>('9')) + return static_cast<unsigned char>(ch - static_cast<TChar>('0')); + if (ch >= static_cast<TChar>('a') && ch <= static_cast<TChar>('f')) + return static_cast<unsigned char>(10 + ch - static_cast<TChar>('a')); + if (ch >= static_cast<TChar>('A') && ch <= static_cast<TChar>('F')) + return static_cast<unsigned char>(10 + ch - static_cast<TChar>('A')); + return 0; +} - template <typename TChar> - [[nodiscard]] constexpr inline bool is_hex(TChar const ch) noexcept - { - return - (ch >= static_cast<TChar>('0') && ch <= static_cast<TChar>('9')) || - (ch >= static_cast<TChar>('a') && ch <= static_cast<TChar>('f')) || - (ch >= static_cast<TChar>('A') && ch <= static_cast<TChar>('F')); - } +template <typename TChar> +[[nodiscard]] constexpr inline bool is_hex(TChar const ch) noexcept { + return (ch >= static_cast<TChar>('0') && ch <= static_cast<TChar>('9')) || + (ch >= static_cast<TChar>('a') && ch <= static_cast<TChar>('f')) || + (ch >= static_cast<TChar>('A') && ch <= static_cast<TChar>('F')); +} - template <typename TChar> - [[nodiscard]] constexpr std::basic_string_view<TChar> to_string_view(TChar const * str) noexcept - { - if (str) return str; - return {}; - } +template <typename TChar> +[[nodiscard]] constexpr std::basic_string_view<TChar> to_string_view( + TChar const* str) noexcept { + if (str) return str; + return {}; +} - template <typename StringType> - [[nodiscard]] - constexpr std::basic_string_view< - typename StringType::value_type, - typename StringType::traits_type> - to_string_view(StringType const & str) noexcept - { - return str; - } +template <typename StringType> +[[nodiscard]] constexpr std::basic_string_view<typename StringType::value_type, + typename StringType::traits_type> +to_string_view(StringType const& str) noexcept { + return str; +} - class sha1 - { - public: - using digest32_t = uint32_t[5]; - using digest8_t = uint8_t[20]; - - static constexpr unsigned int block_bytes = 64; - - [[nodiscard]] inline static uint32_t left_rotate(uint32_t value, size_t const count) noexcept - { - return (value << count) ^ (value >> (32 - count)); - } - - sha1() { reset(); } - - void reset() noexcept - { - m_digest[0] = 0x67452301; - m_digest[1] = 0xEFCDAB89; - m_digest[2] = 0x98BADCFE; - m_digest[3] = 0x10325476; - m_digest[4] = 0xC3D2E1F0; - m_blockByteIndex = 0; - m_byteCount = 0; - } - - void process_byte(uint8_t octet) - { - this->m_block[this->m_blockByteIndex++] = octet; - ++this->m_byteCount; - if (m_blockByteIndex == block_bytes) - { - this->m_blockByteIndex = 0; - process_block(); - } - } - - void process_block(void const * const start, void const * const end) - { - const uint8_t* begin = static_cast<const uint8_t*>(start); - const uint8_t* finish = static_cast<const uint8_t*>(end); - while (begin != finish) - { - process_byte(*begin); - begin++; - } - } - - void process_bytes(void const * const data, size_t const len) - { - const uint8_t* block = static_cast<const uint8_t*>(data); - process_block(block, block + len); - } - - uint32_t const * get_digest(digest32_t digest) - { - size_t const bitCount = this->m_byteCount * 8; - process_byte(0x80); - if (this->m_blockByteIndex > 56) { - while (m_blockByteIndex != 0) { - process_byte(0); - } - while (m_blockByteIndex < 56) { - process_byte(0); - } - } - else { - while (m_blockByteIndex < 56) { - process_byte(0); - } - } - process_byte(0); - process_byte(0); - process_byte(0); - process_byte(0); - process_byte(static_cast<unsigned char>((bitCount >> 24) & 0xFF)); - process_byte(static_cast<unsigned char>((bitCount >> 16) & 0xFF)); - process_byte(static_cast<unsigned char>((bitCount >> 8) & 0xFF)); - process_byte(static_cast<unsigned char>((bitCount) & 0xFF)); - - memcpy(digest, m_digest, 5 * sizeof(uint32_t)); - return digest; - } - - uint8_t const * get_digest_bytes(digest8_t digest) - { - digest32_t d32; - get_digest(d32); - size_t di = 0; - digest[di++] = static_cast<uint8_t>(d32[0] >> 24); - digest[di++] = static_cast<uint8_t>(d32[0] >> 16); - digest[di++] = static_cast<uint8_t>(d32[0] >> 8); - digest[di++] = static_cast<uint8_t>(d32[0] >> 0); - - digest[di++] = static_cast<uint8_t>(d32[1] >> 24); - digest[di++] = static_cast<uint8_t>(d32[1] >> 16); - digest[di++] = static_cast<uint8_t>(d32[1] >> 8); - digest[di++] = static_cast<uint8_t>(d32[1] >> 0); - - digest[di++] = static_cast<uint8_t>(d32[2] >> 24); - digest[di++] = static_cast<uint8_t>(d32[2] >> 16); - digest[di++] = static_cast<uint8_t>(d32[2] >> 8); - digest[di++] = static_cast<uint8_t>(d32[2] >> 0); - - digest[di++] = static_cast<uint8_t>(d32[3] >> 24); - digest[di++] = static_cast<uint8_t>(d32[3] >> 16); - digest[di++] = static_cast<uint8_t>(d32[3] >> 8); - digest[di++] = static_cast<uint8_t>(d32[3] >> 0); - - digest[di++] = static_cast<uint8_t>(d32[4] >> 24); - digest[di++] = static_cast<uint8_t>(d32[4] >> 16); - digest[di++] = static_cast<uint8_t>(d32[4] >> 8); - digest[di++] = static_cast<uint8_t>(d32[4] >> 0); - - return digest; - } - - private: - void process_block() - { - uint32_t w[80]; - for (size_t i = 0; i < 16; i++) { - w[i] = static_cast<uint32_t>(m_block[i * 4 + 0] << 24); - w[i] |= static_cast<uint32_t>(m_block[i * 4 + 1] << 16); - w[i] |= static_cast<uint32_t>(m_block[i * 4 + 2] << 8); - w[i] |= static_cast<uint32_t>(m_block[i * 4 + 3]); - } - for (size_t i = 16; i < 80; i++) { - w[i] = left_rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1); - } - - uint32_t a = m_digest[0]; - uint32_t b = m_digest[1]; - uint32_t c = m_digest[2]; - uint32_t d = m_digest[3]; - uint32_t e = m_digest[4]; - - for (std::size_t i = 0; i < 80; ++i) - { - uint32_t f = 0; - uint32_t k = 0; - - if (i < 20) { - f = (b & c) | (~b & d); - k = 0x5A827999; - } - else if (i < 40) { - f = b ^ c ^ d; - k = 0x6ED9EBA1; - } - else if (i < 60) { - f = (b & c) | (b & d) | (c & d); - k = 0x8F1BBCDC; - } - else { - f = b ^ c ^ d; - k = 0xCA62C1D6; - } - uint32_t temp = left_rotate(a, 5) + f + e + k + w[i]; - e = d; - d = c; - c = left_rotate(b, 30); - b = a; - a = temp; - } - - m_digest[0] += a; - m_digest[1] += b; - m_digest[2] += c; - m_digest[3] += d; - m_digest[4] += e; - } - - private: - digest32_t m_digest; - uint8_t m_block[64]; - size_t m_blockByteIndex; - size_t m_byteCount; - }; - - template <typename CharT> - inline constexpr CharT empty_guid[37] = "00000000-0000-0000-0000-000000000000"; - - template <> - inline constexpr wchar_t empty_guid<wchar_t>[37] = L"00000000-0000-0000-0000-000000000000"; - - template <typename CharT> - inline constexpr CharT guid_encoder[17] = "0123456789abcdef"; - - template <> - inline constexpr wchar_t guid_encoder<wchar_t>[17] = L"0123456789abcdef"; - } - - // -------------------------------------------------------------------------------------------------------------------------- - // UUID format https://tools.ietf.org/html/rfc4122 - // -------------------------------------------------------------------------------------------------------------------------- - - // -------------------------------------------------------------------------------------------------------------------------- - // Field NDR Data Type Octet # Note - // -------------------------------------------------------------------------------------------------------------------------- - // time_low unsigned long 0 - 3 The low field of the timestamp. - // time_mid unsigned short 4 - 5 The middle field of the timestamp. - // time_hi_and_version unsigned short 6 - 7 The high field of the timestamp multiplexed with the version number. - // clock_seq_hi_and_reserved unsigned small 8 The high field of the clock sequence multiplexed with the variant. - // clock_seq_low unsigned small 9 The low field of the clock sequence. - // node character 10 - 15 The spatially unique node identifier. - // -------------------------------------------------------------------------------------------------------------------------- - // 0 1 2 3 - // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - // | time_low | - // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - // | time_mid | time_hi_and_version | - // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - // |clk_seq_hi_res | clk_seq_low | node (0-1) | - // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - // | node (2-5) | - // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - - // -------------------------------------------------------------------------------------------------------------------------- - // enumerations - // -------------------------------------------------------------------------------------------------------------------------- - - // indicated by a bit pattern in octet 8, marked with N in xxxxxxxx-xxxx-xxxx-Nxxx-xxxxxxxxxxxx - enum class uuid_variant - { - // NCS backward compatibility (with the obsolete Apollo Network Computing System 1.5 UUID format) - // N bit pattern: 0xxx - // > the first 6 octets of the UUID are a 48-bit timestamp (the number of 4 microsecond units of time since 1 Jan 1980 UTC); - // > the next 2 octets are reserved; - // > the next octet is the "address family"; - // > the final 7 octets are a 56-bit host ID in the form specified by the address family - ncs, - - // RFC 4122/DCE 1.1 - // N bit pattern: 10xx - // > big-endian byte order - rfc, - - // Microsoft Corporation backward compatibility - // N bit pattern: 110x - // > little endian byte order - // > formely used in the Component Object Model (COM) library - microsoft, - - // reserved for possible future definition - // N bit pattern: 111x - reserved - }; - - // indicated by a bit pattern in octet 6, marked with M in xxxxxxxx-xxxx-Mxxx-xxxx-xxxxxxxxxxxx - enum class uuid_version - { - none = 0, // only possible for nil or invalid uuids - time_based = 1, // The time-based version specified in RFC 4122 - dce_security = 2, // DCE Security version, with embedded POSIX UIDs. - name_based_md5 = 3, // The name-based version specified in RFS 4122 with MD5 hashing - random_number_based = 4, // The randomly or pseudo-randomly generated version specified in RFS 4122 - name_based_sha1 = 5 // The name-based version specified in RFS 4122 with SHA1 hashing - }; - - // Forward declare uuid & to_string so that we can declare to_string as a friend later. - class uuid; - template <class CharT = char, - class Traits = std::char_traits<CharT>, - class Allocator = std::allocator<CharT>> - std::basic_string<CharT, Traits, Allocator> to_string(uuid const &id); - - // -------------------------------------------------------------------------------------------------------------------------- - // uuid class - // -------------------------------------------------------------------------------------------------------------------------- - class uuid - { - public: - using value_type = uint8_t; - - constexpr uuid() noexcept = default; - - uuid(value_type(&arr)[16]) noexcept - { - std::copy(std::cbegin(arr), std::cend(arr), std::begin(data)); +class sha1 { + public: + using digest32_t = uint32_t[5]; + using digest8_t = uint8_t[20]; + + static constexpr unsigned int block_bytes = 64; + + [[nodiscard]] inline static uint32_t left_rotate( + uint32_t value, size_t const count) noexcept { + return (value << count) ^ (value >> (32 - count)); + } + + sha1() { reset(); } + + void reset() noexcept { + m_digest[0] = 0x67452301; + m_digest[1] = 0xEFCDAB89; + m_digest[2] = 0x98BADCFE; + m_digest[3] = 0x10325476; + m_digest[4] = 0xC3D2E1F0; + m_blockByteIndex = 0; + m_byteCount = 0; + } + + void process_byte(uint8_t octet) { + this->m_block[this->m_blockByteIndex++] = octet; + ++this->m_byteCount; + if (m_blockByteIndex == block_bytes) { + this->m_blockByteIndex = 0; + process_block(); + } + } + + void process_block(void const* const start, void const* const end) { + const uint8_t* begin = static_cast<const uint8_t*>(start); + const uint8_t* finish = static_cast<const uint8_t*>(end); + while (begin != finish) { + process_byte(*begin); + begin++; + } + } + + void process_bytes(void const* const data, size_t const len) { + const uint8_t* block = static_cast<const uint8_t*>(data); + process_block(block, block + len); + } + + uint32_t const* get_digest(digest32_t digest) { + size_t const bitCount = this->m_byteCount * 8; + process_byte(0x80); + if (this->m_blockByteIndex > 56) { + while (m_blockByteIndex != 0) { + process_byte(0); } - - constexpr uuid(std::array<value_type, 16> const & arr) noexcept : data{arr} {} - - explicit uuid(span<value_type, 16> bytes) - { - std::copy(std::cbegin(bytes), std::cend(bytes), std::begin(data)); + while (m_blockByteIndex < 56) { + process_byte(0); } - - template<typename ForwardIterator> - explicit uuid(ForwardIterator first, ForwardIterator last) - { - if (std::distance(first, last) == 16) - std::copy(first, last, std::begin(data)); + } else { + while (m_blockByteIndex < 56) { + process_byte(0); } - - [[nodiscard]] constexpr uuid_variant variant() const noexcept - { - if ((data[8] & 0x80) == 0x00) - return uuid_variant::ncs; - else if ((data[8] & 0xC0) == 0x80) - return uuid_variant::rfc; - else if ((data[8] & 0xE0) == 0xC0) - return uuid_variant::microsoft; - else - return uuid_variant::reserved; + } + process_byte(0); + process_byte(0); + process_byte(0); + process_byte(0); + process_byte(static_cast<unsigned char>((bitCount >> 24) & 0xFF)); + process_byte(static_cast<unsigned char>((bitCount >> 16) & 0xFF)); + process_byte(static_cast<unsigned char>((bitCount >> 8) & 0xFF)); + process_byte(static_cast<unsigned char>((bitCount) & 0xFF)); + + memcpy(digest, m_digest, 5 * sizeof(uint32_t)); + return digest; + } + + uint8_t const* get_digest_bytes(digest8_t digest) { + digest32_t d32; + get_digest(d32); + size_t di = 0; + digest[di++] = static_cast<uint8_t>(d32[0] >> 24); + digest[di++] = static_cast<uint8_t>(d32[0] >> 16); + digest[di++] = static_cast<uint8_t>(d32[0] >> 8); + digest[di++] = static_cast<uint8_t>(d32[0] >> 0); + + digest[di++] = static_cast<uint8_t>(d32[1] >> 24); + digest[di++] = static_cast<uint8_t>(d32[1] >> 16); + digest[di++] = static_cast<uint8_t>(d32[1] >> 8); + digest[di++] = static_cast<uint8_t>(d32[1] >> 0); + + digest[di++] = static_cast<uint8_t>(d32[2] >> 24); + digest[di++] = static_cast<uint8_t>(d32[2] >> 16); + digest[di++] = static_cast<uint8_t>(d32[2] >> 8); + digest[di++] = static_cast<uint8_t>(d32[2] >> 0); + + digest[di++] = static_cast<uint8_t>(d32[3] >> 24); + digest[di++] = static_cast<uint8_t>(d32[3] >> 16); + digest[di++] = static_cast<uint8_t>(d32[3] >> 8); + digest[di++] = static_cast<uint8_t>(d32[3] >> 0); + + digest[di++] = static_cast<uint8_t>(d32[4] >> 24); + digest[di++] = static_cast<uint8_t>(d32[4] >> 16); + digest[di++] = static_cast<uint8_t>(d32[4] >> 8); + digest[di++] = static_cast<uint8_t>(d32[4] >> 0); + + return digest; + } + + private: + void process_block() { + uint32_t w[80]; + for (size_t i = 0; i < 16; i++) { + w[i] = static_cast<uint32_t>(m_block[i * 4 + 0] << 24); + w[i] |= static_cast<uint32_t>(m_block[i * 4 + 1] << 16); + w[i] |= static_cast<uint32_t>(m_block[i * 4 + 2] << 8); + w[i] |= static_cast<uint32_t>(m_block[i * 4 + 3]); + } + for (size_t i = 16; i < 80; i++) { + w[i] = left_rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1); + } + + uint32_t a = m_digest[0]; + uint32_t b = m_digest[1]; + uint32_t c = m_digest[2]; + uint32_t d = m_digest[3]; + uint32_t e = m_digest[4]; + + for (std::size_t i = 0; i < 80; ++i) { + uint32_t f = 0; + uint32_t k = 0; + + if (i < 20) { + f = (b & c) | (~b & d); + k = 0x5A827999; + } else if (i < 40) { + f = b ^ c ^ d; + k = 0x6ED9EBA1; + } else if (i < 60) { + f = (b & c) | (b & d) | (c & d); + k = 0x8F1BBCDC; + } else { + f = b ^ c ^ d; + k = 0xCA62C1D6; } - - [[nodiscard]] constexpr uuid_version version() const noexcept - { - if ((data[6] & 0xF0) == 0x10) - return uuid_version::time_based; - else if ((data[6] & 0xF0) == 0x20) - return uuid_version::dce_security; - else if ((data[6] & 0xF0) == 0x30) - return uuid_version::name_based_md5; - else if ((data[6] & 0xF0) == 0x40) - return uuid_version::random_number_based; - else if ((data[6] & 0xF0) == 0x50) - return uuid_version::name_based_sha1; - else - return uuid_version::none; + uint32_t temp = left_rotate(a, 5) + f + e + k + w[i]; + e = d; + d = c; + c = left_rotate(b, 30); + b = a; + a = temp; + } + + m_digest[0] += a; + m_digest[1] += b; + m_digest[2] += c; + m_digest[3] += d; + m_digest[4] += e; + } + + private: + digest32_t m_digest; + uint8_t m_block[64]; + size_t m_blockByteIndex; + size_t m_byteCount; +}; + +template <typename CharT> +inline constexpr CharT empty_guid[37] = "00000000-0000-0000-0000-000000000000"; + +template <> +inline constexpr wchar_t empty_guid<wchar_t>[37] = + L"00000000-0000-0000-0000-000000000000"; + +template <typename CharT> +inline constexpr CharT guid_encoder[17] = "0123456789abcdef"; + +template <> +inline constexpr wchar_t guid_encoder<wchar_t>[17] = L"0123456789abcdef"; +} // namespace detail + +// -------------------------------------------------------------------------------------------------------------------------- +// UUID format https://tools.ietf.org/html/rfc4122 +// -------------------------------------------------------------------------------------------------------------------------- + +// -------------------------------------------------------------------------------------------------------------------------- +// Field NDR Data Type Octet # Note +// -------------------------------------------------------------------------------------------------------------------------- +// time_low unsigned long 0 - 3 The low field +// of the timestamp. time_mid unsigned short 4 - 5 +// The middle field of the timestamp. time_hi_and_version unsigned +// short 6 - 7 The high field of the timestamp multiplexed +// with the version number. clock_seq_hi_and_reserved unsigned small 8 +// The high field of the clock sequence multiplexed with the variant. +// clock_seq_low unsigned small 9 The low +// field of the clock sequence. node character 10 +// - 15 The spatially unique node identifier. +// -------------------------------------------------------------------------------------------------------------------------- +// 0 1 2 3 +// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// | time_low | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// | time_mid | time_hi_and_version | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// |clk_seq_hi_res | clk_seq_low | node (0-1) | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// | node (2-5) | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +// -------------------------------------------------------------------------------------------------------------------------- +// enumerations +// -------------------------------------------------------------------------------------------------------------------------- + +// indicated by a bit pattern in octet 8, marked with N in +// xxxxxxxx-xxxx-xxxx-Nxxx-xxxxxxxxxxxx +enum class uuid_variant { + // NCS backward compatibility (with the obsolete Apollo Network Computing + // System 1.5 UUID format) N bit pattern: 0xxx > the first 6 octets of the + // UUID are a 48-bit timestamp (the number of 4 microsecond units of time + // since 1 Jan 1980 UTC); > the next 2 octets are reserved; > the next octet + // is the "address family"; > the final 7 octets are a 56-bit host ID in the + // form specified by the address family + ncs, + + // RFC 4122/DCE 1.1 + // N bit pattern: 10xx + // > big-endian byte order + rfc, + + // Microsoft Corporation backward compatibility + // N bit pattern: 110x + // > little endian byte order + // > formely used in the Component Object Model (COM) library + microsoft, + + // reserved for possible future definition + // N bit pattern: 111x + reserved +}; + +// indicated by a bit pattern in octet 6, marked with M in +// xxxxxxxx-xxxx-Mxxx-xxxx-xxxxxxxxxxxx +enum class uuid_version { + none = 0, // only possible for nil or invalid uuids + time_based = 1, // The time-based version specified in RFC 4122 + dce_security = 2, // DCE Security version, with embedded POSIX UIDs. + name_based_md5 = + 3, // The name-based version specified in RFS 4122 with MD5 hashing + random_number_based = 4, // The randomly or pseudo-randomly generated version + // specified in RFS 4122 + name_based_sha1 = + 5 // The name-based version specified in RFS 4122 with SHA1 hashing +}; + +// Forward declare uuid & to_string so that we can declare to_string as a friend +// later. +class uuid; +template <class CharT = char, class Traits = std::char_traits<CharT>, + class Allocator = std::allocator<CharT>> +std::basic_string<CharT, Traits, Allocator> to_string(uuid const& id); + +// -------------------------------------------------------------------------------------------------------------------------- +// uuid class +// -------------------------------------------------------------------------------------------------------------------------- +class uuid { + public: + using value_type = uint8_t; + + constexpr uuid() noexcept = default; + + uuid(value_type (&arr)[16]) noexcept { + std::copy(std::cbegin(arr), std::cend(arr), std::begin(data)); + } + + constexpr uuid(std::array<value_type, 16> const& arr) noexcept : data{arr} {} + + explicit uuid(span<value_type, 16> bytes) { + std::copy(std::cbegin(bytes), std::cend(bytes), std::begin(data)); + } + + template <typename ForwardIterator> + explicit uuid(ForwardIterator first, ForwardIterator last) { + if (std::distance(first, last) == 16) + std::copy(first, last, std::begin(data)); + } + + [[nodiscard]] constexpr uuid_variant variant() const noexcept { + if ((data[8] & 0x80) == 0x00) + return uuid_variant::ncs; + else if ((data[8] & 0xC0) == 0x80) + return uuid_variant::rfc; + else if ((data[8] & 0xE0) == 0xC0) + return uuid_variant::microsoft; + else + return uuid_variant::reserved; + } + + [[nodiscard]] constexpr uuid_version version() const noexcept { + if ((data[6] & 0xF0) == 0x10) + return uuid_version::time_based; + else if ((data[6] & 0xF0) == 0x20) + return uuid_version::dce_security; + else if ((data[6] & 0xF0) == 0x30) + return uuid_version::name_based_md5; + else if ((data[6] & 0xF0) == 0x40) + return uuid_version::random_number_based; + else if ((data[6] & 0xF0) == 0x50) + return uuid_version::name_based_sha1; + else + return uuid_version::none; + } + + [[nodiscard]] constexpr bool is_nil() const noexcept { + for (size_t i = 0; i < data.size(); ++i) + if (data[i] != 0) return false; + return true; + } + + void swap(uuid& other) noexcept { data.swap(other.data); } + + [[nodiscard]] inline span<std::byte const, 16> as_bytes() const { + return span<std::byte const, 16>( + reinterpret_cast<std::byte const*>(data.data()), 16); + } + + template <typename StringType> + [[nodiscard]] constexpr static bool is_valid_uuid( + StringType const& in_str) noexcept { + auto str = detail::to_string_view(in_str); + bool firstDigit = true; + size_t hasBraces = 0; + size_t index = 0; + + if (str.empty()) return false; + + if (str.front() == '{') hasBraces = 1; + if (hasBraces && str.back() != '}') return false; + + for (size_t i = hasBraces; i < str.size() - hasBraces; ++i) { + if (str[i] == '-') continue; + + if (index >= 16 || !detail::is_hex(str[i])) { + return false; } - [[nodiscard]] constexpr bool is_nil() const noexcept - { - for (size_t i = 0; i < data.size(); ++i) if (data[i] != 0) return false; - return true; + if (firstDigit) { + firstDigit = false; + } else { + index++; + firstDigit = true; } + } - void swap(uuid & other) noexcept - { - data.swap(other.data); - } + if (index < 16) { + return false; + } - [[nodiscard]] inline span<std::byte const, 16> as_bytes() const - { - return span<std::byte const, 16>(reinterpret_cast<std::byte const*>(data.data()), 16); - } + return true; + } - template <typename StringType> - [[nodiscard]] constexpr static bool is_valid_uuid(StringType const & in_str) noexcept - { - auto str = detail::to_string_view(in_str); - bool firstDigit = true; - size_t hasBraces = 0; - size_t index = 0; - - if (str.empty()) - return false; - - if (str.front() == '{') - hasBraces = 1; - if (hasBraces && str.back() != '}') - return false; - - for (size_t i = hasBraces; i < str.size() - hasBraces; ++i) - { - if (str[i] == '-') continue; - - if (index >= 16 || !detail::is_hex(str[i])) - { - return false; - } - - if (firstDigit) - { - firstDigit = false; - } - else - { - index++; - firstDigit = true; - } - } - - if (index < 16) - { - return false; - } - - return true; - } + template <typename StringType> + [[nodiscard]] constexpr static std::optional<uuid> from_string( + StringType const& in_str) noexcept { + auto str = detail::to_string_view(in_str); + bool firstDigit = true; + size_t hasBraces = 0; + size_t index = 0; + + std::array<uint8_t, 16> data{{0}}; + + if (str.empty()) return {}; + + if (str.front() == '{') hasBraces = 1; + if (hasBraces && str.back() != '}') return {}; - template <typename StringType> - [[nodiscard]] constexpr static std::optional<uuid> from_string(StringType const & in_str) noexcept - { - auto str = detail::to_string_view(in_str); - bool firstDigit = true; - size_t hasBraces = 0; - size_t index = 0; - - std::array<uint8_t, 16> data{ { 0 } }; - - if (str.empty()) return {}; - - if (str.front() == '{') - hasBraces = 1; - if (hasBraces && str.back() != '}') - return {}; - - for (size_t i = hasBraces; i < str.size() - hasBraces; ++i) - { - if (str[i] == '-') continue; - - if (index >= 16 || !detail::is_hex(str[i])) - { - return {}; - } - - if (firstDigit) - { - data[index] = static_cast<uint8_t>(detail::hex2char(str[i]) << 4); - firstDigit = false; - } - else - { - data[index] = static_cast<uint8_t>(data[index] | detail::hex2char(str[i])); - index++; - firstDigit = true; - } - } - - if (index < 16) - { - return {}; - } - - return uuid{ data }; + for (size_t i = hasBraces; i < str.size() - hasBraces; ++i) { + if (str[i] == '-') continue; + + if (index >= 16 || !detail::is_hex(str[i])) { + return {}; } - private: - std::array<value_type, 16> data{ { 0 } }; - - friend bool operator==(uuid const & lhs, uuid const & rhs) noexcept; - friend bool operator<(uuid const & lhs, uuid const & rhs) noexcept; - - template <class Elem, class Traits> - friend std::basic_ostream<Elem, Traits> & operator<<(std::basic_ostream<Elem, Traits> &s, uuid const & id); - - template<class CharT, class Traits, class Allocator> - friend std::basic_string<CharT, Traits, Allocator> to_string(uuid const& id); - - friend std::hash<uuid>; - }; - - // -------------------------------------------------------------------------------------------------------------------------- - // operators and non-member functions - // -------------------------------------------------------------------------------------------------------------------------- - - [[nodiscard]] inline bool operator== (uuid const& lhs, uuid const& rhs) noexcept - { - return lhs.data == rhs.data; - } - - [[nodiscard]] inline bool operator!= (uuid const& lhs, uuid const& rhs) noexcept - { - return !(lhs == rhs); - } - - [[nodiscard]] inline bool operator< (uuid const& lhs, uuid const& rhs) noexcept - { - return lhs.data < rhs.data; - } - - template <class CharT, - class Traits, - class Allocator> - [[nodiscard]] inline std::basic_string<CharT, Traits, Allocator> to_string(uuid const & id) - { - std::basic_string<CharT, Traits, Allocator> uustr{detail::empty_guid<CharT>}; - - for (size_t i = 0, index = 0; i < 36; ++i) - { - if (i == 8 || i == 13 || i == 18 || i == 23) - { - continue; - } - uustr[i] = detail::guid_encoder<CharT>[id.data[index] >> 4 & 0x0f]; - uustr[++i] = detail::guid_encoder<CharT>[id.data[index] & 0x0f]; - index++; + if (firstDigit) { + data[index] = static_cast<uint8_t>(detail::hex2char(str[i]) << 4); + firstDigit = false; + } else { + data[index] = + static_cast<uint8_t>(data[index] | detail::hex2char(str[i])); + index++; + firstDigit = true; } + } + + if (index < 16) { + return {}; + } + + return uuid{data}; + } + + private: + std::array<value_type, 16> data{{0}}; + + friend bool operator==(uuid const& lhs, uuid const& rhs) noexcept; + friend bool operator<(uuid const& lhs, uuid const& rhs) noexcept; - return uustr; - } + template <class Elem, class Traits> + friend std::basic_ostream<Elem, Traits>& operator<<( + std::basic_ostream<Elem, Traits>& s, uuid const& id); - template <class Elem, class Traits> - std::basic_ostream<Elem, Traits>& operator<<(std::basic_ostream<Elem, Traits>& s, uuid const& id) - { - s << to_string(id); - return s; - } + template <class CharT, class Traits, class Allocator> + friend std::basic_string<CharT, Traits, Allocator> to_string(uuid const& id); - inline void swap(uuids::uuid & lhs, uuids::uuid & rhs) noexcept - { - lhs.swap(rhs); - } + friend std::hash<uuid>; +}; - // -------------------------------------------------------------------------------------------------------------------------- - // namespace IDs that could be used for generating name-based uuids - // -------------------------------------------------------------------------------------------------------------------------- +// -------------------------------------------------------------------------------------------------------------------------- +// operators and non-member functions +// -------------------------------------------------------------------------------------------------------------------------- - // Name string is a fully-qualified domain name - static uuid uuid_namespace_dns{ {0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} }; +[[nodiscard]] inline bool operator==(uuid const& lhs, + uuid const& rhs) noexcept { + return lhs.data == rhs.data; +} - // Name string is a URL - static uuid uuid_namespace_url{ {0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} }; +[[nodiscard]] inline bool operator!=(uuid const& lhs, + uuid const& rhs) noexcept { + return !(lhs == rhs); +} - // Name string is an ISO OID (See https://oidref.com/, https://en.wikipedia.org/wiki/Object_identifier) - static uuid uuid_namespace_oid{ {0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} }; +[[nodiscard]] inline bool operator<(uuid const& lhs, uuid const& rhs) noexcept { + return lhs.data < rhs.data; +} - // Name string is an X.500 DN, in DER or a text output format (See https://en.wikipedia.org/wiki/X.500, https://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One) - static uuid uuid_namespace_x500{ {0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} }; +template <class CharT, class Traits, class Allocator> +[[nodiscard]] inline std::basic_string<CharT, Traits, Allocator> to_string( + uuid const& id) { + std::basic_string<CharT, Traits, Allocator> uustr{detail::empty_guid<CharT>}; + + for (size_t i = 0, index = 0; i < 36; ++i) { + if (i == 8 || i == 13 || i == 18 || i == 23) { + continue; + } + uustr[i] = detail::guid_encoder<CharT>[id.data[index] >> 4 & 0x0f]; + uustr[++i] = detail::guid_encoder<CharT>[id.data[index] & 0x0f]; + index++; + } + + return uustr; +} - // -------------------------------------------------------------------------------------------------------------------------- - // uuid generators - // -------------------------------------------------------------------------------------------------------------------------- +template <class Elem, class Traits> +std::basic_ostream<Elem, Traits>& operator<<( + std::basic_ostream<Elem, Traits>& s, uuid const& id) { + s << to_string(id); + return s; +} + +inline void swap(uuids::uuid& lhs, uuids::uuid& rhs) noexcept { lhs.swap(rhs); } + +// -------------------------------------------------------------------------------------------------------------------------- +// namespace IDs that could be used for generating name-based uuids +// -------------------------------------------------------------------------------------------------------------------------- + +// Name string is a fully-qualified domain name +static uuid uuid_namespace_dns{{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, + 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, + 0xc8}}; + +// Name string is a URL +static uuid uuid_namespace_url{{0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, + 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, + 0xc8}}; + +// Name string is an ISO OID (See https://oidref.com/, +// https://en.wikipedia.org/wiki/Object_identifier) +static uuid uuid_namespace_oid{{0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, + 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, + 0xc8}}; + +// Name string is an X.500 DN, in DER or a text output format (See +// https://en.wikipedia.org/wiki/X.500, +// https://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One) +static uuid uuid_namespace_x500{{0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, + 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, + 0xc8}}; + +// -------------------------------------------------------------------------------------------------------------------------- +// uuid generators +// -------------------------------------------------------------------------------------------------------------------------- #ifdef UUID_SYSTEM_GENERATOR - class uuid_system_generator - { - public: - using result_type = uuid; +class uuid_system_generator { + public: + using result_type = uuid; - uuid operator()() - { + uuid operator()() { #ifdef _WIN32 - GUID newId; - HRESULT hr = ::CoCreateGuid(&newId); + GUID newId; + HRESULT hr = ::CoCreateGuid(&newId); - if (FAILED(hr)) - { - throw std::system_error(hr, std::system_category(), "CoCreateGuid failed"); - } + if (FAILED(hr)) { + throw std::system_error(hr, std::system_category(), + "CoCreateGuid failed"); + } - std::array<uint8_t, 16> bytes = - { { - static_cast<unsigned char>((newId.Data1 >> 24) & 0xFF), - static_cast<unsigned char>((newId.Data1 >> 16) & 0xFF), - static_cast<unsigned char>((newId.Data1 >> 8) & 0xFF), - static_cast<unsigned char>((newId.Data1) & 0xFF), + std::array<uint8_t, 16> bytes = { + {static_cast<unsigned char>((newId.Data1 >> 24) & 0xFF), + static_cast<unsigned char>((newId.Data1 >> 16) & 0xFF), + static_cast<unsigned char>((newId.Data1 >> 8) & 0xFF), + static_cast<unsigned char>((newId.Data1) & 0xFF), - (unsigned char)((newId.Data2 >> 8) & 0xFF), - (unsigned char)((newId.Data2) & 0xFF), + (unsigned char)((newId.Data2 >> 8) & 0xFF), + (unsigned char)((newId.Data2) & 0xFF), - (unsigned char)((newId.Data3 >> 8) & 0xFF), - (unsigned char)((newId.Data3) & 0xFF), + (unsigned char)((newId.Data3 >> 8) & 0xFF), + (unsigned char)((newId.Data3) & 0xFF), - newId.Data4[0], - newId.Data4[1], - newId.Data4[2], - newId.Data4[3], - newId.Data4[4], - newId.Data4[5], - newId.Data4[6], - newId.Data4[7] - } }; + newId.Data4[0], newId.Data4[1], newId.Data4[2], newId.Data4[3], + newId.Data4[4], newId.Data4[5], newId.Data4[6], newId.Data4[7]}}; - return uuid{ std::begin(bytes), std::end(bytes) }; + return uuid{std::begin(bytes), std::end(bytes)}; #elif defined(__linux__) || defined(__unix__) - uuid_t id; - uuid_generate(id); - - std::array<uint8_t, 16> bytes = - { { - id[0], - id[1], - id[2], - id[3], - id[4], - id[5], - id[6], - id[7], - id[8], - id[9], - id[10], - id[11], - id[12], - id[13], - id[14], - id[15] - } }; - - return uuid{ std::begin(bytes), std::end(bytes) }; + uuid_t id; + uuid_generate(id); + + std::array<uint8_t, 16> bytes = {{id[0], id[1], id[2], id[3], id[4], id[5], + id[6], id[7], id[8], id[9], id[10], + id[11], id[12], id[13], id[14], id[15]}}; + + return uuid{std::begin(bytes), std::end(bytes)}; #elif defined(__APPLE__) - auto newId = CFUUIDCreate(NULL); - auto bytes = CFUUIDGetUUIDBytes(newId); - CFRelease(newId); - - std::array<uint8_t, 16> arrbytes = - { { - bytes.byte0, - bytes.byte1, - bytes.byte2, - bytes.byte3, - bytes.byte4, - bytes.byte5, - bytes.byte6, - bytes.byte7, - bytes.byte8, - bytes.byte9, - bytes.byte10, - bytes.byte11, - bytes.byte12, - bytes.byte13, - bytes.byte14, - bytes.byte15 - } }; - return uuid{ std::begin(arrbytes), std::end(arrbytes) }; + auto newId = CFUUIDCreate(NULL); + auto bytes = CFUUIDGetUUIDBytes(newId); + CFRelease(newId); + + std::array<uint8_t, 16> arrbytes = { + {bytes.byte0, bytes.byte1, bytes.byte2, bytes.byte3, bytes.byte4, + bytes.byte5, bytes.byte6, bytes.byte7, bytes.byte8, bytes.byte9, + bytes.byte10, bytes.byte11, bytes.byte12, bytes.byte13, bytes.byte14, + bytes.byte15}}; + return uuid{std::begin(arrbytes), std::end(arrbytes)}; #else - return uuid{}; + return uuid{}; #endif - } - }; + } +}; #endif - template <typename UniformRandomNumberGenerator> - class basic_uuid_random_generator - { - public: - using engine_type = UniformRandomNumberGenerator; - - explicit basic_uuid_random_generator(engine_type& gen) : - generator(&gen, [](auto) {}) {} - explicit basic_uuid_random_generator(engine_type* gen) : - generator(gen, [](auto) {}) {} - - [[nodiscard]] uuid operator()() - { - alignas(uint32_t) uint8_t bytes[16]; - for (int i = 0; i < 16; i += 4) - *reinterpret_cast<uint32_t*>(bytes + i) = distribution(*generator); - - // variant must be 10xxxxxx - bytes[8] &= 0xBF; - bytes[8] |= 0x80; - - // version must be 0100xxxx - bytes[6] &= 0x4F; - bytes[6] |= 0x40; - - return uuid{std::begin(bytes), std::end(bytes)}; +template <typename UniformRandomNumberGenerator> +class basic_uuid_random_generator { + public: + using engine_type = UniformRandomNumberGenerator; + + explicit basic_uuid_random_generator(engine_type& gen) + : generator(&gen, [](auto) {}) {} + explicit basic_uuid_random_generator(engine_type* gen) + : generator(gen, [](auto) {}) {} + + [[nodiscard]] uuid operator()() { + alignas(uint32_t) uint8_t bytes[16]; + for (int i = 0; i < 16; i += 4) + *reinterpret_cast<uint32_t*>(bytes + i) = distribution(*generator); + + // variant must be 10xxxxxx + bytes[8] &= 0xBF; + bytes[8] |= 0x80; + + // version must be 0100xxxx + bytes[6] &= 0x4F; + bytes[6] |= 0x40; + + return uuid{std::begin(bytes), std::end(bytes)}; + } + + private: + std::uniform_int_distribution<uint32_t> distribution; + std::shared_ptr<UniformRandomNumberGenerator> generator; +}; + +using uuid_random_generator = basic_uuid_random_generator<std::mt19937>; + +class uuid_name_generator { + public: + explicit uuid_name_generator(uuid const& namespace_uuid) noexcept + : nsuuid(namespace_uuid) {} + + template <typename StringType> + [[nodiscard]] uuid operator()(StringType const& name) { + reset(); + process_characters(detail::to_string_view(name)); + return make_uuid(); + } + + private: + void reset() { + hasher.reset(); + std::byte bytes[16]; + auto nsbytes = nsuuid.as_bytes(); + std::copy(std::cbegin(nsbytes), std::cend(nsbytes), bytes); + hasher.process_bytes(bytes, 16); + } + + template <typename CharT, typename Traits> + void process_characters(std::basic_string_view<CharT, Traits> const str) { + for (uint32_t c : str) { + hasher.process_byte(static_cast<uint8_t>(c & 0xFF)); + if constexpr (!std::is_same_v<CharT, char>) { + hasher.process_byte(static_cast<uint8_t>((c >> 8) & 0xFF)); + hasher.process_byte(static_cast<uint8_t>((c >> 16) & 0xFF)); + hasher.process_byte(static_cast<uint8_t>((c >> 24) & 0xFF)); } + } + } - private: - std::uniform_int_distribution<uint32_t> distribution; - std::shared_ptr<UniformRandomNumberGenerator> generator; - }; - - using uuid_random_generator = basic_uuid_random_generator<std::mt19937>; - - class uuid_name_generator - { - public: - explicit uuid_name_generator(uuid const& namespace_uuid) noexcept - : nsuuid(namespace_uuid) - {} - - template <typename StringType> - [[nodiscard]] uuid operator()(StringType const & name) - { - reset(); - process_characters(detail::to_string_view(name)); - return make_uuid(); - } + [[nodiscard]] uuid make_uuid() { + detail::sha1::digest8_t digest; + hasher.get_digest_bytes(digest); - private: - void reset() - { - hasher.reset(); - std::byte bytes[16]; - auto nsbytes = nsuuid.as_bytes(); - std::copy(std::cbegin(nsbytes), std::cend(nsbytes), bytes); - hasher.process_bytes(bytes, 16); - } + // variant must be 0b10xxxxxx + digest[8] &= 0xBF; + digest[8] |= 0x80; - template <typename CharT, typename Traits> - void process_characters(std::basic_string_view<CharT, Traits> const str) - { - for (uint32_t c : str) - { - hasher.process_byte(static_cast<uint8_t>(c & 0xFF)); - if constexpr (!std::is_same_v<CharT, char>) - { - hasher.process_byte(static_cast<uint8_t>((c >> 8) & 0xFF)); - hasher.process_byte(static_cast<uint8_t>((c >> 16) & 0xFF)); - hasher.process_byte(static_cast<uint8_t>((c >> 24) & 0xFF)); - } - } - } + // version must be 0b0101xxxx + digest[6] &= 0x5F; + digest[6] |= 0x50; - [[nodiscard]] uuid make_uuid() - { - detail::sha1::digest8_t digest; - hasher.get_digest_bytes(digest); + return uuid{digest, digest + 16}; + } - // variant must be 0b10xxxxxx - digest[8] &= 0xBF; - digest[8] |= 0x80; + private: + uuid nsuuid; + detail::sha1 hasher; +}; - // version must be 0b0101xxxx - digest[6] &= 0x5F; - digest[6] |= 0x50; +#ifdef UUID_TIME_GENERATOR +// !!! DO NOT USE THIS IN PRODUCTION +// this implementation is unreliable for good uuids +class uuid_time_generator { + using mac_address = std::array<unsigned char, 6>; - return uuid{ digest, digest + 16 }; - } + std::optional<mac_address> device_address; - private: - uuid nsuuid; - detail::sha1 hasher; - }; + [[nodiscard]] bool get_mac_address() { + if (device_address.has_value()) { + return true; + } -#ifdef UUID_TIME_GENERATOR - // !!! DO NOT USE THIS IN PRODUCTION - // this implementation is unreliable for good uuids - class uuid_time_generator - { - using mac_address = std::array<unsigned char, 6>; - - std::optional<mac_address> device_address; - - [[nodiscard]] bool get_mac_address() - { - if (device_address.has_value()) - { - return true; - } - #ifdef _WIN32 - DWORD len = 0; - auto ret = GetAdaptersInfo(nullptr, &len); - if (ret != ERROR_BUFFER_OVERFLOW) return false; - std::vector<unsigned char> buf(len); - auto pips = reinterpret_cast<PIP_ADAPTER_INFO>(&buf.front()); - ret = GetAdaptersInfo(pips, &len); - if (ret != ERROR_SUCCESS) return false; - mac_address addr; - std::copy(pips->Address, pips->Address + 6, std::begin(addr)); - device_address = addr; + DWORD len = 0; + auto ret = GetAdaptersInfo(nullptr, &len); + if (ret != ERROR_BUFFER_OVERFLOW) return false; + std::vector<unsigned char> buf(len); + auto pips = reinterpret_cast<PIP_ADAPTER_INFO>(&buf.front()); + ret = GetAdaptersInfo(pips, &len); + if (ret != ERROR_SUCCESS) return false; + mac_address addr; + std::copy(pips->Address, pips->Address + 6, std::begin(addr)); + device_address = addr; #endif - return device_address.has_value(); - } + return device_address.has_value(); + } - [[nodiscard]] long long get_time_intervals() - { - auto start = std::chrono::system_clock::from_time_t(time_t(-12219292800)); - auto diff = std::chrono::system_clock::now() - start; - auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(diff).count(); - return ns / 100; - } + [[nodiscard]] long long get_time_intervals() { + auto start = std::chrono::system_clock::from_time_t(time_t(-12219292800)); + auto diff = std::chrono::system_clock::now() - start; + auto ns = + std::chrono::duration_cast<std::chrono::nanoseconds>(diff).count(); + return ns / 100; + } - [[nodiscard]] static unsigned short get_clock_sequence() - { - static std::mt19937 clock_gen(std::random_device{}()); - static std::uniform_int_distribution<unsigned short> clock_dis; - static std::atomic_ushort clock_sequence = clock_dis(clock_gen); - return clock_sequence++; - } + [[nodiscard]] static unsigned short get_clock_sequence() { + static std::mt19937 clock_gen(std::random_device{}()); + static std::uniform_int_distribution<unsigned short> clock_dis; + static std::atomic_ushort clock_sequence = clock_dis(clock_gen); + return clock_sequence++; + } - public: - [[nodiscard]] uuid operator()() - { - if (get_mac_address()) - { - std::array<uuids::uuid::value_type, 16> data; + public: + [[nodiscard]] uuid operator()() { + if (get_mac_address()) { + std::array<uuids::uuid::value_type, 16> data; - auto tm = get_time_intervals(); + auto tm = get_time_intervals(); - auto clock_seq = get_clock_sequence(); + auto clock_seq = get_clock_sequence(); - auto ptm = reinterpret_cast<uuids::uuid::value_type*>(&tm); + auto ptm = reinterpret_cast<uuids::uuid::value_type*>(&tm); - memcpy(&data[0], ptm + 4, 4); - memcpy(&data[4], ptm + 2, 2); - memcpy(&data[6], ptm, 2); + memcpy(&data[0], ptm + 4, 4); + memcpy(&data[4], ptm + 2, 2); + memcpy(&data[6], ptm, 2); - memcpy(&data[8], &clock_seq, 2); + memcpy(&data[8], &clock_seq, 2); - // variant must be 0b10xxxxxx - data[8] &= 0xBF; - data[8] |= 0x80; + // variant must be 0b10xxxxxx + data[8] &= 0xBF; + data[8] |= 0x80; - // version must be 0b0001xxxx - data[6] &= 0x1F; - data[6] |= 0x10; + // version must be 0b0001xxxx + data[6] &= 0x1F; + data[6] |= 0x10; - memcpy(&data[10], &device_address.value()[0], 6); + memcpy(&data[10], &device_address.value()[0], 6); - return uuids::uuid{std::cbegin(data), std::cend(data)}; - } + return uuids::uuid{std::cbegin(data), std::cend(data)}; + } - return {}; - } - }; + return {}; + } +}; #endif -} +} // namespace uuids -namespace std -{ - template <> - struct hash<uuids::uuid> - { - using argument_type = uuids::uuid; - using result_type = std::size_t; +namespace std { +template <> +struct hash<uuids::uuid> { + using argument_type = uuids::uuid; + using result_type = std::size_t; - [[nodiscard]] result_type operator()(argument_type const &uuid) const - { + [[nodiscard]] result_type operator()(argument_type const& uuid) const { #ifdef UUID_HASH_STRING_BASED - std::hash<std::string> hasher; - return static_cast<result_type>(hasher(uuids::to_string(uuid))); + std::hash<std::string> hasher; + return static_cast<result_type>(hasher(uuids::to_string(uuid))); #else - uint64_t l = - static_cast<uint64_t>(uuid.data[0]) << 56 | - static_cast<uint64_t>(uuid.data[1]) << 48 | - static_cast<uint64_t>(uuid.data[2]) << 40 | - static_cast<uint64_t>(uuid.data[3]) << 32 | - static_cast<uint64_t>(uuid.data[4]) << 24 | - static_cast<uint64_t>(uuid.data[5]) << 16 | - static_cast<uint64_t>(uuid.data[6]) << 8 | - static_cast<uint64_t>(uuid.data[7]); - uint64_t h = - static_cast<uint64_t>(uuid.data[8]) << 56 | - static_cast<uint64_t>(uuid.data[9]) << 48 | - static_cast<uint64_t>(uuid.data[10]) << 40 | - static_cast<uint64_t>(uuid.data[11]) << 32 | - static_cast<uint64_t>(uuid.data[12]) << 24 | - static_cast<uint64_t>(uuid.data[13]) << 16 | - static_cast<uint64_t>(uuid.data[14]) << 8 | - static_cast<uint64_t>(uuid.data[15]); - - if constexpr (sizeof(result_type) > 4) - { - return result_type(l ^ h); - } - else - { - uint64_t hash64 = l ^ h; - return result_type(uint32_t(hash64 >> 32) ^ uint32_t(hash64)); - } + uint64_t l = static_cast<uint64_t>(uuid.data[0]) << 56 | + static_cast<uint64_t>(uuid.data[1]) << 48 | + static_cast<uint64_t>(uuid.data[2]) << 40 | + static_cast<uint64_t>(uuid.data[3]) << 32 | + static_cast<uint64_t>(uuid.data[4]) << 24 | + static_cast<uint64_t>(uuid.data[5]) << 16 | + static_cast<uint64_t>(uuid.data[6]) << 8 | + static_cast<uint64_t>(uuid.data[7]); + uint64_t h = static_cast<uint64_t>(uuid.data[8]) << 56 | + static_cast<uint64_t>(uuid.data[9]) << 48 | + static_cast<uint64_t>(uuid.data[10]) << 40 | + static_cast<uint64_t>(uuid.data[11]) << 32 | + static_cast<uint64_t>(uuid.data[12]) << 24 | + static_cast<uint64_t>(uuid.data[13]) << 16 | + static_cast<uint64_t>(uuid.data[14]) << 8 | + static_cast<uint64_t>(uuid.data[15]); + + if constexpr (sizeof(result_type) > 4) { + return result_type(l ^ h); + } else { + uint64_t hash64 = l ^ h; + return result_type(uint32_t(hash64 >> 32) ^ uint32_t(hash64)); + } #endif - } - }; -} + } +}; +} // namespace std #endif /* STDUUID_H */
\ No newline at end of file diff --git a/SDK/PowerPC.inc b/SDK/PowerPC.inc index c6dce6d..98e445f 100644 --- a/SDK/PowerPC.inc +++ b/SDK/PowerPC.inc @@ -1,35 +1,5 @@ -# Path: SDK/PowerPC.inc -# Language: Mahrouss PowerPC Assembly support for GNU. -# Build Date: 2024-6-4 - -%def lda li -%def sta stw -%def ldw li - -%def r0 0 -%def r1 1 -%def r2 2 - -%def r3 3 -%def r4 4 -%def r5 5 - -%def r6 6 -%def r7 7 -%def r8 8 - -%def r9 9 -%def r10 10 -%def r11 11 - -%def r12 12 -%def r13 13 -%def r14 14 - -%def r15 15 -%def r16 16 -%def r17 17 - -%def r18 18 -%def r19 19 -%def r20 20 +# Path: SDK/PowerPC.inc # Language: Mahrouss PowerPC Assembly support for GNU. # +Build Date: 2024-6-4 %def lda li %def sta stw %def ldw li %def r0 0 %def r1 1 +%def r2 2 %def r3 3 %def r4 4 %def r5 5 %def r6 6 %def r7 7 %def r8 8 %def r9 9 +%def r10 10 %def r11 11 %def r12 12 %def r13 13 %def r14 14 %def r15 15 %def r16 +16 %def r17 17 %def r18 18 %def r19 19 %def r20 20 diff --git a/Sources/32asm.cc b/Sources/32asm.cc index 12a83eb..e5aa500 100644 --- a/Sources/32asm.cc +++ b/Sources/32asm.cc @@ -26,9 +26,8 @@ #include <filesystem> #include <fstream> #include <iostream> -#include <vector> #include <memory> - +#include <vector> ///////////////////// @@ -50,6 +49,4 @@ ///////////////////////////////////////////////////////////////////////////////////////// -MPCC_MODULE(NewOSAssembler32000) { - return 0; -} +MPCC_MODULE(NewOSAssembler32000) { return 0; } diff --git a/Sources/64asm.cc b/Sources/64asm.cc index 58e02b4..b955ad6 100644 --- a/Sources/64asm.cc +++ b/Sources/64asm.cc @@ -23,12 +23,12 @@ #include <Headers/ParserKit.hpp> #include <Headers/StdKit/AE.hpp> #include <Headers/StdKit/PEF.hpp> +#include <algorithm> #include <filesystem> #include <fstream> #include <iostream> -#include <vector> #include <memory> -#include <algorithm> +#include <vector> ///////////////////// @@ -442,13 +442,13 @@ bool is_valid(const std::string &str) { ///////////////////////////////////////////////////////////////////////////////////////// -std::string CompilerKit::Encoder64x0::CheckLine( - std::string &line, const std::string &file) { +std::string CompilerKit::Encoder64x0::CheckLine(std::string &line, + const std::string &file) { std::string err_str; if (line.empty() || ParserKit::find_word(line, "import") || - ParserKit::find_word(line, "export") || line.find('#') != std::string::npos || - ParserKit::find_word(line, ";")) { + ParserKit::find_word(line, "export") || + line.find('#') != std::string::npos || ParserKit::find_word(line, ";")) { if (line.find('#') != std::string::npos) { line.erase(line.find('#')); } else if (line.find(';') != std::string::npos) { @@ -552,7 +552,7 @@ std::string CompilerKit::Encoder64x0::CheckLine( } bool CompilerKit::Encoder64x0::WriteNumber(const std::size_t &pos, - std::string &jump_label) { + std::string &jump_label) { if (!isdigit(jump_label[pos])) return false; switch (jump_label[pos + 1]) { @@ -659,7 +659,7 @@ bool CompilerKit::Encoder64x0::WriteNumber(const std::size_t &pos, ///////////////////////////////////////////////////////////////////////////////////////// bool CompilerKit::Encoder64x0::WriteLine(std::string &line, - const std::string &file) { + const std::string &file) { if (ParserKit::find_word(line, "export ")) return true; for (auto &opcode64x0 : kOpcodes64x0) { @@ -907,9 +907,7 @@ bool CompilerKit::Encoder64x0::WriteLine(std::string &line, /// don't go any further if: /// load word (ldw) or store word. (stw) - if (name == "ldw" || - name == "stw") - break; + if (name == "ldw" || name == "stw") break; auto mld_reloc_str = std::to_string(cpy_jump_label.size()); mld_reloc_str += kUndefinedSymbol; diff --git a/Sources/64x0-cc.cc b/Sources/64x0-cc.cc index 834217b..cd96c85 100644 --- a/Sources/64x0-cc.cc +++ b/Sources/64x0-cc.cc @@ -8,21 +8,20 @@ */ /// BUGS: ? -/// TODO: +/// TODO: #include <Headers/AsmKit/CPU/64x0.hpp> #include <Headers/ParserKit.hpp> +#include <Headers/UUID.hpp> #include <cstdio> +#include <filesystem> #include <fstream> #include <iostream> +#include <memory> +#include <random> #include <string> #include <utility> #include <vector> -#include <memory> -#include <filesystem> - -#include <random> -#include <Headers/UUID.hpp> #define kOk 0 @@ -103,7 +102,7 @@ void print_error(std::string reason, std::string file) noexcept { if (kState.fLastFile != file) { std::cout << kRed << "[ cc ] " << kWhite << ((file == "cc") ? "internal compiler error " - : ("in file, " + file)) + : ("in file, " + file)) << kBlank << std::endl; std::cout << kRed << "[ cc ] " << kWhite << reason << kBlank << std::endl; @@ -214,7 +213,7 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { // setup generator. std::random_device rd; - auto seed_data = std::array<int, std::mt19937::state_size> {}; + auto seed_data = std::array<int, std::mt19937::state_size>{}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::mt19937 generator(seq); @@ -292,8 +291,8 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { for (size_t return_index = text_index; return_index < textBuffer.size(); ++return_index) { if (textBuffer[return_index] != return_keyword[index]) { - for (size_t value_index = return_index; value_index < textBuffer.size(); - ++value_index) { + for (size_t value_index = return_index; + value_index < textBuffer.size(); ++value_index) { if (textBuffer[value_index] == ';') break; value += textBuffer[value_index]; @@ -338,7 +337,7 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { } } - if (textBuffer[text_index] == 'i' && textBuffer[text_index+1] == 'f') { + if (textBuffer[text_index] == 'i' && textBuffer[text_index + 1] == 'f') { auto expr = textBuffer.substr(text_index + 2); textBuffer.erase(text_index, 2); @@ -346,17 +345,19 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { expr.erase(expr.find("{")); } - if (expr.find("(") != std::string::npos) - expr.erase(expr.find("(")); - - if (expr.find(")") != std::string::npos) - expr.erase(expr.find(")")); - + if (expr.find("(") != std::string::npos) expr.erase(expr.find("(")); + + if (expr.find(")") != std::string::npos) expr.erase(expr.find(")")); + kIfFunction = "__MPCC_IF_PROC_"; kIfFunction += std::to_string(time_off._Raw); syntaxLeaf.fUserValue = "\tlda r12, import "; - syntaxLeaf.fUserValue += kIfFunction + "\n\t#r12 = Code to jump on, r11 right cond, r10 left cond.\n\tbeq r10, r11, r12\ndword export .code64 " + kIfFunction + "\n"; + syntaxLeaf.fUserValue += + kIfFunction + + "\n\t#r12 = Code to jump on, r11 right cond, r10 left cond.\n\tbeq " + "r10, r11, r12\ndword export .code64 " + + kIfFunction + "\n"; kState.fSyntaxTree->fLeafList.push_back(syntaxLeaf); kIfFound = true; @@ -374,7 +375,8 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { if (textBuffer.find("typedef ") != std::string::npos) continue; if (textBuffer[text_index] == '=' && kInStruct) { - detail::print_error("assignement of value in struct " + textBuffer, file); + detail::print_error("assignement of value in struct " + textBuffer, + file); continue; } @@ -407,15 +409,16 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { continue; } - if (textBuffer[text_index + 1] == '=' || textBuffer[text_index - 1] == '!' || - textBuffer[text_index - 1] == '<' || textBuffer[text_index - 1] == '>') { + if (textBuffer[text_index + 1] == '=' || + textBuffer[text_index - 1] == '!' || + textBuffer[text_index - 1] == '<' || + textBuffer[text_index - 1] == '>') { continue; } std::string substr; - if (textBuffer.find('=') != std::string::npos && kInBraces && - !kIfFound) { + if (textBuffer.find('=') != std::string::npos && kInBraces && !kIfFound) { if (textBuffer.find("*") != std::string::npos) { if (textBuffer.find("=") > textBuffer.find("*")) substr += "\tlda "; @@ -448,16 +451,19 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { } } - if (textBuffer[text_index_2] == '{' || textBuffer[text_index_2] == '}') continue; + if (textBuffer[text_index_2] == '{' || textBuffer[text_index_2] == '}') + continue; if (textBuffer[text_index_2] == ';') { break; } - if (textBuffer[text_index_2] == ' ' || textBuffer[text_index_2] == '\t') { + if (textBuffer[text_index_2] == ' ' || + textBuffer[text_index_2] == '\t') { if (first_encountered != 2) { if (textBuffer[text_index] != '=' && - substr.find("export .data64") == std::string::npos && !kInStruct) + substr.find("export .data64") == std::string::npos && + !kInStruct) substr += "export .data64 "; } @@ -468,8 +474,8 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { if (textBuffer[text_index_2] == '=') { if (!kInBraces) { - substr.replace(substr.find("export .data64"), strlen("export .data64"), - "export .page_zero "); + substr.replace(substr.find("export .data64"), + strlen("export .data64"), "export .page_zero "); } substr += ","; @@ -533,7 +539,8 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { bool type_crossed = false; - for (size_t idx = textBuffer.find('(') + 1; idx < textBuffer.size(); ++idx) { + for (size_t idx = textBuffer.find('(') + 1; idx < textBuffer.size(); + ++idx) { if (textBuffer[idx] == ',') continue; if (textBuffer[idx] == ' ') continue; @@ -637,8 +644,7 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { kBracesCount = 0; } - if (kIfFound) - kIfFound = false; + if (kIfFound) kIfFound = false; if (kInStruct) kInStruct = false; @@ -1199,9 +1205,12 @@ class AssemblyMountpointCLang final : public CompilerKit::AssemblyInterface { } if (ParserKit::find_word(leaf.fUserValue, needle)) { - if (leaf.fUserValue.find("import " + needle) != std::string::npos) { + if (leaf.fUserValue.find("import " + needle) != + std::string::npos) { std::string range = "import " + needle; - leaf.fUserValue.replace(leaf.fUserValue.find("import " + needle), range.size(), needle); + leaf.fUserValue.replace( + leaf.fUserValue.find("import " + needle), range.size(), + needle); } if (leaf.fUserValue.find("ldw r6") != std::string::npos) { @@ -1248,11 +1257,10 @@ class AssemblyMountpointCLang final : public CompilerKit::AssemblyInterface { #include <Version.hxx> #define kPrintF printf -#define kSplashCxx() kPrintF(kWhite "cc, %s, (c) Mahrouss Logic\n", kDistVersion) +#define kSplashCxx() \ + kPrintF(kWhite "cc, %s, (c) Mahrouss Logic\n", kDistVersion) -static void cc_print_help() { - kSplashCxx(); -} +static void cc_print_help() { kSplashCxx(); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -1291,8 +1299,7 @@ MPCC_MODULE(NewOSCompilerCLang64x0) { continue; } - if (strcmp(argv[index], "-h") == 0 || - strcmp(argv[index], "-help") == 0) { + if (strcmp(argv[index], "-h") == 0 || strcmp(argv[index], "-help") == 0) { cc_print_help(); return kOk; diff --git a/Sources/Detail/Readme.md b/Sources/Detail/Readme.md index aba110f..91d496a 100644 --- a/Sources/Detail/Readme.md +++ b/Sources/Detail/Readme.md @@ -1,2 +1 @@ # Compiler utilities. - diff --git a/Sources/Detail/asmutils.h b/Sources/Detail/asmutils.h index 00d4daa..6a7d055 100644 --- a/Sources/Detail/asmutils.h +++ b/Sources/Detail/asmutils.h @@ -3,9 +3,9 @@ using namespace CompilerKit; /// @brief Get Number from line. -/// @param line -/// @param name -/// @return +/// @param line +/// @param name +/// @return static NumberCast32 GetNumber32(std::string line, std::string name) { auto pos = line.find(name) + name.size(); diff --git a/Sources/bpp.cc b/Sources/bpp.cc index dcebae7..1bc235c 100644 --- a/Sources/bpp.cc +++ b/Sources/bpp.cc @@ -11,11 +11,11 @@ #include <Headers/ParserKit.hpp> #include <Headers/StdKit/ErrorID.hpp> +#include <algorithm> +#include <filesystem> #include <fstream> #include <iostream> -#include <filesystem> #include <vector> -#include <algorithm> #define kMacroPrefix '%' @@ -72,8 +72,8 @@ static std::vector<std::string> kIncludes; static std::string kWorkingDir; static std::vector<std::string> kKeywords = { - "include", "if", "pragma", "def", "elif", - "ifdef", "ifndef", "else", "warning", "error"}; + "include", "if", "pragma", "def", "elif", + "ifdef", "ifndef", "else", "warning", "error"}; #define kKeywordCxxCnt kKeywords.size() @@ -281,7 +281,7 @@ void bpp_parse_file(std::ifstream &hdr_file, std::ofstream &pp_out) { if (ParserKit::find_word(hdr_line, macro.fName) && hdr_line.find("%def") == std::string::npos) { auto value = macro.fValue; - + hdr_line.replace(hdr_line.find(macro.fName), macro.fName.size(), value); } @@ -839,8 +839,8 @@ MPCC_MODULE(NewOSPreprocessor) { kWorkingDir = inc; } - if (strcmp(argv[index], "-def") == 0 && - argv[index + 1] != nullptr && argv[index + 2] != nullptr) { + if (strcmp(argv[index], "-def") == 0 && argv[index + 1] != nullptr && + argv[index + 2] != nullptr) { std::string macro_key = argv[index + 1]; std::string macro_value; diff --git a/Sources/ccplus.cc b/Sources/ccplus.cc index 9075636..ee73093 100644 --- a/Sources/ccplus.cc +++ b/Sources/ccplus.cc @@ -391,8 +391,7 @@ MPCC_MODULE(CompilerCPlusPlus) { continue; } - if (strcmp(argv[index], "-h") == 0 || - strcmp(argv[index], "-help") == 0) { + if (strcmp(argv[index], "-h") == 0 || strcmp(argv[index], "-help") == 0) { cxx_print_help(); return kOk; diff --git a/Sources/i64asm.cc b/Sources/i64asm.cc index b85b6b3..f7fa5e5 100644 --- a/Sources/i64asm.cc +++ b/Sources/i64asm.cc @@ -30,13 +30,13 @@ #include <Headers/ParserKit.hpp> #include <Headers/StdKit/AE.hpp> #include <Headers/StdKit/PEF.hpp> +#include <algorithm> #include <cstdlib> #include <filesystem> #include <fstream> #include <iostream> #include <memory> #include <vector> -#include <algorithm> ///////////////////// @@ -125,8 +125,9 @@ MPCC_MODULE(NewOSAssemblerAMD64) { "jno", "jnp", "jns", "jnz", "jo", "jp", "jpe", "jpo", "js", "jz"}; for (i64_hword_t i = 0; i < kJumpLimit; i++) { - CpuOpcodeAMD64 code{.fName = opcodes_jump[i], - .fOpcode = static_cast<i64_hword_t>(kAsmJumpOpcode + i)}; + CpuOpcodeAMD64 code{ + .fName = opcodes_jump[i], + .fOpcode = static_cast<i64_hword_t>(kAsmJumpOpcode + i)}; kOpcodesAMD64.push_back(code); } @@ -389,7 +390,7 @@ static bool asm_read_attributes(std::string &line) { // this is a special case for the start stub. // we want this so that ld can find it. - + if (name == kPefStart) { kCurrentRecord.fKind = CompilerKit::kPefCode; } @@ -411,7 +412,8 @@ static bool asm_read_attributes(std::string &line) { return true; } // export is a special keyword used by i64asm to tell the AE output stage to - // mark this section as a header. it currently supports .code64, .data64 and .zero64. + // mark this section as a header. it currently supports .code64, .data64 and + // .zero64. else if (ParserKit::find_word(line, "export")) { if (kOutputAsBinary) { detail::print_error("Invalid directive in flat binary mode.", "i64asm"); @@ -1106,12 +1108,10 @@ bool CompilerKit::EncoderAMD64::WriteLine(std::string &line, uint8_t modrm = 0x0; - for (size_t i = 0; i < REGISTER_LIST.size(); ++i) - { - if (REGISTER_LIST[i].fModRM != currentRegList[0].fModRM) - modrm += 16; + for (size_t i = 0; i < REGISTER_LIST.size(); ++i) { + if (REGISTER_LIST[i].fModRM != currentRegList[0].fModRM) modrm += 16; } - + modrm += currentRegList[1].fModRM; kAppBytes.emplace_back(modrm); @@ -1162,8 +1162,7 @@ bool CompilerKit::EncoderAMD64::WriteLine(std::string &line, continue; } else { if (kVerbose) { - kStdOut << "i64asm: Assembler origin set: " << kOrigin - << std::endl; + kStdOut << "i64asm: origin set: " << kOrigin << std::endl; } break; @@ -1172,9 +1171,17 @@ bool CompilerKit::EncoderAMD64::WriteLine(std::string &line, } } } - - if (line.find(".number") != std::string::npos) { - this->WriteNumber32(line.find(".number") + strlen(".number") + 1, line); + /// write a dword + else if (line.find(".dword") != std::string::npos) { + this->WriteNumber32(line.find(".dword") + strlen(".dword") + 1, line); + } + /// write a long + else if (line.find(".long") != std::string::npos) { + this->WriteNumber(line.find(".long") + strlen(".long") + 1, line); + } + /// write a 16-bit number + else if (line.find(".word") != std::string::npos) { + this->WriteNumber16(line.find(".word") + strlen(".word") + 1, line); } return true; diff --git a/Sources/link.cc b/Sources/link.cc index 36e9132..8c88411 100644 --- a/Sources/link.cc +++ b/Sources/link.cc @@ -24,12 +24,10 @@ //! Portable Executable Format #include <Headers/StdKit/PEF.hpp> - -#include <vector> +#include <Headers/UUID.hpp> #include <filesystem> - #include <random> -#include <Headers/UUID.hpp> +#include <vector> //! Advanced Executable Object Format #include <Headers/StdKit/AE.hpp> @@ -118,7 +116,7 @@ MPCC_MODULE(NewOSLinker) { kArch = CompilerKit::kPefArch32000; continue; - } else if (StringCompare(argv[i], "-ppc64") == 0) { + } else if (StringCompare(argv[i], "-ppc64") == 0) { kArch = CompilerKit::kPefArchPowerPC; continue; @@ -352,11 +350,9 @@ MPCC_MODULE(NewOSLinker) { // check if this symbol needs to be resolved. if (std::string(commandHdr.Name).find(kLdDefineSymbol) != std::string::npos && - std::string(commandHdr.Name).find(kLdDynamicSym) == - std::string::npos) { + std::string(commandHdr.Name).find(kLdDynamicSym) == std::string::npos) { if (kVerbose) - kStdOut << "link: found undefined symbol: " << commandHdr.Name - << "\n"; + kStdOut << "link: found undefined symbol: " << commandHdr.Name << "\n"; if (auto it = std::find(not_found.begin(), not_found.end(), std::string(commandHdr.Name)); @@ -420,11 +416,12 @@ MPCC_MODULE(NewOSLinker) { if (!kStartFound && is_executable) { if (kVerbose) - kStdOut << "link: undefined entrypoint: __start, you may have forget to link " - "against your compiler's runtime library.\n"; + kStdOut + << "link: undefined entrypoint: __start, you may have forget to link " + "against your compiler's runtime library.\n"; - kStdOut << "link: undefined entrypoint " << kPefStart << " for executable: " - << kOutput << "\n"; + kStdOut << "link: undefined entrypoint " << kPefStart + << " for executable: " << kOutput << "\n"; } // step 4: write some pef commands. @@ -482,7 +479,7 @@ MPCC_MODULE(NewOSLinker) { std::random_device rd; - auto seedData = std::array<int, std::mt19937::state_size> {}; + auto seedData = std::array<int, std::mt19937::state_size>{}; std::generate(std::begin(seedData), std::end(seedData), std::ref(rd)); std::seed_seq seq(std::begin(seedData), std::end(seedData)); std::mt19937 generator(seq); @@ -492,7 +489,8 @@ MPCC_MODULE(NewOSLinker) { auto uuidStr = uuids::to_string(id); memcpy(uuidHeader.Name, "Container:GUID:4:", strlen("Container:GUID:4:")); - memcpy(uuidHeader.Name + strlen("Container:GUID:4:"), uuidStr.c_str(), uuidStr.size()); + memcpy(uuidHeader.Name + strlen("Container:GUID:4:"), uuidStr.c_str(), + uuidStr.size()); uuidHeader.Size = 16; uuidHeader.Offset = outputFc.tellp(); @@ -508,11 +506,12 @@ MPCC_MODULE(NewOSLinker) { // Finally write down the command headers. // And check for any duplications - for (size_t commandHeaderIndex = 0UL; commandHeaderIndex < commandHdrsList.size(); ++commandHeaderIndex) { - if (std::string(commandHdrsList[commandHeaderIndex].Name).find(kLdDefineSymbol) != - std::string::npos && - std::string(commandHdrsList[commandHeaderIndex].Name).find(kLdDynamicSym) == - std::string::npos) { + for (size_t commandHeaderIndex = 0UL; + commandHeaderIndex < commandHdrsList.size(); ++commandHeaderIndex) { + if (std::string(commandHdrsList[commandHeaderIndex].Name) + .find(kLdDefineSymbol) != std::string::npos && + std::string(commandHdrsList[commandHeaderIndex].Name) + .find(kLdDynamicSym) == std::string::npos) { // ignore :UndefinedSymbol: headers, they do not contain code. continue; } @@ -525,14 +524,15 @@ MPCC_MODULE(NewOSLinker) { outputFc << commandHdrsList[commandHeaderIndex]; - for (size_t subCommandHeaderIndex = 0UL; subCommandHeaderIndex < commandHdrsList.size(); + for (size_t subCommandHeaderIndex = 0UL; + subCommandHeaderIndex < commandHdrsList.size(); ++subCommandHeaderIndex) { if (subCommandHeaderIndex == commandHeaderIndex) continue; if (std::string(commandHdrsList[subCommandHeaderIndex].Name) .find(kLdDefineSymbol) != std::string::npos && - std::string(commandHdrsList[subCommandHeaderIndex].Name).find(kLdDynamicSym) == - std::string::npos) { + std::string(commandHdrsList[subCommandHeaderIndex].Name) + .find(kLdDynamicSym) == std::string::npos) { if (kVerbose) { kStdOut << "link: ignore :UndefinedSymbol: command header...\n"; } @@ -543,7 +543,8 @@ MPCC_MODULE(NewOSLinker) { auto &commandHdr = commandHdrsList[subCommandHeaderIndex]; - if (commandHdr.Name == std::string(commandHdrsList[commandHeaderIndex].Name)) { + if (commandHdr.Name == + std::string(commandHdrsList[commandHeaderIndex].Name)) { if (std::find(duplSymbols.cbegin(), duplSymbols.cend(), commandHdr.Name) == duplSymbols.cend()) { duplSymbols.emplace_back(commandHdr.Name); diff --git a/Sources/ppc-cc.cc b/Sources/ppc-cc.cc index 27fc4ab..dc40383 100644 --- a/Sources/ppc-cc.cc +++ b/Sources/ppc-cc.cc @@ -8,21 +8,20 @@ */ /// BUGS: 0 -/// TODO: +/// TODO: #include <Headers/AsmKit/CPU/ppc.hpp> #include <Headers/ParserKit.hpp> +#include <Headers/UUID.hpp> #include <cstdio> +#include <filesystem> #include <fstream> #include <iostream> +#include <memory> +#include <random> #include <string> #include <utility> #include <vector> -#include <memory> -#include <filesystem> - -#include <random> -#include <Headers/UUID.hpp> #define kOk 0 @@ -103,7 +102,7 @@ void print_error(std::string reason, std::string file) noexcept { if (kState.fLastFile != file) { std::cout << kRed << "[ cc ] " << kWhite << ((file == "cc") ? "internal compiler error " - : ("in file, " + file)) + : ("in file, " + file)) << kBlank << std::endl; std::cout << kRed << "[ cc ] " << kWhite << reason << kBlank << std::endl; @@ -214,7 +213,7 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { // setup generator. std::random_device rd; - auto seed_data = std::array<int, std::mt19937::state_size> {}; + auto seed_data = std::array<int, std::mt19937::state_size>{}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::mt19937 generator(seq); @@ -292,8 +291,8 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { for (size_t return_index = text_index; return_index < textBuffer.size(); ++return_index) { if (textBuffer[return_index] != return_keyword[index]) { - for (size_t value_index = return_index; value_index < textBuffer.size(); - ++value_index) { + for (size_t value_index = return_index; + value_index < textBuffer.size(); ++value_index) { if (textBuffer[value_index] == ';') break; value += textBuffer[value_index]; @@ -338,7 +337,7 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { } } - if (textBuffer[text_index] == 'i' && textBuffer[text_index+1] == 'f') { + if (textBuffer[text_index] == 'i' && textBuffer[text_index + 1] == 'f') { auto expr = textBuffer.substr(text_index + 2); textBuffer.erase(text_index, 2); @@ -346,17 +345,19 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { expr.erase(expr.find("{")); } - if (expr.find("(") != std::string::npos) - expr.erase(expr.find("(")); - - if (expr.find(")") != std::string::npos) - expr.erase(expr.find(")")); - + if (expr.find("(") != std::string::npos) expr.erase(expr.find("(")); + + if (expr.find(")") != std::string::npos) expr.erase(expr.find(")")); + kIfFunction = "__MPCC_IF_PROC_"; kIfFunction += std::to_string(time_off._Raw); syntaxLeaf.fUserValue = "\tlda r12, import "; - syntaxLeaf.fUserValue += kIfFunction + "\n\t#r12 = Code to jump on, r11 right cond, r10 left cond.\n\tbeq r10, r11, r12\ndword export .code64 " + kIfFunction + "\n"; + syntaxLeaf.fUserValue += + kIfFunction + + "\n\t#r12 = Code to jump on, r11 right cond, r10 left cond.\n\tbeq " + "r10, r11, r12\ndword export .code64 " + + kIfFunction + "\n"; kState.fSyntaxTree->fLeafList.push_back(syntaxLeaf); kIfFound = true; @@ -374,7 +375,8 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { if (textBuffer.find("typedef ") != std::string::npos) continue; if (textBuffer[text_index] == '=' && kInStruct) { - detail::print_error("assignement of value in struct " + textBuffer, file); + detail::print_error("assignement of value in struct " + textBuffer, + file); continue; } @@ -407,15 +409,16 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { continue; } - if (textBuffer[text_index + 1] == '=' || textBuffer[text_index - 1] == '!' || - textBuffer[text_index - 1] == '<' || textBuffer[text_index - 1] == '>') { + if (textBuffer[text_index + 1] == '=' || + textBuffer[text_index - 1] == '!' || + textBuffer[text_index - 1] == '<' || + textBuffer[text_index - 1] == '>') { continue; } std::string substr; - if (textBuffer.find('=') != std::string::npos && kInBraces && - !kIfFound) { + if (textBuffer.find('=') != std::string::npos && kInBraces && !kIfFound) { if (textBuffer.find("*") != std::string::npos) { if (textBuffer.find("=") > textBuffer.find("*")) substr += "\tlda "; @@ -448,16 +451,19 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { } } - if (textBuffer[text_index_2] == '{' || textBuffer[text_index_2] == '}') continue; + if (textBuffer[text_index_2] == '{' || textBuffer[text_index_2] == '}') + continue; if (textBuffer[text_index_2] == ';') { break; } - if (textBuffer[text_index_2] == ' ' || textBuffer[text_index_2] == '\t') { + if (textBuffer[text_index_2] == ' ' || + textBuffer[text_index_2] == '\t') { if (first_encountered != 2) { if (textBuffer[text_index] != '=' && - substr.find("export .data64") == std::string::npos && !kInStruct) + substr.find("export .data64") == std::string::npos && + !kInStruct) substr += "export .data64 "; } @@ -468,8 +474,8 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { if (textBuffer[text_index_2] == '=') { if (!kInBraces) { - substr.replace(substr.find("export .data64"), strlen("export .data64"), - "export .page_zero "); + substr.replace(substr.find("export .data64"), + strlen("export .data64"), "export .page_zero "); } substr += ","; @@ -533,7 +539,8 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { bool type_crossed = false; - for (size_t idx = textBuffer.find('(') + 1; idx < textBuffer.size(); ++idx) { + for (size_t idx = textBuffer.find('(') + 1; idx < textBuffer.size(); + ++idx) { if (textBuffer[idx] == ',') continue; if (textBuffer[idx] == ' ') continue; @@ -637,8 +644,7 @@ bool CompilerBackendCLang::Compile(const std::string &text, const char *file) { kBracesCount = 0; } - if (kIfFound) - kIfFound = false; + if (kIfFound) kIfFound = false; if (kInStruct) kInStruct = false; @@ -1155,8 +1161,7 @@ class AssemblyMountpointCLang final : public CompilerKit::AssemblyInterface { if (kAcceptableErrors > 0) return -1; - std::vector<std::string> keywords = {"ld", "stw", - "add", "sub", "or"}; + std::vector<std::string> keywords = {"ld", "stw", "add", "sub", "or"}; /// /// Replace, optimize, fix assembly output. @@ -1199,9 +1204,12 @@ class AssemblyMountpointCLang final : public CompilerKit::AssemblyInterface { } if (ParserKit::find_word(leaf.fUserValue, needle)) { - if (leaf.fUserValue.find("import " + needle) != std::string::npos) { + if (leaf.fUserValue.find("import " + needle) != + std::string::npos) { std::string range = "import " + needle; - leaf.fUserValue.replace(leaf.fUserValue.find("import " + needle), range.size(), needle); + leaf.fUserValue.replace( + leaf.fUserValue.find("import " + needle), range.size(), + needle); } if (leaf.fUserValue.find("ldw r6") != std::string::npos) { @@ -1248,11 +1256,10 @@ class AssemblyMountpointCLang final : public CompilerKit::AssemblyInterface { #include <Version.hxx> #define kPrintF printf -#define kSplashCxx() kPrintF(kWhite "cc, %s, (c) Mahrouss Logic\n", kDistVersion) +#define kSplashCxx() \ + kPrintF(kWhite "cc, %s, (c) Mahrouss Logic\n", kDistVersion) -static void cc_print_help() { - kSplashCxx(); -} +static void cc_print_help() { kSplashCxx(); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -1291,8 +1298,7 @@ MPCC_MODULE(NewOSCompilerCLangPowerPC) { continue; } - if (strcmp(argv[index], "-h") == 0 || - strcmp(argv[index], "-help") == 0) { + if (strcmp(argv[index], "-h") == 0 || strcmp(argv[index], "-help") == 0) { cc_print_help(); return kOk; diff --git a/Sources/ppcasm.cc b/Sources/ppcasm.cc index 69627bb..b010cf5 100644 --- a/Sources/ppcasm.cc +++ b/Sources/ppcasm.cc @@ -23,12 +23,12 @@ #include <Headers/ParserKit.hpp> #include <Headers/StdKit/AE.hpp> #include <Headers/StdKit/PEF.hpp> +#include <algorithm> #include <filesystem> #include <fstream> #include <iostream> #include <memory> #include <vector> -#include <algorithm> ///////////////////// @@ -664,11 +664,11 @@ bool CompilerKit::EncoderPowerPC::WriteNumber(const std::size_t &pos, bool CompilerKit::EncoderPowerPC::WriteLine(std::string &line, const std::string &file) { if (ParserKit::find_word(line, "export")) return true; + if (!detail::algorithm::is_valid(line)) return true; for (auto &opcodePPC : kOpcodesPowerPC) { // strict check here - if (ParserKit::find_word(line, opcodePPC.name) && - detail::algorithm::is_valid(line)) { + if (ParserKit::find_word(line, opcodePPC.name)) { std::string name(opcodePPC.name); std::string jump_label, cpy_jump_label; @@ -819,13 +819,13 @@ bool CompilerKit::EncoderPowerPC::WriteLine(std::string &line, /// FIXME: Prompt correct opcode in little endian. if (opcodeName == "addi") { - if (found_some_count == 2 || found_some_count == 0) kBytes.emplace_back(reg_index); - else if (found_some_count == 1) kBytes.emplace_back(0x00); + else if (found_some_count == 1) + kBytes.emplace_back(0x00); ++found_some_count; - + if (found_some_count > 3) { detail::print_error("Too much registers. -> " + line, file); throw std::runtime_error("too_much_regs"); @@ -912,13 +912,8 @@ bool CompilerKit::EncoderPowerPC::WriteLine(std::string &line, } kOrigin += 0x04; - } else { - /// check if we got a valid sequence of characters. - if (!isalnum(line[0])) { - detail::print_error("syntax error", file); - throw std::runtime_error("syntax_error"); - } - } + break; + } } return true; |
