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