1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/common/net/x509_certificate_model.h" 6 7 #include <unicode/uidna.h> 8 9 #include <algorithm> 10 11 #include "base/strings/utf_string_conversions.h" 12 #include "chrome/grit/generated_resources.h" 13 #include "net/base/net_util.h" 14 #include "ui/base/l10n/l10n_util.h" 15 16 namespace x509_certificate_model { 17 18 std::string ProcessIDN(const std::string& input) { 19 // Convert the ASCII input to a string16 for ICU. 20 base::string16 input16; 21 input16.reserve(input.length()); 22 input16.insert(input16.end(), input.begin(), input.end()); 23 24 base::string16 output16 = net::IDNToUnicode(input, std::string()); 25 if (input16 == output16) 26 return input; // Input did not contain any encoded data. 27 28 // Input contained encoded data, return formatted string showing original and 29 // decoded forms. 30 return l10n_util::GetStringFUTF8(IDS_CERT_INFO_IDN_VALUE_FORMAT, 31 input16, output16); 32 } 33 34 std::string ProcessRawBytesWithSeparators(const unsigned char* data, 35 size_t data_length, 36 char hex_separator, 37 char line_separator) { 38 static const char kHexChars[] = "0123456789ABCDEF"; 39 40 // Each input byte creates two output hex characters + a space or newline, 41 // except for the last byte. 42 std::string ret; 43 size_t kMin = 0U; 44 45 if (!data_length) 46 return std::string(); 47 48 ret.reserve(std::max(kMin, data_length * 3 - 1)); 49 50 for (size_t i = 0; i < data_length; ++i) { 51 unsigned char b = data[i]; 52 ret.push_back(kHexChars[(b >> 4) & 0xf]); 53 ret.push_back(kHexChars[b & 0xf]); 54 if (i + 1 < data_length) { 55 if ((i + 1) % 16 == 0) 56 ret.push_back(line_separator); 57 else 58 ret.push_back(hex_separator); 59 } 60 } 61 return ret; 62 } 63 64 std::string ProcessRawBytes(const unsigned char* data, size_t data_length) { 65 return ProcessRawBytesWithSeparators(data, data_length, ' ', '\n'); 66 } 67 68 #if defined(USE_NSS) 69 std::string ProcessRawBits(const unsigned char* data, size_t data_length) { 70 return ProcessRawBytes(data, (data_length + 7) / 8); 71 } 72 #endif // USE_NSS 73 74 } // namespace x509_certificate_model 75 76