memref.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 MemRef MemRef_MakeCstr (char const *ptr);
  43. static char MemRef_At (MemRef o, size_t pos);
  44. static void MemRef_AssertRange (MemRef o, size_t offset, size_t length);
  45. static MemRef MemRef_SubFrom (MemRef o, size_t offset);
  46. static MemRef MemRef_SubTo (MemRef o, size_t length);
  47. static MemRef MemRef_Sub (MemRef o, size_t offset, size_t length);
  48. static char * MemRef_StrDup (MemRef o);
  49. static void MemRef_CopyOut (MemRef o, char *out);
  50. static int MemRef_Equal (MemRef o, MemRef other);
  51. static int MemRef_FindChar (MemRef o, char ch, size_t *out_index);
  52. #define MEMREF_LOOP_CHARS__BODY(char_rel_pos_var, char_var, body) \
  53. { \
  54. for (size_t char_rel_pos_var = 0; char_rel_pos_var < MemRef__Loop_length; char_rel_pos_var++) { \
  55. char char_var = MemRef__Loop_o.ptr[MemRef__Loop_offset + char_rel_pos_var]; \
  56. { body } \
  57. } \
  58. }
  59. #define MEMREF_LOOP_CHARS_RANGE(o, offset, length, char_rel_pos_var, char_var, body) \
  60. { \
  61. MemRef MemRef__Loop_o = (o); \
  62. size_t MemRef__Loop_offset = (offset); \
  63. size_t MemRef__Loop_length = (length); \
  64. MEMREF_LOOP_CHARS__BODY(char_rel_pos_var, char_var, body) \
  65. }
  66. #define MEMREF_LOOP_CHARS(o, char_rel_pos_var, char_var, body) \
  67. { \
  68. MemRef MemRef__Loop_o = (o); \
  69. size_t MemRef__Loop_offset = 0; \
  70. size_t MemRef__Loop_length = MemRef__Loop_o.len; \
  71. MEMREF_LOOP_CHARS__BODY(char_rel_pos_var, char_var, body) \
  72. }
  73. //
  74. static MemRef MemRef_Make (char const *ptr, size_t len)
  75. {
  76. MemRef res;
  77. res.ptr = ptr;
  78. res.len = len;
  79. return res;
  80. }
  81. static MemRef MemRef_MakeCstr (char const *ptr)
  82. {
  83. ASSERT(ptr)
  84. return MemRef_Make(ptr, strlen(ptr));
  85. }
  86. static char MemRef_At (MemRef o, size_t pos)
  87. {
  88. ASSERT(o.ptr)
  89. ASSERT(pos < o.len)
  90. return o.ptr[pos];
  91. }
  92. static void MemRef_AssertRange (MemRef o, size_t offset, size_t length)
  93. {
  94. ASSERT(offset <= o.len)
  95. ASSERT(length <= o.len - offset)
  96. }
  97. static MemRef MemRef_SubFrom (MemRef o, size_t offset)
  98. {
  99. ASSERT(o.ptr)
  100. ASSERT(offset <= o.len)
  101. return MemRef_Make(o.ptr + offset, o.len - offset);
  102. }
  103. static MemRef MemRef_SubTo (MemRef o, size_t length)
  104. {
  105. ASSERT(o.ptr)
  106. ASSERT(length <= o.len)
  107. return MemRef_Make(o.ptr, length);
  108. }
  109. static MemRef MemRef_Sub (MemRef o, size_t offset, size_t length)
  110. {
  111. ASSERT(o.ptr)
  112. MemRef_AssertRange(o, offset, length);
  113. return MemRef_Make(o.ptr + offset, length);
  114. }
  115. static char * MemRef_StrDup (MemRef o)
  116. {
  117. ASSERT(o.ptr)
  118. return b_strdup_bin(o.ptr, o.len);
  119. }
  120. static void MemRef_CopyOut (MemRef o, char *out)
  121. {
  122. ASSERT(o.ptr)
  123. ASSERT(out)
  124. memcpy(out, o.ptr, o.len);
  125. }
  126. static int MemRef_Equal (MemRef o, MemRef other)
  127. {
  128. ASSERT(o.ptr)
  129. ASSERT(other.ptr)
  130. return (o.len == other.len) && !memcmp(o.ptr, other.ptr, o.len);
  131. }
  132. static int MemRef_FindChar (MemRef o, char ch, size_t *out_index)
  133. {
  134. ASSERT(o.ptr)
  135. for (size_t i = 0; i < o.len; i++) {
  136. if (o.ptr[i] == ch) {
  137. if (out_index) {
  138. *out_index = i;
  139. }
  140. return 1;
  141. }
  142. }
  143. return 0;
  144. }
  145. #endif