-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertFrom-YamlTagUriEscape.ps1
More file actions
90 lines (82 loc) · 3.42 KB
/
Copy pathConvertFrom-YamlTagUriEscape.ps1
File metadata and controls
90 lines (82 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function ConvertFrom-YamlTagUriEscape {
<#
.SYNOPSIS
Decodes YAML tag URI %xx escapes as UTF-8.
#>
[CmdletBinding()]
[OutputType([string])]
param (
[Parameter(Mandatory)]
[string] $Text,
[Parameter(Mandatory)]
[pscustomobject] $Mark,
[Parameter(Mandatory)]
[string] $Token,
[Parameter(Mandatory)]
[ValidateRange(1, 1048576)]
[int] $MaxLength
)
$builder = [System.Text.StringBuilder]::new()
$escapedBytes = [System.Collections.Generic.List[byte]]::new()
$utf8 = [System.Text.UTF8Encoding]::new($false, $true)
for ($index = 0; $index -lt $Text.Length; $index++) {
$character = $Text[$index]
if ($character -ne '%') {
if ($escapedBytes.Count -gt 0) {
try {
$decoded = $utf8.GetString($escapedBytes.ToArray())
} catch [System.Text.DecoderFallbackException] {
throw (New-YamlException -Start $Mark -End $Mark -ErrorId 'YamlInvalidTag' -Message (
"The tag token '$Token' contains an invalid UTF-8 escape sequence."
))
}
if ($builder.Length + $decoded.Length -gt $MaxLength) {
throw (New-YamlException -Start $Mark -End $Mark `
-ErrorId 'YamlTagLimitExceeded' -Message (
"A YAML tag exceeds the configured limit of $MaxLength characters."
))
}
[void] $builder.Append($decoded)
$escapedBytes.Clear()
}
if ($builder.Length -ge $MaxLength) {
throw (New-YamlException -Start $Mark -End $Mark `
-ErrorId 'YamlTagLimitExceeded' -Message (
"A YAML tag exceeds the configured limit of $MaxLength characters."
))
}
[void] $builder.Append($character)
continue
}
if ($index + 2 -ge $Text.Length) {
throw (New-YamlException -Start $Mark -End $Mark -ErrorId 'YamlInvalidTag' -Message (
"The tag token '$Token' contains a malformed percent escape."
))
}
$pair = '{0}{1}' -f $Text[$index + 1], $Text[$index + 2]
if ($pair -cnotmatch '^[0-9A-Fa-f]{2}$') {
throw (New-YamlException -Start $Mark -End $Mark -ErrorId 'YamlInvalidTag' -Message (
"The tag token '$Token' contains a malformed percent escape."
))
}
$escapedBytes.Add([System.Convert]::ToByte($pair, 16))
$index += 2
}
if ($escapedBytes.Count -gt 0) {
try {
$decoded = $utf8.GetString($escapedBytes.ToArray())
} catch [System.Text.DecoderFallbackException] {
throw (New-YamlException -Start $Mark -End $Mark -ErrorId 'YamlInvalidTag' -Message (
"The tag token '$Token' contains an invalid UTF-8 escape sequence."
))
}
if ($builder.Length + $decoded.Length -gt $MaxLength) {
throw (New-YamlException -Start $Mark -End $Mark `
-ErrorId 'YamlTagLimitExceeded' -Message (
"A YAML tag exceeds the configured limit of $MaxLength characters."
))
}
[void] $builder.Append($decoded)
}
$builder.ToString()
}