File: protobuf.c

package info (click to toggle)
pidgin 2.14.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 52,348 kB
  • sloc: ansic: 253,272; sh: 4,565; makefile: 3,170; python: 1,232; perl: 533; cs: 209; tcl: 96; xml: 9
file content (153 lines) | stat: -rw-r--r-- 3,897 bytes parent folder | download | duplicates (10)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* $Id$ */

/*
 *  (C) Copyright 2012 Tomek Wasilczyk <www.wasilczyk.pl>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License Version
 *  2.1 as published by the Free Software Foundation.
 *
 *  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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser 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.
 */

/**
 * \file protobuf.c
 *
 * \brief Funkcje pomocnicze do obsługi formatu protocol buffers
 */

#include "protobuf.h"

#define GG_PROTOBUFF_UIN_MAXLEN 15
struct _gg_protobuf_uin_buff
{
	char data[GG_PROTOBUFF_UIN_MAXLEN + 1 + 2];
};

void gg_protobuf_expected(struct gg_session *gs, const char *field_name,
	uint32_t value, uint32_t expected)
{
	if (value == expected) {
		return;
	}
	gg_debug_session(gs, GG_DEBUG_WARNING, "// gg_packet: field %s was "
		"expected to be %#x, but its value was %#x\n",
		field_name, expected, value);
}

int gg_protobuf_valid_chknull(struct gg_session *gs, const char *msg_name,
	int isNull)
{
	if (isNull) {
		gg_debug_session(gs, GG_DEBUG_ERROR, "// gg_protobuf: couldn't "
			"unpack %s message\n", msg_name);
	}
	return !isNull;
}

int gg_protobuf_valid_chkunknown(struct gg_session *gs, const char *msg_name,
	ProtobufCMessage *base)
{
	if (base->n_unknown_fields > 0) {
		gg_debug_session(gs, GG_DEBUG_WARNING, "// gg_protobuf: message"
		" %s had %d unknown field(s)\n",
		msg_name, base->n_unknown_fields);
	}
	return 1;
}

int gg_protobuf_send_ex(struct gg_session *gs, struct gg_event *ge, int type,
	void *msg, gg_protobuf_size_cb_t size_cb,
	gg_protobuf_pack_cb_t pack_cb)
{
	void *buffer;
	size_t len;
	int succ = 1;
	enum gg_failure_t failure;

	len = size_cb(msg);
	buffer = malloc(len);
	if (buffer == NULL) {
		gg_debug_session(gs, GG_DEBUG_ERROR, "// gg_protobuf_send: out "
			"of memory - tried to allocate %" GG_SIZE_FMT
			" bytes for %#x packet\n", len, type);
		succ = 0;
		failure = GG_FAILURE_INTERNAL;
	} else {
		pack_cb(msg, buffer);
		succ = (-1 != gg_send_packet(gs, type, buffer, len, NULL));
		free(buffer);
		buffer = NULL;
		if (!succ) {
			failure = GG_FAILURE_WRITING;
			gg_debug_session(gs, GG_DEBUG_ERROR,
				"// gg_protobuf_send: sending packet %#x "
				"failed. (errno=%d, %s)\n", type, errno,
				strerror(errno));
		}
	}

	if (!succ) {
		gg_connection_failure(gs, ge, failure);
	}

	return succ;
}

void gg_protobuf_set_uin(ProtobufCBinaryData *dst, uin_t uin, gg_protobuf_uin_buff_t *buff)
{
	char *uin_str;
	int uin_len;
	static gg_protobuf_uin_buff_t static_buffer;

	if (buff == NULL) {
		buff = &static_buffer;
	}

	uin_str = buff->data + 2;
	uin_len = snprintf(uin_str, GG_PROTOBUFF_UIN_MAXLEN + 1, "%u", uin);

	buff->data[0] = 0x01; /* magic: 0x00 lub 0x01 */
	buff->data[1] = uin_len;

	dst->len = uin_len + 2;
	dst->data = (uint8_t*)&buff->data;
}

uin_t gg_protobuf_get_uin(ProtobufCBinaryData uin_data)
{
	uint8_t magic;
	size_t uin_len;
	const char *uin_str;
	uin_t uin;

	magic = (uin_data.len > 0) ? uin_data.data[0] : 0;
	uin_len = (uin_data.len > 1) ? uin_data.data[1] : 0;

	if (uin_data.len != uin_len + 2 || uin_len > 10) {
		gg_debug(GG_DEBUG_ERROR, "// gg_protobuf_get_uin: "
			"invalid length\n");
		return 0;
	}
	if (magic != 0) {
		gg_debug(GG_DEBUG_WARNING, "// gg_protobuf_get_uin: "
			"unexpected magic value=%#x\n", magic);
	}

	uin_str = (char*)(uin_data.data + 2);
	uin = gg_str_to_uin(uin_str, uin_len);

	if (uin == 0) {
		gg_debug(GG_DEBUG_ERROR, "// gg_protobuf_get_uin: "
			"invalid uin\n");
	}
	return uin;
}