Resurrect battery_status_dispatcher_unittest.
[chromium-blink-merge.git] / chrome / common / net / x509_certificate_model.cc
bloba7bb46c486262d3c41778e01ab1a3c86938fb0ba
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.
5 #include "chrome/common/net/x509_certificate_model.h"
7 #include <unicode/uidna.h>
9 #include <algorithm>
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"
16 namespace x509_certificate_model {
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());
24 base::string16 output16 = net::IDNToUnicode(input, std::string());
25 if (input16 == output16)
26 return input; // Input did not contain any encoded data.
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);
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";
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;
45 if (!data_length)
46 return std::string();
48 ret.reserve(std::max(kMin, data_length * 3 - 1));
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);
61 return ret;
64 std::string ProcessRawBytes(const unsigned char* data, size_t data_length) {
65 return ProcessRawBytesWithSeparators(data, data_length, ' ', '\n');
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);
72 #endif // USE_NSS
74 } // namespace x509_certificate_model