comp_lib.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <openssl/objects.h>
  13. #include <openssl/comp.h>
  14. #include "comp_lcl.h"
  15. COMP_CTX *COMP_CTX_new(COMP_METHOD *meth)
  16. {
  17. COMP_CTX *ret;
  18. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
  19. return (NULL);
  20. ret->meth = meth;
  21. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  22. OPENSSL_free(ret);
  23. ret = NULL;
  24. }
  25. return (ret);
  26. }
  27. const COMP_METHOD *COMP_CTX_get_method(const COMP_CTX *ctx)
  28. {
  29. return ctx->meth;
  30. }
  31. int COMP_get_type(const COMP_METHOD *meth)
  32. {
  33. return meth->type;
  34. }
  35. const char *COMP_get_name(const COMP_METHOD *meth)
  36. {
  37. return meth->name;
  38. }
  39. void COMP_CTX_free(COMP_CTX *ctx)
  40. {
  41. if (ctx == NULL)
  42. return;
  43. if (ctx->meth->finish != NULL)
  44. ctx->meth->finish(ctx);
  45. OPENSSL_free(ctx);
  46. }
  47. int COMP_compress_block(COMP_CTX *ctx, unsigned char *out, int olen,
  48. unsigned char *in, int ilen)
  49. {
  50. int ret;
  51. if (ctx->meth->compress == NULL) {
  52. return (-1);
  53. }
  54. ret = ctx->meth->compress(ctx, out, olen, in, ilen);
  55. if (ret > 0) {
  56. ctx->compress_in += ilen;
  57. ctx->compress_out += ret;
  58. }
  59. return (ret);
  60. }
  61. int COMP_expand_block(COMP_CTX *ctx, unsigned char *out, int olen,
  62. unsigned char *in, int ilen)
  63. {
  64. int ret;
  65. if (ctx->meth->expand == NULL) {
  66. return (-1);
  67. }
  68. ret = ctx->meth->expand(ctx, out, olen, in, ilen);
  69. if (ret > 0) {
  70. ctx->expand_in += ilen;
  71. ctx->expand_out += ret;
  72. }
  73. return (ret);
  74. }
  75. int COMP_CTX_get_type(const COMP_CTX* comp)
  76. {
  77. return comp->meth ? comp->meth->type : NID_undef;
  78. }