File: obex_hdr_ptr.c

package info (click to toggle)
libopenobex 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 972 kB
  • ctags: 1,552
  • sloc: ansic: 9,988; xml: 407; makefile: 96
file content (161 lines) | stat: -rw-r--r-- 3,308 bytes parent folder | download | duplicates (2)
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
154
155
156
157
158
159
160
161
/**
 * @file obex_hdr_ptr.c
 *
 * OBEX header releated functions.
 * OpenOBEX library - Free implementation of the Object Exchange protocol.
 *
 * Copyright (c) 2012 Hendrik Sattler, All Rights Reserved.
 *
 * OpenOBEX is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with OpenOBEX. If not, see <http://www.gnu.org/>.
 */

#include "obex_hdr.h"
#include "debug.h"

#ifndef _WIN32
#include <arpa/inet.h>
#endif
#include <string.h>

struct obex_hdr_ptr {
	enum obex_hdr_id id;
	enum obex_hdr_type type;
	size_t size;
	const void *value;
};

static
void obex_hdr_ptr_destroy(void *self)
{
	free(self);
}

static
enum obex_hdr_id obex_hdr_ptr_get_id(void *self)
{
	struct obex_hdr_ptr *ptr = self;
	return ptr->id;
}

static
enum obex_hdr_type obex_hdr_ptr_get_type(void *self)
{
	struct obex_hdr_ptr *ptr = self;
	return ptr->type;
}

static
size_t obex_hdr_ptr_get_data_size(void *self)
{
	struct obex_hdr_ptr *ptr = self;
	return ptr->size;
}

static
const void * obex_hdr_ptr_get_data_ptr(void *self)
{
	struct obex_hdr_ptr *ptr = self;
	return ptr->value;
}

static
bool obex_hdr_ptr_set_data(void *self, const void *data, size_t size)
{
	struct obex_hdr_ptr *ptr = self;
	ptr->value = data;
	ptr->size = size;
	return true;
}

static
struct obex_hdr_ops obex_hdr_ptr_ops = {
	&obex_hdr_ptr_destroy,
	&obex_hdr_ptr_get_id,
	&obex_hdr_ptr_get_type,
	&obex_hdr_ptr_get_data_size,
	&obex_hdr_ptr_get_data_ptr,
	&obex_hdr_ptr_set_data,
	NULL,
	NULL,
};

struct obex_hdr * obex_hdr_ptr_create(enum obex_hdr_id id,
				      enum obex_hdr_type type,
				      const void *data, size_t size)
{
	struct obex_hdr_ptr *ptr = malloc(sizeof(*ptr));

	if (!ptr)
		return NULL;

	ptr->id = id;
	ptr->type = type;
	ptr->size = size;
	ptr->value = data;

	return obex_hdr_new(&obex_hdr_ptr_ops, ptr);
}

struct obex_hdr * obex_hdr_ptr_parse(const void *msgdata, size_t size)
{
	struct obex_hdr_ptr *ptr;
	uint16_t hsize;

	if (size < 1)
		return NULL;

	ptr = malloc(sizeof(*ptr));
	if (!ptr)
		return NULL;

	ptr->id = ((uint8_t *)msgdata)[0] & OBEX_HDR_ID_MASK;
	ptr->type = ((uint8_t *)msgdata)[0] & OBEX_HDR_TYPE_MASK;

	switch (ptr->type) {
	case OBEX_HDR_TYPE_UNICODE:
	case OBEX_HDR_TYPE_BYTES:
		if (size < 3)
			goto err;
		memcpy(&hsize, (uint8_t *)msgdata + 1, 2);
		ptr->size = ntohs(hsize) - 3;
		if (size < (3 + ptr->size))
			goto err;
		ptr->value = (uint8_t *)msgdata + 3;
		break;

	case OBEX_HDR_TYPE_UINT8:
		if (size < 2)
			goto err;
		ptr->size = 1;
		ptr->value = (uint8_t *)msgdata + 1;
		break;

	case OBEX_HDR_TYPE_UINT32:
		if (size < 5)
			goto err;
		ptr->size = 4;
		ptr->value = (uint8_t *)msgdata + 1;
		break;

	default:
		goto err;
	}

	return obex_hdr_new(&obex_hdr_ptr_ops, ptr);

err:
	DEBUG(1, "Header too big.\n");
	free(ptr);
	return NULL;
}