]> git.ipfire.org Git - thirdparty/glibc.git/blame - resolv/ns_name.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / resolv / ns_name.c
CommitLineData
b43b13ac 1/*
cd5743fd 2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
b43b13ac
UD
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 *
cd5743fd
UD
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.
b43b13ac
UD
16 */
17
b43b13ac
UD
18#include <sys/types.h>
19
20#include <netinet/in.h>
21#include <arpa/nameser.h>
22
23#include <errno.h>
24#include <resolv.h>
25#include <string.h>
26#include <ctype.h>
cd5743fd
UD
27#include <stdlib.h>
28#include <limits.h>
29
30# define SPRINTF(x) ((size_t)sprintf x)
31
b43b13ac
UD
32/* Forward. */
33
cd5743fd 34static int labellen(const u_char *);
b43b13ac
UD
35
36/* Public. */
37
cd5743fd 38/*%
b43b13ac 39 * Convert a network strings labels into all lowercase.
cd5743fd 40 *
b43b13ac 41 * return:
cd5743fd
UD
42 *\li Number of bytes written to buffer, or -1 (with errno set)
43 *
b43b13ac 44 * notes:
cd5743fd 45 *\li Enforces label and domain length limits.
b43b13ac
UD
46 */
47
48int
cd5743fd
UD
49ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz)
50{
b43b13ac
UD
51 const u_char *cp;
52 u_char *dn, *eom;
53 u_char c;
54 u_int n;
cd5743fd 55 int l;
b43b13ac
UD
56
57 cp = src;
58 dn = dst;
59 eom = dst + dstsiz;
60
cd5743fd
UD
61 if (dn >= eom) {
62 __set_errno (EMSGSIZE);
63 return (-1);
64 }
b43b13ac 65 while ((n = *cp++) != 0) {
cd5743fd 66 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
b43b13ac
UD
67 /* Some kind of compression pointer. */
68 __set_errno (EMSGSIZE);
69 return (-1);
70 }
71 *dn++ = n;
cd5743fd 72 if ((l = labellen(cp - 1)) < 0) {
b43b13ac
UD
73 __set_errno (EMSGSIZE);
74 return (-1);
75 }
cd5743fd
UD
76 if (dn + l >= eom) {
77 __set_errno (EMSGSIZE);
78 return (-1);
79 }
80 for ((void)NULL; l > 0; l--) {
b43b13ac
UD
81 c = *cp++;
82 if (isupper(c))
83 *dn++ = tolower(c);
84 else
85 *dn++ = c;
86 }
87 }
88 *dn++ = '\0';
89 return (dn - dst);
90}
91
cd5743fd 92/*%
e685e07d
UD
93 * Reset dnptrs so that there are no active references to pointers at or
94 * after src.
95 */
96void
97ns_name_rollback(const u_char *src, const u_char **dnptrs,
98 const u_char **lastdnptr)
99{
100 while (dnptrs < lastdnptr && *dnptrs != NULL) {
101 if (*dnptrs >= src) {
102 *dnptrs = NULL;
103 break;
104 }
105 dnptrs++;
106 }
107}
108
b43b13ac
UD
109/* Private. */
110
5140d036
FW
111/* Return the length of the encoded label starting at LP, or -1 for
112 compression references and extended label types. */
cd5743fd 113static int
5140d036 114labellen (const unsigned char *lp)
cd5743fd 115{
5140d036
FW
116 if (*lp <= 63)
117 return *lp;
118 return -1;
cd5743fd
UD
119}
120
121/*! \file */