Crc32 calculator.
Example #1
0
        public static byte[] CreateData(IPacket packet)
        {
            if (packet == null)
                throw new ArgumentNullException("packet");

            var content = packet.GetContent();
            var idLengthContent = new byte[content.Length + PacketIdFieldWidth + ContentLengthFieldWidth];

            idLengthContent[0] = (byte)packet.Id;

            var contentLength = BitConverter.GetBytes(content.Length);
            idLengthContent[1] = contentLength[0]; // Not in the mood for a for loop
            idLengthContent[2] = contentLength[1];
            idLengthContent[3] = contentLength[2];
            idLengthContent[4] = contentLength[3];

            for (int i = (PacketIdFieldWidth + ContentLengthFieldWidth); i < idLengthContent.Length; ++i)
                idLengthContent[i] = content[i - (PacketIdFieldWidth + ContentLengthFieldWidth)];

            byte[] checksum;
            using (var provider = new Crc32())
                checksum = provider.ComputeHash(idLengthContent, 0, idLengthContent.Length);

            using (var ms = new MemoryStream())
            {
                ms.Write(checksum, 0, ChecksumWidth);
                ms.Write(idLengthContent, 0, idLengthContent.Length);
                return ms.ToArray();
            }
        }
    private void zip(string strFile, ZipOutputStream s, string staticFile)
    {
        if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
        Crc32 crc = new Crc32();
        string[] filenames = Directory.GetFileSystemEntries(strFile);
        foreach (string file in filenames)
        {

            if (Directory.Exists(file))
            {
                zip(file, s, staticFile);
            }

            else // 否则直接压缩文件
            {
                //打开压缩文件
                FileStream fs = File.OpenRead(file);

                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
                ZipEntry entry = new ZipEntry(tempfile);

                entry.DateTime = DateTime.Now;
                entry.Size = fs.Length;
                fs.Close();
                crc.Reset();
                crc.Update(buffer);
                entry.Crc = crc.Value;
                s.PutNextEntry(entry);

                s.Write(buffer, 0, buffer.Length);
            }
        }
    }
Example #3
0
        static int checksum_file(string file, ref byte[] p, ref uint size, ref uint crc)
        {
            int length;
            byte[] data;
            Stream f;

            f = fopen(file, "rb");
            if (f == null)
                return -1;

            length = (int)f.Length;

            /* allocate space for entire file */
            data = new byte[length];

            /* read entire file into memory */
            f.Read(data, 0, length);

            size = (uint)length;
            //crc = crc32(0L, data, length);
            Crc32 crc32 = new Crc32();
            string hash = "";
            foreach (byte b in crc32.ComputeHash(data)) hash += b.ToString("x2").ToLower();
            crc = Convert.ToUInt32(hash,16);
            if (p != null)
                p = data;
            else
                data = null;

            fclose(f);

            return 0;
        }
Example #4
0
        public static uint CheckSum(Stream s)
        {
            var crc32 = new Crc32();
            crc32.ComputeHash(s);

            return crc32.CrcValue;
        }
Example #5
0
        public static byte[] Compress( byte[] buffer )
        {
            using ( MemoryStream ms = new MemoryStream() )
            using ( BinaryWriter writer = new BinaryWriter( ms ) )
            {
                uint checkSum = 0;

                using ( var crc = new Crc32() )
                {
                    checkSum = BitConverter.ToUInt32( crc.ComputeHash( buffer ), 0 );
                }

                byte[] compressed = DeflateBuffer( buffer );

                Int32 poslocal = WriteHeader( writer, LocalFileHeader );
                WriteLocalFile( writer, "z", checkSum, ( UInt32 )buffer.Length, compressed );

                Int32 posCDR = WriteHeader( writer, CentralDirectoryHeader );
                UInt32 CDRSize = WriteCentralDirectory( writer, "z", checkSum, ( UInt32 )compressed.Length, ( UInt32 )buffer.Length, poslocal );

                Int32 posEOD = WriteHeader( writer, EndOfDirectoryHeader );
                WriteEndOfDirectory( writer, 1, CDRSize, posCDR );

                return ms.ToArray();
            }
        }
Example #6
0
 public string Hash(string input)
 {
     using (var crc = new Crc32())
     {
         var byteArray = crc.ComputeHash(Encoding.Unicode.GetBytes(input));
         return byteArray.Aggregate("", (current, b) => current + b.ToString("x2"));
     }
 }
Example #7
0
 public static int CalcSecureHash(string text)
 {
     Crc32 crc32 = new Crc32();
     String hash = String.Empty;
     byte[] bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(text);
     byte[] data = crc32.ComputeHash(bytes);
     int res = data[0] + (data[1] * 256) + (data[2] * 65536) + ( data[3] * 16777216);
     return res;
 }
 public int GetIndex(string key)
 {
     using (var crc32 = new Crc32())
     {
         var keyBytes = Encoding.UTF8.GetBytes(key);
         var hashedKeyBytes = crc32.ComputeHash(keyBytes);
         var hash = BitConverter.ToUInt32(hashedKeyBytes, 0);
         return (int)hash & _mask;
     }
 }
Example #9
0
        static void Main(string[] args)
        {
            Crc32 crc32 = new Crc32();
            String hash = String.Empty;

            Console.Write("Enter Path: ");
            string path = Console.ReadLine();

            var dir = new DirectoryInfo(path);
            var files = new List<string>();
            var CRC = new List<string>();
            foreach (FileInfo file in dir.GetFiles())
            {
                using (FileStream fs = File.Open(file.FullName, FileMode.Open))
                foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();

                files.Add(file.FullName);
                CRC.Add(file.FullName + " - " + hash);
            }

            foreach (var element in CRC)
            {
                Console.WriteLine(element);
            }

            path = path + "/CRC.txt";

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            using (FileStream file = File.Create(path))
            {
                Byte[] info = new UTF8Encoding(true).GetBytes("");
                file.Write(info, 0, info.Length);
            }

            using (System.IO.StreamWriter file_dump =
            new System.IO.StreamWriter(@path, true))
            {
                foreach (var element in CRC)
                {
                    file_dump.WriteLine(element);
                }

                file_dump.WriteLine();
            }

            Console.WriteLine(" \t \t  Dump Done");

            //Комменты у меня (с) Константин Лебейко

            Console.ReadKey();
        }
Example #10
0
 public void Tests()
 {
     var crc = new Crc32();
     Assert.AreEqual(0, crc.Value);
     crc.Update(145);
     Assert.AreEqual(1426738271, crc.Value);
     crc.Update(123456789);
     Assert.AreEqual(1147030863, crc.Value);
     byte[] data = new byte[] { 145, 234, 156 };
     crc.Update(data);
     Assert.AreEqual(3967437022, crc.Value);
 }
Example #11
0
        public async Task AddStreamAsync(string key, Stream stream)
        {
            var crc = new Crc32();
            var useStream = stream as BufferedStream ?? new BufferedStream(stream);

            byte[] buffer = new byte[0x1000];
            int bytesRead;
            while ((bytesRead = await useStream.ReadAsync(buffer, 0, 0x1000)) > 0)
                crc.Update(buffer, 0, bytesRead);

            _entries.Add(key, crc.Value);
        }
Example #12
0
    public static int Main(string[] args)
    {
        if (args.Length == 0) {
            ShowHelp();
            return 1;
        }

        var parser = new ArgumentParser(args);

        if (!File.Exists(file_)) {
            Console.Error.WriteLine("Cannot find file {0}", file_);
            ShowHelp();
            return 1;
        }

        using (FileStream checksumStream = File.OpenRead(file_)) {

            byte[] buffer = new byte[4096];
            int bytesRead;

            switch (parser.Command) {
                case Command.Help:
                    ShowHelp();
                    break;

                case Command.Crc32:
                    var currentCrc = new Crc32();
                    while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) {
                        currentCrc.Update(buffer, 0, bytesRead);
                    }
                    Console.WriteLine("CRC32 for {0} is 0x{1:X8}", args[0], currentCrc.Value);
                    break;

                case Command.BZip2:
                    var currentBZip2Crc = new BZip2Crc();
                    while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) {
                        currentBZip2Crc.Update(buffer, 0, bytesRead);
                    }
                    Console.WriteLine("BZip2CRC32 for {0} is 0x{1:X8}", args[0], currentBZip2Crc.Value);
                    break;

                case Command.Adler:
                    var currentAdler = new Adler32();
                    while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) {
                        currentAdler.Update(buffer, 0, bytesRead);
                    }
                    Console.WriteLine("Adler32 for {0} is 0x{1:X8}", args[0], currentAdler.Value);
                    break;
            }
        }
        return 0;
    }
Example #13
0
		public long getChecksum() {
			byte[] bytes = new byte[32];
			Crc32 checksum=new Crc32();
			System.IO.MemoryStream ms = new System.IO.MemoryStream();
			System.IO.BinaryWriter bytebuffer = new System.IO.BinaryWriter(ms);
			
			for (int y = 0; y < height; y++) {
				for (int x = 0; x < width; x++) {
					bytebuffer.Write(getPixel(x, y));
					bytes=checksum.ComputeHash(ms);
				}
			}
			return BitConverter.ToInt64(bytes,0);
		}
Example #14
0
        public static void WriteZipFile(List<FileDetails> filesToZip, string path,string manifestPath,string manifest )
        {
            int compression = 9;
          
            FileDetails fd = new FileDetails(manifest, manifestPath,manifestPath);
            filesToZip.Insert(0,fd);

            foreach (FileDetails obj in filesToZip)
                if (!File.Exists(obj.FilePath))
                    throw new ArgumentException(string.Format("The File {0} does not exist!", obj.FileName));

            Object _locker=new Object();
            lock(_locker)
            {
                Crc32 crc32 = new Crc32();
                ZipOutputStream stream = new ZipOutputStream(File.Create(path));
                stream.SetLevel(compression);
                for (int i = 0; i < filesToZip.Count; i++)
                {
                    ZipEntry entry = new ZipEntry(filesToZip[i].FolderInfo + "/" + filesToZip[i].FileName);
                    entry.DateTime = DateTime.Now;
                    if (i == 0)
                    {
                        entry = new ZipEntry(manifest);
                    }

                    using (FileStream fs = File.OpenRead(filesToZip[i].FilePath))
                    {
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);
                        entry.Size = fs.Length;
                        fs.Close();
                        crc32.Reset();
                        crc32.Update(buffer);
                        entry.Crc = crc32.Value;
                        stream.PutNextEntry(entry);
                        stream.Write(buffer, 0, buffer.Length);
                    }
                }
                stream.Finish();
                stream.Close();
                DeleteManifest(manifestPath);
            }
            

          

           
        }
Example #15
0
 public static byte[] Inject(byte[] png, byte[] data)
 {
     VerifyPng(png);
     var size = new byte[] { (byte)((data.Length >> 24) & 0xff), (byte)((data.Length >> 16) & 0xff),
                             (byte)((data.Length >> 8) & 0xff),  (byte)(data.Length & 0xff) };
     var crc = new Crc32().ComputeHash(data);
     var result = new byte[png.Length + size.Length + tag.Length + data.Length + crc.Length];
     var index = 0;
     png.CopyTo(result, index); index += png.Length - iend.Length;
     size.CopyTo(result, index); index += size.Length;
     tag.CopyTo(result, index); index += tag.Length;
     data.CopyTo(result, index); index += data.Length;
     crc.CopyTo(result, index); index += crc.Length;
     iend.CopyTo(result, index);
     return result;
 }
        public void TestOperatorEquality()
        {
            Crc32 empty = new Crc32();
            Crc32 value = new Crc32("Hello");
            Crc32 copy = new Crc32(value.Value);

            Assert.IsTrue(value == copy);
            Assert.IsFalse(value == empty);
            Assert.IsTrue(value == copy.Value);
            Assert.IsFalse(value == empty.Value);

            Assert.IsFalse(value != copy);
            Assert.IsTrue(value != empty);
            Assert.IsFalse(value != copy.Value);
            Assert.IsTrue(value != empty.Value);
        }
Example #17
0
        /// <summary>
        /// 压缩多个文件或目录
        /// </summary>
        /// <param name="输出压缩文件路径">压缩包文件路径</param>
        /// <param name="压缩级别">压缩程度,范围0-9,数值越大,压缩程序越高</param>
        ///// <param name="密码">密码,如不需要设密码只需传入null或空字符串</param>
        /// <param name="目录或文件">多个文件或目录</param>
        public static void 压缩(string 输出压缩文件路径, int 压缩级别, params string[] 目录或文件)
        {
            Crc32 crc = new Crc32();
            ZipOutputStream outPutStream = new ZipOutputStream(File.Create(输出压缩文件路径));
            outPutStream.SetLevel(压缩级别); // 0 - store only to 9 - means best compression
            //if (!密码.IsNullOrEmpty()) outPutStream.Password = 密码;  //密码部分有问题,设了之后输入对密码也没法解压
            outPutStream.UseZip64 = UseZip64.Off;   //执行此命令可修正Android上解压缩出现的这个错误:08-14 09:08:38.111: D/outpath(17145): Cannot read local header version 45
            递归处理("", 目录或文件, crc, outPutStream);

            //GetAllDirectories(rootPath);
            //while (rootPath.LastIndexOf("\\") + 1 == rootPath.Length)//检查路径是否以"\"结尾
            //{
            //    rootPath = rootPath.Substring(0, rootPath.Length - 1);//如果是则去掉末尾的"\"
            //}
            //string rootMark = rootPath.Substring(0, rootPath.LastIndexOf("\\") + 1);//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径
            //foreach (string file in files)
            //{
            //    FileStream fileStream = File.OpenRead(file);//打开压缩文件
            //    byte[] buffer = new byte[fileStream.Length];
            //    fileStream.Read(buffer, 0, buffer.Length);
            //    ZipEntry entry = new ZipEntry(file.Replace(rootMark, string.Empty));
            //    entry.DateTime = DateTime.Now;
            //    // set Size and the crc, because the information
            //    // about the size and crc should be stored in the header
            //    // if it is not set it is automatically written in the footer.
            //    // (in this case size == crc == -1 in the header)
            //    // Some ZIP programs have problems with zip files that don't store
            //    // the size and crc in the header.
            //    entry.Size = fileStream.Length;
            //    fileStream.Close();
            //    crc.Reset();
            //    crc.Update(buffer);
            //    entry.Crc = crc.Value;
            //    outPutStream.PutNextEntry(entry);
            //    outPutStream.Write(buffer, 0, buffer.Length);
            //}
            //this.files.Clear();
            //foreach (string emptyPath in paths)
            //{
            //    ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/");
            //    outPutStream.PutNextEntry(entry);
            //}
            //this.paths.Clear();
            outPutStream.Finish();
            outPutStream.Close();
        }
        private static void AssertCrc32(byte[] input, int expected)
        {
            Crc32 crc = new Crc32(input);
            Assert.AreEqual(expected, crc.Value);

            crc = new Crc32(0);
            crc.Add(input);
            Assert.AreEqual(expected, crc.Value);

            crc = new Crc32();
            crc.Add(input, 0, input.Length);
            Assert.AreEqual(expected, crc.Value);

            crc = new Crc32();
            foreach (byte b in input)
                crc.Add(b);
            Assert.AreEqual(expected, crc.Value);
        }
Example #19
0
        public static long CalculateCRC32(
            string theFileName)
        {
            using (FileStream theFileStream = File.OpenRead(theFileName))
            {
                Crc32 theCRC32Provider = new Crc32();

                byte[] theBuffer = new byte[theFileStream.Length];

                theFileStream.Read(theBuffer, 0, theBuffer.Length);

                theCRC32Provider.Reset();

                theCRC32Provider.Update(theBuffer);

                return theCRC32Provider.Value;
            }
        }
	public GZipStream(Stream stream, CompressionMode mode, bool leaveOpen)
			{
				if(stream == null)
				{
					throw new ArgumentNullException("stream");
				}
				if(mode == CompressionMode.Decompress)
				{
					if(!stream.CanRead)
					{
						throw new ArgumentException
							(S._("IO_NotReadable"), "stream");
					}
				}
				else if(mode == CompressionMode.Compress)
				{
					if(!stream.CanWrite)
					{
						throw new ArgumentException
							(S._("IO_NotWritable"), "stream");
					}
				}
				else
				{
					throw new ArgumentException
						(S._("IO_CompressionMode"), "mode");
				}
				this.stream = stream;
				this.mode = mode;
				this.leaveOpen = leaveOpen;
				this.buf = new byte [4096];
				this.crc32 = new Crc32();
				this.endOfStream = false;
				this.headerDone = false;
				if(mode == CompressionMode.Decompress)
				{
					inflater = new Inflater(true);
				}
				else
				{
					deflater = new Deflater(-1, true);
				}
			}
Example #21
0
        /// <summary>
        /// Calculate file CRC
        /// </summary>
        /// <param name="filePath">The complete file path</param>
        /// <returns>File CRC</returns>
        public static string CalculateCRC(string filePath)
        {
            if (File.Exists(filePath))
            {
                Stream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                byte[] fileBuffer = new byte[fileStream.Length];
                fileStream.Read(fileBuffer, 0, (int)fileStream.Length);
                fileStream.Close();
                string crc = "";
                Crc32 crc32 = new Crc32();
                byte[] crc32Buffer = crc32.ComputeHash(fileBuffer);

                foreach (byte b in crc32Buffer)
                    crc += b.ToString("x2").ToLower();

                return crc;
            }
            return "";
        }
Example #22
0
        public static string GetHash(string filename)
        {
            Crc32 crc32 = new Crc32();
            String hash = String.Empty;

            try
            {
                if (!File.Exists(filename))
                    throw new IOException("Unknown File");

                using (FileStream fs = File.Open(filename, FileMode.Open))
                    foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
            return hash;
        }
Example #23
0
	public static void Main(string[] args)
	{
		string[] filenames = Directory.GetFiles(args[0]);
		
		Crc32 crc = new Crc32();
		ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
		
		s.SetLevel(6); // 0 - store only to 9 - means best compression
		
		foreach (string file in filenames) {
			FileStream fs = File.OpenRead(file);
			
			byte[] buffer = new byte[fs.Length];
			fs.Read(buffer, 0, buffer.Length);
			ZipEntry entry = new ZipEntry(file);
			
			entry.DateTime = DateTime.Now;
			
			// set Size and the crc, because the information
			// about the size and crc should be stored in the header
			// if it is not set it is automatically written in the footer.
			// (in this case size == crc == -1 in the header)
			// Some ZIP programs have problems with zip files that don't store
			// the size and crc in the header.
			entry.Size = fs.Length;
			fs.Close();
			
			crc.Reset();
			crc.Update(buffer);
			
			entry.Crc  = crc.Value;
			
			s.PutNextEntry(entry);
			
			s.Write(buffer, 0, buffer.Length);
			
		}
		
		s.Finish();
		s.Close();
	}
Example #24
0
        /// <param name="stream"></param><param name="compressionMode"></param> <param name="level"></param><param name="flavor"></param><param name="leaveOpen"></param>
        public ZlibBaseStream(
            Stream stream, 
            CompressionMode compressionMode, 
            CompressionLevel level, 
            ZlibStreamFlavor flavor, 
            bool leaveOpen)
        {
            this.flushMode = FlushType.None;

            // this._workingBuffer = new byte[WORKING_BUFFER_SIZE_DEFAULT];
            this.Stream = stream;
            this.leaveOpen = leaveOpen;
            this.compressionMode = compressionMode;
            this.flavor = flavor;
            this.level = level;

            // workitem 7159
            if (flavor == ZlibStreamFlavor.Gzip)
            {
                this.crc = new Crc32();
            }
        }
Example #25
0
 public static byte[] Extract(byte[] png)
 {
     VerifyPng(png);
     Func<byte[],int,int> getFour = (b,j) => b[j] << 24 | b[j + 1] << 16 | b[j + 2] << 8 | b[j + 3];
     var dataTag = getFour(tag, 0);
     var i = sig.Length;
     while (i < png.Length) {
      			var length = getFour(png, i);
         i += 4;
         if (getFour(png, i) == dataTag) {
             var result = new byte[length];
             i += 4;
             Array.Copy(png, i, result, 0, length);
             var crc = new Crc32().ComputeHash(result);
             i += length;
             if (getFour(crc, 0) != getFour(png, i))
                 throw new ArgumentException("PNG's daTa chunk fails CRC.", "png");
             return result;
         }
         i += length + 8;
     }
     throw new ArgumentException("PNG does not contain a daTa chunk.", "png");
 }
	}//ComputeMD5

	/// <summary>
	///  计算指定文件的CRC32值
	/// </summary>
	/// <param name="fileName">指定文件的完全限定名称</param>
	/// <returns>返回值的字符串形式</returns>
	public static String GetCRC32(String fileName)
	{
		String hashCRC32 = String.Empty;
		//检查文件是否存在,如果文件存在则进行计算,否则返回空值
		if (File.Exists(fileName))
		{
			using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
			{
				//计算文件的CSC32值
				Crc32 calculator = new Crc32();
				Byte[] buffer = calculator.ComputeHash(fs);
				calculator.Clear();
				//将字节数组转换成十六进制的字符串形式
				StringBuilder stringBuilder = new StringBuilder();
				for (int i = 0; i < buffer.Length; i++)
				{
					stringBuilder.Append(buffer[i].ToString("x2"));
				}
				hashCRC32 = stringBuilder.ToString();
			}//关闭文件流
		}
		return hashCRC32;
	}//ComputeCRC32
Example #27
0
    /// <summary>
    /// Архивирует данные одного потока в другой поток.
    /// </summary>
    /// <param name="inputStream">Входной поток.</param>
    /// <param name="outputStream">Выходной поток.</param>
    /// <param name="entryFileName">Имя файла, которое будет помещено в выходном архиве.</param>
    public static void ZipData( Stream inputStream, Stream outputStream, string entryFileName )
    {
        Crc32 crc = new Crc32();
        ZipOutputStream zipStream = new ZipOutputStream( outputStream );
        // начинаем архивировать
        zipStream.SetLevel( 9 ); // уровень сжатия

        long length = inputStream.Length;
        byte[] buffer = new byte[length];
        inputStream.Read( buffer, 0, buffer.Length );

        ZipEntry entry = new ZipEntry( entryFileName );
        entry.DateTime = DateTime.Now;
        entry.Size = length;
        crc.Reset();
        crc.Update( buffer );
        entry.Crc = crc.Value;

        zipStream.PutNextEntry( entry );

        zipStream.Write( buffer, 0, buffer.Length );

        zipStream.Finish();
    }
Example #28
0
        bool ReadHeader()
        {
            // Initialize CRC for this block
            crc = new Crc32();

            // Make sure there is data in file. We can't rely on ReadLeByte() to fill the buffer, as this could be EOF,
            // which is fine, but ReadLeByte() throws an exception if it doesn't find data, so we do this part ourselves.
            if (inputBuffer.Available <= 0)
            {
                inputBuffer.Fill();
                if (inputBuffer.Available <= 0)
                {
                    // No header, EOF.
                    return(false);
                }
            }

            // 1. Check the two magic bytes
            Crc32 headCRC = new Crc32();
            int   magic   = inputBuffer.ReadLeByte();

            if (magic < 0)
            {
                throw new EndOfStreamException("EOS reading GZIP header");
            }

            headCRC.Update(magic);
            if (magic != (GZipConstants.GZIP_MAGIC >> 8))
            {
                throw new GZipException("Error GZIP header, first magic byte doesn't match");
            }

            //magic = baseInputStream.ReadByte();
            magic = inputBuffer.ReadLeByte();

            if (magic < 0)
            {
                throw new EndOfStreamException("EOS reading GZIP header");
            }

            if (magic != (GZipConstants.GZIP_MAGIC & 0xFF))
            {
                throw new GZipException("Error GZIP header,  second magic byte doesn't match");
            }

            headCRC.Update(magic);

            // 2. Check the compression type (must be 8)
            int compressionType = inputBuffer.ReadLeByte();

            if (compressionType < 0)
            {
                throw new EndOfStreamException("EOS reading GZIP header");
            }

            if (compressionType != 8)
            {
                throw new GZipException("Error GZIP header, data not in deflate format");
            }
            headCRC.Update(compressionType);

            // 3. Check the flags
            int flags = inputBuffer.ReadLeByte();

            if (flags < 0)
            {
                throw new EndOfStreamException("EOS reading GZIP header");
            }
            headCRC.Update(flags);

            /*    This flag byte is divided into individual bits as follows:
             *
             * bit 0   FTEXT
             * bit 1   FHCRC
             * bit 2   FEXTRA
             * bit 3   FNAME
             * bit 4   FCOMMENT
             * bit 5   reserved
             * bit 6   reserved
             * bit 7   reserved
             */

            // 3.1 Check the reserved bits are zero

            if ((flags & 0xE0) != 0)
            {
                throw new GZipException("Reserved flag bits in GZIP header != 0");
            }

            // 4.-6. Skip the modification time, extra flags, and OS type
            for (int i = 0; i < 6; i++)
            {
                int readByte = inputBuffer.ReadLeByte();
                if (readByte < 0)
                {
                    throw new EndOfStreamException("EOS reading GZIP header");
                }
                headCRC.Update(readByte);
            }

            // 7. Read extra field
            if ((flags & GZipConstants.FEXTRA) != 0)
            {
                // Skip subfield id
                for (int i = 0; i < 2; i++)
                {
                    int readByte = inputBuffer.ReadLeByte();
                    if (readByte < 0)
                    {
                        throw new EndOfStreamException("EOS reading GZIP header");
                    }
                    headCRC.Update(readByte);
                }

                if (inputBuffer.ReadLeByte() < 0 || inputBuffer.ReadLeByte() < 0)
                {
                    throw new EndOfStreamException("EOS reading GZIP header");
                }

                int len1, len2;
                len1 = inputBuffer.ReadLeByte();
                len2 = inputBuffer.ReadLeByte();
                if ((len1 < 0) || (len2 < 0))
                {
                    throw new EndOfStreamException("EOS reading GZIP header");
                }
                headCRC.Update(len1);
                headCRC.Update(len2);

                int extraLen = (len1 << 8) | len2;
                for (int i = 0; i < extraLen; i++)
                {
                    int readByte = inputBuffer.ReadLeByte();
                    if (readByte < 0)
                    {
                        throw new EndOfStreamException("EOS reading GZIP header");
                    }
                    headCRC.Update(readByte);
                }
            }

            // 8. Read file name
            if ((flags & GZipConstants.FNAME) != 0)
            {
                int readByte;
                while ((readByte = inputBuffer.ReadLeByte()) > 0)
                {
                    headCRC.Update(readByte);
                }

                if (readByte < 0)
                {
                    throw new EndOfStreamException("EOS reading GZIP header");
                }
                headCRC.Update(readByte);
            }

            // 9. Read comment
            if ((flags & GZipConstants.FCOMMENT) != 0)
            {
                int readByte;
                while ((readByte = inputBuffer.ReadLeByte()) > 0)
                {
                    headCRC.Update(readByte);
                }

                if (readByte < 0)
                {
                    throw new EndOfStreamException("EOS reading GZIP header");
                }

                headCRC.Update(readByte);
            }

            // 10. Read header CRC
            if ((flags & GZipConstants.FHCRC) != 0)
            {
                int tempByte;
                int crcval = inputBuffer.ReadLeByte();
                if (crcval < 0)
                {
                    throw new EndOfStreamException("EOS reading GZIP header");
                }

                tempByte = inputBuffer.ReadLeByte();
                if (tempByte < 0)
                {
                    throw new EndOfStreamException("EOS reading GZIP header");
                }

                crcval = (crcval << 8) | tempByte;
                if (crcval != ((int)headCRC.Value & 0xffff))
                {
                    throw new GZipException("Header CRC value mismatch");
                }
            }

            readGZIPHeader = true;
            return(true);
        }
Example #29
0
    private static bool BuildAssetBundle(string[] assetsList, string outputPath, out uint crc)
    {
        crc = 0;
        // Load all of assets in this bundle
        List <UnityEngine.Object> assets        = new List <UnityEngine.Object>();
        List <string>             assetPathNews = new List <string>();

        foreach (string assetPath in assetsList)
        {
            //dll�ļ����⴦��
            string assetEx      = Path.GetExtension(assetPath);
            string assetPathNew = assetPath;
            if (string.Compare(assetEx, ".dll", StringComparison.OrdinalIgnoreCase) == 0)
            {
                string assetName = Path.GetFileNameWithoutExtension(assetPath);
                string assetDic  = Path.GetDirectoryName(assetPath);
                assetPathNew = assetDic + "/" + assetName + ".bytes";
                //FileUtil.CopyFileOrDirectory(assetPath, assetPathNew);
                File.Copy(assetPath, assetPathNew);
                assetPathNews.Add(assetPathNew);
                AssetDatabase.Refresh();
            }

            UnityEngine.Object[] assetsAtPath = AssetDatabase.LoadAllAssetsAtPath(assetPathNew);

            if (assetsAtPath != null || assetsAtPath.Length != 0)
            {
                assets.AddRange(assetsAtPath);
            }
            else
            {
                Debug.LogError("Cannnot load [" + assetPathNew + "] as asset object");
            }
        }
        // Build bundle
//#if UNITY_4_2 || UNITY_4_1 || UNITY_4_0
//        bool succeed = BuildPipeline.BuildAssetBundle(	null,
//                                                        assets.ToArray(),
//                                                        outputPath,
//                                                        CurrentBuildAssetOpts,
//                                                        BuildConfiger.UnityBuildTarget);
//#else
        bool succeed = BuildPipeline.BuildAssetBundle(null,
                                                      assets.ToArray(),
                                                      outputPath,
#if UNITY_WEBPLAYER
                                                      out crc,
#endif
                                                      CurrentBuildAssetOpts,
                                                      BuildConfiger.UnityBuildTarget);

//#endif
#if UNITY_STANDALONE_WIN
        crc = Crc32.GetFileCRC32(outputPath);
#endif
        if (assetPathNews.Count != 0)
        {
            foreach (string path in assetPathNews)
            {
                File.Delete(path);
            }
            AssetDatabase.Refresh();
        }
        return(succeed);
    }
Example #30
0
        public async Task <byte[]> Receieve()
        {
            var stream = await TcpService.Receieve().ConfigureAwait(false);

            try
            {
                var packetLengthBytes = new byte[4];
                var readLenghtBytes   = await stream.ReadAsync(packetLengthBytes, 0, 4).ConfigureAwait(false);

                if (readLenghtBytes != 4)
                {
                    throw new InvalidOperationException("Couldn't read the packet length");
                }

                var packetLength = BitConverter.ToInt32(packetLengthBytes, 0);

                var seqBytes     = new byte[4];
                var readSeqBytes = await stream.ReadAsync(seqBytes, 0, 4).ConfigureAwait(false);

                if (readSeqBytes != 4)
                {
                    throw new InvalidOperationException("Couldn't read the sequence");
                }

                var mesSeqNo = BitConverter.ToInt32(seqBytes, 0);

                Log.Debug($"Recieve message with seq_no {mesSeqNo}");

                if (packetLength < 12)
                {
                    throw new InvalidOperationException("Invalid packet length");
                }

                var readBytes    = 0;
                var body         = new byte[packetLength - 12];
                var neededToRead = packetLength - 12;

                do
                {
                    var bodyByte       = new byte[packetLength - 12];
                    var availableBytes = await stream.ReadAsync(bodyByte, 0, neededToRead).ConfigureAwait(false);

                    neededToRead -= availableBytes;
                    Buffer.BlockCopy(bodyByte, 0, body, readBytes, availableBytes);
                    readBytes += availableBytes;
                }while (readBytes < packetLength - 12 && stream.CanRead && stream.DataAvailable);

                var crcBytes     = new byte[4];
                var readCrcBytes = await stream.ReadAsync(crcBytes, 0, 4).ConfigureAwait(false);

                if (readCrcBytes != 4)
                {
                    throw new InvalidOperationException("Couldn't read the crc");
                }

                var checksum = BitConverter.ToInt32(crcBytes, 0);

                var rv = new byte[packetLengthBytes.Length + seqBytes.Length + body.Length];

                Buffer.BlockCopy(packetLengthBytes, 0, rv, 0, packetLengthBytes.Length);
                Buffer.BlockCopy(seqBytes, 0, rv, packetLengthBytes.Length, seqBytes.Length);
                Buffer.BlockCopy(body, 0, rv, packetLengthBytes.Length + seqBytes.Length, body.Length);
                var crc32 = new Crc32();
                crc32.SlurpBlock(rv, 0, rv.Length);
                var validChecksum = crc32.Crc32Result;

                if (checksum != validChecksum)
                {
                    throw new InvalidOperationException("invalid checksum! skip");
                }

                return(body);
            }
            catch
            {
                var buffer = new byte[4];
                while (stream.CanRead && stream.DataAvailable)
                {
                    await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
                }
                throw;
            }
        }
Example #31
0
    public static void SVGenerateConfigeFiles()
    {
        // 生成abbundleConfige.xml 及 二进制文件

        string[] bundleNames = AssetDatabase.GetAllAssetBundleNames();
        // Debug.LogWarning("bundleName: " + bundleNames.Length);

        Dictionary <string, string> resList = new Dictionary <string, string> ();
        AssetBundleConfig           config  = new AssetBundleConfig();

        config.ABList = new List <ABBase>();
        foreach (string name in bundleNames)
        {
            string[] pathNames = AssetDatabase.GetAssetPathsFromAssetBundle(name);
            foreach (string path in pathNames)
            {
                if (!ValidPath(path))
                {
                    continue;
                }
                resList.Add(path, name);
            }
        }

        foreach (string path in resList.Keys)
        {
            ABBase        aBase      = new ABBase();
            List <string> temDepends = new List <string>();
            aBase.ABDependce = temDepends;
            aBase.ABName     = resList[path];
            aBase.AssetName  = path.Remove(0, path.LastIndexOf("/") + 1);
            aBase.Crc        = Crc32.GetCrc32(path);
            aBase.Path       = path;

            string[] depends = AssetDatabase.GetDependencies(path);
            foreach (string bundlePath in depends)
            {
                if (bundlePath.EndsWith(".cs"))
                {
                    continue;
                }

                string temBundleName;
                if (resList.TryGetValue(bundlePath, out temBundleName))
                {
                    if (aBase.ABName == temBundleName)
                    {
                        continue;
                    }

                    temDepends.Add(temBundleName);
                }
            }

            config.ABList.Add(aBase);
        }

        //写入xml
        string xmlPath = Application.dataPath + "/AssetbundleConfig.xml";

        if (File.Exists(xmlPath))
        {
            File.Delete(xmlPath);
        }
        using (FileStream fileStream = new FileStream(xmlPath, FileMode.Create, FileAccess.ReadWrite))
        {
            using (StreamWriter sr = new StreamWriter(fileStream, Encoding.UTF8))
            {
                XmlSerializer xmlSer = new XmlSerializer(typeof(AssetBundleConfig));
                xmlSer.Serialize(sr, config);
                sr.Close();
                fileStream.Close();
            }
        }

        //写入二进制
        foreach (ABBase abBase in config.ABList)
        {
            abBase.Path = "";
        }
        using (FileStream fileStream = new FileStream(ABBYTEPATH, FileMode.Create, FileAccess.ReadWrite))
        {
            fileStream.Seek(0, SeekOrigin.Begin);
            fileStream.SetLength(0);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fileStream, config);
            fileStream.Close();
        }
        SetABName("assetbundleconfig", ABBYTEPATH);

        Debug.LogWarning("generate configfile success");
    }
Example #32
0
 protected override bool TryStaticOneShot(ReadOnlySpan <byte> source, Span <byte> destination, out int bytesWritten) =>
 Crc32.TryHash(source, destination, out bytesWritten);
Example #33
0
        public void CanElementCreate()
        {
            // Sample.
            var sample = new byte[] {
                0x44, 0x66, 0x75, 0x53, 0x65,
                0x01,
                0x00, 0x00, 0x00, 0x00,
                0x01,

                0x54, 0x61, 0x72, 0x67, 0x65, 0x74,
                0x01,
                0x01, 0x00, 0x00, 0x00,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,

                0x78, 0x56,
                0x34, 0x12,
                0x83, 0x04,
                0x1A, 0x01,
                0x55, 0x46, 0x44,
                0x10,
                0x00, 0x00, 0x00, 0x00
            };

            var length = sample.Length - 16;

            Array.Copy(
                BitConverter.GetBytes(length),
                0,
                sample,
                6,
                4);

            var crc32 = Crc32.ComputeAll(sample, 0, Convert.ToUInt32(sample.Length - 4));

            Array.Copy(
                BitConverter.GetBytes(crc32 ^ 0xffffffffu),
                0,
                sample,
                sample.Length - 4,
                4);


            var elementData = new byte[] {
                0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
            };
            var elementAddress = 0x08000000u;

            // Expected.
            var expected = new byte[] {
                0x44, 0x66, 0x75, 0x53, 0x65,
                0x01,
                0x00, 0x00, 0x00, 0x00,
                0x01,

                0x54, 0x61, 0x72, 0x67, 0x65, 0x74,
                0x01,
                0x01, 0x00, 0x00, 0x00,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x18, 0x00, 0x00, 0x00,
                0x01, 0x00, 0x00, 0x00,

                0x00, 0x00, 0x00, 0x08,
                0x10, 0x00, 0x00, 0x00,
                0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,

                0x78, 0x56,
                0x34, 0x12,
                0x83, 0x04,
                0x1A, 0x01,
                0x55, 0x46, 0x44,
                0x10,
                0x00, 0x00, 0x00, 0x00
            };

            length = expected.Length - 16;
            expected.WriteInteger(6, Convert.ToUInt32(length));

            crc32 = Crc32.ComputeAll(expected, 0, Convert.ToUInt32(expected.Length - 4));
            expected.WriteInteger(expected.Length - 4, crc32 ^ 0xffffffffu);

            // Test.
            var sut = this.Resolve <Processor>();

            using (var dfuStream = new MemoryStream()) {
                dfuStream.Write(sample, 0, sample.Length);
                dfuStream.Seek(0, SeekOrigin.Begin);

                using (var elementStream = new MemoryStream(elementData, false)) {
                    sut.ProcessInternal(dfuStream, 1, elementAddress, elementStream);

                    var actual = dfuStream.ToArray();

                    Assert.That(actual, Is.EqualTo(expected));
                }
            }
        }
        private void ReadHeader()
        {
            /* 1. Check the two magic bytes */
            Crc32 headCRC = new Crc32();
            int   magic   = baseInputStream.ReadByte();

            if (magic < 0)
            {
                eos = true;
                return;
            }
            headCRC.Update(magic);
            if (magic != (GZipConstants.GZIP_MAGIC >> 8))
            {
                throw new IOException("Error baseInputStream GZIP header, first byte doesn't match");
            }

            magic = baseInputStream.ReadByte();
            if (magic != (GZipConstants.GZIP_MAGIC & 0xFF))
            {
                throw new IOException("Error baseInputStream GZIP header,  second byte doesn't match");
            }
            headCRC.Update(magic);

            /* 2. Check the compression type (must be 8) */
            int CM = baseInputStream.ReadByte();

            if (CM != 8)
            {
                throw new IOException("Error baseInputStream GZIP header, data not baseInputStream deflate format");
            }
            headCRC.Update(CM);

            /* 3. Check the flags */
            int flags = baseInputStream.ReadByte();

            if (flags < 0)
            {
                throw new Exception("Early EOF baseInputStream GZIP header");
            }
            headCRC.Update(flags);

            /*    This flag byte is divided into individual bits as follows:
             *
             *      bit 0   FTEXT
             *      bit 1   FHCRC
             *      bit 2   FEXTRA
             *      bit 3   FNAME
             *      bit 4   FCOMMENT
             *      bit 5   reserved
             *      bit 6   reserved
             *      bit 7   reserved
             */

            /* 3.1 Check the reserved bits are zero */

            if ((flags & 0xd0) != 0)
            {
                throw new IOException("Reserved flag bits baseInputStream GZIP header != 0");
            }

            /* 4.-6. Skip the modification time, extra flags, and OS type */
            for (int i = 0; i < 6; i++)
            {
                int readByte = baseInputStream.ReadByte();
                if (readByte < 0)
                {
                    throw new Exception("Early EOF baseInputStream GZIP header");
                }
                headCRC.Update(readByte);
            }

            /* 7. Read extra field */
            if ((flags & GZipConstants.FEXTRA) != 0)
            {
                /* Skip subfield id */
                for (int i = 0; i < 2; i++)
                {
                    int readByte = baseInputStream.ReadByte();
                    if (readByte < 0)
                    {
                        throw new Exception("Early EOF baseInputStream GZIP header");
                    }
                    headCRC.Update(readByte);
                }
                if (baseInputStream.ReadByte() < 0 || baseInputStream.ReadByte() < 0)
                {
                    throw new Exception("Early EOF baseInputStream GZIP header");
                }

                int len1, len2, extraLen;
                len1 = baseInputStream.ReadByte();
                len2 = baseInputStream.ReadByte();
                if ((len1 < 0) || (len2 < 0))
                {
                    throw new Exception("Early EOF baseInputStream GZIP header");
                }
                headCRC.Update(len1);
                headCRC.Update(len2);

                extraLen = (len1 << 8) | len2;
                for (int i = 0; i < extraLen; i++)
                {
                    int readByte = baseInputStream.ReadByte();
                    if (readByte < 0)
                    {
                        throw new Exception("Early EOF baseInputStream GZIP header");
                    }
                    headCRC.Update(readByte);
                }
            }

            /* 8. Read file name */
            if ((flags & GZipConstants.FNAME) != 0)
            {
                int readByte;
                while ((readByte = baseInputStream.ReadByte()) > 0)
                {
                    headCRC.Update(readByte);
                }
                if (readByte < 0)
                {
                    throw new Exception("Early EOF baseInputStream GZIP file name");
                }
                headCRC.Update(readByte);
            }

            /* 9. Read comment */
            if ((flags & GZipConstants.FCOMMENT) != 0)
            {
                int readByte;
                while ((readByte = baseInputStream.ReadByte()) > 0)
                {
                    headCRC.Update(readByte);
                }

                if (readByte < 0)
                {
                    throw new Exception("Early EOF baseInputStream GZIP comment");
                }
                headCRC.Update(readByte);
            }

            /* 10. Read header CRC */
            if ((flags & GZipConstants.FHCRC) != 0)
            {
                int tempByte;
                int crcval = baseInputStream.ReadByte();
                if (crcval < 0)
                {
                    throw new Exception("Early EOF baseInputStream GZIP header");
                }

                tempByte = baseInputStream.ReadByte();
                if (tempByte < 0)
                {
                    throw new Exception("Early EOF baseInputStream GZIP header");
                }

                crcval = (crcval << 8) | tempByte;
                if (crcval != ((int)headCRC.Value & 0xffff))
                {
                    throw new IOException("Header CRC value mismatch");
                }
            }

            readGZIPHeader = true;
            //System.err.println("Read GZIP header");
        }
Example #35
0
        public void Save(string filename, Platform platform, Game game)
        {
            //Get Text Blob
            var TextBlob = Encoding.UTF8.GetBytes(GMDContent.Content.Aggregate("", (output, c) => output + c.Text.Replace("\r\n", "\xa").Replace("\xa", "\r\n") + "\0"));

            //ReXOR, if needed
            if (platform == Platform.CTR && game == Game.DGS2)
            {
                TextBlob = new BinaryReaderX(XOR.ReXOR(TextBlob, 1)).ReadAllBytes();
            }

            //Get Label Blob
            var LabelBlob = Encoding.ASCII.GetBytes(GMDContent.Content.Aggregate("", (output, c) => output + (c.Name.Contains("no_name") ? "" : c.Name + "\0")));

            if (platform == Platform.Mobile || platform == Platform.Switch)
            {
                //Create Entries
                var LabelEntries = new List <LabelEntryMobile>();
                var Buckets      = new Dictionary <byte, int>();
                int LabelOffset  = 0;
                var LabelCount   = GMDContent.Content.Count(c => !c.Name.Contains("no_name"));
                var counter      = 0;
                for (var i = 0; i < GMDContent.Content.Count(); i++)
                {
                    if (!GMDContent.Content[i].Name.Contains("no_name"))
                    {
                        LabelEntries.Add(new LabelEntryMobile
                        {
                            SectionID   = i,
                            Hash1       = ~Crc32.Create(GMDContent.Content[i].Name + GMDContent.Content[i].Name),
                            Hash2       = ~Crc32.Create(GMDContent.Content[i].Name + GMDContent.Content[i].Name + GMDContent.Content[i].Name),
                            LabelOffset = LabelOffset,
                            ListLink    = 0,
                            ZeroPadding = platform == Platform.Switch ? 0xCDCDCDCD : 0
                        });
                        LabelOffset += Encoding.ASCII.GetByteCount(GMDContent.Content[i].Name) + 1;

                        var bucket = (byte)(~Crc32.Create(GMDContent.Content[i].Name) & 0xff);
                        if (Buckets.ContainsKey(bucket))
                        {
                            LabelEntries[Buckets[bucket]].ListLink = counter;
                            Buckets[bucket] = counter;
                        }
                        else
                        {
                            Buckets.Add(bucket, counter);
                        }
                        counter++;
                    }
                }

                //Create bucketList Blob
                var BucketBlob = new long[0x100];
                if (LabelCount > 0)
                {
                    var counter2 = 0;
                    for (var i = 0; i < GMDContent.Content.Count(); i++)
                    {
                        if (!GMDContent.Content[i].Name.Contains("no_name"))
                        {
                            var bucket = (byte)(~Crc32.Create(GMDContent.Content[i].Name) & 0xff);
                            if (BucketBlob[bucket] == 0)
                            {
                                BucketBlob[bucket] = (counter2 == 0) ? -1 : counter2;
                            }
                            counter2++;
                        }
                    }
                }

                //Create Header
                var Header = new Header
                {
                    Magic        = "GMD\0",
                    Version      = 0x00010302,
                    Language     = Language.ENGLISH,
                    LabelCount   = LabelCount,
                    SectionCount = GMDContent.Content.Count(),
                    LabelSize    = LabelBlob.Length,
                    SectionSize  = TextBlob.Length,
                    NameSize     = Encoding.ASCII.GetByteCount(GMDContent.Name)
                };

                //Write Stuff
                using (var bw = new BinaryWriterX(File.Create(filename)))
                {
                    //Header
                    bw.WriteStruct(Header);
                    bw.Write(Encoding.ASCII.GetBytes(GMDContent.Name + "\0"));

                    //Entries
                    foreach (var entry in LabelEntries)
                    {
                        bw.WriteStruct(entry);
                    }

                    //BucketList
                    if (LabelCount > 0)
                    {
                        foreach (var bucket in BucketBlob)
                        {
                            bw.Write(bucket);
                        }
                    }

                    //Labels
                    bw.Write(LabelBlob);

                    //Text Sections
                    bw.Write(TextBlob);
                }
            }
            else if (platform == Platform.CTR)
            {
                //Create Entries
                var LabelEntries = new List <LabelEntry>();
                var Buckets      = new Dictionary <byte, int>();
                int LabelOffset  = 0;
                var LabelCount   = GMDContent.Content.Count(c => !c.Name.Contains("no_name"));
                var counter      = 0;
                for (var i = 0; i < GMDContent.Content.Count(); i++)
                {
                    if (!GMDContent.Content[i].Name.Contains("no_name"))
                    {
                        LabelEntries.Add(new LabelEntry
                        {
                            SectionID   = i,
                            Hash1       = ~Crc32.Create(GMDContent.Content[i].Name + GMDContent.Content[i].Name),
                            Hash2       = ~Crc32.Create(GMDContent.Content[i].Name + GMDContent.Content[i].Name + GMDContent.Content[i].Name),
                            LabelOffset = LabelOffset,
                            ListLink    = 0
                        });
                        LabelOffset += Encoding.ASCII.GetByteCount(GMDContent.Content[i].Name) + 1;

                        var bucket = (byte)(~Crc32.Create(GMDContent.Content[i].Name) & 0xff);
                        if (Buckets.ContainsKey(bucket))
                        {
                            LabelEntries[Buckets[bucket]].ListLink = counter;
                            Buckets[bucket] = counter;
                        }
                        else
                        {
                            Buckets.Add(bucket, counter);
                        }
                        counter++;
                    }
                }

                //Create bucketList Blob
                var BucketBlob = new int[0x100];
                if (LabelCount > 0)
                {
                    var counter2 = 0;
                    for (var i = 0; i < GMDContent.Content.Count(); i++)
                    {
                        if (!GMDContent.Content[i].Name.Contains("no_name"))
                        {
                            var bucket = (byte)(~Crc32.Create(GMDContent.Content[i].Name) & 0xff);
                            if (BucketBlob[bucket] == 0)
                            {
                                BucketBlob[bucket] = (counter2 == 0) ? -1 : counter2;
                            }
                            counter2++;
                        }
                    }
                }

                //Create Header
                var Header = new Header
                {
                    Magic        = "GMD\0",
                    Version      = 0x00010302,
                    Language     = Language.ENGLISH,
                    LabelCount   = LabelCount,
                    SectionCount = GMDContent.Content.Count(),
                    LabelSize    = LabelBlob.Length,
                    SectionSize  = TextBlob.Length,
                    NameSize     = Encoding.ASCII.GetByteCount(GMDContent.Name)
                };

                //Write Stuff
                using (var bw = new BinaryWriterX(File.Create(filename)))
                {
                    //Header
                    bw.WriteStruct(Header);
                    bw.Write(Encoding.ASCII.GetBytes(GMDContent.Name + "\0"));

                    //Entries
                    foreach (var entry in LabelEntries)
                    {
                        bw.WriteStruct(entry);
                    }

                    //BucketList
                    if (LabelCount > 0)
                    {
                        foreach (var bucket in BucketBlob)
                        {
                            bw.Write(bucket);
                        }
                    }

                    //Labels
                    bw.Write(LabelBlob);

                    //Text Sections
                    bw.Write(TextBlob);
                }
            }
        }
Example #36
0
 private static uint Checksum(int pid)
 {
     return(Crc32.ComputeChecksum(Encoding.ASCII.GetBytes("Lb34c2enjDmD9Zv4MS8xaCB" + pid)));
 }
Example #37
0
    /// <summary>
    /// 递归遍历目录
    /// </summary>
    /// <param name="strDirectory">The directory.</param>
    /// <param name="s">The ZipOutputStream Object.</param>
    /// <param name="parentPath">The parent path.</param>
    /// <param name="ignoreSuffixs">ignore files with suffix</param>
    private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath, string[] ignoreSuffixs)
    {
        if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
        {
            strDirectory += Path.DirectorySeparatorChar;
        }
        Crc32 crc = new Crc32();

        string[] filenames = Directory.GetFileSystemEntries(strDirectory);

        foreach (string file in filenames) // 遍历所有的文件和目录
        {
            if (Directory.Exists(file))    // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
            {
                string pPath = parentPath;
                pPath += file.Substring(file.LastIndexOf("\\") + 1);
                pPath += "\\";
                ZipSetp(file, s, pPath, ignoreSuffixs);
            }

            else // 否则直接压缩文件
            {
                #region ignore files
                bool ignore = false;

                if (ignoreSuffixs != null)
                {
                    for (int i = 0; i < ignoreSuffixs.Length; i++)
                    {
                        string suffix = FileHelper.getFileTypeByPath(file);
                        if (suffix == ignoreSuffixs[i])
                        {
                            ignore = true;
                            break;
                        }
                    }
                }

                if (ignore)
                {
                    continue;
                }
                #endregion

                //打开压缩文件
                using (FileStream fs = File.OpenRead(file))
                {
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);

                    string   fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);
                    ZipEntry entry    = new ZipEntry(fileName);

                    entry.DateTime = DateTime.Now;
                    entry.Size     = fs.Length;

                    fs.Close();

                    crc.Reset();
                    crc.Update(buffer);

                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);

                    s.Write(buffer, 0, buffer.Length);
                }
            }
        }
    }
Example #38
0
        public PackagedFileInfo WriteFile(AbstractFileInfo info)
        {
            // Assume that all files are written uncompressed (worst-case) when calculating package sizes
            long size = (long)info.Size();

            if ((Version < PackageVersion.V15 && _streams.Last().Position + size > MaxPackageSizeDOS) ||
                (Version >= PackageVersion.V16 && _streams.Last().Position + size > MaxPackageSizeBG3))
            {
                // Start a new package file if the current one is full.
                string partPath = Package.MakePartFilename(_path, _streams.Count);
                var    nextPart = File.Open(partPath, FileMode.Create, FileAccess.Write);
                _streams.Add(nextPart);
            }

            Stream stream   = _streams.Last();
            var    packaged = new PackagedFileInfo
            {
                PackageStream    = stream,
                Name             = info.Name,
                UncompressedSize = (ulong)size,
                ArchivePart      = (UInt32)(_streams.Count - 1),
                OffsetInFile     = (UInt32)stream.Position,
                Flags            = BinUtils.MakeCompressionFlags(Compression, CompressionLevel)
            };

            Stream packagedStream = info.MakeStream();

            byte[] compressed;
            try
            {
                using (var reader = new BinaryReader(packagedStream, Encoding.UTF8, true))
                {
                    byte[] uncompressed = reader.ReadBytes((int)reader.BaseStream.Length);
                    compressed = BinUtils.Compress(uncompressed, Compression, CompressionLevel);
                    stream.Write(compressed, 0, compressed.Length);
                }
            }
            finally
            {
                info.ReleaseStream();
            }

            packaged.SizeOnDisk = (UInt64)(stream.Position - (long)packaged.OffsetInFile);
            packaged.Crc        = Crc32.Compute(compressed, 0);

            int padLength = PaddingLength();

            if (stream.Position % padLength <= 0)
            {
                return(packaged);
            }

            if ((_package.Metadata.Flags & PackageFlags.Solid) == 0)
            {
                // Pad the file to a multiple of 64 bytes
                var pad = new byte[padLength - stream.Position % padLength];
                for (var i = 0; i < pad.Length; i++)
                {
                    pad[i] = 0xAD;
                }

                stream.Write(pad, 0, pad.Length);
            }

            return(packaged);
        }
Example #39
0
        public string getFileCrc32(string filename, BackgroundWorker sender)
        {
            Crc32 crc32 = new Crc32();
            String hash = String.Empty;

            using (FileStream fs = File.Open(filename, FileMode.Open))
                foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();
            return hash;
        }
Example #40
0
        private void BuildTables(IEnumerable <Arc0ArchiveFileInfo> files,
                                 out IList <Arc0DirectoryEntry> directoryEntries, out IList <uint> directoryHashes,
                                 out IList <Arc0ArchiveFileInfo> fileEntries, out Stream nameStream)
        {
            var groupedFiles = files.OrderBy(x => x.FilePath.GetDirectory())
                               .GroupBy(x => x.FilePath.GetDirectory())
                               .ToArray();

            var crc32 = Crc32.Create(Crc32Formula.Normal);
            var sjis  = Encoding.GetEncoding("SJIS");

            nameStream       = new MemoryStream();
            using var nameBw = new BinaryWriterX(nameStream, true);

            var fileInfos = new List <Arc0ArchiveFileInfo>();

            directoryEntries = new List <Arc0DirectoryEntry>();
            directoryHashes  = new List <uint>();
            var fileOffset = 0u;

            foreach (var fileGroup in groupedFiles)
            {
                var fileIndex        = fileInfos.Count;
                var fileGroupEntries = fileGroup.ToArray();

                // Add directory entry first
                var directoryNameOffset = (int)nameBw.BaseStream.Position;
                var directoryName       = fileGroup.Key.ToRelative().FullName;
                if (!string.IsNullOrEmpty(directoryName))
                {
                    directoryName += "/";
                }
                nameBw.WriteString(directoryName, sjis, false);

                var hash = BinaryPrimitives.ReadUInt32BigEndian(crc32.Compute(sjis.GetBytes(directoryName)));
                var newDirectoryEntry = new Arc0DirectoryEntry
                {
                    crc32 = string.IsNullOrEmpty(fileGroup.Key.ToRelative().FullName) ? 0xFFFFFFFF : hash,

                    directoryCount = (short)groupedFiles.Count(gf => fileGroup.Key != gf.Key && gf.Key.IsInDirectory(fileGroup.Key, false)),

                    fileCount      = (short)fileGroupEntries.Length,
                    firstFileIndex = (ushort)fileIndex,

                    directoryNameStartOffset = directoryNameOffset,
                    fileNameStartOffset      = (int)nameBw.BaseStream.Position
                };
                if (newDirectoryEntry.crc32 != 0xFFFFFFFF)
                {
                    directoryHashes.Add(newDirectoryEntry.crc32);
                }
                directoryEntries.Add(newDirectoryEntry);

                // Write file names in alphabetic order
                foreach (var fileEntry in fileGroupEntries)
                {
                    fileEntry.Entry.nameOffsetInFolder = (uint)(nameBw.BaseStream.Position - newDirectoryEntry.fileNameStartOffset);
                    fileEntry.Entry.crc32      = BinaryPrimitives.ReadUInt32BigEndian(crc32.Compute(sjis.GetBytes(fileEntry.FilePath.GetName())));
                    fileEntry.Entry.fileOffset = fileOffset;
                    fileEntry.Entry.fileSize   = (uint)fileEntry.FileSize;

                    fileOffset = (uint)((fileOffset + fileEntry.FileSize + 3) & ~3);

                    nameBw.WriteString(fileEntry.FilePath.GetName(), sjis, false);
                }

                // Add file entries in order of ascending hash
                fileInfos.AddRange(fileGroupEntries.OrderBy(x => x.Entry.crc32));
            }

            fileEntries = fileInfos;

            // Order directory entries by hash and set directoryIndex accordingly
            var directoryIndex = 0;

            directoryEntries = directoryEntries.OrderBy(x => x.crc32).Select(x =>
            {
                x.firstDirectoryIndex = (ushort)directoryIndex;
                directoryIndex       += x.directoryCount;
                return(x);
            }).ToList();
        }
Example #41
0
        private void BackgroundWorker_unpacker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker unpacker = sender as BackgroundWorker;

            File_info_bytes[] F_I_B = new File_info_bytes[(int)(H_I.Conf_Files + H_I.Bin_Files)];                                                                                                       //Информация для одиночного файла
            int start_adress        = 0x2800;                                                                                                                                                           //Адрес начала списка файлов

            for (int i = 0; i < H_I.Conf_Files + H_I.Bin_Files; i++)                                                                                                                                    // Первый цикл из всех файлов, запакованных в прошивку
            {
                F_I_B[i].ROM_Name  = new ASCIIEncoding().GetString(decode_header_bytes, i * (int)Func.Parsing_Bytes.File_info + start_adress, (int)Func.Parsing_Bytes.String_parse).TrimEnd('\0');      // 0х40 (64) Имя файла прошивки
                F_I_B[i].File_Name = new ASCIIEncoding().GetString(decode_header_bytes, i * (int)Func.Parsing_Bytes.File_info + 64 + start_adress, (int)Func.Parsing_Bytes.String_parse).TrimEnd('\0'); // 0х40 (64) Имя файла
                F_I_B[i].Offset    = BitConverter.ToInt64(decode_header_bytes, i * (int)Func.Parsing_Bytes.File_info + 128 + start_adress) * 512;                                                       // 0х8 (2х4) Начальный адрес блока
                F_I_B[i].Size      = BitConverter.ToInt64(decode_header_bytes, i * (int)Func.Parsing_Bytes.File_info + 136 + start_adress);                                                             // 0х8 (2х4) Размер
                F_I_B[i].CRC       = BitConverter.ToInt64(decode_header_bytes, i * (int)Func.Parsing_Bytes.File_info + 144 + start_adress);                                                             // 0х8 (2х4) CRC32 Контрольная сумма
                F_I_B[i].Res       = BitConverter.ToInt64(decode_header_bytes, i * (int)Func.Parsing_Bytes.File_info + 152 + start_adress);                                                             // 0х8 Резерв
                long   size_for_decode = F_I_B[i].Size;
                string path_file       = Decode_ROM_Path + "\\" + F_I_B[i].File_Name;
                while (size_for_decode % 512 != 0) // Читаем блоками по 512 байт
                {
                    size_for_decode++;
                }
                byte[] convert_bytes = new byte[size_for_decode];
                using (BinaryReader br = new BinaryReader(File.OpenRead(ROM_File_Path)))
                {
                    br.BaseStream.Position = F_I_B[i].Offset; // Смещение позиции чтения на начальный адрес
                    byte[] file_bytes = br.ReadBytes((int)size_for_decode);
                    if (i < H_I.Conf_Files)                   // Обрабатываем тут конфигурационные файлы, требующие расшифровки (идут в начале)
                    {
                        convert_bytes = func.Decoder(file_bytes, key_decode);
                    }
                    else // За ними бинарные файлы в чистом виде.
                    {
                        convert_bytes = file_bytes;
                    }
                }
                if (convert_bytes.Length > F_I_B[i].Size)
                {
                    Array.Resize(ref convert_bytes, (int)F_I_B[i].Size);
                }
                if (unpacker.CancellationPending || Convert.ToUInt32(F_I_B[i].CRC) != Crc32.CalculateCrc(convert_bytes)) // Проверяем контрольную сумму
                {
                    e.Cancel = true;
                    break;
                }
                using (BinaryWriter writer = new BinaryWriter(File.Open(path_file, FileMode.Create), new ASCIIEncoding()))
                {
                    writer.Write(convert_bytes);
                }
                unpacker.ReportProgress(i * 100 / (int)(H_I.Conf_Files + H_I.Bin_Files), F_I_B[i]);
                Thread.Sleep(1);
            }
        }
Example #42
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MimeKit.Encodings.YDecoder"/> class.
 /// </summary>
 /// <remarks>
 /// Creates a new yEnc decoder.
 /// </remarks>
 /// <param name="payloadOnly">
 /// If <c>true</c>, decoding begins immediately rather than after finding an =ybegin line.
 /// </param>
 public YDecoder(bool payloadOnly)
 {
     initial = payloadOnly ? YDecoderState.Payload : YDecoderState.ExpectYBegin;
     crc     = new Crc32(-1);
     Reset();
 }
Example #43
0
        /// <summary>
        /// 递归压缩文件夹方法,实际的处理方法
        /// </summary>
        /// <param name="cfg">压缩配置</param>
        /// <param name="FolderToZip">待压缩目录</param>
        /// <param name="s">zip压缩文件流</param>
        /// <param name="ParentFolderName">待压缩目录所处的父目录</param>
        private static bool ZipFileDictory(ZipConfig cfg, string FolderToZip, ZipOutputStream s, string ParentFolderName)
        {
            //勾选全部备份,则包含temp目录
            //if (!ContainTemp && FolderToZip.Substring(FolderToZip.Length - 5, 5).ToLower().Equals(@"\temp")) return true;
            string dirPath = FolderToZip.ToLower().TrimEnd('\\') + "\\";

            if (cfg.IgnoreDirs.FirstOrDefault(p => function.VToP(p).ToLower().Equals(dirPath)) != null)
            {
                return(true);
            }
            bool res = true;

            string[]   folders, filenames;
            ZipEntry   entry = null;
            FileStream fs    = null;
            Crc32      crc   = new Crc32();

            try
            {
                //创建当前文件夹
                entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/"));  //加上 “/” 才会当成是文件夹创建
                //entry.CompressionMethod = CompressionMethod.Stored;//如果有报错match则加上此句
                s.PutNextEntry(entry);
                s.Flush();
                //先压缩文件,再递归压缩文件夹
                filenames = Directory.GetFiles(FolderToZip);
                foreach (string file in filenames)
                {
                    //如已在忽略列表,或是正在压缩的文件,则忽略
                    if (cfg.IgnoreFiles.FirstOrDefault(p => function.VToP(p).ToLower().Equals(file.ToLower())) != null ||
                        cfg.ZipSavePath.ToLower().Equals(file.ToLower()))
                    {
                        cfg.Result.P_Position++; continue;
                    }
                    //打开压缩文件
                    fs = File.OpenRead(file);
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    entry          = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));
                    entry.DateTime = DateTime.Now;
                    entry.Size     = fs.Length;
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                    cfg.Result.P_Position++;
                }
            }
            catch
            {
                res = false;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
                if (entry != null)
                {
                    entry = null;
                }
                GC.Collect();
                GC.Collect(1);
            }
            folders = Directory.GetDirectories(FolderToZip);
            foreach (string folder in folders)
            {
                if (!ZipFileDictory(cfg, folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
                {
                    return(false);
                }
            }
            return(res);
        }
Example #44
0
 protected override byte[] StaticOneShot(ReadOnlySpan <byte> source) => Crc32.Hash(source);
Example #45
0
 public static UInt32 HashBufferV8(byte[] buffer, int modulo)
 {
     return((uint)(Crc32.Compute(buffer) % modulo));
 }
Example #46
0
        private uint[] ComputeCrc(Stream stream)
        {
            // From https://github.com/meganz/sdk/blob/d4b462efc702a9c645e90c202b57e14da3de3501/src/filefingerprint.cpp

            stream.Seek(0, SeekOrigin.Begin);

            uint[] crc          = new uint[CrcArrayLength];
            byte[] newCrcBuffer = new byte[CrcSize];
            uint   crcVal       = 0;

            if (stream.Length <= CrcSize)
            {
                // tiny file: read verbatim, NUL pad
                if (0 != stream.Read(newCrcBuffer, 0, (int)stream.Length))
                {
                    Buffer.BlockCopy(newCrcBuffer, 0, crc, 0, newCrcBuffer.Length);
                }
            }
            else if (stream.Length <= MAXFULL)
            {
                // small file: full coverage, four full CRC32s
                byte[] fileBuffer = new byte[stream.Length];
                int    read       = 0;
                while ((read += stream.Read(fileBuffer, read, (int)stream.Length - read)) < stream.Length)
                {
                    ;
                }
                for (int i = 0; i < crc.Length; i++)
                {
                    int begin = (int)(i * stream.Length / crc.Length);
                    int end   = (int)((i + 1) * stream.Length / crc.Length);

                    using (var crc32Hasher = new Crc32(CryptoPPCRC32Polynomial, Crc32.DefaultSeed))
                    {
#if NETCORE
                        var crcValBytes = crc32Hasher.ComputeHash(fileBuffer, begin, end - begin);
#else
                        crc32Hasher.TransformFinalBlock(fileBuffer, begin, end - begin);
                        var crcValBytes = crc32Hasher.Hash;
#endif
                        crcVal = BitConverter.ToUInt32(crcValBytes, 0);
                    }
                    crc[i] = crcVal;
                }
            }
            else
            {
                // large file: sparse coverage, four sparse CRC32s
                byte[] block   = new byte[4 * CrcSize];
                uint   blocks  = (uint)(MAXFULL / (block.Length * CrcArrayLength));
                long   current = 0;

                for (uint i = 0; i < CrcArrayLength; i++)
                {
                    byte[] crc32ValBytes = null;

                    uint seed = Crc32.DefaultSeed;
                    for (uint j = 0; j < blocks; j++)
                    {
                        long offset = (stream.Length - block.Length) * (i * blocks + j) / (CrcArrayLength * blocks - 1);

                        stream.Seek(offset - current, SeekOrigin.Current);
                        current += (offset - current);

                        int blockWritten = stream.Read(block, 0, block.Length);
                        current += blockWritten;

                        using (var crc32Hasher = new Crc32(CryptoPPCRC32Polynomial, seed))
                        {
#if NETCORE
                            crc32ValBytes = crc32Hasher.ComputeHash(block, 0, blockWritten);
#else
                            crc32Hasher.TransformFinalBlock(block, 0, blockWritten);
                            crc32ValBytes = crc32Hasher.Hash;
#endif
                            var seedBytes = new byte[crc32ValBytes.Length];
                            crc32ValBytes.CopyTo(seedBytes, 0);
                            if (BitConverter.IsLittleEndian)
                            {
                                Array.Reverse(seedBytes);
                            }

                            seed = BitConverter.ToUInt32(seedBytes, 0);
                            seed = ~seed;
                        }
                    }

                    crcVal = BitConverter.ToUInt32(crc32ValBytes, 0);


                    crc[i] = crcVal;
                }
            }

            return(crc);
        }
Example #47
0
            public void RunStructFldScenario(ScalarBinaryOpTest__ComputeCrc32C_Byte testClass)
            {
                var result = Crc32.ComputeCrc32C(_fld1, _fld2);

                testClass.ValidateResult(_fld1, _fld2, result);
            }
Example #48
0
        public void CanTargetClear()
        {
            // Sample.
            var sample = new byte[] {
                0x44, 0x66, 0x75, 0x53, 0x65,
                0x01,
                0x00, 0x00, 0x00, 0x00,
                0x02,

                0x54, 0x61, 0x72, 0x67, 0x65, 0x74,
                0x01,
                0x01, 0x00, 0x00, 0x00,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x70, 0x00, 0x00, 0x00,
                0x02, 0x00, 0x00, 0x00,

                0x00, 0x00, 0x00, 0x08,
                0x30, 0x00, 0x00, 0x00,
                0x12, 0x13, 0x13, 0x21, 0x34, 0x56, 0x46, 0x46,
                0x46, 0x46, 0x04, 0x86, 0x46, 0x40, 0x64, 0x06,
                0x46, 0x30, 0x48, 0x47, 0x09, 0x87, 0x98, 0x79,
                0x86, 0x78, 0x69, 0x78, 0x69, 0x78, 0x69, 0x78,
                0x97, 0x89, 0x07, 0x98, 0x60, 0x76, 0x97, 0x69,
                0x87, 0x98, 0x79, 0x78, 0x97, 0x98, 0x79, 0x80,

                0x00, 0x00, 0x00, 0x18,
                0x30, 0x00, 0x00, 0x00,
                0x12, 0x13, 0x13, 0x21, 0x34, 0x56, 0x46, 0x46,
                0x46, 0x46, 0x04, 0x86, 0x46, 0x40, 0x64, 0x06,
                0x46, 0x30, 0x48, 0x47, 0x09, 0x87, 0x98, 0x79,
                0x86, 0x78, 0x69, 0x78, 0x69, 0x78, 0x69, 0x78,
                0x97, 0x89, 0x07, 0x98, 0x60, 0x76, 0x97, 0x69,
                0x87, 0x98, 0x79, 0x78, 0x97, 0x98, 0x79, 0x80,

                0x54, 0x61, 0x72, 0x67, 0x65, 0x74,
                0x00,
                0x01, 0x00, 0x00, 0x00,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x70, 0x00, 0x00, 0x00,
                0x02, 0x00, 0x00, 0x00,

                0x00, 0x00, 0x00, 0x08,
                0x30, 0x00, 0x00, 0x00,
                0x12, 0x13, 0x13, 0x21, 0x34, 0x56, 0x46, 0x46,
                0x46, 0x46, 0x04, 0x86, 0x46, 0x40, 0x64, 0x06,
                0x46, 0x30, 0x48, 0x47, 0x09, 0x87, 0x98, 0x79,
                0x86, 0x78, 0x69, 0x78, 0x69, 0x78, 0x69, 0x78,
                0x97, 0x89, 0x07, 0x98, 0x60, 0x76, 0x97, 0x69,
                0x87, 0x98, 0x79, 0x78, 0x97, 0x98, 0x79, 0x80,

                0x00, 0x00, 0x00, 0x18,
                0x30, 0x00, 0x00, 0x00,
                0x12, 0x13, 0x13, 0x21, 0x34, 0x56, 0x46, 0x46,
                0x46, 0x46, 0x04, 0x86, 0x46, 0x40, 0x64, 0x06,
                0x46, 0x30, 0x48, 0x47, 0x09, 0x87, 0x98, 0x79,
                0x86, 0x78, 0x69, 0x78, 0x69, 0x78, 0x69, 0x78,
                0x97, 0x89, 0x07, 0x98, 0x60, 0x76, 0x97, 0x69,
                0x87, 0x98, 0x79, 0x78, 0x97, 0x98, 0x79, 0x80,

                0x78, 0x56,
                0x34, 0x12,
                0x83, 0x04,
                0x1A, 0x01,
                0x55, 0x46, 0x44,
                0x10,
                0x00, 0x00, 0x00, 0x00
            };

            var length = sample.Length - 16;

            Array.Copy(
                BitConverter.GetBytes(length),
                0,
                sample,
                6,
                4);

            var crc32 = Crc32.ComputeAll(sample, 0, Convert.ToUInt32(sample.Length - 4));

            Array.Copy(
                BitConverter.GetBytes(crc32 ^ 0xffffffffu),
                0,
                sample,
                sample.Length - 4,
                4);

            // Expected.
            var expected = new byte[] {
                0x44, 0x66, 0x75, 0x53, 0x65,
                0x01,
                0x00, 0x00, 0x00, 0x00,
                0x02,

                0x54, 0x61, 0x72, 0x67, 0x65, 0x74,
                0x01,
                0x01, 0x00, 0x00, 0x00,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
                0x70, 0x00, 0x00, 0x00,
                0x02, 0x00, 0x00, 0x00,

                0x00, 0x00, 0x00, 0x08,
                0x30, 0x00, 0x00, 0x00,
                0x12, 0x13, 0x13, 0x21, 0x34, 0x56, 0x46, 0x46,
                0x46, 0x46, 0x04, 0x86, 0x46, 0x40, 0x64, 0x06,
                0x46, 0x30, 0x48, 0x47, 0x09, 0x87, 0x98, 0x79,
                0x86, 0x78, 0x69, 0x78, 0x69, 0x78, 0x69, 0x78,
                0x97, 0x89, 0x07, 0x98, 0x60, 0x76, 0x97, 0x69,
                0x87, 0x98, 0x79, 0x78, 0x97, 0x98, 0x79, 0x80,

                0x00, 0x00, 0x00, 0x18,
                0x30, 0x00, 0x00, 0x00,
                0x12, 0x13, 0x13, 0x21, 0x34, 0x56, 0x46, 0x46,
                0x46, 0x46, 0x04, 0x86, 0x46, 0x40, 0x64, 0x06,
                0x46, 0x30, 0x48, 0x47, 0x09, 0x87, 0x98, 0x79,
                0x86, 0x78, 0x69, 0x78, 0x69, 0x78, 0x69, 0x78,
                0x97, 0x89, 0x07, 0x98, 0x60, 0x76, 0x97, 0x69,
                0x87, 0x98, 0x79, 0x78, 0x97, 0x98, 0x79, 0x80,

                0x54, 0x61, 0x72, 0x67, 0x65, 0x74,
                0x00,
                0x01, 0x00, 0x00, 0x00,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,

                0x78, 0x56,
                0x34, 0x12,
                0x83, 0x04,
                0x1A, 0x01,
                0x55, 0x46, 0x44,
                0x10,
                0x00, 0x00, 0x00, 0x00
            };

            length = expected.Length - 16;
            expected.WriteInteger(6, Convert.ToUInt32(length));

            crc32 = Crc32.ComputeAll(expected, 0, Convert.ToUInt32(expected.Length - 4));
            expected.WriteInteger(expected.Length - 4, crc32 ^ 0xffffffffu);

            // Test.
            var sut = this.Resolve <Processor>();

            using (var stream = new MemoryStream()) {
                stream.Write(sample, 0, sample.Length);
                stream.Seek(0, SeekOrigin.Begin);

                sut.ProcessInternal(stream, 0);

                var actual = stream.ToArray();

                Assert.That(actual, Is.EqualTo(expected));
            }
        }
Example #49
0
 protected override int StaticOneShot(ReadOnlySpan <byte> source, Span <byte> destination) =>
 Crc32.Hash(source, destination);
Example #50
0
        /// <summary>
        /// Validates a received webhook event by checking the signature of the event and verifying the event originated from PayPal.
        /// </summary>
        /// <param name="apiContext">APIContext containing any configuration settings to be used when validating the event.</param>
        /// <param name="requestHeaders">A collection of HTTP request headers included with the received webhook event.</param>
        /// <param name="requestBody">The body of the received HTTP request.</param>
        /// <param name="webhookId">ID of the webhook resource associated with this webhook event. If not specified, it is assumed the ID is provided via the Config property of the <paramref name="apiContext"/> parameter.</param>
        /// <returns>True if the webhook event is valid and was sent from PayPal; false otherwise.</returns>
        public static bool ValidateReceivedEvent(APIContext apiContext, NameValueCollection requestHeaders, string requestBody, string webhookId = "")
        {
            bool isValid = false;

            // Validate the APIContext object.
            ArgumentValidator.ValidateAndSetupAPIContext(apiContext);

            // Make sure the webhook ID has been provided.
            if (string.IsNullOrEmpty(webhookId))
            {
                if (apiContext.Config == null ||
                    !apiContext.Config.ContainsKey(BaseConstants.WebhookIdConfig) ||
                    string.IsNullOrEmpty(apiContext.Config[BaseConstants.WebhookIdConfig]))
                {
                    throw new PayPalException("Webhook ID needed for event validation was not found. Ensure the 'webhook.id' key is included in your application's config file or provide the webhook ID when you call this method.");
                }
                webhookId = apiContext.Config[BaseConstants.WebhookIdConfig];
            }

            // Check the headers and ensure all the correct information is present.
            var transmissionId        = requestHeaders[BaseConstants.PayPalTransmissionIdHeader];
            var transmissionTimestamp = requestHeaders[BaseConstants.PayPalTransmissionTimeHeader];
            var signature             = requestHeaders[BaseConstants.PayPalTransmissionSignatureHeader];
            var certUrl       = requestHeaders[BaseConstants.PayPalCertificateUrlHeader];
            var authAlgorithm = requestHeaders[BaseConstants.PayPalAuthAlgorithmHeader];

            ArgumentValidator.Validate(transmissionId, BaseConstants.PayPalTransmissionIdHeader);
            ArgumentValidator.Validate(transmissionTimestamp, BaseConstants.PayPalTransmissionTimeHeader);
            ArgumentValidator.Validate(signature, BaseConstants.PayPalTransmissionSignatureHeader);
            ArgumentValidator.Validate(certUrl, BaseConstants.PayPalCertificateUrlHeader);
            ArgumentValidator.Validate(authAlgorithm, BaseConstants.PayPalAuthAlgorithmHeader);

            try
            {
                // Convert the provided auth alrogithm header into a known hash alrogithm name.
                var hashAlgorithm = ConvertAuthAlgorithmHeaderToHashAlgorithmName(authAlgorithm);

                // Calculate a CRC32 checksum using the request body.
                var crc32 = Crc32.ComputeChecksum(requestBody);

                // Generate the expected signature.
                var expectedSignature      = string.Format("{0}|{1}|{2}|{3}", transmissionId, transmissionTimestamp, webhookId, crc32);
                var expectedSignatureBytes = Encoding.UTF8.GetBytes(expectedSignature);

                // Get the cert from the cache and load the trusted certificate.
                var x509CertificateCollection = CertificateManager.Instance.GetCertificatesFromUrl(certUrl);
                var trustedX509Certificate    = CertificateManager.Instance.GetTrustedCertificateFromFile(apiContext == null ? null : apiContext.Config);

                // Validate the certificate chain.
                isValid = CertificateManager.Instance.ValidateCertificateChain(trustedX509Certificate, x509CertificateCollection);

                // Verify the received signature matches the expected signature.
                if (isValid)
                {
                    var rsa            = x509CertificateCollection[0].PublicKey.Key as RSACryptoServiceProvider;
                    var signatureBytes = Convert.FromBase64String(signature);
                    isValid = rsa.VerifyData(expectedSignatureBytes, CryptoConfig.MapNameToOID(hashAlgorithm), signatureBytes);
                }
            }
            catch (PayPalException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new PayPalException("Encountered an error while attepting to validate a webhook event.", ex);
            }

            return(isValid);
        }
Example #51
0
 protected override byte[] StaticOneShot(byte[] source) => Crc32.Hash(source);
Example #52
0
        /// <summary>
        /// 把文件集合打包压缩,并增加解压密码
        /// </summary>
        /// <param name="outputPath">生成的压缩包的绝对路径</param>
        /// <param name="filesPath">要打包进来的文件集合</param>
        /// <param name="password">压缩包密码,不设置密码传入null即可</param>
        /// <param name="opLogs">操作日志,包含错误信息等</param>
        /// <returns></returns>
        public static bool PackFilesWithPassword(string outputPath, List <string> filesPath, string password,
                                                 out string opLogs)
        {
            string dir;

            try
            {
                dir = Path.GetDirectoryName(outputPath);
            }
            catch (Exception ex)
            {
                opLogs = "非法的文件路径";
                return(false);
            }

            if (!Directory.Exists(dir))
            {
                opLogs = "非法的文件路径";
                return(false);
            }
            List <string> filesOnErr = new List <string>();

            opLogs = "操作成功";
            StringBuilder logSb        = new StringBuilder();
            var           tempName     = Path.GetFileNameWithoutExtension(outputPath) + "temp";
            var           tempFilePath = Path.Combine(dir, tempName); //最终生成的压缩包的路径

            try
            {
                Crc32           crc       = new Crc32();
                ZipOutputStream zipStream = new ZipOutputStream(File.Create(tempFilePath));
                zipStream.SetLevel(6);  // 压缩级别 0-9
                if (!string.IsNullOrEmpty(password))
                {
                    zipStream.Password = password;
                }
                for (int i = 0; i < filesPath.Count; i++)
                {
                    if (string.IsNullOrEmpty(filesPath[i]))
                    {
                        continue;
                    }
                    //如果文件不存在就跳过
                    if (!File.Exists(filesPath[i]))
                    {
                        filesOnErr.Add(Path.GetFileName(filesPath[i]));
                        filesPath.RemoveAt(i);
                        i--;
                        continue;
                    }
                    //压缩进文件

                    FileStream fileStream = new FileStream(filesPath[i], FileMode.Open);
                    byte[]     buffer     = new byte[fileStream.Length];
                    fileStream.Read(buffer, 0, buffer.Length);
                    ZipEntry entry = new ZipEntry(Path.GetFileName(filesPath[i]));
                    entry.DateTime = DateTime.Now;
                    entry.Size     = fileStream.Length;
                    fileStream.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    zipStream.PutNextEntry(entry);
                    zipStream.Write(buffer, 0, buffer.Length);
                }
                zipStream.Finish();
                zipStream.Close();
                //报告不存在的文件
                if (filesOnErr.Count > 0)
                {
                    logSb.Append("文件");
                    filesOnErr.ForEach(file => { logSb.Append($"{file}、"); });
                    logSb.Append("不存在");
                    opLogs = logSb.ToString();
                }
                //改名临时文件
                if (File.Exists(outputPath))
                {
                    File.Delete(outputPath);
                }
                File.Move(tempFilePath, outputPath);
                return(true);
            }
            catch (Exception ex)
            {
                opLogs = ex.Message;
                return(false);
            }
        }
Example #53
0
        private bool ReadHeader()
        {
            // Initialize CRC for this block
            crc = new Crc32();

            // Make sure there is data in file. We can't rely on ReadLeByte() to fill the buffer, as this could be EOF,
            // which is fine, but ReadLeByte() throws an exception if it doesn't find data, so we do this part ourselves.
            if (inputBuffer.Available <= 0)
            {
                inputBuffer.Fill();
                if (inputBuffer.Available <= 0)
                {
                    // No header, EOF.
                    return(false);
                }
            }

            var headCRC = new Crc32();

            // 1. Check the two magic bytes

            var magic = inputBuffer.ReadLeByte();

            headCRC.Update(magic);
            if (magic != GZipConstants.ID1)
            {
                throw new GZipException("Error GZIP header, first magic byte doesn't match");
            }

            magic = inputBuffer.ReadLeByte();
            if (magic != GZipConstants.ID2)
            {
                throw new GZipException("Error GZIP header,  second magic byte doesn't match");
            }
            headCRC.Update(magic);

            // 2. Check the compression type (must be 8)
            var compressionType = inputBuffer.ReadLeByte();

            if (compressionType != GZipConstants.CompressionMethodDeflate)
            {
                throw new GZipException("Error GZIP header, data not in deflate format");
            }
            headCRC.Update(compressionType);

            // 3. Check the flags
            var flagsByte = inputBuffer.ReadLeByte();

            headCRC.Update(flagsByte);

            // 3.1 Check the reserved bits are zero

            if ((flagsByte & 0xE0) != 0)
            {
                throw new GZipException("Reserved flag bits in GZIP header != 0");
            }

            var flags = (GZipFlags)flagsByte;

            // 4.-6. Skip the modification time, extra flags, and OS type
            for (int i = 0; i < 6; i++)
            {
                headCRC.Update(inputBuffer.ReadLeByte());
            }

            // 7. Read extra field
            if (flags.HasFlag(GZipFlags.FEXTRA))
            {
                // XLEN is total length of extra subfields, we will skip them all
                var len1 = inputBuffer.ReadLeByte();
                var len2 = inputBuffer.ReadLeByte();

                headCRC.Update(len1);
                headCRC.Update(len2);

                int extraLen = (len2 << 8) | len1;                      // gzip is LSB first
                for (int i = 0; i < extraLen; i++)
                {
                    headCRC.Update(inputBuffer.ReadLeByte());
                }
            }

            // 8. Read file name
            if (flags.HasFlag(GZipFlags.FNAME))
            {
                var fname    = new byte[1024];
                var fnamePos = 0;
                int readByte;
                while ((readByte = inputBuffer.ReadLeByte()) > 0)
                {
                    if (fnamePos < 1024)
                    {
                        fname[fnamePos++] = (byte)readByte;
                    }
                    headCRC.Update(readByte);
                }

                headCRC.Update(readByte);

                fileName = GZipConstants.Encoding.GetString(fname, 0, fnamePos);
            }
            else
            {
                fileName = null;
            }

            // 9. Read comment
            if (flags.HasFlag(GZipFlags.FCOMMENT))
            {
                int readByte;
                while ((readByte = inputBuffer.ReadLeByte()) > 0)
                {
                    headCRC.Update(readByte);
                }

                headCRC.Update(readByte);
            }

            // 10. Read header CRC
            if (flags.HasFlag(GZipFlags.FHCRC))
            {
                int tempByte;
                int crcval = inputBuffer.ReadLeByte();
                if (crcval < 0)
                {
                    throw new EndOfStreamException("EOS reading GZIP header");
                }

                tempByte = inputBuffer.ReadLeByte();
                if (tempByte < 0)
                {
                    throw new EndOfStreamException("EOS reading GZIP header");
                }

                crcval = (crcval << 8) | tempByte;
                if (crcval != ((int)headCRC.Value & 0xffff))
                {
                    throw new GZipException("Header CRC value mismatch");
                }
            }

            readGZIPHeader = true;
            return(true);
        }
Example #54
0
        /// <summary>
        /// compress the files
        /// </summary>
        /// <param name="FolderToZip"></param>
        /// <param name="s"></param>
        /// <param name="ParentFolderName"></param>
        private static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)
        {
            bool res = true;

            string[]   folders, filenames;
            ZipEntry   entry = null;
            FileStream fs    = null;
            Crc32      crc   = new Crc32();

            try
            {
                //create current folder
                entry          = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/")); // "/" is needed
                entry.DateTime = File.GetLastWriteTime(FolderToZip);
                s.PutNextEntry(entry);
                s.Flush();


                //first compress the file, then the folder
                filenames = Directory.GetFiles(FolderToZip);
                foreach (string file in filenames)
                {
                    //open the file
                    fs = File.OpenRead(file);

                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    entry          = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));
                    entry.DateTime = File.GetLastWriteTime(file);
                    entry.Size     = fs.Length;
                    fs.Close();

                    crc.Reset();
                    crc.Update(buffer);

                    entry.Crc = crc.Value;

                    s.PutNextEntry(entry);

                    s.Write(buffer, 0, buffer.Length);
                }
            }
            catch
            {
                res = false;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
                if (entry != null)
                {
                    entry = null;
                }
                GC.Collect();
                GC.Collect(1);
            }
            folders = Directory.GetDirectories(FolderToZip);
            foreach (string folder in folders)
            {
                if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
                {
                    return(false);
                }
            }

            return(res);
        }
Example #55
0
    static void WriteData(Dictionary <string, string> resPathDic)
    {
        AssetBundleConfig config = new AssetBundleConfig();

        config.ABList = new List <ABBase>();
        foreach (string path in resPathDic.Keys)
        {
            if (!ValidPath(path))  //m_ConfigFil
            // Debug.LogError("invalidPath: "+ path + "____" + resPathDic[path]);
            {
                continue;
            }

            ABBase abBase = new ABBase();
            abBase.Path       = path;
            abBase.Crc        = Crc32.GetCrc32(path);
            abBase.ABName     = resPathDic[path];
            abBase.AssetName  = path.Remove(0, path.LastIndexOf("/") + 1);
            abBase.ABDependce = new List <string>();
            string[] resDependce = AssetDatabase.GetDependencies(path);
            for (int i = 0; i < resDependce.Length; i++)
            {
                string tempPath = resDependce[i];
                if (tempPath == path || path.EndsWith(".cs"))
                {
                    continue;
                }

                string abName = "";
                if (resPathDic.TryGetValue(tempPath, out abName))
                {
                    if (abName == resPathDic[path])
                    {
                        continue;
                    }

                    if (!abBase.ABDependce.Contains(abName))
                    {
                        abBase.ABDependce.Add(abName);
                    }
                }
            }
            config.ABList.Add(abBase);
        }

        //写入xml
        string xmlPath = Application.dataPath + "/AssetbundleConfig.xml";

        if (File.Exists(xmlPath))
        {
            File.Delete(xmlPath);
        }
        FileStream    fileStream = new FileStream(xmlPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        StreamWriter  sw         = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
        XmlSerializer xs         = new XmlSerializer(config.GetType());

        xs.Serialize(sw, config);
        sw.Close();
        fileStream.Close();

        //写入二进制
        foreach (ABBase abBase in config.ABList)
        {
            abBase.Path = "";
        }
        FileStream fs = new FileStream(ABBYTEPATH, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

        fs.Seek(0, SeekOrigin.Begin);
        fs.SetLength(0);
        BinaryFormatter bf = new BinaryFormatter();

        bf.Serialize(fs, config);
        fs.Close();
        AssetDatabase.Refresh();
        SetABName("assetbundleconfig", ABBYTEPATH);
    }
        private static void CreatePackFromFiles(string[] files, string savePath)
        {
            List <Chunk> chunks = new List <Chunk>();

            Chunk chunk = new Chunk();

            uint chunkFilesProcessed = 0;

            //Create File Headers for each file to pack
            for (int i = 0; i < files.Length; i++)
            {
                string file  = files[i];
                byte[] fdata = File.ReadAllBytes(file);
                chunk.fileData.Add(fdata);

                Crc32  crc = new Crc32();
                byte[] c   = crc.ComputeHash(fdata);

                string filename = file.Substring(file.LastIndexOf("\\") + 1);

                FileHeader h = new FileHeader
                {
                    name_len = (uint)filename.Length,
                    name     = Encoding.ASCII.GetBytes(filename),
                    offset   = 0,
                    length   = (uint)fdata.Length,
                    crc32    = (uint)BitConverter.ToInt32(c, 0)
                };

                chunk.fheader.Add(h);

                chunkFilesProcessed++;

                if (chunkFilesProcessed != 255) //Chunks can only hold 255 assets. We create a new chunk after we hit this limit.
                {
                    continue;
                }

                chunk.FileCount = chunkFilesProcessed;
                chunks.Add(chunk);

                chunk = new Chunk();

                chunkFilesProcessed = 0;
            }

            chunk.FileCount = chunkFilesProcessed;
            chunks.Add(chunk);

            // Create a chunk header for each chunk
            for (int i = 0; i < chunks.Count; i++)
            {
                Chunk chunki = chunks[i];

                chunki.NextChunkOffset = 0;
                chunki.files           = chunki.fheader.ToArray();

                // Update the files with the offset of each data chunk of the file
                int length = chunki.Encode().Length;
                int offset = 0;
                if (i > 0)
                {
                    offset = (int)chunks[i - 1].NextChunkOffset;
                }

                for (int j = 0; j < chunki.fheader.Count; j++)
                {
                    if (j != 0)
                    {
                        offset += chunki.fileData[j - 1].Length;
                    }

                    chunki.fheader[j].offset = (uint)length + (uint)offset;
                }

                offset += chunki.fileData[chunki.fheader.Count - 1].Length;
                offset += length;

                // There is no next chunk.
                if (i == chunks.Count - 1)
                {
                    break;
                }

                chunki.NextChunkOffset = (uint)offset;
            }

            // Write the chunks to a file
            using (FileStream fileStream = File.Open(savePath, FileMode.OpenOrCreate))
                using (BinaryStream wr = new BinaryStream(fileStream, ByteConverter.Big))
                {
                    foreach (Chunk chunki in chunks)
                    {
                        byte[] ph = chunki.Encode();
                        wr.Write(ph);

                        foreach (byte[] t in chunki.fileData)
                        {
                            wr.Write(t);
                        }
                    }
                }
        }
Example #57
0
		/// <summary>
		/// Test an archive for integrity/validity
		/// </summary>
		/// <param name="testData">Perform low level data Crc check</param>
		/// <param name="strategy">The <see cref="TestStrategy"></see> to apply.</param>
		/// <param name="resultHandler">The <see cref="ZipTestResultHandler"></see> handler to call during testing.</param>
		/// <returns>true if all tests pass, false otherwise</returns>
		/// <exception cref="ObjectDisposedException">The object has already been closed.</exception>
		public bool TestArchive(bool testData, TestStrategy strategy, ZipTestResultHandler resultHandler)
		{
			if (isDisposed_) {
				throw new ObjectDisposedException("ZipFile");
			}
			
			TestStatus status = new TestStatus(this);

			if ( resultHandler != null ) {
				resultHandler(status, null);
			}

			HeaderTest test = testData ? (HeaderTest.Header | HeaderTest.Extract) : HeaderTest.Header;

			bool testing = true;

			try {
				int entryIndex = 0;

				while ( testing && (entryIndex < Count) ) {
					if ( resultHandler != null ) {
						status.SetEntry(this[entryIndex]);
						status.SetOperation(TestOperation.EntryHeader);
						resultHandler(status, null);
					}

					try	{
						TestLocalHeader(this[entryIndex], test);
					}
					catch(ZipException ex) {
						status.AddError();

						if ( resultHandler != null ) {
							resultHandler(status,
								string.Format("Exception during test - '{0}'", ex.Message));
						}

						if ( strategy == TestStrategy.FindFirstError ) {
							testing = false; 
						}
					}

					if ( testing && testData && this[entryIndex].IsFile ) {
						if ( resultHandler != null ) {
							status.SetOperation(TestOperation.EntryData);
							resultHandler(status, null);
						}

                        Crc32 crc = new Crc32();

                        using (Stream entryStream = this.GetInputStream(this[entryIndex]))
                        {

                            byte[] buffer = new byte[4096];
                            long totalBytes = 0;
                            int bytesRead;
                            while ((bytesRead = entryStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                crc.Update(buffer, 0, bytesRead);

                                if (resultHandler != null)
                                {
                                    totalBytes += bytesRead;
                                    status.SetBytesTested(totalBytes);
                                    resultHandler(status, null);
                                }
                            }
                        }

						if (this[entryIndex].Crc != crc.Value) {
							status.AddError();
							
							if ( resultHandler != null ) {
								resultHandler(status, "CRC mismatch");
							}

							if ( strategy == TestStrategy.FindFirstError ) {
								testing = false;
							}
						}

						if (( this[entryIndex].Flags & (int)GeneralBitFlags.Descriptor) != 0 ) {
							ZipHelperStream helper = new ZipHelperStream(baseStream_);
							DescriptorData data = new DescriptorData();
							helper.ReadDataDescriptor(this[entryIndex].LocalHeaderRequiresZip64, data);
							if (this[entryIndex].Crc != data.Crc) {
								status.AddError();
							}

							if (this[entryIndex].CompressedSize != data.CompressedSize) {
								status.AddError();
							}

							if (this[entryIndex].Size != data.Size) {
								status.AddError();
							}
						}
					}

					if ( resultHandler != null ) {
						status.SetOperation(TestOperation.EntryComplete);
						resultHandler(status, null);
					}

					entryIndex += 1;
				}

				if ( resultHandler != null ) {
					status.SetOperation(TestOperation.MiscellaneousTests);
					resultHandler(status, null);
				}

				// TODO: the 'Corrina Johns' test where local headers are missing from
				// the central directory.  They are therefore invisible to many archivers.
			}
			catch (Exception ex) {
				status.AddError();

				if ( resultHandler != null ) {
					resultHandler(status, string.Format("Exception during test - '{0}'", ex.Message));
				}
			}

			if ( resultHandler != null ) {
				status.SetOperation(TestOperation.Complete);
				status.SetEntry(null);
				resultHandler(status, null);
			}

			return (status.ErrorCount == 0);
		}
Example #58
0
        /// <summary>
        /// 递归压缩文件夹的内部方法
        /// </summary>
        /// <param name="folderToZip">要压缩的文件夹路径</param>
        /// <param name="zipStream">压缩输出流</param>
        /// <param name="parentFolderName">此文件夹的上级文件夹</param>
        /// <returns></returns>
        private static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName)
        {
            bool result = true;

            string[]   folders, files;
            ZipEntry   ent = null;
            FileStream fs  = null;
            Crc32      crc = new Crc32();

            try
            {
                ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/"));
                zipStream.PutNextEntry(ent);
                zipStream.Flush();

                files = Directory.GetFiles(folderToZip);
                foreach (string file in files)
                {
                    fs = File.OpenRead(file);

                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    ent          = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(file)));
                    ent.DateTime = DateTime.Now;
                    ent.Size     = fs.Length;

                    fs.Close();

                    crc.Reset();
                    crc.Update(buffer);

                    ent.Crc = crc.Value;
                    zipStream.PutNextEntry(ent);
                    zipStream.Write(buffer, 0, buffer.Length);
                }
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                }
                if (ent != null)
                {
                    ent = null;
                }
                GC.Collect();
                GC.Collect(1);
            }

            folders = Directory.GetDirectories(folderToZip);
            foreach (string folder in folders)
            {
                if (!ZipDirectory(folder, zipStream, folderToZip))
                {
                    return(false);
                }
            }

            return(result);
        }
Example #59
0
 private int ReadBlock()
 {
     ulong marker = ReadMarker();
     if (marker == 0x314159265359)
     {
         int blockSize = _blockDecoder.Process(_bitstream, _blockBuffer, 0);
         _rleStream.Reset(_blockBuffer, 0, blockSize);
         _blockCrc = _blockDecoder.Crc;
         _calcBlockCrc = new Crc32BigEndian(Crc32Algorithm.Common);
         return blockSize;
     }
     else if (marker == 0x177245385090)
     {
         _compoundCrc = ReadUint();
         return 0;
     }
     else
     {
         throw new InvalidDataException("Found invalid marker in stream");
     }
 }
Example #60
0
 /// <summary>
 /// Closes the zip input stream
 /// </summary>
 public override void Close()
 {
     base.Close();
     crc   = null;
     entry = null;
 }