c_rle.c revision 296465
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <openssl/objects.h>
5#include <openssl/comp.h>
6
7static int rle_compress_block(COMP_CTX *ctx, unsigned char *out,
8                              unsigned int olen, unsigned char *in,
9                              unsigned int ilen);
10static int rle_expand_block(COMP_CTX *ctx, unsigned char *out,
11                            unsigned int olen, unsigned char *in,
12                            unsigned int ilen);
13
14static 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
25COMP_METHOD *COMP_rle(void)
26{
27    return (&rle_method);
28}
29
30static int rle_compress_block(COMP_CTX *ctx, unsigned char *out,
31                              unsigned int olen, unsigned char *in,
32                              unsigned int ilen)
33{
34    /* int i; */
35
36    if (olen < (ilen + 1)) {
37        /* ZZZZZZZZZZZZZZZZZZZZZZ */
38        return (-1);
39    }
40
41    *(out++) = 0;
42    memcpy(out, in, ilen);
43    return (ilen + 1);
44}
45
46static int rle_expand_block(COMP_CTX *ctx, unsigned char *out,
47                            unsigned int olen, unsigned char *in,
48                            unsigned int ilen)
49{
50    int i;
51
52    if (ilen == 0 || olen < (ilen - 1)) {
53        /* ZZZZZZZZZZZZZZZZZZZZZZ */
54        return (-1);
55    }
56
57    i = *(in++);
58    if (i == 0) {
59        memcpy(out, in, ilen - 1);
60    }
61    return (ilen - 1);
62}
63