1 // Copyright (C) 2003-2021 Free Software Foundation, Inc.
2 //
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)
7 // any later version.
8 
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.
13 
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/>.
17 
18 // 27.8.1.4 Overridden virtual functions
19 
20 #include <fstream>
21 #include <string>
22 #include <iterator>
23 #include <algorithm>
24 #include <locale>
25 #include <testsuite_hooks.h>
26 
27 template <typename InternT, typename StateT = mbstate_t>
28 class checksumcvt : public std::codecvt<InternT, char, StateT>
29 {
30   typedef std::codecvt<InternT, char, StateT> Base;
31   static const size_t width = sizeof(InternT) + 1;
32 
33 public:
34   typedef InternT intern_type;
35   typedef char extern_type;
36 
checksumcvt(size_t refs=0)37   explicit checksumcvt(size_t refs = 0)
38   : Base(refs)
39   { }
40 
41 protected:
42   virtual typename std::codecvt<InternT, char, StateT>::result
do_out(StateT &,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) const43   do_out(StateT&, const intern_type* from,
44 	 const intern_type* from_end, const intern_type*& from_next,
45 	 extern_type* to, extern_type* to_end,
46 	 extern_type*& to_next) const
47   {
48     size_t len = std::min(static_cast<size_t>(from_end - from),
49 			  static_cast<size_t>(to_end - to) / width);
50 
51     while (len--)
52       {
53 	const char* p = reinterpret_cast<const char*>(from);
54 	unsigned char checksum = 0;
55 
56 	for (size_t i = 0; i < sizeof(intern_type); ++i)
57 	  {
58 	    *to++ = p[i];
59 	    checksum ^= static_cast<unsigned char>(p[i]);
60 	  }
61 
62 	*to++ = checksum;
63 	++from;
64       }
65 
66     from_next = from;
67     to_next = to;
68     return from_next == from_end ? std::codecvt<InternT, char, StateT>::ok
69            : std::codecvt<InternT, char, StateT>::partial;
70   }
71 
72   virtual typename std::codecvt<InternT, char, StateT>::result
do_unshift(StateT &,extern_type * to,extern_type *,extern_type * & to_next) const73   do_unshift(StateT&, extern_type* to,
74 	     extern_type*, extern_type*& to_next) const
75   {
76     to_next = to;
77     return std::codecvt<InternT, char, StateT>::ok;
78   }
79 
80   virtual typename std::codecvt<InternT, char, StateT>::result
do_in(StateT &,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) const81   do_in(StateT&, const extern_type* from,
82 	const extern_type* from_end, const extern_type*& from_next,
83 	intern_type* to, intern_type* to_end,
84 	intern_type*& to_next) const
85   {
86     size_t len = std::min(static_cast<size_t>(to_end - to),
87 			  static_cast<size_t>(from_end - from) / width);
88 
89     while (len)
90       {
91 	const char* f = from;
92 	intern_type tmp;
93 	char* p = reinterpret_cast<char*>(&tmp);
94 	unsigned char checksum = 0;
95 
96 	for (size_t i = 0; i < sizeof(intern_type); ++i)
97 	  {
98 	    p[i] = *f;
99 	    checksum ^= static_cast<unsigned char>(*f++);
100 	  }
101 
102 	if (*f++ != checksum)
103 	  break;
104 
105 	from = f;
106 	*to++ = tmp;
107 	len--;
108       }
109 
110     from_next = from;
111     to_next = to;
112     return len ? std::codecvt<InternT, char, StateT>::error :
113       (from_next == from_end ? std::codecvt<InternT, char, StateT>::ok
114        : std::codecvt<InternT, char, StateT>::partial);
115   }
116 
117   virtual int
do_encoding() const118   do_encoding() const throw()
119   {
120     return width;
121   }
122 
123   virtual int
do_length(StateT &,const extern_type * from,const extern_type * end,size_t max) const124   do_length(StateT&, const extern_type* from,
125 	    const extern_type* end, size_t max) const
126   {
127     size_t len = std::min(max, static_cast<size_t>(end - from) / width);
128 
129     int ret = 0;
130     while (len--)
131       {
132 	unsigned char checksum = 0;
133 
134 	for (size_t i = 0; i < sizeof(intern_type); ++i)
135 	  {
136 	    checksum ^= static_cast<unsigned char>(*from++);
137 	  }
138 
139 	if (*from++ != checksum)
140 	  break;
141 
142 	ret++;
143       }
144 
145     return ret;
146   }
147 
148   virtual int
do_max_length() const149   do_max_length() const throw()
150   {
151     return width;
152   }
153 
154   virtual bool
do_always_noconv() const155   do_always_noconv() const throw()
156   {
157     return false;
158   }
159 };
160 
test01()161 void test01()
162 {
163   using namespace std;
164 
165   locale loc;
166   loc = locale(loc, new checksumcvt<wchar_t>);
167 
168   wfilebuf fbuf1;
169   fbuf1.pubimbue(loc);
170   fbuf1.open("tmp_9178", ios_base::out | ios_base::trunc);
171 
172   string tmpstr = "abcdefghijklmnopqrstuvwxyz0123456789 \t\n";
173 
174   wifstream stream;
175   wstring str1;
176 
177   while (str1.length() < 20000)
178     {
179       transform(tmpstr.begin(), tmpstr.end(),
180 		back_inserter(str1),
181 		bind1st(std::mem_fun(&std::wios::widen), &stream));
182     }
183 
184   fbuf1.sputn(str1.data(), str1.size());
185   fbuf1.close();
186 
187   wfilebuf fbuf2;
188   fbuf2.pubimbue(loc);
189   fbuf2.open("tmp_9178", std::ios_base::in);
190 
191   wstring str2;
192   copy(istreambuf_iterator<wchar_t>(&fbuf2),
193        istreambuf_iterator<wchar_t>(),
194        back_inserter(str2));
195 
196   VERIFY( str1 == str2 );
197 }
198 
main()199 int main()
200 {
201   test01();
202   return 0;
203 }
204