Beispiel #1
0
        public void CheckParseBase64_Fixed()
        {
            string[] str_list = new string[]
            {
                "ABCDEFG",
                "abcdefg",
                "ABCDEF",
                "abcdef",
            };

            var bytes_native   = new NativeList <byte>(Allocator.Persistent);
            var encoded_native = new NativeList <Char16>(Allocator.Persistent);
            var bytes_decoded  = new NativeList <byte>(Allocator.Persistent);

            var b64_encoder = new NativeBase64Encoder(Allocator.Persistent);
            var b64_decoder = new NativeBase64Decoder(Allocator.Persistent);

            foreach (var str_ref in str_list)
            {
                byte[] bytes_ref   = Encoding.UTF8.GetBytes(str_ref);
                string encoded_ref = Convert.ToBase64String(bytes_ref);

                bytes_native.Clear();
                encoded_native.Clear();
                bytes_decoded.Clear();

                foreach (var b in bytes_ref)
                {
                    bytes_native.Add(b);
                }

                b64_encoder.Clear();
                b64_encoder.GetChars(encoded_native, bytes_native);

                CheckBase64EncodedStr(encoded_native, encoded_ref, true);

                b64_decoder.Clear();
                b64_decoder.GetBytes(bytes_decoded, encoded_native);

                CheckBase64DecodedBytes(bytes_decoded, bytes_ref, true);
            }

            bytes_native.Dispose();
            encoded_native.Dispose();
            bytes_decoded.Dispose();

            b64_encoder.Dispose();
            b64_decoder.Dispose();
        }
Beispiel #2
0
        private bool ParseLineImpl(ReadOnlyStringEntity line)
        {
            pack.Lines++;
            if (line.Length < 1)
            {
                return(true);
            }

            if (EnableBurstFunc)
            {
                BurstFunc.Split(line, '\t', _str_list);
            }
            else
            {
                line.Split('\t', _str_list);
            }

            //--- check data block
            if (_str_list.Length >= 2 && _str_list[0] == pack.mark_tag)
            {
                if (_str_list[1] == pack.mark_header)
                {
                    pack.read_mode = ReadMode.Header;
                }
                else if (_str_list[1] == pack.mark_ext)
                {
                    pack.read_mode = ReadMode.ExtData;
                }
                else if (_str_list[1] == pack.mark_ext_end)
                {
                    pack.read_mode = ReadMode.None;
                }
                else if (_str_list[1] == pack.mark_body)
                {
                    //--- check header info was read correctly or not
                    if (pack.N <= 0 || pack.D <= 0 || pack.R < 0.0 || pack.R >= 1.0)
                    {
                        pack.read_mode = ReadMode.HeaderError;
                        return(false);
                    }
                    pack.read_mode = ReadMode.Body;
                }
                return(true);
            }
            if (pack.read_mode == ReadMode.None)
            {
                return(true);
            }

            //--- ignore comment line
            if (_str_list[0].Length >= 1 && _str_list[0].Slice(0, 1) == pack.mark_comment)
            {
                return(true);
            }

            //--- store data
            if (pack.read_mode == ReadMode.Header)
            {
                bool success = true;
                if (EnableBurstFunc)
                {
                    // using BurstCompiler applied TryParse
                    if (_str_list[0] == pack.mark_n_total)
                    {
                        success = BurstFunc.TryParse(_str_list[1], out pack.N);
                    }
                    else if (_str_list[0] == pack.mark_d)
                    {
                        success = BurstFunc.TryParse(_str_list[1], out pack.D);
                    }
                    else if (_str_list[0] == pack.mark_r)
                    {
                        success = BurstFunc.TryParse(_str_list[1], out pack.R);
                    }
                }
                else
                {
                    // using normal TryParse()
                    if (_str_list[0] == pack.mark_n_total)
                    {
                        success = _str_list[1].TryParse(out pack.N);
                    }
                    else if (_str_list[0] == pack.mark_d)
                    {
                        success = _str_list[1].TryParse(out pack.D);
                    }
                    else if (_str_list[0] == pack.mark_r)
                    {
                        success = _str_list[1].TryParse(out pack.R);
                    }
                }

                if (!success)
                {
                    pack.read_mode = ReadMode.HeaderError;
                    return(false);
                }
            }
            else if (pack.read_mode == ReadMode.ExtData)
            {
                if (_str_list.Length > 1)
                {
                    // must be 1 element in line
                    pack.read_mode = ReadMode.Base64DataError;
                    return(false);
                }
                else if (_str_list.Length == 1)
                {
                    bool success = true;
                    if (EnableBurst)
                    {
                        // using BurstCompiler applied GetChars()
                        success = BurstFunc.GetBytes(_b64_decoder, _b64_decoded_bytes, _str_list[0]);
                    }
                    else
                    {
                        // using normal GetChars()
                        success = _b64_decoder.GetBytes(_b64_decoded_bytes, _str_list[0]);
                    }

                    if (!success)
                    {
                        pack.read_mode = ReadMode.Base64DataError;
                        return(false);
                    }
                }
            }
            else if (pack.read_mode == ReadMode.Body)
            {
                if (_str_list.Length < 6)
                {
                    return(true);
                }

                var  tmp     = new CharaData();
                bool success = true;
                if (EnableBurstFunc)
                {
                    // using BurstCompiler applied TryParse
                    success = success && BurstFunc.TryParse(_str_list[0], out tmp.ID);
                    success = success && BurstFunc.TryParse(_str_list[2], out tmp.HP);
                    success = success && BurstFunc.TryParse(_str_list[3], out tmp.MP);
                    success = success && BurstFunc.TryParse(_str_list[4], out tmp.Attack);
                    success = success && BurstFunc.TryParse(_str_list[5], out tmp.Defence);
                }
                else
                {
                    // using normal TryParse()
                    success = success && _str_list[0].TryParse(out tmp.ID);
                    success = success && _str_list[2].TryParse(out tmp.HP);
                    success = success && _str_list[3].TryParse(out tmp.MP);
                    success = success && _str_list[4].TryParse(out tmp.Attack);
                    success = success && _str_list[5].TryParse(out tmp.Defence);
                }

                if (!success)
                {
                    pack.read_mode = ReadMode.FormatError;
                    return(false);
                }

                _tmp_name.Add(_str_list[1]);
                tmp.Name = _tmp_name.Last;

                Data.Add(tmp);

                if (Data.Length > pack.N)
                {
                    pack.read_mode = ReadMode.FormatError;
                    return(false);
                }
            }

            return(true);
        }
Beispiel #3
0
        public void CheckParseBase64_Random(int n)
        {
            Assert.IsTrue(n > 0);
            const int Base64InLine = 76;

            var bytes_native   = new NativeList <byte>(Allocator.Persistent);
            var encoded_native = new NativeList <Char16>(Allocator.Persistent);
            var bytes_decoded  = new NativeList <byte>(Allocator.Persistent);

            var b64_encoder = new NativeBase64Encoder(Allocator.Persistent);
            var b64_decoder = new NativeBase64Decoder(Allocator.Persistent);

            long total_bytes = 0;

            for (int i = 0; i < n; i++)
            {
                int byte_len = random.Next(16, 256);
                total_bytes += byte_len;

                byte[] bytes_ref = new byte[byte_len];
                bytes_native.Clear();
                for (int j = 0; j < byte_len; j++)
                {
                    byte b = (byte)random.Next(0, 255);
                    bytes_ref[j] = b;
                    bytes_native.Add(b);
                }

                string encoded_ref = Convert.ToBase64String(bytes_ref);

                var sb        = new StringBuilder();
                int charcount = 0;
                foreach (char c in encoded_ref)
                {
                    if (charcount == Base64InLine)
                    {
                        sb.Append("\r\n");
                        charcount = 0;
                    }

                    sb.Append(c);
                    charcount++;
                }
                string encoded_ref_withLF = sb.ToString();

                // test for encoded str with CRLF
                encoded_native.Clear();
                bytes_decoded.Clear();

                b64_encoder.Clear();
                b64_encoder.GetChars(encoded_native, bytes_native);

                CheckBase64EncodedStr(encoded_native, encoded_ref_withLF);

                b64_decoder.Clear();
                b64_decoder.GetBytes(bytes_decoded, encoded_native);

                CheckBase64DecodedBytes(bytes_decoded, bytes_ref);

                // test for encoded str without CRLF
                encoded_native.Clear();
                bytes_decoded.Clear();

                b64_encoder.Clear();
                b64_encoder.InsertLineBrakes = false;  // default: encode with "CRLF".
                b64_encoder.GetChars(encoded_native, bytes_native);
                b64_encoder.InsertLineBrakes = true;

                CheckBase64EncodedStr(encoded_native, encoded_ref);

                b64_decoder.Clear();
                b64_decoder.GetBytes(bytes_decoded, encoded_native);

                CheckBase64DecodedBytes(bytes_decoded, bytes_ref);
            }

            Debug.Log("total test bytes: " + total_bytes.ToString() + " B\n");

            bytes_native.Dispose();
            encoded_native.Dispose();
            bytes_decoded.Dispose();

            b64_encoder.Dispose();
            b64_decoder.Dispose();
        }