~zorba-coders/zorba/bug1123835

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "unparsed_streambuf.h"

#include "diagnostics/xquery_diagnostics.h"
#include "diagnostics/util_macros.h"
#include "util/oseparator.h"

#include <iomanip>

using namespace std;

namespace zorba {
namespace unparsed{
  namespace xml{
    streambuf::pos_type streambuf::seekoff( off_type o, ios_base::seekdir d, ios_base::openmode m ) 
    {
      clear();
      return original()->pubseekoff( o, d, m );
    }

    streambuf::pos_type streambuf::seekpos( pos_type p, ios_base::openmode m ) 
    {
      clear();
      return original()->pubseekpos( p, m );
    }

    streambuf::int_type streambuf::pbackfail( int_type c ) 
    {
      if ( !traits_type::eq_int_type( c, traits_type::eof() ) &&
           gbuf_.cur_len_ &&
           original()->sputbackc( traits_type::to_char_type( c ) ) ) {
        --gbuf_.cur_len_;
        return c;
      }
      return traits_type::eof();
    }

    streambuf::int_type streambuf::uflow() 
    {
    #ifdef ZORBA_DEBUG_UTF8_STREAMBUF
      printf( "uflow()\n" );
    #endif
      int_type const c = original()->sbumpc();
      if ( traits_type::eq_int_type( c, traits_type::eof() ) )
        return traits_type::eof();
      gbuf_.validate( traits_type::to_char_type( c ) );
      return c;
    }

    inline void streambuf::clear() {
      gbuf_.clear();
    }
  
    streamsize streambuf::xsgetn(char_type* to, std::streamsize size )
    {
    #ifdef ZORBA_DEBUG_UTF8_STREAMBUF
      printf( "xsgetn()\n" );
    #endif
      streamsize return_size = 0;

      if ( gbuf_.char_len_ ) {
        streamsize const want = gbuf_.char_len_ - gbuf_.cur_len_;
        streamsize const get = min( want, size );
        streamsize const got = original()->sgetn( to, get );
        for ( streamsize i = 0; i < got; ++i )
          gbuf_.validate( to[i] );
        to += got;
        size -= got, return_size += got;
      }

      while ( size > 0 ) {
        if ( streamsize const got = original()->sgetn( to, size ) ) {
          for ( streamsize i = 0; i < got; ++i )
            gbuf_.validate( to[i] );
          to += got;
          size -= got, return_size += got;
        } else
          break;
      }
      return return_size;
    }

    inline void streambuf::buf_type::clear() 
    {
      char_len_ = 0;
    }

    void streambuf::buf_type::throw_invalid_utf8( utf8::storage_type *buf, utf8::size_type len ) {
      ostringstream oss;
      oss << hex << setfill('0') << setw(2) << uppercase;
      oseparator comma( ',' );

      for ( utf8::size_type i = 0; i < len; ++i )
        oss << comma << "0x" << (static_cast<unsigned>( buf[i] ) & 0xFF);

      clear();
      throw ZORBA_EXCEPTION(
        zerr::ZXQD0006_INVALID_UTF8_BYTE_SEQUENCE,
        ERROR_PARAMS( oss.str() )
      );
    }

    void streambuf::buf_type::validate( utf8::storage_type c, bool bump ) {
      utf8::size_type char_len_copy = char_len_, cur_len_copy = cur_len_;

      if ( !char_len_copy ) {
        //
        // This means we're (hopefully) at the first byte of a UTF-8 byte sequence
        // comprising a character.
        //
        try {
          char_len_copy = utf8::char_length( c );
          cur_len_copy = 0;
          if (!c)
            throw_invalid_utf8 ( &c, 1);
        }
        catch ( utf8::invalid_byte const& ) {
          throw_invalid_utf8( &c, 1 );
        }
      }

      utf8::storage_type *const cur_byte_ptr = utf8_char_ + cur_len_copy;
      utf8::storage_type const old_byte = *cur_byte_ptr;
      *cur_byte_ptr = c;

      if ( cur_len_copy++ && !utf8::is_continuation_byte( c ) )
        throw_invalid_utf8( utf8_char_, cur_len_copy );

      if ( bump ) {
        char_len_ = (cur_len_copy == char_len_copy ? 0 : char_len_copy);
        cur_len_ = cur_len_copy;
      } else {
        *cur_byte_ptr = old_byte;
      }
    }
  } //xml namespace

  streambuf::streambuf(std::streambuf* orig, zstring const& uri, QueryLoc const& loc) :
    proxy_buf(new xml::streambuf(orig)), 
    i_uri(uri),
    i_loc(loc),
    i_mark(0)
  {
  }

  streambuf::streambuf(std::streambuf* orig, int mark, zstring const& uri, QueryLoc const& loc) :
    proxy_buf(new xml::streambuf(orig)), 
    i_uri(uri),
    i_loc(loc),
    i_mark(mark)
  {
  }

  streambuf::~streambuf() {}

  void streambuf::imbue( std::locale const &loc)
  {
    proxy_buf->pubimbue( loc );
  }

  streambuf::pos_type streambuf::seekoff( off_type o, ios_base::seekdir d, ios_base::openmode m )
  {
    return proxy_buf->pubseekoff( o + i_mark, d, m);
  }
  
  streambuf::pos_type streambuf::seekpos( pos_type p, ios_base::openmode m ) {
    return proxy_buf->pubseekpos( p, m );
  }

  std::streambuf* streambuf::setbuf( char_type *p, streamsize s ) {
    proxy_buf->pubsetbuf( p, s );
    return this;
  }

  streamsize streambuf::showmanyc() {
    return proxy_buf->in_avail();
  }

  int streambuf::sync() {
    return proxy_buf->pubsync();
  }

  streambuf::int_type streambuf::overflow( int_type c ) {
    return proxy_buf->sputc( c );
  }

  streambuf::int_type streambuf::pbackfail( int_type c ) {
    return  traits_type::eq_int_type( c, traits_type::eof() ) ?
            c : proxy_buf->sputbackc( traits_type::to_char_type( c ) );
  }

  streambuf::int_type streambuf::uflow() {
    return proxy_buf->sbumpc();
  }

  streambuf::int_type streambuf::underflow() {
    return proxy_buf->sgetc();
  }

  streamsize streambuf::xsgetn( char_type *to, streamsize size ) {
    streamsize res;
    try 
    {
      res = proxy_buf->sgetn( to, size );
    }
    catch (ZorbaException const& e)
    {
      if (e.diagnostic() == zerr::ZXQD0006_INVALID_UTF8_BYTE_SEQUENCE)
        throw XQUERY_EXCEPTION(err::FOUT1190, ERROR_PARAMS(i_uri.c_str()), ERROR_LOC(i_loc));
      else throw;
    }
    return res;
  }

  streamsize streambuf::xsputn( char_type const *from, streamsize size ) {
    return proxy_buf->sputn( from, size );
    }

  /*********************************************************************/
  /*********************************************************************/
  std::streambuf* alloc_streambuf(std::streambuf *orig, int mark, zstring const& uri, QueryLoc const& loc) 
  {
    return new zorba::unparsed::streambuf(orig, mark, uri, loc);
  }

}//namesapce unparsed
}//namesapce zorba