Esempio n. 1
0
        private byte[] PackedChunk(ResourceIndexEntry ie)
        {
            byte[] chunk;
            if (ie.IsDirty)
            {
                Stream       res = GetResource(ie);
                BinaryReader r   = new BinaryReader(res);

                res.Position = 0;
                chunk        = r.ReadBytes((int)ie.Memsize);
                if (Checking)
                {
                    if (chunk.Length != (int)ie.Memsize)
                    {
                        throw new OverflowException(
                                  $"packedChunk, dirty resource - T: 0x{ie.ResourceType:X}, G: 0x{ie.ResourceGroup:X}, I: 0x{ie.Instance:X}: Length expected: 0x{ie.Memsize:X}, read: 0x{chunk.Length:X}");
                    }
                }

                byte[] comp = ie.Compressed != 0 ? Compression.CompressStream(chunk) : chunk;
                if (comp.Length < chunk.Length)
                {
                    chunk = comp;
                }
            }
            else
            {
                if (Checking)
                {
                    if (_packageStream == null)
                    {
                        throw new InvalidOperationException(
                                  $"Clean resource with undefined \"current package\" - T: 0x{ie.ResourceType:X}, G: 0x{ie.ResourceGroup:X}, I: 0x{ie.Instance:X}");
                    }
                }
                _packageStream.Position = ie.Chunkoffset;
                chunk = (new BinaryReader(_packageStream)).ReadBytes((int)ie.Filesize);
                if (Checking)
                {
                    if (chunk.Length != (int)ie.Filesize)
                    {
                        throw new OverflowException(
                                  $"packedChunk, clean resource - T: 0x{ie.ResourceType:X}, G: 0x{ie.ResourceGroup:X}, I: 0x{ie.Instance:X}: Length expected: 0x{ie.Filesize:X}, read: 0x{chunk.Length:X}");
                    }
                }
            }
            return(chunk);
        }
Esempio n. 2
0
        public Stream InvokeMethod(Stream Params)
        {
            String            ipAddress  = "";
            OperationContext  context    = OperationContext.Current;
            MessageProperties properties = context.IncomingMessageProperties;

            if (properties.ContainsKey(RemoteEndpointMessageProperty.Name))
            {
                RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                ipAddress = string.Format("{0}:{1}", endpoint.Address, endpoint.Port);
            }
            List <string> parameters = new List <string>();

            try
            {
                ApplicationContext AppContext = ApplicationContext.Current;
                AppContext.Identification.IPAddress = ipAddress;
                LoginLog(AppContext);
            }
            catch (Exception ex)
            {
                logger.Error("log login:"******"";

            try
            {
                Stream ms = ReadMemoryStream(Params);
                //Params.Dispose();
                Stream unzipstream = Yqun.Common.Encoder.Compression.DeCompressStream(ms);
                //ms.Dispose();
                Hashtable paramsList = Yqun.Common.Encoder.Serialize.DeSerializeFromStream(unzipstream) as Hashtable;
                //unzipstream.Dispose();
                string path = ServerLoginInfos.DBConnectionInfo.LocalStartPath;
                parameters.Add(path);
                string Assembly_Name = paramsList["assembly_name"].ToString();
                parameters.Add(Assembly_Name);
                string FileName = Path.Combine(path.Trim(), Assembly_Name.Trim());
                parameters.Add(FileName);
                Method_Name       = paramsList["method_name"].ToString();
                Method_Paremeters = paramsList["method_paremeters"] as object[];
                object    o = InvokeMethod(FileName, Method_Name, Method_Paremeters);
                Hashtable t = new Hashtable();
                t.Add("return_value", o);

                Stream stream    = Serialize.SerializeToStream(t);
                Stream zipstream = Compression.CompressStream(stream);
                //stream.Dispose();
                return(zipstream);
            }
            catch (Exception ex)
            {
                String log = "";
                foreach (var item in Method_Paremeters)
                {
                    log += item.ToString() + ";";
                }


                logger.Error(string.Format("[{0}]访问服务出错,原因为“{1}”,参数列表为{2}, 传入参数为{3},方法名称{4}",
                                           ApplicationContext.Current.UserName, ex.Message,
                                           string.Join(",", parameters.ToArray()),
                                           log,
                                           Method_Name
                                           ));
            }

            return(null);
        }
Esempio n. 3
0
        public void Write(Stream output, IDirectory inputDirectory)
        {
            using (Stream inputStream = inputDirectory.ReadFileStream(Hashing.NormalizeFilePath(FilePath)))
                using (Md5Stream md5OutputStream = new Md5Stream(output))
                {
                    long      headerPosition    = output.Position;
                    const int entryHeaderSize   = 32;
                    long      dataStartPosition = headerPosition + entryHeaderSize;
                    output.Position = dataStartPosition;

                    uint uncompressedSize = (uint)inputStream.Length;

                    Stream outputDataStream           = md5OutputStream;
                    Stream outputDataStreamCompressed = null;
                    if (Compressed)
                    {
                        outputDataStreamCompressed = Compression.CompressStream(outputDataStream);
                        outputDataStream           = outputDataStreamCompressed;
                    }

                    if (Encryption != 0)
                    {
                        int encryptionHeaderSize = Cryptography.GetHeaderSize(Encryption);
                        if (encryptionHeaderSize >= 8)
                        {
                            byte[] header = new byte[encryptionHeaderSize];
                            Buffer.BlockCopy(BitConverter.GetBytes(Encryption), 0, header, 0, sizeof(uint));
                            Buffer.BlockCopy(BitConverter.GetBytes(Key), 0, header, 4, sizeof(uint));
                            if (encryptionHeaderSize == 16)
                            {
                                Buffer.BlockCopy(BitConverter.GetBytes(uncompressedSize), 0, header, 8, sizeof(uint));
                                Buffer.BlockCopy(BitConverter.GetBytes(uncompressedSize), 0, header, 12, sizeof(uint));
                            }

                            using (var headerStream = new MemoryStream(header))
                            {
                                headerStream.CopyTo(outputDataStream);
                            }
                        }

                        outputDataStream = new Decrypt2Stream(outputDataStream, (int)uncompressedSize, Key, StreamMode.Write);
                    }

                    inputStream.CopyTo(outputDataStream);
                    outputDataStreamCompressed?.Close();

                    // TODO: HACK to support repacked files
                    if (DataHash == null)
                    {
                        md5OutputStream.Flush();
                        DataHash = md5OutputStream.Hash;
                    }

                    long dataEndPosition = output.Position;
                    uint compressedSize  = (uint)(dataEndPosition - dataStartPosition);
                    uncompressedSize = Compressed ? uncompressedSize : compressedSize;
                    using (var decrypt1Stream = new Decrypt1Stream(output, (int)Version, (int)compressedSize, DataHash, hashLow: (uint)(Hash & 0xFFFFFFFF), streamMode: StreamMode.Write))
                    {
                        CopyTo(output, decrypt1Stream, dataStartPosition, dataEndPosition);
                    }

                    output.Position = headerPosition;

                    const ulong  xorMask1Long = 0x4144104341441043;
                    const uint   xorMask1     = 0x41441043;
                    const uint   xorMask2     = 0x11C22050;
                    const uint   xorMask3     = 0xD05608C3;
                    const uint   xorMask4     = 0x532C7319;
                    BinaryWriter writer       = new BinaryWriter(output, Encoding.ASCII, true);
                    writer.Write(Hash ^ xorMask1Long);
                    writer.Write((Version != 2 ? uncompressedSize : compressedSize) ^ xorMask2);
                    writer.Write((Version != 2 ? compressedSize : uncompressedSize) ^ xorMask3);
                    writer.Write(BitConverter.ToUInt32(DataHash, 0) ^ xorMask4);
                    writer.Write(BitConverter.ToUInt32(DataHash, 4) ^ xorMask1);
                    writer.Write(BitConverter.ToUInt32(DataHash, 8) ^ xorMask1);
                    writer.Write(BitConverter.ToUInt32(DataHash, 12) ^ xorMask2);

                    output.Position = dataEndPosition;
                }
        }