diff --git a/include/Attributes.h b/include/Attributes.h new file mode 100644 index 0000000..38dff5b --- /dev/null +++ b/include/Attributes.h @@ -0,0 +1,109 @@ +#ifndef __CAVM_ATTRIBUTES_H +#define __CAVM_ATTRIBUTES_H + +#include +#include +#include +#include + +namespace Attributes +{ +enum class ClassAttributes : uint32_t +{ + SOURCE_FILE = "SourceFile"_crc32, + INNER_CLASSES = "InnerClasses"_crc32, + ENCLOSING_METHOD = "EnclosingMethod"_crc32, + SOURCE_DEBUG_EXTENSION = "SourceDebugExtension"_crc32, + BOOTSTRAP_METHODS = "BootstrapMethods"_crc32, + MODULE = "Module"_crc32, + MODULE_PACKAGES = "ModulePackages"_crc32, + MODULE_MAIN_CLASS = "ModuleMainClass"_crc32, + NEST_HOST = "NestHost"_crc32, + NEST_MEMBERS = "NestMembers"_crc32, + RECORD = "Record"_crc32, + PERMITTED_SUBCLASSES = "PermittedSubclasses"_crc32, + SYNTHETIC = "Synthetic"_crc32, + DEPRECATED = "Deprecated"_crc32, + SIGNATURE = "Signature"_crc32, + RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations"_crc32, +}; + +enum class FieldAttributes : uint32_t +{ + CONSTANT_VALUE = "ConstantValue"_crc32, + SYNTHETIC = "Synthetic"_crc32, + DEPRECATED = "Deprecated"_crc32, + SIGNATURE = "Signature"_crc32, + RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations"_crc32, + RUNTIME_INVISIBLE_ANNOTATIONS = "RuntimeInvisibleAnnotations"_crc32, + RUNTIME_VISIBLE_TYPE_ANNOTATIONS = "RuntimeVisibleTypeAnnotations"_crc32, + RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = "RuntimeInvisibleTypeAnnotations"_crc32, +}; + +enum class MethodAttributes : uint32_t +{ + CODE = "Code"_crc32, + EXCEPTIONS = "Exceptions"_crc32, + RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS = "RuntimeVisibleParameterAnnotations"_crc32, + RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS = "RuntimeInvisibleParameterAnnotations"_crc32, + ANNOTATION_DEFAULT = "AnnotationDefault"_crc32, + METHOD_PARAMETERS = "MethodParameters"_crc32, + SYNTHETIC = "Synthetic"_crc32, + DEPRECATED = "Deprecated"_crc32, + SIGNATURE = "Signature"_crc32, + RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations"_crc32, + RUNTIME_INVISIBLE_ANNOTATIONS = "RuntimeInvisibleAnnotations"_crc32, + RUNTIME_VISIBLE_TYPE_ANNOTATIONS = "RuntimeVisibleTypeAnnotations"_crc32, + RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = "RuntimeInvisibleTypeAnnotations"_crc32, +}; + +enum class RecordComponentAttributes : uint32_t +{ + RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations"_crc32, + RUNTIME_INVISIBLE_ANNOTATIONS = "RuntimeInvisibleAnnotations"_crc32, + RUNTIME_VISIBLE_TYPE_ANNOTATIONS = "RuntimeVisibleTypeAnnotations"_crc32, + RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = "RuntimeInvisibleTypeAnnotations"_crc32, +}; + +enum class CodeAttributes : uint32_t +{ + LINE_NUMBER_TABLE = "LineNumberTable"_crc32, + LOCAL_VARIABLE_TABLE = "LocalVariableTable"_crc32, + LOCAL_VARIABLE_TYPE_TABLE = "LocalVariableTypeTable"_crc32, + STACK_MAP_TABLE = "StackMapTable"_crc32, + RUNTIME_VISIBLE_TYPE_ANNOTATIONS = "RuntimeVisibleTypeAnnotations"_crc32, + RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = "RuntimeInvisibleTypeAnnotations"_crc32, +}; + +struct CODE_Attribute : BaseAttribute +{ + private: + typedef struct + { + u2 start_pc; + u2 end_pc; + u2 handler_pc; + u2 catch_type; + } exception_table; + + public: + u2 max_stack; + u2 max_locals; + u4 code_length; + std::vector code; + u2 exception_table_length; + std::vector exceptions; + u2 attributes_count; + std::vector attributes; +}; + + +struct SOURCE_FILE_Attribute : BaseAttribute +{ + u2 sourcefile_index; +}; + +attribute_info AttributesParser(JavaClassFormat &jc); +} // namespace Attributes + +#endif \ No newline at end of file diff --git a/src/AttributeParser.cpp b/src/AttributeParser.cpp new file mode 100644 index 0000000..fc932e6 --- /dev/null +++ b/src/AttributeParser.cpp @@ -0,0 +1,52 @@ +#include "ConstantPoolTags/Tags.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +attribute_info Attributes::AttributesParser(JavaClassFormat& jc) +{ + attribute_info ret; + + ret.attribute_name_index = Utils::ReadFromStartIntoVal(Globals::buffer()); + ret.attribute_length = Utils::ReadFromStartIntoVal(Globals::buffer()); + + ASSERT(ret.attribute_length.ToHostFormat() > 0); + + for (int i = 0; i < ret.attribute_length.ToHostFormat(); i++) + { + ret.info.push_back(Utils::ReadFromStartIntoVal(Globals::buffer(), 1)); + } + + std::reverse(ret.info.begin(), ret.info.end()); + + int idx = ret.info.data()->ToHostFormat(); + + ASSERT(VEC_ELEM_TYPE(jc.constant_pool[ret.attribute_name_index.ToHostFormat() - 1]) == TYPE(ConstantPoolTags::ConstantUtf8Info)); + std::string str = reinterpret_cast(std::any_cast(jc.constant_pool[ret.attribute_name_index.ToHostFormat() - 1]).bytes); + + switch ((ClassAttributes)crc32_rec(CRC32, str.c_str())) + { + case ClassAttributes::SOURCE_FILE: + { + ASSERT(VEC_ELEM_TYPE(jc.constant_pool[idx - 1]) == TYPE(ConstantPoolTags::ConstantUtf8Info)); + std::cout << "Source File: " + << std::any_cast(jc.constant_pool[idx - 1]).bytes + << std::endl; + SOURCE_FILE_Attribute attr; + // attr. + break; + } + default: + std::cerr << "Unknown attribute: \"" << str << "\"" << std::endl; + exit(EXIT_FAILURE); + } + // switch () + + return ret; +} \ No newline at end of file