expstring.h 4.1 KB

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