version 0.0.1
[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         static std::string chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";\r
60         std::string temp="";\r
61         for(int i=0; i<len; i++)\r
62         {\r
63                 temp+=chars[rand()%chars.size()];\r
64         }\r
65         return temp;\r
66 }\r
67 \r
68 const bool SimpleCaptcha::GetPuzzle(std::vector<unsigned char> &puzzle)\r
69 {\r
70         puzzle=m_puzzle;\r
71         return true;\r
72 }\r
73 \r
74 const bool SimpleCaptcha::GetSolution(std::vector<unsigned char> &solution)\r
75 {\r
76         solution=m_solution;\r
77         return true;\r
78 }\r
79 \r
80 void SimpleCaptcha::ReadPuzzleData(const std::string &filename)\r
81 {\r
82         long filelen;\r
83         FILE *infile=fopen(filename.c_str(),"rb");\r
84         if(infile)\r
85         {\r
86                 fseek(infile,0,SEEK_END);\r
87                 filelen=ftell(infile);\r
88                 fseek(infile,0,SEEK_SET);\r
89                 m_puzzle.resize(filelen);\r
90                 fread(&m_puzzle[0],1,filelen,infile);\r
91                 fclose(infile);\r
92         }\r
93 }