Skip to content

Commit 35d61ef

Browse files
committed
Adds support to check encoding of files
1 parent 5210643 commit 35d61ef

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
2121

2222
* [#732](https://github.com/Icinga/icinga-powershell-framework/pull/732) Adds support for TLS 1.3 and improves startup response
2323
* [#735](https://github.com/Icinga/icinga-powershell-framework/pull/735) Adds support to provide occuring problem event id's for the Eventlog and corresponding acknowledgement id's, providing an indicator if certain issues are resolved or still present
24+
* [#739](https://github.com/Icinga/icinga-powershell-framework/pull/739) Adds support to check the encoding of files to ensure we can properly load them and throw errors for unsupported encoding
2425
* [#740](https://github.com/Icinga/icinga-powershell-framework/pull/740) Adds new command `Invoke-IcingaForWindowsRESTApi` for easier API communication
2526
* [#742](https://github.com/Icinga/icinga-powershell-framework/pull/742) Adds support for the CPU provider to limit the CPU usage to 100% for each thread
2627

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<#
2+
.SYNOPSIS
3+
Determines the encoding of a file.
4+
5+
.DESCRIPTION
6+
The Get-FileEncoding function determines the encoding of a file by examining the file's byte order mark (BOM) or by checking if the file is ASCII or UTF8 without BOM.
7+
8+
.PARAMETER Path
9+
Specifies the path of the file to check.
10+
11+
.EXAMPLE
12+
Get-FileEncoding -Path "C:\path\to\file.txt"
13+
Returns the encoding of the specified file.
14+
15+
.OUTPUTS
16+
System.String
17+
The function returns a string representing the encoding of the file. Possible values are:
18+
- UTF8-BOM: UTF-8 encoding with a byte order mark (BOM).
19+
- Unicode: UTF-16 encoding (little-endian).
20+
- BigEndianUnicode: UTF-16 encoding (big-endian).
21+
- UTF7: UTF-7 encoding.
22+
- UTF32: UTF-32 encoding.
23+
- UTF8: UTF-8 encoding without a byte order mark (BOM).
24+
- ASCII: ASCII encoding.
25+
26+
.NOTES
27+
This function requires PowerShell version 3.0 or later.
28+
#>
29+
30+
function Get-FileEncoding()
31+
{
32+
param (
33+
[string]$Path = ''
34+
);
35+
36+
if ([string]::IsNullOrEmpty($Path) -Or (Test-Path -Path $Path) -eq $FALSE) {
37+
Write-IcingaConsoleError 'The specified file "{0}" was not found' -Objects $Path;
38+
return $null;
39+
}
40+
41+
$Bytes = Get-Content -Encoding Byte -ReadCount 4 -TotalCount 4 -Path $Path;
42+
43+
if ($Bytes[0] -eq 0xef -and $Bytes[1] -eq 0xbb -and $Bytes[2] -eq 0xbf) {
44+
return 'UTF8-BOM';
45+
} elseif ($Bytes[0] -eq 0xff -and $Bytes[1] -eq 0xfe) {
46+
return 'Unicode';
47+
} elseif ($Bytes[0] -eq 0xfe -and $Bytes[1] -eq 0xff) {
48+
return 'BigEndianUnicode';
49+
} elseif ($Bytes[0] -eq 0x2b -and $Bytes[1] -eq 0x2f -and $Bytes[2] -eq 0x76) {
50+
return 'UTF7';
51+
} elseif ($Bytes[0] -eq 0xff -and $Bytes[1] -eq 0xfe -and $Bytes[2] -eq 0x00 -and $Bytes[3] -eq 0x00) {
52+
return 'UTF32';
53+
} else {
54+
# Check if the file is ASCII or UTF8 without BOM
55+
$Content = Get-Content -Encoding String -Path $Path;
56+
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($content);
57+
58+
# Check each byte to see if it's outside the ASCII range
59+
foreach ($byte in $Bytes) {
60+
if ($byte -gt 127) {
61+
return 'UTF8';
62+
}
63+
}
64+
}
65+
66+
# This is the default encoding, as UTF8 without BOM could be valid ASCII
67+
return 'ASCII';
68+
}

lib/core/tools/Read-IcingaFileSecure.psm1

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@ function Read-IcingaFileSecure()
3939
[System.IO.FileShare]::Read
4040
);
4141

42-
$ReadArray = New-Object Byte[] $FileStream.Length;
43-
$UTF8Encoding = New-Object System.Text.UTF8Encoding $TRUE;
44-
$FileContent = '';
42+
$ReadArray = New-Object Byte[] $FileStream.Length;
43+
[bool]$UTF8BOM = $FALSE;
44+
45+
if ((Get-FileEncoding -Path $File) -eq 'UTF8-BOM') {
46+
$UTF8BOM = $TRUE;
47+
}
48+
49+
$UTF8Encoding = New-Object System.Text.UTF8Encoding $UTF8BOM;
50+
$FileContent = '';
4551

4652
while ($FileStream.Read($ReadArray, 0 , $ReadArray.Length)) {
4753
$FileContent = [System.String]::Concat($FileContent, $UTF8Encoding.GetString($ReadArray));

0 commit comments

Comments
 (0)