Example #1
0
 public override int Read(byte[] buffer, int offset, int count)
 {
     if (currentStream == null) {
         if (currentIndex == Int32.MaxValue) {
             return 0;
         }
         if (currentIndex < listaFicheros.Count) {
             currentIndex++;
         }
         if (currentIndex >= listaFicheros.Count) {
             return 0;
         }
         currentFile = listaFicheros[currentIndex];
         currentStream = new HashStream (
             File.OpenRead (currentFile.File.FullName),
             HashAlgorithm.Create ("SHA512"),
             currentFile.Hash
         );
     }
     int tmp = 0;
     tmp = currentStream.Read (buffer, offset, count);
     position += tmp;
     if (tmp < count) {
         try {
             currentStream.Close ();
         }
         catch (ChecksumVerificationException ex)
         {
             throw new ChecksumVerificationException (ex.Message, currentFile.File.FullName, ex);
         }
         catch (IOException e) {
             throw new IOException (currentFile.File + " " + e.Message);
         }
         currentStream = null;
         tmp += this.Read(buffer, offset+tmp, count - tmp);
     }
     return tmp;
 }
Example #2
0
 public override void Close()
 {
     currentIndex = Int32.MaxValue;
     if (currentStream != null) {
         try {
             currentStream.Close ();
             currentStream = null;
         }
         catch (ChecksumVerificationException ex)
         {
             throw new ChecksumVerificationException (ex.Message, currentFile.File.FullName, ex);
         }
         catch (IOException e) {
             throw new IOException (currentFile.File + " " + e.Message);
         }
     }
 }
Example #3
0
        protected override void _Unir(string fichero, string dirDest)
        {
            OnProgress (0, 1);
            string nombre = new FileInfo (fichero).Name.Replace (".001.gsp", "");
            string baseName = fichero.Replace (".001.gsp", "");
            string fSalida = dirDest + Path.DirectorySeparatorChar + nombre;
            Stream salida = UtilidadesFicheros.CreateWriter (fSalida);
            HashStream hs = new HashStream (salida, MD5.Create ());

            int i = 0;
            byte[] buffer = new byte[Consts.BUFFER_LENGTH];
            int bytesRead = 0;
            long parcial = 0;
            long total = 0;
            int fragmentosTotales = 0;
            bool hasMd5 = false;
            string fileMd5 = "";
            //string headerVersion;
            //string headerFileName;

            for (i = 1; File.Exists (String.Format (baseName + ".{0:000}.gsp", i)); i++)
            {
                String nombreFragmento = String.Format (baseName + ".{0:000}.gsp", i);
                Stream fileInStream = File.OpenRead (nombreFragmento);
                Stream inStream = fileInStream;

                if (i == 1)
                {
                    // Read header
                    bytesRead = inStream.Read (buffer, 0, 69);
                    if (bytesRead != 69)
                    {
                        throw new IOException ("Premature end of file");
                    }
                    hasMd5 = (buffer[0x38] != 0);
                    fragmentosTotales = UtArrays.LeerInt32BE (buffer, 0x39);
                    //headerVersion = UtArrays.LeerTexto (buffer, 1, 4);
                    //headerFileName = UtArrays.LeerTexto (buffer, 6, 50);
                    total = UtArrays.LeerInt64BE(buffer, 0x3D);
                }

                if (i == fragmentosTotales && hasMd5)
                {
                    inStream = new SizeLimiterStream (inStream, total - parcial);
                }

                while ((bytesRead = inStream.Read (buffer, 0, buffer.Length)) > 0)
                {
                    hs.Write (buffer, 0, bytesRead);
                    parcial += bytesRead;
                    OnProgress(parcial, total);
                }
                if (i == fragmentosTotales && hasMd5) {
                    bytesRead = fileInStream.Read (buffer, 0, 32);
                    if (bytesRead != 32) {
                        throw new IOException ("Premature end of file:" + nombreFragmento);
                    }
                    fileMd5 = UtArrays.LeerTexto (buffer, 0, 32);
                }
                fileInStream.Close ();
            }
            hs.Close ();
            if (hasMd5 && !fileMd5.ToLower().Equals (hs.Hash.ToLower()))
            {
                throw new IOException ("md5 verification failed");
            }
            OnProgress (parcial, total);
        }
Example #4
0
 private Stream CreateStream()
 {
     currentFileInfo = new FileInfo (info.GetFragmentFullPath (++currentFragment));
     FileStream fis = currentFileInfo.OpenWrite ();
     if (md5File != null && hashAlg != null) {
         if (md5Stream == null)
         {
              md5Stream = new StreamWriter(new FileInfo (md5File).OpenWrite ());
         }
         HashAlgorithm ha = HashAlgorithm.Create (hashAlg);
         HashStream st = new HashStream (fis, ha);
         return st;
     }
     return fis;
 }
        private void ReadHeader()
        {
            byte[] header = new byte[XAR_HEADER_SIZE];
            int leidos = this.inputStream.Read (header, 0, header.Length);
            this.Count (leidos);
            if (leidos != header.Length) {
                throw new IOException ("Invalid header, readed " + leidos + " bytes expected " + header.Length);
            }
            int magic = UtArrays.LeerInt32BE (header, 0);
            int size = UtArrays.LeerInt16BE (header, 4);
            version = UtArrays.LeerInt16BE (header, 6);
            long toc_length_compressed = UtArrays.LeerInt64BE (header, 8);
            long toc_length_uncompressed = UtArrays.LeerInt64BE (header, 16);
            int cksum_alg = UtArrays.LeerInt32BE (header, 24);

            if (magic != XAR_HEADER_MAGIC)
            {
                throw new Exception ("Invalid header, not valid magic:" + magic);
            }
            if (size != header.Length) {
                throw new IOException ("Invalid header, size = " + size);
            }
            this.tocHashAlgorithm = (XarHashAlgorithm)cksum_alg;

            Stream s = new SizeLimiterStream (this.inputStream, toc_length_compressed);
            HashStream hs1 = null;
            switch (this.tocHashAlgorithm) {
            case XarHashAlgorithm.Sha1:
                hs1 = new HashStream (s, SHA1.Create ());
                s = hs1;
                break;
            case XarHashAlgorithm.Md5:
                hs1 = new HashStream (s, MD5.Create ());
                s = hs1;
                break;
            default:
                break;
            }

            Stream st = new InflaterInputStream (
                s,
                new Inflater (false)
            );

            this.ReadToc (st);
            st.Close ();
            this.Count ((int)toc_length_uncompressed);
            streamLength = this.Position;
            foreach (XarArchiveEntry e in entryList)
            {
                streamLength += e.Size;
            }

            if (this.tocHashAlgorithm != XarHashAlgorithm.None && this.tocHashOffset == 0)
            {
                byte[] h = new byte[this.tocHashSize];
                int br = this.inputStream.Read (h, 0, h.Length);
                this.heapPosition += br;
                string ss = Dalle.Utilidades.UtilidadesCadenas.FormatHexHash (h).ToLower ();
                if (!ss.Equals(hs1.Hash)){
                    throw new IOException("Invalid toc checksum : " + hs1.Hash + " expected " + ss);
                }
            }
        }
        public XarArchiveEntry GetNextXarEntry()
        {
            if (dataStream != null) {
                while (this.Read (tmpBuffer) > 0) {
                }
                dataStream.Close ();
            }
            dataStream = null;

            if (entryCursor >= entryList.Count) {
                return null;
            }
            this.currentEntry = (XarArchiveEntry)entryList[entryCursor++];
            if (this.currentEntry.IsDirectory) {
                return this.currentEntry;
            }

            Stream s = new SizeLimiterStream (this.inputStream, this.currentEntry.Length);
            if (this.currentEntry.ArchivedChecksum != null) {
                switch (this.currentEntry.HashAlgorithmArchived) {
                case XarHashAlgorithm.Md5:
                    s = new HashStream (s, MD5.Create (), this.currentEntry.ArchivedChecksum);
                    break;
                case XarHashAlgorithm.Sha1:
                    s = new HashStream (s, SHA1.Create (), this.currentEntry.ArchivedChecksum);
                    break;
                default:
                    break;
                }

            }
            switch (this.currentEntry.Encoding) {
            case XarEncoding.None:
                dataStream = s;
                break;
            case XarEncoding.Gzip:
                dataStream = new InflaterInputStream (s, new Inflater (false));
                break;
            case XarEncoding.Bzip2:
                dataStream = new BZip2InputStream (s);
                break;
            default:
                break;
            }
            if (this.currentEntry.ExtractedChecksum != null) {
                switch (this.currentEntry.HashAlgorithmExtracted) {
                case XarHashAlgorithm.Md5:
                    dataStream = new HashStream (dataStream, MD5.Create (), this.currentEntry.ExtractedChecksum);
                    break;
                case XarHashAlgorithm.Sha1:
                    dataStream = new HashStream (dataStream, SHA1.Create (), this.currentEntry.ExtractedChecksum);
                    break;
                default:
                    break;
                }
            }

            return this.currentEntry;
        }