httpd-modules-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ben Noordhuis <i...@bnoordhuis.nl>
Subject Re: Converting a 16-bit string to 8-bit?
Date Fri, 04 Mar 2011 11:01:21 GMT
On Fri, Mar 4, 2011 at 02:24, Adelle Hartley <adelle@akemi.com.au> wrote:
> This is a helper class I wrote for the module I'm working on.  It assumes
> the native wide encoding is UTF-32.  To make it cross platform, you'd have
> to check what the correct wide encoding is.
>
> This is my first apache module, so any corrections welcome.
>
> class our_response_t
> {
> protected:
>        request_rec* m;
>        apr_xlate_t* m_convset;
>        char m_bufferBytes[CHARSET_CONVERSION_BUFFER_SIZE];
> public:
>        our_response_t(request_rec* request) : m(request), m_convset(NULL),
> m_html(NULL), m_json(NULL)
>        {
>                apr_pool_t* pool = m->pool;
>                apr_status_t status = apr_xlate_open(&m_convset, "UTF-8",
> "UTF-32", pool);
>
>                if (m_convset)
>                {
>                        ap_set_content_type(m, "text/html;charset=UTF-8");
>                }
>        }
>
>        ~our_response_t()
>        {
>                if (m_convset)
>                {
>                        apr_xlate_close(m_convset);
>                }
>        }
>
>        void append_chars(const wchar_t* str, size_t num_chars)
>        {
>                apr_size_t inbytes_left = num_chars*sizeof(wchar_t);
>                apr_size_t outbytes_left = CHARSET_CONVERSION_BUFFER_SIZE-1;
>                apr_status_t status = apr_xlate_conv_buffer(m_convset, (const
> char*)str, &inbytes_left, m_bufferBytes, &outbytes_left);
>                m_bufferBytes[CHARSET_CONVERSION_BUFFER_SIZE-outbytes_left-1]
> = 0;
>                ap_rputs(m_bufferBytes, m);
>        }
>
> };

Adelle, your code doesn't appear to be handling errors. A number of
things can go wrong here:

1. The conversion may not be supported.

2. Partial character sequences (not an issue here since the input is
UTF-32 but I mention it for posterity's sake), reported as
APR_EINCOMPLETE.

3. Illegal character sequences, reported as APR_EINVAL.

4. Output buffer too short. Reported as APR_SUCCESS but with inbytes_left > 0.

Mime
View raw message