@@ -265,6 +265,7 @@ void Utf8DecoderBase::Reset(uint16_t* buffer,
265
265
// Assume everything will fit in the buffer and stream won't be needed.
266
266
last_byte_of_buffer_unused_ = false ;
267
267
unbuffered_start_ = NULL ;
268
+ unbuffered_length_ = 0 ;
268
269
bool writing_to_buffer = true ;
269
270
// Loop until stream is read, writing to buffer as long as buffer has space.
270
271
unsigned utf16_length = 0 ;
@@ -291,6 +292,7 @@ void Utf8DecoderBase::Reset(uint16_t* buffer,
291
292
// Just wrote last character of buffer
292
293
writing_to_buffer = false ;
293
294
unbuffered_start_ = stream;
295
+ unbuffered_length_ = stream_length;
294
296
}
295
297
continue ;
296
298
}
@@ -300,20 +302,24 @@ void Utf8DecoderBase::Reset(uint16_t* buffer,
300
302
writing_to_buffer = false ;
301
303
last_byte_of_buffer_unused_ = true ;
302
304
unbuffered_start_ = stream - cursor;
305
+ unbuffered_length_ = stream_length + cursor;
303
306
}
304
307
utf16_length_ = utf16_length;
305
308
}
306
309
307
310
308
311
void Utf8DecoderBase::WriteUtf16Slow (const uint8_t * stream,
312
+ unsigned stream_length,
309
313
uint16_t * data,
310
314
unsigned data_length) {
311
315
while (data_length != 0 ) {
312
316
unsigned cursor = 0 ;
313
- uint32_t character = Utf8::ValueOf (stream, Utf8::kMaxEncodedSize , &cursor);
317
+
318
+ uint32_t character = Utf8::ValueOf (stream, stream_length, &cursor);
314
319
// There's a total lack of bounds checking for stream
315
320
// as it was already done in Reset.
316
321
stream += cursor;
322
+ stream_length -= cursor;
317
323
if (character > unibrow::Utf16::kMaxNonSurrogateCharCode ) {
318
324
*data++ = Utf16::LeadSurrogate (character);
319
325
*data++ = Utf16::TrailSurrogate (character);
@@ -324,6 +330,7 @@ void Utf8DecoderBase::WriteUtf16Slow(const uint8_t* stream,
324
330
data_length -= 1 ;
325
331
}
326
332
}
333
+ DCHECK (stream_length >= 0 );
327
334
}
328
335
329
336
0 commit comments