Rich Salz | 6286757 | 2016-05-17 14:24:46 -0400 | [diff] [blame] | 1 | /* |
Matt Caswell | c4d3c19 | 2018-04-03 13:57:12 +0100 | [diff] [blame] | 2 | * Copyright 1998-2018 The OpenSSL Project Authors. All Rights Reserved. |
Rich Salz | 9a55570 | 2015-05-08 12:05:36 -0400 | [diff] [blame] | 3 | * |
Rich Salz | 6286757 | 2016-05-17 14:24:46 -0400 | [diff] [blame] | 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 |
Rich Salz | 9a55570 | 2015-05-08 12:05:36 -0400 | [diff] [blame] | 8 | */ |
| 9 | |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 13 | #include <openssl/objects.h> |
| 14 | #include <openssl/comp.h> |
Rich Salz | fe1128d | 2018-04-26 14:02:24 -0400 | [diff] [blame] | 15 | #include <openssl/err.h> |
Rich Salz | 9a55570 | 2015-05-08 12:05:36 -0400 | [diff] [blame] | 16 | #include "comp_lcl.h" |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 17 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 18 | COMP_CTX *COMP_CTX_new(COMP_METHOD *meth) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 19 | { |
| 20 | COMP_CTX *ret; |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 21 | |
Rich Salz | fe1128d | 2018-04-26 14:02:24 -0400 | [diff] [blame] | 22 | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) { |
| 23 | COMPerr(COMP_F_COMP_CTX_NEW, ERR_R_MALLOC_FAILURE); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 24 | return NULL; |
Rich Salz | fe1128d | 2018-04-26 14:02:24 -0400 | [diff] [blame] | 25 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 26 | ret->meth = meth; |
| 27 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { |
| 28 | OPENSSL_free(ret); |
| 29 | ret = NULL; |
| 30 | } |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 31 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 32 | } |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 33 | |
Rich Salz | 9a55570 | 2015-05-08 12:05:36 -0400 | [diff] [blame] | 34 | const COMP_METHOD *COMP_CTX_get_method(const COMP_CTX *ctx) |
| 35 | { |
| 36 | return ctx->meth; |
| 37 | } |
| 38 | |
| 39 | int COMP_get_type(const COMP_METHOD *meth) |
| 40 | { |
| 41 | return meth->type; |
| 42 | } |
| 43 | |
| 44 | const char *COMP_get_name(const COMP_METHOD *meth) |
| 45 | { |
| 46 | return meth->name; |
| 47 | } |
| 48 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 49 | void COMP_CTX_free(COMP_CTX *ctx) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 50 | { |
Rich Salz | e6e9170 | 2018-03-27 16:25:08 -0400 | [diff] [blame] | 51 | if (ctx == NULL) |
| 52 | return; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 53 | if (ctx->meth->finish != NULL) |
| 54 | ctx->meth->finish(ctx); |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 55 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 56 | OPENSSL_free(ctx); |
| 57 | } |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 58 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 59 | int COMP_compress_block(COMP_CTX *ctx, unsigned char *out, int olen, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 60 | unsigned char *in, int ilen) |
| 61 | { |
| 62 | int ret; |
| 63 | if (ctx->meth->compress == NULL) { |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 64 | return -1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 65 | } |
| 66 | ret = ctx->meth->compress(ctx, out, olen, in, ilen); |
| 67 | if (ret > 0) { |
| 68 | ctx->compress_in += ilen; |
| 69 | ctx->compress_out += ret; |
| 70 | } |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 71 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 72 | } |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 73 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 74 | int COMP_expand_block(COMP_CTX *ctx, unsigned char *out, int olen, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 75 | unsigned char *in, int ilen) |
| 76 | { |
| 77 | int ret; |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 78 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 79 | if (ctx->meth->expand == NULL) { |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 80 | return -1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 81 | } |
| 82 | ret = ctx->meth->expand(ctx, out, olen, in, ilen); |
| 83 | if (ret > 0) { |
| 84 | ctx->expand_in += ilen; |
| 85 | ctx->expand_out += ret; |
| 86 | } |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 87 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 88 | } |
Rich Salz | 9a55570 | 2015-05-08 12:05:36 -0400 | [diff] [blame] | 89 | |
| 90 | int COMP_CTX_get_type(const COMP_CTX* comp) |
| 91 | { |
| 92 | return comp->meth ? comp->meth->type : NID_undef; |
| 93 | } |