expstring.h 4.5 KB

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