sanitize MIDI parser (#38806)
Co-authored-by: Pieter-Jan Briers <pieterjan.briers@gmail.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
using Robust.Shared.Audio.Midi;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
@@ -207,6 +208,18 @@ public sealed class MidiTrack
|
||||
ProgramName = Truncate(ProgramName, limit);
|
||||
}
|
||||
|
||||
public void SanitizeFields()
|
||||
{
|
||||
if (InstrumentName != null)
|
||||
InstrumentName = Sanitize(InstrumentName);
|
||||
|
||||
if (TrackName != null)
|
||||
TrackName = Sanitize(TrackName);
|
||||
|
||||
if (ProgramName != null)
|
||||
ProgramName = Sanitize(ProgramName);
|
||||
}
|
||||
|
||||
private const string Postfix = "…";
|
||||
// TODO: Make a general method to use in RT? idk if we have that.
|
||||
private string Truncate(string input, int limit)
|
||||
@@ -218,4 +231,17 @@ public sealed class MidiTrack
|
||||
|
||||
return input.Substring(0, truncatedLength) + Postfix;
|
||||
}
|
||||
|
||||
private static string Sanitize(string input)
|
||||
{
|
||||
var sanitized = new StringBuilder(input.Length);
|
||||
|
||||
foreach (char c in input)
|
||||
{
|
||||
if (!char.IsControl(c) && c <= 127) // no control characters, only ASCII
|
||||
sanitized.Append(c);
|
||||
}
|
||||
|
||||
return sanitized.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user