diff --git a/src/KeyKeeper/PasswordStore/FileFormatUtil.cs b/src/KeyKeeper/PasswordStore/FileFormatUtil.cs index c98ccaf..18ce17c 100644 --- a/src/KeyKeeper/PasswordStore/FileFormatUtil.cs +++ b/src/KeyKeeper/PasswordStore/FileFormatUtil.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers.Binary; using System.IO; using System.Text; @@ -22,6 +23,28 @@ static class FileFormatUtil return size; } + public static ulong ReadVarUint16(Stream str) + { + ulong number = 0; + int i = 0; + while (true) + { + int b = str.ReadByte(); + if (b == -1) + throw PassStoreFileException.UnexpectedEndOfFile; + number |= (ulong)b << i; + i += 8; + b = str.ReadByte(); + if (b == -1) + throw PassStoreFileException.UnexpectedEndOfFile; + number |= (ulong)(b & 0x7f) << i; + if ((b & 0x80) == 0) + break; + i += 7; + } + return number; + } + public static int WriteU8TaggedString(Stream str, string s) { byte[] b = Encoding.UTF8.GetBytes(s); @@ -42,4 +65,16 @@ static class FileFormatUtil str.Write(b); return b.Length + 2; } + + public static string ReadU16TaggedString(Stream str) + { + byte[] lenBytes = new byte[2]; + if (str.Read(lenBytes) < 2) + throw PassStoreFileException.UnexpectedEndOfFile; + int len = BinaryPrimitives.ReadUInt16LittleEndian(lenBytes); + byte[] s = new byte[len]; + if (str.Read(s) < len) + throw PassStoreFileException.UnexpectedEndOfFile; + return Encoding.UTF8.GetString(s); + } } \ No newline at end of file