io.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2017 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <jose/io.h>
  18. #include "misc.h"
  19. #include <string.h>
  20. typedef struct {
  21. jose_io_t io;
  22. void **buf;
  23. size_t *len;
  24. } io_malloc_t;
  25. typedef struct {
  26. jose_io_t io;
  27. uint8_t *buf;
  28. size_t max;
  29. size_t *len;
  30. } io_buffer_t;
  31. typedef struct {
  32. jose_io_t io;
  33. FILE *file;
  34. } io_file_t;
  35. typedef struct {
  36. jose_io_t io;
  37. bool all;
  38. size_t nnexts;
  39. jose_io_t *nexts[];
  40. } io_plex_t;
  41. void
  42. jose_io_auto(jose_io_t **io)
  43. {
  44. if (!io || !*io)
  45. return;
  46. jose_io_decref(*io);
  47. *io = NULL;
  48. }
  49. jose_io_t *
  50. jose_io_incref(jose_io_t *io)
  51. {
  52. if (!io)
  53. return NULL;
  54. io->refs++;
  55. return io;
  56. }
  57. void
  58. jose_io_decref(jose_io_t *io)
  59. {
  60. if (!io)
  61. return;
  62. if (io->refs-- == 1)
  63. io->free(io);
  64. }
  65. static bool
  66. malloc_feed(jose_io_t *io, const void *in, size_t len)
  67. {
  68. io_malloc_t *i = containerof(io, io_malloc_t, io);
  69. uint8_t *tmp = NULL;
  70. if (len == 0)
  71. return true;
  72. tmp = jose_realloc(*i->buf, *i->len + len);
  73. if (!tmp)
  74. return false;
  75. memcpy(&tmp[*i->len], in, len);
  76. *i->buf = tmp;
  77. *i->len += len;
  78. return true;
  79. }
  80. static bool
  81. malloc_done(jose_io_t *io)
  82. {
  83. return true;
  84. }
  85. static void
  86. malloc_free(jose_io_t *io)
  87. {
  88. io_malloc_t *i = containerof(io, io_malloc_t, io);
  89. if (i->buf && *i->buf && i->len) {
  90. zero(*i->buf, *i->len);
  91. jose_free(*i->buf);
  92. *i->len = 0;
  93. }
  94. zero(i, sizeof(*i));
  95. jose_free(i);
  96. }
  97. jose_io_t *
  98. jose_io_malloc(jose_cfg_t *cfg, void **buf, size_t *len)
  99. {
  100. jose_io_auto_t *io = NULL;
  101. io_malloc_t *i = NULL;
  102. if (!buf || !len)
  103. return NULL;
  104. i = jose_malloc(sizeof(*i));
  105. if (!i)
  106. return NULL;
  107. memset(i, 0, sizeof(*i));
  108. io = jose_io_incref(&i->io);
  109. io->feed = malloc_feed;
  110. io->done = malloc_done;
  111. io->free = malloc_free;
  112. i->buf = buf;
  113. i->len = len;
  114. return jose_io_incref(io);
  115. }
  116. void *
  117. jose_io_malloc_steal(void **buf)
  118. {
  119. if (!buf)
  120. return NULL;
  121. void *out = *buf;
  122. *buf = NULL;
  123. return out;
  124. }
  125. static bool
  126. buffer_feed(jose_io_t *io, const void *in, size_t len)
  127. {
  128. io_buffer_t *i = containerof(io, io_buffer_t, io);
  129. if (len > i->max - *i->len)
  130. return false;
  131. memcpy(&i->buf[*i->len], in, len);
  132. *i->len += len;
  133. return true;
  134. }
  135. static bool
  136. buffer_done(jose_io_t *io)
  137. {
  138. return true;
  139. }
  140. static void
  141. buffer_free(jose_io_t *io)
  142. {
  143. io_buffer_t *i = containerof(io, io_buffer_t, io);
  144. zero(i, sizeof(*i));
  145. jose_free(i);
  146. }
  147. jose_io_t *
  148. jose_io_buffer(jose_cfg_t *cfg, void *buf, size_t *len)
  149. {
  150. jose_io_auto_t *io = NULL;
  151. io_buffer_t *i = NULL;
  152. if (!buf || !len)
  153. return NULL;
  154. i = jose_malloc(sizeof(*i));
  155. if (!i)
  156. return NULL;
  157. memset(i, 0, sizeof(*i));
  158. io = jose_io_incref(&i->io);
  159. io->feed = buffer_feed;
  160. io->done = buffer_done;
  161. io->free = buffer_free;
  162. i->buf = buf;
  163. i->max = *len;
  164. i->len = len;
  165. *len = 0;
  166. return jose_io_incref(io);
  167. }
  168. static bool
  169. file_feed(jose_io_t *io, const void *in, size_t len)
  170. {
  171. io_file_t *i = containerof(io, io_file_t, io);
  172. return fwrite(in, 1, len, i->file) == len;
  173. }
  174. static bool
  175. file_done(jose_io_t *io)
  176. {
  177. return true;
  178. }
  179. static void
  180. file_free(jose_io_t *io)
  181. {
  182. io_file_t *i = containerof(io, io_file_t, io);
  183. zero(i, sizeof(*i));
  184. jose_free(i);
  185. }
  186. jose_io_t *
  187. jose_io_file(jose_cfg_t *cfg, FILE *file)
  188. {
  189. jose_io_auto_t *io = NULL;
  190. io_file_t *i = NULL;
  191. if (!file)
  192. return NULL;
  193. i = jose_malloc(sizeof(*i));
  194. if (!i)
  195. return NULL;
  196. memset(i, 0, sizeof(*i));
  197. io = jose_io_incref(&i->io);
  198. io->feed = file_feed;
  199. io->done = file_done;
  200. io->free = file_free;
  201. i->file = file;
  202. return jose_io_incref(&i->io);
  203. }
  204. static bool
  205. plex_feed(jose_io_t *io, const void *in, size_t len)
  206. {
  207. io_plex_t *i = containerof(io, io_plex_t, io);
  208. bool status = false;
  209. for (size_t j = 0; j < i->nnexts; j++) {
  210. bool s = false;
  211. if (!i->nexts[j])
  212. continue;
  213. s = i->nexts[j]->feed(i->nexts[j], in, len);
  214. status |= s;
  215. if (!s) {
  216. jose_io_auto(&i->nexts[j]);
  217. if (i->all)
  218. return false;
  219. }
  220. }
  221. return status;
  222. }
  223. static bool
  224. plex_done(jose_io_t *io)
  225. {
  226. io_plex_t *i = containerof(io, io_plex_t, io);
  227. bool status = false;
  228. for (size_t j = 0; j < i->nnexts; j++) {
  229. bool s = false;
  230. if (!i->nexts[j])
  231. continue;
  232. s = i->nexts[j]->done(i->nexts[j]);
  233. status |= s;
  234. if (!s) {
  235. jose_io_auto(&i->nexts[j]);
  236. if (i->all)
  237. return false;
  238. }
  239. }
  240. return status;
  241. }
  242. static void
  243. plex_free(jose_io_t *io)
  244. {
  245. io_plex_t *i = containerof(io, io_plex_t, io);
  246. for (size_t j = 0; j < i->nnexts; j++)
  247. jose_io_decref(i->nexts[j]);
  248. zero(i, sizeof(*i));
  249. jose_free(i);
  250. }
  251. jose_io_t *
  252. jose_io_multiplex(jose_cfg_t *cfg, jose_io_t **nexts, bool all)
  253. {
  254. jose_io_auto_t *io = NULL;
  255. io_plex_t *i = NULL;
  256. size_t nnexts = 0;
  257. while (nexts && nexts[nnexts])
  258. nnexts++;
  259. i = jose_malloc(sizeof(*i) + sizeof(jose_io_t *) * nnexts);
  260. if (!i)
  261. return NULL;
  262. memset(i, 0, sizeof(*i) + sizeof(jose_io_t *) * nnexts);
  263. io = jose_io_incref(&i->io);
  264. io->feed = plex_feed;
  265. io->done = plex_done;
  266. io->free = plex_free;
  267. i->all = all;
  268. i->nnexts = nnexts;
  269. for (size_t j = 0; nexts && j < nnexts; j++)
  270. i->nexts[j] = jose_io_incref(nexts[j]);
  271. return jose_io_incref(&i->io);
  272. }