rucaptcha 2.6.1 → 3.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,301 +0,0 @@
1
- // http://github.com/ITikhonov/captcha
2
- const int gifsize;
3
- void captcha(unsigned char im[70 * 200], unsigned char l[8], int length, int i_line, int i_filter);
4
- void makegif(unsigned char im[70 * 200], unsigned char gif[gifsize], int style);
5
-
6
- #include <unistd.h>
7
- #include <stdint.h>
8
- #include <stdlib.h>
9
- #include <fcntl.h>
10
- #include <string.h>
11
- #include <time.h>
12
- #include <ruby.h>
13
- #include "font.h"
14
- #include "colors.h"
15
-
16
- static int8_t *lt[];
17
- const int gifsize = 17646;
18
-
19
- void makegif(unsigned char im[70 * 200], unsigned char gif[gifsize], int style)
20
- {
21
- // tag ; widthxheight ; GCT:0:0:7 ; bgcolor + aspect // GCT
22
- // Image Separator // left x top // widthxheight // Flags
23
- // LZW code size
24
- srand(time(NULL));
25
- int color_len = (int)sizeof(colors) / sizeof(colors[0]);
26
- int color_idx = rand() % color_len;
27
- if (style == 0)
28
- {
29
- color_idx = 0;
30
- }
31
- memcpy(gif, colors[color_idx], 13 + 48 + 10 + 1);
32
-
33
- int x, y;
34
- unsigned char *i = im;
35
- unsigned char *p = gif + 13 + 48 + 10 + 1;
36
- for (y = 0; y < 70; y++)
37
- {
38
- *p++ = 250; // Data length 5*50=250
39
- for (x = 0; x < 50; x++)
40
- {
41
- unsigned char a = i[0] >> 4, b = i[1] >> 4, c = i[2] >> 4, d = i[3] >> 4;
42
-
43
- p[0] = 16 | (a << 5); // bbb10000
44
- p[1] = (a >> 3) | 64 | (b << 7); // b10000xb
45
- p[2] = b >> 1; // 0000xbbb
46
- p[3] = 1 | (c << 1); // 00xbbbb1
47
- p[4] = 4 | (d << 3); // xbbbb100
48
- i += 4;
49
- p += 5;
50
- }
51
- }
52
-
53
- // Data length // End of LZW (b10001) // Terminator // GIF End
54
- memcpy(gif + gifsize - 4, "\x01"
55
- "\x11"
56
- "\x00"
57
- ";",
58
- 4);
59
- }
60
-
61
- static const int8_t sw[200] = {0, 4, 8, 12, 16, 20, 23, 27, 31, 35, 39, 43, 47, 50, 54, 58, 61, 65, 68, 71, 75, 78, 81, 84, 87, 90, 93, 96, 98, 101, 103, 105, 108, 110, 112, 114, 115, 117, 119, 120, 121, 122, 123, 124, 125, 126, 126, 127, 127, 127, 127, 127, 127, 127, 126, 126, 125, 124, 123, 122, 121, 120, 119, 117, 115, 114, 112, 110, 108, 105, 103, 101, 98, 96, 93, 90, 87, 84, 81, 78, 75, 71, 68, 65, 61, 58, 54, 50, 47, 43, 39, 35, 31, 27, 23, 20, 16, 12, 8, 4, 0, -4, -8, -12, -16, -20, -23, -27, -31, -35, -39, -43, -47, -50, -54, -58, -61, -65, -68, -71, -75, -78, -81, -84, -87, -90, -93, -96, -98, -101, -103, -105, -108, -110, -112, -114, -115, -117, -119, -120, -121, -122, -123, -124, -125, -126, -126, -127, -127, -127, -127, -127, -127, -127, -126, -126, -125, -124, -123, -122, -121, -120, -119, -117, -115, -114, -112, -110, -108, -105, -103, -101, -98, -96, -93, -90, -87, -84, -81, -78, -75, -71, -68, -65, -61, -58, -54, -50, -47, -43, -39, -35, -31, -27, -23, -20, -16, -12, -8, -4};
62
-
63
- #define MAX(x, y) ((x > y) ? (x) : (y))
64
-
65
- static int letter(int n, int pos, unsigned char im[70 * 200], unsigned char swr[200], uint8_t s1, uint8_t s2)
66
- {
67
- int8_t *p = lt[n];
68
- unsigned char *r = im + 200 * 16 + pos;
69
- unsigned char *i = r;
70
- int sk1 = s1 + pos;
71
- int sk2 = s2 + pos;
72
- int mpos = pos;
73
- int row = 0;
74
- for (; *p != -101; p++)
75
- {
76
- if (*p < 0)
77
- {
78
- if (*p == -100)
79
- {
80
- r += 200;
81
- i = r;
82
- sk1 = s1 + pos;
83
- row++;
84
- continue;
85
- }
86
- i += -*p;
87
- continue;
88
- }
89
-
90
- if (sk1 >= 200)
91
- sk1 = sk1 % 200;
92
- int skew = sw[sk1] / 16;
93
- sk1 += (swr[pos + i - r] & 0x1) + 1;
94
-
95
- if (sk2 >= 200)
96
- sk2 = sk2 % 200;
97
- int skewh = sw[sk2] / 70;
98
- sk2 += (swr[row] & 0x1);
99
-
100
- unsigned char *x = i + skew * 200 + skewh;
101
- mpos = MAX(mpos, pos + i - r);
102
-
103
- if ((x - im) < 70 * 200)
104
- *x = (*p) << 4;
105
- i++;
106
- }
107
- return mpos + 3;
108
- }
109
-
110
- #define NDOTS 10
111
-
112
- uint32_t dr[NDOTS];
113
-
114
- static void line(unsigned char im[70 * 200], unsigned char swr[200], uint8_t s1)
115
- {
116
- int x;
117
- int sk1 = s1;
118
- for (x = 0; x < 199; x++)
119
- {
120
- if (sk1 >= 200)
121
- sk1 = sk1 % 200;
122
- int skew = sw[sk1] / 20;
123
- sk1 += swr[x] & 0x3 + 1;
124
- unsigned char *i = im + (200 * (45 + skew) + x);
125
- i[0] = 0;
126
- i[1] = 0;
127
- i[2] = 0;
128
- i[3] = 0;
129
- i[4] = 0;
130
- i[195] = 0;
131
- i[196] = 0;
132
- i[197] = 0;
133
- i[198] = 0;
134
- i[200] = 0;
135
- }
136
- }
137
-
138
- static void dots(unsigned char im[70 * 200])
139
- {
140
- int n;
141
- for (n = 0; n < NDOTS; n++)
142
- {
143
- uint32_t v = dr[n];
144
- unsigned char *i = im + v % (200 * 67);
145
-
146
- i[0] = 0xff;
147
- i[1] = 0xff;
148
- i[2] = 0xff;
149
- i[200] = 0xff;
150
- i[201] = 0xff;
151
- i[202] = 0xff;
152
- }
153
- }
154
-
155
- static void blur(unsigned char im[70 * 200])
156
- {
157
- unsigned char *i = im;
158
- int x, y;
159
- for (y = 0; y < 68; y++)
160
- {
161
- for (x = 0; x < 198; x++)
162
- {
163
- unsigned int c11 = *i, c12 = i[1], c21 = i[200], c22 = i[201];
164
- *i++ = ((c11 + c12 + c21 + c22) / 4);
165
- }
166
- }
167
- }
168
-
169
- static void filter(unsigned char im[70 * 200])
170
- {
171
- unsigned char om[70 * 200];
172
- unsigned char *i = im;
173
- unsigned char *o = om;
174
-
175
- memset(om, 0xff, sizeof(om));
176
-
177
- int x, y;
178
- for (y = 0; y < 70; y++)
179
- {
180
- for (x = 4; x < 200 - 4; x++)
181
- {
182
- if (i[0] > 0xf0 && i[1] < 0xf0)
183
- {
184
- o[0] = 0;
185
- o[1] = 0;
186
- }
187
- else if (i[0] < 0xf0 && i[1] > 0xf0)
188
- {
189
- o[0] = 0;
190
- o[1] = 0;
191
- }
192
-
193
- i++;
194
- o++;
195
- }
196
- }
197
-
198
- memmove(im, om, sizeof(om));
199
- }
200
-
201
- static const char *letters = "abcdafahijklmnopqrstuvwxyz";
202
-
203
- void captcha(unsigned char im[70 * 200], unsigned char l[8], int length, int i_line, int i_filter)
204
- {
205
- unsigned char swr[200];
206
- uint8_t s1, s2;
207
-
208
- int f = open("/dev/urandom", O_RDONLY);
209
- read(f, l, 5);
210
- read(f, swr, 200);
211
- read(f, dr, sizeof(dr));
212
- read(f, &s1, 1);
213
- read(f, &s2, 1);
214
- close(f);
215
- memset(im, 0xff, 200 * 70);
216
- s1 = s1 & 0x7f;
217
- s2 = s2 & 0x3f;
218
-
219
- int x;
220
- for (x = 0; x < length; x++)
221
- {
222
- l[x] %= 25;
223
- }
224
- for (x = length; x < 8; x++)
225
- {
226
- l[length] = 0;
227
- }
228
- //l[0]%=25; l[1]%=25; l[2]%=25; l[3]%=25; l[4]=0; // l[4]%=25; l[5]=0;
229
- int p = 30;
230
- for (x = 0; x < length; x++)
231
- {
232
- p = letter(l[x], p, im, swr, s1, s2);
233
- }
234
-
235
- if (i_line == 1)
236
- {
237
- line(im, swr, s1);
238
- }
239
- dots(im);
240
- if (i_filter == 1)
241
- {
242
- blur(im);
243
- filter(im);
244
- }
245
-
246
- for (x = 0; x < length; x++)
247
- {
248
- l[x] = letters[l[x]];
249
- }
250
- //l[1]=letters[l[1]]; l[2]=letters[l[2]]; l[3]=letters[l[3]]; //l[4]=letters[l[4]];
251
- }
252
-
253
- // #ifdef CAPTCHA
254
- //
255
- // int main() {
256
- // char l[6];
257
- // unsigned char im[70*200];
258
- // unsigned char gif[gifsize];
259
- //
260
- // captcha(im,l);
261
- // makegif(im,gif);
262
- //
263
- // write(1,gif,gifsize);
264
- // write(2,l,5);
265
- //
266
- // return 0;
267
- // }
268
- //
269
- // #endif
270
-
271
- VALUE RuCaptcha = Qnil;
272
-
273
- void Init_rucaptcha();
274
-
275
- VALUE create(VALUE self, VALUE style, VALUE length, VALUE line, VALUE filter);
276
-
277
- void Init_rucaptcha()
278
- {
279
- RuCaptcha = rb_define_module("RuCaptcha");
280
- rb_define_singleton_method(RuCaptcha, "create", create, 4);
281
- }
282
-
283
- VALUE create(VALUE self, VALUE style, VALUE length, VALUE line, VALUE filter)
284
- {
285
- char l[8];
286
- unsigned char im[80 * 200];
287
- unsigned char gif[gifsize];
288
- int i_style = FIX2INT(style);
289
- int i_length = FIX2INT(length);
290
- int i_line = FIX2INT(line);
291
- int i_filter = FIX2INT(filter);
292
-
293
- captcha(im, l, i_length, i_line, i_filter);
294
- makegif(im, gif, i_style);
295
-
296
- VALUE result = rb_ary_new2(2);
297
- rb_ary_push(result, rb_str_new2(l));
298
- rb_ary_push(result, rb_str_new(gif, gifsize));
299
-
300
- return result;
301
- }