Skip to content

Commit

Permalink
Merge pull request #281 from gdt050579/192-eml-email-electronic-mail-…
Browse files Browse the repository at this point in the history
…format-plugin

192 eml email electronic mail format plugin
  • Loading branch information
rzaharia authored Apr 12, 2024
2 parents 5c2aed8 + 873b4ef commit 1d07ad6
Show file tree
Hide file tree
Showing 16 changed files with 865 additions and 5 deletions.
11 changes: 6 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,18 @@ if(NOT DEFINED CMAKE_TESTING_ENABLED)
add_subdirectory(Types/ZIP)
add_subdirectory(Types/SQLite)
add_subdirectory(Types/JCLASS)

add_subdirectory(Types/EML)

# Generic plugins supported by GView
add_subdirectory(GenericPlugins/CharacterTable)
add_subdirectory(GenericPlugins/Hashes)
add_subdirectory(GenericPlugins/SyncCompare)
add_subdirectory(GenericPlugins/DropperStrings)

add_subdirectory(GenericPlugins/Unpackers)

if (APPLE)
set_property(TARGET "${PROJECT_NAME}" PROPERTY INSTALL_RPATH "@loader_path")
set_property(TARGET "${PROJECT_NAME}" PROPERTY INSTALL_RPATH "@loader_path")
elseif (UNIX)
set_property(TARGET "${PROJECT_NAME}" PROPERTY INSTALL_RPATH "$ORIGIN")
set_property(TARGET "${PROJECT_NAME}" PROPERTY INSTALL_RPATH "$ORIGIN")
endif()
endif()
endif()
6 changes: 6 additions & 0 deletions GViewCore/include/GView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,12 @@ namespace App
uint32 CORE_EXPORT GetTypePluginsCount();

}; // namespace App

namespace Unpack::Base64
{
CORE_EXPORT void Encode(BufferView view, Buffer& output);
CORE_EXPORT bool Decode(BufferView view, Buffer& output);
}
}; // namespace GView

ADD_FLAG_OPERATORS(GView::View::LexicalViewer::StringFormat, AppCUI::uint32);
Expand Down
1 change: 1 addition & 0 deletions GViewCore/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_subdirectory(ZLIB)
add_subdirectory(Dissasembly)
add_subdirectory(ZIP)
add_subdirectory(SQLite3)
add_subdirectory(Unpack)

if(NOT DEFINED CMAKE_TESTING_ENABLED)
target_sources(GViewCore PRIVATE main.cpp)
Expand Down
95 changes: 95 additions & 0 deletions GViewCore/src/Unpack/Base64.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "Internal.hpp"

constexpr char BASE64_ENCODE_TABLE[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };

constexpr char BASE64_DECODE_TABLE[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };

namespace GView::Unpack::Base64
{

void Encode(BufferView view, Buffer& output)
{
uint32 sequence = 0;
uint32 sequenceIndex = 0;

for (uint32 i = 0; i < view.GetLength(); ++i) {
char decoded = view[i];

sequence |= decoded << ((3 - sequenceIndex) * 8);
sequenceIndex++;

if (sequenceIndex % 3 == 0) {
// get 4 encoded components out of this one
// 0x3f -> 0b00111111
char buffer[] = {
BASE64_ENCODE_TABLE[(sequence >> 26) & 0x3f],
BASE64_ENCODE_TABLE[(sequence >> 20) & 0x3f],
BASE64_ENCODE_TABLE[(sequence >> 14) & 0x3f],
BASE64_ENCODE_TABLE[(sequence >> 8) & 0x3f],
};

output.Add(string_view(buffer, 4));

sequence = 0;
sequenceIndex = 0;
}
}

output.AddMultipleTimes(string_view("=", 1), (3 - sequenceIndex) % 3);
}

bool Decode(BufferView view, Buffer& output)
{
uint32 sequence = 0;
uint32 sequenceIndex = 0;
char lastEncoded = 0;

for (uint32 i = 0; i < view.GetLength(); ++i)
{
char encoded = view[i];
CHECK(encoded < sizeof(BASE64_DECODE_TABLE) / sizeof(*BASE64_DECODE_TABLE), false, "");

if (encoded == '\r' || encoded == '\n') {
continue;
}

if (lastEncoded == '=' && sequenceIndex == 0) {
AppCUI::Dialogs::MessageBox::ShowError("Warning!", "Ignoring extra bytes after the end of buffer");
break;
}

uint32 decoded;

if (encoded == '=') {
// padding
decoded = 0;
} else {
decoded = BASE64_DECODE_TABLE[encoded];
CHECK(decoded != -1, false, "");
}

sequence |= decoded << (2 + (4 - sequenceIndex) * 6);
sequenceIndex++;

if (sequenceIndex % 4 == 0) {
char* buffer = (char*) &sequence;
output.Add(string_view(buffer + 3, 1));
output.Add(string_view(buffer + 2, 1));
output.Add(string_view(buffer + 1, 1));

sequence = 0;
sequenceIndex = 0;
}

lastEncoded = encoded;
}

return true;
}
}
3 changes: 3 additions & 0 deletions GViewCore/src/Unpack/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target_sources(GViewCore PRIVATE
Base64.cpp
)
2 changes: 2 additions & 0 deletions GenericPlugins/Unpackers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include(generic_plugin)
create_generic_plugin(Unpackers)
25 changes: 25 additions & 0 deletions GenericPlugins/Unpackers/include/Unpackers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include "GView.hpp"
#include <cmath>

namespace GView::GenericPlugins::Unpackers
{
using namespace AppCUI::Graphics;
using namespace GView::View;

class Plugin : public Window, public Handlers::OnButtonPressedInterface
{
Reference<ListView> list;
Reference<CheckBox> sync;

public:
Plugin();

void OnButtonPressed(Reference<Button> button) override;
void Update();

bool Base64Decode(BufferView view, Buffer& output);
void Base64Encode(BufferView view, Buffer& output);
};
} // namespace GView::GenericPlugins::Unpackers
1 change: 1 addition & 0 deletions GenericPlugins/Unpackers/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target_sources(Unpackers PRIVATE Unpackers.cpp)
Loading

0 comments on commit 1d07ad6

Please sign in to comment.