Skip to content

Commit

Permalink
bugfix: File Encoding Detector
Browse files Browse the repository at this point in the history
  • Loading branch information
MircoBabin committed Feb 24, 2025
1 parent f925ccb commit 5cd04ea
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/BuildStamp/FileEncodingDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private void setUTF16BigEndian_WITH_BOM()

public FileEncodingDetector(BomType type)
{
switch(type)
switch (type)
{
case BomType.UTF8:
setUTF8_WITH_BOM();
Expand Down Expand Up @@ -70,19 +70,19 @@ public FileEncodingDetector(string filename)
Array.Resize(ref bomBytes, bytes_read);
}

if (bomBytes.Length == 3 && bomBytes[0] == 0xEF && bomBytes[1] == 0xBB && bomBytes[2] == 0xBF)
if (bomBytes.Length >= 3 && bomBytes[0] == 0xEF && bomBytes[1] == 0xBB && bomBytes[2] == 0xBF)
{
setUTF8_WITH_BOM();
return;
}

if (bomBytes.Length == 2 && bomBytes[0] == 0xFF && bomBytes[1] == 0xFE)
if (bomBytes.Length >= 2 && bomBytes[0] == 0xFF && bomBytes[1] == 0xFE)
{
setUTF16LittleEndian_WITH_BOM();
return;
}

if (bomBytes.Length == 2 && bomBytes[0] == 0xFE && bomBytes[1] == 0xFF)
if (bomBytes.Length >= 2 && bomBytes[0] == 0xFE && bomBytes[1] == 0xFF)
{
setUTF16BigEndian_WITH_BOM();
return;
Expand All @@ -91,6 +91,7 @@ public FileEncodingDetector(string filename)
/* If a Byte Order Mark (BOM) is not present, assume UTF-8 (is also ASCII) encoding.
* This will be mostly valid for a programming source file.
*/
setUTF8_WITHOUT_BOM(); }
setUTF8_WITHOUT_BOM();
}
}
}

0 comments on commit 5cd04ea

Please sign in to comment.