private static IReadOnlyList <byte> ConvertHexToBinary(IReadOnlyList <byte> bytes) { var result = new List <byte>(bytes.Count / 2); var last = '\0'; var offset = 0; for (var i = 0; i < bytes.Count; i++) { var c = (char)bytes[i]; if (!ReadHelper.IsHex(c)) { // TODO: do I need to assert this must be whitespace? continue; } if (offset == 1) { result.Add(HexToken.Convert(last, c)); offset = 0; } else { offset++; } last = c; } return(result); }
/// <summary> /// To distinguish between binary and hex the first 4 bytes (of the ciphertext) for hex must obey these restrictions: /// The first byte must not be whitespace. /// One of the first four ciphertext bytes must not be an ASCII hex character. /// </summary> private static bool IsBinary(IReadOnlyList <byte> bytes) { if (bytes.Count < 4) { return(true); } if (ReadHelper.IsWhitespace(bytes[0])) { return(true); } for (var i = 1; i < 4; i++) { var b = bytes[i]; if (!ReadHelper.IsHex(b)) { return(true); } } return(false); }
public bool TryTokenize(byte currentByte, IInputBytes inputBytes, out IToken token) { token = null; if (currentByte != '/') { return(false); } var bytes = new List <byte>(); bool escapeActive = false; int postEscapeRead = 0; var escapedChars = new char[2]; while (inputBytes.MoveNext()) { var b = inputBytes.CurrentByte; if (b == '#') { escapeActive = true; } else if (escapeActive) { if (ReadHelper.IsHex((char)b)) { escapedChars[postEscapeRead] = (char)b; postEscapeRead++; if (postEscapeRead == 2) { var hex = new string(escapedChars); var characterToWrite = (byte)Convert.ToInt32(hex, 16); bytes.Add(characterToWrite); escapeActive = false; postEscapeRead = 0; } } else { bytes.Add((byte)'#'); if (postEscapeRead == 1) { bytes.Add((byte)escapedChars[0]); } if (ReadHelper.IsEndOfName(b)) { break; } if (b == '#') { // Make it clear what's going on, we read something like #m#AE // ReSharper disable once RedundantAssignment escapeActive = true; postEscapeRead = 0; continue; } bytes.Add(b); escapeActive = false; postEscapeRead = 0; } } else if (ReadHelper.IsEndOfName(b)) { break; } else { bytes.Add(b); } } byte[] byteArray = bytes.ToArray(); var str = ReadHelper.IsValidUtf8(byteArray) ? Encoding.UTF8.GetString(byteArray) : Encoding.GetEncoding("windows-1252").GetString(byteArray); token = NameToken.Create(str); return(true); }