diff options
| author | manuel <manuel@mausz.at> | 2020-10-19 00:52:24 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2020-10-19 00:52:24 +0200 |
| commit | be933ef2241d79558f91796cc5b3a161f72ebf9c (patch) | |
| tree | fe3ab2f130e20c99001f2d7a81d610c78c96a3f4 /xbmc/utils/Base64.cpp | |
| parent | 5f8335c1e49ce108ef3481863833c98efa00411b (diff) | |
| download | kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.gz kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.bz2 kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.zip | |
sync with upstream
Diffstat (limited to 'xbmc/utils/Base64.cpp')
| -rw-r--r-- | xbmc/utils/Base64.cpp | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/xbmc/utils/Base64.cpp b/xbmc/utils/Base64.cpp new file mode 100644 index 0000000..6b41519 --- /dev/null +++ b/xbmc/utils/Base64.cpp | |||
| @@ -0,0 +1,128 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2011-2018 Team Kodi | ||
| 3 | * This file is part of Kodi - https://kodi.tv | ||
| 4 | * | ||
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later | ||
| 6 | * See LICENSES/README.md for more information. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include "Base64.h" | ||
| 10 | |||
| 11 | #define PADDING '=' | ||
| 12 | |||
| 13 | const std::string Base64::m_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
| 14 | "abcdefghijklmnopqrstuvwxyz" | ||
| 15 | "0123456789+/"; | ||
| 16 | |||
| 17 | void Base64::Encode(const char* input, unsigned int length, std::string &output) | ||
| 18 | { | ||
| 19 | if (input == NULL || length == 0) | ||
| 20 | return; | ||
| 21 | |||
| 22 | long l; | ||
| 23 | output.clear(); | ||
| 24 | output.reserve(((length + 2) / 3) * 4); | ||
| 25 | |||
| 26 | for (unsigned int i = 0; i < length; i += 3) | ||
| 27 | { | ||
| 28 | l = ((((unsigned long) input[i]) << 16) & 0xFFFFFF) | | ||
| 29 | ((((i + 1) < length) ? (((unsigned long) input[i + 1]) << 8) : 0) & 0xFFFF) | | ||
| 30 | ((((i + 2) < length) ? (((unsigned long) input[i + 2]) << 0) : 0) & 0x00FF); | ||
| 31 | |||
| 32 | output.push_back(m_characters[(l >> 18) & 0x3F]); | ||
| 33 | output.push_back(m_characters[(l >> 12) & 0x3F]); | ||
| 34 | |||
| 35 | if (i + 1 < length) | ||
| 36 | output.push_back(m_characters[(l >> 6) & 0x3F]); | ||
| 37 | if (i + 2 < length) | ||
| 38 | output.push_back(m_characters[(l >> 0) & 0x3F]); | ||
| 39 | } | ||
| 40 | |||
| 41 | int left = 3 - (length % 3); | ||
| 42 | |||
| 43 | if (length % 3) | ||
| 44 | { | ||
| 45 | for (int i = 0; i < left; i++) | ||
| 46 | output.push_back(PADDING); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | std::string Base64::Encode(const char* input, unsigned int length) | ||
| 51 | { | ||
| 52 | std::string output; | ||
| 53 | Encode(input, length, output); | ||
| 54 | |||
| 55 | return output; | ||
| 56 | } | ||
| 57 | |||
| 58 | void Base64::Encode(const std::string &input, std::string &output) | ||
| 59 | { | ||
| 60 | Encode(input.c_str(), input.size(), output); | ||
| 61 | } | ||
| 62 | |||
| 63 | std::string Base64::Encode(const std::string &input) | ||
| 64 | { | ||
| 65 | std::string output; | ||
| 66 | Encode(input, output); | ||
| 67 | |||
| 68 | return output; | ||
| 69 | } | ||
| 70 | |||
| 71 | void Base64::Decode(const char* input, unsigned int length, std::string &output) | ||
| 72 | { | ||
| 73 | if (input == NULL || length == 0) | ||
| 74 | return; | ||
| 75 | |||
| 76 | long l; | ||
| 77 | output.clear(); | ||
| 78 | |||
| 79 | for (unsigned int index = 0; index < length; index++) | ||
| 80 | { | ||
| 81 | if (input[index] == '=') | ||
| 82 | { | ||
| 83 | length = index; | ||
| 84 | break; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | output.reserve(length - ((length + 2) / 4)); | ||
| 89 | |||
| 90 | for (unsigned int i = 0; i < length; i += 4) | ||
| 91 | { | ||
| 92 | l = ((((unsigned long) m_characters.find(input[i])) & 0x3F) << 18); | ||
| 93 | l |= (((i + 1) < length) ? ((((unsigned long) m_characters.find(input[i + 1])) & 0x3F) << 12) : 0); | ||
| 94 | l |= (((i + 2) < length) ? ((((unsigned long) m_characters.find(input[i + 2])) & 0x3F) << 6) : 0); | ||
| 95 | l |= (((i + 3) < length) ? ((((unsigned long) m_characters.find(input[i + 3])) & 0x3F) << 0) : 0); | ||
| 96 | |||
| 97 | output.push_back((char)((l >> 16) & 0xFF)); | ||
| 98 | if (i + 2 < length) | ||
| 99 | output.push_back((char)((l >> 8) & 0xFF)); | ||
| 100 | if (i + 3 < length) | ||
| 101 | output.push_back((char)((l >> 0) & 0xFF)); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | std::string Base64::Decode(const char* input, unsigned int length) | ||
| 106 | { | ||
| 107 | std::string output; | ||
| 108 | Decode(input, length, output); | ||
| 109 | |||
| 110 | return output; | ||
| 111 | } | ||
| 112 | |||
| 113 | void Base64::Decode(const std::string &input, std::string &output) | ||
| 114 | { | ||
| 115 | size_t length = input.find_first_of(PADDING); | ||
| 116 | if (length == std::string::npos) | ||
| 117 | length = input.size(); | ||
| 118 | |||
| 119 | Decode(input.c_str(), length, output); | ||
| 120 | } | ||
| 121 | |||
| 122 | std::string Base64::Decode(const std::string &input) | ||
| 123 | { | ||
| 124 | std::string output; | ||
| 125 | Decode(input, output); | ||
| 126 | |||
| 127 | return output; | ||
| 128 | } | ||
