Beispiel #1
0
        public int PutFile(string filename)
        {
            byte[] txBuf = new byte[516];
            int    txCount;
            string s = System.IO.File.ReadAllText(filename);

            System.IO.StringReader sr = new System.IO.StringReader(s);
            short         blockNumber;
            ASCIIEncoding ae = new ASCIIEncoding();

            sendTftpPacket(Client.OP_CODE.WRQ, filename, null, -1);//send WRQ
            char[] chunk      = new char[512];
            int    dataLength = sr.ReadBlock(chunk, 0, 512);

            do
            {
                txCount = recvTftpPacket(ref txBuf);
                OP_CODE op = (OP_CODE)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(txBuf, 0));
                System.Diagnostics.Debug.Assert(op == OP_CODE.ACK);
                blockNumber = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(txBuf, 2));
                //s += new string(ae.GetChars(rxBuf, sizeof(short) * 2, rxCount - 4 >= 4 ? rxCount - 4 : 0));
                byte[] data = new byte[dataLength];
                int    chrCount, byteCount;
                bool   completed;
                ae.GetEncoder().Convert(chunk, 0, dataLength, data, 0, data.Length, true, out chrCount, out byteCount, out completed);

                sendTftpPacket(Client.OP_CODE.DATA, string.Empty, data, ++blockNumber);
            } while ((dataLength = sr.ReadBlock(chunk, 0, 512)) == 512);
            //sendTftpPacket(Client.OP_CODE.ACK, string.Empty, null, blockNumber);//final ACK
            return(txCount);
        }
        /// <summary>
        /// Convert the given text to an ASCII representation as best as possible, mapping non-ASCII characters to their *compatible* equivalent
        /// whenever possible.
        ///
        /// WARNING: This does *not* transliterate non-euro languages to pinyin or other euro-alphabet representations.
        ///
        /// See also:
        /// - https://unicode.org/reports/tr15/#Compatibility_Composite_Figure
        /// - https://stackoverflow.com/questions/3288114/what-does-nets-string-normalize-do/3288164#answer-3288164
        /// - https://stackoverflow.com/questions/138449/how-to-convert-a-unicode-character-to-its-ascii-equivalent#comment-6466005
        /// - https://docs.microsoft.com/en-us/dotnet/api/system.text.normalizationform?view=netframework-4.8
        /// - https://stackoverflow.com/questions/31728249/convert-nepali-unicode-to-plain-text-in-c-sharp
        /// - http://www.pinyin.info/index.html
        /// - https://github.com/peterolson/hanzi-tools
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string StripToASCII(string source)
        {
            // the normalization to FormD / FormKD splits accented letters in accents+letters
            // the rest removes those accents (and other non-spacing characters)
            string rv = source.Normalize(System.Text.NormalizationForm.FormKD);

            char[] dst = new char[rv.Length];
            int    j   = 0;

            foreach (char c in rv)
            {
                UnicodeCategory t = CharUnicodeInfo.GetUnicodeCategory(c);
                switch (t)
                {
                case UnicodeCategory.UppercaseLetter:
                case UnicodeCategory.LowercaseLetter:
                case UnicodeCategory.TitlecaseLetter:
                case UnicodeCategory.DecimalDigitNumber:
                    //case UnicodeCategory.LetterNumber:
                    dst[j++] = c;      // keep as-is
                    continue;

                case UnicodeCategory.SpaceSeparator:
                    dst[j++] = ' ';
                    continue;

                case UnicodeCategory.LineSeparator:
                case UnicodeCategory.ParagraphSeparator:
                case UnicodeCategory.Control:                     // \r and \n are Category:Control!
                    dst[j++] = '\n';
                    continue;

                default:
                    // skip 'em
                    continue;
                }
            }
            Array.Resize(ref dst, j);

            //rv = new string(dst);

            // https://stackoverflow.com/questions/497782/how-to-convert-transliterate-a-string-from-utf8-to-ascii-single-byte-in-c
            ASCIIEncoding ASCII_encoder = new ASCIIEncoding();
            Encoder       encoder       = ASCII_encoder.GetEncoder();

            encoder.Fallback = new EncoderReplacementFallback(string.Empty);

            // https://docs.microsoft.com/en-us/dotnet/api/system.text.encoder.getbytes
            byte[] b = new byte[j * 3];
            int    in_l;
            int    out_l;
            bool   done;

            encoder.Convert(dst, 0, j, b, 0, byteCount: j * 3, flush: true, out in_l, out out_l, out done);
            Array.Resize(ref b, out_l);
            rv = Encoding.ASCII.GetString(b);

            return(rv);
        }
        public void PosTest1()
        {
            ASCIIEncoding ascii;
            Encoder       actualEncoder;

            ascii         = new ASCIIEncoding();
            actualEncoder = ascii.GetEncoder();
            Assert.NotNull(actualEncoder);
        }
        public void PosTest1()
        {
            ASCIIEncoding ascii;
            Encoder actualEncoder;

            ascii = new ASCIIEncoding();
            actualEncoder = ascii.GetEncoder();
            Assert.NotNull(actualEncoder);
        }
        /// <summary>
        /// Add a standard EXIF field to the image
        /// </summary>
        /// <param name="image"></param>
        /// <param name="field"></param>
        /// <param name="fieldValue"></param>
        private static void WriteEXIFField(Image image, ExifField field, string fieldValue)
        {
            Encoding asciiEncoding = new ASCIIEncoding();

            System.Text.Encoder encoder = asciiEncoding.GetEncoder();
            char[] tagTextChars         = fieldValue.ToCharArray();
            int    byteCount            = encoder.GetByteCount(tagTextChars, 0, tagTextChars.Length, true);

            byte[] tagTextBytes = new byte[byteCount];
            encoder.GetBytes(tagTextChars, 0, tagTextChars.Length, tagTextBytes, 0, true);

            if (image.PropertyItems != null && image.PropertyItems.Length > 0)
            {
                PropertyItem propertyItem = image.PropertyItems[0];
                propertyItem.Id    = (int)field;
                propertyItem.Type  = 2; // ASCII
                propertyItem.Len   = tagTextBytes.Length;
                propertyItem.Value = tagTextBytes;
                image.SetPropertyItem(propertyItem);
            }
        }