cstring.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @file composed_string.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_COMPOSED_STRING_H
  30. #define BADVPN_COMPOSED_STRING_H
  31. #include <stddef.h>
  32. #include <string.h>
  33. #include <limits.h>
  34. #include <misc/debug.h>
  35. #include <misc/balloc.h>
  36. struct b_cstring_s;
  37. typedef const char * (*b_cstring_func) (const struct b_cstring_s *cstr, size_t offset, size_t *out_length);
  38. typedef struct b_cstring_s {
  39. size_t length;
  40. b_cstring_func func;
  41. union {
  42. size_t size;
  43. void *ptr;
  44. void (*fptr) (void);
  45. } user1;
  46. union {
  47. size_t size;
  48. void *ptr;
  49. void (*fptr) (void);
  50. } user2;
  51. union {
  52. size_t size;
  53. void *ptr;
  54. void (*fptr) (void);
  55. } user3;
  56. } b_cstring;
  57. static b_cstring b_cstring_make_buf (const char *data, size_t length);
  58. static const char * b_cstring_get (b_cstring cstr, size_t offset, size_t maxlen, size_t *out_chunk_len);
  59. static void b_cstring_assert_range (b_cstring cstr, size_t offset, size_t length);
  60. static void b_cstring_copy_to_buf (b_cstring cstr, size_t offset, size_t length, char *dest);
  61. static int b_cstring_memcmp (b_cstring cstr1, b_cstring cstr2, size_t offset1, size_t offset2, size_t length);
  62. static char * b_cstring_strdup (b_cstring cstr, size_t offset, size_t length);
  63. #define B_CSTRING_LOOP_RANGE(cstr, offset, length, rel_pos_var, chunk_data_var, chunk_length_var, body) \
  64. { \
  65. size_t rel_pos_var = 0; \
  66. while (rel_pos_var < (length)) { \
  67. size_t chunk_length_var; \
  68. const char *chunk_data_var = b_cstring_get((cstr), (offset) + rel_pos_var, (length) - rel_pos_var, &chunk_length_var); \
  69. { body } \
  70. rel_pos_var += chunk_length_var; \
  71. } \
  72. }
  73. #define B_CSTRING_LOOP(cstr, rel_pos_var, chunk_data_var, chunk_length_var, body) B_CSTRING_LOOP_RANGE(cstr, 0, (cstr).length, rel_pos_var, chunk_data_var, chunk_length_var, body)
  74. static const char * b_cstring__buf_func (const b_cstring *cstr, size_t offset, size_t *out_length)
  75. {
  76. ASSERT(offset < cstr->length)
  77. ASSERT(out_length)
  78. ASSERT(cstr->func == b_cstring__buf_func)
  79. ASSERT(cstr->user1.ptr)
  80. *out_length = cstr->length - offset;
  81. return (const char *)cstr->user1.ptr + offset;
  82. }
  83. static b_cstring b_cstring_make_buf (const char *data, size_t length)
  84. {
  85. ASSERT(length == 0 || data)
  86. b_cstring cstr;
  87. cstr.length = length;
  88. cstr.func = b_cstring__buf_func;
  89. cstr.user1.ptr = (void *)data;
  90. return cstr;
  91. }
  92. static const char * b_cstring_get (b_cstring cstr, size_t offset, size_t maxlen, size_t *out_chunk_len)
  93. {
  94. ASSERT(offset < cstr.length)
  95. ASSERT(maxlen > 0)
  96. ASSERT(out_chunk_len)
  97. const char *data = cstr.func(&cstr, offset, out_chunk_len);
  98. ASSERT(data)
  99. ASSERT(*out_chunk_len > 0)
  100. if (*out_chunk_len > maxlen) {
  101. *out_chunk_len = maxlen;
  102. }
  103. return data;
  104. }
  105. static void b_cstring_assert_range (b_cstring cstr, size_t offset, size_t length)
  106. {
  107. ASSERT(offset <= cstr.length)
  108. ASSERT(length <= cstr.length - offset)
  109. }
  110. static void b_cstring_copy_to_buf (b_cstring cstr, size_t offset, size_t length, char *dest)
  111. {
  112. b_cstring_assert_range(cstr, offset, length);
  113. ASSERT(length == 0 || dest)
  114. B_CSTRING_LOOP_RANGE(cstr, offset, length, pos, chunk_data, chunk_length, {
  115. memcpy(dest + pos, chunk_data, chunk_length);
  116. })
  117. }
  118. static int b_cstring_memcmp (b_cstring cstr1, b_cstring cstr2, size_t offset1, size_t offset2, size_t length)
  119. {
  120. b_cstring_assert_range(cstr1, offset1, length);
  121. b_cstring_assert_range(cstr2, offset2, length);
  122. B_CSTRING_LOOP_RANGE(cstr1, offset1, length, pos1, chunk_data1, chunk_len1, {
  123. B_CSTRING_LOOP_RANGE(cstr2, offset2 + pos1, chunk_len1, pos2, chunk_data2, chunk_len2, {
  124. int cmp = memcmp(chunk_data1 + pos2, chunk_data2, chunk_len2);
  125. if (cmp) {
  126. return cmp;
  127. }
  128. })
  129. })
  130. return 0;
  131. }
  132. static char * b_cstring_strdup (b_cstring cstr, size_t offset, size_t length)
  133. {
  134. b_cstring_assert_range(cstr, offset, length);
  135. if (length == SIZE_MAX) {
  136. return NULL;
  137. }
  138. char *buf = BAlloc(length + 1);
  139. if (buf) {
  140. b_cstring_copy_to_buf(cstr, offset, length, buf);
  141. buf[length] = '\0';
  142. }
  143. return buf;
  144. }
  145. #endif