content.c 8.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
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
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "linphonecore.h"
#include "private.h"



25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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;
}

46
static void linphone_content_destroy(LinphoneContent *content) {
47
	if (content->owned_fields == TRUE) {
48 49 50
		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);
51
		/* note : crypto context is allocated/destroyed by the encryption function */
52
	}
53 54 55
}

static void linphone_content_clone(LinphoneContent *obj, const LinphoneContent *ref) {
56
	obj->owned_fields = TRUE;
57 58 59 60 61 62 63 64
	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));
	}
65
	linphone_content_set_name(obj, linphone_content_get_name(ref));
66
	linphone_content_set_key(obj, linphone_content_get_key(ref), linphone_content_get_key_size(ref));
67 68 69 70
	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));
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
	}
}


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) {
107
	return sal_body_handler_get_type(content->body_handler);
108 109 110
}

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

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

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

122
void * linphone_content_get_buffer(const LinphoneContent *content) {
123
	return sal_body_handler_get_data(content->body_handler);
124 125
}

126
void linphone_content_set_buffer(LinphoneContent *content, const void *buffer, size_t size) {
127 128 129 130 131 132
	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);
133 134
}

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

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

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

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

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

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

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

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

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

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

void linphone_content_set_key(LinphoneContent *content, const char *key, const size_t keyLength) {
183 184 185
	if (content->key != NULL) {
		belle_sip_free(content->key);
		content->key = NULL;
186 187
	}
	if (key != NULL) {
188 189
		content->key = belle_sip_malloc(keyLength);
		memcpy(content->key, key, keyLength);
190 191 192 193 194
	}
}

/* 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) {
195
	return &(content->cryptoContext);
196
}
197

198
bool_t linphone_content_is_multipart(const LinphoneContent *content) {
199
	return sal_body_handler_is_multipart(content->body_handler);
200
}
201

202 203 204 205 206
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);
207 208
}

209 210 211 212 213
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);
214 215
}

216
const char * linphone_content_get_custom_header(const LinphoneContent *content, const char *header_name) {
217
	return sal_body_handler_get_header(content->body_handler, header_name);
218 219 220
}


221 222 223 224 225 226 227 228 229 230 231 232 233
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;
234 235
}

236 237 238
SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) {
	if (content == NULL) return NULL;
	return content->body_handler;
239
}