2023-09-27 19:24:48 +03:00
|
|
|
#include "prism/extension.h"
|
2023-06-20 18:53:02 +03:00
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
static VALUE rb_cPrism;
|
|
|
|
static VALUE rb_cPrismPack;
|
|
|
|
static VALUE rb_cPrismPackDirective;
|
|
|
|
static VALUE rb_cPrismPackFormat;
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
static VALUE v3_2_0_symbol;
|
|
|
|
static VALUE pack_symbol;
|
|
|
|
static VALUE unpack_symbol;
|
|
|
|
|
2023-08-05 05:11:26 +03:00
|
|
|
#if SIZEOF_UINT64_T == SIZEOF_LONG_LONG
|
|
|
|
# define UINT64T2NUM(x) ULL2NUM(x)
|
|
|
|
# define NUM2UINT64T(x) (uint64_t)NUM2ULL(x)
|
|
|
|
#elif SIZEOF_UINT64_T == SIZEOF_LONG
|
|
|
|
# define UINT64T2NUM(x) ULONG2NUM(x)
|
|
|
|
# define NUM2UINT64T(x) (uint64_t)NUM2ULONG(x)
|
|
|
|
#else
|
|
|
|
// error No uint64_t conversion
|
|
|
|
#endif
|
|
|
|
|
2023-06-20 18:53:02 +03:00
|
|
|
static VALUE
|
2023-09-27 19:24:48 +03:00
|
|
|
pack_type_to_symbol(pm_pack_type type) {
|
2023-06-20 18:53:02 +03:00
|
|
|
switch (type) {
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SPACE:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("SPACE"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_COMMENT:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("COMMENT"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_INTEGER:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("INTEGER"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_UTF8:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("UTF8"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_BER:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("BER"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_FLOAT:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("FLOAT"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_SPACE_PADDED:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("STRING_SPACE_PADDED"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_NULL_PADDED:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("STRING_NULL_PADDED"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_NULL_TERMINATED:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("STRING_NULL_TERMINATED"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_MSB:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("STRING_MSB"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_LSB:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("STRING_LSB"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_HEX_HIGH:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("STRING_HEX_HIGH"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_HEX_LOW:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("STRING_HEX_LOW"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_UU:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("STRING_UU"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_MIME:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("STRING_MIME"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_BASE64:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("STRING_BASE64"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_FIXED:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("STRING_FIXED"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_POINTER:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("STRING_POINTER"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_MOVE:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("MOVE"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_BACK:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("BACK"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_NULL:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("NULL"));
|
|
|
|
default:
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2023-09-27 19:24:48 +03:00
|
|
|
pack_signed_to_symbol(pm_pack_signed signed_type) {
|
2023-06-20 18:53:02 +03:00
|
|
|
switch (signed_type) {
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_UNSIGNED:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("UNSIGNED"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIGNED:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("SIGNED"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIGNED_NA:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("SIGNED_NA"));
|
|
|
|
default:
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2023-09-27 19:24:48 +03:00
|
|
|
pack_endian_to_symbol(pm_pack_endian endian) {
|
2023-06-20 18:53:02 +03:00
|
|
|
switch (endian) {
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_AGNOSTIC_ENDIAN:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("AGNOSTIC_ENDIAN"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_LITTLE_ENDIAN:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("LITTLE_ENDIAN"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_BIG_ENDIAN:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("BIG_ENDIAN"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_NATIVE_ENDIAN:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("NATIVE_ENDIAN"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_ENDIAN_NA:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("ENDIAN_NA"));
|
|
|
|
default:
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2023-09-27 19:24:48 +03:00
|
|
|
pack_size_to_symbol(pm_pack_size size) {
|
2023-06-20 18:53:02 +03:00
|
|
|
switch (size) {
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_SHORT:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("SIZE_SHORT"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_INT:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("SIZE_INT"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_LONG:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("SIZE_LONG"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_LONG_LONG:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("SIZE_LONG_LONG"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_8:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("SIZE_8"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_16:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("SIZE_16"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_32:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("SIZE_32"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_64:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("SIZE_64"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_P:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("SIZE_P"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_NA:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("SIZE_NA"));
|
|
|
|
default:
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2023-09-27 19:24:48 +03:00
|
|
|
pack_length_type_to_symbol(pm_pack_length_type length_type) {
|
2023-06-20 18:53:02 +03:00
|
|
|
switch (length_type) {
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_LENGTH_FIXED:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("LENGTH_FIXED"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_LENGTH_MAX:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("LENGTH_MAX"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_LENGTH_RELATIVE:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("LENGTH_RELATIVE"));
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_LENGTH_NA:
|
2023-06-20 18:53:02 +03:00
|
|
|
return ID2SYM(rb_intern("LENGTH_NA"));
|
|
|
|
default:
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2023-09-27 19:24:48 +03:00
|
|
|
pack_encoding_to_ruby(pm_pack_encoding encoding) {
|
2023-06-20 18:53:02 +03:00
|
|
|
int index;
|
|
|
|
switch (encoding) {
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_ENCODING_ASCII_8BIT:
|
2023-06-20 18:53:02 +03:00
|
|
|
index = rb_ascii8bit_encindex();
|
|
|
|
break;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_ENCODING_US_ASCII:
|
2023-06-20 18:53:02 +03:00
|
|
|
index = rb_usascii_encindex();
|
|
|
|
break;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_ENCODING_UTF_8:
|
2023-06-20 18:53:02 +03:00
|
|
|
index = rb_utf8_encindex();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
return rb_enc_from_encoding(rb_enc_from_index(index));
|
|
|
|
}
|
|
|
|
|
2023-10-30 22:11:24 +03:00
|
|
|
/**
|
|
|
|
* call-seq:
|
|
|
|
* Pack::parse(version, variant, source) -> Format
|
2023-10-31 22:40:50 +03:00
|
|
|
*
|
2023-10-30 22:11:24 +03:00
|
|
|
* Parse the given source and return a format object.
|
|
|
|
*/
|
2023-06-20 18:53:02 +03:00
|
|
|
static VALUE
|
|
|
|
pack_parse(VALUE self, VALUE version_symbol, VALUE variant_symbol, VALUE format_string) {
|
|
|
|
if (version_symbol != v3_2_0_symbol) {
|
|
|
|
rb_raise(rb_eArgError, "invalid version");
|
|
|
|
}
|
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
pm_pack_variant variant;
|
2023-06-20 18:53:02 +03:00
|
|
|
if (variant_symbol == pack_symbol) {
|
2023-09-27 19:24:48 +03:00
|
|
|
variant = PM_PACK_VARIANT_PACK;
|
2023-06-20 18:53:02 +03:00
|
|
|
} else if (variant_symbol == unpack_symbol) {
|
2023-09-27 19:24:48 +03:00
|
|
|
variant = PM_PACK_VARIANT_UNPACK;
|
2023-06-20 18:53:02 +03:00
|
|
|
} else {
|
|
|
|
rb_raise(rb_eArgError, "invalid variant");
|
|
|
|
}
|
|
|
|
|
|
|
|
StringValue(format_string);
|
|
|
|
|
|
|
|
const char *format = RSTRING_PTR(format_string);
|
|
|
|
const char *format_end = format + RSTRING_LEN(format_string);
|
2023-09-27 19:24:48 +03:00
|
|
|
pm_pack_encoding encoding = PM_PACK_ENCODING_START;
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
VALUE directives_array = rb_ary_new();
|
|
|
|
|
|
|
|
while (format < format_end) {
|
2023-09-27 19:24:48 +03:00
|
|
|
pm_pack_type type;
|
|
|
|
pm_pack_signed signed_type;
|
|
|
|
pm_pack_endian endian;
|
|
|
|
pm_pack_size size;
|
|
|
|
pm_pack_length_type length_type;
|
2023-06-20 18:53:02 +03:00
|
|
|
uint64_t length;
|
|
|
|
|
|
|
|
const char *directive_start = format;
|
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
pm_pack_result parse_result = pm_pack_parse(variant, &format, format_end, &type, &signed_type, &endian,
|
2023-08-05 05:09:10 +03:00
|
|
|
&size, &length_type, &length, &encoding);
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
const char *directive_end = format;
|
|
|
|
|
|
|
|
switch (parse_result) {
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_OK:
|
2023-06-20 18:53:02 +03:00
|
|
|
break;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_ERROR_UNSUPPORTED_DIRECTIVE:
|
2023-06-20 18:53:02 +03:00
|
|
|
rb_raise(rb_eArgError, "unsupported directive");
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_ERROR_UNKNOWN_DIRECTIVE:
|
2023-06-20 18:53:02 +03:00
|
|
|
rb_raise(rb_eArgError, "unsupported directive");
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_ERROR_LENGTH_TOO_BIG:
|
2023-06-20 18:53:02 +03:00
|
|
|
rb_raise(rb_eRangeError, "pack length too big");
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_ERROR_BANG_NOT_ALLOWED:
|
2023-06-20 18:53:02 +03:00
|
|
|
rb_raise(rb_eRangeError, "bang not allowed");
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_ERROR_DOUBLE_ENDIAN:
|
2023-06-20 18:53:02 +03:00
|
|
|
rb_raise(rb_eRangeError, "double endian");
|
|
|
|
default:
|
|
|
|
rb_bug("parse result");
|
|
|
|
}
|
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
if (type == PM_PACK_END) {
|
2023-06-20 18:53:02 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-10-30 22:11:24 +03:00
|
|
|
VALUE directive_args[9] = {
|
|
|
|
version_symbol,
|
|
|
|
variant_symbol,
|
|
|
|
rb_usascii_str_new(directive_start, directive_end - directive_start),
|
|
|
|
pack_type_to_symbol(type),
|
|
|
|
pack_signed_to_symbol(signed_type),
|
|
|
|
pack_endian_to_symbol(endian),
|
|
|
|
pack_size_to_symbol(size),
|
|
|
|
pack_length_type_to_symbol(length_type),
|
|
|
|
UINT64T2NUM(length)
|
|
|
|
};
|
2023-06-20 18:53:02 +03:00
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
rb_ary_push(directives_array, rb_class_new_instance(9, directive_args, rb_cPrismPackDirective));
|
2023-06-20 18:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE format_args[2];
|
|
|
|
format_args[0] = directives_array;
|
|
|
|
format_args[1] = pack_encoding_to_ruby(encoding);
|
2023-09-27 19:24:48 +03:00
|
|
|
return rb_class_new_instance(2, format_args, rb_cPrismPackFormat);
|
2023-06-20 18:53:02 +03:00
|
|
|
}
|
|
|
|
|
2023-10-30 22:11:24 +03:00
|
|
|
/**
|
|
|
|
* The function that gets called when Ruby initializes the prism extension.
|
|
|
|
*/
|
2023-06-20 18:53:02 +03:00
|
|
|
void
|
2023-09-27 19:24:48 +03:00
|
|
|
Init_prism_pack(void) {
|
|
|
|
rb_cPrism = rb_define_module("Prism");
|
|
|
|
rb_cPrismPack = rb_define_module_under(rb_cPrism, "Pack");
|
|
|
|
rb_cPrismPackDirective = rb_define_class_under(rb_cPrismPack, "Directive", rb_cObject);
|
|
|
|
rb_cPrismPackFormat = rb_define_class_under(rb_cPrismPack, "Format", rb_cObject);
|
|
|
|
rb_define_singleton_method(rb_cPrismPack, "parse", pack_parse, 3);
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
v3_2_0_symbol = ID2SYM(rb_intern("v3_2_0"));
|
|
|
|
pack_symbol = ID2SYM(rb_intern("pack"));
|
|
|
|
unpack_symbol = ID2SYM(rb_intern("unpack"));
|
|
|
|
}
|