opencv

Форк
0
/
compress.c 
98 строк · 3.8 Кб
1
/* compress.c -- compress a memory buffer
2
 * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
 */
5

6
#include "zbuild.h"
7
#include "zutil.h"
8

9
/* ===========================================================================
10
 *  Architecture-specific hooks.
11
 */
12
#ifdef S390_DFLTCC_DEFLATE
13
#  include "arch/s390/dfltcc_common.h"
14
#else
15
/* Returns the upper bound on compressed data length based on uncompressed data length, assuming default settings.
16
 * Zero means that arch-specific deflation code behaves identically to the regular zlib-ng algorithms. */
17
#  define DEFLATE_BOUND_COMPLEN(source_len) 0
18
#endif
19

20
/* ===========================================================================
21
     Compresses the source buffer into the destination buffer. The level
22
   parameter has the same meaning as in deflateInit.  sourceLen is the byte
23
   length of the source buffer. Upon entry, destLen is the total size of the
24
   destination buffer, which must be at least 0.1% larger than sourceLen plus
25
   12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
26

27
     compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
28
   memory, Z_BUF_ERROR if there was not enough room in the output buffer,
29
   Z_STREAM_ERROR if the level parameter is invalid.
30
*/
31
int Z_EXPORT PREFIX(compress2)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source,
32
                        z_uintmax_t sourceLen, int level) {
33
    PREFIX3(stream) stream;
34
    int err;
35
    const unsigned int max = (unsigned int)-1;
36
    z_size_t left;
37

38
    left = *destLen;
39
    *destLen = 0;
40

41
    stream.zalloc = NULL;
42
    stream.zfree = NULL;
43
    stream.opaque = NULL;
44

45
    err = PREFIX(deflateInit)(&stream, level);
46
    if (err != Z_OK)
47
        return err;
48

49
    stream.next_out = dest;
50
    stream.avail_out = 0;
51
    stream.next_in = (z_const unsigned char *)source;
52
    stream.avail_in = 0;
53

54
    do {
55
        if (stream.avail_out == 0) {
56
            stream.avail_out = left > (unsigned long)max ? max : (unsigned int)left;
57
            left -= stream.avail_out;
58
        }
59
        if (stream.avail_in == 0) {
60
            stream.avail_in = sourceLen > (unsigned long)max ? max : (unsigned int)sourceLen;
61
            sourceLen -= stream.avail_in;
62
        }
63
        err = PREFIX(deflate)(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
64
    } while (err == Z_OK);
65

66
    *destLen = stream.total_out;
67
    PREFIX(deflateEnd)(&stream);
68
    return err == Z_STREAM_END ? Z_OK : err;
69
}
70

71
/* ===========================================================================
72
 */
73
int Z_EXPORT PREFIX(compress)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t sourceLen) {
74
    return PREFIX(compress2)(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
75
}
76

77
/* ===========================================================================
78
   If the default memLevel or windowBits for deflateInit() is changed, then
79
   this function needs to be updated.
80
 */
81
z_uintmax_t Z_EXPORT PREFIX(compressBound)(z_uintmax_t sourceLen) {
82
    z_uintmax_t complen = DEFLATE_BOUND_COMPLEN(sourceLen);
83

84
    if (complen > 0)
85
        /* Architecture-specific code provided an upper bound. */
86
        return complen + ZLIB_WRAPLEN;
87

88
#ifndef NO_QUICK_STRATEGY
89
    return sourceLen                       /* The source size itself */
90
      + (sourceLen == 0 ? 1 : 0)           /* Always at least one byte for any input */
91
      + (sourceLen < 9 ? 1 : 0)            /* One extra byte for lengths less than 9 */
92
      + DEFLATE_QUICK_OVERHEAD(sourceLen)  /* Source encoding overhead, padded to next full byte */
93
      + DEFLATE_BLOCK_OVERHEAD             /* Deflate block overhead bytes */
94
      + ZLIB_WRAPLEN;                      /* zlib wrapper */
95
#else
96
    return sourceLen + (sourceLen >> 4) + 7 + ZLIB_WRAPLEN;
97
#endif
98
}
99

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.