-/* Returns:
- >= 0 - successfull, 0 means conversion doesn't use multibyte sequences
- -1 - error during iconv_open call
- -2 - error during iconv_close call
- ----------------------------------
- This function expects that multibyte encoding in 'charset' wouldn't have
- characters with more than 3 bytes. It is not intended to convert UTF-8 because
- we'll never receive UTF-8 in our handler (it is handled by Exat itself).
-*/
-static int
-fill_encoding_info (const char *charset, XML_Encoding * info)
-{
- iconv_t cd = (iconv_t)(-1);
- int flag;
- CtdlLogPrintf(0, "RSS: fill encoding info ...\n");
-
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
- cd = iconv_open ("UCS-2LE", charset);
-#else
- cd = iconv_open ("UCS-2BE", charset);
-#endif
-
- if (cd == (iconv_t) (-1))
- {
- return -1;
- }
-
- {
- unsigned short out;
- unsigned char buf[4];
- unsigned int i0, i1, i2;
- int result;
- flag = 0;
- for (i0 = 0; i0 < 0x100; i0++)
- {
- buf[0] = i0;
- info->map[i0] = 0;
- //result = try (cd, buf, 1, &out);
- if (result < 0)
- {
- }
- else if (result > 0)
- {
- info->map[i0] = out;
- }
- else
- {
- for (i1 = 0; i1 < 0x100; i1++)
- {
- buf[1] = i1;
- ///result = try (cd, buf, 2, &out);
- if (result < 0)
- {
- }
- else if (result > 0)
- {
- flag++;
- info->map[i0] = -2;
- }
- else
- {
- for (i2 = 0; i2 < 0x100; i2++)
- {
- buf[2] = i2;
- ////result = try (cd, buf, 3, &out);
- if (result < 0)
- {
- }
- else if (result > 0)
- {
- flag++;
- info->map[i0] = -3;
- }
- }
- }
- }
- }
- }
- }
-
- if (iconv_close (cd) < 0)
- {
- return -2;
- }
- return flag;
-}
-
-static int
-iconv_convertor (void *data, const char *s)
-{
- XML_Encoding *info = data;
- int res;
- CtdlLogPrintf(0, "RSS: Converting ...\n");
-
- if (s == NULL)
- return -1;
-/*
- GByteArray *result;
- result = g_byte_array_new ();
- if (process_block (info->data, (char *) s, strlen (s), result) == 0)
- {
- res = *(result->data);
- g_byte_array_free (result, TRUE);
- return res;
- }
- g_byte_array_free (result, TRUE);
-*/
- return -1;
-}
-
-static void
-my_release (void *data)
-{
- iconv_t cd = (iconv_t) data;
- if (iconv_close (cd) != 0)
- {
-/// TODO: uh no. exit (1);
- }
-}
-int
-handle_unknown_xml_encoding (void *encodingHandleData,
- const XML_Char * name,
- XML_Encoding * info)
-{
- int result;
- CtdlLogPrintf(0, "RSS: unknown encoding ...\n");
- result = fill_encoding_info (name, info);
- if (result >= 0)
- {
- /*
- Special case: client asked for reverse conversion, we'll provide him with
- iconv descriptor which handles it. Client should release it by himself.
- */
- if(encodingHandleData != NULL)
- *((iconv_t *)encodingHandleData) = iconv_open(name, "UTF-8");
- /*
- Optimization: we do not need conversion function if encoding is one-to-one,
- info->map table will be enough
- */
- if (result == 0)
- {
- info->data = NULL;
- info->convert = NULL;
- info->release = NULL;
- return 1;
- }
- /*
- We do need conversion function because this encoding uses multibyte sequences
- */
- info->data = (void *) iconv_open ("UTF-8", name);
- if ((int)info->data == -1)
- return -1;
- info->convert = iconv_convertor;
- info->release = my_release;
- return 1;
- }
- if(encodingHandleData != NULL)
- *(iconv_t *)encodingHandleData = NULL;
- return 0;
-}
-
-#endif
-#endif