ares_expand_string.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* $Id$ */
  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. #include "setup.h"
  17. #ifdef HAVE_SYS_SOCKET_H
  18. # include <sys/socket.h>
  19. #endif
  20. #ifdef HAVE_NETINET_IN_H
  21. # include <netinet/in.h>
  22. #endif
  23. #ifdef HAVE_ARPA_NAMESER_H
  24. # include <arpa/nameser.h>
  25. #else
  26. # include "nameser.h"
  27. #endif
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include "ares.h"
  31. #include "ares_private.h" /* for the memdebug */
  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. len = *encoded;
  48. if (encoded+len+1 > abuf+alen)
  49. return ARES_EBADSTR;
  50. encoded++;
  51. *s = malloc(len+1);
  52. if (*s == NULL)
  53. return ARES_ENOMEM;
  54. q = *s;
  55. strncpy((char *)q, (char *)encoded, len);
  56. q[len] = '\0';
  57. *s = q;
  58. *enclen = len+1;
  59. return ARES_SUCCESS;
  60. }