comp_lib.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 1998-2020 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 (meth == NULL)
  20. return NULL;
  21. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
  22. return NULL;
  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. if (meth == NULL)
  37. return NID_undef;
  38. return meth->type;
  39. }
  40. const char *COMP_get_name(const COMP_METHOD *meth)
  41. {
  42. if (meth == NULL)
  43. return NULL;
  44. return meth->name;
  45. }
  46. void COMP_CTX_free(COMP_CTX *ctx)
  47. {
  48. if (ctx == NULL)
  49. return;
  50. if (ctx->meth->finish != NULL)
  51. ctx->meth->finish(ctx);
  52. OPENSSL_free(ctx);
  53. }
  54. int COMP_compress_block(COMP_CTX *ctx, unsigned char *out, int olen,
  55. unsigned char *in, int ilen)
  56. {
  57. int ret;
  58. if (ctx->meth->compress == NULL) {
  59. return -1;
  60. }
  61. ret = ctx->meth->compress(ctx, out, olen, in, ilen);
  62. if (ret > 0) {
  63. ctx->compress_in += ilen;
  64. ctx->compress_out += ret;
  65. }
  66. return ret;
  67. }
  68. int COMP_expand_block(COMP_CTX *ctx, unsigned char *out, int olen,
  69. unsigned char *in, int ilen)
  70. {
  71. int ret;
  72. if (ctx->meth->expand == NULL) {
  73. return -1;
  74. }
  75. ret = ctx->meth->expand(ctx, out, olen, in, ilen);
  76. if (ret > 0) {
  77. ctx->expand_in += ilen;
  78. ctx->expand_out += ret;
  79. }
  80. return ret;
  81. }
  82. int COMP_CTX_get_type(const COMP_CTX* comp)
  83. {
  84. return comp->meth ? comp->meth->type : NID_undef;
  85. }