implement some missing methods in FileFormatUtil

This commit is contained in:
2025-12-04 02:09:37 +03:00
parent 73bb767919
commit 5c8a63f56a

View File

@@ -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);
}
}