Skip to content

Commit

Permalink
Use appropriate encoding for each file type
Browse files Browse the repository at this point in the history
  • Loading branch information
cschneegans committed Apr 1, 2024
1 parent 7583e1d commit 94679cb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
9 changes: 6 additions & 3 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void Append(IEnumerable<string> values)
}
}

class CommandBuilder
static class CommandBuilder
{
public static string Raw(string command)
{
Expand Down Expand Up @@ -251,9 +251,12 @@ public static IEnumerable<string> WriteToFile(string path, IEnumerable<string> l
return lines.SelectMany(line => WriteToFile(path, line));
}

public static IEnumerable<string> SafeWriteToFile(string path, string content)
public static IEnumerable<string> SafeWriteToFile(string path, string content, Encoding encoding)
{
byte[] bytes = Encoding.UTF8.GetBytes(content);
byte[] bytes = Enumerable.Concat(
encoding.GetPreamble(),
encoding.GetBytes(content)
).ToArray();

int chunkSize = 256 - 70;
foreach (string base64 in Convert.ToBase64String(bytes).Chunk(chunkSize).Select(chars => new string(chars)))
Expand Down
31 changes: 30 additions & 1 deletion modifier/Script.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Schneegans.Unattend;

Expand All @@ -24,6 +25,25 @@ public static IEnumerable<ScriptType> GetAllowedTypes(this ScriptPhase phase)
_ => Enum.GetValues<ScriptType>(),
};
}

public static string FileExtension(this ScriptType type)
{
return '.' + type.ToString().ToLowerInvariant();
}

public static Encoding PreferredEncoding(this ScriptType type)
{
UnicodeEncoding utf16WithBom = new(bigEndian: false, byteOrderMark: true);
return type switch
{
ScriptType.Ps1 => Encoding.UTF8,
ScriptType.Cmd => Encoding.Latin1,
ScriptType.Reg => utf16WithBom,
ScriptType.Vbs => utf16WithBom,
ScriptType.Js => utf16WithBom,
_ => throw new NotImplementedException(),
};
}
}

public record class ScriptSettings(
Expand All @@ -39,6 +59,15 @@ public Script(string content, ScriptPhase phase, ScriptType type)
throw new ConfigurationException($"Scripts in phase '{phase}' must not have type '{type}'.");
}

if (phase == ScriptPhase.DefaultUser && type == ScriptType.Reg && !string.IsNullOrWhiteSpace(content))
{
string prefix = @"[HKEY_USERS\DefaultUser\";
if (!content.Contains(prefix, StringComparison.OrdinalIgnoreCase))
{
throw new ConfigurationException($"{type.FileExtension()} script '{content}' does not contain required key prefix '{prefix}'.");
}
}

Content = content;
Phase = phase;
Type = type;
Expand Down Expand Up @@ -111,7 +140,7 @@ static string Clean(Script script)

var appender = new CommandAppender(Document, NamespaceManager, CommandConfig.Specialize);
appender.Append(
CommandBuilder.SafeWriteToFile(scriptId.FullName, Clean(script))
CommandBuilder.SafeWriteToFile(scriptId.FullName, Clean(script), script.Type.PreferredEncoding())
);
}

Expand Down

0 comments on commit 94679cb

Please sign in to comment.