/src/ares/ares_expand_string.c

http://github.com/joyent/libuv · C · 75 lines · 43 code · 13 blank · 19 comment · 5 complexity · 80a2206659ff370cd33d77c6bd150191 MD5 · raw file

  1. /* Copyright 1998 by the Massachusetts Institute of Technology.
  2. *
  3. * Permission to use, copy, modify, and distribute this
  4. * software and its documentation for any purpose and without
  5. * fee is hereby granted, provided that the above copyright
  6. * notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting
  8. * documentation, and that the name of M.I.T. not be used in
  9. * advertising or publicity pertaining to distribution of the
  10. * software without specific, written prior permission.
  11. * M.I.T. makes no representations about the suitability of
  12. * this software for any purpose. It is provided "as is"
  13. * without express or implied warranty.
  14. */
  15. #include "ares_setup.h"
  16. #ifdef HAVE_SYS_SOCKET_H
  17. # include <sys/socket.h>
  18. #endif
  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. #include <string.h>
  28. #include <stdlib.h>
  29. #include "ares.h"
  30. #include "ares_private.h" /* for the memdebug */
  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. if (encoded == abuf+alen)
  48. return ARES_EBADSTR;
  49. elen.uns = *encoded;
  50. if (encoded+elen.sig+1 > abuf+alen)
  51. return ARES_EBADSTR;
  52. encoded++;
  53. *s = malloc(elen.uns+1);
  54. if (*s == NULL)
  55. return ARES_ENOMEM;
  56. q = *s;
  57. strncpy((char *)q, (char *)encoded, elen.uns);
  58. q[elen.uns] = '\0';
  59. *s = q;
  60. *enclen = (long)(elen.sig+1);
  61. return ARES_SUCCESS;
  62. }