mess822x 1.23
mess822x
Loading...
Searching...
No Matches
rewritehost.c
Go to the documentation of this file.
1#include "stralloc.h"
2#include "str.h"
3#include "case.h"
4#include "byte.h"
5#include "rewritehost.h"
6
7static stralloc work = {0};
8
9static int doit(char *rule)
10{
11 int colon;
12 char ch;
13 int prefixlen;
14
15 ch = *rule++;
16 if ((ch != '?') && (ch != '=') && (ch != '*') && (ch != '-')) return 1;
17 colon = str_chr(rule,':');
18 if (!rule[colon]) return 1;
19 if (work.len < colon) return 1;
20
21 prefixlen = work.len - colon;
22 if ((ch == '=') && prefixlen) return 1;
23 if (case_diffb(rule,colon,work.s + prefixlen)) return 1;
24
25 if (ch == '?') {
26 if (byte_chr(work.s,prefixlen,'.') < prefixlen) return 1;
27 if (byte_chr(work.s,prefixlen,'[') < prefixlen) return 1;
28 if (byte_chr(work.s,prefixlen,']') < prefixlen) return 1;
29 }
30
31 work.len = prefixlen;
32 if (ch == '-') work.len = 0;
33
34 return stralloc_cats(&work,rule + colon + 1);
35}
36
37static int appendwork(stralloc *out,stralloc *rules)
38{
39 int i;
40 int j;
41
42 for (j = i = 0;j < rules->len;++j)
43 if (!rules->s[j]) {
44 if (!doit(rules->s + i)) return 0;
45 i = j + 1;
46 }
47 return stralloc_cat(out,&work);
48}
49
50static int appendaddr(stralloc *out,char *in,unsigned int len,stralloc *rules)
51{
52 int at;
53
54 at = byte_chr(in,len,'@');
55 if (!at) if (len <= 1) return 1;
56 if (!stralloc_catb(out,in,at)) return 0;
57 if (!stralloc_append(out,"@")) return 0;
58 if (at < len) ++at;
59 if (!stralloc_copyb(&work,in + at,len - at)) return 0;
60 return appendwork(out,rules);
61}
62
63int rewritehost(stralloc *out,char *in,unsigned int len,stralloc *rules)
64{
65 if (!stralloc_copys(out,"")) return 0;
66 if (!stralloc_copyb(&work,in,len)) return 0;
67 return appendwork(out,rules);
68}
69
70int rewritehost_addr(stralloc *out,char *in,unsigned int len,stralloc *rules)
71{
72 if (!stralloc_copys(out,"")) return 0;
73 return appendaddr(out,in,len,rules);
74}
75
76int rewritehost_list(stralloc *out,char *in,unsigned int len,stralloc *rules)
77{
78 int i;
79 int j;
80
81 if (!stralloc_copys(out,"")) return 0;
82 for (j = i = 0;j < len;++j)
83 if (!in[j]) {
84 if (in[i] == '+') {
85 if (!stralloc_append(out,"+")) return 0;
86 if (!appendaddr(out,in + i + 1,j - i - 1,rules)) return 0;
87 if (!stralloc_0(out)) return 0;
88 }
89 else
90 if (!stralloc_catb(out,in + i,j - i + 1)) return 0;
91 i = j + 1;
92 }
93 return 1;
94}
int rewritehost_addr(stralloc *out, char *in, unsigned int len, stralloc *rules)
Definition: rewritehost.c:70
int rewritehost(stralloc *out, char *in, unsigned int len, stralloc *rules)
Definition: rewritehost.c:63
int rewritehost_list(stralloc *out, char *in, unsigned int len, stralloc *rules)
Definition: rewritehost.c:76
stralloc out
Definition: b64decode.c:12