deflate.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. if (len > MAX_COMPRESSED_SIZE) {
  97. return false;
  98. }
  99. return feed(io, in, len, inflate);
  100. }
  101. static bool
  102. inf_done(jose_io_t *io)
  103. {
  104. return done(io, inflate);
  105. }
  106. static void
  107. inf_free(jose_io_t *io)
  108. {
  109. io_t *i = containerof(io, io_t, io);
  110. inflateEnd(&i->strm);
  111. free(i);
  112. }
  113. static jose_io_t *
  114. alg_comp_def(const jose_hook_alg_t *alg, jose_cfg_t *cfg, jose_io_t *next)
  115. {
  116. jose_io_auto_t *io = NULL;
  117. io_t *i = NULL;
  118. i = calloc(1, sizeof(*i));
  119. if (!i)
  120. return NULL;
  121. io = jose_io_incref(&i->io);
  122. io->feed = def_feed;
  123. io->done = def_done;
  124. io->free = def_free;
  125. i->next = jose_io_incref(next);
  126. if (!i->next)
  127. return NULL;
  128. if (deflateInit2(&i->strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
  129. -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK)
  130. return NULL;
  131. return jose_io_incref(io);
  132. }
  133. static jose_io_t *
  134. alg_comp_inf(const jose_hook_alg_t *alg, jose_cfg_t *cfg, jose_io_t *next)
  135. {
  136. jose_io_auto_t *io = NULL;
  137. io_t *i = NULL;
  138. i = calloc(1, sizeof(*i));
  139. if (!i)
  140. return NULL;
  141. io = jose_io_incref(&i->io);
  142. io->feed = inf_feed;
  143. io->done = inf_done;
  144. io->free = inf_free;
  145. i->next = jose_io_incref(next);
  146. if (!i->next)
  147. return NULL;
  148. if (inflateInit2(&i->strm, -MAX_WBITS) != Z_OK)
  149. return NULL;
  150. return jose_io_incref(io);
  151. }
  152. static void __attribute__((constructor))
  153. constructor(void)
  154. {
  155. static jose_hook_alg_t alg = {
  156. .kind = JOSE_HOOK_ALG_KIND_COMP,
  157. .name = "DEF",
  158. .comp.def = alg_comp_def,
  159. .comp.inf = alg_comp_inf,
  160. };
  161. jose_hook_alg_push(&alg);
  162. }