slice.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Copyright (c) 2016, Robert Escriva
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of this project nor the names of its contributors may
  13. * be used to endorse or promote products derived from this software
  14. * without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef macaroons_slice_h_
  29. #define macaroons_slice_h_
  30. /* c */
  31. #include <stddef.h>
  32. #define EMPTY_SLICE {NULL, 0};
  33. struct slice
  34. {
  35. const unsigned char* data;
  36. size_t size;
  37. };
  38. /* copy data_sz bytes from data to ptr, and set sl to point to the copied bytes;
  39. * return a pointer to the first unused byte after the slice
  40. */
  41. unsigned char*
  42. copy_to_slice(const unsigned char* data, size_t data_sz,
  43. struct slice* sl, unsigned char* ptr);
  44. /* unpack a slice into its parts, result points to same memory
  45. */
  46. void
  47. unstruct_slice(const struct slice* sl, const unsigned char** data, size_t* data_sz);
  48. /* copy slice from to ptr, and set to to point to the copied bytes; return a
  49. * pointer to the first unused byte after to
  50. */
  51. unsigned char*
  52. copy_slice(const struct slice* from,
  53. struct slice* to,
  54. unsigned char* ptr);
  55. /* 0 if equal; !0 if non-equal; no other comparison implied */
  56. int
  57. slice_cmp(const struct slice* lhs,
  58. const struct slice* rhs);
  59. #endif /* macaroons_slice_h_ */