memref.h 4.5 KB

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