-
Notifications
You must be signed in to change notification settings - Fork 46
/
replace-1.c
146 lines (127 loc) · 3.67 KB
/
replace-1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <sys/timeb.h>
//http://creativeandcritical.net/str-replace-c/
char *replace_str(const char *str, const char *old, const char *new)
{
char *ret, *r;
const char *p, *q;
size_t oldlen = strlen(old);
size_t count, retlen, newlen = strlen(new);
if (oldlen != newlen) {
for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
count++;
/* this is undefined if p - str > PTRDIFF_MAX */
retlen = p - str + strlen(p) + count * (newlen - oldlen);
} else
retlen = strlen(str);
if ((ret = malloc(retlen + 1)) == NULL)
return NULL;
for (r = ret, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen) {
/* this is undefined if q - p > PTRDIFF_MAX */
ptrdiff_t l = q - p;
memcpy(r, p, l);
r += l;
memcpy(r, new, newlen);
r += newlen;
}
strcpy(r, p);
return ret;
}
int random_int(int min, int max)
{
/* Seed number for rand() */
struct timeb t;
ftime(&t);
srand((unsigned int) 1000 * t.time + t.millitm + random());
//srandom( time(0)+clock()+random() );
unsigned int s_seed = 214013 * rand() + 2531011;
return min+(s_seed ^ s_seed>>15)%(max-min+1);
}
//http://www.cnblogs.com/chenyuming507950417/archive/2012/01/02/2310114.html
char *replace_ip(const char *str, const char *old)
{
char *ret, *r;
const char *p, *q;
size_t oldlen = strlen(old);
size_t count, retlen, newlen = 4;
char ip[3];
for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
count++;
/* this is undefined if p - str > PTRDIFF_MAX */
retlen = p - str + strlen(p) + count * (newlen - oldlen);
if ((ret = malloc(retlen + 1)) == NULL)
return NULL;
for (r = ret, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen) {
/* this is undefined if q - p > PTRDIFF_MAX */
ptrdiff_t l = q - p;
memcpy(r, p, l);
r += l;
bzero(ip, newlen);
sprintf(ip, "%d", random_int(1, 255));
newlen = strlen(ip);
printf("%s=%d\n", ip, newlen);
memcpy(r, ip, newlen);
r += newlen;
}
strcpy(r, p);
return ret;
}
char *
random_chars(char *dst, int start, int end)
{
srandom( time(0)+clock()+random() ); //生成更好的随机数
static const char allowable_chars[] = "1234567890abcdefhijklnmopqrstuwxyz";
int i, r;
int size = rand()%(end - start + 1) + start;/*n为a~b之间的随机数*/
for (i = 0; i< size; i++) {
r = (int)((double)rand() / ((double)RAND_MAX + 1) * (sizeof(allowable_chars) -1 ));
dst[i] = allowable_chars[r];
}
dst[i] = '\0';
return dst;
}
char *
replace_domain(const char *str, const char *old)
{
char *ret, *r;
const char *p, *q;
size_t oldlen = strlen(old);
size_t count, retlen, newlen = 26;
char s[25];
for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
count++;
/* this is undefined if p - str > PTRDIFF_MAX */
retlen = p - str + strlen(p) + count * (newlen - oldlen);
if ((ret = malloc(retlen + 1)) == NULL)
return NULL;
for (r = ret, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen) {
/* this is undefined if q - p > PTRDIFF_MAX */
ptrdiff_t l = q - p;
memcpy(r, p, l);
r += l;
bzero(s, newlen);
random_chars(s, 5, 25);
newlen = strlen(s);
printf("%s=%d\n", s, newlen);
memcpy(r, s, newlen);
r += newlen;
}
strcpy(r, p);
return ret;
}
int main(int argc, char **argv)
{
char *s1, *s2, *s3, *d1, *d2;
s1 = "1.*.33.*";
d1 = "*.*.com";
printf("%d\n", random_int(1, 300));
s2 = replace_str(s1, "*", "23");
printf("s2=%s\n", s2);
s3 = replace_ip(s1, "*");
printf("s3=%s\n", s3);
d2 = replace_domain(d1, "*");
printf("d2=%s\n", d2);
}