Ejemplo n.º 1
0
        public override string ReadBytes(Stream stream)
        {
            int len = stream.ReadInt32();

            byte[] bytes = stream.ReadBytes(len);
            return(DefaultEncoding.GetString(bytes));
        }
Ejemplo n.º 2
0
        private static void ReadZipFileComment(ZipFile zf)
        {
            // read the comment here
            byte[] block = new byte[2];
            zf.ReadStream.Read(block, 0, block.Length);

            Int16 commentLength = (short)(block[0] + block[1] * 256);

            if (commentLength > 0)
            {
                block = new byte[commentLength];
                zf.ReadStream.Read(block, 0, block.Length);

                // workitem 6513 - only use UTF8 as necessary
                // test reflexivity
                string s1 = DefaultEncoding.GetString(block, 0, block.Length);
                byte[] b2 = DefaultEncoding.GetBytes(s1);
                if (BlocksAreEqual(block, b2))
                {
                    zf.Comment = s1;
                }
                else
                {
                    // need alternate (non IBM437) encoding
                    // workitem 6415
                    // use UTF8 if the caller hasn't already set a non-default encoding
                    System.Text.Encoding e = (zf._provisionalAlternateEncoding.CodePage == 437)
                        ? System.Text.Encoding.UTF8
                        : zf._provisionalAlternateEncoding;
                    zf.Comment = e.GetString(block, 0, block.Length);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Converts bytes using the default encoding
        /// </summary>
        private string ConvertBytesToStringWithDefaultEncoding(byte[] line)
        {
            if (line == null)
            {
                return("");
            }

            return(DefaultEncoding.GetString(line));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="buff"></param>
        public void Send(byte[] buff)
        {
            string recv = DefaultEncoding.GetString(buff);

            LogRevMsg.LogText("发送", recv);
            Lib.LogManager.Logger.LogInfo("发送:" + "\r" + recv);
            this.comm.Write(buff, 0, buff.Length);
            //MachineAdapter.Common.Manager.MonitorManager.Instance.SendLog(recv);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Decrypt with private key
        /// </summary>
        /// <param name="data"></param>
        /// <param name="encryptionEncodeMode"><see cref="CryptoBase.DefaultEncryptionEncodeMode"/></param>
        /// <param name="privateKey">Private key</param>
        /// <returns></returns>
        public string Decrypt(string data, EncodeMode encryptionEncodeMode, string privateKey)
        {
            if (string.IsNullOrWhiteSpace(data))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(data));
            }
            if (string.IsNullOrWhiteSpace(privateKey))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(privateKey));
            }

            return(DefaultEncoding.GetString(Decrypt(encryptionEncodeMode.DecodeToBytes(data, DefaultEncoding), privateKey)));
        }
Ejemplo n.º 6
0
        public void DefaultEncodingTest()
        {
            // from PS 3.5-2009, Page 107
            // H.3.2 Example 2: Value 1 of Attribute Specific Character Set (0008,0005) is ISO 2022 IR 13.

            byte[] bytes = new byte[] { 0xd4, 0xcf, 0xc0, 0xde, 0x5e, 0xc0, 0xdb, 0xb3, 0x3d, 0x1b, 0x24, 0x42, 0x3b, 0x33, 0x45, 0x44,
                                        0x1b, 0x28, 0x4a, 0x5e, 0x1b, 0x24, 0x42, 0x42, 0x40, 0x4f, 0x3a, 0x1b, 0x28, 0x4a, 0x3d, 0x1b,
                                        0x24, 0x42, 0x24, 0x64, 0x24, 0x5e, 0x24, 0x40, 0x1b, 0x28, 0x4a, 0x5e, 0x1b, 0x24, 0x42, 0x24,
                                        0x3f, 0x24, 0x6d, 0x24, 0x26, 0x1b, 0x28, 0x4a };

            DefaultEncoding encoding = new DefaultEncoding();

            string text = "\x00d7d4\x00d7cf\x00d7c0\x00d7de^\x00d7c0\x00d7db\x00d7b3=\x00d71b$B;3ED\x00d71b(J^\x00d71b$BB@O:\x00d71b(J=\x00d71b$B$d$^$@\x00d71b(J^\x00d71b$B$?$m$&\x00d71b(J";

            string decode = encoding.GetString(bytes);

            Assert.AreEqual(decode, text);

            byte[] temp = encoding.GetBytes(text.ToCharArray());

            string second = encoding.GetString(temp);

            Assert.AreEqual(second, text);
        }
Ejemplo n.º 7
0
        public void KoreanTest()
        {
            byte[] bytes = new byte[] { 0x48, 0x6f, 0x6e, 0x67, 0x5e, 0x47, 0x69, 0x6c, 0x64, 0x6f, 0x6e, 0x67, 0x3d, 0x1b, 0x24, 0x29,
                                        0x43, 0xfb, 0xf3, 0x5e, 0x1b, 0x24, 0x29, 0x43, 0xd1, 0xce, 0xd4, 0xd7, 0x3d, 0x1b, 0x24, 0x29,
                                        0x43, 0xc8, 0xab, 0x5e, 0x1b, 0x24, 0x29, 0x43, 0xb1, 0xe6, 0xb5, 0xbf };
            string unicode = @"Hong^Gildong=洪^吉洞=홍^길동";

            // first test the default encoding
            DefaultEncoding encoding = new DefaultEncoding();

            string temp = encoding.GetString(bytes);

            Assert.AreEqual(temp, "Hong^Gildong=\x00d71b$)C\x00d7fb\x00d7f3^\x00d71b$)C\x00d7d1\x00d7ce\x00d7d4\x00d7d7=\x00d71b$)C\x00d7c8\x00d7ab^\x00d71b$)C\x00d7b1\x00d7e6\x00d7b5\x00d7bf", "Default decoding failed.");

            EncodeDecodeTest(@"\ISO 2022 IR 149", unicode, bytes);
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public virtual string Decrypt(string data, string key)
        {
            if (string.IsNullOrWhiteSpace(data))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(data));
            }
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(key));
            }

            var bytes   = DefaultEncryptionEncodeMode.DecodeToBytes(data, DefaultEncoding);
            var decrypt = Decrypt(bytes, key);

            return(DefaultEncoding.GetString(decrypt));
        }
        internal override void Read(MarkingBinaryReader reader)
        {
            Version           = reader.ReadUInt16();
            Flags             = reader.ReadUInt16();
            CompressionMethod = reader.ReadUInt16();
            LastModifiedTime  = reader.ReadUInt16();
            LastModifiedDate  = reader.ReadUInt16();
            Crc              = reader.ReadUInt32();
            CompressedSize   = reader.ReadUInt32();
            UncompressedSize = reader.ReadUInt32();
            ushort nameLength  = reader.ReadUInt16();
            ushort extraLength = reader.ReadUInt16();

            byte[] name = reader.ReadBytes(nameLength);
            Extra = reader.ReadBytes(extraLength);
            Name  = DefaultEncoding.GetString(name, 0, name.Length);
        }
Ejemplo n.º 10
0
        public string GetString(string key, int maxLen)
        {
            int[] offsets = ifsCurrentIniFileSection.GetIntList(key);

            if (offsets.Length == 0)
            {
                return(null);
            }

            int length = GetByte(key + "Length");

            if (length == 0)
            {
                length = maxLen;
            }

            return(DefaultEncoding.GetString(Data, BaseOffset + offsets[0], length).TrimEnd('\0'));
        }
Ejemplo n.º 11
0
 public string Unmarshal_string(Encoding encoding)
 {
     try
     {
         int size = Uncompact_uint32();
         if (_pos + size > Size)
         {
             throw new MarshalException();
         }
         int cur = _pos;
         _pos += size;
         return((encoding == null) ?
                DefaultEncoding.GetString(Buffer(), cur, size) : encoding.GetString(Buffer(), cur, size));
     }
     catch
     {
         throw new MarshalException();
     }
 }
Ejemplo n.º 12
0
        private string GetString(IEnumerable <byte> list)
        {
            var bytes = list.ToArray();

            var te = GetTransferEncoding();

            switch (te)
            {
            case System.Net.Mime.TransferEncoding.Base64:
                return(Convert.ToBase64String(bytes));

            case System.Net.Mime.TransferEncoding.QuotedPrintable:
            {
                var encoding = GetEncoding(this.ContentType.CharSet);
                return(encoding.GetString(bytes));
            }

            default:
                return(DefaultEncoding.GetString(bytes));
            }
        }
Ejemplo n.º 13
0
 public byte[] GetContent()
 {
     if (this._bodyMode == MimeReaderMode.FromBytes)
     {
         if (this.GetTransferEncoding() == System.Net.Mime.TransferEncoding.Base64 ||
             this.GetTransferEncoding() == System.Net.Mime.TransferEncoding.QuotedPrintable)
         {
             //хотя исходные данные были в виде массива байтов, надо их перекодировать
             var rawString = DefaultEncoding.GetString(this._bodyBytes);
             return(GetContent(rawString));
         }
         else
         {
             //можно вернуть исходный массив
             return(this._bodyBytes);
         }
     }
     else
     {
         return(GetContent(GetMessage()));
     }
 }
Ejemplo n.º 14
0
        internal override void Read(MarkingBinaryReader reader)
        {
            Version = reader.ReadUInt16();
            VersionNeededToExtract = reader.ReadUInt16();
            Flags             = reader.ReadUInt16();
            CompressionMethod = reader.ReadUInt16();
            LastModifiedTime  = reader.ReadUInt16();
            LastModifiedDate  = reader.ReadUInt16();
            Crc              = reader.ReadUInt32();
            CompressedSize   = reader.ReadUInt32();
            UncompressedSize = reader.ReadUInt32();
            ushort nameLength    = reader.ReadUInt16();
            ushort extraLength   = reader.ReadUInt16();
            ushort commentLength = reader.ReadUInt16();

            DiskNumberStart             = reader.ReadUInt16();
            InternalFileAttributes      = reader.ReadUInt16();
            ExternalFileAttributes      = reader.ReadUInt32();
            RelativeOffsetOfEntryHeader = reader.ReadUInt32();

            Name    = DefaultEncoding.GetString(reader.ReadBytes(nameLength));
            Extra   = reader.ReadBytes(extraLength);
            Comment = reader.ReadBytes(commentLength);
        }
Ejemplo n.º 15
0
        private byte[] ParseHeaders(byte[] data)
        {
            var crlf = DefaultEncoding.GetBytes(CRLF);

            var endOfHeader   = false;
            var currentHeader = "";
            int index         = 0;

            while (index < data.Length && !endOfHeader)
            {
                int lineBreakIndex = BytesExtensions.IndexOf(data, crlf, index);
                var breakFound     = (lineBreakIndex != -1);
                if (!breakFound)
                {
                    lineBreakIndex = data.Length;
                }

                int subStringLength = lineBreakIndex - index;

                var headerLineBytes = new byte[subStringLength];
                Array.Copy(data, index, headerLineBytes, 0, subStringLength);

                var line = DefaultEncoding.GetString(headerLineBytes);
                processHeaderLine(ref currentHeader, line, ref endOfHeader);
                index = lineBreakIndex;
                if (breakFound)
                {
                    index += crlf.Length;
                }
            }

            this._headerBytes = new byte[index];
            Array.Copy(data, this._headerBytes, index);
            processCommonHeaderAttribs();
            return(getUnprocessedData(data, index));
        }
Ejemplo n.º 16
0
        void comm_DataReceived_Kim(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                //log.Info("开始接收数据");
                byte[]             RS232DataBuff = GetData(comm);
                SerialHandleObject handle        = new SerialHandleObject();
                if (RS232Received != null)
                {
                    RS232Received(handle, RS232DataBuff);
                    if (handle.handled)
                    {
                        return;
                    }
                }
                //Encoding utf8 = Encoding.GetEncoding(65001);
                //Encoding gb2312 = Encoding.GetEncoding("gb2312");
                string recv = DefaultEncoding.GetString(RS232DataBuff);
                //LogRevMsg.LogText("单次接收", recv);
                //log.Info("单次接收" + "\r" + recv);

                if (!IsASTM)
                {
                    currStr += recv;

                    if (CheckEndStr(currStr, ETXStr))
                    {
                        foreach (byte b in RS232DataBuff)
                        {
                            handleComand(b);
                        }
                        LogRevMsg.LogText("接收", currStr);
                        Lib.LogManager.Logger.LogInfo("接收" + "\r" + currStr);
                        //MonitorManager.Instance.SendCommand(
                        //    RemoteCommand.CMD_LOG, new string[] { LocalConfig.Instance.ItrID, currStr }); //kim 发送至监控服务器
                        if (false)
                        {
                            LogRevMsg.GetFile(currStr);
                        }
                        else
                        {
                            LogRevMsg.GetBufferLog(currStr);
                        }
                        currStr = string.Empty;
                    }
                }
                else
                {
                    try
                    {
                        if (RS232DataBuff.Length == 2 && RS232DataBuff[0] == ASTMCommon.ACK_6 && RS232DataBuff[1] == ASTMCommon.EOT_4)
                        {
                            byte[] buff = new byte[1];
                            buff[0]       = RS232DataBuff[0];
                            RS232DataBuff = buff;
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message);
                    }
                    foreach (byte b in RS232DataBuff)
                    {
                        if (IsASTM)
                        {
                            HandleAstmCommand(b);
                        }
                        char RecChar = Convert.ToChar(b);
                        currStr += RecChar;
                        if (CheckEndStr(currStr, ETXStr))
                        {
                            LogRevMsg.LogText("接收", currStr);
                            Lib.LogManager.Logger.LogInfo("接收" + "\r" + currStr);
                            //MonitorManager.Instance.SendCommand(
                            //    RemoteCommand.CMD_LOG, new string[] { LocalConfig.Instance.ItrID, currStr }); //kim 发送至监控服务器
                            LogRevMsg.GetBufferLog(currStr);
                            currStr = string.Empty;
                        }
                        //if (CheckEndStr(SingleStr, ETXStr) || SingleStr.Contains(ASTMCommon.cENQ_5.ToString()))
                        //{
                        //    LogRevMsg.LogText("接收", SingleStr);
                        //    log.Info("接收" + "\r" + SingleStr);
                        //    //MonitorManager.Instance.SendCommand(
                        //    //    RemoteCommand.CMD_LOG, new string[] { LocalConfig.Instance.ItrID, currStr }); //kim 发送至监控服务器
                        //    if (!SingleStr.Contains(ASTMCommon.cEOT_4.ToString()))
                        //    {
                        //        SingleStr = string.Empty;
                        //    }
                        //}
                        //if (SingleStr.Contains(ASTMCommon.cEOT_4.ToString()))
                        //{
                        //    if (ETXStr != ASTMCommon.cEOT_4.ToString())
                        //    {
                        //        LogRevMsg.LogText("接收", SingleStr);
                        //        log.Info("接收" + "\r" + SingleStr);
                        //    }
                        //    //MonitorManager.Instance.SendCommand(
                        //    //    RemoteCommand.CMD_LOG, new string[] { LocalConfig.Instance.ItrID, currStr }); //kim 发送至监控服务器
                        //    LogRevMsg.GetBufferLog(currStr);
                        //    currStr = string.Empty;
                        //    SingleStr = string.Empty;
                        //    break;
                        //}
                    }
                }
                if (IsNeedAck)
                {
                    if (RS232DataBuff.Length == 1)
                    {
                        if (RS232DataBuff[0] == ASTMCommon.NAK_21)
                        {
                            return;
                        }
                        if (RS232DataBuff[0] == ASTMCommon.ACK_6)
                        {
                            return;
                        }
                    }
                    SendACK();
                }
            }
            catch (Exception ex)
            {
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Fetch the specified resource from the specified device and return the MIME type of the response.
        /// </summary>
        /// <param name="url">The URL including device and resource.</param>
        /// <param name="mimeType">The MIME type of the reponse.</param>
        /// <param name="appendSessionToken">Indicates whether the session token should automatically be appended if missing in the url.</param>
        /// <returns>The data fetched from the device encoded as a string using the current DefaultEncoding.</returns>
        public string FetchUrlAsString(string url, out string mimeType, bool appendSessionToken = true)
        {
            var result = FetchUrl(url, out mimeType, appendSessionToken);

            return(DefaultEncoding.GetString(result, 0, result.Length));
        }
Ejemplo n.º 18
0
        private void RecvThreadFunc()
        {
            List <byte> buf = new List <byte>(new byte[MAX_BYTES_NUMBER]);
            List <byte> real_buf = new List <byte>(new byte[MAX_BYTES_NUMBER + 1]);
            int         i = 0, j = 0, packet_size = 0;

            while (true)
            {
                int read_bytes = 0;

                try
                {
                    byte[] temp_buf = new byte[MAX_BYTES_NUMBER];
                    read_bytes = Stream.Read(temp_buf, 0, MAX_BYTES_NUMBER);
                    buf.InsertRange(0, temp_buf);
                }
                catch (Exception)
                {
                    goto delete_client;
                }

                if (read_bytes <= 0)
                {
                    goto delete_client;
                }

                buf.RemoveRange(read_bytes, MAX_BYTES_NUMBER - read_bytes);

                for (i = 0; i < read_bytes; i++)
                {
                    real_buf[i] = buf[i];
                    packet_size++;

                    if (buf[i] == EOP)
                    {
                        byte[] tmp_str = new byte[packet_size - 1];
                        Array.Copy(real_buf.ToArray(), tmp_str, packet_size - 1);

                        PacketProcessor(DefaultEncoding.GetString(tmp_str));

                        j           = 0;
                        packet_size = 0;
                    }
                    else
                    {
                        j++;
                    }
                }
                if (j != 0)
                {
                    real_buf[j] = EOP;

                    byte[] tmp_str = new byte[j - 1];
                    Array.Copy(real_buf.ToArray(), tmp_str, j - 1);

                    PacketProcessor(DefaultEncoding.GetString(tmp_str));
                }
            }

delete_client:
            if (IsNormalClose == false)
            {
                if (OnDisconnected != null)
                {
                    OnDisconnected(this, new DisconnectedEventArgs(ConnectedIP, ConnectedPort,
                                                                   DisconnectedEventArgs.ErrorCode.ServerError));
                }
            }
        }