Remove redundant ifdef.
[openssl.git] / crypto / comp / c_zlib.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <openssl/objects.h>
5 #include <openssl/comp.h>
6
7 COMP_METHOD *COMP_zlib(void );
8
9 #ifndef ZLIB
10
11 static COMP_METHOD zlib_method={
12         NID_undef,
13         "(null)",
14         NULL,
15         NULL,
16         NULL,
17         NULL,
18         NULL,
19         };
20
21 #else
22
23 #include <zlib.h>
24
25 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
26         unsigned int olen, unsigned char *in, unsigned int ilen);
27 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
28         unsigned int olen, unsigned char *in, unsigned int ilen);
29
30 static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
31         uLong sourceLen);
32
33 static COMP_METHOD zlib_method={
34         NID_zlib_compression,
35         LN_zlib_compression,
36         NULL,
37         NULL,
38         zlib_compress_block,
39         zlib_expand_block,
40         NULL,
41         };
42
43 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
44              unsigned int olen, unsigned char *in, unsigned int ilen)
45         {
46         unsigned long l;
47         int i;
48         int clear=1;
49
50         if (ilen > 128)
51                 {
52                 out[0]=1;
53                 l=olen-1;
54                 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
55                 if (i != Z_OK)
56                         return(-1);
57                 if (ilen > l)
58                         {
59                         clear=0;
60                         l++;
61                         }
62                 }
63         if (clear)
64                 {
65                 out[0]=0;
66                 memcpy(&(out[1]),in,ilen);
67                 l=ilen+1;
68                 }
69 fprintf(stderr,"compress(%4d)->%4d %s\n",ilen,(int)l,(clear)?"clear":"zlib");
70         return((int)l);
71         }
72
73 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
74              unsigned int olen, unsigned char *in, unsigned int ilen)
75         {
76         unsigned long l;
77         int i;
78
79         if (in[0])
80                 {
81                 l=olen;
82                 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
83                 if (i != Z_OK)
84                         return(-1);
85                 }
86         else
87                 {
88                 memcpy(out,&(in[1]),ilen-1);
89                 l=ilen-1;
90                 }
91         fprintf(stderr,"expand  (%4d)->%4d %s\n",ilen,(int)l,in[0]?"zlib":"clear");
92         return((int)l);
93         }
94
95 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
96              uLong sourceLen)
97 {
98     z_stream stream;
99     int err;
100
101     stream.next_in = (Bytef*)source;
102     stream.avail_in = (uInt)sourceLen;
103     /* Check for source > 64K on 16-bit machine: */
104     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
105
106     stream.next_out = dest;
107     stream.avail_out = (uInt)*destLen;
108     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
109
110     stream.zalloc = (alloc_func)0;
111     stream.zfree = (free_func)0;
112
113     err = inflateInit(&stream);
114     if (err != Z_OK) return err;
115
116     err = inflate(&stream, Z_FINISH);
117     if (err != Z_STREAM_END) {
118         inflateEnd(&stream);
119         return err;
120     }
121     *destLen = stream.total_out;
122
123     err = inflateEnd(&stream);
124     return err;
125 }
126
127 #endif
128
129 COMP_METHOD *COMP_zlib(void)
130         {
131         return(&zlib_method);
132         }
133