remove rcsid stuff from c files, it serves no useful purpose
[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
28 static int name_length(const unsigned char *encoded, const unsigned char *abuf,
29                        int alen);
30
31 /* Expand an RFC1035-encoded domain name given by encoded.  The
32  * containing message is given by abuf and alen.  The result given by
33  * *s, which is set to a NUL-terminated allocated buffer.  *enclen is
34  * set to the length of the encoded name (not the length of the
35  * expanded name; the goal is to tell the caller how many bytes to
36  * move forward to get past the encoded name).
37  *
38  * In the simple case, an encoded name is a series of labels, each
39  * composed of a one-byte length (limited to values between 0 and 63
40  * inclusive) followed by the label contents.  The name is terminated
41  * by a zero-length label.
42  *
43  * In the more complicated case, a label may be terminated by an
44  * indirection pointer, specified by two bytes with the high bits of
45  * the first byte (corresponding to INDIR_MASK) set to 11.  With the
46  * two high bits of the first byte stripped off, the indirection
47  * pointer gives an offset from the beginning of the containing
48  * message with more labels to decode.  Indirection can happen an
49  * arbitrary number of times, so we have to detect loops.
50  *
51  * Since the expanded name uses '.' as a label separator, we use
52  * backslashes to escape periods or backslashes in the expanded name.
53  */
54
55 int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
56                      int alen, char **s, int *enclen)
57 {
58   int len, indir = 0;
59   char *q;
60   const unsigned char *p;
61
62   len = name_length(encoded, abuf, alen);
63   if (len == -1)
64     return ARES_EBADNAME;
65
66   *s = malloc(len + 1);
67   if (!*s)
68     return ARES_ENOMEM;
69   q = *s;
70
71   /* No error-checking necessary; it was all done by name_length(). */
72   p = encoded;
73   while (*p)
74     {
75       if ((*p & INDIR_MASK) == INDIR_MASK)
76         {
77           if (!indir)
78             {
79               *enclen = p + 2 - encoded;
80               indir = 1;
81             }
82           p = abuf + ((*p & ~INDIR_MASK) << 8 | *(p + 1));
83         }
84       else
85         {
86           len = *p;
87           p++;
88           while (len--)
89             {
90               if (*p == '.' || *p == '\\')
91                 *q++ = '\\';
92               *q++ = *p;
93               p++;
94             }
95           *q++ = '.';
96         }
97     }
98   if (!indir)
99     *enclen = p + 1 - encoded;
100
101   /* Nuke the trailing period if we wrote one. */
102   if (q > *s)
103     *(q - 1) = 0;
104
105   return ARES_SUCCESS;
106 }
107
108 /* Return the length of the expansion of an encoded domain name, or
109  * -1 if the encoding is invalid.
110  */
111 static int name_length(const unsigned char *encoded, const unsigned char *abuf,
112                        int alen)
113 {
114   int n = 0, offset, indir = 0;
115
116   /* Allow the caller to pass us abuf + alen and have us check for it. */
117   if (encoded == abuf + alen)
118     return -1;
119
120   while (*encoded)
121     {
122       if ((*encoded & INDIR_MASK) == INDIR_MASK)
123         {
124           /* Check the offset and go there. */
125           if (encoded + 1 >= abuf + alen)
126             return -1;
127           offset = (*encoded & ~INDIR_MASK) << 8 | *(encoded + 1);
128           if (offset >= alen)
129             return -1;
130           encoded = abuf + offset;
131
132           /* If we've seen more indirects than the message length,
133            * then there's a loop.
134            */
135           if (++indir > alen)
136             return -1;
137         }
138       else
139         {
140           offset = *encoded;
141           if (encoded + offset + 1 >= abuf + alen)
142             return -1;
143           encoded++;
144           while (offset--)
145             {
146               n += (*encoded == '.' || *encoded == '\\') ? 2 : 1;
147               encoded++;
148             }
149           n++;
150         }
151     }
152
153   /* If there were any labels at all, then the number of dots is one
154    * less than the number of labels, so subtract one.
155    */
156   return (n) ? n - 1 : n;
157 }