Skip to content

Commit 849a640

Browse files
author
rstam
committed
CSHARP-694: Provide a way to be more lenient about UTF8 validity.
1 parent 42af288 commit 849a640

21 files changed

+442
-99
lines changed

MongoDB.Bson/IO/BsonBinaryReader.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public BsonBinaryReader(BsonBuffer buffer, bool disposeBuffer, BsonBinaryReaderS
6262
_buffer = buffer;
6363
_disposeBuffer = disposeBuffer;
6464
_binaryReaderSettings = settings; // already frozen by base class
65+
6566
_context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
6667
}
6768

@@ -214,7 +215,7 @@ public override BsonType ReadBsonType<TValue>(BsonTrie<TValue> bsonTrie, out boo
214215
break;
215216
case ContextType.Document:
216217
case ContextType.ScopeDocument:
217-
CurrentName = _buffer.ReadCString(bsonTrie, out found, out value);
218+
CurrentName = _buffer.ReadName(bsonTrie, out found, out value);
218219
State = BsonReaderState.Name;
219220
break;
220221
default:
@@ -376,7 +377,7 @@ public override string ReadJavaScript()
376377
if (Disposed) { ThrowObjectDisposedException(); }
377378
VerifyBsonType("ReadJavaScript", BsonType.JavaScript);
378379
State = GetNextState();
379-
return _buffer.ReadString();
380+
return _buffer.ReadString(_binaryReaderSettings.Encoding);
380381
}
381382

382383
/// <summary>
@@ -391,7 +392,7 @@ public override string ReadJavaScriptWithScope()
391392
var startPosition = _buffer.Position; // position of size field
392393
var size = ReadSize();
393394
_context = new BsonBinaryReaderContext(_context, ContextType.JavaScriptWithScope, startPosition, size);
394-
var code = _buffer.ReadString();
395+
var code = _buffer.ReadString(_binaryReaderSettings.Encoding);
395396

396397
State = BsonReaderState.ScopeDocument;
397398
return code;
@@ -506,8 +507,8 @@ public override BsonRegularExpression ReadRegularExpression()
506507
if (Disposed) { ThrowObjectDisposedException(); }
507508
VerifyBsonType("ReadRegularExpression", BsonType.RegularExpression);
508509
State = GetNextState();
509-
var pattern = _buffer.ReadCString();
510-
var options = _buffer.ReadCString();
510+
var pattern = _buffer.ReadCString(_binaryReaderSettings.Encoding);
511+
var options = _buffer.ReadCString(_binaryReaderSettings.Encoding);
511512
return new BsonRegularExpression(pattern, options);
512513
}
513514

@@ -549,7 +550,7 @@ public override string ReadString()
549550
if (Disposed) { ThrowObjectDisposedException(); }
550551
VerifyBsonType("ReadString", BsonType.String);
551552
State = GetNextState();
552-
return _buffer.ReadString();
553+
return _buffer.ReadString(_binaryReaderSettings.Encoding);
553554
}
554555

555556
/// <summary>
@@ -561,7 +562,7 @@ public override string ReadSymbol()
561562
if (Disposed) { ThrowObjectDisposedException(); }
562563
VerifyBsonType("ReadSymbol", BsonType.Symbol);
563564
State = GetNextState();
564-
return _buffer.ReadString();
565+
return _buffer.ReadString(_binaryReaderSettings.Encoding);
565566
}
566567

567568
/// <summary>

MongoDB.Bson/IO/BsonBinaryReaderSettings.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using System.Text;
1718

1819
namespace MongoDB.Bson.IO
1920
{
@@ -28,6 +29,7 @@ public class BsonBinaryReaderSettings : BsonReaderSettings
2829

2930
// private fields
3031
private bool _closeInput = false;
32+
private UTF8Encoding _encoding = new UTF8Encoding(false, true);
3133
private bool _fixOldBinarySubTypeOnInput = true;
3234
private bool _fixOldDateTimeMaxValueOnInput = true;
3335
private int _maxDocumentSize = BsonDefaults.MaxDocumentSize;
@@ -48,6 +50,7 @@ public BsonBinaryReaderSettings()
4850
/// <param name="fixOldDateTimeMaxValueOnInput">Whether to fix occurrences of the old representation of DateTime.MaxValue on input.</param>
4951
/// <param name="guidRepresentation">The representation for Guids.</param>
5052
/// <param name="maxDocumentSize">The max document size.</param>
53+
[Obsolete("Use the no-argument constructor instead and set the properties.")]
5154
public BsonBinaryReaderSettings(
5255
bool closeInput,
5356
bool fixOldBinarySubTypeOnInput,
@@ -93,6 +96,23 @@ public bool CloseInput
9396
}
9497
}
9598

99+
/// <summary>
100+
/// Gets or sets the Encoding.
101+
/// </summary>
102+
public UTF8Encoding Encoding
103+
{
104+
get { return _encoding; }
105+
set
106+
{
107+
if (value == null)
108+
{
109+
throw new ArgumentNullException("value");
110+
}
111+
if (IsFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen."); }
112+
_encoding = value;
113+
}
114+
}
115+
96116
/// <summary>
97117
/// Gets or sets whether to fix occurrences of the old binary subtype on input.
98118
/// </summary>
@@ -149,7 +169,16 @@ public int MaxDocumentSize
149169
/// <returns>A clone of the settings.</returns>
150170
protected override BsonReaderSettings CloneImplementation()
151171
{
152-
return new BsonBinaryReaderSettings(_closeInput, _fixOldBinarySubTypeOnInput, _fixOldDateTimeMaxValueOnInput, GuidRepresentation, _maxDocumentSize);
172+
var clone = new BsonBinaryReaderSettings
173+
{
174+
CloseInput = _closeInput,
175+
Encoding = _encoding,
176+
FixOldBinarySubTypeOnInput = _fixOldBinarySubTypeOnInput,
177+
FixOldDateTimeMaxValueOnInput = _fixOldDateTimeMaxValueOnInput,
178+
GuidRepresentation = GuidRepresentation,
179+
MaxDocumentSize = _maxDocumentSize
180+
};
181+
return clone;
153182
}
154183
}
155184
}

MongoDB.Bson/IO/BsonBinaryWriter.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using System.IO;
18+
using System.Text;
1819

1920
namespace MongoDB.Bson.IO
2021
{
@@ -378,7 +379,7 @@ public override void WriteJavaScript(string code)
378379

379380
_buffer.WriteByte((byte)BsonType.JavaScript);
380381
WriteNameHelper();
381-
_buffer.WriteString(code);
382+
_buffer.WriteString(_binaryWriterSettings.Encoding, code);
382383

383384
State = GetNextState();
384385
}
@@ -399,7 +400,7 @@ public override void WriteJavaScriptWithScope(string code)
399400
WriteNameHelper();
400401
_context = new BsonBinaryWriterContext(_context, ContextType.JavaScriptWithScope, _buffer.Position);
401402
_buffer.WriteInt32(0); // reserve space for size of JavaScript with scope value
402-
_buffer.WriteString(code);
403+
_buffer.WriteString(_binaryWriterSettings.Encoding, code);
403404

404405
State = BsonWriterState.ScopeDocument;
405406
}
@@ -541,8 +542,8 @@ public override void WriteRegularExpression(BsonRegularExpression regex)
541542

542543
_buffer.WriteByte((byte)BsonType.RegularExpression);
543544
WriteNameHelper();
544-
_buffer.WriteCString(regex.Pattern);
545-
_buffer.WriteCString(regex.Options);
545+
_buffer.WriteCString(_binaryWriterSettings.Encoding, regex.Pattern);
546+
_buffer.WriteCString(_binaryWriterSettings.Encoding, regex.Options);
546547

547548
State = GetNextState();
548549
}
@@ -605,7 +606,7 @@ public override void WriteString(string value)
605606

606607
_buffer.WriteByte((byte)BsonType.String);
607608
WriteNameHelper();
608-
_buffer.WriteString(value);
609+
_buffer.WriteString(_binaryWriterSettings.Encoding, value);
609610

610611
State = GetNextState();
611612
}
@@ -624,7 +625,7 @@ public override void WriteSymbol(string value)
624625

625626
_buffer.WriteByte((byte)BsonType.Symbol);
626627
WriteNameHelper();
627-
_buffer.WriteString(value);
628+
_buffer.WriteString(_binaryWriterSettings.Encoding, value);
628629

629630
State = GetNextState();
630631
}
@@ -713,14 +714,17 @@ private BsonWriterState GetNextState()
713714

714715
private void WriteNameHelper()
715716
{
717+
string name;
716718
if (_context.ContextType == ContextType.Array)
717719
{
718-
_buffer.WriteCString((_context.Index++).ToString());
720+
name = (_context.Index++).ToString();
719721
}
720722
else
721723
{
722-
_buffer.WriteCString(Name);
724+
name = Name;
723725
}
726+
727+
_buffer.WriteCString(new UTF8Encoding(false, true), name); // always use strict encoding for names
724728
}
725729
}
726730
}

MongoDB.Bson/IO/BsonBinaryWriterSettings.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using System.Text;
1718

1819
namespace MongoDB.Bson.IO
1920
{
@@ -28,6 +29,7 @@ public class BsonBinaryWriterSettings : BsonWriterSettings
2829

2930
// private fields
3031
private bool _closeOutput = false;
32+
private UTF8Encoding _encoding = new UTF8Encoding(false, true);
3133
private bool _fixOldBinarySubTypeOnOutput = true;
3234
private int _maxDocumentSize = BsonDefaults.MaxDocumentSize;
3335

@@ -46,6 +48,7 @@ public BsonBinaryWriterSettings()
4648
/// <param name="fixOldBinarySubTypeOnOutput">Whether to fix old binary data subtype on output.</param>
4749
/// <param name="guidRepresentation">The representation for Guids.</param>
4850
/// <param name="maxDocumentSize">The max document size.</param>
51+
[Obsolete("Use the no-argument constructor instead and set the properties.")]
4952
public BsonBinaryWriterSettings(
5053
bool closeOutput,
5154
bool fixOldBinarySubTypeOnOutput,
@@ -89,6 +92,23 @@ public bool CloseOutput
8992
}
9093
}
9194

95+
/// <summary>
96+
/// Gets or sets the Encoding.
97+
/// </summary>
98+
public UTF8Encoding Encoding
99+
{
100+
get { return _encoding; }
101+
set
102+
{
103+
if (value == null)
104+
{
105+
throw new ArgumentNullException("value");
106+
}
107+
if (IsFrozen) { throw new InvalidOperationException("BsonBinaryWriterSettings is frozen."); }
108+
_encoding = value;
109+
}
110+
}
111+
92112
/// <summary>
93113
/// Gets or sets whether to fix the old binary data subtype on output.
94114
/// </summary>
@@ -132,7 +152,16 @@ public int MaxDocumentSize
132152
/// <returns>A clone of the settings.</returns>
133153
protected override BsonWriterSettings CloneImplementation()
134154
{
135-
return new BsonBinaryWriterSettings(_closeOutput, _fixOldBinarySubTypeOnOutput, GuidRepresentation, _maxDocumentSize);
155+
var clone = new BsonBinaryWriterSettings
156+
{
157+
CloseOutput = _closeOutput,
158+
Encoding = _encoding,
159+
FixOldBinarySubTypeOnOutput = _fixOldBinarySubTypeOnOutput,
160+
GuidRepresentation = GuidRepresentation,
161+
MaxDocumentSize = _maxDocumentSize,
162+
MaxSerializationDepth = MaxSerializationDepth
163+
};
164+
return clone;
136165
}
137166
}
138167
}

0 commit comments

Comments
 (0)