示例#1
0
        private byte[] GetData(PackedResourceInfo tocEntry)
        {
            string unpackingError;

            byte[] result;
            if (!_loader.GetData(tocEntry, out result, out unpackingError))
            {
                throw MakeError(unpackingError);
            }
            return(result);
        }
示例#2
0
            public bool GetData(PackedResourceInfo tocEntry, out byte[] result, out string unpackingError)
            {
                unpackingError = null;
                result         = null;
                var fileOffset = tocEntry.FileOffset;
                var dataSize   = tocEntry.DataSize;
                var compress   = tocEntry.Compress;

                try {
                    var stream = GetZipArchive();
                    if (stream == null)
                    {
                        unpackingError = "Resource not found.";
                        return(false);
                    }
                    using (var reader = new BinaryReader(stream)) {
                        // Check to make sure the local file header is correct
                        reader.BaseStream.Seek(fileOffset, SeekOrigin.Begin);
                        var l = reader.ReadInt32();
                        if (l != 0x04034B50)
                        {
                            // Bad: Local File Header
                            unpackingError = "Bad local file header in ZIP resource.";
                            return(false);
                        }
                        reader.BaseStream.Seek(fileOffset + 26, SeekOrigin.Begin);
                        l           = 30 + reader.ReadInt16() + reader.ReadInt16(); // local header size
                        fileOffset += l;                                            // start of file data

                        reader.BaseStream.Seek(fileOffset, SeekOrigin.Begin);
                        byte[] rawData;
                        try {
                            rawData = reader.ReadBytes(compress == 0 ? dataSize : dataSize + 1);
                        }
                        catch {
                            unpackingError = "Can't read data";
                            return(false);
                        }

                        if (compress != 0)
                        {
                            rawData[dataSize] = (byte)'Z';
                        }

                        result = compress == 0 ? rawData : ZlibModule.Decompress(rawData, -15);
                        return(true);
                    }
                }
                catch (Exception exception) {
                    unpackingError = String.Format("{0}: {1}", exception.GetType().Name, exception.Message);
                    return(false);
                }
            }
示例#3
0
            private static IEnumerable <PackedResourceInfo> ReadZipDirectory(BinaryReader reader, int headerOffset,
                                                                             int arcoffset)
            {
                while (true)
                {
                    var name = string.Empty;
                    reader.BaseStream.Seek(headerOffset, SeekOrigin.Begin); // Start of file header
                    int l = reader.ReadInt32();
                    if (l != 0x02014B50)
                    {
                        break; // Bad: Central Dir File Header
                    }
                    reader.BaseStream.Seek(headerOffset + 10, SeekOrigin.Begin);
                    var compress = reader.ReadInt16();
                    /*var time =*/
                    reader.ReadInt16();
                    /*var date =*/
                    reader.ReadInt16();
                    /*var crc =*/
                    reader.ReadInt32();
                    var dataSize   = reader.ReadInt32();
                    var fileSize   = reader.ReadInt32();
                    var nameSize   = reader.ReadInt16();
                    var headerSize = 46 + nameSize +
                                     reader.ReadInt16() +
                                     reader.ReadInt16();

                    reader.BaseStream.Seek(headerOffset + 42, SeekOrigin.Begin);
                    var fileOffset = reader.ReadInt32() + arcoffset;
                    if (nameSize > MaxPathLen)
                    {
                        nameSize = MaxPathLen;
                    }

                    for (int i = 0; i < nameSize; i++)
                    {
                        char c = reader.ReadChar();
                        if (c == '/')
                        {
                            c = Path.DirectorySeparatorChar;
                        }
                        name += c;
                    }
                    headerOffset += headerSize;

                    yield return
                        (PackedResourceInfo.Create(name, compress, dataSize, fileSize, fileOffset));
                }
            }
示例#4
0
        private byte[] GetCodeFromData(CodeContext /*!*/ context, bool isbytecode, PackedResourceInfo tocEntry)
        {
            byte[] data = GetData(tocEntry);
            byte[] code = null;

            if (data != null)
            {
                if (isbytecode)
                {
                    // would put in code to unmarshal the bytecode here...
                }
                else
                {
                    code = data;
                }
            }
            return(code);
        }
        private string GetCodeFromData(CodeContext /*!*/ context, bool isbytecode, PackedResourceInfo tocEntry)
        {
            byte[] data = GetData(tocEntry);
            string code = null;

            if (data != null)
            {
                if (isbytecode)
                {
                    // would put in code to unmarshal the bytecode here...
                }
                else
                {
                    code = context.LanguageContext.DefaultEncoding.GetString(data, 0, data.Length);
                }
            }
            return(code);
        }
            public bool GetData(PackedResourceInfo tocEntry, out byte[] result, out string unpackingError) {
                unpackingError = null;
                result = null;
                var fileOffset = tocEntry.FileOffset;
                var dataSize = tocEntry.DataSize;
                var compress = tocEntry.Compress;
                try {
                    var stream = GetZipArchive();
                    if (stream == null) {
                        unpackingError = "Resource not found.";
                        return false;
                    }
                    using (var reader = new BinaryReader(stream)) {
                        // Check to make sure the local file header is correct
                        reader.BaseStream.Seek(fileOffset, SeekOrigin.Begin);
                        var l = reader.ReadInt32();
                        if (l != 0x04034B50) {
                            // Bad: Local File Header
                            unpackingError = "Bad local file header in ZIP resource.";
                            return false;
                        }
                        reader.BaseStream.Seek(fileOffset + 26, SeekOrigin.Begin);
                        l = 30 + reader.ReadInt16() + reader.ReadInt16(); // local header size
                        fileOffset += l; // start of file data

                        reader.BaseStream.Seek(fileOffset, SeekOrigin.Begin);
                        byte[] rawData;
                        try {
                            rawData = reader.ReadBytes(compress == 0 ? dataSize : dataSize + 1);
                        }
                        catch {
                            unpackingError = "Can't read data";
                            return false;
                        }

                        if (compress != 0) {
                            rawData[dataSize] = (byte) 'Z';
                        }

                        result = compress == 0 ? rawData : ZlibModule.Decompress(rawData, -15);
                        return true;
                    }
                }
                catch (Exception exception) {
                    unpackingError = String.Format("{0}: {1}", exception.GetType().Name, exception.Message);
                    return false;
                }
            }
 private byte[] GetData(PackedResourceInfo tocEntry) {
     string unpackingError;
     byte[] result;
     if (!_loader.GetData(tocEntry, out result, out unpackingError))
         throw MakeError(unpackingError);
     return result;
 }
        private string GetCodeFromData(CodeContext /*!*/ context, bool isbytecode, PackedResourceInfo tocEntry) {
            byte[] data = GetData(tocEntry);
            string code = null;

            if (data != null) {
                if (isbytecode) {
                    // would put in code to unmarshal the bytecode here...                                     
                }
                else {
                    code = context.LanguageContext.DefaultEncoding.GetString(data, 0, data.Length);
                }
            }
            return code;
        }