1*404b540aSrobert // Locale support (codecvt) -*- C++ -*-
2*404b540aSrobert
3*404b540aSrobert // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction. Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License. This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert
31*404b540aSrobert //
32*404b540aSrobert // ISO C++ 14882: 22.2.1.5 Template class codecvt
33*404b540aSrobert //
34*404b540aSrobert
35*404b540aSrobert // Written by Benjamin Kosnik <bkoz@redhat.com>
36*404b540aSrobert
37*404b540aSrobert /** @file ext/codecvt_specializations.h
38*404b540aSrobert * This file is a GNU extension to the Standard C++ Library.
39*404b540aSrobert */
40*404b540aSrobert
41*404b540aSrobert #ifndef _EXT_CODECVT_SPECIALIZATIONS_H
42*404b540aSrobert #define _EXT_CODECVT_SPECIALIZATIONS_H 1
43*404b540aSrobert
44*404b540aSrobert #include <bits/c++config.h>
45*404b540aSrobert
46*404b540aSrobert #ifdef _GLIBCXX_USE_ICONV
47*404b540aSrobert
48*404b540aSrobert #include <locale>
49*404b540aSrobert #include <iconv.h>
50*404b540aSrobert
51*404b540aSrobert // XXX
52*404b540aSrobert // Define this here so codecvt.cc can have _S_max_size definition.
53*404b540aSrobert #define _GLIBCXX_USE_ENCODING_STATE 1
54*404b540aSrobert
_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)55*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
56*404b540aSrobert
57*404b540aSrobert /// @brief Extension to use icov for dealing with character encodings.
58*404b540aSrobert // This includes conversions and comparisons between various character
59*404b540aSrobert // sets. This object encapsulates data that may need to be shared between
60*404b540aSrobert // char_traits, codecvt and ctype.
61*404b540aSrobert class encoding_state
62*404b540aSrobert {
63*404b540aSrobert public:
64*404b540aSrobert // Types:
65*404b540aSrobert // NB: A conversion descriptor subsumes and enhances the
66*404b540aSrobert // functionality of a simple state type such as mbstate_t.
67*404b540aSrobert typedef iconv_t descriptor_type;
68*404b540aSrobert
69*404b540aSrobert protected:
70*404b540aSrobert // Name of internal character set encoding.
71*404b540aSrobert std::string _M_int_enc;
72*404b540aSrobert
73*404b540aSrobert // Name of external character set encoding.
74*404b540aSrobert std::string _M_ext_enc;
75*404b540aSrobert
76*404b540aSrobert // Conversion descriptor between external encoding to internal encoding.
77*404b540aSrobert descriptor_type _M_in_desc;
78*404b540aSrobert
79*404b540aSrobert // Conversion descriptor between internal encoding to external encoding.
80*404b540aSrobert descriptor_type _M_out_desc;
81*404b540aSrobert
82*404b540aSrobert // The byte-order marker for the external encoding, if necessary.
83*404b540aSrobert int _M_ext_bom;
84*404b540aSrobert
85*404b540aSrobert // The byte-order marker for the internal encoding, if necessary.
86*404b540aSrobert int _M_int_bom;
87*404b540aSrobert
88*404b540aSrobert // Number of external bytes needed to construct one complete
89*404b540aSrobert // character in the internal encoding.
90*404b540aSrobert // NB: -1 indicates variable, or stateful, encodings.
91*404b540aSrobert int _M_bytes;
92*404b540aSrobert
93*404b540aSrobert public:
94*404b540aSrobert explicit
95*404b540aSrobert encoding_state()
96*404b540aSrobert : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(0), _M_int_bom(0), _M_bytes(0)
97*404b540aSrobert { }
98*404b540aSrobert
99*404b540aSrobert explicit
100*404b540aSrobert encoding_state(const char* __int, const char* __ext,
101*404b540aSrobert int __ibom = 0, int __ebom = 0, int __bytes = 1)
102*404b540aSrobert : _M_int_enc(__int), _M_ext_enc(__ext), _M_in_desc(0), _M_out_desc(0),
103*404b540aSrobert _M_ext_bom(__ebom), _M_int_bom(__ibom), _M_bytes(__bytes)
104*404b540aSrobert { init(); }
105*404b540aSrobert
106*404b540aSrobert // 21.1.2 traits typedefs
107*404b540aSrobert // p4
108*404b540aSrobert // typedef STATE_T state_type
109*404b540aSrobert // requires: state_type shall meet the requirements of
110*404b540aSrobert // CopyConstructible types (20.1.3)
111*404b540aSrobert // NB: This does not preseve the actual state of the conversion
112*404b540aSrobert // descriptor member, but it does duplicate the encoding
113*404b540aSrobert // information.
114*404b540aSrobert encoding_state(const encoding_state& __obj) : _M_in_desc(0), _M_out_desc(0)
115*404b540aSrobert { construct(__obj); }
116*404b540aSrobert
117*404b540aSrobert // Need assignment operator as well.
118*404b540aSrobert encoding_state&
119*404b540aSrobert operator=(const encoding_state& __obj)
120*404b540aSrobert {
121*404b540aSrobert construct(__obj);
122*404b540aSrobert return *this;
123*404b540aSrobert }
124*404b540aSrobert
125*404b540aSrobert ~encoding_state()
126*404b540aSrobert { destroy(); }
127*404b540aSrobert
128*404b540aSrobert bool
129*404b540aSrobert good() const throw()
130*404b540aSrobert {
131*404b540aSrobert const descriptor_type __err = reinterpret_cast<iconv_t>(-1);
132*404b540aSrobert bool __test = _M_in_desc && _M_in_desc != __err;
133*404b540aSrobert __test &= _M_out_desc && _M_out_desc != __err;
134*404b540aSrobert return __test;
135*404b540aSrobert }
136*404b540aSrobert
137*404b540aSrobert int
138*404b540aSrobert character_ratio() const
139*404b540aSrobert { return _M_bytes; }
140*404b540aSrobert
141*404b540aSrobert const std::string
142*404b540aSrobert internal_encoding() const
143*404b540aSrobert { return _M_int_enc; }
144*404b540aSrobert
145*404b540aSrobert int
146*404b540aSrobert internal_bom() const
147*404b540aSrobert { return _M_int_bom; }
148*404b540aSrobert
149*404b540aSrobert const std::string
150*404b540aSrobert external_encoding() const
151*404b540aSrobert { return _M_ext_enc; }
152*404b540aSrobert
153*404b540aSrobert int
154*404b540aSrobert external_bom() const
155*404b540aSrobert { return _M_ext_bom; }
156*404b540aSrobert
157*404b540aSrobert const descriptor_type&
158*404b540aSrobert in_descriptor() const
159*404b540aSrobert { return _M_in_desc; }
160*404b540aSrobert
161*404b540aSrobert const descriptor_type&
162*404b540aSrobert out_descriptor() const
163*404b540aSrobert { return _M_out_desc; }
164*404b540aSrobert
165*404b540aSrobert protected:
166*404b540aSrobert void
167*404b540aSrobert init()
168*404b540aSrobert {
169*404b540aSrobert const descriptor_type __err = reinterpret_cast<iconv_t>(-1);
170*404b540aSrobert const bool __have_encodings = _M_int_enc.size() && _M_ext_enc.size();
171*404b540aSrobert if (!_M_in_desc && __have_encodings)
172*404b540aSrobert {
173*404b540aSrobert _M_in_desc = iconv_open(_M_int_enc.c_str(), _M_ext_enc.c_str());
174*404b540aSrobert if (_M_in_desc == __err)
175*404b540aSrobert std::__throw_runtime_error(__N("encoding_state::_M_init "
176*404b540aSrobert "creating iconv input descriptor failed"));
177*404b540aSrobert }
178*404b540aSrobert if (!_M_out_desc && __have_encodings)
179*404b540aSrobert {
180*404b540aSrobert _M_out_desc = iconv_open(_M_ext_enc.c_str(), _M_int_enc.c_str());
181*404b540aSrobert if (_M_out_desc == __err)
182*404b540aSrobert std::__throw_runtime_error(__N("encoding_state::_M_init "
183*404b540aSrobert "creating iconv output descriptor failed"));
184*404b540aSrobert }
185*404b540aSrobert }
186*404b540aSrobert
187*404b540aSrobert void
188*404b540aSrobert construct(const encoding_state& __obj)
189*404b540aSrobert {
190*404b540aSrobert destroy();
191*404b540aSrobert _M_int_enc = __obj._M_int_enc;
192*404b540aSrobert _M_ext_enc = __obj._M_ext_enc;
193*404b540aSrobert _M_ext_bom = __obj._M_ext_bom;
194*404b540aSrobert _M_int_bom = __obj._M_int_bom;
195*404b540aSrobert _M_bytes = __obj._M_bytes;
196*404b540aSrobert init();
197*404b540aSrobert }
198*404b540aSrobert
199*404b540aSrobert void
200*404b540aSrobert destroy() throw()
201*404b540aSrobert {
202*404b540aSrobert const descriptor_type __err = reinterpret_cast<iconv_t>(-1);
203*404b540aSrobert if (_M_in_desc && _M_in_desc != __err)
204*404b540aSrobert {
205*404b540aSrobert iconv_close(_M_in_desc);
206*404b540aSrobert _M_in_desc = 0;
207*404b540aSrobert }
208*404b540aSrobert if (_M_out_desc && _M_out_desc != __err)
209*404b540aSrobert {
210*404b540aSrobert iconv_close(_M_out_desc);
211*404b540aSrobert _M_out_desc = 0;
212*404b540aSrobert }
213*404b540aSrobert }
214*404b540aSrobert };
215*404b540aSrobert
216*404b540aSrobert /// @brief encoding_char_traits.
217*404b540aSrobert // Custom traits type with encoding_state for the state type, and the
218*404b540aSrobert // associated fpos<encoding_state> for the position type, all other
219*404b540aSrobert // bits equivalent to the required char_traits instantiations.
220*404b540aSrobert template<typename _CharT>
221*404b540aSrobert struct encoding_char_traits : public std::char_traits<_CharT>
222*404b540aSrobert {
223*404b540aSrobert typedef encoding_state state_type;
224*404b540aSrobert typedef typename std::fpos<state_type> pos_type;
225*404b540aSrobert };
226*404b540aSrobert
227*404b540aSrobert _GLIBCXX_END_NAMESPACE
228*404b540aSrobert
229*404b540aSrobert
230*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
231*404b540aSrobert
232*404b540aSrobert using __gnu_cxx::encoding_state;
233*404b540aSrobert
234*404b540aSrobert /// @brief codecvt<InternT, _ExternT, encoding_state> specialization.
235*404b540aSrobert // This partial specialization takes advantage of iconv to provide
236*404b540aSrobert // code conversions between a large number of character encodings.
237*404b540aSrobert template<typename _InternT, typename _ExternT>
238*404b540aSrobert class codecvt<_InternT, _ExternT, encoding_state>
239*404b540aSrobert : public __codecvt_abstract_base<_InternT, _ExternT, encoding_state>
240*404b540aSrobert {
241*404b540aSrobert public:
242*404b540aSrobert // Types:
243*404b540aSrobert typedef codecvt_base::result result;
244*404b540aSrobert typedef _InternT intern_type;
245*404b540aSrobert typedef _ExternT extern_type;
246*404b540aSrobert typedef __gnu_cxx::encoding_state state_type;
247*404b540aSrobert typedef state_type::descriptor_type descriptor_type;
248*404b540aSrobert
249*404b540aSrobert // Data Members:
250*404b540aSrobert static locale::id id;
251*404b540aSrobert
252*404b540aSrobert explicit
253*404b540aSrobert codecvt(size_t __refs = 0)
254*404b540aSrobert : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
255*404b540aSrobert { }
256*404b540aSrobert
257*404b540aSrobert explicit
258*404b540aSrobert codecvt(state_type& __enc, size_t __refs = 0)
259*404b540aSrobert : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
260*404b540aSrobert { }
261*404b540aSrobert
262*404b540aSrobert protected:
263*404b540aSrobert virtual
~codecvt()264*404b540aSrobert ~codecvt() { }
265*404b540aSrobert
266*404b540aSrobert virtual result
267*404b540aSrobert do_out(state_type& __state, const intern_type* __from,
268*404b540aSrobert const intern_type* __from_end, const intern_type*& __from_next,
269*404b540aSrobert extern_type* __to, extern_type* __to_end,
270*404b540aSrobert extern_type*& __to_next) const;
271*404b540aSrobert
272*404b540aSrobert virtual result
273*404b540aSrobert do_unshift(state_type& __state, extern_type* __to,
274*404b540aSrobert extern_type* __to_end, extern_type*& __to_next) const;
275*404b540aSrobert
276*404b540aSrobert virtual result
277*404b540aSrobert do_in(state_type& __state, const extern_type* __from,
278*404b540aSrobert const extern_type* __from_end, const extern_type*& __from_next,
279*404b540aSrobert intern_type* __to, intern_type* __to_end,
280*404b540aSrobert intern_type*& __to_next) const;
281*404b540aSrobert
282*404b540aSrobert virtual int
283*404b540aSrobert do_encoding() const throw();
284*404b540aSrobert
285*404b540aSrobert virtual bool
286*404b540aSrobert do_always_noconv() const throw();
287*404b540aSrobert
288*404b540aSrobert virtual int
289*404b540aSrobert do_length(state_type&, const extern_type* __from,
290*404b540aSrobert const extern_type* __end, size_t __max) const;
291*404b540aSrobert
292*404b540aSrobert virtual int
293*404b540aSrobert do_max_length() const throw();
294*404b540aSrobert };
295*404b540aSrobert
296*404b540aSrobert template<typename _InternT, typename _ExternT>
297*404b540aSrobert locale::id
298*404b540aSrobert codecvt<_InternT, _ExternT, encoding_state>::id;
299*404b540aSrobert
300*404b540aSrobert // This adaptor works around the signature problems of the second
301*404b540aSrobert // argument to iconv(): SUSv2 and others use 'const char**', but glibc 2.2
302*404b540aSrobert // uses 'char**', which matches the POSIX 1003.1-2001 standard.
303*404b540aSrobert // Using this adaptor, g++ will do the work for us.
304*404b540aSrobert template<typename _Tp>
305*404b540aSrobert inline size_t
__iconv_adaptor(size_t (* __func)(iconv_t,_Tp,size_t *,char **,size_t *),iconv_t __cd,char ** __inbuf,size_t * __inbytes,char ** __outbuf,size_t * __outbytes)306*404b540aSrobert __iconv_adaptor(size_t(*__func)(iconv_t, _Tp, size_t*, char**, size_t*),
307*404b540aSrobert iconv_t __cd, char** __inbuf, size_t* __inbytes,
308*404b540aSrobert char** __outbuf, size_t* __outbytes)
309*404b540aSrobert { return __func(__cd, (_Tp)__inbuf, __inbytes, __outbuf, __outbytes); }
310*404b540aSrobert
311*404b540aSrobert template<typename _InternT, typename _ExternT>
312*404b540aSrobert codecvt_base::result
313*404b540aSrobert codecvt<_InternT, _ExternT, encoding_state>::
do_out(state_type & __state,const intern_type * __from,const intern_type * __from_end,const intern_type * & __from_next,extern_type * __to,extern_type * __to_end,extern_type * & __to_next)314*404b540aSrobert do_out(state_type& __state, const intern_type* __from,
315*404b540aSrobert const intern_type* __from_end, const intern_type*& __from_next,
316*404b540aSrobert extern_type* __to, extern_type* __to_end,
317*404b540aSrobert extern_type*& __to_next) const
318*404b540aSrobert {
319*404b540aSrobert result __ret = codecvt_base::error;
320*404b540aSrobert if (__state.good())
321*404b540aSrobert {
322*404b540aSrobert const descriptor_type& __desc = __state.out_descriptor();
323*404b540aSrobert const size_t __fmultiple = sizeof(intern_type);
324*404b540aSrobert size_t __fbytes = __fmultiple * (__from_end - __from);
325*404b540aSrobert const size_t __tmultiple = sizeof(extern_type);
326*404b540aSrobert size_t __tbytes = __tmultiple * (__to_end - __to);
327*404b540aSrobert
328*404b540aSrobert // Argument list for iconv specifies a byte sequence. Thus,
329*404b540aSrobert // all to/from arrays must be brutally casted to char*.
330*404b540aSrobert char* __cto = reinterpret_cast<char*>(__to);
331*404b540aSrobert char* __cfrom;
332*404b540aSrobert size_t __conv;
333*404b540aSrobert
334*404b540aSrobert // Some encodings need a byte order marker as the first item
335*404b540aSrobert // in the byte stream, to designate endian-ness. The default
336*404b540aSrobert // value for the byte order marker is NULL, so if this is
337*404b540aSrobert // the case, it's not necessary and we can just go on our
338*404b540aSrobert // merry way.
339*404b540aSrobert int __int_bom = __state.internal_bom();
340*404b540aSrobert if (__int_bom)
341*404b540aSrobert {
342*404b540aSrobert size_t __size = __from_end - __from;
343*404b540aSrobert intern_type* __cfixed = static_cast<intern_type*>
344*404b540aSrobert (__builtin_alloca(sizeof(intern_type) * (__size + 1)));
345*404b540aSrobert __cfixed[0] = static_cast<intern_type>(__int_bom);
346*404b540aSrobert char_traits<intern_type>::copy(__cfixed + 1, __from, __size);
347*404b540aSrobert __cfrom = reinterpret_cast<char*>(__cfixed);
348*404b540aSrobert __conv = __iconv_adaptor(iconv, __desc, &__cfrom,
349*404b540aSrobert &__fbytes, &__cto, &__tbytes);
350*404b540aSrobert }
351*404b540aSrobert else
352*404b540aSrobert {
353*404b540aSrobert intern_type* __cfixed = const_cast<intern_type*>(__from);
354*404b540aSrobert __cfrom = reinterpret_cast<char*>(__cfixed);
355*404b540aSrobert __conv = __iconv_adaptor(iconv, __desc, &__cfrom, &__fbytes,
356*404b540aSrobert &__cto, &__tbytes);
357*404b540aSrobert }
358*404b540aSrobert
359*404b540aSrobert if (__conv != size_t(-1))
360*404b540aSrobert {
361*404b540aSrobert __from_next = reinterpret_cast<const intern_type*>(__cfrom);
362*404b540aSrobert __to_next = reinterpret_cast<extern_type*>(__cto);
363*404b540aSrobert __ret = codecvt_base::ok;
364*404b540aSrobert }
365*404b540aSrobert else
366*404b540aSrobert {
367*404b540aSrobert if (__fbytes < __fmultiple * (__from_end - __from))
368*404b540aSrobert {
369*404b540aSrobert __from_next = reinterpret_cast<const intern_type*>(__cfrom);
370*404b540aSrobert __to_next = reinterpret_cast<extern_type*>(__cto);
371*404b540aSrobert __ret = codecvt_base::partial;
372*404b540aSrobert }
373*404b540aSrobert else
374*404b540aSrobert __ret = codecvt_base::error;
375*404b540aSrobert }
376*404b540aSrobert }
377*404b540aSrobert return __ret;
378*404b540aSrobert }
379*404b540aSrobert
380*404b540aSrobert template<typename _InternT, typename _ExternT>
381*404b540aSrobert codecvt_base::result
382*404b540aSrobert codecvt<_InternT, _ExternT, encoding_state>::
do_unshift(state_type & __state,extern_type * __to,extern_type * __to_end,extern_type * & __to_next)383*404b540aSrobert do_unshift(state_type& __state, extern_type* __to,
384*404b540aSrobert extern_type* __to_end, extern_type*& __to_next) const
385*404b540aSrobert {
386*404b540aSrobert result __ret = codecvt_base::error;
387*404b540aSrobert if (__state.good())
388*404b540aSrobert {
389*404b540aSrobert const descriptor_type& __desc = __state.in_descriptor();
390*404b540aSrobert const size_t __tmultiple = sizeof(intern_type);
391*404b540aSrobert size_t __tlen = __tmultiple * (__to_end - __to);
392*404b540aSrobert
393*404b540aSrobert // Argument list for iconv specifies a byte sequence. Thus,
394*404b540aSrobert // all to/from arrays must be brutally casted to char*.
395*404b540aSrobert char* __cto = reinterpret_cast<char*>(__to);
396*404b540aSrobert size_t __conv = __iconv_adaptor(iconv,__desc, NULL, NULL,
397*404b540aSrobert &__cto, &__tlen);
398*404b540aSrobert
399*404b540aSrobert if (__conv != size_t(-1))
400*404b540aSrobert {
401*404b540aSrobert __to_next = reinterpret_cast<extern_type*>(__cto);
402*404b540aSrobert if (__tlen == __tmultiple * (__to_end - __to))
403*404b540aSrobert __ret = codecvt_base::noconv;
404*404b540aSrobert else if (__tlen == 0)
405*404b540aSrobert __ret = codecvt_base::ok;
406*404b540aSrobert else
407*404b540aSrobert __ret = codecvt_base::partial;
408*404b540aSrobert }
409*404b540aSrobert else
410*404b540aSrobert __ret = codecvt_base::error;
411*404b540aSrobert }
412*404b540aSrobert return __ret;
413*404b540aSrobert }
414*404b540aSrobert
415*404b540aSrobert template<typename _InternT, typename _ExternT>
416*404b540aSrobert codecvt_base::result
417*404b540aSrobert codecvt<_InternT, _ExternT, encoding_state>::
do_in(state_type & __state,const extern_type * __from,const extern_type * __from_end,const extern_type * & __from_next,intern_type * __to,intern_type * __to_end,intern_type * & __to_next)418*404b540aSrobert do_in(state_type& __state, const extern_type* __from,
419*404b540aSrobert const extern_type* __from_end, const extern_type*& __from_next,
420*404b540aSrobert intern_type* __to, intern_type* __to_end,
421*404b540aSrobert intern_type*& __to_next) const
422*404b540aSrobert {
423*404b540aSrobert result __ret = codecvt_base::error;
424*404b540aSrobert if (__state.good())
425*404b540aSrobert {
426*404b540aSrobert const descriptor_type& __desc = __state.in_descriptor();
427*404b540aSrobert const size_t __fmultiple = sizeof(extern_type);
428*404b540aSrobert size_t __flen = __fmultiple * (__from_end - __from);
429*404b540aSrobert const size_t __tmultiple = sizeof(intern_type);
430*404b540aSrobert size_t __tlen = __tmultiple * (__to_end - __to);
431*404b540aSrobert
432*404b540aSrobert // Argument list for iconv specifies a byte sequence. Thus,
433*404b540aSrobert // all to/from arrays must be brutally casted to char*.
434*404b540aSrobert char* __cto = reinterpret_cast<char*>(__to);
435*404b540aSrobert char* __cfrom;
436*404b540aSrobert size_t __conv;
437*404b540aSrobert
438*404b540aSrobert // Some encodings need a byte order marker as the first item
439*404b540aSrobert // in the byte stream, to designate endian-ness. The default
440*404b540aSrobert // value for the byte order marker is NULL, so if this is
441*404b540aSrobert // the case, it's not necessary and we can just go on our
442*404b540aSrobert // merry way.
443*404b540aSrobert int __ext_bom = __state.external_bom();
444*404b540aSrobert if (__ext_bom)
445*404b540aSrobert {
446*404b540aSrobert size_t __size = __from_end - __from;
447*404b540aSrobert extern_type* __cfixed = static_cast<extern_type*>
448*404b540aSrobert (__builtin_alloca(sizeof(extern_type) * (__size + 1)));
449*404b540aSrobert __cfixed[0] = static_cast<extern_type>(__ext_bom);
450*404b540aSrobert char_traits<extern_type>::copy(__cfixed + 1, __from, __size);
451*404b540aSrobert __cfrom = reinterpret_cast<char*>(__cfixed);
452*404b540aSrobert __conv = __iconv_adaptor(iconv, __desc, &__cfrom,
453*404b540aSrobert &__flen, &__cto, &__tlen);
454*404b540aSrobert }
455*404b540aSrobert else
456*404b540aSrobert {
457*404b540aSrobert extern_type* __cfixed = const_cast<extern_type*>(__from);
458*404b540aSrobert __cfrom = reinterpret_cast<char*>(__cfixed);
459*404b540aSrobert __conv = __iconv_adaptor(iconv, __desc, &__cfrom,
460*404b540aSrobert &__flen, &__cto, &__tlen);
461*404b540aSrobert }
462*404b540aSrobert
463*404b540aSrobert
464*404b540aSrobert if (__conv != size_t(-1))
465*404b540aSrobert {
466*404b540aSrobert __from_next = reinterpret_cast<const extern_type*>(__cfrom);
467*404b540aSrobert __to_next = reinterpret_cast<intern_type*>(__cto);
468*404b540aSrobert __ret = codecvt_base::ok;
469*404b540aSrobert }
470*404b540aSrobert else
471*404b540aSrobert {
472*404b540aSrobert if (__flen < static_cast<size_t>(__from_end - __from))
473*404b540aSrobert {
474*404b540aSrobert __from_next = reinterpret_cast<const extern_type*>(__cfrom);
475*404b540aSrobert __to_next = reinterpret_cast<intern_type*>(__cto);
476*404b540aSrobert __ret = codecvt_base::partial;
477*404b540aSrobert }
478*404b540aSrobert else
479*404b540aSrobert __ret = codecvt_base::error;
480*404b540aSrobert }
481*404b540aSrobert }
482*404b540aSrobert return __ret;
483*404b540aSrobert }
484*404b540aSrobert
485*404b540aSrobert template<typename _InternT, typename _ExternT>
486*404b540aSrobert int
487*404b540aSrobert codecvt<_InternT, _ExternT, encoding_state>::
do_encoding()488*404b540aSrobert do_encoding() const throw()
489*404b540aSrobert {
490*404b540aSrobert int __ret = 0;
491*404b540aSrobert if (sizeof(_ExternT) <= sizeof(_InternT))
492*404b540aSrobert __ret = sizeof(_InternT) / sizeof(_ExternT);
493*404b540aSrobert return __ret;
494*404b540aSrobert }
495*404b540aSrobert
496*404b540aSrobert template<typename _InternT, typename _ExternT>
497*404b540aSrobert bool
498*404b540aSrobert codecvt<_InternT, _ExternT, encoding_state>::
do_always_noconv()499*404b540aSrobert do_always_noconv() const throw()
500*404b540aSrobert { return false; }
501*404b540aSrobert
502*404b540aSrobert template<typename _InternT, typename _ExternT>
503*404b540aSrobert int
504*404b540aSrobert codecvt<_InternT, _ExternT, encoding_state>::
do_length(state_type &,const extern_type * __from,const extern_type * __end,size_t __max)505*404b540aSrobert do_length(state_type&, const extern_type* __from,
506*404b540aSrobert const extern_type* __end, size_t __max) const
507*404b540aSrobert { return std::min(__max, static_cast<size_t>(__end - __from)); }
508*404b540aSrobert
509*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
510*404b540aSrobert // 74. Garbled text for codecvt::do_max_length
511*404b540aSrobert template<typename _InternT, typename _ExternT>
512*404b540aSrobert int
513*404b540aSrobert codecvt<_InternT, _ExternT, encoding_state>::
do_max_length()514*404b540aSrobert do_max_length() const throw()
515*404b540aSrobert { return 1; }
516*404b540aSrobert
517*404b540aSrobert _GLIBCXX_END_NAMESPACE
518*404b540aSrobert
519*404b540aSrobert #endif
520*404b540aSrobert
521*404b540aSrobert #endif
522