ares_create_query.c: IRIX compilation fix
[platform/upstream/c-ares.git] / 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_SYS_SOCKET_H
20 #  include <sys/socket.h>
21 #endif
22 #ifdef HAVE_NETINET_IN_H
23 #  include <netinet/in.h>
24 #endif
25 #ifdef HAVE_ARPA_NAMESER_H
26 #  include <arpa/nameser.h>
27 #else
28 #  include "nameser.h"
29 #endif
30
31 #include <string.h>
32 #include <stdlib.h>
33 #include "ares.h"
34 #include "ares_private.h" /* for the memdebug */
35
36 /* Simply decodes a length-encoded character string. The first byte of the
37  * input is the length of the string to be returned and the bytes thereafter
38  * are the characters of the string. The returned result will be NULL
39  * terminated.
40  */
41 int ares_expand_string(const unsigned char *encoded,
42                        const unsigned char *abuf,
43                        int alen,
44                        unsigned char **s,
45                        long *enclen)
46 {
47   unsigned char *q;
48   union {
49     ssize_t sig;
50      size_t uns;
51   } elen;
52
53   if (encoded == abuf+alen)
54     return ARES_EBADSTR;
55
56   elen.uns = *encoded;
57   if (encoded+elen.sig+1 > abuf+alen)
58     return ARES_EBADSTR;
59
60   encoded++;
61
62   *s = malloc(elen.uns+1);
63   if (*s == NULL)
64     return ARES_ENOMEM;
65   q = *s;
66   strncpy((char *)q, (char *)encoded, elen.uns);
67   q[elen.uns] = '\0';
68
69   *s = q;
70
71   *enclen = (long)(elen.sig+1);
72
73   return ARES_SUCCESS;
74 }
75