LCOV - code coverage report
Current view: top level - spb/json - base64.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 96.9 % 97 94
Test Date: 2026-10-31 08:41:17 Functions: 100.0 % 12 12

            Line data    Source code
       1              : /*
       2              :  * Protobuf serialization library - RFC 4648 base64 decoder and encoder.
       3              :  *
       4              :  * Copyright (c) 2024 Antonin Kriz <antonin.kriz@gmail.com>
       5              :  * Released under the MIT License. See LICENSE in the project root.
       6              :  */
       7              : 
       8              : #pragma once
       9              : 
      10              : #include "../concepts.h"
      11              : #include <cstddef>
      12              : #include <cstdint>
      13              : #include <span>
      14              : #include <stdexcept>
      15              : 
      16              : namespace spb::json::detail
      17              : {
      18         5502 : void base64_encode(auto &output, std::span<const std::byte> input)
      19              : {
      20              :     static constexpr char encode_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      21              : 
      22         5502 :     const auto *p_char = reinterpret_cast<const uint8_t *>(input.data());
      23              :     //
      24              :     //- +3 means 3 bytes are being processed in one iteration (3 * 8 = 24 bits)
      25              :     //
      26      2189432 :     for (size_t idx = 3; idx <= input.size(); idx += 3)
      27              :     {
      28      2183930 :         auto temp = uint32_t(*p_char++) << 16U;
      29      2183930 :         temp += uint32_t(*p_char++) << 8U;
      30      2183930 :         temp += (*p_char++);
      31      2183930 :         output.write(encode_table[(temp & 0x00FC0000U) >> 18U]);
      32      2183930 :         output.write(encode_table[(temp & 0x0003F000U) >> 12U]);
      33      2183930 :         output.write(encode_table[(temp & 0x00000FC0U) >> 6U]);
      34      2183930 :         output.write(encode_table[(temp & 0x0000003FU)]);
      35              :     }
      36         5502 :     switch (input.size() % 3)
      37              :     {
      38         1928 :     case 1:
      39              :     {
      40         1928 :         auto temp = uint32_t(*p_char++) << 16U;
      41         1928 :         output.write(encode_table[(temp & 0x00FC0000U) >> 18U]);
      42         1928 :         output.write(encode_table[(temp & 0x0003F000U) >> 12U]);
      43         1928 :         output.write('=');
      44         1928 :         output.write('=');
      45              :     }
      46         1928 :     break;
      47         1832 :     case 2:
      48              :     {
      49         1832 :         auto temp = uint32_t(*p_char++) << 16U;
      50         1832 :         temp += uint32_t(*p_char++) << 8U;
      51         1832 :         output.write(encode_table[(temp & 0x00FC0000) >> 18]);
      52         1832 :         output.write(encode_table[(temp & 0x0003F000) >> 12]);
      53         1832 :         output.write(encode_table[(temp & 0x00000FC0) >> 6]);
      54         1832 :         output.write('=');
      55              :     }
      56         1832 :     break;
      57              :     }
      58         5502 : }
      59              : 
      60              : template <typename istream>
      61         2756 : void base64_decode_string(spb::detail::proto_field_bytes auto &output, istream &stream,
      62              :                           size_t max_output_size = 0)
      63              : {
      64              :     static constexpr uint8_t decode_table[256] = {
      65              :         128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
      66              :         128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
      67              :         128, 128, 128, 62,  128, 128, 128, 63,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  128, 128,
      68              :         128, 128, 128, 128, 128, 0,   1,   2,   3,   4,   5,   6,   7,   8,   9,   10,  11,  12,  13,  14,
      69              :         15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  128, 128, 128, 128, 128, 128, 26,  27,  28,
      70              :         29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
      71              :         49,  50,  51,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
      72              :         128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
      73              :         128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
      74              :         128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
      75              :         128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
      76              :         128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
      77              :         128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128};
      78              : 
      79              :     /*static constexpr uint8_t decode_table2[] = {
      80              :         62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 128, 128, 128,
      81              :         128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
      82              :     24, 25, 128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
      83              :     40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
      84              :     };*/
      85              : 
      86              :     if constexpr (spb::detail::proto_field_bytes_resizable<decltype(output)>)
      87         2732 :         output.clear();
      88              : 
      89         2756 :     if (!stream.consume('"')) [[unlikely]]
      90            9 :         throw std::runtime_error("expecting '\"'");
      91              : 
      92         2747 :     if (stream.consume('"'))
      93            5 :         return;
      94              : 
      95         2742 :     auto mask = uint8_t(0);
      96              : 
      97            0 :     for (auto out_index = size_t(0);;)
      98              :     {
      99         2742 :         auto view      = stream.view(1, UINT32_MAX);
     100         2741 :         auto length    = view.find('"');
     101         2741 :         auto end_found = length < view.npos;
     102         2741 :         if ((end_found && length % 4 != 0) || view.size() <= 4) [[unlikely]]
     103            7 :             throw std::runtime_error("invalid base64");
     104              : 
     105         2734 :         length = std::min(length, view.size());
     106              : 
     107              :         //- align to 4 bytes
     108         2734 :         auto aligned_length = length & ~3;
     109         2734 :         if (aligned_length > 4) [[likely]]
     110              :         {
     111         2658 :             auto out_length = ((aligned_length - 4) / 4) * 3;
     112         2658 :             view            = view.substr(0, aligned_length);
     113              : 
     114              :             if constexpr (spb::detail::proto_field_bytes_resizable<decltype(output)>)
     115              :             {
     116         2634 :                 if (max_output_size && (output.size() + out_length > max_output_size)) [[unlikely]]
     117            0 :                     throw std::length_error("bytes is too large");
     118              : 
     119         2634 :                 output.resize(output.size() + out_length);
     120              :             }
     121              :             else
     122              :             {
     123           24 :                 if (out_length > (output.size() - out_index)) [[unlikely]]
     124            0 :                     throw std::runtime_error("too large base64");
     125              :             }
     126              : 
     127         2658 :             auto *p_out       = output.data() + out_index;
     128         2658 :             const auto *p_in  = reinterpret_cast<const uint8_t *>(view.data());
     129         2658 :             const auto *p_end = p_in + aligned_length - 4; //- exclude the last 4 chars (possible padding)
     130              : 
     131      1094616 :             while (p_in < p_end) [[likely]]
     132              :             {
     133      1091958 :                 uint8_t v0 = decode_table[*p_in++];
     134      1091958 :                 uint8_t v1 = decode_table[*p_in++];
     135      1091958 :                 uint8_t v2 = decode_table[*p_in++];
     136      1091958 :                 uint8_t v3 = decode_table[*p_in++];
     137      1091958 :                 mask |= (v0 | v1 | v2 | v3);
     138              : 
     139      1091958 :                 *p_out++ = std::byte((v0 << 2) | (v1 >> 4));
     140      1091958 :                 *p_out++ = std::byte((v1 << 4) | (v2 >> 2));
     141      1091958 :                 *p_out++ = std::byte((v2 << 6) | (v3));
     142              : 
     143      1091958 :                 out_index += 3;
     144              :             }
     145         2658 :             auto consumed_bytes = p_in - reinterpret_cast<const uint8_t *>(view.data());
     146         2658 :             view.remove_prefix(consumed_bytes);
     147         2658 :             stream.skip(consumed_bytes);
     148              :         }
     149              : 
     150         2734 :         if (end_found)
     151              :         {
     152              :             //- handle padding
     153         2734 :             const auto *p_in = reinterpret_cast<const uint8_t *>(view.data());
     154              : 
     155         2734 :             uint8_t v0 = decode_table[*p_in++];
     156         2734 :             uint8_t v1 = decode_table[*p_in++];
     157         2734 :             auto i1    = *p_in++;
     158         2734 :             uint8_t v2 = i1 == '=' ? 0 : decode_table[i1];
     159         2734 :             auto i2    = *p_in++;
     160         2734 :             uint8_t v3 = i2 == '=' ? 0 : decode_table[i2];
     161         2734 :             mask |= (v0 | v1 | v2 | v3);
     162         2734 :             mask |= ((i1 == '=') & (i2 != '=')) ? 128 : 0;
     163         2734 :             if (mask & 128) [[unlikely]]
     164            4 :                 throw std::runtime_error("invalid base64");
     165              : 
     166         2730 :             auto padding_size   = (i1 == '=' ? 1 : 0) + (i2 == '=' ? 1 : 0);
     167         2730 :             auto consumed_bytes = 3 - padding_size;
     168              :             //- +1 is for "
     169         2730 :             stream.skip(5);
     170              :             if constexpr (spb::detail::proto_field_bytes_resizable<decltype(output)>)
     171              :             {
     172         2706 :                 if (max_output_size && (output.size() + consumed_bytes > max_output_size))
     173            8 :                     throw std::length_error("bytes is too large");
     174              : 
     175         2698 :                 output.resize(output.size() + consumed_bytes);
     176              :             }
     177              :             else
     178              :             {
     179           24 :                 if (output.size() != out_index + consumed_bytes) [[unlikely]]
     180            3 :                     throw std::runtime_error("too large base64");
     181              :             }
     182         2719 :             auto *p_out = output.data() + out_index;
     183         2719 :             if (padding_size == 0)
     184              :             {
     185          863 :                 *p_out++ = std::byte((v0 << 2) | (v1 >> 4));
     186          863 :                 *p_out++ = std::byte((v1 << 4) | (v2 >> 2));
     187          863 :                 *p_out++ = std::byte((v2 << 6) | (v3));
     188              :             }
     189         1856 :             else if (padding_size == 1)
     190              :             {
     191          906 :                 *p_out++ = std::byte((v0 << 2) | (v1 >> 4));
     192          906 :                 *p_out++ = std::byte((v1 << 4) | (v2 >> 2));
     193              :             }
     194          950 :             else if (padding_size == 2)
     195              :             {
     196          950 :                 *p_out++ = std::byte((v0 << 2) | (v1 >> 4));
     197              :             }
     198         2719 :             return;
     199              :         }
     200              :     }
     201              : }
     202              : } // namespace spb::json::detail
        

Generated by: LCOV version 2.0-1