version 0.0.3
[fms.git] / src / freenet / captcha / simplecaptcha.cpp
1 #include "../../../include/freenet/captcha/simplecaptcha.h"\r
2 #include "../../../include/freenet/captcha/easybmp/EasyBMP.h"\r
3 #include "../../../include/freenet/captcha/easybmp/EasyBMP_Font.h"\r
4 #include "../../../include/freenet/captcha/easybmp/EasyBMP_Geometry.h"\r
5 \r
6 #ifdef XMEM\r
7         #include <xmem.h>\r
8 #endif\r
9 \r
10 void SimpleCaptcha::Generate()\r
11 {\r
12         BMP bmp;\r
13         int bmpwidth=100;\r
14         int bmpheight=50;\r
15         std::string puzzlestring;\r
16         std::string tempfilename=GenerateRandomString(10);\r
17         tempfilename+=".bmp";\r
18 \r
19         puzzlestring=GenerateRandomString(5);\r
20         m_solution.clear();\r
21         m_solution.insert(m_solution.begin(),puzzlestring.begin(),puzzlestring.end());\r
22 \r
23         bmp.SetSize(bmpwidth,bmpheight);\r
24 \r
25         // first draw some random lines around the bitmap\r
26         int numlines=(rand()%20)+10;\r
27         for(int i=0; i<numlines; i++)\r
28         {\r
29                 RGBApixel pixel;\r
30                 int x1=rand()%bmpwidth;\r
31                 int y1=rand()%bmpheight;\r
32                 int x2=rand()%bmpwidth;\r
33                 int y2=rand()%bmpwidth;\r
34                 // keep the colors light\r
35                 pixel.Red=100+(rand()%150);\r
36                 pixel.Green=100+(rand()%150);\r
37                 pixel.Blue=100+(rand()%150);\r
38                 DrawAALine(bmp,x1,y1,x2,y2,pixel);\r
39         }\r
40 \r
41         // print puzzle onto bitmap\r
42         for(int i=0; i<5; i++)\r
43         {\r
44                 // keep the colors dark\r
45                 RGBApixel pixel;\r
46                 pixel.Red=(rand()%200);\r
47                 pixel.Green=(rand()%200);\r
48                 pixel.Blue=(rand()%200);\r
49                 PrintLetter(bmp,puzzlestring[i],5+(i*20),10+(rand()%10),20,pixel);\r
50         }\r
51 \r
52         bmp.WriteToFile(tempfilename.c_str());\r
53         ReadPuzzleData(tempfilename);\r
54         unlink(tempfilename.c_str());\r
55 }\r
56 \r
57 const std::string SimpleCaptcha::GenerateRandomString(const int len)\r
58 {\r
59         // no l,1 O,0 because they look too much alike\r
60         static std::string chars="abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789";\r
61         std::string temp="";\r
62         for(int i=0; i<len; i++)\r
63         {\r
64                 temp+=chars[rand()%chars.size()];\r
65         }\r
66         return temp;\r
67 }\r
68 \r
69 const bool SimpleCaptcha::GetPuzzle(std::vector<unsigned char> &puzzle)\r
70 {\r
71         puzzle=m_puzzle;\r
72         return true;\r
73 }\r
74 \r
75 const bool SimpleCaptcha::GetSolution(std::vector<unsigned char> &solution)\r
76 {\r
77         solution=m_solution;\r
78         return true;\r
79 }\r
80 \r
81 void SimpleCaptcha::ReadPuzzleData(const std::string &filename)\r
82 {\r
83         long filelen;\r
84         FILE *infile=fopen(filename.c_str(),"rb");\r
85         if(infile)\r
86         {\r
87                 fseek(infile,0,SEEK_END);\r
88                 filelen=ftell(infile);\r
89                 fseek(infile,0,SEEK_SET);\r
90                 m_puzzle.resize(filelen);\r
91                 fread(&m_puzzle[0],1,filelen,infile);\r
92                 fclose(infile);\r
93         }\r
94 }