This repository has been archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Encoding.psm1
173 lines (143 loc) · 4.83 KB
/
Encoding.psm1
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<#
.SYNOPSIS
Returns the name of the encoding associated with a file's BOM
.DESCRIPTION
Reads the Byte Order Marking of a file to determine the file's encoding.
.PARAMETER Path
The path to the text file being read
.EXAMPLE
$isUnicode = (Get-Bom ".\MyFile.txt") -like "utf16*"
.NOTES
Reads the minimum number of characters from the file required to classify. Only supports unicode types.
#>
function Get-Bom {
Param(
[Parameter(Mandatory)]
[ValidateScript( {Test-Path $_ -PathType Leaf} )]
[string]$Path
)
# unicode formats and their byte order mark values
$boms = @{
"utf8" = (239, 187, 191)
"utf16be" = (254, 255)
"utf16le" = (255, 254)
"utf32be" = (0, 0, 254, 255)
"utf32le" = (255, 254, 0, 0)
}
# read the minimum required bytes into $buffer
try {
$bufferSize = ($boms.Keys | % {$boms[$_].Count} | Measure -Maximum).Maximum
$fileStream = [IO.File]::Open((Resolve-Path $Path), [IO.FileMode]::Open)
$binaryReader = [IO.BinaryReader]::new($fileStream)
$buffer = [byte[]]::new($bufferSize)
$binaryReader.Read($buffer, 0, $bufferSize) | Out-Null
} catch {
throw $_
} finally {
$binaryReader.Close()
}
# compare BOMs to $buffer
$matchedBoms = $boms.Keys | ? {
$bomBytes = $boms[$_]
$diffs = 1..($bomBytes.Count - 1) | ? {$buffer[$_] -ne $bomBytes[$_]}
return -not $diffs
}
# return the longest (most specific) matching BOM
return $matchedBoms | Sort {$boms[$_].Count} -Descending | Select -First 1
}
<#
.SYNOPSIS
Returns a boolean indicating whether the file can be parsed as the specified encoding.
.DESCRIPTION
Reads the file and ensures each character conforms to the specified encoding
.PARAMETER Path
The path to the file to test
.PARAMETER Encoding
The name of the encoding to validate
.EXAMPLE
if (-not (Test-Parseable ".\myfile.txt", "ascii")) { throw "Invalid Encoding" }
.NOTES
General notes
#>
function Test-Parseable {
Param(
[Parameter(Mandatory)]
[ValidateScript( {Test-Path $_ -PathType Leaf} )]
[string]$Path,
[Parameter(Mandatory)]
[ValidateSet("ascii", "utf8", "utf16be", "utf16le", "utf32be", "utf32le")]
[string]$Encoding
)
$Path = Resolve-Path $Path
# map encoding identifier to .NET Encoding subclass
$encodings = @{
"utf8" = [Text.UTF8Encoding]::new($true, $true)
"utf16be" = [Text.UnicodeEncoding]::new($true, $true, $true)
"utf16le" = [Text.UnicodeEncoding]::new($false, $true, $true)
"utf32be" = [Text.UTF32Encoding]::new($true, $true, $true)
"utf32le" = [Text.UTF32Encoding]::new($false, $true, $true)
}
if ($Encoding -eq "ascii") {
# parse the file manually, testing if values are in range
try {
$fileStream = [IO.File]::Open($Path, [IO.FileMode]::Open)
$binaryReader = [IO.BinaryReader]::new($fileStream)
while ($binaryReader.PeekChar() -ne -1) {
if ($binaryReader.ReadByte() -gt 127) {
return $false
}
}
return $true
} catch {
throw $_
} finally {
$binaryReader.Close()
}
} else {
# parse the file using .NET parser, throwing an error on invalid chars
try {
$streamReader = [IO.StreamReader]::new($Path, $encodings[$Encoding])
$streamReader.ReadToEnd() | Out-Null
return $true
} catch {
return $false
} finally {
$streamReader.Close()
}
}
}
<#
.SYNOPSIS
Tests that the file can be read without erroring using the specified encoding.
.DESCRIPTION
Tests that the BOM, Encoding, and characters do not conflict. If no encoding is specified, infers the encoding from the BOM. If no BOM is in the file and no Encoding is specified, assumes "ascii" encoding.
.PARAMETER Path
The path of the file being tested
.PARAMETER Encoding
Forces interpretation of the file as the specified encoding instead of only assuming the encoding from BOM.
.EXAMPLE
Test-Encoding ".\myFile.txt" -Encoding "utf8"
#>
function Test-Encoding {
Param(
[Parameter(Mandatory)]
[ValidateScript( {Test-Path $_ -PathType Leaf} )]
[string]$Path,
[ValidateSet("ascii", "utf8", "utf16be", "utf16le", "utf32be", "utf32le")]
[string]$Encoding
)
$bom = Get-Bom $Path
if ($bom) {
if ($Encoding) {
return $bom -eq $Encoding -and (Test-Parseable $Path $Encoding)
} else {
return Test-Parseable $Path $bom
}
} else {
if ($Encoding) {
return $Encoding -in ("ascii", "utf8") -and (Test-Parseable $Path $Encoding)
} else {
return Test-Parseable $Path "ascii"
}
}
}