1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00

chore(webfuse): update dependencies

* update libfuse to 3.8.0 (remain API compatibility 3.1+)
* update libwebsockes to 3.1.0
* update qemu-user-static to 4.1.0-1
* update qtest to 1.10.0
* update C++ standard to 14
* use own base64 implementatin instead of libwebsockets
* introduce new build type "Coverage"
* ignore test code for code coverage
This commit is contained in:
Falk Werner 2019-12-02 21:33:45 +01:00 committed by GitHub
commit 4cdf11ce33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 460 additions and 98 deletions

View File

@ -16,13 +16,14 @@ env:
- PARALLELMFLAGS="-j4"
- PORTABLE_WORSPACE=1
matrix:
- BUILDTYPE=Debug MARCH=amd64 CHECK_TARGET=memcheck COVERAGE=ON
- BUILDTYPE=Release MARCH=amd64 CHECK_TARGET=memcheck COVERAGE=OFF
- BUILDTYPE=Debug MARCH=arm32v7 CHECK_TARGET=check COVERAGE=OFF
- BUILDTYPE=MinSizeRel MARCH=arm32v7 CHECK_TARGET=check COVERAGE=OFF
- BUILDTYPE=Debug MARCH=amd64 CHECK_TARGET=memcheck
- BUILDTYPE=Coverage MARCH=amd64 CHECK_TARGET=check
- BUILDTYPE=Release MARCH=amd64 CHECK_TARGET=memcheck
- BUILDTYPE=Debug MARCH=arm32v7 CHECK_TARGET=check
- BUILDTYPE=MinSizeRel MARCH=arm32v7 CHECK_TARGET=check
before_script:
- make BUILDTYPE=$BUILDTYPE MARCH=$MARCH COVERAGE=$COVERAGE
- make BUILDTYPE=$BUILDTYPE MARCH=$MARCH
script:
- make BUILDTYPE=$BUILDTYPE MARCH=$MARCH COVERAGE=$COVERAGE $CHECK_TARGET
- make BUILDTYPE=$BUILDTYPE MARCH=$MARCH $CHECK_TARGET
after_success:
- bash <(curl -s https://codecov.io/bash)

View File

@ -3,7 +3,9 @@ project(webfuse VERSION 0.2.0 DESCRIPTION "Websocket filesystem based on libfuse
option(WITHOUT_TESTS "disable unit tests" OFF)
option(WITHOUT_EXAMPLE "disable example" OFF)
option(WITH_COVERAGE "enable code coverage" OFF)
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(coverage)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
@ -17,31 +19,12 @@ pkg_check_modules(UUID REQUIRED uuid)
add_definitions(-D_FILE_OFFSET_BITS=64)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(C_WARNINGS -Wall -Wextra)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") AND WITHOUT_COVERAGE)
add_definitions(
-pg
--coverage
-fprofile-arcs
-ftest-coverage
)
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
link_libraries(gcov)
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
endif()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg -fprofile-arcs")
endif()
include_directories(
"include"
${FUSE3_INCLUDE_DIRS}
@ -77,6 +60,7 @@ add_library(webfuse-core STATIC
lib/webfuse/core/status.c
lib/webfuse/core/string.c
lib/webfuse/core/path.c
lib/webfuse/core/base64.c
lib/webfuse/core/lws_log.c
)
@ -300,6 +284,7 @@ add_executable(alltests
test/core/test_string.cc
test/core/test_slist.cc
test/core/test_path.cc
test/core/test_base64.cc
test/core/test_status.cc
test/core/test_message.cc
test/core/test_message_queue.cc

View File

@ -23,7 +23,6 @@ SCRIPTDIR ?= $(PROJECTDIR)/build/mkdocker
OUTDIR ?= $(PROJECTDIR)/.build
FETCHDIR ?= $(PROJECTDIR)/.deps
BUILDTYPE ?= Debug
COVERAGE ?= OFF
SKIP_FETCH ?=
@ -51,23 +50,23 @@ FETCH_TARGETS += $(FETCHDIR)/dumb-init-$(DUMB_INIT_VERSION).tar.gz
$(FETCHDIR)/dumb-init-$(DUMB_INIT_VERSION).tar.gz: URL := https://github.com/Yelp/dumb-init/archive/v${DUMB_INIT_VERSION}.tar.gz
$(SKIP_MD5SUM)$(FETCHDIR)/dumb-init-$(DUMB_INIT_VERSION).tar.gz: MD5 := 6166084b05772cdcf615a762c6f3b32e
GTEST_VERSION ?= 1.8.1
GTEST_VERSION ?= 1.10.0
DOCKER_BUILDARGS += GTEST_VERSION=$(GTEST_VERSION)
FETCH_TARGETS += $(FETCHDIR)/googletest-release-$(GTEST_VERSION).tar.gz
$(FETCHDIR)/googletest-release-$(GTEST_VERSION).tar.gz: URL := https://github.com/google/googletest/archive/release-$(GTEST_VERSION).tar.gz
$(SKIP_MD5SUM)$(FETCHDIR)/googletest-release-$(GTEST_VERSION).tar.gz: MD5 := 2e6fbeb6a91310a16efe181886c59596
$(SKIP_MD5SUM)$(FETCHDIR)/googletest-release-$(GTEST_VERSION).tar.gz: MD5 := ecd1fa65e7de707cd5c00bdac56022cd
FUSE_VERSION ?= 3.1.1
FUSE_VERSION ?= 3.8.0
DOCKER_BUILDARGS += FUSE_VERSION=$(FUSE_VERSION)
FETCH_TARGETS += $(FETCHDIR)/libfuse-fuse-$(FUSE_VERSION).tar.gz
$(FETCHDIR)/libfuse-fuse-$(FUSE_VERSION).tar.gz: URL := https://github.com/libfuse/libfuse/archive/fuse-$(FUSE_VERSION).tar.gz
$(SKIP_MD5SUM)$(FETCHDIR)/libfuse-fuse-$(FUSE_VERSION).tar.gz: MD5 := 097f194856938afdd98bea1a5c046edd
$(SKIP_MD5SUM)$(FETCHDIR)/libfuse-fuse-$(FUSE_VERSION).tar.gz: MD5 := 909d6b2cfc2faa9f4b6033d356280540
WEBSOCKETS_VERSION ?= 3.1.0
WEBSOCKETS_VERSION ?= 3.2.0
DOCKER_BUILDARGS += WEBSOCKETS_VERSION=$(WEBSOCKETS_VERSION)
FETCH_TARGETS += $(FETCHDIR)/libwebsockets-$(WEBSOCKETS_VERSION).tar.gz
$(FETCHDIR)/libwebsockets-$(WEBSOCKETS_VERSION).tar.gz: URL := https://github.com/warmcat/libwebsockets/archive/v$(WEBSOCKETS_VERSION).tar.gz
$(SKIP_MD5SUM)$(FETCHDIR)/libwebsockets-$(WEBSOCKETS_VERSION).tar.gz: MD5 := 325359a25d5f6d22725ff5d086db1c76
$(SKIP_MD5SUM)$(FETCHDIR)/libwebsockets-$(WEBSOCKETS_VERSION).tar.gz: MD5 := 1d06f5602604e67e6f50cef9857c6b0c
JANSSON_VERSION ?= 2.12
DOCKER_BUILDARGS += JANSSON_VERSION=$(JANSSON_VERSION)
@ -75,11 +74,11 @@ FETCH_TARGETS += $(FETCHDIR)/jansson-$(JANSSON_VERSION).tar.gz
$(FETCHDIR)/jansson-$(JANSSON_VERSION).tar.gz: URL := https://github.com/akheron/jansson/archive/v$(JANSSON_VERSION).tar.gz
$(SKIP_MD5SUM)$(FETCHDIR)/jansson-$(JANSSON_VERSION).tar.gz: MD5 := c4b106528d5ffb521178565de1ba950d
QEMU_VERSION ?= v3.1.0-2
QEMU_VERSION ?= v4.1.0-1
DOCKER_BUILDARGS += QEMU_VERSION_=$(QEMU_VERSION)
FETCH_TARGETS += $(FETCHDIR)/qemu-arm-static-$(QEMU_VERSION)
$(FETCHDIR)/qemu-arm-static-$(QEMU_VERSION): URL := https://github.com/multiarch/qemu-user-static/releases/download/$(QEMU_VERSION)/qemu-arm-static
$(SKIP_MD5SUM)$(FETCHDIR)/qemu-arm-static-$(QEMU_VERSION): MD5 := 8ebd24e63fdfa07c557d45373bd831b1
$(SKIP_MD5SUM)$(FETCHDIR)/qemu-arm-static-$(QEMU_VERSION): MD5 := e508e6e4dd7f3a851207aac245a4653f
#######################################################################################################################
# Architecture-specific rule target configuration
@ -116,8 +115,6 @@ OUTDIRS += $(OUTDIR)/src
EXTRACT_TARGETS += $(OUTDIR)/docker/qemu-arm-static-$(QEMU_VERSION)
EXTRACT_TARGETS += $(patsubst $(FETCHDIR)/%.tar.gz,$(OUTDIR)/src/%,$(FETCH_TARGETS))
CMAKEFLAGS += -DWITH_COVERAGE=$(COVERAGE)
#######################################################################################################################
# Makefile dependencies

View File

@ -409,19 +409,22 @@ By default, unit tests and example application are enabled. You can disable them
#### libfuse
wget -O fuse-3.1.1.tar.gz https://github.com/libfuse/libfuse/archive/fuse-3.1.1.tar.gz
tar -xf fuse-3.1.1.tar.gz
cd libfuse-fuse-3.1.1
./makeconf.sh
./configure
make
sudo make install
To install libfuse, meson is needed. Please refer to [meson quick guide](https://mesonbuild.com/Quick-guide.html) for setup instructions.
wget -O fuse-3.8.0.tar.gz https://github.com/libfuse/libfuse/archive/fuse-3.8.0.tar.gz
tar -xf fuse-3.8.0.tar.gz
cd libfuse-fuse-3.8.0
mkdir .build
cd .build
meson ..
ninja
sudo ninja install
#### libwebsockets
wget -O libwebsockets-3.1.0.tar.gz https://github.com/warmcat/libwebsockets/archive/v3.1.0.tar.gz
tar -xf libwebsockets-3.1.0.tar.gz
cd libwebsockets-3.1.0
wget -O libwebsockets-3.2.0.tar.gz https://github.com/warmcat/libwebsockets/archive/v3.2.0.tar.gz
tar -xf libwebsockets-3.2.0.tar.gz
cd libwebsockets-3.2.0
mkdir .build
cd .build
cmake ..
@ -443,9 +446,9 @@ By default, unit tests and example application are enabled. You can disable them
Installation of GoogleTest is optional webfuse library, but required to compile tests.
wget -O gtest-1.8.1.tar.gz https://github.com/google/googletest/archive/release-1.8.1.tar.gz
tar -xf gtest-1.8.1.tar.gz
cd googletest-release-1.8.1
wget -O gtest-1.10.0.tar.gz https://github.com/google/googletest/archive/release-1.10.0.tar.gz
tar -xf gtest-1.10.0.tar.gz
cd googletest-release-1.10.0
mkdir .build
cd .build
cmake ..

View File

@ -40,7 +40,7 @@ RUN set -x \
&& rm -rf "$builddir" \
&& apk del .build-deps
ARG GTEST_VERSION=1.8.1
ARG GTEST_VERSION=1.10.0
RUN set -x \
&& builddir="/tmp/out" \
@ -50,22 +50,24 @@ RUN set -x \
&& make "$PARALLELMFLAGS" install \
&& rm -rf "$builddir"
ARG FUSE_VERSION=3.1.1
ARG FUSE_VERSION=3.8.0
RUN set -x \
&& builddeps="libtool automake autoconf gettext-dev m4 linux-headers" \
&& builddeps="linux-headers eudev-dev python3 py3-pip py3-setuptools py3-cryptography" \
&& apk add --no-cache --virtual .build-deps $builddeps \
&& cd "/usr/local/src/libfuse-fuse-$FUSE_VERSION" \
&& ./makeconf.sh \
&& pip3 install meson \
&& builddir="/tmp/out" \
&& mkdir -p "$builddir" \
&& cd "$builddir" \
&& "/usr/local/src/libfuse-fuse-$FUSE_VERSION/configure" \
&& make "$PARALLELMFLAGS" install \
&& meson "/usr/local/src/libfuse-fuse-$FUSE_VERSION" \
&& meson configure -Dexamples=false \
&& ninja \
&& ninja install \
&& pip3 uninstall -y meson \
&& rm -rf "$builddir" \
&& apk del .build-deps
ARG WEBSOCKETS_VERSION=3.1.0
ARG WEBSOCKETS_VERSION=3.2.0
RUN set -x \
&& apk add --no-cache \
@ -106,6 +108,3 @@ RUN set -x \
WORKDIR "$OUTDIR"
ENTRYPOINT ["dumb-init", "--"]
# unused
ARG QEMU_VERSION_=v3.1.0-2

View File

@ -39,7 +39,7 @@ RUN set -x \
&& rm -rf "$builddir" \
&& apt purge -y $builddeps
ARG GTEST_VERSION=1.8.1
ARG GTEST_VERSION=1.10.0
RUN set -x \
&& builddir="/tmp/out" \
@ -49,22 +49,23 @@ RUN set -x \
&& make "$PARALLELMFLAGS" install \
&& rm -rf "$builddir"
ARG FUSE_VERSION=3.1.1
ARG FUSE_VERSION=3.8.0
RUN set -x \
&& builddeps="libtool automake gettext" \
&& builddeps="udev gettext python3 python3-pip python3-setuptools python3-wheel" \
&& apt install --yes --no-install-recommends $builddeps \
&& cd "/usr/local/src/libfuse-fuse-$FUSE_VERSION" \
&& ./makeconf.sh \
&& pip3 install --system meson \
&& builddir="/tmp/out" \
&& mkdir -p "$builddir" \
&& cd "$builddir" \
&& "/usr/local/src/libfuse-fuse-$FUSE_VERSION/configure" \
&& make "$PARALLELMFLAGS" install \
&& meson "/usr/local/src/libfuse-fuse-$FUSE_VERSION" \
&& ninja \
&& ninja install \
&& pip3 uninstall -y meson \
&& rm -rf "$builddir" \
&& apt purge -y $builddeps
ARG WEBSOCKETS_VERSION=3.1.0
ARG WEBSOCKETS_VERSION=3.2.0
RUN set -x \
&& apt install --yes --no-install-recommends \
@ -104,7 +105,3 @@ RUN set -x \
WORKDIR "$OUTDIR"
ENTRYPOINT ["dumb-init", "--"]
# unused
ARG QEMU_VERSION_=v3.1.0-2

View File

@ -3,7 +3,7 @@ ARG CODENAME=3.9
FROM ${REGISTRY_PREFIX}arm32v7/alpine:${CODENAME} as builder
ARG QEMU_VERSION_=v3.1.0-2
ARG QEMU_VERSION_=v4.1.0-1
COPY docker/qemu-arm-static-$QEMU_VERSION_ /usr/bin/qemu-arm-static
@ -44,7 +44,7 @@ RUN set -x \
&& rm -rf "$builddir" \
&& apk del .build-deps
ARG GTEST_VERSION=1.8.1
ARG GTEST_VERSION=1.10.0
RUN set -x \
&& builddir="/tmp/out" \
@ -54,22 +54,24 @@ RUN set -x \
&& make "$PARALLELMFLAGS" install \
&& rm -rf "$builddir"
ARG FUSE_VERSION=3.1.1
ARG FUSE_VERSION=3.8.0
RUN set -x \
&& builddeps="libtool automake autoconf gettext-dev m4 linux-headers" \
&& builddeps="linux-headers eudev-dev python3 py3-pip py3-setuptools py3-cryptography" \
&& apk add --no-cache --virtual .build-deps $builddeps \
&& cd "/usr/local/src/libfuse-fuse-$FUSE_VERSION" \
&& ./makeconf.sh \
&& pip3 install meson \
&& builddir="/tmp/out" \
&& mkdir -p "$builddir" \
&& cd "$builddir" \
&& "/usr/local/src/libfuse-fuse-$FUSE_VERSION/configure" \
&& make "$PARALLELMFLAGS" install \
&& meson "/usr/local/src/libfuse-fuse-$FUSE_VERSION" \
&& meson configure -Dexamples=false \
&& ninja \
&& ninja install \
&& pip3 uninstall -y meson \
&& rm -rf "$builddir" \
&& apk del .build-deps
ARG WEBSOCKETS_VERSION=3.1.0
ARG WEBSOCKETS_VERSION=3.2.0
RUN set -x \
&& apk add --no-cache \

View File

@ -3,7 +3,7 @@ ARG CODENAME=bionic
FROM ${REGISTRY_PREFIX}arm32v7/ubuntu:${CODENAME} as builder
ARG QEMU_VERSION_=v3.1.0-2
ARG QEMU_VERSION_=v4.1.0-1
COPY docker/qemu-arm-static-$QEMU_VERSION_ /usr/bin/qemu-arm-static
@ -42,7 +42,7 @@ RUN set -x \
&& rm -rf "$builddir" \
&& apt purge -y $builddeps
ARG GTEST_VERSION=1.8.1
ARG GTEST_VERSION=1.10.0
RUN set -x \
&& builddir="/tmp/out" \
@ -52,22 +52,23 @@ RUN set -x \
&& make "$PARALLELMFLAGS" install \
&& rm -rf "$builddir"
ARG FUSE_VERSION=3.1.1
ARG FUSE_VERSION=3.8.0
RUN set -x \
&& builddeps="libtool automake gettext" \
&& builddeps="udev gettext python3 python3-pip python3-setuptools python3-wheel" \
&& apt install --yes --no-install-recommends $builddeps \
&& cd "/usr/local/src/libfuse-fuse-$FUSE_VERSION" \
&& ./makeconf.sh \
&& pip3 install --system meson \
&& builddir="/tmp/out" \
&& mkdir -p "$builddir" \
&& cd "$builddir" \
&& "/usr/local/src/libfuse-fuse-$FUSE_VERSION/configure" \
&& make "$PARALLELMFLAGS" install \
&& meson "/usr/local/src/libfuse-fuse-$FUSE_VERSION" \
&& ninja \
&& ninja install \
&& pip3 uninstall -y meson \
&& rm -rf "$builddir" \
&& apt purge -y $builddeps
ARG WEBSOCKETS_VERSION=3.1.0
ARG WEBSOCKETS_VERSION=3.2.0
RUN set -x \
&& apt install --yes --no-install-recommends \

30
cmake/coverage.cmake Normal file
View File

@ -0,0 +1,30 @@
set(CMAKE_C_FLAGS_COVERAGE
"${CMAKE_C_FLAGS_DEBUG} -pg --coverage -fprofile-arcs -ftest-coverage"
CACHE STRING "Flags used by the C compiler during coverage builds"
FORCE
)
set(CMAKE_CXX_FLAGS_COVERAGE
"${CMAKE_CXX_FLAGS_DEBUG} -pg --coverage -fprofile-arcs -ftest-coverage"
CACHE STRING "Flags used by the C++ compiler during coverage builds."
FORCE
)
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used for linking binaries during coverage builds."
FORCE
)
set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used by the shared libraries linker during coverage builds."
FORCE
)
mark_as_advanced(
CMAKE_C_FLAGS_COVERAGE
CMAKE_CXX_FLAGS_COVERAGE
CMAKE_EXE_LINKER_FLAGS_COVERAGE
CMAKE_SHARED_LINKER_FLAGS_COVERAGE
)

2
codecov.yml Normal file
View File

@ -0,0 +1,2 @@
ignore:
- test

View File

@ -4,9 +4,9 @@
#include <string.h>
#include <limits.h>
#include <jansson.h>
#include <libwebsockets.h>
#include "webfuse/adapter/impl/jsonrpc/proxy.h"
#include "webfuse/core/base64.h"
#define WF_MAX_READ_LENGTH 4096
@ -17,7 +17,7 @@ static char * wf_impl_fill_buffer(
wf_status * status)
{
*status = WF_GOOD;
char * buffer = malloc(count + 1);
char * buffer = malloc(count);
if ((NULL != buffer) && (0 < count))
{
@ -27,7 +27,7 @@ static char * wf_impl_fill_buffer(
}
else if (0 == strcmp("base64", format))
{
lws_b64_decode_string(data, buffer, count + 1);
wf_base64_decode(data, strlen(data), (uint8_t *) buffer, count);
}
else
{

183
lib/webfuse/core/base64.c Normal file
View File

@ -0,0 +1,183 @@
#include "webfuse/core/base64.h"
static const uint8_t wf_base64_decode_table[256] = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 1
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 62, 0x80, 0x80, 0x80, 63, // 2
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0x80, 0x80, 0x80, 0, 0x80, 0x80, // 3
0x80, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 4
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0x80, 0x80, 0x80, 0x80, 0x80, // 5
0x80, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 6
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0x80, 0x80, 0x80, 0x80, 0x80, // 7
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 8
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 9
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // A
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // B
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // C
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // D
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // E
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // F
};
size_t wf_base64_encoded_size(size_t length)
{
return ((length + 2) / 3) * 4;
}
size_t wf_base64_encode(
uint8_t const * data,
size_t length,
char * buffer,
size_t buffer_size)
{
// 0 1 2 3 4 5 6
// 0123456789012345678901234567890123456789012345678901234567890123
static char const table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t const length_needed = wf_base64_encoded_size(length);
if (buffer_size < length_needed)
{
return 0;
}
size_t pos = 0;
size_t out_pos = 0;
for(; (length - pos) >= 3; pos += 3)
{
buffer[out_pos++] = table[ data[pos] >> 2 ];
buffer[out_pos++] = table[ ((data[pos ] & 0x03) << 4) | (data[pos + 1] >> 4) ];
buffer[out_pos++] = table[ ((data[pos + 1] & 0x0f) << 2) | (data[pos + 2] >> 6) ];
buffer[out_pos++] = table[ data[pos + 2] & 0x3f ];
}
switch((length - pos))
{
case 1:
buffer[out_pos++] = table[ data[pos] >> 2 ];
buffer[out_pos++] = table[ ((data[pos] & 0x03) << 4) ];
buffer[out_pos++] = '=';
buffer[out_pos++] = '=';
break;
case 2:
buffer[out_pos++] = table[ data[pos] >> 2 ];
buffer[out_pos++] = table[ ((data[pos ] & 0x03) << 4) | (data[pos + 1] >> 4) ];
buffer[out_pos++] = table[ ((data[pos + 1] & 0x0f) << 2) ];
buffer[out_pos++] = '=';
break;
default:
break;
}
if (buffer_size > out_pos)
{
buffer[out_pos] = '\0';
}
return out_pos;
}
size_t wf_base64_decoded_size(char const * data, size_t length)
{
size_t result = 0;
if ((length > 0) && ((length % 4) == 0))
{
result = (length / 4) * 3;
if ('=' == data[length - 1])
{
result--;
if ('=' == data[length - 2])
{
result--;
}
}
}
return result;
}
size_t wf_base64_decode(
char const * data,
size_t length,
uint8_t * buffer,
size_t buffer_size)
{
uint8_t const * table = wf_base64_decode_table;
size_t needed_size = wf_base64_decoded_size(data, length);
if ((0 == needed_size) || (buffer_size < needed_size))
{
return 0;
}
size_t out_pos = 0;
size_t pos = 0;
for(; pos < length - 4; pos += 4)
{
uint8_t a = table[ (unsigned char) data[pos ] ];
uint8_t b = table[ (unsigned char) data[pos + 1] ];
uint8_t c = table[ (unsigned char) data[pos + 2] ];
uint8_t d = table[ (unsigned char) data[pos + 3] ];
buffer[out_pos++] = (a << 2) | (b >> 4);
buffer[out_pos++] = (b << 4) | (c >> 2);
buffer[out_pos++] = (c << 6) | d;
}
// decode last block
{
uint8_t a = table[ (unsigned char) data[pos ] ];
uint8_t b = table[ (unsigned char) data[pos + 1] ];
uint8_t c = table[ (unsigned char) data[pos + 2] ];
uint8_t d = table[ (unsigned char) data[pos + 3] ];
buffer[out_pos++] = (a << 2) | (b >> 4);
if ('=' != data[pos + 2])
{
buffer[out_pos++] = (b << 4) | (c >> 2);
if ('=' != data[pos + 3])
{
buffer[out_pos++] = (c << 6) | d;
}
}
}
return out_pos;
}
extern bool wf_base64_isvalid(char const * data, size_t length)
{
uint8_t const * table = wf_base64_decode_table;
if ((length == 0) || ((length % 4) != 0))
{
return false;
}
size_t pos = 0;
for(; pos < (length - 2); pos++)
{
unsigned char c = (unsigned char) data[pos];
if (('=' == c) || (0x80 == table[c]))
{
return false;
}
}
if (('=' == data[pos]) && ('=' != data[pos + 1]))
{
return false;
}
for(;pos < length; pos++)
{
char c = data[pos];
if (0x80 == table[ (unsigned char) c])
{
return false;
}
}
return true;
}

40
lib/webfuse/core/base64.h Normal file
View File

@ -0,0 +1,40 @@
#ifndef WF_BASE64_H
#define WF_BASE64_H
#ifndef __cplusplus
#include <inttypes.h>
#include <stddef.h>
#include <stdbool.h>
#else
#include <cinttypes>
#include <cstddef>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
extern size_t wf_base64_encoded_size(size_t length);
extern size_t wf_base64_encode(
uint8_t const * data,
size_t length,
char * buffer,
size_t buffer_size);
extern size_t wf_base64_decoded_size(char const * data, size_t length);
extern size_t wf_base64_decode(
char const * data,
size_t length,
uint8_t * buffer,
size_t buffer_size);
extern bool wf_base64_isvalid(char const * data, size_t length);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,11 +1,11 @@
#include "webfuse/provider/impl/operation/read.h"
#include <stdlib.h>
#include <libwebsockets.h>
#include "webfuse/provider/impl/operation/error.h"
#include "webfuse/provider/impl/request.h"
#include "webfuse/core/util.h"
#include "webfuse/core/base64.h"
void wfp_impl_read(
struct wfp_impl_invokation_context * context,
@ -54,11 +54,11 @@ void wfp_impl_respond_read(
{
if (0 < length)
{
size_t const size = 4 * ((length / 3) + 2);
size_t const size = wf_base64_encoded_size(length) + 1;
char * buffer = malloc(size);
if (NULL != buffer)
{
lws_b64_encode_string(data, length, buffer, size);
wf_base64_encode((uint8_t const *) data, length, buffer, size);
json_t * result = json_object();
json_object_set_new(result, "data", json_string(buffer));

122
test/core/test_base64.cc Normal file
View File

@ -0,0 +1,122 @@
#include <gtest/gtest.h>
#include "webfuse/core/base64.h"
TEST(Base64, EncodedSize)
{
ASSERT_EQ(4, wf_base64_encoded_size(1));
ASSERT_EQ(4, wf_base64_encoded_size(2));
ASSERT_EQ(4, wf_base64_encoded_size(3));
ASSERT_EQ(8, wf_base64_encoded_size(4));
ASSERT_EQ(8, wf_base64_encoded_size(5));
ASSERT_EQ(8, wf_base64_encoded_size(6));
ASSERT_EQ(120, wf_base64_encoded_size(90));
}
TEST(Base64, Encode)
{
char buffer[42];
std::string in = "Hello";
size_t length = wf_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
ASSERT_EQ(8, length);
ASSERT_STREQ("SGVsbG8=", buffer);
in = "Hello\n";
length = wf_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
ASSERT_EQ(8, length);
ASSERT_STREQ("SGVsbG8K", buffer);
in = "Blue";
length = wf_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
ASSERT_EQ(8, length);
ASSERT_STREQ("Qmx1ZQ==", buffer);
}
TEST(Base64, FailedToEncodeBufferTooSmall)
{
char buffer[1];
std::string in = "Hello";
size_t length = wf_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 1);
ASSERT_EQ(0, length);
}
TEST(Base64, DecodedSize)
{
std::string in = "SGVsbG8="; // Hello
size_t length = wf_base64_decoded_size(in.c_str(), in.size());
ASSERT_EQ(5, length);
in = "SGVsbG8K"; // Hello\n
length = wf_base64_decoded_size(in.c_str(), in.size());
ASSERT_EQ(6, length);
in = "Qmx1ZQ=="; // Blue
length = wf_base64_decoded_size(in.c_str(), in.size());
ASSERT_EQ(4, length);
}
TEST(Base64, IsValid)
{
std::string in = "SGVsbG8="; // Hello
ASSERT_TRUE(wf_base64_isvalid(in.c_str(), in.size()));
in = "SGVsbG8K"; // Hello\n
ASSERT_TRUE(wf_base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ=="; // Blue
ASSERT_TRUE(wf_base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ=a";
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ";
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ=";
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
in = "Qmx1Z===";
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ?=";
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
in = "Qm?1ZQ==";
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
}
TEST(Base64, Decode)
{
char buffer[42];
std::string in = "SGVsbG8="; // Hello
size_t length = wf_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
ASSERT_EQ(5, length);
buffer[length] = '\0';
ASSERT_STREQ("Hello", buffer);
in = "SGVsbG8K"; // Hello\n
length = wf_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
ASSERT_EQ(6, length);
buffer[length] = '\0';
ASSERT_STREQ("Hello\n", buffer);
in = "Qmx1ZQ=="; // Blue
length = wf_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
ASSERT_EQ(4, length);
buffer[length] = '\0';
ASSERT_STREQ("Blue", buffer);
}
TEST(Base64, FailToDecodeBufferTooSmall)
{
char buffer[1];
std::string in = "SGVsbG8="; // Hello
size_t length = wf_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 1);
ASSERT_EQ(0, length);
}