xref: /onnv-gate/usr/src/common/openssl/crypto/comp/c_rle.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate #include <stdio.h>
2*0Sstevel@tonic-gate #include <stdlib.h>
3*0Sstevel@tonic-gate #include <string.h>
4*0Sstevel@tonic-gate #include <openssl/objects.h>
5*0Sstevel@tonic-gate #include <openssl/comp.h>
6*0Sstevel@tonic-gate 
7*0Sstevel@tonic-gate static int rle_compress_block(COMP_CTX *ctx, unsigned char *out,
8*0Sstevel@tonic-gate 	unsigned int olen, unsigned char *in, unsigned int ilen);
9*0Sstevel@tonic-gate static int rle_expand_block(COMP_CTX *ctx, unsigned char *out,
10*0Sstevel@tonic-gate 	unsigned int olen, unsigned char *in, unsigned int ilen);
11*0Sstevel@tonic-gate 
12*0Sstevel@tonic-gate static COMP_METHOD rle_method={
13*0Sstevel@tonic-gate 	NID_rle_compression,
14*0Sstevel@tonic-gate 	LN_rle_compression,
15*0Sstevel@tonic-gate 	NULL,
16*0Sstevel@tonic-gate 	NULL,
17*0Sstevel@tonic-gate 	rle_compress_block,
18*0Sstevel@tonic-gate 	rle_expand_block,
19*0Sstevel@tonic-gate 	NULL,
20*0Sstevel@tonic-gate 	NULL,
21*0Sstevel@tonic-gate 	};
22*0Sstevel@tonic-gate 
COMP_rle(void)23*0Sstevel@tonic-gate COMP_METHOD *COMP_rle(void)
24*0Sstevel@tonic-gate 	{
25*0Sstevel@tonic-gate 	return(&rle_method);
26*0Sstevel@tonic-gate 	}
27*0Sstevel@tonic-gate 
rle_compress_block(COMP_CTX * ctx,unsigned char * out,unsigned int olen,unsigned char * in,unsigned int ilen)28*0Sstevel@tonic-gate static int rle_compress_block(COMP_CTX *ctx, unsigned char *out,
29*0Sstevel@tonic-gate 	     unsigned int olen, unsigned char *in, unsigned int ilen)
30*0Sstevel@tonic-gate 	{
31*0Sstevel@tonic-gate 	/* int i; */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate 	if (olen < (ilen+1))
34*0Sstevel@tonic-gate 		{
35*0Sstevel@tonic-gate 		/* ZZZZZZZZZZZZZZZZZZZZZZ */
36*0Sstevel@tonic-gate 		return(-1);
37*0Sstevel@tonic-gate 		}
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate 	*(out++)=0;
40*0Sstevel@tonic-gate 	memcpy(out,in,ilen);
41*0Sstevel@tonic-gate 	return(ilen+1);
42*0Sstevel@tonic-gate 	}
43*0Sstevel@tonic-gate 
rle_expand_block(COMP_CTX * ctx,unsigned char * out,unsigned int olen,unsigned char * in,unsigned int ilen)44*0Sstevel@tonic-gate static int rle_expand_block(COMP_CTX *ctx, unsigned char *out,
45*0Sstevel@tonic-gate 	     unsigned int olen, unsigned char *in, unsigned int ilen)
46*0Sstevel@tonic-gate 	{
47*0Sstevel@tonic-gate 	int i;
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 	if (olen < (ilen-1))
50*0Sstevel@tonic-gate 		{
51*0Sstevel@tonic-gate 		/* ZZZZZZZZZZZZZZZZZZZZZZ */
52*0Sstevel@tonic-gate 		return(-1);
53*0Sstevel@tonic-gate 		}
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 	i= *(in++);
56*0Sstevel@tonic-gate 	if (i == 0)
57*0Sstevel@tonic-gate 		{
58*0Sstevel@tonic-gate 		memcpy(out,in,ilen-1);
59*0Sstevel@tonic-gate 		}
60*0Sstevel@tonic-gate 	return(ilen-1);
61*0Sstevel@tonic-gate 	}
62*0Sstevel@tonic-gate 
63