Mercurial > libbase64
changeset 32:c96f2b26678a
misc: general cleanups
- Remove C++ variant,
- Remove Doxygen,
- Improve README,
- Upgrade license years.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 02 Jun 2021 15:59:46 +0200 |
parents | 16f64f665b1d |
children | 01c29bee0c54 |
files | Doxyfile INSTALL.md LICENSE.md Makefile README.md base64.c base64.h base64.hpp test/base64++.cpp |
diffstat | 9 files changed, 41 insertions(+), 570 deletions(-) [+] |
line wrap: on
line diff
--- a/Doxyfile Wed Jun 02 15:48:33 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -# -# Doxyfile -- generate API documentation for libbase64 -# -# Copyright (c) 2013-2020 David Demelier <markand@malikania.fr> -# -# Permission to use, copy, modify, and/or distribute this software for any -# purpose with or without fee is hereby granted, provided that the above -# copyright notice and this permission notice appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -# - -DOXYFILE_ENCODING = UTF-8 -PROJECT_NAME = "libbase64" -PROJECT_NUMBER = "2.0.0" -PROJECT_BRIEF = "Base64 encoding and decoding" -PROJECT_LOGO = -OUTPUT_DIRECTORY = doxygen -ALLOW_UNICODE_NAMES = YES -STRIP_FROM_PATH = ./ -TAB_SIZE = 8 -OPTIMIZE_OUTPUT_FOR_C = YES -AUTOLINK_SUPPORT = NO -QUIET = YES -WARNINGS = YES -INPUT = base64.h base64.hpp -INPUT_ENCODING = UTF-8 -RECURSIVE = NO -GENERATE_LATEX = NO -GENERATE_MAN = NO -MAX_INITIALIZER_LINES = 0
--- a/INSTALL.md Wed Jun 02 15:48:33 2021 +0200 +++ b/INSTALL.md Wed Jun 02 15:59:46 2021 +0200 @@ -4,14 +4,9 @@ Requirements ------------ -- C++17 or C99. - -Installation (C++ variant) --------------------------- +- C99. -Just copy the file base64.hpp and add it to your project. - -Installation (C variant) ------------------------- +Installation +------------ Copy base64.h and base64.c to your project.
--- a/LICENSE.md Wed Jun 02 15:48:33 2021 +0200 +++ b/LICENSE.md Wed Jun 02 15:59:46 2021 +0200 @@ -1,7 +1,7 @@ libbase64 -- license ==================== -Copyright (c) 2013-2020 David Demelier <markand@malikania.fr> +Copyright (c) 2013-2021 David Demelier <markand@malikania.fr> Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above
--- a/Makefile Wed Jun 02 15:48:33 2021 +0200 +++ b/Makefile Wed Jun 02 15:59:46 2021 +0200 @@ -19,40 +19,26 @@ .POSIX: CC= cc -CXX= c++ CFLAGS= -O3 -DNDEBUG -Wall -Wextra -CXXFLAGS= -std=c++17 -O3 -DNDEBUG -Wall -Wextra .SUFFIXES: -.SUFFIXES: .c .cpp .o +.SUFFIXES: .c .o -all: +all: test/base64 .c.o: ${CC} ${CFLAGS} -Iextern/libgreatest -I. -c $< -o $@ ${LDFLAGS} -.cpp.o: - ${CXX} ${CXXFLAGS} -Iextern/libgreatest -I. -c $< -o $@ ${LDFLAGS} - test/base64.o: base64.h test/base64: test/base64.o base64.o - ${CC} -o $@ test/base64.o base64.o ${LDFLAGS} - -test/base64++.o: base64.hpp - -test/base64++: test/base64++.o - ${CXX} -o $@ test/base64++.o ${LDFLAGS} + ${CC} ${CFLAGS} -o $@ test/base64.o base64.o ${LDFLAGS} -tests: test/base64 test/base64++ +tests: test/base64 test/base64 - test/base64++ - -doxygen: - doxygen Doxyfile clean: rm -f *.o test/*.o - rm -f test/base64 test/base64++ + rm -f test/base64 -.PHONY: all clean doxygen tests +.PHONY: all clean tests
--- a/README.md Wed Jun 02 15:48:33 2021 +0200 +++ b/README.md Wed Jun 02 15:59:46 2021 +0200 @@ -4,4 +4,32 @@ Introduction ------------ -Base64 encoding and decoding easily in C++17 or C99. +Base64 encoding and decoding easily in C99. + +Features +-------- + +- base64 encoding, +- base64 decoding (with base64url support), +- no dynamic allocation required, + +Quick overview +-------------- + +Encode data. We use -1 to read until end of input string. + + char encoded[128]; + size_t size; + + size = b64_encode("Hello world!", -1, encoded, sizeof (encoded)); + +Decode data. As with previous example, we use -1 to read until enf of base64 +input string. + + char decoded[128]; + size_t size; + + size = b64_decode("ABx=", -1, decoded, sizeof (decoded)); + +Note: even with binary data the function `b64_decode` still append a NUL +terminator. Use the return code from the function to get the size binary data.
--- a/base64.c Wed Jun 02 15:48:33 2021 +0200 +++ b/base64.c Wed Jun 02 15:59:46 2021 +0200 @@ -1,7 +1,7 @@ /* * base64.h -- base64 encoding and decoding * - * Copyright (c) 2013-2020 David Demelier <markand@malikania.fr> + * Copyright (c) 2013-2021 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above
--- a/base64.h Wed Jun 02 15:48:33 2021 +0200 +++ b/base64.h Wed Jun 02 15:59:46 2021 +0200 @@ -1,7 +1,7 @@ /* * base64.h -- base64 encoding and decoding * - * Copyright (c) 2013-2020 David Demelier <markand@malikania.fr> + * Copyright (c) 2013-2021 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above
--- a/base64.hpp Wed Jun 02 15:48:33 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,216 +0,0 @@ -/* - * base64.hpp -- base64 encoding and decoding - * - * Copyright (c) 2013-2020 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef BASE64_HPP -#define BASE64_HPP - -/** - * \file base64.hpp - * \brief Base64 encoding and decoding. - * \author David Demelier <markand@malikania.fr> - * \version 2.0.0-dev - */ - -#include <cassert> -#include <cctype> -#include <stdexcept> -#include <string> -#include <string_view> - -/** - * \brief main %base64 namespace. - */ -namespace base64 { - -/** - * Check if the character is a %base64 character, A-Za-z0-9 and +/. - * - * \param ch the character to test - * \return true if valid - */ -inline auto is_base64(char ch) noexcept -> bool -{ - return std::isalnum(ch) != 0 || ch == '+' || ch == '/'; -} - -/** - * Check if the given character is a valid character in %base64 string, - * including '='. - * - * \param ch the character - * \return true if the character is valid - */ -inline auto is_valid(char ch) noexcept -> bool -{ - return is_base64(ch) || ch == '='; -} - -/** - * Get the %base64 character from the 6-bits value. - * - * \pre value must be valid (< 64) - * \param value the value - * \return the %base64 character for value - */ -inline auto lookup(unsigned char value) noexcept -> char -{ - static std::string_view table("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); - - assert(value < 64); - - return table[value]; -} - -/** - * Get the integer value from the %base64 character. - * - * \pre is_base64(ch) - * \param ch the %base64 character - * \return the integer value for the %base64 character ch - * ```` - * auto b64 = base64::rlookup('D') // 3 - * ```` - */ -inline auto rlookup(char ch) noexcept -> unsigned char -{ - assert(is_base64(ch)); - - if (ch >= '0' && ch <= '9') - return static_cast<unsigned char>(ch + 4); - if (ch >= 'A' && ch <= 'Z') - return static_cast<unsigned char>(ch - 65); - if (ch >= 'a' && ch <= 'z') - return static_cast<unsigned char>(ch - 71); - - return (ch == '+') ? 62U : 63U; -} - -/** - * Encode the input to the output. - * - * Requirements: - * - **InputIt** must be [InputIterator](http://en.cppreference.com/w/cpp/concept/InputIterator) - * - **OutputIt** must be [OutputIterator](http://en.cppreference.com/w/cpp/concept/OutputIterator) - * - * \param input the beginning - * \param end the end of the data - * \param output the output destination - * \return output - */ -template <typename InputIt, typename OutputIt> -auto encode(InputIt input, InputIt end, OutputIt output) -> OutputIt -{ - while (input != end) { - char inputbuf[3] = { 0, 0, 0 }; - int count; - - for (count = 0; count < 3 && input != end; ++count) - inputbuf[count] = *input++; - - *output++ = lookup(inputbuf[0] >> 2 & 0x3f); - *output++ = lookup((inputbuf[0] << 4 & 0x3f) | (inputbuf[1] >> 4 & 0x0f)); - *output++ = (count < 2) ? '=' : lookup((inputbuf[1] << 2 & 0x3c) | (inputbuf[2] >> 6 & 0x03)); - *output++ = (count < 3) ? '=' : lookup(inputbuf[2] & 0x3f); - } - - return output; -} - -/** - * Decode the input to the output. - * - * Requirements: - * - **InputIt** must be [InputIterator](http://en.cppreference.com/w/cpp/concept/InputIterator) - * - **OutputIt** must be [OutputIterator](http://en.cppreference.com/w/cpp/concept/OutputIterator) - * - * \param input the beginning - * \param end the end of the data - * \param output the output destination - * \return output - * \throw std::invalid_argument on bad %base64 string - */ -template <typename InputIt, typename OutputIt> -auto decode(InputIt input, InputIt end, OutputIt output) -> OutputIt -{ - while (input != end) { - char inputbuf[4] = { -1, -1, -1, -1 }; - int count; - - for (count = 0; count < 4 && input != end; ++count) { - // Check if the character is valid and get its value. - if ((*input == '=' && count <= 1) || !is_valid(*input)) - throw std::invalid_argument("invalid base64 string"); - if (is_base64(*input)) - inputbuf[count] = static_cast<char>(rlookup(*input)); - - input++; - } - - if (count != 4) - throw std::invalid_argument("truncated string"); - - *output++ = static_cast<char>(((inputbuf[0] << 2) & 0xfc) | ((inputbuf[1] >> 4) & 0x03)); - - if (inputbuf[2] != -1) - *output++ = static_cast<char>(((inputbuf[1] << 4) & 0xf0) | ((inputbuf[2] >> 2) & 0x0f)); - if (inputbuf[3] != -1) { - // "XY=Z" is not allowed. - if (inputbuf[2] == -1) - throw std::invalid_argument("invalid base64 string"); - - *output++ = static_cast<char>(((inputbuf[2] << 6) & 0xc0) | (inputbuf[3] & 0x3f)); - } - } - - return output; -} - -/** - * Encode a string. - * - * \param input the input string - * \return the %base64 formatted string - */ -inline auto encode(std::string_view input) -> std::string -{ - std::string result; - - encode(input.begin(), input.end(), std::back_inserter(result)); - - return result; -} - -/** - * Decode a string. - * - * \param input the %base64 formatted string - * \return the original string - * \throw std::invalid_argument on bad %base64 string - */ -inline auto decode(std::string_view input) -> std::string -{ - std::string result; - - decode(input.begin(), input.end(), std::back_inserter(result)); - - return result; -} - -} // !base64 - -#endif // !BASE64_HPP
--- a/test/base64++.cpp Wed Jun 02 15:48:33 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,285 +0,0 @@ -/* - * base64++.cpp -- main test file for base64 (C++ version) - * - * Copyright (c) 2013-2020 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#define GREATEST_USE_ABBREVS 0 -#include <greatest.h> - -#include <base64.hpp> - -GREATEST_TEST -test_general_b64_lookup() -{ - GREATEST_ASSERT(base64::lookup(0b000000) == 'A'); - GREATEST_ASSERT(base64::lookup(0b000001) == 'B'); - GREATEST_ASSERT(base64::lookup(0b000010) == 'C'); - GREATEST_ASSERT(base64::lookup(0b000011) == 'D'); - GREATEST_ASSERT(base64::lookup(0b000100) == 'E'); - GREATEST_ASSERT(base64::lookup(0b000101) == 'F'); - GREATEST_ASSERT(base64::lookup(0b000110) == 'G'); - GREATEST_ASSERT(base64::lookup(0b000111) == 'H'); - GREATEST_ASSERT(base64::lookup(0b001000) == 'I'); - GREATEST_ASSERT(base64::lookup(0b001001) == 'J'); - GREATEST_ASSERT(base64::lookup(0b001010) == 'K'); - GREATEST_ASSERT(base64::lookup(0b001011) == 'L'); - GREATEST_ASSERT(base64::lookup(0b001100) == 'M'); - GREATEST_ASSERT(base64::lookup(0b001101) == 'N'); - GREATEST_ASSERT(base64::lookup(0b001110) == 'O'); - GREATEST_ASSERT(base64::lookup(0b001111) == 'P'); - GREATEST_ASSERT(base64::lookup(0b010000) == 'Q'); - GREATEST_ASSERT(base64::lookup(0b010001) == 'R'); - GREATEST_ASSERT(base64::lookup(0b010010) == 'S'); - GREATEST_ASSERT(base64::lookup(0b010011) == 'T'); - GREATEST_ASSERT(base64::lookup(0b010100) == 'U'); - GREATEST_ASSERT(base64::lookup(0b010101) == 'V'); - GREATEST_ASSERT(base64::lookup(0b010110) == 'W'); - GREATEST_ASSERT(base64::lookup(0b010111) == 'X'); - GREATEST_ASSERT(base64::lookup(0b011000) == 'Y'); - GREATEST_ASSERT(base64::lookup(0b011001) == 'Z'); - GREATEST_ASSERT(base64::lookup(0b011010) == 'a'); - GREATEST_ASSERT(base64::lookup(0b011011) == 'b'); - GREATEST_ASSERT(base64::lookup(0b011100) == 'c'); - GREATEST_ASSERT(base64::lookup(0b011101) == 'd'); - GREATEST_ASSERT(base64::lookup(0b011110) == 'e'); - GREATEST_ASSERT(base64::lookup(0b011111) == 'f'); - GREATEST_ASSERT(base64::lookup(0b100000) == 'g'); - GREATEST_ASSERT(base64::lookup(0b100001) == 'h'); - GREATEST_ASSERT(base64::lookup(0b100010) == 'i'); - GREATEST_ASSERT(base64::lookup(0b100011) == 'j'); - GREATEST_ASSERT(base64::lookup(0b100100) == 'k'); - GREATEST_ASSERT(base64::lookup(0b100101) == 'l'); - GREATEST_ASSERT(base64::lookup(0b100110) == 'm'); - GREATEST_ASSERT(base64::lookup(0b100111) == 'n'); - GREATEST_ASSERT(base64::lookup(0b101000) == 'o'); - GREATEST_ASSERT(base64::lookup(0b101001) == 'p'); - GREATEST_ASSERT(base64::lookup(0b101010) == 'q'); - GREATEST_ASSERT(base64::lookup(0b101011) == 'r'); - GREATEST_ASSERT(base64::lookup(0b101100) == 's'); - GREATEST_ASSERT(base64::lookup(0b101101) == 't'); - GREATEST_ASSERT(base64::lookup(0b101110) == 'u'); - GREATEST_ASSERT(base64::lookup(0b101111) == 'v'); - GREATEST_ASSERT(base64::lookup(0b110000) == 'w'); - GREATEST_ASSERT(base64::lookup(0b110001) == 'x'); - GREATEST_ASSERT(base64::lookup(0b110010) == 'y'); - GREATEST_ASSERT(base64::lookup(0b110011) == 'z'); - GREATEST_ASSERT(base64::lookup(0b110100) == '0'); - GREATEST_ASSERT(base64::lookup(0b110101) == '1'); - GREATEST_ASSERT(base64::lookup(0b110110) == '2'); - GREATEST_ASSERT(base64::lookup(0b110111) == '3'); - GREATEST_ASSERT(base64::lookup(0b111000) == '4'); - GREATEST_ASSERT(base64::lookup(0b111001) == '5'); - GREATEST_ASSERT(base64::lookup(0b111010) == '6'); - GREATEST_ASSERT(base64::lookup(0b111011) == '7'); - GREATEST_ASSERT(base64::lookup(0b111100) == '8'); - GREATEST_ASSERT(base64::lookup(0b111101) == '9'); - GREATEST_ASSERT(base64::lookup(0b111110) == '+'); - GREATEST_ASSERT(base64::lookup(0b111111) == '/'); - GREATEST_PASS(); -} - -GREATEST_TEST -test_general_b64_rlookup() -{ - GREATEST_ASSERT(base64::rlookup('A') == 0b000000U); - GREATEST_ASSERT(base64::rlookup('B') == 0b000001U); - GREATEST_ASSERT(base64::rlookup('C') == 0b000010U); - GREATEST_ASSERT(base64::rlookup('D') == 0b000011U); - GREATEST_ASSERT(base64::rlookup('E') == 0b000100U); - GREATEST_ASSERT(base64::rlookup('F') == 0b000101U); - GREATEST_ASSERT(base64::rlookup('G') == 0b000110U); - GREATEST_ASSERT(base64::rlookup('H') == 0b000111U); - GREATEST_ASSERT(base64::rlookup('I') == 0b001000U); - GREATEST_ASSERT(base64::rlookup('J') == 0b001001U); - GREATEST_ASSERT(base64::rlookup('K') == 0b001010U); - GREATEST_ASSERT(base64::rlookup('L') == 0b001011U); - GREATEST_ASSERT(base64::rlookup('M') == 0b001100U); - GREATEST_ASSERT(base64::rlookup('N') == 0b001101U); - GREATEST_ASSERT(base64::rlookup('O') == 0b001110U); - GREATEST_ASSERT(base64::rlookup('P') == 0b001111U); - GREATEST_ASSERT(base64::rlookup('Q') == 0b010000U); - GREATEST_ASSERT(base64::rlookup('R') == 0b010001U); - GREATEST_ASSERT(base64::rlookup('S') == 0b010010U); - GREATEST_ASSERT(base64::rlookup('T') == 0b010011U); - GREATEST_ASSERT(base64::rlookup('U') == 0b010100U); - GREATEST_ASSERT(base64::rlookup('V') == 0b010101U); - GREATEST_ASSERT(base64::rlookup('W') == 0b010110U); - GREATEST_ASSERT(base64::rlookup('X') == 0b010111U); - GREATEST_ASSERT(base64::rlookup('Y') == 0b011000U); - GREATEST_ASSERT(base64::rlookup('Z') == 0b011001U); - GREATEST_ASSERT(base64::rlookup('a') == 0b011010U); - GREATEST_ASSERT(base64::rlookup('b') == 0b011011U); - GREATEST_ASSERT(base64::rlookup('c') == 0b011100U); - GREATEST_ASSERT(base64::rlookup('d') == 0b011101U); - GREATEST_ASSERT(base64::rlookup('e') == 0b011110U); - GREATEST_ASSERT(base64::rlookup('f') == 0b011111U); - GREATEST_ASSERT(base64::rlookup('g') == 0b100000U); - GREATEST_ASSERT(base64::rlookup('h') == 0b100001U); - GREATEST_ASSERT(base64::rlookup('i') == 0b100010U); - GREATEST_ASSERT(base64::rlookup('j') == 0b100011U); - GREATEST_ASSERT(base64::rlookup('k') == 0b100100U); - GREATEST_ASSERT(base64::rlookup('l') == 0b100101U); - GREATEST_ASSERT(base64::rlookup('m') == 0b100110U); - GREATEST_ASSERT(base64::rlookup('n') == 0b100111U); - GREATEST_ASSERT(base64::rlookup('o') == 0b101000U); - GREATEST_ASSERT(base64::rlookup('p') == 0b101001U); - GREATEST_ASSERT(base64::rlookup('q') == 0b101010U); - GREATEST_ASSERT(base64::rlookup('r') == 0b101011U); - GREATEST_ASSERT(base64::rlookup('s') == 0b101100U); - GREATEST_ASSERT(base64::rlookup('t') == 0b101101U); - GREATEST_ASSERT(base64::rlookup('u') == 0b101110U); - GREATEST_ASSERT(base64::rlookup('v') == 0b101111U); - GREATEST_ASSERT(base64::rlookup('w') == 0b110000U); - GREATEST_ASSERT(base64::rlookup('x') == 0b110001U); - GREATEST_ASSERT(base64::rlookup('y') == 0b110010U); - GREATEST_ASSERT(base64::rlookup('z') == 0b110011U); - GREATEST_ASSERT(base64::rlookup('0') == 0b110100U); - GREATEST_ASSERT(base64::rlookup('1') == 0b110101U); - GREATEST_ASSERT(base64::rlookup('2') == 0b110110U); - GREATEST_ASSERT(base64::rlookup('3') == 0b110111U); - GREATEST_ASSERT(base64::rlookup('4') == 0b111000U); - GREATEST_ASSERT(base64::rlookup('5') == 0b111001U); - GREATEST_ASSERT(base64::rlookup('6') == 0b111010U); - GREATEST_ASSERT(base64::rlookup('7') == 0b111011U); - GREATEST_ASSERT(base64::rlookup('8') == 0b111100U); - GREATEST_ASSERT(base64::rlookup('9') == 0b111101U); - GREATEST_ASSERT(base64::rlookup('+') == 0b111110U); - GREATEST_ASSERT(base64::rlookup('/') == 0b111111U); - GREATEST_PASS(); -} - -GREATEST_SUITE(suite_general) -{ - GREATEST_RUN_TEST(test_general_b64_lookup); - GREATEST_RUN_TEST(test_general_b64_rlookup); -} - -GREATEST_TEST -test_b64_encode_basic() -{ - GREATEST_ASSERT(base64::encode("a") == "YQ=="); - GREATEST_ASSERT(base64::encode("ab") == "YWI="); - GREATEST_ASSERT(base64::encode("abc") == "YWJj"); - GREATEST_ASSERT(base64::encode("hello") == "aGVsbG8="); - GREATEST_ASSERT(base64::encode("this is a long sentence") == "dGhpcyBpcyBhIGxvbmcgc2VudGVuY2U="); - GREATEST_PASS(); -} - -GREATEST_SUITE(suite_b64_encode) -{ - GREATEST_RUN_TEST(test_b64_encode_basic); -} - -GREATEST_TEST -test_b64_decode_basic() -{ - GREATEST_ASSERT(base64::decode("YQ==") == "a"); - GREATEST_ASSERT(base64::decode("YWI=") == "ab"); - GREATEST_ASSERT(base64::decode("YWJj") == "abc"); - GREATEST_ASSERT(base64::decode("aGVsbG8=") == "hello"); - GREATEST_ASSERT(base64::decode("dGhpcyBpcyBhIGxvbmcgc2VudGVuY2U=") == "this is a long sentence"); - GREATEST_ASSERT(base64::decode("V2VsY29tZSB0byBvdXIgc2VydmVyIGR1ZGU=") == "Welcome to our server dude"); - GREATEST_PASS(); -} - -GREATEST_TEST -test_b64_decode_truncated() -{ - try { - base64::decode("YW="); - GREATEST_FAIL(); - } catch (const std::invalid_argument&) { - GREATEST_PASS(); - } -} - -GREATEST_TEST -test_b64_decode_invalid() -{ - try { - base64::decode("?!"); - GREATEST_FAIL(); - } catch (const std::invalid_argument&) { - GREATEST_PASS(); - } -} - -GREATEST_TEST -test_b64_decode_wrong1() -{ - try { - base64::decode("=ABC"); - GREATEST_FAIL(); - } catch (const std::invalid_argument&) { - GREATEST_PASS(); - } -} - -GREATEST_TEST -test_b64_decode_wrong2() -{ - try { - base64::decode("A=BC"); - GREATEST_FAIL(); - } catch (const std::invalid_argument&) { - GREATEST_PASS(); - } -} - -GREATEST_TEST -test_b64_decode_wrong3() -{ - try { - base64::decode("==BC"); - GREATEST_FAIL(); - } catch (const std::invalid_argument&) { - GREATEST_PASS(); - } -} - -GREATEST_TEST -test_b64_decode_wrong4() -{ - try { - base64::decode("AB=C"); - GREATEST_FAIL(); - } catch (const std::invalid_argument&) { - GREATEST_PASS(); - } -} - -GREATEST_SUITE(suite_b64_decode) -{ - GREATEST_RUN_TEST(test_b64_decode_basic); - GREATEST_RUN_TEST(test_b64_decode_truncated); - GREATEST_RUN_TEST(test_b64_decode_invalid); - GREATEST_RUN_TEST(test_b64_decode_wrong1); - GREATEST_RUN_TEST(test_b64_decode_wrong2); - GREATEST_RUN_TEST(test_b64_decode_wrong3); - GREATEST_RUN_TEST(test_b64_decode_wrong4); -} - -GREATEST_MAIN_DEFS(); - -int -main(int argc, char **argv) -{ - GREATEST_MAIN_BEGIN(); - GREATEST_RUN_SUITE(suite_general); - GREATEST_RUN_SUITE(suite_b64_encode); - GREATEST_RUN_SUITE(suite_b64_decode); - GREATEST_MAIN_END(); -}