Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE #85 : Base64-Encoder Executable #93

Open
wants to merge 14 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(_LOG_MAX "DEBUG")
else()
set(_LOG_MAX "INFORMATION")
set(CMAKE_BUILD_TYPE "Release") # Default to Release, solves KIM slowdown complaints
endif()
set_cache_with_fallback(KIM_API_LOG_MAXIMUM_LEVEL "${_LOG_MAX}" STRING "Maximum log verbosity")
unset(_LOG_MAX)
Expand Down
1 change: 1 addition & 0 deletions cmake/build-tree-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ if("${KIM-API_FIND_VERSION}" VERSION_GREATER "2.1.99")
add_executable(KIM-API::portable-model-info ALIAS portable-model-info)
add_executable(KIM-API::simulator-model ALIAS simulator-model)
add_executable(KIM-API::collections-info ALIAS collections-info)
add_executable(KIM-API::base64-encode ALIAS base64-encode)
add_executable(KIM-API::shared-library-test ALIAS shared-library-test)
endif()

Expand Down
107 changes: 46 additions & 61 deletions cmake/items-macros.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -494,68 +494,53 @@ macro(_set_simulator_model_template_variables)
endmacro()

#
# Function to create a custom command to convert a file (blob) to a c source.
# Function to create a custom command to convert a file (blob) to a cpp source.
#
# Convert _filein to hex then format as c source file and write to _fileout.
# Assumes _filein is a relative file name in the _dirin directory.
#
# Sets _blob_to_c_command to string for use with add_custom_command()
#
function(_add_custom_command_blob_to_c _dirin _filein _fileout)
include(FindPackageMessage)
find_program(XXD_EXECUTABLE "xxd")
if(XXD_EXECUTABLE)
find_package_message(xxd "Found xxd: (${XXD_EXECUTABLE})" "found")

string(MAKE_C_IDENTIFIER ${_filein} _cfilein)
set(_edit_xxd_output "${CMAKE_CURRENT_BINARY_DIR}/EditXXDOutput.cmake")
if(NOT EXISTS "${_edit_xxd_output}")
file(WRITE "${_edit_xxd_output}" # use a bracket argument to avoid ugly escaping
[=[
# This file was automatically generated by CMake; do not edit.
file(READ "${fileout}" _content)
string(REGEX REPLACE "unsigned char.*[[][]]" "unsigned char const ${varname}[]" _content "${_content}")
string(REGEX REPLACE "unsigned int.*=" "unsigned int const ${varname}_len =" _content "${_content}")
file(WRITE "${fileout}" "${_content}")
unset(_content)
]=]
)
function(_add_custom_command_blob_to_cpp _dirin _filein _fileout)
get_property(_IMPORTED TARGET KIM-API::base64-encode PROPERTY "IMPORTED")
if(_IMPORTED) # using install tree config
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(_BUILD_TYPE "NOCONFIG")
else()
string(TOUPPER "${CMAKE_BUILD_TYPE}" _BUILD_TYPE)
endif()
add_custom_command(OUTPUT ${_fileout}
COMMAND ${XXD_EXECUTABLE} -i "${_filein}" "${_fileout}"
COMMAND ${CMAKE_COMMAND} "-Dvarname=${_cfilein}" "-Dfilein=${_filein}" "-Dfileout=${_fileout}" -P "${_edit_xxd_output}"
DEPENDS "${_dirin}/${_filein}" "${_edit_xxd_output}"
WORKING_DIRECTORY "${_dirin}"
)
unset(_cfilein)
unset(_edit_xxd_output)
else()
find_package_message(xxd "Missing xxd: Falling back to less efficient cmake implementation." "missing")

string(MAKE_C_IDENTIFIER ${_filein} _cfilein)
set(_blob_to_source "${CMAKE_CURRENT_BINARY_DIR}/BlobToCSource.cmake")
if(NOT EXISTS "${_blob_to_source}")
file(WRITE "${_blob_to_source}" # use a bracket argument to avoid ugly escaping
[=[
# This file was automatically generated by CMake; do not edit.
file(READ "${filein}" _content HEX)
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," _content "${_content}")
string(REGEX REPLACE ",$" "" _content "${_content}")
file(WRITE "${fileout}" "unsigned char const ${varname}[] = { ${_content} };\n")
file(APPEND "${fileout}" "unsigned int const ${varname}_len = sizeof( ${varname} );\n")
unset(_content)
]=]
)
get_property(_BASE64_ENCODE TARGET KIM-API::base64-encode PROPERTY "IMPORTED_LOCATION_${_BUILD_TYPE}")
unset(_BUILD_TYPE)
if("${_BASE64_ENCODE}" STREQUAL "")
message(FATAL_ERROR "Unable to locate 'base64-encode' utility")
endif()
add_custom_command(OUTPUT ${_fileout}
COMMAND ${CMAKE_COMMAND} "-Dvarname=${_cfilein}" "-Dfilein=${_filein}" "-Dfileout=${_fileout}" -P "${_blob_to_source}"
DEPENDS "${_dirin}/${_filein}" "${_blob_to_source}"
WORKING_DIRECTORY "${_dirin}"
)
unset(_cfilein)
unset(_blob_to_source)
else() # using build tree config
set(_BASE64_ENCODE KIM-API::base64-encode)
endif()
string(MAKE_C_IDENTIFIER ${_filein} _cfilein)
set(_edit_encode_output "${CMAKE_CURRENT_BINARY_DIR}/EditEncodeOutput.cmake")
if(NOT EXISTS "${_edit_encode_output}")
file(WRITE "${_edit_encode_output}" # use a bracket argument to avoid ugly escaping
[=[
# This file was automatically generated by CMake; do not edit.
file(READ "${fileout}" _content)
string(REGEX REPLACE "unsigned char.*[[][]]" "unsigned char const ${varname}[]" _content "${_content}")
string(REGEX REPLACE "unsigned int.*=" "unsigned int const ${varname}_len =" _content "${_content}")
file(WRITE "${fileout}" "${_content}")
unset(_content)
]=]
)
endif()
endfunction(_add_custom_command_blob_to_c)
add_custom_command(OUTPUT ${_fileout}
COMMAND ${_BASE64_ENCODE} -i "${_filein}" "${_fileout}"
COMMAND ${CMAKE_COMMAND} "-Dvarname=${_cfilein}" "-Dfilein=${_filein}" "-Dfileout=${_fileout}" -P "${_edit_encode_output}"
DEPENDS "${_dirin}/${_filein}" "${_edit_encode_output}"
WORKING_DIRECTORY "${_dirin}"
)
unset(_cfilein)
unset(_edit_encode_output)
unset(_BASE64_ENCODE)
endfunction(_add_custom_command_blob_to_cpp)

#
# Function to create and define targets for c source files from provided data
Expand All @@ -564,13 +549,13 @@ endfunction(_add_custom_command_blob_to_c)
# Sets ITEM_*_SOURCES, ITEM_*_DECLARATIONS, ITEM_*_EMBEDDED_FILE_IDENTIFIER,
# and ITEM_*_EMBEDDED_FILE_INITIALIZER_LIST variables in parent scope.
#
function(_xxd_process_files)
function(_encode_process_files)
set(_options "")
set(_oneValueArgs FILE_TYPE)
set(_multiValueArgs FILE_NAMES)
cmake_parse_arguments(_ITEM "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
if(_ITEM_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unparsed arguments found in macro '_xxd_process_files'")
message(FATAL_ERROR "Unparsed arguments found in macro '_encode_process_files'")
endif()
unset(_options)
unset(_oneValueArgs)
Expand Down Expand Up @@ -607,9 +592,9 @@ function(_xxd_process_files)
else()
message(FATAL_ERROR "Cannot find metadata file '${_FNAME}'")
endif()
set(_FILE "${CMAKE_CURRENT_BINARY_DIR}/${_FNAME}.c")
set(_FILE "${CMAKE_CURRENT_BINARY_DIR}/${_FNAME}.cpp")
list(APPEND _ITEM_SOURCES ${_FILE})
_add_custom_command_blob_to_c("${_DIR_IN}" "${_FNAME}" "${_FILE}")
_add_custom_command_blob_to_cpp("${_DIR_IN}" "${_FNAME}" "${_FILE}")
unset(_DIR_IN)
endforeach()
unset(_FNAME)
Expand All @@ -620,7 +605,7 @@ function(_xxd_process_files)
set(ITEM_${_ITEM_FILE_TYPE}_DECLARATIONS "${_ITEM_DECLARATIONS}" PARENT_SCOPE)
set(ITEM_${_ITEM_FILE_TYPE}_EMBEDDED_FILE_IDENTIFIER "${_ITEM_EMBEDDED_FILE_IDENTIFIER}" PARENT_SCOPE)
set(ITEM_${_ITEM_FILE_TYPE}_EMBEDDED_FILE_INITIALIZER_LIST "${_ITEM_EMBEDDED_FILE_INITIALIZER_LIST}" PARENT_SCOPE)
endfunction(_xxd_process_files)
endfunction(_encode_process_files)

#
# Function to set collection install prefix and item library file name.
Expand Down Expand Up @@ -793,11 +778,11 @@ function(_add_kim_api_library)
endif()

set(_ITEM_SOURCES "")
_xxd_process_files(FILE_TYPE SM_SPEC_FILE FILE_NAMES "${ITEM_SM_SPEC_FILE}")
_encode_process_files(FILE_TYPE SM_SPEC_FILE FILE_NAMES "${ITEM_SM_SPEC_FILE}")
list(APPEND _ITEM_SOURCES ${ITEM_SM_SPEC_FILE_SOURCES})
_xxd_process_files(FILE_TYPE PARAMETER_FILE FILE_NAMES "${ITEM_PARAMETER_FILES}")
_encode_process_files(FILE_TYPE PARAMETER_FILE FILE_NAMES "${ITEM_PARAMETER_FILES}")
list(APPEND _ITEM_SOURCES ${ITEM_PARAMETER_FILE_SOURCES})
_xxd_process_files(FILE_TYPE METADATA_FILE FILE_NAMES "${ITEM_METADATA_FILES}")
_encode_process_files(FILE_TYPE METADATA_FILE FILE_NAMES "${ITEM_METADATA_FILES}")
list(APPEND _ITEM_SOURCES ${ITEM_METADATA_FILE_SOURCES})

configure_file(${KIM-API-ITEMS_DIR}/item-info.txt.in ${CMAKE_CURRENT_BINARY_DIR}/item-info.txt @ONLY)
Expand Down
26 changes: 20 additions & 6 deletions cpp/src/KIM_SharedLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// Release: This file is part of the kim-api.git repository.
//

#include "base64-encode/base64.hpp" // For base64 decoding
#include <cstdio>
#include <cstring>
#ifndef _WIN32
Expand Down Expand Up @@ -384,10 +385,7 @@ int SharedLibrary::Close()
LOG_DEBUG("Exit 1=" + callString);
return true;
}
else
{
sharedLibraryHandle_ = NULL;
}
else { sharedLibraryHandle_ = NULL; }

LOG_DEBUG("Exit 0=" + callString);
return false;
Expand Down Expand Up @@ -649,7 +647,14 @@ int SharedLibrary::WriteParameterFileDirectory()
std::ofstream fl;
fl.open(specificationFilePathName.string().c_str(),
std::ifstream::out | std::ifstream::binary);
fl.write(reinterpret_cast<const char *>(specificationData), len);
std::vector<char> binary_line;
binary_line.reserve(base64::decoded_size(len));
std::pair<std::size_t, std::size_t> char_out_and_char_in
= base64::decode(binary_line.data(), specificationData, len);

fl.write(reinterpret_cast<char *>(binary_line.data()),
static_cast<std::streamsize>(char_out_and_char_in.first));

if (!fl)
{
LOG_ERROR("Unable to get write parameter file.");
Expand Down Expand Up @@ -680,7 +685,16 @@ int SharedLibrary::WriteParameterFileDirectory()
std::ofstream fl;
fl.open(parameterFilePathName.string().c_str(),
std::ifstream::out | std::ifstream::binary);
fl.write(reinterpret_cast<const char *>(strPtr), length);
int usable_chars = static_cast<int>(
length); // unsigned int to signed to avoid underflow

std::vector<char> binary_line;
binary_line.reserve(base64::decoded_size(length));
std::pair<std::size_t, std::size_t> char_out_and_char_in
= base64::decode(binary_line.data(), strPtr, usable_chars);

fl.write(reinterpret_cast<char *>(binary_line.data()),
static_cast<std::streamsize>(char_out_and_char_in.first));
if (!fl)
{
LOG_ERROR("Unable to get write parameter file.");
Expand Down
18 changes: 18 additions & 0 deletions cpp/src/base64-encode/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.10)

project(base64-encode VERSION 1.0)

set(CMAKE_CXX_STANDARD 98)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_compile_options("-std=c++98") # Sometime the CMAKE_CXX_STANDARD does not work

set(SOURCE_FILES base64-encode.cpp)

# Add executable target
add_executable(${PROJECT_NAME} ${SOURCE_FILES})

set_target_properties(${PROJECT_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/utils
)

install(TARGETS ${PROJECT_NAME} DESTINATION bin)
23 changes: 23 additions & 0 deletions cpp/src/base64-encode/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE
30 changes: 30 additions & 0 deletions cpp/src/base64-encode/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Base64 Encode
=============

`base64-encode` is a simple xxd replacement to convert files
into embeddable C++ source code. Instead of using binary array
it used base64 strings, which makes it much more performant
for large files. Currently only -i option is supported for
compatibility. More options to be released in future. C++ 98 compliant.
Comment on lines +4 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`base64-encode` is a simple xxd replacement to convert files
into embeddable C++ source code. Instead of using binary array
it used base64 strings, which makes it much more performant
for large files. Currently only -i option is supported for
compatibility. More options to be released in future. C++ 98 compliant.
`base64-encode` is a straightforward replacement for xxd,
designed to convert files into embeddable C++ source code.
Unlike binary arrays, it utilizes base64 strings, significantly
enhancing performance for large files. Currently, only the -i
option is supported for compatibility, with more options
planned for future releases. It is compliant with C++98 standards.


### Usage
```bash
base64-encode -i file.in out.cpp
```
```
Options:
-h Help
-v Show version information
-i Convert input file to C++ source
```

### Build
```bash
mkdir build && cd build
cmake ..
make
sudo make install
```

### License
Boost Software license
Loading
Loading