Ejemplo n.º 1
0
        internal EncodedArray(BinaryReader reader)
        {
            Count         = Leb128.ReadUleb(reader);
            EncodedValues = new EncodedValue[Count];

            for (ulong i = 0; i < Count; i++)
            {
                EncodedValues[i] = EncodedValue.parse(reader);
            }
        }
Ejemplo n.º 2
0
        public string EncodedValueToString(EncodedValue value, Class currentClass)
        {
            switch (value.EncodedType)
            {
            case EncodedValueType.VALUE_BYTE:
                return(((EncodedNumber)value).AsByte().ToString());

            case EncodedValueType.VALUE_NULL:
                return("null");

            case EncodedValueType.VALUE_BOOLEAN:
                return(((EncodedNumber)value).AsBoolean().ToString().ToLower());

            case EncodedValueType.VALUE_SHORT:
                return(((EncodedNumber)value).AsShort().ToString());

            case EncodedValueType.VALUE_CHAR:
                return(((EncodedNumber)value).AsChar().ToString());

            case EncodedValueType.VALUE_INT:
                return(((EncodedNumber)value).AsInt().ToString());

            case EncodedValueType.VALUE_LONG:
                return(((EncodedNumber)value).AsLong().ToString());

            case EncodedValueType.VALUE_FLOAT:
                return(((EncodedNumber)value).AsFloat().ToString());

            case EncodedValueType.VALUE_DOUBLE:
                return(((EncodedNumber)value).AsDouble().ToString());

            case EncodedValueType.VALUE_STRING:
                return(String.Format("\"{0}\"", _dex.GetString(((EncodedNumber)value).AsId()).Replace("\n", "\\n")));

            case EncodedValueType.VALUE_TYPE:
                return(_dex.GetTypeName(((EncodedNumber)value).AsId()));

            case EncodedValueType.VALUE_FIELD:
            case EncodedValueType.VALUE_ENUM:
                return(_getFieldName(((EncodedNumber)value).AsId(), currentClass, true));

            case EncodedValueType.VALUE_METHOD:
                var method = _dex.GetMethod(((EncodedNumber)value).AsId());
                return(string.Format("{0}.{1}", _dex.GetTypeName(method.ClassIndex), method.Name));

            case EncodedValueType.VALUE_ARRAY:
                var encodedArray = (EncodedArray)value;
                var array        = new List <string> ((int)encodedArray.Count);
                foreach (var arrayValue in encodedArray.GetValues())
                {
                    array.Add(EncodedValueToString(arrayValue, currentClass));
                }
                return(string.Format("[{0}]", string.Join(",", array)));

            case EncodedValueType.VALUE_ANNOTATION:
                var stringAnnotation = new StringWriter();
                _annToString(stringAnnotation, (EncodedAnnotation)value, currentClass, new Indentation());
                return(stringAnnotation.ToString());
            }

            return("Unknown");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name='dex'>
        /// Pointer to the DEX file this class was loaded from
        /// </param>
        internal Class(Dex dex, BinaryReader reader)
        {
            Dex = dex;

            ClassIndex      = reader.ReadUInt32();
            AccessFlags     = (AccessFlag)reader.ReadUInt32();
            SuperClassIndex = reader.ReadUInt32();
            var interfacesOffset = reader.ReadUInt32();

            SourceFileIndex = reader.ReadUInt32();
            var annotationsOffset  = reader.ReadUInt32();
            var classDataOffset    = reader.ReadUInt32();
            var staticValuesOffset = reader.ReadUInt32();

            Interfaces = dex.ReadTypeList(interfacesOffset);

            var fieldAnnotations     = new Dictionary <uint, List <Annotation> >();
            var methodAnnotations    = new Dictionary <uint, List <Annotation> >();
            var parameterAnnotations = new Dictionary <uint, List <Annotation> >();

            if (annotationsOffset != 0)
            {
                reader.BaseStream.Position = annotationsOffset;

                // annotations_directory_item
                var classAnnotationsOffset   = reader.ReadUInt32();
                var annotatedFieldsCount     = reader.ReadUInt32();
                var annotatedMethodsCount    = reader.ReadUInt32();
                var annotatedParametersCount = reader.ReadUInt32();

                for (int i = 0; i < annotatedFieldsCount; i++)
                {
                    // field_annotation
                    var fieldIndex  = reader.ReadUInt32();
                    var annotations = Annotation.ReadAnnotations(reader);
                    fieldAnnotations.Add(fieldIndex, annotations);
                }

                for (int i = 0; i < annotatedMethodsCount; i++)
                {
                    // method_annotation
                    var methodIndex = reader.ReadUInt32();
                    var annotations = Annotation.ReadAnnotations(reader);
                    methodAnnotations.Add(methodIndex, annotations);
                }

                for (int i = 0; i < annotatedParametersCount; i++)
                {
                    // parameter_annotation
                    var methodIndex = reader.ReadUInt32();
                    var annotations = Annotation.ReadAnnotations(reader);
                    parameterAnnotations.Add(methodIndex, annotations);
                }

                if (classAnnotationsOffset > 0)
                {
                    reader.BaseStream.Position = classAnnotationsOffset;
                    Annotations = Annotation.ReadAnnotationSetItem(reader);
                }
            }

            if (classDataOffset != 0)
            {
                reader.BaseStream.Position = classDataOffset;

                var staticFieldsCount   = Leb128.ReadUleb(reader);
                var instanceFieldsCount = Leb128.ReadUleb(reader);
                var directMethodsCount  = Leb128.ReadUleb(reader);
                var virtualMethodsCount = Leb128.ReadUleb(reader);

                StaticFields   = ReadFields(staticFieldsCount, reader, fieldAnnotations);
                InstanceFields = ReadFields(instanceFieldsCount, reader, fieldAnnotations);
                DirectMethods  = ReadMethods(directMethodsCount, reader, methodAnnotations, parameterAnnotations);
                VirtualMethods = ReadMethods(virtualMethodsCount, reader, methodAnnotations, parameterAnnotations);
            }
            else
            {
                StaticFields   = new Field[0];
                InstanceFields = new Field[0];
                DirectMethods  = new Method[0];
                VirtualMethods = new Method[0];
            }

            if (staticValuesOffset != 0)
            {
                reader.BaseStream.Position = staticValuesOffset;

                var size   = Leb128.ReadUleb(reader);
                var values = new EncodedValue[size];

                for (int i = 0; i < (int)size; i++)
                {
                    values[i] = EncodedValue.parse(reader);
                }

                StaticFieldsValues = values;
            }
            else
            {
                StaticFieldsValues = new EncodedValue[0];
            }
        }
Ejemplo n.º 4
0
 public AnnotationElement(BinaryReader reader)
 {
     NameIdx = (uint)Leb128.ReadUleb(reader);
     Value   = EncodedValue.parse(reader);
 }