Skip to content

Commit

Permalink
CMake: added a small base64 encoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed May 30, 2024
1 parent 7ad8df8 commit 0a41a39
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
57 changes: 57 additions & 0 deletions cmake/base64encoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright libOpenCOR contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "../extern/modp_b64/modp_b64.h"

#include <filesystem>
#include <fstream>
#include <iostream>

int main(int pArgC, char *pArgV[])
{
// Convert the given file to a base64-encoded string.

if (pArgC != 2) {
std::cerr << "Usage: " << pArgV[0] << " <file>" << std::endl;

return 1;
}

std::uintmax_t fileSize = std::filesystem::file_size(pArgV[1]);
char *buffer = new char[fileSize];
std::ifstream file(pArgV[1], std::ios::binary);

file.read(buffer, fileSize);

if (!file) {
std::cerr << "Error: the file could not be read." << std::endl;

return 1;
}

file.close();

char *base64 = new char[modp_b64_encode_len(fileSize)];

modp_b64_encode(base64, buffer, fileSize);

std::cout << base64 << std::endl;

delete[] base64;
delete[] buffer;

return 0;
}
13 changes: 13 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Generate our base64 encoder.

set(BASE64ENCODER ${CMAKE_BINARY_DIR}/base64encoder)

try_compile(BASE64ENCODER_EXE ${CMAKE_BINARY_DIR}
SOURCES ${CMAKE_SOURCE_DIR}/cmake/base64encoder.cpp
${CMAKE_SOURCE_DIR}/extern/modp_b64/modp_b64.cc
COPY_FILE ${BASE64ENCODER})

if(NOT BASE64ENCODER_EXE)
message(FATAL_ERROR "base64encoder could not be built...")
endif()

# Include our different tests.

include(api/file/tests.cmake)
Expand Down

0 comments on commit 0a41a39

Please sign in to comment.