1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include "../../src/bsdconv.h"
5 
6 #define SWAP(a,b,i) ((i)=(a), (a)=(b), (b)=(i))
7 
cbconv(struct bsdconv_instance * ins)8 void cbconv(struct bsdconv_instance *ins){
9 	char *data, *p, c;
10 	unsigned int len, i;
11 	struct bsdconv_phase *this_phase=THIS_PHASE(ins);
12 	data=this_phase->curr->data;
13 
14 	data+=1;
15 	if(this_phase->curr->len > 3){
16 		this_phase->state.status=NEXTPHASE;
17 
18 		DATA_MALLOC(ins, this_phase->data_tail->next);
19 		this_phase->data_tail=this_phase->data_tail->next;
20 		this_phase->data_tail->next=NULL;
21 		this_phase->data_tail->len=4;
22 		this_phase->data_tail->flags=F_FREE;
23 		p=this_phase->data_tail->data=malloc(4);
24 
25 		c=*data-1;
26 		*p=bb11011000;
27 		*p |= (c >> 2) & bb00000011;
28 		++p;
29 		*p=(c << 6) & bb11000000;
30 		++data;
31 		*p |= (*data >> 2) & bb00111111;
32 		++p;
33 		*p=bb11011100;
34 		*p |= *data & bb00000011;
35 		++p;
36 		++data;
37 		*p=*data;
38 
39 		data=this_phase->data_tail->data;
40 
41 		SWAP(data[0],data[1],i);
42 		SWAP(data[2],data[3],i);
43 	}else{
44 		this_phase->state.status=NEXTPHASE;
45 		len=this_phase->curr->len-1;
46 
47 		DATA_MALLOC(ins, this_phase->data_tail->next);
48 		this_phase->data_tail=this_phase->data_tail->next;
49 		this_phase->data_tail->next=NULL;
50 		this_phase->data_tail->len=2;
51 		this_phase->data_tail->flags=F_FREE;
52 		this_phase->data_tail->data=malloc(2);
53 		for(i=0;i<2-len;++i){
54 			CP(this_phase->data_tail->data)[i]=0x0;
55 		}
56 		memcpy(CP(this_phase->data_tail->data)+i, data, len);
57 		data=this_phase->data_tail->data;
58 
59 		SWAP(data[0],data[1],i);
60 	}
61 	return;
62 }
63