Fixed a crash in print_notice.
[openssl.git] / crypto / comp / c_rle.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 static int rle_compress_block(COMP_CTX *ctx, unsigned char *out,
8                               unsigned int olen, unsigned char *in,
9                               unsigned int ilen);
10 static int rle_expand_block(COMP_CTX *ctx, unsigned char *out,
11                             unsigned int olen, unsigned char *in,
12                             unsigned int ilen);
13
14 static COMP_METHOD rle_method = {
15     NID_rle_compression,
16     LN_rle_compression,
17     NULL,
18     NULL,
19     rle_compress_block,
20     rle_expand_block,
21     NULL,
22     NULL,
23 };
24
25 COMP_METHOD *COMP_rle(void)
26 {
27     return (&rle_method);
28 }
29
30 static int rle_compress_block(COMP_CTX *ctx, unsigned char *out,
31                               unsigned int olen, unsigned char *in,
32                               unsigned int ilen)
33 {
34     if (ilen == 0)
35         return 0;
36
37     if (olen <= ilen)
38         return -1;
39
40     *(out++) = 0;
41     memcpy(out, in, ilen);
42     return (ilen + 1);
43 }
44
45 static int rle_expand_block(COMP_CTX *ctx, unsigned char *out,
46                             unsigned int olen, unsigned char *in,
47                             unsigned int ilen)
48 {
49     int i;
50
51     if (ilen == 0)
52         return 0;
53
54     if (olen < (ilen - 1))
55         return -1;
56
57     i = *(in++);
58     if (i != 0)
59         return -1;
60
61     memcpy(out, in, ilen - 1);
62     return (ilen - 1);
63 }