1 /* Skip over a (potentially compressed) domain name in wire format.
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996,1999 by Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17 
18 #include <arpa/nameser.h>
19 #include <errno.h>
20 #include <shlib-compat.h>
21 
22 /* Advances *PTRPTR to skip over the compressed name it points at.
23    Returns 0 on success, -1 (with errno set) on failure.  */
24 int
___ns_name_skip(const unsigned char ** ptrptr,const unsigned char * eom)25 ___ns_name_skip (const unsigned char **ptrptr, const unsigned char *eom)
26 {
27   const unsigned char *cp;
28   unsigned int n;
29 
30   cp = *ptrptr;
31   while (cp < eom)
32     {
33       n = *cp++;
34       if (n == 0)
35         {
36           /* End of domain name without indirection.  */
37           *ptrptr = cp;
38           return 0;
39         }
40 
41       /* Check for indirection.  */
42       switch (n & NS_CMPRSFLGS)
43         {
44         case 0:                 /* Normal case, n == len.  */
45           if (eom - cp < n)
46             goto malformed;
47           cp += n;
48           break;
49         case NS_CMPRSFLGS:      /* Indirection.  */
50           if (cp == eom)
51             /* No room for second indirection byte.  */
52             goto malformed;
53           *ptrptr = cp + 1;
54           return 0;
55         default:                /* Illegal type.  */
56           goto malformed;
57         }
58     }
59 
60  malformed:
61   __set_errno (EMSGSIZE);
62   return -1;
63 }
64 versioned_symbol (libc, ___ns_name_skip, ns_name_skip, GLIBC_2_34);
65 versioned_symbol (libc, ___ns_name_skip, __ns_name_skip, GLIBC_PRIVATE);
66 libc_hidden_ver (___ns_name_skip, __ns_name_skip)
67 
68 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_9, GLIBC_2_34)
69 compat_symbol (libresolv, ___ns_name_skip, ns_name_skip, GLIBC_2_9);
70 #endif
71