2011-06-29 04:32:53 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* OBEX library with GLib integration
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 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 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-10-18 16:12:25 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-10-20 15:49:42 +08:00
|
|
|
#include <config.h>
|
2011-10-18 16:12:25 +08:00
|
|
|
#endif
|
|
|
|
|
2011-06-29 04:32:53 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "gobex-header.h"
|
2011-10-21 18:34:00 +08:00
|
|
|
#include "gobex-debug.h"
|
2011-06-29 04:32:53 +08:00
|
|
|
|
|
|
|
/* Header types */
|
2011-07-11 02:47:49 +08:00
|
|
|
#define G_OBEX_HDR_ENC_UNICODE (0 << 6)
|
|
|
|
#define G_OBEX_HDR_ENC_BYTES (1 << 6)
|
|
|
|
#define G_OBEX_HDR_ENC_UINT8 (2 << 6)
|
|
|
|
#define G_OBEX_HDR_ENC_UINT32 (3 << 6)
|
2011-06-29 04:32:53 +08:00
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
#define G_OBEX_HDR_ENC(id) ((id) & 0xc0)
|
2011-06-29 04:32:53 +08:00
|
|
|
|
|
|
|
struct _GObexHeader {
|
|
|
|
guint8 id;
|
|
|
|
gboolean extdata;
|
2011-06-29 16:04:12 +08:00
|
|
|
gsize vlen; /* Length of value */
|
|
|
|
gsize hlen; /* Length of full encoded header */
|
2011-06-29 04:32:53 +08:00
|
|
|
union {
|
|
|
|
char *string; /* UTF-8 converted from UTF-16 */
|
|
|
|
guint8 *data; /* Own buffer */
|
|
|
|
const guint8 *extdata; /* Reference to external buffer */
|
|
|
|
guint8 u8;
|
|
|
|
guint32 u32;
|
|
|
|
} v;
|
|
|
|
};
|
|
|
|
|
|
|
|
static glong utf8_to_utf16(gunichar2 **utf16, const char *utf8) {
|
|
|
|
glong utf16_len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (*utf8 == '\0') {
|
|
|
|
*utf16 = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*utf16 = g_utf8_to_utf16(utf8, -1, NULL, &utf16_len, NULL);
|
|
|
|
if (*utf16 == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* g_utf8_to_utf16 produces host-byteorder UTF-16,
|
|
|
|
* but OBEX requires network byteorder (big endian) */
|
|
|
|
for (i = 0; i < utf16_len; i++)
|
|
|
|
(*utf16)[i] = g_htons((*utf16)[i]);
|
|
|
|
|
|
|
|
utf16_len = (utf16_len + 1) << 1;
|
|
|
|
|
|
|
|
return utf16_len;
|
|
|
|
}
|
|
|
|
|
2011-06-29 16:04:12 +08:00
|
|
|
static guint8 *put_bytes(guint8 *to, const void *from, gsize count)
|
2011-06-29 04:32:53 +08:00
|
|
|
{
|
|
|
|
memcpy(to, from, count);
|
|
|
|
return (to + count);
|
|
|
|
}
|
|
|
|
|
2011-06-29 16:04:12 +08:00
|
|
|
static const guint8 *get_bytes(void *to, const guint8 *from, gsize count)
|
2011-06-29 04:32:53 +08:00
|
|
|
{
|
|
|
|
memcpy(to, from, count);
|
|
|
|
return (from + count);
|
|
|
|
}
|
|
|
|
|
2011-07-05 17:33:57 +08:00
|
|
|
gssize g_obex_header_encode(GObexHeader *header, void *buf, gsize buf_len)
|
2011-06-29 04:32:53 +08:00
|
|
|
{
|
|
|
|
guint8 *ptr = buf;
|
|
|
|
guint16 u16;
|
|
|
|
guint32 u32;
|
|
|
|
gunichar2 *utf16;
|
|
|
|
glong utf16_len;
|
|
|
|
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x",
|
|
|
|
G_OBEX_HDR_ENC(header->id));
|
|
|
|
|
2011-06-29 04:32:53 +08:00
|
|
|
if (buf_len < header->hlen)
|
2011-07-05 17:33:57 +08:00
|
|
|
return -1;
|
2011-06-29 04:32:53 +08:00
|
|
|
|
|
|
|
ptr = put_bytes(ptr, &header->id, sizeof(header->id));
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
switch (G_OBEX_HDR_ENC(header->id)) {
|
|
|
|
case G_OBEX_HDR_ENC_UNICODE:
|
2011-06-29 04:32:53 +08:00
|
|
|
utf16_len = utf8_to_utf16(&utf16, header->v.string);
|
|
|
|
if (utf16_len < 0 || (guint16) utf16_len > buf_len)
|
2011-07-05 17:33:57 +08:00
|
|
|
return -1;
|
2011-06-29 04:32:53 +08:00
|
|
|
g_assert_cmpuint(utf16_len + 3, ==, header->hlen);
|
|
|
|
u16 = g_htons(utf16_len + 3);
|
|
|
|
ptr = put_bytes(ptr, &u16, sizeof(u16));
|
|
|
|
ptr = put_bytes(ptr, utf16, utf16_len);
|
|
|
|
g_free(utf16);
|
|
|
|
break;
|
2011-07-11 02:47:49 +08:00
|
|
|
case G_OBEX_HDR_ENC_BYTES:
|
2011-06-29 04:32:53 +08:00
|
|
|
u16 = g_htons(header->hlen);
|
|
|
|
ptr = put_bytes(ptr, &u16, sizeof(u16));
|
|
|
|
if (header->extdata)
|
|
|
|
ptr = put_bytes(ptr, header->v.extdata, header->vlen);
|
|
|
|
else
|
|
|
|
ptr = put_bytes(ptr, header->v.data, header->vlen);
|
|
|
|
break;
|
2011-07-11 02:47:49 +08:00
|
|
|
case G_OBEX_HDR_ENC_UINT8:
|
2011-06-29 04:32:53 +08:00
|
|
|
*ptr = header->v.u8;
|
|
|
|
break;
|
2011-07-11 02:47:49 +08:00
|
|
|
case G_OBEX_HDR_ENC_UINT32:
|
2011-06-29 04:32:53 +08:00
|
|
|
u32 = g_htonl(header->v.u32);
|
|
|
|
ptr = put_bytes(ptr, &u32, sizeof(u32));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
return header->hlen;
|
|
|
|
}
|
|
|
|
|
2011-06-29 16:04:12 +08:00
|
|
|
GObexHeader *g_obex_header_decode(const void *data, gsize len,
|
2011-06-29 18:15:05 +08:00
|
|
|
GObexDataPolicy data_policy, gsize *parsed,
|
|
|
|
GError **err)
|
2011-06-29 04:32:53 +08:00
|
|
|
{
|
|
|
|
GObexHeader *header;
|
|
|
|
const guint8 *ptr = data;
|
|
|
|
guint16 hdr_len;
|
2011-06-29 16:04:12 +08:00
|
|
|
gsize str_len;
|
2011-06-29 18:15:05 +08:00
|
|
|
GError *conv_err = NULL;
|
2011-06-29 04:32:53 +08:00
|
|
|
|
2011-06-29 18:15:05 +08:00
|
|
|
if (len < 2) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR, G_OBEX_ERROR_PARSE_ERROR,
|
|
|
|
"Too short header in packet");
|
2011-10-21 18:34:00 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_ERROR, "%s", (*err)->message);
|
2011-06-29 04:32:53 +08:00
|
|
|
return NULL;
|
2011-06-29 18:15:05 +08:00
|
|
|
}
|
2011-06-29 04:32:53 +08:00
|
|
|
|
|
|
|
header = g_new0(GObexHeader, 1);
|
|
|
|
|
|
|
|
ptr = get_bytes(&header->id, ptr, sizeof(header->id));
|
|
|
|
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x",
|
|
|
|
G_OBEX_HDR_ENC(header->id));
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
switch (G_OBEX_HDR_ENC(header->id)) {
|
|
|
|
case G_OBEX_HDR_ENC_UNICODE:
|
2011-07-04 20:54:53 +08:00
|
|
|
if (len < 3) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR,
|
|
|
|
G_OBEX_ERROR_PARSE_ERROR,
|
|
|
|
"Not enough data for unicode header (0x%02x)",
|
|
|
|
header->id);
|
2011-06-29 04:32:53 +08:00
|
|
|
goto failed;
|
2011-07-04 20:54:53 +08:00
|
|
|
}
|
2011-06-29 04:32:53 +08:00
|
|
|
ptr = get_bytes(&hdr_len, ptr, sizeof(hdr_len));
|
|
|
|
hdr_len = g_ntohs(hdr_len);
|
2011-08-14 21:16:25 +08:00
|
|
|
|
|
|
|
if (hdr_len == 3) {
|
|
|
|
header->v.string = g_strdup("");
|
|
|
|
header->vlen = 0;
|
|
|
|
header->hlen = hdr_len;
|
|
|
|
*parsed = hdr_len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-29 18:15:05 +08:00
|
|
|
if (hdr_len > len || hdr_len < 5) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR,
|
2011-06-29 21:02:29 +08:00
|
|
|
G_OBEX_ERROR_PARSE_ERROR,
|
|
|
|
"Invalid unicode header (0x%02x) length (%u)",
|
|
|
|
header->id, hdr_len);
|
2011-06-29 04:32:53 +08:00
|
|
|
goto failed;
|
2011-06-29 18:15:05 +08:00
|
|
|
}
|
2011-06-29 04:32:53 +08:00
|
|
|
|
|
|
|
header->v.string = g_convert((const char *) ptr, hdr_len - 5,
|
|
|
|
"UTF8", "UTF16BE",
|
2011-06-29 18:15:05 +08:00
|
|
|
NULL, &str_len, &conv_err);
|
|
|
|
if (header->v.string == NULL) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR,
|
|
|
|
G_OBEX_ERROR_PARSE_ERROR,
|
|
|
|
"Unicode conversion failed: %s",
|
|
|
|
conv_err->message);
|
|
|
|
g_error_free(conv_err);
|
2011-06-29 04:32:53 +08:00
|
|
|
goto failed;
|
2011-06-29 18:15:05 +08:00
|
|
|
}
|
2011-06-29 04:32:53 +08:00
|
|
|
|
2011-06-29 16:04:12 +08:00
|
|
|
header->vlen = (gsize) str_len;
|
2011-06-29 04:32:53 +08:00
|
|
|
header->hlen = hdr_len;
|
|
|
|
|
|
|
|
*parsed = hdr_len;
|
|
|
|
|
|
|
|
break;
|
2011-07-11 02:47:49 +08:00
|
|
|
case G_OBEX_HDR_ENC_BYTES:
|
2011-06-29 18:15:05 +08:00
|
|
|
if (len < 3) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR,
|
|
|
|
G_OBEX_ERROR_PARSE_ERROR,
|
|
|
|
"Too short byte array header");
|
2011-06-29 04:32:53 +08:00
|
|
|
goto failed;
|
2011-06-29 18:15:05 +08:00
|
|
|
}
|
2011-06-29 04:32:53 +08:00
|
|
|
ptr = get_bytes(&hdr_len, ptr, sizeof(hdr_len));
|
|
|
|
hdr_len = g_ntohs(hdr_len);
|
2011-06-29 18:15:05 +08:00
|
|
|
if (hdr_len > len) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR,
|
|
|
|
G_OBEX_ERROR_PARSE_ERROR,
|
|
|
|
"Too long byte array header");
|
2011-06-29 04:32:53 +08:00
|
|
|
goto failed;
|
2011-06-29 18:15:05 +08:00
|
|
|
}
|
2011-06-29 04:32:53 +08:00
|
|
|
|
2011-10-06 19:31:13 +08:00
|
|
|
if (hdr_len < 3) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR,
|
|
|
|
G_OBEX_ERROR_PARSE_ERROR,
|
|
|
|
"Too small byte array length");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2011-06-29 04:32:53 +08:00
|
|
|
header->vlen = hdr_len - 3;
|
|
|
|
header->hlen = hdr_len;
|
|
|
|
|
|
|
|
switch (data_policy) {
|
|
|
|
case G_OBEX_DATA_COPY:
|
|
|
|
header->v.data = g_memdup(ptr, header->vlen);
|
|
|
|
break;
|
|
|
|
case G_OBEX_DATA_REF:
|
|
|
|
header->extdata = TRUE;
|
|
|
|
header->v.extdata = ptr;
|
|
|
|
break;
|
|
|
|
default:
|
2011-06-29 18:15:05 +08:00
|
|
|
g_set_error(err, G_OBEX_ERROR,
|
|
|
|
G_OBEX_ERROR_INVALID_ARGS,
|
|
|
|
"Invalid data policy");
|
2011-06-29 04:32:53 +08:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
*parsed = hdr_len;
|
|
|
|
|
|
|
|
break;
|
2011-07-11 02:47:49 +08:00
|
|
|
case G_OBEX_HDR_ENC_UINT8:
|
2011-06-29 04:32:53 +08:00
|
|
|
header->vlen = 1;
|
|
|
|
header->hlen = 2;
|
|
|
|
header->v.u8 = *ptr;
|
|
|
|
*parsed = 2;
|
|
|
|
break;
|
2011-07-11 02:47:49 +08:00
|
|
|
case G_OBEX_HDR_ENC_UINT32:
|
2011-06-29 18:15:05 +08:00
|
|
|
if (len < 5) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR,
|
|
|
|
G_OBEX_ERROR_PARSE_ERROR,
|
|
|
|
"Too short uint32 header");
|
2011-06-29 04:32:53 +08:00
|
|
|
goto failed;
|
2011-06-29 18:15:05 +08:00
|
|
|
}
|
2011-06-29 04:32:53 +08:00
|
|
|
header->vlen = 4;
|
|
|
|
header->hlen = 5;
|
|
|
|
ptr = get_bytes(&header->v.u32, ptr, sizeof(header->v.u32));
|
|
|
|
header->v.u32 = g_ntohl(header->v.u32);
|
|
|
|
*parsed = 5;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
return header;
|
|
|
|
|
|
|
|
failed:
|
2011-10-21 18:34:00 +08:00
|
|
|
if (*err)
|
|
|
|
g_obex_debug(G_OBEX_DEBUG_ERROR, "%s", (*err)->message);
|
2011-06-29 04:32:53 +08:00
|
|
|
g_obex_header_free(header);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_obex_header_free(GObexHeader *header)
|
|
|
|
{
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x",
|
|
|
|
G_OBEX_HDR_ENC(header->id));
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
switch (G_OBEX_HDR_ENC(header->id)) {
|
|
|
|
case G_OBEX_HDR_ENC_UNICODE:
|
2011-06-29 04:32:53 +08:00
|
|
|
g_free(header->v.string);
|
|
|
|
break;
|
2011-07-11 02:47:49 +08:00
|
|
|
case G_OBEX_HDR_ENC_BYTES:
|
2011-06-29 04:32:53 +08:00
|
|
|
if (!header->extdata)
|
|
|
|
g_free(header->v.data);
|
|
|
|
break;
|
2011-07-11 02:47:49 +08:00
|
|
|
case G_OBEX_HDR_ENC_UINT8:
|
|
|
|
case G_OBEX_HDR_ENC_UINT32:
|
2011-06-29 04:32:53 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(header);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean g_obex_header_get_unicode(GObexHeader *header, const char **str)
|
|
|
|
{
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x",
|
|
|
|
G_OBEX_HDR_ENC(header->id));
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
if (G_OBEX_HDR_ENC(header->id) != G_OBEX_HDR_ENC_UNICODE)
|
2011-06-29 04:32:53 +08:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*str = header->v.string;
|
|
|
|
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "%s", *str);
|
|
|
|
|
2011-06-29 04:32:53 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean g_obex_header_get_bytes(GObexHeader *header, const guint8 **val,
|
2011-06-29 16:04:12 +08:00
|
|
|
gsize *len)
|
2011-06-29 04:32:53 +08:00
|
|
|
{
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x",
|
|
|
|
G_OBEX_HDR_ENC(header->id));
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
if (G_OBEX_HDR_ENC(header->id) != G_OBEX_HDR_ENC_BYTES)
|
2011-06-29 04:32:53 +08:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*len = header->vlen;
|
|
|
|
|
|
|
|
if (header->extdata)
|
|
|
|
*val = header->v.extdata;
|
|
|
|
else
|
|
|
|
*val = header->v.data;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean g_obex_header_get_uint8(GObexHeader *header, guint8 *val)
|
|
|
|
{
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x",
|
|
|
|
G_OBEX_HDR_ENC(header->id));
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
if (G_OBEX_HDR_ENC(header->id) != G_OBEX_HDR_ENC_UINT8)
|
2011-06-29 04:32:53 +08:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*val = header->v.u8;
|
|
|
|
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "%u", *val);
|
|
|
|
|
2011-06-29 04:32:53 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean g_obex_header_get_uint32(GObexHeader *header, guint32 *val)
|
|
|
|
{
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x",
|
|
|
|
G_OBEX_HDR_ENC(header->id));
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
if (G_OBEX_HDR_ENC(header->id) != G_OBEX_HDR_ENC_UINT32)
|
2011-06-29 04:32:53 +08:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*val = header->v.u32;
|
|
|
|
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "%u", *val);
|
|
|
|
|
2011-06-29 04:32:53 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GObexHeader *g_obex_header_new_unicode(guint8 id, const char *str)
|
|
|
|
{
|
|
|
|
GObexHeader *header;
|
2011-06-29 16:04:12 +08:00
|
|
|
gsize len;
|
2011-06-29 04:32:53 +08:00
|
|
|
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x", G_OBEX_HDR_ENC(id));
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
if (G_OBEX_HDR_ENC(id) != G_OBEX_HDR_ENC_UNICODE)
|
2011-06-29 04:32:53 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
header = g_new0(GObexHeader, 1);
|
|
|
|
|
|
|
|
header->id = id;
|
|
|
|
|
|
|
|
len = g_utf8_strlen(str, -1);
|
|
|
|
|
|
|
|
header->vlen = len;
|
2011-08-14 21:16:25 +08:00
|
|
|
header->hlen = len == 0 ? 3 : 3 + ((len + 1) * 2);
|
2011-06-29 04:32:53 +08:00
|
|
|
header->v.string = g_strdup(str);
|
|
|
|
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "%s", header->v.string);
|
|
|
|
|
2011-06-29 04:32:53 +08:00
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
2011-07-11 01:59:12 +08:00
|
|
|
GObexHeader *g_obex_header_new_bytes(guint8 id, const void *data, gsize len)
|
2011-06-29 04:32:53 +08:00
|
|
|
{
|
|
|
|
GObexHeader *header;
|
|
|
|
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x", G_OBEX_HDR_ENC(id));
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
if (G_OBEX_HDR_ENC(id) != G_OBEX_HDR_ENC_BYTES)
|
2011-06-29 04:32:53 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
header = g_new0(GObexHeader, 1);
|
|
|
|
|
|
|
|
header->id = id;
|
|
|
|
header->vlen = len;
|
|
|
|
header->hlen = len + 3;
|
2011-07-10 18:57:55 +08:00
|
|
|
header->v.data = g_memdup(data, len);
|
2011-06-29 04:32:53 +08:00
|
|
|
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
GObexHeader *g_obex_header_new_uint8(guint8 id, guint8 val)
|
|
|
|
{
|
|
|
|
GObexHeader *header;
|
|
|
|
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x", G_OBEX_HDR_ENC(id));
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
if (G_OBEX_HDR_ENC(id) != G_OBEX_HDR_ENC_UINT8)
|
2011-06-29 04:32:53 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
header = g_new0(GObexHeader, 1);
|
|
|
|
|
|
|
|
header->id = id;
|
|
|
|
header->vlen = 1;
|
|
|
|
header->hlen = 2;
|
|
|
|
header->v.u8 = val;
|
|
|
|
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "%u", header->v.u8);
|
|
|
|
|
2011-06-29 04:32:53 +08:00
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
GObexHeader *g_obex_header_new_uint32(guint8 id, guint32 val)
|
|
|
|
{
|
|
|
|
GObexHeader *header;
|
|
|
|
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x", G_OBEX_HDR_ENC(id));
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
if (G_OBEX_HDR_ENC(id) != G_OBEX_HDR_ENC_UINT32)
|
2011-06-29 04:32:53 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
header = g_new0(GObexHeader, 1);
|
|
|
|
|
|
|
|
header->id = id;
|
|
|
|
header->vlen = 4;
|
|
|
|
header->hlen = 5;
|
|
|
|
header->v.u32 = val;
|
|
|
|
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "%u", header->v.u32);
|
|
|
|
|
2011-06-29 04:32:53 +08:00
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
guint8 g_obex_header_get_id(GObexHeader *header)
|
|
|
|
{
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x id 0x%02x",
|
|
|
|
G_OBEX_HDR_ENC(header->id), header->id);
|
|
|
|
|
2011-06-29 04:32:53 +08:00
|
|
|
return header->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
guint16 g_obex_header_get_length(GObexHeader *header)
|
|
|
|
{
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x length %zu",
|
|
|
|
G_OBEX_HDR_ENC(header->id), header->hlen);
|
|
|
|
|
2011-06-29 04:32:53 +08:00
|
|
|
return header->hlen;
|
|
|
|
}
|
2011-07-11 02:36:47 +08:00
|
|
|
|
|
|
|
GSList *g_obex_header_create_list(guint8 first_hdr_id, va_list args,
|
|
|
|
gsize *total_len)
|
|
|
|
{
|
|
|
|
unsigned int id = first_hdr_id;
|
|
|
|
GSList *l = NULL;
|
|
|
|
|
2011-10-21 18:34:03 +08:00
|
|
|
g_obex_debug(G_OBEX_DEBUG_HEADER, "");
|
|
|
|
|
2011-07-11 02:36:47 +08:00
|
|
|
*total_len = 0;
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
while (id != G_OBEX_HDR_INVALID) {
|
2011-07-11 02:36:47 +08:00
|
|
|
GObexHeader *hdr;
|
|
|
|
const char *str;
|
|
|
|
const void *bytes;
|
|
|
|
unsigned int val;
|
|
|
|
gsize len;
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
switch (G_OBEX_HDR_ENC(id)) {
|
|
|
|
case G_OBEX_HDR_ENC_UNICODE:
|
2011-07-11 02:36:47 +08:00
|
|
|
str = va_arg(args, const char *);
|
|
|
|
hdr = g_obex_header_new_unicode(id, str);
|
|
|
|
break;
|
2011-07-11 02:47:49 +08:00
|
|
|
case G_OBEX_HDR_ENC_BYTES:
|
2011-07-11 02:36:47 +08:00
|
|
|
bytes = va_arg(args, void *);
|
|
|
|
len = va_arg(args, gsize);
|
|
|
|
hdr = g_obex_header_new_bytes(id, bytes, len);
|
|
|
|
break;
|
2011-07-11 02:47:49 +08:00
|
|
|
case G_OBEX_HDR_ENC_UINT8:
|
2011-07-11 02:36:47 +08:00
|
|
|
val = va_arg(args, unsigned int);
|
|
|
|
hdr = g_obex_header_new_uint8(id, val);
|
|
|
|
break;
|
2011-07-11 02:47:49 +08:00
|
|
|
case G_OBEX_HDR_ENC_UINT32:
|
2011-07-11 02:36:47 +08:00
|
|
|
val = va_arg(args, unsigned int);
|
|
|
|
hdr = g_obex_header_new_uint32(id, val);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
l = g_slist_append(l, hdr);
|
|
|
|
*total_len += hdr->hlen;
|
|
|
|
id = va_arg(args, int);
|
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|