This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / crypto / comp / c_zlib.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "objects.h"
5 #include "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(ctx,out,olen,in,ilen)
44 COMP_CTX *ctx;
45 unsigned char *out;
46 unsigned int olen;
47 unsigned char *in;
48 unsigned int ilen;
49         {
50         unsigned long l;
51         int i;
52         int clear=1;
53
54         if (ilen > 128)
55                 {
56                 out[0]=1;
57                 l=olen-1;
58                 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
59                 if (i != Z_OK)
60                         return(-1);
61                 if (ilen > l)
62                         {
63                         clear=0;
64                         l++;
65                         }
66                 }
67         if (clear)
68                 {
69                 out[0]=0;
70                 memcpy(&(out[1]),in,ilen);
71                 l=ilen+1;
72                 }
73 fprintf(stderr,"compress(%4d)->%4d %s\n",ilen,(int)l,(clear)?"clear":"zlib");
74         return((int)l);
75         }
76
77 static int zlib_expand_block(ctx,out,olen,in,ilen)
78 COMP_CTX *ctx;
79 unsigned char *out;
80 unsigned int olen;
81 unsigned char *in;
82 unsigned int ilen;
83         {
84         unsigned long l;
85         int i;
86
87         if (in[0])
88                 {
89                 l=olen;
90                 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
91                 if (i != Z_OK)
92                         return(-1);
93                 }
94         else
95                 {
96                 memcpy(out,&(in[1]),ilen-1);
97                 l=ilen-1;
98                 }
99         fprintf(stderr,"expand  (%4d)->%4d %s\n",ilen,(int)l,in[0]?"zlib":"clear");
100         return((int)l);
101         }
102
103 static int zz_uncompress (dest, destLen, source, sourceLen)
104     Bytef *dest;
105     uLongf *destLen;
106     const Bytef *source;
107     uLong sourceLen;
108 {
109     z_stream stream;
110     int err;
111
112     stream.next_in = (Bytef*)source;
113     stream.avail_in = (uInt)sourceLen;
114     /* Check for source > 64K on 16-bit machine: */
115     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
116
117     stream.next_out = dest;
118     stream.avail_out = (uInt)*destLen;
119     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
120
121     stream.zalloc = (alloc_func)0;
122     stream.zfree = (free_func)0;
123
124     err = inflateInit(&stream);
125     if (err != Z_OK) return err;
126
127     err = inflate(&stream, Z_FINISH);
128     if (err != Z_STREAM_END) {
129         inflateEnd(&stream);
130         return err;
131     }
132     *destLen = stream.total_out;
133
134     err = inflateEnd(&stream);
135     return err;
136 }
137
138 #endif
139
140 COMP_METHOD *COMP_zlib()
141         {
142         return(&zlib_method);
143         }
144