public CDIItem ReadItem()
            {
                long nextIndex = Index;

                // Read the CDIBaseInfo header
                byte version = ReadByte();

                if (version != CDIVERSION)
                {
                    throw new FormatException("Got unexpected CDIBaseInfo version at " + Index);
                }
                byte kind = ReadByte();

                SkipPadding(4);
                uint size = ReadUInt32();

                nextIndex += size;

                CDIItem item = null;

                // Read kind-specific data
                switch ((CDIKind)kind)
                {
                case CDIKind.UsingInfo:
                    ushort   cUsings  = ReadUInt16();
                    CDIUsing cdiUsing = new CDIUsing();
                    cdiUsing.countOfUsing = new int[cUsings];
                    for (ushort j = 0; j < cUsings; j++)
                    {
                        cdiUsing.countOfUsing[j] = ReadUInt16();
                    }
                    SkipPadding(4);
                    item = cdiUsing;
                    break;

                case CDIKind.ForwardInfo:
                    uint       tokenToForwardTo = ReadUInt32();
                    CDIForward fwd = new CDIForward();
                    fwd.tokenToForwardTo = Util.AsToken((int)tokenToForwardTo);
                    item = fwd;
                    break;

                case CDIKind.ForwardToModuleInfo:
                    uint             token  = ReadUInt32();
                    CDIForwardModule modFwd = new CDIForwardModule();
                    modFwd.tokenOfModuleInfo = Util.AsToken((int)token);
                    item = modFwd;
                    break;

                case CDIKind.IteratorLocals:
                    int cBuckets = ReadInt32();
                    if (cBuckets < 0)
                    {
                        throw new FormatException("Unexpected negative iteratorLocal bucket count at " + BaseStream.Position);
                    }
                    CDIIteratorLocals iterLocals = new CDIIteratorLocals();
                    iterLocals.buckets = new CDIIteratorLocalBucket[cBuckets];
                    for (int j = 0; j < cBuckets; j++)
                    {
                        CDIIteratorLocalBucket b = new CDIIteratorLocalBucket();
                        b.ilOffsetStart       = ReadInt32();
                        b.ilOffsetEnd         = ReadInt32();
                        iterLocals.buckets[j] = b;
                    }
                    item = iterLocals;
                    break;

                case CDIKind.ForwardIterator:
                    CDIForwardIterator fwdIter = new CDIForwardIterator();
                    fwdIter.iteratorClassName = ReadString();
                    SkipPadding(4);
                    item = fwdIter;
                    break;

                default:
                    CDIUnknown u = new CDIUnknown();
                    u.kind = kind;
                    byte[] bytes = ReadBytes(checked ((int)(nextIndex - Index)));
                    u.bytes = Util.ToHexString(bytes);
                    item    = u;
                    break;
                }

                if (Index != nextIndex)
                {
                    throw new FormatException(String.Format("Expected CDI item to end at index {0}, but ended at {1} instead", nextIndex, Index));
                }
                item.version = version;
                return(item);
            }
Example #2
0
            public CDIItem ReadItem()
            {
                long nextIndex = Index;

                // Read the CDIBaseInfo header
                byte version = ReadByte();
                if (version != CDIVERSION)
                    throw new FormatException("Got unexpected CDIBaseInfo version at " + Index);
                byte kind = ReadByte();
                SkipPadding(4);
                uint size = ReadUInt32();
                nextIndex += size;

                CDIItem item = null;

                // Read kind-specific data
                switch ((CDIKind)kind)
                {
                    case CDIKind.UsingInfo:
                        ushort cUsings = ReadUInt16();
                        CDIUsing cdiUsing = new CDIUsing();
                        cdiUsing.countOfUsing = new int[cUsings];
                        for (ushort j = 0; j < cUsings; j++)
                            cdiUsing.countOfUsing[j] = ReadUInt16();
                        SkipPadding(4);
                        item = cdiUsing;
                        break;

                    case CDIKind.ForwardInfo:
                        uint tokenToForwardTo = ReadUInt32();
                        CDIForward fwd = new CDIForward();
                        fwd.tokenToForwardTo = Util.AsToken((int)tokenToForwardTo);
                        item = fwd;
                        break;

                    case CDIKind.ForwardToModuleInfo:
                        uint token = ReadUInt32();
                        CDIForwardModule modFwd = new CDIForwardModule();
                        modFwd.tokenOfModuleInfo = Util.AsToken((int)token);
                        item = modFwd;
                        break;

                    case CDIKind.IteratorLocals:
                        int cBuckets = ReadInt32();
                        if (cBuckets < 0)
                            throw new FormatException("Unexpected negative iteratorLocal bucket count at " + BaseStream.Position);
                        CDIIteratorLocals iterLocals = new CDIIteratorLocals();
                        iterLocals.buckets = new CDIIteratorLocalBucket[cBuckets];
                        for (int j = 0; j < cBuckets; j++)
                        {
                            CDIIteratorLocalBucket b = new CDIIteratorLocalBucket();
                            b.ilOffsetStart = ReadInt32();
                            b.ilOffsetEnd = ReadInt32();
                            iterLocals.buckets[j] = b;
                        }
                        item = iterLocals;
                        break;

                    case CDIKind.ForwardIterator:
                        CDIForwardIterator fwdIter = new CDIForwardIterator();
                        fwdIter.iteratorClassName = ReadString();
                        SkipPadding(4);
                        item = fwdIter;
                        break;

                    default:
                        CDIUnknown u = new CDIUnknown();
                        u.kind = kind;
                        byte[] bytes = ReadBytes(checked((int)(nextIndex - Index)));
                        u.bytes = Util.ToHexString(bytes);
                        item = u;
                        break;
                }

                if (Index != nextIndex)
                {
                    throw new FormatException(String.Format("Expected CDI item to end at index {0}, but ended at {1} instead", nextIndex, Index));
                }
                item.version = version;
                return item;
            }