version 0.3.20
[fms.git] / src / bitmapvalidator.cpp
index 68482c1..12ba6de 100644 (file)
@@ -1,27 +1,52 @@
 #include "../include/bitmapvalidator.h"\r
 #include "../include/freenet/captcha/easybmp/EasyBMP.h"\r
 \r
+#include <Poco/TemporaryFile.h>\r
+\r
 #include <sstream>\r
+#include <cstdlib>\r
+\r
+BitmapValidator::BitmapValidator():m_maxwidth(-1),m_maxheight(-1)\r
+{\r
+       \r
+}\r
+\r
+BitmapValidator::~BitmapValidator()\r
+{\r
+       \r
+}\r
 \r
 const bool BitmapValidator::Validate(const std::vector<unsigned char> &data)\r
 {\r
        bool validated=false;\r
-       std::ostringstream tempname;\r
-       \r
-       tempname << "validatebmp-" << rand() << ".tmp";\r
-       FILE *outfile=fopen(tempname.str().c_str(),"w+b");\r
+       std::string tempname=Poco::TemporaryFile::tempName();\r
+\r
+       if(data.size()==0)\r
+       {\r
+               return false;\r
+       }\r
+\r
+       FILE *outfile=fopen(tempname.c_str(),"w+b");\r
        if(outfile)\r
        {\r
                fwrite(&data[0],1,data.size(),outfile);\r
                fclose(outfile);\r
                \r
                BMP temp;\r
-               if(temp.ReadFromFile(tempname.str().c_str()))\r
+               if(temp.ReadFromFile(tempname.c_str()))\r
                {\r
-                       validated=true; \r
+                       validated=true;\r
+                       if(m_maxwidth!=-1 && temp.TellWidth()>m_maxwidth)\r
+                       {\r
+                               validated=false;\r
+                       }\r
+                       if(m_maxheight!=-1 && temp.TellHeight()>m_maxheight)\r
+                       {\r
+                               validated=false;\r
+                       }\r
                }\r
 \r
-               unlink(tempname.str().c_str());\r
+               unlink(tempname.c_str());\r
                \r
        }\r
        \r