2 * Functions converting HTSMSGs to/from a simple binary format
3 * Copyright (C) 2007 Andreas Ă–man
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <sys/types.h>
27 #include "htsmsg_binary.h"
33 htsmsg_binary_des0(htsmsg_t *msg, const uint8_t *buf, size_t len)
35 unsigned type, namelen, datalen;
46 datalen = (buf[2] << 24) |
54 if(len < namelen + datalen)
57 f = malloc(sizeof(htsmsg_field_t));
61 n = malloc(namelen + 1);
62 memcpy(n, buf, namelen);
67 f->hmf_flags = HMF_NAME_ALLOCED;
78 f->hmf_str = n = malloc(datalen + 1);
79 memcpy(n, buf, datalen);
81 f->hmf_flags |= HMF_ALLOCED;
85 f->hmf_bin = (const void *)buf;
86 f->hmf_binsize = datalen;
91 for(i = datalen - 1; i >= 0; i--)
92 u64 = (u64 << 8) | buf[i];
99 TAILQ_INIT(&sub->hm_fields);
101 if(htsmsg_binary_des0(sub, buf, datalen) < 0)
111 TAILQ_INSERT_TAIL(&msg->hm_fields, f, hmf_link);
124 htsmsg_binary_deserialize(const void *data, size_t len, const void *buf)
126 htsmsg_t *msg = htsmsg_create_map();
129 if(htsmsg_binary_des0(msg, data, len) < 0) {
142 htsmsg_binary_count(htsmsg_t *msg)
148 TAILQ_FOREACH(f, &msg->hm_fields, hmf_link) {
151 len += f->hmf_name ? strlen(f->hmf_name) : 0;
153 switch(f->hmf_type) {
156 len += htsmsg_binary_count(&f->hmf_msg);
160 len += strlen(f->hmf_str);
164 len += f->hmf_binsize;
184 htsmsg_binary_write(htsmsg_t *msg, uint8_t *ptr)
190 TAILQ_FOREACH(f, &msg->hm_fields, hmf_link) {
191 namelen = f->hmf_name ? strlen(f->hmf_name) : 0;
192 *ptr++ = f->hmf_type;
195 switch(f->hmf_type) {
198 l = htsmsg_binary_count(&f->hmf_msg);
202 l = strlen(f->hmf_str);
228 memcpy(ptr, f->hmf_name, namelen);
232 switch(f->hmf_type) {
235 htsmsg_binary_write(&f->hmf_msg, ptr);
239 memcpy(ptr, f->hmf_str, l);
243 memcpy(ptr, f->hmf_bin, l);
248 for(i = 0; i < l; i++) {
249 ptr[i] = (uint8_t)(u64 & 0xFF);
263 htsmsg_binary_serialize(htsmsg_t *msg, void **datap, size_t *lenp, int maxlen)
268 len = htsmsg_binary_count(msg);
269 if(len + 4 > (size_t)maxlen)
272 data = malloc(len + 4);
279 htsmsg_binary_write(msg, data + 4);