Skip to content

Commit

Permalink
Use byte values to determine BOM
Browse files Browse the repository at this point in the history
In order to reliably detect the BOM headers in files, we need to read the actual bytes instead of comparing to a string constant. The constant works fine on most systems, but notably not on those using the UTF-8 (beta) option in Windows 10. #378
  • Loading branch information
joyfullservice committed Feb 11, 2023
1 parent 1a4ad27 commit d4b0658
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Version Control.accda.src/dbs-properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"Type": 10
},
"AppVersion": {
"Value": "4.0.6",
"Value": "4.0.7",
"Type": 10
},
"Auto Compact": {
Expand Down
1 change: 0 additions & 1 deletion Version Control.accda.src/modules/modConstants.bas
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Public Const JSON_WHITESPACE As Integer = 2

' BOM characters for UTF-8/UTF-16 files
Public Const UTF8_BOM As String = ""
Public Const UCS2_BOM As String = "ÿþ"

' Default hashing algorithm
Public Const DefaultHashAlgorithm As String = "SHA256"
Expand Down
24 changes: 9 additions & 15 deletions Version Control.accda.src/modules/modEncoding.bas
Original file line number Diff line number Diff line change
Expand Up @@ -235,31 +235,25 @@ End Sub
'---------------------------------------------------------------------------------------
'
Public Function HasUtf8Bom(strFilePath As String) As Boolean
HasUtf8Bom = FileHasBom(strFilePath, UTF8_BOM)
Dim bte() As Byte
bte = GetFileBytes(strFilePath, 3)
HasUtf8Bom = ((bte(0) = &HEF) And (bte(1) = &HBB) And (bte(2) = &HBF))
End Function


'---------------------------------------------------------------------------------------
' Procedure : HasUcs2Bom
' Author : Adam Waller
' Date : 8/1/2020
' Purpose : Returns true if the file begins with
' Purpose : Returns true if the file begins with the bytes `FF FE` (ÿþ)
' : Note that these must be read as bytes, not compared to a string value
' : if the system is using the UTF-8 (beta) option in Windows 10. See #378
'---------------------------------------------------------------------------------------
'
Public Function HasUcs2Bom(strFilePath As String) As Boolean
HasUcs2Bom = FileHasBom(strFilePath, UCS2_BOM)
End Function


'---------------------------------------------------------------------------------------
' Procedure : FileHasBom
' Author : Adam Waller
' Date : 8/1/2020
' Purpose : Check for the specified BOM by reading the first few bytes in the file.
'---------------------------------------------------------------------------------------
'
Private Function FileHasBom(strFilePath As String, strBom As String) As Boolean
FileHasBom = (strBom = StrConv(GetFileBytes(strFilePath, Len(strBom)), vbUnicode))
Dim bte() As Byte
bte = GetFileBytes(strFilePath, 2)
HasUcs2Bom = ((bte(0) = &HFF) And (bte(1) = &HFE))
End Function


Expand Down

0 comments on commit d4b0658

Please sign in to comment.