expstring.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * @file expstring.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_MISC_EXPSTRING_H
  30. #define BADVPN_MISC_EXPSTRING_H
  31. #include <stddef.h>
  32. #include <misc/debug.h>
  33. #include <misc/exparray.h>
  34. #include <misc/bsize.h>
  35. #include <misc/memref.h>
  36. typedef struct {
  37. struct ExpArray arr;
  38. size_t n;
  39. } ExpString;
  40. static int ExpString_Init (ExpString *c);
  41. static void ExpString_Free (ExpString *c);
  42. static int ExpString_Append (ExpString *c, const char *str);
  43. static int ExpString_AppendChar (ExpString *c, char ch);
  44. static int ExpString_AppendByte (ExpString *c, uint8_t x);
  45. static int ExpString_AppendBinary (ExpString *c, const uint8_t *data, size_t len);
  46. static int ExpString_AppendBinaryMr (ExpString *c, MemRef data);
  47. static int ExpString_AppendZeros (ExpString *c, size_t len);
  48. static char * ExpString_Get (ExpString *c);
  49. static size_t ExpString_Length (ExpString *c);
  50. static MemRef ExpString_GetMr (ExpString *c);
  51. int ExpString_Init (ExpString *c)
  52. {
  53. if (!ExpArray_init(&c->arr, 1, 16)) {
  54. return 0;
  55. }
  56. c->n = 0;
  57. ((char *)c->arr.v)[c->n] = '\0';
  58. return 1;
  59. }
  60. void ExpString_Free (ExpString *c)
  61. {
  62. free(c->arr.v);
  63. }
  64. int ExpString_Append (ExpString *c, const char *str)
  65. {
  66. ASSERT(str)
  67. size_t l = strlen(str);
  68. bsize_t newsize = bsize_add(bsize_fromsize(c->n), bsize_add(bsize_fromsize(l), bsize_fromint(1)));
  69. if (newsize.is_overflow || !ExpArray_resize(&c->arr, newsize.value)) {
  70. return 0;
  71. }
  72. memcpy((char *)c->arr.v + c->n, str, l);
  73. c->n += l;
  74. ((char *)c->arr.v)[c->n] = '\0';
  75. return 1;
  76. }
  77. int ExpString_AppendChar (ExpString *c, char ch)
  78. {
  79. ASSERT(ch != '\0')
  80. bsize_t newsize = bsize_add(bsize_fromsize(c->n), bsize_fromint(2));
  81. if (newsize.is_overflow || !ExpArray_resize(&c->arr, newsize.value)) {
  82. return 0;
  83. }
  84. ((char *)c->arr.v)[c->n] = ch;
  85. c->n++;
  86. ((char *)c->arr.v)[c->n] = '\0';
  87. return 1;
  88. }
  89. int ExpString_AppendByte (ExpString *c, uint8_t x)
  90. {
  91. bsize_t newsize = bsize_add(bsize_fromsize(c->n), bsize_fromint(2));
  92. if (newsize.is_overflow || !ExpArray_resize(&c->arr, newsize.value)) {
  93. return 0;
  94. }
  95. ((uint8_t *)c->arr.v)[c->n] = x;
  96. c->n++;
  97. ((char *)c->arr.v)[c->n] = '\0';
  98. return 1;
  99. }
  100. int ExpString_AppendBinary (ExpString *c, const uint8_t *data, size_t len)
  101. {
  102. bsize_t newsize = bsize_add(bsize_fromsize(c->n), bsize_add(bsize_fromsize(len), bsize_fromint(1)));
  103. if (newsize.is_overflow || !ExpArray_resize(&c->arr, newsize.value)) {
  104. return 0;
  105. }
  106. memcpy((char *)c->arr.v + c->n, data, len);
  107. c->n += len;
  108. ((char *)c->arr.v)[c->n] = '\0';
  109. return 1;
  110. }
  111. int ExpString_AppendBinaryMr (ExpString *c, MemRef data)
  112. {
  113. return ExpString_AppendBinary(c, (uint8_t const *)data.ptr, data.len);
  114. }
  115. int ExpString_AppendZeros (ExpString *c, size_t len)
  116. {
  117. bsize_t newsize = bsize_add(bsize_fromsize(c->n), bsize_add(bsize_fromsize(len), bsize_fromint(1)));
  118. if (newsize.is_overflow || !ExpArray_resize(&c->arr, newsize.value)) {
  119. return 0;
  120. }
  121. memset((char *)c->arr.v + c->n, 0, len);
  122. c->n += len;
  123. ((char *)c->arr.v)[c->n] = '\0';
  124. return 1;
  125. }
  126. char * ExpString_Get (ExpString *c)
  127. {
  128. return (char *)c->arr.v;
  129. }
  130. size_t ExpString_Length (ExpString *c)
  131. {
  132. return c->n;
  133. }
  134. MemRef ExpString_GetMr (ExpString *c)
  135. {
  136. return MemRef_Make((char const *)c->arr.v, c->n);
  137. }
  138. #endif