From 9cabfe37dd9a49bc3f2e7281b3aaf60589a88de8 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Thu, 20 Nov 2014 17:25:25 +0100 Subject: [PATCH] A LinphoneContent object now owns all its data fields including the buffer. A normal LinphoneContent object will now copy the data from the given buffer when calling linphone_content_set_buffer(). However LinphoneContent objects converted from LinphoneContentPrivate structures do not own its data fields. --- coreapi/content.c | 49 ++++++++++++++++--------------- coreapi/content.h | 10 +++---- coreapi/private.h | 1 + coreapi/quality_reporting.c | 3 +- tester/eventapi_tester.c | 17 ++++------- tester/flexisip_tester.c | 3 +- tester/message_tester.c | 7 ++--- tester/quality_reporting_tester.c | 8 ++--- 8 files changed, 46 insertions(+), 52 deletions(-) diff --git a/coreapi/content.c b/coreapi/content.c index 944ebb2e0..84e45c853 100644 --- a/coreapi/content.c +++ b/coreapi/content.c @@ -23,27 +23,24 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static void linphone_content_destroy(LinphoneContent *content) { - if (content->lcp.type) belle_sip_free(content->lcp.type); - if (content->lcp.subtype) belle_sip_free(content->lcp.subtype); - if (content->lcp.data) belle_sip_free(content->lcp.data); - if (content->lcp.encoding) belle_sip_free(content->lcp.encoding); - if (content->lcp.name) belle_sip_free(content->lcp.name); + if (content->owned_fields == TRUE) { + if (content->lcp.type) belle_sip_free(content->lcp.type); + if (content->lcp.subtype) belle_sip_free(content->lcp.subtype); + if (content->lcp.data) belle_sip_free(content->lcp.data); + if (content->lcp.encoding) belle_sip_free(content->lcp.encoding); + if (content->lcp.name) belle_sip_free(content->lcp.name); + } } static void linphone_content_clone(LinphoneContent *obj, const LinphoneContent *ref) { - void *data; linphone_content_set_type(obj, linphone_content_get_type(ref)); linphone_content_set_subtype(obj, linphone_content_get_subtype(ref)); linphone_content_set_encoding(obj, linphone_content_get_encoding(ref)); linphone_content_set_name(obj, linphone_content_get_name(ref)); - linphone_content_set_size(obj, linphone_content_get_size(ref)); - data = linphone_content_get_data(ref); - if (data != NULL) { - size_t size = linphone_content_get_size(ref); - void *objdata = belle_sip_malloc(size + 1); - memcpy(objdata, data, size); - ((char *)objdata)[size] = '\0'; - linphone_content_set_data(obj, objdata); + 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)); } } @@ -107,12 +104,15 @@ void linphone_content_set_subtype(LinphoneContent *content, const char *subtype) } } -void * linphone_content_get_data(const LinphoneContent *content) { +void * linphone_content_get_buffer(const LinphoneContent *content) { return content->lcp.data; } -void linphone_content_set_data(LinphoneContent *content, void *data) { - content->lcp.data = data; +void linphone_content_set_buffer(LinphoneContent *content, const void *buffer, size_t size) { + content->lcp.size = size; + content->lcp.data = belle_sip_malloc(size + 1); + memcpy(content->lcp.data, buffer, size); + ((char *)content->lcp.data)[size] = '\0'; } size_t linphone_content_get_size(const LinphoneContent *content) { @@ -156,6 +156,7 @@ void linphone_content_set_name(LinphoneContent *content, const char *name) { LinphoneContent * linphone_content_new(void) { LinphoneContent *content = belle_sip_object_new(LinphoneContent); belle_sip_object_ref(content); + content->owned_fields = TRUE; return content; } @@ -165,16 +166,15 @@ LinphoneContent * linphone_content_copy(const LinphoneContent *ref) { LinphoneContent * linphone_content_from_sal_body(const SalBody *ref) { if (ref && ref->type) { - void *objdata; LinphoneContent *content = linphone_content_new(); linphone_content_set_type(content, ref->type); linphone_content_set_subtype(content, ref->subtype); linphone_content_set_encoding(content, ref->encoding); - linphone_content_set_size(content, ref->size); - objdata = belle_sip_malloc(ref->size + 1); - memcpy(objdata, ref->data, ref->size); - ((char *)objdata)[ref->size] = '\0'; - linphone_content_set_data(content, objdata); + if (ref->data != NULL) { + linphone_content_set_buffer(content, ref->data, ref->size); + } else { + linphone_content_set_size(content, ref->size); + } return content; } return NULL; @@ -184,7 +184,7 @@ SalBody *sal_body_from_content(SalBody *body, const LinphoneContent *content) { if (content && linphone_content_get_type(content)) { body->type = linphone_content_get_type(content); body->subtype = linphone_content_get_subtype(content); - body->data = linphone_content_get_data(content); + body->data = linphone_content_get_buffer(content); body->size = linphone_content_get_size(content); body->encoding = linphone_content_get_encoding(content); return body; @@ -197,6 +197,7 @@ SalBody *sal_body_from_content(SalBody *body, const LinphoneContent *content) { LinphoneContent * linphone_content_private_to_linphone_content(const LinphoneContentPrivate *lcp) { LinphoneContent *content = belle_sip_object_new(LinphoneContent); memcpy(&content->lcp, lcp, sizeof(LinphoneContentPrivate)); + content->owned_fields = FALSE; return content; } diff --git a/coreapi/content.h b/coreapi/content.h index 24f137857..b2e04de27 100644 --- a/coreapi/content.h +++ b/coreapi/content.h @@ -150,26 +150,26 @@ LINPHONE_PUBLIC void linphone_content_set_subtype(LinphoneContent *content, cons * @param[in] content LinphoneContent object. * @return The content data buffer. */ -LINPHONE_PUBLIC void * linphone_content_get_data(const LinphoneContent *content); +LINPHONE_PUBLIC void * linphone_content_get_buffer(const LinphoneContent *content); /** * Set the content data buffer, usually a string. * @param[in] content LinphoneContent object. * @param[in] data The content data buffer. */ -LINPHONE_PUBLIC void linphone_content_set_data(LinphoneContent *content, void *data); +LINPHONE_PUBLIC void linphone_content_set_buffer(LinphoneContent *content, const void *buffer, size_t size); /** - * Get the content data size, excluding null character despite null character is always set for convenience. + * Get the content data buffer size, excluding null character despite null character is always set for convenience. * @param[in] content LinphoneContent object. - * @return The content data size. + * @return The content data buffer size. */ LINPHONE_PUBLIC size_t linphone_content_get_size(const LinphoneContent *content); /** * Set the content data size, excluding null character despite null character is always set for convenience. * @param[in] content LinphoneContent object - * @param[in] size The content data size. + * @param[in] size The content data buffer size. */ LINPHONE_PUBLIC void linphone_content_set_size(LinphoneContent *content, size_t size); diff --git a/coreapi/private.h b/coreapi/private.h index 8e1bfaf8a..808e1f57e 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -909,6 +909,7 @@ struct _LinphoneContent { belle_sip_object_t base; void *user_data; struct _LinphoneContentPrivate lcp; + bool_t owned_fields; }; BELLE_SIP_DECLARE_VPTR(LinphoneContent); diff --git a/coreapi/quality_reporting.c b/coreapi/quality_reporting.c index 1beea886a..bb1d443bf 100644 --- a/coreapi/quality_reporting.c +++ b/coreapi/quality_reporting.c @@ -331,8 +331,7 @@ static int send_report(LinphoneCall* call, reporting_session_report_t * report, append_to_buffer(&buffer, &size, &offset, "\r\n"); } - linphone_content_set_data(content, buffer); - linphone_content_set_size(content, strlen(buffer)); + linphone_content_set_buffer(content, buffer, strlen(buffer)); if (call->log->reporting.on_report_sent != NULL){ call->log->reporting.on_report_sent( diff --git a/tester/eventapi_tester.c b/tester/eventapi_tester.c index 835130f54..abd8c0a30 100644 --- a/tester/eventapi_tester.c +++ b/tester/eventapi_tester.c @@ -40,7 +40,7 @@ const char *liblinphone_tester_get_notify_content(void){ void linphone_notify_received(LinphoneCore *lc, LinphoneEvent *lev, const char *eventname, const LinphoneContent *content){ LinphoneCoreManager *mgr; CU_ASSERT_PTR_NOT_NULL_FATAL(content); - CU_ASSERT_TRUE(strcmp(notify_content,(const char*)linphone_content_get_data(content))==0); + CU_ASSERT_TRUE(strcmp(notify_content,(const char*)linphone_content_get_buffer(content))==0); mgr=get_manager(lc); mgr->stat.number_of_NotifyReceived++; } @@ -54,8 +54,7 @@ void linphone_subscription_state_change(LinphoneCore *lc, LinphoneEvent *lev, Li content = linphone_core_create_content(lc); linphone_content_set_type(content,"application"); linphone_content_set_subtype(content,"somexml2"); - linphone_content_set_data(content,belle_sip_strdup(notify_content)); - linphone_content_set_size(content,strlen(notify_content)); + linphone_content_set_buffer(content,notify_content,strlen(notify_content)); ms_message("Subscription state [%s] from [%s]",linphone_subscription_state_to_string(state),from); ms_free(from); @@ -134,8 +133,7 @@ static void subscribe_test_declined(void) { content = linphone_core_create_content(marie->lc); linphone_content_set_type(content,"application"); linphone_content_set_subtype(content,"somexml"); - linphone_content_set_data(content,belle_sip_strdup(subscribe_content)); - linphone_content_set_size(content,strlen(subscribe_content)); + linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content)); pauline->decline_subscribe=TRUE; @@ -182,8 +180,7 @@ static void subscribe_test_with_args(bool_t terminated_by_subscriber, RefreshTes content = linphone_core_create_content(marie->lc); linphone_content_set_type(content,"application"); linphone_content_set_subtype(content,"somexml"); - linphone_content_set_data(content,belle_sip_strdup(subscribe_content)); - linphone_content_set_size(content,strlen(subscribe_content)); + linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content)); lev=linphone_core_subscribe(marie->lc,pauline->identity,"dodo",expires,content); @@ -236,8 +233,7 @@ static void subscribe_test_with_args2(bool_t terminated_by_subscriber, RefreshTe content = linphone_core_create_content(marie->lc); linphone_content_set_type(content,"application"); linphone_content_set_subtype(content,"somexml"); - linphone_content_set_data(content,belle_sip_strdup(subscribe_content)); - linphone_content_set_size(content,strlen(subscribe_content)); + linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content)); lev=linphone_core_create_subscribe(marie->lc,pauline->identity,"dodo",expires); linphone_event_add_custom_header(lev,"My-Header","pouet"); @@ -315,8 +311,7 @@ static void publish_test_with_args(bool_t refresh, int expires){ content = linphone_core_create_content(marie->lc); linphone_content_set_type(content,"application"); linphone_content_set_subtype(content,"somexml"); - linphone_content_set_data(content,belle_sip_strdup(subscribe_content)); - linphone_content_set_size(content,strlen(subscribe_content)); + linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content)); lp_config_set_int(marie->lc->config,"sip","refresh_generic_publish",refresh); diff --git a/tester/flexisip_tester.c b/tester/flexisip_tester.c index f2ec9192e..8abd9b903 100644 --- a/tester/flexisip_tester.c +++ b/tester/flexisip_tester.c @@ -38,8 +38,7 @@ static void subscribe_forking(void) { content = linphone_core_create_content(marie->lc); linphone_content_set_type(content,"application"); linphone_content_set_subtype(content,"somexml"); - linphone_content_set_data(content, belle_sip_strdup(liblinphone_tester_get_subscribe_content())); - linphone_content_set_size(content, strlen(liblinphone_tester_get_subscribe_content())); + linphone_content_set_buffer(content, liblinphone_tester_get_subscribe_content(), strlen(liblinphone_tester_get_subscribe_content())); lev=linphone_core_subscribe(marie->lc,pauline->identity,"dodo",expires,content); diff --git a/tester/message_tester.c b/tester/message_tester.c index 65ad16d9d..de6b90c0d 100644 --- a/tester/message_tester.c +++ b/tester/message_tester.c @@ -836,8 +836,7 @@ static void info_message_with_args(bool_t with_content) { LinphoneContent* ct=linphone_core_create_content(marie->lc); linphone_content_set_type(ct,"application"); linphone_content_set_subtype(ct,"somexml"); - linphone_content_set_data(ct,belle_sip_strdup(info_content)); - linphone_content_set_size(ct,strlen(info_content)); + linphone_content_set_buffer(ct,info_content,strlen(info_content)); linphone_info_message_set_content(info,ct); linphone_content_unref(ct); } @@ -863,12 +862,12 @@ static void info_message_with_args(bool_t with_content) { if (with_content){ CU_ASSERT_PTR_NOT_NULL(content); if (content) { - CU_ASSERT_PTR_NOT_NULL(linphone_content_get_data(content)); + CU_ASSERT_PTR_NOT_NULL(linphone_content_get_buffer(content)); CU_ASSERT_PTR_NOT_NULL(linphone_content_get_type(content)); CU_ASSERT_PTR_NOT_NULL(linphone_content_get_subtype(content)); if (linphone_content_get_type(content)) CU_ASSERT_TRUE(strcmp(linphone_content_get_type(content),"application")==0); if (linphone_content_get_subtype(content)) CU_ASSERT_TRUE(strcmp(linphone_content_get_subtype(content),"somexml")==0); - if (linphone_content_get_data(content))CU_ASSERT_TRUE(strcmp((const char*)linphone_content_get_data(content),info_content)==0); + if (linphone_content_get_buffer(content))CU_ASSERT_TRUE(strcmp((const char*)linphone_content_get_buffer(content),info_content)==0); CU_ASSERT_EQUAL(linphone_content_get_size(content),strlen(info_content)); } } diff --git a/tester/quality_reporting_tester.c b/tester/quality_reporting_tester.c index 81bae75cf..33df384be 100644 --- a/tester/quality_reporting_tester.c +++ b/tester/quality_reporting_tester.c @@ -25,7 +25,7 @@ #define __strstr(x, y) ((x==NULL)?NULL:strstr(x,y)) void on_report_send_mandatory(const LinphoneCall *call, int stream_type, const LinphoneContent *content){ - char * body = (char *)linphone_content_get_data(content); + char * body = (char *)linphone_content_get_buffer(content); char * remote_metrics_start = __strstr(body, "RemoteMetrics:"); reporting_session_report_t * report = call->log->reporting.reports[stream_type]; MediaStream * ms; @@ -91,7 +91,7 @@ char * on_report_send_verify_metrics(const reporting_content_metrics_t *metrics, } void on_report_send_with_rtcp_xr_local(const LinphoneCall *call, int stream_type, const LinphoneContent *content){ - char * body = (char*)linphone_content_get_data(content); + char * body = (char*)linphone_content_get_buffer(content); char * remote_metrics_start = __strstr(body, "RemoteMetrics:"); reporting_session_report_t * report = call->log->reporting.reports[stream_type]; on_report_send_mandatory(call,stream_type,content); @@ -99,7 +99,7 @@ void on_report_send_with_rtcp_xr_local(const LinphoneCall *call, int stream_type CU_ASSERT_TRUE(!remote_metrics_start || on_report_send_verify_metrics(&report->local_metrics,body) < remote_metrics_start); } void on_report_send_with_rtcp_xr_remote(const LinphoneCall *call, int stream_type, const LinphoneContent *content){ - char * body = (char*)linphone_content_get_data(content); + char * body = (char*)linphone_content_get_buffer(content); reporting_session_report_t * report = call->log->reporting.reports[stream_type]; on_report_send_mandatory(call,stream_type,content); @@ -214,7 +214,7 @@ static void quality_reporting_not_sent_if_low_bandwidth() { } void on_report_send_remove_fields(const LinphoneCall *call, int stream_type, const LinphoneContent *content){ - char *body = (char*)linphone_content_get_data(content); + char *body = (char*)linphone_content_get_buffer(content); /*corrupt start of the report*/ strncpy(body, "corrupted report is corrupted", strlen("corrupted report is corrupted")); }