File: dns_fmt.c

package info (click to toggle)
bglibs 2.04%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,468 kB
  • sloc: ansic: 15,821; perl: 674; sh: 63; makefile: 29
file content (44 lines) | stat: -rw-r--r-- 829 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "dns.h"

/** Format a DNS domain name as a dotted name. */
unsigned fmt_dns_domain(char* out, const char* domain)
{
  unsigned char ch;
  unsigned char ch2;
  char* orig;

  if (!*domain) {
    if (out)
      out[0] = '.';
    return 1;
  }
  orig = out;
  for (;;) {
    ch = *domain++;
    while (ch--) {
      ch2 = *domain++;
      if ((ch2 >= 'A') && (ch2 <= 'Z'))
	ch2 += 32;
      if (((ch2 >= 'a') && (ch2 <= 'z')) || ((ch2 >= '0') && (ch2 <= '9')) || (ch2 == '-') || (ch2 == '_')) {
	if (orig)
	  *out = ch2;
	out++;
      }
      else {
	if (orig) {
	  out[3] = '0' + (ch2 & 7); ch2 >>= 3;
	  out[2] = '0' + (ch2 & 7); ch2 >>= 3;
	  out[1] = '0' + (ch2 & 7);
	  out[0] = '\\';
	}
	out += 4;
      }
    }
    if (*domain == 0)
      break;
    if (orig)
      *out = '.';
    ++out;
  }
  return out - orig;
}