comp_lib.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 1998-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 <openssl/err.h>
  15. #include "comp_local.h"
  16. COMP_CTX *COMP_CTX_new(COMP_METHOD *meth)
  17. {
  18. COMP_CTX *ret;
  19. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
  20. COMPerr(COMP_F_COMP_CTX_NEW, ERR_R_MALLOC_FAILURE);
  21. return NULL;
  22. }
  23. ret->meth = meth;
  24. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  25. OPENSSL_free(ret);
  26. ret = NULL;
  27. }
  28. return ret;
  29. }
  30. const COMP_METHOD *COMP_CTX_get_method(const COMP_CTX *ctx)
  31. {
  32. return ctx->meth;
  33. }
  34. int COMP_get_type(const COMP_METHOD *meth)
  35. {
  36. return meth->type;
  37. }
  38. const char *COMP_get_name(const COMP_METHOD *meth)
  39. {
  40. return meth->name;
  41. }
  42. void COMP_CTX_free(COMP_CTX *ctx)
  43. {
  44. if (ctx == NULL)
  45. return;
  46. if (ctx->meth->finish != NULL)
  47. ctx->meth->finish(ctx);
  48. OPENSSL_free(ctx);
  49. }
  50. int COMP_compress_block(COMP_CTX *ctx, unsigned char *out, int olen,
  51. unsigned char *in, int ilen)
  52. {
  53. int ret;
  54. if (ctx->meth->compress == NULL) {
  55. return -1;
  56. }
  57. ret = ctx->meth->compress(ctx, out, olen, in, ilen);
  58. if (ret > 0) {
  59. ctx->compress_in += ilen;
  60. ctx->compress_out += ret;
  61. }
  62. return ret;
  63. }
  64. int COMP_expand_block(COMP_CTX *ctx, unsigned char *out, int olen,
  65. unsigned char *in, int ilen)
  66. {
  67. int ret;
  68. if (ctx->meth->expand == NULL) {
  69. return -1;
  70. }
  71. ret = ctx->meth->expand(ctx, out, olen, in, ilen);
  72. if (ret > 0) {
  73. ctx->expand_in += ilen;
  74. ctx->expand_out += ret;
  75. }
  76. return ret;
  77. }
  78. int COMP_CTX_get_type(const COMP_CTX* comp)
  79. {
  80. return comp->meth ? comp->meth->type : NID_undef;
  81. }