OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Plugin tests link against this file, but not against the rest of | 5 // Plugin tests link against this file, but not against the rest of |
6 // cromo. Therefore this file should not have dependencies on the | 6 // cromo. Therefore this file should not have dependencies on the |
7 // rest of cromo. | 7 // rest of cromo. |
8 #include "utilities.h" | 8 #include "utilities.h" |
9 | 9 |
10 #include <glog/logging.h> | 10 #include <glog/logging.h> |
11 #include <stdio.h> | 11 #include <stdio.h> |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 octet |= septets.at(k+1) << (7-shift); | 266 octet |= septets.at(k+1) << (7-shift); |
267 octets.push_back(octet); | 267 octets.push_back(octet); |
268 } | 268 } |
269 if (++shift == 8) | 269 if (++shift == 8) |
270 shift = 0; | 270 shift = 0; |
271 } | 271 } |
272 | 272 |
273 return octets; | 273 return octets; |
274 } | 274 } |
275 | 275 |
| 276 std::string Ucs2ToUtf8String(const uint8_t *ucs2) { |
| 277 std::string str; |
| 278 uint8_t num_chars = *ucs2++ >> 1; |
| 279 |
| 280 for (int i = 0; i < num_chars; ++i) { |
| 281 uint16_t ucs2char = ucs2[0] << 8 | ucs2[1]; |
| 282 if (ucs2char <= 0x7f) { |
| 283 str += ucs2[1]; |
| 284 } else if (ucs2char <= 0x7ff) { |
| 285 str += (uint8_t)(0xc0 | ((ucs2char & 0x7c0) >> 6)); |
| 286 str += (uint8_t)(0x80 | (ucs2char & 0x3f)); |
| 287 } else { |
| 288 str += (uint8_t)(0xe0 | ((ucs2char & 0xf000) >> 12)); |
| 289 str += (uint8_t)(0x80 | ((ucs2char & 0xfc0) >> 6)); |
| 290 str += (uint8_t)(0x80 | (ucs2char & 0x3f)); |
| 291 } |
| 292 ucs2 += 2; |
| 293 } |
| 294 return str; |
| 295 } |
| 296 |
| 297 std::vector<uint8_t> Utf8StringToUcs2(const std::string& input) |
| 298 { |
| 299 std::vector<uint8_t> octets; |
| 300 size_t length = input.length(); |
| 301 |
| 302 // First byte gives the length in octets of the UCS-2 string |
| 303 // Insert a placeholder value until we know the true length. |
| 304 octets.push_back(0); |
| 305 for (size_t i = 0; i < length; i++) { |
| 306 char char1 = input.at(i); |
| 307 // Check whether this is a one byte UTF-8 sequence, or the |
| 308 // start of a two or three byte sequence. |
| 309 if ((char1 & 0x80) == 0) { |
| 310 octets.push_back(0); |
| 311 octets.push_back(char1); |
| 312 } else if ((char1 & 0xe0) == 0xc0) { |
| 313 uint8_t char2 = input.at(++i); |
| 314 octets.push_back((char1 >> 2) & 0x7); |
| 315 octets.push_back(((char1 & 0x3) << 6) | (char2 & 0x3f)); |
| 316 } else if ((char1 & 0xf0) == 0xe0) { |
| 317 uint8_t char2 = input.at(++i); |
| 318 uint8_t char3 = input.at(++i); |
| 319 octets.push_back(((char1 & 0xf) << 4) | ((char2 & 0x30) >> 2)); |
| 320 octets.push_back(((char2 & 0x3) << 6) | (char3 & 0x3f)); |
| 321 } else { |
| 322 // character not representable in UCS-2, insert a space |
| 323 octets.push_back(0); |
| 324 octets.push_back(' '); |
| 325 } |
| 326 } |
| 327 octets[0] = octets.size() - 1; |
| 328 return octets; |
| 329 } |
| 330 |
276 void DumpHex(const uint8_t* buf, size_t size) { | 331 void DumpHex(const uint8_t* buf, size_t size) { |
277 size_t nlines = (size+15) / 16; | 332 size_t nlines = (size+15) / 16; |
278 size_t limit; | 333 size_t limit; |
279 | 334 |
280 for (size_t i = 0; i < nlines; i++) { | 335 for (size_t i = 0; i < nlines; i++) { |
281 std::ostringstream ostr; | 336 std::ostringstream ostr; |
282 ostr << std::hex; | 337 ostr << std::hex; |
283 ostr.fill('0'); | 338 ostr.fill('0'); |
284 ostr.width(8); | 339 ostr.width(8); |
285 if (i*16 + 16 >= size) | 340 if (i*16 + 16 >= size) |
286 limit = size - i*16; | 341 limit = size - i*16; |
287 else | 342 else |
288 limit = 16; | 343 limit = 16; |
289 ostr << i*16 << " "; | 344 ostr << i*16 << " "; |
290 ostr.fill('0'); | 345 ostr.fill('0'); |
291 ostr.width(2); | 346 ostr.width(2); |
292 for (size_t j = 0; j < limit; j++) { | 347 for (size_t j = 0; j < limit; j++) { |
293 uint8_t byte = buf[i*16+j]; | 348 uint8_t byte = buf[i*16+j]; |
294 ostr << std::setw(0) << " " << std::setw(2) << std::setfill('0') | 349 ostr << std::setw(0) << " " << std::setw(2) << std::setfill('0') |
295 << static_cast<unsigned int>(byte); | 350 << static_cast<unsigned int>(byte); |
296 } | 351 } |
297 LOG(INFO) << ostr.str(); | 352 LOG(INFO) << ostr.str(); |
298 } | 353 } |
299 } | 354 } |
300 | 355 |
301 } // namespace utilities | 356 } // namespace utilities |
OLD | NEW |