name.c revision 6f5c11ea91e890e78eaa31a73e309e07f09f0ec0
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Copyright (C) 1998, 1999 Internet Software Consortium.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Permission to use, copy, modify, and distribute this software for any
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * purpose with or without fee is hereby granted, provided that the above
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * copyright notice and this permission notice appear in all copies.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleytypedef enum {
078d49b63324f01d98301ee21671abee0c41fcdeBob Halleytypedef enum {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
2cd0c38115b1efb043ed3104c0d08e51ceade0d7Bob Halley -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic unsigned char maptolower[] = {
95552051abdb3b6fc4f56b015da27c000742646bBob Halley var = default; \
95552051abdb3b6fc4f56b015da27c000742646bBob Halley set_offsets(name, var, ISC_FALSE, ISC_FALSE, ISC_FALSE); \
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic void set_offsets(dns_name_t *name, unsigned char *offsets,
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley isc_boolean_t set_labels, isc_boolean_t set_length,
95552051abdb3b6fc4f56b015da27c000742646bBob Halleystatic void compact(dns_name_t *name, unsigned char *offsets);
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley * Yes, get_bit and set_bit are lame. We define them here so they can
0d0d9a9d9895605aba3a44fd03c051e15f88ecf5Bob Halley * be inlined by smart compilers.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic unsigned int
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleyget_bit(unsigned char *array, unsigned int index) {
0d0d9a9d9895605aba3a44fd03c051e15f88ecf5Bob Halleyset_bit(unsigned char *array, unsigned int index, unsigned int bit) {
0d0d9a9d9895605aba3a44fd03c051e15f88ecf5Bob Halley * Get the type of 'label'.
0d0d9a9d9895605aba3a44fd03c051e15f88ecf5Bob Halley unsigned int count;
0d0d9a9d9895605aba3a44fd03c051e15f88ecf5Bob Halley * The number of bits in a bitstring label.
0d0d9a9d9895605aba3a44fd03c051e15f88ecf5Bob Halley REQUIRE(label->base[0] == DNS_LABELTYPE_BITSTRING);
0d0d9a9d9895605aba3a44fd03c051e15f88ecf5Bob Halleydns_label_getbit(dns_label_t *label, unsigned int n) {
0d0d9a9d9895605aba3a44fd03c051e15f88ecf5Bob Halley * The 'n'th most significant bit of 'label'.
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley * Numbering starts at 0.
0d0d9a9d9895605aba3a44fd03c051e15f88ecf5Bob Halley REQUIRE(label->base[0] == DNS_LABELTYPE_BITSTRING);
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halleydns_name_init(dns_name_t *name, unsigned char *offsets) {
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley * Make 'name' empty.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Make 'name' invalid.
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graffdns_name_setbuffer(dns_name_t *name, isc_buffer_t *buffer) {
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff * Dedicate a binary buffer for use with 'name'.
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff REQUIRE(isc_buffer_type(buffer) == ISC_BUFFERTYPE_BINARY);
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff * Does 'name' have a dedicated buffer?
return (ISC_TRUE);
return (ISC_FALSE);
unsigned int length;
if (case_sensitive) {
while (length > 0) {
length--;
while (length > 0) {
c = maptolower[*s];
length--;
int *orderp,
nlabels = 0;
nbits = 0;
l = l1;
l = l2;
ldiff = 0;
l1--;
l2--;
cdiff = 0;
while (count > 0) {
count--;
goto done;
goto done;
if (cdiff != 0) {
goto done;
nlabels++;
if (count2 == 0)
goto done;
if (count1 == 0)
goto done;
if (count1 == 0)
if (count2 == 0)
cdiff = 0;
for (n = 0; n < count; n++) {
goto done;
goto done;
if (nbits == 0)
nlabels++;
nbits++;
if (cdiff != 0) {
if (cdiff < 0) {
if (l1 > 0)
namereln =
if (l2 > 0)
namereln =
goto done;
nbits = 0;
if (ldiff < 0)
else if (ldiff > 0)
done:
return (namereln);
int order;
return (order);
unsigned int l, count;
return (ISC_FALSE);
return (ISC_FALSE);
return (ISC_FALSE);
while (count > 0) {
count--;
return (ISC_FALSE);
return (ISC_FALSE);
if (count == 0)
while (count > 0) {
count--;
c = *label1++;
if (c != *label2++)
return (ISC_FALSE);
return (ISC_TRUE);
while (count > 0) {
count--;
if (count1 == 0)
if (count2 == 0)
while (count > 0) {
count--;
int order;
&nbits);
return (ISC_TRUE);
return (ISC_FALSE);
unsigned char *offsets;
unsigned int first, unsigned int n,
unsigned char *offsets;
REQUIRE(n > 0);
unsigned char *offsets;
unsigned int len;
if (r->length > 0)
char *tdata;
unsigned char *offsets;
offsets[0] = 0;
n1 = 0;
n2 = 0;
vlen = 0;
digits = 0;
value = 0;
count = 0;
tbcount = 0;
bitlength = 0;
maxlength = 0;
tused = 0;
nused = 0;
labels = 0;
c = *tdata++;
tlen--;
tused++;
switch (state) {
case ft_init:
if (tlen != 0)
return (DNS_R_EMPTYLABEL);
labels++;
*ndata++ = 0;
nrem--;
nused++;
case ft_start:
ndata++;
nrem--;
nused++;
count = 0;
case ft_ordinary:
if (count == 0)
return (DNS_R_EMPTYLABEL);
labels++;
if (tlen == 0) {
labels++;
*ndata++ = 0;
nrem--;
nused++;
return (DNS_R_LABELTOOLONG);
count++;
CONVERTTOASCII(c);
if (downcase)
c = maptolower[(int)c];
*ndata++ = c;
nrem--;
nused++;
case ft_initialescape:
ndata++;
nrem--;
nused++;
case ft_escape:
if (!isdigit(c)) {
return (DNS_R_LABELTOOLONG);
count++;
CONVERTTOASCII(c);
if (downcase)
c = maptolower[(int)c];
*ndata++ = c;
nrem--;
nused++;
digits = 0;
value = 0;
case ft_escdecimal:
if (!isdigit(c))
return (DNS_R_BADESCAPE);
digits++;
return (DNS_R_BADESCAPE);
return (DNS_R_LABELTOOLONG);
count++;
if (downcase)
nrem--;
nused++;
case ft_bitstring:
tbcount = 0;
value = 0;
} else if (isdigit(c)) {
n1 = 0;
n2 = 0;
digits = 0;
goto no_read;
return (DNS_R_BADBITSTRING);
case ft_binary:
goto no_read;
count++;
tbcount++;
return (DNS_R_BITSTRINGTOOLONG);
nrem--;
nused++;
count = 0;
case ft_octal:
goto no_read;
return (DNS_R_BITSTRINGTOOLONG);
nrem--;
nused++;
count = 0;
nrem--;
nused++;
nrem--;
nused++;
case ft_hex:
if (!isxdigit(c)) {
goto no_read;
return (DNS_R_BITSTRINGTOOLONG);
nrem--;
nused++;
count = 0;
case ft_dottedquad:
return (DNS_R_BADDOTTEDQUAD);
n1++;
goto no_read;
value = 0;
digits = 0;
case ft_dqdecimal:
if (!isdigit(c)) {
return (DNS_R_BADDOTTEDQUAD);
goto no_read;
digits++;
return (DNS_R_BADDOTTEDQUAD);
case ft_maybeslash:
bitlength = 0;
case ft_finishbitstring:
if (tbcount == 0)
return (DNS_R_BADBITSTRING);
if (bitlength != 0) {
return (DNS_R_BADBITSTRING);
return (DNS_R_BADBITSTRING);
n1++;
return (DNS_R_BADBITSTRING);
if (value != 0)
count = 0;
n1++;
return (DNS_R_BADBITSTRING);
if (n1 != 0) {
if ((value &
return (DNS_R_BADBITSTRING);
return (DNS_R_BADBITSTRING);
if (count > 0) {
if (n1 != 0)
nrem--;
nused++;
n1++;
return (DNS_R_NOSPACE);
nrem--;
nused++;
*label = 0;
labels++;
return (DNS_R_BADBITSTRING);
case ft_bitlength:
if (!isdigit(c)) {
if (bitlength == 0)
return (DNS_R_BADBITSTRING);
goto no_read;
return (DNS_R_BADBITSTRING);
case ft_eatdot:
return (DNS_R_BADBITSTRING);
if (tlen == 0) {
labels++;
*ndata++ = 0;
nrem--;
nused++;
if (!done) {
if (nrem == 0)
return (DNS_R_NOSPACE);
return (DNS_R_UNEXPECTEDEND);
labels++;
return (DNS_R_NOSPACE);
while (n1 > 0) {
while (n2 > 0) {
c = *label++;
if (downcase)
c = maptolower[(int)c];
*ndata++ = c;
n2--;
if (bitlength == 0)
n2++;
while (n2 > 0) {
n2--;
labels++;
if (n1 > 0) {
if (saw_bitstring)
return (DNS_R_SUCCESS);
unsigned char *ndata;
char *tdata;
unsigned int labels;
labels = 0;
nlen = 0;
if (trem == 0)
return (DNS_R_NOSPACE);
trem--;
labels--;
nlen--;
if (count == 0) {
while (count > 0) {
c = *ndata;
return (DNS_R_NOSPACE);
CONVERTFROMASCII(c);
*tdata++ = c;
ndata++;
nlen--;
if (trem == 0)
return (DNS_R_NOSPACE);
CONVERTFROMASCII(c);
*tdata++ = c;
ndata++;
trem--;
nlen--;
return (DNS_R_NOSPACE);
ndata++;
nlen--;
count--;
return (DNS_R_NOSPACE);
if (count == 0)
nlen--;
bytes++;
nibbles++;
return (DNS_R_NOSPACE);
while (nibbles > 0) {
c = *ndata++;
nibbles--;
if (nibbles != 0) {
nibbles--;
return (DNS_R_NOSPACE);
for (i = 0; i < len; i++)
if (trem == 0)
return (DNS_R_NOSPACE);
trem--;
return (DNS_R_NOSPACE);
trem++;
return (DNS_R_SUCCESS);
unsigned char *ndata;
offset = 0;
nlabels = 0;
while (nrem > 0) {
nrem--;
offset++;
if (count == 0) {
n = *ndata++;
nrem--;
offset++;
count++;
if (set_labels)
if (set_length)
if (set_absolute) {
if (absolute)
if (currbits == 0)
currindex = 0;
if (headbits == 0)
if (headrem != 0)
if (headrem != 0) {
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++;
if (newrem != 0) {
count++;
while (newrem > 0) {
newrem--;
newindex++;
count++;
while (count > 0) {
count--;
count++;
while (count > 0) {
count--;
goto again;
unsigned char *offsets;
unsigned int bits;
new_current = 0;
labels = 0;
hops = 0;
nused = 0;
cused = 0;
c = *cdata++;
current++;
if (hops == 0)
cused++;
switch (state) {
case fw_start:
labels++;
return (DNS_R_NOSPACE);
*ndata++ = c;
return (DNS_R_DISALLOWED);
return (DNS_R_DISALLOWED);
} else if (c == DNS_LABELTYPE_BITSTRING) {
labels++;
if (nrem == 0)
return (DNS_R_NOSPACE);
nrem--;
nused++;
*ndata++ = c;
} else if (c == DNS_LABELTYPE_GLOBALCOMP16) {
return (DNS_R_DISALLOWED);
new_current = 0;
} else if (c == DNS_LABELTYPE_LOCALCOMP) {
return (DNS_R_DISALLOWED);
new_current = 0;
return (DNS_R_BADLABELTYPE);
case fw_ordinary:
if (downcase)
c = maptolower[c];
case fw_copy:
*ndata++ = c;
case fw_bitstring:
return (DNS_R_NOSPACE);
*ndata++ = c;
case fw_newcurrent:
new_current += c;
return (DNS_R_BADPOINTER);
return (DNS_R_BADPOINTER);
lcount--;
ll = 0;
&label);
if (labeltype ==
ll++;
if (i > lcount)
return (DNS_R_BADPOINTER);
&label);
return (DNS_R_NOSPACE);
bits--;
bits);
} while (bits != 0);
labels++;
lcount - i,
return (DNS_R_NOSPACE);
if (local)
return (DNS_R_BADPOINTER);
hops++;
return (DNS_R_TOOMANYHOPS);
if (!done)
return (DNS_R_UNEXPECTEDEND);
if (saw_bitstring)
return (DNS_R_SUCCESS);
unsigned int methods;
unsigned int offset;
if (gf) {
return (DNS_R_NOSPACE);
return (DNS_R_NOSPACE);
return (DNS_R_NOSPACE);
} else if (lf) {
return (DNS_R_NOSPACE);
return (DNS_R_NOSPACE);
return (DNS_R_NOSPACE);
return (DNS_R_NOSPACE);
return (DNS_R_SUCCESS);
if (copy_prefix &&
length = 0;
prefix_length = 0;
labels = 0;
if (copy_prefix) {
if (copy_suffix) {
return (DNS_R_NOSPACE);
if (copy_suffix) {
if (copy_prefix &&
if (absolute)
if (saw_bitstring)
return (DNS_R_SUCCESS);
(nbits != 0 &&
(*p == 0 || *p > nbits)));
if (nbits > 0) {
*p = *p - nbits;
return(DNS_R_NOSPACE);
if (mod == 0) {
while (len--) {
if (len > 0)
*dst++ |=
prefix);
if (nbits > 0) {
return (DNS_R_NOSPACE);
if (len > 0) {
src = p;
if (len > 0) {
if (mod)
return (result);
return (DNS_R_NOMEMORY);
return (DNS_R_SUCCESS);