| 2180 | } |
| 2181 | |
| 2182 | PUGI_IMPL_FN bool convert_buffer(char_t*& out_buffer, size_t& out_length, xml_encoding encoding, const void* contents, size_t size, bool is_mutable) |
| 2183 | { |
| 2184 | // get native encoding |
| 2185 | xml_encoding wchar_encoding = get_wchar_encoding(); |
| 2186 | |
| 2187 | // fast path: no conversion required |
| 2188 | if (encoding == wchar_encoding) |
| 2189 | return get_mutable_buffer(out_buffer, out_length, contents, size, is_mutable); |
| 2190 | |
| 2191 | // only endian-swapping is required |
| 2192 | if (need_endian_swap_utf(encoding, wchar_encoding)) |
| 2193 | return convert_buffer_endian_swap(out_buffer, out_length, contents, size, is_mutable); |
| 2194 | |
| 2195 | // source encoding is utf8 |
| 2196 | if (encoding == encoding_utf8) |
| 2197 | return convert_buffer_generic(out_buffer, out_length, contents, size, utf8_decoder()); |
| 2198 | |
| 2199 | // source encoding is utf16 |
| 2200 | if (encoding == encoding_utf16_be || encoding == encoding_utf16_le) |
| 2201 | { |
| 2202 | xml_encoding native_encoding = is_little_endian() ? encoding_utf16_le : encoding_utf16_be; |
| 2203 | |
| 2204 | return (native_encoding == encoding) ? |
| 2205 | convert_buffer_generic(out_buffer, out_length, contents, size, utf16_decoder<opt_false>()) : |
| 2206 | convert_buffer_generic(out_buffer, out_length, contents, size, utf16_decoder<opt_true>()); |
| 2207 | } |
| 2208 | |
| 2209 | // source encoding is utf32 |
| 2210 | if (encoding == encoding_utf32_be || encoding == encoding_utf32_le) |
| 2211 | { |
| 2212 | xml_encoding native_encoding = is_little_endian() ? encoding_utf32_le : encoding_utf32_be; |
| 2213 | |
| 2214 | return (native_encoding == encoding) ? |
| 2215 | convert_buffer_generic(out_buffer, out_length, contents, size, utf32_decoder<opt_false>()) : |
| 2216 | convert_buffer_generic(out_buffer, out_length, contents, size, utf32_decoder<opt_true>()); |
| 2217 | } |
| 2218 | |
| 2219 | // source encoding is latin1 |
| 2220 | if (encoding == encoding_latin1) |
| 2221 | return convert_buffer_generic(out_buffer, out_length, contents, size, latin1_decoder()); |
| 2222 | |
| 2223 | assert(false && "Invalid encoding"); // unreachable |
| 2224 | return false; |
| 2225 | } |
| 2226 | #else |
| 2227 | template <typename D> PUGI_IMPL_FN bool convert_buffer_generic(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, D) |
| 2228 | { |
no test coverage detected