pax_global_header00006660000000000000000000000064141630127000014504gustar00rootroot0000000000000052 comment=54968906337eccdd7901ced1d51dd1e72160c3dc libbaseencode-1.0.12/000077500000000000000000000000001416301270000143445ustar00rootroot00000000000000libbaseencode-1.0.12/.gitignore000066400000000000000000000004351416301270000163360ustar00rootroot00000000000000.idea/ cmake-build-debug/ build/ # Object files *.o *.ko *.obj *.elf # Precompiled Headers *.gch *.pch # Libraries *.lib *.a *.la *.lo # Shared objects (inc. Windows DLLs) *.dll *.so *.so.* *.dylib # Executables *.exe *.out *.app *.i*86 *.x86_64 *.hex # Debug files *.dSYM/ *.su libbaseencode-1.0.12/CMakeLists.txt000066400000000000000000000020631416301270000171050ustar00rootroot00000000000000cmake_minimum_required(VERSION 3.5) project(baseencode) enable_testing() add_subdirectory(tests) # set up versioning. set(BUILD_MAJOR "1") set(BUILD_MINOR "0") set(BUILD_VERSION "12") set(BUILD_VERSION ${BUILD_MAJOR}.${BUILD_MINOR}.${BUILD_VERSION}) set(CMAKE_C_STANDARD 11) set(BASEENCODE_HEADERS src/baseencode.h) set(SOURCE_FILES src/base32.c src/base64.c) set(CMAKE_C_FLAGS "-Wall -Werror -fPIC") add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES}) target_link_libraries(${PROJECT_NAME} ${PROJECT_LIBS}) set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${BUILD_VERSION} SOVERSION ${BUILD_MAJOR}) include(GNUInstallDirs) set(LIB_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}") # "lib" or "lib64" install( TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION ${LIB_INSTALL_DIR} LIBRARY DESTINATION ${LIB_INSTALL_DIR} COMPONENT library ) install( FILES ${BASEENCODE_HEADERS} DESTINATION include ) configure_file("baseencode.pc.in" "baseencode.pc" @ONLY) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/baseencode.pc" DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig/) libbaseencode-1.0.12/LICENSE000066400000000000000000000261201416301270000153520ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2018 Paolo Stivanin 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. libbaseencode-1.0.12/README.md000066400000000000000000000024621416301270000156270ustar00rootroot00000000000000# libbaseencode Coverity Scan Build Status Library written in C for encoding and decoding data using base32 or base64 according to [RFC-4648](https://tools.ietf.org/html/rfc4648) # Requiremens - GCC or Clang - CMake # Build and Install ``` $ git clone https://github.com/paolostivanin/libbaseencode.git $ cd libbaseencode $ mkdir build && cd $_ $ cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr ../ $ make # make install ``` # How To Use It ``` char *b32_encoded = base32_encode(unsigned char *input, size_t input_length, baseencode_error_t *err); unsigned char *b32_decoded = base32_decode(char *input, size_t input_length, baseencode_error_t *err); char *b64_encoded = base64_encode(unsigned char *input, size_t input_length, baseencode_error_t *err); unsigned char *b64_decoded = base64_decode(char *input, size_t input_length, baseencode_error_t *err); ``` Please note that all the returned value **must be freed** once not needed any more. ## Errors In case of errors, `NULL` is returned and `err` is set to either one of: ``` INVALID_INPUT, EMPTY_STRING, INPUT_TOO_BIG, INVALID_B32_DATA, INVALID_B64_DATA, MEMORY_ALLOCATION, ``` otherwise, `err` is set to `SUCCESS` libbaseencode-1.0.12/baseencode.pc.in000066400000000000000000000004761416301270000173740ustar00rootroot00000000000000prefix=@CMAKE_INSTALL_PREFIX@ exec_prefix=${prefix} libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@ includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ Name: libbaseencode Description: C library for encoding and decoding data using base32 or base64 Version: @BUILD_VERSION@ Libs: -L${libdir} -lbaseencode Cflags: -I${includedir} libbaseencode-1.0.12/src/000077500000000000000000000000001416301270000151335ustar00rootroot00000000000000libbaseencode-1.0.12/src/base32.c000066400000000000000000000146031416301270000163620ustar00rootroot00000000000000#include #include #include #include #include "common.h" static int is_valid_b32_input(const char *user_data, size_t data_len); static int get_char_index(unsigned char c); static const unsigned char b32_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; // The encoding process represents 40-bit groups of input bits as output strings of 8 encoded characters. The input data must be null terminated. char * base32_encode(const unsigned char *user_data, size_t data_len, baseencode_error_t *err) { baseencode_error_t error; check_input(user_data, data_len, MAX_ENCODE_INPUT_LEN, &error); if (error != SUCCESS) { *err = error; if (error == EMPTY_STRING) { return strdup(""); } else { return NULL; } } size_t user_data_chars = 0, total_bits = 0; int num_of_equals = 0; for (int i = 0; i < data_len; i++) { // As it's not known whether data_len is with or without the +1 for the null byte, a manual check is required. // Check for null byte only at the end of the user given length, otherwise issue#23 may occur if (user_data[i] == '\0' && i == data_len-1) { break; } else { total_bits += 8; user_data_chars += 1; } } switch (total_bits % 40) { case 8: num_of_equals = 6; break; case 16: num_of_equals = 4; break; case 24: num_of_equals = 3; break; case 32: num_of_equals = 1; break; default: break; } size_t output_length = (user_data_chars * 8 + 4) / 5; char *encoded_data = calloc(output_length + num_of_equals + 1, 1); if (encoded_data == NULL) { *err = MEMORY_ALLOCATION; return NULL; } uint64_t first_octet, second_octet, third_octet, fourth_octet, fifth_octet; uint64_t quintuple; for (int i = 0, j = 0; i < user_data_chars;) { first_octet = i < user_data_chars ? user_data[i++] : 0; second_octet = i < user_data_chars ? user_data[i++] : 0; third_octet = i < user_data_chars ? user_data[i++] : 0; fourth_octet = i < user_data_chars ? user_data[i++] : 0; fifth_octet = i < user_data_chars ? user_data[i++] : 0; quintuple = ((first_octet >> 3) << 35) + ((((first_octet & 0x7) << 2) | (second_octet >> 6)) << 30) + (((second_octet & 0x3F) >> 1) << 25) + ((((second_octet & 0x01) << 4) | (third_octet >> 4)) << 20) + ((((third_octet & 0xF) << 1) | (fourth_octet >> 7)) << 15) + (((fourth_octet & 0x7F) >> 2) << 10) + ((((fourth_octet & 0x3) << 3) | (fifth_octet >> 5)) << 5) + (fifth_octet & 0x1F); encoded_data[j++] = b32_alphabet[(quintuple >> 35) & 0x1F]; encoded_data[j++] = b32_alphabet[(quintuple >> 30) & 0x1F]; encoded_data[j++] = b32_alphabet[(quintuple >> 25) & 0x1F]; encoded_data[j++] = b32_alphabet[(quintuple >> 20) & 0x1F]; encoded_data[j++] = b32_alphabet[(quintuple >> 15) & 0x1F]; encoded_data[j++] = b32_alphabet[(quintuple >> 10) & 0x1F]; encoded_data[j++] = b32_alphabet[(quintuple >> 5) & 0x1F]; encoded_data[j++] = b32_alphabet[(quintuple >> 0) & 0x1F]; } for (int i = 0; i < num_of_equals; i++) { encoded_data[output_length + i] = '='; } encoded_data[output_length + num_of_equals] = '\0'; *err = SUCCESS; return encoded_data; } unsigned char * base32_decode(const char *user_data_untrimmed, size_t data_len, baseencode_error_t *err) { baseencode_error_t error; check_input((unsigned char *)user_data_untrimmed, data_len, MAX_DECODE_BASE32_INPUT_LEN, &error); if (error != SUCCESS) { *err = error; if (error == EMPTY_STRING) { return (unsigned char *) strdup(""); } else { return NULL; } } char *user_data = strdup(user_data_untrimmed); data_len -= strip_char(user_data, ' '); if (!is_valid_b32_input(user_data, data_len)) { *err = INVALID_B32_DATA; free(user_data); return NULL; } size_t user_data_chars = 0; for (int i = 0; i < data_len; i++) { // As it's not known whether data_len is with or without the +1 for the null byte, a manual check is required. if (user_data[i] != '=' && user_data[i] != '\0') { user_data_chars += 1; } } size_t output_length = (size_t) ((user_data_chars + 1.6 + 1) / 1.6); // round up unsigned char *decoded_data = calloc(output_length + 1, 1); if (decoded_data == NULL) { *err = MEMORY_ALLOCATION; free(user_data); return NULL; } uint8_t mask = 0, current_byte = 0; int bits_left = 8; for (int i = 0, j = 0; i < user_data_chars; i++) { int char_index = get_char_index((unsigned char)user_data[i]); if (bits_left > BITS_PER_B32_BLOCK) { mask = (uint8_t) char_index << (bits_left - BITS_PER_B32_BLOCK); current_byte = (uint8_t) (current_byte | mask); bits_left -= BITS_PER_B32_BLOCK; } else { mask = (uint8_t) char_index >> (BITS_PER_B32_BLOCK - bits_left); current_byte = (uint8_t) (current_byte | mask); decoded_data[j++] = current_byte; current_byte = (uint8_t) (char_index << (BITS_PER_BYTE - BITS_PER_B32_BLOCK + bits_left)); bits_left += BITS_PER_BYTE - BITS_PER_B32_BLOCK; } } decoded_data[output_length] = '\0'; free(user_data); *err = SUCCESS; return decoded_data; } static int is_valid_b32_input(const char *user_data, size_t data_len) { size_t found = 0, b32_alphabet_len = sizeof(b32_alphabet); for (int i = 0; i < data_len; i++) { if (user_data[i] == '\0') { found++; break; } for(int j = 0; j < b32_alphabet_len; j++) { if(user_data[i] == b32_alphabet[j] || user_data[i] == '=') { found++; break; } } } if (found != data_len) { return 0; } else { return 1; } } static int get_char_index(unsigned char c) { for (int i = 0; i < sizeof(b32_alphabet); i++) { if (b32_alphabet[i] == c) { return i; } } return -1; } libbaseencode-1.0.12/src/base64.c000066400000000000000000000121441416301270000163650ustar00rootroot00000000000000#include #include #include #include #include #include "common.h" static int is_valid_b64_input(const char *user_data, size_t data_len); static int get_char_index(unsigned char c); static const unsigned char b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; char * base64_encode(const unsigned char *user_data, size_t data_len, baseencode_error_t *err) { baseencode_error_t error; check_input(user_data, data_len, MAX_ENCODE_INPUT_LEN, &error); if (error != SUCCESS) { *err = error; if (error == EMPTY_STRING) { return strdup(""); } else { return NULL; } } size_t user_data_chars = 0, total_bits = 0; int num_of_equals = 0; for (int i = 0; i < data_len; i++) { // As it's not known whether data_len is with or without the +1 for the null byte, a manual check is required. if (user_data[i] != '\0') { total_bits += 8; user_data_chars += 1; } else { break; } } switch (total_bits % 24) { case 8: num_of_equals = 2; break; case 16: num_of_equals = 1; break; default: break; } size_t output_length = (user_data_chars * 8 + 4) / 6; char *encoded_data = calloc(output_length + num_of_equals + 1 + 3, 1); if (encoded_data == NULL) { *err = MEMORY_ALLOCATION; return NULL; } uint8_t first_octet, second_octet, third_octet; for (int i = 0, j = 0, triple = 0; i < user_data_chars + 1;) { first_octet = (uint8_t) (i < user_data_chars+1 ? user_data[i++] : 0); second_octet = (uint8_t) (i < user_data_chars+1 ? user_data[i++] : 0); third_octet = (uint8_t) (i < user_data_chars+1 ? user_data[i++] : 0); triple = (first_octet << 0x10) + (second_octet << 0x08) + third_octet; encoded_data[j++] = b64_alphabet[(triple >> 0x12) & 0x3F]; encoded_data[j++] = b64_alphabet[(triple >> 0x0C) & 0x3F]; encoded_data[j++] = b64_alphabet[(triple >> 0x06) & 0x3F]; encoded_data[j++] = b64_alphabet[(triple >> 0x00) & 0x3F]; } for (int i = 0; i < num_of_equals; i++) { encoded_data[output_length + i] = '='; } encoded_data[output_length + num_of_equals] = '\0'; *err = SUCCESS; return encoded_data; } unsigned char * base64_decode(const char *user_data_untrimmed, size_t data_len, baseencode_error_t *err) { baseencode_error_t error; check_input((unsigned char *)user_data_untrimmed, data_len, MAX_DECODE_BASE64_INPUT_LEN, &error); if (error != SUCCESS) { *err = error; if (error == EMPTY_STRING) { return (unsigned char *) strdup(""); } else { return NULL; } } char *user_data = strdup(user_data_untrimmed); data_len -= strip_char(user_data, ' '); if (!is_valid_b64_input(user_data, data_len)) { *err = INVALID_B64_DATA; free(user_data); return NULL; } size_t user_data_chars = 0; for (int z = 0; z < data_len; z++) { // As it's not known whether data_len is with or without the +1 for the null byte, a manual check is required. if (user_data[z] != '=' && user_data[z] != '\0') { user_data_chars += 1; } } size_t output_length = data_len / 4 * 3; unsigned char *decoded_data = calloc(output_length + 1, 1); if (decoded_data == NULL) { *err = MEMORY_ALLOCATION; free(user_data); return NULL; } uint8_t mask = 0, current_byte = 0; int bits_left = 8; for (int i = 0, j = 0; i < user_data_chars; i++) { int char_index = get_char_index((unsigned char)user_data[i]); if (bits_left > BITS_PER_B64_BLOCK) { mask = (uint8_t) char_index << (bits_left - BITS_PER_B64_BLOCK); current_byte = (uint8_t) (current_byte | mask); bits_left -= BITS_PER_B64_BLOCK; } else { mask = (uint8_t) char_index >> (BITS_PER_B64_BLOCK - bits_left); current_byte = (uint8_t) (current_byte | mask); decoded_data[j++] = current_byte; current_byte = (uint8_t) (char_index << (BITS_PER_BYTE - BITS_PER_B64_BLOCK + bits_left)); bits_left += BITS_PER_BYTE - BITS_PER_B64_BLOCK; } } decoded_data[output_length] = '\0'; free(user_data); *err = SUCCESS; return decoded_data; } static int is_valid_b64_input(const char *user_data, size_t data_len) { size_t found = 0, b64_alphabet_len = sizeof(b64_alphabet); for (int i = 0; i < data_len; i++) { for(int j = 0; j < b64_alphabet_len; j++) { if(user_data[i] == b64_alphabet[j] || user_data[i] == '=') { found++; break; } } } if (found != data_len) { return 0; } else { return 1; } } static int get_char_index(unsigned char c) { for (int i = 0; i < sizeof(b64_alphabet); i++) { if (b64_alphabet[i] == c) { return i; } } return -1; } libbaseencode-1.0.12/src/baseencode.h000066400000000000000000000017431416301270000174010ustar00rootroot00000000000000#pragma once typedef enum _baseencode_errno { SUCCESS = 0, INVALID_INPUT = 1, EMPTY_STRING = 2, INPUT_TOO_BIG = 3, INVALID_B32_DATA = 4, INVALID_B64_DATA = 5, MEMORY_ALLOCATION = 6, } baseencode_error_t; char *base32_encode (const unsigned char *user_data, size_t data_len, baseencode_error_t *err); unsigned char *base32_decode (const char *user_data, size_t data_len, baseencode_error_t *err); char *base64_encode (const unsigned char *input_string, size_t input_length, baseencode_error_t *err); unsigned char *base64_decode (const char *input_string, size_t input_length, baseencode_error_t *err);libbaseencode-1.0.12/src/common.h000066400000000000000000000022551416301270000166000ustar00rootroot00000000000000#pragma once #include "baseencode.h" #define BITS_PER_BYTE 8 #define BITS_PER_B32_BLOCK 5 #define BITS_PER_B64_BLOCK 6 // 64 MB should be more than enough #define MAX_ENCODE_INPUT_LEN 64*1024*1024 // if 64 MB of data is encoded than it should be also possible to decode it. That's why a bigger input is allowed for decoding #define MAX_DECODE_BASE32_INPUT_LEN ((MAX_ENCODE_INPUT_LEN * 8 + 4) / 5) #define MAX_DECODE_BASE64_INPUT_LEN ((MAX_ENCODE_INPUT_LEN * 8 + 4) / 6) static int strip_char(char *str, char strip) { int found = 0; char *p, *q; for (q = p = str; *p; p++) { if (*p != strip) { *q++ = *p; } else { found++; } } *q = '\0'; return found; } static void check_input(const unsigned char *user_data, size_t data_len, int max_len, baseencode_error_t *err) { if (user_data == NULL || (data_len == 0 && user_data[0] != '\0')) { *err = INVALID_INPUT; return; } else if (user_data[0] == '\0') { *err = EMPTY_STRING; return; } if (data_len > max_len) { *err = INPUT_TOO_BIG; return; } *err = SUCCESS; }libbaseencode-1.0.12/tests/000077500000000000000000000000001416301270000155065ustar00rootroot00000000000000libbaseencode-1.0.12/tests/CMakeLists.txt000066400000000000000000000004321416301270000202450ustar00rootroot00000000000000 IF(BUILD_TESTING) add_executable (test_all ../src/base32.c ../src/base64.c test-base32-encode.c test-base32-decode.c test-base64-encode.c test-base64-decode.c) target_link_libraries (test_all -lcriterion) add_test (NAME TestAll COMMAND test_all) ENDIF(BUILD_TESTING) libbaseencode-1.0.12/tests/docker/000077500000000000000000000000001416301270000167555ustar00rootroot00000000000000libbaseencode-1.0.12/tests/docker/Dockerfile000066400000000000000000000015001416301270000207430ustar00rootroot00000000000000FROM archlinux:latest ARG BRANCH=master COPY PKGBUILD /tmp/PKGBUILD RUN pacman -Syu --noconfirm ; \ pacman -S gcc git clang cmake pkg-config libgcrypt fakeroot sudo --noconfirm ; \ pacman -S base-devel --noconfirm RUN useradd -m -G wheel -s /bin/bash test ; \ cp /tmp/PKGBUILD /home/test/ && chown test:test /home/test/PKGBUILD ; \ sed -i 's/# %wheel ALL=(ALL) ALL/%wheel ALL=(ALL) NOPASSWD: ALL/' /etc/sudoers USER test RUN cd /home/test && makepkg USER root RUN pacman -U /home/test/*zst --noconfirm USER test RUN yay -S criterion --noconfirm USER root RUN git clone https://github.com/paolostivanin/libbaseencode -b $BRANCH ; \ cd libbaseencode ; \ mkdir build && cd $_ ; \ cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_TESTING=ON ; \ make -j2 ;\ ./tests/test_all ;\ make install libbaseencode-1.0.12/tests/docker/PKGBUILD000066400000000000000000000035601416301270000201050ustar00rootroot00000000000000# Maintainer: Jguer pkgname=yay-bin pkgver=11.0.2 pkgrel=1 pkgdesc="Yet another yogurt. Pacman wrapper and AUR helper written in go. Pre-compiled." arch=('x86_64' 'aarch64' 'armv6h' 'armv7h') url="https://github.com/Jguer/yay" license=('GPL3') depends=( 'git' ) optdepends=( 'sudo' ) provides=('yay') conflicts=('yay' 'libalpm.so<13') source_x86_64=("https://github.com/Jguer/yay/releases/download/v${pkgver}/${pkgname/-bin/}_${pkgver}_x86_64.tar.gz") source_aarch64=("https://github.com/Jguer/yay/releases/download/v${pkgver}/${pkgname/-bin/}_${pkgver}_aarch64.tar.gz") source_armv6h=("https://github.com/Jguer/yay/releases/download/v${pkgver}/${pkgname/-bin/}_${pkgver}_armv6h.tar.gz") source_armv7h=("https://github.com/Jguer/yay/releases/download/v${pkgver}/${pkgname/-bin/}_${pkgver}_armv7h.tar.gz") sha256sums_x86_64=('3b6334a4e719138c80f4c271a77b97bd740329677dfffe6bbe54679a0052140e') sha256sums_aarch64=('d1c1b2d74783cb366a86480fcea9d7fbb325d170815014642ac0790e9bad6637') sha256sums_armv6h=('7b4dd9d33d56b600955ead52bb5bd86bc9ac6fc45c24f0c6b686153b184eb905') sha256sums_armv7h=('80a6317383552272ca9a9d251531fb2ca6e310e811b034a680e6632d4a294f15') package() { _output="${srcdir}/${pkgname/-bin/}_${pkgver}_${CARCH}" install -Dm755 "${_output}/${pkgname/-bin/}" "${pkgdir}/usr/bin/${pkgname/-bin/}" install -Dm644 "${_output}/yay.8" "${pkgdir}/usr/share/man/man8/yay.8" # Shell autocompletion script install -Dm644 "${_output}/bash" "${pkgdir}/usr/share/bash-completion/completions/yay" install -Dm644 "${_output}/zsh" "${pkgdir}/usr/share/zsh/site-functions/_yay" install -Dm644 "${_output}/fish" "${pkgdir}/usr/share/fish/vendor_completions.d/yay.fish" LANGS="pt pt_BR en es eu fr_FR ja pl_PL ru_RU zh_CN ko" for lang in ${LANGS}; do install -Dm644 "${_output}/${lang}.mo" "${pkgdir}/usr/share/locale/${lang}/LC_MESSAGES/yay.mo" done } libbaseencode-1.0.12/tests/docker/run_docker.sh000066400000000000000000000002241416301270000214420ustar00rootroot00000000000000#!/bin/bash if [[ -z "$1" ]]; then echo "Usage: $0 " exit 1 fi docker build -t "testme:Dockerfile" --build-arg BRANCH="$1" . libbaseencode-1.0.12/tests/test-base32-decode.c000066400000000000000000000055601416301270000211350ustar00rootroot00000000000000#include #include #include "../src/baseencode.h" Test(b32_decode_test, b32_all_chars) { baseencode_error_t err; const char *k = "IFCEMRZUGEZSDQVDEQSSMJRIFAXT6XWDU7B2SKS3LURSSLJOFR6DYPRL"; const char *k_dec = "ADFG413!£$%&&((/?^çé*[]#)-.,|<>+"; char *dk = base32_decode(k, strlen(k)+1, &err); cr_expect(strcmp(dk, k_dec) == 0, "Expected %s to be equal to %s", dk, k_dec); free(dk); } Test(b32_decode_test, b32_all_chars_noplusone) { baseencode_error_t err; const char *k = "IFCEMRZUGEZSDQVDEQSSMJRIFAXT6XWDU7B2SKS3LURSSLJOFR6DYPRL"; const char *k_dec = "ADFG413!£$%&&((/?^çé*[]#)-.,|<>+"; char *dk = base32_decode(k, strlen(k), &err); cr_expect(strcmp(dk, k_dec) == 0, "Expected %s to be equal to %s", dk, k_dec); free(dk); } Test(b32_decode_test, b32_rfc4648) { baseencode_error_t err; const char *k[] = {"", "MY======", "MZXQ====", "MZXW6===", "MZXW6YQ=", "MZXW6YTB", "MZXW6YTBOI======"}; const char *k_dec[] = {"", "f", "fo", "foo", "foob", "fooba", "foobar"}; for (int i = 0; i < 7; i++) { char *dk = base32_decode(k[i], strlen(k[i])+1, &err); cr_expect(strcmp(dk, k_dec[i]) == 0, "Expected %s to be equal to %s", dk, k_dec[i]); free(dk); } } Test(b32_decode_test, b32_rfc4648_noplusone) { baseencode_error_t err; const char *k[] = {"", "MY======", "MZXQ====", "MZXW6===", "MZXW6YQ=", "MZXW6YTB", "MZXW6YTBOI======"}; const char *k_dec[] = {"", "f", "fo", "foo", "foob", "fooba", "foobar"}; for (int i = 0; i < 7; i++) { char *dk = base32_decode(k[i], strlen(k[i]), &err); cr_expect(strcmp(dk, k_dec[i]) == 0, "Expected %s to be equal to %s", dk, k_dec[i]); free(dk); } } Test(b32_decode_test, b32_invalid_input) { baseencode_error_t err; const char *k = "£&/(&/"; size_t len = strlen(k); unsigned char *dk = base32_decode(k, len, &err); cr_expect_null(dk, "%s"); cr_expect_eq(err, INVALID_B32_DATA); } Test(b32_decode_test, b32_decode_input_exceeded) { baseencode_error_t err; const char *k = "ASDF"; size_t len = 128*1024*1024; unsigned char *dk = base32_decode(k, len, &err); cr_expect_null(dk, "%s"); cr_expect_eq(err, INPUT_TOO_BIG); } Test(b32_decode_test, b32_decode_input_whitespaces) { baseencode_error_t err; const char *k = "MZ XW 6Y TB"; const char *expected = "fooba"; unsigned char *dk = base32_decode(k, strlen(k), &err); cr_expect_str_eq(dk, expected, "%s"); } Test(b32_decode_test, b32_decode_encode_null) { const char* token = "LLFTSZYMUGKHEDQBAAACAZAMUFKKVFLS"; baseencode_error_t err; unsigned char* binary = base32_decode(token, strlen(token)+1, &err); cr_expect_eq(err, SUCCESS); char* result = base32_encode(binary, 20, &err); cr_expect_eq(err, SUCCESS); cr_expect_str_eq(result, token, "%s"); } libbaseencode-1.0.12/tests/test-base32-encode.c000066400000000000000000000051001416301270000211350ustar00rootroot00000000000000#include #include #include "../src/baseencode.h" Test(b32_encode_test, null_input) { baseencode_error_t err; const char *k = NULL; char *ek = base32_encode(k, 5, &err); cr_expect_null(ek, "%s"); } Test(b32_encode_test, data_nodata_size_nosize) { baseencode_error_t err; const char *k1 = ""; const char *k2 = "asdiasjdijis"; // test no-data with given size, data with no-size and no-data no-size char *ek1 = base32_encode(k1, 30, &err); char *ek2 = base32_encode(k2, 0, &err); cr_expect(strcmp(k1, ek1) == 0, "Expected %s to be equal to %s", ek1, k1); cr_expect_null(ek2, "%s"); free(ek1); } Test(b32_encode_test, b32_all_chars) { baseencode_error_t err; const char *k = "ADFG413!£$%&&((/?^çé*[]#)-.,|<>+"; const char *k_enc = "IFCEMRZUGEZSDQVDEQSSMJRIFAXT6XWDU7B2SKS3LURSSLJOFR6DYPRL"; char *ek = base32_encode(k, strlen(k)+1, &err); cr_expect(strcmp(ek, k_enc) == 0, "Expected %s to be equal to %s", ek, k_enc); free(ek); } Test(b32_encode_test, b32_all_chars_noplusone) { baseencode_error_t err; const char *k = "ADFG413!£$%&&((/?^çé*[]#)-.,|<>+"; const char *k_enc = "IFCEMRZUGEZSDQVDEQSSMJRIFAXT6XWDU7B2SKS3LURSSLJOFR6DYPRL"; char *ek = base32_encode(k, strlen(k), &err); cr_expect(strcmp(ek, k_enc) == 0, "Expected %s to be equal to %s", ek, k_enc); free(ek); } Test(b32_encode_test, b32_rfc4648) { baseencode_error_t err; const char *k[] = {"", "f", "fo", "foo", "foob", "fooba", "foobar"}; const char *k_enc[] = {"", "MY======", "MZXQ====", "MZXW6===", "MZXW6YQ=", "MZXW6YTB", "MZXW6YTBOI======"}; for (int i = 0; i < 7; i++) { char *ek = base32_encode(k[i], strlen(k[i])+1, &err); cr_expect(strcmp(ek, k_enc[i]) == 0, "Expected %s to be equal to %s", ek, k_enc[i]); free(ek); } } Test(b32_encode_test, b32_rfc4648_noplusone) { baseencode_error_t err; const char *k[] = {"", "f", "fo", "foo", "foob", "fooba", "foobar"}; const char *k_enc[] = {"", "MY======", "MZXQ====", "MZXW6===", "MZXW6YQ=", "MZXW6YTB", "MZXW6YTBOI======"}; for (int i = 0; i < 7; i++) { char *ek = base32_encode(k[i], strlen(k[i]), &err); cr_expect(strcmp(ek, k_enc[i]) == 0, "Expected %s to be equal to %s", ek, k_enc[i]); free(ek); } } Test(b32_encode_test, b32_encode_input_exceeded) { baseencode_error_t err; const char *k = "test"; size_t len = 65*1024*1024; char *ek = base32_encode(k, len, &err); cr_expect_null(ek, "%s"); cr_expect_eq(err, INPUT_TOO_BIG); }libbaseencode-1.0.12/tests/test-base64-decode.c000066400000000000000000000047021416301270000211370ustar00rootroot00000000000000#include #include #include "../src/baseencode.h" Test(b64_decode_test, b64_all_chars) { baseencode_error_t err; const char *k = "QURGRzQxMyHCoyQlJiYoKC8/XsOnw6kqW10jKS0uLHw8Pis="; const char *k_dec = "ADFG413!£$%&&((/?^çé*[]#)-.,|<>+"; char *dk = base64_decode(k, strlen(k)+1, &err); cr_expect(strcmp(dk, k_dec) == 0, "Expected %s to be equal to %s", dk, k_dec); free(dk); } Test(b64_decode_test, b64_all_chars_noplusone) { baseencode_error_t err; const char *k = "QURGRzQxMyHCoyQlJiYoKC8/XsOnw6kqW10jKS0uLHw8Pis="; const char *k_dec = "ADFG413!£$%&&((/?^çé*[]#)-.,|<>+"; char *dk = base64_decode(k, strlen(k), &err); cr_expect(strcmp(dk, k_dec) == 0, "Expected %s to be equal to %s", dk, k_dec); free(dk); } Test(b64_decode_test, b64_rfc4648) { baseencode_error_t err; const char *k[] = {"", "Zg==" ,"Zm8=", "Zm9v", "Zm9vYg==" ,"Zm9vYmE=", "Zm9vYmFy"}; const char *k_dec[] = {"", "f", "fo", "foo", "foob", "fooba", "foobar"}; for (int i = 0; i < 7; i++) { char *dk = base64_decode(k[i], strlen(k[i])+1, &err); cr_expect(strcmp(dk, k_dec[i]) == 0, "Expected %s to be equal to %s", dk, k_dec[i]); free(dk); } } Test(b64_decode_test, b64_rfc4648_noplusone) { baseencode_error_t err; const char *k[] = {"", "Zg==" ,"Zm8=", "Zm9v", "Zm9vYg==" ,"Zm9vYmE=", "Zm9vYmFy"}; const char *k_dec[] = {"", "f", "fo", "foo", "foob", "fooba", "foobar"}; for (int i = 0; i < 7; i++) { char *dk = base64_decode(k[i], strlen(k[i]), &err); cr_expect(strcmp(dk, k_dec[i]) == 0, "Expected %s to be equal to %s", dk, k_dec[i]); free(dk); } } Test(b64_decode_test, b64_invalid_input) { baseencode_error_t err; const char *k = "£&/(&/"; size_t len = strlen(k); unsigned char *dk = base64_decode(k, len, &err); cr_expect_null(dk, "%s"); cr_expect_eq(err, INVALID_B64_DATA); } Test(b64_decode_test, b64_decode_input_exceeded) { baseencode_error_t err; const char *k = "ASDF"; size_t len = 128*1024*1024; unsigned char *dk = base64_decode(k, len, &err); cr_expect_null(dk, "%s"); cr_expect_eq(err, INPUT_TOO_BIG); } Test(b64_decode_test, b64_decode_input_whitespaces) { baseencode_error_t err; const char *k = "Zm 9v Y mFy"; const char *expected = "foobar"; unsigned char *dk = base64_decode(k, strlen(k), &err); cr_expect_str_eq(dk, expected, "%s"); }libbaseencode-1.0.12/tests/test-base64-encode.c000066400000000000000000000050101416301270000211420ustar00rootroot00000000000000#include #include #include "../src/baseencode.h" Test(b64_encode_test, null_input) { baseencode_error_t err; const char *k = NULL; char *ek = base64_encode(k, 5, &err); cr_expect_null(ek, "%s"); } Test(b64_encode_test, data_nodata_size_nosize) { baseencode_error_t err; const char *k1 = ""; const char *k2 = "asdiasjdijis"; // test no-data with given size, data with no-size and no-data no-size char *ek1 = base64_encode(k1, 30, &err); char *ek2 = base64_encode(k2, 0, &err); cr_expect(strcmp(k1, ek1) == 0, "Expected %s to be equal to %s", ek1, k1); cr_expect_null(ek2, "%s"); free(ek1); } Test(b64_encode_test, b64_all_chars) { baseencode_error_t err; const char *k = "ADFG413!£$%&&((/?^çé*[]#)-.,|<>+"; const char *k_enc = "QURGRzQxMyHCoyQlJiYoKC8/XsOnw6kqW10jKS0uLHw8Pis="; char *ek = base64_encode(k, strlen(k)+1, &err); cr_expect(strcmp(ek, k_enc) == 0, "Expected %s to be equal to %s", ek, k_enc); free(ek); } Test(b64_encode_test, b64_all_chars_noplusone) { baseencode_error_t err; const char *k = "ADFG413!£$%&&((/?^çé*[]#)-.,|<>+"; const char *k_enc = "QURGRzQxMyHCoyQlJiYoKC8/XsOnw6kqW10jKS0uLHw8Pis="; char *ek = base64_encode(k, strlen(k), &err); cr_expect(strcmp(ek, k_enc) == 0, "Expected %s to be equal to %s", ek, k_enc); free(ek); } Test(b64_encode_test, b64_rfc4648) { baseencode_error_t err; const char *k[] = {"", "f", "fo", "foo", "foob", "fooba", "foobar"}; const char *k_enc[] = {"", "Zg==" ,"Zm8=", "Zm9v", "Zm9vYg==" ,"Zm9vYmE=", "Zm9vYmFy"}; for (int i = 0; i < 7; i++) { char *ek = base64_encode(k[i], strlen(k[i])+1, &err); cr_expect(strcmp(ek, k_enc[i]) == 0, "Expected %s to be equal to %s", ek, k_enc[i]); free(ek); } } Test(b64_encode_test, b64_rfc4648_noplusone) { baseencode_error_t err; const char *k[] = {"", "f", "fo", "foo", "foob", "fooba", "foobar"}; const char *k_enc[] = {"", "Zg==" ,"Zm8=", "Zm9v", "Zm9vYg==" ,"Zm9vYmE=", "Zm9vYmFy"}; for (int i = 0; i < 7; i++) { char *ek = base64_encode(k[i], strlen(k[i]), &err); cr_expect(strcmp(ek, k_enc[i]) == 0, "Expected %s to be equal to %s", ek, k_enc[i]); free(ek); } } Test(b64_encode_test, b64_encode_input_exceeded) { baseencode_error_t err; const char *k = "test"; size_t len = 65*1024*1024; char *ek = base64_encode(k, len, &err); cr_expect_null(ek, "%s"); cr_expect_eq(err, INPUT_TOO_BIG); }