| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #include "net/base/dns_util.h" | 5 #include "net/base/dns_util.h" |
| 6 | 6 |
| 7 #include <cstring> | 7 #include <cstring> |
| 8 | 8 |
| 9 namespace net { | 9 namespace net { |
| 10 | 10 |
| 11 // Based on DJB's public domain code. | 11 // Based on DJB's public domain code. |
| 12 bool DNSDomainFromDot(const std::string& dotted, std::string* out) { | 12 bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) { |
| 13 const char* buf = dotted.data(); | 13 const char* buf = dotted.data(); |
| 14 unsigned n = dotted.size(); | 14 unsigned n = dotted.size(); |
| 15 char label[63]; | 15 char label[63]; |
| 16 unsigned int labellen = 0; /* <= sizeof label */ | 16 unsigned int labellen = 0; /* <= sizeof label */ |
| 17 char name[255]; | 17 char name[255]; |
| 18 unsigned int namelen = 0; /* <= sizeof name */ | 18 unsigned int namelen = 0; /* <= sizeof name */ |
| 19 char ch; | 19 char ch; |
| 20 | 20 |
| 21 for (;;) { | 21 for (;;) { |
| 22 if (!n) | 22 if (!n) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 49 } | 49 } |
| 50 | 50 |
| 51 if (namelen + 1 > sizeof name) | 51 if (namelen + 1 > sizeof name) |
| 52 return false; | 52 return false; |
| 53 name[namelen++] = 0; // This is the root label (of length 0). | 53 name[namelen++] = 0; // This is the root label (of length 0). |
| 54 | 54 |
| 55 *out = std::string(name, namelen); | 55 *out = std::string(name, namelen); |
| 56 return true; | 56 return true; |
| 57 } | 57 } |
| 58 | 58 |
| 59 std::string DNSDomainToString(const std::string& domain) { | 59 std::string DNSDomainToString(const base::StringPiece& domain) { |
| 60 std::string ret; | 60 std::string ret; |
| 61 | 61 |
| 62 for (unsigned i = 0; i < domain.size() && domain[i]; i += domain[i] + 1) { | 62 for (unsigned i = 0; i < domain.size() && domain[i]; i += domain[i] + 1) { |
| 63 #if CHAR_MIN < 0 | 63 #if CHAR_MIN < 0 |
| 64 if (domain[i] < 0) | 64 if (domain[i] < 0) |
| 65 return ""; | 65 return ""; |
| 66 #endif | 66 #endif |
| 67 if (domain[i] > 63) | 67 if (domain[i] > 63) |
| 68 return ""; | 68 return ""; |
| 69 | 69 |
| 70 if (i) | 70 if (i) |
| 71 ret += "."; | 71 ret += "."; |
| 72 | 72 |
| 73 if (static_cast<unsigned>(domain[i]) + i + 1 > domain.size()) | 73 if (static_cast<unsigned>(domain[i]) + i + 1 > domain.size()) |
| 74 return ""; | 74 return ""; |
| 75 | 75 |
| 76 ret += domain.substr(i + 1, domain[i]); | 76 domain.substr(i + 1, domain[i]).AppendToString(&ret); |
| 77 } | 77 } |
| 78 return ret; | 78 return ret; |
| 79 } | 79 } |
| 80 | 80 |
| 81 bool IsSTD3ASCIIValidCharacter(char c) { | 81 bool IsSTD3ASCIIValidCharacter(char c) { |
| 82 if (c <= 0x2c) | 82 if (c <= 0x2c) |
| 83 return false; | 83 return false; |
| 84 if (c >= 0x7b) | 84 if (c >= 0x7b) |
| 85 return false; | 85 return false; |
| 86 if (c >= 0x2e && c <= 0x2f) | 86 if (c >= 0x2e && c <= 0x2f) |
| 87 return false; | 87 return false; |
| 88 if (c >= 0x3a && c <= 0x40) | 88 if (c >= 0x3a && c <= 0x40) |
| 89 return false; | 89 return false; |
| 90 if (c >= 0x5b && c <= 0x60) | 90 if (c >= 0x5b && c <= 0x60) |
| 91 return false; | 91 return false; |
| 92 return true; | 92 return true; |
| 93 } | 93 } |
| 94 | 94 |
| 95 std::string TrimEndingDot(const std::string& host) { | 95 std::string TrimEndingDot(const base::StringPiece& host) { |
| 96 std::string host_trimmed = host; | 96 base::StringPiece host_trimmed = host; |
| 97 size_t len = host_trimmed.length(); | 97 size_t len = host_trimmed.length(); |
| 98 if (len > 1 && host_trimmed[len - 1] == '.') | 98 if (len > 1 && host_trimmed[len - 1] == '.') { |
| 99 host_trimmed.erase(len - 1); | 99 host_trimmed.remove_suffix(1); |
| 100 return host_trimmed; | 100 } |
| 101 return host_trimmed.as_string(); |
| 101 } | 102 } |
| 102 | 103 |
| 103 bool DnsResponseBuffer::U8(uint8* v) { | 104 bool DnsResponseBuffer::U8(uint8* v) { |
| 104 if (len_ < 1) | 105 if (len_ < 1) |
| 105 return false; | 106 return false; |
| 106 *v = *p_; | 107 *v = *p_; |
| 107 p_++; | 108 p_++; |
| 108 len_--; | 109 len_--; |
| 109 return true; | 110 return true; |
| 110 } | 111 } |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 break; | 211 break; |
| 211 } else { | 212 } else { |
| 212 return false; | 213 return false; |
| 213 } | 214 } |
| 214 } | 215 } |
| 215 | 216 |
| 216 return true; | 217 return true; |
| 217 } | 218 } |
| 218 | 219 |
| 219 } // namespace net | 220 } // namespace net |
| OLD | NEW |