@@ -236,7 +236,7 @@ function deserializeObject(
236236 if ( i >= buffer . byteLength ) throw new BSONError ( 'Bad BSON Document: illegal CString' ) ;
237237
238238 // Represents the key
239- const name = isArray ? arrayIndex ++ : ByteUtils . toUTF8 ( buffer , index , i ) ;
239+ const name = isArray ? arrayIndex ++ : ByteUtils . toUTF8 ( buffer , index , i , false ) ;
240240
241241 // shouldValidateKey is true if the key should be validated, false otherwise
242242 let shouldValidateKey = true ;
@@ -266,7 +266,7 @@ function deserializeObject(
266266 ) {
267267 throw new BSONError ( 'bad string length in bson' ) ;
268268 }
269- value = getValidatedString ( buffer , index , index + stringSize - 1 , shouldValidateKey ) ;
269+ value = ByteUtils . toUTF8 ( buffer , index , index + stringSize - 1 , shouldValidateKey ) ;
270270 index = index + stringSize ;
271271 } else if ( elementType === constants . BSON_DATA_OID ) {
272272 const oid = ByteUtils . allocate ( 12 ) ;
@@ -476,7 +476,7 @@ function deserializeObject(
476476 // If are at the end of the buffer there is a problem with the document
477477 if ( i >= buffer . length ) throw new BSONError ( 'Bad BSON Document: illegal CString' ) ;
478478 // Return the C string
479- const source = ByteUtils . toUTF8 ( buffer , index , i ) ;
479+ const source = ByteUtils . toUTF8 ( buffer , index , i , false ) ;
480480 // Create the regexp
481481 index = i + 1 ;
482482
@@ -489,7 +489,7 @@ function deserializeObject(
489489 // If are at the end of the buffer there is a problem with the document
490490 if ( i >= buffer . length ) throw new BSONError ( 'Bad BSON Document: illegal CString' ) ;
491491 // Return the C string
492- const regExpOptions = ByteUtils . toUTF8 ( buffer , index , i ) ;
492+ const regExpOptions = ByteUtils . toUTF8 ( buffer , index , i , false ) ;
493493 index = i + 1 ;
494494
495495 // For each option add the corresponding one for javascript
@@ -521,7 +521,7 @@ function deserializeObject(
521521 // If are at the end of the buffer there is a problem with the document
522522 if ( i >= buffer . length ) throw new BSONError ( 'Bad BSON Document: illegal CString' ) ;
523523 // Return the C string
524- const source = ByteUtils . toUTF8 ( buffer , index , i ) ;
524+ const source = ByteUtils . toUTF8 ( buffer , index , i , false ) ;
525525 index = i + 1 ;
526526
527527 // Get the start search index
@@ -533,7 +533,7 @@ function deserializeObject(
533533 // If are at the end of the buffer there is a problem with the document
534534 if ( i >= buffer . length ) throw new BSONError ( 'Bad BSON Document: illegal CString' ) ;
535535 // Return the C string
536- const regExpOptions = ByteUtils . toUTF8 ( buffer , index , i ) ;
536+ const regExpOptions = ByteUtils . toUTF8 ( buffer , index , i , false ) ;
537537 index = i + 1 ;
538538
539539 // Set the object
@@ -551,7 +551,7 @@ function deserializeObject(
551551 ) {
552552 throw new BSONError ( 'bad string length in bson' ) ;
553553 }
554- const symbol = getValidatedString ( buffer , index , index + stringSize - 1 , shouldValidateKey ) ;
554+ const symbol = ByteUtils . toUTF8 ( buffer , index , index + stringSize - 1 , shouldValidateKey ) ;
555555 value = promoteValues ? symbol : new BSONSymbol ( symbol ) ;
556556 index = index + stringSize ;
557557 } else if ( elementType === constants . BSON_DATA_TIMESTAMP ) {
@@ -587,7 +587,7 @@ function deserializeObject(
587587 ) {
588588 throw new BSONError ( 'bad string length in bson' ) ;
589589 }
590- const functionString = getValidatedString (
590+ const functionString = ByteUtils . toUTF8 (
591591 buffer ,
592592 index ,
593593 index + stringSize - 1 ,
@@ -626,7 +626,7 @@ function deserializeObject(
626626 }
627627
628628 // Javascript function
629- const functionString = getValidatedString (
629+ const functionString = ByteUtils . toUTF8 (
630630 buffer ,
631631 index ,
632632 index + stringSize - 1 ,
@@ -678,7 +678,7 @@ function deserializeObject(
678678 throw new BSONError ( 'Invalid UTF-8 string in BSON document' ) ;
679679 }
680680 }
681- const namespace = ByteUtils . toUTF8 ( buffer , index , index + stringSize - 1 ) ;
681+ const namespace = ByteUtils . toUTF8 ( buffer , index , index + stringSize - 1 , false ) ;
682682 // Update parse index position
683683 index = index + stringSize ;
684684
@@ -728,24 +728,3 @@ function deserializeObject(
728728
729729 return object ;
730730}
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