Exemplo n.º 1
0
        internal unsafe ProviderManifest AddChunk(TraceEvent data)
        {
            if (provider != null)
            {
                return(null);
            }

            if (data.EventDataLength <= sizeof(ManifestEnvelope) || data.GetByteAt(3) != 0x5B)  // magic number
            {
                return(null);
            }

            ushort totalChunks = (ushort)data.GetInt16At(4);
            ushort chunkNum    = (ushort)data.GetInt16At(6);

            if (chunkNum >= totalChunks || totalChunks == 0)
            {
                return(null);
            }

            if (Chunks == null)
            {
                format       = (ManifestEnvelope.ManifestFormats)data.GetByteAt(0);
                majorVersion = (byte)data.GetByteAt(1);
                minorVersion = (byte)data.GetByteAt(2);
                ChunksLeft   = totalChunks;
                Chunks       = new byte[ChunksLeft][];
            }
            else
            {
                // Chunks have to agree with the format and version information.
                if (format != (ManifestEnvelope.ManifestFormats)data.GetByteAt(0) ||
                    majorVersion != data.GetByteAt(1) || minorVersion == data.GetByteAt(2))
                {
                    return(null);
                }
            }

            if (Chunks[chunkNum] != null)
            {
                return(null);
            }

            byte[] chunk = new byte[data.EventDataLength - 8];
            Chunks[chunkNum] = data.EventData(chunk, 0, 8, chunk.Length);
            --ChunksLeft;
            if (ChunksLeft > 0)
            {
                return(null);
            }

            // OK we have a complete set of chunks
            byte[] serializedData = Chunks[0];
            if (Chunks.Length > 1)
            {
                int totalLength = 0;
                for (int i = 0; i < Chunks.Length; i++)
                {
                    totalLength += Chunks[i].Length;
                }

                // Concatinate all the arrays.
                serializedData = new byte[totalLength];
                int pos = 0;
                for (int i = 0; i < Chunks.Length; i++)
                {
                    Array.Copy(Chunks[i], 0, serializedData, pos, Chunks[i].Length);
                    pos += Chunks[i].Length;
                }
            }
            Chunks = null;
            // string str = Encoding.UTF8.GetString(serializedData);
            provider = new ProviderManifest(serializedData, format, majorVersion, minorVersion);
            return(provider);
        }