aboutsummaryrefslogtreecommitdiff
path: root/zenserver/frontend/frontend.cpp
diff options
context:
space:
mode:
authorMartin Ridgers <[email protected]>2022-03-02 09:19:25 +0100
committerMartin Ridgers <[email protected]>2022-03-15 13:45:20 +0100
commit6a955b38cfcc3a566b332f1d1fcc630f7a2f641f (patch)
treed87930177b2389fbe547cba2d7752c6a1582a734 /zenserver/frontend/frontend.cpp
parentAdded some new mime types; javascript, css, png and ico (diff)
downloadzen-6a955b38cfcc3a566b332f1d1fcc630f7a2f641f.tar.xz
zen-6a955b38cfcc3a566b332f1d1fcc630f7a2f641f.zip
Function to find .zip appended to a IoBuffer-wrapped executable file
Diffstat (limited to 'zenserver/frontend/frontend.cpp')
-rw-r--r--zenserver/frontend/frontend.cpp97
1 files changed, 97 insertions, 0 deletions
diff --git a/zenserver/frontend/frontend.cpp b/zenserver/frontend/frontend.cpp
index 4bd3ec90a..0611f068c 100644
--- a/zenserver/frontend/frontend.cpp
+++ b/zenserver/frontend/frontend.cpp
@@ -5,8 +5,105 @@
#include <zencore/filesystem.h>
#include <zencore/string.h>
+ZEN_THIRD_PARTY_INCLUDES_START
+#if ZEN_PLATFORM_WINDOWS
+# include <Windows.h>
+#endif
+ZEN_THIRD_PARTY_INCLUDES_END
+
namespace zen {
+//////////////////////////////////////////////////////////////////////////
+static IoBuffer FindZipFsInBinary(const IoBuffer& BinBuffer)
+{
+ if (BinBuffer.GetSize() < 4)
+ {
+ return {};
+ }
+
+ uintptr_t Cursor = uintptr_t(BinBuffer.GetData());
+ size_t BinSize = 0;
+
+ uint32_t Magic = *(uint32_t*)(BinBuffer.GetData());
+#if ZEN_PLATFORM_LINUX
+ if (Magic == 0x464c457f)
+ {
+ struct Elf64Header
+ {
+ char Ident[16];
+ uint16_t Type;
+ uint16_t Machine;
+ uint32_t Version;
+ uint64_t Entry;
+ uint64_t ProgHeaderOffset;
+ uint64_t SectionHeaderOffset;
+ uint32_t Flags;
+ uint16_t EhSize;
+ uint16_t ProgHeaderEntrySize;
+ uint16_t ProgHeaderCount;
+ uint16_t SectionHeaderEntrySize;
+ uint16_t SectionHeaderCount;
+ uint16_t SectionStringIndex;
+ };
+
+ struct SectionHeader
+ {
+ uint32_t NameIndex;
+ uint32_t Type;
+ uint64_t Flags;
+ uint64_t Address;
+ uint64_t Offset;
+ uint64_t Size;
+ uint64_t _Other[3];
+ };
+
+ const auto* Elf = (Elf64Header*)Cursor;
+ if (Elf->Ident[4] != 0x02) // Elf64
+ {
+ return {};
+ }
+
+ const auto* Section = (SectionHeader*)(Cursor + Elf->SectionHeaderOffset);
+
+ /*
+ size_t BinSize = 0;
+ for (int i = 0, n = Elf->SectionHeaderCount; i < n; ++i, ++Section)
+ {
+ uint32_t SectionEnd = Section->Offset + Section->Size;
+ BinSize = (SectionEnd > BinSize) ? SectionEnd : BinSize;
+ }
+ */
+
+ // What if the section headers aren't the last thing in the fiile though?
+ BinSize = Elf->SectionHeaderEntrySize;
+ BinSize *= Elf->SectionHeaderCount;
+ BinSize += Elf->SectionHeaderOffset;
+ }
+#elif ZEN_PLATFORM_WINDOWS
+ if ((Magic & 0xffff) == 0x5a4d)
+ {
+ const auto* Dos = (IMAGE_DOS_HEADER*)Cursor;
+ const auto* Nt = (IMAGE_NT_HEADERS64*)(Cursor + Dos->e_lfanew);
+ const auto* Section = (IMAGE_SECTION_HEADER*)(uintptr_t(&Nt->OptionalHeader) + Nt->FileHeader.SizeOfOptionalHeader);
+
+ for (int i = 0, n = Nt->FileHeader.NumberOfSections; i < n; ++i, ++Section)
+ {
+ uint32_t SectionEnd = Section->PointerToRawData + Section->SizeOfRawData;
+ BinSize = (SectionEnd > BinSize) ? SectionEnd : BinSize;
+ }
+ }
+#elif ZEN_PLATFORM_MAC
+# error "needs to be implemented!"
+#endif // win/linux/mac
+
+ if (!BinSize || BinSize > BinBuffer.GetSize())
+ {
+ return {};
+ }
+
+ return IoBuffer(BinBuffer, BinSize);
+}
+
namespace html {
constexpr std::string_view Index = R"(