Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2009 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 "net/base/dns_util.h"
      6 
      7 #include <cstring>
      8 
      9 namespace net {
     10 
     11 // Based on DJB's public domain code.
     12 bool DNSDomainFromDot(const std::string& dotted, std::string* out) {
     13   const char* buf = dotted.data();
     14   unsigned n = dotted.size();
     15   char label[63];
     16   unsigned int labellen = 0; /* <= sizeof label */
     17   char name[255];
     18   unsigned int namelen = 0; /* <= sizeof name */
     19   char ch;
     20 
     21   for (;;) {
     22     if (!n)
     23       break;
     24     ch = *buf++;
     25     --n;
     26     if (ch == '.') {
     27       if (labellen) {
     28         if (namelen + labellen + 1 > sizeof name)
     29           return false;
     30         name[namelen++] = labellen;
     31         memcpy(name + namelen, label, labellen);
     32         namelen += labellen;
     33         labellen = 0;
     34       }
     35       continue;
     36     }
     37     if (labellen >= sizeof label)
     38       return false;
     39     label[labellen++] = ch;
     40   }
     41 
     42   if (labellen) {
     43     if (namelen + labellen + 1 > sizeof name)
     44       return false;
     45     name[namelen++] = labellen;
     46     memcpy(name + namelen, label, labellen);
     47     namelen += labellen;
     48     labellen = 0;
     49   }
     50 
     51   if (namelen + 1 > sizeof name)
     52     return false;
     53   name[namelen++] = 0;  // This is the root label (of length 0).
     54 
     55   *out = std::string(name, namelen);
     56   return true;
     57 }
     58 
     59 std::string DNSDomainToString(const std::string& domain) {
     60   std::string ret;
     61 
     62   for (unsigned i = 0; i < domain.size() && domain[i]; i += domain[i] + 1) {
     63     if (domain[i] < 0 || domain[i] > 63)
     64       return "";
     65 
     66     if (i)
     67       ret += ".";
     68 
     69     if (static_cast<unsigned>(domain[i]) + i + 1 > domain.size())
     70       return "";
     71 
     72     ret += domain.substr(i + 1, domain[i]);
     73   }
     74   return ret;
     75 }
     76 
     77 bool IsSTD3ASCIIValidCharacter(char c) {
     78   if (c <= 0x2c)
     79     return false;
     80   if (c >= 0x7b)
     81     return false;
     82   if (c >= 0x2e && c <= 0x2f)
     83     return false;
     84   if (c >= 0x3a && c <= 0x40)
     85     return false;
     86   if (c >= 0x5b && c <= 0x60)
     87     return false;
     88   return true;
     89 }
     90 
     91 std::string TrimEndingDot(const std::string& host) {
     92   std::string host_trimmed = host;
     93   size_t len = host_trimmed.length();
     94   if (len > 1 && host_trimmed[len - 1] == '.')
     95     host_trimmed.erase(len - 1);
     96   return host_trimmed;
     97 }
     98 
     99 }  // namespace net
    100