Beispiel #1
0
        private static bool UseMD5(string path)
        {
            if (!XLPack.IsFileExist(path))
            {
                return(false);
            }
            var position = XLPack.FOpen(path, "r");
            var res      = XLPack.FUseMD5(position);

            XLPack.FClose(ref position);
            return(res);
        }
Beispiel #2
0
        private static long GetFileSize(string path)
        {
            if (!XLPack.IsFileExist(path))
            {
                return(-1);
            }
            long s        = 0;
            var  position = XLPack.FOpen(path, "r");

            s = XLPack.FSize(position);
            XLPack.FClose(ref position);
            return(s);
        }
Beispiel #3
0
        private static TreeDictionary.XlFile GetFileStat2(string path)
        {
            if (!XLPack.IsFileExist(path))
            {
                return(null);
            }
            var position = XLPack.FOpen(path, "r");
            var stat2    = new XLPack.pack_stat2();
            var res      = XLPack.FGetStat(position, ref stat2);

            XLPack.FClose(ref position);
            return(res ? new TreeDictionary.XlFile(path, stat2) : null);
        }
Beispiel #4
0
        private static string GetFileMD5(string path)
        {
            if (!XLPack.IsFileExist(path))
            {
                return("");
            }
            var position = XLPack.FOpen(path, "r");

            XLPack.afs_md5_ctx md5info = new XLPack.afs_md5_ctx();
            var res = XLPack.FGetMD5(position, ref md5info);

            XLPack.FClose(ref position);
            return(res ? BitConverter.ToString(md5info.md5).Replace("-", "").ToLower() : "");
        }
Beispiel #5
0
        private static XLPack.pack_stat_t GetFileStat(string path)
        {
            var stat = new XLPack.pack_stat_t();

            if (!XLPack.IsFileExist(path))
            {
                return(stat);
            }
            var position = XLPack.FOpen(path, "r");
            var res      = XLPack.FGetStat(position, ref stat);

            XLPack.FClose(ref position);
            return(stat);
        }
Beispiel #6
0
        private static bool SetFileMD5(string path, string hash)
        {
            if (!XLPack.IsFileExist(path))
            {
                return(false);
            }

            XLPack.afs_md5_ctx md5info = new XLPack.afs_md5_ctx();
            md5info.md5 = StringToByteArray(hash);
            var position = XLPack.FOpen(path, "r");
            // fsetmd5 00001111222233334444555566667777 /master/bin32/zlib1.dll
            var res = XLPack.FSetMD5(position, ref md5info);

            XLPack.FClose(ref position);
            return(res);
        }
Beispiel #7
0
        private static bool ReCalculateFileMD5(string path)
        {
            if (!XLPack.IsFileExist(path))
            {
                return(false);
            }

            var       position = XLPack.FOpen(path, "r");
            long      fileSize = XLPack.FSize(position);
            const int bufSize  = 0x4000;

            byte[] buffer = new byte[bufSize];
            IntPtr bufPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
            // TODO: Do this without reading the entire file into memory to calculate the MD5
            //       Maybe try to use the XLPack.DLL's MD5Init, MD5Update and MD5 Finalize functions ?
            // Using ChunkedMemoryStream instead of MemoryStream to hopefully avoid outofmemory errors on large files
            ChunkedMemoryStream ms = new ChunkedMemoryStream();
            long readTotalSize     = 0;

            while (readTotalSize < fileSize)
            {
                long readSize = fileSize - readTotalSize;
                if (readSize > bufSize)
                {
                    readSize = bufSize;
                }
                XLPack.FRead(position, bufPtr, readSize);
                ms.Write(buffer, 0, (int)readSize); // readSize should never be out of int range, so it's safe to cast it
                readTotalSize += readSize;
            }
            XLPack.FClose(ref position);
            ms.Position = 0;
            MD5    md5Hash   = MD5.Create();
            string md5String = GetMd5Hash(md5Hash, ms).Replace("-", "").ToLower();

            ms.Dispose();
            var res = SetFileMD5(path, md5String);

            return(res);
        }