blob: 32afd0dba8185a0131c41e778dd215d41bab8c9f [file] [log] [blame]
Rich Salz62867572016-05-17 14:24:46 -04001/*
2 * Copyright 1998-2016 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 Salz9a555702015-05-08 12:05:36 -040015#include "comp_lcl.h"
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000016
Ulf Möller6b691a51999-04-19 21:31:43 +000017COMP_CTX *COMP_CTX_new(COMP_METHOD *meth)
Matt Caswell0f113f32015-01-22 03:40:55 +000018{
19 COMP_CTX *ret;
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000020
Rich Salzb51bce92015-08-25 13:25:58 -040021 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +000022 return (NULL);
Matt Caswell0f113f32015-01-22 03:40:55 +000023 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}
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000030
Rich Salz9a555702015-05-08 12:05:36 -040031const COMP_METHOD *COMP_CTX_get_method(const COMP_CTX *ctx)
32{
33 return ctx->meth;
34}
35
36int COMP_get_type(const COMP_METHOD *meth)
37{
38 return meth->type;
39}
40
41const char *COMP_get_name(const COMP_METHOD *meth)
42{
43 return meth->name;
44}
45
Ulf Möller6b691a51999-04-19 21:31:43 +000046void COMP_CTX_free(COMP_CTX *ctx)
Matt Caswell0f113f32015-01-22 03:40:55 +000047{
48 if (ctx == NULL)
49 return;
Ben Lauriee03ddfa1999-01-07 19:15:59 +000050
Matt Caswell0f113f32015-01-22 03:40:55 +000051 if (ctx->meth->finish != NULL)
52 ctx->meth->finish(ctx);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000053
Matt Caswell0f113f32015-01-22 03:40:55 +000054 OPENSSL_free(ctx);
55}
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000056
Ulf Möller6b691a51999-04-19 21:31:43 +000057int COMP_compress_block(COMP_CTX *ctx, unsigned char *out, int olen,
Matt Caswell0f113f32015-01-22 03:40:55 +000058 unsigned char *in, int ilen)
59{
60 int ret;
61 if (ctx->meth->compress == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +000062 return (-1);
63 }
64 ret = ctx->meth->compress(ctx, out, olen, in, ilen);
65 if (ret > 0) {
66 ctx->compress_in += ilen;
67 ctx->compress_out += ret;
68 }
69 return (ret);
70}
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000071
Ulf Möller6b691a51999-04-19 21:31:43 +000072int COMP_expand_block(COMP_CTX *ctx, unsigned char *out, int olen,
Matt Caswell0f113f32015-01-22 03:40:55 +000073 unsigned char *in, int ilen)
74{
75 int ret;
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000076
Matt Caswell0f113f32015-01-22 03:40:55 +000077 if (ctx->meth->expand == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +000078 return (-1);
79 }
80 ret = ctx->meth->expand(ctx, out, olen, in, ilen);
81 if (ret > 0) {
82 ctx->expand_in += ilen;
83 ctx->expand_out += ret;
84 }
85 return (ret);
86}
Rich Salz9a555702015-05-08 12:05:36 -040087
88int COMP_CTX_get_type(const COMP_CTX* comp)
89{
90 return comp->meth ? comp->meth->type : NID_undef;
91}