Esempio n. 1
0
        static Utf8String s_emptyHeaders = (Utf8String)"\n\n\n\n\n\n\n\n\n\n\nx-ms-date:"; // this wont be needed once we have UTF8 literals

        public static bool TryWrite(Span <byte> output, Sha256 hash, string verb, string canonicalizedResource, DateTime utc, out int bytesWritten)
        {
            try
            {
                var writer = new SpanWriter(output);
                writer.WriteLine(verb);
                writer.Write("\n\n\n\n\n\n\n\n\n\n\nx-ms-date:");
                writer.WriteLine(utc, 'R');
                writer.Write(canonicalizedResource);
                hash.Append(writer.Written);
                writer.Index = 0;
                writer.WriteBytes(hash, default, Base64Experimental.BytesToUtf8Encoder);
Esempio n. 2
0
        public void Writable()
        {
            Span <IBufferTransformation> transformations = s_transformations;
            Span <byte> buffer = stackalloc byte[256];
            var         writer = new SpanWriter(buffer);

            var ulonger = new UInt128();

            ulonger.Lower = ulong.MaxValue;
            ulonger.Upper = 1;

            writer.WriteBytes(ulonger, default, transformations.Slice(3));
Esempio n. 3
0
        public static void EncodeAndAdvance(ref SpanWriter sw, uint value)
        {
            uint        mask   = 0x80;
            Span <byte> buffer = Array.Empty <byte>();

            if (value <= 0x7F)
            {
                sw.WriteByte((byte)value);
                return;
            }
            else if (value <= 0x3FFF)
            {
                Span <byte> tempBuf = BitConverter.GetBytes(value).AsSpan();
                tempBuf.Reverse();
                buffer = tempBuf.Slice(2, 2);
            }
            else if (value <= 0x1FFFFF)
            {
                Span <byte> tempBuf = BitConverter.GetBytes(value).AsSpan();
                tempBuf.Reverse();
                buffer = tempBuf.Slice(1, 3);
            }
            else if (value <= 0xFFFFFFF)
            {
                buffer = BitConverter.GetBytes(value);
                buffer.Reverse();
            }
            else if (value <= 0xFFFFFFFF)
            {
                buffer = BitConverter.GetBytes(value);
                buffer.Reverse();
                buffer = new byte[] { 0, buffer[0], buffer[1], buffer[2], buffer[3] };
            }
            else
            {
                throw new Exception("????");
            }

            for (int i = 1; i < buffer.Length; i++)
            {
                buffer[0] += (byte)mask;
                mask     >>= 1;
            }

            sw.WriteBytes(buffer);
        }
Esempio n. 4
0
        public void WriteNextDataEntry(Entry lastPrevBlockEntry, Entry firstNextBlockEntry)
        {
            var blockWriter = new SpanWriter(Buffer);

            blockWriter.Position = LastPosition;

            byte[] indexer = CompareEntries(lastPrevBlockEntry, firstNextBlockEntry);

            int actualSpace = MeasureEntrySize(blockWriter.Position, indexer);
            int entrySize   = indexer.Length;

            if (actualSpace > _spaceLeft)
            {
                throw new Exception("Not enough space to write index entry.");
            }

            // Begin to write the entry's common information
            int entryOffset = blockWriter.Position;

            blockWriter.WriteBytes(indexer);
            blockWriter.Align(0x04); // Entry is aligned

            int endPos = blockWriter.Position;

            _spaceLeft -= actualSpace + TocEntrySize; // Include the block's toc entry

            // Write the lookup information at the end of the block
            blockWriter.Position = BlockSize - ((EntryCount + 1) * TocEntrySize);
            blockWriter.WriteUInt16((ushort)entryOffset);
            blockWriter.WriteUInt16((ushort)entrySize);
            blockWriter.WriteUInt32(0); // We will write the block index later as we don't have it

            // Move on to next.
            EntryCount++;

            LastPosition = endPos;
        }
Esempio n. 5
0
        public byte[] Serialize()
        {
            byte[]     newHeader = new byte[HeaderSize];
            SpanWriter sw        = new SpanWriter(newHeader, Endian.Big);

            sw.WriteBytes(HeaderMagic);
            sw.WriteUInt32(TOCEntryIndex);
            sw.WriteUInt32(CompressedTOCSize);
            sw.WriteUInt32(TOCSize);
            sw.WriteUInt64(Unk);
            sw.WriteUInt64(TotalVolumeSize);

            if (HasCustomGameID)
            {
                sw.WriteStringRaw(TitleID);
            }
            else
            {
                string newTitle = TitleID.Split('|')[0].TrimEnd() + $" | Last repacked with GTToolsSharp: {DateTimeOffset.UtcNow:G}";
                sw.WriteStringRaw(newTitle.Length < 128 ? newTitle : TitleID);
            }

            return(newHeader);
        }
Esempio n. 6
0
        public static bool TryWrite(Span <byte> output, Sha256 hash, string keyType, string verb, string resourceId, string resourceType, string tokenVersion, DateTime utc, out int bytesWritten)
        {
            Span <byte> buffer = stackalloc byte[AuthenticationHeaderBufferSize];
            var         writer = new SpanWriter(buffer);

            writer.Enlarge = (minumumSize) => { return(new byte[minumumSize * 2]); };

            // compute signature hash
            writer.WriteLine(verb, Ascii.ToLowercase);
            writer.WriteLine(resourceType, Ascii.ToLowercase);
            writer.WriteLine(resourceId, Ascii.ToLowercase);
            writer.WriteLine(utc, 'l');
            writer.Write('\n');
            hash.Append(writer.Written);

            // combine token
            writer.Index = 0; // reuse writer and buffer
            writer.Write("type=");
            writer.Write(keyType);
            writer.Write("&ver=");
            writer.Write(tokenVersion);
            writer.Write("&sig=");

            writer.WriteBytes(hash, default, Base64Experimental.BytesToUtf8Encoder);