-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add callback-based Encode to AsnWriter #106728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Note regarding the |
1 similar comment
|
Note regarding the |
|
@bartonjs thought: someone could write, reset, clear, etc. the AsnWriter in its own Basically, change our encode impl to try
{
_encoding = true;
ReadOnlySpan<byte> encoded = EncodeAsSpan();
return encodeCallback(encoded);
}
finally
{
_encoding = false;
}And check that flag is false in all writing functions. Is that useful or am I over thinking it? |
|
Any time that we're willing to release the data from Encode, it's already at a place it will never change (except Reset()). Any other calls would just be appending new data beyond where the span is pointing, so it wouldn't have any major surprises. writer.Reset();
writer.WriteNull();
writer.Encode((writer, span) => writer.WriteEncodedValue(span));
writer.Encode();// now returns [ 0x05, 0x00, 0x05, 0x00 ]I can't come up with a case where it would result in overwriting existing data. The biggest surprise I can come up with is if the callback does PushSequence/SetOf/OctetString, since it would move the object from "encodable" to "not encodable". If you want to block it, that's fine, but instead of reading |
If the write causes the buffer to resize, the span will be pointing at the old buffer, which has been zeroed. runtime/src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/AsnWriter.cs Lines 288 to 291 in 4e8892e
|
|
Ah, so in that case writer.Encode(
(writer, span) =>
{
writer.WriteNull();
writer.WriteEncodedObject(span);
});would work 99% of the time, but would try writing all zeros (which should fail unless it happens to be Okay, yeah, may as well block it. |
This adds a new callback-based mechanism for
AsnWriter, and uses it where appropriate through the rest of the libraries.Closes #75759