1 /* Compression of DNS domain names.
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <arpa/nameser.h>
21 #include <shlib-compat.h>
23 /* Thinking in noninternationalized USASCII (per the DNS spec),
24 convert this character to lower case if it's upper case. */
28 if (ch
>= 'A' && ch
<= 'Z')
29 return ch
- 'A' + 'a';
33 /* Search for the counted-label name in an array of compressed names.
34 Returns the offset from MSG if found, or -1.
36 DNPTRS is the pointer to the first name on the list, not the
37 pointer to the start of the message. */
39 dn_find (const unsigned char *domain
, const unsigned char *msg
,
40 const unsigned char **dnptrs
,
41 const unsigned char **lastdnptr
)
43 const unsigned char *dn
, *cp
, *sp
;
44 const unsigned char **cpp
;
47 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++)
50 /* Terminate search on: root label, compression pointer, unusable
52 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 && (sp
- msg
) < 0x4000)
56 while ((n
= *cp
++) != 0)
58 /* Check for indirection. */
59 switch (n
& NS_CMPRSFLGS
)
61 case 0: /* Normal case, n == len. */
66 if (mklower (*dn
++) != mklower (*cp
++))
68 /* Is next root for both? */
69 if (*dn
== '\0' && *cp
== '\0')
74 case NS_CMPRSFLGS
: /* Indirection. */
75 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
78 default: /* Illegal type. */
79 __set_errno (EMSGSIZE
);
91 /* Packs domain name SRC into DST. Returns size of the compressed
94 DNPTRS is an array of pointers to previous compressed names.
95 DNPTRS[0] is a pointer to the beginning of the message. The array
96 ends with NULL. LASTDNPTR is a pointer to the end of the array
97 pointed to by 'dnptrs'.
99 The list of pointers in DNPTRS is updated for labels inserted into
100 the message as we compress the name. If DNPTRS is NULL, we don't
101 try to compress names. If LASTDNPTR is NULL, we don't update the
104 ___ns_name_pack (const unsigned char *src
, unsigned char *dst
, int dstsiz
,
105 const unsigned char **dnptrs
, const unsigned char **lastdnptr
)
108 const unsigned char **cpp
, **lpp
, *eob
, *msg
;
109 const unsigned char *srcp
;
118 if ((msg
= *dnptrs
++) != NULL
)
120 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
122 lpp
= cpp
; /* End of list to search. */
128 /* Make sure the domain we are about to add is legal. */
135 __set_errno (EMSGSIZE
);
141 __set_errno (EMSGSIZE
);
148 /* from here on we need to reset compression pointer array on error */
152 /* Look to see if we can use pointers. */
154 if (n
!= 0 && msg
!= NULL
)
156 l
= dn_find (srcp
, msg
, dnptrs
, lpp
);
161 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
165 /* Not found, save it. */
166 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1
167 && (dstp
- msg
) < 0x4000 && first
)
174 /* Copy label to buffer. */
176 /* Should not happen. */
178 if (n
+ 1 > eob
- dstp
)
180 memcpy (dstp
, srcp
, n
+ 1);
191 __set_errno (EMSGSIZE
);
196 versioned_symbol (libc
, ___ns_name_pack
, ns_name_pack
, GLIBC_2_34
);
197 versioned_symbol (libc
, ___ns_name_pack
, __ns_name_pack
, GLIBC_PRIVATE
);
198 libc_hidden_ver (___ns_name_pack
, __ns_name_pack
)
200 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_9, GLIBC_2_34)
201 compat_symbol (libresolv
, ___ns_name_pack
, ns_name_pack
, GLIBC_2_9
);