]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/wchar_t/11544-2.cc
1 // Copyright (C) 2003-2021 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // 27.8.1.4 Overridden virtual functions
23 #include <testsuite_hooks.h>
25 template <typename InternT
, typename StateT
= mbstate_t>
26 class checksumcvt
: public std::codecvt
<InternT
, char, StateT
>
28 typedef std::codecvt
<InternT
, char, StateT
> Base
;
29 static const size_t width
= sizeof(InternT
) + 1;
32 typedef InternT intern_type
;
33 typedef char extern_type
;
35 explicit checksumcvt(size_t refs
= 0)
40 virtual typename
std::codecvt
<InternT
, char, StateT
>::result
41 do_out(StateT
&, const intern_type
* from
,
42 const intern_type
* from_end
, const intern_type
*& from_next
,
43 extern_type
* to
, extern_type
* to_end
,
44 extern_type
*& to_next
) const
46 size_t len
= std::min(static_cast<size_t>(from_end
- from
),
47 static_cast<size_t>(to_end
- to
) / width
);
51 const char* p
= reinterpret_cast<const char*>(from
);
52 unsigned char checksum
= 0;
54 for (size_t i
= 0; i
< sizeof(intern_type
); ++i
)
57 checksum
^= static_cast<unsigned char>(p
[i
]);
66 return from_next
== from_end
? std::codecvt
<InternT
, char, StateT
>::ok
67 : std::codecvt
<InternT
, char, StateT
>::partial
;
70 virtual typename
std::codecvt
<InternT
, char, StateT
>::result
71 do_unshift(StateT
&, extern_type
* to
,
72 extern_type
*, extern_type
*& to_next
) const
75 return std::codecvt
<InternT
, char, StateT
>::ok
;
78 virtual typename
std::codecvt
<InternT
, char, StateT
>::result
79 do_in(StateT
&, const extern_type
* from
,
80 const extern_type
* from_end
, const extern_type
*& from_next
,
81 intern_type
* to
, intern_type
* to_end
,
82 intern_type
*& to_next
) const
84 size_t len
= std::min(static_cast<size_t>(to_end
- to
),
85 static_cast<size_t>(from_end
- from
) / width
);
91 char* p
= reinterpret_cast<char*>(&tmp
);
92 unsigned char checksum
= 0;
94 for (size_t i
= 0; i
< sizeof(intern_type
); ++i
)
97 checksum
^= static_cast<unsigned char>(*f
++);
100 if (*f
++ != checksum
)
110 return len
? std::codecvt
<InternT
, char, StateT
>::error
:
111 (from_next
== from_end
? std::codecvt
<InternT
, char, StateT
>::ok
112 : std::codecvt
<InternT
, char, StateT
>::partial
);
116 do_encoding() const throw()
120 do_length(StateT
&, const extern_type
* from
,
121 const extern_type
* end
, size_t max
) const
123 size_t len
= std::min(max
, static_cast<size_t>(end
- from
) / width
);
128 unsigned char checksum
= 0;
130 for (size_t i
= 0; i
< sizeof(intern_type
); ++i
)
132 checksum
^= static_cast<unsigned char>(*from
++);
135 if (*from
++ != checksum
)
145 do_max_length() const throw()
149 do_always_noconv() const throw()
153 // libstdc++/11544 (invalid byte sequence in file)
158 locale
loc(locale::classic(), new checksumcvt
<wchar_t>);
160 const char* name
= "tmp_11544-2";
162 FILE* f
= fopen(name
, "w");
163 VERIFY( fwrite("aaaab", 1, 5, f
) == 5 );
172 VERIFY( !in
.good() );