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
10 void SimpleCaptcha::Generate()
\r
15 std::string puzzlestring;
\r
16 std::string tempfilename=GenerateRandomString(10);
\r
17 tempfilename+=".bmp";
\r
19 puzzlestring=GenerateRandomString(5);
\r
21 m_solution.insert(m_solution.begin(),puzzlestring.begin(),puzzlestring.end());
\r
23 bmp.SetSize(bmpwidth,bmpheight);
\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
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
41 // print puzzle onto bitmap
\r
42 for(int i=0; i<5; i++)
\r
44 // keep the colors dark
\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
52 bmp.WriteToFile(tempfilename.c_str());
\r
53 ReadPuzzleData(tempfilename);
\r
54 unlink(tempfilename.c_str());
\r
57 const std::string SimpleCaptcha::GenerateRandomString(const int len)
\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
64 temp+=chars[rand()%chars.size()];
\r
69 const bool SimpleCaptcha::GetPuzzle(std::vector<unsigned char> &puzzle)
\r
75 const bool SimpleCaptcha::GetSolution(std::vector<unsigned char> &solution)
\r
77 solution=m_solution;
\r
81 void SimpleCaptcha::ReadPuzzleData(const std::string &filename)
\r
84 FILE *infile=fopen(filename.c_str(),"rb");
\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