1 #include "datathread.h"
3 bool DataThread::Init()
5 parser = XML_ParserCreate(NULL);
7 printfd(__FILE__, "Error creating XML parser\n");
9 XML_SetStartElementHandler(parser, StartHandler);
10 XML_SetEndElementHandler(parser, EndHandler);
11 XML_SetCharacterDataHandler(parser, DataHandler);
14 DataThread::~DataThread()
16 XML_ParserFree(parser);
19 void * DataThread::Run(void * val)
21 DataThread * dt = reinterpret_cast<DataThread *>(val);
42 void DataThread::Handle()
47 if (!PrepareContext())
50 res = read(sock, &size, sizeof(size));
51 if (res != sizeof(size))
53 printfd(__FILE__, "Reading stream size failed! Wanted %d bytes, got %d bytes.\n", sizeof(size), res);
57 printfd(__FILE__, "DataThread::Handle() size = %d\n", size);
60 printfd(__FILE__, "DataThread::Handle() Invalid data size.\n");
64 buf = new unsigned char[size];
65 res = read(sock, buf, size);
68 printfd(__FILE__, "Reading stream failed! Wanted %d bytes, got %d bytes.\n", size, res);
73 Decode(buf, data, size);
75 printfd(__FILE__, "Received XML: %s\n", data.c_str());
77 XML_ParserReset(parser, NULL);
79 if (XML_Parse(parser, data.c_str(), data.length(), true) == XML_STATUS_OK) {
90 bool DataThread::PrepareContext()
95 int res = read(sock, &size, sizeof(size));
96 if (res != sizeof(size))
98 printfd(__FILE__, "Reading stream size failed! Wanted %d bytes, got %d bytes.\n", sizeof(size), res);
102 printfd(__FILE__, "DataThread::Handle() size = %d\n", size);
105 printfd(__FILE__, "DataThread::Handle() Invalid data size.\n");
109 login = new char[size];
111 res = read(sock, login, size);
114 printfd(__FILE__, "Reading login failed! Wanted %d bytes, got %d bytes.\n", 32, res);
119 l.assign(login, size);
123 if (users->FindByName(l, &it))
125 printfd(__FILE__, "User '%s' not found.\n", login);
129 password = it->property.password;
131 printfd(__FILE__, "DataThread::Handle() Requested user: '%s'\n", login);
132 printfd(__FILE__, "DataThread::Handle() Encryption initiated using password: '%s'\n", password.c_str());
134 char * key = new char[password.length()];
135 strncpy(key, password.c_str(), password.length());
138 reinterpret_cast<unsigned char *>(key),
145 void DataThread::Encode(const std::string & src, char * dst, int size)
147 const char * ptr = src.c_str();
148 for (int i = 0; i < size / 8; ++i) {
151 a = n2l(ptr + i * 8);
152 b = n2l(ptr + i * 8 + 4);
153 Blowfish_Encrypt(&ctx,
157 l2n(b, dst + i * 8 + 4);
161 void DataThread::Decode(char * src, std::string & dst, int size)
167 for (int i = 0; i < size / 8; ++i) {
170 a = n2l(src + i * 8);
171 b = n2l(src + i * 8 + 4);
172 Blowfish_Decrypt(&ctx,
182 void StartHandler(void *data, const char *el, const char **attr)
184 printfd(__FILE__, "Node: %s\n", el);
187 void EndHandler(void *data, const char *el)
191 void DataHandler(void *data, const char *el)