esl_buffer.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (c) 2010-2012, Anthony Minessale II
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of the original author; nor the names of any contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  25. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "esl_buffer.h"
  34. static unsigned buffer_id = 0;
  35. struct esl_buffer {
  36. unsigned char *data;
  37. unsigned char *head;
  38. esl_size_t used;
  39. esl_size_t actually_used;
  40. esl_size_t datalen;
  41. esl_size_t max_len;
  42. esl_size_t blocksize;
  43. unsigned id;
  44. int loops;
  45. };
  46. ESL_DECLARE(esl_status_t) esl_buffer_create(esl_buffer_t **buffer, esl_size_t blocksize, esl_size_t start_len, esl_size_t max_len)
  47. {
  48. esl_buffer_t *new_buffer;
  49. new_buffer = malloc(sizeof(*new_buffer));
  50. if (new_buffer) {
  51. memset(new_buffer, 0, sizeof(*new_buffer));
  52. if (start_len) {
  53. new_buffer->data = malloc(start_len);
  54. if (!new_buffer->data) {
  55. free(new_buffer);
  56. return ESL_FAIL;
  57. }
  58. memset(new_buffer->data, 0, start_len);
  59. }
  60. new_buffer->max_len = max_len;
  61. new_buffer->datalen = start_len;
  62. new_buffer->id = buffer_id++;
  63. new_buffer->blocksize = blocksize;
  64. new_buffer->head = new_buffer->data;
  65. *buffer = new_buffer;
  66. return ESL_SUCCESS;
  67. }
  68. return ESL_FAIL;
  69. }
  70. ESL_DECLARE(esl_size_t) esl_buffer_len(esl_buffer_t *buffer)
  71. {
  72. esl_assert(buffer != NULL);
  73. return buffer->datalen;
  74. }
  75. ESL_DECLARE(esl_size_t) esl_buffer_freespace(esl_buffer_t *buffer)
  76. {
  77. esl_assert(buffer != NULL);
  78. if (buffer->max_len) {
  79. return (esl_size_t) (buffer->max_len - buffer->used);
  80. }
  81. return 1000000;
  82. }
  83. ESL_DECLARE(esl_size_t) esl_buffer_inuse(esl_buffer_t *buffer)
  84. {
  85. esl_assert(buffer != NULL);
  86. return buffer->used;
  87. }
  88. ESL_DECLARE(esl_size_t) esl_buffer_seek(esl_buffer_t *buffer, esl_size_t datalen)
  89. {
  90. esl_size_t reading = 0;
  91. esl_assert(buffer != NULL);
  92. if (buffer->used < 1) {
  93. buffer->used = 0;
  94. return 0;
  95. } else if (buffer->used >= datalen) {
  96. reading = datalen;
  97. } else {
  98. reading = buffer->used;
  99. }
  100. buffer->used = buffer->actually_used - reading;
  101. buffer->head = buffer->data + reading;
  102. return reading;
  103. }
  104. ESL_DECLARE(esl_size_t) esl_buffer_toss(esl_buffer_t *buffer, esl_size_t datalen)
  105. {
  106. esl_size_t reading = 0;
  107. esl_assert(buffer != NULL);
  108. if (buffer->used < 1) {
  109. buffer->used = 0;
  110. return 0;
  111. } else if (buffer->used >= datalen) {
  112. reading = datalen;
  113. } else {
  114. reading = buffer->used;
  115. }
  116. buffer->used -= reading;
  117. buffer->head += reading;
  118. return buffer->used;
  119. }
  120. ESL_DECLARE(void) esl_buffer_set_loops(esl_buffer_t *buffer, int loops)
  121. {
  122. buffer->loops = loops;
  123. }
  124. ESL_DECLARE(esl_size_t) esl_buffer_read_loop(esl_buffer_t *buffer, void *data, esl_size_t datalen)
  125. {
  126. esl_size_t len;
  127. if ((len = esl_buffer_read(buffer, data, datalen)) < datalen) {
  128. if (buffer->loops == 0) {
  129. return len;
  130. }
  131. buffer->head = buffer->data;
  132. buffer->used = buffer->actually_used;
  133. len = esl_buffer_read(buffer, (char*)data + len, datalen - len);
  134. buffer->loops--;
  135. }
  136. return len;
  137. }
  138. ESL_DECLARE(esl_size_t) esl_buffer_read(esl_buffer_t *buffer, void *data, esl_size_t datalen)
  139. {
  140. esl_size_t reading = 0;
  141. esl_assert(buffer != NULL);
  142. esl_assert(data != NULL);
  143. if (buffer->used < 1) {
  144. buffer->used = 0;
  145. return 0;
  146. } else if (buffer->used >= datalen) {
  147. reading = datalen;
  148. } else {
  149. reading = buffer->used;
  150. }
  151. memcpy(data, buffer->head, reading);
  152. buffer->used -= reading;
  153. buffer->head += reading;
  154. /* if (buffer->id == 4) printf("%u o %d = %d\n", buffer->id, (unsigned)reading, (unsigned)buffer->used); */
  155. return reading;
  156. }
  157. ESL_DECLARE(esl_size_t) esl_buffer_packet_count(esl_buffer_t *buffer)
  158. {
  159. char *pe, *p, *e, *head = (char *) buffer->head;
  160. esl_size_t x = 0;
  161. esl_assert(buffer != NULL);
  162. e = (head + buffer->used);
  163. for (p = head; p && *p && p < e; p++) {
  164. if (*p == '\n') {
  165. pe = p+1;
  166. if (*pe == '\r') pe++;
  167. if (pe <= e && *pe == '\n') {
  168. p = pe++;
  169. x++;
  170. }
  171. }
  172. }
  173. return x;
  174. }
  175. ESL_DECLARE(esl_size_t) esl_buffer_read_packet(esl_buffer_t *buffer, void *data, esl_size_t maxlen)
  176. {
  177. char *pe, *p, *e, *head = (char *) buffer->head;
  178. esl_size_t datalen = 0;
  179. esl_assert(buffer != NULL);
  180. esl_assert(data != NULL);
  181. e = (head + buffer->used);
  182. for (p = head; p && *p && p < e; p++) {
  183. if (*p == '\n') {
  184. pe = p+1;
  185. if (*pe == '\r') pe++;
  186. if (pe <= e && *pe == '\n') {
  187. pe++;
  188. datalen = pe - head;
  189. if (datalen > maxlen) {
  190. datalen = maxlen;
  191. }
  192. break;
  193. }
  194. }
  195. }
  196. return esl_buffer_read(buffer, data, datalen);
  197. }
  198. ESL_DECLARE(esl_size_t) esl_buffer_write(esl_buffer_t *buffer, const void *data, esl_size_t datalen)
  199. {
  200. esl_size_t freespace, actual_freespace;
  201. esl_assert(buffer != NULL);
  202. esl_assert(data != NULL);
  203. esl_assert(buffer->data != NULL);
  204. if (!datalen) {
  205. return buffer->used;
  206. }
  207. actual_freespace = buffer->datalen - buffer->actually_used;
  208. if (actual_freespace < datalen && (!buffer->max_len || (buffer->used + datalen <= buffer->max_len))) {
  209. memmove(buffer->data, buffer->head, buffer->used);
  210. buffer->head = buffer->data;
  211. buffer->actually_used = buffer->used;
  212. }
  213. freespace = buffer->datalen - buffer->used;
  214. /*
  215. if (buffer->data != buffer->head) {
  216. memmove(buffer->data, buffer->head, buffer->used);
  217. buffer->head = buffer->data;
  218. }
  219. */
  220. if (freespace < datalen) {
  221. esl_size_t new_size, new_block_size;
  222. void *data1;
  223. new_size = buffer->datalen + datalen;
  224. new_block_size = buffer->datalen + buffer->blocksize;
  225. if (new_block_size > new_size) {
  226. new_size = new_block_size;
  227. }
  228. buffer->head = buffer->data;
  229. data1 = realloc(buffer->data, new_size);
  230. if (!data1) {
  231. return 0;
  232. }
  233. buffer->data = data1;
  234. buffer->head = buffer->data;
  235. buffer->datalen = new_size;
  236. }
  237. freespace = buffer->datalen - buffer->used;
  238. if (freespace < datalen) {
  239. return 0;
  240. } else {
  241. memcpy(buffer->head + buffer->used, data, datalen);
  242. buffer->used += datalen;
  243. buffer->actually_used += datalen;
  244. }
  245. /* if (buffer->id == 4) printf("%u i %d = %d\n", buffer->id, (unsigned)datalen, (unsigned)buffer->used); */
  246. return buffer->used;
  247. }
  248. ESL_DECLARE(void) esl_buffer_zero(esl_buffer_t *buffer)
  249. {
  250. esl_assert(buffer != NULL);
  251. esl_assert(buffer->data != NULL);
  252. buffer->used = 0;
  253. buffer->actually_used = 0;
  254. buffer->head = buffer->data;
  255. }
  256. ESL_DECLARE(esl_size_t) esl_buffer_zwrite(esl_buffer_t *buffer, const void *data, esl_size_t datalen)
  257. {
  258. esl_size_t w;
  259. if (!(w = esl_buffer_write(buffer, data, datalen))) {
  260. esl_buffer_zero(buffer);
  261. return esl_buffer_write(buffer, data, datalen);
  262. }
  263. return w;
  264. }
  265. ESL_DECLARE(void) esl_buffer_destroy(esl_buffer_t **buffer)
  266. {
  267. if (*buffer) {
  268. free((*buffer)->data);
  269. (*buffer)->data = NULL;
  270. free(*buffer);
  271. }
  272. *buffer = NULL;
  273. }
  274. /* For Emacs:
  275. * Local Variables:
  276. * mode:c
  277. * indent-tabs-mode:t
  278. * tab-width:4
  279. * c-basic-offset:4
  280. * End:
  281. * For VIM:
  282. * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
  283. */