blob: 6ae2114496b03b24a18cc4b5f19e69719f16b805 [file] [log] [blame]
Rich Salz62867572016-05-17 14:24:46 -04001/*
Matt Caswellc4d3c192018-04-03 13:57:12 +01002 * Copyright 1998-2018 The OpenSSL Project Authors. All Rights Reserved.
Rich Salz9a555702015-05-08 12:05:36 -04003 *
Rich Salz62867572016-05-17 14:24:46 -04004 * 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 Salz9a555702015-05-08 12:05:36 -04008 */
9
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000010#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
Bodo Möllerec577821999-04-23 22:13:45 +000013#include <openssl/objects.h>
14#include <openssl/comp.h>
Rich Salzfe1128d2018-04-26 14:02:24 -040015#include <openssl/err.h>
Rich Salz9a555702015-05-08 12:05:36 -040016#include "comp_lcl.h"
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000017
Ulf Möller6b691a51999-04-19 21:31:43 +000018COMP_CTX *COMP_CTX_new(COMP_METHOD *meth)
Matt Caswell0f113f32015-01-22 03:40:55 +000019{
20 COMP_CTX *ret;
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000021
Rich Salzfe1128d2018-04-26 14:02:24 -040022 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
23 COMPerr(COMP_F_COMP_CTX_NEW, ERR_R_MALLOC_FAILURE);
KaoruToda26a7d932017-10-17 23:04:09 +090024 return NULL;
Rich Salzfe1128d2018-04-26 14:02:24 -040025 }
Matt Caswell0f113f32015-01-22 03:40:55 +000026 ret->meth = meth;
27 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
28 OPENSSL_free(ret);
29 ret = NULL;
30 }
KaoruToda26a7d932017-10-17 23:04:09 +090031 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +000032}
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000033
Rich Salz9a555702015-05-08 12:05:36 -040034const COMP_METHOD *COMP_CTX_get_method(const COMP_CTX *ctx)
35{
36 return ctx->meth;
37}
38
39int COMP_get_type(const COMP_METHOD *meth)
40{
41 return meth->type;
42}
43
44const char *COMP_get_name(const COMP_METHOD *meth)
45{
46 return meth->name;
47}
48
Ulf Möller6b691a51999-04-19 21:31:43 +000049void COMP_CTX_free(COMP_CTX *ctx)
Matt Caswell0f113f32015-01-22 03:40:55 +000050{
Rich Salze6e91702018-03-27 16:25:08 -040051 if (ctx == NULL)
52 return;
Matt Caswell0f113f32015-01-22 03:40:55 +000053 if (ctx->meth->finish != NULL)
54 ctx->meth->finish(ctx);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000055
Matt Caswell0f113f32015-01-22 03:40:55 +000056 OPENSSL_free(ctx);
57}
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000058
Ulf Möller6b691a51999-04-19 21:31:43 +000059int COMP_compress_block(COMP_CTX *ctx, unsigned char *out, int olen,
Matt Caswell0f113f32015-01-22 03:40:55 +000060 unsigned char *in, int ilen)
61{
62 int ret;
63 if (ctx->meth->compress == NULL) {
KaoruToda26a7d932017-10-17 23:04:09 +090064 return -1;
Matt Caswell0f113f32015-01-22 03:40:55 +000065 }
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 }
KaoruToda26a7d932017-10-17 23:04:09 +090071 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +000072}
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000073
Ulf Möller6b691a51999-04-19 21:31:43 +000074int COMP_expand_block(COMP_CTX *ctx, unsigned char *out, int olen,
Matt Caswell0f113f32015-01-22 03:40:55 +000075 unsigned char *in, int ilen)
76{
77 int ret;
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000078
Matt Caswell0f113f32015-01-22 03:40:55 +000079 if (ctx->meth->expand == NULL) {
KaoruToda26a7d932017-10-17 23:04:09 +090080 return -1;
Matt Caswell0f113f32015-01-22 03:40:55 +000081 }
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 }
KaoruToda26a7d932017-10-17 23:04:09 +090087 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +000088}
Rich Salz9a555702015-05-08 12:05:36 -040089
90int COMP_CTX_get_type(const COMP_CTX* comp)
91{
92 return comp->meth ? comp->meth->type : NID_undef;
93}