Skip to content

Commit

Permalink
Imported sources
Browse files Browse the repository at this point in the history
  • Loading branch information
maroontress-tomohisa committed Aug 8, 2023
0 parents commit a41aa38
Show file tree
Hide file tree
Showing 20 changed files with 977 additions and 0 deletions.
81 changes: 81 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Android

on:
push:
branches: [main]
pull_request:
branches: [main]

env:
BUILD_TYPE: Release

jobs:
build:
runs-on: macos-latest
timeout-minutes: 30
strategy:
matrix:
abi: [x86_64, x86, arm64-v8a, armeabi-v7a]

steps:
- uses: actions/checkout@v3
with:
submodules: true

- name: Build ${{matrix.abi}}
run: |
sh build-android.sh ${{matrix.abi}} -DBUILD_TESTSUITE=OFF -DCMAKE_INSTALL_PREFIX:PATH="$HOME/android/${{matrix.abi}}"
cmake --install build-${{matrix.abi}}
- name: Archive artifacts
uses: actions/upload-artifact@v3
with:
name: android
path: ~/android/

test:
timeout-minutes: 30
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
abi: [x86_64]
runs-on: ${{matrix.os}}

steps:
- uses: actions/checkout@v3
with:
submodules: true

- name: AVD cache
uses: actions/cache@v3
id: avd-cache
with:
path: |
~/.android/avd/*
~/.android/adb*
key: hostos-${{matrix.os}}-api-21-abi-${{matrix.abi}}
- name: Create AVD and generate snapshot for caching
if: steps.avd-cache.outputs.cache-hit != 'true'
uses: reactivecircus/android-emulator-runner@v2
with:
emulator-options: -no-window -gpu swiftshader_indirect -no-audio -no-boot-anim -camera-back none
api-level: 21
arch: ${{matrix.abi}}
ndk: 25.2.9519653
force-avd-creation: false
disable-animations: false
script: echo "Generated AVD snapshot for caching."
- name: Run tests
uses: reactivecircus/android-emulator-runner@v2
with:
emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -no-audio -no-boot-anim -camera-back none
api-level: 21
arch: ${{matrix.abi}}
ndk: 25.2.9519653
force-avd-creation: false
disable-animations: true
script: sh build-android.sh ${{matrix.abi}} -DCMAKE_INSTALL_PREFIX:PATH="$HOME/android/${{matrix.abi}}"
- name: Archive artifacts
uses: actions/upload-artifact@v3
with:
name: android-testsuite-${{matrix.os}}-${{matrix.abi}}
path: build-${{matrix.abi}}/testsuite/log.txt
31 changes: 31 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CMake

on:
push:
branches: [main]
pull_request:
branches: [main]

env:
BUILD_TYPE: Release

jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{matrix.os}}

steps:
- uses: actions/checkout@v3
with:
submodules: true

- name: Configure CMake
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

- name: Build
run: cmake --build build --config ${{env.BUILD_TYPE}}

- name: Test
run: ctest --test-dir build -C ${{env.BUILD_TYPE}} -V
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/out/
/.vs/
/.vscode/
/build/
/build-*/
/Testing/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lighter"]
path = lighter
url = [email protected]:maroontress/lighter.git
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.24)

project("mimicssl-md5" VERSION 1.0.0)

add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

add_subdirectory(libmimicssl-md5)
add_subdirectory(mimicssl-md5-cli)

option(BUILD_TESTSUITE "Build testsuite" ON)
if (${BUILD_TESTSUITE})
include(CTest)
add_subdirectory(testsuite)
endif()
23 changes: 23 additions & 0 deletions COPYRIGHT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright (c) 2023 Maroontress Fast Software. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS *AS IS* AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# MimicSSL-MD5

This is an [MD5][wikipedia::md5] implementation in C23, and its API is
compatible with [OpenSSL 1.1][openssl::md5_init]. See the
[RFC 1321][ietf::rfc1321] for the MD5 specifications.

Note that the current implementation works only on little-endian platforms.

## Example

An example usage would be as follows:

```c
MD5_CTX ctx;
uint8_t md[16];
char buffer[1024];

MD5_Init(&ctx);
for (;;) {
size_t size = fread(buffer, 1, sizeof(buffer), file);
if (size == 0) {
break;
}
MD5_Update(&ctx, buffer, size);
}
MD5_Final(md, &ctx);
```
## Build
This repository uses [lighter][maroontress::lighter] for testing as a sub-module
of Git. Therefore, clone it as follows:
```plaintext
git clone --recursive URL
```

Then build the library as follows:

```textplain
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release
ctest --test-dir build -C Release
```

## Build for Android

Set environment variables `ANDROID_HOME` and `ANDROID_NDK` appropriately. For
example:

```sh
export ANDROID_HOME=/usr/local/lib/android/sdk
export ANDROID_NDK=$ANDROID_HOME/ndk/25.2.9519653
```

Note that the value of `ANDROID_HOME` will vary depending on your environment,
but a typical configuration would be as follows:

- Windows: `C:\\Users\\USERNAME\\AppData\\Local\\Android\\sdk`
- Linux: `/home/USERNAME/Android/Sdk`
- macOS: `/Users/USERNAME/Library/Android/sdk`

Then build as follows:

```
sh build-android.sh ABI -DBUILD_TESTSUITE=OFF
```

`ABI` should be replaced by `arm64-v8a`, `armeabi-v7a`, `x86`, or `x86_64`.

[wikipedia::md5]: https://en.wikipedia.org/wiki/MD5
[ietf::rfc1321]: https://www.ietf.org/rfc/rfc1321.txt
[openssl::md5_init]: https://www.openssl.org/docs/man1.1.1/man3/MD5_Init.html
[maroontress::lighter]: https://github.com/maroontress/lighter
40 changes: 40 additions & 0 deletions build-android.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/sh

if [ "$#" = 0 ] ; then
echo usage: $0 ABI [CMAKE_OPTIONS...]
exit 1
fi

# Environment variables:
# ANDROID_NDK (e.g., "$HOME/Library/Android/sdk/ndk/25.2.9519653")
# ANDROID_HOME (e.g., "$HOME/Library/Android/sdk")

# ABI:
# "x86"
# "x86_64"
# "arm64-v8a"
# "armeabi-v7a"

# Options:
# -DCMAKE_INSTALL_PREFIX:PATH="$HOME/android/$ABI"
# -DBUILD_TESTSUITE=OFF

ABI=$1
if [ "$ABI" = "armeabi-v7a" ] ; then
EXTRA_ARGS='-DCMAKE_ANDROID_ARM_NEON=ON'
fi
shift
rm -rf "build-$ABI" || exit 1
cmake -S . -B "build-$ABI" -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_SYSTEM_NAME=Android \
-DCMAKE_CROSSCOMPILING_EMULATOR="$PWD/emulator-android.sh" \
-DCMAKE_SYSTEM_VERSION="21" \
-DCMAKE_ANDROID_ARCH_ABI="$ABI" \
-DCMAKE_ANDROID_NDK="$ANDROID_NDK" \
$EXTRA_ARGS "$@" || exit 1
cmake --build "build-$ABI" --config Release -v || exit 1
if test -f "build-$ABI/testsuite/testsuite" ; then
sh emulator-android.sh --adb-push "build-$ABI/testsuite"
ctest --test-dir "build-$ABI" -C Release -V || exit 1
sh emulator-android.sh --adb-pull "build-$ABI/testsuite"
fi
53 changes: 53 additions & 0 deletions emulator-android.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/sh

# Usage:
# emulator-android.sh --adb-push DIR
# emulator-android.sh --adb-pull DIR
# emulator-android.sh TESTCASE [ARGS...]

ADB="$ANDROID_HOME/platform-tools/adb"
REMOTE_DIR="/data/local/tmp"
LOG="log.txt"

setupLog() {
echo "---" >> $LOG
echo args: "$@" >> $LOG
echo pwd: $PWD >> $LOG
echo log: >> $LOG
}

if [ "$1" = "--adb-push" ] ; then
cd "$2"
setupLog "$@"
echo "--adb-push: do nothing" >> $LOG
exit
fi
if [ "$1" = "--adb-pull" ] ; then
cd "$2"
setupLog "$@"
echo "--adb-pull: do nothing" >> $LOG
exit
fi

setupLog "$@"
name="$1"
if [ "${name##*/}" != "testsuite" ] ; then
echo unexpected testcase >> $LOG
exit 1
fi
shift
if [ "$1" = "--gtest_list_tests" ] ; then
echo devices: >> $LOG
$ADB devices >> $LOG
$ADB push testsuite $REMOTE_DIR/testsuite >> $LOG
$ADB shell chmod +x $REMOTE_DIR/testsuite >> $LOG
fi

n="'"
args=$n$1$n
shift
for i in "$@" ; do
args="$args $n$i$n"
done
echo $ADB shell "cd $REMOTE_DIR && ./testsuite $args" >> $LOG
$ADB shell "cd $REMOTE_DIR && ./testsuite $args"
40 changes: 40 additions & 0 deletions libmimicssl-md5/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
set(CMAKE_C_STANDARD 23)

if("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang"
OR "${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang"
OR "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
add_compile_options(-Wall -Wextra -Wpedantic)
set(CMAKE_C_FLAGS_DEBUG "-O0 -g")
set(CMAKE_C_FLAGS_RELEASE "-O3")
elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
add_compile_options(/W4 /WX)
endif()

include(GenerateExportHeader)

add_library(mimicssl-md5 STATIC)
generate_export_header(mimicssl-md5
BASE_NAME MD5
EXPORT_FILE_NAME ${PROJECT_BINARY_DIR}/md5_export.h)
target_sources(mimicssl-md5 PRIVATE
src/md5.c)
target_include_directories(mimicssl-md5 PUBLIC
include
${PROJECT_BINARY_DIR})

add_library(mimicssl-md5-shared SHARED)
target_sources(mimicssl-md5-shared PRIVATE
src/md5.c)
target_include_directories(mimicssl-md5-shared PUBLIC
include
${PROJECT_BINARY_DIR})
set_target_properties(mimicssl-md5-shared
PROPERTIES OUTPUT_NAME mimicssl-md5)

include(GNUInstallDirs)
install(TARGETS mimicssl-md5 DESTINATION lib)
install(TARGETS mimicssl-md5-shared DESTINATION lib)
install(FILES
include/md5.h
${PROJECT_BINARY_DIR}/md5_export.h
DESTINATION include/mimicssl)
Loading

0 comments on commit a41aa38

Please sign in to comment.