content.c 8.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
linphone
Copyright (C) 2010-2014 Belledonne Communications SARL

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
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 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
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 19
*/

20
#include "linphone/core.h"
21

22
#include "c-wrapper/c-wrapper.h"
23

24 25 26
// TODO: From coreapi. Remove me later.
#include "private.h"

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
static void linphone_content_set_sal_body_handler(LinphoneContent *content, SalBodyHandler *body_handler) {
	if (content->body_handler != NULL) {
		sal_body_handler_unref(content->body_handler);
		content->body_handler = NULL;
	}
	content->body_handler = sal_body_handler_ref(body_handler);
}

static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler *body_handler) {
	LinphoneContent *content = belle_sip_object_new(LinphoneContent);
	belle_sip_object_ref(content);
	content->owned_fields = TRUE;
	content->cryptoContext = NULL; /* this field is managed externally by encryption/decryption functions so be careful to initialise it to NULL */
	if (body_handler == NULL) {
		linphone_content_set_sal_body_handler(content, sal_body_handler_new());
	} else {
		linphone_content_set_sal_body_handler(content, body_handler);
	}
	return content;
}

48
static void linphone_content_destroy(LinphoneContent *content) {
49
	if (content->owned_fields == TRUE) {
50 51 52
		if (content->body_handler) sal_body_handler_unref(content->body_handler);
		if (content->name) belle_sip_free(content->name);
		if (content->key) belle_sip_free(content->key);
53
		/* note : crypto context is allocated/destroyed by the encryption function */
54
	}
55 56 57
}

static void linphone_content_clone(LinphoneContent *obj, const LinphoneContent *ref) {
58
	obj->owned_fields = TRUE;
59 60 61 62 63 64 65 66
	linphone_content_set_sal_body_handler(obj, sal_body_handler_new());
	if ((linphone_content_get_type(ref) != NULL) || (linphone_content_get_subtype(ref) != NULL)) {
		linphone_content_set_type(obj, linphone_content_get_type(ref));
		linphone_content_set_subtype(obj, linphone_content_get_subtype(ref));
	}
	if (linphone_content_get_encoding(ref) != NULL) {
		linphone_content_set_encoding(obj, linphone_content_get_encoding(ref));
	}
67
	linphone_content_set_name(obj, linphone_content_get_name(ref));
68
	linphone_content_set_key(obj, linphone_content_get_key(ref), linphone_content_get_key_size(ref));
69 70 71 72
	if (linphone_content_get_buffer(ref) != NULL) {
		linphone_content_set_buffer(obj, linphone_content_get_buffer(ref), linphone_content_get_size(ref));
	} else {
		linphone_content_set_size(obj, linphone_content_get_size(ref));
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
	}
}


BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneContent);

BELLE_SIP_INSTANCIATE_VPTR(LinphoneContent, belle_sip_object_t,
	(belle_sip_object_destroy_t)linphone_content_destroy,
	(belle_sip_object_clone_t)linphone_content_clone,
	NULL, // marshal
	TRUE
);


LinphoneContent * linphone_core_create_content(LinphoneCore *lc) {
	return linphone_content_new();
}

LinphoneContent * linphone_content_ref(LinphoneContent *content) {
	belle_sip_object_ref(content);
	return content;
}

void linphone_content_unref(LinphoneContent *content) {
	belle_sip_object_unref(content);
}

void *linphone_content_get_user_data(const LinphoneContent *content) {
	return content->user_data;
}

void linphone_content_set_user_data(LinphoneContent *content, void *ud) {
	content->user_data = ud;
}

const char * linphone_content_get_type(const LinphoneContent *content) {
109
	return sal_body_handler_get_type(content->body_handler);
110 111 112
}

void linphone_content_set_type(LinphoneContent *content, const char *type) {
113
	sal_body_handler_set_type(content->body_handler, type);
114 115 116
}

const char * linphone_content_get_subtype(const LinphoneContent *content) {
117
	return sal_body_handler_get_subtype(content->body_handler);
118 119 120
}

void linphone_content_set_subtype(LinphoneContent *content, const char *subtype) {
121
	sal_body_handler_set_subtype(content->body_handler, subtype);
122 123
}

124 125
uint8_t * linphone_content_get_buffer(const LinphoneContent *content) {
	return (uint8_t *)sal_body_handler_get_data(content->body_handler);
126 127
}

128
void linphone_content_set_buffer(LinphoneContent *content, const uint8_t *buffer, size_t size) {
129 130 131 132 133 134
	void *data;
	sal_body_handler_set_size(content->body_handler, size);
	data = belle_sip_malloc(size + 1);
	memcpy(data, buffer, size);
	((char *)data)[size] = '\0';
	sal_body_handler_set_data(content->body_handler, data);
135 136
}

137
const char * linphone_content_get_string_buffer(const LinphoneContent *content) {
138
	return (const char *)linphone_content_get_buffer(content);
139 140 141
}

void linphone_content_set_string_buffer(LinphoneContent *content, const char *buffer) {
142 143
	sal_body_handler_set_size(content->body_handler, strlen(buffer));
	sal_body_handler_set_data(content->body_handler, belle_sip_strdup(buffer));
144 145
}

146
size_t linphone_content_get_size(const LinphoneContent *content) {
147
	return sal_body_handler_get_size(content->body_handler);
148 149 150
}

void linphone_content_set_size(LinphoneContent *content, size_t size) {
151
	sal_body_handler_set_size(content->body_handler, size);
152 153 154
}

const char * linphone_content_get_encoding(const LinphoneContent *content) {
155
	return sal_body_handler_get_encoding(content->body_handler);
156 157 158
}

void linphone_content_set_encoding(LinphoneContent *content, const char *encoding) {
159
	sal_body_handler_set_encoding(content->body_handler, encoding);
160 161 162
}

const char * linphone_content_get_name(const LinphoneContent *content) {
163
	return content->name;
164 165 166
}

void linphone_content_set_name(LinphoneContent *content, const char *name) {
167 168 169
	if (content->name != NULL) {
		belle_sip_free(content->name);
		content->name = NULL;
170 171
	}
	if (name != NULL) {
172
		content->name = belle_sip_strdup(name);
173 174 175
	}
}

176
size_t linphone_content_get_key_size(const LinphoneContent *content) {
177
	return content->keyLength;
178 179 180
}

const char * linphone_content_get_key(const LinphoneContent *content) {
181
	return content->key;
182 183 184
}

void linphone_content_set_key(LinphoneContent *content, const char *key, const size_t keyLength) {
185 186 187
	if (content->key != NULL) {
		belle_sip_free(content->key);
		content->key = NULL;
188 189
	}
	if (key != NULL) {
Ghislain MARY's avatar
Ghislain MARY committed
190
		content->key = reinterpret_cast<char *>(belle_sip_malloc(keyLength + 1));
191
		memcpy(content->key, key, keyLength);
192
		content->key[keyLength] = '\0';
193
		content->keyLength = keyLength;
194 195 196 197 198
	}
}

/* crypto context is managed(allocated/freed) by the encryption function, so provide the address of field in the private structure */
void ** linphone_content_get_cryptoContext_address(LinphoneContent *content) {
199
	return &(content->cryptoContext);
200
}
201

202
bool_t linphone_content_is_multipart(const LinphoneContent *content) {
Ghislain MARY's avatar
Ghislain MARY committed
203
	return sal_body_handler_is_multipart(content->body_handler);
204
}
205

Ghislain MARY's avatar
Ghislain MARY committed
206 207 208 209 210
LinphoneContent * linphone_content_get_part(const LinphoneContent *content, int idx) {
	SalBodyHandler *part_body_handler;
	if (!linphone_content_is_multipart(content)) return NULL;
	part_body_handler = sal_body_handler_get_part(content->body_handler, idx);
	return linphone_content_from_sal_body_handler(part_body_handler);
211 212
}

Ghislain MARY's avatar
Ghislain MARY committed
213 214 215 216 217
LinphoneContent * linphone_content_find_part_by_header(const LinphoneContent *content, const char *header_name, const char *header_value) {
	SalBodyHandler *part_body_handler;
	if (!linphone_content_is_multipart(content)) return NULL;
	part_body_handler = sal_body_handler_find_part_by_header(content->body_handler, header_name, header_value);
	return linphone_content_from_sal_body_handler(part_body_handler);
218 219
}

220
const char * linphone_content_get_custom_header(const LinphoneContent *content, const char *header_name) {
Ghislain MARY's avatar
Ghislain MARY committed
221
	return sal_body_handler_get_header(content->body_handler, header_name);
222 223 224
}


225 226 227 228 229 230 231 232 233 234 235 236 237
LinphoneContent * linphone_content_new(void) {
	return linphone_content_new_with_body_handler(NULL);
}

LinphoneContent * linphone_content_copy(const LinphoneContent *ref) {
	return (LinphoneContent *)belle_sip_object_ref(belle_sip_object_clone(BELLE_SIP_OBJECT(ref)));
}

LinphoneContent * linphone_content_from_sal_body_handler(SalBodyHandler *body_handler) {
	if (body_handler) {
		return linphone_content_new_with_body_handler(body_handler);
	}
	return NULL;
238 239
}

240 241 242
SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) {
	if (content == NULL) return NULL;
	return content->body_handler;
243
}