Example #1
0
 private byte[] nextData(Stream input, Stream output, verifyCallback callback, uint size)
 { // Returns the next given number of bytes from the input
     byte[] bytes = IO.unmarshal_n(input, size);
     callback?.Invoke(size);
     if (output != null)
     {
         output.Write(bytes, 0, bytes.Length);
     }
     return(bytes);
 }
Example #2
0
    public void verify(Stream input, Stream output, cancellingCallback cancelling, verifyCallback callback)
    {
        Hashtable recomputed_checksums = new Hashtable();
        Hashtable original_checksums   = null;

        try
        {
            while (!cancelling())
            {
                Header header_data = nextHeader(input, output, callback);
                debug(header_data.ToString());

                byte[] bytes_data = nextData(input, output, callback, header_data.file_size);

                if (header_data.file_name.Equals("ova.xml"))
                {
                    debug("Skipping ova.xml");
                }
                else if (header_data.file_name.EndsWith("checksum.xml"))
                {
                    string xml = string_of_byte_array(bytes_data);
                    original_checksums = parse_checksum_table(xml);
                }
                else
                { // The file has no extension (must be a data file) so will have a .checksum or .xxhash file right after it
                    Header header_checksum = nextHeader(input, output, callback);
                    byte[] bytes_checksum  = nextData(input, output, callback, header_checksum.file_size);
                    string csum_compare    = string_of_byte_array(bytes_checksum);

                    string csum = header_checksum.file_name.EndsWith(".xxhash") ? checksum_xxhash(bytes_data) : checksum_sha1(bytes_data);

                    if (!csum.Equals(csum_compare))
                    {
                        throw new BlockChecksumFailed(header_data.file_name, csum, csum_compare);
                    }

                    debug(String.Format(" has checksum: {0}", csum));
                    recomputed_checksums.Add(header_data.file_name, csum);

                    nextData(input, output, callback, header_checksum.paddingLength()); // Eat the padding for the checksum file
                }
                nextData(input, output, callback, header_data.paddingLength());         // Eat the padding for the data file
            }
        }
        catch (EndOfArchive)
        {
            debug("EOF");
            if (original_checksums != null)
            {
                compare_tables(recomputed_checksums, original_checksums);
            }
        }
    }
Example #3
0
    private Header nextHeader(Stream input, Stream output, verifyCallback callback)
    { // Interperate the next bytes from the stream as a Tar header
        byte[] bytes = nextData(input, output, callback, Header.length);

        if (Header.all_zeroes(bytes)) // Tar headers are 512-byte blocks in size
        {
            bytes = nextData(input, output, callback, Header.length);

            if (Header.all_zeroes(bytes))
            {
                // Tars end with an End-Of-Archive marker, which is two consecutive 512-byte blocks of zero bytes
                throw new EndOfArchive();
            }
        }

        return(new Header(bytes));
    }
Example #4
0
    public void verify(Stream input, Stream output, cancellingCallback cancelling, verifyCallback callback)
    {
        Hashtable recomputed_checksums = new Hashtable();
        Hashtable original_checksums = null;
        try
        {
            while (!cancelling())
            {
                Header x = null;
                byte[] one = IO.unmarshal_n(input, Header.length);
                if (callback != null) callback(Header.length);
                if (output != null) output.Write(one, 0, one.Length);

                if (Header.all_zeroes(one))
                {
                    byte[] two = IO.unmarshal_n(input, Header.length);
                    if (callback != null) callback(Header.length);
                    if (output != null) output.Write(two, 0, two.Length);
                    if (Header.all_zeroes(two))
                        throw new EndOfArchive();
                    x = new Header(two);
                }
                else
                {
                    x = new Header(one);
                }

                debug(x.ToString());
                byte[] payload = IO.unmarshal_n(input, x.file_size);
                if (callback != null) callback(x.file_size);
                if (output != null) output.Write(payload, 0, payload.Length);

                if (x.file_name.Equals("ova.xml"))
                {
                    debug("skipping ova.xml");
                }
                else if (x.file_name.EndsWith(".checksum"))
                {
                    string csum = string_of_byte_array(payload);
                    string base_name = x.file_name.Substring(0, x.file_name.Length - 9);
                    string ours = (string)recomputed_checksums[base_name];
                    if (!ours.Equals(csum))
                        throw new BlockChecksumFailed(base_name, ours, csum);
                    debug(String.Format("{0} hash OK", base_name));
                }
                else if (x.file_name.Equals("checksum.xml"))
                {
                    string xml = string_of_byte_array(payload);
                    original_checksums = parse_checksum_table(xml);
                }
                else
                {
                    string csum = checksum(payload);
                    debug(String.Format("   has checksum: {0}", csum));
                    recomputed_checksums.Add(x.file_name, csum);
                }
                byte[] padding = IO.unmarshal_n(input, x.paddingLength());
                if (callback != null) callback(x.paddingLength());
                if (output != null) output.Write(padding, 0, padding.Length);
            }

        }
        catch (EndOfArchive)
        {
            debug("EOF");
            if(original_checksums != null)
                compare_tables(recomputed_checksums, original_checksums);
        }
    }
Example #5
0
    public void verify(Stream input, Stream output, cancellingCallback cancelling, verifyCallback callback)
    {
        Hashtable recomputed_checksums = new Hashtable();
        Hashtable original_checksums   = null;

        try
        {
            while (!cancelling())
            {
                Header x   = null;
                byte[] one = IO.unmarshal_n(input, Header.length);
                if (callback != null)
                {
                    callback(Header.length);
                }
                if (output != null)
                {
                    output.Write(one, 0, one.Length);
                }

                if (Header.all_zeroes(one))
                {
                    byte[] two = IO.unmarshal_n(input, Header.length);
                    if (callback != null)
                    {
                        callback(Header.length);
                    }
                    if (output != null)
                    {
                        output.Write(two, 0, two.Length);
                    }
                    if (Header.all_zeroes(two))
                    {
                        throw new EndOfArchive();
                    }
                    x = new Header(two);
                }
                else
                {
                    x = new Header(one);
                }

                debug(x.ToString());
                byte[] payload = IO.unmarshal_n(input, x.file_size);
                if (callback != null)
                {
                    callback(x.file_size);
                }
                if (output != null)
                {
                    output.Write(payload, 0, payload.Length);
                }

                if (x.file_name.Equals("ova.xml"))
                {
                    debug("skipping ova.xml");
                }
                else if (x.file_name.EndsWith(".checksum"))
                {
                    string csum      = string_of_byte_array(payload);
                    string base_name = x.file_name.Substring(0, x.file_name.Length - 9);
                    string ours      = (string)recomputed_checksums[base_name];
                    if (!ours.Equals(csum))
                    {
                        throw new BlockChecksumFailed(base_name, ours, csum);
                    }
                    debug(String.Format("{0} hash OK", base_name));
                }
                else if (x.file_name.Equals("checksum.xml"))
                {
                    string xml = string_of_byte_array(payload);
                    original_checksums = parse_checksum_table(xml);
                }
                else
                {
                    string csum = checksum(payload);
                    debug(String.Format("   has checksum: {0}", csum));
                    recomputed_checksums.Add(x.file_name, csum);
                }
                byte[] padding = IO.unmarshal_n(input, x.paddingLength());
                if (callback != null)
                {
                    callback(x.paddingLength());
                }
                if (output != null)
                {
                    output.Write(padding, 0, padding.Length);
                }
            }
        }
        catch (EndOfArchive)
        {
            debug("EOF");
            if (original_checksums != null)
            {
                compare_tables(recomputed_checksums, original_checksums);
            }
        }
    }