Imported Upstream version 1.18.1
[platform/upstream/c-ares.git] / src / lib / ares_expand_string.c
1
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of M.I.T. not be used in
10  * advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.
12  * M.I.T. makes no representations about the suitability of
13  * this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  */
16
17 #include "ares_setup.h"
18
19 #ifdef HAVE_NETINET_IN_H
20 #  include <netinet/in.h>
21 #endif
22
23 #include "ares_nameser.h"
24
25 #include "ares.h"
26 #include "ares_private.h" /* for the memdebug */
27
28 /* Simply decodes a length-encoded character string. The first byte of the
29  * input is the length of the string to be returned and the bytes thereafter
30  * are the characters of the string. The returned result will be NULL
31  * terminated.
32  */
33 int ares_expand_string(const unsigned char *encoded,
34                        const unsigned char *abuf,
35                        int alen,
36                        unsigned char **s,
37                        long *enclen)
38 {
39   unsigned char *q;
40   union {
41     ares_ssize_t sig;
42      size_t uns;
43   } elen;
44
45   if (encoded == abuf+alen)
46     return ARES_EBADSTR;
47
48   elen.uns = *encoded;
49   if (encoded+elen.sig+1 > abuf+alen)
50     return ARES_EBADSTR;
51
52   encoded++;
53
54   *s = ares_malloc(elen.uns+1);
55   if (*s == NULL)
56     return ARES_ENOMEM;
57   q = *s;
58   strncpy((char *)q, (char *)encoded, elen.uns);
59   q[elen.uns] = '\0';
60
61   *s = q;
62
63   *enclen = (long)(elen.sig+1);
64
65   return ARES_SUCCESS;
66 }
67