/* Generated by zi2c from compiler_bundle.zi. */ #define ZIRAN_COMPILER_BUNDLE_H_SOURCE 1 #include #include "zir_bounds.h" #include "zir_string.h" #define ZIRAN_COMPILER_BUNDLE_H_PRIVATE 1 #include "compiler_bundle.h" #undef ZIRAN_COMPILER_BUNDLE_H_PRIVATE #ifndef ZIRAN_HELPER_ZiShl32 #define ZIRAN_HELPER_ZiShl32 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__OPTIMIZE__) #define ZiShl32(a, s, w) __extension__ ({ uint32_t __zi_a = (a); uint64_t __zi_s = (s); if(__zi_s >= (uint64_t)(w)) abort(); (uint32_t)(__zi_a << __zi_s); }) #else static inline uint32_t ZiShl32(uint32_t a, uint64_t s, int w) { if(s >= (uint64_t)w) abort(); return (uint32_t)(a << s); } #endif #endif #ifndef ZIRAN_HELPER_ZiShrU32 #define ZIRAN_HELPER_ZiShrU32 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__OPTIMIZE__) #define ZiShrU32(a, s, w) __extension__ ({ uint32_t __zi_a = (a); uint64_t __zi_s = (s); if(__zi_s >= (uint64_t)(w)) abort(); __zi_a >> __zi_s; }) #else static inline uint32_t ZiShrU32(uint32_t a, uint64_t s, int w) { if(s >= (uint64_t)w) abort(); return a >> s; } #endif #endif #ifndef ZIRAN_HELPER_ZiShl64 #define ZIRAN_HELPER_ZiShl64 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__OPTIMIZE__) #define ZiShl64(a, s, w) __extension__ ({ uint64_t __zi_a = (a); uint64_t __zi_s = (s); if(__zi_s >= (uint64_t)(w)) abort(); (uint64_t)(__zi_a << __zi_s); }) #else static inline uint64_t ZiShl64(uint64_t a, uint64_t s, int w) { if(s >= (uint64_t)w) abort(); return (uint64_t)(a << s); } #endif #endif #ifndef ZIRAN_HELPER_ZiShrU64 #define ZIRAN_HELPER_ZiShrU64 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__OPTIMIZE__) #define ZiShrU64(a, s, w) __extension__ ({ uint64_t __zi_a = (a); uint64_t __zi_s = (s); if(__zi_s >= (uint64_t)(w)) abort(); __zi_a >> __zi_s; }) #else static inline uint64_t ZiShrU64(uint64_t a, uint64_t s, int w) { if(s >= (uint64_t)w) abort(); return a >> s; } #endif #endif #ifndef ZIRAN_MAYBE_UNUSED #if defined(__GNUC__) || defined(__clang__) #define ZIRAN_MAYBE_UNUSED __attribute__((unused)) #else #define ZIRAN_MAYBE_UNUSED #endif #endif static bool compiler_bundle_Fits(String text, bool empty, int64_t limit) ZIRAN_MAYBE_UNUSED; static bool compiler_bundle_Before(String left, String right) ZIRAN_MAYBE_UNUSED; static void compiler_bundle_Fail(Codec* codec, bool ended) ZIRAN_MAYBE_UNUSED; static uint32_t compiler_bundle_Word(Codec* codec, uint32_t value) ZIRAN_MAYBE_UNUSED; static int64_t compiler_bundle_Count(Codec* codec, int64_t count) ZIRAN_MAYBE_UNUSED; static String compiler_bundle_Bytes(Codec* codec, String text, int64_t limit) ZIRAN_MAYBE_UNUSED; static String compiler_bundle_Field(Codec* codec, String text, bool empty, int64_t limit) ZIRAN_MAYBE_UNUSED; static int64_t compiler_bundle_Assets(Codec* codec, Slice assets) ZIRAN_MAYBE_UNUSED; static int64_t compiler_bundle_ExecutableAlign(int64_t value, int64_t alignment) ZIRAN_MAYBE_UNUSED; static int64_t compiler_bundle_ExecutableReserve(ExecutableWriter* writer, int64_t count) ZIRAN_MAYBE_UNUSED; static void compiler_bundle_ExecutableU8(ExecutableWriter* writer, uint8_t value) ZIRAN_MAYBE_UNUSED; static void compiler_bundle_ExecutableU32(ExecutableWriter* writer, uint32_t value) ZIRAN_MAYBE_UNUSED; static void compiler_bundle_ExecutableU64(ExecutableWriter* writer, uint64_t value) ZIRAN_MAYBE_UNUSED; static void compiler_bundle_ExecutableRaw(ExecutableWriter* writer, String value) ZIRAN_MAYBE_UNUSED; static void compiler_bundle_ExecutablePad(ExecutableWriter* writer, int64_t alignment) ZIRAN_MAYBE_UNUSED; static bool compiler_bundle_ExecutableValidAttachment(ExecutableAttachment attachment, uint32_t previous) ZIRAN_MAYBE_UNUSED; static int64_t compiler_bundle_ExecutableSectionCount(ExecutableHeader header) ZIRAN_MAYBE_UNUSED; static int64_t compiler_bundle_ExecutableSectionSize(ExecutableHeader header, int64_t index, int64_t asset_size) ZIRAN_MAYBE_UNUSED; static uint32_t compiler_bundle_ExecutableSectionKind(ExecutableHeader header, int64_t index) ZIRAN_MAYBE_UNUSED; static void compiler_bundle_ExecutablePayload(ExecutableWriter* writer, ExecutableHeader header, int64_t index, int64_t asset_size) ZIRAN_MAYBE_UNUSED; static ExecutableProblem compiler_bundle_ExecutableValidateHeader(ExecutableHeader header) ZIRAN_MAYBE_UNUSED; static int64_t compiler_bundle_ExecutableNeed(ExecutableCursor* cursor, int64_t count) ZIRAN_MAYBE_UNUSED; static uint8_t compiler_bundle_ExecutableTakeU8(ExecutableCursor* cursor) ZIRAN_MAYBE_UNUSED; static uint32_t compiler_bundle_ExecutableTakeU32(ExecutableCursor* cursor) ZIRAN_MAYBE_UNUSED; static uint64_t compiler_bundle_ExecutableTakeU64(ExecutableCursor* cursor) ZIRAN_MAYBE_UNUSED; static bool compiler_bundle_ExecutableZero(String input, int64_t begin, int64_t end) ZIRAN_MAYBE_UNUSED; static ExecutableLayout compiler_bundle_ExecutableDecodeInternal(String bytes, Slice assets) ZIRAN_MAYBE_UNUSED; static int64_t compiler_bundle_EncodeAssets(Slice output, Slice assets) ZIRAN_MAYBE_UNUSED; static bool compiler_bundle_Fits(String text, bool empty, int64_t limit) { if (((int64_t)(text).length == 0LL && !empty) || (int64_t)(text).length >= limit) { return false; } for (int64_t it_index = 0LL; it_index < (int64_t)(text).length; it_index++) { uint8_t byte = (uint8_t)text.data[it_index]; if (byte == 0) { return false; } } return true; } static bool compiler_bundle_Before(String left, String right) { int64_t at = 0LL; while (at < (int64_t)(left).length && at < (int64_t)(right).length) { if ((uint8_t)left.data[at] != (uint8_t)right.data[at]) { return (uint8_t)left.data[at] < (uint8_t)right.data[at]; } at = (int64_t)((uint64_t)at + UINT64_C(1)); } return (int64_t)(left).length < (int64_t)(right).length; } static void compiler_bundle_Fail(Codec* codec, bool ended) { if (codec->valid) { bool* value_0 = &(codec->ended); (*value_0) = ended; } bool* value_1 = &(codec->valid); (*value_1) = false; } static uint32_t compiler_bundle_Word(Codec* codec, uint32_t value) { if (!codec->valid) { return 0; } int64_t at = codec->at; bool value_0 = codec->reading; if (value_0) { int64_t value_1 = (int64_t)((uint64_t)((int64_t)(codec->input).length) - (uint64_t)at); value_0 = (value_1 < 4LL); } if (value_0) { Codec* value_2 = codec; { Codec* codec = value_2; bool ended = true; if (codec->valid) { bool* value_4 = &(codec->ended); (*value_4) = ended; } bool* value_5 = &(codec->valid); (*value_5) = false; } return 0; } int64_t* value_6 = &(codec->at); (*value_6) = (int64_t)((uint64_t)((*value_6)) + UINT64_C(4)); if (codec->reading) { uint8_t value_7 = (uint8_t)ZIRAN_INDEX(codec->input.data, codec->input.length, ((int64_t)((uint64_t)at + UINT64_C(1)))); uint32_t value_8 = (uint32_t)((uint32_t)((uint8_t)ZIRAN_INDEX(codec->input.data, codec->input.length, at)) | ((uint32_t)(value_7) << 8)); uint8_t value_9 = (uint8_t)ZIRAN_INDEX(codec->input.data, codec->input.length, ((int64_t)((uint64_t)at + UINT64_C(2)))); uint8_t value_10 = (uint8_t)ZIRAN_INDEX(codec->input.data, codec->input.length, ((int64_t)((uint64_t)at + UINT64_C(3)))); return (uint32_t)(((uint32_t)value_8 | ((uint32_t)(value_9) << 16)) | ((uint32_t)(value_10) << 24)); } if (codec->write) { uint8_t* value_11 = &(((uint8_t *)codec->output.data)[SliceIndex(codec->output, (int64_t)at)]); (*value_11) = (uint8_t)(((uint32_t)((uint32_t)value & 255u))); uint8_t* value_12 = &(((uint8_t *)codec->output.data)[SliceIndex(codec->output, (int64_t)((int64_t)((uint64_t)at + UINT64_C(1))))]); (*value_12) = (uint8_t)(((uint32_t)((uint32_t)((value >> 8)) & 255u))); uint8_t* value_13 = &(((uint8_t *)codec->output.data)[SliceIndex(codec->output, (int64_t)((int64_t)((uint64_t)at + UINT64_C(2))))]); (*value_13) = (uint8_t)(((uint32_t)((uint32_t)((value >> 16)) & 255u))); uint8_t* value_14 = &(((uint8_t *)codec->output.data)[SliceIndex(codec->output, (int64_t)((int64_t)((uint64_t)at + UINT64_C(3))))]); (*value_14) = (uint8_t)((value >> 24)); } return value; } static int64_t compiler_bundle_Count(Codec* codec, int64_t count) { if (!codec->reading && (count < 0LL || count >= 4294967296LL)) { Codec* value_0 = codec; { Codec* codec = value_0; bool ended = false; if (codec->valid) { bool* value_2 = &(codec->ended); (*value_2) = ended; } bool* value_3 = &(codec->valid); (*value_3) = false; } return 0LL; } uint32_t value = 0; if (!codec->reading) { value = (uint32_t)(count); } uint32_t value_4 = compiler_bundle_Word(codec, value); return (int64_t)(value_4); } static String compiler_bundle_Bytes(Codec* codec, String text, int64_t limit) { Codec* value_0 = codec; int64_t value_1 = (int64_t)(text).length; int64_t value_2 = 0; { Codec* codec = value_0; int64_t count = value_1; if (!codec->reading && (count < 0LL || count >= 4294967296LL)) { Codec* value_4 = codec; { Codec* codec = value_4; bool ended = false; if (codec->valid) { bool* value_6 = &(codec->ended); (*value_6) = ended; } bool* value_7 = &(codec->valid); (*value_7) = false; } value_2 = 0LL; goto value_3; } uint32_t value = 0; if (!codec->reading) { value = (uint32_t)(count); } uint32_t value_8 = compiler_bundle_Word(codec, value); value_2 = (int64_t)(value_8); value_3: ; } int64_t length = value_2; if (codec->valid && length >= limit) { Codec* value_9 = codec; { Codec* codec = value_9; bool ended = false; if (codec->valid) { bool* value_11 = &(codec->ended); (*value_11) = ended; } bool* value_12 = &(codec->valid); (*value_12) = false; } } bool value_13 = codec->valid && codec->reading; if (value_13) { int64_t value_14 = (int64_t)((uint64_t)((int64_t)(codec->input).length) - (uint64_t)(codec->at)); value_13 = (length > value_14); } if (value_13) { Codec* value_15 = codec; { Codec* codec = value_15; bool ended = true; if (codec->valid) { bool* value_17 = &(codec->ended); (*value_17) = ended; } bool* value_18 = &(codec->valid); (*value_18) = false; } } if (!codec->valid) { return StringLiteral(""); } int64_t at = codec->at; int64_t* value_19 = &(codec->at); (*value_19) = (int64_t)((uint64_t)((*value_19)) + (uint64_t)length); if (codec->reading) { String value_20 = codec->input; return StringRange(value_20, (int64_t)at, (int64_t)((int64_t)((uint64_t)at + (uint64_t)length))); } if (codec->write) { { int64_t range_first_16 = 0LL; int64_t range_last_16 = (int64_t)((uint64_t)length - UINT64_C(1)); int64_t index = range_first_16; if (index <= range_last_16) for (;;) { uint8_t* value_21 = &(((uint8_t *)codec->output.data)[SliceIndex(codec->output, (int64_t)((int64_t)((uint64_t)at + (uint64_t)index)))]); (*value_21) = (uint8_t)ZIRAN_INDEX(text.data, text.length, index); if (index == range_last_16) { break; } index = (int64_t)((uint64_t)index + UINT64_C(1)); } } } return text; } static String compiler_bundle_Field(Codec* codec, String text, bool empty, int64_t limit) { String field = compiler_bundle_Bytes(codec, text, limit); bool value_0 = codec->valid; bool value_1 = value_0; if (value_1) { bool value_2 = compiler_bundle_Fits(field, empty, limit); value_1 = !value_2; } if (value_1) { Codec* value_3 = codec; { Codec* codec = value_3; bool ended = false; if (codec->valid) { bool* value_5 = &(codec->ended); (*value_5) = ended; } bool* value_6 = &(codec->valid); (*value_6) = false; } } return field; } static int64_t compiler_bundle_Assets(Codec* codec, Slice assets) { Codec* value_0 = codec; int64_t value_1 = (int64_t)(assets).length; int64_t value_2 = 0; { Codec* codec = value_0; int64_t count = value_1; if (!codec->reading && (count < 0LL || count >= 4294967296LL)) { Codec* value_4 = codec; { Codec* codec = value_4; bool ended = false; if (codec->valid) { bool* value_6 = &(codec->ended); (*value_6) = ended; } bool* value_7 = &(codec->valid); (*value_7) = false; } value_2 = 0LL; goto value_3; } uint32_t value = 0; if (!codec->reading) { value = (uint32_t)(count); } uint32_t value_8 = compiler_bundle_Word(codec, value); value_2 = (int64_t)(value_8); value_3: ; } int64_t count = value_2; String previous = StringLiteral(""); int64_t index = 0LL; while (codec->valid && index < count) { Asset asset = {0}; if (index < (int64_t)(assets).length) { asset = ((Asset *)assets.data)[index]; } Codec* value_9 = codec; String value_10 = asset.name; String value_11 = StringView(NULL, 0); { Codec* codec = value_9; String text = value_10; bool empty = false; int64_t limit = 4294967296LL; String field = compiler_bundle_Bytes(codec, text, limit); bool value_13 = codec->valid; bool value_14 = value_13; if (value_14) { bool value_15 = compiler_bundle_Fits(field, empty, limit); value_14 = !value_15; } if (value_14) { Codec* value_16 = codec; { Codec* codec = value_16; bool ended = false; if (codec->valid) { bool* value_18 = &(codec->ended); (*value_18) = ended; } bool* value_19 = &(codec->valid); (*value_19) = false; } } value_11 = field; } String name = value_11; bool value_20 = codec->valid; bool value_21 = value_20; if (value_21) { bool value_22 = compiler_bundle_AssetName(name); bool value_23 = !value_22; if (!value_23) { bool value_24 = index > 0LL; if (value_24) { bool value_25 = compiler_bundle_Before(previous, name); value_24 = !value_25; } value_23 = value_24; } value_21 = value_23; } if (value_21) { Codec* value_26 = codec; { Codec* codec = value_26; bool ended = false; if (codec->valid) { bool* value_28 = &(codec->ended); (*value_28) = ended; } bool* value_29 = &(codec->valid); (*value_29) = false; } } String data = compiler_bundle_Bytes(codec, asset.data, 4294967296LL); if (codec->reading && index < (int64_t)(assets).length) { Asset* value_30 = &(((Asset *)assets.data)[index]); (*value_30) = (Asset){.name = name, .data = data}; } previous = name; index = (int64_t)((uint64_t)index + UINT64_C(1)); } bool value_31 = codec->reading; if (value_31) { int64_t value_32 = codec->at; int64_t value_33 = (int64_t)(codec->input).length; value_31 = (value_32 != value_33); } if (value_31) { Codec* value_34 = codec; { Codec* codec = value_34; bool ended = false; if (codec->valid) { bool* value_36 = &(codec->ended); (*value_36) = ended; } bool* value_37 = &(codec->valid); (*value_37) = false; } } return count; } static int64_t compiler_bundle_ExecutableAlign(int64_t value, int64_t alignment) { bool value_0 = value < 0LL || alignment < 1LL || ((int64_t)((uint64_t)alignment & ((uint64_t)alignment - UINT64_C(1)))) != 0LL; if (value_0 || value > (int64_t)(UINT64_C(268435456) - ((uint64_t)alignment - UINT64_C(1)))) { return -1LL; } return (int64_t)((uint64_t)(((uint64_t)value + (uint64_t)alignment) - UINT64_C(1)) & (uint64_t)(((int64_t)~((uint64_t)alignment - UINT64_C(1))))); } static int64_t compiler_bundle_ExecutableReserve(ExecutableWriter* writer, int64_t count) { if (((ExecutableProblem)(writer->problem)) != ExecutableProblem_None) { return 0LL; } bool value_0 = count < 0LL; if (!value_0) { int64_t value_1 = writer->at; value_0 = (value_1 < 0LL); } bool value_2 = value_0; if (!value_2) { int64_t value_3 = (int64_t)(UINT64_C(268435456) - (uint64_t)(writer->at)); value_2 = (count > value_3); } if (value_2) { ExecutableProblem* value_4 = &(writer->problem); (*value_4) = ExecutableProblem_Limit; return 0LL; } int64_t start = writer->at; int64_t* value_5 = &(writer->at); (*value_5) = (int64_t)((uint64_t)((*value_5)) + (uint64_t)count); int64_t value_6 = (int64_t)(writer->output).length; bool value_7 = value_6 != 0LL; if (value_7) { int64_t value_8 = writer->at; int64_t value_9 = (int64_t)(writer->output).length; value_7 = (value_8 > value_9); } if (value_7) { ExecutableProblem* value_10 = &(writer->problem); (*value_10) = ExecutableProblem_Output; } return start; } static void compiler_bundle_ExecutableU8(ExecutableWriter* writer, uint8_t value) { ExecutableWriter* value_0 = writer; int64_t value_1 = 0; { ExecutableWriter* writer = value_0; int64_t count = 1LL; if (((ExecutableProblem)(writer->problem)) != ExecutableProblem_None) { value_1 = 0LL; goto value_2; } bool value_3 = count < 0LL; if (!value_3) { int64_t value_4 = writer->at; value_3 = (value_4 < 0LL); } bool value_5 = value_3; if (!value_5) { int64_t value_6 = (int64_t)(UINT64_C(268435456) - (uint64_t)(writer->at)); value_5 = (count > value_6); } if (value_5) { ExecutableProblem* value_7 = &(writer->problem); (*value_7) = ExecutableProblem_Limit; value_1 = 0LL; goto value_2; } int64_t start = writer->at; int64_t* value_8 = &(writer->at); (*value_8) = (int64_t)((uint64_t)((*value_8)) + (uint64_t)count); int64_t value_9 = (int64_t)(writer->output).length; bool value_10 = value_9 != 0LL; if (value_10) { int64_t value_11 = writer->at; int64_t value_12 = (int64_t)(writer->output).length; value_10 = (value_11 > value_12); } if (value_10) { ExecutableProblem* value_13 = &(writer->problem); (*value_13) = ExecutableProblem_Output; } value_1 = start; value_2: ; } int64_t at = value_1; bool value_14 = ((ExecutableProblem)(writer->problem)) == ExecutableProblem_None; if (value_14) { int64_t value_15 = (int64_t)(writer->output).length; value_14 = (value_15 != 0LL); } if (value_14) { uint8_t* value_16 = &(((uint8_t *)writer->output.data)[SliceIndex(writer->output, (int64_t)at)]); (*value_16) = value; } } static void compiler_bundle_ExecutableU32(ExecutableWriter* writer, uint32_t value) { for (int64_t byte = 0LL; byte <= 3LL; byte++) { ExecutableWriter* value_0 = writer; uint8_t value_1 = (uint8_t)((ZiShrU32(value, (uint64_t)((uint32_t)(((int64_t)((uint64_t)byte * UINT64_C(8))))), 32))); { ExecutableWriter* writer = value_0; uint8_t value = value_1; ExecutableWriter* value_3 = writer; int64_t value_4 = 0; { ExecutableWriter* writer = value_3; int64_t count = 1LL; if (((ExecutableProblem)(writer->problem)) != ExecutableProblem_None) { value_4 = 0LL; goto value_5; } bool value_6 = count < 0LL; if (!value_6) { int64_t value_7 = writer->at; value_6 = (value_7 < 0LL); } bool value_8 = value_6; if (!value_8) { int64_t value_9 = (int64_t)(UINT64_C(268435456) - (uint64_t)(writer->at)); value_8 = (count > value_9); } if (value_8) { ExecutableProblem* value_10 = &(writer->problem); (*value_10) = ExecutableProblem_Limit; value_4 = 0LL; goto value_5; } int64_t start = writer->at; int64_t* value_11 = &(writer->at); (*value_11) = (int64_t)((uint64_t)((*value_11)) + (uint64_t)count); int64_t value_12 = (int64_t)(writer->output).length; bool value_13 = value_12 != 0LL; if (value_13) { int64_t value_14 = writer->at; int64_t value_15 = (int64_t)(writer->output).length; value_13 = (value_14 > value_15); } if (value_13) { ExecutableProblem* value_16 = &(writer->problem); (*value_16) = ExecutableProblem_Output; } value_4 = start; value_5: ; } int64_t at = value_4; bool value_17 = ((ExecutableProblem)(writer->problem)) == ExecutableProblem_None; if (value_17) { int64_t value_18 = (int64_t)(writer->output).length; value_17 = (value_18 != 0LL); } if (value_17) { uint8_t* value_19 = &(((uint8_t *)writer->output.data)[SliceIndex(writer->output, (int64_t)at)]); (*value_19) = value; } } } } static void compiler_bundle_ExecutableU64(ExecutableWriter* writer, uint64_t value) { for (int64_t byte = 0LL; byte <= 7LL; byte++) { ExecutableWriter* value_0 = writer; uint8_t value_1 = (uint8_t)((ZiShrU64(value, (uint64_t)((uint64_t)(((int64_t)((uint64_t)byte * UINT64_C(8))))), 64))); { ExecutableWriter* writer = value_0; uint8_t value = value_1; ExecutableWriter* value_3 = writer; int64_t value_4 = 0; { ExecutableWriter* writer = value_3; int64_t count = 1LL; if (((ExecutableProblem)(writer->problem)) != ExecutableProblem_None) { value_4 = 0LL; goto value_5; } bool value_6 = count < 0LL; if (!value_6) { int64_t value_7 = writer->at; value_6 = (value_7 < 0LL); } bool value_8 = value_6; if (!value_8) { int64_t value_9 = (int64_t)(UINT64_C(268435456) - (uint64_t)(writer->at)); value_8 = (count > value_9); } if (value_8) { ExecutableProblem* value_10 = &(writer->problem); (*value_10) = ExecutableProblem_Limit; value_4 = 0LL; goto value_5; } int64_t start = writer->at; int64_t* value_11 = &(writer->at); (*value_11) = (int64_t)((uint64_t)((*value_11)) + (uint64_t)count); int64_t value_12 = (int64_t)(writer->output).length; bool value_13 = value_12 != 0LL; if (value_13) { int64_t value_14 = writer->at; int64_t value_15 = (int64_t)(writer->output).length; value_13 = (value_14 > value_15); } if (value_13) { ExecutableProblem* value_16 = &(writer->problem); (*value_16) = ExecutableProblem_Output; } value_4 = start; value_5: ; } int64_t at = value_4; bool value_17 = ((ExecutableProblem)(writer->problem)) == ExecutableProblem_None; if (value_17) { int64_t value_18 = (int64_t)(writer->output).length; value_17 = (value_18 != 0LL); } if (value_17) { uint8_t* value_19 = &(((uint8_t *)writer->output.data)[SliceIndex(writer->output, (int64_t)at)]); (*value_19) = value; } } } } static void compiler_bundle_ExecutableRaw(ExecutableWriter* writer, String value) { for (int64_t it_index = 0LL; it_index < (int64_t)(value).length; it_index++) { uint8_t byte = (uint8_t)value.data[it_index]; ExecutableWriter* value_0 = writer; { ExecutableWriter* writer = value_0; uint8_t value = byte; ExecutableWriter* value_2 = writer; int64_t value_3 = 0; { ExecutableWriter* writer = value_2; int64_t count = 1LL; if (((ExecutableProblem)(writer->problem)) != ExecutableProblem_None) { value_3 = 0LL; goto value_4; } bool value_5 = count < 0LL; if (!value_5) { int64_t value_6 = writer->at; value_5 = (value_6 < 0LL); } bool value_7 = value_5; if (!value_7) { int64_t value_8 = (int64_t)(UINT64_C(268435456) - (uint64_t)(writer->at)); value_7 = (count > value_8); } if (value_7) { ExecutableProblem* value_9 = &(writer->problem); (*value_9) = ExecutableProblem_Limit; value_3 = 0LL; goto value_4; } int64_t start = writer->at; int64_t* value_10 = &(writer->at); (*value_10) = (int64_t)((uint64_t)((*value_10)) + (uint64_t)count); int64_t value_11 = (int64_t)(writer->output).length; bool value_12 = value_11 != 0LL; if (value_12) { int64_t value_13 = writer->at; int64_t value_14 = (int64_t)(writer->output).length; value_12 = (value_13 > value_14); } if (value_12) { ExecutableProblem* value_15 = &(writer->problem); (*value_15) = ExecutableProblem_Output; } value_3 = start; value_4: ; } int64_t at = value_3; bool value_16 = ((ExecutableProblem)(writer->problem)) == ExecutableProblem_None; if (value_16) { int64_t value_17 = (int64_t)(writer->output).length; value_16 = (value_17 != 0LL); } if (value_16) { uint8_t* value_18 = &(((uint8_t *)writer->output.data)[SliceIndex(writer->output, (int64_t)at)]); (*value_18) = value; } } } } static void compiler_bundle_ExecutablePad(ExecutableWriter* writer, int64_t alignment) { int64_t value_0 = writer->at; int64_t value_1 = alignment; int64_t value_2 = 0; { int64_t value = value_0; int64_t alignment = value_1; bool value_4 = value < 0LL || alignment < 1LL || ((int64_t)((uint64_t)alignment & ((uint64_t)alignment - UINT64_C(1)))) != 0LL; if (value_4 || value > (int64_t)(UINT64_C(268435456) - ((uint64_t)alignment - UINT64_C(1)))) { value_2 = -1LL; goto value_3; } value_2 = (int64_t)((uint64_t)(((uint64_t)value + (uint64_t)alignment) - UINT64_C(1)) & (uint64_t)(((int64_t)~((uint64_t)alignment - UINT64_C(1))))); value_3: ; } int64_t end = value_2; if (end < 0LL) { ExecutableProblem* value_5 = &(writer->problem); (*value_5) = ExecutableProblem_Limit; return; } while (true) { int64_t value_6 = writer->at; if (!(value_6 < end)) { break; } ExecutableWriter* value_7 = writer; { ExecutableWriter* writer = value_7; uint8_t value = 0; ExecutableWriter* value_9 = writer; int64_t value_10 = 0; { ExecutableWriter* writer = value_9; int64_t count = 1LL; if (((ExecutableProblem)(writer->problem)) != ExecutableProblem_None) { value_10 = 0LL; goto value_11; } bool value_12 = count < 0LL; if (!value_12) { int64_t value_13 = writer->at; value_12 = (value_13 < 0LL); } bool value_14 = value_12; if (!value_14) { int64_t value_15 = (int64_t)(UINT64_C(268435456) - (uint64_t)(writer->at)); value_14 = (count > value_15); } if (value_14) { ExecutableProblem* value_16 = &(writer->problem); (*value_16) = ExecutableProblem_Limit; value_10 = 0LL; goto value_11; } int64_t start = writer->at; int64_t* value_17 = &(writer->at); (*value_17) = (int64_t)((uint64_t)((*value_17)) + (uint64_t)count); int64_t value_18 = (int64_t)(writer->output).length; bool value_19 = value_18 != 0LL; if (value_19) { int64_t value_20 = writer->at; int64_t value_21 = (int64_t)(writer->output).length; value_19 = (value_20 > value_21); } if (value_19) { ExecutableProblem* value_22 = &(writer->problem); (*value_22) = ExecutableProblem_Output; } value_10 = start; value_11: ; } int64_t at = value_10; bool value_23 = ((ExecutableProblem)(writer->problem)) == ExecutableProblem_None; if (value_23) { int64_t value_24 = (int64_t)(writer->output).length; value_23 = (value_24 != 0LL); } if (value_23) { uint8_t* value_25 = &(((uint8_t *)writer->output.data)[SliceIndex(writer->output, (int64_t)at)]); (*value_25) = value; } } } } static bool compiler_bundle_ExecutableValidAttachment(ExecutableAttachment attachment, uint32_t previous) { return attachment.kind >= 2147483904 && attachment.kind > previous && (int64_t)(attachment.data).length >= 0LL; } static int64_t compiler_bundle_ExecutableSectionCount(ExecutableHeader header) { int64_t count = 3LL; if ((int64_t)(header.debug).length != 0LL) { count = (int64_t)((uint64_t)count + UINT64_C(1)); } if ((int64_t)(header.source).length != 0LL) { count = (int64_t)((uint64_t)count + UINT64_C(1)); } if ((int64_t)(header.interface).length != 0LL) { count = (int64_t)((uint64_t)count + UINT64_C(1)); } if ((int64_t)(header.attachments).length > (int64_t)(UINT64_C(1024) - (uint64_t)count)) { return -1LL; } return (int64_t)((uint64_t)count + (uint64_t)((int64_t)(header.attachments).length)); } static int64_t compiler_bundle_ExecutableSectionSize(ExecutableHeader header, int64_t index, int64_t asset_size) { if (index == 0LL) { return 128LL; } if (index == 1LL) { return (int64_t)(header.program).length; } if (index == 2LL) { return asset_size; } index = (int64_t)((uint64_t)index - UINT64_C(3)); if ((int64_t)(header.debug).length != 0LL) { if (index == 0LL) { return (int64_t)(header.debug).length; } index = (int64_t)((uint64_t)index - UINT64_C(1)); } if ((int64_t)(header.source).length != 0LL) { if (index == 0LL) { return (int64_t)(header.source).length; } index = (int64_t)((uint64_t)index - UINT64_C(1)); } if ((int64_t)(header.interface).length != 0LL) { if (index == 0LL) { return (int64_t)(header.interface).length; } index = (int64_t)((uint64_t)index - UINT64_C(1)); } ExecutableAttachment value_0 = ((ExecutableAttachment *)header.attachments.data)[SliceIndex(header.attachments, (int64_t)index)]; return (int64_t)(value_0.data).length; } static uint32_t compiler_bundle_ExecutableSectionKind(ExecutableHeader header, int64_t index) { if (index == 0LL) { return 1; } if (index == 1LL) { return 2; } if (index == 2LL) { return 3; } index = (int64_t)((uint64_t)index - UINT64_C(3)); if ((int64_t)(header.debug).length != 0LL) { if (index == 0LL) { return 2147483649; } index = (int64_t)((uint64_t)index - UINT64_C(1)); } if ((int64_t)(header.source).length != 0LL) { if (index == 0LL) { return 2147483650; } index = (int64_t)((uint64_t)index - UINT64_C(1)); } if ((int64_t)(header.interface).length != 0LL) { if (index == 0LL) { return 2147483651; } index = (int64_t)((uint64_t)index - UINT64_C(1)); } ExecutableAttachment value_0 = ((ExecutableAttachment *)header.attachments.data)[SliceIndex(header.attachments, (int64_t)index)]; return value_0.kind; } static void compiler_bundle_ExecutablePayload(ExecutableWriter* writer, ExecutableHeader header, int64_t index, int64_t asset_size) { if (index == 0LL) { compiler_bundle_ExecutableU32(writer, 1); compiler_bundle_ExecutableU32(writer, 3); compiler_bundle_ExecutableU32(writer, (uint32_t)(header.entry)); compiler_bundle_ExecutableU32(writer, (uint32_t)(header.capabilities)); compiler_bundle_ExecutableU32(writer, 0); compiler_bundle_ExecutableU32(writer, (uint32_t)((int64_t)(header.assets).length)); compiler_bundle_ExecutableU32(writer, 0); compiler_bundle_ExecutableU32(writer, 0); compiler_bundle_ExecutableRaw(writer, header.program_identity); compiler_bundle_ExecutableRaw(writer, header.assets_identity); compiler_bundle_ExecutableRaw(writer, header.content_identity); return; } if (index == 1LL) { compiler_bundle_ExecutableRaw(writer, header.program); return; } if (index == 2LL) { int64_t value_0 = (int64_t)(writer->output).length; if (value_0 == 0LL) { int64_t* value_1 = &(writer->at); (*value_1) = (int64_t)((uint64_t)((*value_1)) + (uint64_t)asset_size); return; } Slice value_2 = writer->output; Slice value_3 = SliceRange(value_2, (int64_t)writer->at, (int64_t)((int64_t)((uint64_t)(writer->at) + (uint64_t)asset_size)), sizeof(uint8_t)); int64_t encoded = compiler_bundle_EncodeAssets(value_3, header.assets); if (encoded != asset_size) { ExecutableProblem* value_4 = &(writer->problem); (*value_4) = ExecutableProblem_Assets; return; } int64_t* value_5 = &(writer->at); (*value_5) = (int64_t)((uint64_t)((*value_5)) + (uint64_t)asset_size); return; } index = (int64_t)((uint64_t)index - UINT64_C(3)); if ((int64_t)(header.debug).length != 0LL) { if (index == 0LL) { compiler_bundle_ExecutableRaw(writer, header.debug); return; } index = (int64_t)((uint64_t)index - UINT64_C(1)); } if ((int64_t)(header.source).length != 0LL) { if (index == 0LL) { compiler_bundle_ExecutableRaw(writer, header.source); return; } index = (int64_t)((uint64_t)index - UINT64_C(1)); } if ((int64_t)(header.interface).length != 0LL) { if (index == 0LL) { compiler_bundle_ExecutableRaw(writer, header.interface); return; } index = (int64_t)((uint64_t)index - UINT64_C(1)); } ExecutableAttachment value_6 = ((ExecutableAttachment *)header.attachments.data)[SliceIndex(header.attachments, (int64_t)index)]; compiler_bundle_ExecutableRaw(writer, value_6.data); } static ExecutableProblem compiler_bundle_ExecutableValidateHeader(ExecutableHeader header) { if (header.entry < 0 || (int64_t)(header.program).length < 1LL || (int64_t)(header.program).length > 268435456LL) { return ExecutableProblem_Entry; } if (header.capabilities < 0LL || header.capabilities > 4096LL) { return ExecutableProblem_Capabilities; } bool value_0 = (int64_t)(header.program_identity).length != 32LL || (int64_t)(header.assets_identity).length != 32LL; if (value_0 || (int64_t)(header.content_identity).length != 32LL) { return ExecutableProblem_Identity; } int64_t count = compiler_bundle_ExecutableSectionCount(header); if (count < 3LL || count > 1024LL) { return ExecutableProblem_Limit; } uint32_t previous = 2147483651; { Slice value_1 = header.attachments; Slice loop_view_14 = value_1; int64_t loop_count_14 = (int64_t)(loop_view_14).length; int64_t loop_cursor_14 = 0LL; while (loop_cursor_14 < loop_count_14) { int64_t loop_index_14 = loop_cursor_14; ExecutableAttachment attachment = ((ExecutableAttachment *)loop_view_14.data)[loop_index_14]; ExecutableAttachment value_2 = attachment; uint32_t value_3 = previous; bool value_4 = false; { ExecutableAttachment attachment = value_2; uint32_t previous = value_3; value_4 = attachment.kind >= 2147483904 && attachment.kind > previous && (int64_t)(attachment.data).length >= 0LL; } if (!value_4) { return ExecutableProblem_Section; } previous = attachment.kind; loop_cursor_14 = (int64_t)((uint64_t)loop_cursor_14 + UINT64_C(1)); } } Slice empty = {0}; int64_t asset_size = compiler_bundle_EncodeAssets(empty, header.assets); if (asset_size < 0LL) { return ExecutableProblem_Assets; } return ExecutableProblem_None; } static int64_t compiler_bundle_ExecutableNeed(ExecutableCursor* cursor, int64_t count) { if (((ExecutableProblem)(cursor->problem)) != ExecutableProblem_None) { return 0LL; } bool value_0 = count < 0LL; if (!value_0) { int64_t value_1 = cursor->at; value_0 = (value_1 < 0LL); } bool value_2 = value_0; if (!value_2) { int64_t value_3 = cursor->at; int64_t value_4 = cursor->end; value_2 = (value_3 > value_4); } bool value_5 = value_2; if (!value_5) { int64_t value_6 = (int64_t)((uint64_t)(cursor->end) - (uint64_t)(cursor->at)); value_5 = (count > value_6); } if (value_5) { ExecutableProblem* value_7 = &(cursor->problem); (*value_7) = ExecutableProblem_Truncated; return 0LL; } int64_t start = cursor->at; int64_t* value_8 = &(cursor->at); (*value_8) = (int64_t)((uint64_t)((*value_8)) + (uint64_t)count); return start; } static uint8_t compiler_bundle_ExecutableTakeU8(ExecutableCursor* cursor) { ExecutableCursor* value_0 = cursor; int64_t value_1 = 0; { ExecutableCursor* cursor = value_0; int64_t count = 1LL; if (((ExecutableProblem)(cursor->problem)) != ExecutableProblem_None) { value_1 = 0LL; goto value_2; } bool value_3 = count < 0LL; if (!value_3) { int64_t value_4 = cursor->at; value_3 = (value_4 < 0LL); } bool value_5 = value_3; if (!value_5) { int64_t value_6 = cursor->at; int64_t value_7 = cursor->end; value_5 = (value_6 > value_7); } bool value_8 = value_5; if (!value_8) { int64_t value_9 = (int64_t)((uint64_t)(cursor->end) - (uint64_t)(cursor->at)); value_8 = (count > value_9); } if (value_8) { ExecutableProblem* value_10 = &(cursor->problem); (*value_10) = ExecutableProblem_Truncated; value_1 = 0LL; goto value_2; } int64_t start = cursor->at; int64_t* value_11 = &(cursor->at); (*value_11) = (int64_t)((uint64_t)((*value_11)) + (uint64_t)count); value_1 = start; value_2: ; } int64_t at = value_1; if (((ExecutableProblem)(cursor->problem)) != ExecutableProblem_None) { return 0; } return (uint8_t)ZIRAN_INDEX(cursor->input.data, cursor->input.length, at); } static uint32_t compiler_bundle_ExecutableTakeU32(ExecutableCursor* cursor) { uint32_t value = 0; for (int64_t byte = 0LL; byte <= 3LL; byte++) { uint32_t value_0 = value; ExecutableCursor* value_1 = cursor; uint8_t value_2 = 0; { ExecutableCursor* cursor = value_1; ExecutableCursor* value_4 = cursor; int64_t value_5 = 0; { ExecutableCursor* cursor = value_4; int64_t count = 1LL; if (((ExecutableProblem)(cursor->problem)) != ExecutableProblem_None) { value_5 = 0LL; goto value_6; } bool value_7 = count < 0LL; if (!value_7) { int64_t value_8 = cursor->at; value_7 = (value_8 < 0LL); } bool value_9 = value_7; if (!value_9) { int64_t value_10 = cursor->at; int64_t value_11 = cursor->end; value_9 = (value_10 > value_11); } bool value_12 = value_9; if (!value_12) { int64_t value_13 = (int64_t)((uint64_t)(cursor->end) - (uint64_t)(cursor->at)); value_12 = (count > value_13); } if (value_12) { ExecutableProblem* value_14 = &(cursor->problem); (*value_14) = ExecutableProblem_Truncated; value_5 = 0LL; goto value_6; } int64_t start = cursor->at; int64_t* value_15 = &(cursor->at); (*value_15) = (int64_t)((uint64_t)((*value_15)) + (uint64_t)count); value_5 = start; value_6: ; } int64_t at = value_5; if (((ExecutableProblem)(cursor->problem)) != ExecutableProblem_None) { value_2 = 0; goto value_3; } value_2 = (uint8_t)ZIRAN_INDEX(cursor->input.data, cursor->input.length, at); value_3: ; } uint32_t value_16 = ZiShl32((uint32_t)(value_2), (uint64_t)((uint32_t)(((int64_t)((uint64_t)byte * UINT64_C(8))))), 32); value = (uint32_t)((uint32_t)value_0 | (uint32_t)value_16); } return value; } static uint64_t compiler_bundle_ExecutableTakeU64(ExecutableCursor* cursor) { uint64_t value = 0; for (int64_t byte = 0LL; byte <= 7LL; byte++) { uint64_t value_0 = value; ExecutableCursor* value_1 = cursor; uint8_t value_2 = 0; { ExecutableCursor* cursor = value_1; ExecutableCursor* value_4 = cursor; int64_t value_5 = 0; { ExecutableCursor* cursor = value_4; int64_t count = 1LL; if (((ExecutableProblem)(cursor->problem)) != ExecutableProblem_None) { value_5 = 0LL; goto value_6; } bool value_7 = count < 0LL; if (!value_7) { int64_t value_8 = cursor->at; value_7 = (value_8 < 0LL); } bool value_9 = value_7; if (!value_9) { int64_t value_10 = cursor->at; int64_t value_11 = cursor->end; value_9 = (value_10 > value_11); } bool value_12 = value_9; if (!value_12) { int64_t value_13 = (int64_t)((uint64_t)(cursor->end) - (uint64_t)(cursor->at)); value_12 = (count > value_13); } if (value_12) { ExecutableProblem* value_14 = &(cursor->problem); (*value_14) = ExecutableProblem_Truncated; value_5 = 0LL; goto value_6; } int64_t start = cursor->at; int64_t* value_15 = &(cursor->at); (*value_15) = (int64_t)((uint64_t)((*value_15)) + (uint64_t)count); value_5 = start; value_6: ; } int64_t at = value_5; if (((ExecutableProblem)(cursor->problem)) != ExecutableProblem_None) { value_2 = 0; goto value_3; } value_2 = (uint8_t)ZIRAN_INDEX(cursor->input.data, cursor->input.length, at); value_3: ; } uint64_t value_16 = ZiShl64((uint64_t)(value_2), (uint64_t)((uint64_t)(((int64_t)((uint64_t)byte * UINT64_C(8))))), 64); value = (uint64_t)((uint64_t)value_0 | (uint64_t)value_16); } return value; } static bool compiler_bundle_ExecutableZero(String input, int64_t begin, int64_t end) { if (begin < 0LL || end < begin || end > (int64_t)(input).length) { return false; } { int64_t range_first_3 = begin; int64_t range_last_3 = (int64_t)((uint64_t)end - UINT64_C(1)); int64_t index = range_first_3; if (index <= range_last_3) for (;;) { if ((uint8_t)input.data[index] != 0) { return false; } if (index == range_last_3) { break; } index = (int64_t)((uint64_t)index + UINT64_C(1)); } } return true; } static ExecutableLayout compiler_bundle_ExecutableDecodeInternal(String bytes, Slice assets) { ExecutableLayout layout = {0}; if ((int64_t)(bytes).length > 268435456LL) { ExecutableProblem* value_0 = &(layout.problem); (*value_0) = ExecutableProblem_Limit; return layout; } ExecutableCursor cursor = {.input = bytes, .end = (int64_t)(bytes).length}; uint32_t magic = compiler_bundle_ExecutableTakeU32(&(cursor)); uint32_t version = compiler_bundle_ExecutableTakeU32(&(cursor)); uint32_t* value_1 = &(layout.found_version); (*value_1) = version; if (((ExecutableProblem)(cursor.problem)) != ExecutableProblem_None) { ExecutableProblem* value_2 = &(layout.problem); (*value_2) = (ExecutableProblem)(cursor.problem); return layout; } if (magic != 22618) { ExecutableProblem* value_3 = &(layout.problem); (*value_3) = ExecutableProblem_Magic; return layout; } if (version == 26 || version == 27) { ExecutableProblem* value_4 = &(layout.problem); (*value_4) = ExecutableProblem_Legacy; return layout; } if (version != 28) { ExecutableProblem* value_5 = &(layout.problem); (*value_5) = ExecutableProblem_Version; return layout; } uint32_t header_size = compiler_bundle_ExecutableTakeU32(&(cursor)); uint32_t directory_entry_size = compiler_bundle_ExecutableTakeU32(&(cursor)); uint64_t* value_6 = &(layout.required_features); (*value_6) = compiler_bundle_ExecutableTakeU64(&(cursor)); uint64_t* value_7 = &(layout.optional_features); (*value_7) = compiler_bundle_ExecutableTakeU64(&(cursor)); uint32_t value_8 = compiler_bundle_ExecutableTakeU32(&(cursor)); int64_t section_count = (int64_t)(value_8); uint32_t flags = compiler_bundle_ExecutableTakeU32(&(cursor)); uint64_t total = compiler_bundle_ExecutableTakeU64(&(cursor)); uint64_t directory = compiler_bundle_ExecutableTakeU64(&(cursor)); uint32_t identity_algorithm = compiler_bundle_ExecutableTakeU32(&(cursor)); uint32_t reserved = compiler_bundle_ExecutableTakeU32(&(cursor)); if (((ExecutableProblem)(cursor.problem)) != ExecutableProblem_None) { ExecutableProblem* value_9 = &(layout.problem); (*value_9) = (ExecutableProblem)(cursor.problem); return layout; } if (header_size != 64 || directory_entry_size != 32 || directory != 64ULL || identity_algorithm != 1 || reserved != 0) { ExecutableProblem* value_10 = &(layout.problem); (*value_10) = ExecutableProblem_Header; return layout; } if (layout.required_features != 1ULL) { ExecutableProblem* value_11 = &(layout.problem); (*value_11) = ExecutableProblem_Feature; return layout; } if (flags != 0) { ExecutableProblem* value_12 = &(layout.problem); (*value_12) = ExecutableProblem_Flags; return layout; } if (section_count < 3LL || section_count > 1024LL) { ExecutableProblem* value_13 = &(layout.problem); (*value_13) = ExecutableProblem_Limit; return layout; } if (total > (uint64_t)((int64_t)(bytes).length)) { ExecutableProblem* value_14 = &(layout.problem); (*value_14) = ExecutableProblem_Truncated; return layout; } if (total < (uint64_t)((int64_t)(bytes).length)) { ExecutableProblem* value_15 = &(layout.problem); (*value_15) = ExecutableProblem_Trailing; return layout; } int64_t directory_end = (int64_t)(UINT64_C(64) + ((uint64_t)section_count * UINT64_C(32))); if (directory_end > (int64_t)(bytes).length) { ExecutableProblem* value_16 = &(layout.problem); (*value_16) = ExecutableProblem_Truncated; return layout; } String manifest = StringView(NULL, 0); String asset_bytes = StringView(NULL, 0); int64_t expected = directory_end; uint32_t previous = 0; bool found_manifest = false; bool found_program = false; bool found_assets = false; { int64_t range_first_75 = 0LL; int64_t range_last_75 = (int64_t)((uint64_t)section_count - UINT64_C(1)); int64_t index = range_first_75; if (index <= range_last_75) for (;;) { uint32_t kind = compiler_bundle_ExecutableTakeU32(&(cursor)); uint32_t section_flags = compiler_bundle_ExecutableTakeU32(&(cursor)); uint64_t offset_raw = compiler_bundle_ExecutableTakeU64(&(cursor)); uint64_t size_raw = compiler_bundle_ExecutableTakeU64(&(cursor)); uint32_t value_17 = compiler_bundle_ExecutableTakeU32(&(cursor)); int64_t alignment = (int64_t)(value_17); uint32_t section_reserved = compiler_bundle_ExecutableTakeU32(&(cursor)); if (((ExecutableProblem)(cursor.problem)) != ExecutableProblem_None) { ExecutableProblem* value_18 = &(layout.problem); (*value_18) = (ExecutableProblem)(cursor.problem); return layout; } if (index > 0LL && kind <= previous) { ExecutableProblem* value_19 = &(layout.problem); (*value_19) = ExecutableProblem_Duplicate; return layout; } previous = kind; if (section_reserved != 0 || ((uint32_t)((uint32_t)section_flags & 4294967294u)) != 0) { ExecutableProblem* value_20 = &(layout.problem); (*value_20) = ExecutableProblem_Flags; return layout; } if (alignment < 1LL || alignment > 4096LL || ((int64_t)((uint64_t)alignment & ((uint64_t)alignment - UINT64_C(1)))) != 0LL) { ExecutableProblem* value_21 = &(layout.problem); (*value_21) = ExecutableProblem_Alignment; return layout; } int64_t value_22 = alignment; int64_t value_23 = 0; { int64_t value = expected; int64_t alignment = value_22; bool value_25 = value < 0LL || alignment < 1LL || ((int64_t)((uint64_t)alignment & ((uint64_t)alignment - UINT64_C(1)))) != 0LL; if (value_25 || value > (int64_t)(UINT64_C(268435456) - ((uint64_t)alignment - UINT64_C(1)))) { value_23 = -1LL; goto value_24; } value_23 = (int64_t)((uint64_t)(((uint64_t)value + (uint64_t)alignment) - UINT64_C(1)) & (uint64_t)(((int64_t)~((uint64_t)alignment - UINT64_C(1))))); value_24: ; } int64_t aligned = value_23; if (aligned < 0LL) { ExecutableProblem* value_26 = &(layout.problem); (*value_26) = ExecutableProblem_Limit; return layout; } if (offset_raw > 9223372036854775807ULL || size_raw > 9223372036854775807ULL) { ExecutableProblem* value_27 = &(layout.problem); (*value_27) = ExecutableProblem_Limit; return layout; } int64_t offset = (int64_t)(offset_raw); int64_t size = (int64_t)(size_raw); if (offset < aligned) { ExecutableProblem* value_28 = &(layout.problem); (*value_28) = ExecutableProblem_Overlap; return layout; } bool value_29 = offset > aligned; if (!value_29) { bool value_30 = compiler_bundle_ExecutableZero(bytes, expected, offset); value_29 = !value_30; } if (value_29) { ExecutableProblem* value_31 = &(layout.problem); (*value_31) = ExecutableProblem_Alignment; return layout; } if (offset > (int64_t)(bytes).length || size > (int64_t)((uint64_t)((int64_t)(bytes).length) - (uint64_t)offset)) { ExecutableProblem* value_32 = &(layout.problem); (*value_32) = ExecutableProblem_Truncated; return layout; } String section = StringRange(bytes, (int64_t)offset, (int64_t)((int64_t)((uint64_t)offset + (uint64_t)size))); bool required = ((uint32_t)((uint32_t)section_flags & 1u)) != 0; if (kind == 1) { if (!required || size != 128LL) { ExecutableProblem* value_33 = &(layout.problem); (*value_33) = ExecutableProblem_Manifest; return layout; } manifest = section; found_manifest = true; } else if (kind == 2) { if (!required || size < 1LL || size > 268435456LL) { ExecutableProblem* value_34 = &(layout.problem); (*value_34) = ExecutableProblem_Section; return layout; } String* value_35 = &(layout.program); (*value_35) = section; found_program = true; } else if (kind == 3) { if (!required) { ExecutableProblem* value_36 = &(layout.problem); (*value_36) = ExecutableProblem_Assets; return layout; } asset_bytes = section; found_assets = true; } else if (kind == 4) { if (required) { ExecutableProblem* value_37 = &(layout.problem); (*value_37) = ExecutableProblem_Variant; return layout; } int64_t* value_38 = &(layout.optional_sections); (*value_38) = (int64_t)((uint64_t)((*value_38)) + UINT64_C(1)); } else if (kind == 2147483649) { if (required) { ExecutableProblem* value_39 = &(layout.problem); (*value_39) = ExecutableProblem_RequiredSection; return layout; } String* value_40 = &(layout.debug); (*value_40) = section; int64_t* value_41 = &(layout.optional_sections); (*value_41) = (int64_t)((uint64_t)((*value_41)) + UINT64_C(1)); } else if (kind == 2147483650) { if (required) { ExecutableProblem* value_42 = &(layout.problem); (*value_42) = ExecutableProblem_RequiredSection; return layout; } String* value_43 = &(layout.source); (*value_43) = section; int64_t* value_44 = &(layout.optional_sections); (*value_44) = (int64_t)((uint64_t)((*value_44)) + UINT64_C(1)); } else if (kind == 2147483651) { if (required) { ExecutableProblem* value_45 = &(layout.problem); (*value_45) = ExecutableProblem_RequiredSection; return layout; } String* value_46 = &(layout.interface); (*value_46) = section; int64_t* value_47 = &(layout.optional_sections); (*value_47) = (int64_t)((uint64_t)((*value_47)) + UINT64_C(1)); } else { if (required) { ExecutableProblem* value_48 = &(layout.problem); (*value_48) = ExecutableProblem_RequiredSection; return layout; } int64_t* value_49 = &(layout.optional_sections); (*value_49) = (int64_t)((uint64_t)((*value_49)) + UINT64_C(1)); } expected = (int64_t)((uint64_t)offset + (uint64_t)size); if (index == range_last_75) { break; } index = (int64_t)((uint64_t)index + UINT64_C(1)); } } if (expected != (int64_t)(bytes).length) { ExecutableProblem* value_50 = &(layout.problem); ExecutableProblem value_51 = (ExecutableProblem)(0); if (expected < (int64_t)(bytes).length) { value_51 = ExecutableProblem_Trailing; } else { value_51 = ExecutableProblem_Truncated; } (*value_50) = value_51; return layout; } if (!found_manifest || !found_program || !found_assets) { ExecutableProblem* value_52 = &(layout.problem); (*value_52) = ExecutableProblem_Section; return layout; } ExecutableCursor manifest_cursor = {.input = manifest, .end = (int64_t)(manifest).length}; uint32_t schema = compiler_bundle_ExecutableTakeU32(&(manifest_cursor)); uint32_t lir_schema = compiler_bundle_ExecutableTakeU32(&(manifest_cursor)); int32_t* value_53 = &(layout.entry); uint32_t value_54 = compiler_bundle_ExecutableTakeU32(&(manifest_cursor)); (*value_53) = (int32_t)(value_54); uint32_t capabilities = compiler_bundle_ExecutableTakeU32(&(manifest_cursor)); uint32_t variants = compiler_bundle_ExecutableTakeU32(&(manifest_cursor)); uint32_t value_55 = compiler_bundle_ExecutableTakeU32(&(manifest_cursor)); int64_t manifest_assets = (int64_t)(value_55); uint32_t proof_status = compiler_bundle_ExecutableTakeU32(&(manifest_cursor)); uint32_t manifest_reserved = compiler_bundle_ExecutableTakeU32(&(manifest_cursor)); bool value_56 = ((ExecutableProblem)(manifest_cursor.problem)) != ExecutableProblem_None || schema != 1 || lir_schema != 3; if (value_56 || proof_status != 0 || manifest_reserved != 0) { ExecutableProblem* value_57 = &(layout.problem); (*value_57) = ExecutableProblem_Manifest; return layout; } if (layout.entry < 0) { ExecutableProblem* value_58 = &(layout.problem); (*value_58) = ExecutableProblem_Entry; return layout; } if (capabilities > 4096) { ExecutableProblem* value_59 = &(layout.problem); (*value_59) = ExecutableProblem_Capabilities; return layout; } int64_t* value_60 = &(layout.capabilities); (*value_60) = (int64_t)(capabilities); if (variants != 0) { ExecutableProblem* value_61 = &(layout.problem); (*value_61) = ExecutableProblem_Variant; return layout; } int64_t identity_at = manifest_cursor.at; String* value_62 = &(layout.program_identity); (*value_62) = StringRange(manifest, (int64_t)identity_at, (int64_t)((int64_t)((uint64_t)identity_at + UINT64_C(32)))); identity_at = (int64_t)((uint64_t)identity_at + UINT64_C(32)); String* value_63 = &(layout.assets_identity); (*value_63) = StringRange(manifest, (int64_t)identity_at, (int64_t)((int64_t)((uint64_t)identity_at + UINT64_C(32)))); identity_at = (int64_t)((uint64_t)identity_at + UINT64_C(32)); String* value_64 = &(layout.content_identity); (*value_64) = StringRange(manifest, (int64_t)identity_at, (int64_t)((int64_t)((uint64_t)identity_at + UINT64_C(32)))); Slice output = {0}; Codec asset_codec = {.input = asset_bytes, .output = output, .at = 0LL, .reading = true, .write = false, .valid = true, .ended = false}; int64_t* value_65 = &(layout.assets); (*value_65) = compiler_bundle_Assets(&(asset_codec), assets); if (!asset_codec.valid || layout.assets != manifest_assets) { ExecutableProblem* value_66 = &(layout.problem); (*value_66) = ExecutableProblem_Assets; return layout; } return layout; } static int64_t compiler_bundle_EncodeAssets(Slice output, Slice assets) { for (int64_t pass = 0LL; pass <= 1LL; pass++) { Codec codec = {.input = StringLiteral(""), .output = output, .at = 0LL, .reading = false, .write = pass == 1LL, .valid = true, .ended = false}; compiler_bundle_Assets(&(codec), assets); if (!codec.valid) { return -1LL; } if (codec.at > (int64_t)(output).length || pass == 1LL) { return codec.at; } } return -1LL; } bool compiler_bundle_AssetName(String name) { if ((int64_t)(name).length == 0LL) { return false; } int64_t begin = 0LL; { int64_t range_first_4 = 0LL; int64_t range_last_4 = (int64_t)(name).length; int64_t at = range_first_4; if (at <= range_last_4) for (;;) { if (at < (int64_t)(name).length && ((uint8_t)ZIRAN_INDEX(name.data, name.length, at) == 92 || (uint8_t)ZIRAN_INDEX(name.data, name.length, at) == 58)) { return false; } if (at == (int64_t)(name).length || (uint8_t)ZIRAN_INDEX(name.data, name.length, at) == 47) { String part = StringRange(name, (int64_t)begin, (int64_t)at); if ((int64_t)(part).length == 0LL || StringEqual(part, StringLiteral(".")) || StringEqual(part, StringLiteral(".."))) { return false; } begin = (int64_t)((uint64_t)at + UINT64_C(1)); } if (at == range_last_4) { break; } at = (int64_t)((uint64_t)at + UINT64_C(1)); } } return true; } ExecutableResult compiler_bundle_ExecutableEncode(Slice output, ExecutableHeader header) { ExecutableProblem problem = (ExecutableProblem)(compiler_bundle_ExecutableValidateHeader(header)); if (problem != ExecutableProblem_None) { return (ExecutableResult){.problem = problem}; } Slice empty = {0}; int64_t asset_size = compiler_bundle_EncodeAssets(empty, header.assets); int64_t section_count = compiler_bundle_ExecutableSectionCount(header); int64_t directory_end = (int64_t)(UINT64_C(64) + ((uint64_t)section_count * UINT64_C(32))); int64_t at = directory_end; { int64_t range_first_9 = 0LL; int64_t range_last_9 = (int64_t)((uint64_t)section_count - UINT64_C(1)); int64_t index = range_first_9; if (index <= range_last_9) for (;;) { int64_t value_0 = 0; { int64_t value = at; int64_t alignment = 8LL; bool value_2 = value < 0LL || alignment < 1LL || ((int64_t)((uint64_t)alignment & ((uint64_t)alignment - UINT64_C(1)))) != 0LL; if (value_2 || value > (int64_t)(UINT64_C(268435456) - ((uint64_t)alignment - UINT64_C(1)))) { value_0 = -1LL; goto value_1; } value_0 = (int64_t)((uint64_t)(((uint64_t)value + (uint64_t)alignment) - UINT64_C(1)) & (uint64_t)(((int64_t)~((uint64_t)alignment - UINT64_C(1))))); value_1: ; } at = value_0; int64_t size = compiler_bundle_ExecutableSectionSize(header, index, asset_size); if (at < 0LL || size < 0LL || size > (int64_t)(UINT64_C(268435456) - (uint64_t)at)) { return (ExecutableResult){.problem = ExecutableProblem_Limit}; } at = (int64_t)((uint64_t)at + (uint64_t)size); if (index == range_last_9) { break; } index = (int64_t)((uint64_t)index + UINT64_C(1)); } } int64_t total = at; if ((int64_t)(output).length == 0LL) { return (ExecutableResult){.size = total}; } if ((int64_t)(output).length < total) { return (ExecutableResult){.problem = ExecutableProblem_Output, .size = total}; } ExecutableWriter writer = {.output = output}; compiler_bundle_ExecutableU32(&(writer), 22618); compiler_bundle_ExecutableU32(&(writer), 28); compiler_bundle_ExecutableU32(&(writer), 64); compiler_bundle_ExecutableU32(&(writer), 32); compiler_bundle_ExecutableU64(&(writer), 1ULL); compiler_bundle_ExecutableU64(&(writer), 0ULL); compiler_bundle_ExecutableU32(&(writer), (uint32_t)(section_count)); compiler_bundle_ExecutableU32(&(writer), 0); compiler_bundle_ExecutableU64(&(writer), (uint64_t)(total)); compiler_bundle_ExecutableU64(&(writer), 64ULL); compiler_bundle_ExecutableU32(&(writer), 1); compiler_bundle_ExecutableU32(&(writer), 0); int64_t payload_at = directory_end; { int64_t range_first_38 = 0LL; int64_t range_last_38 = (int64_t)((uint64_t)section_count - UINT64_C(1)); int64_t index = range_first_38; if (index <= range_last_38) for (;;) { int64_t value_3 = 0; { int64_t value = payload_at; int64_t alignment = 8LL; bool value_5 = value < 0LL || alignment < 1LL || ((int64_t)((uint64_t)alignment & ((uint64_t)alignment - UINT64_C(1)))) != 0LL; if (value_5 || value > (int64_t)(UINT64_C(268435456) - ((uint64_t)alignment - UINT64_C(1)))) { value_3 = -1LL; goto value_4; } value_3 = (int64_t)((uint64_t)(((uint64_t)value + (uint64_t)alignment) - UINT64_C(1)) & (uint64_t)(((int64_t)~((uint64_t)alignment - UINT64_C(1))))); value_4: ; } payload_at = value_3; uint32_t kind = compiler_bundle_ExecutableSectionKind(header, index); uint32_t required = 0; if (index < 3LL) { required = 1; } int64_t size = compiler_bundle_ExecutableSectionSize(header, index, asset_size); compiler_bundle_ExecutableU32(&(writer), kind); compiler_bundle_ExecutableU32(&(writer), required); compiler_bundle_ExecutableU64(&(writer), (uint64_t)(payload_at)); compiler_bundle_ExecutableU64(&(writer), (uint64_t)(size)); compiler_bundle_ExecutableU32(&(writer), 8); compiler_bundle_ExecutableU32(&(writer), 0); payload_at = (int64_t)((uint64_t)payload_at + (uint64_t)size); if (index == range_last_38) { break; } index = (int64_t)((uint64_t)index + UINT64_C(1)); } } { int64_t range_first_54 = 0LL; int64_t range_last_54 = (int64_t)((uint64_t)section_count - UINT64_C(1)); int64_t index = range_first_54; if (index <= range_last_54) for (;;) { compiler_bundle_ExecutablePad(&(writer), 8LL); compiler_bundle_ExecutablePayload(&(writer), header, index, asset_size); if (index == range_last_54) { break; } index = (int64_t)((uint64_t)index + UINT64_C(1)); } } if (((ExecutableProblem)(writer.problem)) != ExecutableProblem_None || writer.at != total) { ExecutableProblem value_6 = (ExecutableProblem)(0); if (((ExecutableProblem)(writer.problem)) != ExecutableProblem_None) { value_6 = (ExecutableProblem)(writer.problem); } else { value_6 = ExecutableProblem_Invalid; } return (ExecutableResult){.problem = value_6, .size = total, .offset = writer.at}; } return (ExecutableResult){.size = total}; } ExecutableLayout compiler_bundle_ExecutableDecode(String bytes, Slice assets) { return compiler_bundle_ExecutableDecodeInternal(bytes, assets); } uint32_t compiler_bundle_ExecutableVersionNumber(void) { return 28; } String compiler_bundle_ExecutableMessage(ExecutableProblem problem) { if (problem == ExecutableProblem_None) { return StringLiteral("none"); } if (problem == ExecutableProblem_Magic) { return StringLiteral("not a ZX executable container"); } if (problem == ExecutableProblem_Legacy) { return StringLiteral("legacy ZX version contains semantic IR; rebuild with zi2zx"); } if (problem == ExecutableProblem_Version) { return StringLiteral("unsupported executable ZX version"); } if (problem == ExecutableProblem_Header) { return StringLiteral("invalid executable ZX header"); } if (problem == ExecutableProblem_Feature) { return StringLiteral("unsupported required executable ZX feature"); } if (problem == ExecutableProblem_Flags) { return StringLiteral("invalid executable ZX flags"); } if (problem == ExecutableProblem_RequiredSection) { return StringLiteral("unsupported required executable ZX section"); } if (problem == ExecutableProblem_Duplicate) { return StringLiteral("duplicate or unordered executable ZX section"); } if (problem == ExecutableProblem_Alignment) { return StringLiteral("invalid executable ZX section alignment or padding"); } if (problem == ExecutableProblem_Overlap) { return StringLiteral("overlapping executable ZX sections"); } if (problem == ExecutableProblem_Truncated) { return StringLiteral("truncated executable ZX container"); } if (problem == ExecutableProblem_Trailing) { return StringLiteral("trailing bytes after executable ZX sections"); } if (problem == ExecutableProblem_Limit) { return StringLiteral("executable ZX container exceeds a bounded limit"); } if (problem == ExecutableProblem_Output) { return StringLiteral("executable ZX output is too short"); } if (problem == ExecutableProblem_Manifest) { return StringLiteral("invalid executable ZX manifest"); } if (problem == ExecutableProblem_Entry) { return StringLiteral("invalid executable ZX entry function ID"); } if (problem == ExecutableProblem_Capabilities) { return StringLiteral("executable ZX requires unsupported host capabilities"); } if (problem == ExecutableProblem_Variant) { return StringLiteral("executable ZX requires unsupported native variants"); } if (problem == ExecutableProblem_Assets) { return StringLiteral("invalid executable ZX asset section"); } if (problem == ExecutableProblem_Identity) { return StringLiteral("executable ZX content identity is invalid"); } return StringLiteral("invalid executable ZX section layout"); }