id
int64
1
36.7k
label
int64
0
1
bug_url
stringlengths
91
134
bug_function
stringlengths
13
72.7k
functions
stringlengths
17
79.2k
35,801
0
https://github.com/libav/libav/blob/ab492ca2ab105aeb24d955f3f03756bdb3139ee1/libavcodec/dnxhddec.c/#L229
static av_always_inline void dnxhd_decode_dct_block(DNXHDContext *ctx, DCTELEM *block, int n, int qscale, int index_bits, int level_bias, int level_shift) { int i, j, index1, index2, len; int level, component, sign; const uint8_t *weight_matrix; OPEN_READER(bs, &ctx->gb); if (n&2) { component = 1 + (n&1); weight_matrix = ctx->cid_table->chroma_weight; } else { component = 0; weight_matrix = ctx->cid_table->luma_weight; } UPDATE_CACHE(bs, &ctx->gb); GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1); if (len) { level = GET_CACHE(bs, &ctx->gb); LAST_SKIP_BITS(bs, &ctx->gb, len); sign = ~level >> 31; level = (NEG_USR32(sign ^ level, len) ^ sign) - sign; ctx->last_dc[component] += level; } block[0] = ctx->last_dc[component]; for (i = 1; ; i++) { UPDATE_CACHE(bs, &ctx->gb); GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table, DNXHD_VLC_BITS, 2); level = ctx->cid_table->ac_level[index1]; if (!level) { break; } sign = SHOW_SBITS(bs, &ctx->gb, 1); SKIP_BITS(bs, &ctx->gb, 1); if (ctx->cid_table->ac_index_flag[index1]) { level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 6; SKIP_BITS(bs, &ctx->gb, index_bits); } if (ctx->cid_table->ac_run_flag[index1]) { UPDATE_CACHE(bs, &ctx->gb); GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table, DNXHD_VLC_BITS, 2); i += ctx->cid_table->run[index2]; } if (i > 63) { av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i); break; } j = ctx->scantable.permutated[i]; level = (2*level+1) * qscale * weight_matrix[i]; if (level_bias < 32 || weight_matrix[i] != level_bias) level += level_bias; level >>= level_shift; block[j] = (level^sign) - sign; } CLOSE_READER(bs, &ctx->gb); }
['static av_always_inline void dnxhd_decode_dct_block(DNXHDContext *ctx,\n DCTELEM *block, int n,\n int qscale,\n int index_bits,\n int level_bias,\n int level_shift)\n{\n int i, j, index1, index2, len;\n int level, component, sign;\n const uint8_t *weight_matrix;\n OPEN_READER(bs, &ctx->gb);\n if (n&2) {\n component = 1 + (n&1);\n weight_matrix = ctx->cid_table->chroma_weight;\n } else {\n component = 0;\n weight_matrix = ctx->cid_table->luma_weight;\n }\n UPDATE_CACHE(bs, &ctx->gb);\n GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);\n if (len) {\n level = GET_CACHE(bs, &ctx->gb);\n LAST_SKIP_BITS(bs, &ctx->gb, len);\n sign = ~level >> 31;\n level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;\n ctx->last_dc[component] += level;\n }\n block[0] = ctx->last_dc[component];\n for (i = 1; ; i++) {\n UPDATE_CACHE(bs, &ctx->gb);\n GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,\n DNXHD_VLC_BITS, 2);\n level = ctx->cid_table->ac_level[index1];\n if (!level) {\n break;\n }\n sign = SHOW_SBITS(bs, &ctx->gb, 1);\n SKIP_BITS(bs, &ctx->gb, 1);\n if (ctx->cid_table->ac_index_flag[index1]) {\n level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 6;\n SKIP_BITS(bs, &ctx->gb, index_bits);\n }\n if (ctx->cid_table->ac_run_flag[index1]) {\n UPDATE_CACHE(bs, &ctx->gb);\n GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table,\n DNXHD_VLC_BITS, 2);\n i += ctx->cid_table->run[index2];\n }\n if (i > 63) {\n av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\\n", n, i);\n break;\n }\n j = ctx->scantable.permutated[i];\n level = (2*level+1) * qscale * weight_matrix[i];\n if (level_bias < 32 || weight_matrix[i] != level_bias)\n level += level_bias;\n level >>= level_shift;\n block[j] = (level^sign) - sign;\n }\n CLOSE_READER(bs, &ctx->gb);\n}']
35,802
0
https://github.com/libav/libav/blob/5351964a2b524d1cb70c268c3e9436fd2990429b/libavcodec/mpeg12dec.c/#L238
static inline int mpeg1_decode_block_inter(MpegEncContext *s, int16_t *block, int n) { int level, i, j, run; RLTable *rl = &ff_rl_mpeg1; uint8_t *const scantable = s->intra_scantable.permutated; const uint16_t *quant_matrix = s->inter_matrix; const int qscale = s->qscale; { OPEN_READER(re, &s->gb); i = -1; UPDATE_CACHE(re, &s->gb); if (((int32_t) GET_CACHE(re, &s->gb)) < 0) { level = (3 * qscale * quant_matrix[0]) >> 5; level = (level - 1) | 1; if (GET_CACHE(re, &s->gb) & 0x40000000) level = -level; block[0] = level; i++; SKIP_BITS(re, &s->gb, 2); if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF) goto end; } for (;;) { GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0); if (level != 0) { i += run; check_scantable_index(s, i); j = scantable[i]; level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5; level = (level - 1) | 1; level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1); } else { run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6); UPDATE_CACHE(re, &s->gb); level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8); if (level == -128) { level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8); } else if (level == 0) { level = SHOW_UBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8); } i += run; check_scantable_index(s, i); j = scantable[i]; if (level < 0) { level = -level; level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5; level = (level - 1) | 1; level = -level; } else { level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5; level = (level - 1) | 1; } } block[j] = level; if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF) break; UPDATE_CACHE(re, &s->gb); } end: LAST_SKIP_BITS(re, &s->gb, 2); CLOSE_READER(re, &s->gb); } s->block_last_index[n] = i; return 0; }
['static inline int mpeg1_decode_block_inter(MpegEncContext *s,\n int16_t *block, int n)\n{\n int level, i, j, run;\n RLTable *rl = &ff_rl_mpeg1;\n uint8_t *const scantable = s->intra_scantable.permutated;\n const uint16_t *quant_matrix = s->inter_matrix;\n const int qscale = s->qscale;\n {\n OPEN_READER(re, &s->gb);\n i = -1;\n UPDATE_CACHE(re, &s->gb);\n if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {\n level = (3 * qscale * quant_matrix[0]) >> 5;\n level = (level - 1) | 1;\n if (GET_CACHE(re, &s->gb) & 0x40000000)\n level = -level;\n block[0] = level;\n i++;\n SKIP_BITS(re, &s->gb, 2);\n if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)\n goto end;\n }\n for (;;) {\n GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],\n TEX_VLC_BITS, 2, 0);\n if (level != 0) {\n i += run;\n check_scantable_index(s, i);\n j = scantable[i];\n level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;\n level = (level - 1) | 1;\n level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -\n SHOW_SBITS(re, &s->gb, 1);\n SKIP_BITS(re, &s->gb, 1);\n } else {\n run = SHOW_UBITS(re, &s->gb, 6) + 1;\n LAST_SKIP_BITS(re, &s->gb, 6);\n UPDATE_CACHE(re, &s->gb);\n level = SHOW_SBITS(re, &s->gb, 8);\n SKIP_BITS(re, &s->gb, 8);\n if (level == -128) {\n level = SHOW_UBITS(re, &s->gb, 8) - 256;\n SKIP_BITS(re, &s->gb, 8);\n } else if (level == 0) {\n level = SHOW_UBITS(re, &s->gb, 8);\n SKIP_BITS(re, &s->gb, 8);\n }\n i += run;\n check_scantable_index(s, i);\n j = scantable[i];\n if (level < 0) {\n level = -level;\n level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;\n level = (level - 1) | 1;\n level = -level;\n } else {\n level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;\n level = (level - 1) | 1;\n }\n }\n block[j] = level;\n if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)\n break;\n UPDATE_CACHE(re, &s->gb);\n }\nend:\n LAST_SKIP_BITS(re, &s->gb, 2);\n CLOSE_READER(re, &s->gb);\n }\n s->block_last_index[n] = i;\n return 0;\n}']
35,803
0
https://github.com/openssl/openssl/blob/183733f882056ea3e6fe95e665b85fcc6a45dcb4/apps/srp.c/#L156
static int update_index(CA_DB *db, char **row) { char **irow; int i; irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers"); for (i = 0; i < DB_NUMBER; i++) { irow[i] = row[i]; row[i] = NULL; } irow[DB_NUMBER] = NULL; if (!TXT_DB_insert(db->db, irow)) { BIO_printf(bio_err, "failed to update srpvfile\n"); BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error); OPENSSL_free(irow); return 0; } return 1; }
['static int update_index(CA_DB *db, char **row)\n{\n char **irow;\n int i;\n irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers");\n for (i = 0; i < DB_NUMBER; i++) {\n irow[i] = row[i];\n row[i] = NULL;\n }\n irow[DB_NUMBER] = NULL;\n if (!TXT_DB_insert(db->db, irow)) {\n BIO_printf(bio_err, "failed to update srpvfile\\n");\n BIO_printf(bio_err, "TXT_DB error number %ld\\n", db->db->error);\n OPENSSL_free(irow);\n return 0;\n }\n return 1;\n}', 'void* app_malloc(int sz, const char *what)\n{\n void *vp = OPENSSL_malloc(sz);\n if (vp == NULL) {\n BIO_printf(bio_err, "%s: Could not allocate %d bytes for %s\\n",\n opt_getprog(), sz, what);\n ERR_print_errors(bio_err);\n exit(1);\n }\n return vp;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}']
35,804
0
https://github.com/openssl/openssl/blob/2864df8f9d3264e19b49a246e272fb513f4c1be3/crypto/bn/bn_ctx.c/#L270
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int test_is_composite_enhanced(int id)\n{\n int ret;\n int status = 0;\n BIGNUM *bn = NULL;\n ret = TEST_ptr(bn = BN_new())\n && TEST_true(BN_set_word(bn, composites[id]))\n && TEST_true(bn_miller_rabin_is_prime(bn, 10, ctx, NULL, 1, &status))\n && TEST_int_ne(status, BN_PRIMETEST_PROBABLY_PRIME);\n BN_free(bn);\n return ret;\n}', 'int bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,\n BN_GENCB *cb, int enhanced, int *status)\n{\n int i, j, a, ret = 0;\n BIGNUM *g, *w1, *w3, *x, *m, *z, *b;\n BN_MONT_CTX *mont = NULL;\n if (!BN_is_odd(w))\n return 0;\n BN_CTX_start(ctx);\n g = BN_CTX_get(ctx);\n w1 = BN_CTX_get(ctx);\n w3 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n m = BN_CTX_get(ctx);\n z = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (!(b != NULL\n && BN_copy(w1, w)\n && BN_sub_word(w1, 1)\n && BN_copy(w3, w)\n && BN_sub_word(w3, 3)))\n goto err;\n if (BN_is_zero(w3) || BN_is_negative(w3))\n goto err;\n a = 1;\n while (!BN_is_bit_set(w1, a))\n a++;\n if (!BN_rshift(m, w1, a))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL || !BN_MONT_CTX_set(mont, w, ctx))\n goto err;\n if (iterations == BN_prime_checks)\n iterations = BN_prime_checks_for_size(BN_num_bits(w));\n for (i = 0; i < iterations; ++i) {\n if (!BN_priv_rand_range(b, w3) || !BN_add_word(b, 2))\n goto err;\n if (enhanced) {\n if (!BN_gcd(g, b, w, ctx))\n goto err;\n if (!BN_is_one(g)) {\n *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;\n ret = 1;\n goto err;\n }\n }\n if (!BN_mod_exp_mont(z, b, m, w, ctx, mont))\n goto err;\n if (BN_is_one(z) || BN_cmp(z, w1) == 0)\n goto outer_loop;\n for (j = 1; j < a ; ++j) {\n if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))\n goto err;\n if (BN_cmp(z, w1) == 0)\n goto outer_loop;\n if (BN_is_one(z))\n goto composite;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))\n goto err;\n if (BN_is_one(z))\n goto composite;\n if (!BN_copy(x, z))\n goto err;\ncomposite:\n if (enhanced) {\n if (!BN_sub_word(x, 1) || !BN_gcd(g, x, w, ctx))\n goto err;\n if (BN_is_one(g))\n *status = BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME;\n else\n *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;\n } else {\n *status = BN_PRIMETEST_COMPOSITE;\n }\n ret = 1;\n goto err;\nouter_loop: ;\n }\n *status = BN_PRIMETEST_PROBABLY_PRIME;\n ret = 1;\nerr:\n BN_clear(g);\n BN_clear(w1);\n BN_clear(w3);\n BN_clear(x);\n BN_clear(m);\n BN_clear(z);\n BN_clear(b);\n BN_CTX_end(ctx);\n BN_MONT_CTX_free(mont);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)\n mont->RR.d[i] = 0;\n mont->RR.top = ret;\n mont->RR.flags |= BN_FLG_FIXED_TOP;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {\n if (pnoinv != NULL)\n *pnoinv = 1;\n return NULL;\n }\n if (pnoinv != NULL)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n {\n BIGNUM local_B;\n bn_init(&local_B);\n BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, &local_B, A, ctx))\n goto err;\n }\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n {\n BIGNUM local_A;\n bn_init(&local_A);\n BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, &local_A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
35,805
0
https://github.com/openssl/openssl/blob/866cc2334c95c8602eb4d018bfc224357c47b511/crypto/evp/pmeth_lib.c/#L393
int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd, int p1, void *p2) { int ret; if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) { EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); return -2; } if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype)) return -1; if (ctx->pmeth->digest_custom != NULL) goto doit; if (ctx->operation == EVP_PKEY_OP_UNDEFINED) { EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET); return -1; } if ((optype != -1) && !(ctx->operation & optype)) { EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION); return -1; } doit: ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2); if (ret == -2) EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); return ret; }
['BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)\n{\n unsigned char dig[SHA_DIGEST_LENGTH];\n EVP_MD_CTX *ctxt;\n unsigned char *cs = NULL;\n BIGNUM *res = NULL;\n if ((s == NULL) || (user == NULL) || (pass == NULL))\n return NULL;\n ctxt = EVP_MD_CTX_new();\n if (ctxt == NULL)\n return NULL;\n if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)\n goto err;\n if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)\n || !EVP_DigestUpdate(ctxt, user, strlen(user))\n || !EVP_DigestUpdate(ctxt, ":", 1)\n || !EVP_DigestUpdate(ctxt, pass, strlen(pass))\n || !EVP_DigestFinal_ex(ctxt, dig, NULL)\n || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL))\n goto err;\n if (BN_bn2bin(s, cs) < 0)\n goto err;\n if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))\n goto err;\n if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))\n || !EVP_DigestFinal_ex(ctxt, dig, NULL))\n goto err;\n res = BN_bin2bn(dig, sizeof(dig), NULL);\n err:\n OPENSSL_free(cs);\n EVP_MD_CTX_free(ctxt);\n return res;\n}', 'int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)\n{\n int ret;\n size_t size = 0;\n if (ctx->digest == NULL || ctx->digest->prov == NULL)\n goto legacy;\n if (ctx->digest->dfinal == NULL) {\n EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);\n return 0;\n }\n ret = ctx->digest->dfinal(ctx->provctx, md, &size);\n if (isize != NULL) {\n if (size <= UINT_MAX) {\n *isize = (int)size;\n } else {\n EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);\n ret = 0;\n }\n }\n EVP_MD_CTX_reset(ctx);\n return ret;\n legacy:\n OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);\n ret = ctx->digest->final(ctx, md);\n if (isize != NULL)\n *isize = ctx->digest->md_size;\n if (ctx->digest->cleanup) {\n ctx->digest->cleanup(ctx);\n EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n }\n OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);\n return ret;\n}', 'int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)\n{\n if (ctx == NULL)\n return 1;\n if (ctx->digest == NULL || ctx->digest->prov == NULL)\n goto legacy;\n if (ctx->provctx != NULL) {\n if (ctx->digest->freectx != NULL)\n ctx->digest->freectx(ctx->provctx);\n ctx->provctx = NULL;\n EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n }\n if (ctx->pctx != NULL)\n goto legacy;\n return 1;\n legacy:\n if (ctx->digest && ctx->digest->cleanup\n && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))\n ctx->digest->cleanup(ctx);\n if (ctx->digest && ctx->digest->ctx_size && ctx->md_data\n && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {\n OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);\n }\n if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))\n EVP_PKEY_CTX_free(ctx->pctx);\n#ifndef OPENSSL_NO_ENGINE\n ENGINE_finish(ctx->engine);\n#endif\n OPENSSL_cleanse(ctx, sizeof(*ctx));\n return 1;\n}', 'void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n if (ctx->pmeth && ctx->pmeth->cleanup)\n ctx->pmeth->cleanup(ctx);\n EVP_PKEY_free(ctx->pkey);\n EVP_PKEY_free(ctx->peerkey);\n#ifndef OPENSSL_NO_ENGINE\n ENGINE_finish(ctx->engine);\n#endif\n OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n INCREMENT(free_count);\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}', 'int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)\n{\n EVP_MD *provmd;\n ENGINE *tmpimpl = NULL;\n EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n#ifndef OPENSSL_NO_ENGINE\n if (ctx->engine && ctx->digest &&\n (type == NULL || (type->type == ctx->digest->type)))\n goto skip_to_init;\n if (type != NULL && impl == NULL)\n tmpimpl = ENGINE_get_digest_engine(type->type);\n#endif\n if (ctx->engine != NULL\n || impl != NULL\n || tmpimpl != NULL\n || ctx->pctx != NULL\n || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {\n if (ctx->digest == ctx->fetched_digest)\n ctx->digest = NULL;\n EVP_MD_meth_free(ctx->fetched_digest);\n ctx->fetched_digest = NULL;\n goto legacy;\n }\n if (type->prov == NULL) {\n switch(type->type) {\n case NID_sha256:\n break;\n default:\n goto legacy;\n }\n }\n if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {\n OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);\n ctx->md_data = NULL;\n }\n if (type->prov == NULL) {\n provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");\n if (provmd == NULL) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n return 0;\n }\n type = provmd;\n EVP_MD_meth_free(ctx->fetched_digest);\n ctx->fetched_digest = provmd;\n }\n ctx->digest = type;\n if (ctx->provctx == NULL) {\n ctx->provctx = ctx->digest->newctx();\n if (ctx->provctx == NULL) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n return 0;\n }\n }\n if (ctx->digest->dinit == NULL) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n return 0;\n }\n return ctx->digest->dinit(ctx->provctx);\n legacy:\n#ifndef OPENSSL_NO_ENGINE\n if (type) {\n ENGINE_finish(ctx->engine);\n if (impl != NULL) {\n if (!ENGINE_init(impl)) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n return 0;\n }\n } else {\n impl = tmpimpl;\n }\n if (impl != NULL) {\n const EVP_MD *d = ENGINE_get_digest(impl, type->type);\n if (d == NULL) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n ENGINE_finish(impl);\n return 0;\n }\n type = d;\n ctx->engine = impl;\n } else\n ctx->engine = NULL;\n } else {\n if (!ctx->digest) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);\n return 0;\n }\n type = ctx->digest;\n }\n#endif\n if (ctx->digest != type) {\n if (ctx->digest && ctx->digest->ctx_size) {\n OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);\n ctx->md_data = NULL;\n }\n ctx->digest = type;\n if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {\n ctx->update = type->update;\n ctx->md_data = OPENSSL_zalloc(type->ctx_size);\n if (ctx->md_data == NULL) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n }\n }\n#ifndef OPENSSL_NO_ENGINE\n skip_to_init:\n#endif\n if (ctx->pctx) {\n int r;\n r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,\n EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);\n if (r <= 0 && (r != -2))\n return 0;\n }\n if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)\n return 1;\n return ctx->digest->init(ctx);\n}', 'int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,\n int cmd, int p1, void *p2)\n{\n int ret;\n if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {\n EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);\n return -2;\n }\n if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))\n return -1;\n if (ctx->pmeth->digest_custom != NULL)\n goto doit;\n if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {\n EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);\n return -1;\n }\n if ((optype != -1) && !(ctx->operation & optype)) {\n EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);\n return -1;\n }\n doit:\n ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);\n if (ret == -2)\n EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);\n return ret;\n}']
35,806
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L260
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int ec_GFp_simple_group_set_curve(EC_GROUP *group,\n const BIGNUM *p, const BIGNUM *a,\n const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp_a;\n if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {\n ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD);\n return 0;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n tmp_a = BN_CTX_get(ctx);\n if (tmp_a == NULL)\n goto err;\n if (!BN_copy(group->field, p))\n goto err;\n BN_set_negative(group->field, 0);\n if (!BN_nnmod(tmp_a, a, p, ctx))\n goto err;\n if (group->meth->field_encode) {\n if (!group->meth->field_encode(group, group->a, tmp_a, ctx))\n goto err;\n } else if (!BN_copy(group->a, tmp_a))\n goto err;\n if (!BN_nnmod(group->b, b, p, ctx))\n goto err;\n if (group->meth->field_encode)\n if (!group->meth->field_encode(group, group->b, group->b, ctx))\n goto err;\n if (!BN_add_word(tmp_a, 3))\n goto err;\n group->a_is_minus3 = (0 == BN_cmp(tmp_a, group->field));\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, j, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l, tmp;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n rb = n % BN_BITS2;\n lb = BN_BITS2 - rb;\n if (nw >= a->top || a->top == 0) {\n BN_zero(r);\n return 1;\n }\n i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;\n if (r != a) {\n if (bn_wexpand(r, i) == NULL)\n return 0;\n r->neg = a->neg;\n } else {\n if (n == 0)\n return 1;\n }\n f = &(a->d[nw]);\n t = r->d;\n j = a->top - nw;\n r->top = i;\n if (rb == 0) {\n for (i = j; i != 0; i--)\n *(t++) = *(f++);\n } else {\n l = *(f++);\n for (i = j - 1; i != 0; i--) {\n tmp = (l >> rb) & BN_MASK2;\n l = *(f++);\n *(t++) = (tmp | (l << lb)) & BN_MASK2;\n }\n if ((l = (l >> rb) & BN_MASK2))\n *(t) = l;\n }\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
35,807
0
https://github.com/libav/libav/blob/124c21d79f2124d028890022e98ea853a834a964/libavcodec/vorbis_enc.c/#L916
static av_cold int vorbis_encode_init(AVCodecContext * avccontext) { vorbis_enc_context * venc = avccontext->priv_data; if (avccontext->channels != 2) { av_log(avccontext, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n"); return -1; } create_vorbis_context(venc, avccontext); if (avccontext->flags & CODEC_FLAG_QSCALE) venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 10.; else venc->quality = 1.; venc->quality *= venc->quality; avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata); avccontext->frame_size = 1 << (venc->log2_blocksize[0] - 1); avccontext->coded_frame = avcodec_alloc_frame(); avccontext->coded_frame->key_frame = 1; return 0; }
['static av_cold int vorbis_encode_init(AVCodecContext * avccontext)\n{\n vorbis_enc_context * venc = avccontext->priv_data;\n if (avccontext->channels != 2) {\n av_log(avccontext, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\\n");\n return -1;\n }\n create_vorbis_context(venc, avccontext);\n if (avccontext->flags & CODEC_FLAG_QSCALE)\n venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 10.;\n else\n venc->quality = 1.;\n venc->quality *= venc->quality;\n avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);\n avccontext->frame_size = 1 << (venc->log2_blocksize[0] - 1);\n avccontext->coded_frame = avcodec_alloc_frame();\n avccontext->coded_frame->key_frame = 1;\n return 0;\n}', 'AVFrame *avcodec_alloc_frame(void){\n AVFrame *pic= av_malloc(sizeof(AVFrame));\n if(pic==NULL) return NULL;\n avcodec_get_frame_defaults(pic);\n return pic;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr;\n#ifdef CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#ifdef CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif defined (HAVE_MEMALIGN)\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
35,808
0
https://github.com/openssl/openssl/blob/3cdd1e94b1d71f2ce3002738f9506da91fe2af45/ssl/t1_lib.c/#L4199
DH *ssl_get_auto_dh(SSL *s) { int dh_secbits = 80; if (s->cert->dh_tmp_auto == 2) return DH_get_1024_160(); if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { if (s->s3->tmp.new_cipher->strength_bits == 256) dh_secbits = 128; else dh_secbits = 80; } else { CERT_PKEY *cpk = ssl_get_server_send_pkey(s); dh_secbits = EVP_PKEY_security_bits(cpk->privatekey); } if (dh_secbits >= 128) { DH *dhp = DH_new(); if (!dhp) return NULL; dhp->g = BN_new(); if (dhp->g) BN_set_word(dhp->g, 2); if (dh_secbits >= 192) dhp->p = get_rfc3526_prime_8192(NULL); else dhp->p = get_rfc3526_prime_3072(NULL); if (!dhp->p || !dhp->g) { DH_free(dhp); return NULL; } return dhp; } if (dh_secbits >= 112) return DH_get_2048_224(); return DH_get_1024_160(); }
['DH *ssl_get_auto_dh(SSL *s)\n{\n int dh_secbits = 80;\n if (s->cert->dh_tmp_auto == 2)\n return DH_get_1024_160();\n if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {\n if (s->s3->tmp.new_cipher->strength_bits == 256)\n dh_secbits = 128;\n else\n dh_secbits = 80;\n } else {\n CERT_PKEY *cpk = ssl_get_server_send_pkey(s);\n dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);\n }\n if (dh_secbits >= 128) {\n DH *dhp = DH_new();\n if (!dhp)\n return NULL;\n dhp->g = BN_new();\n if (dhp->g)\n BN_set_word(dhp->g, 2);\n if (dh_secbits >= 192)\n dhp->p = get_rfc3526_prime_8192(NULL);\n else\n dhp->p = get_rfc3526_prime_3072(NULL);\n if (!dhp->p || !dhp->g) {\n DH_free(dhp);\n return NULL;\n }\n return dhp;\n }\n if (dh_secbits >= 112)\n return DH_get_2048_224();\n return DH_get_1024_160();\n}', 'CERT_PKEY *ssl_get_server_send_pkey(SSL *s)\n{\n CERT *c;\n int i;\n c = s->cert;\n if (!s->s3 || !s->s3->tmp.new_cipher)\n return NULL;\n ssl_set_masks(s, s->s3->tmp.new_cipher);\n#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL\n if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)\n return c->key;\n#endif\n i = ssl_get_server_cert_index(s);\n if (i < 0)\n return NULL;\n return &c->pkeys[i];\n}']
35,809
0
https://gitlab.com/libtiff/libtiff/blob/163627448aa8d2893582f2546dd85706586e6243/tools/tiff2pdf.c/#L5237
tsize_t t2p_write_pdf(T2P* t2p, TIFF* input, TIFF* output){ tsize_t written=0; ttile_t i2=0; tsize_t streamlen=0; uint16 i=0; t2p_read_tiff_init(t2p, input); if(t2p->t2p_error!=T2P_ERR_OK){return(0);} t2p->pdf_xrefoffsets= (uint32*) _TIFFmalloc(t2p->pdf_xrefcount * sizeof(uint32) ); if(t2p->pdf_xrefoffsets==NULL){ TIFFError( TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for t2p_write_pdf", (unsigned int) (t2p->pdf_xrefcount * sizeof(uint32)) ); t2p->t2p_error = T2P_ERR_ERROR; return(written); } t2p->pdf_xrefcount=0; t2p->pdf_catalog=1; t2p->pdf_info=2; t2p->pdf_pages=3; written += t2p_write_pdf_header(t2p, output); t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; t2p->pdf_catalog=t2p->pdf_xrefcount; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_catalog(t2p, output); written += t2p_write_pdf_obj_end(output); t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; t2p->pdf_info=t2p->pdf_xrefcount; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_info(t2p, input, output); written += t2p_write_pdf_obj_end(output); t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; t2p->pdf_pages=t2p->pdf_xrefcount; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_pages(t2p, output); written += t2p_write_pdf_obj_end(output); for(t2p->pdf_page=0;t2p->pdf_page<t2p->tiff_pagecount;t2p->pdf_page++){ t2p_read_tiff_data(t2p, input); if(t2p->t2p_error!=T2P_ERR_OK){return(0);} t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_page(t2p->pdf_xrefcount, t2p, output); written += t2p_write_pdf_obj_end(output); t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_stream_dict_start(output); written += t2p_write_pdf_stream_dict(0, t2p->pdf_xrefcount+1, output); written += t2p_write_pdf_stream_dict_end(output); written += t2p_write_pdf_stream_start(output); streamlen=written; written += t2p_write_pdf_page_content_stream(t2p, output); streamlen=written-streamlen; written += t2p_write_pdf_stream_end(output); written += t2p_write_pdf_obj_end(output); t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_stream_length(streamlen, output); written += t2p_write_pdf_obj_end(output); if(t2p->tiff_transferfunctioncount != 0){ t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_transfer(t2p, output); written += t2p_write_pdf_obj_end(output); for(i=0; i < t2p->tiff_transferfunctioncount; i++){ t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_stream_dict_start(output); written += t2p_write_pdf_transfer_dict(t2p, output, i); written += t2p_write_pdf_stream_dict_end(output); written += t2p_write_pdf_stream_start(output); streamlen=written; written += t2p_write_pdf_transfer_stream(t2p, output, i); streamlen=written-streamlen; written += t2p_write_pdf_stream_end(output); written += t2p_write_pdf_obj_end(output); } } if( (t2p->pdf_colorspace & T2P_CS_PALETTE) != 0){ t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; t2p->pdf_palettecs=t2p->pdf_xrefcount; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_stream_dict_start(output); written += t2p_write_pdf_stream_dict(t2p->pdf_palettesize, 0, output); written += t2p_write_pdf_stream_dict_end(output); written += t2p_write_pdf_stream_start(output); streamlen=written; written += t2p_write_pdf_xobject_palettecs_stream(t2p, output); streamlen=written-streamlen; written += t2p_write_pdf_stream_end(output); written += t2p_write_pdf_obj_end(output); } if( (t2p->pdf_colorspace & T2P_CS_ICCBASED) != 0){ t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; t2p->pdf_icccs=t2p->pdf_xrefcount; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_stream_dict_start(output); written += t2p_write_pdf_xobject_icccs_dict(t2p, output); written += t2p_write_pdf_stream_dict_end(output); written += t2p_write_pdf_stream_start(output); streamlen=written; written += t2p_write_pdf_xobject_icccs_stream(t2p, output); streamlen=written-streamlen; written += t2p_write_pdf_stream_end(output); written += t2p_write_pdf_obj_end(output); } if(t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount !=0){ for(i2=0;i2<t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount;i2++){ t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_stream_dict_start(output); written += t2p_write_pdf_xobject_stream_dict( i2+1, t2p, output); written += t2p_write_pdf_stream_dict_end(output); written += t2p_write_pdf_stream_start(output); streamlen=written; t2p_read_tiff_size_tile(t2p, input, i2); written += t2p_readwrite_pdf_image_tile(t2p, input, output, i2); t2p_write_advance_directory(t2p, output); if(t2p->t2p_error!=T2P_ERR_OK){return(0);} streamlen=written-streamlen; written += t2p_write_pdf_stream_end(output); written += t2p_write_pdf_obj_end(output); t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_stream_length(streamlen, output); written += t2p_write_pdf_obj_end(output); } } else { t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_stream_dict_start(output); written += t2p_write_pdf_xobject_stream_dict( 0, t2p, output); written += t2p_write_pdf_stream_dict_end(output); written += t2p_write_pdf_stream_start(output); streamlen=written; t2p_read_tiff_size(t2p, input); written += t2p_readwrite_pdf_image(t2p, input, output); t2p_write_advance_directory(t2p, output); if(t2p->t2p_error!=T2P_ERR_OK){return(0);} streamlen=written-streamlen; written += t2p_write_pdf_stream_end(output); written += t2p_write_pdf_obj_end(output); t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); written += t2p_write_pdf_stream_length(streamlen, output); written += t2p_write_pdf_obj_end(output); } } t2p->pdf_startxref = written; written += t2p_write_pdf_xreftable(t2p, output); written += t2p_write_pdf_trailer(t2p, output); t2p_disable(output); return(written); }
['tsize_t t2p_write_pdf(T2P* t2p, TIFF* input, TIFF* output){\n\ttsize_t written=0;\n\tttile_t i2=0;\n\ttsize_t streamlen=0;\n\tuint16 i=0;\n\tt2p_read_tiff_init(t2p, input);\n\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\tt2p->pdf_xrefoffsets= (uint32*) _TIFFmalloc(t2p->pdf_xrefcount * sizeof(uint32) );\n\tif(t2p->pdf_xrefoffsets==NULL){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"Can\'t allocate %u bytes of memory for t2p_write_pdf",\n\t\t\t(unsigned int) (t2p->pdf_xrefcount * sizeof(uint32)) );\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn(written);\n\t}\n\tt2p->pdf_xrefcount=0;\n\tt2p->pdf_catalog=1;\n\tt2p->pdf_info=2;\n\tt2p->pdf_pages=3;\n\twritten += t2p_write_pdf_header(t2p, output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_catalog=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_catalog(t2p, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_info=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_info(t2p, input, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_pages=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_pages(t2p, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tfor(t2p->pdf_page=0;t2p->pdf_page<t2p->tiff_pagecount;t2p->pdf_page++){\n\t\tt2p_read_tiff_data(t2p, input);\n\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_page(t2p->pdf_xrefcount, t2p, output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\twritten += t2p_write_pdf_stream_dict(0, t2p->pdf_xrefcount+1, output);\n\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\twritten += t2p_write_pdf_stream_start(output);\n\t\tstreamlen=written;\n\t\twritten += t2p_write_pdf_page_content_stream(t2p, output);\n\t\tstreamlen=written-streamlen;\n\t\twritten += t2p_write_pdf_stream_end(output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tif(t2p->tiff_transferfunctioncount != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_transfer(t2p, output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\tfor(i=0; i < t2p->tiff_transferfunctioncount; i++){\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\t\twritten += t2p_write_pdf_transfer_dict(t2p, output, i);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\t\tstreamlen=written;\n\t\t\t\twritten += t2p_write_pdf_transfer_stream(t2p, output, i);\n\t\t\t\tstreamlen=written-streamlen;\n\t\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t}\n\t\t}\n\t\tif( (t2p->pdf_colorspace & T2P_CS_PALETTE) != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\tt2p->pdf_palettecs=t2p->pdf_xrefcount;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_stream_dict(t2p->pdf_palettesize, 0, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\tstreamlen=written;\n\t\t\twritten += t2p_write_pdf_xobject_palettecs_stream(t2p, output);\n\t\t\tstreamlen=written-streamlen;\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t\tif( (t2p->pdf_colorspace & T2P_CS_ICCBASED) != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\tt2p->pdf_icccs=t2p->pdf_xrefcount;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_icccs_dict(t2p, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\tstreamlen=written;\n\t\t\twritten += t2p_write_pdf_xobject_icccs_stream(t2p, output);\n\t\t\tstreamlen=written-streamlen;\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t\tif(t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount !=0){\n\t\t\tfor(i2=0;i2<t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount;i2++){\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\t\twritten += t2p_write_pdf_xobject_stream_dict(\n\t\t\t\t\ti2+1,\n\t\t\t\t\tt2p,\n\t\t\t\t\toutput);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\t\tstreamlen=written;\n\t\t\t\tt2p_read_tiff_size_tile(t2p, input, i2);\n\t\t\t\twritten += t2p_readwrite_pdf_image_tile(t2p, input, output, i2);\n\t\t\t\tt2p_write_advance_directory(t2p, output);\n\t\t\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\t\t\tstreamlen=written-streamlen;\n\t\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t}\n\t\t} else {\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_stream_dict(\n\t\t\t\t0,\n\t\t\t\tt2p,\n\t\t\t\toutput);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\tstreamlen=written;\n\t\t\tt2p_read_tiff_size(t2p, input);\n\t\t\twritten += t2p_readwrite_pdf_image(t2p, input, output);\n\t\t\tt2p_write_advance_directory(t2p, output);\n\t\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\t\tstreamlen=written-streamlen;\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t}\n\tt2p->pdf_startxref = written;\n\twritten += t2p_write_pdf_xreftable(t2p, output);\n\twritten += t2p_write_pdf_trailer(t2p, output);\n\tt2p_disable(output);\n\treturn(written);\n}', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n if (s == 0)\n return ((void *) NULL);\n\treturn (malloc((size_t) s));\n}', 'tsize_t t2p_write_pdf_header(T2P* t2p, TIFF* output){\n\ttsize_t written=0;\n\tchar buffer[16];\n\tint buflen=0;\n\tbuflen = snprintf(buffer, sizeof(buffer), "%%PDF-%u.%u ",\n\t\t\t t2p->pdf_majorversion&0xff,\n\t\t\t t2p->pdf_minorversion&0xff);\n\twritten += t2pWriteFile(output, (tdata_t) buffer, buflen);\n\twritten += t2pWriteFile(output, (tdata_t)"\\n%\\342\\343\\317\\323\\n", 7);\n\treturn(written);\n}', 'static tmsize_t\nt2pWriteFile(TIFF *tif, tdata_t data, tmsize_t size)\n{\n\tthandle_t client = TIFFClientdata(tif);\n\tTIFFReadWriteProc proc = TIFFGetWriteProc(tif);\n\tif (proc)\n\t\treturn proc(client, data, size);\n\treturn -1;\n}', 'thandle_t\nTIFFClientdata(TIFF* tif)\n{\n\treturn (tif->tif_clientdata);\n}', 'TIFFReadWriteProc\nTIFFGetWriteProc(TIFF* tif)\n{\n\treturn (tif->tif_writeproc);\n}', 'tsize_t t2p_write_pdf_obj_start(uint32 number, TIFF* output){\n\ttsize_t written=0;\n\tchar buffer[32];\n\tint buflen=0;\n\tbuflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)number);\n\twritten += t2pWriteFile(output, (tdata_t) buffer, buflen );\n\twritten += t2pWriteFile(output, (tdata_t) " 0 obj\\n", 7);\n\treturn(written);\n}', 'tsize_t t2p_write_pdf_catalog(T2P* t2p, TIFF* output)\n{\n\ttsize_t written = 0;\n\tchar buffer[32];\n\tint buflen = 0;\n\twritten += t2pWriteFile(output,\n\t\t(tdata_t)"<< \\n/Type /Catalog \\n/Pages ",\n\t\t27);\n\tbuflen = snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)t2p->pdf_pages);\n\twritten += t2pWriteFile(output, (tdata_t) buffer,\n\t\t\t\tTIFFmin((size_t)buflen, sizeof(buffer) - 1));\n\twritten += t2pWriteFile(output, (tdata_t) " 0 R \\n", 6);\n\tif(t2p->pdf_fitwindow){\n\t\twritten += t2pWriteFile(output,\n\t\t\t(tdata_t) "/ViewerPreferences <</FitWindow true>>\\n",\n\t\t\t39);\n\t}\n\twritten += t2pWriteFile(output, (tdata_t)">>\\n", 3);\n\treturn(written);\n}', 'tsize_t t2p_write_pdf_obj_end(TIFF* output){\n\ttsize_t written=0;\n\twritten += t2pWriteFile(output, (tdata_t) "endobj\\n", 7);\n\treturn(written);\n}']
35,810
0
https://github.com/openssl/openssl/blob/6bc62a620e715f7580651ca932eab052aa527886/crypto/bn/bn_ctx.c/#L268
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,\n const ECDSA_SIG *sig, EC_KEY *eckey)\n{\n int ret = -1, i;\n BN_CTX *ctx;\n const BIGNUM *order;\n BIGNUM *u1, *u2, *m, *X;\n EC_POINT *point = NULL;\n const EC_GROUP *group;\n const EC_POINT *pub_key;\n if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||\n (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);\n return -1;\n }\n if (!EC_KEY_can_sign(eckey)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n return -1;\n }\n ctx = BN_CTX_new();\n if (ctx == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n BN_CTX_start(ctx);\n u1 = BN_CTX_get(ctx);\n u2 = BN_CTX_get(ctx);\n m = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n if (X == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n order = EC_GROUP_get0_order(group);\n if (order == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);\n goto err;\n }\n if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||\n BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||\n BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);\n ret = 0;\n goto err;\n }\n if (!ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n i = BN_num_bits(order);\n if (8 * dgst_len > i)\n dgst_len = (i + 7) / 8;\n if (!BN_bin2bn(dgst, dgst_len, m)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(u1, m, u2, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if ((point = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);\n goto err;\n }\n if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);\n goto err;\n }\n if (!BN_nnmod(u1, X, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n ret = (BN_ucmp(u1, sig->r) == 0);\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n EC_POINT_free(point);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int ret = bn_sqr_fixed_top(r, a, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
35,811
0
https://github.com/openssl/openssl/blob/09977dd095f3c655c99b9e1810a213f7eafa7364/crypto/bn/bn_lib.c/#L342
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *A, *a = NULL; const BN_ULONG *B; int i; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b,BN_FLG_SECURE)) a = A = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = A = OPENSSL_zalloc(words * sizeof(*a)); if (A == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } #if 1 B = b->d; if (B != NULL) { for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { BN_ULONG a0, a1, a2, a3; a0 = B[0]; a1 = B[1]; a2 = B[2]; a3 = B[3]; A[0] = a0; A[1] = a1; A[2] = a2; A[3] = a3; } switch (b->top & 3) { case 3: A[2] = B[2]; case 2: A[1] = B[1]; case 1: A[0] = B[0]; case 0: ; } } #else memset(A, 0, sizeof(*A) * words); memcpy(A, b->d, sizeof(b->d[0]) * b->top); #endif return (a); }
['int ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,\n const EC_POINT *b, BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;\n int ret = 0;\n if (EC_POINT_is_at_infinity(group, a)) {\n if (!EC_POINT_copy(r, b))\n return 0;\n return 1;\n }\n if (EC_POINT_is_at_infinity(group, b)) {\n if (!EC_POINT_copy(r, a))\n return 0;\n return 1;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n x0 = BN_CTX_get(ctx);\n y0 = BN_CTX_get(ctx);\n x1 = BN_CTX_get(ctx);\n y1 = BN_CTX_get(ctx);\n x2 = BN_CTX_get(ctx);\n y2 = BN_CTX_get(ctx);\n s = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (a->Z_is_one) {\n if (!BN_copy(x0, a->X))\n goto err;\n if (!BN_copy(y0, a->Y))\n goto err;\n } else {\n if (!EC_POINT_get_affine_coordinates_GF2m(group, a, x0, y0, ctx))\n goto err;\n }\n if (b->Z_is_one) {\n if (!BN_copy(x1, b->X))\n goto err;\n if (!BN_copy(y1, b->Y))\n goto err;\n } else {\n if (!EC_POINT_get_affine_coordinates_GF2m(group, b, x1, y1, ctx))\n goto err;\n }\n if (BN_GF2m_cmp(x0, x1)) {\n if (!BN_GF2m_add(t, x0, x1))\n goto err;\n if (!BN_GF2m_add(s, y0, y1))\n goto err;\n if (!group->meth->field_div(group, s, s, t, ctx))\n goto err;\n if (!group->meth->field_sqr(group, x2, s, ctx))\n goto err;\n if (!BN_GF2m_add(x2, x2, group->a))\n goto err;\n if (!BN_GF2m_add(x2, x2, s))\n goto err;\n if (!BN_GF2m_add(x2, x2, t))\n goto err;\n } else {\n if (BN_GF2m_cmp(y0, y1) || BN_is_zero(x1)) {\n if (!EC_POINT_set_to_infinity(group, r))\n goto err;\n ret = 1;\n goto err;\n }\n if (!group->meth->field_div(group, s, y1, x1, ctx))\n goto err;\n if (!BN_GF2m_add(s, s, x1))\n goto err;\n if (!group->meth->field_sqr(group, x2, s, ctx))\n goto err;\n if (!BN_GF2m_add(x2, x2, s))\n goto err;\n if (!BN_GF2m_add(x2, x2, group->a))\n goto err;\n }\n if (!BN_GF2m_add(y2, x1, x2))\n goto err;\n if (!group->meth->field_mul(group, y2, y2, s, ctx))\n goto err;\n if (!BN_GF2m_add(y2, y2, x2))\n goto err;\n if (!BN_GF2m_add(y2, y2, y1))\n goto err;\n if (!EC_POINT_set_affine_coordinates_GF2m(group, r, x2, y2, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if(((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int i;\n const BIGNUM *at, *bt;\n bn_check_top(a);\n bn_check_top(b);\n if (a->top < b->top) {\n at = b;\n bt = a;\n } else {\n at = a;\n bt = b;\n }\n if (bn_wexpand(r, at->top) == NULL)\n return 0;\n for (i = 0; i < bt->top; i++) {\n r->d[i] = at->d[i] ^ bt->d[i];\n }\n for (; i < at->top; i++) {\n r->d[i] = at->d[i];\n }\n r->top = at->top;\n bn_correct_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = A = OPENSSL_zalloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
35,812
0
https://gitlab.com/libtiff/libtiff/blob/3adc33842b7533066daea2516741832edc44d5fd/libtiff/tif_strip.c/#L93
uint32 TIFFNumberOfStrips(TIFF* tif) { TIFFDirectory *td = &tif->tif_dir; uint32 nstrips; nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 : TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip)); if (td->td_planarconfig == PLANARCONFIG_SEPARATE) nstrips = multiply_32(tif, nstrips, (uint32)td->td_samplesperpixel, "TIFFNumberOfStrips"); return (nstrips); }
['tsize_t t2p_write_pdf(T2P* t2p, TIFF* input, TIFF* output){\n\ttsize_t written=0;\n\tttile_t i2=0;\n\ttsize_t streamlen=0;\n\tuint16 i=0;\n\tt2p_read_tiff_init(t2p, input);\n\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\tt2p->pdf_xrefoffsets= (uint32*) _TIFFmalloc(t2p->pdf_xrefcount * sizeof(uint32) );\n\tif(t2p->pdf_xrefoffsets==NULL){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"Can\'t allocate %u bytes of memory for t2p_write_pdf",\n\t\t\tt2p->pdf_xrefcount * sizeof(uint32) );\n\t\treturn(written);\n\t}\n\tt2p->pdf_xrefcount=0;\n\tt2p->pdf_catalog=1;\n\tt2p->pdf_info=2;\n\tt2p->pdf_pages=3;\n\twritten += t2p_write_pdf_header(t2p, output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_catalog=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_catalog(t2p, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_info=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_info(t2p, input, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_pages=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_pages(t2p, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tfor(t2p->pdf_page=0;t2p->pdf_page<t2p->tiff_pagecount;t2p->pdf_page++){\n\t\tt2p_read_tiff_data(t2p, input);\n\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_page(t2p->pdf_xrefcount, t2p, output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\twritten += t2p_write_pdf_stream_dict(0, t2p->pdf_xrefcount+1, output);\n\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\twritten += t2p_write_pdf_stream_start(output);\n\t\tstreamlen=written;\n\t\twritten += t2p_write_pdf_page_content_stream(t2p, output);\n\t\tstreamlen=written-streamlen;\n\t\twritten += t2p_write_pdf_stream_end(output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tif(t2p->tiff_transferfunctioncount != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_transfer(t2p, output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\tfor(i=0; i < t2p->tiff_transferfunctioncount; i++){\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\t\twritten += t2p_write_pdf_transfer_dict(t2p, output, i);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\t\tstreamlen=written;\n\t\t\t\twritten += t2p_write_pdf_transfer_stream(t2p, output, i);\n\t\t\t\tstreamlen=written-streamlen;\n\t\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t}\n\t\t}\n\t\tif( (t2p->pdf_colorspace & T2P_CS_PALETTE) != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\tt2p->pdf_palettecs=t2p->pdf_xrefcount;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_stream_dict(t2p->pdf_palettesize, 0, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\tstreamlen=written;\n\t\t\twritten += t2p_write_pdf_xobject_palettecs_stream(t2p, output);\n\t\t\tstreamlen=written-streamlen;\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t\tif( (t2p->pdf_colorspace & T2P_CS_ICCBASED) != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\tt2p->pdf_icccs=t2p->pdf_xrefcount;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_icccs_dict(t2p, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\tstreamlen=written;\n\t\t\twritten += t2p_write_pdf_xobject_icccs_stream(t2p, output);\n\t\t\tstreamlen=written-streamlen;\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t\tif(t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount !=0){\n\t\t\tfor(i2=0;i2<t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount;i2++){\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\t\twritten += t2p_write_pdf_xobject_stream_dict(\n\t\t\t\t\ti2+1,\n\t\t\t\t\tt2p,\n\t\t\t\t\toutput);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\t\tstreamlen=written;\n\t\t\t\tt2p_read_tiff_size_tile(t2p, input, i2);\n\t\t\t\twritten += t2p_readwrite_pdf_image_tile(t2p, input, output, i2);\n\t\t\t\tt2p_write_advance_directory(t2p, output);\n\t\t\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\t\t\tstreamlen=written-streamlen;\n\t\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t}\n\t\t} else {\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_stream_dict(\n\t\t\t\t0,\n\t\t\t\tt2p,\n\t\t\t\toutput);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\tstreamlen=written;\n\t\t\tt2p_read_tiff_size(t2p, input);\n\t\t\twritten += t2p_readwrite_pdf_image(t2p, input, output);\n\t\t\tt2p_write_advance_directory(t2p, output);\n\t\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\t\tstreamlen=written-streamlen;\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t}\n\tt2p->pdf_startxref = written;\n\twritten += t2p_write_pdf_xreftable(t2p, output);\n\twritten += t2p_write_pdf_trailer(t2p, output);\n\tt2p_disable(output);\n\treturn(written);\n}', 'void t2p_read_tiff_data(T2P* t2p, TIFF* input){\n\tint i=0;\n\tuint16* r;\n\tuint16* g;\n\tuint16* b;\n\tuint16* a;\n\tuint16 xuint16;\n\tuint16* xuint16p;\n\tfloat* xfloatp;\n\tt2p->pdf_transcode = T2P_TRANSCODE_ENCODE;\n\tt2p->pdf_sample = T2P_SAMPLE_NOTHING;\n t2p->pdf_switchdecode = t2p->pdf_colorspace_invert;\n\tTIFFSetDirectory(input, t2p->tiff_pages[t2p->pdf_page].page_directory);\n\tTIFFGetField(input, TIFFTAG_IMAGEWIDTH, &(t2p->tiff_width));\n\tif(t2p->tiff_width == 0){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"No support for %s with zero width",\n\t\t\tTIFFFileName(input)\t);\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn;\n\t}\n\tTIFFGetField(input, TIFFTAG_IMAGELENGTH, &(t2p->tiff_length));\n\tif(t2p->tiff_length == 0){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"No support for %s with zero length",\n\t\t\tTIFFFileName(input)\t);\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn;\n\t}\n if(TIFFGetField(input, TIFFTAG_COMPRESSION, &(t2p->tiff_compression)) == 0){\n TIFFError(\n TIFF2PDF_MODULE,\n "No support for %s with no compression tag",\n TIFFFileName(input) );\n t2p->t2p_error = T2P_ERR_ERROR;\n return;\n }\n if( TIFFIsCODECConfigured(t2p->tiff_compression) == 0){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"No support for %s with compression type %u: not configured",\n\t\t\tTIFFFileName(input),\n\t\t\tt2p->tiff_compression\n\t\t\t);\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn;\n\t}\n\tTIFFGetFieldDefaulted(input, TIFFTAG_BITSPERSAMPLE, &(t2p->tiff_bitspersample));\n\tswitch(t2p->tiff_bitspersample){\n\t\tcase 1:\n\t\tcase 2:\n\t\tcase 4:\n\t\tcase 8:\n\t\t\tbreak;\n\t\tcase 0:\n\t\t\tTIFFWarning(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"Image %s has 0 bits per sample, assuming 1",\n\t\t\t\tTIFFFileName(input));\n\t\t\tt2p->tiff_bitspersample=1;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tTIFFError(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"No support for %s with %u bits per sample",\n\t\t\t\tTIFFFileName(input),\n\t\t\t\tt2p->tiff_bitspersample);\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn;\n\t}\n\tTIFFGetFieldDefaulted(input, TIFFTAG_SAMPLESPERPIXEL, &(t2p->tiff_samplesperpixel));\n\tif(t2p->tiff_samplesperpixel>4){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"No support for %s with %u samples per pixel",\n\t\t\tTIFFFileName(input),\n\t\t\tt2p->tiff_samplesperpixel);\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn;\n\t}\n\tif(t2p->tiff_samplesperpixel==0){\n\t\tTIFFWarning(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"Image %s has 0 samples per pixel, assuming 1",\n\t\t\tTIFFFileName(input));\n\t\tt2p->tiff_samplesperpixel=1;\n\t}\n\tif(TIFFGetField(input, TIFFTAG_SAMPLEFORMAT, &xuint16) != 0 ){\n\t\tswitch(xuint16){\n\t\t\tcase 0:\n\t\t\tcase 1:\n\t\t\tcase 4:\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"No support for %s with sample format %u",\n\t\t\t\t\tTIFFFileName(input),\n\t\t\t\t\txuint16);\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t\tbreak;\n\t\t}\n\t}\n\tTIFFGetFieldDefaulted(input, TIFFTAG_FILLORDER, &(t2p->tiff_fillorder));\n if(TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &(t2p->tiff_photometric)) == 0){\n TIFFError(\n TIFF2PDF_MODULE,\n "No support for %s with no photometric interpretation tag",\n TIFFFileName(input) );\n t2p->t2p_error = T2P_ERR_ERROR;\n return;\n }\n\tswitch(t2p->tiff_photometric){\n\t\tcase PHOTOMETRIC_MINISWHITE:\n\t\tcase PHOTOMETRIC_MINISBLACK:\n\t\t\tif (t2p->tiff_bitspersample==1){\n\t\t\t\tt2p->pdf_colorspace=T2P_CS_BILEVEL;\n\t\t\t\tif(t2p->tiff_photometric==PHOTOMETRIC_MINISWHITE){\n\t\t\t\t\tt2p->pdf_switchdecode ^= 1;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tt2p->pdf_colorspace=T2P_CS_GRAY;\n\t\t\t\tif(t2p->tiff_photometric==PHOTOMETRIC_MINISWHITE){\n\t\t\t\t\tt2p->pdf_switchdecode ^= 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_RGB:\n\t\t\tt2p->pdf_colorspace=T2P_CS_RGB;\n\t\t\tif(t2p->tiff_samplesperpixel == 3){\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif(TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)){\n\t\t\t\tif(xuint16==1)\n\t\t\t\t\tgoto photometric_palette;\n\t\t\t}\n\t\t\tif(t2p->tiff_samplesperpixel > 3) {\n\t\t\t\tif(t2p->tiff_samplesperpixel == 4) {\n\t\t\t\t\tt2p->pdf_colorspace = T2P_CS_RGB;\n\t\t\t\t\tif(TIFFGetField(input,\n\t\t\t\t\t\t\tTIFFTAG_EXTRASAMPLES,\n\t\t\t\t\t\t\t&xuint16, &xuint16p)\n\t\t\t\t\t && xuint16 == 1) {\n\t\t\t\t\t\tif(xuint16p[0] == EXTRASAMPLE_ASSOCALPHA){\n\t\t\t\t\t\t\tt2p->pdf_sample=T2P_SAMPLE_RGBAA_TO_RGB;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(xuint16p[0] == EXTRASAMPLE_UNASSALPHA){\n\t\t\t\t\t\t\tt2p->pdf_sample=T2P_SAMPLE_RGBA_TO_RGB;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tTIFFWarning(\n\t\t\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t\t\t"RGB image %s has 4 samples per pixel, assuming RGBA",\n\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tt2p->pdf_colorspace=T2P_CS_CMYK;\n\t\t\t\t\tt2p->pdf_switchdecode ^= 1;\n\t\t\t\t\tTIFFWarning(\n\t\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t\t"RGB image %s has 4 samples per pixel, assuming inverse CMYK",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\tbreak;\n\t\t\t\t} else {\n\t\t\t\t\tTIFFError(\n\t\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t\t"No support for RGB image %s with %u samples per pixel",\n\t\t\t\t\t\tTIFFFileName(input),\n\t\t\t\t\t\tt2p->tiff_samplesperpixel);\n\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"No support for RGB image %s with %u samples per pixel",\n\t\t\t\t\tTIFFFileName(input),\n\t\t\t\t\tt2p->tiff_samplesperpixel);\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\tbreak;\n\t\t\t}\n\t\tcase PHOTOMETRIC_PALETTE:\n\t\t\tphotometric_palette:\n\t\t\tif(t2p->tiff_samplesperpixel!=1){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"No support for palettized image %s with not one sample per pixel",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tt2p->pdf_colorspace=T2P_CS_RGB | T2P_CS_PALETTE;\n\t\t\tt2p->pdf_palettesize=0x0001<<t2p->tiff_bitspersample;\n\t\t\tif(!TIFFGetField(input, TIFFTAG_COLORMAP, &r, &g, &b)){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Palettized image %s has no color map",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif(t2p->pdf_palette != NULL){\n\t\t\t\t_TIFFfree(t2p->pdf_palette);\n\t\t\t\tt2p->pdf_palette=NULL;\n\t\t\t}\n\t\t\tt2p->pdf_palette = (unsigned char*)\n\t\t\t\t_TIFFmalloc(t2p->pdf_palettesize*3);\n\t\t\tif(t2p->pdf_palette==NULL){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %u bytes of memory for t2p_read_tiff_image, %s",\n\t\t\t\t\tt2p->pdf_palettesize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor(i=0;i<t2p->pdf_palettesize;i++){\n\t\t\t\tt2p->pdf_palette[(i*3)] = (unsigned char) (r[i]>>8);\n\t\t\t\tt2p->pdf_palette[(i*3)+1]= (unsigned char) (g[i]>>8);\n\t\t\t\tt2p->pdf_palette[(i*3)+2]= (unsigned char) (b[i]>>8);\n\t\t\t}\n\t\t\tt2p->pdf_palettesize *= 3;\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_SEPARATED:\n\t\t\tif(TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)){\n\t\t\t\tif(xuint16==1){\n\t\t\t\t\t\tgoto photometric_palette_cmyk;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif( TIFFGetField(input, TIFFTAG_INKSET, &xuint16) ){\n\t\t\t\tif(xuint16 != INKSET_CMYK){\n\t\t\t\t\tTIFFError(\n\t\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t\t"No support for %s because its inkset is not CMYK",\n\t\t\t\t\t\tTIFFFileName(input) );\n\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(t2p->tiff_samplesperpixel==4){\n\t\t\t\tt2p->pdf_colorspace=T2P_CS_CMYK;\n\t\t\t} else {\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"No support for %s because it has %u samples per pixel",\n\t\t\t\t\tTIFFFileName(input),\n\t\t\t\t\tt2p->tiff_samplesperpixel);\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tbreak;\n\t\t\tphotometric_palette_cmyk:\n\t\t\tif(t2p->tiff_samplesperpixel!=1){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"No support for palettized CMYK image %s with not one sample per pixel",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tt2p->pdf_colorspace=T2P_CS_CMYK | T2P_CS_PALETTE;\n\t\t\tt2p->pdf_palettesize=0x0001<<t2p->tiff_bitspersample;\n\t\t\tif(!TIFFGetField(input, TIFFTAG_COLORMAP, &r, &g, &b, &a)){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Palettized image %s has no color map",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif(t2p->pdf_palette != NULL){\n\t\t\t\t_TIFFfree(t2p->pdf_palette);\n\t\t\t\tt2p->pdf_palette=NULL;\n\t\t\t}\n\t\t\tt2p->pdf_palette = (unsigned char*)\n\t\t\t\t_TIFFmalloc(t2p->pdf_palettesize*4);\n\t\t\tif(t2p->pdf_palette==NULL){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %u bytes of memory for t2p_read_tiff_image, %s",\n\t\t\t\t\tt2p->pdf_palettesize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor(i=0;i<t2p->pdf_palettesize;i++){\n\t\t\t\tt2p->pdf_palette[(i*4)] = (unsigned char) (r[i]>>8);\n\t\t\t\tt2p->pdf_palette[(i*4)+1]= (unsigned char) (g[i]>>8);\n\t\t\t\tt2p->pdf_palette[(i*4)+2]= (unsigned char) (b[i]>>8);\n\t\t\t\tt2p->pdf_palette[(i*4)+3]= (unsigned char) (a[i]>>8);\n\t\t\t}\n\t\t\tt2p->pdf_palettesize *= 4;\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_YCBCR:\n\t\t\tt2p->pdf_colorspace=T2P_CS_RGB;\n\t\t\tif(t2p->tiff_samplesperpixel==1){\n\t\t\t\tt2p->pdf_colorspace=T2P_CS_GRAY;\n\t\t\t\tt2p->tiff_photometric=PHOTOMETRIC_MINISBLACK;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tt2p->pdf_sample=T2P_SAMPLE_YCBCR_TO_RGB;\n#ifdef JPEG_SUPPORT\n\t\t\tif(t2p->pdf_defaultcompression==T2P_COMPRESS_JPEG){\n\t\t\t\tt2p->pdf_sample=T2P_SAMPLE_NOTHING;\n\t\t\t}\n#endif\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_CIELAB:\n\t\t\tt2p->pdf_labrange[0]= -127;\n\t\t\tt2p->pdf_labrange[1]= 127;\n\t\t\tt2p->pdf_labrange[2]= -127;\n\t\t\tt2p->pdf_labrange[3]= 127;\n\t\t\tt2p->pdf_sample=T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED;\n\t\t\tt2p->pdf_colorspace=T2P_CS_LAB;\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_ICCLAB:\n\t\t\tt2p->pdf_labrange[0]= 0;\n\t\t\tt2p->pdf_labrange[1]= 255;\n\t\t\tt2p->pdf_labrange[2]= 0;\n\t\t\tt2p->pdf_labrange[3]= 255;\n\t\t\tt2p->pdf_colorspace=T2P_CS_LAB;\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_ITULAB:\n\t\t\tt2p->pdf_labrange[0]=-85;\n\t\t\tt2p->pdf_labrange[1]=85;\n\t\t\tt2p->pdf_labrange[2]=-75;\n\t\t\tt2p->pdf_labrange[3]=124;\n\t\t\tt2p->pdf_sample=T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED;\n\t\t\tt2p->pdf_colorspace=T2P_CS_LAB;\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_LOGL:\n\t\tcase PHOTOMETRIC_LOGLUV:\n\t\t\tTIFFError(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"No support for %s with photometric interpretation LogL/LogLuv",\n\t\t\t\tTIFFFileName(input));\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn;\n\t\tdefault:\n\t\t\tTIFFError(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"No support for %s with photometric interpretation %u",\n\t\t\t\tTIFFFileName(input),\n\t\t\t\tt2p->tiff_photometric);\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn;\n\t}\n\tif(TIFFGetField(input, TIFFTAG_PLANARCONFIG, &(t2p->tiff_planar))){\n\t\tswitch(t2p->tiff_planar){\n\t\t\tcase 0:\n\t\t\t\tTIFFWarning(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Image %s has planar configuration 0, assuming 1",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->tiff_planar=PLANARCONFIG_CONTIG;\n\t\t\tcase PLANARCONFIG_CONTIG:\n\t\t\t\tbreak;\n\t\t\tcase PLANARCONFIG_SEPARATE:\n\t\t\t\tt2p->pdf_sample=T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG;\n\t\t\t\tif(t2p->tiff_bitspersample!=8){\n\t\t\t\t\tTIFFError(\n\t\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t\t"No support for %s with separated planar configuration and %u bits per sample",\n\t\t\t\t\t\tTIFFFileName(input),\n\t\t\t\t\t\tt2p->tiff_bitspersample);\n\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"No support for %s with planar configuration %u",\n\t\t\t\t\tTIFFFileName(input),\n\t\t\t\t\tt2p->tiff_planar);\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t}\n\t}\n TIFFGetFieldDefaulted(input, TIFFTAG_ORIENTATION,\n &(t2p->tiff_orientation));\n if(t2p->tiff_orientation>8){\n TIFFWarning(TIFF2PDF_MODULE,\n "Image %s has orientation %u, assuming 0",\n TIFFFileName(input), t2p->tiff_orientation);\n t2p->tiff_orientation=0;\n }\n if(TIFFGetField(input, TIFFTAG_XRESOLUTION, &(t2p->tiff_xres) ) == 0){\n t2p->tiff_xres=0.0;\n }\n if(TIFFGetField(input, TIFFTAG_YRESOLUTION, &(t2p->tiff_yres) ) == 0){\n t2p->tiff_yres=0.0;\n }\n\tTIFFGetFieldDefaulted(input, TIFFTAG_RESOLUTIONUNIT,\n\t\t\t &(t2p->tiff_resunit));\n\tif(t2p->tiff_resunit == RESUNIT_CENTIMETER) {\n\t\tt2p->tiff_xres *= 2.54F;\n\t\tt2p->tiff_yres *= 2.54F;\n\t} else if (t2p->tiff_resunit != RESUNIT_INCH\n\t\t && t2p->pdf_centimeters != 0) {\n\t\tt2p->tiff_xres *= 2.54F;\n\t\tt2p->tiff_yres *= 2.54F;\n\t}\n\tt2p_compose_pdf_page(t2p);\n\tt2p->pdf_transcode = T2P_TRANSCODE_ENCODE;\n\tif(t2p->pdf_nopassthrough==0){\n#ifdef CCITT_SUPPORT\n\t\tif(t2p->tiff_compression==COMPRESSION_CCITTFAX4\n\t\t\t){\n\t\t\tif(TIFFIsTiled(input) || (TIFFNumberOfStrips(input)==1) ){\n\t\t\t\tt2p->pdf_transcode = T2P_TRANSCODE_RAW;\n\t\t\t\tt2p->pdf_compression=T2P_COMPRESS_G4;\n\t\t\t}\n\t\t}\n#endif\n#ifdef ZIP_SUPPORT\n\t\tif(t2p->tiff_compression== COMPRESSION_ADOBE_DEFLATE\n\t\t\t|| t2p->tiff_compression==COMPRESSION_DEFLATE){\n\t\t\tif(TIFFIsTiled(input) || (TIFFNumberOfStrips(input)==1) ){\n\t\t\t\tt2p->pdf_transcode = T2P_TRANSCODE_RAW;\n\t\t\t\tt2p->pdf_compression=T2P_COMPRESS_ZIP;\n\t\t\t}\n\t\t}\n#endif\n#ifdef OJPEG_SUPPORT\n\t\tif(t2p->tiff_compression==COMPRESSION_OJPEG){\n\t\t\tt2p->pdf_transcode = T2P_TRANSCODE_RAW;\n\t\t\tt2p->pdf_compression=T2P_COMPRESS_JPEG;\n\t\t\tt2p_process_ojpeg_tables(t2p, input);\n\t\t}\n#endif\n#ifdef JPEG_SUPPORT\n\t\tif(t2p->tiff_compression==COMPRESSION_JPEG){\n\t\t\tt2p->pdf_transcode = T2P_TRANSCODE_RAW;\n\t\t\tt2p->pdf_compression=T2P_COMPRESS_JPEG;\n\t\t}\n#endif\n\t\t(void)0;\n\t}\n\tif(t2p->pdf_transcode!=T2P_TRANSCODE_RAW){\n\t\tt2p->pdf_compression = t2p->pdf_defaultcompression;\n\t}\n#ifdef JPEG_SUPPORT\n\tif(t2p->pdf_defaultcompression==T2P_COMPRESS_JPEG){\n\t\tif(t2p->pdf_colorspace & T2P_CS_PALETTE){\n\t\t\tt2p->pdf_sample|=T2P_SAMPLE_REALIZE_PALETTE;\n\t\t\tt2p->pdf_colorspace ^= T2P_CS_PALETTE;\n\t\t\tt2p->tiff_pages[t2p->pdf_page].page_extra--;\n\t\t}\n\t}\n\tif(t2p->tiff_compression==COMPRESSION_JPEG){\n\t\tif(t2p->tiff_planar==PLANARCONFIG_SEPARATE){\n\t\t\tTIFFError(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"No support for %s with JPEG compression and separated planar configuration",\n\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\treturn;\n\t\t}\n\t}\n#endif\n#ifdef OJPEG_SUPPORT\n\tif(t2p->tiff_compression==COMPRESSION_OJPEG){\n\t\tif(t2p->tiff_planar==PLANARCONFIG_SEPARATE){\n\t\t\tTIFFError(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"No support for %s with OJPEG compression and separated planar configuration",\n\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\treturn;\n\t\t}\n\t}\n#endif\n\tif(t2p->pdf_sample & T2P_SAMPLE_REALIZE_PALETTE){\n\t\tif(t2p->pdf_colorspace & T2P_CS_CMYK){\n\t\t\tt2p->tiff_samplesperpixel=4;\n\t\t\tt2p->tiff_photometric=PHOTOMETRIC_SEPARATED;\n\t\t} else {\n\t\t\tt2p->tiff_samplesperpixel=3;\n\t\t\tt2p->tiff_photometric=PHOTOMETRIC_RGB;\n\t\t}\n\t}\n\tif (TIFFGetField(input, TIFFTAG_TRANSFERFUNCTION,\n\t\t\t &(t2p->tiff_transferfunction[0]),\n\t\t\t &(t2p->tiff_transferfunction[1]),\n\t\t\t &(t2p->tiff_transferfunction[2]))) {\n\t\tif(t2p->tiff_transferfunction[1] !=\n\t\t t2p->tiff_transferfunction[0]) {\n\t\t\tt2p->tiff_transferfunctioncount=3;\n\t\t} else {\n\t\t\tt2p->tiff_transferfunctioncount=1;\n\t\t}\n\t} else {\n\t\tt2p->tiff_transferfunctioncount=0;\n\t}\n\tif(TIFFGetField(input, TIFFTAG_WHITEPOINT, &xfloatp)!=0){\n\t\tt2p->tiff_whitechromaticities[0]=xfloatp[0];\n\t\tt2p->tiff_whitechromaticities[1]=xfloatp[1];\n\t\tif(t2p->pdf_colorspace & T2P_CS_GRAY){\n\t\t\tt2p->pdf_colorspace |= T2P_CS_CALGRAY;\n\t\t}\n\t\tif(t2p->pdf_colorspace & T2P_CS_RGB){\n\t\t\tt2p->pdf_colorspace |= T2P_CS_CALRGB;\n\t\t}\n\t}\n\tif(TIFFGetField(input, TIFFTAG_PRIMARYCHROMATICITIES, &xfloatp)!=0){\n\t\tt2p->tiff_primarychromaticities[0]=xfloatp[0];\n\t\tt2p->tiff_primarychromaticities[1]=xfloatp[1];\n\t\tt2p->tiff_primarychromaticities[2]=xfloatp[2];\n\t\tt2p->tiff_primarychromaticities[3]=xfloatp[3];\n\t\tt2p->tiff_primarychromaticities[4]=xfloatp[4];\n\t\tt2p->tiff_primarychromaticities[5]=xfloatp[5];\n\t\tif(t2p->pdf_colorspace & T2P_CS_RGB){\n\t\t\tt2p->pdf_colorspace |= T2P_CS_CALRGB;\n\t\t}\n\t}\n\tif(t2p->pdf_colorspace & T2P_CS_LAB){\n\t\tif(TIFFGetField(input, TIFFTAG_WHITEPOINT, &xfloatp) != 0){\n\t\t\tt2p->tiff_whitechromaticities[0]=xfloatp[0];\n\t\t\tt2p->tiff_whitechromaticities[1]=xfloatp[1];\n\t\t} else {\n\t\t\tt2p->tiff_whitechromaticities[0]=0.3457F;\n\t\t\tt2p->tiff_whitechromaticities[1]=0.3585F;\n\t\t}\n\t}\n\tif(TIFFGetField(input,\n\t\tTIFFTAG_ICCPROFILE,\n\t\t&(t2p->tiff_iccprofilelength),\n\t\t&(t2p->tiff_iccprofile))!=0){\n\t\tt2p->pdf_colorspace |= T2P_CS_ICCBASED;\n\t} else {\n\t\tt2p->tiff_iccprofilelength=0;\n\t\tt2p->tiff_iccprofile=NULL;\n\t}\n#ifdef CCITT_SUPPORT\n\tif( t2p->tiff_bitspersample==1 &&\n\t\tt2p->tiff_samplesperpixel==1){\n\t\tt2p->pdf_compression = T2P_COMPRESS_G4;\n\t}\n#endif\n\treturn;\n}', 'int\nTIFFSetDirectory(TIFF* tif, uint16 dirn)\n{\n\tuint64 nextdir;\n\tuint16 n;\n\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\tnextdir = tif->tif_header.classic.tiff_diroff;\n\telse\n\t\tnextdir = tif->tif_header.big.tiff_diroff;\n\tfor (n = dirn; n > 0 && nextdir != 0; n--)\n\t\tif (!TIFFAdvanceDirectory(tif, &nextdir, NULL))\n\t\t\treturn (0);\n\ttif->tif_nextdiroff = nextdir;\n\ttif->tif_curdir = (dirn - n) - 1;\n\ttif->tif_dirnumber = 0;\n\treturn (TIFFReadDirectory(tif));\n}', 'void t2p_read_tiff_size(T2P* t2p, TIFF* input){\n\tuint64* sbc=NULL;\n#if defined(JPEG_SUPPORT) || defined (OJPEG_SUPPORT)\n\tunsigned char* jpt=NULL;\n\ttstrip_t i=0;\n\ttstrip_t stripcount=0;\n#endif\n#ifdef OJPEG_SUPPORT\n tsize_t k = 0;\n#endif\n\tif(t2p->pdf_transcode == T2P_TRANSCODE_RAW){\n#ifdef CCITT_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_G4 ){\n\t\t\tTIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);\n\t\t\tt2p->tiff_datasize=(tmsize_t)sbc[0];\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef ZIP_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_ZIP){\n\t\t\tTIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);\n\t\t\tt2p->tiff_datasize=(tmsize_t)sbc[0];\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef OJPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_OJPEG){\n\t\t\tif(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tk += sbc[i];\n\t\t\t}\n\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGIFOFFSET, &(t2p->tiff_dataoffset))){\n\t\t\t\tif(t2p->tiff_dataoffset != 0){\n\t\t\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGIFBYTECOUNT, &(t2p->tiff_datasize))!=0){\n\t\t\t\t\t\tif(t2p->tiff_datasize < k) {\n\t\t\t\t\t\t\tt2p->pdf_ojpegiflength=t2p->tiff_datasize;\n\t\t\t\t\t\t\tt2p->tiff_datasize+=k;\n\t\t\t\t\t\t\tt2p->tiff_datasize+=6;\n\t\t\t\t\t\t\tt2p->tiff_datasize+=2*stripcount;\n\t\t\t\t\t\t\tTIFFWarning(TIFF2PDF_MODULE,\n\t\t\t\t\t\t\t\t"Input file %s has short JPEG interchange file byte count",\n\t\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}else {\n\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t\t\t"Input file %s missing field: TIFFTAG_JPEGIFBYTECOUNT",\n\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tt2p->tiff_datasize+=k;\n\t\t\tt2p->tiff_datasize+=2*stripcount;\n\t\t\tt2p->tiff_datasize+=2048;\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef JPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_JPEG) {\n\t\t\tuint32 count = 0;\n\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0 ){\n\t\t\t\tif(count > 4){\n\t\t\t\t\tt2p->tiff_datasize += count;\n\t\t\t\t\tt2p->tiff_datasize -= 2;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tt2p->tiff_datasize = 2;\n\t\t\t}\n\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\tif(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tt2p->tiff_datasize += sbc[i];\n\t\t\t\tt2p->tiff_datasize -=4;\n\t\t\t}\n\t\t\tt2p->tiff_datasize +=2;\n\t\t}\n#endif\n\t\t(void) 0;\n\t}\n\tt2p->tiff_datasize=TIFFScanlineSize(input) * t2p->tiff_length;\n\tif(t2p->tiff_planar==PLANARCONFIG_SEPARATE){\n\t\tt2p->tiff_datasize*= t2p->tiff_samplesperpixel;\n\t}\n\treturn;\n}', 'uint32\nTIFFNumberOfStrips(TIFF* tif)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 nstrips;\n\tnstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :\n\t TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\tnstrips = multiply_32(tif, nstrips, (uint32)td->td_samplesperpixel,\n\t\t "TIFFNumberOfStrips");\n\treturn (nstrips);\n}']
35,813
0
https://github.com/libav/libav/blob/150adea6da24e5342584b403dc0f901aa79f53c8/libavformat/apetag.c/#L147
int64_t ff_ape_parse_tag(AVFormatContext *s) { AVIOContext *pb = s->pb; int file_size = avio_size(pb); uint32_t val, fields, tag_bytes; uint8_t buf[8]; int64_t tag_start; int i; if (file_size < APE_TAG_FOOTER_BYTES) return 0; avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET); avio_read(pb, buf, 8); if (strncmp(buf, "APETAGEX", 8)) { return 0; } val = avio_rl32(pb); if (val > APE_TAG_VERSION) { av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION); return 0; } tag_bytes = avio_rl32(pb); if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) { av_log(s, AV_LOG_ERROR, "Tag size is way too big\n"); return 0; } tag_start = file_size - tag_bytes - APE_TAG_FOOTER_BYTES; if (tag_start < 0) { av_log(s, AV_LOG_ERROR, "Invalid tag size %u.\n", tag_bytes); return 0; } fields = avio_rl32(pb); if (fields > 65536) { av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields); return 0; } val = avio_rl32(pb); if (val & APE_TAG_FLAG_IS_HEADER) { av_log(s, AV_LOG_ERROR, "APE Tag is a header\n"); return 0; } avio_seek(pb, file_size - tag_bytes, SEEK_SET); for (i=0; i<fields; i++) if (ape_tag_read_field(s) < 0) break; return tag_start; }
['int64_t ff_ape_parse_tag(AVFormatContext *s)\n{\n AVIOContext *pb = s->pb;\n int file_size = avio_size(pb);\n uint32_t val, fields, tag_bytes;\n uint8_t buf[8];\n int64_t tag_start;\n int i;\n if (file_size < APE_TAG_FOOTER_BYTES)\n return 0;\n avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);\n avio_read(pb, buf, 8);\n if (strncmp(buf, "APETAGEX", 8)) {\n return 0;\n }\n val = avio_rl32(pb);\n if (val > APE_TAG_VERSION) {\n av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\\n", APE_TAG_VERSION);\n return 0;\n }\n tag_bytes = avio_rl32(pb);\n if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {\n av_log(s, AV_LOG_ERROR, "Tag size is way too big\\n");\n return 0;\n }\n tag_start = file_size - tag_bytes - APE_TAG_FOOTER_BYTES;\n if (tag_start < 0) {\n av_log(s, AV_LOG_ERROR, "Invalid tag size %u.\\n", tag_bytes);\n return 0;\n }\n fields = avio_rl32(pb);\n if (fields > 65536) {\n av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\\n", fields);\n return 0;\n }\n val = avio_rl32(pb);\n if (val & APE_TAG_FLAG_IS_HEADER) {\n av_log(s, AV_LOG_ERROR, "APE Tag is a header\\n");\n return 0;\n }\n avio_seek(pb, file_size - tag_bytes, SEEK_SET);\n for (i=0; i<fields; i++)\n if (ape_tag_read_field(s) < 0) break;\n return tag_start;\n}', 'int64_t avio_size(AVIOContext *s)\n{\n int64_t size;\n if(!s)\n return AVERROR(EINVAL);\n if (!s->seek)\n return AVERROR(ENOSYS);\n size = s->seek(s->opaque, 0, AVSEEK_SIZE);\n if(size<0){\n if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)\n return size;\n size++;\n s->seek(s->opaque, s->pos, SEEK_SET);\n }\n return size;\n}', 'unsigned int avio_rl32(AVIOContext *s)\n{\n unsigned int val;\n val = avio_rl16(s);\n val |= avio_rl16(s) << 16;\n return val;\n}', 'unsigned int avio_rl16(AVIOContext *s)\n{\n unsigned int val;\n val = avio_r8(s);\n val |= avio_r8(s) << 8;\n return val;\n}', 'int avio_r8(AVIOContext *s)\n{\n if (s->buf_ptr >= s->buf_end)\n fill_buffer(s);\n if (s->buf_ptr < s->buf_end)\n return *s->buf_ptr++;\n return 0;\n}']
35,814
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L354
static void pred4x4_horizontal_up_rv40_c(uint8_t *src, uint8_t *topright, int stride){ LOAD_LEFT_EDGE LOAD_DOWN_LEFT_EDGE LOAD_TOP_EDGE LOAD_TOP_RIGHT_EDGE src[0+0*stride]=(t1 + 2*t2 + t3 + 2*l0 + 2*l1 + 4)>>3; src[1+0*stride]=(t2 + 2*t3 + t4 + l0 + 2*l1 + l2 + 4)>>3; src[2+0*stride]= src[0+1*stride]=(t3 + 2*t4 + t5 + 2*l1 + 2*l2 + 4)>>3; src[3+0*stride]= src[1+1*stride]=(t4 + 2*t5 + t6 + l1 + 2*l2 + l3 + 4)>>3; src[2+1*stride]= src[0+2*stride]=(t5 + 2*t6 + t7 + 2*l2 + 2*l3 + 4)>>3; src[3+1*stride]= src[1+2*stride]=(t6 + 3*t7 + l2 + 3*l3 + 4)>>3; src[3+2*stride]= src[1+3*stride]=(l3 + 2*l4 + l5 + 2)>>2; src[0+3*stride]= src[2+2*stride]=(t6 + t7 + l3 + l4 + 2)>>2; src[2+3*stride]=(l4 + l5 + 1)>>1; src[3+3*stride]=(l4 + 2*l5 + l6 + 2)>>2; }
['static void pred4x4_horizontal_up_rv40_c(uint8_t *src, uint8_t *topright, int stride){\n LOAD_LEFT_EDGE\n LOAD_DOWN_LEFT_EDGE\n LOAD_TOP_EDGE\n LOAD_TOP_RIGHT_EDGE\n src[0+0*stride]=(t1 + 2*t2 + t3 + 2*l0 + 2*l1 + 4)>>3;\n src[1+0*stride]=(t2 + 2*t3 + t4 + l0 + 2*l1 + l2 + 4)>>3;\n src[2+0*stride]=\n src[0+1*stride]=(t3 + 2*t4 + t5 + 2*l1 + 2*l2 + 4)>>3;\n src[3+0*stride]=\n src[1+1*stride]=(t4 + 2*t5 + t6 + l1 + 2*l2 + l3 + 4)>>3;\n src[2+1*stride]=\n src[0+2*stride]=(t5 + 2*t6 + t7 + 2*l2 + 2*l3 + 4)>>3;\n src[3+1*stride]=\n src[1+2*stride]=(t6 + 3*t7 + l2 + 3*l3 + 4)>>3;\n src[3+2*stride]=\n src[1+3*stride]=(l3 + 2*l4 + l5 + 2)>>2;\n src[0+3*stride]=\n src[2+2*stride]=(t6 + t7 + l3 + l4 + 2)>>2;\n src[2+3*stride]=(l4 + l5 + 1)>>1;\n src[3+3*stride]=(l4 + 2*l5 + l6 + 2)>>2;\n}']
35,815
0
https://github.com/libav/libav/blob/9e93001b6135a23fe4e200196c08fb4fbffed6fc/libavcodec/bitstream.h/#L138
static inline uint64_t get_val(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1); bc->bits >>= n; #else uint64_t ret = bc->bits >> (64 - n); bc->bits <<= n; #endif bc->bits_left -= n; return ret; }
['int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, BitstreamContext *bc,\n int width, const char *name, uint32_t *write_to,\n uint32_t range_min, uint32_t range_max)\n{\n uint32_t value;\n int position;\n av_assert0(width <= 32);\n if (ctx->trace_enable)\n position = bitstream_tell(bc);\n value = bitstream_read(bc, width);\n if (ctx->trace_enable) {\n char bits[33];\n int i;\n for (i = 0; i < width; i++)\n bits[i] = value >> (width - i - 1) & 1 ? \'1\' : \'0\';\n bits[i] = 0;\n ff_cbs_trace_syntax_element(ctx, position, name, bits, value);\n }\n if (value < range_min || value > range_max) {\n av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "\n "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\\n",\n name, value, range_min, range_max);\n return AVERROR_INVALIDDATA;\n }\n *write_to = value;\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
35,816
0
https://github.com/openssl/openssl/blob/6c2c3e9ba9146ef8c9b1fd2b660357b657706969/apps/verify.c/#L126
int MAIN(int argc, char **argv) { int i,ret=1; int purpose = -1; char *CApath=NULL,*CAfile=NULL; char *untfile = NULL; STACK_OF(X509) *untrusted = NULL; X509_STORE *cert_ctx=NULL; X509_LOOKUP *lookup=NULL; cert_ctx=X509_STORE_new(); if (cert_ctx == NULL) goto end; X509_STORE_set_verify_cb_func(cert_ctx,cb); ERR_load_crypto_strings(); apps_startup(); if (bio_err == NULL) if ((bio_err=BIO_new(BIO_s_file())) != NULL) BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); argc--; argv++; for (;;) { if (argc >= 1) { if (strcmp(*argv,"-CApath") == 0) { if (argc-- < 1) goto end; CApath= *(++argv); } else if (strcmp(*argv,"-CAfile") == 0) { if (argc-- < 1) goto end; CAfile= *(++argv); } else if (strcmp(*argv,"-purpose") == 0) { X509_PURPOSE *xptmp; if (argc-- < 1) goto end; i = X509_PURPOSE_get_by_sname(*(++argv)); if(i < 0) { BIO_printf(bio_err, "unrecognised purpose\n"); goto end; } xptmp = X509_PURPOSE_iget(i); purpose = X509_PURPOSE_get_id(xptmp); } else if (strcmp(*argv,"-untrusted") == 0) { if (argc-- < 1) goto end; untfile= *(++argv); } else if (strcmp(*argv,"-help") == 0) goto end; else if (strcmp(*argv,"-verbose") == 0) v_verbose=1; else if (argv[0][0] == '-') goto end; else break; argc--; argv++; } else break; } lookup=X509_STORE_add_lookup(cert_ctx,X509_LOOKUP_file()); if (lookup == NULL) abort(); if (CAfile) { i=X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM); if(!i) { BIO_printf(bio_err, "Error loading file %s\n", CAfile); ERR_print_errors(bio_err); goto end; } } else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT); lookup=X509_STORE_add_lookup(cert_ctx,X509_LOOKUP_hash_dir()); if (lookup == NULL) abort(); if (CApath) { i=X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM); if(!i) { BIO_printf(bio_err, "Error loading directory %s\n", CApath); ERR_print_errors(bio_err); goto end; } } else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT); ERR_clear_error(); if(untfile) { if(!(untrusted = load_untrusted(untfile))) { BIO_printf(bio_err, "Error loading untrusted file %s\n", untfile); ERR_print_errors(bio_err); goto end; } } if (argc < 1) check(cert_ctx, NULL, untrusted, purpose); else for (i=0; i<argc; i++) check(cert_ctx,argv[i], untrusted, purpose); ret=0; end: if (ret == 1) { BIO_printf(bio_err,"usage: verify [-verbose] [-CApath path] [-CAfile file] cert1 cert2 ...\n"); BIO_printf(bio_err,"recognised usages:\n"); for(i = 0; i < X509_PURPOSE_get_count(); i++) { X509_PURPOSE *ptmp; ptmp = X509_PURPOSE_iget(i); BIO_printf(bio_err, "\t%-10s\t%s\n", X509_PURPOSE_iget_sname(ptmp), X509_PURPOSE_iget_name(ptmp)); } } if (cert_ctx != NULL) X509_STORE_free(cert_ctx); sk_X509_pop_free(untrusted, X509_free); EXIT(ret); }
['int MAIN(int argc, char **argv)\n\t{\n\tint i,ret=1;\n\tint purpose = -1;\n\tchar *CApath=NULL,*CAfile=NULL;\n\tchar *untfile = NULL;\n\tSTACK_OF(X509) *untrusted = NULL;\n\tX509_STORE *cert_ctx=NULL;\n\tX509_LOOKUP *lookup=NULL;\n\tcert_ctx=X509_STORE_new();\n\tif (cert_ctx == NULL) goto end;\n\tX509_STORE_set_verify_cb_func(cert_ctx,cb);\n\tERR_load_crypto_strings();\n\tapps_startup();\n\tif (bio_err == NULL)\n\t\tif ((bio_err=BIO_new(BIO_s_file())) != NULL)\n\t\t\tBIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);\n\targc--;\n\targv++;\n\tfor (;;)\n\t\t{\n\t\tif (argc >= 1)\n\t\t\t{\n\t\t\tif (strcmp(*argv,"-CApath") == 0)\n\t\t\t\t{\n\t\t\t\tif (argc-- < 1) goto end;\n\t\t\t\tCApath= *(++argv);\n\t\t\t\t}\n\t\t\telse if (strcmp(*argv,"-CAfile") == 0)\n\t\t\t\t{\n\t\t\t\tif (argc-- < 1) goto end;\n\t\t\t\tCAfile= *(++argv);\n\t\t\t\t}\n\t\t\telse if (strcmp(*argv,"-purpose") == 0)\n\t\t\t\t{\n\t\t\t\tX509_PURPOSE *xptmp;\n\t\t\t\tif (argc-- < 1) goto end;\n\t\t\t\ti = X509_PURPOSE_get_by_sname(*(++argv));\n\t\t\t\tif(i < 0)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err, "unrecognised purpose\\n");\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\txptmp = X509_PURPOSE_iget(i);\n\t\t\t\tpurpose = X509_PURPOSE_get_id(xptmp);\n\t\t\t\t}\n\t\t\telse if (strcmp(*argv,"-untrusted") == 0)\n\t\t\t\t{\n\t\t\t\tif (argc-- < 1) goto end;\n\t\t\t\tuntfile= *(++argv);\n\t\t\t\t}\n\t\t\telse if (strcmp(*argv,"-help") == 0)\n\t\t\t\tgoto end;\n\t\t\telse if (strcmp(*argv,"-verbose") == 0)\n\t\t\t\tv_verbose=1;\n\t\t\telse if (argv[0][0] == \'-\')\n\t\t\t\tgoto end;\n\t\t\telse\n\t\t\t\tbreak;\n\t\t\targc--;\n\t\t\targv++;\n\t\t\t}\n\t\telse\n\t\t\tbreak;\n\t\t}\n\tlookup=X509_STORE_add_lookup(cert_ctx,X509_LOOKUP_file());\n\tif (lookup == NULL) abort();\n\tif (CAfile) {\n\t\ti=X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM);\n\t\tif(!i) {\n\t\t\tBIO_printf(bio_err, "Error loading file %s\\n", CAfile);\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t}\n\t} else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);\n\tlookup=X509_STORE_add_lookup(cert_ctx,X509_LOOKUP_hash_dir());\n\tif (lookup == NULL) abort();\n\tif (CApath) {\n\t\ti=X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM);\n\t\tif(!i) {\n\t\t\tBIO_printf(bio_err, "Error loading directory %s\\n", CApath);\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t}\n\t} else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);\n\tERR_clear_error();\n\tif(untfile) {\n\t\tif(!(untrusted = load_untrusted(untfile))) {\n\t\t\tBIO_printf(bio_err, "Error loading untrusted file %s\\n", untfile);\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t}\n\t}\n\tif (argc < 1) check(cert_ctx, NULL, untrusted, purpose);\n\telse\n\t\tfor (i=0; i<argc; i++)\n\t\t\tcheck(cert_ctx,argv[i], untrusted, purpose);\n\tret=0;\nend:\n\tif (ret == 1) {\n\t\tBIO_printf(bio_err,"usage: verify [-verbose] [-CApath path] [-CAfile file] cert1 cert2 ...\\n");\n\t\tBIO_printf(bio_err,"recognised usages:\\n");\n\t\tfor(i = 0; i < X509_PURPOSE_get_count(); i++) {\n\t\t\tX509_PURPOSE *ptmp;\n\t\t\tptmp = X509_PURPOSE_iget(i);\n\t\t\tBIO_printf(bio_err, "\\t%-10s\\t%s\\n", X509_PURPOSE_iget_sname(ptmp),\n\t\t\t\t\t\t\t\tX509_PURPOSE_iget_name(ptmp));\n\t\t}\n\t}\n\tif (cert_ctx != NULL) X509_STORE_free(cert_ctx);\n\tsk_X509_pop_free(untrusted, X509_free);\n\tEXIT(ret);\n\t}', 'BIO_METHOD *BIO_s_file(void)\n\t{\n\treturn(&methods_filep);\n\t}', 'BIO *BIO_new(BIO_METHOD *method)\n\t{\n\tBIO *ret=NULL;\n\tret=(BIO *)Malloc(sizeof(BIO));\n\tif (ret == NULL)\n\t\t{\n\t\tBIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tif (!BIO_set(ret,method))\n\t\t{\n\t\tFree(ret);\n\t\tret=NULL;\n\t\t}\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, char *file, int line)\n\t{\n\tchar *ret = NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_func(num);\n#ifdef LEVITTE_DEBUG\n\tfprintf(stderr, "LEVITTE_DEBUG: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'X509_PURPOSE * X509_PURPOSE_iget(int idx)\n{\n\tif(idx < 0) return NULL;\n\tif(idx < X509_PURPOSE_COUNT) return xstandard + idx;\n\treturn sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);\n}', 'IMPLEMENT_STACK_OF(X509_PURPOSE)', 'char *sk_value(STACK *st, int i)\n{\n\tif(st == NULL) return NULL;\n\treturn st->data[i];\n}', 'int X509_PURPOSE_get_id(X509_PURPOSE *xp)\n{\n\treturn xp->purpose;\n}']
35,817
0
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/bn/bn_shift.c/#L110
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { int i, nw, lb, rb; BN_ULONG *t, *f; BN_ULONG l; bn_check_top(r); bn_check_top(a); if (n < 0) { BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT); return 0; } r->neg = a->neg; nw = n / BN_BITS2; if (bn_wexpand(r, a->top + nw + 1) == NULL) return (0); lb = n % BN_BITS2; rb = BN_BITS2 - lb; f = a->d; t = r->d; t[a->top + nw] = 0; if (lb == 0) for (i = a->top - 1; i >= 0; i--) t[nw + i] = f[i]; else for (i = a->top - 1; i >= 0; i--) { l = f[i]; t[nw + i + 1] |= (l >> rb) & BN_MASK2; t[nw + i] = (l << lb) & BN_MASK2; } memset(t, 0, sizeof(*t) * nw); r->top = a->top + nw + 1; bn_correct_top(r); bn_check_top(r); return (1); }
['EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params)\n{\n int ok = 0, tmp;\n EC_GROUP *ret = NULL;\n BIGNUM *p = NULL, *a = NULL, *b = NULL;\n EC_POINT *point = NULL;\n long field_bits;\n if (!params->fieldID || !params->fieldID->fieldType ||\n !params->fieldID->p.ptr) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n if (!params->curve || !params->curve->a ||\n !params->curve->a->data || !params->curve->b ||\n !params->curve->b->data) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);\n if (a == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);\n goto err;\n }\n b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);\n if (b == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);\n goto err;\n }\n tmp = OBJ_obj2nid(params->fieldID->fieldType);\n if (tmp == NID_X9_62_characteristic_two_field)\n#ifdef OPENSSL_NO_EC2M\n {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED);\n goto err;\n }\n#else\n {\n X9_62_CHARACTERISTIC_TWO *char_two;\n char_two = params->fieldID->p.char_two;\n field_bits = char_two->m;\n if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);\n goto err;\n }\n if ((p = BN_new()) == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n tmp = OBJ_obj2nid(char_two->type);\n if (tmp == NID_X9_62_tpBasis) {\n long tmp_long;\n if (!char_two->p.tpBasis) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);\n if (!(char_two->m > tmp_long && tmp_long > 0)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,\n EC_R_INVALID_TRINOMIAL_BASIS);\n goto err;\n }\n if (!BN_set_bit(p, (int)char_two->m))\n goto err;\n if (!BN_set_bit(p, (int)tmp_long))\n goto err;\n if (!BN_set_bit(p, 0))\n goto err;\n } else if (tmp == NID_X9_62_ppBasis) {\n X9_62_PENTANOMIAL *penta;\n penta = char_two->p.ppBasis;\n if (!penta) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n if (!\n (char_two->m > penta->k3 && penta->k3 > penta->k2\n && penta->k2 > penta->k1 && penta->k1 > 0)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,\n EC_R_INVALID_PENTANOMIAL_BASIS);\n goto err;\n }\n if (!BN_set_bit(p, (int)char_two->m))\n goto err;\n if (!BN_set_bit(p, (int)penta->k1))\n goto err;\n if (!BN_set_bit(p, (int)penta->k2))\n goto err;\n if (!BN_set_bit(p, (int)penta->k3))\n goto err;\n if (!BN_set_bit(p, 0))\n goto err;\n } else if (tmp == NID_X9_62_onBasis) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);\n goto err;\n } else {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);\n }\n#endif\n else if (tmp == NID_X9_62_prime_field) {\n if (!params->fieldID->p.prime) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);\n if (p == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(p) || BN_is_zero(p)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);\n goto err;\n }\n field_bits = BN_num_bits(p);\n if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);\n goto err;\n }\n ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);\n } else {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);\n goto err;\n }\n if (ret == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);\n goto err;\n }\n if (params->curve->seed != NULL) {\n OPENSSL_free(ret->seed);\n if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memcpy(ret->seed, params->curve->seed->data,\n params->curve->seed->length);\n ret->seed_len = params->curve->seed->length;\n }\n if (!params->order || !params->base || !params->base->data) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n if ((point = EC_POINT_new(ret)) == NULL)\n goto err;\n EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)\n (params->base->data[0] & ~0x01));\n if (!EC_POINT_oct2point(ret, point, params->base->data,\n params->base->length, NULL)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);\n goto err;\n }\n if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(a) || BN_is_zero(a)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);\n goto err;\n }\n if (BN_num_bits(a) > (int)field_bits + 1) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);\n goto err;\n }\n if (params->cofactor == NULL) {\n BN_free(b);\n b = NULL;\n } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);\n goto err;\n }\n if (!EC_GROUP_set_generator(ret, point, a, b)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);\n goto err;\n }\n ok = 1;\n err:\n if (!ok) {\n EC_GROUP_clear_free(ret);\n ret = NULL;\n }\n BN_free(p);\n BN_free(a);\n BN_free(b);\n EC_POINT_free(point);\n return (ret);\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'int BN_set_bit(BIGNUM *a, int n)\n{\n int i, j, k;\n if (n < 0)\n return 0;\n i = n / BN_BITS2;\n j = n % BN_BITS2;\n if (a->top <= i) {\n if (bn_wexpand(a, i + 1) == NULL)\n return (0);\n for (k = a->top; k < i + 1; k++)\n a->d[k] = 0;\n a->top = i + 1;\n }\n a->d[i] |= (((BN_ULONG)1) << j);\n bn_check_top(a);\n return (1);\n}', 'int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,\n const unsigned char *buf, size_t len, BN_CTX *ctx)\n{\n if (group->meth->oct2point == 0\n && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {\n ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n if (group->meth != point->meth) {\n ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {\n if (group->meth->field_type == NID_X9_62_prime_field)\n return ec_GFp_simple_oct2point(group, point, buf, len, ctx);\n else\n#ifdef OPENSSL_NO_EC2M\n {\n ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);\n return 0;\n }\n#else\n return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);\n#endif\n }\n return group->meth->oct2point(group, point, buf, len, ctx);\n}', 'int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,\n const unsigned char *buf, size_t len,\n BN_CTX *ctx)\n{\n point_conversion_form_t form;\n int y_bit;\n BN_CTX *new_ctx = NULL;\n BIGNUM *x, *y, *yxi;\n size_t field_len, enc_len;\n int ret = 0;\n if (len == 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);\n return 0;\n }\n form = buf[0];\n y_bit = form & 1;\n form = form & ~1U;\n if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)\n && (form != POINT_CONVERSION_UNCOMPRESSED)\n && (form != POINT_CONVERSION_HYBRID)) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if (form == 0) {\n if (len != 1) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n return EC_POINT_set_to_infinity(group, point);\n }\n field_len = (EC_GROUP_get_degree(group) + 7) / 8;\n enc_len =\n (form ==\n POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;\n if (len != enc_len) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n yxi = BN_CTX_get(ctx);\n if (yxi == NULL)\n goto err;\n if (!BN_bin2bn(buf + 1, field_len, x))\n goto err;\n if (BN_ucmp(x, group->field) >= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n if (form == POINT_CONVERSION_COMPRESSED) {\n if (!EC_POINT_set_compressed_coordinates_GF2m\n (group, point, x, y_bit, ctx))\n goto err;\n } else {\n if (!BN_bin2bn(buf + 1 + field_len, field_len, y))\n goto err;\n if (BN_ucmp(y, group->field) >= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n if (form == POINT_CONVERSION_HYBRID) {\n if (!group->meth->field_div(group, yxi, y, x, ctx))\n goto err;\n if (y_bit != BN_is_odd(yxi)) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n }\n if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx))\n goto err;\n }\n if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);\n goto err;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,\n EC_POINT *point, const BIGNUM *x,\n int y_bit, BN_CTX *ctx)\n{\n if (group->meth->point_set_compressed_coordinates == 0\n && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {\n ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,\n ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n if (group->meth != point->meth) {\n ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,\n EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {\n if (group->meth->field_type == NID_X9_62_prime_field)\n return ec_GFp_simple_set_compressed_coordinates(group, point, x,\n y_bit, ctx);\n else\n return ec_GF2m_simple_set_compressed_coordinates(group, point, x,\n y_bit, ctx);\n }\n return group->meth->point_set_compressed_coordinates(group, point, x,\n y_bit, ctx);\n}', 'int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x_, int y_bit,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp1, *tmp2, *x, *y;\n int ret = 0;\n ERR_clear_error();\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n y_bit = (y_bit != 0);\n BN_CTX_start(ctx);\n tmp1 = BN_CTX_get(ctx);\n tmp2 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_nnmod(x, x_, group->field, ctx))\n goto err;\n if (group->meth->field_decode == 0) {\n if (!group->meth->field_sqr(group, tmp2, x_, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))\n goto err;\n } else {\n if (!BN_mod_sqr(tmp2, x_, group->field, ctx))\n goto err;\n if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))\n goto err;\n }\n if (group->a_is_minus3) {\n if (!BN_mod_lshift1_quick(tmp2, x, group->field))\n goto err;\n if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))\n goto err;\n if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->a, ctx))\n goto err;\n if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))\n goto err;\n } else {\n if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))\n goto err;\n }\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n }\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->b, ctx))\n goto err;\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))\n goto err;\n }\n if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {\n unsigned long err = ERR_peek_last_error();\n if (ERR_GET_LIB(err) == ERR_LIB_BN\n && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {\n ERR_clear_error();\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n } else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_BN_LIB);\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n if (BN_is_zero(y)) {\n int kron;\n kron = BN_kronecker(x, group->field, ctx);\n if (kron == -2)\n goto err;\n if (kron == 1)\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSION_BIT);\n else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n goto err;\n }\n if (!BN_usub(y, group->field, y))\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n r->neg = a->neg;\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
35,818
0
https://github.com/libav/libav/blob/9e12002f114d7e0b0ef69519518cdc0391e5e198/avconv.c/#L3441
static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type) { OutputStream *ost; AVStream *st = avformat_new_stream(oc, NULL); int idx = oc->nb_streams - 1, ret = 0; char *bsf = NULL, *next, *codec_tag = NULL; AVBitStreamFilterContext *bsfc, *bsfc_prev = NULL; double qscale = -1; char *buf = NULL, *arg = NULL, *preset = NULL; AVIOContext *s = NULL; if (!st) { av_log(NULL, AV_LOG_FATAL, "Could not alloc stream.\n"); exit_program(1); } if (oc->nb_streams - 1 < o->nb_streamid_map) st->id = o->streamid_map[oc->nb_streams - 1]; output_streams = grow_array(output_streams, sizeof(*output_streams), &nb_output_streams, nb_output_streams + 1); ost = &output_streams[nb_output_streams - 1]; ost->file_index = nb_output_files; ost->index = idx; ost->st = st; st->codec->codec_type = type; choose_encoder(o, oc, ost); if (ost->enc) { ost->opts = filter_codec_opts(codec_opts, ost->enc->id, oc, st); } avcodec_get_context_defaults3(st->codec, ost->enc); st->codec->codec_type = type; MATCH_PER_STREAM_OPT(presets, str, preset, oc, st); if (preset && (!(ret = get_preset_file_2(preset, ost->enc->name, &s)))) { do { buf = get_line(s); if (!buf[0] || buf[0] == '#') { av_free(buf); continue; } if (!(arg = strchr(buf, '='))) { av_log(NULL, AV_LOG_FATAL, "Invalid line found in the preset file.\n"); exit_program(1); } *arg++ = 0; av_dict_set(&ost->opts, buf, arg, AV_DICT_DONT_OVERWRITE); av_free(buf); } while (!s->eof_reached); avio_close(s); } if (ret) { av_log(NULL, AV_LOG_FATAL, "Preset %s specified for stream %d:%d, but could not be opened.\n", preset, ost->file_index, ost->index); exit_program(1); } ost->max_frames = INT64_MAX; MATCH_PER_STREAM_OPT(max_frames, i64, ost->max_frames, oc, st); MATCH_PER_STREAM_OPT(bitstream_filters, str, bsf, oc, st); while (bsf) { if (next = strchr(bsf, ',')) *next++ = 0; if (!(bsfc = av_bitstream_filter_init(bsf))) { av_log(NULL, AV_LOG_FATAL, "Unknown bitstream filter %s\n", bsf); exit_program(1); } if (bsfc_prev) bsfc_prev->next = bsfc; else ost->bitstream_filters = bsfc; bsfc_prev = bsfc; bsf = next; } MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st); if (codec_tag) { uint32_t tag = strtol(codec_tag, &next, 0); if (*next) tag = AV_RL32(codec_tag); st->codec->codec_tag = tag; } MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st); if (qscale >= 0 || same_quant) { st->codec->flags |= CODEC_FLAG_QSCALE; st->codec->global_quality = FF_QP2LAMBDA * qscale; } if (oc->oformat->flags & AVFMT_GLOBALHEADER) st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER; av_opt_get_int(sws_opts, "sws_flags", 0, &ost->sws_flags); return ost; }
['static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type)\n{\n OutputStream *ost;\n AVStream *st = avformat_new_stream(oc, NULL);\n int idx = oc->nb_streams - 1, ret = 0;\n char *bsf = NULL, *next, *codec_tag = NULL;\n AVBitStreamFilterContext *bsfc, *bsfc_prev = NULL;\n double qscale = -1;\n char *buf = NULL, *arg = NULL, *preset = NULL;\n AVIOContext *s = NULL;\n if (!st) {\n av_log(NULL, AV_LOG_FATAL, "Could not alloc stream.\\n");\n exit_program(1);\n }\n if (oc->nb_streams - 1 < o->nb_streamid_map)\n st->id = o->streamid_map[oc->nb_streams - 1];\n output_streams = grow_array(output_streams, sizeof(*output_streams), &nb_output_streams,\n nb_output_streams + 1);\n ost = &output_streams[nb_output_streams - 1];\n ost->file_index = nb_output_files;\n ost->index = idx;\n ost->st = st;\n st->codec->codec_type = type;\n choose_encoder(o, oc, ost);\n if (ost->enc) {\n ost->opts = filter_codec_opts(codec_opts, ost->enc->id, oc, st);\n }\n avcodec_get_context_defaults3(st->codec, ost->enc);\n st->codec->codec_type = type;\n MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);\n if (preset && (!(ret = get_preset_file_2(preset, ost->enc->name, &s)))) {\n do {\n buf = get_line(s);\n if (!buf[0] || buf[0] == \'#\') {\n av_free(buf);\n continue;\n }\n if (!(arg = strchr(buf, \'=\'))) {\n av_log(NULL, AV_LOG_FATAL, "Invalid line found in the preset file.\\n");\n exit_program(1);\n }\n *arg++ = 0;\n av_dict_set(&ost->opts, buf, arg, AV_DICT_DONT_OVERWRITE);\n av_free(buf);\n } while (!s->eof_reached);\n avio_close(s);\n }\n if (ret) {\n av_log(NULL, AV_LOG_FATAL,\n "Preset %s specified for stream %d:%d, but could not be opened.\\n",\n preset, ost->file_index, ost->index);\n exit_program(1);\n }\n ost->max_frames = INT64_MAX;\n MATCH_PER_STREAM_OPT(max_frames, i64, ost->max_frames, oc, st);\n MATCH_PER_STREAM_OPT(bitstream_filters, str, bsf, oc, st);\n while (bsf) {\n if (next = strchr(bsf, \',\'))\n *next++ = 0;\n if (!(bsfc = av_bitstream_filter_init(bsf))) {\n av_log(NULL, AV_LOG_FATAL, "Unknown bitstream filter %s\\n", bsf);\n exit_program(1);\n }\n if (bsfc_prev)\n bsfc_prev->next = bsfc;\n else\n ost->bitstream_filters = bsfc;\n bsfc_prev = bsfc;\n bsf = next;\n }\n MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st);\n if (codec_tag) {\n uint32_t tag = strtol(codec_tag, &next, 0);\n if (*next)\n tag = AV_RL32(codec_tag);\n st->codec->codec_tag = tag;\n }\n MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st);\n if (qscale >= 0 || same_quant) {\n st->codec->flags |= CODEC_FLAG_QSCALE;\n st->codec->global_quality = FF_QP2LAMBDA * qscale;\n }\n if (oc->oformat->flags & AVFMT_GLOBALHEADER)\n st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;\n av_opt_get_int(sws_opts, "sws_flags", 0, &ost->sws_flags);\n return ost;\n}', 'AVStream *avformat_new_stream(AVFormatContext *s, AVCodec *c)\n{\n AVStream *st;\n int i;\n AVStream **streams;\n if (s->nb_streams >= INT_MAX/sizeof(*streams))\n return NULL;\n streams = av_realloc(s->streams, (s->nb_streams + 1) * sizeof(*streams));\n if (!streams)\n return NULL;\n s->streams = streams;\n st = av_mallocz(sizeof(AVStream));\n if (!st)\n return NULL;\n if (!(st->info = av_mallocz(sizeof(*st->info)))) {\n av_free(st);\n return NULL;\n }\n st->codec = avcodec_alloc_context3(c);\n if (s->iformat) {\n st->codec->bit_rate = 0;\n }\n st->index = s->nb_streams;\n st->start_time = AV_NOPTS_VALUE;\n st->duration = AV_NOPTS_VALUE;\n st->cur_dts = 0;\n st->first_dts = AV_NOPTS_VALUE;\n st->probe_packets = MAX_PROBE_PACKETS;\n avpriv_set_pts_info(st, 33, 1, 90000);\n st->last_IP_pts = AV_NOPTS_VALUE;\n for(i=0; i<MAX_REORDER_DELAY+1; i++)\n st->pts_buffer[i]= AV_NOPTS_VALUE;\n st->reference_dts = AV_NOPTS_VALUE;\n st->sample_aspect_ratio = (AVRational){0,1};\n s->streams[s->nb_streams++] = st;\n return st;\n}']
35,819
0
https://github.com/libav/libav/blob/e4e30256f87f177decf59b59e923d05ef64147df/libavcodec/mpegvideo_enc.c/#L1124
static int estimate_best_b_count(MpegEncContext *s) { AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id); AVCodecContext *c = avcodec_alloc_context3(NULL); AVFrame input[FF_MAX_B_FRAMES + 2]; const int scale = s->avctx->brd_scale; int i, j, out_size, p_lambda, b_lambda, lambda2; int outbuf_size = s->width * s->height; uint8_t *outbuf = av_malloc(outbuf_size); int64_t best_rd = INT64_MAX; int best_b_count = -1; assert(scale >= 0 && scale <= 3); p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P]; b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B]; if (!b_lambda) b_lambda = p_lambda; lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >> FF_LAMBDA_SHIFT; c->width = s->width >> scale; c->height = s->height >> scale; c->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR | CODEC_FLAG_INPUT_PRESERVED ; c->flags |= s->avctx->flags & CODEC_FLAG_QPEL; c->mb_decision = s->avctx->mb_decision; c->me_cmp = s->avctx->me_cmp; c->mb_cmp = s->avctx->mb_cmp; c->me_sub_cmp = s->avctx->me_sub_cmp; c->pix_fmt = PIX_FMT_YUV420P; c->time_base = s->avctx->time_base; c->max_b_frames = s->max_b_frames; if (avcodec_open2(c, codec, NULL) < 0) return -1; for (i = 0; i < s->max_b_frames + 2; i++) { int ysize = c->width * c->height; int csize = (c->width / 2) * (c->height / 2); Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] : s->next_picture_ptr; avcodec_get_frame_defaults(&input[i]); input[i].data[0] = av_malloc(ysize + 2 * csize); input[i].data[1] = input[i].data[0] + ysize; input[i].data[2] = input[i].data[1] + csize; input[i].linesize[0] = c->width; input[i].linesize[1] = input[i].linesize[2] = c->width / 2; if (pre_input_ptr && (!i || s->input_picture[i - 1])) { pre_input = *pre_input_ptr; if (pre_input.f.type != FF_BUFFER_TYPE_SHARED && i) { pre_input.f.data[0] += INPLACE_OFFSET; pre_input.f.data[1] += INPLACE_OFFSET; pre_input.f.data[2] += INPLACE_OFFSET; } s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0], pre_input.f.data[0], pre_input.f.linesize[0], c->width, c->height); s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1], pre_input.f.data[1], pre_input.f.linesize[1], c->width >> 1, c->height >> 1); s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2], pre_input.f.data[2], pre_input.f.linesize[2], c->width >> 1, c->height >> 1); } } for (j = 0; j < s->max_b_frames + 1; j++) { int64_t rd = 0; if (!s->input_picture[j]) break; c->error[0] = c->error[1] = c->error[2] = 0; input[0].pict_type = AV_PICTURE_TYPE_I; input[0].quality = 1 * FF_QP2LAMBDA; out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[0]); for (i = 0; i < s->max_b_frames + 1; i++) { int is_p = i % (j + 1) == j || i == s->max_b_frames; input[i + 1].pict_type = is_p ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B; input[i + 1].quality = is_p ? p_lambda : b_lambda; out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[i + 1]); rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3); } while (out_size) { out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL); rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3); } rd += c->error[0] + c->error[1] + c->error[2]; if (rd < best_rd) { best_rd = rd; best_b_count = j; } } av_freep(&outbuf); avcodec_close(c); av_freep(&c); for (i = 0; i < s->max_b_frames + 2; i++) { av_freep(&input[i].data[0]); } return best_b_count; }
['static int estimate_best_b_count(MpegEncContext *s)\n{\n AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);\n AVCodecContext *c = avcodec_alloc_context3(NULL);\n AVFrame input[FF_MAX_B_FRAMES + 2];\n const int scale = s->avctx->brd_scale;\n int i, j, out_size, p_lambda, b_lambda, lambda2;\n int outbuf_size = s->width * s->height;\n uint8_t *outbuf = av_malloc(outbuf_size);\n int64_t best_rd = INT64_MAX;\n int best_b_count = -1;\n assert(scale >= 0 && scale <= 3);\n p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];\n b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];\n if (!b_lambda)\n b_lambda = p_lambda;\n lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>\n FF_LAMBDA_SHIFT;\n c->width = s->width >> scale;\n c->height = s->height >> scale;\n c->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR |\n CODEC_FLAG_INPUT_PRESERVED ;\n c->flags |= s->avctx->flags & CODEC_FLAG_QPEL;\n c->mb_decision = s->avctx->mb_decision;\n c->me_cmp = s->avctx->me_cmp;\n c->mb_cmp = s->avctx->mb_cmp;\n c->me_sub_cmp = s->avctx->me_sub_cmp;\n c->pix_fmt = PIX_FMT_YUV420P;\n c->time_base = s->avctx->time_base;\n c->max_b_frames = s->max_b_frames;\n if (avcodec_open2(c, codec, NULL) < 0)\n return -1;\n for (i = 0; i < s->max_b_frames + 2; i++) {\n int ysize = c->width * c->height;\n int csize = (c->width / 2) * (c->height / 2);\n Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :\n s->next_picture_ptr;\n avcodec_get_frame_defaults(&input[i]);\n input[i].data[0] = av_malloc(ysize + 2 * csize);\n input[i].data[1] = input[i].data[0] + ysize;\n input[i].data[2] = input[i].data[1] + csize;\n input[i].linesize[0] = c->width;\n input[i].linesize[1] =\n input[i].linesize[2] = c->width / 2;\n if (pre_input_ptr && (!i || s->input_picture[i - 1])) {\n pre_input = *pre_input_ptr;\n if (pre_input.f.type != FF_BUFFER_TYPE_SHARED && i) {\n pre_input.f.data[0] += INPLACE_OFFSET;\n pre_input.f.data[1] += INPLACE_OFFSET;\n pre_input.f.data[2] += INPLACE_OFFSET;\n }\n s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0],\n pre_input.f.data[0], pre_input.f.linesize[0],\n c->width, c->height);\n s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1],\n pre_input.f.data[1], pre_input.f.linesize[1],\n c->width >> 1, c->height >> 1);\n s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2],\n pre_input.f.data[2], pre_input.f.linesize[2],\n c->width >> 1, c->height >> 1);\n }\n }\n for (j = 0; j < s->max_b_frames + 1; j++) {\n int64_t rd = 0;\n if (!s->input_picture[j])\n break;\n c->error[0] = c->error[1] = c->error[2] = 0;\n input[0].pict_type = AV_PICTURE_TYPE_I;\n input[0].quality = 1 * FF_QP2LAMBDA;\n out_size = avcodec_encode_video(c, outbuf,\n outbuf_size, &input[0]);\n for (i = 0; i < s->max_b_frames + 1; i++) {\n int is_p = i % (j + 1) == j || i == s->max_b_frames;\n input[i + 1].pict_type = is_p ?\n AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;\n input[i + 1].quality = is_p ? p_lambda : b_lambda;\n out_size = avcodec_encode_video(c, outbuf, outbuf_size,\n &input[i + 1]);\n rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);\n }\n while (out_size) {\n out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);\n rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);\n }\n rd += c->error[0] + c->error[1] + c->error[2];\n if (rd < best_rd) {\n best_rd = rd;\n best_b_count = j;\n }\n }\n av_freep(&outbuf);\n avcodec_close(c);\n av_freep(&c);\n for (i = 0; i < s->max_b_frames + 2; i++) {\n av_freep(&input[i].data[0]);\n }\n return best_b_count;\n}', 'AVCodecContext *avcodec_alloc_context3(AVCodec *codec){\n AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));\n if(avctx==NULL) return NULL;\n if(avcodec_get_context_defaults3(avctx, codec) < 0){\n av_free(avctx);\n return NULL;\n }\n return avctx;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-32) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+32);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&31) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,32,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(32,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
35,820
0
https://github.com/libav/libav/blob/47399ccdfd93d337c96c76fbf591f0e3637131ef/libavcodec/bitstream.h/#L236
static inline void skip_remaining(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE bc->bits >>= n; #else bc->bits <<= n; #endif bc->bits_left -= n; }
['static int check_bits_for_superframe(BitstreamContext *orig_bc,\n WMAVoiceContext *s)\n{\n BitstreamContext s_bc, *bc = &s_bc;\n int n, need_bits, bd_idx;\n const struct frame_type_desc *frame_desc;\n *bc = *orig_bc;\n if (bitstream_bits_left(bc) < 14)\n return 1;\n if (!bitstream_read_bit(bc))\n return AVERROR(ENOSYS);\n if (bitstream_read_bit(bc)) bitstream_skip(bc, 12);\n if (s->has_residual_lsps) {\n if (bitstream_bits_left(bc) < s->sframe_lsp_bitsize)\n return 1;\n bitstream_skip(bc, s->sframe_lsp_bitsize);\n }\n for (n = 0; n < MAX_FRAMES; n++) {\n int aw_idx_is_ext = 0;\n if (!s->has_residual_lsps) {\n if (bitstream_bits_left(bc) < s->frame_lsp_bitsize)\n return 1;\n bitstream_skip(bc, s->frame_lsp_bitsize);\n }\n bd_idx = s->vbm_tree[bitstream_read_vlc(bc, frame_type_vlc.table, 6, 3)];\n if (bd_idx < 0)\n return AVERROR_INVALIDDATA;\n frame_desc = &frame_descs[bd_idx];\n if (frame_desc->acb_type == ACB_TYPE_ASYMMETRIC) {\n if (bitstream_bits_left(bc) < s->pitch_nbits)\n return 1;\n bitstream_skip(bc, s->pitch_nbits);\n }\n if (frame_desc->fcb_type == FCB_TYPE_SILENCE) {\n bitstream_skip(bc, 8);\n } else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) {\n int tmp = bitstream_read(bc, 6);\n if (tmp >= 0x36) {\n bitstream_skip(bc, 2);\n aw_idx_is_ext = 1;\n }\n }\n if (frame_desc->acb_type == ACB_TYPE_HAMMING) {\n need_bits = s->block_pitch_nbits +\n (frame_desc->n_blocks - 1) * s->block_delta_pitch_nbits;\n } else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) {\n need_bits = 2 * !aw_idx_is_ext;\n } else\n need_bits = 0;\n need_bits += frame_desc->frame_size;\n if (bitstream_bits_left(bc) < need_bits)\n return 1;\n bitstream_skip(bc, need_bits);\n }\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n < bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n bc->bits = 0;\n bc->bits_left = 0;\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void skip_remaining(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n bc->bits >>= n;\n#else\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n}']
35,821
0
https://github.com/openssl/openssl/blob/23dd0c9f8dc6f7edf4b872d13e5644dfbbee585b/apps/nseq.c/#L65
int nseq_main(int argc, char **argv) { BIO *in = NULL, *out = NULL; X509 *x509 = NULL; NETSCAPE_CERT_SEQUENCE *seq = NULL; OPTION_CHOICE o; int toseq = 0, ret = 1, i; char *infile = NULL, *outfile = NULL, *prog; prog = opt_init(argc, argv, nseq_options); while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_EOF: case OPT_ERR: opthelp: BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); goto end; case OPT_HELP: ret = 0; opt_help(nseq_options); goto end; case OPT_TOSEQ: toseq = 1; break; case OPT_IN: infile = opt_arg(); break; case OPT_OUT: outfile = opt_arg(); break; } } argc = opt_num_rest(); if (argc != 0) goto opthelp; in = bio_open_default(infile, 'r', FORMAT_PEM); if (in == NULL) goto end; out = bio_open_default(outfile, 'w', FORMAT_PEM); if (out == NULL) goto end; if (toseq) { seq = NETSCAPE_CERT_SEQUENCE_new(); if (seq == NULL) goto end; seq->certs = sk_X509_new_null(); if (seq->certs == NULL) goto end; while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) sk_X509_push(seq->certs, x509); if (!sk_X509_num(seq->certs)) { BIO_printf(bio_err, "%s: Error reading certs file %s\n", prog, infile); ERR_print_errors(bio_err); goto end; } PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq); ret = 0; goto end; } seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL); if (seq == NULL) { BIO_printf(bio_err, "%s: Error reading sequence file %s\n", prog, infile); ERR_print_errors(bio_err); goto end; } for (i = 0; i < sk_X509_num(seq->certs); i++) { x509 = sk_X509_value(seq->certs, i); dump_cert_text(out, x509); PEM_write_bio_X509(out, x509); } ret = 0; end: BIO_free(in); BIO_free_all(out); NETSCAPE_CERT_SEQUENCE_free(seq); return (ret); }
['int nseq_main(int argc, char **argv)\n{\n BIO *in = NULL, *out = NULL;\n X509 *x509 = NULL;\n NETSCAPE_CERT_SEQUENCE *seq = NULL;\n OPTION_CHOICE o;\n int toseq = 0, ret = 1, i;\n char *infile = NULL, *outfile = NULL, *prog;\n prog = opt_init(argc, argv, nseq_options);\n while ((o = opt_next()) != OPT_EOF) {\n switch (o) {\n case OPT_EOF:\n case OPT_ERR:\n opthelp:\n BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n goto end;\n case OPT_HELP:\n ret = 0;\n opt_help(nseq_options);\n goto end;\n case OPT_TOSEQ:\n toseq = 1;\n break;\n case OPT_IN:\n infile = opt_arg();\n break;\n case OPT_OUT:\n outfile = opt_arg();\n break;\n }\n }\n argc = opt_num_rest();\n if (argc != 0)\n goto opthelp;\n in = bio_open_default(infile, \'r\', FORMAT_PEM);\n if (in == NULL)\n goto end;\n out = bio_open_default(outfile, \'w\', FORMAT_PEM);\n if (out == NULL)\n goto end;\n if (toseq) {\n seq = NETSCAPE_CERT_SEQUENCE_new();\n if (seq == NULL)\n goto end;\n seq->certs = sk_X509_new_null();\n if (seq->certs == NULL)\n goto end;\n while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL)))\n sk_X509_push(seq->certs, x509);\n if (!sk_X509_num(seq->certs)) {\n BIO_printf(bio_err, "%s: Error reading certs file %s\\n",\n prog, infile);\n ERR_print_errors(bio_err);\n goto end;\n }\n PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);\n ret = 0;\n goto end;\n }\n seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL);\n if (seq == NULL) {\n BIO_printf(bio_err, "%s: Error reading sequence file %s\\n",\n prog, infile);\n ERR_print_errors(bio_err);\n goto end;\n }\n for (i = 0; i < sk_X509_num(seq->certs); i++) {\n x509 = sk_X509_value(seq->certs, i);\n dump_cert_text(out, x509);\n PEM_write_bio_X509(out, x509);\n }\n ret = 0;\n end:\n BIO_free(in);\n BIO_free_all(out);\n NETSCAPE_CERT_SEQUENCE_free(seq);\n return (ret);\n}', 'int opt_num_rest(void)\n{\n int i = 0;\n char **pp;\n for (pp = opt_rest(); *pp; pp++, i++)\n continue;\n return i;\n}', 'char **opt_rest(void)\n{\n return &argv[opt_index];\n}', 'BIO *bio_open_default(const char *filename, char mode, int format)\n{\n return bio_open_default_(filename, mode, format, 0);\n}', 'static BIO *bio_open_default_(const char *filename, char mode, int format,\n int quiet)\n{\n BIO *ret;\n if (filename == NULL || strcmp(filename, "-") == 0) {\n ret = mode == \'r\' ? dup_bio_in(format) : dup_bio_out(format);\n if (quiet) {\n ERR_clear_error();\n return ret;\n }\n if (ret != NULL)\n return ret;\n BIO_printf(bio_err,\n "Can\'t open %s, %s\\n",\n mode == \'r\' ? "stdin" : "stdout", strerror(errno));\n } else {\n ret = BIO_new_file(filename, modestr(mode, format));\n if (quiet) {\n ERR_clear_error();\n return ret;\n }\n if (ret != NULL)\n return ret;\n BIO_printf(bio_err,\n "Can\'t open %s for %s, %s\\n",\n filename, modeverb(mode), strerror(errno));\n }\n ERR_print_errors(bio_err);\n return NULL;\n}', 'BIO *dup_bio_in(int format)\n{\n return BIO_new_fp(stdin,\n BIO_NOCLOSE | (istext(format) ? BIO_FP_TEXT : 0));\n}', 'static int istext(int format)\n{\n return (format & B_FORMAT_TEXT) == B_FORMAT_TEXT;\n}']
35,822
0
https://github.com/openssl/openssl/blob/ff7b6ce9db329eb48775bb81e0ecbbd2a9b23c1c/crypto/bn/bn_ctx.c/#L355
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,\n\t\t\t\t\tBIGNUM **kinvp, BIGNUM **rp,\n\t\t\t\t\tconst unsigned char *dgst, int dlen)\n{\n\tBN_CTX *ctx = NULL;\n\tBIGNUM\t *k = NULL, *r = NULL, *order = NULL, *X = NULL;\n\tEC_POINT *tmp_point=NULL;\n\tconst EC_GROUP *group;\n\tint \t ret = 0;\n\tif (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL)\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);\n\t\treturn 0;\n\t}\n\tif (ctx_in == NULL)\n\t{\n\t\tif ((ctx = BN_CTX_new()) == NULL)\n\t\t{\n\t\t\tECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_MALLOC_FAILURE);\n\t\t\treturn 0;\n\t\t}\n\t}\n\telse\n\t\tctx = ctx_in;\n\tk = BN_new();\n\tr = BN_new();\n\torder = BN_new();\n\tX = BN_new();\n\tif (!k || !r || !order || !X)\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t}\n\tif ((tmp_point = EC_POINT_new(group)) == NULL)\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n\t\tgoto err;\n\t}\n\tif (!EC_GROUP_get_order(group, order, ctx))\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n\t\tgoto err;\n\t}\n#ifdef OPENSSL_FIPS\n\tif (!fips_check_ec_prng(eckey))\n\t\tgoto err;\n#endif\n\tdo\n\t{\n\t\tdo\n#ifndef OPENSSL_NO_SHA512\n\t\t\tif (dgst != NULL)\n\t\t\t{\n\t\t\t\tif (!BN_generate_dsa_nonce(k, order, EC_KEY_get0_private_key(eckey),\n\t\t\t\t\t\t\t dgst, dlen, ctx))\n\t\t\t\t\t{\n\t\t\t\t\tECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,\n\t\t\t\t\t\t ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t}\n\t\t\telse\n#endif\n\t\t\t{\n\t\t\t\tif (!BN_rand_range(k, order))\n\t\t\t\t{\n\t\t\t\t\tECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,\n\t\t\t\t\t\t ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\twhile (BN_is_zero(k));\n\t\tif (!BN_add(k, k, order)) goto err;\n\t\tif (BN_num_bits(k) <= BN_num_bits(order))\n\t\t\tif (!BN_add(k, k, order)) goto err;\n\t\tif (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx))\n\t\t{\n\t\t\tECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n\t\t\tgoto err;\n\t\t}\n\t\tif (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)\n\t\t{\n\t\t\tif (!EC_POINT_get_affine_coordinates_GFp(group,\n\t\t\t\ttmp_point, X, NULL, ctx))\n\t\t\t{\n\t\t\t\tECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n#ifndef OPENSSL_NO_EC2M\n\t\telse\n\t\t{\n\t\t\tif (!EC_POINT_get_affine_coordinates_GF2m(group,\n\t\t\t\ttmp_point, X, NULL, ctx))\n\t\t\t{\n\t\t\t\tECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n#endif\n\t\tif (!BN_nnmod(r, X, order, ctx))\n\t\t{\n\t\t\tECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t}\n\t}\n\twhile (BN_is_zero(r));\n\tif (!BN_mod_inverse(k, k, order, ctx))\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n\t\tgoto err;\n\t}\n\tif (*rp != NULL)\n\t\tBN_clear_free(*rp);\n\tif (*kinvp != NULL)\n\t\tBN_clear_free(*kinvp);\n\t*rp = r;\n\t*kinvp = k;\n\tret = 1;\nerr:\n\tif (!ret)\n\t{\n\t\tif (k != NULL) BN_clear_free(k);\n\t\tif (r != NULL) BN_clear_free(r);\n\t}\n\tif (ctx_in == NULL)\n\t\tBN_CTX_free(ctx);\n\tif (order != NULL)\n\t\tBN_free(order);\n\tif (tmp_point != NULL)\n\t\tEC_POINT_free(tmp_point);\n\tif (X)\n\t\tBN_clear_free(X);\n\treturn(ret);\n}', 'int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range, const BIGNUM* priv,\n\t\t\t const unsigned char *message, size_t message_len,\n\t\t\t BN_CTX *ctx)\n\t{\n\tSHA512_CTX sha;\n\tunsigned char random_bytes[64];\n\tunsigned char digest[SHA512_DIGEST_LENGTH];\n\tunsigned done, todo;\n\tconst unsigned num_k_bytes = BN_num_bytes(range) + 8;\n\tunsigned char private_bytes[96];\n\tunsigned char *k_bytes;\n\tint ret = 0;\n\tk_bytes = OPENSSL_malloc(num_k_bytes);\n\tif (!k_bytes)\n\t\tgoto err;\n\ttodo = sizeof(priv->d[0])*priv->top;\n\tif (todo > sizeof(private_bytes))\n\t\t{\n\t\tBNerr(BN_F_BN_GENERATE_DSA_NONCE, BN_R_PRIVATE_KEY_TOO_LARGE);\n\t\tgoto err;\n\t\t}\n\tmemcpy(private_bytes, priv->d, todo);\n\tmemset(private_bytes + todo, 0, sizeof(private_bytes) - todo);\n\tfor (done = 0; done < num_k_bytes;) {\n\t\tif (RAND_bytes(random_bytes, sizeof(random_bytes)) != 1)\n\t\t\tgoto err;\n\t\tSHA512_Init(&sha);\n\t\tSHA512_Update(&sha, &done, sizeof(done));\n\t\tSHA512_Update(&sha, private_bytes, sizeof(private_bytes));\n\t\tSHA512_Update(&sha, message, message_len);\n\t\tSHA512_Update(&sha, random_bytes, sizeof(random_bytes));\n\t\tSHA512_Final(digest, &sha);\n\t\ttodo = num_k_bytes - done;\n\t\tif (todo > SHA512_DIGEST_LENGTH)\n\t\t\ttodo = SHA512_DIGEST_LENGTH;\n\t\tmemcpy(k_bytes + done, digest, todo);\n\t\tdone += todo;\n\t}\n\tif (!BN_bin2bn(k_bytes, num_k_bytes, out))\n\t\tgoto err;\n\tif (BN_mod(out, out, range, ctx) != 1)\n\t\tgoto err;\n\tret = 1;\nerr:\n\tif (k_bytes)\n\t\tOPENSSL_free(k_bytes);\n\treturn ret;\n\t}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tint no_branch=0;\n\tif (num->top > 0 && num->d[num->top - 1] == 0)\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_NOT_INITIALIZED);\n\t\treturn 0;\n\t\t}\n\tbn_check_top(num);\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\tno_branch=1;\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (!no_branch && BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n\t\tgoto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (no_branch)\n\t\t{\n\t\tif (snum->top <= sdiv->top+1)\n\t\t\t{\n\t\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\t\tsnum->top = sdiv->top + 2;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\t\tsnum->d[snum->top] = 0;\n\t\t\tsnum->top ++;\n\t\t\t}\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-no_branch;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (!no_branch)\n\t\t{\n\t\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t\t{\n\t\t\tbn_clear_top2max(&wnum);\n\t\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t\t*resp=1;\n\t\t\t}\n\t\telse\n\t\t\tres->top--;\n\t\t}\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tif (no_branch)\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_START,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
35,823
0
https://github.com/openssl/openssl/blob/0ca6d7c6b1e73d17ca67b7ffd8435bde43bf50af/crypto/x509/x509_cmp.c/#L250
int X509_check_private_key(X509 *x, EVP_PKEY *k) { EVP_PKEY *xk=NULL; int ok=0; xk=X509_get_pubkey(x); if (xk->type != k->type) { X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_TYPE_MISMATCH); goto err; } switch (k->type) { #ifndef NO_RSA case EVP_PKEY_RSA: if (BN_cmp(xk->pkey.rsa->n,k->pkey.rsa->n) != 0 || BN_cmp(xk->pkey.rsa->e,k->pkey.rsa->e) != 0) { X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH); goto err; } break; #endif #ifndef NO_DSA case EVP_PKEY_DSA: if (BN_cmp(xk->pkey.dsa->pub_key,k->pkey.dsa->pub_key) != 0) { X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH); goto err; } break; #endif #ifndef NO_DH case EVP_PKEY_DH: X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_CANT_CHECK_DH_KEY); goto err; #endif default: X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_UNKNOWN_KEY_TYPE); goto err; } ok=1; err: EVP_PKEY_free(xk); return(ok); }
['int X509_check_private_key(X509 *x, EVP_PKEY *k)\n\t{\n\tEVP_PKEY *xk=NULL;\n\tint ok=0;\n\txk=X509_get_pubkey(x);\n\tif (xk->type != k->type)\n\t {\n\t X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_TYPE_MISMATCH);\n\t goto err;\n\t }\n\tswitch (k->type)\n\t\t{\n#ifndef NO_RSA\n\tcase EVP_PKEY_RSA:\n\t\tif (BN_cmp(xk->pkey.rsa->n,k->pkey.rsa->n) != 0\n\t\t || BN_cmp(xk->pkey.rsa->e,k->pkey.rsa->e) != 0)\n\t\t {\n\t\t X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH);\n\t\t goto err;\n\t\t }\n\t\tbreak;\n#endif\n#ifndef NO_DSA\n\tcase EVP_PKEY_DSA:\n\t\tif (BN_cmp(xk->pkey.dsa->pub_key,k->pkey.dsa->pub_key) != 0)\n\t\t {\n\t\t X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH);\n\t\t goto err;\n\t\t }\n\t\tbreak;\n#endif\n#ifndef NO_DH\n\tcase EVP_PKEY_DH:\n\t X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_CANT_CHECK_DH_KEY);\n\t\tgoto err;\n#endif\n\tdefault:\n\t X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_UNKNOWN_KEY_TYPE);\n\t\tgoto err;\n\t\t}\n\tok=1;\nerr:\n\tEVP_PKEY_free(xk);\n\treturn(ok);\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}']
35,824
0
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/bn/bn_add.c/#L219
int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { int max, min, dif; register BN_ULONG t1, t2, *rp; register const BN_ULONG *ap, *bp; int i, carry; bn_check_top(a); bn_check_top(b); max = a->top; min = b->top; dif = max - min; if (dif < 0) { BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3); return (0); } if (bn_wexpand(r, max) == NULL) return (0); ap = a->d; bp = b->d; rp = r->d; #if 1 carry = 0; for (i = min; i != 0; i--) { t1 = *(ap++); t2 = *(bp++); if (carry) { carry = (t1 <= t2); t1 = (t1 - t2 - 1) & BN_MASK2; } else { carry = (t1 < t2); t1 = (t1 - t2) & BN_MASK2; } *(rp++) = t1 & BN_MASK2; } #else carry = bn_sub_words(rp, ap, bp, min); ap += min; bp += min; rp += min; #endif if (carry) { if (!dif) return 0; while (dif) { dif--; t1 = *(ap++); t2 = (t1 - 1) & BN_MASK2; *(rp++) = t2; if (t1) break; } } #if 0 memcpy(rp, ap, sizeof(*rp) * (max - i)); #else if (rp != ap) { for (;;) { if (!dif--) break; rp[0] = ap[0]; if (!dif--) break; rp[1] = ap[1]; if (!dif--) break; rp[2] = ap[2]; if (!dif--) break; rp[3] = ap[3]; rp += 4; ap += 4; } } #endif r->top = max; r->neg = 0; bn_correct_top(r); return (1); }
['int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,\n BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *a;\n const BIGNUM *ca;\n BN_CTX_start(ctx);\n if ((a = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (y != NULL) {\n if (x == y) {\n if (!BN_sqr(a, x, ctx))\n goto err;\n } else {\n if (!BN_mul(a, x, y, ctx))\n goto err;\n }\n ca = a;\n } else\n ca = x;\n ret = BN_div_recp(NULL, r, ca, recp, ctx);\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return (ret);\n}', 'int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,\n BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n int i, j, ret = 0;\n BIGNUM *a, *b, *d, *r;\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (dv != NULL)\n d = dv;\n else\n d = BN_CTX_get(ctx);\n if (rem != NULL)\n r = rem;\n else\n r = BN_CTX_get(ctx);\n if (a == NULL || b == NULL || d == NULL || r == NULL)\n goto err;\n if (BN_ucmp(m, &(recp->N)) < 0) {\n BN_zero(d);\n if (!BN_copy(r, m))\n return 0;\n BN_CTX_end(ctx);\n return (1);\n }\n i = BN_num_bits(m);\n j = recp->num_bits << 1;\n if (j > i)\n i = j;\n if (i != recp->shift)\n recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);\n if (recp->shift == -1)\n goto err;\n if (!BN_rshift(a, m, recp->num_bits))\n goto err;\n if (!BN_mul(b, a, &(recp->Nr), ctx))\n goto err;\n if (!BN_rshift(d, b, i - recp->num_bits))\n goto err;\n d->neg = 0;\n if (!BN_mul(b, &(recp->N), d, ctx))\n goto err;\n if (!BN_usub(r, m, b))\n goto err;\n r->neg = 0;\n#if 1\n j = 0;\n while (BN_ucmp(r, &(recp->N)) >= 0) {\n if (j++ > 2) {\n BNerr(BN_F_BN_DIV_RECP, BN_R_BAD_RECIPROCAL);\n goto err;\n }\n if (!BN_usub(r, r, &(recp->N)))\n goto err;\n if (!BN_add_word(d, 1))\n goto err;\n }\n#endif\n r->neg = BN_is_zero(r) ? 0 : m->neg;\n d->neg = m->neg ^ recp->N.neg;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(dv);\n bn_check_top(rem);\n return (ret);\n}', 'int BN_ucmp(const BIGNUM *a, const BIGNUM *b)\n{\n int i;\n BN_ULONG t1, t2, *ap, *bp;\n bn_check_top(a);\n bn_check_top(b);\n i = a->top - b->top;\n if (i != 0)\n return (i);\n ap = a->d;\n bp = b->d;\n for (i = a->top - 1; i >= 0; i--) {\n t1 = ap[i];\n t2 = bp[i];\n if (t1 != t2)\n return ((t1 > t2) ? 1 : -1);\n }\n return (0);\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, j, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l, tmp;\n bn_check_top(r);\n bn_check_top(a);\n nw = n / BN_BITS2;\n rb = n % BN_BITS2;\n lb = BN_BITS2 - rb;\n if (nw >= a->top || a->top == 0) {\n BN_zero(r);\n return (1);\n }\n i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;\n if (r != a) {\n r->neg = a->neg;\n if (bn_wexpand(r, i) == NULL)\n return (0);\n } else {\n if (n == 0)\n return 1;\n }\n f = &(a->d[nw]);\n t = r->d;\n j = a->top - nw;\n r->top = i;\n if (rb == 0) {\n for (i = j; i != 0; i--)\n *(t++) = *(f++);\n } else {\n l = *(f++);\n for (i = j - 1; i != 0; i--) {\n tmp = (l >> rb) & BN_MASK2;\n l = *(f++);\n *(t++) = (tmp | (l << lb)) & BN_MASK2;\n }\n if ((l = (l >> rb) & BN_MASK2))\n *(t) = l;\n }\n bn_check_top(r);\n return (1);\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n register BN_ULONG t1, t2, *rp;\n register const BN_ULONG *ap, *bp;\n int i, carry;\n bn_check_top(a);\n bn_check_top(b);\n max = a->top;\n min = b->top;\n dif = max - min;\n if (dif < 0) {\n BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n return (0);\n }\n if (bn_wexpand(r, max) == NULL)\n return (0);\n ap = a->d;\n bp = b->d;\n rp = r->d;\n#if 1\n carry = 0;\n for (i = min; i != 0; i--) {\n t1 = *(ap++);\n t2 = *(bp++);\n if (carry) {\n carry = (t1 <= t2);\n t1 = (t1 - t2 - 1) & BN_MASK2;\n } else {\n carry = (t1 < t2);\n t1 = (t1 - t2) & BN_MASK2;\n }\n *(rp++) = t1 & BN_MASK2;\n }\n#else\n carry = bn_sub_words(rp, ap, bp, min);\n ap += min;\n bp += min;\n rp += min;\n#endif\n if (carry) {\n if (!dif)\n return 0;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 - 1) & BN_MASK2;\n *(rp++) = t2;\n if (t1)\n break;\n }\n }\n#if 0\n memcpy(rp, ap, sizeof(*rp) * (max - i));\n#else\n if (rp != ap) {\n for (;;) {\n if (!dif--)\n break;\n rp[0] = ap[0];\n if (!dif--)\n break;\n rp[1] = ap[1];\n if (!dif--)\n break;\n rp[2] = ap[2];\n if (!dif--)\n break;\n rp[3] = ap[3];\n rp += 4;\n ap += 4;\n }\n }\n#endif\n r->top = max;\n r->neg = 0;\n bn_correct_top(r);\n return (1);\n}']
35,825
0
https://github.com/libav/libav/blob/1878f685c0f69d1bf0acc78c5fc09dae03ac48d5/libavfilter/avfilter.c/#L51
AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask) { AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef)); *ret = *ref; ret->perms &= pmask; ret->pic->refcount ++; return ret; }
['AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)\n{\n AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));\n *ret = *ref;\n ret->perms &= pmask;\n ret->pic->refcount ++;\n return ret;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
35,826
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/md5/md5_dgst.c/#L133
void MD5_Update(MD5_CTX *c, const void *_data, unsigned long len) { register const unsigned char *data=_data; register ULONG *p; int sw,sc; ULONG l; if (len == 0) return; l=(c->Nl+(len<<3))&0xffffffffL; if (l < c->Nl) c->Nh++; c->Nh+=(len>>29); c->Nl=l; if (c->num != 0) { p=c->data; sw=c->num>>2; sc=c->num&0x03; if ((c->num+len) >= MD5_CBLOCK) { l= p[sw]; p_c2l(data,l,sc); p[sw++]=l; for (; sw<MD5_LBLOCK; sw++) { c2l(data,l); p[sw]=l; } len-=(MD5_CBLOCK-c->num); md5_block(c,p,64); c->num=0; } else { int ew,ec; c->num+=(int)len; if ((sc+len) < 4) { l= p[sw]; p_c2l_p(data,l,sc,len); p[sw]=l; } else { ew=(c->num>>2); ec=(c->num&0x03); l= p[sw]; p_c2l(data,l,sc); p[sw++]=l; for (; sw < ew; sw++) { c2l(data,l); p[sw]=l; } if (ec) { c2l_p(data,l,ec); p[sw]=l; } } return; } } #ifdef L_ENDIAN if ((((unsigned long)data)%sizeof(ULONG)) == 0) { sw=(int)len/MD5_CBLOCK; if (sw > 0) { sw*=MD5_CBLOCK; md5_block(c,(ULONG *)data,sw); data+=sw; len-=sw; } } #endif p=c->data; while (len >= MD5_CBLOCK) { #if defined(L_ENDIAN) || defined(B_ENDIAN) if (p != (unsigned long *)data) memcpy(p,data,MD5_CBLOCK); data+=MD5_CBLOCK; #ifdef B_ENDIAN for (sw=(MD5_LBLOCK/4); sw; sw--) { Endian_Reverse32(p[0]); Endian_Reverse32(p[1]); Endian_Reverse32(p[2]); Endian_Reverse32(p[3]); p+=4; } #endif #else for (sw=(MD5_LBLOCK/4); sw; sw--) { c2l(data,l); *(p++)=l; c2l(data,l); *(p++)=l; c2l(data,l); *(p++)=l; c2l(data,l); *(p++)=l; } #endif p=c->data; md5_block(c,p,64); len-=MD5_CBLOCK; } sc=(int)len; c->num=sc; if (sc) { sw=sc>>2; #ifdef L_ENDIAN p[sw]=0; memcpy(p,data,sc); #else sc&=0x03; for ( ; sw; sw--) { c2l(data,l); *(p++)=l; } c2l_p(data,l,sc); *p=l; #endif } }
['int ssl2_connect(SSL *s)\n\t{\n\tunsigned long l=time(NULL);\n\tBUF_MEM *buf=NULL;\n\tint ret= -1;\n\tvoid (*cb)()=NULL;\n\tint new_state,state;\n\tRAND_seed(&l,sizeof(l));\n\tERR_clear_error();\n\tclear_sys_error();\n\tif (s->info_callback != NULL)\n\t\tcb=s->info_callback;\n\telse if (s->ctx->info_callback != NULL)\n\t\tcb=s->ctx->info_callback;\n\tif (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);\n\ts->in_handshake++;\n\tfor (;;)\n\t\t{\n\t\tstate=s->state;\n\t\tswitch (s->state)\n\t\t\t{\n\t\tcase SSL_ST_BEFORE:\n\t\tcase SSL_ST_CONNECT:\n\t\tcase SSL_ST_BEFORE|SSL_ST_CONNECT:\n\t\tcase SSL_ST_OK|SSL_ST_CONNECT:\n\t\t\ts->server=0;\n\t\t\tif (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);\n\t\t\ts->version=SSL2_VERSION;\n\t\t\ts->type=SSL_ST_CONNECT;\n\t\t\tbuf=s->init_buf;\n\t\t\tif ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))\n\t\t\t\t{\n\t\t\t\tret= -1;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tif (!BUF_MEM_grow(buf,\n\t\t\t\tSSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))\n\t\t\t\t{\n\t\t\t\tret= -1;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\ts->init_buf=buf;\n\t\t\ts->init_num=0;\n\t\t\ts->state=SSL2_ST_SEND_CLIENT_HELLO_A;\n\t\t\ts->ctx->stats.sess_connect++;\n\t\t\ts->handshake_func=ssl2_connect;\n\t\t\tBREAK;\n\t\tcase SSL2_ST_SEND_CLIENT_HELLO_A:\n\t\tcase SSL2_ST_SEND_CLIENT_HELLO_B:\n\t\t\ts->shutdown=0;\n\t\t\tret=client_hello(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->init_num=0;\n\t\t\ts->state=SSL2_ST_GET_SERVER_HELLO_A;\n\t\t\tBREAK;\n\t\tcase SSL2_ST_GET_SERVER_HELLO_A:\n\t\tcase SSL2_ST_GET_SERVER_HELLO_B:\n\t\t\tret=get_server_hello(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->init_num=0;\n\t\t\tif (!s->hit)\n\t\t\t\t{\n\t\t\t\ts->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_A;\n\t\t\t\tBREAK;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts->state=SSL2_ST_CLIENT_START_ENCRYPTION;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\tcase SSL2_ST_SEND_CLIENT_MASTER_KEY_A:\n\t\tcase SSL2_ST_SEND_CLIENT_MASTER_KEY_B:\n\t\t\tret=client_master_key(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->init_num=0;\n\t\t\ts->state=SSL2_ST_CLIENT_START_ENCRYPTION;\n\t\t\tbreak;\n\t\tcase SSL2_ST_CLIENT_START_ENCRYPTION:\n\t\t\tif (!ssl2_enc_init(s,1))\n\t\t\t\t{\n\t\t\t\tret= -1;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\ts->s2->clear_text=0;\n\t\t\ts->state=SSL2_ST_SEND_CLIENT_FINISHED_A;\n\t\t\tbreak;\n\t\tcase SSL2_ST_SEND_CLIENT_FINISHED_A:\n\t\tcase SSL2_ST_SEND_CLIENT_FINISHED_B:\n\t\t\tret=client_finished(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->init_num=0;\n\t\t\ts->state=SSL2_ST_GET_SERVER_VERIFY_A;\n\t\t\tbreak;\n\t\tcase SSL2_ST_GET_SERVER_VERIFY_A:\n\t\tcase SSL2_ST_GET_SERVER_VERIFY_B:\n\t\t\tret=get_server_verify(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->init_num=0;\n\t\t\ts->state=SSL2_ST_GET_SERVER_FINISHED_A;\n\t\t\tbreak;\n\t\tcase SSL2_ST_GET_SERVER_FINISHED_A:\n\t\tcase SSL2_ST_GET_SERVER_FINISHED_B:\n\t\t\tret=get_server_finished(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tbreak;\n\t\tcase SSL2_ST_SEND_CLIENT_CERTIFICATE_A:\n\t\tcase SSL2_ST_SEND_CLIENT_CERTIFICATE_B:\n\t\tcase SSL2_ST_SEND_CLIENT_CERTIFICATE_C:\n\t\tcase SSL2_ST_SEND_CLIENT_CERTIFICATE_D:\n\t\tcase SSL2_ST_X509_GET_CLIENT_CERTIFICATE:\n\t\t\tret=client_certificate(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->init_num=0;\n\t\t\ts->state=SSL2_ST_GET_SERVER_FINISHED_A;\n\t\t\tbreak;\n\t\tcase SSL_ST_OK:\n\t\t\tif (s->init_buf != NULL)\n\t\t\t\t{\n\t\t\t\tBUF_MEM_free(s->init_buf);\n\t\t\t\ts->init_buf=NULL;\n\t\t\t\t}\n\t\t\ts->init_num=0;\n\t\t\tssl_update_cache(s,SSL_SESS_CACHE_CLIENT);\n\t\t\tif (s->hit) s->ctx->stats.sess_hit++;\n\t\t\tret=1;\n\t\t\ts->ctx->stats.sess_connect_good++;\n\t\t\tif (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);\n\t\t\tgoto end;\n\t\tdefault:\n\t\t\tSSLerr(SSL_F_SSL2_CONNECT,SSL_R_UNKNOWN_STATE);\n\t\t\treturn(-1);\n\t\t\t}\n\t\tif ((cb != NULL) && (s->state != state))\n\t\t\t{\n\t\t\tnew_state=s->state;\n\t\t\ts->state=state;\n\t\t\tcb(s,SSL_CB_CONNECT_LOOP,1);\n\t\t\ts->state=new_state;\n\t\t\t}\n\t\t}\nend:\n\ts->in_handshake--;\n\tif (cb != NULL)\n\t\tcb(s,SSL_CB_CONNECT_EXIT,ret);\n\treturn(ret);\n\t}', 'int ssl2_enc_init(SSL *s, int client)\n\t{\n\tEVP_CIPHER_CTX *rs,*ws;\n\tconst EVP_CIPHER *c;\n\tconst EVP_MD *md;\n\tint num;\n\tif (!ssl_cipher_get_evp(s->session,&c,&md,NULL))\n\t\t{\n\t\tssl2_return_error(s,SSL2_PE_NO_CIPHER);\n\t\tSSLerr(SSL_F_SSL2_ENC_INIT,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);\n\t\treturn(0);\n\t\t}\n\ts->read_hash=md;\n\ts->write_hash=md;\n\tif ((s->enc_read_ctx == NULL) &&\n\t\t((s->enc_read_ctx=(EVP_CIPHER_CTX *)\n\t\tMalloc(sizeof(EVP_CIPHER_CTX))) == NULL))\n\t\tgoto err;\n\tif ((s->enc_write_ctx == NULL) &&\n\t\t((s->enc_write_ctx=(EVP_CIPHER_CTX *)\n\t\tMalloc(sizeof(EVP_CIPHER_CTX))) == NULL))\n\t\tgoto err;\n\trs= s->enc_read_ctx;\n\tws= s->enc_write_ctx;\n\tEVP_CIPHER_CTX_init(rs);\n\tEVP_CIPHER_CTX_init(ws);\n\tnum=c->key_len;\n\ts->s2->key_material_length=num*2;\n\tssl2_generate_key_material(s);\n\tEVP_EncryptInit(ws,c,&(s->s2->key_material[(client)?num:0]),\n\t\ts->session->key_arg);\n\tEVP_DecryptInit(rs,c,&(s->s2->key_material[(client)?0:num]),\n\t\ts->session->key_arg);\n\ts->s2->read_key= &(s->s2->key_material[(client)?0:num]);\n\ts->s2->write_key= &(s->s2->key_material[(client)?num:0]);\n\treturn(1);\nerr:\n\tSSLerr(SSL_F_SSL2_ENC_INIT,ERR_R_MALLOC_FAILURE);\n\treturn(0);\n\t}', "void ssl2_generate_key_material(SSL *s)\n\t{\n\tunsigned int i;\n\tMD5_CTX ctx;\n\tunsigned char *km;\n\tunsigned char c='0';\n\tkm=s->s2->key_material;\n\tfor (i=0; i<s->s2->key_material_length; i+=MD5_DIGEST_LENGTH)\n\t\t{\n\t\tMD5_Init(&ctx);\n\t\tMD5_Update(&ctx,s->session->master_key,s->session->master_key_length);\n\t\tMD5_Update(&ctx,(unsigned char *)&c,1);\n\t\tc++;\n\t\tMD5_Update(&ctx,s->s2->challenge,s->s2->challenge_length);\n\t\tMD5_Update(&ctx,s->s2->conn_id,s->s2->conn_id_length);\n\t\tMD5_Final(km,&ctx);\n\t\tkm+=MD5_DIGEST_LENGTH;\n\t\t}\n\t}", 'void MD5_Update(MD5_CTX *c, const void *_data, unsigned long len)\n\t{\n\tregister const unsigned char *data=_data;\n\tregister ULONG *p;\n\tint sw,sc;\n\tULONG l;\n\tif (len == 0) return;\n\tl=(c->Nl+(len<<3))&0xffffffffL;\n\tif (l < c->Nl)\n\t\tc->Nh++;\n\tc->Nh+=(len>>29);\n\tc->Nl=l;\n\tif (c->num != 0)\n\t\t{\n\t\tp=c->data;\n\t\tsw=c->num>>2;\n\t\tsc=c->num&0x03;\n\t\tif ((c->num+len) >= MD5_CBLOCK)\n\t\t\t{\n\t\t\tl= p[sw];\n\t\t\tp_c2l(data,l,sc);\n\t\t\tp[sw++]=l;\n\t\t\tfor (; sw<MD5_LBLOCK; sw++)\n\t\t\t\t{\n\t\t\t\tc2l(data,l);\n\t\t\t\tp[sw]=l;\n\t\t\t\t}\n\t\t\tlen-=(MD5_CBLOCK-c->num);\n\t\t\tmd5_block(c,p,64);\n\t\t\tc->num=0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tint ew,ec;\n\t\t\tc->num+=(int)len;\n\t\t\tif ((sc+len) < 4)\n\t\t\t\t{\n\t\t\t\tl= p[sw];\n\t\t\t\tp_c2l_p(data,l,sc,len);\n\t\t\t\tp[sw]=l;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tew=(c->num>>2);\n\t\t\t\tec=(c->num&0x03);\n\t\t\t\tl= p[sw];\n\t\t\t\tp_c2l(data,l,sc);\n\t\t\t\tp[sw++]=l;\n\t\t\t\tfor (; sw < ew; sw++)\n\t\t\t\t\t{ c2l(data,l); p[sw]=l; }\n\t\t\t\tif (ec)\n\t\t\t\t\t{\n\t\t\t\t\tc2l_p(data,l,ec);\n\t\t\t\t\tp[sw]=l;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\treturn;\n\t\t\t}\n\t\t}\n#ifdef L_ENDIAN\n\tif ((((unsigned long)data)%sizeof(ULONG)) == 0)\n\t\t{\n\t\tsw=(int)len/MD5_CBLOCK;\n\t\tif (sw > 0)\n\t\t\t{\n\t\t\tsw*=MD5_CBLOCK;\n\t\t\tmd5_block(c,(ULONG *)data,sw);\n\t\t\tdata+=sw;\n\t\t\tlen-=sw;\n\t\t\t}\n\t\t}\n#endif\n\tp=c->data;\n\twhile (len >= MD5_CBLOCK)\n\t\t{\n#if defined(L_ENDIAN) || defined(B_ENDIAN)\n\t\tif (p != (unsigned long *)data)\n\t\t\tmemcpy(p,data,MD5_CBLOCK);\n\t\tdata+=MD5_CBLOCK;\n#ifdef B_ENDIAN\n\t\tfor (sw=(MD5_LBLOCK/4); sw; sw--)\n\t\t\t{\n\t\t\tEndian_Reverse32(p[0]);\n\t\t\tEndian_Reverse32(p[1]);\n\t\t\tEndian_Reverse32(p[2]);\n\t\t\tEndian_Reverse32(p[3]);\n\t\t\tp+=4;\n\t\t\t}\n#endif\n#else\n\t\tfor (sw=(MD5_LBLOCK/4); sw; sw--)\n\t\t\t{\n\t\t\tc2l(data,l); *(p++)=l;\n\t\t\tc2l(data,l); *(p++)=l;\n\t\t\tc2l(data,l); *(p++)=l;\n\t\t\tc2l(data,l); *(p++)=l;\n\t\t\t}\n#endif\n\t\tp=c->data;\n\t\tmd5_block(c,p,64);\n\t\tlen-=MD5_CBLOCK;\n\t\t}\n\tsc=(int)len;\n\tc->num=sc;\n\tif (sc)\n\t\t{\n\t\tsw=sc>>2;\n#ifdef L_ENDIAN\n\t\tp[sw]=0;\n\t\tmemcpy(p,data,sc);\n#else\n\t\tsc&=0x03;\n\t\tfor ( ; sw; sw--)\n\t\t\t{ c2l(data,l); *(p++)=l; }\n\t\tc2l_p(data,l,sc);\n\t\t*p=l;\n#endif\n\t\t}\n\t}']
35,827
0
https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/crypto/bn/bn_lib.c/#L271
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['static int gf2m_Mxy(const EC_GROUP *group, const BIGNUM *x, const BIGNUM *y,\n BIGNUM *x1, BIGNUM *z1, BIGNUM *x2, BIGNUM *z2,\n BN_CTX *ctx)\n{\n BIGNUM *t3, *t4, *t5;\n int ret = 0;\n if (BN_is_zero(z1)) {\n BN_zero(x2);\n BN_zero(z2);\n return 1;\n }\n if (BN_is_zero(z2)) {\n if (!BN_copy(x2, x))\n return 0;\n if (!BN_GF2m_add(z2, x, y))\n return 0;\n return 2;\n }\n BN_CTX_start(ctx);\n t3 = BN_CTX_get(ctx);\n t4 = BN_CTX_get(ctx);\n t5 = BN_CTX_get(ctx);\n if (t5 == NULL)\n goto err;\n if (!BN_one(t5))\n goto err;\n if (!group->meth->field_mul(group, t3, z1, z2, ctx))\n goto err;\n if (!group->meth->field_mul(group, z1, z1, x, ctx))\n goto err;\n if (!BN_GF2m_add(z1, z1, x1))\n goto err;\n if (!group->meth->field_mul(group, z2, z2, x, ctx))\n goto err;\n if (!group->meth->field_mul(group, x1, z2, x1, ctx))\n goto err;\n if (!BN_GF2m_add(z2, z2, x2))\n goto err;\n if (!group->meth->field_mul(group, z2, z2, z1, ctx))\n goto err;\n if (!group->meth->field_sqr(group, t4, x, ctx))\n goto err;\n if (!BN_GF2m_add(t4, t4, y))\n goto err;\n if (!group->meth->field_mul(group, t4, t4, t3, ctx))\n goto err;\n if (!BN_GF2m_add(t4, t4, z2))\n goto err;\n if (!group->meth->field_mul(group, t3, t3, x, ctx))\n goto err;\n if (!group->meth->field_div(group, t3, t5, t3, ctx))\n goto err;\n if (!group->meth->field_mul(group, t4, t3, t4, ctx))\n goto err;\n if (!group->meth->field_mul(group, x2, x1, t3, ctx))\n goto err;\n if (!BN_GF2m_add(z2, x2, x))\n goto err;\n if (!group->meth->field_mul(group, z2, z2, t4, ctx))\n goto err;\n if (!BN_GF2m_add(z2, z2, y))\n goto err;\n ret = 2;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
35,828
0
https://github.com/libav/libav/blob/2c8077621b6466da205ba26fd20a9c906bb71893/libavcodec/snow.c/#L1034
static void horizontal_compose53i(IDWTELEM *b, int width){ IDWTELEM temp[width]; const int width2= width>>1; const int w2= (width+1)>>1; int x; for(x=0; x<width2; x++){ temp[2*x ]= b[x ]; temp[2*x + 1]= b[x+w2]; } if(width&1) temp[2*x ]= b[x ]; b[0] = temp[0] - ((temp[1]+1)>>1); for(x=2; x<width-1; x+=2){ b[x ] = temp[x ] - ((temp[x-1] + temp[x+1]+2)>>2); b[x-1] = temp[x-1] + ((b [x-2] + b [x ]+1)>>1); } if(width&1){ b[x ] = temp[x ] - ((temp[x-1]+1)>>1); b[x-1] = temp[x-1] + ((b [x-2] + b [x ]+1)>>1); }else b[x-1] = temp[x-1] + b[x-2]; }
['static void horizontal_compose53i(IDWTELEM *b, int width){\n IDWTELEM temp[width];\n const int width2= width>>1;\n const int w2= (width+1)>>1;\n int x;\n for(x=0; x<width2; x++){\n temp[2*x ]= b[x ];\n temp[2*x + 1]= b[x+w2];\n }\n if(width&1)\n temp[2*x ]= b[x ];\n b[0] = temp[0] - ((temp[1]+1)>>1);\n for(x=2; x<width-1; x+=2){\n b[x ] = temp[x ] - ((temp[x-1] + temp[x+1]+2)>>2);\n b[x-1] = temp[x-1] + ((b [x-2] + b [x ]+1)>>1);\n }\n if(width&1){\n b[x ] = temp[x ] - ((temp[x-1]+1)>>1);\n b[x-1] = temp[x-1] + ((b [x-2] + b [x ]+1)>>1);\n }else\n b[x-1] = temp[x-1] + b[x-2];\n}']
35,829
0
https://github.com/openssl/openssl/blob/1dc920c8de5b7109727a21163843feecdf06a8cf/crypto/objects/obj_dat.c/#L479
int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name) { int i,idx=0,n=0,len,nid; unsigned long l; unsigned char *p; const char *s; char tbuf[DECIMAL_SIZE(i)+DECIMAL_SIZE(l)+2]; if (buf_len <= 0) return(0); if ((a == NULL) || (a->data == NULL)) { buf[0]='\0'; return(0); } if (no_name || (nid=OBJ_obj2nid(a)) == NID_undef) { len=a->length; p=a->data; idx=0; l=0; while (idx < a->length) { l|=(p[idx]&0x7f); if (!(p[idx] & 0x80)) break; l<<=7L; idx++; } idx++; i=(int)(l/40); if (i > 2) i=2; l-=(long)(i*40); sprintf(tbuf,"%d.%lu",i,l); i=strlen(tbuf); strncpy(buf,tbuf,buf_len); buf_len-=i; buf+=i; n+=i; l=0; for (; idx<len; idx++) { l|=p[idx]&0x7f; if (!(p[idx] & 0x80)) { sprintf(tbuf,".%lu",l); i=strlen(tbuf); if (buf_len > 0) strncpy(buf,tbuf,buf_len); buf_len-=i; buf+=i; n+=i; l=0; } l<<=7L; } } else { s=OBJ_nid2ln(nid); if (s == NULL) s=OBJ_nid2sn(nid); strncpy(buf,s,buf_len); n=strlen(s); } buf[buf_len-1]='\0'; return(n); }
['EVP_PKEY *EVP_PKCS82PKEY (PKCS8_PRIV_KEY_INFO *p8)\n{\n\tEVP_PKEY *pkey = NULL;\n#ifndef OPENSSL_NO_RSA\n\tRSA *rsa = NULL;\n#endif\n#ifndef OPENSSL_NO_DSA\n\tDSA *dsa = NULL;\n#endif\n#ifndef OPENSSL_NO_ECDSA\n\tECDSA *ecdsa = NULL;\n#endif\n#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)\n\tASN1_INTEGER *privkey;\n\tASN1_TYPE *t1, *t2, *param = NULL;\n\tSTACK_OF(ASN1_TYPE) *n_stack = NULL;\n\tBN_CTX *ctx = NULL;\n\tint plen;\n#endif\n\tX509_ALGOR *a;\n\tunsigned char *p;\n\tconst unsigned char *cp;\n\tint pkeylen;\n\tint nid;\n\tchar obj_tmp[80];\n\tif(p8->pkey->type == V_ASN1_OCTET_STRING) {\n\t\tp8->broken = PKCS8_OK;\n\t\tp = p8->pkey->value.octet_string->data;\n\t\tpkeylen = p8->pkey->value.octet_string->length;\n\t} else {\n\t\tp8->broken = PKCS8_NO_OCTET;\n\t\tp = p8->pkey->value.sequence->data;\n\t\tpkeylen = p8->pkey->value.sequence->length;\n\t}\n\tif (!(pkey = EVP_PKEY_new())) {\n\t\tEVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\ta = p8->pkeyalg;\n\tnid = OBJ_obj2nid(a->algorithm);\n\tswitch(nid)\n\t{\n#ifndef OPENSSL_NO_RSA\n\t\tcase NID_rsaEncryption:\n\t\tcp = p;\n\t\tif (!(rsa = d2i_RSAPrivateKey (NULL,&cp, pkeylen))) {\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\treturn NULL;\n\t\t}\n\t\tEVP_PKEY_assign_RSA (pkey, rsa);\n\t\tbreak;\n#endif\n#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)\n\t\tcase NID_ecdsa_with_SHA1:\n\t\tcase NID_dsa:\n\t\tif(*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED))\n\t\t{\n\t\t \tif(!(n_stack = ASN1_seq_unpack_ASN1_TYPE(p, pkeylen,\n\t\t\t\t\t\t\t d2i_ASN1_TYPE,\n\t\t\t\t\t\t\t ASN1_TYPE_free)))\n\t\t\t{\n\t\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\t\tgoto err;\n\t\t \t}\n\t\t \tif(sk_ASN1_TYPE_num(n_stack) != 2 )\n\t\t\t{\n\t\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\t\tgoto err;\n\t\t \t}\n\t\t t1 = sk_ASN1_TYPE_value(n_stack, 0);\n\t\t t2 = sk_ASN1_TYPE_value(n_stack, 1);\n\t\t if(t1->type == V_ASN1_SEQUENCE)\n\t\t {\n\t\t\tp8->broken = PKCS8_EMBEDDED_PARAM;\n\t\t\tparam = t1;\n\t\t }\n\t\t else if(a->parameter->type == V_ASN1_SEQUENCE)\n\t\t {\n\t\t\tp8->broken = PKCS8_NS_DB;\n\t\t\tparam = a->parameter;\n\t\t }\n\t\t else\n\t\t {\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\tgoto err;\n\t\t }\n\t\t if(t2->type != V_ASN1_INTEGER) {\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\tgoto err;\n\t\t }\n\t\t privkey = t2->value.integer;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (!(privkey=d2i_ASN1_INTEGER (NULL, &p, pkeylen)))\n\t\t\t{\n\t\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t\tparam = p8->pkeyalg->parameter;\n\t\t}\n\t\tif (!param || (param->type != V_ASN1_SEQUENCE))\n\t\t{\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\tgoto err;\n\t\t}\n\t\tcp = p = param->value.sequence->data;\n\t\tplen = param->value.sequence->length;\n\t\tif (!(ctx = BN_CTX_new()))\n\t\t{\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto err;\n\t\t}\n\t\tif (nid == NID_dsa)\n\t\t{\n#ifndef OPENSSL_NO_DSA\n\t\t\tif (!(dsa = d2i_DSAparams (NULL, &cp, plen)))\n\t\t\t{\n\t\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t\tif (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL)))\n\t\t\t{\n\t\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_DECODE_ERROR);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t\tif (!(dsa->pub_key = BN_new()))\n\t\t\t{\n\t\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t\tif (!BN_mod_exp(dsa->pub_key, dsa->g,\n\t\t\t\t\t\t dsa->priv_key, dsa->p, ctx))\n\t\t\t{\n\t\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_PUBKEY_ERROR);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t\tEVP_PKEY_assign_DSA(pkey, dsa);\n\t\t\tBN_CTX_free(ctx);\n\t\t\tif(n_stack) sk_ASN1_TYPE_pop_free(n_stack, ASN1_TYPE_free);\n\t\t\telse ASN1_INTEGER_free(privkey);\n#else\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);\n\t\t\tgoto err;\n#endif\n\t\t}\n\t\telse\n\t\t{\n#ifndef OPENSSL_NO_ECDSA\n\t\t\tif ((ecdsa = d2i_ECDSAParameters(NULL, &cp, plen)) == NULL)\n\t\t\t{\n\t\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t\tif ((ecdsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL)) == NULL)\n\t\t\t{\n\t\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t\tif ((ecdsa->pub_key = EC_POINT_new(ecdsa->group)) == NULL)\n\t\t\t{\n\t\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t\tif (!EC_POINT_copy(ecdsa->pub_key, EC_GROUP_get0_generator(ecdsa->group)))\n\t\t\t{\n\t\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t\tif (!EC_POINT_mul(ecdsa->group, ecdsa->pub_key, ecdsa->priv_key,\n\t\t\t\t\t NULL, NULL, ctx))\n\t\t\t{\n\t\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t\tEVP_PKEY_assign_ECDSA(pkey, ecdsa);\n\t\t\tBN_CTX_free(ctx);\n\t\t\tif (n_stack) sk_ASN1_TYPE_pop_free(n_stack, ASN1_TYPE_free);\n\t\t\telse\n\t\t\t\tASN1_INTEGER_free(privkey);\n#else\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);\n\t\t\tgoto err;\n#endif\n\t\t}\n\t\tbreak;\nerr:\n\t\tif (ctx) BN_CTX_free(ctx);\n\t\tsk_ASN1_TYPE_pop_free(n_stack, ASN1_TYPE_free);\n#ifndef OPENSSL_NO_DSA\n\t\tif (dsa) DSA_free(dsa);\n#endif\n#ifndef OPENSSL_NO_ECDSA\n\t\tif (ecdsa) ECDSA_free(ecdsa);\n#endif\n\t\tif (pkey) EVP_PKEY_free(pkey);\n\t\treturn NULL;\n\t\tbreak;\n#endif\n\t\tdefault:\n\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);\n\t\tif (!a->algorithm) strcpy (obj_tmp, "NULL");\n\t\telse i2t_ASN1_OBJECT(obj_tmp, 80, a->algorithm);\n\t\tERR_add_error_data(2, "TYPE=", obj_tmp);\n\t\tEVP_PKEY_free (pkey);\n\t\treturn NULL;\n\t}\n\treturn pkey;\n}', 'int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)\n{\n\treturn OBJ_obj2txt(buf, buf_len, a, 0);\n}', 'int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)\n{\n\tint i,idx=0,n=0,len,nid;\n\tunsigned long l;\n\tunsigned char *p;\n\tconst char *s;\n\tchar tbuf[DECIMAL_SIZE(i)+DECIMAL_SIZE(l)+2];\n\tif (buf_len <= 0) return(0);\n\tif ((a == NULL) || (a->data == NULL)) {\n\t\tbuf[0]=\'\\0\';\n\t\treturn(0);\n\t}\n\tif (no_name || (nid=OBJ_obj2nid(a)) == NID_undef) {\n\t\tlen=a->length;\n\t\tp=a->data;\n\t\tidx=0;\n\t\tl=0;\n\t\twhile (idx < a->length) {\n\t\t\tl|=(p[idx]&0x7f);\n\t\t\tif (!(p[idx] & 0x80)) break;\n\t\t\tl<<=7L;\n\t\t\tidx++;\n\t\t}\n\t\tidx++;\n\t\ti=(int)(l/40);\n\t\tif (i > 2) i=2;\n\t\tl-=(long)(i*40);\n\t\tsprintf(tbuf,"%d.%lu",i,l);\n\t\ti=strlen(tbuf);\n\t\tstrncpy(buf,tbuf,buf_len);\n\t\tbuf_len-=i;\n\t\tbuf+=i;\n\t\tn+=i;\n\t\tl=0;\n\t\tfor (; idx<len; idx++) {\n\t\t\tl|=p[idx]&0x7f;\n\t\t\tif (!(p[idx] & 0x80)) {\n\t\t\t\tsprintf(tbuf,".%lu",l);\n\t\t\t\ti=strlen(tbuf);\n\t\t\t\tif (buf_len > 0)\n\t\t\t\t\tstrncpy(buf,tbuf,buf_len);\n\t\t\t\tbuf_len-=i;\n\t\t\t\tbuf+=i;\n\t\t\t\tn+=i;\n\t\t\t\tl=0;\n\t\t\t}\n\t\t\tl<<=7L;\n\t\t}\n\t} else {\n\t\ts=OBJ_nid2ln(nid);\n\t\tif (s == NULL)\n\t\t\ts=OBJ_nid2sn(nid);\n\t\tstrncpy(buf,s,buf_len);\n\t\tn=strlen(s);\n\t}\n\tbuf[buf_len-1]=\'\\0\';\n\treturn(n);\n}']
35,830
0
https://github.com/openssl/openssl/blob/57108f0ad573347af91681568dc790f606fd0a5b/crypto/x509v3/v3_purp.c/#L121
int X509_check_purpose(X509 *x, int id, int ca) { int idx; const X509_PURPOSE *pt; if(!(x->ex_flags & EXFLAG_SET)) { CRYPTO_w_lock(CRYPTO_LOCK_X509); x509v3_cache_extensions(x); CRYPTO_w_unlock(CRYPTO_LOCK_X509); } if(id == -1) return 1; idx = X509_PURPOSE_get_by_id(id); if(idx == -1) return -1; pt = X509_PURPOSE_get0(idx); return pt->check_purpose(pt, x, ca); }
['int X509_check_purpose(X509 *x, int id, int ca)\n{\n\tint idx;\n\tconst X509_PURPOSE *pt;\n\tif(!(x->ex_flags & EXFLAG_SET)) {\n\t\tCRYPTO_w_lock(CRYPTO_LOCK_X509);\n\t\tx509v3_cache_extensions(x);\n\t\tCRYPTO_w_unlock(CRYPTO_LOCK_X509);\n\t}\n\tif(id == -1) return 1;\n\tidx = X509_PURPOSE_get_by_id(id);\n\tif(idx == -1) return -1;\n\tpt = X509_PURPOSE_get0(idx);\n\treturn pt->check_purpose(pt, x, ca);\n}', 'void CRYPTO_lock(int mode, int type, const char *file, int line)\n\t{\n#ifdef LOCK_DEBUG\n\t\t{\n\t\tchar *rw_text,*operation_text;\n\t\tif (mode & CRYPTO_LOCK)\n\t\t\toperation_text="lock ";\n\t\telse if (mode & CRYPTO_UNLOCK)\n\t\t\toperation_text="unlock";\n\t\telse\n\t\t\toperation_text="ERROR ";\n\t\tif (mode & CRYPTO_READ)\n\t\t\trw_text="r";\n\t\telse if (mode & CRYPTO_WRITE)\n\t\t\trw_text="w";\n\t\telse\n\t\t\trw_text="ERROR";\n\t\tfprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\\n",\n\t\t\tCRYPTO_thread_id(), rw_text, operation_text,\n\t\t\tCRYPTO_get_lock_name(type), file, line);\n\t\t}\n#endif\n\tif (type < 0)\n\t\t{\n\t\tint i = -type - 1;\n\t\tstruct CRYPTO_dynlock_value *pointer\n\t\t\t= CRYPTO_get_dynlock_value(i);\n\t\tif (pointer)\n\t\t\t{\n\t\t\tdynlock_lock_callback(mode, pointer, file, line);\n\t\t\t}\n\t\tCRYPTO_destroy_dynlockid(i);\n\t\t}\n\telse\n\t\tif (locking_callback != NULL)\n\t\t\tlocking_callback(mode,type,file,line);\n\t}', 'static void x509v3_cache_extensions(X509 *x)\n{\n\tBASIC_CONSTRAINTS *bs;\n\tASN1_BIT_STRING *usage;\n\tASN1_BIT_STRING *ns;\n\tEXTENDED_KEY_USAGE *extusage;\n\tint i;\n\tif(x->ex_flags & EXFLAG_SET) return;\n#ifndef NO_SHA\n\tX509_digest(x, EVP_sha1(), x->sha1_hash, NULL);\n#endif\n\tif(!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)))\n\t\t\t x->ex_flags |= EXFLAG_SS;\n\tif(!X509_get_version(x)) x->ex_flags |= EXFLAG_V1;\n\tif((bs=X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL))) {\n\t\tif(bs->ca) x->ex_flags |= EXFLAG_CA;\n\t\tif(bs->pathlen) {\n\t\t\tif((bs->pathlen->type == V_ASN1_NEG_INTEGER)\n\t\t\t\t\t\t|| !bs->ca) {\n\t\t\t\tx->ex_flags |= EXFLAG_INVALID;\n\t\t\t\tx->ex_pathlen = 0;\n\t\t\t} else x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);\n\t\t} else x->ex_pathlen = -1;\n\t\tBASIC_CONSTRAINTS_free(bs);\n\t\tx->ex_flags |= EXFLAG_BCONS;\n\t}\n\tif((usage=X509_get_ext_d2i(x, NID_key_usage, NULL, NULL))) {\n\t\tif(usage->length > 0) {\n\t\t\tx->ex_kusage = usage->data[0];\n\t\t\tif(usage->length > 1)\n\t\t\t\tx->ex_kusage |= usage->data[1] << 8;\n\t\t} else x->ex_kusage = 0;\n\t\tx->ex_flags |= EXFLAG_KUSAGE;\n\t\tASN1_BIT_STRING_free(usage);\n\t}\n\tx->ex_xkusage = 0;\n\tif((extusage=X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL))) {\n\t\tx->ex_flags |= EXFLAG_XKUSAGE;\n\t\tfor(i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {\n\t\t\tswitch(OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage,i))) {\n\t\t\t\tcase NID_server_auth:\n\t\t\t\tx->ex_xkusage |= XKU_SSL_SERVER;\n\t\t\t\tbreak;\n\t\t\t\tcase NID_client_auth:\n\t\t\t\tx->ex_xkusage |= XKU_SSL_CLIENT;\n\t\t\t\tbreak;\n\t\t\t\tcase NID_email_protect:\n\t\t\t\tx->ex_xkusage |= XKU_SMIME;\n\t\t\t\tbreak;\n\t\t\t\tcase NID_code_sign:\n\t\t\t\tx->ex_xkusage |= XKU_CODE_SIGN;\n\t\t\t\tbreak;\n\t\t\t\tcase NID_ms_sgc:\n\t\t\t\tcase NID_ns_sgc:\n\t\t\t\tx->ex_xkusage |= XKU_SGC;\n\t\t\t\tbreak;\n\t\t\t\tcase NID_OCSP_sign:\n\t\t\t\tx->ex_xkusage |= XKU_OCSP_SIGN;\n\t\t\t\tbreak;\n\t\t\t\tcase NID_time_stamp:\n\t\t\t\tx->ex_xkusage |= XKU_TIMESTAMP;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tsk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);\n\t}\n\tif((ns=X509_get_ext_d2i(x, NID_netscape_cert_type, NULL, NULL))) {\n\t\tif(ns->length > 0) x->ex_nscert = ns->data[0];\n\t\telse x->ex_nscert = 0;\n\t\tx->ex_flags |= EXFLAG_NSCERT;\n\t\tASN1_BIT_STRING_free(ns);\n\t}\n\tx->skid =X509_get_ext_d2i(x, NID_subject_key_identifier, NULL, NULL);\n\tx->akid =X509_get_ext_d2i(x, NID_authority_key_identifier, NULL, NULL);\n\tx->ex_flags |= EXFLAG_SET;\n}', 'int X509_PURPOSE_get_by_id(int purpose)\n{\n\tX509_PURPOSE tmp;\n\tint idx;\n\tif((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))\n\t\treturn purpose - X509_PURPOSE_MIN;\n\ttmp.purpose = purpose;\n\tif(!xptable) return -1;\n\tidx = sk_X509_PURPOSE_find(xptable, &tmp);\n\tif(idx == -1) return -1;\n\treturn idx + X509_PURPOSE_COUNT;\n}', 'X509_PURPOSE * X509_PURPOSE_get0(int idx)\n{\n\tif(idx < 0) return NULL;\n\tif(idx < X509_PURPOSE_COUNT) return xstandard + idx;\n\treturn sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);\n}']
35,831
0
https://github.com/openssl/openssl/blob/1a50eedf2a1fbb1e0e009ad616d8be678e4c6340/crypto/bn/bn_lib.c/#L232
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,\n const EC_POINT *pub_key, const EC_KEY *ecdh)\n{\n BN_CTX *ctx;\n EC_POINT *tmp = NULL;\n BIGNUM *x = NULL;\n const BIGNUM *priv_key;\n const EC_GROUP *group;\n int ret = 0;\n size_t buflen, len;\n unsigned char *buf = NULL;\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n x = BN_CTX_get(ctx);\n if (x == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n priv_key = EC_KEY_get0_private_key(ecdh);\n if (priv_key == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_NO_PRIVATE_VALUE);\n goto err;\n }\n group = EC_KEY_get0_group(ecdh);\n if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {\n if (!EC_GROUP_get_cofactor(group, x, NULL) ||\n !BN_mul(x, x, priv_key, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n priv_key = x;\n }\n if ((tmp = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);\n goto err;\n }\n if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==\n NID_X9_62_prime_field) {\n if (!EC_POINT_get_affine_coordinates_GFp(group, tmp, x, NULL, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);\n goto err;\n }\n }\n#ifndef OPENSSL_NO_EC2M\n else {\n if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp, x, NULL, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);\n goto err;\n }\n }\n#endif\n buflen = (EC_GROUP_get_degree(group) + 7) / 8;\n len = BN_num_bytes(x);\n if (len > buflen) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((buf = OPENSSL_malloc(buflen)) == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memset(buf, 0, buflen - len);\n if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_BN_LIB);\n goto err;\n }\n *pout = buf;\n *poutlen = buflen;\n buf = NULL;\n ret = 1;\n err:\n EC_POINT_free(tmp);\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n OPENSSL_free(buf);\n return ret;\n}', 'int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,\n const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)\n{\n const EC_POINT *points[1];\n const BIGNUM *scalars[1];\n points[0] = point;\n scalars[0] = p_scalar;\n return EC_POINTs_mul(group, r, g_scalar,\n (point != NULL\n && p_scalar != NULL), points, scalars, ctx);\n}', 'int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[],\n const BIGNUM *scalars[], BN_CTX *ctx)\n{\n int ret = 0;\n size_t i = 0;\n BN_CTX *new_ctx = NULL;\n if ((scalar == NULL) && (num == 0)) {\n return EC_POINT_set_to_infinity(group, r);\n }\n if (!ec_point_is_compat(r, group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n for (i = 0; i < num; i++) {\n if (!ec_point_is_compat(points[i], group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n }\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {\n ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (group->meth->mul != NULL)\n ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);\n else\n ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[], const BIGNUM *scalars[],\n BN_CTX *ctx)\n{\n const EC_POINT *generator = NULL;\n EC_POINT *tmp = NULL;\n size_t totalnum;\n size_t blocksize = 0, numblocks = 0;\n size_t pre_points_per_block = 0;\n size_t i, j;\n int k;\n int r_is_inverted = 0;\n int r_is_at_infinity = 1;\n size_t *wsize = NULL;\n signed char **wNAF = NULL;\n size_t *wNAF_len = NULL;\n size_t max_len = 0;\n size_t num_val;\n EC_POINT **val = NULL;\n EC_POINT **v;\n EC_POINT ***val_sub = NULL;\n const EC_PRE_COMP *pre_comp = NULL;\n int num_scalar = 0;\n int ret = 0;\n if (!BN_is_zero(group->order) && !BN_is_zero(group->cofactor)) {\n if ((scalar != NULL) && (num == 0)) {\n return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);\n }\n if ((scalar == NULL) && (num == 1)) {\n return ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx);\n }\n }\n if (scalar != NULL) {\n generator = EC_GROUP_get0_generator(group);\n if (generator == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);\n goto err;\n }\n pre_comp = group->pre_comp.ec;\n if (pre_comp && pre_comp->numblocks\n && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==\n 0)) {\n blocksize = pre_comp->blocksize;\n numblocks = (BN_num_bits(scalar) / blocksize) + 1;\n if (numblocks > pre_comp->numblocks)\n numblocks = pre_comp->numblocks;\n pre_points_per_block = (size_t)1 << (pre_comp->w - 1);\n if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n pre_comp = NULL;\n numblocks = 1;\n num_scalar = 1;\n }\n }\n totalnum = num + numblocks;\n wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));\n wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));\n wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));\n val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));\n if (wNAF != NULL)\n wNAF[0] = NULL;\n if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n num_val = 0;\n for (i = 0; i < num + num_scalar; i++) {\n size_t bits;\n bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);\n wsize[i] = EC_window_bits_for_scalar_size(bits);\n num_val += (size_t)1 << (wsize[i] - 1);\n wNAF[i + 1] = NULL;\n wNAF[i] =\n bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],\n &wNAF_len[i]);\n if (wNAF[i] == NULL)\n goto err;\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n }\n if (numblocks) {\n if (pre_comp == NULL) {\n if (num_scalar != 1) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n signed char *tmp_wNAF = NULL;\n size_t tmp_len = 0;\n if (num_scalar != 0) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n wsize[num] = pre_comp->w;\n tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);\n if (!tmp_wNAF)\n goto err;\n if (tmp_len <= max_len) {\n numblocks = 1;\n totalnum = num + 1;\n wNAF[num] = tmp_wNAF;\n wNAF[num + 1] = NULL;\n wNAF_len[num] = tmp_len;\n val_sub[num] = pre_comp->points;\n } else {\n signed char *pp;\n EC_POINT **tmp_points;\n if (tmp_len < numblocks * blocksize) {\n numblocks = (tmp_len + blocksize - 1) / blocksize;\n if (numblocks > pre_comp->numblocks) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n totalnum = num + numblocks;\n }\n pp = tmp_wNAF;\n tmp_points = pre_comp->points;\n for (i = num; i < totalnum; i++) {\n if (i < totalnum - 1) {\n wNAF_len[i] = blocksize;\n if (tmp_len < blocksize) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n tmp_len -= blocksize;\n } else\n wNAF_len[i] = tmp_len;\n wNAF[i + 1] = NULL;\n wNAF[i] = OPENSSL_malloc(wNAF_len[i]);\n if (wNAF[i] == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n memcpy(wNAF[i], pp, wNAF_len[i]);\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n if (*tmp_points == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n val_sub[i] = tmp_points;\n tmp_points += pre_points_per_block;\n pp += blocksize;\n }\n OPENSSL_free(tmp_wNAF);\n }\n }\n }\n val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));\n if (val == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n val[num_val] = NULL;\n v = val;\n for (i = 0; i < num + num_scalar; i++) {\n val_sub[i] = v;\n for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n *v = EC_POINT_new(group);\n if (*v == NULL)\n goto err;\n v++;\n }\n }\n if (!(v == val + num_val)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((tmp = EC_POINT_new(group)) == NULL)\n goto err;\n for (i = 0; i < num + num_scalar; i++) {\n if (i < num) {\n if (!EC_POINT_copy(val_sub[i][0], points[i]))\n goto err;\n } else {\n if (!EC_POINT_copy(val_sub[i][0], generator))\n goto err;\n }\n if (wsize[i] > 1) {\n if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))\n goto err;\n for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n if (!EC_POINT_add\n (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))\n goto err;\n }\n }\n }\n if (!EC_POINTs_make_affine(group, num_val, val, ctx))\n goto err;\n r_is_at_infinity = 1;\n for (k = max_len - 1; k >= 0; k--) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_dbl(group, r, r, ctx))\n goto err;\n }\n for (i = 0; i < totalnum; i++) {\n if (wNAF_len[i] > (size_t)k) {\n int digit = wNAF[i][k];\n int is_neg;\n if (digit) {\n is_neg = digit < 0;\n if (is_neg)\n digit = -digit;\n if (is_neg != r_is_inverted) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n r_is_inverted = !r_is_inverted;\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))\n goto err;\n r_is_at_infinity = 0;\n } else {\n if (!EC_POINT_add\n (group, r, r, val_sub[i][digit >> 1], ctx))\n goto err;\n }\n }\n }\n }\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_set_to_infinity(group, r))\n goto err;\n } else {\n if (r_is_inverted)\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n ret = 1;\n err:\n EC_POINT_free(tmp);\n OPENSSL_free(wsize);\n OPENSSL_free(wNAF_len);\n if (wNAF != NULL) {\n signed char **w;\n for (w = wNAF; *w != NULL; w++)\n OPENSSL_free(*w);\n OPENSSL_free(wNAF);\n }\n if (val != NULL) {\n for (v = val; *v != NULL; v++)\n EC_POINT_clear_free(*v);\n OPENSSL_free(val);\n }\n OPENSSL_free(val_sub);\n return ret;\n}', 'int ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,\n const BIGNUM *scalar, const EC_POINT *point,\n BN_CTX *ctx)\n{\n int i, cardinality_bits, group_top, kbit, pbit, Z_is_one;\n EC_POINT *p = NULL;\n EC_POINT *s = NULL;\n BIGNUM *k = NULL;\n BIGNUM *lambda = NULL;\n BIGNUM *cardinality = NULL;\n int ret = 0;\n if (point != NULL && EC_POINT_is_at_infinity(group, point))\n return EC_POINT_set_to_infinity(group, r);\n if (BN_is_zero(group->order)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_UNKNOWN_ORDER);\n return 0;\n }\n if (BN_is_zero(group->cofactor)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_UNKNOWN_COFACTOR);\n return 0;\n }\n BN_CTX_start(ctx);\n if (((p = EC_POINT_new(group)) == NULL)\n || ((s = EC_POINT_new(group)) == NULL)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (point == NULL) {\n if (!EC_POINT_copy(p, group->generator)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_EC_LIB);\n goto err;\n }\n } else {\n if (!EC_POINT_copy(p, point)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_EC_LIB);\n goto err;\n }\n }\n EC_POINT_BN_set_flags(p, BN_FLG_CONSTTIME);\n EC_POINT_BN_set_flags(r, BN_FLG_CONSTTIME);\n EC_POINT_BN_set_flags(s, BN_FLG_CONSTTIME);\n cardinality = BN_CTX_get(ctx);\n lambda = BN_CTX_get(ctx);\n k = BN_CTX_get(ctx);\n if (k == NULL) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!BN_mul(cardinality, group->order, group->cofactor, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n cardinality_bits = BN_num_bits(cardinality);\n group_top = bn_get_top(cardinality);\n if ((bn_wexpand(k, group_top + 1) == NULL)\n || (bn_wexpand(lambda, group_top + 1) == NULL)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_copy(k, scalar)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n BN_set_flags(k, BN_FLG_CONSTTIME);\n if ((BN_num_bits(k) > cardinality_bits) || (BN_is_negative(k))) {\n if (!BN_nnmod(k, k, cardinality, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n }\n if (!BN_add(lambda, k, cardinality)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n BN_set_flags(lambda, BN_FLG_CONSTTIME);\n if (!BN_add(k, lambda, cardinality)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n kbit = BN_is_bit_set(lambda, cardinality_bits);\n BN_consttime_swap(kbit, k, lambda, group_top + 1);\n group_top = bn_get_top(group->field);\n if ((bn_wexpand(s->X, group_top) == NULL)\n || (bn_wexpand(s->Y, group_top) == NULL)\n || (bn_wexpand(s->Z, group_top) == NULL)\n || (bn_wexpand(r->X, group_top) == NULL)\n || (bn_wexpand(r->Y, group_top) == NULL)\n || (bn_wexpand(r->Z, group_top) == NULL)\n || (bn_wexpand(p->X, group_top) == NULL)\n || (bn_wexpand(p->Y, group_top) == NULL)\n || (bn_wexpand(p->Z, group_top) == NULL)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n if (!ec_point_blind_coordinates(group, p, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_POINT_COORDINATES_BLIND_FAILURE);\n goto err;\n }\n if (!ec_point_ladder_pre(group, r, s, p, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_LADDER_PRE_FAILURE);\n goto err;\n }\n pbit = 1;\n#define EC_POINT_CSWAP(c, a, b, w, t) do { \\\n BN_consttime_swap(c, (a)->X, (b)->X, w); \\\n BN_consttime_swap(c, (a)->Y, (b)->Y, w); \\\n BN_consttime_swap(c, (a)->Z, (b)->Z, w); \\\n t = ((a)->Z_is_one ^ (b)->Z_is_one) & (c); \\\n (a)->Z_is_one ^= (t); \\\n (b)->Z_is_one ^= (t); \\\n} while(0)\n for (i = cardinality_bits - 1; i >= 0; i--) {\n kbit = BN_is_bit_set(k, i) ^ pbit;\n EC_POINT_CSWAP(kbit, r, s, group_top, Z_is_one);\n if (!ec_point_ladder_step(group, r, s, p, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_LADDER_STEP_FAILURE);\n goto err;\n }\n pbit ^= kbit;\n }\n EC_POINT_CSWAP(pbit, r, s, group_top, Z_is_one);\n#undef EC_POINT_CSWAP\n if (!ec_point_ladder_post(group, r, s, p, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_LADDER_POST_FAILURE);\n goto err;\n }\n ret = 1;\n err:\n EC_POINT_free(p);\n EC_POINT_free(s);\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
35,832
0
https://github.com/openssl/openssl/blob/3b5873567d24bf0d8bc2a175848e716e295d6c94/crypto/bn/bn_shift.c/#L112
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { int i, nw, lb, rb; BN_ULONG *t, *f; BN_ULONG l; bn_check_top(r); bn_check_top(a); if (n < 0) { BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT); return 0; } nw = n / BN_BITS2; if (bn_wexpand(r, a->top + nw + 1) == NULL) return 0; r->neg = a->neg; lb = n % BN_BITS2; rb = BN_BITS2 - lb; f = a->d; t = r->d; t[a->top + nw] = 0; if (lb == 0) for (i = a->top - 1; i >= 0; i--) t[nw + i] = f[i]; else for (i = a->top - 1; i >= 0; i--) { l = f[i]; t[nw + i + 1] |= (l >> rb) & BN_MASK2; t[nw + i] = (l << lb) & BN_MASK2; } memset(t, 0, sizeof(*t) * nw); r->top = a->top + nw + 1; bn_correct_top(r); bn_check_top(r); return 1; }
['static int test_rsa_mp(void)\n{\n int ret = 0;\n RSA *key;\n unsigned char ptext[512];\n unsigned char ctext[512];\n static unsigned char ptext_ex[] = "\\x54\\x85\\x9b\\x34\\x2c\\x49\\xea\\x2a";\n int plen;\n int clen = 0;\n int num;\n plen = sizeof(ptext_ex) - 1;\n key = RSA_new();\n if (!TEST_ptr(key))\n goto err;\n clen = key4096p7(key);\n if (!TEST_int_eq(clen, 512))\n goto err;\n if (!TEST_true(RSA_check_key_ex(key, NULL)))\n goto err;\n num = RSA_public_encrypt(plen, ptext_ex, ctext, key,\n RSA_PKCS1_PADDING);\n if (!TEST_int_eq(num, clen))\n goto err;\n num = RSA_private_decrypt(num, ctext, ptext, key, RSA_PKCS1_PADDING);\n if (!TEST_mem_eq(ptext, num, ptext_ex, plen))\n goto err;\n ret = 1;\nerr:\n RSA_free(key);\n return ret;\n}', 'static int key4096p7(RSA *key)\n{\n static unsigned char n1[] =\n "\\x00\\xeb\\xa2\\x2b\\x74\\x76\\x32\\x33\\xbf\\xb9\\x9a\\xe4\\xb0\\x84\\xac"\n "\\x9a\\x20\\x1b\\xe6\\x1a\\x22\\x3f\\xcd\\x13\\xe1\\x42\\xee\\xd8\\x1a\\x6e"\n "\\xe3\\xd5\\x91\\x1e\\xee\\x32\\x5c\\x78\\x76\\x8b\\x5d\\x82\\x6f\\xd0\\xaf"\n "\\xa3\\x56\\xd6\\x15\\x5f\\x64\\xb3\\x5a\\xcf\\xc8\\x7d\\xa3\\xb1\\x3d\\x10"\n "\\xbf\\x47\\x00\\xa9\\xc9\\x96\\x9d\\xfd\\x7c\\x82\\x4f\\xb5\\x70\\x39\\xd4"\n "\\xd9\\x0f\\xd7\\xd9\\x86\\xd4\\xd4\\x35\\x06\\x35\\x07\\xa3\\x1b\\x8c\\xf4"\n "\\x35\\xb8\\xb2\\xd4\\xa3\\xbe\\x6c\\x64\\x7d\\x74\\x17\\x88\\x36\\x07\\x32"\n "\\x6f\\x84\\x48\\xd4\\xb8\\x4a\\x12\\xba\\xe8\\x40\\x5b\\xc6\\xa5\\xa4\\x11"\n "\\x0d\\x8a\\xa6\\x60\\x3d\\x98\\x8b\\xf8\\x11\\x8f\\x43\\xab\\x2f\\x95\\x0f"\n "\\x3b\\x20\\x3f\\x48\\x88\\xaa\\x3d\\x20\\xa6\\xaf\\x6a\\xdf\\xc0\\x95\\x38"\n "\\xff\\x47\\x7c\\x8a\\x8b\\xec\\x76\\x45\\x43\\xd0\\x06\\x7c\\x71\\x76\\x5f"\n "\\x0d\\x11\\x58\\x0a\\xa9\\x0e\\x7e\\x07\\xe9\\x89\\xc1\\x9e\\x56\\x6d\\x08"\n "\\xe4\\xe3\\x16\\xb4\\xa0\\x03\\x7a\\x49\\x96\\xa8\\x88\\xcf\\x89\\xe3\\xdb"\n "\\x47\\x0c\\xd2\\x1b\\x6f\\x67\\x4d\\xc8\\xee\\x53\\xe9\\x0b\\x3d\\xe5\\x11"\n "\\x9d\\x85\\x75\\xfe\\x26\\x4a\\x99\\x58\\x79\\x28\\x91\\x03\\x8b\\xe4\\xbf"\n "\\x49\\xd3\\xfa\\x8e\\x25\\xe3\\xb1\\xb2\\xc2\\x33\\xd9\\x8b\\xe0\\x9d\\x1b"\n "\\xa4\\xa0\\xcf\\x05\\x5f\\xb9\\x19\\x65\\x74\\x24\\xae\\x2e\\x33\\xc8\\xd9";\n static unsigned char n2[] =\n "\\x55\\xae\\xcb\\x37\\xa1\\x3b\\x95\\xf7\\x5f\\xff\\x1f\\x06\\x4a\\xab\\x85"\n "\\x94\\x37\\x15\\x6f\\xf2\\xc8\\x9b\\x73\\xb9\\x1d\\x8e\\x30\\x63\\x5a\\xa1"\n "\\x84\\x31\\xa8\\xbe\\x7e\\x4f\\xeb\\xa0\\xe6\\x73\\xc7\\xbe\\xd4\\x5e\\xe7"\n "\\x5e\\xae\\x5d\\xfd\\xf7\\xf5\\xa4\\xb6\\xb8\\x3c\\x15\\xac\\x1b\\xcd\\x3a"\n "\\xb6\\xbd\\xa9\\x11\\xd0\\x44\\xa5\\x08\\x7e\\x60\\x1d\\x1d\\x4d\\xa6\\xd7"\n "\\x06\\xe5\\x4f\\x37\\x0d\\x5c\\xbd\\xad\\x8d\\x40\\x18\\xbd\\xc7\\x58\\x20"\n "\\xed\\xa8\\x4d\\xd8\\x29\\xdc\\x42\\x4f\\x50\\xa6\\x03\\xf0\\xa5\\xa2\\x18"\n "\\x2b\\xdd\\x1c\\x0e\\x89\\x3f\\xbc\\x5d\\x35\\xac\\xa8\\x55\\x6d\\x17\\x28"\n "\\xae\\xee\\x78\\xeb\\x6d\\x7a\\x21\\xd6\\x6c\\xdd\\x21\\x1d\\xcc\\xe5\\xb0"\n "\\xf2\\xef\\x7f\\x1d\\x8a\\xb7\\x95\\xaf\\x40\\xb5\\x5c\\x8b\\x0d\\x59\\x45"\n "\\x21\\x86\\x34\\x6d\\x13\\xd7\\x14\\xa5\\x0e\\xca\\x88\\x32\\x57\\xfe\\x2d"\n "\\xd1\\xcb\\xad\\x14\\xbe\\x30\\xb1\\x69\\x9b\\x72\\x82\\xad\\x47\\xb5\\x45"\n "\\xc8\\x65\\x83\\xce\\x07\\x21\\x15\\x6b\\x65\\x9d\\xf3\\x4d\\x92\\x19\\xbe"\n "\\x67\\x73\\xd0\\x60\\x9f\\xc7\\xef\\xee\\xfa\\xb0\\xd3\\xa4\\x95\\x81\\xb0"\n "\\xbd\\x4b\\xd5\\xf8\\xbd\\xbd\\x9f\\x41\\x41\\xc0\\xfd\\xe9\\x17\\xe3\\x16"\n "\\xd3\\xdc\\xc8\\xe2\\xa5\\xca\\xff\\xea\\x91\\xf4\\x33\\xc0\\xfb\\xdc\\x02"\n "\\x62\\x0a\\x4e\\x26\\x92\\xb5\\x13\\xec\\x43\\xd2\\x25\\x19\\xe2\\x6d\\xef"\n "\\xea\\x38\\x75";\n static unsigned char e[] = "\\x01\\x00\\x01";\n static unsigned char d1[] =\n "\\x60\\x43\\x92\\x69\\x33\\xd8\\x72\\x97\\xc3\\x25\\xea\\x83\\xca\\xd0\\x10"\n "\\xef\\x49\\x36\\x8a\\x3a\\xaf\\xc2\\x02\\x7b\\x26\\xb3\\x19\\x0a\\x43\\x7f"\n "\\x44\\xc2\\xd2\\xd6\\x11\\x31\\x01\\xed\\xbc\\x25\\xe9\\xa1\\xf0\\xa9\\xb0"\n "\\x9b\\x4b\\x3e\\xd4\\x07\\xf9\\xd6\\x01\\xc9\\x30\\xba\\xed\\x2f\\xbb\\x65"\n "\\xc9\\x86\\x15\\xd7\\x4b\\x77\\x24\\x15\\xf7\\xce\\xc4\\x9b\\x21\\x62\\x4d"\n "\\xde\\x77\\xf0\\x1e\\x62\\x49\\x54\\xda\\xda\\xcd\\xdd\\x0f\\x53\\xe2\\xd2"\n "\\x39\\x4f\\x60\\x8e\\xec\\xe3\\x49\\x1b\\xe7\\x9a\\x3e\\x17\\x8e\\x5a\\x77"\n "\\x4a\\x15\\xf6\\x57\\xc5\\xfe\\x2b\\xa5\\x94\\x04\\x94\\x9c\\xe7\\xa8\\xcc"\n "\\x07\\x4b\\xd8\\xb8\\x55\\xaa\\xdf\\x33\\xca\\x2c\\x8c\\xee\\x61\\x1a\\x89"\n "\\x0d\\x3b\\xcd\\x16\\x28\\xb5\\x50\\x62\\x2f\\xb1\\x9e\\x61\\x27\\xd7\\xf1"\n "\\xe1\\x22\\x58\\x84\\xcd\\x18\\xc3\\xbf\\xde\\x52\\x25\\xdd\\xc1\\xcd\\x2a"\n "\\x20\\x78\\x5e\\xde\\x65\\x6a\\x25\\x12\\x2d\\x78\\x28\\xdb\\x2c\\x6f\\x1b"\n "\\x65\\x71\\x1b\\x2e\\x1c\\x18\\x62\\xf6\\x71\\x82\\xdc\\xa8\\x80\\x0e\\xb5"\n "\\x8b\\x09\\x97\\xc3\\x81\\xe1\\x23\\x2e\\x40\\x75\\xbf\\xd3\\x78\\xdc\\xd5"\n "\\xc3\\xe6\\x75\\x8b\\xb0\\x48\\x7a\\x78\\x7d\\xfe\\xcc\\x4c\\xa1\\x40\\x11"\n "\\x20\\xbe\\x2a\\xc1\\xaf\\x18\\x53\\x45\\x6a\\xb4\\x2a\\x0a\\xc9\\x01\\x36"\n "\\x60\\xf1\\x61\\x97\\x73\\xde\\xac\\x3e\\x24\\xff\\x5b\\x25\\xdb\\x93\\x97";\n static unsigned char d2[] =\n "\\x03\\xf4\\xef\\x4a\\x73\\x47\\xa8\\x31\\xd1\\x63\\x5b\\xba\\xa6\\xe2\\x7a"\n "\\xd8\\xef\\xea\\x2f\\x04\\x76\\x3c\\x7a\\x8d\\x4e\\xc1\\x81\\xc5\\x39\\x4f"\n "\\x09\\xc4\\xb1\\xfb\\x75\\xd0\\xae\\x32\\x2e\\x44\\xa3\\xb6\\xdc\\x60\\xc6"\n "\\x60\\x1e\\x40\\x6f\\x04\\xc2\\x7a\\xd3\\x0f\\x8a\\xa4\\x23\\x8f\\x0e\\x2e"\n "\\x2a\\xcd\\x65\\x2a\\x6e\\x97\\xa8\\xe5\\x01\\x96\\xd1\\x41\\x27\\xa5\\x16"\n "\\x2f\\xb0\\x15\\x0a\\x33\\x06\\x72\\x40\\xa5\\xe4\\x74\\xbe\\x4a\\x87\\x28"\n "\\x8a\\x68\\xea\\x39\\x42\\x08\\x47\\xff\\x9d\\x9e\\x06\\x03\\xfa\\x4a\\x6a"\n "\\xbb\\xe5\\x6f\\x97\\x05\\x5f\\xae\\x47\\xe6\\xc4\\xe0\\x48\\x62\\x65\\x9b"\n "\\x91\\x50\\xda\\x84\\xcf\\xa3\\xb2\\x9d\\x51\\x84\\x02\\x4b\\xe6\\x12\\xa2"\n "\\x87\\x6e\\x1f\\x43\\x2f\\x90\\x36\\xb1\\x15\\xc6\\xf6\\xe0\\xde\\x52\\x39"\n "\\x8e\\x22\\x39\\x8c\\x4f\\x1c\\x08\\xac\\x55\\x5c\\xa9\\x69\\x4e\\x77\\x8d"\n "\\x74\\x6e\\x0b\\x06\\xda\\x61\\xcd\\xe2\\xc8\\xc6\\x02\\x54\\x70\\x05\\xc1"\n "\\x50\\x6d\\x5e\\x3e\\x66\\x7d\\x41\\x3c\\x57\\x68\\x8a\\x16\\xb9\\xd8\\x29"\n "\\xfb\\xeb\\x05\\x57\\x87\\xdc\\x88\\xc6\\x91\\x67\\x3c\\xcc\\x19\\x28\\xa1"\n "\\x23\\x54\\xc3\\x88\\xc4\\x28\\x63\\xca\\xf7\\xb2\\xea\\x34\\xa1\\x9e\\xc1"\n "\\xca\\x9b\\x96\\xe7\\x4d\\x82\\x22\\xf9\\xcb\\xc3\\x2a\\x3f\\x19\\xe3\\x7c"\n "\\xce\\x8c\\x66\\x45\\xfe\\xe6\\x31\\x31\\x66\\xd4\\x1a\\xb5\\x47\\x98\\xea"\n "\\x80\\x01";\n static unsigned char p[] =\n "\\x03\\xf5\\x90\\x21\\xf3\\xa6\\x13\\xdd\\x33\\x2a\\x4c\\x1f\\x53\\xd6\\x9e"\n "\\x64\\x1d\\xfc\\xed\\xfb\\xa1\\xe6\\xd0\\x80\\xd1\\x6a\\xcd\\x20\\x68\\xea"\n "\\x75\\x72\\x59\\x24\\x8b\\x1b\\x28\\x37\\xbd\\x8f\\xc4\\xc9\\xb6\\xda\\xef"\n "\\x55\\xb9\\x0e\\x26\\x93\\x8a\\x8a\\xec\\x2f\\x47\\xe6\\x4e\\xa9\\x42\\xfb"\n "\\x02\\x28\\x5e\\x37\\xb7\\x5a\\x61\\xfa\\x39\\xab\\x63\\x61\\x1b\\xbb";\n static unsigned char q[] =\n "\\x01\\xea\\x64\\x6e\\x7d\\xaa\\xd7\\x6a\\xfa\\x0c\\x52\\xfa\\x2b\\x00\\x51"\n "\\x7d\\x4d\\xc3\\x9f\\x1d\\x21\\xb2\\x62\\x11\\xd4\\x7d\\x0e\\xc2\\xe7\\xdf"\n "\\xd0\\x04\\xd6\\x30\\x02\\x54\\xb2\\xa5\\xb7\\x85\\xd5\\x96\\xc7\\x24\\x83"\n "\\x8e\\x3d\\x60\\xc3\\x42\\xa5\\xa3\\x76\\xdd\\x6e\\x60\\xa0\\xbf\\x85\\x05"\n "\\xb6\\x52\\x32\\xeb\\x41\\x6f\\x08\\xc8\\x56\\x43\\xd8\\x80\\x06\\xf7";\n static unsigned char dmp1[] =\n "\\x03\\xcc\\x5c\\xe7\\x45\\x91\\x49\\xb3\\x47\\x77\\xc7\\xa9\\xb2\\x4b\\xce"\n "\\x8e\\xab\\xfa\\x4f\\xf1\\xbd\\x63\\xeb\\x19\\xfa\\x4e\\x64\\xd6\\x37\\xf0"\n "\\xde\\x95\\xc2\\x11\\x7d\\xe6\\xa2\\xd1\\xbe\\xe9\\x23\\x58\\x85\\x35\\x4a"\n "\\xb0\\xc9\\xa5\\x5a\\xba\\xe7\\x09\\xda\\x06\\x8e\\x0a\\xd3\\xe2\\x2c\\x61"\n "\\x14\\xb3\\xd7\\x97\\xca\\x2e\\x4a\\x9a\\xbd\\x22\\xc0\\x67\\x94\\x2b";\n static unsigned char dmq1[] =\n "\\x01\\x66\\x28\\x8d\\xce\\x38\\x8d\\x76\\xb3\\x43\\x77\\x03\\x01\\x8f\\x0c"\n "\\xf5\\x40\\x6b\\x84\\x75\\x69\\x5b\\xf8\\x66\\x5f\\x54\\x2b\\x08\\xcd\\x03"\n "\\x58\\xd1\\x7f\\x81\\xb6\\xe2\\x17\\x4c\\x13\\x3a\\xab\\x21\\xa1\\x36\\x98"\n "\\xe2\\xb5\\x0f\\x4b\\xed\\x0c\\x3e\\xd4\\x1c\\xab\\x75\\xe5\\x51\\x9b\\x9c"\n "\\xed\\x69\\x21\\x89\\x52\\xd3\\xfe\\x8d\\x1a\\xfc\\x18\\x4e\\x81\\x47";\n static unsigned char iqmp[] =\n "\\x02\\x31\\xad\\xe8\\xa5\\xa9\\x5d\\x52\\x70\\x68\\xc1\\x6e\\x6b\\x4e\\xb5"\n "\\x0b\\xc2\\xd9\\x89\\xe5\\xbc\\x46\\xca\\xac\\xac\\x04\\xe2\\x39\\x7f\\xc4"\n "\\xa0\\xe4\\x9e\\x8a\\xb8\\x1e\\x67\\x66\\x85\\xb8\\x3e\\x42\\x7c\\xc7\\x2c"\n "\\x02\\x64\\x7c\\xd3\\x40\\xda\\x5d\\x9f\\x38\\xa7\\x7c\\x30\\xcd\\x6b\\x7d"\n "\\x4d\\xd0\\x77\\x2d\\x9b\\xd1\\xc3\\x55\\x03\\x69\\x3d\\xda\\x48\\xe2";\n static char *primes[] = {\n "\\x01\\xb2\\xe2\\xf7\\x3b\\x29\\xdc\\x90\\xf9\\xc4\\x5a\\x75\\x09\\x00\\xbe"\n "\\xec\\xa6\\x97\\xbc\\x47\\x89\\x24\\x71\\x48\\xa7\\xc9\\xc8\\xed\\x9c\\x88"\n "\\xb9\\x9f\\xcd\\x79\\xc9\\x2d\\xb1\\x78\\xf7\\x54\\x2c\\x28\\x01\\x37\\x01"\n "\\xa0\\xff\\xf2\\x69\\x52\\x93\\xf0\\x16\\x08\\xaa\\x2f\\xff\\x35\\x14\\x2d"\n "\\x65\\x8d\\x21\\x05\\x30\\x84\\x9b\\xcb\\x58\\x42\\xc6\\x9b\\xa7\\xb5",\n "\\x01\\xb7\\x66\\xfe\\xb5\\x94\\xf8\\x46\\xcb\\x06\\x2a\\x7a\\xd4\\x35\\xe4"\n "\\x68\\x6d\\xcf\\x58\\x0b\\xb3\\x12\\xe6\\x7b\\x66\\xee\\x36\\xf5\\x1f\\x79"\n "\\x8f\\x82\\x1e\\xd9\\x97\\x33\\x45\\x37\\xa3\\x84\\x54\\xab\\x4b\\x12\\x96"\n "\\x76\\x9f\\xf9\\x98\\x02\\xf1\\x5e\\xcb\\x97\\xe8\\x13\\xde\\x91\\x0b\\xd5"\n "\\xec\\x7f\\x2b\\x27\\x55\\xc1\\x69\\x58\\xad\\x8b\\xaa\\xce\\x68\\xc5",\n "\\x01\\xbe\\xdf\\xa5\\x33\\xfb\\xa1\\xfa\\xf6\\xd2\\x43\\xcd\\x76\\x3e\\x23"\n "\\xa2\\xf7\\x29\\xeb\\x65\\xe5\\xae\\xe8\\xfc\\xfd\\xca\\x55\\x16\\x01\\x57"\n "\\xcd\\xca\\x9e\\x83\\xf9\\x61\\x6a\\x17\\xac\\x61\\xec\\x04\\x32\\x9f\\x69"\n "\\xb8\\x6d\\x66\\x33\\x9b\\xef\\xf4\\x91\\xf1\\x99\\x02\\xc2\\xea\\x58\\x6e"\n "\\xad\\x3d\\x0e\\xa9\\x87\\x17\\x7c\\x9b\\x72\\xcc\\xab\\x1c\\x93\\x2b",\n "\\x03\\x2f\\x4a\\x9c\\x49\\x66\\x9d\\xbc\\xae\\xec\\x54\\x7d\\xe6\\x07\\x52"\n "\\xc2\\x9a\\x09\\xf5\\xa7\\x56\\xa6\\x68\\x45\\x38\\x75\\x7d\\xb0\\x0e\\xee"\n "\\xf3\\x23\\xec\\x74\\x1e\\x3b\\xc0\\x58\\x1d\\x43\\xd9\\x35\\x15\\x7e\\x2f"\n "\\x28\\xff\\x08\\x7a\\x79\\x44\\xfc\\x24\\x06\\xc5\\x3f\\xaa\\xe0\\xfa\\xb2"\n "\\x1f\\xd9\\x4e\\xd8\\xc8\\xda\\xed\\x1f\\xaa\\x77\\x64\\xae\\xb3\\xe3",\n "\\x01\\xea\\xaa\\x9d\\x90\\x67\\x3b\\x2f\\x9a\\xe3\\x7b\\x56\\x5c\\x35\\x9f"\n "\\x65\\x0b\\x0c\\x3b\\xa6\\x60\\x5f\\x98\\x36\\x2c\\xb6\\xaa\\xdb\\xb2\\x86"\n "\\x8b\\x20\\xba\\xa4\\x85\\x7b\\xa8\\x80\\x41\\xee\\x22\\x1c\\x7a\\x92\\xf5"\n "\\x39\\x4e\\x4a\\x0b\\x29\\x0b\\xe7\\xdd\\x3c\\xb6\\x51\\xec\\xc3\\x26\\xbc"\n "\\x33\\x11\\xbc\\x29\\x7a\\x10\\x68\\x32\\x55\\xb4\\x15\\xd9\\x8e\\xc1"\n };\n static int primes_len[] = { 74, 74, 74, 74, 74 };\n static char *exponents[] = {\n "\\x01\\xa4\\x49\\xb7\\x57\\xc5\\x50\\x36\\x08\\x3c\\xdc\\x93\\x29\\x1d\\x40"\n "\\x67\\x63\\x65\\x57\\x7f\\xe7\\x29\\x82\\x16\\x0e\\x9a\\x74\\x06\\x37\\x76"\n "\\xe7\\xb6\\x6a\\x15\\x5d\\xf9\\x3c\\x00\\x45\\x3f\\x62\\xe1\\x52\\xb3\\x3f"\n "\\x6e\\xc2\\x8d\\x1b\\x7e\\xc4\\x1c\\x8e\\x9e\\xd7\\x23\\x45\\xc8\\x9d\\x74"\n "\\x76\\x25\\x5b\\x99\\x31\\x57\\xa7\\x5d\\x71\\x32\\x2f\\xd1\\x74\\xd5",\n "\\x01\\x3a\\xa4\\x6d\\x05\\xf7\\xeb\\xa5\\x3d\\xe2\\x67\\x6e\\xd7\\x20\\xd4"\n "\\x33\\x17\\x56\\xdf\\x34\\x59\\x81\\xd2\\x3b\\x51\\x64\\x89\\x44\\x13\\xca"\n "\\xc7\\x41\\xa4\\xf7\\xa8\\xf6\\xd4\\xbc\\xd7\\xc1\\x7d\\xa3\\xbf\\x39\\x4b"\n "\\x37\\x1c\\xac\\xec\\xf6\\x46\\x82\\xdc\\x05\\x25\\xf1\\x7c\\x71\\x9e\\xe9"\n "\\x0b\\xd5\\xb0\\x40\\x15\\x7f\\x4f\\x01\\x6a\\x1c\\x56\\x2e\\x42\\x05",\n "\\x00\\x9b\\xd7\\x14\\x9e\\xcf\\x47\\x4a\\xe5\\x1e\\xa8\\xc4\\x93\\x52\\xd2"\n "\\x4c\\xb7\\xd3\\x67\\xa3\\x4e\\x79\\x34\\x09\\x5e\\x5c\\x5c\\x55\\xe3\\x3c"\n "\\x02\\xa9\\x81\\xa4\\x56\\xa8\\xb1\\x3d\\xf6\\x40\\xe3\\xf5\\x06\\xce\\x6f"\n "\\x29\\x01\\x05\\xde\\x43\\xa8\\x67\\xeb\\x29\\x8d\\x09\\xd8\\x7d\\xaf\\x3f"\n "\\x51\\xac\\xf4\\x5b\\x0c\\xa0\\x95\\x35\\x04\\xd0\\xf9\\x6f\\x6a\\xa7",\n "\\x02\\xfd\\x38\\x42\\x48\\x82\\x90\\x3a\\xb0\\xd4\\x10\\xd9\\xba\\x35\\xd5"\n "\\x6f\\xe1\\xb4\\xc7\\x65\\x30\\xe7\\x2f\\xa7\\x08\\xbe\\xfe\\x21\\x69\\x62"\n "\\xcd\\xc3\\x42\\x04\\x1a\\xfc\\x6a\\x24\\x4a\\x13\\x8c\\xa3\\x4e\\x71\\x09"\n "\\x42\\xa9\\x5d\\x03\\xd7\\x1e\\xf0\\xa9\\xbf\\xd1\\x13\\x59\\x07\\xa1\\x45"\n "\\xde\\xae\\xd0\\x5a\\x98\\xeb\\x22\\xf5\\x3d\\xc2\\xa2\\x35\\x77\\x91",\n "\\x13\\x1d\\x2c\\x60\\x28\\xb5\\x54\\x88\\x6b\\x1e\\x2d\\xe2\\x0f\\xb0\\xb2"\n "\\xe5\\xf8\\x47\\x06\\x97\\x30\\x82\\x24\\x72\\x1f\\x77\\x8e\\x71\\x68\\xee"\n "\\x58\\x8b\\x0c\\xc7\\xaa\\x66\\x89\\x00\\x88\\x7f\\x49\\xae\\xb8\\xb4\\xd6"\n "\\xd3\\xa6\\xec\\xc2\\x5f\\x95\\x5b\\xb7\\xf6\\xbe\\x40\\x43\\xe5\\xe9\\x64"\n "\\xef\\xe6\\xed\\x92\\xb4\\xba\\xea\\x63\\x0e\\x4d\\xdf\\x98\\xc1"\n };\n static int exps_len[] = { 74, 74, 74, 74, 73 };\n static char *coefficients[] = {\n "\\x18\\x19\\x50\\xc2\\x69\\x6a\\x63\\x66\\x40\\x6a\\x43\\xa6\\x7d\\x0d\\x12"\n "\\x0f\\x04\\x3a\\xe7\\xb2\\x1c\\xe3\\xfc\\x6f\\xa7\\x8b\\x5e\\x99\\xe3\\x43"\n "\\x97\\x68\\x10\\xee\\x68\\x73\\x01\\xc6\\xaf\\x5e\\x26\\xaf\\xcc\\x1f\\x39"\n "\\x9c\\x8d\\xa9\\x06\\x80\\x21\\x7a\\xaa\\x29\\x8a\\x4b\\x71\\x03\\xb0\\x9a"\n "\\x2f\\xd6\\x6b\\xb0\\x0b\\x93\\xc9\\x4d\\x0b\\x9d\\x3f\\xe6\\x21",\n "\\x00\\xa4\\x80\\x6e\\xef\\x34\\xba\\x3f\\xe7\\x96\\xec\\x25\\x16\\x25\\xe9"\n "\\x77\\x5f\\x61\\xb1\\x48\\xdb\\x03\\xfa\\x80\\xf3\\xbd\\x20\\xd1\\x60\\x9a"\n "\\x10\\x9d\\xf0\\x1b\\xff\\xb8\\x50\\x14\\x54\\x75\\x53\\x2b\\x89\\x9f\\x39"\n "\\x96\\x63\\x05\\x89\\xfb\\xfb\\x7f\\x59\\x93\\xdc\\x61\\xe2\\x8c\\xfc\\x5a"\n "\\x6f\\x2c\\x6a\\xcf\\xd4\\xac\\xa5\\x81\\xf2\\xdd\\x68\\x75\\xd4\\x48",\n "\\x00\\xf6\\xec\\xd6\\x98\\x10\\x42\\x38\\x94\\x30\\x2c\\xd4\\xe7\\xb1\\x5f"\n "\\xa2\\xfd\\xc9\\xc4\\x92\\x78\\xf6\\x31\\x34\\xb7\\x26\\xa1\\x7f\\x0b\\xa3"\n "\\xc3\\xf6\\x4f\\xd0\\x05\\x4e\\x98\\x1b\\xa5\\x98\\xde\\x26\\xb8\\xc2\\x14"\n "\\x12\\xa4\\xae\\x2f\\xd8\\x48\\x39\\x1b\\x33\\x1d\\x0f\\x72\\xaf\\xd3\\x8d"\n "\\xd8\\xb0\\x9f\\x52\\x42\\x5d\\xae\\xf6\\x3c\\xe1\\xd2\\x09\\xd8\\x53",\n "\\x00\\xb3\\xce\\x4b\\x87\\x41\\x21\\x34\\x7b\\xe3\\x64\\x74\\xef\\x9f\\x71"\n "\\xcc\\x01\\x19\\x50\\x69\\xbb\\x5f\\x69\\xc8\\xbc\\x62\\x8b\\x4d\\xa9\\x73"\n "\\x23\\x7f\\xc6\\xce\\xfa\\xe7\\x96\\xa7\\x22\\x44\\x33\\x32\\x47\\x60\\x23"\n "\\xb4\\xd2\\x5e\\xa4\\xa1\\xbd\\x31\\x1f\\x04\\x1a\\xdf\\xdb\\x05\\xe9\\x4c"\n "\\x44\\xfb\\x9b\\x73\\xfe\\x25\\x3d\\x7a\\x61\\xc2\\x22\\x9a\\xd6\\x18",\n "\\x01\\x8d\\x43\\x63\\x89\\x6d\\x97\\xf3\\x4a\\x3e\\x10\\xa0\\x94\\x46\\x1a"\n "\\x1c\\x34\\x22\\xb3\\xe3\\x21\\xff\\xad\\xf2\\x1b\\x56\\x74\\x32\\xdc\\xd2"\n "\\x0e\\xd7\\x3a\\x9c\\xe9\\x87\\xcc\\xf1\\x9c\\x56\\xfe\\xff\\x2f\\x3d\\xec"\n "\\x70\\xba\\xfb\\x5d\\x37\\xa8\\x57\\xd1\\xc4\\xa9\\x1b\\xc9\\xdc\\x76\\x68"\n "\\xca\\x7d\\x39\\x59\\x97\\x2d\\x07\\x03\\x52\\xf6\\x8d\\xb6\\x0e\\x24"\n };\n static int coeffs_len[] = { 73, 74, 74, 74, 74 };\n BIGNUM **pris = NULL, **exps = NULL, **coeffs = NULL;\n int i, rv = 512;\n unsigned char *n, *d;\n size_t len_n, len_d;\n len_n = sizeof(n1) + sizeof(n2) - 2;\n n = OPENSSL_zalloc(len_n);\n if (n == NULL)\n return 0;\n memcpy(n, n1, sizeof(n1) - 1);\n memcpy(n + sizeof(n1) - 1, n2, sizeof(n2) - 1);\n len_d = sizeof(d1) + sizeof(d2) - 2;\n d = OPENSSL_zalloc(len_d);\n if (d == NULL)\n goto err;\n memcpy(d, d1, sizeof(d1) - 1);\n memcpy(d + sizeof(d1) - 1, d2, sizeof(d2) - 1);\n if (!TEST_int_eq(RSA_set0_key(key,\n BN_bin2bn(n, len_n, NULL),\n BN_bin2bn(e, sizeof(e) - 1, NULL),\n BN_bin2bn(d, len_d, NULL)), 1))\n goto err;\n if (!TEST_int_eq(RSA_set0_factors(key,\n BN_bin2bn(p, sizeof(p) - 1, NULL),\n BN_bin2bn(q, sizeof(q) - 1, NULL)), 1))\n goto err;\n if (!TEST_int_eq(RSA_set0_crt_params(key,\n BN_bin2bn(dmp1, sizeof(dmp1) - 1, NULL),\n BN_bin2bn(dmq1, sizeof(dmq1) - 1, NULL),\n BN_bin2bn(iqmp, sizeof(iqmp) - 1,\n NULL)), 1))\n return 0;\n pris = OPENSSL_zalloc(sizeof(BIGNUM *) * NUM_EXTRA_PRIMES);\n exps = OPENSSL_zalloc(sizeof(BIGNUM *) * NUM_EXTRA_PRIMES);\n coeffs = OPENSSL_zalloc(sizeof(BIGNUM *) * NUM_EXTRA_PRIMES);\n if (!TEST_ptr(pris) || !TEST_ptr(exps) || !TEST_ptr(coeffs))\n goto err;\n for (i = 0; i < NUM_EXTRA_PRIMES; i++) {\n pris[i] = BN_bin2bn((unsigned char *)primes[i], primes_len[i], NULL);\n exps[i] = BN_bin2bn((unsigned char *)exponents[i], exps_len[i], NULL);\n coeffs[i] = BN_bin2bn((unsigned char *)coefficients[i],\n coeffs_len[i], NULL);\n if (!TEST_ptr(pris[i]) || !TEST_ptr(exps[i]) || !TEST_ptr(coeffs[i]))\n goto err;\n }\n if (!TEST_true(RSA_set0_multi_prime_params(key, pris, exps,\n coeffs, NUM_EXTRA_PRIMES)))\n goto err;\n ret:\n OPENSSL_free(pris);\n OPENSSL_free(exps);\n OPENSSL_free(coeffs);\n OPENSSL_free(n);\n OPENSSL_free(d);\n return rv;\n err:\n for (i = 0; i < 5; i++) {\n if (pris != NULL)\n BN_free(pris[i]);\n if (exps != NULL)\n BN_free(exps[i]);\n if (coeffs != NULL)\n BN_free(coeffs[i]);\n }\n rv = 0;\n goto ret;\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return NULL;\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return ret;\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return ret;\n}', 'int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)\n{\n BIGNUM *i, *j, *k, *l, *m;\n BN_CTX *ctx;\n int ret = 1, ex_primes = 0, idx;\n RSA_PRIME_INFO *pinfo;\n if (key->p == NULL || key->q == NULL || key->n == NULL\n || key->e == NULL || key->d == NULL) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);\n return 0;\n }\n if (key->version == RSA_ASN1_VERSION_MULTI\n && (ex_primes = sk_RSA_PRIME_INFO_num(key->prime_infos)) <= 0) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_INVALID_MULTI_PRIME_KEY);\n return 0;\n }\n i = BN_new();\n j = BN_new();\n k = BN_new();\n l = BN_new();\n m = BN_new();\n ctx = BN_CTX_new();\n if (i == NULL || j == NULL || k == NULL || l == NULL\n || m == NULL || ctx == NULL) {\n ret = -1;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (BN_is_one(key->e)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);\n }\n if (!BN_is_odd(key->e)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);\n }\n if (BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);\n }\n if (BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (BN_is_prime_ex(pinfo->r, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_R_NOT_PRIME);\n }\n }\n if (!BN_mul(i, key->p, key->q, ctx)) {\n ret = -1;\n goto err;\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_mul(i, i, pinfo->r, ctx)) {\n ret = -1;\n goto err;\n }\n }\n if (BN_cmp(i, key->n) != 0) {\n ret = 0;\n if (ex_primes)\n RSAerr(RSA_F_RSA_CHECK_KEY_EX,\n RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES);\n else\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);\n }\n if (!BN_sub(i, key->p, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_sub(j, key->q, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mul(l, i, j, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_gcd(m, i, j, ctx)) {\n ret = -1;\n goto err;\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_sub(k, pinfo->r, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mul(l, l, k, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_gcd(m, m, k, ctx)) {\n ret = -1;\n goto err;\n }\n }\n if (!BN_div(k, NULL, l, m, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_mod_mul(i, key->d, key->e, k, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_is_one(i)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);\n }\n if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {\n if (!BN_sub(i, key->p, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmp1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_sub(i, key->q, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmq1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, key->q, key->p, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, key->iqmp) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);\n }\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_sub(i, pinfo->r, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, pinfo->d) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, pinfo->pp, pinfo->r, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, pinfo->t) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R);\n }\n }\n err:\n BN_free(i);\n BN_free(j);\n BN_free(k);\n BN_free(l);\n BN_free(m);\n BN_CTX_free(ctx);\n return ret;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return 0;\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
35,833
0
https://github.com/libav/libav/blob/688417399c69aadd4c287bdb0dec82ef8799011c/libavcodec/hevcdsp_template.c/#L907
PUT_HEVC_QPEL_HV(3, 1)
['QPEL(64)', 'PUT_HEVC_QPEL_HV(3, 1)']
35,834
0
https://gitlab.com/libtiff/libtiff/blob/bfbc717684115d7beb96c82255dad2dd4a4b8845/tools/tiffcrop.c/#L8501
static int reverseSamples24bits (uint16 spp, uint16 bps, uint32 width, uint8 *ibuff, uint8 *obuff) { int ready_bits = 0; uint32 col; uint32 src_byte = 0, high_bit = 0; uint32 bit_offset = 0; uint32 match_bits = 0, mask_bits = 0; uint32 buff1 = 0, buff2 = 0; uint8 bytebuff1 = 0, bytebuff2 = 0; unsigned char *src; unsigned char *dst; tsample_t sample; if ((ibuff == NULL) || (obuff == NULL)) { TIFFError("reverseSamples24bits","Invalid image or work buffer"); return (1); } ready_bits = 0; mask_bits = (uint32)-1 >> (32 - bps); dst = obuff; for (col = width; col > 0; col--) { bit_offset = (col - 1) * bps * spp; for (sample = 0; sample < spp; sample++) { if (sample == 0) { src_byte = bit_offset / 8; high_bit = bit_offset % 8; } else { src_byte = (bit_offset + (sample * bps)) / 8; high_bit = (bit_offset + (sample * bps)) % 8; } src = ibuff + src_byte; match_bits = mask_bits << (32 - high_bit - bps); if (little_endian) buff1 = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3]; else buff1 = (src[3] << 24) | (src[2] << 16) | (src[1] << 8) | src[0]; buff1 = (buff1 & match_bits) << (high_bit); if (ready_bits < 16) { bytebuff1 = bytebuff2 = 0; buff2 = (buff2 | (buff1 >> ready_bits)); } else { bytebuff1 = (buff2 >> 24); *dst++ = bytebuff1; bytebuff2 = (buff2 >> 16); *dst++ = bytebuff2; ready_bits -= 16; buff2 = ((buff2 << 16) | (buff1 >> ready_bits)); } ready_bits += bps; } } while (ready_bits > 0) { bytebuff1 = (buff2 >> 24); *dst++ = bytebuff1; buff2 = (buff2 << 8); bytebuff2 = bytebuff1; ready_bits -= 8; } return (0); }
['static int\nmirrorImage(uint16 spp, uint16 bps, uint16 mirror, uint32 width, uint32 length, unsigned char *ibuff)\n {\n int shift_width;\n uint32 bytes_per_pixel, bytes_per_sample;\n uint32 row, rowsize, row_offset;\n unsigned char *line_buff = NULL;\n unsigned char *src;\n unsigned char *dst;\n src = ibuff;\n rowsize = ((width * bps * spp) + 7) / 8;\n switch (mirror)\n {\n case MIRROR_BOTH:\n case MIRROR_VERT:\n line_buff = (unsigned char *)_TIFFmalloc(rowsize);\n if (line_buff == NULL)\n {\n\t TIFFError ("mirrorImage", "Unable to allocate mirror line buffer of %1u bytes", rowsize);\n return (-1);\n }\n dst = ibuff + (rowsize * (length - 1));\n for (row = 0; row < length / 2; row++)\n {\n\t _TIFFmemcpy(line_buff, src, rowsize);\n\t _TIFFmemcpy(src, dst, rowsize);\n\t _TIFFmemcpy(dst, line_buff, rowsize);\n src += (rowsize);\n dst -= (rowsize);\n }\n if (line_buff)\n _TIFFfree(line_buff);\n if (mirror == MIRROR_VERT)\n break;\n case MIRROR_HORIZ :\n if ((bps % 8) == 0)\n {\n for (row = 0; row < length; row++)\n {\n\t\t row_offset = row * rowsize;\n src = ibuff + row_offset;\n dst = ibuff + row_offset + rowsize;\n if (reverseSamplesBytes(spp, bps, width, src, dst))\n {\n\t\t return (-1);\n }\n\t\t }\n\t\t}\n\t else\n {\n if (!(line_buff = (unsigned char *)_TIFFmalloc(rowsize + 1)))\n {\n TIFFError("mirrorImage", "Unable to allocate mirror line buffer");\n return (-1);\n }\n bytes_per_sample = (bps + 7) / 8;\n bytes_per_pixel = ((bps * spp) + 7) / 8;\n if (bytes_per_pixel < (bytes_per_sample + 1))\n shift_width = bytes_per_pixel;\n else\n shift_width = bytes_per_sample + 1;\n for (row = 0; row < length; row++)\n {\n\t\t row_offset = row * rowsize;\n src = ibuff + row_offset;\n _TIFFmemset (line_buff, \'\\0\', rowsize);\n switch (shift_width)\n {\n case 1: if (reverseSamples16bits(spp, bps, width, src, line_buff))\n {\n\t\t _TIFFfree(line_buff);\n return (-1);\n }\n _TIFFmemcpy (src, line_buff, rowsize);\n break;\n case 2: if (reverseSamples24bits(spp, bps, width, src, line_buff))\n {\n\t\t _TIFFfree(line_buff);\n return (-1);\n }\n _TIFFmemcpy (src, line_buff, rowsize);\n break;\n case 3:\n case 4:\n case 5: if (reverseSamples32bits(spp, bps, width, src, line_buff))\n {\n\t\t _TIFFfree(line_buff);\n return (-1);\n }\n _TIFFmemcpy (src, line_buff, rowsize);\n break;\n default: TIFFError("mirrorImage","Unsupported bit depth %d", bps);\n\t\t _TIFFfree(line_buff);\n return (-1);\n }\n\t\t }\n if (line_buff)\n _TIFFfree(line_buff);\n\t\t}\n break;\n default: TIFFError ("mirrorImage", "Invalid mirror axis %d", mirror);\n return (-1);\n break;\n }\n return (0);\n }', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n\treturn (malloc((size_t) s));\n}', 'static int\nreverseSamples24bits (uint16 spp, uint16 bps, uint32 width,\n uint8 *ibuff, uint8 *obuff)\n {\n int ready_bits = 0;\n uint32 col;\n uint32 src_byte = 0, high_bit = 0;\n uint32 bit_offset = 0;\n uint32 match_bits = 0, mask_bits = 0;\n uint32 buff1 = 0, buff2 = 0;\n uint8 bytebuff1 = 0, bytebuff2 = 0;\n unsigned char *src;\n unsigned char *dst;\n tsample_t sample;\n if ((ibuff == NULL) || (obuff == NULL))\n {\n TIFFError("reverseSamples24bits","Invalid image or work buffer");\n return (1);\n }\n ready_bits = 0;\n mask_bits = (uint32)-1 >> (32 - bps);\n dst = obuff;\n for (col = width; col > 0; col--)\n {\n bit_offset = (col - 1) * bps * spp;\n for (sample = 0; sample < spp; sample++)\n {\n if (sample == 0)\n {\n src_byte = bit_offset / 8;\n high_bit = bit_offset % 8;\n }\n else\n {\n src_byte = (bit_offset + (sample * bps)) / 8;\n high_bit = (bit_offset + (sample * bps)) % 8;\n }\n src = ibuff + src_byte;\n match_bits = mask_bits << (32 - high_bit - bps);\n if (little_endian)\n\tbuff1 = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];\n else\n\tbuff1 = (src[3] << 24) | (src[2] << 16) | (src[1] << 8) | src[0];\n buff1 = (buff1 & match_bits) << (high_bit);\n if (ready_bits < 16)\n {\n bytebuff1 = bytebuff2 = 0;\n buff2 = (buff2 | (buff1 >> ready_bits));\n }\n else\n {\n bytebuff1 = (buff2 >> 24);\n *dst++ = bytebuff1;\n bytebuff2 = (buff2 >> 16);\n *dst++ = bytebuff2;\n ready_bits -= 16;\n buff2 = ((buff2 << 16) | (buff1 >> ready_bits));\n }\n ready_bits += bps;\n }\n }\n while (ready_bits > 0)\n {\n bytebuff1 = (buff2 >> 24);\n *dst++ = bytebuff1;\n buff2 = (buff2 << 8);\n bytebuff2 = bytebuff1;\n ready_bits -= 8;\n }\n return (0);\n }']
35,835
0
https://github.com/openssl/openssl/blob/6fc1748ec65c94c195d02b59556434e36a5f7651/crypto/lhash/lhash.c/#L123
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data) { unsigned long hash; OPENSSL_LH_NODE *nn, **rn; void *ret; lh->error = 0; rn = getrn(lh, data, &hash); if (*rn == NULL) { lh->num_no_delete++; return (NULL); } else { nn = *rn; *rn = nn->next; ret = nn->data; OPENSSL_free(nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) contract(lh); return (ret); }
['int dtls1_process_buffered_records(SSL *s)\n{\n pitem *item;\n item = pqueue_peek(s->rlayer.d->unprocessed_rcds.q);\n if (item) {\n if (s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch)\n return (1);\n while (pqueue_peek(s->rlayer.d->unprocessed_rcds.q)) {\n dtls1_get_unprocessed_record(s);\n if (!dtls1_process_record(s))\n return (0);\n if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),\n SSL3_RECORD_get_seq_num(s->rlayer.rrec)) <\n 0)\n return -1;\n }\n }\n s->rlayer.d->processed_rcds.epoch = s->rlayer.d->r_epoch;\n s->rlayer.d->unprocessed_rcds.epoch = s->rlayer.d->r_epoch + 1;\n return (1);\n}', 'int dtls1_process_record(SSL *s)\n{\n int i, al;\n int enc_err;\n SSL_SESSION *sess;\n SSL3_RECORD *rr;\n unsigned int mac_size;\n unsigned char md[EVP_MAX_MD_SIZE];\n rr = RECORD_LAYER_get_rrec(&s->rlayer);\n sess = s->session;\n rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);\n if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);\n goto f_err;\n }\n rr->data = rr->input;\n rr->orig_len = rr->length;\n enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);\n if (enc_err == 0) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto err;\n }\n#ifdef SSL_DEBUG\n printf("dec %d\\n", rr->length);\n {\n unsigned int z;\n for (z = 0; z < rr->length; z++)\n printf("%02X%c", rr->data[z], ((z + 1) % 16) ? \' \' : \'\\n\');\n }\n printf("\\n");\n#endif\n if ((sess != NULL) &&\n (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {\n unsigned char *mac = NULL;\n unsigned char mac_tmp[EVP_MAX_MD_SIZE];\n mac_size = EVP_MD_CTX_size(s->read_hash);\n OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);\n if (rr->orig_len < mac_size ||\n (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&\n rr->orig_len < mac_size + 1)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);\n goto f_err;\n }\n if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {\n mac = mac_tmp;\n ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);\n rr->length -= mac_size;\n } else {\n rr->length -= mac_size;\n mac = &rr->data[rr->length];\n }\n i = s->method->ssl3_enc->mac(s, rr, md, 0 );\n if (i < 0 || mac == NULL\n || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)\n enc_err = -1;\n if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)\n enc_err = -1;\n }\n if (enc_err < 0) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto err;\n }\n if (s->expand != NULL) {\n if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD,\n SSL_R_COMPRESSED_LENGTH_TOO_LONG);\n goto f_err;\n }\n if (!ssl3_do_uncompress(s, rr)) {\n al = SSL_AD_DECOMPRESSION_FAILURE;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);\n goto f_err;\n }\n }\n if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);\n goto f_err;\n }\n rr->off = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n return (1);\n f_err:\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\n err:\n return (0);\n}', 'int ssl3_send_alert(SSL *s, int level, int desc)\n{\n desc = s->method->ssl3_enc->alert_value(desc);\n if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)\n desc = SSL_AD_HANDSHAKE_FAILURE;\n if (desc < 0)\n return -1;\n if ((level == SSL3_AL_FATAL) && (s->session != NULL))\n SSL_CTX_remove_session(s->session_ctx, s->session);\n s->s3->alert_dispatch = 1;\n s->s3->send_alert[0] = level;\n s->s3->send_alert[1] = desc;\n if (!RECORD_LAYER_write_pending(&s->rlayer)) {\n return s->method->ssl_dispatch_alert(s);\n }\n return -1;\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_THREAD_write_lock(ctx->lock);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, c);\n SSL_SESSION_list_remove(ctx, c);\n }\n c->not_resumable = 1;\n if (lck)\n CRYPTO_THREAD_unlock(ctx->lock);\n if (ret)\n SSL_SESSION_free(r);\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, c);\n } else\n ret = 0;\n return (ret);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return (NULL);\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return (ret);\n}']
35,836
0
https://github.com/nginx/nginx/blob/149fda55f730c38fb9e2c5b63370da92c0ad7c22/src/core/ngx_string.c/#L1579
uintptr_t ngx_escape_uri(u_char *dst, u_char *src, size_t size, ngx_uint_t type) { ngx_uint_t n; uint32_t *escape; static u_char hex[] = "0123456789ABCDEF"; static uint32_t uri[] = { 0xffffffff, 0x80000029, 0x00000000, 0x80000000, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }; static uint32_t args[] = { 0xffffffff, 0x88000869, 0x00000000, 0x80000000, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }; static uint32_t uri_component[] = { 0xffffffff, 0xfc009fff, 0x78000001, 0xb8000001, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }; static uint32_t html[] = { 0xffffffff, 0x000000ad, 0x00000000, 0x80000000, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }; static uint32_t refresh[] = { 0xffffffff, 0x00000085, 0x00000000, 0x80000000, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }; static uint32_t memcached[] = { 0xffffffff, 0x00000021, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, }; static uint32_t *map[] = { uri, args, uri_component, html, refresh, memcached, memcached }; escape = map[type]; if (dst == NULL) { n = 0; while (size) { if (escape[*src >> 5] & (1U << (*src & 0x1f))) { n++; } src++; size--; } return (uintptr_t) n; } while (size) { if (escape[*src >> 5] & (1U << (*src & 0x1f))) { *dst++ = '%'; *dst++ = hex[*src >> 4]; *dst++ = hex[*src & 0xf]; src++; } else { *dst++ = *src++; } size--; } return (uintptr_t) dst; }
['static ngx_int_t\nngx_http_proxy_create_request(ngx_http_request_t *r)\n{\n size_t len, uri_len, loc_len, body_len;\n uintptr_t escape;\n ngx_buf_t *b;\n ngx_str_t method;\n ngx_uint_t i, unparsed_uri;\n ngx_chain_t *cl, *body;\n ngx_list_part_t *part;\n ngx_table_elt_t *header;\n ngx_http_upstream_t *u;\n ngx_http_proxy_ctx_t *ctx;\n ngx_http_script_code_pt code;\n ngx_http_proxy_headers_t *headers;\n ngx_http_script_engine_t e, le;\n ngx_http_proxy_loc_conf_t *plcf;\n ngx_http_script_len_code_pt lcode;\n u = r->upstream;\n plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);\n#if (NGX_HTTP_CACHE)\n headers = u->cacheable ? &plcf->headers_cache : &plcf->headers;\n#else\n headers = &plcf->headers;\n#endif\n if (u->method.len) {\n method = u->method;\n } else if (plcf->method) {\n if (ngx_http_complex_value(r, plcf->method, &method) != NGX_OK) {\n return NGX_ERROR;\n }\n } else {\n method = r->method_name;\n }\n ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);\n if (method.len == 4\n && ngx_strncasecmp(method.data, (u_char *) "HEAD", 4) == 0)\n {\n ctx->head = 1;\n }\n len = method.len + 1 + sizeof(ngx_http_proxy_version) - 1\n + sizeof(CRLF) - 1;\n escape = 0;\n loc_len = 0;\n unparsed_uri = 0;\n if (plcf->proxy_lengths && ctx->vars.uri.len) {\n uri_len = ctx->vars.uri.len;\n } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri && r == r->main)\n {\n unparsed_uri = 1;\n uri_len = r->unparsed_uri.len;\n } else {\n loc_len = (r->valid_location && ctx->vars.uri.len) ?\n plcf->location.len : 0;\n if (r->quoted_uri || r->space_in_uri || r->internal) {\n escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,\n r->uri.len - loc_len, NGX_ESCAPE_URI);\n }\n uri_len = ctx->vars.uri.len + r->uri.len - loc_len + escape\n + sizeof("?") - 1 + r->args.len;\n }\n if (uri_len == 0) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n "zero length URI to proxy");\n return NGX_ERROR;\n }\n len += uri_len;\n ngx_memzero(&le, sizeof(ngx_http_script_engine_t));\n ngx_http_script_flush_no_cacheable_variables(r, plcf->body_flushes);\n ngx_http_script_flush_no_cacheable_variables(r, headers->flushes);\n if (plcf->body_lengths) {\n le.ip = plcf->body_lengths->elts;\n le.request = r;\n le.flushed = 1;\n body_len = 0;\n while (*(uintptr_t *) le.ip) {\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n body_len += lcode(&le);\n }\n ctx->internal_body_length = body_len;\n len += body_len;\n } else if (r->headers_in.chunked && r->reading_body) {\n ctx->internal_body_length = -1;\n ctx->internal_chunked = 1;\n } else {\n ctx->internal_body_length = r->headers_in.content_length_n;\n }\n le.ip = headers->lengths->elts;\n le.request = r;\n le.flushed = 1;\n while (*(uintptr_t *) le.ip) {\n while (*(uintptr_t *) le.ip) {\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n len += lcode(&le);\n }\n le.ip += sizeof(uintptr_t);\n }\n if (plcf->upstream.pass_request_headers) {\n part = &r->headers_in.headers.part;\n header = part->elts;\n for (i = 0; ; i++) {\n if (i >= part->nelts) {\n if (part->next == NULL) {\n break;\n }\n part = part->next;\n header = part->elts;\n i = 0;\n }\n if (ngx_hash_find(&headers->hash, header[i].hash,\n header[i].lowcase_key, header[i].key.len))\n {\n continue;\n }\n len += header[i].key.len + sizeof(": ") - 1\n + header[i].value.len + sizeof(CRLF) - 1;\n }\n }\n b = ngx_create_temp_buf(r->pool, len);\n if (b == NULL) {\n return NGX_ERROR;\n }\n cl = ngx_alloc_chain_link(r->pool);\n if (cl == NULL) {\n return NGX_ERROR;\n }\n cl->buf = b;\n b->last = ngx_copy(b->last, method.data, method.len);\n *b->last++ = \' \';\n u->uri.data = b->last;\n if (plcf->proxy_lengths && ctx->vars.uri.len) {\n b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);\n } else if (unparsed_uri) {\n b->last = ngx_copy(b->last, r->unparsed_uri.data, r->unparsed_uri.len);\n } else {\n if (r->valid_location) {\n b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);\n }\n if (escape) {\n ngx_escape_uri(b->last, r->uri.data + loc_len,\n r->uri.len - loc_len, NGX_ESCAPE_URI);\n b->last += r->uri.len - loc_len + escape;\n } else {\n b->last = ngx_copy(b->last, r->uri.data + loc_len,\n r->uri.len - loc_len);\n }\n if (r->args.len > 0) {\n *b->last++ = \'?\';\n b->last = ngx_copy(b->last, r->args.data, r->args.len);\n }\n }\n u->uri.len = b->last - u->uri.data;\n if (plcf->http_version == NGX_HTTP_VERSION_11) {\n b->last = ngx_cpymem(b->last, ngx_http_proxy_version_11,\n sizeof(ngx_http_proxy_version_11) - 1);\n } else {\n b->last = ngx_cpymem(b->last, ngx_http_proxy_version,\n sizeof(ngx_http_proxy_version) - 1);\n }\n ngx_memzero(&e, sizeof(ngx_http_script_engine_t));\n e.ip = headers->values->elts;\n e.pos = b->last;\n e.request = r;\n e.flushed = 1;\n le.ip = headers->lengths->elts;\n while (*(uintptr_t *) le.ip) {\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n (void) lcode(&le);\n if (*(ngx_http_script_len_code_pt *) le.ip) {\n for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) {\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n }\n e.skip = (len == sizeof(CRLF) - 1) ? 1 : 0;\n } else {\n e.skip = 0;\n }\n le.ip += sizeof(uintptr_t);\n while (*(uintptr_t *) e.ip) {\n code = *(ngx_http_script_code_pt *) e.ip;\n code((ngx_http_script_engine_t *) &e);\n }\n e.ip += sizeof(uintptr_t);\n }\n b->last = e.pos;\n if (plcf->upstream.pass_request_headers) {\n part = &r->headers_in.headers.part;\n header = part->elts;\n for (i = 0; ; i++) {\n if (i >= part->nelts) {\n if (part->next == NULL) {\n break;\n }\n part = part->next;\n header = part->elts;\n i = 0;\n }\n if (ngx_hash_find(&headers->hash, header[i].hash,\n header[i].lowcase_key, header[i].key.len))\n {\n continue;\n }\n b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);\n *b->last++ = \':\'; *b->last++ = \' \';\n b->last = ngx_copy(b->last, header[i].value.data,\n header[i].value.len);\n *b->last++ = CR; *b->last++ = LF;\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http proxy header: \\"%V: %V\\"",\n &header[i].key, &header[i].value);\n }\n }\n *b->last++ = CR; *b->last++ = LF;\n if (plcf->body_values) {\n e.ip = plcf->body_values->elts;\n e.pos = b->last;\n e.skip = 0;\n while (*(uintptr_t *) e.ip) {\n code = *(ngx_http_script_code_pt *) e.ip;\n code((ngx_http_script_engine_t *) &e);\n }\n b->last = e.pos;\n }\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http proxy header:%N\\"%*s\\"",\n (size_t) (b->last - b->pos), b->pos);\n if (r->request_body_no_buffering) {\n u->request_bufs = cl;\n if (ctx->internal_chunked) {\n u->output.output_filter = ngx_http_proxy_body_output_filter;\n u->output.filter_ctx = r;\n }\n } else if (plcf->body_values == NULL && plcf->upstream.pass_request_body) {\n body = u->request_bufs;\n u->request_bufs = cl;\n while (body) {\n b = ngx_alloc_buf(r->pool);\n if (b == NULL) {\n return NGX_ERROR;\n }\n ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));\n cl->next = ngx_alloc_chain_link(r->pool);\n if (cl->next == NULL) {\n return NGX_ERROR;\n }\n cl = cl->next;\n cl->buf = b;\n body = body->next;\n }\n } else {\n u->request_bufs = cl;\n }\n b->flush = 1;\n cl->next = NULL;\n return NGX_OK;\n}', 'uintptr_t\nngx_escape_uri(u_char *dst, u_char *src, size_t size, ngx_uint_t type)\n{\n ngx_uint_t n;\n uint32_t *escape;\n static u_char hex[] = "0123456789ABCDEF";\n static uint32_t uri[] = {\n 0xffffffff,\n 0x80000029,\n 0x00000000,\n 0x80000000,\n 0xffffffff,\n 0xffffffff,\n 0xffffffff,\n 0xffffffff\n };\n static uint32_t args[] = {\n 0xffffffff,\n 0x88000869,\n 0x00000000,\n 0x80000000,\n 0xffffffff,\n 0xffffffff,\n 0xffffffff,\n 0xffffffff\n };\n static uint32_t uri_component[] = {\n 0xffffffff,\n 0xfc009fff,\n 0x78000001,\n 0xb8000001,\n 0xffffffff,\n 0xffffffff,\n 0xffffffff,\n 0xffffffff\n };\n static uint32_t html[] = {\n 0xffffffff,\n 0x000000ad,\n 0x00000000,\n 0x80000000,\n 0xffffffff,\n 0xffffffff,\n 0xffffffff,\n 0xffffffff\n };\n static uint32_t refresh[] = {\n 0xffffffff,\n 0x00000085,\n 0x00000000,\n 0x80000000,\n 0xffffffff,\n 0xffffffff,\n 0xffffffff,\n 0xffffffff\n };\n static uint32_t memcached[] = {\n 0xffffffff,\n 0x00000021,\n 0x00000000,\n 0x00000000,\n 0x00000000,\n 0x00000000,\n 0x00000000,\n 0x00000000,\n };\n static uint32_t *map[] =\n { uri, args, uri_component, html, refresh, memcached, memcached };\n escape = map[type];\n if (dst == NULL) {\n n = 0;\n while (size) {\n if (escape[*src >> 5] & (1U << (*src & 0x1f))) {\n n++;\n }\n src++;\n size--;\n }\n return (uintptr_t) n;\n }\n while (size) {\n if (escape[*src >> 5] & (1U << (*src & 0x1f))) {\n *dst++ = \'%\';\n *dst++ = hex[*src >> 4];\n *dst++ = hex[*src & 0xf];\n src++;\n } else {\n *dst++ = *src++;\n }\n size--;\n }\n return (uintptr_t) dst;\n}']
35,837
0
https://github.com/libav/libav/blob/c15fea7933b3801962851a37c3ef00ce968431c4/libavcodec/indeo3.c/#L193
static av_cold int allocate_frame_buffers(Indeo3DecodeContext *ctx, AVCodecContext *avctx) { int p, luma_width, luma_height, chroma_width, chroma_height; int luma_pitch, chroma_pitch, luma_size, chroma_size; luma_width = ctx->width; luma_height = ctx->height; if (luma_width < 16 || luma_width > 640 || luma_height < 16 || luma_height > 480 || luma_width & 3 || luma_height & 3) { av_log(avctx, AV_LOG_ERROR, "Invalid picture dimensions: %d x %d!\n", luma_width, luma_height); return AVERROR_INVALIDDATA; } chroma_width = FFALIGN(luma_width >> 2, 4); chroma_height = FFALIGN(luma_height >> 2, 4); luma_pitch = FFALIGN(luma_width, 16); chroma_pitch = FFALIGN(chroma_width, 16); luma_size = luma_pitch * (luma_height + 1); chroma_size = chroma_pitch * (chroma_height + 1); for (p = 0; p < 3; p++) { ctx->planes[p].pitch = !p ? luma_pitch : chroma_pitch; ctx->planes[p].width = !p ? luma_width : chroma_width; ctx->planes[p].height = !p ? luma_height : chroma_height; ctx->planes[p].buffers[0] = av_malloc(!p ? luma_size : chroma_size); ctx->planes[p].buffers[1] = av_malloc(!p ? luma_size : chroma_size); memset(ctx->planes[p].buffers[0], 0x40, ctx->planes[p].pitch); memset(ctx->planes[p].buffers[1], 0x40, ctx->planes[p].pitch); ctx->planes[p].pixels[0] = ctx->planes[p].buffers[0] + ctx->planes[p].pitch; ctx->planes[p].pixels[1] = ctx->planes[p].buffers[1] + ctx->planes[p].pitch; memset(ctx->planes[p].pixels[0], 0, ctx->planes[p].pitch * ctx->planes[p].height); memset(ctx->planes[p].pixels[1], 0, ctx->planes[p].pitch * ctx->planes[p].height); } return 0; }
['static av_cold int allocate_frame_buffers(Indeo3DecodeContext *ctx,\n AVCodecContext *avctx)\n{\n int p, luma_width, luma_height, chroma_width, chroma_height;\n int luma_pitch, chroma_pitch, luma_size, chroma_size;\n luma_width = ctx->width;\n luma_height = ctx->height;\n if (luma_width < 16 || luma_width > 640 ||\n luma_height < 16 || luma_height > 480 ||\n luma_width & 3 || luma_height & 3) {\n av_log(avctx, AV_LOG_ERROR, "Invalid picture dimensions: %d x %d!\\n",\n luma_width, luma_height);\n return AVERROR_INVALIDDATA;\n }\n chroma_width = FFALIGN(luma_width >> 2, 4);\n chroma_height = FFALIGN(luma_height >> 2, 4);\n luma_pitch = FFALIGN(luma_width, 16);\n chroma_pitch = FFALIGN(chroma_width, 16);\n luma_size = luma_pitch * (luma_height + 1);\n chroma_size = chroma_pitch * (chroma_height + 1);\n for (p = 0; p < 3; p++) {\n ctx->planes[p].pitch = !p ? luma_pitch : chroma_pitch;\n ctx->planes[p].width = !p ? luma_width : chroma_width;\n ctx->planes[p].height = !p ? luma_height : chroma_height;\n ctx->planes[p].buffers[0] = av_malloc(!p ? luma_size : chroma_size);\n ctx->planes[p].buffers[1] = av_malloc(!p ? luma_size : chroma_size);\n memset(ctx->planes[p].buffers[0], 0x40, ctx->planes[p].pitch);\n memset(ctx->planes[p].buffers[1], 0x40, ctx->planes[p].pitch);\n ctx->planes[p].pixels[0] = ctx->planes[p].buffers[0] + ctx->planes[p].pitch;\n ctx->planes[p].pixels[1] = ctx->planes[p].buffers[1] + ctx->planes[p].pitch;\n memset(ctx->planes[p].pixels[0], 0, ctx->planes[p].pitch * ctx->planes[p].height);\n memset(ctx->planes[p].pixels[1], 0, ctx->planes[p].pitch * ctx->planes[p].height);\n }\n return 0;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if (size > (INT_MAX - 32) || !size)\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size + 32);\n if (!ptr)\n return ptr;\n diff = ((-(long)ptr - 1) & 31) + 1;\n ptr = (char *)ptr + diff;\n ((char *)ptr)[-1] = diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr, 32, size))\n ptr = NULL;\n#elif HAVE_ALIGNED_MALLOC\n ptr = _aligned_malloc(size, 32);\n#elif HAVE_MEMALIGN\n ptr = memalign(32, size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
35,838
0
https://github.com/openssl/openssl/blob/05ec6a25f80ac8edfb7d7cb764d2dd68156a6965/test/evp_test.c/#L1703
static int encode_test_run(struct evp_test *t) { struct encode_data *edata = t->data; unsigned char *encode_out = NULL, *decode_out = NULL; int output_len, chunk_len; const char *err = "INTERNAL_ERROR"; EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new(); if (decode_ctx == NULL) goto err; if (edata->encoding == BASE64_CANONICAL_ENCODING) { EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new(); if (encode_ctx == NULL) goto err; encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len)); if (encode_out == NULL) goto err; EVP_EncodeInit(encode_ctx); EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len, edata->input, edata->input_len); output_len = chunk_len; EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len); output_len += chunk_len; EVP_ENCODE_CTX_free(encode_ctx); if (check_var_length_output(t, edata->output, edata->output_len, encode_out, output_len)) { err = "BAD_ENCODING"; goto err; } } decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len)); if (decode_out == NULL) goto err; EVP_DecodeInit(decode_ctx); if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output, edata->output_len) < 0) { err = "DECODE_ERROR"; goto err; } output_len = chunk_len; if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) { err = "DECODE_ERROR"; goto err; } output_len += chunk_len; if (edata->encoding != BASE64_INVALID_ENCODING && check_var_length_output(t, edata->input, edata->input_len, decode_out, output_len)) { err = "BAD_DECODING"; goto err; } err = NULL; err: t->err = err; OPENSSL_free(encode_out); OPENSSL_free(decode_out); EVP_ENCODE_CTX_free(decode_ctx); return 1; }
['static int encode_test_run(struct evp_test *t)\n{\n struct encode_data *edata = t->data;\n unsigned char *encode_out = NULL, *decode_out = NULL;\n int output_len, chunk_len;\n const char *err = "INTERNAL_ERROR";\n EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();\n if (decode_ctx == NULL)\n goto err;\n if (edata->encoding == BASE64_CANONICAL_ENCODING) {\n EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();\n if (encode_ctx == NULL)\n goto err;\n encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));\n if (encode_out == NULL)\n goto err;\n EVP_EncodeInit(encode_ctx);\n EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,\n edata->input, edata->input_len);\n output_len = chunk_len;\n EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);\n output_len += chunk_len;\n EVP_ENCODE_CTX_free(encode_ctx);\n if (check_var_length_output(t, edata->output, edata->output_len,\n encode_out, output_len)) {\n err = "BAD_ENCODING";\n goto err;\n }\n }\n decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));\n if (decode_out == NULL)\n goto err;\n EVP_DecodeInit(decode_ctx);\n if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,\n edata->output_len) < 0) {\n err = "DECODE_ERROR";\n goto err;\n }\n output_len = chunk_len;\n if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {\n err = "DECODE_ERROR";\n goto err;\n }\n output_len += chunk_len;\n if (edata->encoding != BASE64_INVALID_ENCODING &&\n check_var_length_output(t, edata->input, edata->input_len,\n decode_out, output_len)) {\n err = "BAD_DECODING";\n goto err;\n }\n err = NULL;\n err:\n t->err = err;\n OPENSSL_free(encode_out);\n OPENSSL_free(decode_out);\n EVP_ENCODE_CTX_free(decode_ctx);\n return 1;\n}', 'EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_ENCODE_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
35,839
0
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/lhash/lhash.c/#L222
static void expand(OPENSSL_LHASH *lh) { OPENSSL_LH_NODE **n, **n1, **n2, *np; unsigned int p, i, j; unsigned long hash, nni; lh->num_nodes++; lh->num_expands++; p = (int)lh->p++; n1 = &(lh->b[p]); n2 = &(lh->b[p + (int)lh->pmax]); *n2 = NULL; nni = lh->num_alloc_nodes; for (np = *n1; np != NULL;) { hash = np->hash; if ((hash % nni) != p) { *n1 = (*n1)->next; np->next = *n2; *n2 = np; } else n1 = &((*n1)->next); np = *n1; } if ((lh->p) >= lh->pmax) { j = (int)lh->num_alloc_nodes * 2; n = OPENSSL_realloc(lh->b, (int)(sizeof(OPENSSL_LH_NODE *) * j)); if (n == NULL) { lh->error++; lh->p = 0; return; } for (i = (int)lh->num_alloc_nodes; i < j; i++) n[i] = NULL; lh->pmax = lh->num_alloc_nodes; lh->num_alloc_nodes = j; lh->num_expand_reallocs++; lh->p = 0; lh->b = n; } }
['int srp_main(int argc, char **argv)\n{\n CA_DB *db = NULL;\n CONF *conf = NULL;\n int gNindex = -1, maxgN = -1, ret = 1, errors = 0, verbose = 0, i;\n int doupdatedb = 0, mode = OPT_ERR;\n char *user = NULL, *passinarg = NULL, *passoutarg = NULL;\n char *passin = NULL, *passout = NULL, *gN = NULL, *userinfo = NULL;\n char *randfile = NULL, *section = NULL;\n char **gNrow = NULL, *configfile = NULL;\n char *srpvfile = NULL, **pp, *prog;\n OPTION_CHOICE o;\n prog = opt_init(argc, argv, srp_options);\n while ((o = opt_next()) != OPT_EOF) {\n switch (o) {\n case OPT_EOF:\n case OPT_ERR:\n opthelp:\n BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n goto end;\n case OPT_HELP:\n opt_help(srp_options);\n ret = 0;\n goto end;\n case OPT_VERBOSE:\n verbose++;\n break;\n case OPT_CONFIG:\n configfile = opt_arg();\n break;\n case OPT_NAME:\n section = opt_arg();\n break;\n case OPT_SRPVFILE:\n srpvfile = opt_arg();\n break;\n case OPT_ADD:\n case OPT_DELETE:\n case OPT_MODIFY:\n case OPT_LIST:\n if (mode != OPT_ERR) {\n BIO_printf(bio_err,\n "%s: Only one of -add/delete-modify/-list\\n",\n prog);\n goto opthelp;\n }\n mode = o;\n break;\n case OPT_GN:\n gN = opt_arg();\n break;\n case OPT_USERINFO:\n userinfo = opt_arg();\n break;\n case OPT_PASSIN:\n passinarg = opt_arg();\n break;\n case OPT_PASSOUT:\n passoutarg = opt_arg();\n break;\n case OPT_ENGINE:\n (void)setup_engine(opt_arg(), 0);\n break;\n }\n }\n argc = opt_num_rest();\n argv = opt_rest();\n if (srpvfile && configfile) {\n BIO_printf(bio_err,\n "-srpvfile and -configfile cannot be specified together.\\n");\n goto end;\n }\n if (mode == OPT_ERR) {\n BIO_printf(bio_err,\n "Exactly one of the options -add, -delete, -modify -list must be specified.\\n");\n goto opthelp;\n }\n if ((mode == OPT_DELETE || mode == OPT_MODIFY || mode == OPT_ADD)\n && argc < 1) {\n BIO_printf(bio_err,\n "Need at least one user for options -add, -delete, -modify. \\n");\n goto opthelp;\n }\n if ((passin || passout) && argc != 1) {\n BIO_printf(bio_err,\n "-passin, -passout arguments only valid with one user.\\n");\n goto opthelp;\n }\n if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {\n BIO_printf(bio_err, "Error getting passwords\\n");\n goto end;\n }\n if (!srpvfile) {\n if (!configfile)\n configfile = default_config_file;\n if (verbose)\n BIO_printf(bio_err, "Using configuration from %s\\n",\n configfile);\n conf = app_load_config(configfile);\n if (conf == NULL)\n goto end;\n if (configfile != default_config_file && !app_load_modules(conf))\n goto end;\n if (section == NULL) {\n if (verbose)\n BIO_printf(bio_err,\n "trying to read " ENV_DEFAULT_SRP\n " in " BASE_SECTION "\\n");\n section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_SRP);\n if (section == NULL) {\n lookup_fail(BASE_SECTION, ENV_DEFAULT_SRP);\n goto end;\n }\n }\n if (randfile == NULL && conf)\n randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE");\n if (verbose)\n BIO_printf(bio_err,\n "trying to read " ENV_DATABASE " in section \\"%s\\"\\n",\n section);\n if ((srpvfile = NCONF_get_string(conf, section, ENV_DATABASE))\n == NULL) {\n lookup_fail(section, ENV_DATABASE);\n goto end;\n }\n }\n if (randfile == NULL)\n ERR_clear_error();\n else\n app_RAND_load_file(randfile, 0);\n if (verbose)\n BIO_printf(bio_err, "Trying to read SRP verifier file \\"%s\\"\\n",\n srpvfile);\n db = load_index(srpvfile, NULL);\n if (db == NULL)\n goto end;\n for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {\n pp = sk_OPENSSL_PSTRING_value(db->db->data, i);\n if (pp[DB_srptype][0] == DB_SRP_INDEX) {\n maxgN = i;\n if ((gNindex < 0) && (gN != NULL) && strcmp(gN, pp[DB_srpid]) == 0)\n gNindex = i;\n print_index(db, i, verbose > 1);\n }\n }\n if (verbose)\n BIO_printf(bio_err, "Database initialised\\n");\n if (gNindex >= 0) {\n gNrow = sk_OPENSSL_PSTRING_value(db->db->data, gNindex);\n print_entry(db, gNindex, verbose > 1, "Default g and N");\n } else if (maxgN > 0 && !SRP_get_default_gN(gN)) {\n BIO_printf(bio_err, "No g and N value for index \\"%s\\"\\n", gN);\n goto end;\n } else {\n if (verbose)\n BIO_printf(bio_err, "Database has no g N information.\\n");\n gNrow = NULL;\n }\n if (verbose > 1)\n BIO_printf(bio_err, "Starting user processing\\n");\n if (argc > 0)\n user = *(argv++);\n while (mode == OPT_LIST || user) {\n int userindex = -1;\n if (user)\n if (verbose > 1)\n BIO_printf(bio_err, "Processing user \\"%s\\"\\n", user);\n if ((userindex = get_index(db, user, \'U\')) >= 0) {\n print_user(db, userindex, (verbose > 0)\n || mode == OPT_LIST);\n }\n if (mode == OPT_LIST) {\n if (user == NULL) {\n BIO_printf(bio_err, "List all users\\n");\n for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {\n print_user(db, i, 1);\n }\n } else if (userindex < 0) {\n BIO_printf(bio_err,\n "user \\"%s\\" does not exist, ignored. t\\n", user);\n errors++;\n }\n } else if (mode == OPT_ADD) {\n if (userindex >= 0) {\n char **row =\n sk_OPENSSL_PSTRING_value(db->db->data, userindex);\n BIO_printf(bio_err, "user \\"%s\\" reactivated.\\n", user);\n row[DB_srptype][0] = \'V\';\n doupdatedb = 1;\n } else {\n char *row[DB_NUMBER];\n char *gNid;\n row[DB_srpverifier] = NULL;\n row[DB_srpsalt] = NULL;\n row[DB_srpinfo] = NULL;\n if (!\n (gNid =\n srp_create_user(user, &(row[DB_srpverifier]),\n &(row[DB_srpsalt]),\n gNrow ? gNrow[DB_srpsalt] : gN,\n gNrow ? gNrow[DB_srpverifier] : NULL,\n passout, verbose))) {\n BIO_printf(bio_err,\n "Cannot create srp verifier for user \\"%s\\", operation abandoned .\\n",\n user);\n errors++;\n goto end;\n }\n row[DB_srpid] = OPENSSL_strdup(user);\n row[DB_srptype] = OPENSSL_strdup("v");\n row[DB_srpgN] = OPENSSL_strdup(gNid);\n if ((row[DB_srpid] == NULL)\n || (row[DB_srpgN] == NULL)\n || (row[DB_srptype] == NULL)\n || (row[DB_srpverifier] == NULL)\n || (row[DB_srpsalt] == NULL)\n || (userinfo\n && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo)) == NULL))\n || !update_index(db, row)) {\n OPENSSL_free(row[DB_srpid]);\n OPENSSL_free(row[DB_srpgN]);\n OPENSSL_free(row[DB_srpinfo]);\n OPENSSL_free(row[DB_srptype]);\n OPENSSL_free(row[DB_srpverifier]);\n OPENSSL_free(row[DB_srpsalt]);\n goto end;\n }\n doupdatedb = 1;\n }\n } else if (mode == OPT_MODIFY) {\n if (userindex < 0) {\n BIO_printf(bio_err,\n "user \\"%s\\" does not exist, operation ignored.\\n",\n user);\n errors++;\n } else {\n char **row =\n sk_OPENSSL_PSTRING_value(db->db->data, userindex);\n char type = row[DB_srptype][0];\n if (type == \'v\') {\n BIO_printf(bio_err,\n "user \\"%s\\" already updated, operation ignored.\\n",\n user);\n errors++;\n } else {\n char *gNid;\n if (row[DB_srptype][0] == \'V\') {\n int user_gN;\n char **irow = NULL;\n if (verbose)\n BIO_printf(bio_err,\n "Verifying password for user \\"%s\\"\\n",\n user);\n if ((user_gN =\n get_index(db, row[DB_srpgN], DB_SRP_INDEX)) >= 0)\n irow =\n sk_OPENSSL_PSTRING_value(db->db->data,\n userindex);\n if (!srp_verify_user\n (user, row[DB_srpverifier], row[DB_srpsalt],\n irow ? irow[DB_srpsalt] : row[DB_srpgN],\n irow ? irow[DB_srpverifier] : NULL, passin,\n verbose)) {\n BIO_printf(bio_err,\n "Invalid password for user \\"%s\\", operation abandoned.\\n",\n user);\n errors++;\n goto end;\n }\n }\n if (verbose)\n BIO_printf(bio_err, "Password for user \\"%s\\" ok.\\n",\n user);\n if (!\n (gNid =\n srp_create_user(user, &(row[DB_srpverifier]),\n &(row[DB_srpsalt]),\n gNrow ? gNrow[DB_srpsalt] : NULL,\n gNrow ? gNrow[DB_srpverifier] : NULL,\n passout, verbose))) {\n BIO_printf(bio_err,\n "Cannot create srp verifier for user \\"%s\\", operation abandoned.\\n",\n user);\n errors++;\n goto end;\n }\n row[DB_srptype][0] = \'v\';\n row[DB_srpgN] = OPENSSL_strdup(gNid);\n if (row[DB_srpid] == NULL\n || row[DB_srpgN] == NULL\n || row[DB_srptype] == NULL\n || row[DB_srpverifier] == NULL\n || row[DB_srpsalt] == NULL\n || (userinfo\n && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo))\n == NULL)))\n goto end;\n doupdatedb = 1;\n }\n }\n } else if (mode == OPT_DELETE) {\n if (userindex < 0) {\n BIO_printf(bio_err,\n "user \\"%s\\" does not exist, operation ignored. t\\n",\n user);\n errors++;\n } else {\n char **xpp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);\n BIO_printf(bio_err, "user \\"%s\\" revoked. t\\n", user);\n xpp[DB_srptype][0] = \'R\';\n doupdatedb = 1;\n }\n }\n if (--argc > 0)\n user = *(argv++);\n else {\n user = NULL;\n }\n }\n if (verbose)\n BIO_printf(bio_err, "User procession done.\\n");\n if (doupdatedb) {\n for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {\n pp = sk_OPENSSL_PSTRING_value(db->db->data, i);\n if (pp[DB_srptype][0] == \'v\') {\n pp[DB_srptype][0] = \'V\';\n print_user(db, i, verbose);\n }\n }\n if (verbose)\n BIO_printf(bio_err, "Trying to update srpvfile.\\n");\n if (!save_index(srpvfile, "new", db))\n goto end;\n if (verbose)\n BIO_printf(bio_err, "Temporary srpvfile created.\\n");\n if (!rotate_index(srpvfile, "new", "old"))\n goto end;\n if (verbose)\n BIO_printf(bio_err, "srpvfile updated.\\n");\n }\n ret = (errors != 0);\n end:\n if (errors != 0)\n if (verbose)\n BIO_printf(bio_err, "User errors %d.\\n", errors);\n if (verbose)\n BIO_printf(bio_err, "SRP terminating with code %d.\\n", ret);\n if (ret)\n ERR_print_errors(bio_err);\n if (randfile)\n app_RAND_write_file(randfile);\n NCONF_free(conf);\n free_index(db);\n return (ret);\n}', 'static char *srp_create_user(char *user, char **srp_verifier,\n char **srp_usersalt, char *g, char *N,\n char *passout, int verbose)\n{\n char password[1024];\n PW_CB_DATA cb_tmp;\n char *gNid = NULL;\n char *salt = NULL;\n cb_tmp.prompt_info = user;\n cb_tmp.password = passout;\n if (password_callback(password, 1024, 1, &cb_tmp) > 0) {\n if (verbose)\n BIO_printf(bio_err, "Creating\\n user=\\"%s\\"\\n g=\\"%s\\"\\n N=\\"%s\\"\\n",\n user, g, N);\n if (!\n (gNid =\n SRP_create_verifier(user, password, &salt, srp_verifier, N,\n g))) {\n BIO_printf(bio_err, "Internal error creating SRP verifier\\n");\n } else\n *srp_usersalt = salt;\n if (verbose > 1)\n BIO_printf(bio_err, "gNid=%s salt =\\"%s\\"\\n verifier =\\"%s\\"\\n", gNid,\n salt, *srp_verifier);\n }\n return gNid;\n}', 'char *SRP_create_verifier(const char *user, const char *pass, char **salt,\n char **verifier, const char *N, const char *g)\n{\n int len;\n char *result = NULL, *vf = NULL;\n BIGNUM *N_bn = NULL, *g_bn = NULL, *s = NULL, *v = NULL;\n unsigned char tmp[MAX_LEN];\n unsigned char tmp2[MAX_LEN];\n char *defgNid = NULL;\n int vfsize = 0;\n if ((user == NULL) ||\n (pass == NULL) || (salt == NULL) || (verifier == NULL))\n goto err;\n if (N) {\n if ((len = t_fromb64(tmp, N)) == 0)\n goto err;\n N_bn = BN_bin2bn(tmp, len, NULL);\n if ((len = t_fromb64(tmp, g)) == 0)\n goto err;\n g_bn = BN_bin2bn(tmp, len, NULL);\n defgNid = "*";\n } else {\n SRP_gN *gN = SRP_get_gN_by_id(g, NULL);\n if (gN == NULL)\n goto err;\n N_bn = gN->N;\n g_bn = gN->g;\n defgNid = gN->id;\n }\n if (*salt == NULL) {\n if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n goto err;\n s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n } else {\n if ((len = t_fromb64(tmp2, *salt)) == 0)\n goto err;\n s = BN_bin2bn(tmp2, len, NULL);\n }\n if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))\n goto err;\n BN_bn2bin(v, tmp);\n vfsize = BN_num_bytes(v) * 2;\n if (((vf = OPENSSL_malloc(vfsize)) == NULL))\n goto err;\n t_tob64(vf, tmp, BN_num_bytes(v));\n if (*salt == NULL) {\n char *tmp_salt;\n if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {\n goto err;\n }\n t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);\n *salt = tmp_salt;\n }\n *verifier = vf;\n vf = NULL;\n result = defgNid;\n err:\n if (N) {\n BN_free(N_bn);\n BN_free(g_bn);\n }\n OPENSSL_clear_free(vf, vfsize);\n BN_clear_free(s);\n BN_clear_free(v);\n return result;\n}', 'int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,\n BIGNUM **verifier, const BIGNUM *N,\n const BIGNUM *g)\n{\n int result = 0;\n BIGNUM *x = NULL;\n BN_CTX *bn_ctx = BN_CTX_new();\n unsigned char tmp2[MAX_LEN];\n BIGNUM *salttmp = NULL;\n if ((user == NULL) ||\n (pass == NULL) ||\n (salt == NULL) ||\n (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))\n goto err;\n if (*salt == NULL) {\n if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n goto err;\n salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n } else {\n salttmp = *salt;\n }\n x = SRP_Calc_x(salttmp, user, pass);\n *verifier = BN_new();\n if (*verifier == NULL)\n goto err;\n if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {\n BN_clear_free(*verifier);\n goto err;\n }\n result = 1;\n *salt = salttmp;\n err:\n if (salt != NULL && *salt != salttmp)\n BN_clear_free(salttmp);\n BN_clear_free(x);\n BN_CTX_free(bn_ctx);\n return result;\n}', 'BIGNUM *SRP_Calc_x(BIGNUM *s, const char *user, const char *pass)\n{\n unsigned char dig[SHA_DIGEST_LENGTH];\n EVP_MD_CTX *ctxt;\n unsigned char *cs;\n BIGNUM *res = NULL;\n if ((s == NULL) || (user == NULL) || (pass == NULL))\n return NULL;\n ctxt = EVP_MD_CTX_new();\n if (ctxt == NULL)\n return NULL;\n if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)\n goto err;\n EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL);\n EVP_DigestUpdate(ctxt, user, strlen(user));\n EVP_DigestUpdate(ctxt, ":", 1);\n EVP_DigestUpdate(ctxt, pass, strlen(pass));\n EVP_DigestFinal_ex(ctxt, dig, NULL);\n EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL);\n BN_bn2bin(s, cs);\n EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s));\n OPENSSL_free(cs);\n EVP_DigestUpdate(ctxt, dig, sizeof(dig));\n EVP_DigestFinal_ex(ctxt, dig, NULL);\n res = BN_bin2bn(dig, sizeof(dig), NULL);\n err:\n EVP_MD_CTX_free(ctxt);\n return res;\n}', 'int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)\n{\n EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n#ifndef OPENSSL_NO_ENGINE\n if (ctx->engine && ctx->digest &&\n (type == NULL || (type->type == ctx->digest->type)))\n goto skip_to_init;\n if (type) {\n ENGINE_finish(ctx->engine);\n if (impl != NULL) {\n if (!ENGINE_init(impl)) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n return 0;\n }\n } else {\n impl = ENGINE_get_digest_engine(type->type);\n }\n if (impl != NULL) {\n const EVP_MD *d = ENGINE_get_digest(impl, type->type);\n if (d == NULL) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n ENGINE_finish(impl);\n return 0;\n }\n type = d;\n ctx->engine = impl;\n } else\n ctx->engine = NULL;\n } else {\n if (!ctx->digest) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);\n return 0;\n }\n type = ctx->digest;\n }\n#endif\n if (ctx->digest != type) {\n if (ctx->digest && ctx->digest->ctx_size) {\n OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);\n ctx->md_data = NULL;\n }\n ctx->digest = type;\n if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {\n ctx->update = type->update;\n ctx->md_data = OPENSSL_zalloc(type->ctx_size);\n if (ctx->md_data == NULL) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n }\n }\n#ifndef OPENSSL_NO_ENGINE\n skip_to_init:\n#endif\n if (ctx->pctx) {\n int r;\n r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,\n EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);\n if (r <= 0 && (r != -2))\n return 0;\n }\n if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)\n return 1;\n return ctx->digest->init(ctx);\n}', 'ENGINE *ENGINE_get_digest_engine(int nid)\n{\n return engine_table_select(&digest_table, nid);\n}', 'ENGINE *engine_table_select(ENGINE_TABLE **table, int nid)\n#else\nENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,\n int l)\n#endif\n{\n ENGINE *ret = NULL;\n ENGINE_PILE tmplate, *fnd = NULL;\n int initres, loop = 0;\n if (!(*table)) {\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing "\n "registered!\\n", f, l, nid);\n#endif\n return NULL;\n }\n ERR_set_mark();\n CRYPTO_THREAD_write_lock(global_engine_lock);\n if (!int_table_check(table, 0))\n goto end;\n tmplate.nid = nid;\n fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);\n if (!fnd)\n goto end;\n if (fnd->funct && engine_unlocked_init(fnd->funct)) {\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "\n "ENGINE \'%s\' cached\\n", f, l, nid, fnd->funct->id);\n#endif\n ret = fnd->funct;\n goto end;\n }\n if (fnd->uptodate) {\n ret = fnd->funct;\n goto end;\n }\n trynext:\n ret = sk_ENGINE_value(fnd->sk, loop++);\n if (!ret) {\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no "\n "registered implementations would initialise\\n", f, l, nid);\n#endif\n goto end;\n }\n if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))\n initres = engine_unlocked_init(ret);\n else\n initres = 0;\n if (initres) {\n if ((fnd->funct != ret) && engine_unlocked_init(ret)) {\n if (fnd->funct)\n engine_unlocked_finish(fnd->funct, 0);\n fnd->funct = ret;\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, "\n "setting default to \'%s\'\\n", f, l, nid, ret->id);\n#endif\n }\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "\n "newly initialised \'%s\'\\n", f, l, nid, ret->id);\n#endif\n goto end;\n }\n goto trynext;\n end:\n if (fnd)\n fnd->uptodate = 1;\n#ifdef ENGINE_TABLE_DEBUG\n if (ret)\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "\n "ENGINE \'%s\'\\n", f, l, nid, ret->id);\n else\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "\n "\'no matching ENGINE\'\\n", f, l, nid);\n#endif\n CRYPTO_THREAD_unlock(global_engine_lock);\n ERR_pop_to_mark();\n return ret;\n}', 'static int int_table_check(ENGINE_TABLE **t, int create)\n{\n LHASH_OF(ENGINE_PILE) *lh;\n if (*t)\n return 1;\n if (!create)\n return 0;\n if ((lh = lh_ENGINE_PILE_new(engine_pile_hash, engine_pile_cmp)) == NULL)\n return 0;\n *t = (ENGINE_TABLE *)lh;\n return 1;\n}', 'DEFINE_LHASH_OF(ENGINE_PILE)', 'OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)\n{\n OPENSSL_LHASH *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)\n goto err0;\n if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)\n goto err1;\n ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);\n ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);\n ret->num_nodes = MIN_NODES / 2;\n ret->num_alloc_nodes = MIN_NODES;\n ret->pmax = MIN_NODES / 2;\n ret->up_load = UP_LOAD;\n ret->down_load = DOWN_LOAD;\n return (ret);\n err1:\n OPENSSL_free(ret);\n err0:\n return (NULL);\n}', 'static int update_index(CA_DB *db, char **row)\n{\n char **irow;\n int i;\n irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers");\n for (i = 0; i < DB_NUMBER; i++) {\n irow[i] = row[i];\n row[i] = NULL;\n }\n irow[DB_NUMBER] = NULL;\n if (!TXT_DB_insert(db->db, irow)) {\n BIO_printf(bio_err, "failed to update srpvfile\\n");\n BIO_printf(bio_err, "TXT_DB error number %ld\\n", db->db->error);\n OPENSSL_free(irow);\n return 0;\n }\n return 1;\n}', 'int TXT_DB_insert(TXT_DB *db, OPENSSL_STRING *row)\n{\n int i;\n OPENSSL_STRING *r;\n for (i = 0; i < db->num_fields; i++) {\n if (db->index[i] != NULL) {\n if ((db->qual[i] != NULL) && (db->qual[i] (row) == 0))\n continue;\n r = lh_OPENSSL_STRING_retrieve(db->index[i], row);\n if (r != NULL) {\n db->error = DB_ERROR_INDEX_CLASH;\n db->arg1 = i;\n db->arg_row = r;\n goto err;\n }\n }\n }\n if (!sk_OPENSSL_PSTRING_push(db->data, row)) {\n db->error = DB_ERROR_MALLOC;\n goto err;\n }\n for (i = 0; i < db->num_fields; i++) {\n if (db->index[i] != NULL) {\n if ((db->qual[i] != NULL) && (db->qual[i] (row) == 0))\n continue;\n (void)lh_OPENSSL_STRING_insert(db->index[i], row);\n }\n }\n return (1);\n err:\n return (0);\n}', 'void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n if (lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))\n expand(lh);\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {\n lh->error++;\n return (NULL);\n }\n nn->data = data;\n nn->next = NULL;\n nn->hash = hash;\n *rn = nn;\n ret = NULL;\n lh->num_insert++;\n lh->num_items++;\n } else {\n ret = (*rn)->data;\n (*rn)->data = data;\n lh->num_replace++;\n }\n return (ret);\n}', 'static void expand(OPENSSL_LHASH *lh)\n{\n OPENSSL_LH_NODE **n, **n1, **n2, *np;\n unsigned int p, i, j;\n unsigned long hash, nni;\n lh->num_nodes++;\n lh->num_expands++;\n p = (int)lh->p++;\n n1 = &(lh->b[p]);\n n2 = &(lh->b[p + (int)lh->pmax]);\n *n2 = NULL;\n nni = lh->num_alloc_nodes;\n for (np = *n1; np != NULL;) {\n hash = np->hash;\n if ((hash % nni) != p) {\n *n1 = (*n1)->next;\n np->next = *n2;\n *n2 = np;\n } else\n n1 = &((*n1)->next);\n np = *n1;\n }\n if ((lh->p) >= lh->pmax) {\n j = (int)lh->num_alloc_nodes * 2;\n n = OPENSSL_realloc(lh->b, (int)(sizeof(OPENSSL_LH_NODE *) * j));\n if (n == NULL) {\n lh->error++;\n lh->p = 0;\n return;\n }\n for (i = (int)lh->num_alloc_nodes; i < j; i++)\n n[i] = NULL;\n lh->pmax = lh->num_alloc_nodes;\n lh->num_alloc_nodes = j;\n lh->num_expand_reallocs++;\n lh->p = 0;\n lh->b = n;\n }\n}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)\n return realloc_impl(str, num, file, line);\n if (str == NULL)\n return CRYPTO_malloc(num, file, line);\n if (num == 0) {\n CRYPTO_free(str, file, line);\n return NULL;\n }\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n void *ret;\n CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n ret = realloc(str, num);\n CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n return ret;\n }\n#else\n osslargused(file); osslargused(line);\n#endif\n return realloc(str, num);\n}']
35,840
0
https://github.com/openssl/openssl/blob/a4af39ac4482355ffdd61fb61231a0c79b96997b/crypto/pkcs7/pk7_doit.c/#L824
PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx) { STACK_OF(PKCS7_RECIP_INFO) *rsk; PKCS7_RECIP_INFO *ri; int i; i=OBJ_obj2nid(p7->type); if (i != NID_pkcs7_signedAndEnveloped) return(NULL); rsk=p7->d.signed_and_enveloped->recipientinfo; ri=sk_PKCS7_RECIP_INFO_value(rsk,0); if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) return(NULL); ri=sk_PKCS7_RECIP_INFO_value(rsk,idx); return(ri->issuer_and_serial); }
['PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)\n\t{\n\tSTACK_OF(PKCS7_RECIP_INFO) *rsk;\n\tPKCS7_RECIP_INFO *ri;\n\tint i;\n\ti=OBJ_obj2nid(p7->type);\n\tif (i != NID_pkcs7_signedAndEnveloped) return(NULL);\n\trsk=p7->d.signed_and_enveloped->recipientinfo;\n\tri=sk_PKCS7_RECIP_INFO_value(rsk,0);\n\tif (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) return(NULL);\n\tri=sk_PKCS7_RECIP_INFO_value(rsk,idx);\n\treturn(ri->issuer_and_serial);\n\t}', 'int OBJ_obj2nid(ASN1_OBJECT *a)\n\t{\n\tASN1_OBJECT **op;\n\tADDED_OBJ ad,*adp;\n\tif (a == NULL)\n\t\treturn(NID_undef);\n\tif (a->nid != 0)\n\t\treturn(a->nid);\n\tif (added != NULL)\n\t\t{\n\t\tad.type=ADDED_DATA;\n\t\tad.obj=a;\n\t\tadp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);\n\t\tif (adp != NULL) return (adp->obj->nid);\n\t\t}\n\top=(ASN1_OBJECT **)OBJ_bsearch((char *)&a,(char *)obj_objs,NUM_OBJ,\n\t\tsizeof(ASN1_OBJECT *),(int (*)())obj_cmp);\n\tif (op == NULL)\n\t\treturn(NID_undef);\n\treturn((*op)->nid);\n\t}', 'IMPLEMENT_STACK_OF(PKCS7_RECIP_INFO)', 'char *sk_value(STACK *st, int i)\n{\n\tif(st == NULL) return NULL;\n\treturn st->data[i];\n}']
35,841
0
https://github.com/openssl/openssl/blob/54d00677f305375eee65a0c9edb5f0980c5f020f/crypto/bn/bn_lib.c/#L232
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],\n BN_CTX *ctx)\n{\n int ret = 0, count = 0, j;\n BIGNUM *a, *z, *rho, *w, *w2, *tmp;\n bn_check_top(a_);\n if (!p[0]) {\n BN_zero(r);\n return 1;\n }\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n z = BN_CTX_get(ctx);\n w = BN_CTX_get(ctx);\n if (w == NULL)\n goto err;\n if (!BN_GF2m_mod_arr(a, a_, p))\n goto err;\n if (BN_is_zero(a)) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n if (p[0] & 0x1) {\n if (!BN_copy(z, a))\n goto err;\n for (j = 1; j <= (p[0] - 1) / 2; j++) {\n if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))\n goto err;\n if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))\n goto err;\n if (!BN_GF2m_add(z, z, a))\n goto err;\n }\n } else {\n rho = BN_CTX_get(ctx);\n w2 = BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n do {\n if (!BN_priv_rand(rho, p[0], BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))\n goto err;\n if (!BN_GF2m_mod_arr(rho, rho, p))\n goto err;\n BN_zero(z);\n if (!BN_copy(w, rho))\n goto err;\n for (j = 1; j <= p[0] - 1; j++) {\n if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))\n goto err;\n if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))\n goto err;\n if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))\n goto err;\n if (!BN_GF2m_add(z, z, tmp))\n goto err;\n if (!BN_GF2m_add(w, w2, rho))\n goto err;\n }\n count++;\n } while (BN_is_zero(w) && (count < MAX_ITERATIONS));\n if (BN_is_zero(w)) {\n BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_TOO_MANY_ITERATIONS);\n goto err;\n }\n }\n if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))\n goto err;\n if (!BN_GF2m_add(w, z, w))\n goto err;\n if (BN_GF2m_cmp(w, a)) {\n BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_NO_SOLUTION);\n goto err;\n }\n if (!BN_copy(r, z))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[])\n{\n int j, k;\n int n, dN, d0, d1;\n BN_ULONG zz, *z;\n bn_check_top(a);\n if (!p[0]) {\n BN_zero(r);\n return 1;\n }\n if (a != r) {\n if (!bn_wexpand(r, a->top))\n return 0;\n for (j = 0; j < a->top; j++) {\n r->d[j] = a->d[j];\n }\n r->top = a->top;\n }\n z = r->d;\n dN = p[0] / BN_BITS2;\n for (j = r->top - 1; j > dN;) {\n zz = z[j];\n if (z[j] == 0) {\n j--;\n continue;\n }\n z[j] = 0;\n for (k = 1; p[k] != 0; k++) {\n n = p[0] - p[k];\n d0 = n % BN_BITS2;\n d1 = BN_BITS2 - d0;\n n /= BN_BITS2;\n z[j - n] ^= (zz >> d0);\n if (d0)\n z[j - n - 1] ^= (zz << d1);\n }\n n = dN;\n d0 = p[0] % BN_BITS2;\n d1 = BN_BITS2 - d0;\n z[j - n] ^= (zz >> d0);\n if (d0)\n z[j - n - 1] ^= (zz << d1);\n }\n while (j == dN) {\n d0 = p[0] % BN_BITS2;\n zz = z[dN] >> d0;\n if (zz == 0)\n break;\n d1 = BN_BITS2 - d0;\n if (d0)\n z[dN] = (z[dN] << d1) >> d1;\n else\n z[dN] = 0;\n z[0] ^= zz;\n for (k = 1; p[k] != 0; k++) {\n BN_ULONG tmp_ulong;\n n = p[k] / BN_BITS2;\n d0 = p[k] % BN_BITS2;\n d1 = BN_BITS2 - d0;\n z[n] ^= (zz << d0);\n if (d0 && (tmp_ulong = zz >> d1))\n z[n + 1] ^= tmp_ulong;\n }\n }\n bn_correct_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
35,842
0
https://github.com/openssl/openssl/blob/54d00677f305375eee65a0c9edb5f0980c5f020f/crypto/bn/bn_lib.c/#L232
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int ec_GFp_mont_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,\n BN_CTX *ctx)\n{\n BIGNUM *e = NULL;\n BN_CTX *new_ctx = NULL;\n int ret = 0;\n if (group->field_data1 == NULL)\n return 0;\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)\n return 0;\n BN_CTX_start(ctx);\n if ((e = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_set_word(e, 2))\n goto err;\n if (!BN_sub(e, group->field, e))\n goto err;\n if (!BN_mod_exp_mont(r, a, e, group->field, ctx, group->field_data1))\n goto err;\n if (BN_is_zero(r)) {\n ECerr(EC_F_EC_GFP_MONT_FIELD_INV, EC_R_CANNOT_INVERT);\n goto err;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n BN_ULONG t1, t2, borrow, *rp;\n const BN_ULONG *ap, *bp;\n bn_check_top(a);\n bn_check_top(b);\n max = a->top;\n min = b->top;\n dif = max - min;\n if (dif < 0) {\n BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n return 0;\n }\n if (bn_wexpand(r, max) == NULL)\n return 0;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n borrow = bn_sub_words(rp, ap, bp, min);\n ap += min;\n rp += min;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 - borrow) & BN_MASK2;\n *(rp++) = t2;\n borrow &= (t1 == 0);\n }\n while (max && *--rp == 0)\n max--;\n r->top = max;\n r->neg = 0;\n bn_pollute(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
35,843
0
https://github.com/libav/libav/blob/09f25533a564eab743f258d168697a11122914c4/libavformat/movenc.c/#L1165
static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track) { MOVStts *ctts_entries; uint32_t entries = 0; uint32_t atom_size; int i; ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); ctts_entries[0].count = 1; ctts_entries[0].duration = track->cluster[0].cts; for (i = 1; i < track->entry; i++) { if (track->cluster[i].cts == ctts_entries[entries].duration) { ctts_entries[entries].count++; } else { entries++; ctts_entries[entries].duration = track->cluster[i].cts; ctts_entries[entries].count = 1; } } entries++; atom_size = 16 + (entries * 8); avio_wb32(pb, atom_size); ffio_wfourcc(pb, "ctts"); avio_wb32(pb, 0); avio_wb32(pb, entries); for (i = 0; i < entries; i++) { avio_wb32(pb, ctts_entries[i].count); avio_wb32(pb, ctts_entries[i].duration); } av_free(ctts_entries); return atom_size; }
['static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)\n{\n MOVStts *ctts_entries;\n uint32_t entries = 0;\n uint32_t atom_size;\n int i;\n ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries));\n ctts_entries[0].count = 1;\n ctts_entries[0].duration = track->cluster[0].cts;\n for (i = 1; i < track->entry; i++) {\n if (track->cluster[i].cts == ctts_entries[entries].duration) {\n ctts_entries[entries].count++;\n } else {\n entries++;\n ctts_entries[entries].duration = track->cluster[i].cts;\n ctts_entries[entries].count = 1;\n }\n }\n entries++;\n atom_size = 16 + (entries * 8);\n avio_wb32(pb, atom_size);\n ffio_wfourcc(pb, "ctts");\n avio_wb32(pb, 0);\n avio_wb32(pb, entries);\n for (i = 0; i < entries; i++) {\n avio_wb32(pb, ctts_entries[i].count);\n avio_wb32(pb, ctts_entries[i].duration);\n }\n av_free(ctts_entries);\n return atom_size;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if (size > (INT_MAX - 32) || !size)\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size + 32);\n if (!ptr)\n return ptr;\n diff = ((-(long)ptr - 1) & 31) + 1;\n ptr = (char *)ptr + diff;\n ((char *)ptr)[-1] = diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr, 32, size))\n ptr = NULL;\n#elif HAVE_ALIGNED_MALLOC\n ptr = _aligned_malloc(size, 32);\n#elif HAVE_MEMALIGN\n ptr = memalign(32, size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
35,844
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/wmaenc.c/#L269
static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], int total_gain){ int v, bsize, ch, coef_nb_bits, parse_exponents; float mdct_norm; int nb_coefs[MAX_CHANNELS]; static const int fixed_exp[25]={20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20}; if (s->use_variable_block_len) { assert(0); }else{ s->next_block_len_bits = s->frame_len_bits; s->prev_block_len_bits = s->frame_len_bits; s->block_len_bits = s->frame_len_bits; } s->block_len = 1 << s->block_len_bits; bsize = s->frame_len_bits - s->block_len_bits; v = s->coefs_end[bsize] - s->coefs_start; for(ch = 0; ch < s->nb_channels; ch++) nb_coefs[ch] = v; { int n4 = s->block_len / 2; mdct_norm = 1.0 / (float)n4; if (s->version == 1) { mdct_norm *= sqrt(n4); } } if (s->nb_channels == 2) { put_bits(&s->pb, 1, s->ms_stereo= 1); } for(ch = 0; ch < s->nb_channels; ch++) { if ((s->channel_coded[ch]= 1)) { init_exp(s, ch, fixed_exp); } } for(ch = 0; ch < s->nb_channels; ch++) { if (s->channel_coded[ch]) { int16_t *coefs1; float *coefs, *exponents, mult; int i, n; coefs1 = s->coefs1[ch]; exponents = s->exponents[ch]; mult = pow(10, total_gain * 0.05) / s->max_exponent[ch]; mult *= mdct_norm; coefs = src_coefs[ch]; if (s->use_noise_coding && 0) { assert(0); } else { coefs += s->coefs_start; n = nb_coefs[ch]; for(i = 0;i < n; i++){ double t= *coefs++ / (exponents[i] * mult); if(t<-32768 || t>32767) return -1; coefs1[i] = lrint(t); } } } } v = 0; for(ch = 0; ch < s->nb_channels; ch++) { int a = s->channel_coded[ch]; put_bits(&s->pb, 1, a); v |= a; } if (!v) return 1; for(v= total_gain-1; v>=127; v-= 127) put_bits(&s->pb, 7, 127); put_bits(&s->pb, 7, v); coef_nb_bits= ff_wma_total_gain_to_bits(total_gain); if (s->use_noise_coding) { for(ch = 0; ch < s->nb_channels; ch++) { if (s->channel_coded[ch]) { int i, n; n = s->exponent_high_sizes[bsize]; for(i=0;i<n;i++) { put_bits(&s->pb, 1, s->high_band_coded[ch][i]= 0); if (0) nb_coefs[ch] -= s->exponent_high_bands[bsize][i]; } } } } parse_exponents = 1; if (s->block_len_bits != s->frame_len_bits) { put_bits(&s->pb, 1, parse_exponents); } if (parse_exponents) { for(ch = 0; ch < s->nb_channels; ch++) { if (s->channel_coded[ch]) { if (s->use_exp_vlc) { encode_exp_vlc(s, ch, fixed_exp); } else { assert(0); } } } } else { assert(0); } for(ch = 0; ch < s->nb_channels; ch++) { if (s->channel_coded[ch]) { int run, tindex; int16_t *ptr, *eptr; tindex = (ch == 1 && s->ms_stereo); ptr = &s->coefs1[ch][0]; eptr = ptr + nb_coefs[ch]; run=0; for(;ptr < eptr; ptr++){ if(*ptr){ int level= *ptr; int abs_level= FFABS(level); int code= 0; if(abs_level <= s->coef_vlcs[tindex]->max_level){ if(run < s->coef_vlcs[tindex]->levels[abs_level-1]) code= run + s->int_table[tindex][abs_level-1]; } assert(code < s->coef_vlcs[tindex]->n); put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[code], s->coef_vlcs[tindex]->huffcodes[code]); if(code == 0){ if(1<<coef_nb_bits <= abs_level) return -1; put_bits(&s->pb, coef_nb_bits, abs_level); put_bits(&s->pb, s->frame_len_bits, run); } put_bits(&s->pb, 1, level < 0); run=0; }else{ run++; } } if(run) put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1], s->coef_vlcs[tindex]->huffcodes[1]); } if (s->version == 1 && s->nb_channels >= 2) { align_put_bits(&s->pb); } } return 0; }
['static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], int total_gain){\n int v, bsize, ch, coef_nb_bits, parse_exponents;\n float mdct_norm;\n int nb_coefs[MAX_CHANNELS];\n static const int fixed_exp[25]={20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20};\n if (s->use_variable_block_len) {\n assert(0);\n }else{\n s->next_block_len_bits = s->frame_len_bits;\n s->prev_block_len_bits = s->frame_len_bits;\n s->block_len_bits = s->frame_len_bits;\n }\n s->block_len = 1 << s->block_len_bits;\n bsize = s->frame_len_bits - s->block_len_bits;\n v = s->coefs_end[bsize] - s->coefs_start;\n for(ch = 0; ch < s->nb_channels; ch++)\n nb_coefs[ch] = v;\n {\n int n4 = s->block_len / 2;\n mdct_norm = 1.0 / (float)n4;\n if (s->version == 1) {\n mdct_norm *= sqrt(n4);\n }\n }\n if (s->nb_channels == 2) {\n put_bits(&s->pb, 1, s->ms_stereo= 1);\n }\n for(ch = 0; ch < s->nb_channels; ch++) {\n if ((s->channel_coded[ch]= 1)) {\n init_exp(s, ch, fixed_exp);\n }\n }\n for(ch = 0; ch < s->nb_channels; ch++) {\n if (s->channel_coded[ch]) {\n int16_t *coefs1;\n float *coefs, *exponents, mult;\n int i, n;\n coefs1 = s->coefs1[ch];\n exponents = s->exponents[ch];\n mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];\n mult *= mdct_norm;\n coefs = src_coefs[ch];\n if (s->use_noise_coding && 0) {\n assert(0);\n } else {\n coefs += s->coefs_start;\n n = nb_coefs[ch];\n for(i = 0;i < n; i++){\n double t= *coefs++ / (exponents[i] * mult);\n if(t<-32768 || t>32767)\n return -1;\n coefs1[i] = lrint(t);\n }\n }\n }\n }\n v = 0;\n for(ch = 0; ch < s->nb_channels; ch++) {\n int a = s->channel_coded[ch];\n put_bits(&s->pb, 1, a);\n v |= a;\n }\n if (!v)\n return 1;\n for(v= total_gain-1; v>=127; v-= 127)\n put_bits(&s->pb, 7, 127);\n put_bits(&s->pb, 7, v);\n coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);\n if (s->use_noise_coding) {\n for(ch = 0; ch < s->nb_channels; ch++) {\n if (s->channel_coded[ch]) {\n int i, n;\n n = s->exponent_high_sizes[bsize];\n for(i=0;i<n;i++) {\n put_bits(&s->pb, 1, s->high_band_coded[ch][i]= 0);\n if (0)\n nb_coefs[ch] -= s->exponent_high_bands[bsize][i];\n }\n }\n }\n }\n parse_exponents = 1;\n if (s->block_len_bits != s->frame_len_bits) {\n put_bits(&s->pb, 1, parse_exponents);\n }\n if (parse_exponents) {\n for(ch = 0; ch < s->nb_channels; ch++) {\n if (s->channel_coded[ch]) {\n if (s->use_exp_vlc) {\n encode_exp_vlc(s, ch, fixed_exp);\n } else {\n assert(0);\n }\n }\n }\n } else {\n assert(0);\n }\n for(ch = 0; ch < s->nb_channels; ch++) {\n if (s->channel_coded[ch]) {\n int run, tindex;\n int16_t *ptr, *eptr;\n tindex = (ch == 1 && s->ms_stereo);\n ptr = &s->coefs1[ch][0];\n eptr = ptr + nb_coefs[ch];\n run=0;\n for(;ptr < eptr; ptr++){\n if(*ptr){\n int level= *ptr;\n int abs_level= FFABS(level);\n int code= 0;\n if(abs_level <= s->coef_vlcs[tindex]->max_level){\n if(run < s->coef_vlcs[tindex]->levels[abs_level-1])\n code= run + s->int_table[tindex][abs_level-1];\n }\n assert(code < s->coef_vlcs[tindex]->n);\n put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[code], s->coef_vlcs[tindex]->huffcodes[code]);\n if(code == 0){\n if(1<<coef_nb_bits <= abs_level)\n return -1;\n put_bits(&s->pb, coef_nb_bits, abs_level);\n put_bits(&s->pb, s->frame_len_bits, run);\n }\n put_bits(&s->pb, 1, level < 0);\n run=0;\n }else{\n run++;\n }\n }\n if(run)\n put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1], s->coef_vlcs[tindex]->huffcodes[1]);\n }\n if (s->version == 1 && s->nb_channels >= 2) {\n align_put_bits(&s->pb);\n }\n }\n return 0;\n}']
35,845
0
https://github.com/libav/libav/blob/a7ac1a7b94447f33ae95be4d6d186e2775977f91/libavcodec/mpegvideo.c/#L657
static int init_context_frame(MpegEncContext *s) { int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y; s->mb_width = (s->width + 15) / 16; s->mb_stride = s->mb_width + 1; s->b8_stride = s->mb_width * 2 + 1; mb_array_size = s->mb_height * s->mb_stride; mv_table_size = (s->mb_height + 2) * s->mb_stride + 1; s->h_edge_pos = s->mb_width * 16; s->v_edge_pos = s->mb_height * 16; s->mb_num = s->mb_width * s->mb_height; s->block_wrap[0] = s->block_wrap[1] = s->block_wrap[2] = s->block_wrap[3] = s->b8_stride; s->block_wrap[4] = s->block_wrap[5] = s->mb_stride; y_size = s->b8_stride * (2 * s->mb_height + 1); c_size = s->mb_stride * (s->mb_height + 1); yc_size = y_size + 2 * c_size; FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); for (y = 0; y < s->mb_height; y++) for (x = 0; x < s->mb_width; x++) s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride; s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; if (s->encoding) { FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1; s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1; s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1; s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1; s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1; s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1; FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail); FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab, mb_array_size * sizeof(float), fail); FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab, mb_array_size * sizeof(float), fail); } if (s->codec_id == AV_CODEC_ID_MPEG4 || (s->avctx->flags & CODEC_FLAG_INTERLACED_ME)) { for (i = 0; i < 2; i++) { int j, k; for (j = 0; j < 2; j++) { for (k = 0; k < 2; k++) { FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_mv_table_base[i][j][k], mv_table_size * 2 * sizeof(int16_t), fail); s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1; } FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail); s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1; } FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail); } } if (s->out_format == FMT_H263) { FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail); s->coded_block = s->coded_block_base + s->b8_stride + 1; FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table, mb_array_size * sizeof(uint8_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail); } if (s->h263_pred || s->h263_plus || !s->encoding) { FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail); s->dc_val[0] = s->dc_val_base + s->b8_stride + 1; s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1; s->dc_val[2] = s->dc_val[1] + c_size; for (i = 0; i < yc_size; i++) s->dc_val_base[i] = 1024; } FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail); memset(s->mbintra_table, 1, mb_array_size); FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail); return ff_mpeg_er_init(s); fail: return AVERROR(ENOMEM); }
['static int init_context_frame(MpegEncContext *s)\n{\n int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;\n s->mb_width = (s->width + 15) / 16;\n s->mb_stride = s->mb_width + 1;\n s->b8_stride = s->mb_width * 2 + 1;\n mb_array_size = s->mb_height * s->mb_stride;\n mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;\n s->h_edge_pos = s->mb_width * 16;\n s->v_edge_pos = s->mb_height * 16;\n s->mb_num = s->mb_width * s->mb_height;\n s->block_wrap[0] =\n s->block_wrap[1] =\n s->block_wrap[2] =\n s->block_wrap[3] = s->b8_stride;\n s->block_wrap[4] =\n s->block_wrap[5] = s->mb_stride;\n y_size = s->b8_stride * (2 * s->mb_height + 1);\n c_size = s->mb_stride * (s->mb_height + 1);\n yc_size = y_size + 2 * c_size;\n FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),\n fail);\n for (y = 0; y < s->mb_height; y++)\n for (x = 0; x < s->mb_width; x++)\n s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;\n s->mb_index2xy[s->mb_height * s->mb_width] =\n (s->mb_height - 1) * s->mb_stride + s->mb_width;\n if (s->encoding) {\n FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;\n s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;\n s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;\n s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +\n s->mb_stride + 1;\n s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +\n s->mb_stride + 1;\n s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;\n FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *\n sizeof(uint16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *\n sizeof(int), fail);\n FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,\n mb_array_size * sizeof(float), fail);\n FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,\n mb_array_size * sizeof(float), fail);\n }\n if (s->codec_id == AV_CODEC_ID_MPEG4 ||\n (s->avctx->flags & CODEC_FLAG_INTERLACED_ME)) {\n for (i = 0; i < 2; i++) {\n int j, k;\n for (j = 0; j < 2; j++) {\n for (k = 0; k < 2; k++) {\n FF_ALLOCZ_OR_GOTO(s->avctx,\n s->b_field_mv_table_base[i][j][k],\n mv_table_size * 2 * sizeof(int16_t),\n fail);\n s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +\n s->mb_stride + 1;\n }\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],\n mb_array_size * 2 * sizeof(uint8_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],\n mv_table_size * 2 * sizeof(int16_t), fail);\n s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]\n + s->mb_stride + 1;\n }\n FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],\n mb_array_size * 2 * sizeof(uint8_t), fail);\n }\n }\n if (s->out_format == FMT_H263) {\n FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);\n s->coded_block = s->coded_block_base + s->b8_stride + 1;\n FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,\n mb_array_size * sizeof(uint8_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,\n mb_array_size * sizeof(uint8_t), fail);\n }\n if (s->h263_pred || s->h263_plus || !s->encoding) {\n FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,\n yc_size * sizeof(int16_t), fail);\n s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;\n s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;\n s->dc_val[2] = s->dc_val[1] + c_size;\n for (i = 0; i < yc_size; i++)\n s->dc_val_base[i] = 1024;\n }\n FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);\n memset(s->mbintra_table, 1, mb_array_size);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);\n return ff_mpeg_er_init(s);\nfail:\n return AVERROR(ENOMEM);\n}', 'void *av_mallocz(size_t size)\n{\n void *ptr = av_malloc(size);\n if (ptr)\n memset(ptr, 0, size);\n return ptr;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if (size > (INT_MAX - 32) || !size)\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size + 32);\n if (!ptr)\n return ptr;\n diff = ((-(long)ptr - 1) & 31) + 1;\n ptr = (char *)ptr + diff;\n ((char *)ptr)[-1] = diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr, 32, size))\n ptr = NULL;\n#elif HAVE_ALIGNED_MALLOC\n ptr = _aligned_malloc(size, 32);\n#elif HAVE_MEMALIGN\n ptr = memalign(32, size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
35,846
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L260
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['static int dh_builtin_genparams(DH *ret, int prime_len, int generator,\n BN_GENCB *cb)\n{\n BIGNUM *t1, *t2;\n int g, ok = -1;\n BN_CTX *ctx = NULL;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n t2 = BN_CTX_get(ctx);\n if (t2 == NULL)\n goto err;\n if (!ret->p && ((ret->p = BN_new()) == NULL))\n goto err;\n if (!ret->g && ((ret->g = BN_new()) == NULL))\n goto err;\n if (generator <= 1) {\n DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);\n goto err;\n }\n if (generator == DH_GENERATOR_2) {\n if (!BN_set_word(t1, 24))\n goto err;\n if (!BN_set_word(t2, 11))\n goto err;\n g = 2;\n } else if (generator == DH_GENERATOR_5) {\n if (!BN_set_word(t1, 10))\n goto err;\n if (!BN_set_word(t2, 3))\n goto err;\n g = 5;\n } else {\n if (!BN_set_word(t1, 2))\n goto err;\n if (!BN_set_word(t2, 1))\n goto err;\n g = generator;\n }\n if (!BN_generate_prime_ex(ret->p, prime_len, 1, t1, t2, cb))\n goto err;\n if (!BN_GENCB_call(cb, 3, 0))\n goto err;\n if (!BN_set_word(ret->g, g))\n goto err;\n ok = 1;\n err:\n if (ok == -1) {\n DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB);\n ok = 0;\n }\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n }\n return ok;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
35,847
0
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_lib.c/#L250
int BN_num_bits(const BIGNUM *a) { int i = a->top - 1; bn_check_top(a); if (BN_is_zero(a)) return 0; return ((i*BN_BITS2) + BN_num_bits_word(a->d[i])); }
['static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)\n\t{\n\tunsigned char *p;\n\tunsigned int bitlen, magic = 0, keyalg;\n\tint outlen, noinc = 0;\n\tif (pk->type == EVP_PKEY_DSA)\n\t\t{\n\t\tbitlen = check_bitlen_dsa(pk->pkey.dsa, ispub, &magic);\n\t\tkeyalg = MS_KEYALG_DSS_SIGN;\n\t\t}\n\telse if (pk->type == EVP_PKEY_RSA)\n\t\t{\n\t\tbitlen = check_bitlen_rsa(pk->pkey.rsa, ispub, &magic);\n\t\tkeyalg = MS_KEYALG_RSA_KEYX;\n\t\t}\n\telse\n\t\treturn -1;\n\tif (bitlen == 0)\n\t\treturn -1;\n\toutlen = 16 + blob_length(bitlen,\n\t\t\tkeyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);\n\tif (out == NULL)\n\t\treturn outlen;\n\tif (*out)\n\t\tp = *out;\n\telse\n\t\t{\n\t\tp = OPENSSL_malloc(outlen);\n\t\tif (!p)\n\t\t\treturn -1;\n\t\t*out = p;\n\t\tnoinc = 1;\n\t\t}\n\tif (ispub)\n\t\t*p++ = MS_PUBLICKEYBLOB;\n\telse\n\t\t*p++ = MS_PRIVATEKEYBLOB;\n\t*p++ = 0x2;\n\t*p++ = 0;\n\t*p++ = 0;\n\twrite_ledword(&p, keyalg);\n\twrite_ledword(&p, magic);\n\twrite_ledword(&p, bitlen);\n\tif (keyalg == MS_KEYALG_DSS_SIGN)\n\t\twrite_dsa(&p, pk->pkey.dsa, ispub);\n\telse\n\t\twrite_rsa(&p, pk->pkey.rsa, ispub);\n\tif (!noinc)\n\t\t*out += outlen;\n\treturn outlen;\n\t}', 'static void write_dsa(unsigned char **out, DSA *dsa, int ispub)\n\t{\n\tint nbyte;\n\tnbyte = BN_num_bytes(dsa->p);\n\twrite_lebn(out, dsa->p, nbyte);\n\twrite_lebn(out, dsa->q, 20);\n\twrite_lebn(out, dsa->g, nbyte);\n\tif (ispub)\n\t\twrite_lebn(out, dsa->pub_key, nbyte);\n\telse\n\t\twrite_lebn(out, dsa->priv_key, 20);\n\tmemset(*out, 0xff, 24);\n\t*out += 24;\n\treturn;\n\t}', 'static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)\n\t{\n\tint nb, i;\n\tunsigned char *p = *out, *q, c;\n\tnb = BN_num_bytes(bn);\n\tBN_bn2bin(bn, p);\n\tq = p + nb - 1;\n\tfor (i = 0; i < nb/2; i++)\n\t\t{\n\t\tc = *p;\n\t\t*p++ = *q;\n\t\t*q-- = c;\n\t\t}\n\t*out += nb;\n\tif (len > 0)\n\t\t{\n\t\tlen -= nb;\n\t\tif (len > 0)\n\t\t\t{\n\t\t\tmemset(*out, 0, len);\n\t\t\t*out += len;\n\t\t\t}\n\t\t}\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tint i = a->top - 1;\n\tbn_check_top(a);\n\tif (BN_is_zero(a)) return 0;\n\treturn ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));\n\t}']
35,848
0
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/bn/bn_lib.c/#L351
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *A, *a = NULL; const BN_ULONG *B; int i; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b,BN_FLG_SECURE)) a = A = OPENSSL_secure_malloc(words * sizeof(*a)); else a = A = OPENSSL_malloc(words * sizeof(*a)); if (A == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } #ifdef PURIFY memset(a, 0, sizeof(*a) * words); #endif #if 1 B = b->d; if (B != NULL) { for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { BN_ULONG a0, a1, a2, a3; a0 = B[0]; a1 = B[1]; a2 = B[2]; a3 = B[3]; A[0] = a0; A[1] = a1; A[2] = a2; A[3] = a3; } switch (b->top & 3) { case 3: A[2] = B[2]; case 2: A[1] = B[1]; case 1: A[0] = B[0]; case 0: ; } } #else memset(A, 0, sizeof(*A) * words); memcpy(A, b->d, sizeof(b->d[0]) * b->top); #endif return (a); }
['int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,\n const EVP_MD *evpmd, const unsigned char *seed_in,\n size_t seed_len, int idx, unsigned char *seed_out,\n int *counter_ret, unsigned long *h_ret,\n BN_GENCB *cb)\n{\n int ok = -1;\n unsigned char *seed = NULL, *seed_tmp = NULL;\n unsigned char md[EVP_MAX_MD_SIZE];\n int mdsize;\n BIGNUM *r0, *W, *X, *c, *test;\n BIGNUM *g = NULL, *q = NULL, *p = NULL;\n BN_MONT_CTX *mont = NULL;\n int i, k, n = 0, m = 0, qsize = N >> 3;\n int counter = 0;\n int r = 0;\n BN_CTX *ctx = NULL;\n EVP_MD_CTX *mctx = EVP_MD_CTX_new();\n unsigned int h = 2;\n if (mctx == NULL)\n goto err;\n if (evpmd == NULL) {\n if (N == 160)\n evpmd = EVP_sha1();\n else if (N == 224)\n evpmd = EVP_sha224();\n else\n evpmd = EVP_sha256();\n }\n mdsize = EVP_MD_size(evpmd);\n if (!ret->p || !ret->q || idx >= 0) {\n if (seed_len == 0)\n seed_len = mdsize;\n seed = OPENSSL_malloc(seed_len);\n if (seed_out)\n seed_tmp = seed_out;\n else\n seed_tmp = OPENSSL_malloc(seed_len);\n if (seed == NULL || seed_tmp == NULL)\n goto err;\n if (seed_in)\n memcpy(seed, seed_in, seed_len);\n }\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n r0 = BN_CTX_get(ctx);\n g = BN_CTX_get(ctx);\n W = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n c = BN_CTX_get(ctx);\n test = BN_CTX_get(ctx);\n if (ret->p && ret->q) {\n p = ret->p;\n q = ret->q;\n if (idx >= 0)\n memcpy(seed_tmp, seed, seed_len);\n goto g_only;\n } else {\n p = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n }\n if (!BN_lshift(test, BN_value_one(), L - 1))\n goto err;\n for (;;) {\n for (;;) {\n unsigned char *pmd;\n if (!BN_GENCB_call(cb, 0, m++))\n goto err;\n if (!seed_in) {\n if (RAND_bytes(seed, seed_len) <= 0)\n goto err;\n }\n if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))\n goto err;\n if (mdsize > qsize)\n pmd = md + mdsize - qsize;\n else\n pmd = md;\n if (mdsize < qsize)\n memset(md + mdsize, 0, qsize - mdsize);\n pmd[0] |= 0x80;\n pmd[qsize - 1] |= 0x01;\n if (!BN_bin2bn(pmd, qsize, q))\n goto err;\n r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,\n seed_in ? 1 : 0, cb);\n if (r > 0)\n break;\n if (r != 0)\n goto err;\n if (seed_in) {\n ok = 0;\n DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);\n goto err;\n }\n }\n if (seed_out)\n memcpy(seed_out, seed, seed_len);\n if (!BN_GENCB_call(cb, 2, 0))\n goto err;\n if (!BN_GENCB_call(cb, 3, 0))\n goto err;\n counter = 0;\n n = (L - 1) / (mdsize << 3);\n for (;;) {\n if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))\n goto err;\n BN_zero(W);\n for (k = 0; k <= n; k++) {\n for (i = seed_len - 1; i >= 0; i--) {\n seed[i]++;\n if (seed[i] != 0)\n break;\n }\n if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))\n goto err;\n if (!BN_bin2bn(md, mdsize, r0))\n goto err;\n if (!BN_lshift(r0, r0, (mdsize << 3) * k))\n goto err;\n if (!BN_add(W, W, r0))\n goto err;\n }\n if (!BN_mask_bits(W, L - 1))\n goto err;\n if (!BN_copy(X, W))\n goto err;\n if (!BN_add(X, X, test))\n goto err;\n if (!BN_lshift1(r0, q))\n goto err;\n if (!BN_mod(c, X, r0, ctx))\n goto err;\n if (!BN_sub(r0, c, BN_value_one()))\n goto err;\n if (!BN_sub(p, X, r0))\n goto err;\n if (BN_cmp(p, test) >= 0) {\n r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);\n if (r > 0)\n goto end;\n if (r != 0)\n goto err;\n }\n counter++;\n if (counter >= (int)(4 * L))\n break;\n }\n if (seed_in) {\n ok = 0;\n DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);\n goto err;\n }\n }\n end:\n if (!BN_GENCB_call(cb, 2, 1))\n goto err;\n g_only:\n if (!BN_sub(test, p, BN_value_one()))\n goto err;\n if (!BN_div(r0, NULL, test, q, ctx))\n goto err;\n if (idx < 0) {\n if (!BN_set_word(test, h))\n goto err;\n } else\n h = 1;\n if (!BN_MONT_CTX_set(mont, p, ctx))\n goto err;\n for (;;) {\n static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };\n if (idx >= 0) {\n md[0] = idx & 0xff;\n md[1] = (h >> 8) & 0xff;\n md[2] = h & 0xff;\n if (!EVP_DigestInit_ex(mctx, evpmd, NULL))\n goto err;\n if (!EVP_DigestUpdate(mctx, seed_tmp, seed_len))\n goto err;\n if (!EVP_DigestUpdate(mctx, ggen, sizeof(ggen)))\n goto err;\n if (!EVP_DigestUpdate(mctx, md, 3))\n goto err;\n if (!EVP_DigestFinal_ex(mctx, md, NULL))\n goto err;\n if (!BN_bin2bn(md, mdsize, test))\n goto err;\n }\n if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))\n goto err;\n if (!BN_is_one(g))\n break;\n if (idx < 0 && !BN_add(test, test, BN_value_one()))\n goto err;\n h++;\n if (idx >= 0 && h > 0xffff)\n goto err;\n }\n if (!BN_GENCB_call(cb, 3, 1))\n goto err;\n ok = 1;\n err:\n if (ok == 1) {\n if (p != ret->p) {\n BN_free(ret->p);\n ret->p = BN_dup(p);\n }\n if (q != ret->q) {\n BN_free(ret->q);\n ret->q = BN_dup(q);\n }\n BN_free(ret->g);\n ret->g = BN_dup(g);\n if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {\n ok = -1;\n goto err;\n }\n if (counter_ret != NULL)\n *counter_ret = counter;\n if (h_ret != NULL)\n *h_ret = h;\n }\n OPENSSL_free(seed);\n if (seed_out != seed_tmp)\n OPENSSL_free(seed_tmp);\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n BN_MONT_CTX_free(mont);\n EVP_MD_CTX_free(mctx);\n return ok;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_malloc(words * sizeof(*a));\n else\n a = A = OPENSSL_malloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#ifdef PURIFY\n memset(a, 0, sizeof(*a) * words);\n#endif\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
35,849
0
https://github.com/openssl/openssl/blob/04fac50045929e7078cad4835478dd7f16b6d4bd/crypto/lhash/lhash.c/#L240
void *lh_delete(_LHASH *lh, const void *data) { unsigned long hash; LHASH_NODE *nn,**rn; void *ret; lh->error=0; rn=getrn(lh,data,&hash); if (*rn == NULL) { lh->num_no_delete++; return(NULL); } else { nn= *rn; *rn=nn->next; ret=nn->data; OPENSSL_free(nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))) contract(lh); return(ret); }
['int MAIN(int argc, char **argv)\n\t{\n\tint build_chain = 0;\n\tSSL *con=NULL;\n#ifndef OPENSSL_NO_KRB5\n\tKSSL_CTX *kctx;\n#endif\n\tint s,k,width,state=0;\n\tchar *cbuf=NULL,*sbuf=NULL,*mbuf=NULL;\n\tint cbuf_len,cbuf_off;\n\tint sbuf_len,sbuf_off;\n\tfd_set readfds,writefds;\n\tshort port=PORT;\n\tint full_log=1;\n\tchar *host=SSL_HOST_NAME;\n\tchar *cert_file=NULL,*key_file=NULL,*chain_file=NULL;\n\tint cert_format = FORMAT_PEM, key_format = FORMAT_PEM;\n\tchar *passarg = NULL, *pass = NULL;\n\tX509 *cert = NULL;\n\tEVP_PKEY *key = NULL;\n\tSTACK_OF(X509) *chain = NULL;\n\tchar *CApath=NULL,*CAfile=NULL;\n\tchar *chCApath=NULL,*chCAfile=NULL;\n\tchar *vfyCApath=NULL,*vfyCAfile=NULL;\n\tint reconnect=0,badop=0,verify=SSL_VERIFY_NONE;\n\tint crlf=0;\n\tint write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending;\n\tSSL_CTX *ctx=NULL;\n\tint ret=1,in_init=1,i,nbio_test=0;\n\tint starttls_proto = PROTO_OFF;\n\tint prexit = 0;\n\tX509_VERIFY_PARAM *vpm = NULL;\n\tint badarg = 0;\n\tconst SSL_METHOD *meth=NULL;\n\tint socket_type=SOCK_STREAM;\n\tBIO *sbio;\n\tchar *inrand=NULL;\n\tint mbuf_len=0;\n\tstruct timeval timeout, *timeoutp;\n#ifndef OPENSSL_NO_ENGINE\n\tchar *engine_id=NULL;\n\tchar *ssl_client_engine_id=NULL;\n\tENGINE *ssl_client_engine=NULL;\n#endif\n\tENGINE *e=NULL;\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)\n\tstruct timeval tv;\n#if defined(OPENSSL_SYS_BEOS_R5)\n\tint stdin_set = 0;\n#endif\n#endif\n#ifndef OPENSSL_NO_TLSEXT\n\tchar *servername = NULL;\n tlsextctx tlsextcbp =\n {NULL,0};\n# ifndef OPENSSL_NO_NEXTPROTONEG\n\tconst char *next_proto_neg_in = NULL;\n# endif\n#endif\n\tchar *sess_in = NULL;\n\tchar *sess_out = NULL;\n\tstruct sockaddr peer;\n\tint peerlen = sizeof(peer);\n\tint enable_timeouts = 0 ;\n\tlong socket_mtu = 0;\n#ifndef OPENSSL_NO_JPAKE\nstatic char *jpake_secret = NULL;\n#define no_jpake !jpake_secret\n#else\n#define no_jpake 1\n#endif\n#ifndef OPENSSL_NO_SRP\n\tchar * srppass = NULL;\n\tint srp_lateuser = 0;\n\tSRP_ARG srp_arg = {NULL,NULL,0,0,0,1024};\n#endif\n\tSSL_EXCERT *exc = NULL;\n\tSSL_CONF_CTX *cctx = NULL;\n\tSTACK_OF(OPENSSL_STRING) *ssl_args = NULL;\n\tchar *crl_file = NULL;\n\tint crl_format = FORMAT_PEM;\n\tint crl_download = 0;\n\tSTACK_OF(X509_CRL) *crls = NULL;\n\tmeth=SSLv23_client_method();\n\tapps_startup();\n\tc_Pause=0;\n\tc_quiet=0;\n\tc_ign_eof=0;\n\tc_debug=0;\n\tc_msg=0;\n\tc_showcerts=0;\n\tif (bio_err == NULL)\n\t\tbio_err=BIO_new_fp(stderr,BIO_NOCLOSE);\n\tif (!load_config(bio_err, NULL))\n\t\tgoto end;\n\tcctx = SSL_CONF_CTX_new();\n\tif (!cctx)\n\t\tgoto end;\n\tSSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);\n\tSSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CMDLINE);\n\tif (\t((cbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||\n\t\t((sbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||\n\t\t((mbuf=OPENSSL_malloc(BUFSIZZ)) == NULL))\n\t\t{\n\t\tBIO_printf(bio_err,"out of memory\\n");\n\t\tgoto end;\n\t\t}\n\tverify_depth=0;\n\tverify_error=X509_V_OK;\n#ifdef FIONBIO\n\tc_nbio=0;\n#endif\n\targc--;\n\targv++;\n\twhile (argc >= 1)\n\t\t{\n\t\tif\t(strcmp(*argv,"-host") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\thost= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-port") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tport=atoi(*(++argv));\n\t\t\tif (port == 0) goto bad;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-connect") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tif (!extract_host_port(*(++argv),&host,NULL,&port))\n\t\t\t\tgoto bad;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-verify") == 0)\n\t\t\t{\n\t\t\tverify=SSL_VERIFY_PEER;\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tverify_depth=atoi(*(++argv));\n\t\t\tif (!c_quiet)\n\t\t\t\tBIO_printf(bio_err,"verify depth is %d\\n",verify_depth);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-cert") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tcert_file= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-CRL") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tcrl_file= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-crl_download") == 0)\n\t\t\tcrl_download = 1;\n\t\telse if\t(strcmp(*argv,"-sess_out") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tsess_out = *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-sess_in") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tsess_in = *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-certform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tcert_format = str2fmt(*(++argv));\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-CRLform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tcrl_format = str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (args_verify(&argv, &argc, &badarg, bio_err, &vpm))\n\t\t\t{\n\t\t\tif (badarg)\n\t\t\t\tgoto bad;\n\t\t\tcontinue;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-verify_return_error") == 0)\n\t\t\tverify_return_error = 1;\n\t\telse if (strcmp(*argv,"-verify_quiet") == 0)\n\t\t\tverify_quiet = 1;\n\t\telse if (strcmp(*argv,"-brief") == 0)\n\t\t\t{\n\t\t\tc_brief = 1;\n\t\t\tverify_quiet = 1;\n\t\t\tc_quiet = 1;\n\t\t\t}\n\t\telse if (args_excert(&argv, &argc, &badarg, bio_err, &exc))\n\t\t\t{\n\t\t\tif (badarg)\n\t\t\t\tgoto bad;\n\t\t\tcontinue;\n\t\t\t}\n\t\telse if (args_ssl(&argv, &argc, cctx, &badarg, bio_err, &ssl_args))\n\t\t\t{\n\t\t\tif (badarg)\n\t\t\t\tgoto bad;\n\t\t\tcontinue;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-prexit") == 0)\n\t\t\tprexit=1;\n\t\telse if\t(strcmp(*argv,"-crlf") == 0)\n\t\t\tcrlf=1;\n\t\telse if\t(strcmp(*argv,"-quiet") == 0)\n\t\t\t{\n\t\t\tc_quiet=1;\n\t\t\tc_ign_eof=1;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-ign_eof") == 0)\n\t\t\tc_ign_eof=1;\n\t\telse if\t(strcmp(*argv,"-no_ign_eof") == 0)\n\t\t\tc_ign_eof=0;\n\t\telse if\t(strcmp(*argv,"-pause") == 0)\n\t\t\tc_Pause=1;\n\t\telse if\t(strcmp(*argv,"-debug") == 0)\n\t\t\tc_debug=1;\n#ifndef OPENSSL_NO_TLSEXT\n\t\telse if\t(strcmp(*argv,"-tlsextdebug") == 0)\n\t\t\tc_tlsextdebug=1;\n\t\telse if\t(strcmp(*argv,"-status") == 0)\n\t\t\tc_status_req=1;\n\t\telse if\t(strcmp(*argv,"-proof_debug") == 0)\n\t\t\tc_proof_debug=1;\n#endif\n#ifdef WATT32\n\t\telse if (strcmp(*argv,"-wdebug") == 0)\n\t\t\tdbug_init();\n#endif\n\t\telse if\t(strcmp(*argv,"-msg") == 0)\n\t\t\tc_msg=1;\n\t\telse if\t(strcmp(*argv,"-msgfile") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tbio_c_msg = BIO_new_file(*(++argv), "w");\n\t\t\t}\n#ifndef OPENSSL_NO_SSL_TRACE\n\t\telse if\t(strcmp(*argv,"-trace") == 0)\n\t\t\tc_msg=2;\n#endif\n\t\telse if\t(strcmp(*argv,"-showcerts") == 0)\n\t\t\tc_showcerts=1;\n\t\telse if\t(strcmp(*argv,"-nbio_test") == 0)\n\t\t\tnbio_test=1;\n\t\telse if\t(strcmp(*argv,"-state") == 0)\n\t\t\tstate=1;\n#ifndef OPENSSL_NO_PSK\n else if (strcmp(*argv,"-psk_identity") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tpsk_identity=*(++argv);\n\t\t\t}\n else if (strcmp(*argv,"-psk") == 0)\n\t\t\t{\n size_t j;\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tpsk_key=*(++argv);\n\t\t\tfor (j = 0; j < strlen(psk_key); j++)\n {\n if (isxdigit((unsigned char)psk_key[j]))\n continue;\n BIO_printf(bio_err,"Not a hex number \'%s\'\\n",*argv);\n goto bad;\n }\n\t\t\t}\n#endif\n#ifndef OPENSSL_NO_SRP\n\t\telse if (strcmp(*argv,"-srpuser") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tsrp_arg.srplogin= *(++argv);\n\t\t\tmeth=TLSv1_client_method();\n\t\t\t}\n\t\telse if (strcmp(*argv,"-srppass") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tsrppass= *(++argv);\n\t\t\tmeth=TLSv1_client_method();\n\t\t\t}\n\t\telse if (strcmp(*argv,"-srp_strength") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tsrp_arg.strength=atoi(*(++argv));\n\t\t\tBIO_printf(bio_err,"SRP minimal length for N is %d\\n",srp_arg.strength);\n\t\t\tmeth=TLSv1_client_method();\n\t\t\t}\n\t\telse if (strcmp(*argv,"-srp_lateuser") == 0)\n\t\t\t{\n\t\t\tsrp_lateuser= 1;\n\t\t\tmeth=TLSv1_client_method();\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-srp_moregroups") == 0)\n\t\t\t{\n\t\t\tsrp_arg.amp=1;\n\t\t\tmeth=TLSv1_client_method();\n\t\t\t}\n#endif\n#ifndef OPENSSL_NO_SSL2\n\t\telse if\t(strcmp(*argv,"-ssl2") == 0)\n\t\t\tmeth=SSLv2_client_method();\n#endif\n#ifndef OPENSSL_NO_SSL3\n\t\telse if\t(strcmp(*argv,"-ssl3") == 0)\n\t\t\tmeth=SSLv3_client_method();\n#endif\n#ifndef OPENSSL_NO_TLS1\n\t\telse if\t(strcmp(*argv,"-tls1_2") == 0)\n\t\t\tmeth=TLSv1_2_client_method();\n\t\telse if\t(strcmp(*argv,"-tls1_1") == 0)\n\t\t\tmeth=TLSv1_1_client_method();\n\t\telse if\t(strcmp(*argv,"-tls1") == 0)\n\t\t\tmeth=TLSv1_client_method();\n#endif\n#ifndef OPENSSL_NO_DTLS1\n\t\telse if\t(strcmp(*argv,"-dtls1") == 0)\n\t\t\t{\n\t\t\tmeth=DTLSv1_client_method();\n\t\t\tsocket_type=SOCK_DGRAM;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-dtls1_2") == 0)\n\t\t\t{\n\t\t\tmeth=DTLSv1_2_client_method();\n\t\t\tsocket_type=SOCK_DGRAM;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-timeout") == 0)\n\t\t\tenable_timeouts=1;\n\t\telse if (strcmp(*argv,"-mtu") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tsocket_mtu = atol(*(++argv));\n\t\t\t}\n#endif\n\t\telse if\t(strcmp(*argv,"-keyform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkey_format = str2fmt(*(++argv));\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-pass") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tpassarg = *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-cert_chain") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tchain_file= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-key") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkey_file= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-reconnect") == 0)\n\t\t\t{\n\t\t\treconnect=5;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-CApath") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCApath= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-chainCApath") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tchCApath= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-verifyCApath") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tvfyCApath= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-build_chain") == 0)\n\t\t\tbuild_chain = 1;\n\t\telse if\t(strcmp(*argv,"-CAfile") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAfile= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-chainCAfile") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tchCAfile= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-verifyCAfile") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tvfyCAfile= *(++argv);\n\t\t\t}\n#ifndef OPENSSL_NO_TLSEXT\n# ifndef OPENSSL_NO_NEXTPROTONEG\n\t\telse if (strcmp(*argv,"-nextprotoneg") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tnext_proto_neg_in = *(++argv);\n\t\t\t}\n# endif\n#endif\n#ifdef FIONBIO\n\t\telse if (strcmp(*argv,"-nbio") == 0)\n\t\t\t{ c_nbio=1; }\n#endif\n\t\telse if\t(strcmp(*argv,"-starttls") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\t++argv;\n\t\t\tif (strcmp(*argv,"smtp") == 0)\n\t\t\t\tstarttls_proto = PROTO_SMTP;\n\t\t\telse if (strcmp(*argv,"pop3") == 0)\n\t\t\t\tstarttls_proto = PROTO_POP3;\n\t\t\telse if (strcmp(*argv,"imap") == 0)\n\t\t\t\tstarttls_proto = PROTO_IMAP;\n\t\t\telse if (strcmp(*argv,"ftp") == 0)\n\t\t\t\tstarttls_proto = PROTO_FTP;\n\t\t\telse if (strcmp(*argv, "xmpp") == 0)\n\t\t\t\tstarttls_proto = PROTO_XMPP;\n\t\t\telse\n\t\t\t\tgoto bad;\n\t\t\t}\n#ifndef OPENSSL_NO_ENGINE\n\t\telse if\t(strcmp(*argv,"-engine") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tengine_id = *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-ssl_client_engine") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tssl_client_engine_id = *(++argv);\n\t\t\t}\n#endif\n\t\telse if (strcmp(*argv,"-rand") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinrand= *(++argv);\n\t\t\t}\n#ifndef OPENSSL_NO_TLSEXT\n\t\telse if (strcmp(*argv,"-servername") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tservername= *(++argv);\n\t\t\t}\n#endif\n#ifndef OPENSSL_NO_JPAKE\n\t\telse if (strcmp(*argv,"-jpake") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tjpake_secret = *++argv;\n\t\t\t}\n#endif\n\t\telse if (strcmp(*argv,"-use_srtp") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tsrtp_profiles = *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-keymatexport") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkeymatexportlabel= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-keymatexportlen") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkeymatexportlen=atoi(*(++argv));\n\t\t\tif (keymatexportlen == 0) goto bad;\n\t\t\t}\n else\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unknown option %s\\n",*argv);\n\t\t\tbadop=1;\n\t\t\tbreak;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (badop)\n\t\t{\nbad:\n\t\tsc_usage();\n\t\tgoto end;\n\t\t}\n#if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)\n\tif (jpake_secret)\n\t\t{\n\t\tif (psk_key)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,\n\t\t\t\t "Can\'t use JPAKE and PSK together\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tpsk_identity = "JPAKE";\n\t\t}\n#endif\n\tOpenSSL_add_ssl_algorithms();\n\tSSL_load_error_strings();\n#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)\n\tnext_proto.status = -1;\n\tif (next_proto_neg_in)\n\t\t{\n\t\tnext_proto.data = next_protos_parse(&next_proto.len, next_proto_neg_in);\n\t\tif (next_proto.data == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "Error parsing -nextprotoneg argument\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\telse\n\t\tnext_proto.data = NULL;\n#endif\n#ifndef OPENSSL_NO_ENGINE\n e = setup_engine(bio_err, engine_id, 1);\n\tif (ssl_client_engine_id)\n\t\t{\n\t\tssl_client_engine = ENGINE_by_id(ssl_client_engine_id);\n\t\tif (!ssl_client_engine)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,\n\t\t\t\t\t"Error getting client auth engine\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n#endif\n\tif (!app_passwd(bio_err, passarg, NULL, &pass, NULL))\n\t\t{\n\t\tBIO_printf(bio_err, "Error getting password\\n");\n\t\tgoto end;\n\t\t}\n\tif (key_file == NULL)\n\t\tkey_file = cert_file;\n\tif (key_file)\n\t\t{\n\t\tkey = load_key(bio_err, key_file, key_format, 0, pass, e,\n\t\t\t "client certificate private key file");\n\t\tif (!key)\n\t\t\t{\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (cert_file)\n\t\t{\n\t\tcert = load_cert(bio_err,cert_file,cert_format,\n\t\t\t\tNULL, e, "client certificate file");\n\t\tif (!cert)\n\t\t\t{\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (chain_file)\n\t\t{\n\t\tchain = load_certs(bio_err, chain_file,FORMAT_PEM,\n\t\t\t\t\tNULL, e, "client certificate chain");\n\t\tif (!chain)\n\t\t\tgoto end;\n\t\t}\n\tif (crl_file)\n\t\t{\n\t\tX509_CRL *crl;\n\t\tcrl = load_crl(crl_file, crl_format);\n\t\tif (!crl)\n\t\t\t{\n\t\t\tBIO_puts(bio_err, "Error loading CRL\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\tcrls = sk_X509_CRL_new_null();\n\t\tif (!crls || !sk_X509_CRL_push(crls, crl))\n\t\t\t{\n\t\t\tBIO_puts(bio_err, "Error adding CRL\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tX509_CRL_free(crl);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (!load_excert(&exc, bio_err))\n\t\tgoto end;\n\tif (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL\n\t\t&& !RAND_status())\n\t\t{\n\t\tBIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\\n");\n\t\t}\n\tif (inrand != NULL)\n\t\tBIO_printf(bio_err,"%ld semi-random bytes loaded\\n",\n\t\t\tapp_RAND_load_files(inrand));\n\tif (bio_c_out == NULL)\n\t\t{\n\t\tif (c_quiet && !c_debug)\n\t\t\t{\n\t\t\tbio_c_out=BIO_new(BIO_s_null());\n\t\t\tif (c_msg && !bio_c_msg)\n\t\t\t\tbio_c_msg=BIO_new_fp(stdout,BIO_NOCLOSE);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bio_c_out == NULL)\n\t\t\t\tbio_c_out=BIO_new_fp(stdout,BIO_NOCLOSE);\n\t\t\t}\n\t\t}\n#ifndef OPENSSL_NO_SRP\n\tif(!app_passwd(bio_err, srppass, NULL, &srp_arg.srppassin, NULL))\n\t\t{\n\t\tBIO_printf(bio_err, "Error getting password\\n");\n\t\tgoto end;\n\t\t}\n#endif\n\tctx=SSL_CTX_new(meth);\n\tif (ctx == NULL)\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (vpm)\n\t\tSSL_CTX_set1_param(ctx, vpm);\n\tif (!args_ssl_call(ctx, bio_err, cctx, ssl_args, 1, no_jpake))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile,\n\t\t\t\t\t\tcrls, crl_download))\n\t\t{\n\t\tBIO_printf(bio_err, "Error loading store locations\\n");\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n#ifndef OPENSSL_NO_ENGINE\n\tif (ssl_client_engine)\n\t\t{\n\t\tif (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine))\n\t\t\t{\n\t\t\tBIO_puts(bio_err, "Error setting client auth engine\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tENGINE_free(ssl_client_engine);\n\t\t\tgoto end;\n\t\t\t}\n\t\tENGINE_free(ssl_client_engine);\n\t\t}\n#endif\n#ifndef OPENSSL_NO_PSK\n#ifdef OPENSSL_NO_JPAKE\n\tif (psk_key != NULL)\n#else\n\tif (psk_key != NULL || jpake_secret)\n#endif\n\t\t{\n\t\tif (c_debug)\n\t\t\tBIO_printf(bio_c_out, "PSK key given or JPAKE in use, setting client callback\\n");\n\t\tSSL_CTX_set_psk_client_callback(ctx, psk_client_cb);\n\t\t}\n\tif (srtp_profiles != NULL)\n\t\tSSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);\n#endif\n\tif (exc) ssl_ctx_set_excert(ctx, exc);\n\tif (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);\n#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)\n\tif (next_proto.data)\n\t\tSSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto);\n#endif\n\tif (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);\n#if 0\n\telse\n\t\tSSL_CTX_set_cipher_list(ctx,getenv("SSL_CIPHER"));\n#endif\n\tSSL_CTX_set_verify(ctx,verify,verify_callback);\n\tif ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||\n\t\t(!SSL_CTX_set_default_verify_paths(ctx)))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\t}\n\tssl_ctx_add_crls(ctx, crls, crl_download);\n\tif (!set_cert_key_stuff(ctx,cert,key,chain,build_chain))\n\t\tgoto end;\n#ifndef OPENSSL_NO_TLSEXT\n\tif (servername != NULL)\n\t\t{\n\t\ttlsextcbp.biodebug = bio_err;\n\t\tSSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);\n\t\tSSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);\n\t\t}\n#ifndef OPENSSL_NO_SRP\n if (srp_arg.srplogin)\n\t\t{\n\t\tif (!srp_lateuser && !SSL_CTX_set_srp_username(ctx, srp_arg.srplogin))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"Unable to set SRP username\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tsrp_arg.msg = c_msg;\n\t\tsrp_arg.debug = c_debug ;\n\t\tSSL_CTX_set_srp_cb_arg(ctx,&srp_arg);\n\t\tSSL_CTX_set_srp_client_pwd_callback(ctx, ssl_give_srp_client_pwd_cb);\n\t\tSSL_CTX_set_srp_strength(ctx, srp_arg.strength);\n\t\tif (c_msg || c_debug || srp_arg.amp == 0)\n\t\t\tSSL_CTX_set_srp_verify_param_callback(ctx, ssl_srp_verify_param_cb);\n\t\t}\n#endif\n\tif (c_proof_debug)\n\t\tSSL_CTX_set_tlsext_authz_server_audit_proof_cb(ctx,\n\t\t\t\t\t\t\t audit_proof_cb);\n#endif\n\tcon=SSL_new(ctx);\n\tif (sess_in)\n\t\t{\n\t\tSSL_SESSION *sess;\n\t\tBIO *stmp = BIO_new_file(sess_in, "r");\n\t\tif (!stmp)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "Can\'t open session file %s\\n",\n\t\t\t\t\t\tsess_in);\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\tsess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);\n\t\tBIO_free(stmp);\n\t\tif (!sess)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "Can\'t open session file %s\\n",\n\t\t\t\t\t\tsess_in);\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\tSSL_set_session(con, sess);\n\t\tSSL_SESSION_free(sess);\n\t\t}\n#ifndef OPENSSL_NO_TLSEXT\n\tif (servername != NULL)\n\t\t{\n\t\tif (!SSL_set_tlsext_host_name(con,servername))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"Unable to set TLS servername extension.\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n#endif\n#ifndef OPENSSL_NO_KRB5\n\tif (con && (kctx = kssl_ctx_new()) != NULL)\n {\n\t\tSSL_set0_kssl_ctx(con, kctx);\n kssl_ctx_setstring(kctx, KSSL_SERVER, host);\n\t\t}\n#endif\n#if 0\n#ifdef TLSEXT_TYPE_opaque_prf_input\n\tSSL_set_tlsext_opaque_prf_input(con, "Test client", 11);\n#endif\n#endif\nre_start:\n\tif (init_client(&s,host,port,socket_type) == 0)\n\t\t{\n\t\tBIO_printf(bio_err,"connect:errno=%d\\n",get_last_socket_error());\n\t\tSHUTDOWN(s);\n\t\tgoto end;\n\t\t}\n\tBIO_printf(bio_c_out,"CONNECTED(%08X)\\n",s);\n#ifdef FIONBIO\n\tif (c_nbio)\n\t\t{\n\t\tunsigned long l=1;\n\t\tBIO_printf(bio_c_out,"turning on non blocking io\\n");\n\t\tif (BIO_socket_ioctl(s,FIONBIO,&l) < 0)\n\t\t\t{\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n#endif\n\tif (c_Pause & 0x01) SSL_set_debug(con, 1);\n\tif (socket_type == SOCK_DGRAM)\n\t\t{\n\t\tsbio=BIO_new_dgram(s,BIO_NOCLOSE);\n\t\tif (getsockname(s, &peer, (void *)&peerlen) < 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "getsockname:errno=%d\\n",\n\t\t\t\tget_last_socket_error());\n\t\t\tSHUTDOWN(s);\n\t\t\tgoto end;\n\t\t\t}\n\t\t(void)BIO_ctrl_set_connected(sbio, 1, &peer);\n\t\tif (enable_timeouts)\n\t\t\t{\n\t\t\ttimeout.tv_sec = 0;\n\t\t\ttimeout.tv_usec = DGRAM_RCV_TIMEOUT;\n\t\t\tBIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);\n\t\t\ttimeout.tv_sec = 0;\n\t\t\ttimeout.tv_usec = DGRAM_SND_TIMEOUT;\n\t\t\tBIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);\n\t\t\t}\n\t\tif (socket_mtu > 28)\n\t\t\t{\n\t\t\tSSL_set_options(con, SSL_OP_NO_QUERY_MTU);\n\t\t\tSSL_set_mtu(con, socket_mtu - 28);\n\t\t\t}\n\t\telse\n\t\t\tBIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);\n\t\t}\n\telse\n\t\tsbio=BIO_new_socket(s,BIO_NOCLOSE);\n\tif (nbio_test)\n\t\t{\n\t\tBIO *test;\n\t\ttest=BIO_new(BIO_f_nbio_test());\n\t\tsbio=BIO_push(test,sbio);\n\t\t}\n\tif (c_debug)\n\t\t{\n\t\tSSL_set_debug(con, 1);\n\t\tBIO_set_callback(sbio,bio_dump_callback);\n\t\tBIO_set_callback_arg(sbio,(char *)bio_c_out);\n\t\t}\n\tif (c_msg)\n\t\t{\n#ifndef OPENSSL_NO_SSL_TRACE\n\t\tif (c_msg == 2)\n\t\t\tSSL_set_msg_callback(con, SSL_trace);\n\t\telse\n#endif\n\t\t\tSSL_set_msg_callback(con, msg_cb);\n\t\tSSL_set_msg_callback_arg(con, bio_c_msg ? bio_c_msg : bio_c_out);\n\t\t}\n#ifndef OPENSSL_NO_TLSEXT\n\tif (c_tlsextdebug)\n\t\t{\n\t\tSSL_set_tlsext_debug_callback(con, tlsext_cb);\n\t\tSSL_set_tlsext_debug_arg(con, bio_c_out);\n\t\t}\n\tif (c_status_req)\n\t\t{\n\t\tSSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);\n\t\tSSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);\n\t\tSSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);\n#if 0\n{\nSTACK_OF(OCSP_RESPID) *ids = sk_OCSP_RESPID_new_null();\nOCSP_RESPID *id = OCSP_RESPID_new();\nid->value.byKey = ASN1_OCTET_STRING_new();\nid->type = V_OCSP_RESPID_KEY;\nASN1_STRING_set(id->value.byKey, "Hello World", -1);\nsk_OCSP_RESPID_push(ids, id);\nSSL_set_tlsext_status_ids(con, ids);\n}\n#endif\n\t\t}\n#endif\n#ifndef OPENSSL_NO_JPAKE\n\tif (jpake_secret)\n\t\tjpake_client_auth(bio_c_out, sbio, jpake_secret);\n#endif\n\tSSL_set_bio(con,sbio,sbio);\n\tSSL_set_connect_state(con);\n\twidth=SSL_get_fd(con)+1;\n\tread_tty=1;\n\twrite_tty=0;\n\ttty_on=0;\n\tread_ssl=1;\n\twrite_ssl=1;\n\tcbuf_len=0;\n\tcbuf_off=0;\n\tsbuf_len=0;\n\tsbuf_off=0;\n\tif (starttls_proto == PROTO_SMTP)\n\t\t{\n\t\tint foundit=0;\n\t\tBIO *fbio = BIO_new(BIO_f_buffer());\n\t\tBIO_push(fbio, sbio);\n\t\tdo\n\t\t\t{\n\t\t\tmbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);\n\t\t\t}\n\t\twhile (mbuf_len>3 && mbuf[3]==\'-\');\n\t\tBIO_printf(fbio,"EHLO openssl.client.net\\r\\n");\n\t\t(void)BIO_flush(fbio);\n\t\tdo\n\t\t\t{\n\t\t\tmbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);\n\t\t\tif (strstr(mbuf,"STARTTLS"))\n\t\t\t\tfoundit=1;\n\t\t\t}\n\t\twhile (mbuf_len>3 && mbuf[3]==\'-\');\n\t\t(void)BIO_flush(fbio);\n\t\tBIO_pop(fbio);\n\t\tBIO_free(fbio);\n\t\tif (!foundit)\n\t\t\tBIO_printf(bio_err,\n\t\t\t\t "didn\'t found starttls in server response,"\n\t\t\t\t " try anyway...\\n");\n\t\tBIO_printf(sbio,"STARTTLS\\r\\n");\n\t\tBIO_read(sbio,sbuf,BUFSIZZ);\n\t\t}\n\telse if (starttls_proto == PROTO_POP3)\n\t\t{\n\t\tBIO_read(sbio,mbuf,BUFSIZZ);\n\t\tBIO_printf(sbio,"STLS\\r\\n");\n\t\tBIO_read(sbio,sbuf,BUFSIZZ);\n\t\t}\n\telse if (starttls_proto == PROTO_IMAP)\n\t\t{\n\t\tint foundit=0;\n\t\tBIO *fbio = BIO_new(BIO_f_buffer());\n\t\tBIO_push(fbio, sbio);\n\t\tBIO_gets(fbio,mbuf,BUFSIZZ);\n\t\tBIO_printf(fbio,". CAPABILITY\\r\\n");\n\t\t(void)BIO_flush(fbio);\n\t\tdo\n\t\t\t{\n\t\t\tmbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);\n\t\t\tif (strstr(mbuf,"STARTTLS"))\n\t\t\t\tfoundit=1;\n\t\t\t}\n\t\twhile (mbuf_len>3 && mbuf[0]!=\'.\');\n\t\t(void)BIO_flush(fbio);\n\t\tBIO_pop(fbio);\n\t\tBIO_free(fbio);\n\t\tif (!foundit)\n\t\t\tBIO_printf(bio_err,\n\t\t\t\t "didn\'t found STARTTLS in server response,"\n\t\t\t\t " try anyway...\\n");\n\t\tBIO_printf(sbio,". STARTTLS\\r\\n");\n\t\tBIO_read(sbio,sbuf,BUFSIZZ);\n\t\t}\n\telse if (starttls_proto == PROTO_FTP)\n\t\t{\n\t\tBIO *fbio = BIO_new(BIO_f_buffer());\n\t\tBIO_push(fbio, sbio);\n\t\tdo\n\t\t\t{\n\t\t\tmbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);\n\t\t\t}\n\t\twhile (mbuf_len>3 && mbuf[3]==\'-\');\n\t\t(void)BIO_flush(fbio);\n\t\tBIO_pop(fbio);\n\t\tBIO_free(fbio);\n\t\tBIO_printf(sbio,"AUTH TLS\\r\\n");\n\t\tBIO_read(sbio,sbuf,BUFSIZZ);\n\t\t}\n\tif (starttls_proto == PROTO_XMPP)\n\t\t{\n\t\tint seen = 0;\n\t\tBIO_printf(sbio,"<stream:stream "\n\t\t "xmlns:stream=\'http://etherx.jabber.org/streams\' "\n\t\t "xmlns=\'jabber:client\' to=\'%s\' version=\'1.0\'>", host);\n\t\tseen = BIO_read(sbio,mbuf,BUFSIZZ);\n\t\tmbuf[seen] = 0;\n\t\twhile (!strstr(mbuf, "<starttls xmlns=\'urn:ietf:params:xml:ns:xmpp-tls\'"))\n\t\t\t{\n\t\t\tif (strstr(mbuf, "/stream:features>"))\n\t\t\t\tgoto shut;\n\t\t\tseen = BIO_read(sbio,mbuf,BUFSIZZ);\n\t\t\tmbuf[seen] = 0;\n\t\t\t}\n\t\tBIO_printf(sbio, "<starttls xmlns=\'urn:ietf:params:xml:ns:xmpp-tls\'/>");\n\t\tseen = BIO_read(sbio,sbuf,BUFSIZZ);\n\t\tsbuf[seen] = 0;\n\t\tif (!strstr(sbuf, "<proceed"))\n\t\t\tgoto shut;\n\t\tmbuf[0] = 0;\n\t\t}\n\tfor (;;)\n\t\t{\n\t\tFD_ZERO(&readfds);\n\t\tFD_ZERO(&writefds);\n\t\tif ((SSL_version(con) == DTLS1_VERSION) &&\n\t\t\tDTLSv1_get_timeout(con, &timeout))\n\t\t\ttimeoutp = &timeout;\n\t\telse\n\t\t\ttimeoutp = NULL;\n\t\tif (SSL_in_init(con) && !SSL_total_renegotiations(con))\n\t\t\t{\n\t\t\tin_init=1;\n\t\t\ttty_on=0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\ttty_on=1;\n\t\t\tif (in_init)\n\t\t\t\t{\n\t\t\t\tin_init=0;\n#if 0\n#ifndef OPENSSL_NO_TLSEXT\n\t\t\t\tif (servername != NULL && !SSL_session_reused(con))\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_c_out,"Server did %sacknowledge servername extension.\\n",tlsextcbp.ack?"":"not ");\n\t\t\t\t\t}\n#endif\n#endif\n\t\t\t\tif (sess_out)\n\t\t\t\t\t{\n\t\t\t\t\tBIO *stmp = BIO_new_file(sess_out, "w");\n\t\t\t\t\tif (stmp)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tPEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));\n\t\t\t\t\t\tBIO_free(stmp);\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\tBIO_printf(bio_err, "Error writing session file %s\\n", sess_out);\n\t\t\t\t\t}\n\t\t\t\tif (c_brief)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_puts(bio_err,\n\t\t\t\t\t\t"CONNECTION ESTABLISHED\\n");\n\t\t\t\t\tprint_ssl_summary(bio_err, con);\n\t\t\t\t\t}\n\t\t\t\tprint_stuff(bio_c_out,con,full_log);\n\t\t\t\tif (full_log > 0) full_log--;\n\t\t\t\tif (starttls_proto)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"%s",mbuf);\n\t\t\t\t\tstarttls_proto = PROTO_OFF;\n\t\t\t\t\t}\n\t\t\t\tif (reconnect)\n\t\t\t\t\t{\n\t\t\t\t\treconnect--;\n\t\t\t\t\tBIO_printf(bio_c_out,"drop connection and then reconnect\\n");\n\t\t\t\t\tSSL_shutdown(con);\n\t\t\t\t\tSSL_set_connect_state(con);\n\t\t\t\t\tSHUTDOWN(SSL_get_fd(con));\n\t\t\t\t\tgoto re_start;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\tssl_pending = read_ssl && SSL_pending(con);\n\t\tif (!ssl_pending)\n\t\t\t{\n#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined (OPENSSL_SYS_BEOS_R5)\n\t\t\tif (tty_on)\n\t\t\t\t{\n\t\t\t\tif (read_tty) openssl_fdset(fileno(stdin),&readfds);\n\t\t\t\tif (write_tty) openssl_fdset(fileno(stdout),&writefds);\n\t\t\t\t}\n\t\t\tif (read_ssl)\n\t\t\t\topenssl_fdset(SSL_get_fd(con),&readfds);\n\t\t\tif (write_ssl)\n\t\t\t\topenssl_fdset(SSL_get_fd(con),&writefds);\n#else\n\t\t\tif(!tty_on || !write_tty) {\n\t\t\t\tif (read_ssl)\n\t\t\t\t\topenssl_fdset(SSL_get_fd(con),&readfds);\n\t\t\t\tif (write_ssl)\n\t\t\t\t\topenssl_fdset(SSL_get_fd(con),&writefds);\n\t\t\t}\n#endif\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)\n\t\t\ti=0;\n\t\t\tif(!write_tty) {\n\t\t\t\tif(read_tty) {\n\t\t\t\t\ttv.tv_sec = 1;\n\t\t\t\t\ttv.tv_usec = 0;\n\t\t\t\t\ti=select(width,(void *)&readfds,(void *)&writefds,\n\t\t\t\t\t\t NULL,&tv);\n#if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)\n\t\t\t\t\tif(!i && (!_kbhit() || !read_tty) ) continue;\n#else\n\t\t\t\t\tif(!i && (!((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) || !read_tty) ) continue;\n#endif\n\t\t\t\t} else \ti=select(width,(void *)&readfds,(void *)&writefds,\n\t\t\t\t\t NULL,timeoutp);\n\t\t\t}\n#elif defined(OPENSSL_SYS_NETWARE)\n\t\t\tif(!write_tty) {\n\t\t\t\tif(read_tty) {\n\t\t\t\t\ttv.tv_sec = 1;\n\t\t\t\t\ttv.tv_usec = 0;\n\t\t\t\t\ti=select(width,(void *)&readfds,(void *)&writefds,\n\t\t\t\t\t\tNULL,&tv);\n\t\t\t\t} else \ti=select(width,(void *)&readfds,(void *)&writefds,\n\t\t\t\t\tNULL,timeoutp);\n\t\t\t}\n#elif defined(OPENSSL_SYS_BEOS_R5)\n\t\t\ti=0;\n\t\t\tstdin_set = 0;\n\t\t\t(void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);\n\t\t\tif(!write_tty) {\n\t\t\t\tif(read_tty) {\n\t\t\t\t\ttv.tv_sec = 1;\n\t\t\t\t\ttv.tv_usec = 0;\n\t\t\t\t\ti=select(width,(void *)&readfds,(void *)&writefds,\n\t\t\t\t\t\t NULL,&tv);\n\t\t\t\t\tif (read(fileno(stdin), sbuf, 0) >= 0)\n\t\t\t\t\t\tstdin_set = 1;\n\t\t\t\t\tif (!i && (stdin_set != 1 || !read_tty))\n\t\t\t\t\t\tcontinue;\n\t\t\t\t} else \ti=select(width,(void *)&readfds,(void *)&writefds,\n\t\t\t\t\t NULL,timeoutp);\n\t\t\t}\n\t\t\t(void)fcntl(fileno(stdin), F_SETFL, 0);\n#else\n\t\t\ti=select(width,(void *)&readfds,(void *)&writefds,\n\t\t\t\t NULL,timeoutp);\n#endif\n\t\t\tif ( i < 0)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"bad select %d\\n",\n\t\t\t\tget_last_socket_error());\n\t\t\t\tgoto shut;\n\t\t\t\t}\n\t\t\t}\n\t\tif ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"TIMEOUT occured\\n");\n\t\t\t}\n\t\tif (!ssl_pending && FD_ISSET(SSL_get_fd(con),&writefds))\n\t\t\t{\n\t\t\tk=SSL_write(con,&(cbuf[cbuf_off]),\n\t\t\t\t(unsigned int)cbuf_len);\n\t\t\tswitch (SSL_get_error(con,k))\n\t\t\t\t{\n\t\t\tcase SSL_ERROR_NONE:\n\t\t\t\tcbuf_off+=k;\n\t\t\t\tcbuf_len-=k;\n\t\t\t\tif (k <= 0) goto end;\n\t\t\t\tif (cbuf_len <= 0)\n\t\t\t\t\t{\n\t\t\t\t\tread_tty=1;\n\t\t\t\t\twrite_ssl=0;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tread_tty=0;\n\t\t\t\t\twrite_ssl=1;\n\t\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_WANT_WRITE:\n\t\t\t\tBIO_printf(bio_c_out,"write W BLOCK\\n");\n\t\t\t\twrite_ssl=1;\n\t\t\t\tread_tty=0;\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_WANT_READ:\n\t\t\t\tBIO_printf(bio_c_out,"write R BLOCK\\n");\n\t\t\t\twrite_tty=0;\n\t\t\t\tread_ssl=1;\n\t\t\t\twrite_ssl=0;\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_WANT_X509_LOOKUP:\n\t\t\t\tBIO_printf(bio_c_out,"write X BLOCK\\n");\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_ZERO_RETURN:\n\t\t\t\tif (cbuf_len != 0)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_c_out,"shutdown\\n");\n\t\t\t\t\tret = 0;\n\t\t\t\t\tgoto shut;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tread_tty=1;\n\t\t\t\t\twrite_ssl=0;\n\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\tcase SSL_ERROR_SYSCALL:\n\t\t\t\tif ((k != 0) || (cbuf_len != 0))\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"write:errno=%d\\n",\n\t\t\t\t\t\tget_last_socket_error());\n\t\t\t\t\tgoto shut;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tread_tty=1;\n\t\t\t\t\twrite_ssl=0;\n\t\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_SSL:\n\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\tgoto shut;\n\t\t\t\t}\n\t\t\t}\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)\n\t\telse if (!ssl_pending && write_tty)\n#else\n\t\telse if (!ssl_pending && FD_ISSET(fileno(stdout),&writefds))\n#endif\n\t\t\t{\n#ifdef CHARSET_EBCDIC\n\t\t\tascii2ebcdic(&(sbuf[sbuf_off]),&(sbuf[sbuf_off]),sbuf_len);\n#endif\n\t\t\ti=raw_write_stdout(&(sbuf[sbuf_off]),sbuf_len);\n\t\t\tif (i <= 0)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_c_out,"DONE\\n");\n\t\t\t\tret = 0;\n\t\t\t\tgoto shut;\n\t\t\t\t}\n\t\t\tsbuf_len-=i;;\n\t\t\tsbuf_off+=i;\n\t\t\tif (sbuf_len <= 0)\n\t\t\t\t{\n\t\t\t\tread_ssl=1;\n\t\t\t\twrite_tty=0;\n\t\t\t\t}\n\t\t\t}\n\t\telse if (ssl_pending || FD_ISSET(SSL_get_fd(con),&readfds))\n\t\t\t{\n#ifdef RENEG\n{ static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii=0; } }\n#endif\n#if 1\n\t\t\tk=SSL_read(con,sbuf,1024 );\n#else\n\t\t\tk=SSL_read(con,sbuf,16);\n{ char zbuf[10240];\nprintf("read=%d pending=%d peek=%d\\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240));\n}\n#endif\n\t\t\tswitch (SSL_get_error(con,k))\n\t\t\t\t{\n\t\t\tcase SSL_ERROR_NONE:\n\t\t\t\tif (k <= 0)\n\t\t\t\t\tgoto end;\n\t\t\t\tsbuf_off=0;\n\t\t\t\tsbuf_len=k;\n\t\t\t\tread_ssl=0;\n\t\t\t\twrite_tty=1;\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_WANT_WRITE:\n\t\t\t\tBIO_printf(bio_c_out,"read W BLOCK\\n");\n\t\t\t\twrite_ssl=1;\n\t\t\t\tread_tty=0;\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_WANT_READ:\n\t\t\t\tBIO_printf(bio_c_out,"read R BLOCK\\n");\n\t\t\t\twrite_tty=0;\n\t\t\t\tread_ssl=1;\n\t\t\t\tif ((read_tty == 0) && (write_ssl == 0))\n\t\t\t\t\twrite_ssl=1;\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_WANT_X509_LOOKUP:\n\t\t\t\tBIO_printf(bio_c_out,"read X BLOCK\\n");\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_SYSCALL:\n\t\t\t\tret=get_last_socket_error();\n\t\t\t\tif (c_brief)\n\t\t\t\t\tBIO_puts(bio_err, "CONNECTION CLOSED BY SERVER\\n");\n\t\t\t\telse\n\t\t\t\t\tBIO_printf(bio_err,"read:errno=%d\\n",ret);\n\t\t\t\tgoto shut;\n\t\t\tcase SSL_ERROR_ZERO_RETURN:\n\t\t\t\tBIO_printf(bio_c_out,"closed\\n");\n\t\t\t\tret=0;\n\t\t\t\tgoto shut;\n\t\t\tcase SSL_ERROR_SSL:\n\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\tgoto shut;\n\t\t\t\t}\n\t\t\t}\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)\n#if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)\n\t\telse if (_kbhit())\n#else\n\t\telse if ((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))\n#endif\n#elif defined (OPENSSL_SYS_NETWARE)\n\t\telse if (_kbhit())\n#elif defined(OPENSSL_SYS_BEOS_R5)\n\t\telse if (stdin_set)\n#else\n\t\telse if (FD_ISSET(fileno(stdin),&readfds))\n#endif\n\t\t\t{\n\t\t\tif (crlf)\n\t\t\t\t{\n\t\t\t\tint j, lf_num;\n\t\t\t\ti=raw_read_stdin(cbuf,BUFSIZZ/2);\n\t\t\t\tlf_num = 0;\n\t\t\t\tfor (j = 0; j < i; j++)\n\t\t\t\t\tif (cbuf[j] == \'\\n\')\n\t\t\t\t\t\tlf_num++;\n\t\t\t\tfor (j = i-1; j >= 0; j--)\n\t\t\t\t\t{\n\t\t\t\t\tcbuf[j+lf_num] = cbuf[j];\n\t\t\t\t\tif (cbuf[j] == \'\\n\')\n\t\t\t\t\t\t{\n\t\t\t\t\t\tlf_num--;\n\t\t\t\t\t\ti++;\n\t\t\t\t\t\tcbuf[j+lf_num] = \'\\r\';\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\tassert(lf_num == 0);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\ti=raw_read_stdin(cbuf,BUFSIZZ);\n\t\t\tif ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == \'Q\')))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"DONE\\n");\n\t\t\t\tret=0;\n\t\t\t\tgoto shut;\n\t\t\t\t}\n\t\t\tif ((!c_ign_eof) && (cbuf[0] == \'R\'))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"RENEGOTIATING\\n");\n\t\t\t\tSSL_renegotiate(con);\n\t\t\t\tcbuf_len=0;\n\t\t\t\t}\n#ifndef OPENSSL_NO_HEARTBEATS\n\t\t\telse if ((!c_ign_eof) && (cbuf[0] == \'B\'))\n \t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"HEARTBEATING\\n");\n\t\t\t\tSSL_heartbeat(con);\n\t\t\t\tcbuf_len=0;\n\t\t\t\t}\n#endif\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tcbuf_len=i;\n\t\t\t\tcbuf_off=0;\n#ifdef CHARSET_EBCDIC\n\t\t\t\tebcdic2ascii(cbuf, cbuf, i);\n#endif\n\t\t\t\t}\n\t\t\twrite_ssl=1;\n\t\t\tread_tty=0;\n\t\t\t}\n\t\t}\n\tret=0;\nshut:\n\tif (in_init)\n\t\tprint_stuff(bio_c_out,con,full_log);\n\tSSL_shutdown(con);\n\tSHUTDOWN(SSL_get_fd(con));\nend:\n\tif (con != NULL)\n\t\t{\n\t\tif (prexit != 0)\n\t\t\tprint_stuff(bio_c_out,con,1);\n\t\tSSL_free(con);\n\t\t}\n#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)\n\tif (next_proto.data)\n\t\tOPENSSL_free(next_proto.data);\n#endif\n\tif (ctx != NULL) SSL_CTX_free(ctx);\n\tif (cert)\n\t\tX509_free(cert);\n\tif (crls)\n\t\tsk_X509_CRL_pop_free(crls, X509_CRL_free);\n\tif (key)\n\t\tEVP_PKEY_free(key);\n\tif (chain)\n\t\tsk_X509_pop_free(chain, X509_free);\n\tif (pass)\n\t\tOPENSSL_free(pass);\n\tif (vpm)\n\t\tX509_VERIFY_PARAM_free(vpm);\n\tssl_excert_free(exc);\n\tif (ssl_args)\n\t\tsk_OPENSSL_STRING_free(ssl_args);\n\tif (cctx)\n\t\tSSL_CONF_CTX_free(cctx);\n#ifndef OPENSSL_NO_JPAKE\n\tif (jpake_secret && psk_key)\n\t\tOPENSSL_free(psk_key);\n#endif\n\tif (cbuf != NULL) { OPENSSL_cleanse(cbuf,BUFSIZZ); OPENSSL_free(cbuf); }\n\tif (sbuf != NULL) { OPENSSL_cleanse(sbuf,BUFSIZZ); OPENSSL_free(sbuf); }\n\tif (mbuf != NULL) { OPENSSL_cleanse(mbuf,BUFSIZZ); OPENSSL_free(mbuf); }\n\tif (bio_c_out != NULL)\n\t\t{\n\t\tBIO_free(bio_c_out);\n\t\tbio_c_out=NULL;\n\t\t}\n\tif (bio_c_msg != NULL)\n\t\t{\n\t\tBIO_free(bio_c_msg);\n\t\tbio_c_msg=NULL;\n\t\t}\n\tapps_shutdown();\n\tOPENSSL_EXIT(ret);\n\t}', 'int set_cert_key_stuff(SSL_CTX *ctx, X509 *cert, EVP_PKEY *key,\n\t\t STACK_OF(X509) *chain, int build_chain)\n\t{\n\tif (cert == NULL)\n\t\treturn 1;\n\tif (SSL_CTX_use_certificate(ctx,cert) <= 0)\n\t\t{\n\t\tBIO_printf(bio_err,"error setting certificate\\n");\n\t\tERR_print_errors(bio_err);\n\t\treturn 0;\n\t\t}\n \tif (SSL_CTX_use_PrivateKey(ctx,key) <= 0)\n \t\t{\n \t\tBIO_printf(bio_err,"error setting private key\\n");\n \t\tERR_print_errors(bio_err);\n \t\treturn 0;\n \t\t}\n\tif (!SSL_CTX_check_private_key(ctx))\n\t\t{\n\t\tBIO_printf(bio_err,"Private key does not match the certificate public key\\n");\n\t\treturn 0;\n\t\t}\n\tif (chain && !SSL_CTX_set1_chain(ctx, chain))\n\t\t{\n\t\tBIO_printf(bio_err,"error setting certificate chain\\n");\n\t\tERR_print_errors(bio_err);\n\t\treturn 0;\n\t\t}\n\tif (!chain && build_chain && !SSL_CTX_build_cert_chain(ctx, 0))\n\t\t{\n\t\tBIO_printf(bio_err,"error building certificate chain\\n");\n\t\tERR_print_errors(bio_err);\n\t\treturn 0;\n\t\t}\n\treturn 1;\n\t}', 'int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)\n\t{\n\tif (x == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);\n\t\treturn(0);\n\t\t}\n\tif (!ssl_cert_inst(&ctx->cert))\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);\n\t\treturn(0);\n\t\t}\n\treturn(ssl_set_cert(ctx->cert, x));\n\t}', 'static int ssl_set_cert(CERT *c, X509 *x)\n\t{\n\tEVP_PKEY *pkey;\n\tint i;\n\tpkey=X509_get_pubkey(x);\n\tif (pkey == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);\n\t\treturn(0);\n\t\t}\n\ti=ssl_cert_type(x,pkey);\n\tif (i < 0)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);\n\t\tEVP_PKEY_free(pkey);\n\t\treturn(0);\n\t\t}\n\tif (c->pkeys[i].privatekey != NULL)\n\t\t{\n\t\tEVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);\n\t\tERR_clear_error();\n#ifndef OPENSSL_NO_RSA\n\t\tif ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&\n\t\t\t(RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &\n\t\t\t RSA_METHOD_FLAG_NO_CHECK))\n\t\t\t ;\n\t\telse\n#endif\n\t\tif (!X509_check_private_key(x,c->pkeys[i].privatekey))\n\t\t\t{\n\t\t\tEVP_PKEY_free(c->pkeys[i].privatekey);\n\t\t\tc->pkeys[i].privatekey=NULL;\n\t\t\tERR_clear_error();\n\t\t\t}\n\t\t}\n\tEVP_PKEY_free(pkey);\n\tif (c->pkeys[i].x509 != NULL)\n\t\tX509_free(c->pkeys[i].x509);\n\tCRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);\n\tc->pkeys[i].x509=x;\n#ifndef OPENSSL_NO_TLSEXT\n\tif (c->pkeys[i].authz != NULL)\n\t\t{\n\t\tOPENSSL_free(c->pkeys[i].authz);\n\t\tc->pkeys[i].authz = NULL;\n\t\tc->pkeys[i].authz_length = 0;\n\t\t}\n#endif\n\tc->key= &(c->pkeys[i]);\n\tc->valid=0;\n\treturn(1);\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}', 'EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)\n\t{\n\tEVP_PKEY *ret=NULL;\n\tif (key == NULL) goto error;\n\tif (key->pkey != NULL)\n\t\t{\n\t\tCRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);\n\t\treturn key->pkey;\n\t\t}\n\tif (key->public_key == NULL) goto error;\n\tif ((ret = EVP_PKEY_new()) == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);\n\t\tgoto error;\n\t\t}\n\tif (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm)))\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET,X509_R_UNSUPPORTED_ALGORITHM);\n\t\tgoto error;\n\t\t}\n\tif (ret->ameth->pub_decode)\n\t\t{\n\t\tif (!ret->ameth->pub_decode(ret, key))\n\t\t\t{\n\t\t\tX509err(X509_F_X509_PUBKEY_GET,\n\t\t\t\t\t\tX509_R_PUBLIC_KEY_DECODE_ERROR);\n\t\t\tgoto error;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET, X509_R_METHOD_NOT_SUPPORTED);\n\t\tgoto error;\n\t\t}\n\tCRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY);\n\tif (key->pkey)\n\t\t{\n\t\tCRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);\n\t\tEVP_PKEY_free(ret);\n\t\tret = key->pkey;\n\t\t}\n\telse\n\t\t{\n\t\tkey->pkey = ret;\n\t\tCRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);\n\t\t}\n\tCRYPTO_add(&ret->references, 1, CRYPTO_LOCK_EVP_PKEY);\n\treturn ret;\n\terror:\n\tif (ret != NULL)\n\t\tEVP_PKEY_free(ret);\n\treturn(NULL);\n\t}', 'int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)\n\t{\n\treturn pkey_set_type(pkey, type, NULL, -1);\n\t}', 'static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)\n\t{\n\tconst EVP_PKEY_ASN1_METHOD *ameth;\n\tENGINE *e = NULL;\n\tif (pkey)\n\t\t{\n\t\tif (pkey->pkey.ptr)\n\t\t\tEVP_PKEY_free_it(pkey);\n\t\tif ((type == pkey->save_type) && pkey->ameth)\n\t\t\treturn 1;\n#ifndef OPENSSL_NO_ENGINE\n\t\tif (pkey->engine)\n\t\t\t{\n\t\t\tENGINE_finish(pkey->engine);\n\t\t\tpkey->engine = NULL;\n\t\t\t}\n#endif\n\t\t}\n\tif (str)\n\t\tameth = EVP_PKEY_asn1_find_str(&e, str, len);\n\telse\n\t\tameth = EVP_PKEY_asn1_find(&e, type);\n#ifndef OPENSSL_NO_ENGINE\n\tif (!pkey && e)\n\t\tENGINE_finish(e);\n#endif\n\tif (!ameth)\n\t\t{\n\t\tEVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);\n\t\treturn 0;\n\t\t}\n\tif (pkey)\n\t\t{\n\t\tpkey->ameth = ameth;\n\t\tpkey->engine = e;\n\t\tpkey->type = pkey->ameth->pkey_id;\n\t\tpkey->save_type=type;\n\t\t}\n\treturn 1;\n\t}', 'const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)\n\t{\n\tconst EVP_PKEY_ASN1_METHOD *t;\n\tfor (;;)\n\t\t{\n\t\tt = pkey_asn1_find(type);\n\t\tif (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))\n\t\t\tbreak;\n\t\ttype = t->pkey_base_id;\n\t\t}\n\tif (pe)\n\t\t{\n#ifndef OPENSSL_NO_ENGINE\n\t\tENGINE *e;\n\t\te = ENGINE_get_pkey_asn1_meth_engine(type);\n\t\tif (e)\n\t\t\t{\n\t\t\t*pe = e;\n\t\t\treturn ENGINE_get_pkey_asn1_meth(e, type);\n\t\t\t}\n#endif\n\t\t*pe = NULL;\n\t\t}\n\treturn t;\n\t}', 'ENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid)\n\t{\n\treturn engine_table_select(&pkey_asn1_meth_table, nid);\n\t}', 'ENGINE *engine_table_select(ENGINE_TABLE **table, int nid)\n#else\nENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f, int l)\n#endif\n\t{\n\tENGINE *ret = NULL;\n\tENGINE_PILE tmplate, *fnd=NULL;\n\tint initres, loop = 0;\n\tif(!(*table))\n\t\t{\n#ifdef ENGINE_TABLE_DEBUG\n\t\tfprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing "\n\t\t\t"registered!\\n", f, l, nid);\n#endif\n\t\treturn NULL;\n\t\t}\n\tERR_set_mark();\n\tCRYPTO_w_lock(CRYPTO_LOCK_ENGINE);\n\tif(!int_table_check(table, 0)) goto end;\n\ttmplate.nid = nid;\n\tfnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);\n\tif(!fnd) goto end;\n\tif(fnd->funct && engine_unlocked_init(fnd->funct))\n\t\t{\n#ifdef ENGINE_TABLE_DEBUG\n\t\tfprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "\n\t\t\t"ENGINE \'%s\' cached\\n", f, l, nid, fnd->funct->id);\n#endif\n\t\tret = fnd->funct;\n\t\tgoto end;\n\t\t}\n\tif(fnd->uptodate)\n\t\t{\n\t\tret = fnd->funct;\n\t\tgoto end;\n\t\t}\ntrynext:\n\tret = sk_ENGINE_value(fnd->sk, loop++);\n\tif(!ret)\n\t\t{\n#ifdef ENGINE_TABLE_DEBUG\n\t\tfprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no "\n\t\t\t\t"registered implementations would initialise\\n",\n\t\t\t\tf, l, nid);\n#endif\n\t\tgoto end;\n\t\t}\n\tif((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))\n\t\tinitres = engine_unlocked_init(ret);\n\telse\n\t\tinitres = 0;\n\tif(initres)\n\t\t{\n\t\tif((fnd->funct != ret) && engine_unlocked_init(ret))\n\t\t\t{\n\t\t\tif(fnd->funct)\n\t\t\t\tengine_unlocked_finish(fnd->funct, 0);\n\t\t\tfnd->funct = ret;\n#ifdef ENGINE_TABLE_DEBUG\n\t\t\tfprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, "\n\t\t\t\t"setting default to \'%s\'\\n", f, l, nid, ret->id);\n#endif\n\t\t\t}\n#ifdef ENGINE_TABLE_DEBUG\n\t\tfprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "\n\t\t\t\t"newly initialised \'%s\'\\n", f, l, nid, ret->id);\n#endif\n\t\tgoto end;\n\t\t}\n\tgoto trynext;\nend:\n\tif(fnd) fnd->uptodate = 1;\n#ifdef ENGINE_TABLE_DEBUG\n\tif(ret)\n\t\tfprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "\n\t\t\t\t"ENGINE \'%s\'\\n", f, l, nid, ret->id);\n\telse\n\t\tfprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "\n\t\t\t\t"\'no matching ENGINE\'\\n", f, l, nid);\n#endif\n\tCRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);\n\tERR_pop_to_mark();\n\treturn ret;\n\t}', 'static int int_table_check(ENGINE_TABLE **t, int create)\n\t{\n\tLHASH_OF(ENGINE_PILE) *lh;\n\tif(*t) return 1;\n\tif(!create) return 0;\n\tif((lh = lh_ENGINE_PILE_new()) == NULL)\n\t\treturn 0;\n\t*t = (ENGINE_TABLE *)lh;\n\treturn 1;\n\t}', '_LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)\n\t{\n\t_LHASH *ret;\n\tint i;\n\tif ((ret=OPENSSL_malloc(sizeof(_LHASH))) == NULL)\n\t\tgoto err0;\n\tif ((ret->b=OPENSSL_malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)\n\t\tgoto err1;\n\tfor (i=0; i<MIN_NODES; i++)\n\t\tret->b[i]=NULL;\n\tret->comp=((c == NULL)?(LHASH_COMP_FN_TYPE)strcmp:c);\n\tret->hash=((h == NULL)?(LHASH_HASH_FN_TYPE)lh_strhash:h);\n\tret->num_nodes=MIN_NODES/2;\n\tret->num_alloc_nodes=MIN_NODES;\n\tret->p=0;\n\tret->pmax=MIN_NODES/2;\n\tret->up_load=UP_LOAD;\n\tret->down_load=DOWN_LOAD;\n\tret->num_items=0;\n\tret->num_expands=0;\n\tret->num_expand_reallocs=0;\n\tret->num_contracts=0;\n\tret->num_contract_reallocs=0;\n\tret->num_hash_calls=0;\n\tret->num_comp_calls=0;\n\tret->num_insert=0;\n\tret->num_replace=0;\n\tret->num_delete=0;\n\tret->num_no_delete=0;\n\tret->num_retrieve=0;\n\tret->num_retrieve_miss=0;\n\tret->num_hash_comps=0;\n\tret->error=0;\n\treturn(ret);\nerr1:\n\tOPENSSL_free(ret);\nerr0:\n\treturn(NULL);\n\t}', 'SSL *SSL_new(SSL_CTX *ctx)\n\t{\n\tSSL *s;\n\tif (ctx == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);\n\t\treturn(NULL);\n\t\t}\n\tif (ctx->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n\t\treturn(NULL);\n\t\t}\n\ts=(SSL *)OPENSSL_malloc(sizeof(SSL));\n\tif (s == NULL) goto err;\n\tmemset(s,0,sizeof(SSL));\n#ifndef\tOPENSSL_NO_KRB5\n\ts->kssl_ctx = kssl_ctx_new();\n#endif\n\ts->options=ctx->options;\n\ts->mode=ctx->mode;\n\ts->max_cert_list=ctx->max_cert_list;\n\tif (ctx->cert != NULL)\n\t\t{\n\t\ts->cert = ssl_cert_dup(ctx->cert);\n\t\tif (s->cert == NULL)\n\t\t\tgoto err;\n\t\t}\n\telse\n\t\ts->cert=NULL;\n\ts->read_ahead=ctx->read_ahead;\n\ts->msg_callback=ctx->msg_callback;\n\ts->msg_callback_arg=ctx->msg_callback_arg;\n\ts->verify_mode=ctx->verify_mode;\n\ts->not_resumable_session_cb=ctx->not_resumable_session_cb;\n#if 0\n\ts->verify_depth=ctx->verify_depth;\n#endif\n\ts->sid_ctx_length=ctx->sid_ctx_length;\n\tOPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);\n\tmemcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));\n\ts->verify_callback=ctx->default_verify_callback;\n\ts->generate_session_id=ctx->generate_session_id;\n\ts->param = X509_VERIFY_PARAM_new();\n\tif (!s->param)\n\t\tgoto err;\n\tX509_VERIFY_PARAM_inherit(s->param, ctx->param);\n#if 0\n\ts->purpose = ctx->purpose;\n\ts->trust = ctx->trust;\n#endif\n\ts->quiet_shutdown=ctx->quiet_shutdown;\n\ts->max_send_fragment = ctx->max_send_fragment;\n\tCRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);\n\ts->ctx=ctx;\n#ifndef OPENSSL_NO_TLSEXT\n\ts->tlsext_debug_cb = 0;\n\ts->tlsext_debug_arg = NULL;\n\ts->tlsext_ticket_expected = 0;\n\ts->tlsext_status_type = -1;\n\ts->tlsext_status_expected = 0;\n\ts->tlsext_ocsp_ids = NULL;\n\ts->tlsext_ocsp_exts = NULL;\n\ts->tlsext_ocsp_resp = NULL;\n\ts->tlsext_ocsp_resplen = -1;\n\tCRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);\n\ts->initial_ctx=ctx;\n#ifndef OPENSSL_NO_EC\n\tif (ctx->tlsext_ecpointformatlist)\n\t\t{\n\t\ts->tlsext_ecpointformatlist =\n\t\t\tBUF_memdup(ctx->tlsext_ecpointformatlist,\n\t\t\t\t\tctx->tlsext_ecpointformatlist_length);\n\t\tif (!s->tlsext_ecpointformatlist)\n\t\t\tgoto err;\n\t\ts->tlsext_ecpointformatlist_length =\n\t\t\t\t\tctx->tlsext_ecpointformatlist_length;\n\t\t}\n\tif (ctx->tlsext_ellipticcurvelist)\n\t\t{\n\t\ts->tlsext_ellipticcurvelist =\n\t\t\tBUF_memdup(ctx->tlsext_ellipticcurvelist,\n\t\t\t\t\tctx->tlsext_ellipticcurvelist_length);\n\t\tif (!s->tlsext_ellipticcurvelist)\n\t\t\tgoto err;\n\t\ts->tlsext_ellipticcurvelist_length =\n\t\t\t\t\tctx->tlsext_ellipticcurvelist_length;\n\t\t}\n#endif\n# ifndef OPENSSL_NO_NEXTPROTONEG\n\ts->next_proto_negotiated = NULL;\n# endif\n#endif\n\ts->verify_result=X509_V_OK;\n\ts->method=ctx->method;\n\tif (!s->method->ssl_new(s))\n\t\tgoto err;\n\ts->references=1;\n\ts->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;\n\tSSL_clear(s);\n\tCRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n#ifndef OPENSSL_NO_PSK\n\ts->psk_client_callback=ctx->psk_client_callback;\n\ts->psk_server_callback=ctx->psk_server_callback;\n#endif\n\treturn(s);\nerr:\n\tif (s != NULL)\n\t\t{\n\t\tif (s->cert != NULL)\n\t\t\tssl_cert_free(s->cert);\n\t\tif (s->ctx != NULL)\n\t\t\tSSL_CTX_free(s->ctx);\n\t\tOPENSSL_free(s);\n\t\t}\n\tSSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);\n\treturn(NULL);\n\t}', 'int SSL_clear(SSL *s)\n\t{\n\tif (s->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);\n\t\treturn(0);\n\t\t}\n\tif (ssl_clear_bad_session(s))\n\t\t{\n\t\tSSL_SESSION_free(s->session);\n\t\ts->session=NULL;\n\t\t}\n\ts->error=0;\n\ts->hit=0;\n\ts->shutdown=0;\n#if 0\n\tif (s->renegotiate) return(1);\n#else\n\tif (s->renegotiate)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR);\n\t\treturn 0;\n\t\t}\n#endif\n\ts->type=0;\n\ts->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);\n\ts->version=s->method->version;\n\ts->client_version=s->version;\n\ts->rwstate=SSL_NOTHING;\n\ts->rstate=SSL_ST_READ_HEADER;\n#if 0\n\ts->read_ahead=s->ctx->read_ahead;\n#endif\n\tif (s->init_buf != NULL)\n\t\t{\n\t\tBUF_MEM_free(s->init_buf);\n\t\ts->init_buf=NULL;\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tssl_clear_hash_ctx(&s->read_hash);\n\tssl_clear_hash_ctx(&s->write_hash);\n\ts->first_packet=0;\n#if 1\n\tif (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))\n\t\t{\n\t\ts->method->ssl_free(s);\n\t\ts->method=s->ctx->method;\n\t\tif (!s->method->ssl_new(s))\n\t\t\treturn(0);\n\t\t}\n\telse\n#endif\n\t\ts->method->ssl_clear(s);\n\treturn(1);\n\t}', 'int ssl_clear_bad_session(SSL *s)\n\t{\n\tif (\t(s->session != NULL) &&\n\t\t!(s->shutdown & SSL_SENT_SHUTDOWN) &&\n\t\t!(SSL_in_init(s) || SSL_in_before(s)))\n\t\t{\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\treturn(1);\n\t\t}\n\telse\n\t\treturn(0);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tif ((r = lh_SSL_SESSION_retrieve(ctx->sessions,c)) == c)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tr=lh_SSL_SESSION_delete(ctx->sessions,c);\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'void *lh_delete(_LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tvoid *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tOPENSSL_free(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}']
35,850
0
https://github.com/libav/libav/blob/0ca0924c10d9617a5793964bf79655424ef32b68/libavcodec/vp3.c/#L1992
static int vp3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt) { const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; Vp3DecodeContext *s = avctx->priv_data; GetBitContext gb; int i, ret; init_get_bits(&gb, buf, buf_size * 8); if (s->theora && get_bits1(&gb)) { av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n"); return -1; } s->keyframe = !get_bits1(&gb); if (!s->theora) skip_bits(&gb, 1); for (i = 0; i < 3; i++) s->last_qps[i] = s->qps[i]; s->nqps = 0; do { s->qps[s->nqps++] = get_bits(&gb, 6); } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb)); for (i = s->nqps; i < 3; i++) s->qps[i] = -1; if (s->avctx->debug & FF_DEBUG_PICT_INFO) av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n", s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]); s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] || avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL : AVDISCARD_NONKEY); if (s->qps[0] != s->last_qps[0]) init_loop_filter(s); for (i = 0; i < s->nqps; i++) if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0]) init_dequantizer(s, i); if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe) return buf_size; s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P; if (ff_thread_get_buffer(avctx, &s->current_frame, AV_GET_BUFFER_FLAG_REF) < 0) { av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); goto error; } if (!s->edge_emu_buffer) s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0])); if (s->keyframe) { if (!s->theora) { skip_bits(&gb, 4); skip_bits(&gb, 4); if (s->version) { s->version = get_bits(&gb, 5); if (avctx->frame_number == 0) av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version); } } if (s->version || s->theora) { if (get_bits1(&gb)) av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n"); skip_bits(&gb, 2); } } else { if (!s->golden_frame.f->data[0]) { av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n"); s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I; if (ff_thread_get_buffer(avctx, &s->golden_frame, AV_GET_BUFFER_FLAG_REF) < 0) { av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); goto error; } ff_thread_release_buffer(avctx, &s->last_frame); if ((ret = ff_thread_ref_frame(&s->last_frame, &s->golden_frame)) < 0) goto error; ff_thread_report_progress(&s->last_frame, INT_MAX, 0); } } memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment)); ff_thread_finish_setup(avctx); if (unpack_superblocks(s, &gb)) { av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n"); goto error; } if (unpack_modes(s, &gb)) { av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n"); goto error; } if (unpack_vectors(s, &gb)) { av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n"); goto error; } if (unpack_block_qpis(s, &gb)) { av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n"); goto error; } if (unpack_dct_coeffs(s, &gb)) { av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n"); goto error; } for (i = 0; i < 3; i++) { int height = s->height >> (i && s->chroma_y_shift); if (s->flipped_image) s->data_offset[i] = 0; else s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i]; } s->last_slice_end = 0; for (i = 0; i < s->c_superblock_height; i++) render_slice(s, i); for (i = 0; i < 3; i++) { int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1; apply_loop_filter(s, i, row, row + 1); } vp3_draw_horiz_band(s, s->avctx->height); if ((ret = av_frame_ref(data, s->current_frame.f)) < 0) return ret; *got_frame = 1; if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) { ret = update_frames(avctx); if (ret < 0) return ret; } return buf_size; error: ff_thread_report_progress(&s->current_frame, INT_MAX, 0); if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) av_frame_unref(s->current_frame.f); return -1; }
['static int vp3_decode_frame(AVCodecContext *avctx,\n void *data, int *got_frame,\n AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n Vp3DecodeContext *s = avctx->priv_data;\n GetBitContext gb;\n int i, ret;\n init_get_bits(&gb, buf, buf_size * 8);\n if (s->theora && get_bits1(&gb)) {\n av_log(avctx, AV_LOG_ERROR,\n "Header packet passed to frame decoder, skipping\\n");\n return -1;\n }\n s->keyframe = !get_bits1(&gb);\n if (!s->theora)\n skip_bits(&gb, 1);\n for (i = 0; i < 3; i++)\n s->last_qps[i] = s->qps[i];\n s->nqps = 0;\n do {\n s->qps[s->nqps++] = get_bits(&gb, 6);\n } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));\n for (i = s->nqps; i < 3; i++)\n s->qps[i] = -1;\n if (s->avctx->debug & FF_DEBUG_PICT_INFO)\n av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\\n",\n s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]);\n s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||\n avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL\n : AVDISCARD_NONKEY);\n if (s->qps[0] != s->last_qps[0])\n init_loop_filter(s);\n for (i = 0; i < s->nqps; i++)\n if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])\n init_dequantizer(s, i);\n if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)\n return buf_size;\n s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I\n : AV_PICTURE_TYPE_P;\n if (ff_thread_get_buffer(avctx, &s->current_frame, AV_GET_BUFFER_FLAG_REF) < 0) {\n av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n goto error;\n }\n if (!s->edge_emu_buffer)\n s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0]));\n if (s->keyframe) {\n if (!s->theora) {\n skip_bits(&gb, 4);\n skip_bits(&gb, 4);\n if (s->version) {\n s->version = get_bits(&gb, 5);\n if (avctx->frame_number == 0)\n av_log(s->avctx, AV_LOG_DEBUG,\n "VP version: %d\\n", s->version);\n }\n }\n if (s->version || s->theora) {\n if (get_bits1(&gb))\n av_log(s->avctx, AV_LOG_ERROR,\n "Warning, unsupported keyframe coding type?!\\n");\n skip_bits(&gb, 2);\n }\n } else {\n if (!s->golden_frame.f->data[0]) {\n av_log(s->avctx, AV_LOG_WARNING,\n "vp3: first frame not a keyframe\\n");\n s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;\n if (ff_thread_get_buffer(avctx, &s->golden_frame,\n AV_GET_BUFFER_FLAG_REF) < 0) {\n av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n goto error;\n }\n ff_thread_release_buffer(avctx, &s->last_frame);\n if ((ret = ff_thread_ref_frame(&s->last_frame,\n &s->golden_frame)) < 0)\n goto error;\n ff_thread_report_progress(&s->last_frame, INT_MAX, 0);\n }\n }\n memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));\n ff_thread_finish_setup(avctx);\n if (unpack_superblocks(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\\n");\n goto error;\n }\n if (unpack_modes(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\\n");\n goto error;\n }\n if (unpack_vectors(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\\n");\n goto error;\n }\n if (unpack_block_qpis(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\\n");\n goto error;\n }\n if (unpack_dct_coeffs(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\\n");\n goto error;\n }\n for (i = 0; i < 3; i++) {\n int height = s->height >> (i && s->chroma_y_shift);\n if (s->flipped_image)\n s->data_offset[i] = 0;\n else\n s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];\n }\n s->last_slice_end = 0;\n for (i = 0; i < s->c_superblock_height; i++)\n render_slice(s, i);\n for (i = 0; i < 3; i++) {\n int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;\n apply_loop_filter(s, i, row, row + 1);\n }\n vp3_draw_horiz_band(s, s->avctx->height);\n if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)\n return ret;\n *got_frame = 1;\n if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) {\n ret = update_frames(avctx);\n if (ret < 0)\n return ret;\n }\n return buf_size;\nerror:\n ff_thread_report_progress(&s->current_frame, INT_MAX, 0);\n if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME))\n av_frame_unref(s->current_frame.f);\n return -1;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size;\n int ret = 0;\n if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n ret = AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n return ret;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n unsigned int index = s->index;\n uint8_t result = s->buffer[index >> 3];\n#ifdef BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}']
35,851
0
https://github.com/openssl/openssl/blob/0bde1089f895718db2fe2637fda4a0c2ed6df904/crypto/lhash/lhash.c/#L240
void *lh_delete(LHASH *lh, void *data) { unsigned long hash; LHASH_NODE *nn,**rn; void *ret; lh->error=0; rn=getrn(lh,data,&hash); if (*rn == NULL) { lh->num_no_delete++; return(NULL); } else { nn= *rn; *rn=nn->next; ret=nn->data; Free(nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))) contract(lh); return(ret); }
['static int www_body(char *hostname, int s, unsigned char *context)\n\t{\n\tchar *buf=NULL;\n\tint ret=1;\n\tint i,j,k,blank,dot;\n\tstruct stat st_buf;\n\tSSL *con;\n\tSSL_CIPHER *c;\n\tBIO *io,*ssl_bio,*sbio;\n\tlong total_bytes;\n\tbuf=Malloc(bufsize);\n\tif (buf == NULL) return(0);\n\tio=BIO_new(BIO_f_buffer());\n\tssl_bio=BIO_new(BIO_f_ssl());\n\tif ((io == NULL) || (ssl_bio == NULL)) goto err;\n#ifdef FIONBIO\n\tif (s_nbio)\n\t\t{\n\t\tunsigned long sl=1;\n\t\tif (!s_quiet)\n\t\t\tBIO_printf(bio_err,"turning on non blocking io\\n");\n\t\tif (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)\n\t\t\tERR_print_errors(bio_err);\n\t\t}\n#endif\n\tif (!BIO_set_write_buffer_size(io,bufsize)) goto err;\n\tif ((con=(SSL *)SSL_new(ctx)) == NULL) goto err;\n\tif(context) SSL_set_session_id_context(con, context,\n\t\t\t\t\t strlen((char *)context));\n\tsbio=BIO_new_socket(s,BIO_NOCLOSE);\n\tif (s_nbio_test)\n\t\t{\n\t\tBIO *test;\n\t\ttest=BIO_new(BIO_f_nbio_test());\n\t\tsbio=BIO_push(test,sbio);\n\t\t}\n\tSSL_set_bio(con,sbio,sbio);\n\tSSL_set_accept_state(con);\n\tBIO_set_ssl(ssl_bio,con,BIO_CLOSE);\n\tBIO_push(io,ssl_bio);\n#ifdef CHARSET_EBCDIC\n\tio = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io);\n#endif\n\tif (s_debug)\n\t\t{\n\t\tcon->debug=1;\n\t\tBIO_set_callback(SSL_get_rbio(con),bio_dump_cb);\n\t\tBIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);\n\t\t}\n\tblank=0;\n\tfor (;;)\n\t\t{\n\t\tif (hack)\n\t\t\t{\n\t\t\ti=SSL_accept(con);\n\t\t\tswitch (SSL_get_error(con,i))\n\t\t\t\t{\n\t\t\tcase SSL_ERROR_NONE:\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_WANT_WRITE:\n\t\t\tcase SSL_ERROR_WANT_READ:\n\t\t\tcase SSL_ERROR_WANT_X509_LOOKUP:\n\t\t\t\tcontinue;\n\t\t\tcase SSL_ERROR_SYSCALL:\n\t\t\tcase SSL_ERROR_SSL:\n\t\t\tcase SSL_ERROR_ZERO_RETURN:\n\t\t\t\tret=1;\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tSSL_renegotiate(con);\n\t\t\tSSL_write(con,NULL,0);\n\t\t\t}\n\t\ti=BIO_gets(io,buf,bufsize-1);\n\t\tif (i < 0)\n\t\t\t{\n\t\t\tif (!BIO_should_retry(io))\n\t\t\t\t{\n\t\t\t\tif (!s_quiet)\n\t\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_s_out,"read R BLOCK\\n");\n#ifndef MSDOS\n\t\t\t\tsleep(1);\n#endif\n\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\telse if (i == 0)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (\t((www == 1) && (strncmp("GET ",buf,4) == 0)) ||\n\t\t\t((www == 2) && (strncmp("GET /stats ",buf,10) == 0)))\n\t\t\t{\n\t\t\tchar *p;\n\t\t\tX509 *peer;\n\t\t\tSTACK_OF(SSL_CIPHER) *sk;\n\t\t\tstatic char *space=" ";\n\t\t\tBIO_puts(io,"HTTP/1.0 200 ok\\r\\nContent-type: text/html\\r\\n\\r\\n");\n\t\t\tBIO_puts(io,"<HTML><BODY BGCOLOR=\\"#ffffff\\">\\n");\n\t\t\tBIO_puts(io,"<pre>\\n");\n\t\t\tBIO_puts(io,"\\n");\n\t\t\tfor (i=0; i<local_argc; i++)\n\t\t\t\t{\n\t\t\t\tBIO_puts(io,local_argv[i]);\n\t\t\t\tBIO_write(io," ",1);\n\t\t\t\t}\n\t\t\tBIO_puts(io,"\\n");\n\t\t\tBIO_printf(io,"Ciphers supported in s_server binary\\n");\n\t\t\tsk=SSL_get_ciphers(con);\n\t\t\tj=sk_SSL_CIPHER_num(sk);\n\t\t\tfor (i=0; i<j; i++)\n\t\t\t\t{\n\t\t\t\tc=sk_SSL_CIPHER_value(sk,i);\n\t\t\t\tBIO_printf(io,"%-11s:%-25s",\n\t\t\t\t\tSSL_CIPHER_get_version(c),\n\t\t\t\t\tSSL_CIPHER_get_name(c));\n\t\t\t\tif ((((i+1)%2) == 0) && (i+1 != j))\n\t\t\t\t\tBIO_puts(io,"\\n");\n\t\t\t\t}\n\t\t\tBIO_puts(io,"\\n");\n\t\t\tp=SSL_get_shared_ciphers(con,buf,bufsize);\n\t\t\tif (p != NULL)\n\t\t\t\t{\n\t\t\t\tBIO_printf(io,"---\\nCiphers common between both SSL end points:\\n");\n\t\t\t\tj=i=0;\n\t\t\t\twhile (*p)\n\t\t\t\t\t{\n\t\t\t\t\tif (*p == \':\')\n\t\t\t\t\t\t{\n\t\t\t\t\t\tBIO_write(io,space,26-j);\n\t\t\t\t\t\ti++;\n\t\t\t\t\t\tj=0;\n\t\t\t\t\t\tBIO_write(io,((i%3)?" ":"\\n"),1);\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\tBIO_write(io,p,1);\n\t\t\t\t\t\tj++;\n\t\t\t\t\t\t}\n\t\t\t\t\tp++;\n\t\t\t\t\t}\n\t\t\t\tBIO_puts(io,"\\n");\n\t\t\t\t}\n\t\t\tBIO_printf(io,((con->hit)\n\t\t\t\t?"---\\nReused, "\n\t\t\t\t:"---\\nNew, "));\n\t\t\tc=SSL_get_current_cipher(con);\n\t\t\tBIO_printf(io,"%s, Cipher is %s\\n",\n\t\t\t\tSSL_CIPHER_get_version(c),\n\t\t\t\tSSL_CIPHER_get_name(c));\n\t\t\tSSL_SESSION_print(io,SSL_get_session(con));\n\t\t\tBIO_printf(io,"---\\n");\n\t\t\tprint_stats(io,SSL_get_SSL_CTX(con));\n\t\t\tBIO_printf(io,"---\\n");\n\t\t\tpeer=SSL_get_peer_certificate(con);\n\t\t\tif (peer != NULL)\n\t\t\t\t{\n\t\t\t\tBIO_printf(io,"Client certificate\\n");\n\t\t\t\tX509_print(io,peer);\n\t\t\t\tPEM_write_bio_X509(io,peer);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tBIO_puts(io,"no client certificate available\\n");\n\t\t\tBIO_puts(io,"</BODY></HTML>\\r\\n\\r\\n");\n\t\t\tbreak;\n\t\t\t}\n\t\telse if ((www == 2) && (strncmp("GET /",buf,5) == 0))\n\t\t\t{\n\t\t\tBIO *file;\n\t\t\tchar *p,*e;\n\t\t\tstatic char *text="HTTP/1.0 200 ok\\r\\nContent-type: text/plain\\r\\n\\r\\n";\n\t\t\tp= &(buf[5]);\n\t\t\tdot=0;\n\t\t\tfor (e=p; *e != \'\\0\'; e++)\n\t\t\t\t{\n\t\t\t\tif (e[0] == \' \') break;\n\t\t\t\tif (\t(e[0] == \'.\') &&\n\t\t\t\t\t(strncmp(&(e[-1]),"/../",4) == 0))\n\t\t\t\t\tdot=1;\n\t\t\t\t}\n\t\t\tif (*e == \'\\0\')\n\t\t\t\t{\n\t\t\t\tBIO_puts(io,text);\n\t\t\t\tBIO_printf(io,"\'%s\' is an invalid file name\\r\\n",p);\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t*e=\'\\0\';\n\t\t\tif (dot)\n\t\t\t\t{\n\t\t\t\tBIO_puts(io,text);\n\t\t\t\tBIO_printf(io,"\'%s\' contains \'..\' reference\\r\\n",p);\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\tif (*p == \'/\')\n\t\t\t\t{\n\t\t\t\tBIO_puts(io,text);\n\t\t\t\tBIO_printf(io,"\'%s\' is an invalid path\\r\\n",p);\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\tif (e[-1] == \'/\')\n\t\t\t\tstrcat(p,"index.html");\n\t\t\tif (stat(p,&st_buf) < 0)\n\t\t\t\t{\n\t\t\t\tBIO_puts(io,text);\n\t\t\t\tBIO_printf(io,"Error accessing \'%s\'\\r\\n",p);\n\t\t\t\tERR_print_errors(io);\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\tif (S_ISDIR(st_buf.st_mode))\n\t\t\t\t{\n\t\t\t\tstrcat(p,"/index.html");\n\t\t\t\t}\n\t\t\tif ((file=BIO_new_file(p,"r")) == NULL)\n\t\t\t\t{\n\t\t\t\tBIO_puts(io,text);\n\t\t\t\tBIO_printf(io,"Error opening \'%s\'\\r\\n",p);\n\t\t\t\tERR_print_errors(io);\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\tif (!s_quiet)\n\t\t\t\tBIO_printf(bio_err,"FILE:%s\\n",p);\n\t\t\ti=strlen(p);\n\t\t\tif (\t((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||\n\t\t\t\t((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||\n\t\t\t\t((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))\n\t\t\t\tBIO_puts(io,"HTTP/1.0 200 ok\\r\\nContent-type: text/html\\r\\n\\r\\n");\n\t\t\telse\n\t\t\t\tBIO_puts(io,"HTTP/1.0 200 ok\\r\\nContent-type: text/plain\\r\\n\\r\\n");\n\t\t\ttotal_bytes=0;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\ti=BIO_read(file,buf,bufsize);\n\t\t\t\tif (i <= 0) break;\n#ifdef RENEG\n\t\t\t\ttotal_bytes+=i;\n\t\t\t\tfprintf(stderr,"%d\\n",i);\n\t\t\t\tif (total_bytes > 3*1024)\n\t\t\t\t\t{\n\t\t\t\t\ttotal_bytes=0;\n\t\t\t\t\tfprintf(stderr,"RENEGOTIATE\\n");\n\t\t\t\t\tSSL_renegotiate(con);\n\t\t\t\t\t}\n#endif\n\t\t\t\tfor (j=0; j<i; )\n\t\t\t\t\t{\n#ifdef RENEG\n{ static count=0; if (++count == 13) { SSL_renegotiate(con); } }\n#endif\n\t\t\t\t\tk=BIO_write(io,&(buf[j]),i-j);\n\t\t\t\t\tif (k <= 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BIO_should_retry(io))\n\t\t\t\t\t\t\tgoto write_error;\n\t\t\t\t\t\telse\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\tBIO_printf(bio_s_out,"rwrite W BLOCK\\n");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\tj+=k;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\nwrite_error:\n\t\t\tBIO_free(file);\n\t\t\tbreak;\n\t\t\t}\n\t\t}\n\tfor (;;)\n\t\t{\n\t\ti=(int)BIO_flush(io);\n\t\tif (i <= 0)\n\t\t\t{\n\t\t\tif (!BIO_should_retry(io))\n\t\t\t\tbreak;\n\t\t\t}\n\t\telse\n\t\t\tbreak;\n\t\t}\nend:\n#if 1\n\tSSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);\n#else\n#endif\nerr:\n\tif (ret >= 0)\n\t\tBIO_printf(bio_s_out,"ACCEPT\\n");\n\tif (buf != NULL) Free(buf);\n\tif (io != NULL) BIO_free_all(io);\n\treturn(ret);\n\t}', 'SSL *SSL_new(SSL_CTX *ctx)\n\t{\n\tSSL *s;\n\tif (ctx == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);\n\t\treturn(NULL);\n\t\t}\n\tif (ctx->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n\t\treturn(NULL);\n\t\t}\n\ts=(SSL *)Malloc(sizeof(SSL));\n\tif (s == NULL) goto err;\n\tmemset(s,0,sizeof(SSL));\n\tif (ctx->cert != NULL)\n\t\t{\n\t\ts->cert = ssl_cert_dup(ctx->cert);\n\t\tif (s->cert == NULL)\n\t\t\tgoto err;\n\t\t}\n\telse\n\t\ts->cert=NULL;\n\ts->sid_ctx_length=ctx->sid_ctx_length;\n\tmemcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));\n\ts->verify_mode=ctx->verify_mode;\n\ts->verify_depth=ctx->verify_depth;\n\ts->verify_callback=ctx->default_verify_callback;\n\ts->purpose = ctx->purpose;\n\ts->trust = ctx->trust;\n\tCRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);\n\ts->ctx=ctx;\n\ts->verify_result=X509_V_OK;\n\ts->method=ctx->method;\n\tif (!s->method->ssl_new(s))\n\t\tgoto err;\n\ts->quiet_shutdown=ctx->quiet_shutdown;\n\ts->references=1;\n\ts->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;\n\ts->options=ctx->options;\n\ts->mode=ctx->mode;\n\tSSL_clear(s);\n\tCRYPTO_new_ex_data(ssl_meth,s,&s->ex_data);\n\treturn(s);\nerr:\n\tif (s != NULL)\n\t\t{\n\t\tif (s->cert != NULL)\n\t\t\tssl_cert_free(s->cert);\n\t\tif (s->ctx != NULL)\n\t\t\tSSL_CTX_free(s->ctx);\n\t\tFree(s);\n\t\t}\n\tSSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);\n\treturn(NULL);\n\t}', 'int SSL_clear(SSL *s)\n\t{\n\tint state;\n\tif (s->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);\n\t\treturn(0);\n\t\t}\n\ts->error=0;\n\ts->hit=0;\n\ts->shutdown=0;\n#if 0\n\tif (s->new_session) return(1);\n#endif\n\tstate=s->state;\n\ts->type=0;\n\ts->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);\n\ts->version=s->method->version;\n\ts->client_version=s->version;\n\ts->rwstate=SSL_NOTHING;\n\ts->rstate=SSL_ST_READ_HEADER;\n\ts->read_ahead=s->ctx->read_ahead;\n\tif (s->init_buf != NULL)\n\t\t{\n\t\tBUF_MEM_free(s->init_buf);\n\t\ts->init_buf=NULL;\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tif (ssl_clear_bad_session(s))\n\t\t{\n\t\tSSL_SESSION_free(s->session);\n\t\ts->session=NULL;\n\t\t}\n\ts->first_packet=0;\n#if 1\n\tif ((s->session == NULL) && (s->method != s->ctx->method))\n\t\t{\n\t\ts->method->ssl_free(s);\n\t\ts->method=s->ctx->method;\n\t\tif (!s->method->ssl_new(s))\n\t\t\treturn(0);\n\t\t}\n\telse\n#endif\n\t\ts->method->ssl_clear(s);\n\treturn(1);\n\t}', 'int ssl_clear_bad_session(SSL *s)\n\t{\n\tif (\t(s->session != NULL) &&\n\t\t!(s->shutdown & SSL_SENT_SHUTDOWN) &&\n\t\t!(SSL_in_init(s) || SSL_in_before(s)))\n\t\t{\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\treturn(1);\n\t\t}\n\telse\n\t\treturn(0);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'void *lh_delete(LHASH *lh, void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tvoid *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}']
35,852
0
https://github.com/openssl/openssl/blob/4f090f76a412a0f69f85621468bd445ea6a65af6/crypto/bn/bn_ctx.c/#L273
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,\n const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,\n int (*bn_mod_exp) (BIGNUM *r,\n const BIGNUM *a,\n const BIGNUM *p,\n const BIGNUM *m,\n BN_CTX *ctx,\n BN_MONT_CTX *m_ctx),\n BN_MONT_CTX *m_ctx)\n{\n int retry_counter = 32;\n BN_BLINDING *ret = NULL;\n if (b == NULL)\n ret = BN_BLINDING_new(NULL, NULL, m);\n else\n ret = b;\n if (ret == NULL)\n goto err;\n if (ret->A == NULL && (ret->A = BN_new()) == NULL)\n goto err;\n if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL)\n goto err;\n if (e != NULL) {\n BN_free(ret->e);\n ret->e = BN_dup(e);\n }\n if (ret->e == NULL)\n goto err;\n if (bn_mod_exp != NULL)\n ret->bn_mod_exp = bn_mod_exp;\n if (m_ctx != NULL)\n ret->m_ctx = m_ctx;\n do {\n int rv;\n if (!BN_priv_rand_range(ret->A, ret->mod))\n goto err;\n if (!int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv)) {\n if (rv) {\n if (retry_counter-- == 0) {\n BNerr(BN_F_BN_BLINDING_CREATE_PARAM,\n BN_R_TOO_MANY_ITERATIONS);\n goto err;\n }\n } else\n goto err;\n } else\n break;\n } while (1);\n if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL) {\n if (!ret->bn_mod_exp\n (ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx))\n goto err;\n } else {\n if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx))\n goto err;\n }\n return ret;\n err:\n if (b == NULL) {\n BN_BLINDING_free(ret);\n ret = NULL;\n }\n return ret;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n {\n BIGNUM local_B;\n bn_init(&local_B);\n BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, &local_B, A, ctx))\n goto err;\n }\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n {\n BIGNUM local_A;\n bn_init(&local_A);\n BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, &local_A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
35,853
0
https://github.com/openssl/openssl/blob/9f9442918aeaed5dc2442d81ab8d29fe3e1fb906/crypto/bn/bn_lib.c/#L333
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) { bn_check_top(b); if (a == b) return a; if (bn_wexpand(a, b->top) == NULL) return NULL; if (b->top > 0) memcpy(a->d, b->d, sizeof(b->d[0]) * b->top); if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0) BN_set_flags(a, BN_FLG_CONSTTIME); a->top = b->top; a->neg = b->neg; bn_check_top(a); return a; }
['static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)\n{\n RSA_PKEY_CTX *dctx, *sctx;\n if (!pkey_rsa_init(dst))\n return 0;\n sctx = src->data;\n dctx = dst->data;\n dctx->nbits = sctx->nbits;\n if (sctx->pub_exp) {\n dctx->pub_exp = BN_dup(sctx->pub_exp);\n if (!dctx->pub_exp)\n return 0;\n }\n dctx->pad_mode = sctx->pad_mode;\n dctx->md = sctx->md;\n dctx->mgf1md = sctx->mgf1md;\n if (sctx->oaep_label) {\n OPENSSL_free(dctx->oaep_label);\n dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);\n if (!dctx->oaep_label)\n return 0;\n dctx->oaep_labellen = sctx->oaep_labellen;\n }\n return 1;\n}', 'BIGNUM *BN_dup(const BIGNUM *a)\n{\n BIGNUM *t;\n if (a == NULL)\n return NULL;\n bn_check_top(a);\n t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();\n if (t == NULL)\n return NULL;\n if (!BN_copy(t, a)) {\n BN_free(t);\n return NULL;\n }\n bn_check_top(t);\n return t;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(a, BN_FLG_CONSTTIME);\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
35,854
0
https://github.com/openssl/openssl/blob/1bc563caa144a568fc505f210568f5ac5b66315e/crypto/hmac/hmac.c/#L132
HMAC_CTX *HMAC_CTX_new(void) { HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX)); if (ctx != NULL) { if (!HMAC_CTX_reset(ctx)) { HMAC_CTX_free(ctx); return NULL; } } return ctx; }
['HMAC_CTX *HMAC_CTX_new(void)\n{\n HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));\n if (ctx != NULL) {\n if (!HMAC_CTX_reset(ctx)) {\n HMAC_CTX_free(ctx);\n return NULL;\n }\n }\n return ctx;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int HMAC_CTX_reset(HMAC_CTX *ctx)\n{\n hmac_ctx_cleanup(ctx);\n if (!hmac_ctx_alloc_mds(ctx)) {\n hmac_ctx_cleanup(ctx);\n return 0;\n }\n return 1;\n}', 'static void hmac_ctx_cleanup(HMAC_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx->i_ctx);\n EVP_MD_CTX_reset(ctx->o_ctx);\n EVP_MD_CTX_reset(ctx->md_ctx);\n ctx->md = NULL;\n ctx->key_length = 0;\n OPENSSL_cleanse(ctx->key, sizeof(ctx->key));\n}']
35,855
0
https://github.com/openssl/openssl/blob/6bc62a620e715f7580651ca932eab052aa527886/crypto/bn/bn_ctx.c/#L268
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int field_tests_default(int n)\n{\n BN_CTX *ctx = NULL;\n EC_GROUP *group = NULL;\n int nid = curves[n].nid;\n int ret = 0;\n TEST_info("Testing curve %s\\n", OBJ_nid2sn(nid));\n if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))\n || !TEST_ptr(ctx = BN_CTX_new())\n || !group_field_tests(group, ctx))\n goto err;\n ret = 1;\n err:\n if (group != NULL)\n EC_GROUP_free(group);\n if (ctx != NULL)\n BN_CTX_free(ctx);\n return ret;\n}', 'static int group_field_tests(const EC_GROUP *group, BN_CTX *ctx)\n{\n BIGNUM *a = NULL, *b = NULL, *c = NULL;\n int ret = 0;\n if (group->meth->field_inv == NULL || group->meth->field_mul == NULL)\n return 1;\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (!TEST_ptr(c = BN_CTX_get(ctx))\n || !TEST_true(group->meth->field_inv(group, b, BN_value_one(), ctx))\n || !TEST_true(BN_is_one(b))\n || !TEST_true(BN_pseudo_rand(a, BN_num_bits(group->field) - 1,\n BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))\n || !TEST_true(group->meth->field_inv(group, b, a, ctx))\n || (group->meth->field_encode &&\n !TEST_true(group->meth->field_encode(group, a, a, ctx)))\n || (group->meth->field_encode &&\n !TEST_true(group->meth->field_encode(group, b, b, ctx)))\n || !TEST_true(group->meth->field_mul(group, c, a, b, ctx))\n || (group->meth->field_decode &&\n !TEST_true(group->meth->field_decode(group, c, c, ctx)))\n || !TEST_true(BN_is_one(c)))\n goto err;\n BN_zero(a);\n if (!TEST_false(group->meth->field_inv(group, b, a, ctx))\n || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC)\n || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) ==\n EC_R_CANNOT_INVERT)\n || !TEST_false(group->meth->field_inv(group, b, group->field, ctx))\n || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC)\n || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) ==\n EC_R_CANNOT_INVERT))\n goto err;\n ERR_clear_error();\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
35,856
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/des/des_enc.c/#L144
void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc) { register DES_LONG l,r,t,u; #ifdef DES_PTR register const unsigned char *des_SP=(const unsigned char *)des_SPtrans; #endif #ifndef DES_UNROLL register int i; #endif register DES_LONG *s; r=data[0]; l=data[1]; IP(r,l); r=ROTATE(r,29)&0xffffffffL; l=ROTATE(l,29)&0xffffffffL; s=(DES_LONG *)ks; if (enc) { #ifdef DES_UNROLL D_ENCRYPT(l,r, 0); D_ENCRYPT(r,l, 2); D_ENCRYPT(l,r, 4); D_ENCRYPT(r,l, 6); D_ENCRYPT(l,r, 8); D_ENCRYPT(r,l,10); D_ENCRYPT(l,r,12); D_ENCRYPT(r,l,14); D_ENCRYPT(l,r,16); D_ENCRYPT(r,l,18); D_ENCRYPT(l,r,20); D_ENCRYPT(r,l,22); D_ENCRYPT(l,r,24); D_ENCRYPT(r,l,26); D_ENCRYPT(l,r,28); D_ENCRYPT(r,l,30); #else for (i=0; i<32; i+=8) { D_ENCRYPT(l,r,i+0); D_ENCRYPT(r,l,i+2); D_ENCRYPT(l,r,i+4); D_ENCRYPT(r,l,i+6); } #endif } else { #ifdef DES_UNROLL D_ENCRYPT(l,r,30); D_ENCRYPT(r,l,28); D_ENCRYPT(l,r,26); D_ENCRYPT(r,l,24); D_ENCRYPT(l,r,22); D_ENCRYPT(r,l,20); D_ENCRYPT(l,r,18); D_ENCRYPT(r,l,16); D_ENCRYPT(l,r,14); D_ENCRYPT(r,l,12); D_ENCRYPT(l,r,10); D_ENCRYPT(r,l, 8); D_ENCRYPT(l,r, 6); D_ENCRYPT(r,l, 4); D_ENCRYPT(l,r, 2); D_ENCRYPT(r,l, 0); #else for (i=30; i>0; i-=8) { D_ENCRYPT(l,r,i-0); D_ENCRYPT(r,l,i-2); D_ENCRYPT(l,r,i-4); D_ENCRYPT(r,l,i-6); } #endif } l=ROTATE(l,3)&0xffffffffL; r=ROTATE(r,3)&0xffffffffL; FP(r,l); data[0]=l; data[1]=r; l=r=t=u=0; }
['static int cfb64_test(unsigned char *cfb_cipher)\n\t{\n\tdes_key_schedule ks;\n\tint err=0,i,n;\n\tdes_key_sched(cfb_key,ks);\n\tmemcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));\n\tn=0;\n\tdes_cfb64_encrypt(plain,cfb_buf1,12,ks,cfb_tmp,&n,DES_ENCRYPT);\n\tdes_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),sizeof(plain)-12,ks,\n\t\t\t cfb_tmp,&n,DES_ENCRYPT);\n\tif (memcmp(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)\n\t\t{\n\t\terr=1;\n\t\tprintf("cfb_encrypt encrypt error\\n");\n\t\tfor (i=0; i<24; i+=8)\n\t\t\tprintf("%s\\n",pt(&(cfb_buf1[i])));\n\t\t}\n\tmemcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));\n\tn=0;\n\tdes_cfb64_encrypt(cfb_buf1,cfb_buf2,17,ks,cfb_tmp,&n,DES_DECRYPT);\n\tdes_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),\n\t\t\t sizeof(plain)-17,ks,cfb_tmp,&n,DES_DECRYPT);\n\tif (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)\n\t\t{\n\t\terr=1;\n\t\tprintf("cfb_encrypt decrypt error\\n");\n\t\tfor (i=0; i<24; i+=8)\n\t\t\tprintf("%s\\n",pt(&(cfb_buf2[i])));\n\t\t}\n\treturn(err);\n\t}', 'void des_cfb64_encrypt(const unsigned char *in, unsigned char *out,\n\t long length, des_key_schedule schedule, des_cblock ivec, int *num,\n\t int enc)\n\t{\n\tregister DES_LONG v0,v1;\n\tregister long l=length;\n\tregister int n= *num;\n\tDES_LONG ti[2];\n\tunsigned char *iv,c,cc;\n\tiv=ivec;\n\tif (enc)\n\t\t{\n\t\twhile (l--)\n\t\t\t{\n\t\t\tif (n == 0)\n\t\t\t\t{\n\t\t\t\tc2l(iv,v0); ti[0]=v0;\n\t\t\t\tc2l(iv,v1); ti[1]=v1;\n\t\t\t\tdes_encrypt(ti,schedule,DES_ENCRYPT);\n\t\t\t\tiv=ivec;\n\t\t\t\tv0=ti[0]; l2c(v0,iv);\n\t\t\t\tv0=ti[1]; l2c(v0,iv);\n\t\t\t\tiv=ivec;\n\t\t\t\t}\n\t\t\tc= *(in++)^iv[n];\n\t\t\t*(out++)=c;\n\t\t\tiv[n]=c;\n\t\t\tn=(n+1)&0x07;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\twhile (l--)\n\t\t\t{\n\t\t\tif (n == 0)\n\t\t\t\t{\n\t\t\t\tc2l(iv,v0); ti[0]=v0;\n\t\t\t\tc2l(iv,v1); ti[1]=v1;\n\t\t\t\tdes_encrypt(ti,schedule,DES_ENCRYPT);\n\t\t\t\tiv=ivec;\n\t\t\t\tv0=ti[0]; l2c(v0,iv);\n\t\t\t\tv0=ti[1]; l2c(v0,iv);\n\t\t\t\tiv=ivec;\n\t\t\t\t}\n\t\t\tcc= *(in++);\n\t\t\tc=iv[n];\n\t\t\tiv[n]=cc;\n\t\t\t*(out++)=c^cc;\n\t\t\tn=(n+1)&0x07;\n\t\t\t}\n\t\t}\n\tv0=v1=ti[0]=ti[1]=c=cc=0;\n\t*num=n;\n\t}', 'void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc)\n\t{\n\tregister DES_LONG l,r,t,u;\n#ifdef DES_PTR\n\tregister const unsigned char *des_SP=(const unsigned char *)des_SPtrans;\n#endif\n#ifndef DES_UNROLL\n\tregister int i;\n#endif\n\tregister DES_LONG *s;\n\tr=data[0];\n\tl=data[1];\n\tIP(r,l);\n\tr=ROTATE(r,29)&0xffffffffL;\n\tl=ROTATE(l,29)&0xffffffffL;\n\ts=(DES_LONG *)ks;\n\tif (enc)\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r, 0);\n\t\tD_ENCRYPT(r,l, 2);\n\t\tD_ENCRYPT(l,r, 4);\n\t\tD_ENCRYPT(r,l, 6);\n\t\tD_ENCRYPT(l,r, 8);\n\t\tD_ENCRYPT(r,l,10);\n\t\tD_ENCRYPT(l,r,12);\n\t\tD_ENCRYPT(r,l,14);\n\t\tD_ENCRYPT(l,r,16);\n\t\tD_ENCRYPT(r,l,18);\n\t\tD_ENCRYPT(l,r,20);\n\t\tD_ENCRYPT(r,l,22);\n\t\tD_ENCRYPT(l,r,24);\n\t\tD_ENCRYPT(r,l,26);\n\t\tD_ENCRYPT(l,r,28);\n\t\tD_ENCRYPT(r,l,30);\n#else\n\t\tfor (i=0; i<32; i+=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i+0);\n\t\t\tD_ENCRYPT(r,l,i+2);\n\t\t\tD_ENCRYPT(l,r,i+4);\n\t\t\tD_ENCRYPT(r,l,i+6);\n\t\t\t}\n#endif\n\t\t}\n\telse\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r,30);\n\t\tD_ENCRYPT(r,l,28);\n\t\tD_ENCRYPT(l,r,26);\n\t\tD_ENCRYPT(r,l,24);\n\t\tD_ENCRYPT(l,r,22);\n\t\tD_ENCRYPT(r,l,20);\n\t\tD_ENCRYPT(l,r,18);\n\t\tD_ENCRYPT(r,l,16);\n\t\tD_ENCRYPT(l,r,14);\n\t\tD_ENCRYPT(r,l,12);\n\t\tD_ENCRYPT(l,r,10);\n\t\tD_ENCRYPT(r,l, 8);\n\t\tD_ENCRYPT(l,r, 6);\n\t\tD_ENCRYPT(r,l, 4);\n\t\tD_ENCRYPT(l,r, 2);\n\t\tD_ENCRYPT(r,l, 0);\n#else\n\t\tfor (i=30; i>0; i-=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i-0);\n\t\t\tD_ENCRYPT(r,l,i-2);\n\t\t\tD_ENCRYPT(l,r,i-4);\n\t\t\tD_ENCRYPT(r,l,i-6);\n\t\t\t}\n#endif\n\t\t}\n\tl=ROTATE(l,3)&0xffffffffL;\n\tr=ROTATE(r,3)&0xffffffffL;\n\tFP(r,l);\n\tdata[0]=l;\n\tdata[1]=r;\n\tl=r=t=u=0;\n\t}']
35,857
0
https://github.com/libav/libav/blob/80bdf7e0b7975956c42f17589cb21a5531f179ef/libavcodec/vc1dec.c/#L1586
static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra, int pred_flag, int dir) { MpegEncContext *s = &v->s; int xy, wrap, off = 0; int16_t *A, *B, *C; int px, py; int sum; int mixedmv_pic, num_samefield = 0, num_oppfield = 0; int opposit, f; int16_t samefield_pred[2], oppfield_pred[2]; int16_t samefield_predA[2], oppfield_predA[2]; int16_t samefield_predB[2], oppfield_predB[2]; int16_t samefield_predC[2], oppfield_predC[2]; int16_t *predA, *predC; int a_valid, b_valid, c_valid; int hybridmv_thresh, y_bias = 0; if (v->mv_mode == MV_PMODE_MIXED_MV || ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV))) mixedmv_pic = 1; else mixedmv_pic = 0; dmv_x <<= 1 - s->quarter_sample; dmv_y <<= 1 - s->quarter_sample; wrap = s->b8_stride; xy = s->block_index[n]; if(s->mb_intra){ s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0; s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0; s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0; s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0; if(mv1) { s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0; s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0; s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0; s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0; s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0; s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0; v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0; s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0; s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0; s->current_picture.f.motion_val[1][xy + wrap][0] = 0; s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0; s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0; s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0; } return; } C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off]; A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off]; if(mv1) { if (v->field_mode && mixedmv_pic) off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2; else off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2; } else { switch (n) { case 0: off = (s->mb_x > 0) ? -1 : 1; break; case 1: off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1; break; case 2: off = 1; break; case 3: off = -1; } } B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off]; a_valid = !s->first_slice_line || (n==2 || n==3); b_valid = a_valid && (s->mb_width > 1); c_valid = s->mb_x || (n==1 || n==3); if (v->field_mode) { a_valid = a_valid && !is_intra[xy - wrap]; b_valid = b_valid && !is_intra[xy - wrap + off]; c_valid = c_valid && !is_intra[xy - 1]; } if (a_valid) { f = v->mv_f[dir][xy - wrap + v->blocks_off]; num_oppfield += f; num_samefield += 1 - f; if (f) { oppfield_predA[0] = A[0]; oppfield_predA[1] = A[1]; samefield_predA[0] = scaleforsame(v, 0, A[0], 0, dir); samefield_predA[1] = scaleforsame(v, n, A[1], 1, dir); } else { samefield_predA[0] = A[0]; samefield_predA[1] = A[1]; if (v->numref) oppfield_predA[0] = scaleforopp(v, A[0], 0, dir); if (v->numref) oppfield_predA[1] = scaleforopp(v, A[1], 1, dir); } } else { samefield_predA[0] = samefield_predA[1] = 0; oppfield_predA[0] = oppfield_predA[1] = 0; } if (c_valid) { f = v->mv_f[dir][xy - 1 + v->blocks_off]; num_oppfield += f; num_samefield += 1 - f; if (f) { oppfield_predC[0] = C[0]; oppfield_predC[1] = C[1]; samefield_predC[0] = scaleforsame(v, 0, C[0], 0, dir); samefield_predC[1] = scaleforsame(v, n, C[1], 1, dir); } else { samefield_predC[0] = C[0]; samefield_predC[1] = C[1]; if (v->numref) oppfield_predC[0] = scaleforopp(v, C[0], 0, dir); if (v->numref) oppfield_predC[1] = scaleforopp(v, C[1], 1, dir); } } else { samefield_predC[0] = samefield_predC[1] = 0; oppfield_predC[0] = oppfield_predC[1] = 0; } if (b_valid) { f = v->mv_f[dir][xy - wrap + off + v->blocks_off]; num_oppfield += f; num_samefield += 1 - f; if (f) { oppfield_predB[0] = B[0]; oppfield_predB[1] = B[1]; samefield_predB[0] = scaleforsame(v, 0, B[0], 0, dir); samefield_predB[1] = scaleforsame(v, n, B[1], 1, dir); } else { samefield_predB[0] = B[0]; samefield_predB[1] = B[1]; if (v->numref) oppfield_predB[0] = scaleforopp(v, B[0], 0, dir); if (v->numref) oppfield_predB[1] = scaleforopp(v, B[1], 1, dir); } } else { samefield_predB[0] = samefield_predB[1] = 0; oppfield_predB[0] = oppfield_predB[1] = 0; } if (a_valid) { samefield_pred[0] = samefield_predA[0]; samefield_pred[1] = samefield_predA[1]; oppfield_pred[0] = oppfield_predA[0]; oppfield_pred[1] = oppfield_predA[1]; } else if (c_valid) { samefield_pred[0] = samefield_predC[0]; samefield_pred[1] = samefield_predC[1]; oppfield_pred[0] = oppfield_predC[0]; oppfield_pred[1] = oppfield_predC[1]; } else if (b_valid) { samefield_pred[0] = samefield_predB[0]; samefield_pred[1] = samefield_predB[1]; oppfield_pred[0] = oppfield_predB[0]; oppfield_pred[1] = oppfield_predB[1]; } else { samefield_pred[0] = samefield_pred[1] = 0; oppfield_pred[0] = oppfield_pred[1] = 0; } if (num_samefield + num_oppfield > 1) { samefield_pred[0] = mid_pred(samefield_predA[0], samefield_predB[0], samefield_predC[0]); samefield_pred[1] = mid_pred(samefield_predA[1], samefield_predB[1], samefield_predC[1]); if (v->numref) oppfield_pred[0] = mid_pred(oppfield_predA[0], oppfield_predB[0], oppfield_predC[0]); if (v->numref) oppfield_pred[1] = mid_pred(oppfield_predA[1], oppfield_predB[1], oppfield_predC[1]); } if (v->field_mode) { if (num_samefield <= num_oppfield) opposit = 1 - pred_flag; else opposit = pred_flag; } else opposit = 0; if (opposit) { px = oppfield_pred[0]; py = oppfield_pred[1]; predA = oppfield_predA; predC = oppfield_predC; v->mv_f[dir][xy + v->blocks_off] = f = 1; v->ref_field_type[dir] = !v->cur_field_type; } else { px = samefield_pred[0]; py = samefield_pred[1]; predA = samefield_predA; predC = samefield_predC; v->mv_f[dir][xy + v->blocks_off] = f = 0; v->ref_field_type[dir] = v->cur_field_type; } if (!v->field_mode) { int qx, qy, X, Y; qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0); qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0); X = (s->mb_width << 6) - 4; Y = (s->mb_height << 6) - 4; if(mv1) { if(qx + px < -60) px = -60 - qx; if(qy + py < -60) py = -60 - qy; } else { if(qx + px < -28) px = -28 - qx; if(qy + py < -28) py = -28 - qy; } if(qx + px > X) px = X - qx; if(qy + py > Y) py = Y - qy; } if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) { if (v->field_mode && !s->quarter_sample) hybridmv_thresh = 16; else hybridmv_thresh = 32; if (a_valid && c_valid) { if (is_intra[xy - wrap]) sum = FFABS(px) + FFABS(py); else sum = FFABS(px - predA[0]) + FFABS(py - predA[1]); if (sum > hybridmv_thresh) { if (get_bits1(&s->gb)) { px = predA[0]; py = predA[1]; } else { px = predC[0]; py = predC[1]; } } else { if (is_intra[xy - 1]) sum = FFABS(px) + FFABS(py); else sum = FFABS(px - predC[0]) + FFABS(py - predC[1]); if (sum > hybridmv_thresh) { if(get_bits1(&s->gb)) { px = predA[0]; py = predA[1]; } else { px = predC[0]; py = predC[1]; } } } } } if (v->field_mode && !s->quarter_sample) { r_x <<= 1; r_y <<= 1; } if (v->field_mode && v->numref) r_y >>= 1; if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0) y_bias = 1; s->mv[dir][n][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x; s->mv[dir][n][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias; if(mv1) { s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0]; s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1]; s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0]; s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1]; s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0]; s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1]; v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off]; v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off]; } }
['static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra, int pred_flag, int dir)\n{\n MpegEncContext *s = &v->s;\n int xy, wrap, off = 0;\n int16_t *A, *B, *C;\n int px, py;\n int sum;\n int mixedmv_pic, num_samefield = 0, num_oppfield = 0;\n int opposit, f;\n int16_t samefield_pred[2], oppfield_pred[2];\n int16_t samefield_predA[2], oppfield_predA[2];\n int16_t samefield_predB[2], oppfield_predB[2];\n int16_t samefield_predC[2], oppfield_predC[2];\n int16_t *predA, *predC;\n int a_valid, b_valid, c_valid;\n int hybridmv_thresh, y_bias = 0;\n if (v->mv_mode == MV_PMODE_MIXED_MV ||\n ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV))) mixedmv_pic = 1;\n else mixedmv_pic = 0;\n dmv_x <<= 1 - s->quarter_sample;\n dmv_y <<= 1 - s->quarter_sample;\n wrap = s->b8_stride;\n xy = s->block_index[n];\n if(s->mb_intra){\n s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;\n s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;\n s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;\n s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;\n if(mv1) {\n s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;\n s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;\n s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;\n s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;\n s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;\n s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;\n v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;\n s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;\n s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;\n s->current_picture.f.motion_val[1][xy + wrap][0] = 0;\n s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;\n s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;\n s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;\n }\n return;\n }\n C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];\n A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];\n if(mv1) {\n if (v->field_mode && mixedmv_pic)\n off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;\n else\n off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;\n } else {\n switch (n) {\n case 0:\n off = (s->mb_x > 0) ? -1 : 1;\n break;\n case 1:\n off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;\n break;\n case 2:\n off = 1;\n break;\n case 3:\n off = -1;\n }\n }\n B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];\n a_valid = !s->first_slice_line || (n==2 || n==3);\n b_valid = a_valid && (s->mb_width > 1);\n c_valid = s->mb_x || (n==1 || n==3);\n if (v->field_mode) {\n a_valid = a_valid && !is_intra[xy - wrap];\n b_valid = b_valid && !is_intra[xy - wrap + off];\n c_valid = c_valid && !is_intra[xy - 1];\n }\n if (a_valid) {\n f = v->mv_f[dir][xy - wrap + v->blocks_off];\n num_oppfield += f;\n num_samefield += 1 - f;\n if (f) {\n oppfield_predA[0] = A[0];\n oppfield_predA[1] = A[1];\n samefield_predA[0] = scaleforsame(v, 0, A[0], 0, dir);\n samefield_predA[1] = scaleforsame(v, n, A[1], 1, dir);\n } else {\n samefield_predA[0] = A[0];\n samefield_predA[1] = A[1];\n if (v->numref)\n oppfield_predA[0] = scaleforopp(v, A[0], 0, dir);\n if (v->numref)\n oppfield_predA[1] = scaleforopp(v, A[1], 1, dir);\n }\n } else {\n samefield_predA[0] = samefield_predA[1] = 0;\n oppfield_predA[0] = oppfield_predA[1] = 0;\n }\n if (c_valid) {\n f = v->mv_f[dir][xy - 1 + v->blocks_off];\n num_oppfield += f;\n num_samefield += 1 - f;\n if (f) {\n oppfield_predC[0] = C[0];\n oppfield_predC[1] = C[1];\n samefield_predC[0] = scaleforsame(v, 0, C[0], 0, dir);\n samefield_predC[1] = scaleforsame(v, n, C[1], 1, dir);\n } else {\n samefield_predC[0] = C[0];\n samefield_predC[1] = C[1];\n if (v->numref)\n oppfield_predC[0] = scaleforopp(v, C[0], 0, dir);\n if (v->numref)\n oppfield_predC[1] = scaleforopp(v, C[1], 1, dir);\n }\n } else {\n samefield_predC[0] = samefield_predC[1] = 0;\n oppfield_predC[0] = oppfield_predC[1] = 0;\n }\n if (b_valid) {\n f = v->mv_f[dir][xy - wrap + off + v->blocks_off];\n num_oppfield += f;\n num_samefield += 1 - f;\n if (f) {\n oppfield_predB[0] = B[0];\n oppfield_predB[1] = B[1];\n samefield_predB[0] = scaleforsame(v, 0, B[0], 0, dir);\n samefield_predB[1] = scaleforsame(v, n, B[1], 1, dir);\n } else {\n samefield_predB[0] = B[0];\n samefield_predB[1] = B[1];\n if (v->numref)\n oppfield_predB[0] = scaleforopp(v, B[0], 0, dir);\n if (v->numref)\n oppfield_predB[1] = scaleforopp(v, B[1], 1, dir);\n }\n } else {\n samefield_predB[0] = samefield_predB[1] = 0;\n oppfield_predB[0] = oppfield_predB[1] = 0;\n }\n if (a_valid) {\n samefield_pred[0] = samefield_predA[0];\n samefield_pred[1] = samefield_predA[1];\n oppfield_pred[0] = oppfield_predA[0];\n oppfield_pred[1] = oppfield_predA[1];\n } else if (c_valid) {\n samefield_pred[0] = samefield_predC[0];\n samefield_pred[1] = samefield_predC[1];\n oppfield_pred[0] = oppfield_predC[0];\n oppfield_pred[1] = oppfield_predC[1];\n } else if (b_valid) {\n samefield_pred[0] = samefield_predB[0];\n samefield_pred[1] = samefield_predB[1];\n oppfield_pred[0] = oppfield_predB[0];\n oppfield_pred[1] = oppfield_predB[1];\n } else {\n samefield_pred[0] = samefield_pred[1] = 0;\n oppfield_pred[0] = oppfield_pred[1] = 0;\n }\n if (num_samefield + num_oppfield > 1) {\n samefield_pred[0] = mid_pred(samefield_predA[0], samefield_predB[0], samefield_predC[0]);\n samefield_pred[1] = mid_pred(samefield_predA[1], samefield_predB[1], samefield_predC[1]);\n if (v->numref)\n oppfield_pred[0] = mid_pred(oppfield_predA[0], oppfield_predB[0], oppfield_predC[0]);\n if (v->numref)\n oppfield_pred[1] = mid_pred(oppfield_predA[1], oppfield_predB[1], oppfield_predC[1]);\n }\n if (v->field_mode) {\n if (num_samefield <= num_oppfield)\n opposit = 1 - pred_flag;\n else\n opposit = pred_flag;\n } else\n opposit = 0;\n if (opposit) {\n px = oppfield_pred[0];\n py = oppfield_pred[1];\n predA = oppfield_predA;\n predC = oppfield_predC;\n v->mv_f[dir][xy + v->blocks_off] = f = 1;\n v->ref_field_type[dir] = !v->cur_field_type;\n } else {\n px = samefield_pred[0];\n py = samefield_pred[1];\n predA = samefield_predA;\n predC = samefield_predC;\n v->mv_f[dir][xy + v->blocks_off] = f = 0;\n v->ref_field_type[dir] = v->cur_field_type;\n }\n if (!v->field_mode) {\n int qx, qy, X, Y;\n qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);\n qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);\n X = (s->mb_width << 6) - 4;\n Y = (s->mb_height << 6) - 4;\n if(mv1) {\n if(qx + px < -60) px = -60 - qx;\n if(qy + py < -60) py = -60 - qy;\n } else {\n if(qx + px < -28) px = -28 - qx;\n if(qy + py < -28) py = -28 - qy;\n }\n if(qx + px > X) px = X - qx;\n if(qy + py > Y) py = Y - qy;\n }\n if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {\n if (v->field_mode && !s->quarter_sample)\n hybridmv_thresh = 16;\n else\n hybridmv_thresh = 32;\n if (a_valid && c_valid) {\n if (is_intra[xy - wrap])\n sum = FFABS(px) + FFABS(py);\n else\n sum = FFABS(px - predA[0]) + FFABS(py - predA[1]);\n if (sum > hybridmv_thresh) {\n if (get_bits1(&s->gb)) {\n px = predA[0];\n py = predA[1];\n } else {\n px = predC[0];\n py = predC[1];\n }\n } else {\n if (is_intra[xy - 1])\n sum = FFABS(px) + FFABS(py);\n else\n sum = FFABS(px - predC[0]) + FFABS(py - predC[1]);\n if (sum > hybridmv_thresh) {\n if(get_bits1(&s->gb)) {\n px = predA[0];\n py = predA[1];\n } else {\n px = predC[0];\n py = predC[1];\n }\n }\n }\n }\n }\n if (v->field_mode && !s->quarter_sample) {\n r_x <<= 1;\n r_y <<= 1;\n }\n if (v->field_mode && v->numref)\n r_y >>= 1;\n if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)\n y_bias = 1;\n s->mv[dir][n][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;\n s->mv[dir][n][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;\n if(mv1) {\n s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];\n s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];\n s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];\n s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];\n s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];\n s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];\n v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];\n v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];\n }\n}']
35,858
0
https://github.com/openssl/openssl/blob/404fb7149effa40d04625272424a28ff25c0e673/crypto/lhash/lhash.c/#L359
static void contract(LHASH *lh) { LHASH_NODE **n,*n1,*np; np=lh->b[lh->p+lh->pmax-1]; lh->b[lh->p+lh->pmax-1]=NULL; if (lh->p == 0) { n=(LHASH_NODE **)Realloc((char *)lh->b, (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax)); if (n == NULL) { lh->error++; return; } lh->num_contract_reallocs++; lh->num_alloc_nodes/=2; lh->pmax/=2; lh->p=lh->pmax-1; lh->b=n; } else lh->p--; lh->num_nodes--; lh->num_contracts++; n1=lh->b[(int)lh->p]; if (n1 == NULL) lh->b[(int)lh->p]=np; else { while (n1->next != NULL) n1=n1->next; n1->next=np; } }
['static int ssl3_get_certificate_request(SSL *s)\n\t{\n\tint ok,ret=0;\n\tunsigned long n,nc,l;\n\tunsigned int llen,ctype_num,i;\n\tX509_NAME *xn=NULL;\n\tunsigned char *p,*d,*q;\n\tSTACK_OF(X509_NAME) *ca_sk=NULL;\n\tn=ssl3_get_message(s,\n\t\tSSL3_ST_CR_CERT_REQ_A,\n\t\tSSL3_ST_CR_CERT_REQ_B,\n\t\t-1,\n#if defined(MSDOS) && !defined(WIN32)\n\t\t1024*30,\n#else\n\t\t1024*100,\n#endif\n\t\t&ok);\n\tif (!ok) return((int)n);\n\ts->s3->tmp.cert_req=0;\n\tif (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)\n\t\t{\n\t\ts->s3->tmp.reuse_message=1;\n\t\treturn(1);\n\t\t}\n\tif (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)\n\t\t{\n\t\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);\n\t\tSSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);\n\t\tgoto err;\n\t\t}\n\tif (s->version > SSL3_VERSION)\n\t\t{\n\t\tl=s->s3->tmp.new_cipher->algorithms;\n\t\tif (l & SSL_aNULL)\n\t\t\t{\n\t\t\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);\n\t\t\tSSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\td=p=(unsigned char *)s->init_buf->data;\n\tif ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tctype_num= *(p++);\n\tif (ctype_num > SSL3_CT_NUMBER)\n\t\tctype_num=SSL3_CT_NUMBER;\n\tfor (i=0; i<ctype_num; i++)\n\t\ts->s3->tmp.ctype[i]= p[i];\n\tp+=ctype_num;\n\tn2s(p,llen);\n#if 0\n{\nFILE *out;\nout=fopen("/tmp/vsign.der","w");\nfwrite(p,1,llen,out);\nfclose(out);\n}\n#endif\n\tif ((llen+ctype_num+2+1) != n)\n\t\t{\n\t\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);\n\t\tSSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);\n\t\tgoto err;\n\t\t}\n\tfor (nc=0; nc<llen; )\n\t\t{\n\t\tn2s(p,l);\n\t\tif ((l+nc+2) > llen)\n\t\t\t{\n\t\t\tif ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))\n\t\t\t\tgoto cont;\n\t\t\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);\n\t\t\tSSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);\n\t\t\tgoto err;\n\t\t\t}\n\t\tq=p;\n\t\tif ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)\n\t\t\t{\n\t\t\tif (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)\n\t\t\t\tgoto cont;\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\tif (q != (p+l))\n\t\t\t{\n\t\t\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);\n\t\t\tSSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (!sk_X509_NAME_push(ca_sk,xn))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=l;\n\t\tnc+=l+2;\n\t\t}\n\tif (0)\n\t\t{\ncont:\n\t\tERR_clear_error();\n\t\t}\n\ts->s3->tmp.cert_req=1;\n\ts->s3->tmp.ctype_num=ctype_num;\n\tif (s->s3->tmp.ca_names != NULL)\n\t\tsk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);\n\ts->s3->tmp.ca_names=ca_sk;\n\tca_sk=NULL;\n\tret=1;\nerr:\n\tif (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);\n\treturn(ret);\n\t}', 'long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)\n\t{\n\tunsigned char *p;\n\tunsigned long l;\n\tlong n;\n\tint i,al;\n\tif (s->s3->tmp.reuse_message)\n\t\t{\n\t\ts->s3->tmp.reuse_message=0;\n\t\tif ((mt >= 0) && (s->s3->tmp.message_type != mt))\n\t\t\t{\n\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t*ok=1;\n\t\treturn((int)s->s3->tmp.message_size);\n\t\t}\n\tp=(unsigned char *)s->init_buf->data;\n\tif (s->state == st1)\n\t\t{\n\t\ti=ssl3_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],\n\t\t\t\t 4-s->init_num);\n\t\tif (i < (4-s->init_num))\n\t\t\t{\n\t\t\t*ok=0;\n\t\t\treturn(ssl3_part_read(s,i));\n\t\t\t}\n\t\tif ((mt >= 0) && (*p != mt))\n\t\t\t{\n\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\ts->s3->tmp.message_type= *(p++);\n\t\tn2l3(p,l);\n\t\tif (l > (unsigned long)max)\n\t\t\t{\n\t\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\t\tSSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_EXCESSIVE_MESSAGE_SIZE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (l && !BUF_MEM_grow(s->init_buf,(int)l))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_MESSAGE,ERR_R_BUF_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\ts->s3->tmp.message_size=l;\n\t\ts->state=stn;\n\t\ts->init_num=0;\n\t\t}\n\tp=(unsigned char *)s->init_buf->data;\n\tn=s->s3->tmp.message_size;\n\tif (n > 0)\n\t\t{\n\t\ti=ssl3_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],n);\n\t\tif (i != (int)n)\n\t\t\t{\n\t\t\t*ok=0;\n\t\t\treturn(ssl3_part_read(s,i));\n\t\t\t}\n\t\t}\n\t*ok=1;\n\treturn(n);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\t*ok=0;\n\treturn(-1);\n\t}', 'void ssl3_send_alert(SSL *s, int level, int desc)\n\t{\n\tdesc=s->method->ssl3_enc->alert_value(desc);\n\tif (desc < 0) return;\n\tif ((level == 2) && (s->session != NULL))\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\ts->s3->alert_dispatch=1;\n\ts->s3->send_alert[0]=level;\n\ts->s3->send_alert[1]=desc;\n\tif (s->s3->wbuf.left == 0)\n\t\tssl3_dispatch_alert(s);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,(char *)c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'char *lh_delete(LHASH *lh, char *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tchar *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree((char *)nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}', 'static void contract(LHASH *lh)\n\t{\n\tLHASH_NODE **n,*n1,*np;\n\tnp=lh->b[lh->p+lh->pmax-1];\n\tlh->b[lh->p+lh->pmax-1]=NULL;\n\tif (lh->p == 0)\n\t\t{\n\t\tn=(LHASH_NODE **)Realloc((char *)lh->b,\n\t\t\t(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));\n\t\tif (n == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn;\n\t\t\t}\n\t\tlh->num_contract_reallocs++;\n\t\tlh->num_alloc_nodes/=2;\n\t\tlh->pmax/=2;\n\t\tlh->p=lh->pmax-1;\n\t\tlh->b=n;\n\t\t}\n\telse\n\t\tlh->p--;\n\tlh->num_nodes--;\n\tlh->num_contracts++;\n\tn1=lh->b[(int)lh->p];\n\tif (n1 == NULL)\n\t\tlh->b[(int)lh->p]=np;\n\telse\n\t\t{\n\t\twhile (n1->next != NULL)\n\t\t\tn1=n1->next;\n\t\tn1->next=np;\n\t\t}\n\t}']
35,859
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bn/bn_asm.c/#L541
void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) { #ifdef BN_LLONG BN_ULLONG t; #else BN_ULONG bl,bh; #endif BN_ULONG t1,t2; BN_ULONG c1,c2,c3; c1=0; c2=0; c3=0; mul_add_c(a[0],b[0],c1,c2,c3); r[0]=c1; c1=0; mul_add_c(a[0],b[1],c2,c3,c1); mul_add_c(a[1],b[0],c2,c3,c1); r[1]=c2; c2=0; mul_add_c(a[2],b[0],c3,c1,c2); mul_add_c(a[1],b[1],c3,c1,c2); mul_add_c(a[0],b[2],c3,c1,c2); r[2]=c3; c3=0; mul_add_c(a[0],b[3],c1,c2,c3); mul_add_c(a[1],b[2],c1,c2,c3); mul_add_c(a[2],b[1],c1,c2,c3); mul_add_c(a[3],b[0],c1,c2,c3); r[3]=c1; c1=0; mul_add_c(a[4],b[0],c2,c3,c1); mul_add_c(a[3],b[1],c2,c3,c1); mul_add_c(a[2],b[2],c2,c3,c1); mul_add_c(a[1],b[3],c2,c3,c1); mul_add_c(a[0],b[4],c2,c3,c1); r[4]=c2; c2=0; mul_add_c(a[0],b[5],c3,c1,c2); mul_add_c(a[1],b[4],c3,c1,c2); mul_add_c(a[2],b[3],c3,c1,c2); mul_add_c(a[3],b[2],c3,c1,c2); mul_add_c(a[4],b[1],c3,c1,c2); mul_add_c(a[5],b[0],c3,c1,c2); r[5]=c3; c3=0; mul_add_c(a[6],b[0],c1,c2,c3); mul_add_c(a[5],b[1],c1,c2,c3); mul_add_c(a[4],b[2],c1,c2,c3); mul_add_c(a[3],b[3],c1,c2,c3); mul_add_c(a[2],b[4],c1,c2,c3); mul_add_c(a[1],b[5],c1,c2,c3); mul_add_c(a[0],b[6],c1,c2,c3); r[6]=c1; c1=0; mul_add_c(a[0],b[7],c2,c3,c1); mul_add_c(a[1],b[6],c2,c3,c1); mul_add_c(a[2],b[5],c2,c3,c1); mul_add_c(a[3],b[4],c2,c3,c1); mul_add_c(a[4],b[3],c2,c3,c1); mul_add_c(a[5],b[2],c2,c3,c1); mul_add_c(a[6],b[1],c2,c3,c1); mul_add_c(a[7],b[0],c2,c3,c1); r[7]=c2; c2=0; mul_add_c(a[7],b[1],c3,c1,c2); mul_add_c(a[6],b[2],c3,c1,c2); mul_add_c(a[5],b[3],c3,c1,c2); mul_add_c(a[4],b[4],c3,c1,c2); mul_add_c(a[3],b[5],c3,c1,c2); mul_add_c(a[2],b[6],c3,c1,c2); mul_add_c(a[1],b[7],c3,c1,c2); r[8]=c3; c3=0; mul_add_c(a[2],b[7],c1,c2,c3); mul_add_c(a[3],b[6],c1,c2,c3); mul_add_c(a[4],b[5],c1,c2,c3); mul_add_c(a[5],b[4],c1,c2,c3); mul_add_c(a[6],b[3],c1,c2,c3); mul_add_c(a[7],b[2],c1,c2,c3); r[9]=c1; c1=0; mul_add_c(a[7],b[3],c2,c3,c1); mul_add_c(a[6],b[4],c2,c3,c1); mul_add_c(a[5],b[5],c2,c3,c1); mul_add_c(a[4],b[6],c2,c3,c1); mul_add_c(a[3],b[7],c2,c3,c1); r[10]=c2; c2=0; mul_add_c(a[4],b[7],c3,c1,c2); mul_add_c(a[5],b[6],c3,c1,c2); mul_add_c(a[6],b[5],c3,c1,c2); mul_add_c(a[7],b[4],c3,c1,c2); r[11]=c3; c3=0; mul_add_c(a[7],b[5],c1,c2,c3); mul_add_c(a[6],b[6],c1,c2,c3); mul_add_c(a[5],b[7],c1,c2,c3); r[12]=c1; c1=0; mul_add_c(a[6],b[7],c2,c3,c1); mul_add_c(a[7],b[6],c2,c3,c1); r[13]=c2; c2=0; mul_add_c(a[7],b[7],c3,c1,c2); r[14]=c3; r[15]=c1; }
['int BN_is_prime(BIGNUM *a, int checks, void (*callback)(P_I_I_P),\n\t BN_CTX *ctx_passed, char *cb_arg)\n\t{\n\tint i,j,c2=0,ret= -1;\n\tBIGNUM *check;\n\tBN_CTX *ctx=NULL,*ctx2=NULL;\n\tBN_MONT_CTX *mont=NULL;\n\tif (!BN_is_odd(a))\n\t\treturn(0);\n\tif (ctx_passed != NULL)\n\t\tctx=ctx_passed;\n\telse\n\t\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tif ((ctx2=BN_CTX_new()) == NULL) goto err;\n\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\tcheck= &(ctx->bn[ctx->tos++]);\n\tif (!BN_MONT_CTX_set(mont,a,ctx2)) goto err;\n\tfor (i=0; i<checks; i++)\n\t\t{\n\t\tif (!BN_rand(check,BN_num_bits(a)-1,0,0)) goto err;\n\t\tj=witness(check,a,ctx,ctx2,mont);\n\t\tif (j == -1) goto err;\n\t\tif (j)\n\t\t\t{\n\t\t\tret=0;\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (callback != NULL) callback(1,c2++,cb_arg);\n\t\t}\n\tret=1;\nerr:\n\tctx->tos--;\n\tif ((ctx_passed == NULL) && (ctx != NULL))\n\t\tBN_CTX_free(ctx);\n\tif (ctx2 != NULL)\n\t\tBN_CTX_free(ctx2);\n\tif (mont != NULL) BN_MONT_CTX_free(mont);\n\treturn(ret);\n\t}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tBIGNUM Ri,*R;\n\tBN_init(&Ri);\n\tR= &(mont->RR);\n\tBN_copy(&(mont->N),mod);\n#ifdef BN_RECURSION_MONT\n\tif (mont->N.top < BN_MONT_CTX_SET_SIZE_WORD)\n#endif\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tmont->use_word=1;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,BN_BITS2);\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.d=buf;\n\t\ttmod.top=1;\n\t\ttmod.max=mod->max;\n\t\ttmod.neg=mod->neg;\n\t\tif ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,BN_BITS2);\n\t\tif (!BN_is_zero(&Ri))\n\t\t\t{\n#if 1\n\t\t\tBN_sub_word(&Ri,1);\n#else\n\t\t\tBN_usub(&Ri,&Ri,BN_value_one());\n#endif\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBN_set_word(&Ri,BN_MASK2);\n\t\t\t}\n\t\tBN_div(&Ri,NULL,&Ri,&tmod,ctx);\n\t\tmont->n0=Ri.d[0];\n\t\tBN_free(&Ri);\n\t\t}\n#ifdef BN_RECURSION_MONT\n\telse\n\t\t{\n\t\tmont->use_word=0;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n#if 1\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,mont->ri);\n#else\n\t\tBN_lshift(R,BN_value_one(),mont->ri);\n#endif\n\t\tif ((BN_mod_inverse(&Ri,R,mod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,mont->ri);\n#if 1\n\t\tBN_sub_word(&Ri,1);\n#else\n\t\tBN_usub(&Ri,&Ri,BN_value_one());\n#endif\n\t\tBN_div(&(mont->Ni),NULL,&Ri,mod,ctx);\n\t\tBN_free(&Ri);\n\t\t}\n#endif\n#if 1\n\tBN_zero(&(mont->RR));\n\tBN_set_bit(&(mont->RR),mont->ri*2);\n#else\n\tBN_lshift(mont->RR,BN_value_one(),mont->ri*2);\n#endif\n\tBN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx);\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tint i,n;\n\tif (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0);\n\tn=sizeof(BN_ULONG)/BN_BYTES;\n\ta->neg=0;\n\ta->top=0;\n\ta->d[0]=(BN_ULONG)w&BN_MASK2;\n\tif (a->d[0] != 0) a->top=1;\n\tfor (i=1; i<n; i++)\n\t\t{\n#ifndef SIXTY_FOUR_BIT\n\t\tw>>=BN_BITS4;\n\t\tw>>=BN_BITS4;\n#endif\n\t\ta->d[i]=(BN_ULONG)w&BN_MASK2;\n\t\tif (a->d[i] != 0) a->top=i+1;\n\t\t}\n\treturn(1);\n\t}', 'int BN_set_bit(BIGNUM *a, int n)\n\t{\n\tint i,j,k;\n\ti=n/BN_BITS2;\n\tj=n%BN_BITS2;\n\tif (a->top <= i)\n\t\t{\n\t\tif (bn_wexpand(a,i+1) == NULL) return(0);\n\t\tfor(k=a->top; k<i+1; k++)\n\t\t\ta->d[k]=0;\n\t\ta->top=i+1;\n\t\t}\n\ta->d[i]|=(1L<<j);\n\treturn(1);\n\t}', 'static int witness(BIGNUM *a, BIGNUM *n, BN_CTX *ctx, BN_CTX *ctx2,\n\t BN_MONT_CTX *mont)\n\t{\n\tint k,i,ret= -1,good;\n\tBIGNUM *d,*dd,*tmp,*d1,*d2,*n1;\n\tBIGNUM *mont_one,*mont_n1,*mont_a;\n\td1= &(ctx->bn[ctx->tos]);\n\td2= &(ctx->bn[ctx->tos+1]);\n\tn1= &(ctx->bn[ctx->tos+2]);\n\tctx->tos+=3;\n\tmont_one= &(ctx2->bn[ctx2->tos]);\n\tmont_n1= &(ctx2->bn[ctx2->tos+1]);\n\tmont_a= &(ctx2->bn[ctx2->tos+2]);\n\tctx2->tos+=3;\n\td=d1;\n\tdd=d2;\n\tif (!BN_one(d)) goto err;\n\tif (!BN_sub(n1,n,d)) goto err;\n\tk=BN_num_bits(n1);\n\tif (!BN_to_montgomery(mont_one,BN_value_one(),mont,ctx2)) goto err;\n\tif (!BN_to_montgomery(mont_n1,n1,mont,ctx2)) goto err;\n\tif (!BN_to_montgomery(mont_a,a,mont,ctx2)) goto err;\n\tBN_copy(d,mont_one);\n\tfor (i=k-1; i>=0; i--)\n\t\t{\n\t\tif (\t(BN_cmp(d,mont_one) != 0) &&\n\t\t\t(BN_cmp(d,mont_n1) != 0))\n\t\t\tgood=1;\n\t\telse\n\t\t\tgood=0;\n\t\tBN_mod_mul_montgomery(dd,d,d,mont,ctx2);\n\t\tif (good && (BN_cmp(dd,mont_one) == 0))\n\t\t\t{\n\t\t\tret=1;\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (BN_is_bit_set(n1,i))\n\t\t\t{\n\t\t\tBN_mod_mul_montgomery(d,dd,mont_a,mont,ctx2);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\ttmp=d;\n\t\t\td=dd;\n\t\t\tdd=tmp;\n\t\t\t}\n\t\t}\n\tif (BN_cmp(d,mont_one) == 0)\n\t\ti=0;\n\telse\ti=1;\n\tret=i;\nerr:\n\tctx->tos-=3;\n\tctx2->tos-=3;\n\treturn(ret);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_MONT_CTX *mont,\n\t BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp,*tmp2;\n tmp= &(ctx->bn[ctx->tos]);\n tmp2= &(ctx->bn[ctx->tos]);\n\tctx->tos+=2;\n\tbn_check_top(tmp);\n\tbn_check_top(tmp2);\n\tif (a == b)\n\t\t{\n#if 0\n\t\tbn_wexpand(tmp,a->top*2);\n\t\tbn_wexpand(tmp2,a->top*4);\n\t\tbn_sqr_recursive(tmp->d,a->d,a->top,tmp2->d);\n\t\ttmp->top=a->top*2;\n\t\tif (tmp->d[tmp->top-1] == 0)\n\t\t\ttmp->top--;\n#else\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n\tctx->tos-=2;\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}', 'void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,\n\t BN_ULONG *t)\n\t{\n\tint n=n2/2,c1,c2;\n\tunsigned int neg,zero;\n\tBN_ULONG ln,lo,*p;\n#ifdef BN_COUNT\nprintf(" bn_mul_recursive %d * %d\\n",n2,n2);\n#endif\n#ifdef BN_MUL_COMBA\n if (n2 == 8)\n\t\t{\n\t\tbn_mul_comba8(r,a,b);\n\t\treturn;\n\t\t}\n#endif\n\tif (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t{\n\t\tbn_mul_normal(r,a,n2,b,n2);\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_words(a,&(a[n]),n);\n\tc2=bn_cmp_words(&(b[n]),b,n);\n\tzero=neg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tbreak;\n\tcase -3:\n\t\tzero=1;\n\t\tbreak;\n\tcase -2:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\t\tzero=1;\n\t\tbreak;\n\tcase 2:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\t\tzero=1;\n\t\tbreak;\n\tcase 4:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tbreak;\n\t\t}\n#ifdef BN_MUL_COMBA\n\tif (n == 4)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,8*sizeof(BN_ULONG));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_comba4(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse if (n == 8)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,16*sizeof(BN_ULONG));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_comba8(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse\n#endif\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tif (!zero)\n\t\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,p);\n\t\telse\n\t\t\tmemset(&(t[n2]),0,n2*sizeof(BN_ULONG));\n\t\tbn_mul_recursive(r,a,b,n,p);\n\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,p);\n\t\t}\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tif (neg)\n\t\t{\n\t\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\t\t}\n\telse\n\t\t{\n\t\tc1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));\n\t\t}\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < (BN_ULONG)c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)\n\t{\n#ifdef BN_LLONG\n\tBN_ULLONG t;\n#else\n\tBN_ULONG bl,bh;\n#endif\n\tBN_ULONG t1,t2;\n\tBN_ULONG c1,c2,c3;\n\tc1=0;\n\tc2=0;\n\tc3=0;\n\tmul_add_c(a[0],b[0],c1,c2,c3);\n\tr[0]=c1;\n\tc1=0;\n\tmul_add_c(a[0],b[1],c2,c3,c1);\n\tmul_add_c(a[1],b[0],c2,c3,c1);\n\tr[1]=c2;\n\tc2=0;\n\tmul_add_c(a[2],b[0],c3,c1,c2);\n\tmul_add_c(a[1],b[1],c3,c1,c2);\n\tmul_add_c(a[0],b[2],c3,c1,c2);\n\tr[2]=c3;\n\tc3=0;\n\tmul_add_c(a[0],b[3],c1,c2,c3);\n\tmul_add_c(a[1],b[2],c1,c2,c3);\n\tmul_add_c(a[2],b[1],c1,c2,c3);\n\tmul_add_c(a[3],b[0],c1,c2,c3);\n\tr[3]=c1;\n\tc1=0;\n\tmul_add_c(a[4],b[0],c2,c3,c1);\n\tmul_add_c(a[3],b[1],c2,c3,c1);\n\tmul_add_c(a[2],b[2],c2,c3,c1);\n\tmul_add_c(a[1],b[3],c2,c3,c1);\n\tmul_add_c(a[0],b[4],c2,c3,c1);\n\tr[4]=c2;\n\tc2=0;\n\tmul_add_c(a[0],b[5],c3,c1,c2);\n\tmul_add_c(a[1],b[4],c3,c1,c2);\n\tmul_add_c(a[2],b[3],c3,c1,c2);\n\tmul_add_c(a[3],b[2],c3,c1,c2);\n\tmul_add_c(a[4],b[1],c3,c1,c2);\n\tmul_add_c(a[5],b[0],c3,c1,c2);\n\tr[5]=c3;\n\tc3=0;\n\tmul_add_c(a[6],b[0],c1,c2,c3);\n\tmul_add_c(a[5],b[1],c1,c2,c3);\n\tmul_add_c(a[4],b[2],c1,c2,c3);\n\tmul_add_c(a[3],b[3],c1,c2,c3);\n\tmul_add_c(a[2],b[4],c1,c2,c3);\n\tmul_add_c(a[1],b[5],c1,c2,c3);\n\tmul_add_c(a[0],b[6],c1,c2,c3);\n\tr[6]=c1;\n\tc1=0;\n\tmul_add_c(a[0],b[7],c2,c3,c1);\n\tmul_add_c(a[1],b[6],c2,c3,c1);\n\tmul_add_c(a[2],b[5],c2,c3,c1);\n\tmul_add_c(a[3],b[4],c2,c3,c1);\n\tmul_add_c(a[4],b[3],c2,c3,c1);\n\tmul_add_c(a[5],b[2],c2,c3,c1);\n\tmul_add_c(a[6],b[1],c2,c3,c1);\n\tmul_add_c(a[7],b[0],c2,c3,c1);\n\tr[7]=c2;\n\tc2=0;\n\tmul_add_c(a[7],b[1],c3,c1,c2);\n\tmul_add_c(a[6],b[2],c3,c1,c2);\n\tmul_add_c(a[5],b[3],c3,c1,c2);\n\tmul_add_c(a[4],b[4],c3,c1,c2);\n\tmul_add_c(a[3],b[5],c3,c1,c2);\n\tmul_add_c(a[2],b[6],c3,c1,c2);\n\tmul_add_c(a[1],b[7],c3,c1,c2);\n\tr[8]=c3;\n\tc3=0;\n\tmul_add_c(a[2],b[7],c1,c2,c3);\n\tmul_add_c(a[3],b[6],c1,c2,c3);\n\tmul_add_c(a[4],b[5],c1,c2,c3);\n\tmul_add_c(a[5],b[4],c1,c2,c3);\n\tmul_add_c(a[6],b[3],c1,c2,c3);\n\tmul_add_c(a[7],b[2],c1,c2,c3);\n\tr[9]=c1;\n\tc1=0;\n\tmul_add_c(a[7],b[3],c2,c3,c1);\n\tmul_add_c(a[6],b[4],c2,c3,c1);\n\tmul_add_c(a[5],b[5],c2,c3,c1);\n\tmul_add_c(a[4],b[6],c2,c3,c1);\n\tmul_add_c(a[3],b[7],c2,c3,c1);\n\tr[10]=c2;\n\tc2=0;\n\tmul_add_c(a[4],b[7],c3,c1,c2);\n\tmul_add_c(a[5],b[6],c3,c1,c2);\n\tmul_add_c(a[6],b[5],c3,c1,c2);\n\tmul_add_c(a[7],b[4],c3,c1,c2);\n\tr[11]=c3;\n\tc3=0;\n\tmul_add_c(a[7],b[5],c1,c2,c3);\n\tmul_add_c(a[6],b[6],c1,c2,c3);\n\tmul_add_c(a[5],b[7],c1,c2,c3);\n\tr[12]=c1;\n\tc1=0;\n\tmul_add_c(a[6],b[7],c2,c3,c1);\n\tmul_add_c(a[7],b[6],c2,c3,c1);\n\tr[13]=c2;\n\tc2=0;\n\tmul_add_c(a[7],b[7],c3,c1,c2);\n\tr[14]=c3;\n\tr[15]=c1;\n\t}']
35,860
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/eval.c/#L387
AVEvalExpr * ff_parse(const char *s, const char **const_name, double (**func1)(void *, double), const char **func1_name, double (**func2)(void *, double, double), char **func2_name, const char **error){ Parser p; AVEvalExpr * e; char w[strlen(s) + 1], * wp = w; while (*s) if (!isspace(*s++)) *wp++ = s[-1]; *wp++ = 0; p.stack_index=100; p.s= w; p.const_name = const_name; p.func1 = func1; p.func1_name = func1_name; p.func2 = func2; p.func2_name = func2_name; p.error= error; e = parse_expr(&p); if (!verify_expr(e)) { ff_eval_free(e); return NULL; } return e; }
['AVEvalExpr * ff_parse(const char *s, const char **const_name,\n double (**func1)(void *, double), const char **func1_name,\n double (**func2)(void *, double, double), char **func2_name,\n const char **error){\n Parser p;\n AVEvalExpr * e;\n char w[strlen(s) + 1], * wp = w;\n while (*s)\n if (!isspace(*s++)) *wp++ = s[-1];\n *wp++ = 0;\n p.stack_index=100;\n p.s= w;\n p.const_name = const_name;\n p.func1 = func1;\n p.func1_name = func1_name;\n p.func2 = func2;\n p.func2_name = func2_name;\n p.error= error;\n e = parse_expr(&p);\n if (!verify_expr(e)) {\n ff_eval_free(e);\n return NULL;\n }\n return e;\n}']
35,861
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/md5/md5_dgst.c/#L133
void MD5_Update(MD5_CTX *c, const void *_data, unsigned long len) { register const unsigned char *data=_data; register ULONG *p; int sw,sc; ULONG l; if (len == 0) return; l=(c->Nl+(len<<3))&0xffffffffL; if (l < c->Nl) c->Nh++; c->Nh+=(len>>29); c->Nl=l; if (c->num != 0) { p=c->data; sw=c->num>>2; sc=c->num&0x03; if ((c->num+len) >= MD5_CBLOCK) { l= p[sw]; p_c2l(data,l,sc); p[sw++]=l; for (; sw<MD5_LBLOCK; sw++) { c2l(data,l); p[sw]=l; } len-=(MD5_CBLOCK-c->num); md5_block(c,p,64); c->num=0; } else { int ew,ec; c->num+=(int)len; if ((sc+len) < 4) { l= p[sw]; p_c2l_p(data,l,sc,len); p[sw]=l; } else { ew=(c->num>>2); ec=(c->num&0x03); l= p[sw]; p_c2l(data,l,sc); p[sw++]=l; for (; sw < ew; sw++) { c2l(data,l); p[sw]=l; } if (ec) { c2l_p(data,l,ec); p[sw]=l; } } return; } } #ifdef L_ENDIAN if ((((unsigned long)data)%sizeof(ULONG)) == 0) { sw=(int)len/MD5_CBLOCK; if (sw > 0) { sw*=MD5_CBLOCK; md5_block(c,(ULONG *)data,sw); data+=sw; len-=sw; } } #endif p=c->data; while (len >= MD5_CBLOCK) { #if defined(L_ENDIAN) || defined(B_ENDIAN) if (p != (unsigned long *)data) memcpy(p,data,MD5_CBLOCK); data+=MD5_CBLOCK; #ifdef B_ENDIAN for (sw=(MD5_LBLOCK/4); sw; sw--) { Endian_Reverse32(p[0]); Endian_Reverse32(p[1]); Endian_Reverse32(p[2]); Endian_Reverse32(p[3]); p+=4; } #endif #else for (sw=(MD5_LBLOCK/4); sw; sw--) { c2l(data,l); *(p++)=l; c2l(data,l); *(p++)=l; c2l(data,l); *(p++)=l; c2l(data,l); *(p++)=l; } #endif p=c->data; md5_block(c,p,64); len-=MD5_CBLOCK; } sc=(int)len; c->num=sc; if (sc) { sw=sc>>2; #ifdef L_ENDIAN p[sw]=0; memcpy(p,data,sc); #else sc&=0x03; for ( ; sw; sw--) { c2l(data,l); *(p++)=l; } c2l_p(data,l,sc); *p=l; #endif } }
['static void ssleay_rand_bytes(unsigned char *buf, int num)\n\t{\n\tint i,j,k,st_num,st_idx;\n\tMD_CTX m;\n\tstatic int init=1;\n\tunsigned long l;\n#ifdef DEVRANDOM\n\tFILE *fh;\n#endif\n#ifdef PREDICT\n\t{\n\tstatic unsigned char val=0;\n\tfor (i=0; i<num; i++)\n\t\tbuf[i]=val++;\n\treturn;\n\t}\n#endif\n\tCRYPTO_w_lock(CRYPTO_LOCK_RAND);\n\tif (init)\n\t\t{\n\t\tinit=0;\n\t\tCRYPTO_w_unlock(CRYPTO_LOCK_RAND);\n\t\tRAND_seed(&m,sizeof(m));\n#ifndef MSDOS\n\t\tl=getpid();\n\t\tRAND_seed(&l,sizeof(l));\n\t\tl=getuid();\n\t\tRAND_seed(&l,sizeof(l));\n#endif\n\t\tl=time(NULL);\n\t\tRAND_seed(&l,sizeof(l));\n\t\tif ((fh = fopen(DEVRANDOM, "r")) != NULL)\n\t\t\t{\n\t\t\tunsigned char tmpbuf[32];\n\t\t\tfread((unsigned char *)tmpbuf,1,32,fh);\n\t\t\tfclose(fh);\n\t\t\tRAND_seed(tmpbuf,32);\n\t\t\tmemset(tmpbuf,0,32);\n\t\t\t}\n#ifdef PURIFY\n\t\tmemset(state,0,STATE_SIZE);\n\t\tmemset(md,0,MD_DIGEST_LENGTH);\n#endif\n\t\tCRYPTO_w_lock(CRYPTO_LOCK_RAND);\n\t\t}\n\tst_idx=state_index;\n\tst_num=state_num;\n\tstate_index+=num;\n\tif (state_index > state_num)\n\t\tstate_index=(state_index%state_num);\n\tCRYPTO_w_unlock(CRYPTO_LOCK_RAND);\n\twhile (num > 0)\n\t\t{\n\t\tj=(num >= MD_DIGEST_LENGTH/2)?MD_DIGEST_LENGTH/2:num;\n\t\tnum-=j;\n\t\tMD_Init(&m);\n\t\tMD_Update(&m,&(md[MD_DIGEST_LENGTH/2]),MD_DIGEST_LENGTH/2);\n\t\tMD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));\n#ifndef PURIFY\n\t\tMD_Update(&m,buf,j);\n#endif\n\t\tk=(st_idx+j)-st_num;\n\t\tif (k > 0)\n\t\t\t{\n\t\t\tMD_Update(&m,&(state[st_idx]),j-k);\n\t\t\tMD_Update(&m,&(state[0]),k);\n\t\t\t}\n\t\telse\n\t\t\tMD_Update(&m,&(state[st_idx]),j);\n\t\tMD_Final(md,&m);\n\t\tfor (i=0; i<j; i++)\n\t\t\t{\n\t\t\tif (st_idx >= st_num)\n\t\t\t\tst_idx=0;\n\t\t\tstate[st_idx++]^=md[i];\n\t\t\t*(buf++)=md[i+MD_DIGEST_LENGTH/2];\n\t\t\t}\n\t\t}\n\tMD_Init(&m);\n\tMD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));\n\tmd_count[0]++;\n\tMD_Update(&m,md,MD_DIGEST_LENGTH);\n\tMD_Final(md,&m);\n\tmemset(&m,0,sizeof(m));\n\t}', 'void MD5_Init(MD5_CTX *c)\n\t{\n\tc->A=INIT_DATA_A;\n\tc->B=INIT_DATA_B;\n\tc->C=INIT_DATA_C;\n\tc->D=INIT_DATA_D;\n\tc->Nl=0;\n\tc->Nh=0;\n\tc->num=0;\n\t}', 'void MD5_Update(MD5_CTX *c, const void *_data, unsigned long len)\n\t{\n\tregister const unsigned char *data=_data;\n\tregister ULONG *p;\n\tint sw,sc;\n\tULONG l;\n\tif (len == 0) return;\n\tl=(c->Nl+(len<<3))&0xffffffffL;\n\tif (l < c->Nl)\n\t\tc->Nh++;\n\tc->Nh+=(len>>29);\n\tc->Nl=l;\n\tif (c->num != 0)\n\t\t{\n\t\tp=c->data;\n\t\tsw=c->num>>2;\n\t\tsc=c->num&0x03;\n\t\tif ((c->num+len) >= MD5_CBLOCK)\n\t\t\t{\n\t\t\tl= p[sw];\n\t\t\tp_c2l(data,l,sc);\n\t\t\tp[sw++]=l;\n\t\t\tfor (; sw<MD5_LBLOCK; sw++)\n\t\t\t\t{\n\t\t\t\tc2l(data,l);\n\t\t\t\tp[sw]=l;\n\t\t\t\t}\n\t\t\tlen-=(MD5_CBLOCK-c->num);\n\t\t\tmd5_block(c,p,64);\n\t\t\tc->num=0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tint ew,ec;\n\t\t\tc->num+=(int)len;\n\t\t\tif ((sc+len) < 4)\n\t\t\t\t{\n\t\t\t\tl= p[sw];\n\t\t\t\tp_c2l_p(data,l,sc,len);\n\t\t\t\tp[sw]=l;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tew=(c->num>>2);\n\t\t\t\tec=(c->num&0x03);\n\t\t\t\tl= p[sw];\n\t\t\t\tp_c2l(data,l,sc);\n\t\t\t\tp[sw++]=l;\n\t\t\t\tfor (; sw < ew; sw++)\n\t\t\t\t\t{ c2l(data,l); p[sw]=l; }\n\t\t\t\tif (ec)\n\t\t\t\t\t{\n\t\t\t\t\tc2l_p(data,l,ec);\n\t\t\t\t\tp[sw]=l;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\treturn;\n\t\t\t}\n\t\t}\n#ifdef L_ENDIAN\n\tif ((((unsigned long)data)%sizeof(ULONG)) == 0)\n\t\t{\n\t\tsw=(int)len/MD5_CBLOCK;\n\t\tif (sw > 0)\n\t\t\t{\n\t\t\tsw*=MD5_CBLOCK;\n\t\t\tmd5_block(c,(ULONG *)data,sw);\n\t\t\tdata+=sw;\n\t\t\tlen-=sw;\n\t\t\t}\n\t\t}\n#endif\n\tp=c->data;\n\twhile (len >= MD5_CBLOCK)\n\t\t{\n#if defined(L_ENDIAN) || defined(B_ENDIAN)\n\t\tif (p != (unsigned long *)data)\n\t\t\tmemcpy(p,data,MD5_CBLOCK);\n\t\tdata+=MD5_CBLOCK;\n#ifdef B_ENDIAN\n\t\tfor (sw=(MD5_LBLOCK/4); sw; sw--)\n\t\t\t{\n\t\t\tEndian_Reverse32(p[0]);\n\t\t\tEndian_Reverse32(p[1]);\n\t\t\tEndian_Reverse32(p[2]);\n\t\t\tEndian_Reverse32(p[3]);\n\t\t\tp+=4;\n\t\t\t}\n#endif\n#else\n\t\tfor (sw=(MD5_LBLOCK/4); sw; sw--)\n\t\t\t{\n\t\t\tc2l(data,l); *(p++)=l;\n\t\t\tc2l(data,l); *(p++)=l;\n\t\t\tc2l(data,l); *(p++)=l;\n\t\t\tc2l(data,l); *(p++)=l;\n\t\t\t}\n#endif\n\t\tp=c->data;\n\t\tmd5_block(c,p,64);\n\t\tlen-=MD5_CBLOCK;\n\t\t}\n\tsc=(int)len;\n\tc->num=sc;\n\tif (sc)\n\t\t{\n\t\tsw=sc>>2;\n#ifdef L_ENDIAN\n\t\tp[sw]=0;\n\t\tmemcpy(p,data,sc);\n#else\n\t\tsc&=0x03;\n\t\tfor ( ; sw; sw--)\n\t\t\t{ c2l(data,l); *(p++)=l; }\n\t\tc2l_p(data,l,sc);\n\t\t*p=l;\n#endif\n\t\t}\n\t}']
35,862
0
https://github.com/openssl/openssl/blob/a21285b3636a8356f01027416b0cd43b016f58ca/crypto/bn/bn_ctx.c/#L276
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue, wmask, window0;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n top = m->top;\n bits = p->top * BN_BITS2;\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if (!a->neg) {\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!bn_to_mont_fixed_top(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(&am, a, m, ctx))\n goto err;\n if (!bn_to_mont_fixed_top(&am, &am, mont, ctx))\n goto err;\n } else if (!bn_to_mont_fixed_top(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits > 0) {\n if (bits < stride)\n stride = bits;\n bits -= stride;\n wvalue = bn_get_bits(p, bits);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7) {\n while (bits > 0) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n } else {\n while (bits > 0) {\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx);\n}', 'int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!bn_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
35,863
0
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_lib.c/#L668
int BN_ucmp(const BIGNUM *a, const BIGNUM *b) { int i; BN_ULONG t1,t2,*ap,*bp; bn_check_top(a); bn_check_top(b); i=a->top-b->top; if (i != 0) return(i); ap=a->d; bp=b->d; for (i=a->top-1; i>=0; i--) { t1= ap[i]; t2= bp[i]; if (t1 != t2) return((t1 > t2) ? 1 : -1); } return(0); }
['static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM local_A, local_B;\n\tBIGNUM *pA, *pB;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tpB = &local_B;\n\t\tBN_with_flags(pB, B, BN_FLG_CONSTTIME);\n\t\tif (!BN_nnmod(B, pB, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tBIGNUM *tmp;\n\t\tpA = &local_A;\n\t\tBN_with_flags(pA, A, BN_FLG_CONSTTIME);\n\t\tif (!BN_div(D,M,pA,B,ctx)) goto err;\n\t\ttmp=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=tmp;\n\t\tsign = -sign;\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n\t{\n\tint i;\n\tBN_ULONG *A;\n\tconst BN_ULONG *B;\n\tbn_check_top(b);\n\tif (a == b) return(a);\n\tif (bn_wexpand(a,b->top) == NULL) return(NULL);\n#if 1\n\tA=a->d;\n\tB=b->d;\n\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t{\n\t\tBN_ULONG a0,a1,a2,a3;\n\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t}\n\tswitch (b->top&3)\n\t\t{\n\t\tcase 3: A[2]=B[2];\n\t\tcase 2: A[1]=B[1];\n\t\tcase 1: A[0]=B[0];\n\t\tcase 0: ;\n\t\t}\n#else\n\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\ta->top=b->top;\n\ta->neg=b->neg;\n\tbn_check_top(a);\n\treturn(a);\n\t}', 'int BN_ucmp(const BIGNUM *a, const BIGNUM *b)\n\t{\n\tint i;\n\tBN_ULONG t1,t2,*ap,*bp;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\ti=a->top-b->top;\n\tif (i != 0) return(i);\n\tap=a->d;\n\tbp=b->d;\n\tfor (i=a->top-1; i>=0; i--)\n\t\t{\n\t\tt1= ap[i];\n\t\tt2= bp[i];\n\t\tif (t1 != t2)\n\t\t\treturn((t1 > t2) ? 1 : -1);\n\t\t}\n\treturn(0);\n\t}']
35,864
0
https://github.com/libav/libav/blob/dad7a9c7c0ae8ebc56f2e3a24e6fa4da5c2cd491/libavcodec/bitstream.h/#L139
static inline uint64_t get_val(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1); bc->bits >>= n; #else uint64_t ret = bc->bits >> (64 - n); bc->bits <<= n; #endif bc->bits_left -= n; return ret; }
['static int twinvq_read_bitstream(AVCodecContext *avctx, TwinVQContext *tctx,\n const uint8_t *buf, int buf_size)\n{\n TwinVQFrameData *bits = &tctx->bits[0];\n const TwinVQModeTab *mtab = tctx->mtab;\n int channels = tctx->avctx->channels;\n int sub;\n BitstreamContext bc;\n int i, j, k;\n bitstream_init8(&bc, buf, buf_size);\n bitstream_skip(&bc, bitstream_read(&bc, 8));\n bits->window_type = bitstream_read(&bc, TWINVQ_WINDOW_TYPE_BITS);\n if (bits->window_type > 8) {\n av_log(avctx, AV_LOG_ERROR, "Invalid window type, broken sample?\\n");\n return AVERROR_INVALIDDATA;\n }\n bits->ftype = ff_twinvq_wtype_to_ftype_table[tctx->bits[0].window_type];\n sub = mtab->fmode[bits->ftype].sub;\n read_cb_data(tctx, &bc, bits->main_coeffs, bits->ftype);\n for (i = 0; i < channels; i++)\n for (j = 0; j < sub; j++)\n for (k = 0; k < mtab->fmode[bits->ftype].bark_n_coef; k++)\n bits->bark1[i][j][k] =\n bitstream_read(&bc, mtab->fmode[bits->ftype].bark_n_bit);\n for (i = 0; i < channels; i++)\n for (j = 0; j < sub; j++)\n bits->bark_use_hist[i][j] = bitstream_read_bit(&bc);\n if (bits->ftype == TWINVQ_FT_LONG) {\n for (i = 0; i < channels; i++)\n bits->gain_bits[i] = bitstream_read(&bc, TWINVQ_GAIN_BITS);\n } else {\n for (i = 0; i < channels; i++) {\n bits->gain_bits[i] = bitstream_read(&bc, TWINVQ_GAIN_BITS);\n for (j = 0; j < sub; j++)\n bits->sub_gain_bits[i * sub + j] = bitstream_read(&bc, TWINVQ_SUB_GAIN_BITS);\n }\n }\n for (i = 0; i < channels; i++) {\n bits->lpc_hist_idx[i] = bitstream_read(&bc, mtab->lsp_bit0);\n bits->lpc_idx1[i] = bitstream_read(&bc, mtab->lsp_bit1);\n for (j = 0; j < mtab->lsp_split; j++)\n bits->lpc_idx2[i][j] = bitstream_read(&bc, mtab->lsp_bit2);\n }\n if (bits->ftype == TWINVQ_FT_LONG) {\n read_cb_data(tctx, &bc, bits->ppc_coeffs, 3);\n for (i = 0; i < channels; i++) {\n bits->p_coef[i] = bitstream_read(&bc, mtab->ppc_period_bit);\n bits->g_coef[i] = bitstream_read(&bc, mtab->pgain_bit);\n }\n }\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
35,865
0
https://github.com/libav/libav/blob/92fdc80cab2acad9f171ba38a08aa89b392bdadd/libavcodec/utils.c/#L1916
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag) { int i, len, ret = 0; #define TAG_PRINT(x) \ (((x) >= '0' && (x) <= '9') || \ ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \ ((x) == '.' || (x) == ' ')) for (i = 0; i < 4; i++) { len = snprintf(buf, buf_size, TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF); buf += len; buf_size = buf_size > len ? buf_size - len : 0; ret += len; codec_tag >>= 8; } return ret; }
['static int parse_pixel_format(AVCodecContext *avctx)\n{\n DDSContext *ctx = avctx->priv_data;\n GetByteContext *gbc = &ctx->gbc;\n char buf[32];\n uint32_t flags, fourcc, gimp_tag;\n enum DDSDXGIFormat dxgi;\n int size, bpp, r, g, b, a;\n int alpha_exponent, ycocg_classic, ycocg_scaled, normal_map, array;\n bytestream2_skip(gbc, 4 * 3);\n gimp_tag = bytestream2_get_le32(gbc);\n alpha_exponent = gimp_tag == MKTAG(\'A\', \'E\', \'X\', \'P\');\n ycocg_classic = gimp_tag == MKTAG(\'Y\', \'C\', \'G\', \'1\');\n ycocg_scaled = gimp_tag == MKTAG(\'Y\', \'C\', \'G\', \'2\');\n bytestream2_skip(gbc, 4 * 7);\n size = bytestream2_get_le32(gbc);\n if (size != 32) {\n av_log(avctx, AV_LOG_ERROR, "Invalid pixel format header %d.\\n", size);\n return AVERROR_INVALIDDATA;\n }\n flags = bytestream2_get_le32(gbc);\n ctx->compressed = flags & DDPF_FOURCC;\n ctx->paletted = flags & DDPF_PALETTE;\n normal_map = flags & DDPF_NORMALMAP;\n fourcc = bytestream2_get_le32(gbc);\n bpp = bytestream2_get_le32(gbc);\n r = bytestream2_get_le32(gbc);\n g = bytestream2_get_le32(gbc);\n b = bytestream2_get_le32(gbc);\n a = bytestream2_get_le32(gbc);\n bytestream2_skip(gbc, 4);\n bytestream2_skip(gbc, 4);\n bytestream2_skip(gbc, 4);\n bytestream2_skip(gbc, 4);\n bytestream2_skip(gbc, 4);\n av_get_codec_tag_string(buf, sizeof(buf), fourcc);\n av_log(avctx, AV_LOG_VERBOSE, "fourcc %s bpp %d "\n "r 0x%x g 0x%x b 0x%x a 0x%x\\n", buf, bpp, r, g, b, a);\n if (gimp_tag) {\n av_get_codec_tag_string(buf, sizeof(buf), gimp_tag);\n av_log(avctx, AV_LOG_VERBOSE, "and GIMP-DDS tag %s\\n", buf);\n }\n if (ctx->compressed)\n avctx->pix_fmt = AV_PIX_FMT_RGBA;\n if (ctx->compressed) {\n switch (fourcc) {\n case MKTAG(\'D\', \'X\', \'T\', \'1\'):\n ctx->tex_ratio = 8;\n ctx->tex_funct = ctx->texdsp.dxt1a_block;\n break;\n case MKTAG(\'D\', \'X\', \'T\', \'2\'):\n ctx->tex_ratio = 16;\n ctx->tex_funct = ctx->texdsp.dxt2_block;\n break;\n case MKTAG(\'D\', \'X\', \'T\', \'3\'):\n ctx->tex_ratio = 16;\n ctx->tex_funct = ctx->texdsp.dxt3_block;\n break;\n case MKTAG(\'D\', \'X\', \'T\', \'4\'):\n ctx->tex_ratio = 16;\n ctx->tex_funct = ctx->texdsp.dxt4_block;\n break;\n case MKTAG(\'D\', \'X\', \'T\', \'5\'):\n ctx->tex_ratio = 16;\n if (ycocg_scaled)\n ctx->tex_funct = ctx->texdsp.dxt5ys_block;\n else if (ycocg_classic)\n ctx->tex_funct = ctx->texdsp.dxt5y_block;\n else\n ctx->tex_funct = ctx->texdsp.dxt5_block;\n break;\n case MKTAG(\'R\', \'X\', \'G\', \'B\'):\n ctx->tex_ratio = 16;\n ctx->tex_funct = ctx->texdsp.dxt5_block;\n ctx->postproc = DDS_SWIZZLE_RXGB;\n normal_map = 0;\n break;\n case MKTAG(\'A\', \'T\', \'I\', \'1\'):\n case MKTAG(\'B\', \'C\', \'4\', \'U\'):\n ctx->tex_ratio = 8;\n ctx->tex_funct = ctx->texdsp.rgtc1u_block;\n break;\n case MKTAG(\'B\', \'C\', \'4\', \'S\'):\n ctx->tex_ratio = 8;\n ctx->tex_funct = ctx->texdsp.rgtc1s_block;\n break;\n case MKTAG(\'A\', \'T\', \'I\', \'2\'):\n ctx->tex_ratio = 16;\n ctx->tex_funct = ctx->texdsp.dxn3dc_block;\n break;\n case MKTAG(\'B\', \'C\', \'5\', \'U\'):\n ctx->tex_ratio = 16;\n ctx->tex_funct = ctx->texdsp.rgtc2u_block;\n break;\n case MKTAG(\'B\', \'C\', \'5\', \'S\'):\n ctx->tex_ratio = 16;\n ctx->tex_funct = ctx->texdsp.rgtc2s_block;\n break;\n case MKTAG(\'U\', \'Y\', \'V\', \'Y\'):\n ctx->compressed = 0;\n avctx->pix_fmt = AV_PIX_FMT_UYVY422;\n break;\n case MKTAG(\'Y\', \'U\', \'Y\', \'2\'):\n ctx->compressed = 0;\n avctx->pix_fmt = AV_PIX_FMT_YUYV422;\n break;\n case MKTAG(\'P\', \'8\', \' \', \' \'):\n ctx->compressed = 0;\n ctx->paletted = 1;\n avctx->pix_fmt = AV_PIX_FMT_PAL8;\n break;\n case MKTAG(\'D\', \'X\', \'1\', \'0\'):\n dxgi = bytestream2_get_le32(gbc);\n bytestream2_skip(gbc, 4);\n bytestream2_skip(gbc, 4);\n array = bytestream2_get_le32(gbc);\n bytestream2_skip(gbc, 4);\n if (array != 0)\n av_log(avctx, AV_LOG_VERBOSE,\n "Found array of size %d (ignored).\\n", array);\n ctx->compressed = (dxgi >= 70) && (dxgi <= 84);\n av_log(avctx, AV_LOG_VERBOSE, "DXGI format %d.\\n", dxgi);\n switch (dxgi) {\n case DXGI_FORMAT_R16G16B16A16_TYPELESS:\n case DXGI_FORMAT_R16G16B16A16_FLOAT:\n case DXGI_FORMAT_R16G16B16A16_UNORM:\n case DXGI_FORMAT_R16G16B16A16_UINT:\n case DXGI_FORMAT_R16G16B16A16_SNORM:\n case DXGI_FORMAT_R16G16B16A16_SINT:\n avctx->pix_fmt = AV_PIX_FMT_BGRA64;\n break;\n case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:\n avctx->colorspace = AVCOL_SPC_RGB;\n case DXGI_FORMAT_R8G8B8A8_TYPELESS:\n case DXGI_FORMAT_R8G8B8A8_UNORM:\n case DXGI_FORMAT_R8G8B8A8_UINT:\n case DXGI_FORMAT_R8G8B8A8_SNORM:\n case DXGI_FORMAT_R8G8B8A8_SINT:\n avctx->pix_fmt = AV_PIX_FMT_BGRA;\n break;\n case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:\n avctx->colorspace = AVCOL_SPC_RGB;\n case DXGI_FORMAT_B8G8R8A8_TYPELESS:\n case DXGI_FORMAT_B8G8R8A8_UNORM:\n avctx->pix_fmt = AV_PIX_FMT_RGBA;\n break;\n case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:\n avctx->colorspace = AVCOL_SPC_RGB;\n case DXGI_FORMAT_B8G8R8X8_TYPELESS:\n case DXGI_FORMAT_B8G8R8X8_UNORM:\n avctx->pix_fmt = AV_PIX_FMT_RGBA;\n break;\n case DXGI_FORMAT_B5G6R5_UNORM:\n avctx->pix_fmt = AV_PIX_FMT_RGB565LE;\n break;\n case DXGI_FORMAT_BC1_UNORM_SRGB:\n avctx->colorspace = AVCOL_SPC_RGB;\n case DXGI_FORMAT_BC1_TYPELESS:\n case DXGI_FORMAT_BC1_UNORM:\n ctx->tex_ratio = 8;\n ctx->tex_funct = ctx->texdsp.dxt1a_block;\n break;\n case DXGI_FORMAT_BC2_UNORM_SRGB:\n avctx->colorspace = AVCOL_SPC_RGB;\n case DXGI_FORMAT_BC2_TYPELESS:\n case DXGI_FORMAT_BC2_UNORM:\n ctx->tex_ratio = 16;\n ctx->tex_funct = ctx->texdsp.dxt3_block;\n break;\n case DXGI_FORMAT_BC3_UNORM_SRGB:\n avctx->colorspace = AVCOL_SPC_RGB;\n case DXGI_FORMAT_BC3_TYPELESS:\n case DXGI_FORMAT_BC3_UNORM:\n ctx->tex_ratio = 16;\n ctx->tex_funct = ctx->texdsp.dxt5_block;\n break;\n case DXGI_FORMAT_BC4_TYPELESS:\n case DXGI_FORMAT_BC4_UNORM:\n ctx->tex_ratio = 8;\n ctx->tex_funct = ctx->texdsp.rgtc1u_block;\n break;\n case DXGI_FORMAT_BC4_SNORM:\n ctx->tex_ratio = 8;\n ctx->tex_funct = ctx->texdsp.rgtc1s_block;\n break;\n case DXGI_FORMAT_BC5_TYPELESS:\n case DXGI_FORMAT_BC5_UNORM:\n ctx->tex_ratio = 16;\n ctx->tex_funct = ctx->texdsp.rgtc2u_block;\n break;\n case DXGI_FORMAT_BC5_SNORM:\n ctx->tex_ratio = 16;\n ctx->tex_funct = ctx->texdsp.rgtc2s_block;\n break;\n default:\n av_log(avctx, AV_LOG_ERROR,\n "Unsupported DXGI format %d.\\n", dxgi);\n return AVERROR_INVALIDDATA;\n }\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "Unsupported %s fourcc.\\n", buf);\n return AVERROR_INVALIDDATA;\n }\n } else if (ctx->paletted) {\n if (bpp == 8) {\n avctx->pix_fmt = AV_PIX_FMT_PAL8;\n } else {\n av_log(avctx, AV_LOG_ERROR, "Unsupported palette bpp %d.\\n", bpp);\n return AVERROR_INVALIDDATA;\n }\n } else {\n if (bpp == 8 && r == 0xff && g == 0 && b == 0 && a == 0)\n avctx->pix_fmt = AV_PIX_FMT_GRAY8;\n else if (bpp == 16 && r == 0xff && g == 0 && b == 0 && a == 0xff00)\n avctx->pix_fmt = AV_PIX_FMT_YA8;\n else if (bpp == 16 && r == 0xffff && g == 0 && b == 0 && a == 0)\n avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;\n else if (bpp == 16 && r == 0xf800 && g == 0x7e0 && b == 0x1f && a == 0)\n avctx->pix_fmt = AV_PIX_FMT_RGB565LE;\n else if (bpp == 24 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)\n avctx->pix_fmt = AV_PIX_FMT_BGR24;\n else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)\n avctx->pix_fmt = AV_PIX_FMT_RGBA;\n else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0)\n avctx->pix_fmt = AV_PIX_FMT_BGRA;\n else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0xff000000)\n avctx->pix_fmt = AV_PIX_FMT_RGBA;\n else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0xff000000)\n avctx->pix_fmt = AV_PIX_FMT_BGRA;\n else {\n av_log(avctx, AV_LOG_ERROR, "Unknown pixel format "\n "[bpp %d r 0x%x g 0x%x b 0x%x a 0x%x].\\n", bpp, r, g, b, a);\n return AVERROR_INVALIDDATA;\n }\n }\n if (alpha_exponent)\n ctx->postproc = DDS_ALPHA_EXP;\n else if (normal_map)\n ctx->postproc = DDS_NORMAL_MAP;\n else if (ycocg_classic && !ctx->compressed)\n ctx->postproc = DDS_RAW_YCOCG;\n else if (avctx->pix_fmt == AV_PIX_FMT_YA8)\n ctx->postproc = DDS_SWAP_ALPHA;\n switch (bpp) {\n case MKTAG(\'A\', \'2\', \'X\', \'Y\'):\n ctx->postproc = DDS_SWIZZLE_A2XY;\n break;\n case MKTAG(\'x\', \'G\', \'B\', \'R\'):\n ctx->postproc = DDS_SWIZZLE_XGBR;\n break;\n case MKTAG(\'x\', \'R\', \'B\', \'G\'):\n ctx->postproc = DDS_SWIZZLE_XRBG;\n break;\n case MKTAG(\'R\', \'B\', \'x\', \'G\'):\n ctx->postproc = DDS_SWIZZLE_RBXG;\n break;\n case MKTAG(\'R\', \'G\', \'x\', \'B\'):\n ctx->postproc = DDS_SWIZZLE_RGXB;\n break;\n case MKTAG(\'R\', \'x\', \'B\', \'G\'):\n ctx->postproc = DDS_SWIZZLE_RXBG;\n break;\n case MKTAG(\'x\', \'G\', \'x\', \'R\'):\n ctx->postproc = DDS_SWIZZLE_XGXR;\n break;\n case MKTAG(\'A\', \'2\', \'D\', \'5\'):\n ctx->postproc = DDS_NORMAL_MAP;\n break;\n }\n return 0;\n}', 'size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)\n{\n int i, len, ret = 0;\n#define TAG_PRINT(x) \\\n (((x) >= \'0\' && (x) <= \'9\') || \\\n ((x) >= \'a\' && (x) <= \'z\') || ((x) >= \'A\' && (x) <= \'Z\') || \\\n ((x) == \'.\' || (x) == \' \'))\n for (i = 0; i < 4; i++) {\n len = snprintf(buf, buf_size,\n TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);\n buf += len;\n buf_size = buf_size > len ? buf_size - len : 0;\n ret += len;\n codec_tag >>= 8;\n }\n return ret;\n}']
35,866
0
https://github.com/openssl/openssl/blob/2d5d70b15559f9813054ddb11b30b816daf62ebe/crypto/bn/bn_ctx.c/#L401
static void BN_POOL_release(BN_POOL *p, unsigned int num) { unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE; p->used -= num; while (num--) { bn_check_top(p->current->vals + offset); if (offset == 0) { offset = BN_CTX_POOL_SIZE - 1; p->current = p->current->prev; } else offset--; } }
['int ec_GFp_simple_group_set_curve(EC_GROUP *group,\n const BIGNUM *p, const BIGNUM *a,\n const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp_a;\n if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {\n ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD);\n return 0;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n tmp_a = BN_CTX_get(ctx);\n if (tmp_a == NULL)\n goto err;\n if (!BN_copy(group->field, p))\n goto err;\n BN_set_negative(group->field, 0);\n if (!BN_nnmod(tmp_a, a, p, ctx))\n goto err;\n if (group->meth->field_encode) {\n if (!group->meth->field_encode(group, group->a, tmp_a, ctx))\n goto err;\n } else if (!BN_copy(group->a, tmp_a))\n goto err;\n if (!BN_nnmod(group->b, b, p, ctx))\n goto err;\n if (group->meth->field_encode)\n if (!group->meth->field_encode(group, group->b, group->b, ctx))\n goto err;\n if (!BN_add_word(tmp_a, 3))\n goto err;\n group->a_is_minus3 = (0 == BN_cmp(tmp_a, group->field));\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static void BN_POOL_release(BN_POOL *p, unsigned int num)\n{\n unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;\n p->used -= num;\n while (num--) {\n bn_check_top(p->current->vals + offset);\n if (offset == 0) {\n offset = BN_CTX_POOL_SIZE - 1;\n p->current = p->current->prev;\n } else\n offset--;\n }\n}']
35,867
0
https://github.com/libav/libav/blob/a529fa2f4dd3292ba7d9cd3e6ba76425b1ba7aef/libavformat/smacker.c/#L318
static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt) { SmackerContext *smk = s->priv_data; int flags; int ret; int i; int frame_size = 0; int palchange = 0; if (s->pb->eof_reached || smk->cur_frame >= smk->frames) return AVERROR_EOF; if(smk->curstream < 0) { avio_seek(s->pb, smk->nextpos, 0); frame_size = smk->frm_size[smk->cur_frame] & (~3); flags = smk->frm_flags[smk->cur_frame]; if(flags & SMACKER_PAL){ int size, sz, t, off, j, pos; uint8_t *pal = smk->pal; uint8_t oldpal[768]; memcpy(oldpal, pal, 768); size = avio_r8(s->pb); size = size * 4 - 1; frame_size -= size; frame_size--; sz = 0; pos = avio_tell(s->pb) + size; while(sz < 256){ t = avio_r8(s->pb); if(t & 0x80){ sz += (t & 0x7F) + 1; pal += ((t & 0x7F) + 1) * 3; } else if(t & 0x40){ off = avio_r8(s->pb); j = (t & 0x3F) + 1; if (off + j > 0x100) { av_log(s, AV_LOG_ERROR, "Invalid palette update, offset=%d length=%d extends beyond palette size\n", off, j); return AVERROR_INVALIDDATA; } off *= 3; while(j-- && sz < 256) { *pal++ = oldpal[off + 0]; *pal++ = oldpal[off + 1]; *pal++ = oldpal[off + 2]; sz++; off += 3; } } else { *pal++ = smk_pal[t]; *pal++ = smk_pal[avio_r8(s->pb) & 0x3F]; *pal++ = smk_pal[avio_r8(s->pb) & 0x3F]; sz++; } } avio_seek(s->pb, pos, 0); palchange |= 1; } flags >>= 1; smk->curstream = -1; for(i = 0; i < 7; i++) { if(flags & 1) { uint32_t size; int err; size = avio_rl32(s->pb) - 4; if (!size || size > frame_size) { av_log(s, AV_LOG_ERROR, "Invalid audio part size\n"); return AVERROR_INVALIDDATA; } frame_size -= size; frame_size -= 4; smk->curstream++; if ((err = av_reallocp(&smk->bufs[smk->curstream], size)) < 0) { smk->buf_sizes[smk->curstream] = 0; return err; } smk->buf_sizes[smk->curstream] = size; ret = avio_read(s->pb, smk->bufs[smk->curstream], size); if(ret != size) return AVERROR(EIO); smk->stream_id[smk->curstream] = smk->indexes[i]; } flags >>= 1; } if (frame_size < 0 || frame_size >= INT_MAX/2) return AVERROR_INVALIDDATA; if (av_new_packet(pkt, frame_size + 769)) return AVERROR(ENOMEM); if(smk->frm_size[smk->cur_frame] & 1) palchange |= 2; pkt->data[0] = palchange; memcpy(pkt->data + 1, smk->pal, 768); ret = avio_read(s->pb, pkt->data + 769, frame_size); if(ret != frame_size) return AVERROR(EIO); pkt->stream_index = smk->videoindex; pkt->pts = smk->cur_frame; pkt->size = ret + 769; smk->cur_frame++; smk->nextpos = avio_tell(s->pb); } else { if (smk->stream_id[smk->curstream] < 0) return AVERROR_INVALIDDATA; if (av_new_packet(pkt, smk->buf_sizes[smk->curstream])) return AVERROR(ENOMEM); memcpy(pkt->data, smk->bufs[smk->curstream], smk->buf_sizes[smk->curstream]); pkt->size = smk->buf_sizes[smk->curstream]; pkt->stream_index = smk->stream_id[smk->curstream]; pkt->pts = smk->aud_pts[smk->curstream]; smk->aud_pts[smk->curstream] += AV_RL32(pkt->data); smk->curstream--; } return 0; }
['static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)\n{\n SmackerContext *smk = s->priv_data;\n int flags;\n int ret;\n int i;\n int frame_size = 0;\n int palchange = 0;\n if (s->pb->eof_reached || smk->cur_frame >= smk->frames)\n return AVERROR_EOF;\n if(smk->curstream < 0) {\n avio_seek(s->pb, smk->nextpos, 0);\n frame_size = smk->frm_size[smk->cur_frame] & (~3);\n flags = smk->frm_flags[smk->cur_frame];\n if(flags & SMACKER_PAL){\n int size, sz, t, off, j, pos;\n uint8_t *pal = smk->pal;\n uint8_t oldpal[768];\n memcpy(oldpal, pal, 768);\n size = avio_r8(s->pb);\n size = size * 4 - 1;\n frame_size -= size;\n frame_size--;\n sz = 0;\n pos = avio_tell(s->pb) + size;\n while(sz < 256){\n t = avio_r8(s->pb);\n if(t & 0x80){\n sz += (t & 0x7F) + 1;\n pal += ((t & 0x7F) + 1) * 3;\n } else if(t & 0x40){\n off = avio_r8(s->pb);\n j = (t & 0x3F) + 1;\n if (off + j > 0x100) {\n av_log(s, AV_LOG_ERROR,\n "Invalid palette update, offset=%d length=%d extends beyond palette size\\n",\n off, j);\n return AVERROR_INVALIDDATA;\n }\n off *= 3;\n while(j-- && sz < 256) {\n *pal++ = oldpal[off + 0];\n *pal++ = oldpal[off + 1];\n *pal++ = oldpal[off + 2];\n sz++;\n off += 3;\n }\n } else {\n *pal++ = smk_pal[t];\n *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];\n *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];\n sz++;\n }\n }\n avio_seek(s->pb, pos, 0);\n palchange |= 1;\n }\n flags >>= 1;\n smk->curstream = -1;\n for(i = 0; i < 7; i++) {\n if(flags & 1) {\n uint32_t size;\n int err;\n size = avio_rl32(s->pb) - 4;\n if (!size || size > frame_size) {\n av_log(s, AV_LOG_ERROR, "Invalid audio part size\\n");\n return AVERROR_INVALIDDATA;\n }\n frame_size -= size;\n frame_size -= 4;\n smk->curstream++;\n if ((err = av_reallocp(&smk->bufs[smk->curstream], size)) < 0) {\n smk->buf_sizes[smk->curstream] = 0;\n return err;\n }\n smk->buf_sizes[smk->curstream] = size;\n ret = avio_read(s->pb, smk->bufs[smk->curstream], size);\n if(ret != size)\n return AVERROR(EIO);\n smk->stream_id[smk->curstream] = smk->indexes[i];\n }\n flags >>= 1;\n }\n if (frame_size < 0 || frame_size >= INT_MAX/2)\n return AVERROR_INVALIDDATA;\n if (av_new_packet(pkt, frame_size + 769))\n return AVERROR(ENOMEM);\n if(smk->frm_size[smk->cur_frame] & 1)\n palchange |= 2;\n pkt->data[0] = palchange;\n memcpy(pkt->data + 1, smk->pal, 768);\n ret = avio_read(s->pb, pkt->data + 769, frame_size);\n if(ret != frame_size)\n return AVERROR(EIO);\n pkt->stream_index = smk->videoindex;\n pkt->pts = smk->cur_frame;\n pkt->size = ret + 769;\n smk->cur_frame++;\n smk->nextpos = avio_tell(s->pb);\n } else {\n if (smk->stream_id[smk->curstream] < 0)\n return AVERROR_INVALIDDATA;\n if (av_new_packet(pkt, smk->buf_sizes[smk->curstream]))\n return AVERROR(ENOMEM);\n memcpy(pkt->data, smk->bufs[smk->curstream], smk->buf_sizes[smk->curstream]);\n pkt->size = smk->buf_sizes[smk->curstream];\n pkt->stream_index = smk->stream_id[smk->curstream];\n pkt->pts = smk->aud_pts[smk->curstream];\n smk->aud_pts[smk->curstream] += AV_RL32(pkt->data);\n smk->curstream--;\n }\n return 0;\n}', 'unsigned int avio_rl32(AVIOContext *s)\n{\n unsigned int val;\n val = avio_rl16(s);\n val |= avio_rl16(s) << 16;\n return val;\n}', 'unsigned int avio_rl16(AVIOContext *s)\n{\n unsigned int val;\n val = avio_r8(s);\n val |= avio_r8(s) << 8;\n return val;\n}', 'int avio_r8(AVIOContext *s)\n{\n if (s->buf_ptr >= s->buf_end)\n fill_buffer(s);\n if (s->buf_ptr < s->buf_end)\n return *s->buf_ptr++;\n return 0;\n}', 'int av_reallocp(void *ptr, size_t size)\n{\n void **ptrptr = ptr;\n void *ret;\n if (!size) {\n av_freep(ptr);\n return 0;\n }\n ret = av_realloc(*ptrptr, size);\n if (!ret) {\n av_freep(ptr);\n return AVERROR(ENOMEM);\n }\n *ptrptr = ret;\n return 0;\n}', 'void *av_realloc(void *ptr, size_t size)\n{\n#if CONFIG_MEMALIGN_HACK\n int diff;\n#endif\n if (size > (INT_MAX - 16))\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n if (!ptr)\n return av_malloc(size);\n diff = ((char *)ptr)[-1];\n return (char *)realloc((char *)ptr - diff, size + diff) + diff;\n#elif HAVE_ALIGNED_MALLOC\n return _aligned_realloc(ptr, size, 32);\n#else\n return realloc(ptr, size);\n#endif\n}']
35,868
0
https://github.com/openssl/openssl/blob/09977dd095f3c655c99b9e1810a213f7eafa7364/crypto/bn/bn_lib.c/#L342
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *A, *a = NULL; const BN_ULONG *B; int i; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b,BN_FLG_SECURE)) a = A = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = A = OPENSSL_zalloc(words * sizeof(*a)); if (A == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } #if 1 B = b->d; if (B != NULL) { for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { BN_ULONG a0, a1, a2, a3; a0 = B[0]; a1 = B[1]; a2 = B[2]; a3 = B[3]; A[0] = a0; A[1] = a1; A[2] = a2; A[3] = a3; } switch (b->top & 3) { case 3: A[2] = B[2]; case 2: A[1] = B[1]; case 1: A[0] = B[0]; case 0: ; } } #else memset(A, 0, sizeof(*A) * words); memcpy(A, b->d, sizeof(b->d[0]) * b->top); #endif return (a); }
['static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,\n unsigned char *to, RSA *rsa, int padding)\n{\n BIGNUM *f, *ret;\n int i, num = 0, r = -1;\n unsigned char *p;\n unsigned char *buf = NULL;\n BN_CTX *ctx = NULL;\n if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);\n return -1;\n }\n if (BN_ucmp(rsa->n, rsa->e) <= 0) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);\n return -1;\n }\n if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {\n if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);\n return -1;\n }\n }\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n f = BN_CTX_get(ctx);\n ret = BN_CTX_get(ctx);\n num = BN_num_bytes(rsa->n);\n buf = OPENSSL_malloc(num);\n if (f == NULL || ret == NULL || buf == NULL) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (flen > num) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);\n goto err;\n }\n if (BN_bin2bn(from, flen, f) == NULL)\n goto err;\n if (BN_ucmp(f, rsa->n) >= 0) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT,\n RSA_R_DATA_TOO_LARGE_FOR_MODULUS);\n goto err;\n }\n if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n if (!BN_MONT_CTX_set_locked\n (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))\n goto err;\n if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))\n if (!BN_sub(ret, rsa->n, ret))\n goto err;\n p = buf;\n i = BN_bn2bin(ret, p);\n switch (padding) {\n case RSA_PKCS1_PADDING:\n r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);\n break;\n case RSA_X931_PADDING:\n r = RSA_padding_check_X931(to, num, buf, i, num);\n break;\n case RSA_NO_PADDING:\n r = RSA_padding_check_none(to, num, buf, i, num);\n break;\n default:\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);\n goto err;\n }\n if (r < 0)\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);\n err:\n if (ctx != NULL)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n OPENSSL_clear_free(buf, num);\n return (r);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if(((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = A = OPENSSL_zalloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
35,869
0
https://github.com/openssl/openssl/blob/313fce7b61ecaf5879cf84b256bdd0964134836e/crypto/bn/bn_ctx.c/#L440
static void BN_POOL_release(BN_POOL *p, unsigned int num) { unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE; p->used -= num; while(num--) { bn_check_top(p->current->vals + offset); if(!offset) { offset = BN_CTX_POOL_SIZE - 1; p->current = p->current->prev; } else offset--; } }
['static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,\n\t\tBIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,\n\t\tBN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tBIGNUM t;\n\tint to_return = 0;\n\tBN_init(&t);\n\tif (!atalla_mod_exp(rr,a1,p1,m,ctx)) goto end;\n\tif (!atalla_mod_exp(&t,a2,p2,m,ctx)) goto end;\n\tif (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;\n\tto_return = 1;\nend:\n\tBN_free(&t);\n\treturn to_return;\n\t}', 'static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n\t\t\tconst BIGNUM *m, BN_CTX *ctx)\n\t{\n\tBIGNUM *modulus;\n\tBIGNUM *exponent;\n\tBIGNUM *argument;\n\tBIGNUM *result;\n\tRSAPrivateKey keydata;\n\tint to_return, numbytes;\n\tmodulus = exponent = argument = result = NULL;\n\tto_return = 0;\n\tif(!atalla_dso)\n\t\t{\n\t\tATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_NOT_LOADED);\n\t\tgoto err;\n\t\t}\n\tBN_CTX_start(ctx);\n\tmodulus = BN_CTX_get(ctx);\n\texponent = BN_CTX_get(ctx);\n\targument = BN_CTX_get(ctx);\n\tresult = BN_CTX_get(ctx);\n\tif (!result)\n\t\t{\n\t\tATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_CTX_FULL);\n\t\tgoto err;\n\t\t}\n\tif(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||\n\t !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top))\n\t\t{\n\t\tATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_EXPAND_FAIL);\n\t\tgoto err;\n\t\t}\n\tmemset(&keydata, 0,sizeof keydata);\n\tnumbytes = BN_num_bytes(m);\n\tmemset(exponent->d, 0, numbytes);\n\tmemset(modulus->d, 0, numbytes);\n\tBN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));\n\tBN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));\n\tkeydata.privateExponent.data = (unsigned char *)exponent->d;\n\tkeydata.privateExponent.len = numbytes;\n\tkeydata.modulus.data = (unsigned char *)modulus->d;\n\tkeydata.modulus.len = numbytes;\n\tmemset(argument->d, 0, numbytes);\n\tmemset(result->d, 0, numbytes);\n\tBN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));\n\tif(p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,\n\t\t\t(unsigned char *)argument->d,\n\t\t\tkeydata.modulus.len) != 0)\n\t\t{\n\t\tATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_REQUEST_FAILED);\n\t\tgoto err;\n\t\t}\n\tBN_bin2bn((unsigned char *)result->d, numbytes, r);\n\tto_return = 1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn to_return;\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n\tBN_CTX *ctx)\n\t{\n\tBIGNUM *t;\n\tint ret=0;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(m);\n\tBN_CTX_start(ctx);\n\tif ((t = BN_CTX_get(ctx)) == NULL) goto err;\n\tif (a == b)\n\t\t{ if (!BN_sqr(t,a,ctx)) goto err; }\n\telse\n\t\t{ if (!BN_mul(t,a,b,ctx)) goto err; }\n\tif (!BN_nnmod(r,t,m,ctx)) goto err;\n\tbn_check_top(r);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n\t{\n\tBIGNUM *ret;\n\tCTXDBG_ENTRY("BN_CTX_get", ctx);\n\tif(ctx->err_stack || ctx->too_many) return NULL;\n\tif((ret = BN_POOL_get(&ctx->pool)) == NULL)\n\t\t{\n\t\tctx->too_many = 1;\n\t\tBNerr(BN_F_BN_CTX_GET,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\treturn NULL;\n\t\t}\n\tBN_zero(ret);\n\tctx->used++;\n\tCTXDBG_RET(ctx, ret);\n\treturn ret;\n\t}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n\t{\n\tint max,al;\n\tint ret = 0;\n\tBIGNUM *tmp,*rr;\n#ifdef BN_COUNT\n\tfprintf(stderr,"BN_sqr %d * %d\\n",a->top,a->top);\n#endif\n\tbn_check_top(a);\n\tal=a->top;\n\tif (al <= 0)\n\t\t{\n\t\tr->top=0;\n\t\treturn 1;\n\t\t}\n\tBN_CTX_start(ctx);\n\trr=(a != r) ? r : BN_CTX_get(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tif (!rr || !tmp) goto err;\n\tmax = 2 * al;\n\tif (bn_wexpand(rr,max) == NULL) goto err;\n\tif (al == 4)\n\t\t{\n#ifndef BN_SQR_COMBA\n\t\tBN_ULONG t[8];\n\t\tbn_sqr_normal(rr->d,a->d,4,t);\n#else\n\t\tbn_sqr_comba4(rr->d,a->d);\n#endif\n\t\t}\n\telse if (al == 8)\n\t\t{\n#ifndef BN_SQR_COMBA\n\t\tBN_ULONG t[16];\n\t\tbn_sqr_normal(rr->d,a->d,8,t);\n#else\n\t\tbn_sqr_comba8(rr->d,a->d);\n#endif\n\t\t}\n\telse\n\t\t{\n#if defined(BN_RECURSION)\n\t\tif (al < BN_SQR_RECURSIVE_SIZE_NORMAL)\n\t\t\t{\n\t\t\tBN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL*2];\n\t\t\tbn_sqr_normal(rr->d,a->d,al,t);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tint j,k;\n\t\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\t\tj=1<<(j-1);\n\t\t\tk=j+j;\n\t\t\tif (al == j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(tmp,k*2) == NULL) goto err;\n\t\t\t\tbn_sqr_recursive(rr->d,a->d,al,tmp->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(tmp,max) == NULL) goto err;\n\t\t\t\tbn_sqr_normal(rr->d,a->d,al,tmp->d);\n\t\t\t\t}\n\t\t\t}\n#else\n\t\tif (bn_wexpand(tmp,max) == NULL) goto err;\n\t\tbn_sqr_normal(rr->d,a->d,al,tmp->d);\n#endif\n\t\t}\n\trr->neg=0;\n\tif(a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n\t\trr->top = max - 1;\n\telse\n\t\trr->top = max;\n\tif (rr != r) BN_copy(r,rr);\n\tret = 1;\n err:\n\tbn_check_top(rr);\n\tbn_check_top(tmp);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'static void BN_POOL_release(BN_POOL *p, unsigned int num)\n\t{\n\tunsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;\n\tp->used -= num;\n\twhile(num--)\n\t\t{\n\t\tbn_check_top(p->current->vals + offset);\n\t\tif(!offset)\n\t\t\t{\n\t\t\toffset = BN_CTX_POOL_SIZE - 1;\n\t\t\tp->current = p->current->prev;\n\t\t\t}\n\t\telse\n\t\t\toffset--;\n\t\t}\n\t}']
35,870
0
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/lhash/lhash.c/#L164
static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg, OPENSSL_LH_DOALL_FUNC func, OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg) { int i; OPENSSL_LH_NODE *a, *n; if (lh == NULL) return; for (i = lh->num_nodes - 1; i >= 0; i--) { a = lh->b[i]; while (a != NULL) { n = a->next; if (use_arg) func_arg(a->data, arg); else func(a->data); a = n; } } }
['static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)\n{\n BIO *conn;\n SSL *serverCon;\n int width, i;\n fd_set readfds;\n if ((conn = BIO_new(BIO_s_connect())) == NULL)\n return (NULL);\n BIO_set_conn_hostname(conn, host);\n if (scon == NULL)\n serverCon = SSL_new(ctx);\n else {\n serverCon = scon;\n SSL_set_connect_state(serverCon);\n }\n SSL_set_bio(serverCon, conn, conn);\n for (;;) {\n i = SSL_connect(serverCon);\n if (BIO_sock_should_retry(i)) {\n BIO_printf(bio_err, "DELAY\\n");\n i = SSL_get_fd(serverCon);\n width = i + 1;\n FD_ZERO(&readfds);\n openssl_fdset(i, &readfds);\n select(width, (void *)&readfds, NULL, NULL, NULL);\n continue;\n }\n break;\n }\n if (i <= 0) {\n BIO_printf(bio_err, "ERROR\\n");\n if (verify_error != X509_V_OK)\n BIO_printf(bio_err, "verify error:%s\\n",\n X509_verify_cert_error_string(verify_error));\n else\n ERR_print_errors(bio_err);\n if (scon == NULL)\n SSL_free(serverCon);\n return NULL;\n }\n return serverCon;\n}', 'SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return (NULL);\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return (NULL);\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(s);\n return NULL;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->references = 1;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->sid_ctx_length = ctx->sid_ctx_length;\n OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);\n memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));\n s->verify_callback = ctx->default_verify_callback;\n s->generate_session_id = ctx->generate_session_id;\n s->param = X509_VERIFY_PARAM_new();\n if (s->param == NULL)\n goto err;\n X509_VERIFY_PARAM_inherit(s->param, ctx->param);\n s->quiet_shutdown = ctx->quiet_shutdown;\n s->max_send_fragment = ctx->max_send_fragment;\n s->split_send_fragment = ctx->split_send_fragment;\n s->max_pipelines = ctx->max_pipelines;\n if (s->max_pipelines > 1)\n RECORD_LAYER_set_read_ahead(&s->rlayer, 1);\n if (ctx->default_read_buf_len > 0)\n SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);\n SSL_CTX_up_ref(ctx);\n s->ctx = ctx;\n s->tlsext_debug_cb = 0;\n s->tlsext_debug_arg = NULL;\n s->tlsext_ticket_expected = 0;\n s->tlsext_status_type = ctx->tlsext_status_type;\n s->tlsext_status_expected = 0;\n s->tlsext_ocsp_ids = NULL;\n s->tlsext_ocsp_exts = NULL;\n s->tlsext_ocsp_resp = NULL;\n s->tlsext_ocsp_resplen = -1;\n SSL_CTX_up_ref(ctx);\n s->initial_ctx = ctx;\n# ifndef OPENSSL_NO_EC\n if (ctx->tlsext_ecpointformatlist) {\n s->tlsext_ecpointformatlist =\n OPENSSL_memdup(ctx->tlsext_ecpointformatlist,\n ctx->tlsext_ecpointformatlist_length);\n if (!s->tlsext_ecpointformatlist)\n goto err;\n s->tlsext_ecpointformatlist_length =\n ctx->tlsext_ecpointformatlist_length;\n }\n if (ctx->tlsext_ellipticcurvelist) {\n s->tlsext_ellipticcurvelist =\n OPENSSL_memdup(ctx->tlsext_ellipticcurvelist,\n ctx->tlsext_ellipticcurvelist_length);\n if (!s->tlsext_ellipticcurvelist)\n goto err;\n s->tlsext_ellipticcurvelist_length =\n ctx->tlsext_ellipticcurvelist_length;\n }\n# endif\n# ifndef OPENSSL_NO_NEXTPROTONEG\n s->next_proto_negotiated = NULL;\n# endif\n if (s->ctx->alpn_client_proto_list) {\n s->alpn_client_proto_list =\n OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);\n if (s->alpn_client_proto_list == NULL)\n goto err;\n memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,\n s->ctx->alpn_client_proto_list_len);\n s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;\n }\n s->verified_chain = NULL;\n s->verify_result = X509_V_OK;\n s->default_passwd_callback = ctx->default_passwd_callback;\n s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;\n s->method = ctx->method;\n if (!s->method->ssl_new(s))\n goto err;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))\n goto err;\n#ifndef OPENSSL_NO_PSK\n s->psk_client_callback = ctx->psk_client_callback;\n s->psk_server_callback = ctx->psk_server_callback;\n#endif\n s->job = NULL;\n#ifndef OPENSSL_NO_CT\n if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,\n ctx->ct_validation_callback_arg))\n goto err;\n#endif\n return s;\n err:\n SSL_free(s);\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n}', 'void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n CRYPTO_atomic_add(&s->references, -1, &i, s->lock);\n REF_PRINT_COUNT("SSL", s);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(s->param);\n dane_final(&s->dane);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n if (s->bbio != NULL) {\n if (s->bbio == s->wbio) {\n s->wbio = BIO_pop(s->wbio);\n }\n BIO_free(s->bbio);\n s->bbio = NULL;\n }\n BIO_free_all(s->rbio);\n if (s->wbio != s->rbio)\n BIO_free_all(s->wbio);\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->tlsext_hostname);\n SSL_CTX_free(s->initial_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->tlsext_ecpointformatlist);\n OPENSSL_free(s->tlsext_ellipticcurvelist);\n#endif\n sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->tlsext_scts);\n#endif\n OPENSSL_free(s->tlsext_ocsp_resp);\n OPENSSL_free(s->alpn_client_proto_list);\n sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);\n sk_X509_pop_free(s->verified_chain, X509_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n RECORD_LAYER_release(&s->rlayer);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->next_proto_negotiated);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', 'void SSL_CTX_free(SSL_CTX *a)\n{\n int i;\n if (a == NULL)\n return;\n CRYPTO_atomic_add(&a->references, -1, &i, a->lock);\n REF_PRINT_COUNT("SSL_CTX", a);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(a->param);\n dane_ctx_final(&a->dane);\n if (a->sessions != NULL)\n SSL_CTX_flush_sessions(a, 0);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);\n lh_SSL_SESSION_free(a->sessions);\n X509_STORE_free(a->cert_store);\n#ifndef OPENSSL_NO_CT\n CTLOG_STORE_free(a->ctlog_store);\n#endif\n sk_SSL_CIPHER_free(a->cipher_list);\n sk_SSL_CIPHER_free(a->cipher_list_by_id);\n ssl_cert_free(a->cert);\n sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);\n sk_X509_pop_free(a->extra_certs, X509_free);\n a->comp_methods = NULL;\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);\n#endif\n#ifndef OPENSSL_NO_SRP\n SSL_CTX_SRP_CTX_free(a);\n#endif\n#ifndef OPENSSL_NO_ENGINE\n ENGINE_finish(a->client_cert_engine);\n#endif\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(a->tlsext_ecpointformatlist);\n OPENSSL_free(a->tlsext_ellipticcurvelist);\n#endif\n OPENSSL_free(a->alpn_client_proto_list);\n CRYPTO_THREAD_lock_free(a->lock);\n OPENSSL_free(a);\n}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n{\n unsigned long i;\n TIMEOUT_PARAM tp;\n tp.ctx = s;\n tp.cache = s->sessions;\n if (tp.cache == NULL)\n return;\n tp.time = t;\n CRYPTO_THREAD_write_lock(s->lock);\n i = lh_SSL_SESSION_get_down_load(s->sessions);\n lh_SSL_SESSION_set_down_load(s->sessions, 0);\n lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);\n lh_SSL_SESSION_set_down_load(s->sessions, i);\n CRYPTO_THREAD_unlock(s->lock);\n}', 'IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM)', 'void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)\n{\n doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);\n}', 'static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,\n OPENSSL_LH_DOALL_FUNC func,\n OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)\n{\n int i;\n OPENSSL_LH_NODE *a, *n;\n if (lh == NULL)\n return;\n for (i = lh->num_nodes - 1; i >= 0; i--) {\n a = lh->b[i];\n while (a != NULL) {\n n = a->next;\n if (use_arg)\n func_arg(a->data, arg);\n else\n func(a->data);\n a = n;\n }\n }\n}']
35,871
0
https://github.com/openssl/openssl/blob/6a2da30347d81245dd4841833808621d189a9629/crypto/rand/rand_lib.c/#L391
int RAND_pseudo_bytes(unsigned char *buf, int num) { const RAND_METHOD *meth = RAND_get_rand_method(); if (meth->pseudorand != NULL) return meth->pseudorand(buf, num); return -1; }
['int RAND_pseudo_bytes(unsigned char *buf, int num)\n{\n const RAND_METHOD *meth = RAND_get_rand_method();\n if (meth->pseudorand != NULL)\n return meth->pseudorand(buf, num);\n return -1;\n}', 'const RAND_METHOD *RAND_get_rand_method(void)\n{\n const RAND_METHOD *tmp_meth = NULL;\n if (!RUN_ONCE(&rand_init, do_rand_init))\n return NULL;\n CRYPTO_THREAD_write_lock(rand_meth_lock);\n if (default_RAND_meth == NULL) {\n#ifndef OPENSSL_NO_ENGINE\n ENGINE *e;\n if ((e = ENGINE_get_default_RAND()) != NULL\n && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {\n funct_ref = e;\n default_RAND_meth = tmp_meth;\n } else {\n ENGINE_finish(e);\n default_RAND_meth = &rand_meth;\n }\n#else\n default_RAND_meth = &rand_meth;\n#endif\n }\n tmp_meth = default_RAND_meth;\n CRYPTO_THREAD_unlock(rand_meth_lock);\n return tmp_meth;\n}', 'int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))\n{\n if (pthread_once(once, init) != 0)\n return 0;\n return 1;\n}']
35,872
0
https://github.com/openssl/openssl/blob/404fb7149effa40d04625272424a28ff25c0e673/crypto/rsa/rsa_saos.c/#L140
int RSA_verify_ASN1_OCTET_STRING(int dtype, unsigned char *m, unsigned int m_len, unsigned char *sigbuf, unsigned int siglen, RSA *rsa) { int i,ret=0; unsigned char *p,*s; ASN1_OCTET_STRING *sig=NULL; if (siglen != (unsigned int)RSA_size(rsa)) { RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_WRONG_SIGNATURE_LENGTH); return(0); } s=(unsigned char *)Malloc((unsigned int)siglen); if (s == NULL) { RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE); goto err; } i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING); if (i <= 0) goto err; p=s; sig=d2i_ASN1_OCTET_STRING(NULL,&p,(long)i); if (sig == NULL) goto err; if ( ((unsigned int)sig->length != m_len) || (memcmp(m,sig->data,m_len) != 0)) { RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_BAD_SIGNATURE); } else ret=1; err: if (sig != NULL) M_ASN1_OCTET_STRING_free(sig); memset(s,0,(unsigned int)siglen); Free(s); return(ret); }
['int RSA_verify_ASN1_OCTET_STRING(int dtype, unsigned char *m,\n\t unsigned int m_len, unsigned char *sigbuf, unsigned int siglen,\n\t RSA *rsa)\n\t{\n\tint i,ret=0;\n\tunsigned char *p,*s;\n\tASN1_OCTET_STRING *sig=NULL;\n\tif (siglen != (unsigned int)RSA_size(rsa))\n\t\t{\n\t\tRSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_WRONG_SIGNATURE_LENGTH);\n\t\treturn(0);\n\t\t}\n\ts=(unsigned char *)Malloc((unsigned int)siglen);\n\tif (s == NULL)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\ti=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);\n\tif (i <= 0) goto err;\n\tp=s;\n\tsig=d2i_ASN1_OCTET_STRING(NULL,&p,(long)i);\n\tif (sig == NULL) goto err;\n\tif (\t((unsigned int)sig->length != m_len) ||\n\t\t(memcmp(m,sig->data,m_len) != 0))\n\t\t{\n\t\tRSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_BAD_SIGNATURE);\n\t\t}\n\telse\n\t\tret=1;\nerr:\n\tif (sig != NULL) M_ASN1_OCTET_STRING_free(sig);\n\tmemset(s,0,(unsigned int)siglen);\n\tFree(s);\n\treturn(ret);\n\t}', 'int RSA_size(RSA *r)\n\t{\n\treturn(BN_num_bytes(r->n));\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tBN_ULONG l;\n\tint i;\n\tbn_check_top(a);\n\tif (a->top == 0) return(0);\n\tl=a->d[a->top-1];\n\ti=(a->top-1)*BN_BITS2;\n\tif (l == 0)\n\t\t{\n#if !defined(NO_STDIO) && !defined(WIN16)\n\t\tfprintf(stderr,"BAD TOP VALUE\\n");\n#endif\n\t\tabort();\n\t\t}\n\treturn(i+BN_num_bits_word(l));\n\t}', 'void *CRYPTO_malloc(int num, char *file, int line)\n\t{\n\tchar *ret = NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_func(num);\n#ifdef LEVITTE_DEBUG\n\tfprintf(stderr, "LEVITTE_DEBUG: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'void CRYPTO_free(void *str)\n\t{\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(str, 0);\n#ifdef LEVITTE_DEBUG\n\tfprintf(stderr, "LEVITTE_DEBUG: < 0x%p\\n", str);\n#endif\n\tfree_func(str);\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(NULL, 1);\n\t}']
35,873
0
https://github.com/libav/libav/blob/587874ef1c94a9b863d2f2db0e5d341e086ee232/libavformat/mxfenc.c/#L1213
static void mxf_write_partition(AVFormatContext *s, int bodysid, int indexsid, const uint8_t *key, int write_metadata) { MXFContext *mxf = s->priv_data; AVIOContext *pb = s->pb; int64_t header_byte_count_offset; unsigned index_byte_count = 0; uint64_t partition_offset = avio_tell(pb); if (!mxf->edit_unit_byte_count && mxf->edit_units_count) index_byte_count = 85 + 12+(s->nb_streams+1)*6 + 12+mxf->edit_units_count*(11+mxf->slice_count*4); else if (mxf->edit_unit_byte_count && indexsid) index_byte_count = 80; if (index_byte_count) { index_byte_count += 16 + klv_ber_length(index_byte_count); index_byte_count += klv_fill_size(index_byte_count); } if (!memcmp(key, body_partition_key, 16)) { mxf->body_partition_offset = av_realloc(mxf->body_partition_offset, (mxf->body_partitions_count+1)* sizeof(*mxf->body_partition_offset)); mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset; } avio_write(pb, key, 16); klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count); avio_wb16(pb, 1); avio_wb16(pb, 2); avio_wb32(pb, KAG_SIZE); avio_wb64(pb, partition_offset); if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1) avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count) avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); else avio_wb64(pb, 0); avio_wb64(pb, mxf->footer_partition_offset); header_byte_count_offset = avio_tell(pb); avio_wb64(pb, 0); avio_wb64(pb, index_byte_count); avio_wb32(pb, index_byte_count ? indexsid : 0); if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) { avio_wb64(pb, mxf->body_offset); } else avio_wb64(pb, 0); avio_wb32(pb, bodysid); avio_write(pb, op1a_ul, 16); mxf_write_essence_container_refs(s); if (write_metadata) { int64_t pos, start; unsigned header_byte_count; mxf_write_klv_fill(s); start = avio_tell(s->pb); mxf_write_primer_pack(s); mxf_write_header_metadata_sets(s); pos = avio_tell(s->pb); header_byte_count = pos - start + klv_fill_size(pos); avio_seek(pb, header_byte_count_offset, SEEK_SET); avio_wb64(pb, header_byte_count); avio_seek(pb, pos, SEEK_SET); } avio_flush(pb); }
['static void mxf_write_partition(AVFormatContext *s, int bodysid,\n int indexsid,\n const uint8_t *key, int write_metadata)\n{\n MXFContext *mxf = s->priv_data;\n AVIOContext *pb = s->pb;\n int64_t header_byte_count_offset;\n unsigned index_byte_count = 0;\n uint64_t partition_offset = avio_tell(pb);\n if (!mxf->edit_unit_byte_count && mxf->edit_units_count)\n index_byte_count = 85 + 12+(s->nb_streams+1)*6 +\n 12+mxf->edit_units_count*(11+mxf->slice_count*4);\n else if (mxf->edit_unit_byte_count && indexsid)\n index_byte_count = 80;\n if (index_byte_count) {\n index_byte_count += 16 + klv_ber_length(index_byte_count);\n index_byte_count += klv_fill_size(index_byte_count);\n }\n if (!memcmp(key, body_partition_key, 16)) {\n mxf->body_partition_offset =\n av_realloc(mxf->body_partition_offset,\n (mxf->body_partitions_count+1)*\n sizeof(*mxf->body_partition_offset));\n mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;\n }\n avio_write(pb, key, 16);\n klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);\n avio_wb16(pb, 1);\n avio_wb16(pb, 2);\n avio_wb32(pb, KAG_SIZE);\n avio_wb64(pb, partition_offset);\n if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)\n avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]);\n else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)\n avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]);\n else\n avio_wb64(pb, 0);\n avio_wb64(pb, mxf->footer_partition_offset);\n header_byte_count_offset = avio_tell(pb);\n avio_wb64(pb, 0);\n avio_wb64(pb, index_byte_count);\n avio_wb32(pb, index_byte_count ? indexsid : 0);\n if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) {\n avio_wb64(pb, mxf->body_offset);\n } else\n avio_wb64(pb, 0);\n avio_wb32(pb, bodysid);\n avio_write(pb, op1a_ul, 16);\n mxf_write_essence_container_refs(s);\n if (write_metadata) {\n int64_t pos, start;\n unsigned header_byte_count;\n mxf_write_klv_fill(s);\n start = avio_tell(s->pb);\n mxf_write_primer_pack(s);\n mxf_write_header_metadata_sets(s);\n pos = avio_tell(s->pb);\n header_byte_count = pos - start + klv_fill_size(pos);\n avio_seek(pb, header_byte_count_offset, SEEK_SET);\n avio_wb64(pb, header_byte_count);\n avio_seek(pb, pos, SEEK_SET);\n }\n avio_flush(pb);\n}', 'static av_always_inline int64_t avio_tell(AVIOContext *s)\n{\n return avio_seek(s, 0, SEEK_CUR);\n}', 'static int klv_ber_length(uint64_t len)\n{\n if (len < 128)\n return 1;\n else\n return (av_log2(len) >> 3) + 2;\n}', 'static unsigned klv_fill_size(uint64_t size)\n{\n unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));\n if (pad < 20)\n return pad + KAG_SIZE;\n else\n return pad & (KAG_SIZE-1);\n}', 'void *av_realloc(void *ptr, size_t size)\n{\n#if CONFIG_MEMALIGN_HACK\n int diff;\n#endif\n if (size > (INT_MAX - 16))\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n if (!ptr)\n return av_malloc(size);\n diff = ((char *)ptr)[-1];\n return (char *)realloc((char *)ptr - diff, size + diff) + diff;\n#elif HAVE_ALIGNED_MALLOC\n return _aligned_realloc(ptr, size, 32);\n#else\n return realloc(ptr, size);\n#endif\n}']
35,874
0
https://github.com/openssl/openssl/blob/31b446e212e2209d62e66a608e540716716430e4/crypto/bn/bn_ctx.c/#L353
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,\n BIGNUM **kinvp, BIGNUM **rp,\n const unsigned char *dgst, int dlen)\n{\n BN_CTX *ctx = NULL;\n BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;\n EC_POINT *tmp_point = NULL;\n const EC_GROUP *group;\n int ret = 0;\n if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);\n return 0;\n }\n if (ctx_in == NULL) {\n if ((ctx = BN_CTX_new()) == NULL) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n } else\n ctx = ctx_in;\n k = BN_new();\n r = BN_new();\n order = BN_new();\n X = BN_new();\n if (!k || !r || !order || !X) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if ((tmp_point = EC_POINT_new(group)) == NULL) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n if (!EC_GROUP_get_order(group, order, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n do {\n do\n if (dgst != NULL) {\n if (!BN_generate_dsa_nonce\n (k, order, EC_KEY_get0_private_key(eckey), dgst, dlen,\n ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,\n ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);\n goto err;\n }\n } else {\n if (!BN_rand_range(k, order)) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,\n ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);\n goto err;\n }\n }\n while (BN_is_zero(k));\n if (!BN_add(k, k, order))\n goto err;\n if (BN_num_bits(k) <= BN_num_bits(order))\n if (!BN_add(k, k, order))\n goto err;\n if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==\n NID_X9_62_prime_field) {\n if (!EC_POINT_get_affine_coordinates_GFp\n (group, tmp_point, X, NULL, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n }\n#ifndef OPENSSL_NO_EC2M\n else {\n if (!EC_POINT_get_affine_coordinates_GF2m(group,\n tmp_point, X, NULL,\n ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n }\n#endif\n if (!BN_nnmod(r, X, order, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n }\n while (BN_is_zero(r));\n if (EC_GROUP_get_mont_data(group) != NULL) {\n if (!BN_set_word(X, 2)) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_sub(X, order, X, order, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n BN_set_flags(X, BN_FLG_CONSTTIME);\n if (!BN_mod_exp_mont_consttime\n (k, k, X, order, ctx, EC_GROUP_get_mont_data(group))) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n } else {\n if (!BN_mod_inverse(k, k, order, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n }\n if (*rp != NULL)\n BN_clear_free(*rp);\n if (*kinvp != NULL)\n BN_clear_free(*kinvp);\n *rp = r;\n *kinvp = k;\n ret = 1;\n err:\n if (!ret) {\n if (k != NULL)\n BN_clear_free(k);\n if (r != NULL)\n BN_clear_free(r);\n }\n if (ctx_in == NULL)\n BN_CTX_free(ctx);\n if (order != NULL)\n BN_free(order);\n if (tmp_point != NULL)\n EC_POINT_free(tmp_point);\n if (X)\n BN_clear_free(X);\n return (ret);\n}', 'int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,\n const BIGNUM *priv, const unsigned char *message,\n size_t message_len, BN_CTX *ctx)\n{\n SHA512_CTX sha;\n unsigned char random_bytes[64];\n unsigned char digest[SHA512_DIGEST_LENGTH];\n unsigned done, todo;\n const unsigned num_k_bytes = BN_num_bytes(range) + 8;\n unsigned char private_bytes[96];\n unsigned char *k_bytes;\n int ret = 0;\n k_bytes = OPENSSL_malloc(num_k_bytes);\n if (!k_bytes)\n goto err;\n todo = sizeof(priv->d[0]) * priv->top;\n if (todo > sizeof(private_bytes)) {\n BNerr(BN_F_BN_GENERATE_DSA_NONCE, BN_R_PRIVATE_KEY_TOO_LARGE);\n goto err;\n }\n memcpy(private_bytes, priv->d, todo);\n memset(private_bytes + todo, 0, sizeof(private_bytes) - todo);\n for (done = 0; done < num_k_bytes;) {\n if (RAND_bytes(random_bytes, sizeof(random_bytes)) != 1)\n goto err;\n SHA512_Init(&sha);\n SHA512_Update(&sha, &done, sizeof(done));\n SHA512_Update(&sha, private_bytes, sizeof(private_bytes));\n SHA512_Update(&sha, message, message_len);\n SHA512_Update(&sha, random_bytes, sizeof(random_bytes));\n SHA512_Final(digest, &sha);\n todo = num_k_bytes - done;\n if (todo > SHA512_DIGEST_LENGTH)\n todo = SHA512_DIGEST_LENGTH;\n memcpy(k_bytes + done, digest, todo);\n done += todo;\n }\n if (!BN_bin2bn(k_bytes, num_k_bytes, out))\n goto err;\n if (BN_mod(out, out, range, ctx) != 1)\n goto err;\n ret = 1;\n err:\n if (k_bytes)\n OPENSSL_free(k_bytes);\n return ret;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
35,875
0
https://github.com/libav/libav/blob/3fec44c640ea0c8fd1944e9a41da50a3d7251274/libavcodec/vp3.c/#L997
static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, VLC *table, int coeff_index, int plane, int eob_run) { int i, j = 0; int token; int zero_run = 0; DCTELEM coeff = 0; int bits_to_get; int blocks_ended; int coeff_i = 0; int num_coeffs = s->num_coded_frags[plane][coeff_index]; int16_t *dct_tokens = s->dct_tokens[plane][coeff_index]; int *coded_fragment_list = s->coded_fragment_list[plane]; Vp3Fragment *all_fragments = s->all_fragments; VLC_TYPE (*vlc_table)[2] = table->table; if (num_coeffs < 0) av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coefficents at level %d\n", coeff_index); if (eob_run > num_coeffs) { coeff_i = blocks_ended = num_coeffs; eob_run -= num_coeffs; } else { coeff_i = blocks_ended = eob_run; eob_run = 0; } if (blocks_ended) dct_tokens[j++] = blocks_ended << 2; while (coeff_i < num_coeffs) { token = get_vlc2(gb, vlc_table, 5, 3); if (token <= 6) { eob_run = eob_run_base[token]; if (eob_run_get_bits[token]) eob_run += get_bits(gb, eob_run_get_bits[token]); if (eob_run > num_coeffs - coeff_i) { dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i); blocks_ended += num_coeffs - coeff_i; eob_run -= num_coeffs - coeff_i; coeff_i = num_coeffs; } else { dct_tokens[j++] = TOKEN_EOB(eob_run); blocks_ended += eob_run; coeff_i += eob_run; eob_run = 0; } } else { bits_to_get = coeff_get_bits[token]; if (bits_to_get) bits_to_get = get_bits(gb, bits_to_get); coeff = coeff_tables[token][bits_to_get]; zero_run = zero_run_base[token]; if (zero_run_get_bits[token]) zero_run += get_bits(gb, zero_run_get_bits[token]); if (zero_run) { dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run); } else { if (!coeff_index) all_fragments[coded_fragment_list[coeff_i]].dc = coeff; dct_tokens[j++] = TOKEN_COEFF(coeff); } if (coeff_index + zero_run > 64) { av_log(s->avctx, AV_LOG_ERROR, "Invalid zero run of %d with" " %d coeffs left\n", zero_run, 64-coeff_index); zero_run = 64 - coeff_index; } for (i = coeff_index+1; i <= coeff_index+zero_run; i++) s->num_coded_frags[plane][i]--; coeff_i++; } } if (blocks_ended > s->num_coded_frags[plane][coeff_index]) av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n"); if (blocks_ended) for (i = coeff_index+1; i < 64; i++) s->num_coded_frags[plane][i] -= blocks_ended; if (plane < 2) s->dct_tokens[plane+1][coeff_index] = dct_tokens + j; else if (coeff_index < 63) s->dct_tokens[0][coeff_index+1] = dct_tokens + j; return eob_run; }
['static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,\n VLC *table, int coeff_index,\n int plane,\n int eob_run)\n{\n int i, j = 0;\n int token;\n int zero_run = 0;\n DCTELEM coeff = 0;\n int bits_to_get;\n int blocks_ended;\n int coeff_i = 0;\n int num_coeffs = s->num_coded_frags[plane][coeff_index];\n int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];\n int *coded_fragment_list = s->coded_fragment_list[plane];\n Vp3Fragment *all_fragments = s->all_fragments;\n VLC_TYPE (*vlc_table)[2] = table->table;\n if (num_coeffs < 0)\n av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coefficents at level %d\\n", coeff_index);\n if (eob_run > num_coeffs) {\n coeff_i = blocks_ended = num_coeffs;\n eob_run -= num_coeffs;\n } else {\n coeff_i = blocks_ended = eob_run;\n eob_run = 0;\n }\n if (blocks_ended)\n dct_tokens[j++] = blocks_ended << 2;\n while (coeff_i < num_coeffs) {\n token = get_vlc2(gb, vlc_table, 5, 3);\n if (token <= 6) {\n eob_run = eob_run_base[token];\n if (eob_run_get_bits[token])\n eob_run += get_bits(gb, eob_run_get_bits[token]);\n if (eob_run > num_coeffs - coeff_i) {\n dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);\n blocks_ended += num_coeffs - coeff_i;\n eob_run -= num_coeffs - coeff_i;\n coeff_i = num_coeffs;\n } else {\n dct_tokens[j++] = TOKEN_EOB(eob_run);\n blocks_ended += eob_run;\n coeff_i += eob_run;\n eob_run = 0;\n }\n } else {\n bits_to_get = coeff_get_bits[token];\n if (bits_to_get)\n bits_to_get = get_bits(gb, bits_to_get);\n coeff = coeff_tables[token][bits_to_get];\n zero_run = zero_run_base[token];\n if (zero_run_get_bits[token])\n zero_run += get_bits(gb, zero_run_get_bits[token]);\n if (zero_run) {\n dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);\n } else {\n if (!coeff_index)\n all_fragments[coded_fragment_list[coeff_i]].dc = coeff;\n dct_tokens[j++] = TOKEN_COEFF(coeff);\n }\n if (coeff_index + zero_run > 64) {\n av_log(s->avctx, AV_LOG_ERROR, "Invalid zero run of %d with"\n " %d coeffs left\\n", zero_run, 64-coeff_index);\n zero_run = 64 - coeff_index;\n }\n for (i = coeff_index+1; i <= coeff_index+zero_run; i++)\n s->num_coded_frags[plane][i]--;\n coeff_i++;\n }\n }\n if (blocks_ended > s->num_coded_frags[plane][coeff_index])\n av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\\n");\n if (blocks_ended)\n for (i = coeff_index+1; i < 64; i++)\n s->num_coded_frags[plane][i] -= blocks_ended;\n if (plane < 2)\n s->dct_tokens[plane+1][coeff_index] = dct_tokens + j;\n else if (coeff_index < 63)\n s->dct_tokens[0][coeff_index+1] = dct_tokens + j;\n return eob_run;\n}']
35,876
1
https://github.com/libav/libav/blob/1a5fdf9519d7f4b9a1bdda623516c37744f569bd/libavcodec/alsdec.c/#L579
static void decode_const_block_data(ALSDecContext *ctx, ALSBlockData *bd) { int smp = bd->block_length - 1; int32_t val = *bd->raw_samples; int32_t *dst = bd->raw_samples + 1; for (; smp; smp--) *dst++ = val; }
['static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)\n{\n ALSSpecificConfig *sconf = &ctx->sconf;\n AVCodecContext *avctx = ctx->avctx;\n GetBitContext *gb = &ctx->gb;\n unsigned int div_blocks[32];\n unsigned int c;\n unsigned int js_blocks[2];\n uint32_t bs_info = 0;\n int ret;\n if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)\n skip_bits_long(gb, 32);\n if (sconf->mc_coding && sconf->joint_stereo) {\n ctx->js_switch = get_bits1(gb);\n align_get_bits(gb);\n }\n if (!sconf->mc_coding || ctx->js_switch) {\n int independent_bs = !sconf->joint_stereo;\n for (c = 0; c < avctx->channels; c++) {\n js_blocks[0] = 0;\n js_blocks[1] = 0;\n get_block_sizes(ctx, div_blocks, &bs_info);\n if (sconf->joint_stereo && sconf->block_switching)\n if (bs_info >> 31)\n independent_bs = 2;\n if (c == avctx->channels - 1)\n independent_bs = 1;\n if (independent_bs) {\n ret = decode_blocks_ind(ctx, ra_frame, c,\n div_blocks, js_blocks);\n if (ret < 0)\n return ret;\n independent_bs--;\n } else {\n ret = decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks);\n if (ret < 0)\n return ret;\n c++;\n }\n memmove(ctx->raw_samples[c] - sconf->max_order,\n ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,\n sizeof(*ctx->raw_samples[c]) * sconf->max_order);\n }\n } else {\n ALSBlockData bd = { 0 };\n int b, ret;\n int *reverted_channels = ctx->reverted_channels;\n unsigned int offset = 0;\n for (c = 0; c < avctx->channels; c++)\n if (ctx->chan_data[c] < ctx->chan_data_buffer) {\n av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data!\\n");\n return AVERROR_INVALIDDATA;\n }\n memset(reverted_channels, 0, sizeof(*reverted_channels) * avctx->channels);\n bd.ra_block = ra_frame;\n bd.prev_raw_samples = ctx->prev_raw_samples;\n get_block_sizes(ctx, div_blocks, &bs_info);\n for (b = 0; b < ctx->num_blocks; b++) {\n bd.block_length = div_blocks[b];\n for (c = 0; c < avctx->channels; c++) {\n bd.const_block = ctx->const_block + c;\n bd.shift_lsbs = ctx->shift_lsbs + c;\n bd.opt_order = ctx->opt_order + c;\n bd.store_prev_samples = ctx->store_prev_samples + c;\n bd.use_ltp = ctx->use_ltp + c;\n bd.ltp_lag = ctx->ltp_lag + c;\n bd.ltp_gain = ctx->ltp_gain[c];\n bd.lpc_cof = ctx->lpc_cof[c];\n bd.quant_cof = ctx->quant_cof[c];\n bd.raw_samples = ctx->raw_samples[c] + offset;\n bd.raw_other = NULL;\n if ((ret = read_block(ctx, &bd)) < 0)\n return ret;\n if ((ret = read_channel_data(ctx, ctx->chan_data[c], c)) < 0)\n return ret;\n }\n for (c = 0; c < avctx->channels; c++) {\n ret = revert_channel_correlation(ctx, &bd, ctx->chan_data,\n reverted_channels, offset, c);\n if (ret < 0)\n return ret;\n }\n for (c = 0; c < avctx->channels; c++) {\n bd.const_block = ctx->const_block + c;\n bd.shift_lsbs = ctx->shift_lsbs + c;\n bd.opt_order = ctx->opt_order + c;\n bd.store_prev_samples = ctx->store_prev_samples + c;\n bd.use_ltp = ctx->use_ltp + c;\n bd.ltp_lag = ctx->ltp_lag + c;\n bd.ltp_gain = ctx->ltp_gain[c];\n bd.lpc_cof = ctx->lpc_cof[c];\n bd.quant_cof = ctx->quant_cof[c];\n bd.raw_samples = ctx->raw_samples[c] + offset;\n if ((ret = decode_block(ctx, &bd)) < 0)\n return ret;\n }\n memset(reverted_channels, 0, avctx->channels * sizeof(*reverted_channels));\n offset += div_blocks[b];\n bd.ra_block = 0;\n }\n for (c = 0; c < avctx->channels; c++)\n memmove(ctx->raw_samples[c] - sconf->max_order,\n ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,\n sizeof(*ctx->raw_samples[c]) * sconf->max_order);\n }\n return 0;\n}', 'static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks,\n uint32_t *bs_info)\n{\n ALSSpecificConfig *sconf = &ctx->sconf;\n GetBitContext *gb = &ctx->gb;\n unsigned int *ptr_div_blocks = div_blocks;\n unsigned int b;\n if (sconf->block_switching) {\n unsigned int bs_info_len = 1 << (sconf->block_switching + 2);\n *bs_info = get_bits_long(gb, bs_info_len);\n *bs_info <<= (32 - bs_info_len);\n }\n ctx->num_blocks = 0;\n parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks);\n for (b = 0; b < ctx->num_blocks; b++)\n div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b];\n if (ctx->cur_frame_length != ctx->sconf.frame_length) {\n unsigned int remaining = ctx->cur_frame_length;\n for (b = 0; b < ctx->num_blocks; b++) {\n if (remaining <= div_blocks[b]) {\n div_blocks[b] = remaining;\n ctx->num_blocks = b + 1;\n break;\n }\n remaining -= div_blocks[b];\n }\n }\n}', 'static int decode_block(ALSDecContext *ctx, ALSBlockData *bd)\n{\n unsigned int smp;\n int ret = 0;\n if (*bd->const_block)\n decode_const_block_data(ctx, bd);\n else\n ret = decode_var_block_data(ctx, bd);\n if (ret < 0)\n return ret;\n if (*bd->shift_lsbs)\n for (smp = 0; smp < bd->block_length; smp++)\n bd->raw_samples[smp] <<= *bd->shift_lsbs;\n return 0;\n}', 'static void decode_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)\n{\n int smp = bd->block_length - 1;\n int32_t val = *bd->raw_samples;\n int32_t *dst = bd->raw_samples + 1;\n for (; smp; smp--)\n *dst++ = val;\n}']
35,877
0
https://github.com/openssl/openssl/blob/516decaef31a13e5bf1b6f855dc0fefe23d7eed9/crypto/init.c/#L380
int ossl_init_thread_start(uint64_t opts) { struct thread_local_inits_st *locals = ossl_init_get_thread_local(1); if (locals == NULL) return 0; if (opts & OPENSSL_INIT_THREAD_ASYNC) { #ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " "marking thread for async\n"); #endif locals->async = 1; } if (opts & OPENSSL_INIT_THREAD_ERR_STATE) { #ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " "marking thread for err_state\n"); #endif locals->err_state = 1; } return 1; }
['int ossl_init_thread_start(uint64_t opts)\n{\n struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);\n if (locals == NULL)\n return 0;\n if (opts & OPENSSL_INIT_THREAD_ASYNC) {\n#ifdef OPENSSL_INIT_DEBUG\n fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "\n "marking thread for async\\n");\n#endif\n locals->async = 1;\n }\n if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {\n#ifdef OPENSSL_INIT_DEBUG\n fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "\n "marking thread for err_state\\n");\n#endif\n locals->err_state = 1;\n }\n return 1;\n}', 'static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)\n{\n struct thread_local_inits_st *local =\n CRYPTO_THREAD_get_local(&threadstopkey);\n if (local == NULL && alloc) {\n local = OPENSSL_zalloc(sizeof *local);\n CRYPTO_THREAD_set_local(&threadstopkey, local);\n }\n if (!alloc) {\n CRYPTO_THREAD_set_local(&threadstopkey, NULL);\n }\n return local;\n}', 'void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)\n{\n return pthread_getspecific(*key);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)\n{\n if (pthread_setspecific(*key, val) != 0)\n return 0;\n return 1;\n}']
35,878
0
https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/bn/bn_asm.c/#L544
void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) { #ifdef BN_LLONG BN_ULLONG t; #else BN_ULONG bl,bh; #endif BN_ULONG t1,t2; BN_ULONG c1,c2,c3; c1=0; c2=0; c3=0; mul_add_c(a[0],b[0],c1,c2,c3); r[0]=c1; c1=0; mul_add_c(a[0],b[1],c2,c3,c1); mul_add_c(a[1],b[0],c2,c3,c1); r[1]=c2; c2=0; mul_add_c(a[2],b[0],c3,c1,c2); mul_add_c(a[1],b[1],c3,c1,c2); mul_add_c(a[0],b[2],c3,c1,c2); r[2]=c3; c3=0; mul_add_c(a[0],b[3],c1,c2,c3); mul_add_c(a[1],b[2],c1,c2,c3); mul_add_c(a[2],b[1],c1,c2,c3); mul_add_c(a[3],b[0],c1,c2,c3); r[3]=c1; c1=0; mul_add_c(a[4],b[0],c2,c3,c1); mul_add_c(a[3],b[1],c2,c3,c1); mul_add_c(a[2],b[2],c2,c3,c1); mul_add_c(a[1],b[3],c2,c3,c1); mul_add_c(a[0],b[4],c2,c3,c1); r[4]=c2; c2=0; mul_add_c(a[0],b[5],c3,c1,c2); mul_add_c(a[1],b[4],c3,c1,c2); mul_add_c(a[2],b[3],c3,c1,c2); mul_add_c(a[3],b[2],c3,c1,c2); mul_add_c(a[4],b[1],c3,c1,c2); mul_add_c(a[5],b[0],c3,c1,c2); r[5]=c3; c3=0; mul_add_c(a[6],b[0],c1,c2,c3); mul_add_c(a[5],b[1],c1,c2,c3); mul_add_c(a[4],b[2],c1,c2,c3); mul_add_c(a[3],b[3],c1,c2,c3); mul_add_c(a[2],b[4],c1,c2,c3); mul_add_c(a[1],b[5],c1,c2,c3); mul_add_c(a[0],b[6],c1,c2,c3); r[6]=c1; c1=0; mul_add_c(a[0],b[7],c2,c3,c1); mul_add_c(a[1],b[6],c2,c3,c1); mul_add_c(a[2],b[5],c2,c3,c1); mul_add_c(a[3],b[4],c2,c3,c1); mul_add_c(a[4],b[3],c2,c3,c1); mul_add_c(a[5],b[2],c2,c3,c1); mul_add_c(a[6],b[1],c2,c3,c1); mul_add_c(a[7],b[0],c2,c3,c1); r[7]=c2; c2=0; mul_add_c(a[7],b[1],c3,c1,c2); mul_add_c(a[6],b[2],c3,c1,c2); mul_add_c(a[5],b[3],c3,c1,c2); mul_add_c(a[4],b[4],c3,c1,c2); mul_add_c(a[3],b[5],c3,c1,c2); mul_add_c(a[2],b[6],c3,c1,c2); mul_add_c(a[1],b[7],c3,c1,c2); r[8]=c3; c3=0; mul_add_c(a[2],b[7],c1,c2,c3); mul_add_c(a[3],b[6],c1,c2,c3); mul_add_c(a[4],b[5],c1,c2,c3); mul_add_c(a[5],b[4],c1,c2,c3); mul_add_c(a[6],b[3],c1,c2,c3); mul_add_c(a[7],b[2],c1,c2,c3); r[9]=c1; c1=0; mul_add_c(a[7],b[3],c2,c3,c1); mul_add_c(a[6],b[4],c2,c3,c1); mul_add_c(a[5],b[5],c2,c3,c1); mul_add_c(a[4],b[6],c2,c3,c1); mul_add_c(a[3],b[7],c2,c3,c1); r[10]=c2; c2=0; mul_add_c(a[4],b[7],c3,c1,c2); mul_add_c(a[5],b[6],c3,c1,c2); mul_add_c(a[6],b[5],c3,c1,c2); mul_add_c(a[7],b[4],c3,c1,c2); r[11]=c3; c3=0; mul_add_c(a[7],b[5],c1,c2,c3); mul_add_c(a[6],b[6],c1,c2,c3); mul_add_c(a[5],b[7],c1,c2,c3); r[12]=c1; c1=0; mul_add_c(a[6],b[7],c2,c3,c1); mul_add_c(a[7],b[6],c2,c3,c1); r[13]=c2; c2=0; mul_add_c(a[7],b[7],c3,c1,c2); r[14]=c3; r[15]=c1; }
['int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,\n\t\t DSA *dsa)\n\t{\n\tBN_CTX *ctx;\n\tBIGNUM u1,u2,t1;\n\tBN_MONT_CTX *mont=NULL;\n\tint ret = -1;\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tBN_init(&u1);\n\tBN_init(&u2);\n\tBN_init(&t1);\n\tif ((BN_mod_inverse(&u2,sig->s,dsa->q,ctx)) == NULL) goto err;\n\tif (BN_bin2bn(dgst,dgst_len,&u1) == NULL) goto err;\n\tif (!BN_mod_mul(&u1,&u1,&u2,dsa->q,ctx)) goto err;\n\tif (!BN_mod_mul(&u2,sig->r,&u2,dsa->q,ctx)) goto err;\n\tif ((dsa->method_mont_p == NULL) && (dsa->flags & DSA_FLAG_CACHE_MONT_P))\n\t\t{\n\t\tif ((dsa->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)\n\t\t\tif (!BN_MONT_CTX_set((BN_MONT_CTX *)dsa->method_mont_p,\n\t\t\t\tdsa->p,ctx)) goto err;\n\t\t}\n\tmont=(BN_MONT_CTX *)dsa->method_mont_p;\n#if 0\n\t{\n\tBIGNUM t2;\n\tBN_init(&t2);\n\tif (!BN_mod_exp_mont(&t1,dsa->g,&u1,dsa->p,ctx,mont)) goto err;\n\tif (!BN_mod_exp_mont(&t2,dsa->pub_key,&u2,dsa->p,ctx,mont)) goto err;\n\tif (!BN_mod_mul(&u1,&t1,&t2,dsa->p,ctx)) goto err_bn;\n\tBN_free(&t2);\n\t}\n\tif (!BN_mod(&u1,&u1,dsa->q,ctx)) goto err;\n#else\n\t{\n\tif (!BN_mod_exp2_mont(&t1,dsa->g,&u1,dsa->pub_key,&u2,dsa->p,ctx,mont))\n\t\tgoto err;\n\tif (!BN_mod(&u1,&t1,dsa->q,ctx)) goto err;\n\t}\n#endif\n\tret=(BN_ucmp(&u1, sig->r) == 0);\n\terr:\n\tif (ret != 1) DSAerr(DSA_F_DSA_DO_VERIFY,ERR_R_BN_LIB);\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\tBN_free(&u1);\n\tBN_free(&u2);\n\tBN_free(&t1);\n\treturn(ret);\n\t}', 'int BN_mod_exp2_mont(BIGNUM *rr, BIGNUM *a1, BIGNUM *p1, BIGNUM *a2,\n\t BIGNUM *p2, BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,j,k,bits,bits1,bits2,ret=0,wstart,wend,window,xvalue,yvalue;\n\tint start=1,ts=0,x,y;\n\tBIGNUM *d,*aa1,*aa2,*r;\n\tBIGNUM val[EXP2_TABLE_SIZE][EXP2_TABLE_SIZE];\n\tBN_MONT_CTX *mont=NULL;\n\tbn_check_top(a1);\n\tbn_check_top(p1);\n\tbn_check_top(a2);\n\tbn_check_top(p2);\n\tbn_check_top(m);\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\td= &(ctx->bn[ctx->tos++]);\n\tr= &(ctx->bn[ctx->tos++]);\n\tbits1=BN_num_bits(p1);\n\tbits2=BN_num_bits(p2);\n\tif ((bits1 == 0) && (bits2 == 0))\n\t\t{\n\t\tBN_one(r);\n\t\treturn(1);\n\t\t}\n\tbits=(bits1 > bits2)?bits1:bits2;\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n\t\t{\n\t\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\t\tif (!BN_MONT_CTX_set(mont,m,ctx)) goto err;\n\t\t}\n\tBN_init(&(val[0][0]));\n\tBN_init(&(val[1][1]));\n\tBN_init(&(val[0][1]));\n\tBN_init(&(val[1][0]));\n\tts=1;\n\tif (BN_ucmp(a1,m) >= 0)\n\t\t{\n\t\tBN_mod(&(val[1][0]),a1,m,ctx);\n\t\taa1= &(val[1][0]);\n\t\t}\n\telse\n\t\taa1=a1;\n\tif (BN_ucmp(a2,m) >= 0)\n\t\t{\n\t\tBN_mod(&(val[0][1]),a2,m,ctx);\n\t\taa2= &(val[0][1]);\n\t\t}\n\telse\n\t\taa2=a2;\n\tif (!BN_to_montgomery(&(val[1][0]),aa1,mont,ctx)) goto err;\n\tif (!BN_to_montgomery(&(val[0][1]),aa2,mont,ctx)) goto err;\n\tif (!BN_mod_mul_montgomery(&(val[1][1]),\n\t\t&(val[1][0]),&(val[0][1]),mont,ctx))\n\t\tgoto err;\n#if 0\n\tif (bits <= 20)\n\t\twindow=1;\n\telse if (bits > 250)\n\t\twindow=5;\n\telse if (bits >= 120)\n\t\twindow=4;\n\telse\n\t\twindow=3;\n#else\n\twindow=EXP2_TABLE_BITS;\n#endif\n\tk=1<<window;\n\tfor (x=0; x<k; x++)\n\t\t{\n\t\tif (x >= 2)\n\t\t\t{\n\t\t\tBN_init(&(val[x][0]));\n\t\t\tBN_init(&(val[x][1]));\n\t\t\tif (!BN_mod_mul_montgomery(&(val[x][0]),\n\t\t\t\t&(val[1][0]),&(val[x-1][0]),mont,ctx)) goto err;\n\t\t\tif (!BN_mod_mul_montgomery(&(val[x][1]),\n\t\t\t\t&(val[1][0]),&(val[x-1][1]),mont,ctx)) goto err;\n\t\t\t}\n\t\tfor (y=2; y<k; y++)\n\t\t\t{\n\t\t\tBN_init(&(val[x][y]));\n\t\t\tif (!BN_mod_mul_montgomery(&(val[x][y]),\n\t\t\t\t&(val[x][y-1]),&(val[0][1]),mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tts=k;\n\tstart=1;\n\txvalue=0;\n\tyvalue=0;\n\twstart=bits-1;\n\twend=0;\n if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;\n\tfor (;;)\n\t\t{\n\t\txvalue=BN_is_bit_set(p1,wstart);\n\t\tyvalue=BN_is_bit_set(p2,wstart);\n\t\tif (!(xvalue || yvalue))\n\t\t\t{\n\t\t\tif (!start)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\twstart--;\n\t\t\tif (wstart < 0) break;\n\t\t\tcontinue;\n\t\t\t}\n\t\tj=wstart;\n\t\twend=0;\n\t\tfor (i=1; i<window; i++)\n\t\t\t{\n\t\t\tif (wstart-i < 0) break;\n\t\t\txvalue+=xvalue;\n\t\t\txvalue|=BN_is_bit_set(p1,wstart-i);\n\t\t\tyvalue+=yvalue;\n\t\t\tyvalue|=BN_is_bit_set(p2,wstart-i);\n\t\t\t}\n\t\tif (!start)\n\t\t\tfor (j=0; j<i; j++)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (xvalue || yvalue)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(r,r,&(val[xvalue][yvalue]),\n\t\t\t\tmont,ctx)) goto err;\n\t\t\t}\n\t\twstart-=i;\n\t\tstart=0;\n\t\tif (wstart < 0) break;\n\t\t}\n\tBN_from_montgomery(rr,r,mont,ctx);\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tctx->tos-=2;\n\tfor (i=0; i<ts; i++)\n\t\t{\n\t\tfor (j=0; j<ts; j++)\n\t\t\t{\n\t\t\tBN_clear_free(&(val[i][j]));\n\t\t\t}\n\t\t}\n\treturn(ret);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_MONT_CTX *mont,\n\t BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp,*tmp2;\n tmp= &(ctx->bn[ctx->tos]);\n tmp2= &(ctx->bn[ctx->tos]);\n\tctx->tos+=2;\n\tbn_check_top(tmp);\n\tbn_check_top(tmp2);\n\tif (a == b)\n\t\t{\n#if 0\n\t\tbn_wexpand(tmp,a->top*2);\n\t\tbn_wexpand(tmp2,a->top*4);\n\t\tbn_sqr_recursive(tmp->d,a->d,a->top,tmp2->d);\n\t\ttmp->top=a->top*2;\n\t\tif (tmp->d[tmp->top-1] == 0)\n\t\t\ttmp->top--;\n#else\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n\tctx->tos-=2;\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}', 'void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,\n\t BN_ULONG *t)\n\t{\n\tint n=n2/2,c1,c2;\n\tunsigned int neg,zero;\n\tBN_ULONG ln,lo,*p;\n#ifdef BN_COUNT\nprintf(" bn_mul_recursive %d * %d\\n",n2,n2);\n#endif\n#ifdef BN_MUL_COMBA\n if (n2 == 8)\n\t\t{\n\t\tbn_mul_comba8(r,a,b);\n\t\treturn;\n\t\t}\n#endif\n\tif (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t{\n\t\tbn_mul_normal(r,a,n2,b,n2);\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_words(a,&(a[n]),n);\n\tc2=bn_cmp_words(&(b[n]),b,n);\n\tzero=neg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tbreak;\n\tcase -3:\n\t\tzero=1;\n\t\tbreak;\n\tcase -2:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\t\tzero=1;\n\t\tbreak;\n\tcase 2:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\t\tzero=1;\n\t\tbreak;\n\tcase 4:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tbreak;\n\t\t}\n#ifdef BN_MUL_COMBA\n\tif (n == 4)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,8*sizeof(BN_ULONG));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_comba4(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse if (n == 8)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,16*sizeof(BN_ULONG));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_comba8(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse\n#endif\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tif (!zero)\n\t\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,p);\n\t\telse\n\t\t\tmemset(&(t[n2]),0,n2*sizeof(BN_ULONG));\n\t\tbn_mul_recursive(r,a,b,n,p);\n\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,p);\n\t\t}\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tif (neg)\n\t\t{\n\t\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\t\t}\n\telse\n\t\t{\n\t\tc1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));\n\t\t}\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < (BN_ULONG)c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)\n\t{\n#ifdef BN_LLONG\n\tBN_ULLONG t;\n#else\n\tBN_ULONG bl,bh;\n#endif\n\tBN_ULONG t1,t2;\n\tBN_ULONG c1,c2,c3;\n\tc1=0;\n\tc2=0;\n\tc3=0;\n\tmul_add_c(a[0],b[0],c1,c2,c3);\n\tr[0]=c1;\n\tc1=0;\n\tmul_add_c(a[0],b[1],c2,c3,c1);\n\tmul_add_c(a[1],b[0],c2,c3,c1);\n\tr[1]=c2;\n\tc2=0;\n\tmul_add_c(a[2],b[0],c3,c1,c2);\n\tmul_add_c(a[1],b[1],c3,c1,c2);\n\tmul_add_c(a[0],b[2],c3,c1,c2);\n\tr[2]=c3;\n\tc3=0;\n\tmul_add_c(a[0],b[3],c1,c2,c3);\n\tmul_add_c(a[1],b[2],c1,c2,c3);\n\tmul_add_c(a[2],b[1],c1,c2,c3);\n\tmul_add_c(a[3],b[0],c1,c2,c3);\n\tr[3]=c1;\n\tc1=0;\n\tmul_add_c(a[4],b[0],c2,c3,c1);\n\tmul_add_c(a[3],b[1],c2,c3,c1);\n\tmul_add_c(a[2],b[2],c2,c3,c1);\n\tmul_add_c(a[1],b[3],c2,c3,c1);\n\tmul_add_c(a[0],b[4],c2,c3,c1);\n\tr[4]=c2;\n\tc2=0;\n\tmul_add_c(a[0],b[5],c3,c1,c2);\n\tmul_add_c(a[1],b[4],c3,c1,c2);\n\tmul_add_c(a[2],b[3],c3,c1,c2);\n\tmul_add_c(a[3],b[2],c3,c1,c2);\n\tmul_add_c(a[4],b[1],c3,c1,c2);\n\tmul_add_c(a[5],b[0],c3,c1,c2);\n\tr[5]=c3;\n\tc3=0;\n\tmul_add_c(a[6],b[0],c1,c2,c3);\n\tmul_add_c(a[5],b[1],c1,c2,c3);\n\tmul_add_c(a[4],b[2],c1,c2,c3);\n\tmul_add_c(a[3],b[3],c1,c2,c3);\n\tmul_add_c(a[2],b[4],c1,c2,c3);\n\tmul_add_c(a[1],b[5],c1,c2,c3);\n\tmul_add_c(a[0],b[6],c1,c2,c3);\n\tr[6]=c1;\n\tc1=0;\n\tmul_add_c(a[0],b[7],c2,c3,c1);\n\tmul_add_c(a[1],b[6],c2,c3,c1);\n\tmul_add_c(a[2],b[5],c2,c3,c1);\n\tmul_add_c(a[3],b[4],c2,c3,c1);\n\tmul_add_c(a[4],b[3],c2,c3,c1);\n\tmul_add_c(a[5],b[2],c2,c3,c1);\n\tmul_add_c(a[6],b[1],c2,c3,c1);\n\tmul_add_c(a[7],b[0],c2,c3,c1);\n\tr[7]=c2;\n\tc2=0;\n\tmul_add_c(a[7],b[1],c3,c1,c2);\n\tmul_add_c(a[6],b[2],c3,c1,c2);\n\tmul_add_c(a[5],b[3],c3,c1,c2);\n\tmul_add_c(a[4],b[4],c3,c1,c2);\n\tmul_add_c(a[3],b[5],c3,c1,c2);\n\tmul_add_c(a[2],b[6],c3,c1,c2);\n\tmul_add_c(a[1],b[7],c3,c1,c2);\n\tr[8]=c3;\n\tc3=0;\n\tmul_add_c(a[2],b[7],c1,c2,c3);\n\tmul_add_c(a[3],b[6],c1,c2,c3);\n\tmul_add_c(a[4],b[5],c1,c2,c3);\n\tmul_add_c(a[5],b[4],c1,c2,c3);\n\tmul_add_c(a[6],b[3],c1,c2,c3);\n\tmul_add_c(a[7],b[2],c1,c2,c3);\n\tr[9]=c1;\n\tc1=0;\n\tmul_add_c(a[7],b[3],c2,c3,c1);\n\tmul_add_c(a[6],b[4],c2,c3,c1);\n\tmul_add_c(a[5],b[5],c2,c3,c1);\n\tmul_add_c(a[4],b[6],c2,c3,c1);\n\tmul_add_c(a[3],b[7],c2,c3,c1);\n\tr[10]=c2;\n\tc2=0;\n\tmul_add_c(a[4],b[7],c3,c1,c2);\n\tmul_add_c(a[5],b[6],c3,c1,c2);\n\tmul_add_c(a[6],b[5],c3,c1,c2);\n\tmul_add_c(a[7],b[4],c3,c1,c2);\n\tr[11]=c3;\n\tc3=0;\n\tmul_add_c(a[7],b[5],c1,c2,c3);\n\tmul_add_c(a[6],b[6],c1,c2,c3);\n\tmul_add_c(a[5],b[7],c1,c2,c3);\n\tr[12]=c1;\n\tc1=0;\n\tmul_add_c(a[6],b[7],c2,c3,c1);\n\tmul_add_c(a[7],b[6],c2,c3,c1);\n\tr[13]=c2;\n\tc2=0;\n\tmul_add_c(a[7],b[7],c3,c1,c2);\n\tr[14]=c3;\n\tr[15]=c1;\n\t}']
35,879
0
https://github.com/libav/libav/blob/f5a2c9816e0b58edf2a87297be8d648631fc3432/libavutil/string.c/#L74
size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) { int len = strlen(dst); va_list vl; va_start(vl, fmt); len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl); va_end(vl); return len; }
['static int rtsp_read_packet(AVFormatContext *s,\n AVPacket *pkt)\n{\n RTSPState *rt = s->priv_data;\n RTSPStream *rtsp_st;\n int ret, len;\n uint8_t buf[RTP_MAX_PACKET_LENGTH];\n if (rt->server_type == RTSP_SERVER_REAL && rt->need_subscription) {\n int i;\n RTSPHeader reply1, *reply = &reply1;\n char cmd[1024];\n snprintf(cmd, sizeof(cmd),\n "SET_PARAMETER %s RTSP/1.0\\r\\n"\n "Subscribe: ",\n s->filename);\n for (i = 0; i < rt->nb_rtsp_streams; i++) {\n if (i != 0) av_strlcat(cmd, ",", sizeof(cmd));\n ff_rdt_subscribe_rule(cmd, sizeof(cmd), i, 0);\n if (rt->transport == RTSP_TRANSPORT_RDT)\n ff_rdt_subscribe_rule2(\n rt->rtsp_streams[i]->tx_ctx,\n cmd, sizeof(cmd), i, 0);\n }\n av_strlcat(cmd, "\\r\\n", sizeof(cmd));\n rtsp_send_cmd(s, cmd, reply, NULL);\n if (reply->status_code != RTSP_STATUS_OK)\n return AVERROR_INVALIDDATA;\n rt->need_subscription = 0;\n if (rt->state == RTSP_STATE_PLAYING)\n rtsp_read_play (s);\n }\n if (rt->cur_tx) {\n if (rt->transport == RTSP_TRANSPORT_RDT)\n ret = ff_rdt_parse_packet(rt->cur_tx, pkt, NULL, 0);\n else\n ret = rtp_parse_packet(rt->cur_tx, pkt, NULL, 0);\n if (ret == 0) {\n rt->cur_tx = NULL;\n return 0;\n } else if (ret == 1) {\n return 0;\n } else {\n rt->cur_tx = NULL;\n }\n }\n redo:\n switch(rt->lower_transport) {\n default:\n case RTSP_LOWER_TRANSPORT_TCP:\n len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));\n break;\n case RTSP_LOWER_TRANSPORT_UDP:\n case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:\n len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));\n if (len >=0 && rtsp_st->tx_ctx)\n rtp_check_and_send_back_rr(rtsp_st->tx_ctx, len);\n break;\n }\n if (len < 0)\n return len;\n if (rt->transport == RTSP_TRANSPORT_RDT)\n ret = ff_rdt_parse_packet(rtsp_st->tx_ctx, pkt, buf, len);\n else\n ret = rtp_parse_packet(rtsp_st->tx_ctx, pkt, buf, len);\n if (ret < 0)\n goto redo;\n if (ret == 1) {\n rt->cur_tx = rtsp_st->tx_ctx;\n }\n return 0;\n}', 'void\nff_rdt_subscribe_rule (char *cmd, int size,\n int stream_nr, int rule_nr)\n{\n av_strlcatf(cmd, size, "stream=%d;rule=%d,stream=%d;rule=%d",\n stream_nr, rule_nr * 2, stream_nr, rule_nr * 2 + 1);\n}', 'size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)\n{\n int len = strlen(dst);\n va_list vl;\n va_start(vl, fmt);\n len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);\n va_end(vl);\n return len;\n}']
35,880
0
https://github.com/openssl/openssl/blob/1d3159bccaa400d6966005b9bc49cca1f6719962/crypto/bn/bntest.c/#L1721
int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_) { BIGNUM *a,*b,*c,*d; int i; b=BN_new(); c=BN_new(); d=BN_new(); BN_one(c); if(a_) a=a_; else { a=BN_new(); BN_bntest_rand(a,200,0,0); a->neg=rand_neg(); } for (i=0; i<num0; i++) { BN_lshift(b,a,i+1); BN_add(c,c,c); if (bp != NULL) { if (!results) { BN_print(bp,a); BIO_puts(bp," * "); BN_print(bp,c); BIO_puts(bp," - "); } BN_print(bp,b); BIO_puts(bp,"\n"); } BN_mul(d,a,c,ctx); BN_sub(d,d,b); if(!BN_is_zero(d)) { fprintf(stderr,"Left shift test failed!\n"); fprintf(stderr,"a="); BN_print_fp(stderr,a); fprintf(stderr,"\nb="); BN_print_fp(stderr,b); fprintf(stderr,"\nc="); BN_print_fp(stderr,c); fprintf(stderr,"\nd="); BN_print_fp(stderr,d); fprintf(stderr,"\n"); return 0; } } BN_free(a); BN_free(b); BN_free(c); BN_free(d); return(1); }
['int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)\n\t{\n\tBIGNUM *a,*b,*c,*d;\n\tint i;\n\tb=BN_new();\n\tc=BN_new();\n\td=BN_new();\n\tBN_one(c);\n\tif(a_)\n\t a=a_;\n\telse\n\t {\n\t a=BN_new();\n\t BN_bntest_rand(a,200,0,0);\n\t a->neg=rand_neg();\n\t }\n\tfor (i=0; i<num0; i++)\n\t\t{\n\t\tBN_lshift(b,a,i+1);\n\t\tBN_add(c,c,c);\n\t\tif (bp != NULL)\n\t\t\t{\n\t\t\tif (!results)\n\t\t\t\t{\n\t\t\t\tBN_print(bp,a);\n\t\t\t\tBIO_puts(bp," * ");\n\t\t\t\tBN_print(bp,c);\n\t\t\t\tBIO_puts(bp," - ");\n\t\t\t\t}\n\t\t\tBN_print(bp,b);\n\t\t\tBIO_puts(bp,"\\n");\n\t\t\t}\n\t\tBN_mul(d,a,c,ctx);\n\t\tBN_sub(d,d,b);\n\t\tif(!BN_is_zero(d))\n\t\t {\n\t\t fprintf(stderr,"Left shift test failed!\\n");\n\t\t fprintf(stderr,"a=");\n\t\t BN_print_fp(stderr,a);\n\t\t fprintf(stderr,"\\nb=");\n\t\t BN_print_fp(stderr,b);\n\t\t fprintf(stderr,"\\nc=");\n\t\t BN_print_fp(stderr,c);\n\t\t fprintf(stderr,"\\nd=");\n\t\t BN_print_fp(stderr,d);\n\t\t fprintf(stderr,"\\n");\n\t\t return 0;\n\t\t }\n\t\t}\n\tBN_free(a);\n\tBN_free(b);\n\tBN_free(c);\n\tBN_free(d);\n\treturn(1);\n\t}', 'BIGNUM *BN_new(void)\n\t{\n\tBIGNUM *ret;\n\tif ((ret=(BIGNUM *)OPENSSL_malloc(sizeof(BIGNUM))) == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->flags=BN_FLG_MALLOCED;\n\tret->top=0;\n\tret->neg=0;\n\tret->dmax=0;\n\tret->d=NULL;\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\textern unsigned char cleanse_ctr;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n if(ret && (num > 2048))\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'int BIO_puts(BIO *b, const char *in)\n\t{\n\tint i;\n\tlong (*cb)();\n\tif ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL))\n\t\t{\n\t\tBIOerr(BIO_F_BIO_PUTS,BIO_R_UNSUPPORTED_METHOD);\n\t\treturn(-2);\n\t\t}\n\tcb=b->callback;\n\tif ((cb != NULL) &&\n\t\t((i=(int)cb(b,BIO_CB_PUTS,in,0,0L,1L)) <= 0))\n\t\t\treturn(i);\n\tif (!b->init)\n\t\t{\n\t\tBIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITIALIZED);\n\t\treturn(-2);\n\t\t}\n\ti=b->method->bputs(b,in);\n\tif (i > 0) b->num_write+=(unsigned long)i;\n\tif (cb != NULL)\n\t\ti=(int)cb(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0,\n\t\t\t0L,(long)i);\n\treturn(i);\n\t}']
35,881
0
https://github.com/openssl/openssl/blob/8ae173bb57819a23717fd3c8e7c51cb62f4268d0/crypto/bn/bn_ctx.c/#L300
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int rsa_check_prime_factor(BIGNUM *p, BIGNUM *e, int nbits, BN_CTX *ctx)\n{\n int checks = bn_rsa_fips186_4_prime_MR_min_checks(nbits);\n int ret = 0;\n BIGNUM *p1 = NULL, *gcd = NULL;\n if (BN_is_prime_fasttest_ex(p, checks, ctx, 1, NULL) != 1\n || rsa_check_prime_factor_range(p, nbits, ctx) != 1)\n return 0;\n BN_CTX_start(ctx);\n p1 = BN_CTX_get(ctx);\n gcd = BN_CTX_get(ctx);\n ret = (gcd != NULL)\n && (BN_copy(p1, p) != NULL)\n && BN_sub_word(p1, 1)\n && BN_gcd(gcd, p1, e, ctx)\n && BN_is_one(gcd);\n BN_clear(p1);\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_is_prime_fasttest_ex(const BIGNUM *w, int checks, BN_CTX *ctx,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, status, ret = -1;\n#ifndef FIPS_MODE\n BN_CTX *ctxlocal = NULL;\n#else\n if (ctx == NULL)\n return -1;\n#endif\n if (BN_cmp(w, BN_value_one()) <= 0)\n return 0;\n if (BN_is_odd(w)) {\n if (BN_is_word(w, 3))\n return 1;\n } else {\n return BN_is_word(w, 2);\n }\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG mod = BN_mod_word(w, primes[i]);\n if (mod == (BN_ULONG)-1)\n return -1;\n if (mod == 0)\n return BN_is_word(w, primes[i]);\n }\n if (!BN_GENCB_call(cb, 1, -1))\n return -1;\n }\n#ifndef FIPS_MODE\n if (ctx == NULL && (ctxlocal = ctx = BN_CTX_new()) == NULL)\n goto err;\n#endif\n ret = bn_miller_rabin_is_prime(w, checks, ctx, cb, 0, &status);\n if (!ret)\n goto err;\n ret = (status == BN_PRIMETEST_PROBABLY_PRIME);\nerr:\n#ifndef FIPS_MODE\n BN_CTX_free(ctxlocal);\n#endif\n return ret;\n}', 'int rsa_check_prime_factor_range(const BIGNUM *p, int nbits, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *tmp, *low;\n nbits >>= 1;\n if (BN_num_bits(p) != nbits)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n low = BN_CTX_get(ctx);\n if (low == NULL || !BN_set_word(tmp, 0xB504F334))\n goto err;\n if (nbits >= 32) {\n if (!BN_lshift(low, tmp, nbits - 32))\n goto err;\n } else if (!BN_rshift(low, tmp, 32 - nbits)) {\n goto err;\n }\n if (BN_cmp(p, low) < 0)\n goto err;\n ret = 1;\nerr:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
35,882
0
https://github.com/libav/libav/blob/f5968788bb3692f2fd503bb2ec1526b0369c7f92/libavformat/utils.c/#L1832
static void av_estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset) { AVPacket pkt1, *pkt = &pkt1; AVStream *st; int read_size, i, ret; int64_t end_time, start_time[MAX_STREAMS]; int64_t filesize, offset, duration; int retry=0; ic->cur_st = NULL; flush_packet_queue(ic); for(i=0;i<ic->nb_streams;i++) { st = ic->streams[i]; if(st->start_time != AV_NOPTS_VALUE){ start_time[i]= st->start_time; }else if(st->first_dts != AV_NOPTS_VALUE){ start_time[i]= st->first_dts; }else av_log(st->codec, AV_LOG_WARNING, "start time is not set in av_estimate_timings_from_pts\n"); if (st->parser) { av_parser_close(st->parser); st->parser= NULL; av_free_packet(&st->cur_pkt); } } filesize = ic->file_size; end_time = AV_NOPTS_VALUE; do{ offset = filesize - (DURATION_MAX_READ_SIZE<<retry); if (offset < 0) offset = 0; url_fseek(ic->pb, offset, SEEK_SET); read_size = 0; for(;;) { if (read_size >= DURATION_MAX_READ_SIZE<<(FFMAX(retry-1,0))) break; do{ ret = av_read_packet(ic, pkt); }while(ret == AVERROR(EAGAIN)); if (ret != 0) break; read_size += pkt->size; st = ic->streams[pkt->stream_index]; if (pkt->pts != AV_NOPTS_VALUE && start_time[pkt->stream_index] != AV_NOPTS_VALUE) { end_time = pkt->pts; duration = end_time - start_time[pkt->stream_index]; if (duration < 0) duration += 1LL<<st->pts_wrap_bits; if (duration > 0) { if (st->duration == AV_NOPTS_VALUE || st->duration < duration) st->duration = duration; } } av_free_packet(pkt); } }while( end_time==AV_NOPTS_VALUE && filesize > (DURATION_MAX_READ_SIZE<<retry) && ++retry <= DURATION_MAX_RETRY); fill_all_stream_timings(ic); url_fseek(ic->pb, old_offset, SEEK_SET); for(i=0; i<ic->nb_streams; i++){ st= ic->streams[i]; st->cur_dts= st->first_dts; st->last_IP_pts = AV_NOPTS_VALUE; } }
['static void av_estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)\n{\n AVPacket pkt1, *pkt = &pkt1;\n AVStream *st;\n int read_size, i, ret;\n int64_t end_time, start_time[MAX_STREAMS];\n int64_t filesize, offset, duration;\n int retry=0;\n ic->cur_st = NULL;\n flush_packet_queue(ic);\n for(i=0;i<ic->nb_streams;i++) {\n st = ic->streams[i];\n if(st->start_time != AV_NOPTS_VALUE){\n start_time[i]= st->start_time;\n }else if(st->first_dts != AV_NOPTS_VALUE){\n start_time[i]= st->first_dts;\n }else\n av_log(st->codec, AV_LOG_WARNING, "start time is not set in av_estimate_timings_from_pts\\n");\n if (st->parser) {\n av_parser_close(st->parser);\n st->parser= NULL;\n av_free_packet(&st->cur_pkt);\n }\n }\n filesize = ic->file_size;\n end_time = AV_NOPTS_VALUE;\n do{\n offset = filesize - (DURATION_MAX_READ_SIZE<<retry);\n if (offset < 0)\n offset = 0;\n url_fseek(ic->pb, offset, SEEK_SET);\n read_size = 0;\n for(;;) {\n if (read_size >= DURATION_MAX_READ_SIZE<<(FFMAX(retry-1,0)))\n break;\n do{\n ret = av_read_packet(ic, pkt);\n }while(ret == AVERROR(EAGAIN));\n if (ret != 0)\n break;\n read_size += pkt->size;\n st = ic->streams[pkt->stream_index];\n if (pkt->pts != AV_NOPTS_VALUE &&\n start_time[pkt->stream_index] != AV_NOPTS_VALUE) {\n end_time = pkt->pts;\n duration = end_time - start_time[pkt->stream_index];\n if (duration < 0)\n duration += 1LL<<st->pts_wrap_bits;\n if (duration > 0) {\n if (st->duration == AV_NOPTS_VALUE ||\n st->duration < duration)\n st->duration = duration;\n }\n }\n av_free_packet(pkt);\n }\n }while( end_time==AV_NOPTS_VALUE\n && filesize > (DURATION_MAX_READ_SIZE<<retry)\n && ++retry <= DURATION_MAX_RETRY);\n fill_all_stream_timings(ic);\n url_fseek(ic->pb, old_offset, SEEK_SET);\n for(i=0; i<ic->nb_streams; i++){\n st= ic->streams[i];\n st->cur_dts= st->first_dts;\n st->last_IP_pts = AV_NOPTS_VALUE;\n }\n}']
35,883
0
https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/asn1/asn1_lib.c/#L101
int ASN1_get_object(unsigned char **pp, long *plength, int *ptag, int *pclass, long omax) { int i,ret; long l; unsigned char *p= *pp; int tag,xclass,inf; long max=omax; if (!max) goto err; ret=(*p&V_ASN1_CONSTRUCTED); xclass=(*p&V_ASN1_PRIVATE); i= *p&V_ASN1_PRIMITIVE_TAG; if (i == V_ASN1_PRIMITIVE_TAG) { p++; if (--max == 0) goto err; l=0; while (*p&0x80) { l<<=7L; l|= *(p++)&0x7f; if (--max == 0) goto err; } l<<=7L; l|= *(p++)&0x7f; tag=(int)l; } else { tag=i; p++; if (--max == 0) goto err; } *ptag=tag; *pclass=xclass; if (!asn1_get_length(&p,&inf,plength,(int)max)) goto err; #if 0 fprintf(stderr,"p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\n", (int)p,*plength,omax,(int)*pp,(int)(p+ *plength), (int)(omax+ *pp)); #endif #if 0 if ((p+ *plength) > (omax+ *pp)) { ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_TOO_LONG); ret|=0x80; } #endif *pp=p; return(ret|inf); err: ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_HEADER_TOO_LONG); return(0x80); }
['int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK *chain)\n\t{\n\tEVP_PKEY *ktmp=NULL,*ktmp2;\n\tint i,j;\n\tif ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey)) return(1);\n\tfor (i=0; i<sk_num(chain); i++)\n\t\t{\n\t\tktmp=X509_get_pubkey((X509 *)sk_value(chain,i));\n\t\tif (ktmp == NULL)\n\t\t\t{\n\t\t\tX509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);\n\t\t\treturn(0);\n\t\t\t}\n\t\tif (!EVP_PKEY_missing_parameters(ktmp))\n\t\t\tbreak;\n\t\telse\n\t\t\t{\n\t\t\tEVP_PKEY_free(ktmp);\n\t\t\tktmp=NULL;\n\t\t\t}\n\t\t}\n\tif (ktmp == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);\n\t\treturn(0);\n\t\t}\n\tfor (j=i-1; j >= 0; j--)\n\t\t{\n\t\tktmp2=X509_get_pubkey((X509 *)sk_value(chain,j));\n\t\tEVP_PKEY_copy_parameters(ktmp2,ktmp);\n\t\tEVP_PKEY_free(ktmp2);\n\t\t}\n\tif (pkey != NULL) EVP_PKEY_copy_parameters(pkey,ktmp);\n\tEVP_PKEY_free(ktmp);\n\treturn(1);\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}', 'EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)\n\t{\n\tEVP_PKEY *ret=NULL;\n\tlong j;\n\tint type;\n\tunsigned char *p;\n#ifndef NO_DSA\n\tX509_ALGOR *a;\n#endif\n\tif (key == NULL) goto err;\n\tif (key->pkey != NULL)\n\t {\n\t CRYPTO_add(&key->pkey->references,1,CRYPTO_LOCK_EVP_PKEY);\n\t return(key->pkey);\n\t }\n\tif (key->public_key == NULL) goto err;\n\ttype=OBJ_obj2nid(key->algor->algorithm);\n\tp=key->public_key->data;\n j=key->public_key->length;\n if ((ret=d2i_PublicKey(type,NULL,&p,(long)j)) == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET,X509_R_ERR_ASN1_LIB);\n\t\tgoto err;\n\t\t}\n\tret->save_parameters=0;\n#ifndef NO_DSA\n\ta=key->algor;\n\tif (ret->type == EVP_PKEY_DSA)\n\t\t{\n\t\tif (a->parameter->type == V_ASN1_SEQUENCE)\n\t\t\t{\n\t\t\tret->pkey.dsa->write_params=0;\n\t\t\tp=a->parameter->value.sequence->data;\n\t\t\tj=a->parameter->value.sequence->length;\n\t\t\tif (!d2i_DSAparams(&ret->pkey.dsa,&p,(long)j))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\tret->save_parameters=1;\n\t\t}\n#endif\n\tkey->pkey=ret;\n\tCRYPTO_add(&ret->references,1,CRYPTO_LOCK_EVP_PKEY);\n\treturn(ret);\nerr:\n\tif (ret != NULL)\n\t\tEVP_PKEY_free(ret);\n\treturn(NULL);\n\t}', 'DSA *d2i_DSAparams(DSA **a, unsigned char **pp, long length)\n\t{\n\tint i=ERR_R_NESTED_ASN1_ERROR;\n\tASN1_INTEGER *bs=NULL;\n\tM_ASN1_D2I_vars(a,DSA *,DSA_new);\n\tM_ASN1_D2I_Init();\n\tM_ASN1_D2I_start_sequence();\n\tM_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);\n\tif ((ret->p=BN_bin2bn(bs->data,bs->length,ret->p)) == NULL) goto err_bn;\n\tM_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);\n\tif ((ret->q=BN_bin2bn(bs->data,bs->length,ret->q)) == NULL) goto err_bn;\n\tM_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);\n\tif ((ret->g=BN_bin2bn(bs->data,bs->length,ret->g)) == NULL) goto err_bn;\n\tASN1_BIT_STRING_free(bs);\n\tM_ASN1_D2I_Finish_2(a);\nerr_bn:\n\ti=ERR_R_BN_LIB;\nerr:\n\tASN1err(ASN1_F_D2I_DSAPARAMS,i);\n\tif ((ret != NULL) && ((a == NULL) || (*a != ret))) DSA_free(ret);\n\tif (bs != NULL) ASN1_BIT_STRING_free(bs);\n\treturn(NULL);\n\t}', 'ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **a, unsigned char **pp,\n\t long length)\n\t{\n\tASN1_INTEGER *ret=NULL;\n\tunsigned char *p,*to,*s;\n\tlong len;\n\tint inf,tag,xclass;\n\tint i;\n\tif ((a == NULL) || ((*a) == NULL))\n\t\t{\n\t\tif ((ret=ASN1_INTEGER_new()) == NULL) return(NULL);\n\t\tret->type=V_ASN1_INTEGER;\n\t\t}\n\telse\n\t\tret=(*a);\n\tp= *pp;\n\tinf=ASN1_get_object(&p,&len,&tag,&xclass,length);\n\tif (inf & 0x80)\n\t\t{\n\t\ti=ASN1_R_BAD_OBJECT_HEADER;\n\t\tgoto err;\n\t\t}\n\tif (tag != V_ASN1_INTEGER)\n\t\t{\n\t\ti=ASN1_R_EXPECTING_AN_INTEGER;\n\t\tgoto err;\n\t\t}\n\ts=(unsigned char *)Malloc((int)len+1);\n\tif (s == NULL)\n\t\t{\n\t\ti=ERR_R_MALLOC_FAILURE;\n\t\tgoto err;\n\t\t}\n\tto=s;\n\tif (*p & 0x80)\n\t\t{\n\t\tret->type=V_ASN1_NEG_INTEGER;\n\t\tif ((*p == 0xff) && (len != 1)) {\n\t\t\tp++;\n\t\t\tlen--;\n\t\t}\n\t\ti = len;\n\t\tp += i - 1;\n\t\tto += i - 1;\n\t\twhile((!*p) && i) {\n\t\t\t*(to--) = 0;\n\t\t\ti--;\n\t\t\tp--;\n\t\t}\n\t\tif(!i) {\n\t\t\t*s = 1;\n\t\t\ts[len] = 0;\n\t\t\tp += len;\n\t\t\tlen++;\n\t\t} else {\n\t\t\t*(to--) = (*(p--) ^ 0xff) + 1;\n\t\t\ti--;\n\t\t\tfor(;i > 0; i--) *(to--) = *(p--) ^ 0xff;\n\t\t\tp += len;\n\t\t}\n\t} else {\n\t\tret->type=V_ASN1_INTEGER;\n\t\tif ((*p == 0) && (len != 1))\n\t\t\t{\n\t\t\tp++;\n\t\t\tlen--;\n\t\t\t}\n\t\tmemcpy(s,p,(int)len);\n\t\tp+=len;\n\t}\n\tif (ret->data != NULL) Free((char *)ret->data);\n\tret->data=s;\n\tret->length=(int)len;\n\tif (a != NULL) (*a)=ret;\n\t*pp=p;\n\treturn(ret);\nerr:\n\tASN1err(ASN1_F_D2I_ASN1_INTEGER,i);\n\tif ((ret != NULL) && ((a == NULL) || (*a != ret)))\n\t\tASN1_INTEGER_free(ret);\n\treturn(NULL);\n\t}', 'int asn1_GetSequence(ASN1_CTX *c, long *length)\n\t{\n\tunsigned char *q;\n\tq=c->p;\n\tc->inf=ASN1_get_object(&(c->p),&(c->slen),&(c->tag),&(c->xclass),\n\t\t*length);\n\tif (c->inf & 0x80)\n\t\t{\n\t\tc->error=ERR_R_BAD_GET_ASN1_OBJECT_CALL;\n\t\treturn(0);\n\t\t}\n\tif (c->tag != V_ASN1_SEQUENCE)\n\t\t{\n\t\tc->error=ERR_R_EXPECTING_AN_ASN1_SEQUENCE;\n\t\treturn(0);\n\t\t}\n\t(*length)-=(c->p-q);\n\tif (c->max && (*length < 0))\n\t\t{\n\t\tc->error=ERR_R_ASN1_LENGTH_MISMATCH;\n\t\treturn(0);\n\t\t}\n\tif (c->inf == (1|V_ASN1_CONSTRUCTED))\n\t\tc->slen= *length+ *(c->pp)-c->p;\n\tc->eos=0;\n\treturn(1);\n\t}', 'int ASN1_get_object(unsigned char **pp, long *plength, int *ptag, int *pclass,\n\t long omax)\n\t{\n\tint i,ret;\n\tlong l;\n\tunsigned char *p= *pp;\n\tint tag,xclass,inf;\n\tlong max=omax;\n\tif (!max) goto err;\n\tret=(*p&V_ASN1_CONSTRUCTED);\n\txclass=(*p&V_ASN1_PRIVATE);\n\ti= *p&V_ASN1_PRIMITIVE_TAG;\n\tif (i == V_ASN1_PRIMITIVE_TAG)\n\t\t{\n\t\tp++;\n\t\tif (--max == 0) goto err;\n\t\tl=0;\n\t\twhile (*p&0x80)\n\t\t\t{\n\t\t\tl<<=7L;\n\t\t\tl|= *(p++)&0x7f;\n\t\t\tif (--max == 0) goto err;\n\t\t\t}\n\t\tl<<=7L;\n\t\tl|= *(p++)&0x7f;\n\t\ttag=(int)l;\n\t\t}\n\telse\n\t\t{\n\t\ttag=i;\n\t\tp++;\n\t\tif (--max == 0) goto err;\n\t\t}\n\t*ptag=tag;\n\t*pclass=xclass;\n\tif (!asn1_get_length(&p,&inf,plength,(int)max)) goto err;\n#if 0\n\tfprintf(stderr,"p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\\n",\n\t\t(int)p,*plength,omax,(int)*pp,(int)(p+ *plength),\n\t\t(int)(omax+ *pp));\n#endif\n#if 0\n\tif ((p+ *plength) > (omax+ *pp))\n\t\t{\n\t\tASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_TOO_LONG);\n\t\tret|=0x80;\n\t\t}\n#endif\n\t*pp=p;\n\treturn(ret|inf);\nerr:\n\tASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_HEADER_TOO_LONG);\n\treturn(0x80);\n\t}']
35,884
0
https://github.com/openssl/openssl/blob/5d1c09de1f2736e1d4b1877206d08455ec75f558/crypto/bn/bn_lib.c/#L97
int BN_num_bits_word(BN_ULONG l) { BN_ULONG x, mask; int bits = (l != 0); #if BN_BITS2 > 32 x = l >> 32; mask = (0 - x) & BN_MASK2; mask = (0 - (mask >> (BN_BITS2 - 1))); bits += 32 & mask; l ^= (x ^ l) & mask; #endif x = l >> 16; mask = (0 - x) & BN_MASK2; mask = (0 - (mask >> (BN_BITS2 - 1))); bits += 16 & mask; l ^= (x ^ l) & mask; x = l >> 8; mask = (0 - x) & BN_MASK2; mask = (0 - (mask >> (BN_BITS2 - 1))); bits += 8 & mask; l ^= (x ^ l) & mask; x = l >> 4; mask = (0 - x) & BN_MASK2; mask = (0 - (mask >> (BN_BITS2 - 1))); bits += 4 & mask; l ^= (x ^ l) & mask; x = l >> 2; mask = (0 - x) & BN_MASK2; mask = (0 - (mask >> (BN_BITS2 - 1))); bits += 2 & mask; l ^= (x ^ l) & mask; x = l >> 1; mask = (0 - x) & BN_MASK2; mask = (0 - (mask >> (BN_BITS2 - 1))); bits += 1 & mask; return bits; }
['static int test_expmodzero(void)\n{\n BIGNUM *a = NULL, *r = NULL, *zero = NULL;\n int st = 0;\n if (!TEST_ptr(zero = BN_new())\n || !TEST_ptr(a = BN_new())\n || !TEST_ptr(r = BN_new()))\n goto err;\n BN_zero(zero);\n if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),\n NULL, NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,\n BN_value_one(),\n NULL, NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,\n BN_value_one(), NULL, NULL))\n || !TEST_BN_eq_zero(r))\n goto err;\n st = 1;\nerr:\n BN_free(zero);\n BN_free(a);\n BN_free(r);\n return st;\n}', 'const BIGNUM *BN_value_one(void)\n{\n static const BN_ULONG data_one = 1L;\n static const BIGNUM const_one =\n { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };\n return &const_one;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_RECP_CTX_free(&recp);\n bn_check_top(r);\n return ret;\n}', 'int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!BN_copy(&(recp->N), d))\n return 0;\n BN_zero(&(recp->Nr));\n recp->num_bits = BN_num_bits(d);\n recp->shift = 0;\n return 1;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_num_bits_word(BN_ULONG l)\n{\n BN_ULONG x, mask;\n int bits = (l != 0);\n#if BN_BITS2 > 32\n x = l >> 32;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 32 & mask;\n l ^= (x ^ l) & mask;\n#endif\n x = l >> 16;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 16 & mask;\n l ^= (x ^ l) & mask;\n x = l >> 8;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 8 & mask;\n l ^= (x ^ l) & mask;\n x = l >> 4;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 4 & mask;\n l ^= (x ^ l) & mask;\n x = l >> 2;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 2 & mask;\n l ^= (x ^ l) & mask;\n x = l >> 1;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 1 & mask;\n return bits;\n}']
35,885
0
https://github.com/libav/libav/blob/6ac0e7818399a57e4684202bac79f35b3561ad1e/libavcodec/bitstream.h/#L139
static inline uint64_t get_val(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1); bc->bits >>= n; #else uint64_t ret = bc->bits >> (64 - n); bc->bits <<= n; #endif bc->bits_left -= n; return ret; }
['static int vp9_raw_reorder_frame_parse(AVBSFContext *bsf, VP9RawReorderFrame *frame)\n{\n BitstreamContext bc;\n int err;\n unsigned int frame_marker;\n unsigned int profile_low_bit, profile_high_bit, reserved_zero;\n unsigned int error_resilient_mode;\n unsigned int frame_sync_code;\n err = bitstream_init8(&bc, frame->packet->data, frame->packet->size);\n if (err)\n return err;\n frame_marker = bitstream_read(&bc, 2);\n if (frame_marker != 2) {\n av_log(bsf, AV_LOG_ERROR, "Invalid frame marker: %u.\\n",\n frame_marker);\n return AVERROR_INVALIDDATA;\n }\n profile_low_bit = bitstream_read_bit(&bc);\n profile_high_bit = bitstream_read_bit(&bc);\n frame->profile = (profile_high_bit << 1) | profile_low_bit;\n if (frame->profile == 3) {\n reserved_zero = bitstream_read_bit(&bc);\n if (reserved_zero != 0) {\n av_log(bsf, AV_LOG_ERROR, "Profile reserved_zero bit set: "\n "unsupported profile or invalid bitstream.\\n");\n return AVERROR_INVALIDDATA;\n }\n }\n frame->show_existing_frame = bitstream_read_bit(&bc);\n if (frame->show_existing_frame) {\n frame->frame_to_show = bitstream_read(&bc, 3);\n return 0;\n }\n frame->frame_type = bitstream_read_bit(&bc);\n frame->show_frame = bitstream_read_bit(&bc);\n error_resilient_mode = bitstream_read_bit(&bc);\n if (frame->frame_type == 0) {\n frame_sync_code = bitstream_read(&bc, 24);\n if (frame_sync_code != 0x498342) {\n av_log(bsf, AV_LOG_ERROR, "Invalid frame sync code: %06x.\\n",\n frame_sync_code);\n return AVERROR_INVALIDDATA;\n }\n frame->refresh_frame_flags = 0xff;\n } else {\n unsigned int intra_only;\n if (frame->show_frame == 0)\n intra_only = bitstream_read_bit(&bc);\n else\n intra_only = 0;\n if (error_resilient_mode == 0) {\n bitstream_skip(&bc, 2);\n }\n if (intra_only) {\n frame_sync_code = bitstream_read(&bc, 24);\n if (frame_sync_code != 0x498342) {\n av_log(bsf, AV_LOG_ERROR, "Invalid frame sync code: "\n "%06x.\\n", frame_sync_code);\n return AVERROR_INVALIDDATA;\n }\n if (frame->profile > 0) {\n unsigned int color_space;\n if (frame->profile >= 2) {\n bitstream_skip(&bc, 1);\n }\n color_space = bitstream_read(&bc, 3);\n if (color_space != 7 ) {\n bitstream_skip(&bc, 1);\n if (frame->profile == 1 || frame->profile == 3) {\n bitstream_skip(&bc, 3);\n }\n } else {\n if (frame->profile == 1 || frame->profile == 3)\n bitstream_skip(&bc, 1);\n }\n }\n frame->refresh_frame_flags = bitstream_read(&bc, 8);\n } else {\n frame->refresh_frame_flags = bitstream_read(&bc, 8);\n }\n }\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
35,886
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L364
int BN_set_word(BIGNUM *a, BN_ULONG w) { bn_check_top(a); if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL) return 0; a->neg = 0; a->d[0] = w; a->top = (w ? 1 : 0); a->flags &= ~BN_FLG_FIXED_TOP; bn_check_top(a); return 1; }
['static int dh_builtin_genparams(DH *ret, int prime_len, int generator,\n BN_GENCB *cb)\n{\n BIGNUM *t1, *t2;\n int g, ok = -1;\n BN_CTX *ctx = NULL;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n t2 = BN_CTX_get(ctx);\n if (t2 == NULL)\n goto err;\n if (!ret->p && ((ret->p = BN_new()) == NULL))\n goto err;\n if (!ret->g && ((ret->g = BN_new()) == NULL))\n goto err;\n if (generator <= 1) {\n DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);\n goto err;\n }\n if (generator == DH_GENERATOR_2) {\n if (!BN_set_word(t1, 24))\n goto err;\n if (!BN_set_word(t2, 11))\n goto err;\n g = 2;\n } else if (generator == DH_GENERATOR_5) {\n if (!BN_set_word(t1, 10))\n goto err;\n if (!BN_set_word(t2, 3))\n goto err;\n g = 5;\n } else {\n if (!BN_set_word(t1, 2))\n goto err;\n if (!BN_set_word(t2, 1))\n goto err;\n g = generator;\n }\n if (!BN_generate_prime_ex(ret->p, prime_len, 1, t1, t2, cb))\n goto err;\n if (!BN_GENCB_call(cb, 3, 0))\n goto err;\n if (!BN_set_word(ret->g, g))\n goto err;\n ok = 1;\n err:\n if (ok == -1) {\n DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB);\n ok = 0;\n }\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n }\n return ok;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}']
35,887
0
https://github.com/libav/libav/blob/dad7a9c7c0ae8ebc56f2e3a24e6fa4da5c2cd491/libavcodec/bitstream.h/#L139
static inline uint64_t get_val(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1); bc->bits >>= n; #else uint64_t ret = bc->bits >> (64 - n); bc->bits <<= n; #endif bc->bits_left -= n; return ret; }
['static int decode_frame(WmallDecodeCtx *s)\n{\n BitstreamContext *bc = &s->bc;\n int more_frames = 0, len = 0, i, ret;\n s->frame->nb_samples = s->samples_per_frame;\n if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0) {\n av_log(s->avctx, AV_LOG_ERROR,\n "not enough space for the output samples\\n");\n s->packet_loss = 1;\n return ret;\n }\n for (i = 0; i < s->num_channels; i++) {\n s->samples_16[i] = (int16_t *)s->frame->extended_data[i];\n s->samples_32[i] = (int32_t *)s->frame->extended_data[i];\n }\n if (s->len_prefix)\n len = bitstream_read(bc, s->log2_frame_size);\n if (decode_tilehdr(s)) {\n s->packet_loss = 1;\n return 0;\n }\n if (s->dynamic_range_compression)\n s->drc_gain = bitstream_read(bc, 8);\n if (bitstream_read_bit(bc)) {\n int av_unused skip;\n if (bitstream_read_bit(bc)) {\n skip = bitstream_read(bc, av_log2(s->samples_per_frame * 2));\n ff_dlog(s->avctx, "start skip: %i\\n", skip);\n }\n if (bitstream_read_bit(bc)) {\n skip = bitstream_read(bc, av_log2(s->samples_per_frame * 2));\n ff_dlog(s->avctx, "end skip: %i\\n", skip);\n }\n }\n s->parsed_all_subframes = 0;\n for (i = 0; i < s->num_channels; i++) {\n s->channel[i].decoded_samples = 0;\n s->channel[i].cur_subframe = 0;\n }\n while (!s->parsed_all_subframes) {\n if (decode_subframe(s) < 0) {\n s->packet_loss = 1;\n return 0;\n }\n }\n ff_dlog(s->avctx, "Frame done\\n");\n if (s->skip_frame)\n s->skip_frame = 0;\n if (s->len_prefix) {\n if (len != (bitstream_tell(bc) - s->frame_offset) + 2) {\n av_log(s->avctx, AV_LOG_ERROR,\n "frame[%"PRIu32"] would have to skip %i bits\\n",\n s->frame_num,\n len - (bitstream_tell(bc) - s->frame_offset) - 1);\n s->packet_loss = 1;\n return 0;\n }\n bitstream_skip(bc, len - (bitstream_tell(bc) - s->frame_offset) - 1);\n }\n more_frames = bitstream_read_bit(bc);\n ++s->frame_num;\n return more_frames;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
35,888
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L260
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['static int test_gf2m_add(void)\n{\n BIGNUM *a = NULL, *b = NULL, *c = NULL;\n int i, st = 0;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_ptr(c = BN_new()))\n goto err;\n for (i = 0; i < NUM0; i++) {\n BN_rand(a, 512, 0, 0);\n BN_copy(b, BN_value_one());\n a->neg = rand_neg();\n b->neg = rand_neg();\n BN_GF2m_add(c, a, b);\n if (!TEST_false((BN_is_odd(a) && BN_is_odd(c))\n || (!BN_is_odd(a) && !BN_is_odd(c))))\n goto err;\n BN_GF2m_add(c, c, c);\n if (!TEST_BN_eq_zero(c))\n goto err;\n }\n st = 1;\n err:\n BN_free(a);\n BN_free(b);\n BN_free(c);\n return st;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(a, BN_FLG_CONSTTIME);\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(NORMAL, rnd, bits, top, bottom);\n}', 'static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int b, ret = 0, bit, bytes, mask;\n if (bits == 0) {\n if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)\n goto toosmall;\n BN_zero(rnd);\n return 1;\n }\n if (bits < 0 || (bits == 1 && top > 0))\n goto toosmall;\n bytes = (bits + 7) / 8;\n bit = (bits - 1) % 8;\n mask = 0xff << (bit + 1);\n buf = OPENSSL_malloc(bytes);\n if (buf == NULL) {\n BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n b = flag == NORMAL ? RAND_bytes(buf, bytes) : RAND_priv_bytes(buf, bytes);\n if (b <= 0)\n goto err;\n if (flag == TESTING) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return ret;\ntoosmall:\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
35,889
0
https://gitlab.com/libtiff/libtiff/blob/163627448aa8d2893582f2546dd85706586e6243/libtiff/tif_getimage.c/#L2236
static int makebwmap(TIFFRGBAImage* img) { TIFFRGBValue* Map = img->Map; int bitspersample = img->bitspersample; int nsamples = 8 / bitspersample; int i; uint32* p; if( nsamples == 0 ) nsamples = 1; img->BWmap = (uint32**) _TIFFmalloc( 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32))); if (img->BWmap == NULL) { TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table"); return (0); } p = (uint32*)(img->BWmap + 256); for (i = 0; i < 256; i++) { TIFFRGBValue c; img->BWmap[i] = p; switch (bitspersample) { #define GREY(x) c = Map[x]; *p++ = PACK(c,c,c); case 1: GREY(i>>7); GREY((i>>6)&1); GREY((i>>5)&1); GREY((i>>4)&1); GREY((i>>3)&1); GREY((i>>2)&1); GREY((i>>1)&1); GREY(i&1); break; case 2: GREY(i>>6); GREY((i>>4)&3); GREY((i>>2)&3); GREY(i&3); break; case 4: GREY(i>>4); GREY(i&0xf); break; case 8: case 16: GREY(i); break; } #undef GREY } return (1); }
['static int\nsetupMap(TIFFRGBAImage* img)\n{\n int32 x, range;\n range = (int32)((1L<<img->bitspersample)-1);\n if( img->bitspersample == 16 )\n range = (int32) 255;\n img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));\n if (img->Map == NULL) {\n\t\tTIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),\n\t\t\t"No space for photometric conversion table");\n\t\treturn (0);\n }\n if (img->photometric == PHOTOMETRIC_MINISWHITE) {\n\tfor (x = 0; x <= range; x++)\n\t img->Map[x] = (TIFFRGBValue) (((range - x) * 255) / range);\n } else {\n\tfor (x = 0; x <= range; x++)\n\t img->Map[x] = (TIFFRGBValue) ((x * 255) / range);\n }\n if (img->bitspersample <= 16 &&\n\t(img->photometric == PHOTOMETRIC_MINISBLACK ||\n\t img->photometric == PHOTOMETRIC_MINISWHITE)) {\n\tif (!makebwmap(img))\n\t return (0);\n\t_TIFFfree(img->Map), img->Map = NULL;\n }\n return (1);\n}', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n if (s == 0)\n return ((void *) NULL);\n\treturn (malloc((size_t) s));\n}', 'static int\nmakebwmap(TIFFRGBAImage* img)\n{\n TIFFRGBValue* Map = img->Map;\n int bitspersample = img->bitspersample;\n int nsamples = 8 / bitspersample;\n int i;\n uint32* p;\n if( nsamples == 0 )\n nsamples = 1;\n img->BWmap = (uint32**) _TIFFmalloc(\n\t256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));\n if (img->BWmap == NULL) {\n\t\tTIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table");\n\t\treturn (0);\n }\n p = (uint32*)(img->BWmap + 256);\n for (i = 0; i < 256; i++) {\n\tTIFFRGBValue c;\n\timg->BWmap[i] = p;\n\tswitch (bitspersample) {\n#define\tGREY(x)\tc = Map[x]; *p++ = PACK(c,c,c);\n\tcase 1:\n\t GREY(i>>7);\n\t GREY((i>>6)&1);\n\t GREY((i>>5)&1);\n\t GREY((i>>4)&1);\n\t GREY((i>>3)&1);\n\t GREY((i>>2)&1);\n\t GREY((i>>1)&1);\n\t GREY(i&1);\n\t break;\n\tcase 2:\n\t GREY(i>>6);\n\t GREY((i>>4)&3);\n\t GREY((i>>2)&3);\n\t GREY(i&3);\n\t break;\n\tcase 4:\n\t GREY(i>>4);\n\t GREY(i&0xf);\n\t break;\n\tcase 8:\n case 16:\n\t GREY(i);\n\t break;\n\t}\n#undef\tGREY\n }\n return (1);\n}']
35,890
0
https://github.com/openssl/openssl/blob/c7af8b0a267981c25cc42643493289a01ffe1bbd/test/evp_test.c/#L1749
static int encode_test_init(EVP_TEST *t, const char *encoding) { ENCODE_DATA *edata; if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata)))) return 0; if (strcmp(encoding, "canonical") == 0) { edata->encoding = BASE64_CANONICAL_ENCODING; } else if (strcmp(encoding, "valid") == 0) { edata->encoding = BASE64_VALID_ENCODING; } else if (strcmp(encoding, "invalid") == 0) { edata->encoding = BASE64_INVALID_ENCODING; if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR"))) return 0; } else { TEST_error("Bad encoding: %s." " Should be one of {canonical, valid, invalid}", encoding); return 0; } t->data = edata; return 1; }
['static int encode_test_init(EVP_TEST *t, const char *encoding)\n{\n ENCODE_DATA *edata;\n if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))\n return 0;\n if (strcmp(encoding, "canonical") == 0) {\n edata->encoding = BASE64_CANONICAL_ENCODING;\n } else if (strcmp(encoding, "valid") == 0) {\n edata->encoding = BASE64_VALID_ENCODING;\n } else if (strcmp(encoding, "invalid") == 0) {\n edata->encoding = BASE64_INVALID_ENCODING;\n if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))\n return 0;\n } else {\n TEST_error("Bad encoding: %s."\n " Should be one of {canonical, valid, invalid}",\n encoding);\n return 0;\n }\n t->data = edata;\n return 1;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int test_ptr(const char *file, int line, const char *s, const void *p)\n{\n if (p != NULL)\n return 1;\n test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);\n return 0;\n}', 'char *CRYPTO_strdup(const char *str, const char* file, int line)\n{\n char *ret;\n if (str == NULL)\n return NULL;\n ret = CRYPTO_malloc(strlen(str) + 1, file, line);\n if (ret != NULL)\n strcpy(ret, str);\n return ret;\n}']
35,891
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_shift.c/#L112
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { int i, nw, lb, rb; BN_ULONG *t, *f; BN_ULONG l; bn_check_top(r); bn_check_top(a); if (n < 0) { BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT); return 0; } nw = n / BN_BITS2; if (bn_wexpand(r, a->top + nw + 1) == NULL) return 0; r->neg = a->neg; lb = n % BN_BITS2; rb = BN_BITS2 - lb; f = a->d; t = r->d; t[a->top + nw] = 0; if (lb == 0) for (i = a->top - 1; i >= 0; i--) t[nw + i] = f[i]; else for (i = a->top - 1; i >= 0; i--) { l = f[i]; t[nw + i + 1] |= (l >> rb) & BN_MASK2; t[nw + i] = (l << lb) & BN_MASK2; } memset(t, 0, sizeof(*t) * nw); r->top = a->top + nw + 1; bn_correct_top(r); bn_check_top(r); return 1; }
['static int file_modexp(STANZA *s)\n{\n BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;\n BIGNUM *b = NULL, *c = NULL, *d = NULL;\n int st = 0;\n if (!TEST_ptr(a = getBN(s, "A"))\n || !TEST_ptr(e = getBN(s, "E"))\n || !TEST_ptr(m = getBN(s, "M"))\n || !TEST_ptr(mod_exp = getBN(s, "ModExp"))\n || !TEST_ptr(ret = BN_new())\n || !TEST_ptr(d = BN_new()))\n goto err;\n if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))\n || !equalBN("A ^ E (mod M)", mod_exp, ret))\n goto err;\n if (BN_is_odd(m)) {\n if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))\n || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)\n || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,\n ctx, NULL))\n || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))\n goto err;\n }\n BN_hex2bn(&a, "050505050505");\n BN_hex2bn(&b, "02");\n BN_hex2bn(&c,\n "4141414141414141414141274141414141414141414141414141414141414141"\n "4141414141414141414141414141414141414141414141414141414141414141"\n "4141414141414141414141800000000000000000000000000000000000000000"\n "0000000000000000000000000000000000000000000000000000000000000000"\n "0000000000000000000000000000000000000000000000000000000000000000"\n "0000000000000000000000000000000000000000000000000000000001");\n if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))\n || !TEST_true(BN_mul(e, a, a, ctx))\n || !TEST_BN_eq(d, e))\n goto err;\n st = 1;\nerr:\n BN_free(a);\n BN_free(b);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n BN_free(m);\n BN_free(mod_exp);\n BN_free(ret);\n return st;\n}', 'static BIGNUM *getBN(STANZA *s, const char *attribute)\n{\n const char *hex;\n BIGNUM *ret = NULL;\n if ((hex = findattr(s, attribute)) == NULL) {\n TEST_error("%s:%d: Can\'t find %s", s->test_file, s->start, attribute);\n return NULL;\n }\n if (parseBN(&ret, hex) != (int)strlen(hex)) {\n TEST_error("Could not decode \'%s\'", hex);\n return NULL;\n }\n return ret;\n}', 'static int parseBN(BIGNUM **out, const char *in)\n{\n *out = NULL;\n return BN_hex2bn(out, in);\n}', "int BN_hex2bn(BIGNUM **bn, const char *a)\n{\n BIGNUM *ret = NULL;\n BN_ULONG l = 0;\n int neg = 0, h, m, i, j, k, c;\n int num;\n if (a == NULL || *a == '\\0')\n return 0;\n if (*a == '-') {\n neg = 1;\n a++;\n }\n for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)\n continue;\n if (i == 0 || i > INT_MAX / 4)\n goto err;\n num = i + neg;\n if (bn == NULL)\n return num;\n if (*bn == NULL) {\n if ((ret = BN_new()) == NULL)\n return 0;\n } else {\n ret = *bn;\n BN_zero(ret);\n }\n if (bn_expand(ret, i * 4) == NULL)\n goto err;\n j = i;\n m = 0;\n h = 0;\n while (j > 0) {\n m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;\n l = 0;\n for (;;) {\n c = a[j - m];\n k = OPENSSL_hexchar2int(c);\n if (k < 0)\n k = 0;\n l = (l << 4) | k;\n if (--m <= 0) {\n ret->d[h++] = l;\n break;\n }\n }\n j -= BN_BYTES * 2;\n }\n ret->top = h;\n bn_correct_top(ret);\n *bn = ret;\n bn_check_top(ret);\n if (ret->top != 0)\n ret->neg = neg;\n return num;\n err:\n if (*bn == NULL)\n BN_free(ret);\n return 0;\n}", 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (BN_is_zero(aa)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n bn_correct_top(r);\n } else\n#endif\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n top = m->top;\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_mod(&am, a, m, ctx))\n goto err;\n if (!BN_to_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits >= 0) {\n if (bits < stride)\n stride = bits + 1;\n bits -= stride;\n wvalue = bn_get_bits(p, bits + 1);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7)\n while (bits >= 0) {\n for (wvalue = 0, i = 0; i < 5; i++, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n } else {\n while (bits >= 0) {\n wvalue = bn_get_bits5(p->d, bits - 4);\n bits -= 5;\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n bits--;\n for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n while (bits >= 0) {\n wvalue = 0;\n for (i = 0; i < window; i++, bits--) {\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n }\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return 0;\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
35,892
0
https://github.com/openssl/openssl/blob/43a0449fe6ce18b750803be8a115a412a7235496/crypto/bn/bn_lib.c/#L271
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['static int generate_key(DH *dh)\n{\n int ok = 0;\n int generate_new_key = 0;\n unsigned l;\n BN_CTX *ctx;\n BN_MONT_CTX *mont = NULL;\n BIGNUM *pub_key = NULL, *priv_key = NULL;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n if (dh->priv_key == NULL) {\n priv_key = BN_secure_new();\n if (priv_key == NULL)\n goto err;\n generate_new_key = 1;\n } else\n priv_key = dh->priv_key;\n if (dh->pub_key == NULL) {\n pub_key = BN_new();\n if (pub_key == NULL)\n goto err;\n } else\n pub_key = dh->pub_key;\n if (dh->flags & DH_FLAG_CACHE_MONT_P) {\n mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,\n dh->lock, dh->p, ctx);\n if (!mont)\n goto err;\n }\n if (generate_new_key) {\n if (dh->q) {\n do {\n if (!BN_rand_range(priv_key, dh->q))\n goto err;\n }\n while (BN_is_zero(priv_key) || BN_is_one(priv_key));\n } else {\n l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;\n if (!BN_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))\n goto err;\n }\n }\n {\n BIGNUM *prk = BN_new();\n if (prk == NULL)\n goto err;\n BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);\n if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {\n BN_free(prk);\n goto err;\n }\n BN_free(prk);\n }\n dh->pub_key = pub_key;\n dh->priv_key = priv_key;\n ok = 1;\n err:\n if (ok != 1)\n DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);\n if (pub_key != dh->pub_key)\n BN_free(pub_key);\n if (priv_key != dh->priv_key)\n BN_free(priv_key);\n BN_CTX_free(ctx);\n return (ok);\n}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(0, rnd, bits, top, bottom);\n}', 'static int bnrand(int testing, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int ret = 0, bit, bytes, mask;\n time_t tim;\n if (bits == 0) {\n if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)\n goto toosmall;\n BN_zero(rnd);\n return 1;\n }\n if (bits < 0 || (bits == 1 && top > 0))\n goto toosmall;\n bytes = (bits + 7) / 8;\n bit = (bits - 1) % 8;\n mask = 0xff << (bit + 1);\n buf = OPENSSL_malloc(bytes);\n if (buf == NULL) {\n BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n time(&tim);\n RAND_add(&tim, sizeof(tim), 0.0);\n if (RAND_bytes(buf, bytes) <= 0)\n goto err;\n if (testing) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return (ret);\ntoosmall:\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
35,893
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L352
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *A, *a = NULL; const BN_ULONG *B; int i; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b,BN_FLG_SECURE)) a = A = OPENSSL_secure_malloc(words * sizeof(*a)); else a = A = OPENSSL_malloc(words * sizeof(*a)); if (A == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } #ifdef PURIFY memset(a, 0, sizeof(*a) * words); #endif #if 1 B = b->d; if (B != NULL) { for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { BN_ULONG a0, a1, a2, a3; a0 = B[0]; a1 = B[1]; a2 = B[2]; a3 = B[3]; A[0] = a0; A[1] = a1; A[2] = a2; A[3] = a3; } switch (b->top & 3) { case 3: A[2] = B[2]; case 2: A[1] = B[1]; case 1: A[0] = B[0]; case 0: ; } } #else memset(A, 0, sizeof(*A) * words); memcpy(A, b->d, sizeof(b->d[0]) * b->top); #endif return (a); }
['int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int i;\n int ret = -2;\n int err = 0;\n BIGNUM *A, *B, *tmp;\n static const int tab[8] = { 0, 1, 0, -1, 0, -1, 0, 1 };\n bn_check_top(a);\n bn_check_top(b);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n if (B == NULL)\n goto end;\n err = !BN_copy(A, a);\n if (err)\n goto end;\n err = !BN_copy(B, b);\n if (err)\n goto end;\n if (BN_is_zero(B)) {\n ret = BN_abs_is_word(A, 1);\n goto end;\n }\n if (!BN_is_odd(A) && !BN_is_odd(B)) {\n ret = 0;\n goto end;\n }\n i = 0;\n while (!BN_is_bit_set(B, i))\n i++;\n err = !BN_rshift(B, B, i);\n if (err)\n goto end;\n if (i & 1) {\n ret = tab[BN_lsw(A) & 7];\n } else {\n ret = 1;\n }\n if (B->neg) {\n B->neg = 0;\n if (A->neg)\n ret = -ret;\n }\n while (1) {\n if (BN_is_zero(A)) {\n ret = BN_is_one(B) ? ret : 0;\n goto end;\n }\n i = 0;\n while (!BN_is_bit_set(A, i))\n i++;\n err = !BN_rshift(A, A, i);\n if (err)\n goto end;\n if (i & 1) {\n ret = ret * tab[BN_lsw(B) & 7];\n }\n if ((A->neg ? ~BN_lsw(A) : BN_lsw(A)) & BN_lsw(B) & 2)\n ret = -ret;\n err = !BN_nnmod(B, B, A, ctx);\n if (err)\n goto end;\n tmp = A;\n A = B;\n B = tmp;\n tmp->neg = 0;\n }\n end:\n BN_CTX_end(ctx);\n if (err)\n return -2;\n else\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n int i;\n BN_ULONG *A;\n const BN_ULONG *B;\n bn_check_top(b);\n if (a == b)\n return (a);\n if (bn_wexpand(a, b->top) == NULL)\n return (NULL);\n#if 1\n A = a->d;\n B = b->d;\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:;\n }\n#else\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n#endif\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return (a);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_malloc(words * sizeof(*a));\n else\n a = A = OPENSSL_malloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#ifdef PURIFY\n memset(a, 0, sizeof(*a) * words);\n#endif\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
35,894
0
https://github.com/openssl/openssl/blob/02cba628daa7fea959c561531a8a984756bdf41c/crypto/bn/bn_shift.c/#L112
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { int i, nw, lb, rb; BN_ULONG *t, *f; BN_ULONG l; bn_check_top(r); bn_check_top(a); if (n < 0) { BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT); return 0; } nw = n / BN_BITS2; if (bn_wexpand(r, a->top + nw + 1) == NULL) return (0); r->neg = a->neg; lb = n % BN_BITS2; rb = BN_BITS2 - lb; f = a->d; t = r->d; t[a->top + nw] = 0; if (lb == 0) for (i = a->top - 1; i >= 0; i--) t[nw + i] = f[i]; else for (i = a->top - 1; i >= 0; i--) { l = f[i]; t[nw + i + 1] |= (l >> rb) & BN_MASK2; t[nw + i] = (l << lb) & BN_MASK2; } memset(t, 0, sizeof(*t) * nw); r->top = a->top + nw + 1; bn_correct_top(r); bn_check_top(r); return (1); }
['static int file_quotient(STANZA *s)\n{\n BIGNUM *a = getBN(s, "A");\n BIGNUM *b = getBN(s, "B");\n BIGNUM *quotient = getBN(s, "Quotient");\n BIGNUM *remainder = getBN(s, "Remainder");\n BIGNUM *ret = BN_new();\n BIGNUM *ret2 = BN_new();\n BIGNUM *nnmod = BN_new();\n BN_ULONG b_word, ret_word;\n int st = 0;\n if (a == NULL || b == NULL || quotient == NULL || remainder == NULL\n || ret == NULL || ret2 == NULL || nnmod == NULL)\n goto err;\n if (!BN_div(ret, ret2, a, b, ctx)\n || !equalBN("A / B", quotient, ret)\n || !equalBN("A % B", remainder, ret2)\n || !BN_mul(ret, quotient, b, ctx)\n || !BN_add(ret, ret, remainder)\n || !equalBN("Quotient * B + Remainder", a, ret))\n goto err;\n b_word = BN_get_word(b);\n if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {\n BN_ULONG remainder_word = BN_get_word(remainder);\n assert(remainder_word != (BN_ULONG)-1);\n if (!BN_copy(ret, a))\n goto err;\n ret_word = BN_div_word(ret, b_word);\n if (ret_word != remainder_word) {\n#ifdef BN_DEC_FMT1\n fprintf(stderr,\n "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "\\n",\n ret_word, remainder_word);\n#else\n fprintf(stderr, "Got A %% B (word) mismatch\\n");\n#endif\n goto err;\n }\n if (!equalBN ("A / B (word)", quotient, ret))\n goto err;\n ret_word = BN_mod_word(a, b_word);\n if (ret_word != remainder_word) {\n#ifdef BN_DEC_FMT1\n fprintf(stderr,\n "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "\\n",\n ret_word, remainder_word);\n#else\n fprintf(stderr, "Got A %% B (word) mismatch\\n");\n#endif\n goto err;\n }\n }\n if (!BN_is_negative(b)) {\n if (!BN_copy(nnmod, remainder)\n || (BN_is_negative(nnmod) && !BN_add(nnmod, nnmod, b))\n || !BN_nnmod(ret, a, b, ctx)\n || !equalBN("A % B (non-negative)", nnmod, ret))\n goto err;\n }\n st = 1;\nerr:\n BN_free(a);\n BN_free(b);\n BN_free(quotient);\n BN_free(remainder);\n BN_free(ret);\n BN_free(ret2);\n BN_free(nnmod);\n return st;\n}', 'static BIGNUM *getBN(STANZA *s, const char *attribute)\n{\n const char *hex;\n BIGNUM *ret = NULL;\n if ((hex = findattr(s, attribute)) == NULL) {\n fprintf(stderr, "Can\'t find %s in test at line %d\\n",\n attribute, s->start);\n return NULL;\n }\n if (parseBN(&ret, hex) != (int)strlen(hex)) {\n fprintf(stderr, "Could not decode \'%s\'.\\n", hex);\n return NULL;\n }\n return ret;\n}', 'static int parseBN(BIGNUM **out, const char *in)\n{\n *out = NULL;\n return BN_hex2bn(out, in);\n}', "int BN_hex2bn(BIGNUM **bn, const char *a)\n{\n BIGNUM *ret = NULL;\n BN_ULONG l = 0;\n int neg = 0, h, m, i, j, k, c;\n int num;\n if ((a == NULL) || (*a == '\\0'))\n return (0);\n if (*a == '-') {\n neg = 1;\n a++;\n }\n for (i = 0; i <= (INT_MAX/4) && isxdigit((unsigned char)a[i]); i++)\n continue;\n if (i == 0 || i > INT_MAX/4)\n goto err;\n num = i + neg;\n if (bn == NULL)\n return (num);\n if (*bn == NULL) {\n if ((ret = BN_new()) == NULL)\n return (0);\n } else {\n ret = *bn;\n BN_zero(ret);\n }\n if (bn_expand(ret, i * 4) == NULL)\n goto err;\n j = i;\n m = 0;\n h = 0;\n while (j > 0) {\n m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j;\n l = 0;\n for (;;) {\n c = a[j - m];\n k = OPENSSL_hexchar2int(c);\n if (k < 0)\n k = 0;\n l = (l << 4) | k;\n if (--m <= 0) {\n ret->d[h++] = l;\n break;\n }\n }\n j -= (BN_BYTES * 2);\n }\n ret->top = h;\n bn_correct_top(ret);\n *bn = ret;\n bn_check_top(ret);\n if (ret->top != 0)\n ret->neg = neg;\n return (num);\n err:\n if (*bn == NULL)\n BN_free(ret);\n return (0);\n}", 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
35,895
0
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_lib.c/#L232
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!bn_sqr_fixed_top(tmp, a, ctx))\n goto err;\n } else {\n if (!bn_mul_fixed_top(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!bn_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
35,896
0
https://github.com/openssl/openssl/blob/4b8515baa6edef1a771f9e4e3fbc0395b4a629e8/crypto/bn/bn_ctx.c/#L273
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int test_modexp_mont5()\n{\n BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;\n BIGNUM *b = NULL, *n = NULL, *c = NULL;\n BN_MONT_CTX *mont = NULL;\n char *bigstring;\n int st = 0;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(p = BN_new())\n || !TEST_ptr(m = BN_new())\n || !TEST_ptr(d = BN_new())\n || !TEST_ptr(e = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_ptr(n = BN_new())\n || !TEST_ptr(c = BN_new())\n || !TEST_ptr(mont = BN_MONT_CTX_new()))\n goto err;\n BN_bntest_rand(m, 1024, 0, 1);\n BN_bntest_rand(a, 1024, 0, 0);\n BN_zero(p);\n if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))\n goto err;\n if (!TEST_BN_eq_one(d))\n goto err;\n BN_hex2bn(&a,\n "7878787878787878787878787878787878787878787878787878787878787878"\n "7878787878787878787878787878787878787878787878787878787878787878"\n "7878787878787878787878787878787878787878787878787878787878787878"\n "7878787878787878787878787878787878787878787878787878787878787878");\n BN_hex2bn(&b,\n "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"\n "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"\n "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"\n "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");\n BN_hex2bn(&n,\n "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"\n "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"\n "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"\n "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");\n BN_MONT_CTX_set(mont, n, ctx);\n BN_mod_mul_montgomery(c, a, b, mont, ctx);\n BN_mod_mul_montgomery(d, b, a, mont, ctx);\n if (!TEST_BN_eq(c, d))\n goto err;\n bigstring = glue(bn1strings);\n BN_hex2bn(&n, bigstring);\n OPENSSL_free(bigstring);\n bigstring = glue(bn2strings);\n BN_hex2bn(&a, bigstring);\n OPENSSL_free(bigstring);\n BN_free(b);\n b = BN_dup(a);\n BN_MONT_CTX_set(mont, n, ctx);\n BN_mod_mul_montgomery(c, a, a, mont, ctx);\n BN_mod_mul_montgomery(d, a, b, mont, ctx);\n if (!TEST_BN_eq(c, d))\n goto err;\n BN_bntest_rand(p, 1024, 0, 0);\n BN_zero(a);\n if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))\n || !TEST_BN_eq_zero(d))\n goto err;\n BN_one(a);\n BN_MONT_CTX_set(mont, m, ctx);\n if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))\n || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))\n || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))\n || !TEST_BN_eq(a, d))\n goto err;\n BN_bntest_rand(e, 1024, 0, 0);\n if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))\n || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))\n || !TEST_BN_eq(a, d))\n goto err;\n st = 1;\nerr:\n BN_MONT_CTX_free(mont);\n BN_free(a);\n BN_free(p);\n BN_free(m);\n BN_free(d);\n BN_free(e);\n BN_free(b);\n BN_free(n);\n BN_free(c);\n return st;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n top = m->top;\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_mod(&am, a, m, ctx))\n goto err;\n if (!BN_to_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits >= 0) {\n if (bits < stride)\n stride = bits + 1;\n bits -= stride;\n wvalue = bn_get_bits(p, bits + 1);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7)\n while (bits >= 0) {\n for (wvalue = 0, i = 0; i < 5; i++, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n } else {\n while (bits >= 0) {\n wvalue = bn_get_bits5(p->d, bits - 4);\n bits -= 5;\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n bits--;\n for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n while (bits >= 0) {\n wvalue = 0;\n for (i = 0; i < window; i++, bits--) {\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n }\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return (ret);\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n {\n BIGNUM local_B;\n bn_init(&local_B);\n BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, &local_B, A, ctx))\n goto err;\n }\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n {\n BIGNUM local_A;\n bn_init(&local_A);\n BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, &local_A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
35,897
0
https://github.com/openssl/openssl/blob/a26d8be9531862af09c69b9704d219f1768d3d0e/crypto/bio/b_print.c/#L425
static int _dopr(char **sbuffer, char **buffer, size_t *maxlen, size_t *retlen, int *truncated, const char *format, va_list args) { char ch; LLONG value; LDOUBLE fvalue; char *strvalue; int min; int max; int state; int flags; int cflags; size_t currlen; state = DP_S_DEFAULT; flags = currlen = cflags = min = 0; max = -1; ch = *format++; while (state != DP_S_DONE) { if (ch == '\0' || (buffer == NULL && currlen >= *maxlen)) state = DP_S_DONE; switch (state) { case DP_S_DEFAULT: if (ch == '%') state = DP_S_FLAGS; else if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch)) return 0; ch = *format++; break; case DP_S_FLAGS: switch (ch) { case '-': flags |= DP_F_MINUS; ch = *format++; break; case '+': flags |= DP_F_PLUS; ch = *format++; break; case ' ': flags |= DP_F_SPACE; ch = *format++; break; case '#': flags |= DP_F_NUM; ch = *format++; break; case '0': flags |= DP_F_ZERO; ch = *format++; break; default: state = DP_S_MIN; break; } break; case DP_S_MIN: if (isdigit((unsigned char)ch)) { min = 10 * min + char_to_int(ch); ch = *format++; } else if (ch == '*') { min = va_arg(args, int); ch = *format++; state = DP_S_DOT; } else state = DP_S_DOT; break; case DP_S_DOT: if (ch == '.') { state = DP_S_MAX; ch = *format++; } else state = DP_S_MOD; break; case DP_S_MAX: if (isdigit((unsigned char)ch)) { if (max < 0) max = 0; max = 10 * max + char_to_int(ch); ch = *format++; } else if (ch == '*') { max = va_arg(args, int); ch = *format++; state = DP_S_MOD; } else state = DP_S_MOD; break; case DP_S_MOD: switch (ch) { case 'h': cflags = DP_C_SHORT; ch = *format++; break; case 'l': if (*format == 'l') { cflags = DP_C_LLONG; format++; } else cflags = DP_C_LONG; ch = *format++; break; case 'q': cflags = DP_C_LLONG; ch = *format++; break; case 'L': cflags = DP_C_LDOUBLE; ch = *format++; break; default: break; } state = DP_S_CONV; break; case DP_S_CONV: switch (ch) { case 'd': case 'i': switch (cflags) { case DP_C_SHORT: value = (short int)va_arg(args, int); break; case DP_C_LONG: value = va_arg(args, long int); break; case DP_C_LLONG: value = va_arg(args, LLONG); break; default: value = va_arg(args, int); break; } if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min, max, flags)) return 0; break; case 'X': flags |= DP_F_UP; case 'x': case 'o': case 'u': flags |= DP_F_UNSIGNED; switch (cflags) { case DP_C_SHORT: value = (unsigned short int)va_arg(args, unsigned int); break; case DP_C_LONG: value = (LLONG) va_arg(args, unsigned long int); break; case DP_C_LLONG: value = va_arg(args, unsigned LLONG); break; default: value = (LLONG) va_arg(args, unsigned int); break; } if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, ch == 'o' ? 8 : (ch == 'u' ? 10 : 16), min, max, flags)) return 0; break; case 'f': if (cflags == DP_C_LDOUBLE) fvalue = va_arg(args, LDOUBLE); else fvalue = va_arg(args, double); if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max, flags)) return 0; break; case 'E': flags |= DP_F_UP; case 'e': if (cflags == DP_C_LDOUBLE) fvalue = va_arg(args, LDOUBLE); else fvalue = va_arg(args, double); break; case 'G': flags |= DP_F_UP; case 'g': if (cflags == DP_C_LDOUBLE) fvalue = va_arg(args, LDOUBLE); else fvalue = va_arg(args, double); break; case 'c': if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, va_arg(args, int))) return 0; break; case 's': strvalue = va_arg(args, char *); if (max < 0) { if (buffer) max = INT_MAX; else max = *maxlen; } if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue, flags, min, max)) return 0; break; case 'p': value = (size_t)va_arg(args, void *); if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 16, min, max, flags | DP_F_NUM)) return 0; break; case 'n': if (cflags == DP_C_SHORT) { short int *num; num = va_arg(args, short int *); *num = currlen; } else if (cflags == DP_C_LONG) { long int *num; num = va_arg(args, long int *); *num = (long int)currlen; } else if (cflags == DP_C_LLONG) { LLONG *num; num = va_arg(args, LLONG *); *num = (LLONG) currlen; } else { int *num; num = va_arg(args, int *); *num = currlen; } break; case '%': if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch)) return 0; break; case 'w': ch = *format++; break; default: break; } ch = *format++; state = DP_S_DEFAULT; flags = cflags = min = 0; max = -1; break; case DP_S_DONE: break; default: break; } } *truncated = (currlen > *maxlen - 1); if (*truncated) currlen = *maxlen - 1; if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0')) return 0; *retlen = currlen - 1; return 1; }
['long BIO_debug_callback(BIO *bio, int cmd, const char *argp,\n int argi, long argl, long ret)\n{\n BIO *b;\n char buf[256];\n char *p;\n long r = 1;\n int len;\n size_t p_maxlen;\n if (BIO_CB_RETURN & cmd)\n r = ret;\n len = BIO_snprintf(buf,sizeof buf,"BIO[%p]: ",(void *)bio);\n p = buf + len;\n p_maxlen = sizeof(buf) - len;\n switch (cmd) {\n case BIO_CB_FREE:\n BIO_snprintf(p, p_maxlen, "Free - %s\\n", bio->method->name);\n break;\n case BIO_CB_READ:\n if (bio->method->type & BIO_TYPE_DESCRIPTOR)\n BIO_snprintf(p, p_maxlen, "read(%d,%lu) - %s fd=%d\\n",\n bio->num, (unsigned long)argi,\n bio->method->name, bio->num);\n else\n BIO_snprintf(p, p_maxlen, "read(%d,%lu) - %s\\n",\n bio->num, (unsigned long)argi, bio->method->name);\n break;\n case BIO_CB_WRITE:\n if (bio->method->type & BIO_TYPE_DESCRIPTOR)\n BIO_snprintf(p, p_maxlen, "write(%d,%lu) - %s fd=%d\\n",\n bio->num, (unsigned long)argi,\n bio->method->name, bio->num);\n else\n BIO_snprintf(p, p_maxlen, "write(%d,%lu) - %s\\n",\n bio->num, (unsigned long)argi, bio->method->name);\n break;\n case BIO_CB_PUTS:\n BIO_snprintf(p, p_maxlen, "puts() - %s\\n", bio->method->name);\n break;\n case BIO_CB_GETS:\n BIO_snprintf(p, p_maxlen, "gets(%lu) - %s\\n", (unsigned long)argi,\n bio->method->name);\n break;\n case BIO_CB_CTRL:\n BIO_snprintf(p, p_maxlen, "ctrl(%lu) - %s\\n", (unsigned long)argi,\n bio->method->name);\n break;\n case BIO_CB_RETURN | BIO_CB_READ:\n BIO_snprintf(p, p_maxlen, "read return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_WRITE:\n BIO_snprintf(p, p_maxlen, "write return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_GETS:\n BIO_snprintf(p, p_maxlen, "gets return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_PUTS:\n BIO_snprintf(p, p_maxlen, "puts return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_CTRL:\n BIO_snprintf(p, p_maxlen, "ctrl return %ld\\n", ret);\n break;\n default:\n BIO_snprintf(p, p_maxlen, "bio callback - unknown type (%d)\\n", cmd);\n break;\n }\n b = (BIO *)bio->cb_arg;\n if (b != NULL)\n BIO_write(b, buf, strlen(buf));\n#if !defined(OPENSSL_NO_STDIO)\n else\n fputs(buf, stderr);\n#endif\n return (r);\n}', 'int BIO_snprintf(char *buf, size_t n, const char *format, ...)\n{\n va_list args;\n int ret;\n va_start(args, format);\n ret = BIO_vsnprintf(buf, n, format, args);\n va_end(args);\n return (ret);\n}', 'int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)\n{\n size_t retlen;\n int truncated;\n if(!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))\n return -1;\n if (truncated)\n return -1;\n else\n return (retlen <= INT_MAX) ? (int)retlen : -1;\n}', "static int\n_dopr(char **sbuffer,\n char **buffer,\n size_t *maxlen,\n size_t *retlen, int *truncated, const char *format, va_list args)\n{\n char ch;\n LLONG value;\n LDOUBLE fvalue;\n char *strvalue;\n int min;\n int max;\n int state;\n int flags;\n int cflags;\n size_t currlen;\n state = DP_S_DEFAULT;\n flags = currlen = cflags = min = 0;\n max = -1;\n ch = *format++;\n while (state != DP_S_DONE) {\n if (ch == '\\0' || (buffer == NULL && currlen >= *maxlen))\n state = DP_S_DONE;\n switch (state) {\n case DP_S_DEFAULT:\n if (ch == '%')\n state = DP_S_FLAGS;\n else\n if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))\n return 0;\n ch = *format++;\n break;\n case DP_S_FLAGS:\n switch (ch) {\n case '-':\n flags |= DP_F_MINUS;\n ch = *format++;\n break;\n case '+':\n flags |= DP_F_PLUS;\n ch = *format++;\n break;\n case ' ':\n flags |= DP_F_SPACE;\n ch = *format++;\n break;\n case '#':\n flags |= DP_F_NUM;\n ch = *format++;\n break;\n case '0':\n flags |= DP_F_ZERO;\n ch = *format++;\n break;\n default:\n state = DP_S_MIN;\n break;\n }\n break;\n case DP_S_MIN:\n if (isdigit((unsigned char)ch)) {\n min = 10 * min + char_to_int(ch);\n ch = *format++;\n } else if (ch == '*') {\n min = va_arg(args, int);\n ch = *format++;\n state = DP_S_DOT;\n } else\n state = DP_S_DOT;\n break;\n case DP_S_DOT:\n if (ch == '.') {\n state = DP_S_MAX;\n ch = *format++;\n } else\n state = DP_S_MOD;\n break;\n case DP_S_MAX:\n if (isdigit((unsigned char)ch)) {\n if (max < 0)\n max = 0;\n max = 10 * max + char_to_int(ch);\n ch = *format++;\n } else if (ch == '*') {\n max = va_arg(args, int);\n ch = *format++;\n state = DP_S_MOD;\n } else\n state = DP_S_MOD;\n break;\n case DP_S_MOD:\n switch (ch) {\n case 'h':\n cflags = DP_C_SHORT;\n ch = *format++;\n break;\n case 'l':\n if (*format == 'l') {\n cflags = DP_C_LLONG;\n format++;\n } else\n cflags = DP_C_LONG;\n ch = *format++;\n break;\n case 'q':\n cflags = DP_C_LLONG;\n ch = *format++;\n break;\n case 'L':\n cflags = DP_C_LDOUBLE;\n ch = *format++;\n break;\n default:\n break;\n }\n state = DP_S_CONV;\n break;\n case DP_S_CONV:\n switch (ch) {\n case 'd':\n case 'i':\n switch (cflags) {\n case DP_C_SHORT:\n value = (short int)va_arg(args, int);\n break;\n case DP_C_LONG:\n value = va_arg(args, long int);\n break;\n case DP_C_LLONG:\n value = va_arg(args, LLONG);\n break;\n default:\n value = va_arg(args, int);\n break;\n }\n if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,\n max, flags))\n return 0;\n break;\n case 'X':\n flags |= DP_F_UP;\n case 'x':\n case 'o':\n case 'u':\n flags |= DP_F_UNSIGNED;\n switch (cflags) {\n case DP_C_SHORT:\n value = (unsigned short int)va_arg(args, unsigned int);\n break;\n case DP_C_LONG:\n value = (LLONG) va_arg(args, unsigned long int);\n break;\n case DP_C_LLONG:\n value = va_arg(args, unsigned LLONG);\n break;\n default:\n value = (LLONG) va_arg(args, unsigned int);\n break;\n }\n if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,\n ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),\n min, max, flags))\n return 0;\n break;\n case 'f':\n if (cflags == DP_C_LDOUBLE)\n fvalue = va_arg(args, LDOUBLE);\n else\n fvalue = va_arg(args, double);\n if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,\n flags))\n return 0;\n break;\n case 'E':\n flags |= DP_F_UP;\n case 'e':\n if (cflags == DP_C_LDOUBLE)\n fvalue = va_arg(args, LDOUBLE);\n else\n fvalue = va_arg(args, double);\n break;\n case 'G':\n flags |= DP_F_UP;\n case 'g':\n if (cflags == DP_C_LDOUBLE)\n fvalue = va_arg(args, LDOUBLE);\n else\n fvalue = va_arg(args, double);\n break;\n case 'c':\n if(!doapr_outch(sbuffer, buffer, &currlen, maxlen,\n va_arg(args, int)))\n return 0;\n break;\n case 's':\n strvalue = va_arg(args, char *);\n if (max < 0) {\n if (buffer)\n max = INT_MAX;\n else\n max = *maxlen;\n }\n if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,\n flags, min, max))\n return 0;\n break;\n case 'p':\n value = (size_t)va_arg(args, void *);\n if (!fmtint(sbuffer, buffer, &currlen, maxlen,\n value, 16, min, max, flags | DP_F_NUM))\n return 0;\n break;\n case 'n':\n if (cflags == DP_C_SHORT) {\n short int *num;\n num = va_arg(args, short int *);\n *num = currlen;\n } else if (cflags == DP_C_LONG) {\n long int *num;\n num = va_arg(args, long int *);\n *num = (long int)currlen;\n } else if (cflags == DP_C_LLONG) {\n LLONG *num;\n num = va_arg(args, LLONG *);\n *num = (LLONG) currlen;\n } else {\n int *num;\n num = va_arg(args, int *);\n *num = currlen;\n }\n break;\n case '%':\n if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))\n return 0;\n break;\n case 'w':\n ch = *format++;\n break;\n default:\n break;\n }\n ch = *format++;\n state = DP_S_DEFAULT;\n flags = cflags = min = 0;\n max = -1;\n break;\n case DP_S_DONE:\n break;\n default:\n break;\n }\n }\n *truncated = (currlen > *maxlen - 1);\n if (*truncated)\n currlen = *maxlen - 1;\n if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\\0'))\n return 0;\n *retlen = currlen - 1;\n return 1;\n}"]
35,898
0
https://github.com/libav/libav/blob/4391805916a1557278351f25428d0145b1073520/libavcodec/smacker.c/#L611
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { SmackerAudioContext *s = avctx->priv_data; const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; GetBitContext gb; HuffContext h[4]; VLC vlc[4]; int16_t *samples; uint8_t *samples8; int val; int i, res, ret; int unp_size; int bits, stereo; int pred[2] = {0, 0}; if (buf_size <= 4) { av_log(avctx, AV_LOG_ERROR, "packet is too small\n"); return AVERROR(EINVAL); } unp_size = AV_RL32(buf); init_get_bits(&gb, buf + 4, (buf_size - 4) * 8); if(!get_bits1(&gb)){ av_log(avctx, AV_LOG_INFO, "Sound: no data\n"); *got_frame_ptr = 0; return 1; } stereo = get_bits1(&gb); bits = get_bits1(&gb); if (stereo ^ (avctx->channels != 1)) { av_log(avctx, AV_LOG_ERROR, "channels mismatch\n"); return AVERROR(EINVAL); } if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) { av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n"); return AVERROR(EINVAL); } s->frame.nb_samples = unp_size / (avctx->channels * (bits + 1)); if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } samples = (int16_t *)s->frame.data[0]; samples8 = s->frame.data[0]; memset(vlc, 0, sizeof(VLC) * 4); memset(h, 0, sizeof(HuffContext) * 4); for(i = 0; i < (1 << (bits + stereo)); i++) { h[i].length = 256; h[i].maxlength = 0; h[i].current = 0; h[i].bits = av_mallocz(256 * 4); h[i].lengths = av_mallocz(256 * sizeof(int)); h[i].values = av_mallocz(256 * sizeof(int)); skip_bits1(&gb); smacker_decode_tree(&gb, &h[i], 0, 0); skip_bits1(&gb); if(h[i].current > 1) { res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length, h[i].lengths, sizeof(int), sizeof(int), h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE); if(res < 0) { av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); return -1; } } } if(bits) { for(i = stereo; i >= 0; i--) pred[i] = av_bswap16(get_bits(&gb, 16)); for(i = 0; i <= stereo; i++) *samples++ = pred[i]; for(; i < unp_size / 2; i++) { if(i & stereo) { if(vlc[2].table) res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3); else res = 0; val = h[2].values[res]; if(vlc[3].table) res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3); else res = 0; val |= h[3].values[res] << 8; pred[1] += sign_extend(val, 16); *samples++ = av_clip_int16(pred[1]); } else { if(vlc[0].table) res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3); else res = 0; val = h[0].values[res]; if(vlc[1].table) res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3); else res = 0; val |= h[1].values[res] << 8; pred[0] += sign_extend(val, 16); *samples++ = av_clip_int16(pred[0]); } } } else { for(i = stereo; i >= 0; i--) pred[i] = get_bits(&gb, 8); for(i = 0; i <= stereo; i++) *samples8++ = pred[i]; for(; i < unp_size; i++) { if(i & stereo){ if(vlc[1].table) res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3); else res = 0; pred[1] += sign_extend(h[1].values[res], 8); *samples8++ = av_clip_uint8(pred[1]); } else { if(vlc[0].table) res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3); else res = 0; pred[0] += sign_extend(h[0].values[res], 8); *samples8++ = av_clip_uint8(pred[0]); } } } for(i = 0; i < 4; i++) { if(vlc[i].table) free_vlc(&vlc[i]); av_free(h[i].bits); av_free(h[i].lengths); av_free(h[i].values); } *got_frame_ptr = 1; *(AVFrame *)data = s->frame; return buf_size; }
['static int smka_decode_frame(AVCodecContext *avctx, void *data,\n int *got_frame_ptr, AVPacket *avpkt)\n{\n SmackerAudioContext *s = avctx->priv_data;\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n GetBitContext gb;\n HuffContext h[4];\n VLC vlc[4];\n int16_t *samples;\n uint8_t *samples8;\n int val;\n int i, res, ret;\n int unp_size;\n int bits, stereo;\n int pred[2] = {0, 0};\n if (buf_size <= 4) {\n av_log(avctx, AV_LOG_ERROR, "packet is too small\\n");\n return AVERROR(EINVAL);\n }\n unp_size = AV_RL32(buf);\n init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);\n if(!get_bits1(&gb)){\n av_log(avctx, AV_LOG_INFO, "Sound: no data\\n");\n *got_frame_ptr = 0;\n return 1;\n }\n stereo = get_bits1(&gb);\n bits = get_bits1(&gb);\n if (stereo ^ (avctx->channels != 1)) {\n av_log(avctx, AV_LOG_ERROR, "channels mismatch\\n");\n return AVERROR(EINVAL);\n }\n if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {\n av_log(avctx, AV_LOG_ERROR, "sample format mismatch\\n");\n return AVERROR(EINVAL);\n }\n s->frame.nb_samples = unp_size / (avctx->channels * (bits + 1));\n if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n samples = (int16_t *)s->frame.data[0];\n samples8 = s->frame.data[0];\n memset(vlc, 0, sizeof(VLC) * 4);\n memset(h, 0, sizeof(HuffContext) * 4);\n for(i = 0; i < (1 << (bits + stereo)); i++) {\n h[i].length = 256;\n h[i].maxlength = 0;\n h[i].current = 0;\n h[i].bits = av_mallocz(256 * 4);\n h[i].lengths = av_mallocz(256 * sizeof(int));\n h[i].values = av_mallocz(256 * sizeof(int));\n skip_bits1(&gb);\n smacker_decode_tree(&gb, &h[i], 0, 0);\n skip_bits1(&gb);\n if(h[i].current > 1) {\n res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,\n h[i].lengths, sizeof(int), sizeof(int),\n h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);\n if(res < 0) {\n av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\\n");\n return -1;\n }\n }\n }\n if(bits) {\n for(i = stereo; i >= 0; i--)\n pred[i] = av_bswap16(get_bits(&gb, 16));\n for(i = 0; i <= stereo; i++)\n *samples++ = pred[i];\n for(; i < unp_size / 2; i++) {\n if(i & stereo) {\n if(vlc[2].table)\n res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val = h[2].values[res];\n if(vlc[3].table)\n res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val |= h[3].values[res] << 8;\n pred[1] += sign_extend(val, 16);\n *samples++ = av_clip_int16(pred[1]);\n } else {\n if(vlc[0].table)\n res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val = h[0].values[res];\n if(vlc[1].table)\n res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val |= h[1].values[res] << 8;\n pred[0] += sign_extend(val, 16);\n *samples++ = av_clip_int16(pred[0]);\n }\n }\n } else {\n for(i = stereo; i >= 0; i--)\n pred[i] = get_bits(&gb, 8);\n for(i = 0; i <= stereo; i++)\n *samples8++ = pred[i];\n for(; i < unp_size; i++) {\n if(i & stereo){\n if(vlc[1].table)\n res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);\n else\n res = 0;\n pred[1] += sign_extend(h[1].values[res], 8);\n *samples8++ = av_clip_uint8(pred[1]);\n } else {\n if(vlc[0].table)\n res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);\n else\n res = 0;\n pred[0] += sign_extend(h[0].values[res], 8);\n *samples8++ = av_clip_uint8(pred[0]);\n }\n }\n }\n for(i = 0; i < 4; i++) {\n if(vlc[i].table)\n free_vlc(&vlc[i]);\n av_free(h[i].bits);\n av_free(h[i].lengths);\n av_free(h[i].values);\n }\n *got_frame_ptr = 1;\n *(AVFrame *)data = s->frame;\n return buf_size;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size = (bit_size+7)>>3;\n if (buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n}', 'static inline unsigned int get_bits1(GetBitContext *s){\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef ALT_BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}']
35,899
1
https://github.com/openssl/openssl/blob/222c3da35cc508446df150a16080943019ba6f54/crypto/evp/evp_enc.c/#L290
int is_partially_overlapping(const void *ptr1, const void *ptr2, int len) { PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2; int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) | (diff > (0 - (PTRDIFF_T)len))); return overlapped; }
['static int enc_read(BIO *b, char *out, int outl)\n{\n int ret = 0, i, blocksize;\n BIO_ENC_CTX *ctx;\n BIO *next;\n if (out == NULL)\n return (0);\n ctx = BIO_get_data(b);\n next = BIO_next(b);\n if ((ctx == NULL) || (next == NULL))\n return 0;\n if (ctx->buf_len > 0) {\n i = ctx->buf_len - ctx->buf_off;\n if (i > outl)\n i = outl;\n memcpy(out, &(ctx->buf[ctx->buf_off]), i);\n ret = i;\n out += i;\n outl -= i;\n ctx->buf_off += i;\n if (ctx->buf_len == ctx->buf_off) {\n ctx->buf_len = 0;\n ctx->buf_off = 0;\n }\n }\n blocksize = EVP_CIPHER_CTX_block_size(ctx->cipher);\n if (blocksize == 1)\n blocksize = 0;\n while (outl > 0) {\n if (ctx->cont <= 0)\n break;\n if (ctx->read_start == ctx->read_end) {\n ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);\n i = BIO_read(next, ctx->read_start, ENC_BLOCK_SIZE);\n if (i > 0)\n ctx->read_end += i;\n } else {\n i = ctx->read_end - ctx->read_start;\n }\n if (i <= 0) {\n if (!BIO_should_retry(next)) {\n ctx->cont = i;\n i = EVP_CipherFinal_ex(ctx->cipher,\n ctx->buf, &(ctx->buf_len));\n ctx->ok = i;\n ctx->buf_off = 0;\n } else {\n ret = (ret == 0) ? i : ret;\n break;\n }\n } else {\n if (outl > ENC_MIN_CHUNK) {\n int j = outl - blocksize, buf_len;\n if (!EVP_CipherUpdate(ctx->cipher,\n (unsigned char *)out, &buf_len,\n ctx->read_start, i > j ? j : i)) {\n BIO_clear_retry_flags(b);\n return 0;\n }\n ret += buf_len;\n out += buf_len;\n outl -= buf_len;\n if ((i -= j) <= 0) {\n ctx->read_start = ctx->read_end;\n continue;\n }\n ctx->read_start += j;\n }\n if (i > ENC_MIN_CHUNK)\n i = ENC_MIN_CHUNK;\n if (!EVP_CipherUpdate(ctx->cipher,\n ctx->buf, &ctx->buf_len,\n ctx->read_start, i)) {\n BIO_clear_retry_flags(b);\n ctx->ok = 0;\n return 0;\n }\n ctx->read_start += i;\n ctx->cont = 1;\n if (ctx->buf_len == 0)\n continue;\n }\n if (ctx->buf_len <= outl)\n i = ctx->buf_len;\n else\n i = outl;\n if (i <= 0)\n break;\n memcpy(out, ctx->buf, i);\n ret += i;\n ctx->buf_off = i;\n outl -= i;\n out += i;\n }\n BIO_clear_retry_flags(b);\n BIO_copy_next_retry(b);\n return ((ret == 0) ? ctx->cont : ret);\n}', 'int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n const unsigned char *in, int inl)\n{\n if (ctx->encrypt)\n return EVP_EncryptUpdate(ctx, out, outl, in, inl);\n else\n return EVP_DecryptUpdate(ctx, out, outl, in, inl);\n}', 'int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n const unsigned char *in, int inl)\n{\n int i, j, bl;\n bl = ctx->cipher->block_size;\n if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {\n if (bl == 1 && is_partially_overlapping(out, in, inl)) {\n EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n i = ctx->cipher->do_cipher(ctx, out, in, inl);\n if (i < 0)\n return 0;\n else\n *outl = i;\n return 1;\n }\n if (inl <= 0) {\n *outl = 0;\n return inl == 0;\n }\n if (is_partially_overlapping(out + ctx->buf_len, in, inl)) {\n EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {\n if (ctx->cipher->do_cipher(ctx, out, in, inl)) {\n *outl = inl;\n return 1;\n } else {\n *outl = 0;\n return 0;\n }\n }\n i = ctx->buf_len;\n OPENSSL_assert(bl <= (int)sizeof(ctx->buf));\n if (i != 0) {\n if (bl - i > inl) {\n memcpy(&(ctx->buf[i]), in, inl);\n ctx->buf_len += inl;\n *outl = 0;\n return 1;\n } else {\n j = bl - i;\n memcpy(&(ctx->buf[i]), in, j);\n inl -= j;\n in += j;\n if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))\n return 0;\n out += bl;\n *outl = bl;\n }\n } else\n *outl = 0;\n i = inl & (bl - 1);\n inl -= i;\n if (inl > 0) {\n if (!ctx->cipher->do_cipher(ctx, out, in, inl))\n return 0;\n *outl += inl;\n }\n if (i != 0)\n memcpy(ctx->buf, &(in[inl]), i);\n ctx->buf_len = i;\n return 1;\n}', 'int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)\n{\n PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;\n int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |\n (diff > (0 - (PTRDIFF_T)len)));\n return overlapped;\n}']
35,900
0
https://github.com/openssl/openssl/blob/e72769aa41c3f49e0f39f44de222fc5ac339e3e0/crypto/evp/evp_enc.c/#L289
static int is_partially_overlapping(const void *ptr1, const void *ptr2, int len) { PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2; int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) | (diff > (0 - (PTRDIFF_T)len))); assert(!overlapped); return overlapped; }
['static int cipher_test_enc(struct evp_test *t, int enc,\n size_t out_misalign, size_t inp_misalign)\n{\n struct cipher_data *cdat = t->data;\n unsigned char *in, *out, *tmp = NULL;\n size_t in_len, out_len;\n int tmplen, tmpflen;\n EVP_CIPHER_CTX *ctx = NULL;\n const char *err;\n err = "INTERNAL_ERROR";\n ctx = EVP_CIPHER_CTX_new();\n if (!ctx)\n goto err;\n EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);\n if (enc) {\n in = cdat->plaintext;\n in_len = cdat->plaintext_len;\n out = cdat->ciphertext;\n out_len = cdat->ciphertext_len;\n } else {\n in = cdat->ciphertext;\n in_len = cdat->ciphertext_len;\n out = cdat->plaintext;\n out_len = cdat->plaintext_len;\n }\n if (inp_misalign == (size_t)-1) {\n tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);\n if (!tmp)\n goto err;\n in = memcpy(tmp + out_misalign, in, in_len);\n } else {\n inp_misalign += 16 - ((out_misalign + in_len) & 15);\n tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +\n inp_misalign + in_len);\n if (!tmp)\n goto err;\n in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +\n inp_misalign, in, in_len);\n }\n err = "CIPHERINIT_ERROR";\n if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))\n goto err;\n err = "INVALID_IV_LENGTH";\n if (cdat->iv) {\n if (cdat->aead) {\n if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,\n cdat->iv_len, 0))\n goto err;\n } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))\n goto err;\n }\n if (cdat->aead) {\n unsigned char *tag;\n if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {\n err = "TAG_LENGTH_SET_ERROR";\n tag = NULL;\n } else {\n err = "TAG_SET_ERROR";\n tag = cdat->tag;\n }\n if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {\n if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,\n cdat->tag_len, tag))\n goto err;\n }\n }\n err = "INVALID_KEY_LENGTH";\n if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))\n goto err;\n err = "KEY_SET_ERROR";\n if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))\n goto err;\n if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {\n if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,\n cdat->tag_len, cdat->tag)) {\n err = "TAG_SET_ERROR";\n goto err;\n }\n }\n if (cdat->aead == EVP_CIPH_CCM_MODE) {\n if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {\n err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";\n goto err;\n }\n }\n if (cdat->aad) {\n if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {\n err = "AAD_SET_ERROR";\n goto err;\n }\n }\n EVP_CIPHER_CTX_set_padding(ctx, 0);\n err = "CIPHERUPDATE_ERROR";\n if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))\n goto err;\n if (cdat->aead == EVP_CIPH_CCM_MODE)\n tmpflen = 0;\n else {\n err = "CIPHERFINAL_ERROR";\n if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen))\n goto err;\n }\n err = "LENGTH_MISMATCH";\n if (out_len != (size_t)(tmplen + tmpflen))\n goto err;\n err = "VALUE_MISMATCH";\n if (check_output(t, out, tmp + out_misalign, out_len))\n goto err;\n if (enc && cdat->aead) {\n unsigned char rtag[16];\n if (cdat->tag_len > sizeof(rtag)) {\n err = "TAG_LENGTH_INTERNAL_ERROR";\n goto err;\n }\n if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,\n cdat->tag_len, rtag)) {\n err = "TAG_RETRIEVE_ERROR";\n goto err;\n }\n if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {\n err = "TAG_VALUE_MISMATCH";\n goto err;\n }\n }\n err = NULL;\n err:\n OPENSSL_free(tmp);\n EVP_CIPHER_CTX_free(ctx);\n t->err = err;\n return err ? 0 : 1;\n}', 'int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n const unsigned char *in, int inl)\n{\n if (ctx->encrypt)\n return EVP_EncryptUpdate(ctx, out, outl, in, inl);\n else\n return EVP_DecryptUpdate(ctx, out, outl, in, inl);\n}', 'int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n const unsigned char *in, int inl)\n{\n int i, j, bl;\n if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {\n if (is_partially_overlapping(out, in, inl)) {\n EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n i = ctx->cipher->do_cipher(ctx, out, in, inl);\n if (i < 0)\n return 0;\n else\n *outl = i;\n return 1;\n }\n if (inl <= 0) {\n *outl = 0;\n return inl == 0;\n }\n if (is_partially_overlapping(out, in, inl)) {\n EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {\n if (ctx->cipher->do_cipher(ctx, out, in, inl)) {\n *outl = inl;\n return 1;\n } else {\n *outl = 0;\n return 0;\n }\n }\n i = ctx->buf_len;\n bl = ctx->cipher->block_size;\n OPENSSL_assert(bl <= (int)sizeof(ctx->buf));\n if (i != 0) {\n if (bl - i > inl) {\n memcpy(&(ctx->buf[i]), in, inl);\n ctx->buf_len += inl;\n *outl = 0;\n return 1;\n } else {\n j = bl - i;\n memcpy(&(ctx->buf[i]), in, j);\n inl -= j;\n in += j;\n if (is_partially_overlapping(out, in, bl)) {\n\t EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))\n return 0;\n out += bl;\n *outl = bl;\n }\n } else\n *outl = 0;\n i = inl & (bl - 1);\n inl -= i;\n if (inl > 0) {\n if (!ctx->cipher->do_cipher(ctx, out, in, inl))\n return 0;\n *outl += inl;\n }\n if (i != 0)\n memcpy(ctx->buf, &(in[inl]), i);\n ctx->buf_len = i;\n return 1;\n}', 'static int is_partially_overlapping(const void *ptr1, const void *ptr2,\n int len)\n{\n PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;\n int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |\n (diff > (0 - (PTRDIFF_T)len)));\n assert(!overlapped);\n return overlapped;\n}']