From 1932238af8876622f567da122fb52fb3791c9514 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Wed, 2 Oct 2013 11:46:43 +0200 Subject: [PATCH] Simplify in a lot of places using odr_strdupn --- src/ber_oct.c | 4 +--- src/http.c | 44 +++++++++++--------------------------------- src/odr_mem.c | 4 +--- src/pquery.c | 4 +--- src/solr.c | 5 ++--- src/srwutil.c | 4 +--- src/uri.c | 5 +---- src/xml_match.c | 5 +---- 8 files changed, 19 insertions(+), 56 deletions(-) diff --git a/src/ber_oct.c b/src/ber_oct.c index 8c60fc7..8e8bb76 100644 --- a/src/ber_oct.c +++ b/src/ber_oct.c @@ -53,9 +53,7 @@ int ber_octetstring(ODR o, Odr_oct *p, int cons) return 0; } p->len = len; - p->buf = odr_malloc(o, len + 1); - memcpy(p->buf, o->op->bp, len); - p->buf[len] = '\0'; + p->buf = odr_strdupn(o, o->op->bp, len); o->op->bp += len; return 1; case ODR_ENCODE: diff --git a/src/http.c b/src/http.c index af51c05..378d145 100644 --- a/src/http.c +++ b/src/http.c @@ -48,19 +48,14 @@ static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers, break; } *headers = (Z_HTTP_Header *) odr_malloc(o, sizeof(**headers)); - (*headers)->name = (char*) odr_malloc(o, i - po + 1); - memcpy ((*headers)->name, buf + po, i - po); - (*headers)->name[i - po] = '\0'; + (*headers)->name = odr_strdupn(o, buf + po, i - po); i++; while (i < size-1 && buf[i] == ' ') i++; for (po = i; i < size-1 && !strchr("\r\n", buf[i]); i++) ; - (*headers)->value = (char*) odr_malloc(o, i - po + 1); - memcpy ((*headers)->value, buf + po, i - po); - (*headers)->value[i - po] = '\0'; - + (*headers)->value = odr_strdupn(o, buf + po, i - po); if (!yaz_strcasecmp((*headers)->name, "Transfer-Encoding") && !yaz_strcasecmp((*headers)->value, "chunked")) @@ -142,9 +137,7 @@ static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers, else { *content_len = size - i; - *content_buf = (char*) odr_malloc(o, *content_len + 1); - memcpy(*content_buf, buf + i, *content_len); - (*content_buf)[*content_len] = '\0'; + *content_buf = odr_strdupn(o, buf + i, *content_len); } } return 1; @@ -287,11 +280,8 @@ Z_GDU *z_get_HTTP_Request_host_path(ODR odr, if (cp0 && cp1) { - char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1); - memcpy (h, cp0, cp1 - cp0); - h[cp1-cp0] = '\0'; - z_HTTP_header_add(odr, &p->u.HTTP_Request->headers, - "Host", h); + char *h = odr_strdupn(odr, cp0, cp1 - cp0); + z_HTTP_header_add(odr, &p->u.HTTP_Request->headers, "Host", h); } } return p; @@ -314,11 +304,8 @@ Z_GDU *z_get_HTTP_Request_uri(ODR odr, const char *uri, const char *args, if (cp0 && cp1) { - char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1); - memcpy (h, cp0, cp1 - cp0); - h[cp1-cp0] = '\0'; - z_HTTP_header_add(odr, &p->u.HTTP_Request->headers, - "Host", h); + char *h = odr_strdupn(odr, cp0, cp1 - cp0); + z_HTTP_header_add(odr, &p->u.HTTP_Request->headers, "Host", h); } if (!args) @@ -495,10 +482,7 @@ int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p) po = i = 5; while (i < size-2 && !strchr(" \r\n", buf[i])) i++; - hr->version = (char *) odr_malloc(o, i - po + 1); - if (i - po) - memcpy(hr->version, buf + po, i - po); - hr->version[i-po] = 0; + hr->version = odr_strdupn(o, buf + po, i - po); if (buf[i] != ' ') { o->error = OHTTP; @@ -533,9 +517,7 @@ int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p) o->error = OHTTP; return 0; } - hr->method = (char *) odr_malloc(o, i+1); - memcpy (hr->method, buf, i); - hr->method[i] = '\0'; + hr->method = odr_strdupn(o, buf, i); /* path */ po = i+1; for (i = po; buf[i] != ' '; i++) @@ -544,9 +526,7 @@ int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p) o->error = OHTTP; return 0; } - hr->path = (char *) odr_malloc(o, i - po + 1); - memcpy (hr->path, buf+po, i - po); - hr->path[i - po] = '\0'; + hr->path = odr_strdupn(o, buf + po, i - po); /* HTTP version */ i++; if (i > size-5 || memcmp(buf+i, "HTTP/", 5)) @@ -558,9 +538,7 @@ int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p) po = i; while (i < size && !strchr("\r\n", buf[i])) i++; - hr->version = (char *) odr_malloc(o, i - po + 1); - memcpy(hr->version, buf + po, i - po); - hr->version[i - po] = '\0'; + hr->version = odr_strdupn(o, buf + po, i - po); /* headers */ if (i < size-1 && buf[i] == '\r') i++; diff --git a/src/odr_mem.c b/src/odr_mem.c index 690477d..5ff9efd 100644 --- a/src/odr_mem.c +++ b/src/odr_mem.c @@ -65,9 +65,7 @@ size_t odr_total(ODR o) Odr_oct *odr_create_Odr_oct(ODR o, const char *buf, int sz) { Odr_oct *p = (Odr_oct *) odr_malloc(o, sizeof(Odr_oct)); - p->buf = (char *) odr_malloc(o, sz + 1); - memcpy(p->buf, buf, sz); - p->buf[sz] = '\0'; + p->buf = odr_strdupn(o, buf, sz); p->len = sz; return p; } diff --git a/src/pquery.c b/src/pquery.c index 55eb1d3..22695bd 100644 --- a/src/pquery.c +++ b/src/pquery.c @@ -386,9 +386,7 @@ static Z_Operand *rpn_simple(struct yaz_pqf_parser *li, ODR o, return 0; } zo->which = Z_Operand_resultSetId; - zo->u.resultSetId = (char *)odr_malloc(o, li->lex_len+1); - memcpy(zo->u.resultSetId, li->lex_buf, li->lex_len); - zo->u.resultSetId[li->lex_len] = '\0'; + zo->u.resultSetId = odr_strdupn(o, li->lex_buf, li->lex_len); lex(li); break; default: diff --git a/src/solr.c b/src/solr.c index 9897bb1..73950b4 100644 --- a/src/solr.c +++ b/src/solr.c @@ -87,9 +87,8 @@ static void yaz_solr_decode_result_docs(ODR o, xmlNodePtr ptr, record->recordSchema = 0; record->recordPacking = Z_SRW_recordPacking_XML; record->recordData_len = buf->use; - record->recordData_buf = odr_malloc(o, buf->use + 1); - memcpy(record->recordData_buf, buf->content, buf->use); - record->recordData_buf[buf->use] = '\0'; + record->recordData_buf = + odr_strdupn(o, (const char *) buf->content, buf->use); record->recordPosition = odr_intdup(o, start + offset + 1); xmlBufferFree(buf); diff --git a/src/srwutil.c b/src/srwutil.c index 83172d5..ae8ecae 100644 --- a/src/srwutil.c +++ b/src/srwutil.c @@ -246,9 +246,7 @@ static void grab_charset(ODR o, const char *content_type, char **charset) while (i < 20 && charset_p[i] && !strchr("; \n\r", charset_p[i])) i++; - *charset = (char*) odr_malloc(o, i+1); - memcpy(*charset, charset_p, i); - (*charset)[i] = '\0'; + *charset = odr_strdupn(o, charset_p, i); } } } diff --git a/src/uri.c b/src/uri.c index 33120ed..fae1411 100644 --- a/src/uri.c +++ b/src/uri.c @@ -153,10 +153,7 @@ int yaz_uri_to_array(const char *path, ODR o, char ***name, char ***val) for (cp = path; *cp && *cp != '=' && *cp != '&'; cp++) ; - (*name)[no] = (char *) odr_malloc(o, (cp-path)+1); - memcpy((*name)[no], path, cp-path); - (*name)[no][cp-path] = '\0'; - + (*name)[no] = odr_strdupn(o, path, cp - path); path = cp; if (*path == '=') { diff --git a/src/xml_match.c b/src/xml_match.c index dd804db..b36e2f7 100644 --- a/src/xml_match.c +++ b/src/xml_match.c @@ -107,10 +107,7 @@ int yaz_match_xsd_XML_n2(xmlNodePtr ptr, const char *elem, ODR o, xmlBufferAddHead(buf, (const xmlChar *) "", -1); xmlBufferAdd(buf, (const xmlChar *) "", -1); } - *val = (char *) odr_malloc(o, buf->use + 1); - memcpy(*val, buf->content, buf->use); - (*val)[buf->use] = '\0'; - + *val = odr_strdupn(o, buf->content, buf->use); if (len) *len = buf->use; -- 1.7.10.4