| 1269 | } |
| 1270 | |
| 1271 | int Debugger::WritePropertyData(LPCTSTR aData, size_t aDataSize, int aMaxEncodedSize) |
| 1272 | // Accepts a "native" string, converts it to UTF-8, base64-encodes it and writes |
| 1273 | // the end of the property's size attribute followed by the base64-encoded data. |
| 1274 | { |
| 1275 | int err; |
| 1276 | |
| 1277 | #ifdef UNICODE |
| 1278 | LPCWSTR utf16_value = aData; |
| 1279 | size_t total_utf16_size = aDataSize; |
| 1280 | #else |
| 1281 | // ANSI mode. For simplicity, convert the entire data to UTF-16 rather than attempting to |
| 1282 | // calculate how much ANSI text will produce the right number of UTF-8 bytes (since UTF-16 |
| 1283 | // is needed as an intermediate step anyway). This would fail if the string length exceeds |
| 1284 | // INT_MAX, but that would only matter if we built ANSI for x64 (and we don't). |
| 1285 | CStringWCharFromChar utf16_buf(aData, aDataSize); |
| 1286 | LPCWSTR utf16_value = utf16_buf.GetString(); |
| 1287 | size_t total_utf16_size = utf16_buf.GetLength(); |
| 1288 | if (!total_utf16_size && aDataSize) // Conversion failed (too large?) |
| 1289 | return DEBUGGER_E_INTERNAL_ERROR; |
| 1290 | #endif |
| 1291 | |
| 1292 | // The spec says: "The IDE should not read more data than the length defined in the packet |
| 1293 | // header. The IDE can determine if there is more data by using the property data length |
| 1294 | // information." This has two implications: |
| 1295 | // 1) The size attribute should represent the total size, not the amount of data actually |
| 1296 | // returned (when limited by aMaxEncodedSize). This is more useful anyway. |
| 1297 | // 2) Since the data is encoded as UTF-8, the size attribute must be a UTF-8 byte count |
| 1298 | // for any comparison by the IDE to give the correct result. |
| 1299 | |
| 1300 | // According to the spec, -m 0 should mean "unlimited". |
| 1301 | if (!aMaxEncodedSize) |
| 1302 | aMaxEncodedSize = INT_MAX; |
| 1303 | |
| 1304 | // Calculate: |
| 1305 | // - the total size in terms of UTF-8 bytes (even if that exceeds INT_MAX). |
| 1306 | size_t total_utf8_size = 0; |
| 1307 | // - the maximum number of wide chars to convert, taking aMaxEncodedSize into account. |
| 1308 | int utf16_size = (int)total_utf16_size; |
| 1309 | // - the required buffer size for conversion, in bytes. |
| 1310 | int utf8_size = -1; |
| 1311 | |
| 1312 | for (size_t i = 0; i < total_utf16_size; ++i) |
| 1313 | { |
| 1314 | wchar_t wc = utf16_value[i]; |
| 1315 | |
| 1316 | int char_size; |
| 1317 | if (wc <= 0x007F) |
| 1318 | char_size = 1; |
| 1319 | else if (wc <= 0x07FF) |
| 1320 | char_size = 2; |
| 1321 | else if (IS_SURROGATE_PAIR(wc, utf16_value[i+1])) |
| 1322 | char_size = 4; |
| 1323 | else |
| 1324 | char_size = 3; |
| 1325 | |
| 1326 | total_utf8_size += char_size; |
| 1327 | |
| 1328 | if (total_utf8_size > (size_t)aMaxEncodedSize) |
nothing calls this directly
no test coverage detected