/* private bool LoadSubFiles(SFFHEADER header, Stream source) { if (mSubHeaders != null) mSubHeaders.Clear(); if ((header.NumberOfGroups == 0) && (header.NumberOfImage == 0)) return true; if ((header.SubHeaderFileOffset == 0)) return false; if (source == null || source.Length <= 0) return false; long offset = (long)header.SubHeaderFileOffset; if (offset < 0) return false; if (source.Seek(offset, SeekOrigin.Begin) != offset) return false; SFFSUBHEADER subHeader = new SFFSUBHEADER(); int subHeaderSize = Marshal.SizeOf(subHeader); for (int i = 0; i < header.NumberOfImage; ++i) { if (subHeaderSize + source.Position > source.Length) { // File is Eof return true; } subHeader = SFFSUBHEADER.LoadFromStream(source); if (subHeader.LenghtOfSubheader > 0) { if (subHeader.LenghtOfSubheader >= source.Length) return true; source.Seek((long)subHeader.LenghtOfSubheader, SeekOrigin.Current); } SubHeaders.Add(subHeader); } bool ret = mSubHeaders != null && (int)header.NumberOfImage == mSubHeaders.Count; return ret; }*/ private bool LoadSubFiles(SFFHEADER header, byte[] source) { if ((header.NumberOfGroups == 0) && (header.NumberOfImage == 0)) return true; if ((header.SubHeaderFileOffset == 0)) return false; if (!LoadSubFiles((int)header.SubHeaderFileOffset, source)) return false; bool ret = (int)header.NumberOfImage == mSubHeaders.Count; return ret; }
private bool Load(byte[] bytes) { mPcxDataMap.Clear(); if (mSubHeaders != null) mSubHeaders.Clear(); if ((bytes == null) || (bytes.Length <= 0)) { mIsVaild = false; return false; } // 判断是否是v2的文件 if (bytes.Length < 16) { mIsVaild = false; return false; } byte v1 = bytes[15]; byte v2; byte v3; byte v4; if (v1 == 2) { SFFHEADERv2 header = new SFFHEADERv2(); int headerSize = Marshal.SizeOf(header); IntPtr headerBuffer = Marshal.AllocHGlobal(headerSize); try { Marshal.Copy(bytes, 0, headerBuffer, headerSize); header = (SFFHEADERv2)Marshal.PtrToStructure(headerBuffer, typeof(SFFHEADERv2)); } finally { Marshal.FreeHGlobal(headerBuffer); } MemoryStream stream = new MemoryStream (bytes); try { // SFFHEADERv2 header = SFFHEADERv2.LoadFromStream (stream); int comp = string.Compare(header.signature, _cElecbyteSpr, true); if (comp != 0) return false; v1 = header.verlo2; v2 = header.verlo; v3 = header.verhi2; v4 = header.verhi; if (!LoadSubFilesV2(header, stream)) return false; } finally { stream.Close (); stream.Dispose (); } } else if (v1 == 1) { SFFHEADER header = new SFFHEADER(); int headerSize = Marshal.SizeOf(header); IntPtr headerBuffer = Marshal.AllocHGlobal(headerSize); try { Marshal.Copy(bytes, 0, headerBuffer, headerSize); header = (SFFHEADER)Marshal.PtrToStructure(headerBuffer, typeof(SFFHEADER)); } finally { Marshal.FreeHGlobal(headerBuffer); } if (string.Compare(header.signature, _cElecbyteSpr, true) != 0) return false; v1 = header.verlo2; v2 = header.verlo; v3 = header.verhi2; v4 = header.verhi; if (v1 > 1) { Debug.LogErrorFormat("sff file not supoort v{0:D}.{1:D}.{2:D}.{3:D}", v1, v2, v3, v4); return false; } // MemoryStream stream = new MemoryStream(bytes); try { if (!LoadSubFiles(header, bytes)) return false; // if (!LoadSubFiles(header, stream)) // return false; if (!LoadPcxs(header, bytes)) return false; } finally { // stream.Close(); // stream.Dispose(); } } else { Debug.LogErrorFormat("sff file not supoort v{0:D}", v1); return false; } if (v1 > 1) { Debug.LogErrorFormat("sff file not supoort v{0:D}.{1:D}.{2:D}.{3:D}", v1, v2, v3, v4); return false; } mIsVaild = true; return true; }
private bool LoadPcxs(SFFHEADER sffHeader, byte[] source) { if ((source == null) || (source.Length <= 0)) return false; if ((mSubHeaders == null) || (mSubHeaders.Count <= 0)) return true; bool ret = true; int offset = (int)sffHeader.SubHeaderFileOffset; for (int i = 0; i < mSubHeaders.Count; ++i) { SFFSUBHEADER header; if (!GetSubHeader(i, out header)) { ret = false; break; } KeyValuePair<uint, uint> key = new KeyValuePair<uint, uint>((uint)header.GroubNumber, (uint)header.ImageNumber); if (mPcxDataMap.ContainsKey(key)) { offset = (int)header.NextSubheaderFileOffset; if (offset == 0 || offset >= source.Length) break; continue; } /* // 檢查indexPrevious if (header.LenghtOfSubheader == 0 && header.IndexOfPrevious != 0) { offset = (int)header.NextSubheaderFileOffset; if (offset == 0 || offset >= source.Length) break; continue; } * */ offset += Marshal.SizeOf(header); KeyValuePair<PCXHEADER, PCXDATA> value; #if _USE_NEW_PCX Stream stream = null; try { if (!LoadPcx2(offset, header, source, ref stream, out value)) #else if (!LoadPcx(offset, header, source, out value)) #endif { Debug.LogErrorFormat("LoadPcxs: index = {0} error", i); ret = false; break; } #if _USE_NEW_PCX } finally { if (stream != null) { stream.Close(); stream.Dispose(); stream = null; } } #endif mPcxDataMap.Add(key, value); offset = (int)header.NextSubheaderFileOffset; if (offset == 0 || offset >= source.Length) break; } return ret; }