Browse Source

Bug 1993: When installing an extension from a file, it is always saved in UTF-8 with BOM, disregarding the original encoding

https://winscp.net/tracker/1993

+ When installing from a URL, not saving the extension with BOM (to allow downloading extensions in languages that do not support it, like batch files)

(cherry picked from commit fb7bad0af225b7276c428ce3887bb198cc3bddfc)

Source commit: 8856e5ac8c6e543eac588bc7bef7d7792a0be2c7
Martin Prikryl 4 years ago
parent
commit
9a59d27b95
1 changed files with 13 additions and 1 deletions
  1. 13 1
      source/forms/Preferences.cpp

+ 13 - 1
source/forms/Preferences.cpp

@@ -3,6 +3,7 @@
 #pragma hdrstop
 
 #include <StrUtils.hpp>
+#include <System.IOUtils.hpp>
 #include <Common.h>
 #include <math.h>
 
@@ -2647,6 +2648,7 @@ void __fastcall TPreferencesDialog::AddExtension()
     bool Latest;
     UnicodeString FileName;
     UnicodeString ExtensionPath;
+    UnicodeString LinesSourcePath;
     std::unique_ptr<TStringList> Lines(new TStringList());
     std::unique_ptr<TCustomCommandType> CustomCommand;
 
@@ -2716,6 +2718,7 @@ void __fastcall TPreferencesDialog::AddExtension()
           ProvisionaryId = WinConfiguration->GetProvisionaryExtensionId(FileName);
         }
 
+        LinesSourcePath = Path;
         LoadScriptFromFile(Path, Lines.get());
       }
 
@@ -2787,7 +2790,16 @@ void __fastcall TPreferencesDialog::AddExtension()
           ExtensionPath = WinConfiguration->UniqueExtensionName(LeftStr(OriginalExtensionPath, P - 1), Counter) + RightStr(OriginalExtensionPath, OriginalExtensionPath.Length() - P + 1);
         }
 
-        Lines->SaveToFile(ApiPath(ExtensionPath));
+        if (!LinesSourcePath.IsEmpty())
+        {
+          // Copy as is, exactly preserving the file encoding
+          TFile::Copy(ApiPath(LinesSourcePath), ApiPath(ExtensionPath));
+        }
+        else
+        {
+          Lines->WriteBOM = false;
+          Lines->SaveToFile(ApiPath(ExtensionPath));
+        }
 
         FAddedExtensions->Add(ExtensionPath);
       }