Martin Stjernholm | c15e7e4 | 2020-12-02 22:50:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_LIBDEXFILE_DEX_BASE64_TEST_UTIL_H_ |
| 18 | #define ART_LIBDEXFILE_DEX_BASE64_TEST_UTIL_H_ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <stdlib.h> |
| 22 | |
| 23 | #include <memory> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <android-base/logging.h> |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | static inline uint8_t* DecodeBase64(const char* src, size_t* dst_size) { |
| 31 | static const uint8_t kBase64Map[256] = { |
| 32 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 33 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 34 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 35 | 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, |
| 36 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, |
| 37 | 255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, |
| 38 | 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, |
| 39 | 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, |
| 40 | 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 41 | 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
| 42 | 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 43 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 44 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 45 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 46 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 47 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 48 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 49 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 50 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 51 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 52 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 53 | 255, 255, 255, 255 |
| 54 | }; |
| 55 | |
| 56 | CHECK(dst_size != nullptr); |
| 57 | std::vector<uint8_t> tmp; |
| 58 | uint32_t t = 0, y = 0; |
| 59 | int g = 3; |
| 60 | for (size_t i = 0; src[i] != '\0'; ++i) { |
| 61 | uint8_t c = kBase64Map[src[i] & 0xFF]; |
| 62 | if (c == 255) continue; |
| 63 | // the final = symbols are read and used to trim the remaining bytes |
| 64 | if (c == 254) { |
| 65 | c = 0; |
| 66 | // prevent g < 0 which would potentially allow an overflow later |
| 67 | if (--g < 0) { |
| 68 | *dst_size = 0; |
| 69 | return nullptr; |
| 70 | } |
| 71 | } else if (g != 3) { |
| 72 | // we only allow = to be at the end |
| 73 | *dst_size = 0; |
| 74 | return nullptr; |
| 75 | } |
| 76 | t = (t << 6) | c; |
| 77 | if (++y == 4) { |
| 78 | tmp.push_back((t >> 16) & 255); |
| 79 | if (g > 1) { |
| 80 | tmp.push_back((t >> 8) & 255); |
| 81 | } |
| 82 | if (g > 2) { |
| 83 | tmp.push_back(t & 255); |
| 84 | } |
| 85 | y = t = 0; |
| 86 | } |
| 87 | } |
| 88 | if (y != 0) { |
| 89 | *dst_size = 0; |
| 90 | return nullptr; |
| 91 | } |
| 92 | std::unique_ptr<uint8_t[]> dst(new uint8_t[tmp.size()]); |
| 93 | *dst_size = tmp.size(); |
| 94 | std::copy(tmp.begin(), tmp.end(), dst.get()); |
| 95 | return dst.release(); |
| 96 | } |
| 97 | |
| 98 | } // namespace art |
| 99 | |
| 100 | #endif // ART_LIBDEXFILE_DEX_BASE64_TEST_UTIL_H_ |