libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

commit a1f2f478e5f939c574cc05460030336761177dd9
parent 4f6615b7dbdb19e2c7a7075bc2060030508332a6
Author: Christian Grothoff <christian@grothoff.org>
Date:   Sat,  4 Aug 2012 21:46:29 +0000

fixing issues with non 0-termianted strings

Diffstat:
Msrc/main/extract.c | 12++++++++----
Msrc/main/extractor_print.c | 7++++++-
Msrc/main/iconv.c | 46++++++++++++++++++++++++++++------------------
3 files changed, 42 insertions(+), 23 deletions(-)

diff --git a/src/main/extract.c b/src/main/extract.c @@ -108,6 +108,7 @@ struct Help */ #define BORDER 29 + /** * Display help text (--help). * @@ -296,7 +297,8 @@ print_selected_keywords (void *cls, cd = iconv_open (nl_langinfo(CODESET), "UTF-8"); if (((iconv_t) -1) != cd) keyword = iconv_helper (cd, - data); + data, + data_len); else keyword = strdup (data); if (NULL != keyword) @@ -318,8 +320,9 @@ print_selected_keywords (void *cls, break; case EXTRACTOR_METAFORMAT_C_STRING: fprintf (stdout, - "%s - %s\n", + "%s - %.*s\n", stype, + (int) data_len, data); break; default: @@ -372,10 +375,11 @@ print_selected_keywords_grep_friendly (void *cls, fprintf (stdout, "%s: ", gettext(mt)); - cd = iconv_open (nl_langinfo(CODESET), "UTF-8"); + cd = iconv_open (nl_langinfo (CODESET), "UTF-8"); if (((iconv_t) -1) != cd) keyword = iconv_helper (cd, - data); + data, + data_len); else keyword = strdup (data); if (NULL != keyword) diff --git a/src/main/extractor_print.c b/src/main/extractor_print.c @@ -17,6 +17,11 @@ Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/** + * @file main/extractor_print.c + * @brief convenience functions for printing meta data + * @author Christian Grothoff + */ #include "platform.h" #include "extractor.h" @@ -62,7 +67,7 @@ EXTRACTOR_meta_data_print (void *handle, LOG_STRERROR ("iconv_open"); return 1; } - buf = iconv_helper (cd, data); + buf = iconv_helper (cd, data, data_len); if (NULL == buf) { LOG_STRERROR ("iconv_helper"); diff --git a/src/main/iconv.c b/src/main/iconv.c @@ -19,43 +19,53 @@ */ /** + * @file main/iconv.c + * @brief convenience functions for character conversion + * @author Christian Grothoff + */ + +/** * Convert the given input using the given converter * and return as a 0-terminated string. + * + * @param cd converter to use + * @param in input string + * @param inSize number of bytes in 'in' + * @return NULL on error, otherwise the converted string (to be free'd by caller) */ static char * -iconv_helper(iconv_t cd, - const char * in) +iconv_helper (iconv_t cd, + const char *in, + size_t inSize) { - size_t inSize; char * buf; char * ibuf; const char * i; size_t outSize; size_t outLeft; - i = in; - /* reset iconv */ - iconv(cd, NULL, NULL, NULL, NULL); - - inSize = strlen(in); if (inSize > 1024 * 1024) return NULL; /* too big to be meta data */ + i = in; + /* reset iconv */ + iconv (cd, NULL, NULL, NULL, NULL); outSize = 4 * inSize + 2; outLeft = outSize - 2; /* make sure we have 2 0-terminations! */ - buf = malloc(outSize); - if (buf == NULL) + if (NULL == (buf = malloc (outSize))) return NULL; ibuf = buf; - memset(buf, 0, outSize); - if (iconv(cd, - (char**) &in, - &inSize, - &ibuf, - &outLeft) == SIZE_MAX) + memset (buf, 0, outSize); + if (iconv (cd, + (char**) &in, + &inSize, + &ibuf, + &outLeft) == SIZE_MAX) { /* conversion failed */ - free(buf); - return strdup(i); + free (buf); + return strdup (i); } return buf; } + +/* end of iconv.c */