"Fossies" - the Fresh Open Source Software Archive

Member "rbldnsd-0.998/dns_ptodn.c" (14 Mar 2013, 1720 Bytes) of package /linux/misc/dns/rbldnsd-0.998.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dns_ptodn.c" see the Fossies "Dox" file reference documentation.

    1 /* dns_ptodn() parses external textual dot-separated format
    2  * into a domain name
    3  */
    4 
    5 #include "dns.h"
    6 #include <errno.h>
    7 
    8 unsigned dns_ptodn(const char *name, unsigned char *dn, unsigned dnsiz) {
    9   unsigned char *d; /* current position in dn (len byte first) */
   10   unsigned char *label; /* start of last label */
   11   unsigned char *m; /* max byte can be filled up */
   12   unsigned l;       /* length of current label */
   13   unsigned c;       /* next input character */
   14 
   15   d = dn + 1;
   16   label = d;
   17   m = dn + (dnsiz > DNS_MAXDN ? DNS_MAXDN : dnsiz) - 1;
   18 
   19   while((c = (unsigned char)*name++) != 0) {
   20     if (c == '.') {
   21       if ((l = d - label) != 0) { /* if there was a non-empty label */
   22         if (l > DNS_MAXLABEL) {
   23           errno = EMSGSIZE;
   24           return 0;
   25         }
   26         label[-1] = (char)l; /* update len of last label */
   27         label = ++d; /* start new label, label[-1] will be len of it */
   28       }
   29       continue;
   30     }
   31     if (c == '\\') { /* handle escapes */
   32       if (!(c = (unsigned char)*name++))
   33         break;
   34       if (c >= '0' && c <= '9') { /* dec number: will be in c */
   35         c -= '0';
   36         if (*name >= '0' && *name <= '9') { /* 2digits */
   37           c = (c * 10) + (*name++ - '0');
   38           if (*name >= '0' && *name <= '9') { /* 3digits */
   39             c = (c * 10) + (*name++ - '0');
   40             if (c > 255) {
   41               errno = EINVAL;
   42               return 0;
   43             }
   44           }
   45         }
   46       }
   47     }
   48     if (d >= m) { /* too long? */
   49       errno = EMSGSIZE;
   50       return 0;
   51     }
   52     *d++ = (char)c; /* place next out byte */
   53   }
   54 
   55   if ((l = d - label) > DNS_MAXLABEL) {
   56     errno = EMSGSIZE;
   57     return 0;
   58   }
   59   if ((label[-1] = (char)l) != 0)
   60     *d++ = 0;
   61   return d - dn;
   62 }