Calin Juravle | 46e27bc | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1 | #include <android-base/logging.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | uint8_t kBase64Map[256] = { |
| 6 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 7 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 8 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 9 | 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, |
| 10 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, |
| 11 | 255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, |
| 12 | 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, |
| 13 | 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, |
| 14 | 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 15 | 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
| 16 | 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 17 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 18 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 19 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 20 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 21 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 22 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 23 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 24 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 25 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 26 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 27 | 255, 255, 255, 255 |
| 28 | }; |
| 29 | |
| 30 | uint8_t* DecodeBase64(const char* src, size_t* dst_size) { |
| 31 | CHECK(dst_size != nullptr); |
| 32 | std::vector<uint8_t> tmp; |
| 33 | uint32_t t = 0, y = 0; |
| 34 | int g = 3; |
| 35 | for (size_t i = 0; src[i] != '\0'; ++i) { |
| 36 | uint8_t c = kBase64Map[src[i] & 0xFF]; |
| 37 | if (c == 255) continue; |
| 38 | // the final = symbols are read and used to trim the remaining bytes |
| 39 | if (c == 254) { |
| 40 | c = 0; |
| 41 | // prevent g < 0 which would potentially allow an overflow later |
| 42 | if (--g < 0) { |
| 43 | *dst_size = 0; |
| 44 | return nullptr; |
| 45 | } |
| 46 | } else if (g != 3) { |
| 47 | // we only allow = to be at the end |
| 48 | *dst_size = 0; |
| 49 | return nullptr; |
| 50 | } |
| 51 | t = (t << 6) | c; |
| 52 | if (++y == 4) { |
| 53 | tmp.push_back((t >> 16) & 255); |
| 54 | if (g > 1) { |
| 55 | tmp.push_back((t >> 8) & 255); |
| 56 | } |
| 57 | if (g > 2) { |
| 58 | tmp.push_back(t & 255); |
| 59 | } |
| 60 | y = t = 0; |
| 61 | } |
| 62 | } |
| 63 | if (y != 0) { |
| 64 | *dst_size = 0; |
| 65 | return nullptr; |
| 66 | } |
| 67 | std::unique_ptr<uint8_t[]> dst(new uint8_t[tmp.size()]); |
| 68 | *dst_size = tmp.size(); |
| 69 | std::copy(tmp.begin(), tmp.end(), dst.get()); |
| 70 | return dst.release(); |
| 71 | } |
| 72 | |
| 73 | bool WriteBase64ToFile(const char* base64, const std::string& file, |
| 74 | uid_t uid, gid_t gid, int mode) { |
| 75 | CHECK(base64 != nullptr); |
| 76 | size_t length; |
| 77 | std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length)); |
| 78 | CHECK(bytes != nullptr); |
| 79 | |
| 80 | |
| 81 | int fd = open(file.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); |
| 82 | |
| 83 | if (fd < 0) { |
| 84 | PLOG(ERROR) << "Could not open file " << file; |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | size_t wrote = 0; |
| 89 | while (wrote < length) { |
| 90 | ssize_t cur = write(fd, bytes.get() + wrote, length - wrote); |
| 91 | if (cur == -1) { |
| 92 | PLOG(ERROR) << "Could not write file " << file; |
| 93 | return false; |
| 94 | } |
| 95 | wrote += cur; |
| 96 | } |
| 97 | |
| 98 | if (::chown(file.c_str(), uid, gid) != 0) { |
| 99 | PLOG(ERROR) << "Could not chown file " << file; |
| 100 | return false; |
| 101 | } |
| 102 | if (::chmod(file.c_str(), mode) != 0) { |
| 103 | PLOG(ERROR) << "Could not chmod file " << file; |
| 104 | return false; |
| 105 | } |
| 106 | return true; |
| 107 | } |