git repos / blockattack-game

blame: source/code/Libs/include/cereal/external/base64.hpp

normal view · raw

7a956470 sago007 2016-02-14 17:09 1
/*
7a956470 sago007 2016-02-14 17:09 2
   Copyright (C) 2004-2008 René Nyffenegger
7a956470 sago007 2016-02-14 17:09 3
7a956470 sago007 2016-02-14 17:09 4
   This source code is provided 'as-is', without any express or implied
7a956470 sago007 2016-02-14 17:09 5
   warranty. In no event will the author be held liable for any damages
7a956470 sago007 2016-02-14 17:09 6
   arising from the use of this software.
7a956470 sago007 2016-02-14 17:09 7
7a956470 sago007 2016-02-14 17:09 8
   Permission is granted to anyone to use this software for any purpose,
7a956470 sago007 2016-02-14 17:09 9
   including commercial applications, and to alter it and redistribute it
7a956470 sago007 2016-02-14 17:09 10
   freely, subject to the following restrictions:
7a956470 sago007 2016-02-14 17:09 11
7a956470 sago007 2016-02-14 17:09 12
   1. The origin of this source code must not be misrepresented; you must not
7a956470 sago007 2016-02-14 17:09 13
      claim that you wrote the original source code. If you use this source code
7a956470 sago007 2016-02-14 17:09 14
      in a product, an acknowledgment in the product documentation would be
7a956470 sago007 2016-02-14 17:09 15
      appreciated but is not required.
7a956470 sago007 2016-02-14 17:09 16
7a956470 sago007 2016-02-14 17:09 17
   2. Altered source versions must be plainly marked as such, and must not be
7a956470 sago007 2016-02-14 17:09 18
      misrepresented as being the original source code.
7a956470 sago007 2016-02-14 17:09 19
7a956470 sago007 2016-02-14 17:09 20
   3. This notice may not be removed or altered from any source distribution.
7a956470 sago007 2016-02-14 17:09 21
7a956470 sago007 2016-02-14 17:09 22
   René Nyffenegger rene.nyffenegger@adp-gmbh.ch
7a956470 sago007 2016-02-14 17:09 23
*/
7a956470 sago007 2016-02-14 17:09 24
7a956470 sago007 2016-02-14 17:09 25
#ifndef CEREAL_EXTERNAL_BASE64_HPP_
7a956470 sago007 2016-02-14 17:09 26
#define CEREAL_EXTERNAL_BASE64_HPP_
7a956470 sago007 2016-02-14 17:09 27
8f94a7f5 sago007 2020-05-10 10:26 28
#ifdef __GNUC__
8f94a7f5 sago007 2020-05-10 10:26 29
#pragma GCC diagnostic push
8f94a7f5 sago007 2020-05-10 10:26 30
#pragma GCC diagnostic ignored "-Wconversion"
8f94a7f5 sago007 2020-05-10 10:26 31
#endif
8f94a7f5 sago007 2020-05-10 10:26 32
7a956470 sago007 2016-02-14 17:09 33
#include <string>
7a956470 sago007 2016-02-14 17:09 34
6c5f2c01 sago007 2017-03-15 17:56 35
namespace cereal
7a956470 sago007 2016-02-14 17:09 36
{
6c5f2c01 sago007 2017-03-15 17:56 37
  namespace base64
6c5f2c01 sago007 2017-03-15 17:56 38
  {
6c5f2c01 sago007 2017-03-15 17:56 39
    static const std::string chars =
6c5f2c01 sago007 2017-03-15 17:56 40
      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
6c5f2c01 sago007 2017-03-15 17:56 41
      "abcdefghijklmnopqrstuvwxyz"
6c5f2c01 sago007 2017-03-15 17:56 42
      "0123456789+/";
6c5f2c01 sago007 2017-03-15 17:56 43
6c5f2c01 sago007 2017-03-15 17:56 44
    static inline bool is_base64(unsigned char c) {
6c5f2c01 sago007 2017-03-15 17:56 45
      return (isalnum(c) || (c == '+') || (c == '/'));
7a956470 sago007 2016-02-14 17:09 46
    }
7a956470 sago007 2016-02-14 17:09 47
6c5f2c01 sago007 2017-03-15 17:56 48
    inline std::string encode(unsigned char const* bytes_to_encode, size_t in_len) {
6c5f2c01 sago007 2017-03-15 17:56 49
      std::string ret;
6c5f2c01 sago007 2017-03-15 17:56 50
      int i = 0;
6c5f2c01 sago007 2017-03-15 17:56 51
      int j = 0;
6c5f2c01 sago007 2017-03-15 17:56 52
      unsigned char char_array_3[3];
6c5f2c01 sago007 2017-03-15 17:56 53
      unsigned char char_array_4[4];
6c5f2c01 sago007 2017-03-15 17:56 54
6c5f2c01 sago007 2017-03-15 17:56 55
      while (in_len--) {
6c5f2c01 sago007 2017-03-15 17:56 56
        char_array_3[i++] = *(bytes_to_encode++);
6c5f2c01 sago007 2017-03-15 17:56 57
        if (i == 3) {
6c5f2c01 sago007 2017-03-15 17:56 58
          char_array_4[0] = static_cast<unsigned char>((char_array_3[0] & 0xfc) >> 2);
6c5f2c01 sago007 2017-03-15 17:56 59
          char_array_4[1] = static_cast<unsigned char>( ( ( char_array_3[0] & 0x03 ) << 4 ) + ( ( char_array_3[1] & 0xf0 ) >> 4 ) );
6c5f2c01 sago007 2017-03-15 17:56 60
          char_array_4[2] = static_cast<unsigned char>( ( ( char_array_3[1] & 0x0f ) << 2 ) + ( ( char_array_3[2] & 0xc0 ) >> 6 ) );
6c5f2c01 sago007 2017-03-15 17:56 61
          char_array_4[3] = static_cast<unsigned char>( char_array_3[2] & 0x3f );
6c5f2c01 sago007 2017-03-15 17:56 62
6c5f2c01 sago007 2017-03-15 17:56 63
          for(i = 0; (i <4) ; i++)
6c5f2c01 sago007 2017-03-15 17:56 64
            ret += chars[char_array_4[i]];
6c5f2c01 sago007 2017-03-15 17:56 65
          i = 0;
6c5f2c01 sago007 2017-03-15 17:56 66
        }
6c5f2c01 sago007 2017-03-15 17:56 67
      }
7a956470 sago007 2016-02-14 17:09 68
6c5f2c01 sago007 2017-03-15 17:56 69
      if (i)
6c5f2c01 sago007 2017-03-15 17:56 70
      {
6c5f2c01 sago007 2017-03-15 17:56 71
        for(j = i; j < 3; j++)
6c5f2c01 sago007 2017-03-15 17:56 72
          char_array_3[j] = '\0';
7a956470 sago007 2016-02-14 17:09 73
6c5f2c01 sago007 2017-03-15 17:56 74
        char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
6c5f2c01 sago007 2017-03-15 17:56 75
        char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
6c5f2c01 sago007 2017-03-15 17:56 76
        char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
6c5f2c01 sago007 2017-03-15 17:56 77
        char_array_4[3] = char_array_3[2] & 0x3f;
7a956470 sago007 2016-02-14 17:09 78
6c5f2c01 sago007 2017-03-15 17:56 79
        for (j = 0; (j < i + 1); j++)
6c5f2c01 sago007 2017-03-15 17:56 80
          ret += chars[char_array_4[j]];
7a956470 sago007 2016-02-14 17:09 81
6c5f2c01 sago007 2017-03-15 17:56 82
        while((i++ < 3))
6c5f2c01 sago007 2017-03-15 17:56 83
          ret += '=';
6c5f2c01 sago007 2017-03-15 17:56 84
      }
7a956470 sago007 2016-02-14 17:09 85
6c5f2c01 sago007 2017-03-15 17:56 86
      return ret;
6c5f2c01 sago007 2017-03-15 17:56 87
    }
7a956470 sago007 2016-02-14 17:09 88
6c5f2c01 sago007 2017-03-15 17:56 89
    inline std::string decode(std::string const& encoded_string) {
6c5f2c01 sago007 2017-03-15 17:56 90
      size_t in_len = encoded_string.size();
6c5f2c01 sago007 2017-03-15 17:56 91
      size_t i = 0;
6c5f2c01 sago007 2017-03-15 17:56 92
      size_t j = 0;
6c5f2c01 sago007 2017-03-15 17:56 93
      int in_ = 0;
6c5f2c01 sago007 2017-03-15 17:56 94
      unsigned char char_array_4[4], char_array_3[3];
6c5f2c01 sago007 2017-03-15 17:56 95
      std::string ret;
6c5f2c01 sago007 2017-03-15 17:56 96
6c5f2c01 sago007 2017-03-15 17:56 97
      while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
6c5f2c01 sago007 2017-03-15 17:56 98
        char_array_4[i++] = encoded_string[in_]; in_++;
6c5f2c01 sago007 2017-03-15 17:56 99
        if (i ==4) {
6c5f2c01 sago007 2017-03-15 17:56 100
          for (i = 0; i <4; i++)
6c5f2c01 sago007 2017-03-15 17:56 101
            char_array_4[i] = static_cast<unsigned char>(chars.find( char_array_4[i] ));
6c5f2c01 sago007 2017-03-15 17:56 102
6c5f2c01 sago007 2017-03-15 17:56 103
          char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
6c5f2c01 sago007 2017-03-15 17:56 104
          char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
6c5f2c01 sago007 2017-03-15 17:56 105
          char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
6c5f2c01 sago007 2017-03-15 17:56 106
6c5f2c01 sago007 2017-03-15 17:56 107
          for (i = 0; (i < 3); i++)
6c5f2c01 sago007 2017-03-15 17:56 108
            ret += char_array_3[i];
6c5f2c01 sago007 2017-03-15 17:56 109
          i = 0;
6c5f2c01 sago007 2017-03-15 17:56 110
        }
6c5f2c01 sago007 2017-03-15 17:56 111
      }
7a956470 sago007 2016-02-14 17:09 112
6c5f2c01 sago007 2017-03-15 17:56 113
      if (i) {
6c5f2c01 sago007 2017-03-15 17:56 114
        for (j = i; j <4; j++)
6c5f2c01 sago007 2017-03-15 17:56 115
          char_array_4[j] = 0;
7a956470 sago007 2016-02-14 17:09 116
6c5f2c01 sago007 2017-03-15 17:56 117
        for (j = 0; j <4; j++)
6c5f2c01 sago007 2017-03-15 17:56 118
          char_array_4[j] = static_cast<unsigned char>(chars.find( char_array_4[j] ));
7a956470 sago007 2016-02-14 17:09 119
7a956470 sago007 2016-02-14 17:09 120
        char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
7a956470 sago007 2016-02-14 17:09 121
        char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
7a956470 sago007 2016-02-14 17:09 122
        char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
7a956470 sago007 2016-02-14 17:09 123
6c5f2c01 sago007 2017-03-15 17:56 124
        for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
7a956470 sago007 2016-02-14 17:09 125
      }
7a956470 sago007 2016-02-14 17:09 126
6c5f2c01 sago007 2017-03-15 17:56 127
      return ret;
7a956470 sago007 2016-02-14 17:09 128
    }
6c5f2c01 sago007 2017-03-15 17:56 129
  } // namespace base64
6c5f2c01 sago007 2017-03-15 17:56 130
} // namespace cereal
8f94a7f5 sago007 2020-05-10 10:26 131
#ifdef __GNUC__
8f94a7f5 sago007 2020-05-10 10:26 132
#pragma GCC diagnostic pop
8f94a7f5 sago007 2020-05-10 10:26 133
#endif
7a956470 sago007 2016-02-14 17:09 134
#endif // CEREAL_EXTERNAL_BASE64_HPP_
1970-01-01 00:00 135