Path: blob/main/contrib/bearssl/src/codec/pemdec.c
39482 views
/* Automatically generated code; do not modify directly. */12#include <stddef.h>3#include <stdint.h>45typedef struct {6uint32_t *dp;7uint32_t *rp;8const unsigned char *ip;9} t0_context;1011static uint32_t12t0_parse7E_unsigned(const unsigned char **p)13{14uint32_t x;1516x = 0;17for (;;) {18unsigned y;1920y = *(*p) ++;21x = (x << 7) | (uint32_t)(y & 0x7F);22if (y < 0x80) {23return x;24}25}26}2728static int32_t29t0_parse7E_signed(const unsigned char **p)30{31int neg;32uint32_t x;3334neg = ((**p) >> 6) & 1;35x = (uint32_t)-neg;36for (;;) {37unsigned y;3839y = *(*p) ++;40x = (x << 7) | (uint32_t)(y & 0x7F);41if (y < 0x80) {42if (neg) {43return -(int32_t)~x - 1;44} else {45return (int32_t)x;46}47}48}49}5051#define T0_VBYTE(x, n) (unsigned char)((((uint32_t)(x) >> (n)) & 0x7F) | 0x80)52#define T0_FBYTE(x, n) (unsigned char)(((uint32_t)(x) >> (n)) & 0x7F)53#define T0_SBYTE(x) (unsigned char)((((uint32_t)(x) >> 28) + 0xF8) ^ 0xF8)54#define T0_INT1(x) T0_FBYTE(x, 0)55#define T0_INT2(x) T0_VBYTE(x, 7), T0_FBYTE(x, 0)56#define T0_INT3(x) T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)57#define T0_INT4(x) T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)58#define T0_INT5(x) T0_SBYTE(x), T0_VBYTE(x, 21), T0_VBYTE(x, 14), T0_VBYTE(x, 7), T0_FBYTE(x, 0)5960/* static const unsigned char t0_datablock[]; */616263void br_pem_decoder_init_main(void *t0ctx);6465void br_pem_decoder_run(void *t0ctx);66676869#include "inner.h"7071#define CTX ((br_pem_decoder_context *)(void *)((unsigned char *)t0ctx - offsetof(br_pem_decoder_context, cpu)))7273/* see bearssl_pem.h */74void75br_pem_decoder_init(br_pem_decoder_context *ctx)76{77memset(ctx, 0, sizeof *ctx);78ctx->cpu.dp = &ctx->dp_stack[0];79ctx->cpu.rp = &ctx->rp_stack[0];80br_pem_decoder_init_main(&ctx->cpu);81br_pem_decoder_run(&ctx->cpu);82}8384/* see bearssl_pem.h */85size_t86br_pem_decoder_push(br_pem_decoder_context *ctx,87const void *data, size_t len)88{89if (ctx->event) {90return 0;91}92ctx->hbuf = data;93ctx->hlen = len;94br_pem_decoder_run(&ctx->cpu);95return len - ctx->hlen;96}9798/* see bearssl_pem.h */99int100br_pem_decoder_event(br_pem_decoder_context *ctx)101{102int event;103104event = ctx->event;105ctx->event = 0;106return event;107}108109110111static const unsigned char t0_datablock[] = {1120x00, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x42, 0x45, 0x47, 0x49, 0x4E, 0x20,1130x00, 0x2D, 0x2D, 0x2D, 0x2D, 0x45, 0x4E, 0x44, 0x20, 0x00114};115116static const unsigned char t0_codeblock[] = {1170x00, 0x01, 0x00, 0x09, 0x00, 0x00, 0x01, 0x01, 0x07, 0x00, 0x00, 0x01,1180x01, 0x08, 0x00, 0x00, 0x13, 0x13, 0x00, 0x00, 0x01,119T0_INT2(offsetof(br_pem_decoder_context, event)), 0x00, 0x00, 0x01,120T0_INT2(offsetof(br_pem_decoder_context, name)), 0x00, 0x00, 0x05,1210x14, 0x2C, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x03, 0x13, 0x04, 0x76, 0x01,1220x2D, 0x0C, 0x06, 0x05, 0x2E, 0x01, 0x03, 0x2D, 0x00, 0x01, 0x0D, 0x27,1230x05, 0x04, 0x01, 0x03, 0x2D, 0x00, 0x15, 0x2E, 0x01, 0x02, 0x2D, 0x00,1240x01, 0x01, 0x7F, 0x03, 0x00, 0x25, 0x01, 0x00, 0x18, 0x0D, 0x06, 0x03,1250x13, 0x04, 0x3C, 0x01, 0x7F, 0x18, 0x0D, 0x06, 0x13, 0x13, 0x02, 0x00,1260x05, 0x06, 0x2E, 0x01, 0x03, 0x2D, 0x04, 0x03, 0x01, 0x7F, 0x23, 0x01,1270x00, 0x00, 0x04, 0x23, 0x01, 0x01, 0x18, 0x0D, 0x06, 0x09, 0x13, 0x01,1280x00, 0x23, 0x01, 0x00, 0x00, 0x04, 0x14, 0x01, 0x02, 0x18, 0x0D, 0x06,1290x06, 0x13, 0x01, 0x7F, 0x00, 0x04, 0x08, 0x13, 0x01, 0x03, 0x2D, 0x01,1300x00, 0x00, 0x13, 0x01, 0x00, 0x03, 0x00, 0x04, 0xFF, 0x33, 0x01, 0x2C,1310x14, 0x01, 0x2D, 0x0D, 0x06, 0x04, 0x13, 0x01, 0x7F, 0x00, 0x14, 0x31,1320x06, 0x02, 0x13, 0x29, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x04, 0x13, 0x01,1330x02, 0x00, 0x16, 0x14, 0x1D, 0x06, 0x05, 0x13, 0x2E, 0x01, 0x03, 0x00,1340x03, 0x00, 0x29, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x04, 0x13, 0x01, 0x03,1350x00, 0x16, 0x14, 0x1D, 0x06, 0x05, 0x13, 0x2E, 0x01, 0x03, 0x00, 0x02,1360x00, 0x01, 0x06, 0x0A, 0x07, 0x03, 0x00, 0x29, 0x14, 0x01, 0x0A, 0x0D,1370x06, 0x04, 0x13, 0x01, 0x03, 0x00, 0x14, 0x01, 0x3D, 0x0D, 0x06, 0x2E,1380x13, 0x29, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x04, 0x13, 0x01, 0x03, 0x00,1390x2F, 0x05, 0x04, 0x13, 0x01, 0x03, 0x00, 0x01, 0x3D, 0x0C, 0x06, 0x03,1400x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x0F, 0x10, 0x06, 0x03, 0x01, 0x03,1410x00, 0x02, 0x00, 0x01, 0x04, 0x0F, 0x1C, 0x01, 0x01, 0x00, 0x16, 0x14,1420x1D, 0x06, 0x05, 0x13, 0x2E, 0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x06,1430x0A, 0x07, 0x03, 0x00, 0x29, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x04, 0x13,1440x01, 0x03, 0x00, 0x14, 0x01, 0x3D, 0x0D, 0x06, 0x20, 0x13, 0x2F, 0x05,1450x03, 0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x03, 0x10, 0x06, 0x03, 0x01,1460x03, 0x00, 0x02, 0x00, 0x01, 0x0A, 0x0F, 0x1C, 0x02, 0x00, 0x01, 0x02,1470x0F, 0x1C, 0x01, 0x01, 0x00, 0x16, 0x14, 0x1D, 0x06, 0x05, 0x13, 0x2E,1480x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x06, 0x0A, 0x07, 0x03, 0x00, 0x02,1490x00, 0x01, 0x10, 0x0F, 0x1C, 0x02, 0x00, 0x01, 0x08, 0x0F, 0x1C, 0x02,1500x00, 0x1C, 0x01, 0x00, 0x00, 0x00, 0x28, 0x01, 0x01, 0x2D, 0x24, 0x06,1510x02, 0x04, 0x7B, 0x04, 0x75, 0x00, 0x14, 0x12, 0x2A, 0x14, 0x05, 0x04,1520x20, 0x01, 0x7F, 0x00, 0x2C, 0x2A, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x05,1530x13, 0x20, 0x01, 0x00, 0x00, 0x0D, 0x05, 0x05, 0x13, 0x2E, 0x01, 0x00,1540x00, 0x1E, 0x04, 0x5E, 0x00, 0x01, 0x01, 0x27, 0x06, 0x0B, 0x22, 0x01,1550x80, 0x7F, 0x2B, 0x14, 0x06, 0x02, 0x30, 0x00, 0x13, 0x04, 0x6E, 0x00,1560x2C, 0x14, 0x31, 0x05, 0x01, 0x00, 0x13, 0x04, 0x77, 0x00, 0x14, 0x14,1570x01, 0x80, 0x61, 0x0E, 0x1B, 0x01, 0x80, 0x7A, 0x0B, 0x10, 0x06, 0x03,1580x01, 0x20, 0x08, 0x00, 0x01, 0x14, 0x03, 0x00, 0x1B, 0x18, 0x05, 0x05,1590x20, 0x2E, 0x01, 0x00, 0x00, 0x2C, 0x14, 0x01, 0x0A, 0x0D, 0x06, 0x06,1600x20, 0x02, 0x00, 0x1B, 0x08, 0x00, 0x14, 0x01, 0x0D, 0x0D, 0x06, 0x03,1610x13, 0x04, 0x03, 0x2A, 0x18, 0x1A, 0x1E, 0x1B, 0x1F, 0x1B, 0x04, 0x59,1620x00, 0x19, 0x14, 0x1D, 0x05, 0x01, 0x00, 0x13, 0x11, 0x04, 0x76, 0x00,1630x21, 0x1A, 0x11, 0x00, 0x00, 0x2C, 0x01, 0x0A, 0x0C, 0x06, 0x02, 0x04,1640x78, 0x00, 0x01, 0x01, 0x7F, 0x03, 0x00, 0x2C, 0x14, 0x01, 0x0A, 0x0C,1650x06, 0x09, 0x31, 0x05, 0x04, 0x01, 0x00, 0x03, 0x00, 0x04, 0x70, 0x13,1660x02, 0x00, 0x00, 0x00, 0x14, 0x06, 0x14, 0x1F, 0x14, 0x22, 0x07, 0x17,1670x01, 0x2D, 0x0C, 0x06, 0x08, 0x22, 0x07, 0x1E, 0x01, 0x00, 0x1B, 0x1A,1680x00, 0x04, 0x69, 0x22, 0x1A, 0x00, 0x00, 0x14, 0x01, 0x0A, 0x0C, 0x1B,1690x01, 0x20, 0x0B, 0x10, 0x00170};171172static const uint16_t t0_caddr[] = {1730,1745,17510,17615,17719,17824,17929,18067,181149,182384,183396,184431,185450,186460,187479,188523,189534,190539,191549,192574,193601194};195196#define T0_INTERPRETED 29197198#define T0_ENTER(ip, rp, slot) do { \199const unsigned char *t0_newip; \200uint32_t t0_lnum; \201t0_newip = &t0_codeblock[t0_caddr[(slot) - T0_INTERPRETED]]; \202t0_lnum = t0_parse7E_unsigned(&t0_newip); \203(rp) += t0_lnum; \204*((rp) ++) = (uint32_t)((ip) - &t0_codeblock[0]) + (t0_lnum << 16); \205(ip) = t0_newip; \206} while (0)207208#define T0_DEFENTRY(name, slot) \209void \210name(void *ctx) \211{ \212t0_context *t0ctx = ctx; \213t0ctx->ip = &t0_codeblock[0]; \214T0_ENTER(t0ctx->ip, t0ctx->rp, slot); \215}216217T0_DEFENTRY(br_pem_decoder_init_main, 38)218219#define T0_NEXT(t0ipp) (*(*(t0ipp)) ++)220221void222br_pem_decoder_run(void *t0ctx)223{224uint32_t *dp, *rp;225const unsigned char *ip;226227#define T0_LOCAL(x) (*(rp - 2 - (x)))228#define T0_POP() (*-- dp)229#define T0_POPi() (*(int32_t *)(-- dp))230#define T0_PEEK(x) (*(dp - 1 - (x)))231#define T0_PEEKi(x) (*(int32_t *)(dp - 1 - (x)))232#define T0_PUSH(v) do { *dp = (v); dp ++; } while (0)233#define T0_PUSHi(v) do { *(int32_t *)dp = (v); dp ++; } while (0)234#define T0_RPOP() (*-- rp)235#define T0_RPOPi() (*(int32_t *)(-- rp))236#define T0_RPUSH(v) do { *rp = (v); rp ++; } while (0)237#define T0_RPUSHi(v) do { *(int32_t *)rp = (v); rp ++; } while (0)238#define T0_ROLL(x) do { \239size_t t0len = (size_t)(x); \240uint32_t t0tmp = *(dp - 1 - t0len); \241memmove(dp - t0len - 1, dp - t0len, t0len * sizeof *dp); \242*(dp - 1) = t0tmp; \243} while (0)244#define T0_SWAP() do { \245uint32_t t0tmp = *(dp - 2); \246*(dp - 2) = *(dp - 1); \247*(dp - 1) = t0tmp; \248} while (0)249#define T0_ROT() do { \250uint32_t t0tmp = *(dp - 3); \251*(dp - 3) = *(dp - 2); \252*(dp - 2) = *(dp - 1); \253*(dp - 1) = t0tmp; \254} while (0)255#define T0_NROT() do { \256uint32_t t0tmp = *(dp - 1); \257*(dp - 1) = *(dp - 2); \258*(dp - 2) = *(dp - 3); \259*(dp - 3) = t0tmp; \260} while (0)261#define T0_PICK(x) do { \262uint32_t t0depth = (x); \263T0_PUSH(T0_PEEK(t0depth)); \264} while (0)265#define T0_CO() do { \266goto t0_exit; \267} while (0)268#define T0_RET() goto t0_next269270dp = ((t0_context *)t0ctx)->dp;271rp = ((t0_context *)t0ctx)->rp;272ip = ((t0_context *)t0ctx)->ip;273goto t0_next;274for (;;) {275uint32_t t0x;276277t0_next:278t0x = T0_NEXT(&ip);279if (t0x < T0_INTERPRETED) {280switch (t0x) {281int32_t t0off;282283case 0: /* ret */284t0x = T0_RPOP();285rp -= (t0x >> 16);286t0x &= 0xFFFF;287if (t0x == 0) {288ip = NULL;289goto t0_exit;290}291ip = &t0_codeblock[t0x];292break;293case 1: /* literal constant */294T0_PUSHi(t0_parse7E_signed(&ip));295break;296case 2: /* read local */297T0_PUSH(T0_LOCAL(t0_parse7E_unsigned(&ip)));298break;299case 3: /* write local */300T0_LOCAL(t0_parse7E_unsigned(&ip)) = T0_POP();301break;302case 4: /* jump */303t0off = t0_parse7E_signed(&ip);304ip += t0off;305break;306case 5: /* jump if */307t0off = t0_parse7E_signed(&ip);308if (T0_POP()) {309ip += t0off;310}311break;312case 6: /* jump if not */313t0off = t0_parse7E_signed(&ip);314if (!T0_POP()) {315ip += t0off;316}317break;318case 7: {319/* + */320321uint32_t b = T0_POP();322uint32_t a = T0_POP();323T0_PUSH(a + b);324325}326break;327case 8: {328/* - */329330uint32_t b = T0_POP();331uint32_t a = T0_POP();332T0_PUSH(a - b);333334}335break;336case 9: {337/* < */338339int32_t b = T0_POPi();340int32_t a = T0_POPi();341T0_PUSH(-(uint32_t)(a < b));342343}344break;345case 10: {346/* << */347348int c = (int)T0_POPi();349uint32_t x = T0_POP();350T0_PUSH(x << c);351352}353break;354case 11: {355/* <= */356357int32_t b = T0_POPi();358int32_t a = T0_POPi();359T0_PUSH(-(uint32_t)(a <= b));360361}362break;363case 12: {364/* <> */365366uint32_t b = T0_POP();367uint32_t a = T0_POP();368T0_PUSH(-(uint32_t)(a != b));369370}371break;372case 13: {373/* = */374375uint32_t b = T0_POP();376uint32_t a = T0_POP();377T0_PUSH(-(uint32_t)(a == b));378379}380break;381case 14: {382/* >= */383384int32_t b = T0_POPi();385int32_t a = T0_POPi();386T0_PUSH(-(uint32_t)(a >= b));387388}389break;390case 15: {391/* >> */392393int c = (int)T0_POPi();394int32_t x = T0_POPi();395T0_PUSHi(x >> c);396397}398break;399case 16: {400/* and */401402uint32_t b = T0_POP();403uint32_t a = T0_POP();404T0_PUSH(a & b);405406}407break;408case 17: {409/* co */410T0_CO();411}412break;413case 18: {414/* data-get8 */415416size_t addr = T0_POP();417T0_PUSH(t0_datablock[addr]);418419}420break;421case 19: {422/* drop */423(void)T0_POP();424}425break;426case 20: {427/* dup */428T0_PUSH(T0_PEEK(0));429}430break;431case 21: {432/* flush-buf */433434if (CTX->ptr > 0) {435if (CTX->dest) {436CTX->dest(CTX->dest_ctx, CTX->buf, CTX->ptr);437}438CTX->ptr = 0;439}440441}442break;443case 22: {444/* from-base64 */445446uint32_t c = T0_POP();447uint32_t p, q, r, z;448p = c - 0x41;449q = c - 0x61;450r = c - 0x30;451452z = ((p + 2) & -LT(p, 26))453| ((q + 28) & -LT(q, 26))454| ((r + 54) & -LT(r, 10))455| (64 & -EQ(c, 0x2B))456| (65 & -EQ(c, 0x2F))457| EQ(c, 0x3D);458T0_PUSHi((int32_t)z - 2);459460}461break;462case 23: {463/* get8 */464465size_t addr = T0_POP();466T0_PUSH(*((unsigned char *)CTX + addr));467468}469break;470case 24: {471/* over */472T0_PUSH(T0_PEEK(1));473}474break;475case 25: {476/* read8-native */477478if (CTX->hlen > 0) {479T0_PUSH(*CTX->hbuf ++);480CTX->hlen --;481} else {482T0_PUSHi(-1);483}484485}486break;487case 26: {488/* set8 */489490size_t addr = T0_POP();491unsigned x = T0_POP();492*((unsigned char *)CTX + addr) = x;493494}495break;496case 27: {497/* swap */498T0_SWAP();499}500break;501case 28: {502/* write8 */503504unsigned char x = (unsigned char)T0_POP();505CTX->buf[CTX->ptr ++] = x;506if (CTX->ptr == sizeof CTX->buf) {507if (CTX->dest) {508CTX->dest(CTX->dest_ctx, CTX->buf, sizeof CTX->buf);509}510CTX->ptr = 0;511}512513}514break;515}516517} else {518T0_ENTER(ip, rp, t0x);519}520}521t0_exit:522((t0_context *)t0ctx)->dp = dp;523((t0_context *)t0ctx)->rp = rp;524((t0_context *)t0ctx)->ip = ip;525}526527528