#include "galias.h"
+/* We try to terminate strings in unknown charsets with this many zero bytes
+ * to ensure that multibyte strings really are nul-terminated when we return
+ * them from g_convert() and friends.
+ */
+#define NUL_TERMINATOR_LENGTH 4
+
GQuark
g_convert_error_quark (void)
{
* </footnote>.
*
* Return value: If the conversion was successful, a newly allocated
- * nul-terminated<footnote id="nul-returns">
- * <para>
- * Nul-terminated return values from conversion functions
- * are terminated by a single 0 byte only. This means that
- * for multibyte character sets like UTF-16, they must be
- * treated as not nul-terminated.
- * </para>
- * </footnote>
- * string, which must be freed with g_free(). Otherwise %NULL
- * and @error will be set.
+ * nul-terminated string, which must be freed with
+ * g_free(). Otherwise %NULL and @error will be set.
**/
gchar*
g_convert_with_iconv (const gchar *str,
p = str;
inbytes_remaining = len;
- outbuf_size = len + 1; /* + 1 for nul in case len == 1 */
+ outbuf_size = len + NUL_TERMINATOR_LENGTH;
- outbytes_remaining = outbuf_size - 1; /* -1 for nul */
+ outbytes_remaining = outbuf_size - NUL_TERMINATOR_LENGTH;
outp = dest = g_malloc (outbuf_size);
while (!done && !have_error)
dest = g_realloc (dest, outbuf_size);
outp = dest + used;
- outbytes_remaining = outbuf_size - used - 1; /* -1 for nul */
+ outbytes_remaining = outbuf_size - used - NUL_TERMINATOR_LENGTH;
}
break;
case EILSEQ:
}
}
- *outp = '\0';
+ memset (outp, 0, NUL_TERMINATOR_LENGTH);
if (bytes_read)
*bytes_read = p - str;
* conversions<footnoteref linkend="streaming-state"/>.
*
* Return value: If the conversion was successful, a newly allocated
- * nul-terminated<footnoteref linkend="nul-returns"/> string,
- * which must be freed with g_free(). Otherwise %NULL and
- * @error will be set.
+ * nul-terminated string, which must be freed with
+ * g_free(). Otherwise %NULL and @error will be set.
**/
gchar*
g_convert (const gchar *str,
* conversions<footnoteref linkend="streaming-state"/>.
*
* Return value: If the conversion was successful, a newly allocated
- * nul-terminated<footnoteref linkend="nul-returns"/> string,
- * which must be freed with g_free(). Otherwise %NULL and
- * @error will be set.
+ * nul-terminated string, which must be freed with
+ * g_free(). Otherwise %NULL and @error will be set.
**/
gchar*
g_convert_with_fallback (const gchar *str,
*/
p = utf8;
- outbuf_size = len + 1; /* + 1 for nul in case len == 1 */
- outbytes_remaining = outbuf_size - 1; /* -1 for nul */
+ outbuf_size = len + NUL_TERMINATOR_LENGTH;
+ outbytes_remaining = outbuf_size - NUL_TERMINATOR_LENGTH;
outp = dest = g_malloc (outbuf_size);
while (!done && !have_error)
dest = g_realloc (dest, outbuf_size);
outp = dest + used;
- outbytes_remaining = outbuf_size - used - 1; /* -1 for nul */
+ outbytes_remaining = outbuf_size - used - NUL_TERMINATOR_LENGTH;
break;
}
/* Cleanup
*/
- *outp = '\0';
+ memset (outp, 0, NUL_TERMINATOR_LENGTH);
close_converter (cd);