xref: /openssl/crypto/comp/comp_lib.c (revision 7e3cacac)
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 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <openssl/objects.h>
14 #include <openssl/comp.h>
15 #include <openssl/err.h>
16 #include "comp_local.h"
17 
COMP_CTX_new(COMP_METHOD * meth)18 COMP_CTX *COMP_CTX_new(COMP_METHOD *meth)
19 {
20     COMP_CTX *ret;
21 
22     if (meth == NULL)
23         return NULL;
24 
25     if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
26         return NULL;
27     ret->meth = meth;
28     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
29         OPENSSL_free(ret);
30         ret = NULL;
31     }
32     return ret;
33 }
34 
COMP_CTX_get_method(const COMP_CTX * ctx)35 const COMP_METHOD *COMP_CTX_get_method(const COMP_CTX *ctx)
36 {
37     return ctx->meth;
38 }
39 
COMP_get_type(const COMP_METHOD * meth)40 int COMP_get_type(const COMP_METHOD *meth)
41 {
42     if (meth == NULL)
43         return NID_undef;
44     return meth->type;
45 }
46 
COMP_get_name(const COMP_METHOD * meth)47 const char *COMP_get_name(const COMP_METHOD *meth)
48 {
49     if (meth == NULL)
50         return NULL;
51     return meth->name;
52 }
53 
COMP_CTX_free(COMP_CTX * ctx)54 void COMP_CTX_free(COMP_CTX *ctx)
55 {
56     if (ctx == NULL)
57         return;
58     if (ctx->meth->finish != NULL)
59         ctx->meth->finish(ctx);
60 
61     OPENSSL_free(ctx);
62 }
63 
COMP_compress_block(COMP_CTX * ctx,unsigned char * out,int olen,unsigned char * in,int ilen)64 int COMP_compress_block(COMP_CTX *ctx, unsigned char *out, int olen,
65                         unsigned char *in, int ilen)
66 {
67     int ret;
68     if (ctx->meth->compress == NULL) {
69         return -1;
70     }
71     ret = ctx->meth->compress(ctx, out, olen, in, ilen);
72     if (ret > 0) {
73         ctx->compress_in += ilen;
74         ctx->compress_out += ret;
75     }
76     return ret;
77 }
78 
COMP_expand_block(COMP_CTX * ctx,unsigned char * out,int olen,unsigned char * in,int ilen)79 int COMP_expand_block(COMP_CTX *ctx, unsigned char *out, int olen,
80                       unsigned char *in, int ilen)
81 {
82     int ret;
83 
84     if (ctx->meth->expand == NULL) {
85         return -1;
86     }
87     ret = ctx->meth->expand(ctx, out, olen, in, ilen);
88     if (ret > 0) {
89         ctx->expand_in += ilen;
90         ctx->expand_out += ret;
91     }
92     return ret;
93 }
94 
COMP_CTX_get_type(const COMP_CTX * comp)95 int COMP_CTX_get_type(const COMP_CTX* comp)
96 {
97     return comp->meth ? comp->meth->type : NID_undef;
98 }
99