示例#1
0
        private static GrfItem Create
        (
            GrfArchive grf,
            string name,
            int compressedLength,
            int compressedLengthAligned,
            int realLength,
            GrfFileFlags flags,
            int position
        )
        {
            GrfItem ret;

            if (IsDirectory(compressedLength, compressedLengthAligned, realLength, flags, position))
            {
                ret = new GrfDirectoryInfo();
            }
            else
            {
                ret = new GrfFileInfo();
            }

            ret.m_Grf  = grf;
            ret.m_Name = FilenameEncoding.Encode(name);
            ret.m_AlignedCompressedLength = compressedLengthAligned;
            ret.m_CompressedLength        = compressedLength;
            ret.m_Length   = realLength;
            ret.m_Flags    = flags;
            ret.m_Position = position;
            ret.m_Hash     = NameHash(ret.m_Name);

            return(ret);
        }
示例#2
0
 internal GrfFileInfo(GrfArchive grf, string name, GrfFileFlags flags, byte[] data)
 {
     m_Grf   = grf;
     m_Flags = flags;
     m_Name  = FilenameEncoding.Encode(name);
     m_Hash  = NameHash(m_Name);
     Data    = data;
 }
示例#3
0
 internal GrfFileInfo(GrfArchive grf, string name, GrfFileFlags flags, byte[] data)
 {
     m_Grf = grf;
     m_Flags = flags;
     m_Name = FilenameEncoding.Encode(name);
     m_Hash = NameHash(m_Name);
     Data = data;
 }
示例#4
0
 /// <summary>
 /// Encrypts the contents of a file inside the <see cref="GrfArchive"/>.
 /// </summary>
 /// <param name="buffer">The unencrypted contents.</param>
 /// <param name="len">The length of the contents.
 /// (Note: this is the AlignedCompressedLength, not the CompressedLength)</param>
 /// <param name="flags">The <see cref="GrfFileFlags"/> of the file.</param>
 /// <returns>A <see cref="Byte"/> array containing the encrypted contents of the file.</returns>
 public static byte[] EncryptFileBuffer(byte[] buffer, int len, GrfFileFlags flags)
 {
     byte[] dst = new byte[buffer.Length];
     using (IntPtrEx pDst = new IntPtrEx(dst),
            pBuffer = new IntPtrEx(buffer))
     {
         GRFProcess(pDst, pBuffer, buffer.Length, (byte)flags, len, m_KeySchedule, CryptoType.Encrypt);
         return(dst);
     }
 }
示例#5
0
        internal GrfDirectoryInfo(GrfArchive grf, string name, GrfFileFlags flags)
        {
            m_Grf   = grf;
            m_Flags = flags;
            m_Name  = FilenameEncoding.Encode(name);
            m_Hash  = NameHash(m_Name);

            m_AlignedCompressedLength = GRFFILE_DIR_SZFILE;
            m_CompressedLength        = GRFFILE_DIR_SZSMALL;
            m_Length   = GRFFILE_DIR_SZORIG;
            m_Position = GRFFILE_DIR_OFFSET;
        }
示例#6
0
        internal GrfDirectoryInfo(GrfArchive grf, string name, GrfFileFlags flags)
        {
            m_Grf = grf;
            m_Flags = flags;
            m_Name = FilenameEncoding.Encode(name);
            m_Hash = NameHash(m_Name);

            m_AlignedCompressedLength = GRFFILE_DIR_SZFILE;
            m_CompressedLength = GRFFILE_DIR_SZSMALL;
            m_Length = GRFFILE_DIR_SZORIG;
            m_Position = GRFFILE_DIR_OFFSET;
        }
示例#7
0
 private static bool IsDirectory
 (
     int compressedLength,
     int compressedLengthAligned,
     int realLength,
     GrfFileFlags flags,
     int position
 )
 {
     return(((flags & GrfFileFlags.File) == GrfFileFlags.None) ||
            ((compressedLengthAligned == GRFFILE_DIR_SZFILE) &&
             (compressedLength == GRFFILE_DIR_SZSMALL) &&
             (realLength == GRFFILE_DIR_SZORIG) &&
             (position == GRFFILE_DIR_OFFSET)));
 }
示例#8
0
        /// <summary>
        /// Adds a new <see cref="GrfItem"/> to the <see cref="GrfArchive"/>.
        /// </summary>
        /// <param name="grfname">The filename inside the <see cref="GrfArchive"/>.</param>
        /// <param name="flags">The <see cref="GrfFileFlags"/> for this entry.</param>
        /// <param name="data">The data of the file, or null if the <see cref="GrfItem"/> is a folder.</param>
        /// <exception cref="ObjectDisposedException">
        /// Thrown when the <see cref="GrfArchive"/> is closed.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// Thrown when the grfname is null or empty, or when the flags indicate a file and the data is null.
        /// </exception>
        /// <exception cref="AccessViolationException">
        /// Thrown when the <see cref="GrfArchive"/> is read-only.
        /// </exception>
        private void Add(string grfname, GrfFileFlags flags, byte[] data)
        {
            if (m_Grf.IsDisposed)
            {
                throw new ObjectDisposedException(m_Grf.GetType().Name);
            }

            if (m_Grf.IsReadOnly)
            {
                throw new AccessViolationException("The GrfArchive is read-only");
            }

            if (string.IsNullOrEmpty(grfname))
            {
                throw new ArgumentNullException("filename");
            }

            if ((flags & GrfFileFlags.File) == GrfFileFlags.File && data == null)
            {
                throw new ArgumentNullException("data");
            }

            GrfItem add;
            int     i = IndexOf(grfname);

            if ((flags & GrfFileFlags.File) == GrfFileFlags.None || data == null)
            {
                add = new GrfDirectoryInfo(m_Grf, grfname, flags);
            }
            else
            {
                add = new GrfFileInfo(m_Grf, grfname, flags, data);
            }

            if (i >= 0)
            {
                m_Items[i] = add;
            }
            else
            {
                m_Items.Add(add);
            }

            m_Grf.Modified = true;
        }
示例#9
0
 internal static GrfItem CreateV2(GrfArchive grf, string name, int compressedLength, int compressedLengthAligned, int realLength, GrfFileFlags flags, int position)
 {
     return(Create(grf, name, compressedLength, compressedLengthAligned, realLength, flags, position));
 }
示例#10
0
        internal static GrfItem CreateV1(GrfArchive grf, string name, int compressedLength, int compressedLengthAligned, int realLength, GrfFileFlags flags, int position)
        {
            GrfItem ret = Create(grf, name, compressedLength, compressedLengthAligned, realLength, flags, position);

            ret.m_Flags |= (CheckExtension(name) ? GrfFileFlags.Des_0x14 : GrfFileFlags.MixCrypt);
            return(ret);
        }
示例#11
0
 /// <summary>
 /// Encrypts the contents of a file inside the <see cref="GrfArchive"/>.
 /// </summary>
 /// <param name="buffer">The unencrypted contents.</param>
 /// <param name="len">The length of the contents.
 /// (Note: this is the AlignedCompressedLength, not the CompressedLength)</param>
 /// <param name="flags">The <see cref="GrfFileFlags"/> of the file.</param>
 /// <returns>A <see cref="Byte"/> array containing the encrypted contents of the file.</returns>
 public static byte[] EncryptFileBuffer(byte[] buffer, int len, GrfFileFlags flags)
 {
     byte[] dst = new byte[buffer.Length];
     using (IntPtrEx pDst = new IntPtrEx(dst),
                     pBuffer = new IntPtrEx(buffer))
     {
         GRFProcess(pDst, pBuffer, buffer.Length, (byte)flags, len, m_KeySchedule, CryptoType.Encrypt);
         return dst;
     }
 }