summaryrefslogtreecommitdiff
path: root/protocols/JabberG/src/jabber_zstream.cpp
blob: 87f21d44aa6064c1e7494c167056d4dd3fb1eb76 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*

Jabber Protocol Plugin for Miranda NG

XEP-0138 (Stream Compression) implementation

Copyright (c) 2005-12  George Hazan
Copyright (c) 2007     Kostya Chukavin, Taras Zackrepa
Copyright (C) 2012-23 Miranda NG team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "stdafx.h"

bool ThreadData::zlibInit(void)
{
	proto->debugLogA("Zlib init...");
	zStreamIn.zalloc = Z_NULL;
	zStreamIn.zfree = Z_NULL;
	zStreamIn.opaque = Z_NULL;
	zStreamIn.next_in = Z_NULL;
	zStreamIn.avail_in = 0;

	zStreamOut.zalloc = Z_NULL;
	zStreamOut.zfree = Z_NULL;
	zStreamOut.opaque = Z_NULL;

	if (deflateInit(&zStreamOut, Z_BEST_COMPRESSION) != Z_OK) return false;
	if (inflateInit(&zStreamIn) != Z_OK) return false;

	zRecvReady = true;
	return true;
}

void ThreadData::zlibUninit(void)
{
	deflateEnd(&zStreamOut);
	inflateEnd(&zStreamIn);
}

int ThreadData::zlibSend(char* data, int datalen)
{
	char send_data[ZLIB_CHUNK_SIZE];
	int bytesOut = 0;

	zStreamOut.avail_in = datalen;
	zStreamOut.next_in = (unsigned char*)data;

	do {
		zStreamOut.avail_out = ZLIB_CHUNK_SIZE;
		zStreamOut.next_out = (unsigned char*)send_data;

		switch (deflate(&zStreamOut, Z_SYNC_FLUSH)) {
			case Z_OK:         proto->debugLogA("Deflate: Z_OK");         break;
			case Z_BUF_ERROR:  proto->debugLogA("Deflate: Z_BUF_ERROR");  break;
			case Z_DATA_ERROR: proto->debugLogA("Deflate: Z_DATA_ERROR"); break;
			case Z_MEM_ERROR:  proto->debugLogA("Deflate: Z_MEM_ERROR");  break;
		}

		int send_datalen = ZLIB_CHUNK_SIZE - zStreamOut.avail_out;
		int len = Netlib_Send(s, send_data, send_datalen, MSG_NODUMP);
		if (len == SOCKET_ERROR || len != send_datalen) {
			proto->debugLogA("Netlib_Send() failed, error=%d", WSAGetLastError());
			return SOCKET_ERROR;
		}

		bytesOut += len;
	}
		while (zStreamOut.avail_out == 0);

	if (db_get_b(0, "Netlib", "DumpSent", TRUE) == TRUE)
		proto->debugLogA("(ZLIB) Data sent\n%s\n===OUT: %d(%d) bytes", data, datalen, bytesOut);

	return bytesOut;
}

int ThreadData::zlibRecv(char* data, long datalen)
{
	if (zRecvReady) {
retry:
		zRecvDatalen = Netlib_Recv(s, zRecvData, ZLIB_CHUNK_SIZE, MSG_NODUMP);
		if (zRecvDatalen == SOCKET_ERROR) {
			proto->debugLogA("Netlib_Recv() failed, error=%d", WSAGetLastError());
			return SOCKET_ERROR;
		}
		if (zRecvDatalen == 0) {
			proto->debugLogA("Connection closed gracefully");
			return 0;
		}

		zStreamIn.avail_in = zRecvDatalen;
		zStreamIn.next_in = (Bytef*)zRecvData;
	}

	zStreamIn.avail_out = datalen;
	zStreamIn.next_out = (uint8_t*)data;

	switch (inflate(&zStreamIn, Z_NO_FLUSH)) {
		case Z_OK:         proto->debugLogA("Inflate: Z_OK");         break;
		case Z_BUF_ERROR:  proto->debugLogA("Inflate: Z_BUF_ERROR");  break;
		case Z_DATA_ERROR: proto->debugLogA("Inflate: Z_DATA_ERROR"); break;
		case Z_MEM_ERROR:  proto->debugLogA("Inflate: Z_MEM_ERROR");  break;
	}

	int len = datalen - zStreamIn.avail_out;
	if (db_get_b(0, "Netlib", "DumpRecv", TRUE) == TRUE) {
		char* szLogBuffer = (char*)alloca(len+32);
		memcpy(szLogBuffer, data, len);
		szLogBuffer[ len ]='\0';
		proto->debugLogA("(ZLIB) Data received\n%s\n===IN: %d(%d) bytes", szLogBuffer, len, zRecvDatalen);
	}

	if (len == 0)
		goto retry;

	zRecvReady = (zStreamIn.avail_out != 0);
	return len;
}