memref.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @file memref.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_MEMREF_H
  30. #define BADVPN_MEMREF_H
  31. #include <stddef.h>
  32. #include <string.h>
  33. #include <limits.h>
  34. #include <misc/debug.h>
  35. #include <misc/balloc.h>
  36. #include <misc/strdup.h>
  37. typedef struct {
  38. char const *ptr;
  39. size_t len;
  40. } MemRef;
  41. static MemRef MemRef_Make (char const *ptr, size_t len);
  42. static char MemRef_At (MemRef o, size_t pos);
  43. static void MemRef_AssertRange (MemRef o, size_t offset, size_t length);
  44. static MemRef MemRef_SubFrom (MemRef o, size_t offset);
  45. static MemRef MemRef_SubTo (MemRef o, size_t length);
  46. static MemRef MemRef_Sub (MemRef o, size_t offset, size_t length);
  47. static char * MemRef_StrDup (MemRef o);
  48. static void MemRef_CopyOut (MemRef o, char *out);
  49. #define MEMREF_LOOP_CHARS__BODY(char_rel_pos_var, char_var, body) \
  50. { \
  51. for (size_t char_rel_pos_var = 0; char_rel_pos_var < MemRef__Loop_length; char_rel_pos_var++) { \
  52. char char_var = MemRef__Loop_o.ptr[MemRef__Loop_offset + char_rel_pos_var]; \
  53. { body } \
  54. } \
  55. }
  56. #define MEMREF_LOOP_CHARS_RANGE(o, offset, length, char_rel_pos_var, char_var, body) \
  57. { \
  58. MemRef MemRef__Loop_o = (o); \
  59. size_t MemRef__Loop_offset = (offset); \
  60. size_t MemRef__Loop_length = (length); \
  61. MEMREF_LOOP_CHARS__BODY(char_rel_pos_var, char_var, body) \
  62. }
  63. #define MEMREF_LOOP_CHARS(o, char_rel_pos_var, char_var, body) \
  64. { \
  65. MemRef MemRef__Loop_o = (o); \
  66. size_t MemRef__Loop_offset = 0; \
  67. size_t MemRef__Loop_length = MemRef__Loop_o.len; \
  68. MEMREF_LOOP_CHARS__BODY(char_rel_pos_var, char_var, body) \
  69. }
  70. //
  71. static MemRef MemRef_Make (char const *ptr, size_t len)
  72. {
  73. MemRef res;
  74. res.ptr = ptr;
  75. res.len = len;
  76. return res;
  77. }
  78. static char MemRef_At (MemRef o, size_t pos)
  79. {
  80. ASSERT(o.ptr)
  81. ASSERT(pos < o.len)
  82. return o.ptr[pos];
  83. }
  84. static void MemRef_AssertRange (MemRef o, size_t offset, size_t length)
  85. {
  86. ASSERT(offset <= o.len)
  87. ASSERT(length <= o.len - offset)
  88. }
  89. static MemRef MemRef_SubFrom (MemRef o, size_t offset)
  90. {
  91. ASSERT(o.ptr)
  92. ASSERT(offset <= o.len)
  93. return MemRef_Make(o.ptr + offset, o.len - offset);
  94. }
  95. static MemRef MemRef_SubTo (MemRef o, size_t length)
  96. {
  97. ASSERT(o.ptr)
  98. ASSERT(length <= o.len)
  99. return MemRef_Make(o.ptr, length);
  100. }
  101. static MemRef MemRef_Sub (MemRef o, size_t offset, size_t length)
  102. {
  103. ASSERT(o.ptr)
  104. MemRef_AssertRange(o, offset, length);
  105. return MemRef_Make(o.ptr + offset, length);
  106. }
  107. static char * MemRef_StrDup (MemRef o)
  108. {
  109. ASSERT(o.ptr)
  110. return b_strdup_bin(o.ptr, o.len);
  111. }
  112. static void MemRef_CopyOut (MemRef o, char *out)
  113. {
  114. ASSERT(o.ptr)
  115. ASSERT(out)
  116. memcpy(out, o.ptr, o.len);
  117. }
  118. #endif