name.c revision c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfee
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <isc/assertions.h>
typedef enum {
tw_init = 0,
} tw_state;
static char digitvalue[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
};
static char hexdigits[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
static unsigned char maptolower[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
};
static unsigned int get_bit(unsigned char *, unsigned int);
static void set_bit(unsigned char *, unsigned int, unsigned int);
static void compact(dns_name_t);
/*
* Get the type of 'label'.
*/
return (dns_labeltype_ordinary);
else
return (dns_labeltype_bitstring);
}
unsigned int
unsigned int count;
/*
* The number of bits in a bitstring label.
*/
if (count == 0)
count = 256;
return (count);
}
/*
* The 'n'th most significant bit of 'label'.
*
* Notes:
* Numbering starts at 0.
*/
if (count == 0)
count = 256;
if (bit == 0)
return (dns_bitlabel_0);
return (dns_bitlabel_1);
}
void
/*
* Make 'name' empty.
*/
}
/*
* Does 'name' end in the root label?
*/
return (ISC_TRUE);
return (ISC_FALSE);
}
int
/*
* Determine the relative ordering under the DNSSEC order relation of
* 'name1' and 'name2'.
*/
l = l1;
ldiff = -1;
} else {
l = l2;
ldiff = 1;
else
ldiff = 0;
}
while (l > 0) {
l--;
l1--;
l2--;
cdiff = -1;
} else {
cdiff = 1;
else
cdiff = 0;
}
while (count > 0) {
count--;
return (-1);
return (1);
}
if (cdiff != 0)
return (cdiff);
if (count2 == 0)
return (1);
return (-1);
if (count1 == 0)
return (-1);
return (1);
} else {
if (count1 == 0)
count1 = 256;
if (count2 == 0)
count2 = 256;
cdiff = -1;
} else {
cdiff = 1;
else
cdiff = 0;
}
/* Yes, this loop is really slow! */
for (n = 0; n < count; n++) {
return (-1);
return (1);
}
if (cdiff != 0)
return (cdiff);
}
}
return (ldiff);
}
/*
* Is 'name1' a subdomain of 'name2'?
*
* Note: It makes no sense for one of the names to be relative and the
* other absolute. If both names are relative, then to be meaningfully
* compared the caller must ensure that they are both relative to the
* same domain.
*/
/* We're not going for maximal speed yet... */
return (ISC_FALSE);
while (l2 > 0) {
l1--;
l2--;
return (ISC_FALSE);
while (count2 > 0) {
count2--;
return (ISC_FALSE);
}
} else {
return (ISC_FALSE);
if (count1 == 0)
count1 = 256;
if (count2 == 0)
count2 = 256;
return (ISC_FALSE);
/* Yes, this loop is really slow! */
for (n = 0; n < count2; n++) {
return (ISC_FALSE);
}
return (ISC_FALSE);
}
}
return (ISC_TRUE);
}
unsigned int
/*
* How many labels does 'name' have?
*/
}
void
/*
* Make 'label' refer to the 'n'th least significant label of 'name'.
*/
else
}
void
unsigned int first, unsigned int n,
{
/*
* Make 'target' refer to the 'n' labels including and following
* 'first' in 'source'.
*/
REQUIRE(n > 0);
else
}
void
/*
* Make 'name' refer to region 'r'.
*/
if (r->length > 0)
else
}
void
/*
* Make 'r' refer to 'name'.
*/
}
{
char *tdata;
char c;
unsigned char dqchars[4];
/*
* Convert the textual representation of a DNS name at source
* into uncompressed wire form stored in target.
*
* Notes:
* Relative domain names will have 'origin' appended to them
* unless 'origin' is NULL, in which case relative domain names
* will remain relative.
*/
/*
* Initialize things to make the compiler happy; they're not required.
*/
n1 = 0;
n2 = 0;
vlen = 0;
digits = 0;
value = 0;
count = 0;
tbcount = 0;
bitlength = 0;
maxlength = 0;
/*
* Invalidate 'name'.
*/
/*
* Set up the state machine.
*/
if (nrem > 255)
nrem = 255;
labels = 0;
c = *tdata++;
tlen--;
switch (state) {
case tw_init:
/*
* Is this the root name?
*/
if (c == '.') {
if (tlen != 0)
return (DNS_R_EMPTYLABEL);
labels++;
*ndata++ = 0;
nrem--;
break;
}
/* FALLTHROUGH */
case tw_start:
ndata++;
nrem--;
count = 0;
if (c == '\\') {
break;
}
kind = tw_ordinary;
state = tw_ordinary;
/* FALLTHROUGH */
case tw_ordinary:
if (c == '.') {
if (count == 0)
return (DNS_R_EMPTYLABEL);
labels++;
if (tlen == 0) {
labels++;
*ndata++ = 0;
nrem--;
}
} else if (c == '\\') {
} else {
if (count >= 63)
return (DNS_R_LABELTOOLONG);
count++;
if (downcase)
c = maptolower[(int)c];
*ndata++ = c;
nrem--;
}
break;
case tw_initialescape:
if (c == '[') {
kind = tw_bitstring;
ndata++;
nrem--;
break;
}
kind = tw_ordinary;
/* FALLTHROUGH */
case tw_escape:
if (!isdigit(c)) {
if (count >= 63)
return (DNS_R_LABELTOOLONG);
count++;
if (downcase)
c = maptolower[(int)c];
*ndata++ = c;
nrem--;
state = tw_ordinary;
break;
}
digits = 0;
value = 0;
/* FALLTHROUGH */
case tw_escdecimal:
if (!isdigit(c))
return (DNS_R_BADESCAPE);
value *= 10;
value += digitvalue[(int)c];
digits++;
if (digits == 3) {
if (value > 255)
return (DNS_R_BADESCAPE);
if (count >= 63)
return (DNS_R_LABELTOOLONG);
count++;
if (downcase)
nrem--;
state = tw_ordinary;
}
break;
case tw_bitstring:
/* count is zero */
tbcount = 0;
value = 0;
if (c == 'b') {
vlen = 8;
maxlength = 256;
} else if (c == 'o') {
vlen = 8;
maxlength = 256;
} else if (c == 'x') {
vlen = 8;
maxlength = 256;
} else if (isdigit(c)) {
vlen = 32;
maxlength = 32;
n1 = 0;
n2 = 0;
digits = 0;
goto no_read;
} else
return (DNS_R_BADBITSTRING);
break;
case tw_binary:
if (c != '0' && c != '1') {
goto no_read;
}
value <<= 1;
if (c == '1')
value |= 1;
count++;
tbcount++;
if (tbcount > 256)
return (DNS_R_BITSTRINGTOOLONG);
if (count == 8) {
nrem--;
count = 0;
}
break;
case tw_octal:
if (!isdigit(c) || c == '9') {
goto no_read;
}
value <<= 3;
value += digitvalue[(int)c];
count += 3;
tbcount += 3;
if (tbcount > 256)
return (DNS_R_BITSTRINGTOOLONG);
if (count == 8) {
nrem--;
count = 0;
} else if (count == 9) {
nrem--;
value &= 1;
count = 1;
} else if (count == 10) {
nrem--;
value &= 3;
count = 2;
}
break;
case tw_hex:
if (!isxdigit(c)) {
goto no_read;
}
value <<= 4;
value += digitvalue[(int)c];
count += 4;
tbcount += 4;
if (tbcount > 256)
return (DNS_R_BITSTRINGTOOLONG);
if (count == 8) {
nrem--;
count = 0;
}
break;
case tw_dottedquad:
return (DNS_R_BADDOTTEDQUAD);
n2 *= 256;
n1++;
if (n1 == 4) {
tbcount = 32;
goto no_read;
}
value = 0;
digits = 0;
break;
case tw_dqdecimal:
if (!isdigit(c)) {
return (DNS_R_BADDOTTEDQUAD);
goto no_read;
}
digits++;
if (digits > 3)
return (DNS_R_BADDOTTEDQUAD);
value *= 10;
value += digitvalue[(int)c];
break;
case tw_maybeslash:
bitlength = 0;
if (c == '/') {
break;
}
/* FALLTHROUGH */
case tw_finishbitstring:
if (c == ']') {
if (tbcount == 0)
return (DNS_R_BADBITSTRING);
if (count > 0) {
if (n1 != 0)
nrem--;
}
if (bitlength != 0) {
return (DNS_R_BADBITSTRING);
return (DNS_R_BADBITSTRING);
/*
* Figure out correct number
* of octal digits for the
* bitlength, and compare to
* what was given.
*/
if (bitlength % 3 != 0)
n1++;
/* tbcount % 3 == 0 */
return (DNS_R_BADBITSTRING);
/*
* Figure out correct number
* of hex digits for the
* bitlength, and compare to
* what was given.
*/
if (bitlength % 4 != 0)
n1++;
/* tbcount % 4 == 0 */
return (DNS_R_BADBITSTRING);
}
if (n1 != 0) {
/*
* Are the pad bits in the
* last 'vlen' bits zero?
*/
if ((value &
return (DNS_R_BADBITSTRING);
}
} else if (kind == tw_dottedquad)
bitlength = 32;
else
if (kind == tw_dottedquad) {
if (bitlength % 8 != 0)
n1++;
return (DNS_R_NOSPACE);
nrem--;
}
}
if (bitlength == 256)
*label = 0;
else
labels++;
} else
return (DNS_R_BADBITSTRING);
break;
case tw_bitlength:
if (!isdigit(c)) {
if (bitlength == 0)
return (DNS_R_BADBITSTRING);
goto no_read;
}
bitlength *= 10;
bitlength += digitvalue[(int)c];
return (DNS_R_BADBITSTRING);
break;
case tw_eatdot:
if (c != '.')
return (DNS_R_BADBITSTRING);
if (tlen == 0) {
labels++;
*ndata++ = 0;
nrem--;
}
break;
default:
INSIST(0);
}
}
if (!done) {
if (nrem == 0)
return (DNS_R_NOSPACE);
return (DNS_R_UNEXPECTEDEND);
if (state == tw_ordinary) {
labels++;
}
return (DNS_R_NOSPACE);
while (n1 > 0) {
c = *label++;
if (downcase)
c = maptolower[(int)c];
*ndata++ = c;
n1--;
}
}
}
/*
* We should build the offsets table directly.
*/
if (saw_bitstring)
return (DNS_R_SUCCESS);
}
{
unsigned char *ndata;
char *tdata;
unsigned char c;
unsigned int labels;
char num[4];
/*
* This function assumes the name is in proper uncompressed
* wire format.
*/
/* Special handling for root label. */
labels = 0;
nlen = 0;
if (trem == 0)
return (DNS_R_NOSPACE);
*tdata++ = '.';
trem--;
}
labels--;
nlen--;
if (count == 0) {
break;
}
if (count < 64) {
while (count > 0) {
c = *ndata;
switch (c) {
case 0x22: /* '"' */
case 0x2E: /* '.' */
case 0x3B: /* ';' */
case 0x5C: /* '\\' */
/* Special modifiers in zone files. */
case 0x40: /* '@' */
case 0x24: /* '$' */
if (trem < 2)
return (DNS_R_NOSPACE);
*tdata++ = '\\';
*tdata++ = c;
ndata++;
trem -= 2;
nlen--;
break;
default:
if (c > 0x20 && c < 0x7f) {
if (trem == 0)
return (DNS_R_NOSPACE);
*tdata++ = c;
ndata++;
trem--;
nlen--;
} else {
if (trem < 4)
return (DNS_R_NOSPACE);
c);
tdata += 4;
trem -= 4;
ndata++;
nlen--;
}
}
count--;
}
} else if (count == DNS_LABELTYPE_BITSTRING) {
if (trem < 3)
return (DNS_R_NOSPACE);
*tdata++ = '\\';
*tdata++ = '[';
*tdata++ = 'x';
trem -= 3;
if (count == 0)
count = 256;
nlen--;
if (count % 8 != 0)
bytes++;
if (count % 4 != 0)
nibbles++;
return (DNS_R_NOSPACE);
while (nibbles > 0) {
c = *ndata++;
nibbles--;
if (nibbles != 0) {
i++;
nibbles--;
}
}
return (DNS_R_NOSPACE);
*tdata++ = '/';
for (i = 0; i < len; i++)
*tdata++ = ']';
} else
INSIST(0);
/*
* The following assumes names are absolute. If not, we
* fix things up later. Note that this means that in some
* cases one more byte of text buffer is required than is
* needed in the final output.
*/
if (trem == 0)
return (DNS_R_NOSPACE);
*tdata++ = '.';
trem--;
}
return (DNS_R_NOSPACE);
if (!saw_root || omit_final_dot)
trem++;
return (DNS_R_SUCCESS);
}
/* Yes, get_bit and set_bit are lame. */
static unsigned int
}
static void
if (bit)
else
}
static void
unsigned char *ndata;
offset = 0;
nlabels = 0;
while (nrem > 0) {
nrem--;
offset++;
if (count > 63) {
nrem--;
offset++;
if (count == 0)
count = 256;
n = count / 8;
if (count % 8 != 0)
n++;
count = n;
}
}
if (set_labels)
}
static void
unsigned char tail[32];
/*
* The caller MUST ensure that all bitstrings are correctly formatted
* and that the offsets table is valid.
*/
while (n > 0) {
if (n != 0) {
n--;
if (curr[0] != DNS_LABELTYPE_BITSTRING)
break;
/*
* We have consecutive bitstrings labels, and
* the more significant label ('head') has
* space.
*/
if (currbits == 0)
currbits = 256;
currindex = 0;
if (headbits == 0)
headbits = 256;
if (headrem != 0)
if (headrem != 0) {
do {
bit);
currindex++;
headindex++;
headbits++;
count--;
headrem--;
} while (headrem != 0);
}
tailindex = 0;
tailbits = 0;
while (count > 0) {
currindex++;
tailindex++;
tailbits++;
count--;
}
newbits = 0;
newindex = 0;
bit);
currindex++;
newindex++;
newbits++;
}
/* Zero remaining pad bits, if any. */
if (newrem != 0) {
count++;
while (newrem > 0) {
0);
newrem--;
newindex++;
}
}
} else {
/* We got rid of curr. */
}
/* copy head, then tail, then rest to curr. */
curr[0] = DNS_LABELTYPE_BITSTRING;
if (count == 256)
curr[1] = 0;
else
curr += 2;
head += 2;
if (headbits % 8 != 0)
count++;
while (count > 0) {
count--;
}
if (tailbits % 8 != 0)
count++;
while (count > 0) {
count--;
}
goto again;
}
}
n--;
};
}