/contrib/bind9/lib/isccc/base64.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 78 lines · 36 code · 10 blank · 32 comment · 4 complexity · a1a8d9b7154f2e2c5038793aa156ff56 MD5 · raw file

  1. /*
  2. * Portions Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
  3. * Portions Copyright (C) 2001 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  11. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
  12. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. *
  17. * Portions Copyright (C) 2001 Nominum, Inc.
  18. *
  19. * Permission to use, copy, modify, and/or distribute this software for any
  20. * purpose with or without fee is hereby granted, provided that the above
  21. * copyright notice and this permission notice appear in all copies.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
  24. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
  26. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  27. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  28. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  29. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  30. */
  31. /* $Id: base64.c,v 1.8 2007/08/28 07:20:43 tbox Exp $ */
  32. /*! \file */
  33. #include <config.h>
  34. #include <isc/base64.h>
  35. #include <isc/buffer.h>
  36. #include <isc/region.h>
  37. #include <isc/result.h>
  38. #include <isccc/base64.h>
  39. #include <isccc/result.h>
  40. #include <isccc/util.h>
  41. isc_result_t
  42. isccc_base64_encode(isccc_region_t *source, int wordlength,
  43. const char *wordbreak, isccc_region_t *target)
  44. {
  45. isc_region_t sr;
  46. isc_buffer_t tb;
  47. isc_result_t result;
  48. sr.base = source->rstart;
  49. sr.length = source->rend - source->rstart;
  50. isc_buffer_init(&tb, target->rstart, target->rend - target->rstart);
  51. result = isc_base64_totext(&sr, wordlength, wordbreak, &tb);
  52. if (result != ISC_R_SUCCESS)
  53. return (result);
  54. source->rstart = source->rend;
  55. target->rstart = isc_buffer_used(&tb);
  56. return (ISC_R_SUCCESS);
  57. }
  58. isc_result_t
  59. isccc_base64_decode(const char *cstr, isccc_region_t *target) {
  60. isc_buffer_t b;
  61. isc_result_t result;
  62. isc_buffer_init(&b, target->rstart, target->rend - target->rstart);
  63. result = isc_base64_decodestring(cstr, &b);
  64. if (result != ISC_R_SUCCESS)
  65. return (result);
  66. target->rstart = isc_buffer_used(&b);
  67. return (ISC_R_SUCCESS);
  68. }