コード例 #1
0
        private static long ReadLength(InputReader reader)
        {
            int len = reader.ReadInt32();

            if (len < -1)
            {
                throw new InvalidDataException();
            }
            else if (len == -1)
            {
                uint len1 = (uint)reader.ReadInt32();
                uint len2 = (uint)reader.ReadInt32();

                long xlen = len1;
                if (len1 > 65536)
                {
                    throw new InvalidDataException();
                }

                return((xlen << 32) + len2);
            }
            else
            {
                return(len);
            }
        }
コード例 #2
0
        private static IRNode UnserializeDecompressed(Stream stream)
        {
            using (BinaryReader binaryReader = new BinaryReader(stream, Encoding.GetEncoding("us-ascii"), true))
            {
                InputReader reader = InFormat(binaryReader);

                /* Read the version numbers */
                int version         = reader.ReadInt32();
                int writer_version  = reader.ReadInt32();
                int release_version = reader.ReadInt32();

                if (version != 2)
                {
                    DecodeVersion(writer_version, out int vw, out int pw, out int sw);
                    if (release_version < 0)
                    {
                        throw new NotSupportedException($"Cannot read unreleased workspace version {version} written by experimental R {vw}.{pw}.{sw}.");
                    }
                    else
                    {
                        DecodeVersion(release_version, out int vm, out int pm, out int sm);
                        throw new NotSupportedException($"Cannot read workspace version {version} written by R {vw}.{pw}.{sw}. Need R {vm}.{pm}.{sm} or newer.");
                    }
                }

                /* Read the actual object back */
                return(ReadItem(reader));
            }
        }
コード例 #3
0
        private static IRNode UnserializeDecompressed(Stream stream)
        {
            using (BinaryReader binaryReader = new BinaryReader(stream, Encoding.GetEncoding("us-ascii"), true))
            {
                InputReader reader = InFormat(binaryReader);

                /* Read the version numbers */
                int version         = reader.ReadInt32();
                int writer_version  = reader.ReadInt32();
                int release_version = reader.ReadInt32();

                if (version == 2)
                {
                    // nothing to do here
                }
                if (version == 3)
                {
                    int nelen = reader.ReadInt32();
                    if (nelen > /*R_CODESET_MAX*/ 63)
                    {
                        throw new InvalidDataException("Invalid length of encoding name");
                    }

                    IRString native_encoding = reader.ReadString(nelen, RString.CharEncoding.Native);
                    // InString(stream, stream->native_encoding, nelen);
                    // stream->native_encoding[nelen] = '\0';
                }
                else
                {
                    DecodeVersion(writer_version, out int vw, out int pw, out int sw);
                    if (release_version < 0)
                    {
                        throw new NotSupportedException($"Cannot read unreleased workspace version {version} written by experimental R {vw}.{pw}.{sw}.");
                    }
                    else
                    {
                        DecodeVersion(release_version, out int vm, out int pm, out int sm);
                        throw new NotSupportedException($"Cannot read workspace version {version} written by R {vw}.{pw}.{sw}. Need R {vm}.{pm}.{sm} or newer.");
                    }
                }

                /* Read the actual object back */
                return(ReadItem(reader));
            }
        }
コード例 #4
0
        private static int ReadRefIndex(InputReader reader, int flags)
        {
            int i = flags >> 8;

            if (i == 0)
            {
                return(reader.ReadInt32());
            }
            else
            {
                return(i);
            }
        }
コード例 #5
0
        private static IRNode ReadItem(InputReader reader)
        {
            int flags = reader.ReadInt32();

            UnpackFlags(flags, out SEXPTYPE type, out int levels, out bool isObject, out bool hasAttribute, out bool hasTag);

            switch (type)
            {
            case SEXPTYPE.NILVALUE_SXP:
                return(null);

            case SEXPTYPE.EMPTYENV_SXP:
                return(RObject.EmptyEnvironment);

            case SEXPTYPE.BASEENV_SXP:
                return(RObject.BaseEnvironment);

            case SEXPTYPE.GLOBALENV_SXP:
                return(RObject.GlobalEnvironment);

            case SEXPTYPE.UNBOUNDVALUE_SXP:
                return(RObject.UnboundValue);

            case SEXPTYPE.MISSINGARG_SXP:
                return(RObject.MissingArg);

            case SEXPTYPE.BASENAMESPACE_SXP:
                return(RObject.BaseNamespace);

            case SEXPTYPE.REFSXP:
                int refIndex = ReadRefIndex(reader, flags);
                // return GetReadRef(ref_table, InRefIndex(stream, flags));
                return(null);

            case SEXPTYPE.PERSISTSXP:
                // return PersistentRestore(reader, InStringVec(reader));
                ThrowTypeNotSupported("PERSISTSXP");
                break;

            case SEXPTYPE.SYMSXP:
                return(ReadItem(reader));

            // return installChar(ReadItem(reader));
            case SEXPTYPE.PACKAGESXP:
                // return R_FindPackageEnv(InStringVec(reader));
                ThrowTypeNotSupported("PACKAGESXP");
                break;

            case SEXPTYPE.NAMESPACESXP:
                // return R_FindNamespace1(InStringVec(reader));
                ThrowTypeNotSupported("NAMESPACESXP");
                break;

            case SEXPTYPE.ENVSXP:
            {
                /*int locked = reader.ReadInt32();
                 * RObject s = allocSExp(SEXPTYPE.ENVSXP);
                 * SET_ENCLOS(s, ReadItem(reader));
                 * SET_FRAME(s, ReadItem(reader));
                 * SET_HASHTAB(s, ReadItem(reader));
                 * SET_ATTRIB(s, ReadItem(reader));
                 *
                 * if (Attribute(s) != R_NilValue && getAttrib(s, R_ClassSymbol) != R_NilValue)
                 * {
                 *  SET_OBJECTS(s, 1);
                 * }
                 *
                 * if (locked != null)
                 * {
                 *  R_LockEnvironment(s, false);
                 * }
                 *
                 * if (ENCLOS(s) == R_NilValue)
                 * {
                 *  SET_ENCLOS(s, R_BaseEnv);
                 * }
                 *
                 * return s;*/
                ThrowTypeNotSupported("ENVSXP");
                break;
            }

            case SEXPTYPE.LISTSXP:
            case SEXPTYPE.LANGSXP:
            case SEXPTYPE.CLOSXP:
            case SEXPTYPE.PROMSXP:
            case SEXPTYPE.DOTSXP:
                IRList s = new RList((RNodeType)type)
                {
                    Levels    = levels,
                    IsObject  = isObject,
                    Attribute = hasAttribute ? ReadItem(reader) : null,
                    Tag       = hasTag ? ReadItem(reader) : null,
                    Head      = ReadItem(reader),
                    Tail      = (IRList)ReadItem(reader),
                };

                /*if (type == CLOSXP && CLOENV(s) == R_NilValue) SET_CLOENV(s, R_BaseEnv);
                 * else if (type == PROMSXP && PRENV(s) == R_NilValue) SET_PRENV(s, R_BaseEnv);*/

                return(s);

            default:
                IRNode result;
                switch (type)
                {
                case SEXPTYPE.CHARSXP:
                {
                    int length = reader.ReadInt32();
                    if (length == -1)
                    {
                        return(RString.NotAvailable);
                    }
                    else
                    {
                        RString.CharEncoding encoding = RString.CharEncoding.Native;
                        if ((levels & (1 << 3)) != 0)
                        {
                            encoding = RString.CharEncoding.Utf8;
                        }
                        else if ((levels & (1 << 2)) != 0)
                        {
                            encoding = RString.CharEncoding.Latin1;
                        }
                        else if ((levels & (1 << 1)) != 0)
                        {
                            encoding = RString.CharEncoding.Bytes;
                        }

                        return(reader.ReadString(length, encoding));
                    }
                }

                case SEXPTYPE.REALSXP:
                {
                    long         length = ReadLength(reader);
                    IRRealVector vector = new RRealVector(length);
                    reader.ReadRealVector(vector);

                    result = vector;
                    break;
                }

                case SEXPTYPE.INTSXP:
                {
                    long            length = ReadLength(reader);
                    IRIntegerVector vector = new RIntegerVector(length);
                    reader.ReadIntegerVector(vector);

                    result = vector;
                    break;
                }

                case SEXPTYPE.STRSXP:
                {
                    long           length = ReadLength(reader);
                    IRStringVector vector = new RStringVector(length);
                    for (long i = 0; i < length; i++)
                    {
                        vector[i] = ((IRString)ReadItem(reader)).String;
                    }

                    result = vector;

                    break;
                }

                case SEXPTYPE.VECSXP:
                case SEXPTYPE.EXPRSXP:
                {
                    long     length = ReadLength(reader);
                    IRVector vector = RVector.AllocateVector((RNodeType)type, length);
                    for (long i = 0; i < length; i++)
                    {
                        vector[i] = ReadItem(reader);
                    }

                    result = vector;
                    break;
                }

                default:
                    throw new NotSupportedException();
                }

                if (result.ObjectType != RNodeType.Char)
                {
                    result.Levels = levels;
                }

                result.IsObject = isObject;

                if (result.ObjectType == RNodeType.Char)
                {
                    if (hasAttribute)
                    {
                        ReadItem(reader);
                    }
                }
                else
                {
                    result.Attribute = hasAttribute ? ReadItem(reader) : null;
                }

                return(result);
            }

            throw new NotSupportedException();
        }