1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1998-2020 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include <stdlib.h>
12e1051a39Sopenharmony_ci#include <string.h>
13e1051a39Sopenharmony_ci#include <openssl/objects.h>
14e1051a39Sopenharmony_ci#include <openssl/comp.h>
15e1051a39Sopenharmony_ci#include <openssl/err.h>
16e1051a39Sopenharmony_ci#include "comp_local.h"
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_ciCOMP_CTX *COMP_CTX_new(COMP_METHOD *meth)
19e1051a39Sopenharmony_ci{
20e1051a39Sopenharmony_ci    COMP_CTX *ret;
21e1051a39Sopenharmony_ci
22e1051a39Sopenharmony_ci    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
23e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
24e1051a39Sopenharmony_ci        return NULL;
25e1051a39Sopenharmony_ci    }
26e1051a39Sopenharmony_ci    ret->meth = meth;
27e1051a39Sopenharmony_ci    if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
28e1051a39Sopenharmony_ci        OPENSSL_free(ret);
29e1051a39Sopenharmony_ci        ret = NULL;
30e1051a39Sopenharmony_ci    }
31e1051a39Sopenharmony_ci    return ret;
32e1051a39Sopenharmony_ci}
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ciconst COMP_METHOD *COMP_CTX_get_method(const COMP_CTX *ctx)
35e1051a39Sopenharmony_ci{
36e1051a39Sopenharmony_ci    return ctx->meth;
37e1051a39Sopenharmony_ci}
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ciint COMP_get_type(const COMP_METHOD *meth)
40e1051a39Sopenharmony_ci{
41e1051a39Sopenharmony_ci    return meth->type;
42e1051a39Sopenharmony_ci}
43e1051a39Sopenharmony_ci
44e1051a39Sopenharmony_ciconst char *COMP_get_name(const COMP_METHOD *meth)
45e1051a39Sopenharmony_ci{
46e1051a39Sopenharmony_ci    return meth->name;
47e1051a39Sopenharmony_ci}
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_civoid COMP_CTX_free(COMP_CTX *ctx)
50e1051a39Sopenharmony_ci{
51e1051a39Sopenharmony_ci    if (ctx == NULL)
52e1051a39Sopenharmony_ci        return;
53e1051a39Sopenharmony_ci    if (ctx->meth->finish != NULL)
54e1051a39Sopenharmony_ci        ctx->meth->finish(ctx);
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ci    OPENSSL_free(ctx);
57e1051a39Sopenharmony_ci}
58e1051a39Sopenharmony_ci
59e1051a39Sopenharmony_ciint COMP_compress_block(COMP_CTX *ctx, unsigned char *out, int olen,
60e1051a39Sopenharmony_ci                        unsigned char *in, int ilen)
61e1051a39Sopenharmony_ci{
62e1051a39Sopenharmony_ci    int ret;
63e1051a39Sopenharmony_ci    if (ctx->meth->compress == NULL) {
64e1051a39Sopenharmony_ci        return -1;
65e1051a39Sopenharmony_ci    }
66e1051a39Sopenharmony_ci    ret = ctx->meth->compress(ctx, out, olen, in, ilen);
67e1051a39Sopenharmony_ci    if (ret > 0) {
68e1051a39Sopenharmony_ci        ctx->compress_in += ilen;
69e1051a39Sopenharmony_ci        ctx->compress_out += ret;
70e1051a39Sopenharmony_ci    }
71e1051a39Sopenharmony_ci    return ret;
72e1051a39Sopenharmony_ci}
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_ciint COMP_expand_block(COMP_CTX *ctx, unsigned char *out, int olen,
75e1051a39Sopenharmony_ci                      unsigned char *in, int ilen)
76e1051a39Sopenharmony_ci{
77e1051a39Sopenharmony_ci    int ret;
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci    if (ctx->meth->expand == NULL) {
80e1051a39Sopenharmony_ci        return -1;
81e1051a39Sopenharmony_ci    }
82e1051a39Sopenharmony_ci    ret = ctx->meth->expand(ctx, out, olen, in, ilen);
83e1051a39Sopenharmony_ci    if (ret > 0) {
84e1051a39Sopenharmony_ci        ctx->expand_in += ilen;
85e1051a39Sopenharmony_ci        ctx->expand_out += ret;
86e1051a39Sopenharmony_ci    }
87e1051a39Sopenharmony_ci    return ret;
88e1051a39Sopenharmony_ci}
89e1051a39Sopenharmony_ci
90e1051a39Sopenharmony_ciint COMP_CTX_get_type(const COMP_CTX* comp)
91e1051a39Sopenharmony_ci{
92e1051a39Sopenharmony_ci    return comp->meth ? comp->meth->type : NID_undef;
93e1051a39Sopenharmony_ci}
94