lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 | #ifdef HAVE_ARPA_NAMESER_H |
| 23 | # include <arpa/nameser.h> |
| 24 | #else |
| 25 | # include "nameser.h" |
| 26 | #endif |
| 27 | |
| 28 | #include "ares.h" |
| 29 | #include "ares_private.h" /* for the memdebug */ |
| 30 | |
| 31 | /* Simply decodes a length-encoded character string. The first byte of the |
| 32 | * input is the length of the string to be returned and the bytes thereafter |
| 33 | * are the characters of the string. The returned result will be NULL |
| 34 | * terminated. |
| 35 | */ |
| 36 | int ares_expand_string(const unsigned char *encoded, |
| 37 | const unsigned char *abuf, |
| 38 | int alen, |
| 39 | unsigned char **s, |
| 40 | long *enclen) |
| 41 | { |
| 42 | unsigned char *q; |
| 43 | union { |
| 44 | ssize_t sig; |
| 45 | size_t uns; |
| 46 | } elen; |
| 47 | |
| 48 | if (encoded == abuf+alen) |
| 49 | return ARES_EBADSTR; |
| 50 | |
| 51 | elen.uns = *encoded; |
| 52 | if (encoded+elen.sig+1 > abuf+alen) |
| 53 | return ARES_EBADSTR; |
| 54 | |
| 55 | encoded++; |
| 56 | |
| 57 | *s = ares_malloc(elen.uns+1); |
| 58 | if (*s == NULL) |
| 59 | return ARES_ENOMEM; |
| 60 | q = *s; |
| 61 | strncpy((char *)q, (char *)encoded, elen.uns); |
| 62 | q[elen.uns] = '\0'; |
| 63 | |
| 64 | *s = q; |
| 65 | |
| 66 | *enclen = (long)(elen.sig+1); |
| 67 | |
| 68 | return ARES_SUCCESS; |
| 69 | } |
| 70 | |