make ares_expand_name() take a long * instead of an int *, since we do
[platform/upstream/c-ares.git] / ares_expand_name.c
1 /* Copyright 1998 by the Massachusetts Institute of Technology.
2  *
3  * Permission to use, copy, modify, and distribute this
4  * software and its documentation for any purpose and without
5  * fee is hereby granted, provided that the above copyright
6  * notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting
8  * documentation, and that the name of M.I.T. not be used in
9  * advertising or publicity pertaining to distribution of the
10  * software without specific, written prior permission.
11  * M.I.T. makes no representations about the suitability of
12  * this software for any purpose.  It is provided "as is"
13  * without express or implied warranty.
14  */
15
16 #include <sys/types.h>
17
18 #ifdef WIN32
19 #include "nameser.h"
20 #else
21 #include <netinet/in.h>
22 #include <arpa/nameser.h>
23 #endif
24
25 #include <stdlib.h>
26 #include "ares.h"
27 #include "ares_private.h" /* for the memdebug */
28
29 static int name_length(const unsigned char *encoded, const unsigned char *abuf,
30                        int alen);
31
32 /* Expand an RFC1035-encoded domain name given by encoded.  The
33  * containing message is given by abuf and alen.  The result given by
34  * *s, which is set to a NUL-terminated allocated buffer.  *enclen is
35  * set to the length of the encoded name (not the length of the
36  * expanded name; the goal is to tell the caller how many bytes to
37  * move forward to get past the encoded name).
38  *
39  * In the simple case, an encoded name is a series of labels, each
40  * composed of a one-byte length (limited to values between 0 and 63
41  * inclusive) followed by the label contents.  The name is terminated
42  * by a zero-length label.
43  *
44  * In the more complicated case, a label may be terminated by an
45  * indirection pointer, specified by two bytes with the high bits of
46  * the first byte (corresponding to INDIR_MASK) set to 11.  With the
47  * two high bits of the first byte stripped off, the indirection
48  * pointer gives an offset from the beginning of the containing
49  * message with more labels to decode.  Indirection can happen an
50  * arbitrary number of times, so we have to detect loops.
51  *
52  * Since the expanded name uses '.' as a label separator, we use
53  * backslashes to escape periods or backslashes in the expanded name.
54  */
55
56 int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
57                      int alen, char **s, long *enclen)
58 {
59   int len, indir = 0;
60   char *q;
61   const unsigned char *p;
62
63   len = name_length(encoded, abuf, alen);
64   if (len == -1)
65     return ARES_EBADNAME;
66
67   *s = malloc(len + 1);
68   if (!*s)
69     return ARES_ENOMEM;
70   q = *s;
71
72   /* No error-checking necessary; it was all done by name_length(). */
73   p = encoded;
74   while (*p)
75     {
76       if ((*p & INDIR_MASK) == INDIR_MASK)
77         {
78           if (!indir)
79             {
80               *enclen = p + 2 - encoded;
81               indir = 1;
82             }
83           p = abuf + ((*p & ~INDIR_MASK) << 8 | *(p + 1));
84         }
85       else
86         {
87           len = *p;
88           p++;
89           while (len--)
90             {
91               if (*p == '.' || *p == '\\')
92                 *q++ = '\\';
93               *q++ = *p;
94               p++;
95             }
96           *q++ = '.';
97         }
98     }
99   if (!indir)
100     *enclen = p + 1 - encoded;
101
102   /* Nuke the trailing period if we wrote one. */
103   if (q > *s)
104     *(q - 1) = 0;
105
106   return ARES_SUCCESS;
107 }
108
109 /* Return the length of the expansion of an encoded domain name, or
110  * -1 if the encoding is invalid.
111  */
112 static int name_length(const unsigned char *encoded, const unsigned char *abuf,
113                        int alen)
114 {
115   int n = 0, offset, indir = 0;
116
117   /* Allow the caller to pass us abuf + alen and have us check for it. */
118   if (encoded == abuf + alen)
119     return -1;
120
121   while (*encoded)
122     {
123       if ((*encoded & INDIR_MASK) == INDIR_MASK)
124         {
125           /* Check the offset and go there. */
126           if (encoded + 1 >= abuf + alen)
127             return -1;
128           offset = (*encoded & ~INDIR_MASK) << 8 | *(encoded + 1);
129           if (offset >= alen)
130             return -1;
131           encoded = abuf + offset;
132
133           /* If we've seen more indirects than the message length,
134            * then there's a loop.
135            */
136           if (++indir > alen)
137             return -1;
138         }
139       else
140         {
141           offset = *encoded;
142           if (encoded + offset + 1 >= abuf + alen)
143             return -1;
144           encoded++;
145           while (offset--)
146             {
147               n += (*encoded == '.' || *encoded == '\\') ? 2 : 1;
148               encoded++;
149             }
150           n++;
151         }
152     }
153
154   /* If there were any labels at all, then the number of dots is one
155    * less than the number of labels, so subtract one.
156    */
157   return (n) ? n - 1 : n;
158 }