version 0.3.20
[fms.git] / src / bitmapvalidator.cpp
1 #include "../include/bitmapvalidator.h"\r
2 #include "../include/freenet/captcha/easybmp/EasyBMP.h"\r
3 \r
4 #include <Poco/TemporaryFile.h>\r
5 \r
6 #include <sstream>\r
7 #include <cstdlib>\r
8 \r
9 BitmapValidator::BitmapValidator():m_maxwidth(-1),m_maxheight(-1)\r
10 {\r
11         \r
12 }\r
13 \r
14 BitmapValidator::~BitmapValidator()\r
15 {\r
16         \r
17 }\r
18 \r
19 const bool BitmapValidator::Validate(const std::vector<unsigned char> &data)\r
20 {\r
21         bool validated=false;\r
22         std::string tempname=Poco::TemporaryFile::tempName();\r
23 \r
24         if(data.size()==0)\r
25         {\r
26                 return false;\r
27         }\r
28 \r
29         FILE *outfile=fopen(tempname.c_str(),"w+b");\r
30         if(outfile)\r
31         {\r
32                 fwrite(&data[0],1,data.size(),outfile);\r
33                 fclose(outfile);\r
34                 \r
35                 BMP temp;\r
36                 if(temp.ReadFromFile(tempname.c_str()))\r
37                 {\r
38                         validated=true;\r
39                         if(m_maxwidth!=-1 && temp.TellWidth()>m_maxwidth)\r
40                         {\r
41                                 validated=false;\r
42                         }\r
43                         if(m_maxheight!=-1 && temp.TellHeight()>m_maxheight)\r
44                         {\r
45                                 validated=false;\r
46                         }\r
47                 }\r
48 \r
49                 unlink(tempname.c_str());\r
50                 \r
51         }\r
52         \r
53         return validated;\r
54 }\r