Skip to content

Instantly share code, notes, and snippets.

@AlLiberali
Created February 27, 2023 17:50
Show Gist options
  • Save AlLiberali/69cb262cc6b85df33b009688ed62d024 to your computer and use it in GitHub Desktop.
Save AlLiberali/69cb262cc6b85df33b009688ed62d024 to your computer and use it in GitHub Desktop.
Hex Workshop Structure library for Java Classfile
/*
* Hex Workshop Structure library file for Java Classfile v61.0 and above
* Copyright (c) 2023 Nader Jafari
* Unfortunately it's nearly impossible to specify all the different attributes
* for Hex Workshop as it requires fetching the name from the constant pool
* and comparing Strings
*/
#pragma displayname("Java Classfile v61.0+")
#pragma fileextensions(".class")
#pragma byteorder(big_endian)
#pragma maxarray(65536)
#pragma maxstring(65536)
#pragma hide()
typedef __int8 u1;
typedef __int16 u2;
typedef __int32 u4;
typedef __int64 u8;
typedef __int16 CPIndex;
typedef __int16 size_t;
#pragma enumsize(2)
#pragma enumsign("unsigned")
enum MinorVersion {
Normal = 0,
Preview = 65535
};
enum MajorVersion {
JavaSE17 = 61,
JavaSE18 = 62,
JavaSE19 = 63,
JavaSE20 = 64,
JavaSE21 = 65,
};
struct ConstantPoolEntry {
u1 tag;
switch (tag & tag) {
case 1:
size_t length;
u1 bytes[length];
break;
case 3:
u4 bytes;
break;
case 4:
u4 bytes;
break;
case 5:
u8 bytes;
break;
case 6:
u8 bytes;
break;
case 7:
CPIndex nameIndex;
break;
case 8:
CPIndex stringIndex;
break;
case 9:
CPIndex classIndex;
CPIndex nameTypeIndex;
break;
case 10:
CPIndex classIndex;
CPIndex nameTypeIndex;
break;
case 11:
CPIndex classIndex;
CPIndex nameTypeIndex;
break;
case 12:
CPIndex nameIndex;
CPIndex descriptorIndex;
break;
case 15:
u1 refType;
__verify(refType >= 0 && refType <= 9);
CPIndex refIndex;
break;
case 16:
CPIndex descriptorIndex;
break;
case 17:
u2 bootMethodTableIndex;
CPIndex nameTypeIndex;
break;
case 18:
u2 bootMethodTableIndex;
CPIndex nameTypeIndex;
break;
case 19:
CPIndex nameIndex;
break;
case 20:
CPIndex nameIndex;
break;
};
};
struct AccessFlagBitfield {
u2 lower : 4;
u2 lowerMid : 4;
u2 upperMid : 4;
u2 upper : 4;
};
struct Attribute {
CPIndex nameIndex;
u4 attributeLength;
u1 info[attributeLength];
};
struct Field {
struct AccessFlagBitfield accessFlags;
CPIndex nameIndex;
CPIndex descriptorIndex;
size_t attributeCount;
struct Attribute attributes[attributeCount];
};
struct Method {
struct AccessFlagBitfield accessFlags;
CPIndex nameIndex;
CPIndex descriptorIndex;
size_t attributeCount;
struct Attribute attributes[attributeCount];
};
#pragma show()
struct Classfile {
u4 magic;
__verify(magic == 0xCAFEBABE);
enum MinorVersion minorVersion;
enum MajorVersion majorVersion;
size_t constantPoolLength;
struct ConstantPoolEntry constantPool[constantPoolLength - 1];
struct AccessFlagBitfield accessFlags;
CPIndex thisClass;
CPIndex superClass;
size_t interfaceCount;
CPIndex interfaces[interfaceCount];
size_t fieldCount;
struct Field fields[fieldCount];
size_t methodCount;
struct Method methods[methodCount];
size_t attributeCount;
struct Attribute attributes[attributeCount];
};
/*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this file (the "Software"), to deal in the Software without restriction,
* including without limitation, the rights to use, copy, modify, publish,
* distribute, sublicence, and/or sell copies of the Softwarem and to permit
* persons to whom the Software is furnished to do so.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment