c2a4a3c7cca47d8e71981f03e270c2c63fc064be
[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         FILE *outfile=fopen(tempname.c_str(),"w+b");\r
25         if(outfile)\r
26         {\r
27                 fwrite(&data[0],1,data.size(),outfile);\r
28                 fclose(outfile);\r
29                 \r
30                 BMP temp;\r
31                 if(temp.ReadFromFile(tempname.c_str()))\r
32                 {\r
33                         validated=true;\r
34                         if(m_maxwidth!=-1 && temp.TellWidth()>m_maxwidth)\r
35                         {\r
36                                 validated=false;\r
37                         }\r
38                         if(m_maxheight!=-1 && temp.TellHeight()>m_maxheight)\r
39                         {\r
40                                 validated=false;\r
41                         }\r
42                 }\r
43 \r
44                 unlink(tempname.c_str());\r
45                 \r
46         }\r
47         \r
48         return validated;\r
49 }\r