@@ -15,7 +15,6 @@ import { BSONRegExp } from '../regexp';
1515import { BSONSymbol } from '../symbol' ;
1616import { Timestamp } from '../timestamp' ;
1717import { BSONDataView , ByteUtils } from '../utils/byte_utils' ;
18- import { validateUtf8 } from '../validate_utf8' ;
1918
2019/** @public */
2120export interface DeserializeOptions {
@@ -236,7 +235,7 @@ function deserializeObject(
236235 if ( i >= buffer . byteLength ) throw new BSONError ( 'Bad BSON Document: illegal CString' ) ;
237236
238237 // Represents the key
239- const name = isArray ? arrayIndex ++ : ByteUtils . toUTF8 ( buffer , index , i ) ;
238+ const name = isArray ? arrayIndex ++ : ByteUtils . toUTF8 ( buffer , index , i , false ) ;
240239
241240 // shouldValidateKey is true if the key should be validated, false otherwise
242241 let shouldValidateKey = true ;
@@ -266,7 +265,7 @@ function deserializeObject(
266265 ) {
267266 throw new BSONError ( 'bad string length in bson' ) ;
268267 }
269- value = getValidatedString ( buffer , index , index + stringSize - 1 , shouldValidateKey ) ;
268+ value = ByteUtils . toUTF8 ( buffer , index , index + stringSize - 1 , shouldValidateKey ) ;
270269 index = index + stringSize ;
271270 } else if ( elementType === constants . BSON_DATA_OID ) {
272271 const oid = ByteUtils . allocate ( 12 ) ;
@@ -476,7 +475,7 @@ function deserializeObject(
476475 // If are at the end of the buffer there is a problem with the document
477476 if ( i >= buffer . length ) throw new BSONError ( 'Bad BSON Document: illegal CString' ) ;
478477 // Return the C string
479- const source = ByteUtils . toUTF8 ( buffer , index , i ) ;
478+ const source = ByteUtils . toUTF8 ( buffer , index , i , false ) ;
480479 // Create the regexp
481480 index = i + 1 ;
482481
@@ -489,7 +488,7 @@ function deserializeObject(
489488 // If are at the end of the buffer there is a problem with the document
490489 if ( i >= buffer . length ) throw new BSONError ( 'Bad BSON Document: illegal CString' ) ;
491490 // Return the C string
492- const regExpOptions = ByteUtils . toUTF8 ( buffer , index , i ) ;
491+ const regExpOptions = ByteUtils . toUTF8 ( buffer , index , i , false ) ;
493492 index = i + 1 ;
494493
495494 // For each option add the corresponding one for javascript
@@ -521,7 +520,7 @@ function deserializeObject(
521520 // If are at the end of the buffer there is a problem with the document
522521 if ( i >= buffer . length ) throw new BSONError ( 'Bad BSON Document: illegal CString' ) ;
523522 // Return the C string
524- const source = ByteUtils . toUTF8 ( buffer , index , i ) ;
523+ const source = ByteUtils . toUTF8 ( buffer , index , i , false ) ;
525524 index = i + 1 ;
526525
527526 // Get the start search index
@@ -533,7 +532,7 @@ function deserializeObject(
533532 // If are at the end of the buffer there is a problem with the document
534533 if ( i >= buffer . length ) throw new BSONError ( 'Bad BSON Document: illegal CString' ) ;
535534 // Return the C string
536- const regExpOptions = ByteUtils . toUTF8 ( buffer , index , i ) ;
535+ const regExpOptions = ByteUtils . toUTF8 ( buffer , index , i , false ) ;
537536 index = i + 1 ;
538537
539538 // Set the object
@@ -551,7 +550,7 @@ function deserializeObject(
551550 ) {
552551 throw new BSONError ( 'bad string length in bson' ) ;
553552 }
554- const symbol = getValidatedString ( buffer , index , index + stringSize - 1 , shouldValidateKey ) ;
553+ const symbol = ByteUtils . toUTF8 ( buffer , index , index + stringSize - 1 , shouldValidateKey ) ;
555554 value = promoteValues ? symbol : new BSONSymbol ( symbol ) ;
556555 index = index + stringSize ;
557556 } else if ( elementType === constants . BSON_DATA_TIMESTAMP ) {
@@ -587,7 +586,7 @@ function deserializeObject(
587586 ) {
588587 throw new BSONError ( 'bad string length in bson' ) ;
589588 }
590- const functionString = getValidatedString (
589+ const functionString = ByteUtils . toUTF8 (
591590 buffer ,
592591 index ,
593592 index + stringSize - 1 ,
@@ -626,7 +625,7 @@ function deserializeObject(
626625 }
627626
628627 // Javascript function
629- const functionString = getValidatedString (
628+ const functionString = ByteUtils . toUTF8 (
630629 buffer ,
631630 index ,
632631 index + stringSize - 1 ,
@@ -673,12 +672,7 @@ function deserializeObject(
673672 )
674673 throw new BSONError ( 'bad string length in bson' ) ;
675674 // Namespace
676- if ( validation != null && validation . utf8 ) {
677- if ( ! validateUtf8 ( buffer , index , index + stringSize - 1 ) ) {
678- throw new BSONError ( 'Invalid UTF-8 string in BSON document' ) ;
679- }
680- }
681- const namespace = ByteUtils . toUTF8 ( buffer , index , index + stringSize - 1 ) ;
675+ const namespace = ByteUtils . toUTF8 ( buffer , index , index + stringSize - 1 , shouldValidateKey ) ;
682676 // Update parse index position
683677 index = index + stringSize ;
684678
@@ -728,24 +722,3 @@ function deserializeObject(
728722
729723 return object ;
730724}
731-
732- function getValidatedString (
733- buffer : Uint8Array ,
734- start : number ,
735- end : number ,
736- shouldValidateUtf8 : boolean
737- ) {
738- const value = ByteUtils . toUTF8 ( buffer , start , end ) ;
739- // if utf8 validation is on, do the check
740- if ( shouldValidateUtf8 ) {
741- for ( let i = 0 ; i < value . length ; i ++ ) {
742- if ( value . charCodeAt ( i ) === 0xfffd ) {
743- if ( ! validateUtf8 ( buffer , start , end ) ) {
744- throw new BSONError ( 'Invalid UTF-8 string in BSON document' ) ;
745- }
746- break ;
747- }
748- }
749- }
750- return value ;
751- }
0 commit comments