1 | /* |
2 | * Copyright 1998-2018 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 | |
18 | COMP_CTX *COMP_CTX_new(COMP_METHOD *meth) |
19 | { |
20 | COMP_CTX *ret; |
21 | |
22 | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) { |
23 | COMPerr(COMP_F_COMP_CTX_NEW, ERR_R_MALLOC_FAILURE); |
24 | return NULL; |
25 | } |
26 | ret->meth = meth; |
27 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { |
28 | OPENSSL_free(ret); |
29 | ret = NULL; |
30 | } |
31 | return ret; |
32 | } |
33 | |
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 | |
49 | void COMP_CTX_free(COMP_CTX *ctx) |
50 | { |
51 | if (ctx == NULL) |
52 | return; |
53 | if (ctx->meth->finish != NULL) |
54 | ctx->meth->finish(ctx); |
55 | |
56 | OPENSSL_free(ctx); |
57 | } |
58 | |
59 | int COMP_compress_block(COMP_CTX *ctx, unsigned char *out, int olen, |
60 | unsigned char *in, int ilen) |
61 | { |
62 | int ret; |
63 | if (ctx->meth->compress == NULL) { |
64 | return -1; |
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 | } |
71 | return ret; |
72 | } |
73 | |
74 | int COMP_expand_block(COMP_CTX *ctx, unsigned char *out, int olen, |
75 | unsigned char *in, int ilen) |
76 | { |
77 | int ret; |
78 | |
79 | if (ctx->meth->expand == NULL) { |
80 | return -1; |
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 | } |
87 | return ret; |
88 | } |
89 | |
90 | int COMP_CTX_get_type(const COMP_CTX* comp) |
91 | { |
92 | return comp->meth ? comp->meth->type : NID_undef; |
93 | } |
94 | |