mirror of
https://github.com/KeyKeeperApp/KeyKeeper.git
synced 2026-05-06 16:37:00 +03:00
implement some missing methods in FileFormatUtil
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Buffers.Binary;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@@ -22,6 +23,28 @@ static class FileFormatUtil
|
|||||||
return size;
|
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)
|
public static int WriteU8TaggedString(Stream str, string s)
|
||||||
{
|
{
|
||||||
byte[] b = Encoding.UTF8.GetBytes(s);
|
byte[] b = Encoding.UTF8.GetBytes(s);
|
||||||
@@ -42,4 +65,16 @@ static class FileFormatUtil
|
|||||||
str.Write(b);
|
str.Write(b);
|
||||||
return b.Length + 2;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user