public void GetChars(UnsafeRefToNativeList <Char16> buff,
                      byte *byte_ptr,
                      int byte_len,
                      bool splitData = false)
 {
     NativeBase64Encoder.GetCharsImpl(info_ptr, buff, byte_ptr, byte_len, splitData);
 }
Esempio n. 2
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();
        }
        private unsafe void GenerateImpl(Encoding encoding, int n, int d, float r)
        {
            _i_chara = 0;
            var sb = new StringBuilder();

            Debug.Log(" >> write header >> ");

            //--- header
            sb.Append("#=======================================");
            sb.Append(_lf);
            sb.Append("# the belong area is a header data.");
            sb.Append(_lf);
            sb.Append("#=======================================");
            sb.Append(_lf);
            sb.Append("<@MARK>" + _delim + "Header");
            sb.Append(_lf);
            sb.Append("n_total" + _delim + n.ToString());
            sb.Append(_lf);
            sb.Append("d" + _delim + d.ToString());
            sb.Append(_lf);
            sb.Append("r" + _delim + r.ToString());
            sb.Append(_lf);

            sb.Append(_lf);  // insert empty lines
            sb.Append(_lf);
            sb.Append(_lf);


            //--- generate random id sequence
            var id_Sequence = new NativeList <int>(n, Allocator.Persistent);

            id_Sequence.Clear();
            for (int id = 0; id < n; id++)
            {
                id_Sequence.Add(id * d);
            }
            //int n_swap = (int)Math.Sqrt(n);
            int n_swap = n;
            var random = new System.Random(n_swap * d);

            for (int i_swap = 0; i_swap < n_swap; i_swap++)
            {
                int index = (int)(n * random.NextDouble() * 0.95);
                var tmp   = id_Sequence[index];
                id_Sequence.RemoveAtSwapBack(index);
                id_Sequence.Add(tmp);
            }

            Debug.Log(" >> write Ex Data >> ");

            //--- Ex data
            sb.Append("#=======================================");
            sb.Append(_lf);
            sb.Append("# the belong area is a Base64 encorded ext data.");
            sb.Append(_lf);
            sb.Append("#=======================================");
            sb.Append(_lf);
            sb.Append("<@MARK>" + _delim + "ExtData");
            sb.Append(_lf);

            int byte_len      = id_Sequence.Length * UnsafeUtility.SizeOf <int>();
            var byte_Sequence = new NativeArray <byte>(byte_len, Allocator.Persistent);

            UnsafeUtility.MemCpy(byte_Sequence.GetUnsafePtr(), id_Sequence.GetUnsafePtr(), byte_len);

            var b64_Sequence = new NativeList <Char16>(Allocator.Persistent);
            var b64_Encoder  = new NativeBase64Encoder(Allocator.Persistent);

            b64_Encoder.GetChars(b64_Sequence, byte_Sequence);
            for (int i = 0; i < b64_Sequence.Length; i++)
            {
                sb.Append(b64_Sequence[i]);
            }
            sb.Append(_lf);
            sb.Append("<@MARK>" + _delim + "ExtDataEnd");

            sb.Append(_lf);
            sb.Append(_lf);

            byte_Sequence.Dispose();
            b64_Sequence.Dispose();
            b64_Encoder.Dispose();

            Debug.Log(" >> write body >> ");

            //--- body
            sb.Append("#=======================================");
            sb.Append(_lf);
            sb.Append("# the belong area is a data body.");
            sb.Append(_lf);
            sb.Append("#=======================================");
            sb.Append(_lf);
            sb.Append("<@MARK>" + _delim + "Body");
            sb.Append(_lf);

            var dummy_line = "# ---" + _delim + "dummy" + _delim + "line" + _delim + "--- #";

            //--- write CharaData
            var NSL_tmp = new NativeStringList(Allocator.Persistent);

            using (StreamWriter writer = new StreamWriter(_path, false, encoding))
            {
                int i = 0;
                _i_chara = i;
                while (i < n)
                {
                    if (random.NextDouble() > r)
                    {
                        NSL_tmp.Clear();
                        int id        = id_Sequence[i];
                        var chara_tmp = CharaDataExt.Generate(id, NSL_tmp);
                        sb.Append(chara_tmp.ToString(_delim));
                        sb.Append(_lf);
                        i++;
                        _i_chara = i;

                        //Debug.Log("   -- body [" + i.ToString() + '/' + n.ToString() + ']');
                    }
                    else
                    {
                        sb.Append(dummy_line);
                        sb.Append(_lf);
                    }

                    if (sb.Length > 4096)  // 4k: page size
                    {
                        writer.Write(sb);
                        sb.Clear();
                    }
                }

                if (sb.Length > 0)
                {
                    writer.Write(sb);
                }
            }

            id_Sequence.Dispose();
            NSL_tmp.Dispose();

            _standby = true;
        }
 public void Terminate(UnsafeRefToNativeList <Char16> buff)
 {
     NativeBase64Encoder.TerminateImpl(info_ptr, buff);
 }
 public void GetChars(UnsafeRefToNativeList <Char16> buff,
                      UnsafeRefToNativeList <byte> bytes,
                      bool splitData = false)
 {
     NativeBase64Encoder.GetCharsImpl(info_ptr, buff, (byte *)bytes.GetUnsafePtr(), bytes.Length, splitData);
 }
 public UnsafeRefToNativeBase64Encoder(NativeBase64Encoder encoder)
 {
     info_ptr = encoder._info.Target;
 }
 public static UnsafeRefToNativeBase64Encoder GetUnsafeRef(this NativeBase64Encoder target)
 {
     return(new UnsafeRefToNativeBase64Encoder(target));
 }
Esempio n. 8
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();
        }