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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//convert label.png -virtual-pixel White -distort Arc 60 arctest.png
//convert -background gray -fill cyan -pointsize 72 label:Alex label.png
//convert label.png -virtual-pixel white -distort Barrel "0.2 0.0 0.0 1.0" barrel.png
#define ADJNUM 200
#define NOUNUM 1524
#define AVGADJ 7
#define AVGNOU 7
#define LONGADJ 14
#define LONGNOU 15
void createRandomWords(char* buf){
long adjnum = rand() % ADJNUM;
long nounum = rand() % NOUNUM;
FILE* adjfile;
FILE* noufile;
adjfile = fopen("../ws2a/adj.txt","r");
noufile = fopen("../ws2a/nouns.txt","r");
//Instead of getting a particular line, seek an average length, and find the next word.
if(adjfile == NULL || noufile == NULL){
printf("Something has gone serirously wrong!");
}
fseek(adjfile,adjnum*AVGADJ,0);
fseek(noufile,nounum*AVGNOU,0);
while(fgetc(adjfile) != '\n'){}
while(fgetc(noufile) != '\n'){}
int pos = 0;
char tchar = fgetc(adjfile);
do{
buf[pos] = tchar;
pos++;
tchar = fgetc(adjfile);
}while(tchar != '\n');
buf[pos] = ' ';
pos++;
tchar = fgetc(noufile);
do{
buf[pos] = tchar;
pos++;
tchar = fgetc(noufile);
}while(tchar != '\n');
buf[pos] = '\0';
}
unsigned long hash(unsigned char *str){
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
int main(){
srand(time(NULL));
char randwords[LONGADJ+LONGNOU+1];
createRandomWords(randwords);
int barraldis1 = rand()%10;
int barraldis2 = rand()%10;
int arcdis = (rand()%99)+1;
char command[1024];
unsigned long rhash = hash(randwords);
sprintf(command,"convert -background white -fill black -pointsize 48 label:\"%s\" \"/home/git/wswebpage/ws2a/captchas/%lu.png\"",randwords,rhash);
system(command);
sprintf(command,"convert \"/home/git/wswebpage/ws2a/captchas/%lu.png\" -distort Barrel \"0.0%d 0.0 0.0%d\" \"/home/git/wswebpage/ws2a/captchas/%lu.png\"",rhash,barraldis1,barraldis2,rhash);
system(command);
sprintf(command,"convert \"/home/git/wswebpage/ws2a/captchas/%lu.png\" -virtual-pixel White -distort Arc %d \"/home/git/wswebpage/ws2a/captchas/%lu.png\"",rhash,arcdis,rhash);
system(command);
FILE* imgfile;
FILE* tofile;
char filepath[100];
char tofilepath[100];
sprintf(filepath,"../ws2a/captchas/%lu.png",rhash);
sprintf(tofilepath,"/home/git/wswebpage/ws2a/captchas/%lu.png",rhash);
//printf("Filepath:%s",filepath);
imgfile = fopen(filepath,"rb");
tofile = fopen(tofilepath,"wb");
if(imgfile == NULL){
printf("Unable to open file!");
return 1;
}
fseek(imgfile,0,SEEK_END);
unsigned long filelen = ftell(imgfile);
//printf("File length:%lu",filelen);
fseek(imgfile,0,SEEK_SET);
//fwrite(imgfile,1,filelen,stdout);
char* buffer = malloc(filelen+1);
if(buffer == NULL){
printf("Memory error!");
return 1;
}
fread(buffer, filelen, 1, imgfile);
fclose(imgfile);
//printf("%s%c%c\n","Content-Type:image/png",13,10);
int i = 0;
while(i < filelen){
fprintf(tofile,"%c",buffer[i]);
i++;
}
free(buffer);
fclose(tofile);
printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10);
printf("%lu",rhash);
/*
char c;
while((c = fgetc(imgfile)) && !feof(imgfile)){
printf("char is:something\n",c);
//putchar(c);
}
*/
}
|