| 136 | } |
| 137 | |
| 138 | static void |
| 139 | encodeBase64String(std::ostream &os, const unsigned char *bytes, size_t size) { |
| 140 | const char *table64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 141 | unsigned char c0, c1, c2, c3; |
| 142 | char buf[4]; |
| 143 | unsigned written; |
| 144 | |
| 145 | os << "\""; |
| 146 | |
| 147 | written = 0; |
| 148 | while (size >= 3) { |
| 149 | c0 = bytes[0] >> 2; |
| 150 | c1 = ((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xf0) >> 4); |
| 151 | c2 = ((bytes[1] & 0x0f) << 2) | ((bytes[2] & 0xc0) >> 6); |
| 152 | c3 = bytes[2] & 0x3f; |
| 153 | |
| 154 | buf[0] = table64[c0]; |
| 155 | buf[1] = table64[c1]; |
| 156 | buf[2] = table64[c2]; |
| 157 | buf[3] = table64[c3]; |
| 158 | |
| 159 | os.write(buf, 4); |
| 160 | |
| 161 | bytes += 3; |
| 162 | size -= 3; |
| 163 | ++written; |
| 164 | |
| 165 | if (written >= 76/4 && size) { |
| 166 | os << "\n"; |
| 167 | written = 0; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (size > 0) { |
| 172 | c0 = bytes[0] >> 2; |
| 173 | c1 = ((bytes[0] & 0x03) << 4); |
| 174 | |
| 175 | buf[3] = '='; |
| 176 | if (size > 1) { |
| 177 | c1 |= ((bytes[1] & 0xf0) >> 4); |
| 178 | c2 = ((bytes[1] & 0x0f) << 2); |
| 179 | buf[2] = table64[c2]; |
| 180 | } else { |
| 181 | buf[2] = '='; |
| 182 | } |
| 183 | buf[1] = table64[c1]; |
| 184 | buf[0] = table64[c0]; |
| 185 | |
| 186 | os.write(buf, 4); |
| 187 | } |
| 188 | |
| 189 | os << "\""; |
| 190 | } |
| 191 | |
| 192 | JSONWriter::JSONWriter(std::ostream &_os) : |
| 193 | os(_os), |