From dff0dd0c63e530e21471531299a8f260cf88f001 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Jun 2008 15:58:06 -0400 Subject: Allow NULL output in _cairo_utf8_to_ucs4() The function can be used to validate UTF-8 text now. --- src/cairo-unicode.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'src/cairo-unicode.c') diff --git a/src/cairo-unicode.c b/src/cairo-unicode.c index caa4727d..c4873d5c 100644 --- a/src/cairo-unicode.c +++ b/src/cairo-unicode.c @@ -202,7 +202,7 @@ _utf8_get_char_extended (const unsigned char *p, * If @len is supplied and the string has an embedded nul * byte, only the portion before the nul byte is converted. * @result: location to store a pointer to a newly allocated UTF-32 - * string (always native endian). Free with free(). A 0 + * string (always native endian), or %NULL. Free with free(). A 0 * word will be written after the last character. * @items_written: location to store number of 32-bit words * written. (Not including the trailing 0) @@ -241,18 +241,21 @@ _cairo_utf8_to_ucs4 (const char *str, in = UTF8_NEXT_CHAR (in); } - str32 = _cairo_malloc_ab (n_chars + 1, sizeof (uint32_t)); - if (!str32) - return _cairo_error (CAIRO_STATUS_NO_MEMORY); + if (result) { + str32 = _cairo_malloc_ab (n_chars + 1, sizeof (uint32_t)); + if (!str32) + return _cairo_error (CAIRO_STATUS_NO_MEMORY); - in = ustr; - for (i=0; i < n_chars; i++) { - str32[i] = _utf8_get_char (in); - in = UTF8_NEXT_CHAR (in); + in = ustr; + for (i=0; i < n_chars; i++) { + str32[i] = _utf8_get_char (in); + in = UTF8_NEXT_CHAR (in); + } + str32[i] = 0; + + *result = str32; } - str32[i] = 0; - *result = str32; if (items_written) *items_written = n_chars; -- cgit v1.2.3