ruby/prism/pack.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

494 строки
16 KiB
C
Исходник Обычный вид История

2023-09-27 19:24:48 +03:00
#include "prism/pack.h"
#include <stdbool.h>
#include <errno.h>
static uintmax_t
strtoumaxc(const char **format);
2023-09-27 19:24:48 +03:00
PRISM_EXPORTED_FUNCTION pm_pack_result
pm_pack_parse(pm_pack_variant variant, const char **format, const char *format_end,
pm_pack_type *type, pm_pack_signed *signed_type, pm_pack_endian *endian, pm_pack_size *size,
pm_pack_length_type *length_type, uint64_t *length, pm_pack_encoding *encoding) {
2023-09-27 19:24:48 +03:00
if (*encoding == PM_PACK_ENCODING_START) {
*encoding = PM_PACK_ENCODING_US_ASCII;
}
if (*format == format_end) {
2023-09-27 19:24:48 +03:00
*type = PM_PACK_END;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
*length_type = PM_PACK_LENGTH_NA;
return PM_PACK_OK;
}
2023-09-27 19:24:48 +03:00
*length_type = PM_PACK_LENGTH_FIXED;
*length = 1;
bool length_changed_allowed = true;
char directive = **format;
(*format)++;
switch (directive) {
case ' ':
case '\t':
case '\n':
case '\v':
case '\f':
case '\r':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_SPACE;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
*length_type = PM_PACK_LENGTH_NA;
*length = 0;
2023-09-27 19:24:48 +03:00
return PM_PACK_OK;
case '#':
while ((*format < format_end) && (**format != '\n')) {
(*format)++;
}
2023-09-27 19:24:48 +03:00
*type = PM_PACK_COMMENT;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
*length_type = PM_PACK_LENGTH_NA;
*length = 0;
2023-09-27 19:24:48 +03:00
return PM_PACK_OK;
case 'C':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_UNSIGNED;
*endian = PM_PACK_AGNOSTIC_ENDIAN;
*size = PM_PACK_SIZE_8;
break;
case 'S':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_UNSIGNED;
*endian = PM_PACK_NATIVE_ENDIAN;
*size = PM_PACK_SIZE_16;
break;
case 'L':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_UNSIGNED;
*endian = PM_PACK_NATIVE_ENDIAN;
*size = PM_PACK_SIZE_32;
break;
case 'Q':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_UNSIGNED;
*endian = PM_PACK_NATIVE_ENDIAN;
*size = PM_PACK_SIZE_64;
break;
case 'J':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_UNSIGNED;
*endian = PM_PACK_NATIVE_ENDIAN;
*size = PM_PACK_SIZE_P;
break;
case 'c':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_SIGNED;
*endian = PM_PACK_AGNOSTIC_ENDIAN;
*size = PM_PACK_SIZE_8;
break;
case 's':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_SIGNED;
*endian = PM_PACK_NATIVE_ENDIAN;
*size = PM_PACK_SIZE_16;
break;
case 'l':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_SIGNED;
*endian = PM_PACK_NATIVE_ENDIAN;
*size = PM_PACK_SIZE_32;
break;
case 'q':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_SIGNED;
*endian = PM_PACK_NATIVE_ENDIAN;
*size = PM_PACK_SIZE_64;
break;
case 'j':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_SIGNED;
*endian = PM_PACK_NATIVE_ENDIAN;
*size = PM_PACK_SIZE_P;
break;
case 'I':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_UNSIGNED;
*endian = PM_PACK_NATIVE_ENDIAN;
*size = PM_PACK_SIZE_INT;
break;
case 'i':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_SIGNED;
*endian = PM_PACK_NATIVE_ENDIAN;
*size = PM_PACK_SIZE_INT;
break;
case 'n':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_UNSIGNED;
*endian = PM_PACK_BIG_ENDIAN;
*size = PM_PACK_SIZE_16;
length_changed_allowed = false;
break;
case 'N':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_UNSIGNED;
*endian = PM_PACK_BIG_ENDIAN;
*size = PM_PACK_SIZE_32;
length_changed_allowed = false;
break;
case 'v':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_UNSIGNED;
*endian = PM_PACK_LITTLE_ENDIAN;
*size = PM_PACK_SIZE_16;
length_changed_allowed = false;
break;
case 'V':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_INTEGER;
*signed_type = PM_PACK_UNSIGNED;
*endian = PM_PACK_LITTLE_ENDIAN;
*size = PM_PACK_SIZE_32;
length_changed_allowed = false;
break;
case 'U':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_UTF8;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'w':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_BER;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'D':
case 'd':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_FLOAT;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_NATIVE_ENDIAN;
*size = PM_PACK_SIZE_64;
break;
case 'F':
case 'f':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_FLOAT;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_NATIVE_ENDIAN;
*size = PM_PACK_SIZE_32;
break;
case 'E':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_FLOAT;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_LITTLE_ENDIAN;
*size = PM_PACK_SIZE_64;
break;
case 'e':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_FLOAT;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_LITTLE_ENDIAN;
*size = PM_PACK_SIZE_32;
break;
case 'G':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_FLOAT;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_BIG_ENDIAN;
*size = PM_PACK_SIZE_64;
break;
case 'g':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_FLOAT;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_BIG_ENDIAN;
*size = PM_PACK_SIZE_32;
break;
case 'A':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_STRING_SPACE_PADDED;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'a':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_STRING_NULL_PADDED;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'Z':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_STRING_NULL_TERMINATED;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'B':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_STRING_MSB;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'b':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_STRING_LSB;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'H':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_STRING_HEX_HIGH;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'h':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_STRING_HEX_LOW;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'u':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_STRING_UU;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'M':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_STRING_MIME;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'm':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_STRING_BASE64;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'P':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_STRING_FIXED;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'p':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_STRING_POINTER;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case '@':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_MOVE;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'X':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_BACK;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case 'x':
2023-09-27 19:24:48 +03:00
*type = PM_PACK_NULL;
*signed_type = PM_PACK_SIGNED_NA;
*endian = PM_PACK_ENDIAN_NA;
*size = PM_PACK_SIZE_NA;
break;
case '%':
2023-09-27 19:24:48 +03:00
return PM_PACK_ERROR_UNSUPPORTED_DIRECTIVE;
default:
2023-09-27 19:24:48 +03:00
return PM_PACK_ERROR_UNKNOWN_DIRECTIVE;
}
bool explicit_endian = false;
while (*format < format_end) {
switch (**format) {
case '_':
case '!':
(*format)++;
2023-09-27 19:24:48 +03:00
if (*type != PM_PACK_INTEGER || !length_changed_allowed) {
return PM_PACK_ERROR_BANG_NOT_ALLOWED;
}
switch (*size) {
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_SHORT:
case PM_PACK_SIZE_INT:
case PM_PACK_SIZE_LONG:
case PM_PACK_SIZE_LONG_LONG:
break;
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_16:
*size = PM_PACK_SIZE_SHORT;
break;
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_32:
*size = PM_PACK_SIZE_LONG;
break;
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_64:
*size = PM_PACK_SIZE_LONG_LONG;
break;
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_P:
break;
default:
2023-09-27 19:24:48 +03:00
return PM_PACK_ERROR_BANG_NOT_ALLOWED;
}
break;
case '<':
(*format)++;
if (explicit_endian) {
2023-09-27 19:24:48 +03:00
return PM_PACK_ERROR_DOUBLE_ENDIAN;
}
2023-09-27 19:24:48 +03:00
*endian = PM_PACK_LITTLE_ENDIAN;
explicit_endian = true;
break;
case '>':
(*format)++;
if (explicit_endian) {
2023-09-27 19:24:48 +03:00
return PM_PACK_ERROR_DOUBLE_ENDIAN;
}
2023-09-27 19:24:48 +03:00
*endian = PM_PACK_BIG_ENDIAN;
explicit_endian = true;
break;
default:
goto exit_modifier_loop;
}
}
exit_modifier_loop:
2023-09-27 19:24:48 +03:00
if (variant == PM_PACK_VARIANT_UNPACK && *type == PM_PACK_MOVE) {
*length = 0;
}
if (*format < format_end) {
if (**format == '*') {
switch (*type) {
2023-09-27 19:24:48 +03:00
case PM_PACK_NULL:
case PM_PACK_BACK:
switch (variant) {
2023-09-27 19:24:48 +03:00
case PM_PACK_VARIANT_PACK:
*length_type = PM_PACK_LENGTH_FIXED;
break;
2023-09-27 19:24:48 +03:00
case PM_PACK_VARIANT_UNPACK:
*length_type = PM_PACK_LENGTH_MAX;
break;
}
*length = 0;
break;
2023-09-27 19:24:48 +03:00
case PM_PACK_MOVE:
switch (variant) {
2023-09-27 19:24:48 +03:00
case PM_PACK_VARIANT_PACK:
*length_type = PM_PACK_LENGTH_FIXED;
break;
2023-09-27 19:24:48 +03:00
case PM_PACK_VARIANT_UNPACK:
*length_type = PM_PACK_LENGTH_RELATIVE;
break;
}
*length = 0;
break;
2023-09-27 19:24:48 +03:00
case PM_PACK_STRING_UU:
*length_type = PM_PACK_LENGTH_FIXED;
*length = 0;
break;
2023-09-27 19:24:48 +03:00
case PM_PACK_STRING_FIXED:
switch (variant) {
2023-09-27 19:24:48 +03:00
case PM_PACK_VARIANT_PACK:
*length_type = PM_PACK_LENGTH_FIXED;
*length = 1;
break;
2023-09-27 19:24:48 +03:00
case PM_PACK_VARIANT_UNPACK:
*length_type = PM_PACK_LENGTH_MAX;
*length = 0;
break;
}
break;
2023-09-27 19:24:48 +03:00
case PM_PACK_STRING_MIME:
case PM_PACK_STRING_BASE64:
*length_type = PM_PACK_LENGTH_FIXED;
*length = 1;
break;
default:
2023-09-27 19:24:48 +03:00
*length_type = PM_PACK_LENGTH_MAX;
*length = 0;
break;
}
(*format)++;
} else if (**format >= '0' && **format <= '9') {
errno = 0;
2023-09-27 19:24:48 +03:00
*length_type = PM_PACK_LENGTH_FIXED;
#if UINTMAX_MAX < UINT64_MAX
2023-09-27 19:24:48 +03:00
#error "prism's design assumes uintmax_t is at least as large as uint64_t"
#endif
uintmax_t length_max = strtoumaxc(format);
if (errno || length_max > UINT64_MAX) {
2023-09-27 19:24:48 +03:00
return PM_PACK_ERROR_LENGTH_TOO_BIG;
}
*length = (uint64_t) length_max;
}
}
switch (*type) {
2023-09-27 19:24:48 +03:00
case PM_PACK_UTF8:
/* if encoding is US-ASCII, upgrade to UTF-8 */
2023-09-27 19:24:48 +03:00
if (*encoding == PM_PACK_ENCODING_US_ASCII) {
*encoding = PM_PACK_ENCODING_UTF_8;
}
break;
2023-09-27 19:24:48 +03:00
case PM_PACK_STRING_MIME:
case PM_PACK_STRING_BASE64:
case PM_PACK_STRING_UU:
/* keep US-ASCII (do nothing) */
break;
default:
/* fall back to BINARY */
2023-09-27 19:24:48 +03:00
*encoding = PM_PACK_ENCODING_ASCII_8BIT;
break;
}
2023-09-27 19:24:48 +03:00
return PM_PACK_OK;
}
2023-09-27 19:24:48 +03:00
PRISM_EXPORTED_FUNCTION size_t
pm_size_to_native(pm_pack_size size) {
switch (size) {
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_SHORT:
return sizeof(short);
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_INT:
return sizeof(int);
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_LONG:
return sizeof(long);
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_LONG_LONG:
return sizeof(long long);
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_8:
return 1;
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_16:
return 2;
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_32:
return 4;
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_64:
return 8;
2023-09-27 19:24:48 +03:00
case PM_PACK_SIZE_P:
return sizeof(void *);
default:
return 0;
}
}
static uintmax_t
strtoumaxc(const char **format) {
uintmax_t value = 0;
while (**format >= '0' && **format <= '9') {
if (value > UINTMAX_MAX / 10) {
errno = ERANGE;
}
value = value * 10 + ((uintmax_t) (**format - '0'));
(*format)++;
}
return value;
}