deflate.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2016 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/jwe.h>
  18. #include "../hooks.h"
  19. #include <zlib.h>
  20. #include <string.h>
  21. #define containerof(ptr, type, member) \
  22. ((type *)((char *) ptr - offsetof(type, member)))
  23. static size_t SIZE = 4096;
  24. typedef struct {
  25. jose_io_t io;
  26. jose_io_t *next;
  27. z_stream strm;
  28. } io_t;
  29. static bool
  30. feed(jose_io_t *io, const void *in, size_t len, typeof(deflate) *func)
  31. {
  32. io_t *i = containerof(io, io_t, io);
  33. i->strm.next_in = (void *) in;
  34. i->strm.avail_in = len;
  35. while (i->strm.avail_in > 0 && i->strm.avail_out < SIZE) {
  36. uint8_t buf[SIZE];
  37. i->strm.next_out = buf;
  38. i->strm.avail_out = sizeof(buf);
  39. switch (func(&i->strm, Z_NO_FLUSH)) {
  40. case Z_STREAM_END: /* fallthrough */
  41. case Z_BUF_ERROR: /* fallthrough */
  42. case Z_OK:
  43. if (i->next->feed(i->next, buf, SIZE - i->strm.avail_out))
  44. break;
  45. /* fallthrough */
  46. default:
  47. return false;
  48. }
  49. }
  50. i->strm.next_in = NULL;
  51. i->strm.next_out = NULL;
  52. i->strm.avail_out = 0;
  53. return i->strm.avail_in == 0;
  54. }
  55. static bool
  56. done(jose_io_t *io, typeof(deflate) *func)
  57. {
  58. io_t *i = containerof(io, io_t, io);
  59. while (i->strm.avail_out < SIZE) {
  60. uint8_t buf[SIZE];
  61. i->strm.next_out = buf;
  62. i->strm.avail_out = sizeof(buf);
  63. switch (func(&i->strm, Z_FINISH)) {
  64. case Z_STREAM_END: /* fallthrough */
  65. case Z_BUF_ERROR: /* fallthrough */
  66. case Z_OK:
  67. if (i->next->feed(i->next, buf, SIZE - i->strm.avail_out))
  68. break;
  69. /* fallthrough */
  70. default:
  71. return false;
  72. }
  73. }
  74. return i->next->done(i->next);
  75. }
  76. static bool
  77. def_feed(jose_io_t *io, const void *in, size_t len)
  78. {
  79. return feed(io, in, len, deflate);
  80. }
  81. static bool
  82. def_done(jose_io_t *io)
  83. {
  84. return done(io, deflate);
  85. }
  86. static void
  87. def_free(jose_io_t *io)
  88. {
  89. io_t *i = containerof(io, io_t, io);
  90. deflateEnd(&i->strm);
  91. free(i);
  92. }
  93. static bool
  94. inf_feed(jose_io_t *io, const void *in, size_t len)
  95. {
  96. return feed(io, in, len, inflate);
  97. }
  98. static bool
  99. inf_done(jose_io_t *io)
  100. {
  101. return done(io, inflate);
  102. }
  103. static void
  104. inf_free(jose_io_t *io)
  105. {
  106. io_t *i = containerof(io, io_t, io);
  107. inflateEnd(&i->strm);
  108. free(i);
  109. }
  110. static jose_io_t *
  111. alg_comp_def(const jose_hook_alg_t *alg, jose_cfg_t *cfg, jose_io_t *next)
  112. {
  113. jose_io_auto_t *io = NULL;
  114. io_t *i = NULL;
  115. i = calloc(1, sizeof(*i));
  116. if (!i)
  117. return NULL;
  118. io = jose_io_incref(&i->io);
  119. io->feed = def_feed;
  120. io->done = def_done;
  121. io->free = def_free;
  122. i->next = jose_io_incref(next);
  123. if (!i->next)
  124. return NULL;
  125. if (deflateInit2(&i->strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
  126. -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK)
  127. return NULL;
  128. return jose_io_incref(io);
  129. }
  130. static jose_io_t *
  131. alg_comp_inf(const jose_hook_alg_t *alg, jose_cfg_t *cfg, jose_io_t *next)
  132. {
  133. jose_io_auto_t *io = NULL;
  134. io_t *i = NULL;
  135. i = calloc(1, sizeof(*i));
  136. if (!i)
  137. return NULL;
  138. io = jose_io_incref(&i->io);
  139. io->feed = inf_feed;
  140. io->done = inf_done;
  141. io->free = inf_free;
  142. i->next = jose_io_incref(next);
  143. if (!i->next)
  144. return NULL;
  145. if (inflateInit2(&i->strm, -MAX_WBITS) != Z_OK)
  146. return NULL;
  147. return jose_io_incref(io);
  148. }
  149. static void __attribute__((constructor))
  150. constructor(void)
  151. {
  152. static jose_hook_alg_t alg = {
  153. .kind = JOSE_HOOK_ALG_KIND_COMP,
  154. .name = "DEF",
  155. .comp.def = alg_comp_def,
  156. .comp.inf = alg_comp_inf,
  157. };
  158. jose_hook_alg_push(&alg);
  159. }