summaryrefslogtreecommitdiff
path: root/src/captcha.cpp
blob: 2d44ebce21195b6fd21815ee58017737768cfa30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "captcha.hpp"

#include <b64/encode.h>
#include <sstream>

#define GIFSIZE 17646
extern "C" {
	const int gifsize = GIFSIZE;
	void captcha( unsigned char im[70*200], unsigned char l[6]);
	void makegif(unsigned char im[70*200], unsigned char gif[gifsize]);
}

struct captcha generateCaptcha( )
{
	struct captcha c;
	unsigned char l[6];
	unsigned char *im = new unsigned char[70*200];
	unsigned char *gif = new unsigned char[gifsize];

	captcha( im, l );

	c.text = std::string( (char *)l );

	std::ostringstream raws( std::stringstream::out | std::stringstream::binary );
	makegif( im, gif );
	raws.write( (char *)gif, gifsize );
	std::string raw = raws.str( );
	
	std::istringstream in( std::ios_base::in | std::ios_base::binary );
	in.str( raw );
	
	base64::encoder E;
	std::string base_s;
	base_s.reserve( 3 * gifsize );
	std::ostringstream base( base_s, std::stringstream::out | std::stringstream::binary );
	E.encode( in, base );
	c.base64 = base.str( );
	
	delete gif;
	delete im;
	
	return c;
}