| |
| |
| using SlobViewer.Common; |
| using System; |
| using System.Collections.Generic; |
| using System.Globalization; |
| using System.IO; |
| using System.Linq; |
|
|
| namespace SlobViewer.Slob |
| { |
| public class SlobReaderWriter |
| { |
| private readonly string _fileName; |
| private System.IO.FileStream _stream; |
| private readonly byte[] _buffer = new byte[65536]; |
|
|
| public const long SupposedMagic = 0x212d31534c4f421f; |
|
|
| public const ulong SupposedVersionHi = 4862287655031097909UL; |
| public const ulong SupposedVersionLo = 11718617693102973101UL; |
|
|
|
|
| |
| |
| |
| |
| public SlobReaderWriter(string fileName) |
| { |
| _fileName = fileName; |
| } |
|
|
| |
| |
| |
| |
| public IWordDictionary Read() |
| { |
|
|
| |
| |
|
|
| using (_stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) |
| { |
| ulong magic = BigEndianBitConverter.ToUInt64(_stream, _buffer); |
| if (magic != SupposedMagic) |
| { |
| throw new InvalidOperationException("Magic number did not match!"); |
| } |
|
|
| ulong uuidHi = BigEndianBitConverter.ToUInt64(_stream, _buffer); |
| ulong uuidLo = BigEndianBitConverter.ToUInt64(_stream, _buffer); |
|
|
| |
| |
| |
| |
| |
| |
| var encoding = System.Text.UTF8Encoding.UTF8; |
| string encodingAsString = BigEndianBitConverter.ReadTinyText(_stream, encoding, _buffer); |
| switch (encodingAsString.ToLowerInvariant()) |
| { |
| case "utf-7": |
| encoding = System.Text.UTF7Encoding.UTF7; |
| break; |
| case "utf-8": |
| encoding = System.Text.UTF8Encoding.UTF8; |
| break; |
| case "utf-32": |
| encoding = System.Text.UTF32Encoding.UTF32; |
| break; |
| case "ascii": |
| encoding = System.Text.ASCIIEncoding.ASCII; |
| break; |
| default: |
| throw new NotImplementedException($"Encoding >>{encodingAsString}<< is not yet implemented!"); |
| } |
|
|
|
|
|
|
| string compression = BigEndianBitConverter.ReadTinyText(_stream, encoding, _buffer); |
|
|
|
|
| |
| |
| int tagCount = _stream.ReadByte(); |
| Dictionary<string, string> tagDict = new Dictionary<string, string>(); |
|
|
| for (int i = 0; i < tagCount; ++i) |
| { |
| string key = BigEndianBitConverter.ReadTinyText(_stream, encoding, _buffer); |
| string value = BigEndianBitConverter.ReadTinyText(_stream, encoding, _buffer); |
| value = value.TrimEnd('\0'); |
| tagDict.Add(key, value); |
| } |
|
|
| |
| |
|
|
| int contentTypeCount = _stream.ReadByte(); |
| string[] contentTypes = new string[contentTypeCount]; |
| for (int i = 0; i < contentTypeCount; ++i) |
| { |
| string contentType = BigEndianBitConverter.ReadText(_stream, encoding, _buffer); |
| contentTypes[i] = contentType; |
| } |
|
|
| |
| uint blobCount = BigEndianBitConverter.ToUInt32(_stream, _buffer); |
|
|
| |
| ulong storeOffset = BigEndianBitConverter.ToUInt64(_stream, _buffer); |
|
|
| |
| ulong size = BigEndianBitConverter.ToUInt64(_stream, _buffer); |
|
|
| |
| uint refCount = BigEndianBitConverter.ToUInt32(_stream, _buffer); |
| ulong[] references = new ulong[refCount]; |
| for (int i = 0; i < refCount; ++i) |
| { |
| ulong offset = BigEndianBitConverter.ToUInt64(_stream, _buffer); |
| references[i] = offset; |
| } |
|
|
| |
| long refOffset = _stream.Position; |
| Reference[] refItems = new Reference[refCount]; |
| for (int i = 0; i < refCount; ++i) |
| { |
| _stream.Seek(refOffset + (long)references[i], SeekOrigin.Begin); |
|
|
| string key = BigEndianBitConverter.ReadText(_stream, encoding, _buffer); |
| uint binIndex = BigEndianBitConverter.ToUInt32(_stream, _buffer); |
| ushort itemIndex = BigEndianBitConverter.ToUInt16(_stream, _buffer); |
| string fragment = BigEndianBitConverter.ReadTinyText(_stream, encoding, _buffer); |
| refItems[i] = new Reference(key, binIndex, itemIndex, fragment); |
| } |
|
|
|
|
| |
| _stream.Seek((long)storeOffset, SeekOrigin.Begin); |
| uint storeCount = BigEndianBitConverter.ToUInt32(_stream, _buffer); |
| ulong[] storeOffsets = new ulong[storeCount]; |
| for (int i = 0; i < storeCount; ++i) |
| { |
| storeOffsets[i] = BigEndianBitConverter.ToUInt64(_stream, _buffer); |
| } |
|
|
| long storeOffset2 = _stream.Position; |
|
|
| |
| for (int i = 0; i < storeCount; ++i) |
| { |
| if (((long)storeOffsets[i] + storeOffset2) >= _stream.Length) |
| throw new System.IO.EndOfStreamException("StoreOffset behind end of file"); |
| } |
|
|
| if (_stream.Length > 128) |
| { |
| var streamCopy = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.Read); |
| var dictionary = new SlobDictionaryFileBased(streamCopy, encoding, compression, tagDict, contentTypes, refItems, storeOffsets.Select(o => new StoreItemFileBased((long)o + storeOffset2)).ToArray()); |
| return dictionary; |
| } |
| else |
| { |
| |
| StoreItemInMemory[] storeItems = new StoreItemInMemory[storeCount]; |
|
|
| for (int i = 0; i < storeCount; ++i) |
| { |
| _stream.Seek(storeOffset2 + (long)storeOffsets[i], SeekOrigin.Begin); |
| uint count = BigEndianBitConverter.ToUInt32(_stream, _buffer); |
| byte[] contentIds = new byte[count]; |
| for (int k = 0; k < count; ++k) |
| { |
| contentIds[k] = (byte)_stream.ReadByte(); |
| } |
|
|
| uint contentLen = BigEndianBitConverter.ToUInt32(_stream, _buffer); |
| byte[] contentBytes = new byte[contentLen]; |
| _stream.Read(contentBytes, 0, (int)contentLen); |
| storeItems[i] = new StoreItemInMemory(contentIds, contentBytes); |
| } |
|
|
| return new SlobDictionaryInMemory(encoding, compression, tagDict, contentTypes, refItems, storeItems); |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| public void Write(Dictionary<string, string> dict, string mimeContentType) |
| { |
| |
| |
|
|
|
|
| KeyValuePair<string, string>[] listDict = dict.ToArray(); |
| CompareInfo myComp_enUS = new CultureInfo("en-US", false).CompareInfo; |
| SortKey[] sortKeys = listDict.Select(x => myComp_enUS.GetSortKey(x.Key)).ToArray(); |
| Array.Sort(sortKeys, listDict, new UnicodeStringSorter()); |
|
|
| using (_stream = new FileStream(_fileName, FileMode.Create, FileAccess.Write, FileShare.Read)) |
| { |
| System.Text.Encoding encoding = System.Text.UTF8Encoding.UTF8; |
|
|
| BigEndianBitConverter.WriteUInt64(SupposedMagic, _stream); |
|
|
| BigEndianBitConverter.WriteUInt64(SupposedVersionHi, _stream); |
|
|
| BigEndianBitConverter.WriteUInt64(SupposedVersionLo, _stream); |
|
|
| BigEndianBitConverter.WriteTinyText("utf-8", _stream, encoding); |
|
|
| BigEndianBitConverter.WriteTinyText("zlib", _stream, encoding); |
|
|
| |
| _stream.WriteByte(0); |
|
|
| |
| _stream.WriteByte(1); |
| BigEndianBitConverter.WriteText(mimeContentType, _stream, encoding); |
|
|
|
|
|
|
| |
| long posBlobCount = _stream.Position; |
| BigEndianBitConverter.WriteUInt32(0, _stream); |
|
|
| |
| long posStoreOffset = _stream.Position; |
| BigEndianBitConverter.WriteUInt64(0, _stream); |
|
|
| |
| long posSize = _stream.Position; |
| BigEndianBitConverter.WriteUInt64(0, _stream); |
|
|
| |
| BigEndianBitConverter.WriteUInt32((uint)listDict.Length, _stream); |
| long posRefTablePositions = _stream.Position; |
|
|
|
|
|
|
| long posRefTableBegin = _stream.Position + 8 * listDict.Length; |
| _stream.Seek(posRefTableBegin, SeekOrigin.Begin); |
|
|
| int i = -1; |
| int binIndex = 0; |
| int itemIndex = 0; |
| int contentLength = 0; |
| List<int> listOfEntryCounts = new List<int>(); |
|
|
| foreach (KeyValuePair<string, string> entry in listDict) |
| { |
| ++i; |
| |
| { |
| long currentPos = _stream.Position; |
| _stream.Seek(posRefTablePositions + i * 8, SeekOrigin.Begin); |
| BigEndianBitConverter.WriteUInt64((ulong)(currentPos - posRefTableBegin), _stream); |
| _stream.Seek(currentPos, SeekOrigin.Begin); |
| } |
|
|
| BigEndianBitConverter.WriteText(entry.Key, _stream, encoding); |
|
|
| BigEndianBitConverter.WriteUInt32((uint)binIndex, _stream); |
| BigEndianBitConverter.WriteUInt16((ushort)itemIndex, _stream); |
| BigEndianBitConverter.WriteTinyText(string.Empty, _stream, encoding); |
|
|
|
|
| ++itemIndex; |
| contentLength += entry.Value.Length; |
| if (itemIndex >= 32768 || contentLength > 320 * 1024) |
| { |
| listOfEntryCounts.Add(itemIndex); |
| contentLength = 0; |
| itemIndex = 0; |
| ++binIndex; |
| } |
| } |
| if (itemIndex != 0) |
| { |
| listOfEntryCounts.Add(itemIndex); |
| } |
|
|
| |
| { |
| long currentPos = _stream.Position; |
| _stream.Seek(posBlobCount, SeekOrigin.Begin); |
| BigEndianBitConverter.WriteUInt32((uint)listOfEntryCounts.Count, _stream); |
|
|
| |
| BigEndianBitConverter.WriteUInt64((ulong)currentPos, _stream); |
|
|
| _stream.Seek(currentPos, SeekOrigin.Begin); |
| } |
|
|
| |
| BigEndianBitConverter.WriteUInt32((uint)listOfEntryCounts.Count, _stream); |
|
|
| long posStoreOffsetTable = _stream.Position; |
| _stream.Seek(posStoreOffsetTable + 8 * listOfEntryCounts.Count, SeekOrigin.Begin); |
| long posStoreBegin = _stream.Position; |
|
|
| int itemIndexOffset = 0; |
| for (binIndex = 0; binIndex < listOfEntryCounts.Count; ++binIndex) |
| { |
| { |
| long currentPos = _stream.Position; |
| _stream.Seek(posStoreOffsetTable + 8 * binIndex, SeekOrigin.Begin); |
| BigEndianBitConverter.WriteUInt64((ulong)(currentPos - posStoreBegin), _stream); |
| _stream.Seek(currentPos, SeekOrigin.Begin); |
| } |
|
|
| BigEndianBitConverter.WriteUInt32((uint)listOfEntryCounts[binIndex], _stream); |
| for (int j = 0; j < listOfEntryCounts[binIndex]; ++j) |
| { |
| _stream.WriteByte(0); |
| } |
|
|
| long posContentLength = _stream.Position; |
| BigEndianBitConverter.WriteUInt32(0, _stream); |
| |
| using (ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream compressStream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(_stream, new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(), 1024 * 1024) { IsStreamOwner = false }) |
| { |
|
|
| |
|
|
| |
| int itemPositionOffset = 0; |
| for (int k = 0; k < listOfEntryCounts[binIndex]; ++k) |
| { |
| BigEndianBitConverter.WriteUInt32((uint)itemPositionOffset, compressStream); |
| itemPositionOffset += 4 + encoding.GetByteCount(listDict[itemIndexOffset + k].Value); |
| } |
|
|
| |
| for (int k = 0; k < listOfEntryCounts[binIndex]; ++k) |
| { |
|
|
| BigEndianBitConverter.WriteBigText(listDict[itemIndexOffset + k].Value, compressStream, encoding); |
| } |
|
|
| itemIndexOffset += listOfEntryCounts[binIndex]; |
|
|
| compressStream.Flush(); |
| compressStream.Close(); |
| } |
|
|
| { |
| |
| long currentPosition = _stream.Position; |
| _stream.Seek(posContentLength, SeekOrigin.Begin); |
| BigEndianBitConverter.WriteUInt32((uint)(currentPosition - posContentLength - 4), _stream); |
| _stream.Seek(currentPosition, SeekOrigin.Begin); |
| } |
|
|
|
|
|
|
| } |
|
|
| } |
| } |
| } |
|
|
|
|
| } |
|
|