Compare commits

...

14 Commits

Author SHA1 Message Date
Merlyn Morgan-Graham
de0e089165 includes/std+type: Add DOS datetime types (#57)
Add type::dosdate16_t and type::dostime16_t in pattern library
2022-11-14 10:08:00 +01:00
Fredrick Brennan
e7ea6fd77f patterns: Added APNG support to PNG pattern (#53) 2022-11-10 10:36:46 +01:00
Fredrick Brennan
ff3c796de8 patterns: Added sRGB, zTXt, tEXt support to PNG pattern (#55)
* patterns: png.hexpat: APNG support

* patterns: png.hexpat: +sRGB, zTXt, tEXt
2022-11-10 10:23:42 +01:00
lieyanqzu
0c83764f24 patterns: Fix java class pattern and update to javase17 format (#56)
fix java class pattern and update to javase17 format
2022-11-10 10:23:06 +01:00
Fredrick Brennan
d87f95dbfa includes/type: Add RGB8 to color.pat (#54) 2022-11-10 10:22:17 +01:00
Nik
51dad63779 includes/std: Removed invalid returns 2022-11-09 21:56:06 +01:00
Nik
764b86acc9 includes/std: Added section functions 2022-11-09 21:53:31 +01:00
Nik
10fdf94899 includes/std: Fixed syntax error 2022-11-09 21:25:01 +01:00
Nik
9ba998e618 includes/std: Added new functions 2022-11-09 21:21:59 +01:00
WerWolv
aceeb2b7b3 git: Don't build plcli tool in tests 2022-11-06 18:40:39 +01:00
fr0ntlin3
3b1b7cc379 patterns: Fixed ELF pattern for 32-bit files (#51)
Co-authored-by: z <z>
2022-11-06 18:36:37 +01:00
gmestanley
49be43e0e1 patterns: Making pe.hexpat even better (#52)
* Update pe.hexpat

Implementing a number of things and fixing others

* Update pe.hexpat

Adding the readonlyData pattern

* Update pe.hexpat

Testing putting the number of tabs on Github to 4 instead of 8 (so that comments on the code for the Sections don't break)

* Update pe.hexpat

Reverting change that turned out to be needless

* Update pe.hexpat

Actually sending the `products[while($ != richHeaderEndPosition)]` to `products[while($ < richHeaderEndPosition)]` change
2022-10-30 12:07:54 +01:00
Nik
8e70a5524d includes/type: Added templates for number types with specific base 2022-10-10 22:36:46 +02:00
Nik
9c0bf1433c includes/std: Added new Array and SizedString type 2022-10-10 22:36:09 +02:00
15 changed files with 1325 additions and 450 deletions

View File

@@ -50,6 +50,7 @@ jobs:
-DCMAKE_C_FLAGS="-fuse-ld=lld --coverage" \
-DCMAKE_CXX_FLAGS="-fuse-ld=lld --coverage" \
-DLIBPL_ENABLE_TESTS=OFF \
-DLIBPL_ENABLE_CLI=OFF \
..
make -j4

9
includes/std/array.pat Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
namespace std {
struct Array<T, auto Size> {
T data[Size] [[inline]];
};
}

View File

@@ -30,7 +30,7 @@ namespace std::core {
fn set_bitfield_order(BitfieldOrder order) {
builtin::std::core::set_bitfield_order(order);
builtin::std::core::set_bitfield_order(u32(order));
};
fn get_bitfield_order() {
@@ -54,4 +54,7 @@ namespace std::core {
return builtin::std::core::formatted_value(pattern);
};
fn is_valid_enum(ref auto pattern) {
return builtin::std::core::is_valid_enum(pattern);
};
}

View File

@@ -2,28 +2,8 @@
namespace std::hash {
fn crc32(u128 address, u64 size, u32 init, u32 poly) {
u8 byte;
u32 crc, mask;
crc = init;
u64 i;
while (i < size) {
byte = std::mem::read_unsigned(address + i, 1);
crc = crc ^ byte;
u8 j;
while (j < 8) {
mask = u32(-(crc & 1));
crc = (crc >> 1) ^ (poly & mask);
j = j + 1;
}
i = i + 1;
}
return u32(~crc);
fn crc32(ref auto pattern, u32 init, u32 poly) {
return builtin::std::hash::crc32(pattern, init, poly);
};
}

View File

@@ -2,6 +2,8 @@
namespace std::mem {
using Section = u128;
enum Endian : u8 {
Native = 0,
Big = 1,
@@ -48,6 +50,28 @@ namespace std::mem {
};
fn create_section(str name) {
return builtin::std::mem::create_section(name);
};
fn delete_section(Section section) {
builtin::std::mem::delete_section(section);
};
fn get_section_size(Section section) {
return builtin::std::mem::get_section_size(section);
};
fn copy_section_to_section(Section from_section, u64 from_address, Section to_section, u64 to_address, u64 size) {
builtin::std::mem::get_section_size(from_section, from_address, to_section, to_address, size);
};
fn copy_value_to_section(ref auto value, Section to_section, u64 to_address) {
builtin::std::mem::copy_value_to_section(value, to_section, to_address);
};
struct MagicSearch<auto Magic, T> {
if ($ < (std::mem::size() - std::string::length(Magic) - 1)) {
char __potentialMagic__[std::string::length(Magic)] [[hidden, no_unique_address]];

View File

@@ -4,6 +4,22 @@
namespace std::string {
struct SizedStringBase<SizeType, DataType> {
SizeType size;
DataType data[size];
} [[sealed, format("std::string::impl::format_sized_string"), transform("std::string::impl::format_sized_string")]];
using SizedString<SizeType> = SizedStringBase<SizeType, char>;
using SizedString16<SizeType> = SizedStringBase<SizeType, char16>;
namespace impl {
fn format_sized_string(ref auto string) {
return string.data;
};
}
fn length(str string) {
return builtin::std::string::length(string);
};

View File

@@ -15,12 +15,12 @@ namespace std::time {
u16 yday;
bool isdst;
} [[sealed]];
union TimeConverter {
Time time;
u128 value;
};
using EpochTime = u128;
enum TimeZone : u8 {
@@ -28,44 +28,94 @@ namespace std::time {
UTC
};
bitfield DOSDate {
day: 5;
month: 4;
year: 7;
} [[sealed]];
bitfield DOSTime {
seconds: 5;
minutes: 6;
hours: 5;
} [[sealed]];
namespace impl {
union DOSDateConverter {
DOSDate date;
u16 value;
};
union DOSTimeConverter {
DOSTime time;
u16 value;
};
}
fn epoch() {
return builtin::std::time::epoch();
};
fn to_local(EpochTime epoch_time) {
TimeConverter converter;
converter.value = builtin::std::time::to_local(epoch_time);
return converter.time;
};
fn to_utc(EpochTime epoch_time) {
TimeConverter converter;
converter.value = builtin::std::time::to_utc(epoch_time);
return converter.time;
};
fn to_utc(EpochTime epoch_time) {
TimeConverter converter;
converter.value = builtin::std::time::to_utc(epoch_time);
return converter.time;
};
fn now(TimeZone time_zone = TimeZone::Local) {
TimeConverter converter;
if (time_zone == TimeZone::Local)
converter.value = builtin::std::time::to_local(std::time::epoch());
else if (time_zone == TimeZone::UTC)
converter.value = builtin::std::time::to_utc(std::time::epoch());
else
converter.value = 0x00;
return converter.time;
};
fn to_dos_date(u16 value) {
impl::DOSDateConverter converter;
converter.value = value;
return converter.date;
};
fn to_dos_time(u16 value) {
impl::DOSTimeConverter converter;
converter.value = value;
return converter.time;
};
fn format(Time time, str format_string = "%c") {
TimeConverter converter;
converter.time = time;
return builtin::std::time::format(format_string, converter.value);
};
}
fn format_dos_date(DOSDate date, str format_string = "{}/{}/{}") {
return std::format(format_string, date.day, date.month, date.year + 1980);
};
fn format_dos_time(DOSTime time, str format_string = "{:02}:{:02}:{:02}") {
return std::format(format_string, time.hours, time.minutes, time.seconds * 2);
};
}

31
includes/type/base.pat Normal file
View File

@@ -0,0 +1,31 @@
#pragma once
#include <std/io.pat>
#include <std/math.pat>
namespace type {
using Hex<T> = T [[format("type::impl::format_hex")]];
using Oct<T> = T [[format("type::impl::format_oct")]];
using Dec<T> = T [[format("type::impl::format_dec")]];
using Bin<T> = T [[format("type::impl::format_bin")]];
namespace impl {
fn format_number(auto value, str fmt) {
bool negative = value < 0;
if (negative)
return std::format("-" + fmt, std::math::abs(value));
else
return std::format(fmt, value);
};
fn format_hex(auto value) { return format_number(value, "0x{:02X}"); };
fn format_oct(auto value) { return format_number(value, "0o{:03o}"); };
fn format_dec(auto value) { return format_number(value, "{}"); };
fn format_bin(auto value) { return format_number(value, "0b{:08b}"); };
}
}

View File

@@ -5,6 +5,10 @@
namespace type {
struct RGB8 {
u8 r, g, b;
} [[sealed, format("type::impl::format_color")]];
struct RGBA8 {
u8 r, g, b, a;
} [[sealed, format("type::impl::format_color")]];

View File

@@ -4,16 +4,26 @@
#include <std/time.pat>
namespace type {
using time32_t = u32 [[format("type::impl::format_time_t")]];
using time64_t = u64 [[format("type::impl::format_time_t")]];
using dosdate16_t = u16 [[format("type::impl::format_dosdate16_t")]];
using dostime16_t = u16 [[format("type::impl::format_dostime16_t")]];
namespace impl {
fn format_time_t(u128 value) {
fn format_time_t(u128 value) {
return std::time::format(std::time::to_utc(value));
};
fn format_dosdate16_t(u16 value) {
return std::time::format_dos_date(std::time::to_dos_date(value));
};
fn format_dostime16_t(u16 value) {
return std::time::format_dos_time(std::time::to_dos_time(value));
};
}
}

View File

@@ -560,7 +560,7 @@ struct Elf32_Phdr {
PF p_flags;
Elf32_Word p_align;
if (p_offset > 0 && p_filesz > 0 && (p_offset + p_filesz) < std::mem::data_size() && p_filesz < std::mem::data_size())
if (p_offset > 0 && p_filesz > 0 && (p_offset + p_filesz) < std::mem::size() && p_filesz < std::mem::size())
u8 p_data[p_filesz] @ p_offset;
};
@@ -731,7 +731,9 @@ struct ELF {
std::core::set_endian(std::mem::Endian::Big);
if (e_ident.EI_CLASS == EI_CLASS::ELFCLASS32) {
Elf32_Ehdr header;
Elf32_Ehdr ehdr;
Elf32_Phdr phdr[ehdr.e_phnum] @ ehdr.e_phoff;
Elf32_Shdr shdr[ehdr.e_shnum] @ ehdr.e_shoff;
} else if (e_ident.EI_CLASS == EI_CLASS::ELFCLASS64) {
Elf64_Ehdr ehdr;
Elf64_Phdr phdr[ehdr.e_phnum] @ ehdr.e_phoff;

View File

@@ -26,7 +26,9 @@ enum cp_tag : u8 {
CONSTANT_NameAndType = 12,
CONSTANT_MethodHandle = 15,
CONSTANT_MethodType = 16,
CONSTANT_InvokeDynamic = 18
CONSTANT_InvokeDynamic = 18,
CONSTANT_Module = 19,
CONSTANT_Package = 20
};
enum major_version : u2 {
@@ -36,7 +38,7 @@ enum major_version : u2 {
JDK_1_4 = 48,
Java_SE_5_0 = 49,
Java_SE_6_0 = 50,
Java_SE_6 = 50,
Java_SE_7 = 51,
Java_SE_8 = 52,
Java_SE_9 = 53,
@@ -129,37 +131,62 @@ struct CONSTANT_InvokeDynamic_info {
u2 name_and_type_index;
};
struct cp_info {
cp_tag tag;
// Tag == CONSTANT_Module
struct CONSTANT_Module_info {
u2 name_index;
};
if (tag == cp_tag::CONSTANT_Utf8)
CONSTANT_Utf8_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Integer)
CONSTANT_Integer_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Float)
CONSTANT_Float_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Long)
CONSTANT_Long_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Double)
CONSTANT_Double_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Class)
CONSTANT_Class_info info [[inline]];
else if (tag == cp_tag::CONSTANT_String)
CONSTANT_String_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Fieldref)
CONSTANT_Fieldref_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Methodref)
CONSTANT_Methodref_info info [[inline]];
else if (tag == cp_tag::CONSTANT_InterfaceMethodref)
CONSTANT_InterfaceMethodref_info info [[inline]];
else if (tag == cp_tag::CONSTANT_NameAndType)
CONSTANT_NameAndType_info info [[inline]];
else if (tag == cp_tag::CONSTANT_MethodHandle)
CONSTANT_MethodHandle_info info [[inline]];
else if (tag == cp_tag::CONSTANT_MethodType)
CONSTANT_MethodType_info info [[inline]];
else if (tag == cp_tag::CONSTANT_InvokeDynamic)
CONSTANT_InvokeDynamic_info info [[inline]];
// Tag == CONSTANT_Package
struct CONSTANT_Package_info {
u2 name_index;
};
// All 8-byte constants take up two entries in the constant_pool table of the class file
u1 padding_entry_flag = 0;
struct cp_info {
if (padding_entry_flag == 0) {
cp_tag tag;
if (tag == cp_tag::CONSTANT_Utf8)
CONSTANT_Utf8_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Integer)
CONSTANT_Integer_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Float)
CONSTANT_Float_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Long) {
CONSTANT_Long_info info [[inline]];
padding_entry_flag = 1;
}
else if (tag == cp_tag::CONSTANT_Double) {
CONSTANT_Double_info info [[inline]];
padding_entry_flag = 1;
}
else if (tag == cp_tag::CONSTANT_Class)
CONSTANT_Class_info info [[inline]];
else if (tag == cp_tag::CONSTANT_String)
CONSTANT_String_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Fieldref)
CONSTANT_Fieldref_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Methodref)
CONSTANT_Methodref_info info [[inline]];
else if (tag == cp_tag::CONSTANT_InterfaceMethodref)
CONSTANT_InterfaceMethodref_info info [[inline]];
else if (tag == cp_tag::CONSTANT_NameAndType)
CONSTANT_NameAndType_info info [[inline]];
else if (tag == cp_tag::CONSTANT_MethodHandle)
CONSTANT_MethodHandle_info info [[inline]];
else if (tag == cp_tag::CONSTANT_MethodType)
CONSTANT_MethodType_info info [[inline]];
else if (tag == cp_tag::CONSTANT_InvokeDynamic)
CONSTANT_InvokeDynamic_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Module)
CONSTANT_Module_info info [[inline]];
else if (tag == cp_tag::CONSTANT_Package)
CONSTANT_Package_info info [[inline]];
} else {
padding_entry_flag = 0;
}
};
bitfield access_flags_method {
@@ -216,7 +243,7 @@ bitfield access_flags_class {
ACC_SYNTHETIC : 1; // 0x1000
ACC_ANNOTATION : 1; // 0x2000
ACC_ENUM : 1; // 0x4000
padding : 1; // 0x8000
ACC_MODULE : 1; // 0x8000
};
struct attribute_info {
@@ -279,31 +306,29 @@ fn main() {
std::print("Fields:");
for (le u16 i = 0, i < class_file.fields_count, i = i + 1) {
str method_string = " ";
str field_string = " ";
if (class_file.fields[i].access_flags.ACC_PUBLIC)
method_string = method_string + "public ";
field_string = field_string + "public ";
if (class_file.fields[i].access_flags.ACC_PRIVATE)
method_string = method_string + "private ";
field_string = field_string + "private ";
if (class_file.fields[i].access_flags.ACC_PROTECTED)
method_string = method_string + "protected ";
field_string = field_string + "protected ";
if (class_file.fields[i].access_flags.ACC_STATIC)
method_string = method_string + "static ";
field_string = field_string + "static ";
if (class_file.fields[i].access_flags.ACC_FINAL)
method_string = method_string + "final ";
field_string = field_string + "final ";
if (class_file.fields[i].access_flags.ACC_VOLATILE)
method_string = method_string + "volatile ";
if (class_file.fields[i].access_flags.ACC_NATIVE)
method_string = method_string + "native ";
field_string = field_string + "volatile ";
if (class_file.fields[i].access_flags.ACC_TRANSIENT)
method_string = method_string + "transient ";
field_string = field_string + "transient ";
if (class_file.fields[i].access_flags.ACC_ENUM)
method_string = method_string + "enum ";
field_string = field_string + "enum ";
method_string = method_string + class_file.constant_pool[class_file.fields[i].name_index - 1].info.bytes;
method_string = method_string + " [ " + class_file.constant_pool[class_file.fields[i].descriptor_index - 1].info.bytes + " ]";
field_string = field_string + class_file.constant_pool[class_file.fields[i].name_index - 1].info.bytes;
field_string = field_string + " [ " + class_file.constant_pool[class_file.fields[i].descriptor_index - 1].info.bytes + " ]";
std::print("{}", method_string);
std::print("{}", field_string);
}
std::print("Methods:");

File diff suppressed because it is too large Load Diff

View File

@@ -1,36 +1,117 @@
#pragma MIME image/png
#pragma endian big
struct header_t
{
struct header_t {
u8 highBitByte;
char signature[3];
char dosLineEnding[2];
char dosEOF;
char unixLineEnding;
char signature[3];
char dosLineEnding[2];
char dosEOF;
char unixLineEnding;
};
struct ihdr_t
{
struct actl_t {
u32 frames [[comment("Total № of frames in animation")]];
u32 plays [[comment("№ of times animation will loop")]];
} [[comment("Animation control chunk"), name("acTL")]];
enum ColorType: u8 {
Grayscale = 0x0,
RGBTriple = 0x2,
Palette,
GrayscaleAlpha,
RGBA = 0x6
};
enum Interlacing: u8 {
None,
Adam7
};
struct ihdr_t {
u32 width [[comment("Image width")]];
u32 height [[comment("Image height")]];
u8 bit_depth;
u8 color_type [[comment("PNG Image Type\n0: greyscale\n2: truecolour\n3: indexed-color\n4: greyscale with alpha\n6: truecolour with alpha")]];
u8 compression_method;
u8 filter_method;
u8 interlace_method [[comment("values 0 \"no interlace\" or 1 \"Adam7 interlace\"")]];
ColorType color_type [[comment("PNG Image Type")]];
u8 compression_method [[comment("Only 0x0 = zlib supported by most")]];
u8 filter_method [[comment("Only 0x0 = adaptive supported by most")]];
Interlacing interlacing;
};
struct palette_entry_t {
u8 r;
u8 g;
u8 b;
enum sRGB: u8 {
Perceptual = 0x0,
RelativeColorimetric,
Saturation,
AbsoluteColorimetric
};
enum Unit: u8 {
Unknown,
Meter
};
struct phys_t {
u32 ppu_x [[comment("Pixels per unit, X axis")]];
u32 ppu_y [[comment("Pixels per unit, Y axis")]];
u8 unit [[comment("Unit Specifier\n0: unit is unknown\n1: unit is the metre")]];
Unit unit;
};
enum BlendOp: u8 {
Source = 0x0,
Over
};
enum DisposeOp: u8 {
None = 0x0,
Background,
Previous
};
struct fctl_t {
u32 sequence_no [[comment("Sequence №")]];
u32 width [[comment("Frame width")]];
u32 height;
u32 xoff;
u32 yoff;
u16 delay_num;
u16 delay_den;
DisposeOp dispose_op;
BlendOp blend_op;
};
struct fdat_t {
u32 sequence_no;
};
fn text_len() {
u64 len = parent.parent.length - ($ - addressof(parent.keyword));
return len;
};
enum BlendOp: u8 {
Source = 0x0,
Over
};
enum DisposeOp: u8 {
None = 0x0,
Background,
Previous
};
struct fctl_t {
u32 sequence_no [[comment("Sequence №")]];
u32 width [[comment("Frame width")]];
u32 height [[comment("Frame height")]];
u32 xoff;
u32 yoff;
u16 delay_num [[comment("Frame delay fraction numerator")]];
u16 delay_den [[comment("Frame delay fraction denominator")]];
DisposeOp dispose_op;
BlendOp blend_op;
};
struct fdat_t {
u32 sequence_no;
};
struct itxt_t {
@@ -39,48 +120,92 @@ struct itxt_t {
u8 compression_method;
char language_tag[];
char translated_keyword[];
char text[parent.length - ($ - addressof(keyword))];
char text[text_len()];
};
struct ztxt_t {
char keyword[];
u8 compression_method;
char text[text_len()];
};
struct text_t {
char keyword[];
char text[text_len()];
};
struct iccp_t {
char profile [];
u8 compression_method;
u8 compressed_profile[parent.length - ($ - addressof(profile))];
char keyword[];
u8 compression_method;
u8 compressed_profile[text_len()];
};
struct palette_entry_t {
u24 color;
} [[inline]];
struct chunk_t {
u32 length [[color("17BECF")]];
char type[4];
char name[4];
#define IHDR_k "IHDR"
#define PLTE_k "PLTE"
#define sRGB_k "sRGB"
#define pHYs_k "pHYs"
#define iTXt_k "iTXt"
#define tEXt_k "tEXt"
#define zTXt_k "zTXt"
#define IDAT_k "IDAT"
#define IEND_k "IEND"
#define gAMA_k "gAMA"
#define iCCP_k "iCCP"
#define acTL_k "acTL"
#define fdAT_k "fdAT"
#define fcTL_k "fcTL"
if (type == IHDR_k) {
if (name == IHDR_k) {
ihdr_t ihdr [[comment("Image Header chunk"), name("IHDR")]];
} else if (type == PLTE_k) {
} else if (name == PLTE_k) {
palette_entry_t entries[length / 3];
} else if (type == pHYs_k) {
} else if (name == sRGB_k) {
sRGB srgb;
} else if (name == pHYs_k) {
phys_t phys;
} else if (type == acTL_k) {
actl_t actl [[comment("Animation control chunk")]];
} else if (type == fcTL_k) {
fctl_t fctl [[comment("Frame control chunk")]];
} else if (type == iTXt_k) {
itxt_t text;
} else if (type == gAMA_k) {
u32 gamma [[name("image gamma"), comment("4 byte unsigned integer representing gamma times 100000")]];
} else if (type == iCCP_k) {
iccp_t iccp;
} else if (name == tEXt_k) {
text_t text;
} else if (name == zTXt_k) {
ztxt_t text;
} else if (name == iCCP_k) {
iccp_t iccp;
} else if (name == fdAT_k) {
fdat_t fdat [[comment("Frame data chunk")]];
u8 data[length-sizeof(u32)];
} else {
u8 data[length];
}
u32 crc;
} [[format("chunkValueName")]];
fn chunkValueName(ref chunk_t chunk) {
return chunk.name;
};
struct chunk_set {
chunk_t chunks[while(builtin::std::mem::read_string($ + 4, 4) != "IEND")] [[inline]];
} [[inline]];
header_t header @ 0x00 [[comment("PNG file signature"), name("Signature")]];
chunk_t ihdr_chunk @ 0x08 [[comment("PNG Header chunk"), name("IHDR")]];
chunk_t chunk_set[while(builtin::std::mem::read_string($ + 4, 4) != "IEND")] @ $ [[comment("PNG file chunks"), name("Chunks"), inline]];
chunk_t iend_chunk @ $ [[name("IEND"), comment("Image End Chunk")]];
chunk_set set @ $ [[comment("PNG Chunks"), name("Chunks"), inline]];
chunk_t iend_chunk @ $ [[comment("Image End Chunk"), name("IEND")]];

Binary file not shown.