2023-09-27 19:24:48 +03:00
|
|
|
#include "prism/pack.h"
|
2023-06-20 18:53:02 +03:00
|
|
|
|
|
|
|
#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-06-20 18:53:02 +03:00
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
if (*encoding == PM_PACK_ENCODING_START) {
|
|
|
|
*encoding = PM_PACK_ENCODING_US_ASCII;
|
2023-06-20 18:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-20 18:53:02 +03:00
|
|
|
}
|
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
*length_type = PM_PACK_LENGTH_FIXED;
|
2023-06-20 18:53:02 +03:00
|
|
|
*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;
|
2023-06-20 18:53:02 +03:00
|
|
|
*length = 0;
|
2023-09-27 19:24:48 +03:00
|
|
|
return PM_PACK_OK;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
*length = 0;
|
2023-09-27 19:24:48 +03:00
|
|
|
return PM_PACK_OK;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
break;
|
|
|
|
case '%':
|
2023-09-27 19:24:48 +03:00
|
|
|
return PM_PACK_ERROR_UNSUPPORTED_DIRECTIVE;
|
2023-06-20 18:53:02 +03:00
|
|
|
default:
|
2023-09-27 19:24:48 +03:00
|
|
|
return PM_PACK_ERROR_UNKNOWN_DIRECTIVE;
|
2023-06-20 18:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2023-06-20 18:53:02 +03:00
|
|
|
}
|
|
|
|
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:
|
2023-06-20 18:53:02 +03:00
|
|
|
break;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_16:
|
|
|
|
*size = PM_PACK_SIZE_SHORT;
|
2023-06-20 18:53:02 +03:00
|
|
|
break;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_32:
|
|
|
|
*size = PM_PACK_SIZE_LONG;
|
2023-06-20 18:53:02 +03:00
|
|
|
break;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_64:
|
|
|
|
*size = PM_PACK_SIZE_LONG_LONG;
|
2023-06-20 18:53:02 +03:00
|
|
|
break;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_P:
|
2023-06-20 18:53:02 +03:00
|
|
|
break;
|
|
|
|
default:
|
2023-09-27 19:24:48 +03:00
|
|
|
return PM_PACK_ERROR_BANG_NOT_ALLOWED;
|
2023-06-20 18:53:02 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
(*format)++;
|
|
|
|
if (explicit_endian) {
|
2023-09-27 19:24:48 +03:00
|
|
|
return PM_PACK_ERROR_DOUBLE_ENDIAN;
|
2023-06-20 18:53:02 +03:00
|
|
|
}
|
2023-09-27 19:24:48 +03:00
|
|
|
*endian = PM_PACK_LITTLE_ENDIAN;
|
2023-06-20 18:53:02 +03:00
|
|
|
explicit_endian = true;
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
(*format)++;
|
|
|
|
if (explicit_endian) {
|
2023-09-27 19:24:48 +03:00
|
|
|
return PM_PACK_ERROR_DOUBLE_ENDIAN;
|
2023-06-20 18:53:02 +03:00
|
|
|
}
|
2023-09-27 19:24:48 +03:00
|
|
|
*endian = PM_PACK_BIG_ENDIAN;
|
2023-06-20 18:53:02 +03:00
|
|
|
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) {
|
2023-06-20 18:53:02 +03:00
|
|
|
*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:
|
2023-06-20 18:53:02 +03:00
|
|
|
switch (variant) {
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_VARIANT_PACK:
|
|
|
|
*length_type = PM_PACK_LENGTH_FIXED;
|
2023-06-20 18:53:02 +03:00
|
|
|
break;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_VARIANT_UNPACK:
|
|
|
|
*length_type = PM_PACK_LENGTH_MAX;
|
2023-06-20 18:53:02 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
*length = 0;
|
|
|
|
break;
|
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_MOVE:
|
2023-06-20 18:53:02 +03:00
|
|
|
switch (variant) {
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_VARIANT_PACK:
|
|
|
|
*length_type = PM_PACK_LENGTH_FIXED;
|
2023-06-20 18:53:02 +03:00
|
|
|
break;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_VARIANT_UNPACK:
|
|
|
|
*length_type = PM_PACK_LENGTH_RELATIVE;
|
2023-06-20 18:53:02 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
*length = 0;
|
|
|
|
break;
|
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_UU:
|
|
|
|
*length_type = PM_PACK_LENGTH_FIXED;
|
2023-06-20 18:53:02 +03:00
|
|
|
*length = 0;
|
|
|
|
break;
|
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_FIXED:
|
2023-06-20 18:53:02 +03:00
|
|
|
switch (variant) {
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_VARIANT_PACK:
|
|
|
|
*length_type = PM_PACK_LENGTH_FIXED;
|
2023-06-20 18:53:02 +03:00
|
|
|
*length = 1;
|
|
|
|
break;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_VARIANT_UNPACK:
|
|
|
|
*length_type = PM_PACK_LENGTH_MAX;
|
2023-06-20 18:53:02 +03:00
|
|
|
*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;
|
2023-06-20 18:53:02 +03:00
|
|
|
*length = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-09-27 19:24:48 +03:00
|
|
|
*length_type = PM_PACK_LENGTH_MAX;
|
2023-06-20 18:53:02 +03:00
|
|
|
*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;
|
2023-06-20 18:53:02 +03:00
|
|
|
#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"
|
2023-06-20 18:53:02 +03:00
|
|
|
#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;
|
2023-06-20 18:53:02 +03:00
|
|
|
}
|
|
|
|
*length = (uint64_t) length_max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*type) {
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_UTF8:
|
2023-06-20 18:53:02 +03:00
|
|
|
/* 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;
|
2023-06-20 18:53:02 +03:00
|
|
|
}
|
|
|
|
break;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_STRING_MIME:
|
|
|
|
case PM_PACK_STRING_BASE64:
|
|
|
|
case PM_PACK_STRING_UU:
|
2023-06-20 18:53:02 +03:00
|
|
|
/* 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;
|
2023-06-20 18:53:02 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
return PM_PACK_OK;
|
2023-06-20 18:53:02 +03:00
|
|
|
}
|
|
|
|
|
2023-09-27 19:24:48 +03:00
|
|
|
PRISM_EXPORTED_FUNCTION size_t
|
|
|
|
pm_size_to_native(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 sizeof(short);
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_INT:
|
2023-06-20 18:53:02 +03:00
|
|
|
return sizeof(int);
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_LONG:
|
2023-06-20 18:53:02 +03:00
|
|
|
return sizeof(long);
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_LONG_LONG:
|
2023-06-20 18:53:02 +03:00
|
|
|
return sizeof(long long);
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_8:
|
2023-06-20 18:53:02 +03:00
|
|
|
return 1;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_16:
|
2023-06-20 18:53:02 +03:00
|
|
|
return 2;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_32:
|
2023-06-20 18:53:02 +03:00
|
|
|
return 4;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_64:
|
2023-06-20 18:53:02 +03:00
|
|
|
return 8;
|
2023-09-27 19:24:48 +03:00
|
|
|
case PM_PACK_SIZE_P:
|
2023-06-20 18:53:02 +03:00
|
|
|
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;
|
|
|
|
}
|