io.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 = 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. free(*i->buf);
  92. *i->len = 0;
  93. }
  94. zero(i, sizeof(*i));
  95. 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 = calloc(1, sizeof(*i));
  105. if (!i)
  106. return NULL;
  107. io = jose_io_incref(&i->io);
  108. io->feed = malloc_feed;
  109. io->done = malloc_done;
  110. io->free = malloc_free;
  111. i->buf = buf;
  112. i->len = len;
  113. return jose_io_incref(io);
  114. }
  115. void *
  116. jose_io_malloc_steal(void **buf)
  117. {
  118. if (!buf)
  119. return NULL;
  120. void *out = *buf;
  121. *buf = NULL;
  122. return out;
  123. }
  124. static bool
  125. buffer_feed(jose_io_t *io, const void *in, size_t len)
  126. {
  127. io_buffer_t *i = containerof(io, io_buffer_t, io);
  128. if (len > i->max - *i->len)
  129. return false;
  130. memcpy(&i->buf[*i->len], in, len);
  131. *i->len += len;
  132. return true;
  133. }
  134. static bool
  135. buffer_done(jose_io_t *io)
  136. {
  137. return true;
  138. }
  139. static void
  140. buffer_free(jose_io_t *io)
  141. {
  142. io_buffer_t *i = containerof(io, io_buffer_t, io);
  143. zero(i, sizeof(*i));
  144. free(i);
  145. }
  146. jose_io_t *
  147. jose_io_buffer(jose_cfg_t *cfg, void *buf, size_t *len)
  148. {
  149. jose_io_auto_t *io = NULL;
  150. io_buffer_t *i = NULL;
  151. if (!buf || !len)
  152. return NULL;
  153. i = calloc(1, sizeof(*i));
  154. if (!i)
  155. return NULL;
  156. io = jose_io_incref(&i->io);
  157. io->feed = buffer_feed;
  158. io->done = buffer_done;
  159. io->free = buffer_free;
  160. i->buf = buf;
  161. i->max = *len;
  162. i->len = len;
  163. *len = 0;
  164. return jose_io_incref(io);
  165. }
  166. static bool
  167. file_feed(jose_io_t *io, const void *in, size_t len)
  168. {
  169. io_file_t *i = containerof(io, io_file_t, io);
  170. return fwrite(in, 1, len, i->file) == len;
  171. }
  172. static bool
  173. file_done(jose_io_t *io)
  174. {
  175. return true;
  176. }
  177. static void
  178. file_free(jose_io_t *io)
  179. {
  180. io_file_t *i = containerof(io, io_file_t, io);
  181. zero(i, sizeof(*i));
  182. free(i);
  183. }
  184. jose_io_t *
  185. jose_io_file(jose_cfg_t *cfg, FILE *file)
  186. {
  187. jose_io_auto_t *io = NULL;
  188. io_file_t *i = NULL;
  189. if (!file)
  190. return NULL;
  191. i = calloc(1, sizeof(*i));
  192. if (!i)
  193. return NULL;
  194. io = jose_io_incref(&i->io);
  195. io->feed = file_feed;
  196. io->done = file_done;
  197. io->free = file_free;
  198. i->file = file;
  199. return jose_io_incref(&i->io);
  200. }
  201. static bool
  202. plex_feed(jose_io_t *io, const void *in, size_t len)
  203. {
  204. io_plex_t *i = containerof(io, io_plex_t, io);
  205. bool status = false;
  206. for (size_t j = 0; j < i->nnexts; j++) {
  207. bool s = false;
  208. if (!i->nexts[j])
  209. continue;
  210. s = i->nexts[j]->feed(i->nexts[j], in, len);
  211. status |= s;
  212. if (!s) {
  213. jose_io_auto(&i->nexts[j]);
  214. if (i->all)
  215. return false;
  216. }
  217. }
  218. return status;
  219. }
  220. static bool
  221. plex_done(jose_io_t *io)
  222. {
  223. io_plex_t *i = containerof(io, io_plex_t, io);
  224. bool status = false;
  225. for (size_t j = 0; j < i->nnexts; j++) {
  226. bool s = false;
  227. if (!i->nexts[j])
  228. continue;
  229. s = i->nexts[j]->done(i->nexts[j]);
  230. status |= s;
  231. if (!s) {
  232. jose_io_auto(&i->nexts[j]);
  233. if (i->all)
  234. return false;
  235. }
  236. }
  237. return status;
  238. }
  239. static void
  240. plex_free(jose_io_t *io)
  241. {
  242. io_plex_t *i = containerof(io, io_plex_t, io);
  243. for (size_t j = 0; j < i->nnexts; j++)
  244. jose_io_decref(i->nexts[j]);
  245. zero(i, sizeof(*i));
  246. free(i);
  247. }
  248. jose_io_t *
  249. jose_io_multiplex(jose_cfg_t *cfg, jose_io_t **nexts, bool all)
  250. {
  251. jose_io_auto_t *io = NULL;
  252. io_plex_t *i = NULL;
  253. size_t nnexts = 0;
  254. while (nexts && nexts[nnexts])
  255. nnexts++;
  256. i = calloc(1, sizeof(*i) + sizeof(jose_io_t *) * nnexts);
  257. if (!i)
  258. return NULL;
  259. io = jose_io_incref(&i->io);
  260. io->feed = plex_feed;
  261. io->done = plex_done;
  262. io->free = plex_free;
  263. i->all = all;
  264. i->nnexts = nnexts;
  265. for (size_t j = 0; nexts && j < nnexts; j++)
  266. i->nexts[j] = jose_io_incref(nexts[j]);
  267. return jose_io_incref(&i->io);
  268. }