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
|
/**
* @file obex_hdr_membuf.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 <membuf.h>
#include <obex_hdr.h>
#include <string.h>
struct obex_hdr_membuf {
enum obex_hdr_id id;
enum obex_hdr_type type;
struct databuffer *buf;
};
static
void * obex_hdr_membuf_new(enum obex_hdr_id id, enum obex_hdr_type type,
const void *value, size_t size)
{
struct obex_hdr_membuf *hdr = malloc(sizeof(*hdr));
if (!hdr)
return NULL;
hdr->id = id;
hdr->type = type;
hdr->buf = membuf_create(size);
if (hdr->buf == NULL) {
free(hdr);
return NULL;
}
buf_append(hdr->buf, value, size);
return hdr;
}
static
void obex_hdr_membuf_destroy(void *self)
{
struct obex_hdr_membuf *hdr = self;
buf_delete(hdr->buf);
free(hdr);
}
static
enum obex_hdr_id obex_hdr_membuf_get_id(void *self)
{
struct obex_hdr_membuf *hdr = self;
return hdr->id;
}
static
enum obex_hdr_type obex_hdr_membuf_get_type(void *self)
{
struct obex_hdr_membuf *hdr = self;
return hdr->type;
}
static
size_t obex_hdr_membuf_get_data_size(void *self)
{
struct obex_hdr_membuf *hdr = self;
return buf_get_length(hdr->buf);
}
static
const void * obex_hdr_membuf_get_data_ptr(void *self)
{
struct obex_hdr_membuf *hdr = self;
return buf_get(hdr->buf);
}
static
bool obex_hdr_membuf_set_data(void *self, const void *data, size_t size)
{
struct obex_hdr_membuf *hdr = self;
buf_clear(hdr->buf, buf_get_length(hdr->buf));
if (buf_set_size(hdr->buf, size) != 0)
return false;
memcpy(buf_get(hdr->buf), data, size);
return true;
}
static
struct obex_hdr_ops obex_hdr_membuf_ops = {
&obex_hdr_membuf_destroy,
&obex_hdr_membuf_get_id,
&obex_hdr_membuf_get_type,
&obex_hdr_membuf_get_data_size,
&obex_hdr_membuf_get_data_ptr,
&obex_hdr_membuf_set_data,
NULL,
NULL,
};
struct obex_hdr * obex_hdr_membuf_create(enum obex_hdr_id id,
enum obex_hdr_type type,
const void *data, size_t size)
{
void *buf = obex_hdr_membuf_new(id, type, data, size);
if (!buf)
return NULL;
return obex_hdr_new(&obex_hdr_membuf_ops, buf);
}
struct databuffer * obex_hdr_membuf_get_databuffer(struct obex_hdr *hdr)
{
struct obex_hdr_membuf *memhdr = hdr->data;
return memhdr->buf;
}
|