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 "net/base/dns_util.h"
11 // Based on DJB's public domain code.
12 bool DNSDomainFromDot(const base::StringPiece
& dotted
, std::string
* out
) {
13 const char* buf
= dotted
.data();
14 unsigned n
= dotted
.size();
16 unsigned int labellen
= 0; /* <= sizeof label */
18 unsigned int namelen
= 0; /* <= sizeof name */
28 if (namelen
+ labellen
+ 1 > sizeof name
)
30 name
[namelen
++] = labellen
;
31 memcpy(name
+ namelen
, label
, labellen
);
37 if (labellen
>= sizeof label
)
39 label
[labellen
++] = ch
;
43 if (namelen
+ labellen
+ 1 > sizeof name
)
45 name
[namelen
++] = labellen
;
46 memcpy(name
+ namelen
, label
, labellen
);
51 if (namelen
+ 1 > sizeof name
)
53 name
[namelen
++] = 0; // This is the root label (of length 0).
55 *out
= std::string(name
, namelen
);
59 std::string
DNSDomainToString(const base::StringPiece
& domain
) {
62 for (unsigned i
= 0; i
< domain
.size() && domain
[i
]; i
+= domain
[i
] + 1) {
73 if (static_cast<unsigned>(domain
[i
]) + i
+ 1 > domain
.size())
76 domain
.substr(i
+ 1, domain
[i
]).AppendToString(&ret
);
81 bool IsSTD3ASCIIValidCharacter(char c
) {
86 if (c
>= 0x2e && c
<= 0x2f)
88 if (c
>= 0x3a && c
<= 0x40)
90 if (c
>= 0x5b && c
<= 0x60)
95 std::string
TrimEndingDot(const base::StringPiece
& host
) {
96 base::StringPiece host_trimmed
= host
;
97 size_t len
= host_trimmed
.length();
98 if (len
> 1 && host_trimmed
[len
- 1] == '.') {
99 host_trimmed
.remove_suffix(1);
101 return host_trimmed
.as_string();