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>
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
30 return l10n_util::GetStringFUTF8(IDS_CERT_INFO_IDN_VALUE_FORMAT
,
34 std::string
ProcessRawBytesWithSeparators(const unsigned char* data
,
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.
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
);
58 ret
.push_back(hex_separator
);
64 std::string
ProcessRawBytes(const unsigned char* data
, size_t data_length
) {
65 return ProcessRawBytesWithSeparators(data
, data_length
, ' ', '\n');
69 std::string
ProcessRawBits(const unsigned char* data
, size_t data_length
) {
70 return ProcessRawBytes(data
, (data_length
+ 7) / 8);
74 } // namespace x509_certificate_model