Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dotNET Platform
runtime
提交
7e95181f
R
runtime
项目概览
dotNET Platform
/
runtime
大约 1 年 前同步成功
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
runtime
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
7e95181f
编写于
8月 09, 2022
作者:
K
Kevin Jones
提交者:
GitHub
8月 09, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement initial capacity for AsnWriter
上级
de03d8a8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
131 addition
and
0 deletion
+131
-0
src/libraries/System.Formats.Asn1/ref/System.Formats.Asn1.cs
src/libraries/System.Formats.Asn1/ref/System.Formats.Asn1.cs
+1
-0
src/libraries/System.Formats.Asn1/src/Resources/Strings.resx
src/libraries/System.Formats.Asn1/src/Resources/Strings.resx
+3
-0
src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/AsnWriter.cs
.../System.Formats.Asn1/src/System/Formats/Asn1/AsnWriter.cs
+25
-0
src/libraries/System.Formats.Asn1/tests/Writer/SimpleWriterTests.cs
...ies/System.Formats.Asn1/tests/Writer/SimpleWriterTests.cs
+102
-0
未找到文件。
src/libraries/System.Formats.Asn1/ref/System.Formats.Asn1.cs
浏览文件 @
7e95181f
...
...
@@ -144,6 +144,7 @@ public partial struct AsnReaderOptions
public
sealed
partial
class
AsnWriter
{
public
AsnWriter
(
System
.
Formats
.
Asn1
.
AsnEncodingRules
ruleSet
)
{
}
public
AsnWriter
(
System
.
Formats
.
Asn1
.
AsnEncodingRules
ruleSet
,
int
initialCapacity
)
{
}
public
System
.
Formats
.
Asn1
.
AsnEncodingRules
RuleSet
{
get
{
throw
null
;
}
}
public
void
CopyTo
(
System
.
Formats
.
Asn1
.
AsnWriter
destination
)
{
}
public
byte
[]
Encode
()
{
throw
null
;
}
...
...
src/libraries/System.Formats.Asn1/src/Resources/Strings.resx
浏览文件 @
7e95181f
...
...
@@ -99,6 +99,9 @@
<data name="Argument_WriteEncodedValue_OneValueAtATime" xml:space="preserve">
<value>The input to WriteEncodedValue must represent a single encoded value with no trailing data.</value>
</data>
<data name="ArgumentOutOfRange_NeedNonNegNum" xml:space="preserve">
<value>Non-negative number required.</value>
</data>
<data name="AsnWriter_EncodeUnbalancedStack" xml:space="preserve">
<value>Encode cannot be called while a Sequence, Set-Of, or Octet String is still open.</value>
</data>
...
...
src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/AsnWriter.cs
浏览文件 @
7e95181f
...
...
@@ -48,6 +48,31 @@ public AsnWriter(AsnEncodingRules ruleSet)
RuleSet
=
ruleSet
;
}
/// <summary>
/// Create a new <see cref="AsnWriter"/> with a given set of encoding rules and an initial capacity.
/// </summary>
/// <param name="ruleSet">The encoding constraints for the writer.</param>
/// <param name="initialCapacity">The minimum capacity with which to initialize the underlying buffer.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="ruleSet"/> is not defined.</para>
/// <para> -or- </para>
/// <para><paramref name="initialCapacity"/> is a negative number.</para>
/// </exception>
/// <remarks>
/// Specifying <paramref name="initialCapacity" /> with a value of zero behaves as if no initial capacity were
/// specified.
/// </remarks>
public
AsnWriter
(
AsnEncodingRules
ruleSet
,
int
initialCapacity
)
:
this
(
ruleSet
)
{
if
(
initialCapacity
<
0
)
throw
new
ArgumentOutOfRangeException
(
nameof
(
initialCapacity
),
SR
.
ArgumentOutOfRange_NeedNonNegNum
);
if
(
initialCapacity
>
0
)
{
_buffer
=
new
byte
[
initialCapacity
];
}
}
/// <summary>
/// Reset the writer to have no data, without releasing resources.
/// </summary>
...
...
src/libraries/System.Formats.Asn1/tests/Writer/SimpleWriterTests.cs
浏览文件 @
7e95181f
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using
System.Reflection
;
using
Xunit
;
namespace
System.Formats.Asn1.Tests.Writer
...
...
@@ -17,6 +18,20 @@ public static void ValidateRuleSet(int value)
AssertExtensions
.
Throws
<
ArgumentOutOfRangeException
>(
"ruleSet"
,
()
=>
new
AsnWriter
((
AsnEncodingRules
)
value
));
AssertExtensions
.
Throws
<
ArgumentOutOfRangeException
>(
"ruleSet"
,
()
=>
new
AsnWriter
((
AsnEncodingRules
)
value
,
initialCapacity
:
1000
));
}
[
Theory
]
[
InlineData
(-
1
)]
[
InlineData
(
int
.
MinValue
)]
public
static
void
ValidateInitialCapacity
(
int
initialCapacity
)
{
AssertExtensions
.
Throws
<
ArgumentOutOfRangeException
>(
"initialCapacity"
,
()
=>
new
AsnWriter
(
AsnEncodingRules
.
DER
,
initialCapacity
));
}
[
Theory
]
...
...
@@ -138,5 +153,92 @@ public static void CopyTo_Null(AsnEncodingRules ruleSet)
"destination"
,
()
=>
writer
.
CopyTo
(
null
));
}
[
Fact
]
public
static
void
InitialCapacity_ExactCapacity
()
{
ReadOnlySpan
<
byte
>
value
=
new
byte
[]
{
0x04
,
0x06
,
0x01
,
0x02
,
0x03
,
0x04
,
0x05
,
0x06
};
AsnWriter
writer
=
new
AsnWriter
(
AsnEncodingRules
.
DER
,
initialCapacity
:
8
);
writer
.
WriteEncodedValue
(
value
);
byte
[]?
buffer
=
PeekRawBuffer
(
writer
);
Assert
.
Equal
(
8
,
buffer
?.
Length
);
byte
[]
encoded
=
writer
.
Encode
();
AssertExtensions
.
SequenceEqual
(
value
,
encoded
);
writer
.
Reset
();
buffer
=
PeekRawBuffer
(
writer
);
Assert
.
Equal
(
8
,
buffer
?.
Length
);
writer
.
WriteEncodedValue
(
value
);
buffer
=
PeekRawBuffer
(
writer
);
Assert
.
Equal
(
8
,
buffer
?.
Length
);
}
[
Fact
]
public
static
void
InitialCapacity_ZeroHasNoInitialCapacity
()
{
AsnWriter
writer
=
new
AsnWriter
(
AsnEncodingRules
.
DER
,
initialCapacity
:
0
);
byte
[]?
buffer
=
PeekRawBuffer
(
writer
);
Assert
.
Null
(
buffer
);
}
[
Fact
]
public
static
void
InitialCapacity_UnderCapacity
()
{
ReadOnlySpan
<
byte
>
value
=
new
byte
[]
{
0x04
,
0x01
,
0x01
};
AsnWriter
writer
=
new
AsnWriter
(
AsnEncodingRules
.
DER
,
initialCapacity
:
8
);
writer
.
WriteEncodedValue
(
value
);
byte
[]?
buffer
=
PeekRawBuffer
(
writer
);
Assert
.
Equal
(
8
,
buffer
?.
Length
);
byte
[]
encoded
=
writer
.
Encode
();
AssertExtensions
.
SequenceEqual
(
value
,
encoded
);
writer
.
Reset
();
buffer
=
PeekRawBuffer
(
writer
);
Assert
.
Equal
(
8
,
buffer
?.
Length
);
}
[
Fact
]
public
static
void
InitialCapacity_ExceedCapacity
()
{
ReadOnlySpan
<
byte
>
value
=
new
byte
[]
{
0x04
,
0x07
,
0x01
,
0x02
,
0x03
,
0x04
,
0x05
,
0x06
,
0x07
};
AsnWriter
writer
=
new
AsnWriter
(
AsnEncodingRules
.
DER
,
initialCapacity
:
8
);
writer
.
WriteEncodedValue
(
value
);
byte
[]?
buffer
=
PeekRawBuffer
(
writer
);
Assert
.
Equal
(
1024
,
buffer
?.
Length
);
}
[
Fact
]
public
static
void
InitialCapacity_ResizeBlockAligns
()
{
ReadOnlySpan
<
byte
>
value
=
new
byte
[]
{
0x04
,
0x06
,
0x01
,
0x02
,
0x03
,
0x04
,
0x05
,
0x06
};
ReadOnlySpan
<
byte
>
valueLarge
=
new
byte
[]
{
0x04
,
0x07
,
0x01
,
0x02
,
0x03
,
0x04
,
0x05
,
0x06
,
0x07
};
AsnWriter
writer
=
new
AsnWriter
(
AsnEncodingRules
.
DER
,
initialCapacity
:
8
);
writer
.
WriteEncodedValue
(
value
);
byte
[]?
buffer
=
PeekRawBuffer
(
writer
);
Assert
.
Equal
(
8
,
buffer
?.
Length
);
writer
.
Reset
();
buffer
=
PeekRawBuffer
(
writer
);
Assert
.
Equal
(
8
,
buffer
?.
Length
);
writer
.
WriteEncodedValue
(
valueLarge
);
buffer
=
PeekRawBuffer
(
writer
);
Assert
.
Equal
(
1024
,
buffer
?.
Length
);
}
private
static
byte
[]?
PeekRawBuffer
(
AsnWriter
writer
)
{
FieldInfo
bufField
=
typeof
(
AsnWriter
).
GetField
(
"_buffer"
,
BindingFlags
.
Instance
|
BindingFlags
.
NonPublic
);
return
(
byte
[]?)
bufField
.
GetValue
(
writer
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录