| 1 |  |  | /* $OpenBSD: c_rle.c,v 1.8 2014/11/03 16:58:28 tedu Exp $ */ | 
    
    | 2 |  |  | #include <stdio.h> | 
    
    | 3 |  |  | #include <stdlib.h> | 
    
    | 4 |  |  | #include <string.h> | 
    
    | 5 |  |  | #include <openssl/objects.h> | 
    
    | 6 |  |  | #include <openssl/comp.h> | 
    
    | 7 |  |  |  | 
    
    | 8 |  |  | static int rle_compress_block(COMP_CTX *ctx, unsigned char *out, | 
    
    | 9 |  |  |     unsigned int olen, unsigned char *in, unsigned int ilen); | 
    
    | 10 |  |  | static int rle_expand_block(COMP_CTX *ctx, unsigned char *out, | 
    
    | 11 |  |  |     unsigned int olen, unsigned char *in, unsigned int ilen); | 
    
    | 12 |  |  |  | 
    
    | 13 |  |  | static COMP_METHOD rle_method = { | 
    
    | 14 |  |  | 	.type = NID_rle_compression, | 
    
    | 15 |  |  | 	.name = LN_rle_compression, | 
    
    | 16 |  |  | 	.compress = rle_compress_block, | 
    
    | 17 |  |  | 	.expand = rle_expand_block | 
    
    | 18 |  |  | }; | 
    
    | 19 |  |  |  | 
    
    | 20 |  |  | COMP_METHOD * | 
    
    | 21 |  |  | COMP_rle(void) | 
    
    | 22 |  |  | { | 
    
    | 23 |  |  | 	return (&rle_method); | 
    
    | 24 |  |  | } | 
    
    | 25 |  |  |  | 
    
    | 26 |  |  | static int | 
    
    | 27 |  |  | rle_compress_block(COMP_CTX *ctx, unsigned char *out, unsigned int olen, | 
    
    | 28 |  |  |     unsigned char *in, unsigned int ilen) | 
    
    | 29 |  |  | { | 
    
    | 30 |  |  |  | 
    
    | 31 |  |  | 	if (ilen == 0 || olen < (ilen - 1)) { | 
    
    | 32 |  |  | 		return (-1); | 
    
    | 33 |  |  | 	} | 
    
    | 34 |  |  |  | 
    
    | 35 |  |  | 	*(out++) = 0; | 
    
    | 36 |  |  | 	memcpy(out, in, ilen); | 
    
    | 37 |  |  | 	return (ilen + 1); | 
    
    | 38 |  |  | } | 
    
    | 39 |  |  |  | 
    
    | 40 |  |  | static int | 
    
    | 41 |  |  | rle_expand_block(COMP_CTX *ctx, unsigned char *out, unsigned int olen, | 
    
    | 42 |  |  |     unsigned char *in, unsigned int ilen) | 
    
    | 43 |  |  | { | 
    
    | 44 |  |  | 	int i; | 
    
    | 45 |  |  |  | 
    
    | 46 |  |  | 	if (olen < (ilen - 1)) { | 
    
    | 47 |  |  | 		return (-1); | 
    
    | 48 |  |  | 	} | 
    
    | 49 |  |  |  | 
    
    | 50 |  |  | 	i= *(in++); | 
    
    | 51 |  |  | 	if (i == 0) { | 
    
    | 52 |  |  | 		memcpy(out, in, ilen - 1); | 
    
    | 53 |  |  | 	} | 
    
    | 54 |  |  | 	return (ilen - 1); | 
    
    | 55 |  |  | } |