1 #include <errno.h>
2 #include <string.h>
3 #include "../../src/bsdconv.h"
4 
5 struct my_s{
6 	struct data_rt *from;
7 	struct data_rt *to;
8 	struct data_rt *cursor;
9 };
10 
cbcreate(struct bsdconv_instance * ins,struct bsdconv_hash_entry * arg)11 int cbcreate(struct bsdconv_instance *ins, struct bsdconv_hash_entry *arg){
12 	struct my_s *r=malloc(sizeof(struct my_s));
13 	int e;
14 	r->from=NULL;
15 	r->to=NULL;
16 	while(arg){
17 		DATA_FREE(ins, r->from);
18 		DATA_FREE(ins, r->to);
19 		r->from=str2data(arg->key, &e, ins);
20 		if(e){
21 			free(r);
22 			return e;
23 		}
24 		if(r->from==NULL){
25 			free(r);
26 			return EINVAL;
27 		}
28 		if(arg->ptr){
29 			r->to=str2data(arg->ptr, &e, ins);
30 			if(e){
31 				DATA_FREE(ins, r->from);
32 				free(r);
33 				return e;
34 			}
35 		}
36 		arg=arg->next;
37 	}
38 	THIS_CODEC(ins)->priv=r;
39 	return 0;
40 }
41 
cbdestroy(struct bsdconv_instance * ins)42 void cbdestroy(struct bsdconv_instance *ins){
43 	struct my_s *r=THIS_CODEC(ins)->priv;
44 	DATA_FREE(ins, r->from);
45 	DATA_FREE(ins, r->to);
46 	free(r);
47 }
48 
cbinit(struct bsdconv_instance * ins)49 void cbinit(struct bsdconv_instance *ins){
50 	struct my_s *r=THIS_CODEC(ins)->priv;
51 	r->cursor=r->from;
52 }
53 
cbconv(struct bsdconv_instance * ins)54 void cbconv(struct bsdconv_instance *ins){
55 	struct bsdconv_phase *this_phase=THIS_PHASE(ins);
56 	struct my_s *r=THIS_CODEC(ins)->priv;
57 	unsigned char *datai=this_phase->curr->data;
58 	unsigned char *datar=r->cursor->data;
59 	size_t l=this_phase->curr->len;
60 	size_t i;
61 
62 	if(l != r->cursor->len){
63 		r->cursor=r->from;
64 		this_phase->state.status=DEADEND;
65 		return;
66 	}
67 
68 	for(i=0;i<l;i+=1){
69 		if(datai[i] != datar[i]){
70 			r->cursor=r->from;
71 			this_phase->state.status=DEADEND;
72 			return;
73 		}
74 	}
75 
76 	if(r->cursor->next != NULL){
77 		r->cursor = r->cursor->next;
78 		this_phase->state.status=CONTINUE;
79 		return;
80 	}else{
81 		r->cursor = r->from;
82 		LISTCPY(ins, this_phase->data_tail, r->to);
83 		this_phase->state.status=NEXTPHASE;
84 		return;
85 	}
86 }
87