示例#1
0
文件: Asm.cs 项目: nofuture-git/31g
        /// <summary>
        /// Given the <see cref="rawIlByteStream"/> the occurance of 
        /// </summary>
        /// <param name="rawIlByteStream"></param>
        /// <param name="opCodes"></param>
        /// <returns></returns>
        /// <remarks>
        /// Pass the metadata token into an assembly's manifest module's <see cref="System.Reflection.Module.ResolveMember(int)"/>
        /// to get the <see cref="MethodInfo"/>
        /// </remarks>
        public static int[] GetOpCodesArgs(byte[] rawIlByteStream, OpCode[] opCodes)
        {
            if (rawIlByteStream == null)
                return null;

            var metadataTokens = new List<int>();

            //expecting first byte to always be an OpCode, never an arg thereof
            for (var i = 0; i < rawIlByteStream.Length; i++)
            {
                var opCodei = GetOpCodeByValue(rawIlByteStream[i]);
                var moveBuffer = GetOpCodeOperandByteSize(opCodei);
                if (moveBuffer == 0 || moveBuffer > 4 ||//don't handle IA64 since its not resolvable as a token
                    (rawIlByteStream.Length <= i + moveBuffer) ||
                    (opCodes.All(x => opCodei != x)))
                {
                    i = i + moveBuffer;
                    continue;
                }

                var metadataToken = 0;
                switch (moveBuffer)
                {
                    case 1:
                        metadataToken = rawIlByteStream[i + 1];
                        break;
                    case 2:
                        metadataToken = ByteArray.ToWord(rawIlByteStream[i + 2], rawIlByteStream[i + 1]);
                        break;
                    case 4:
                        metadataToken = ByteArray.ToDWord(rawIlByteStream[i + 4], rawIlByteStream[i + 3],
                            rawIlByteStream[i + 2], rawIlByteStream[i + 1]);
                        break;
                }

                //get only distinct entries
                if (metadataToken != 0 && metadataTokens.All(x => x != metadataToken))
                    metadataTokens.Add(metadataToken);

                i = i + moveBuffer;
            }

            return metadataTokens.ToArray();
        }