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,701
0
https://github.com/libav/libav/blob/c41b9842ceac42bedfd74a7ba2d02add82f818a9/libavcodec/rv40.c/#L479
static void rv40_loop_filter(RV34DecContext *r, int row) { MpegEncContext *s = &r->s; int mb_pos, mb_x; int i, j, k; uint8_t *Y, *C; int alpha, beta, betaY, betaC; int q; int mbtype[4]; int mb_strong[4]; int clip[4]; int cbp[4]; int uvcbp[4][2]; int mvmasks[4]; mb_pos = row * s->mb_stride; for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){ int mbtype = s->current_picture_ptr->f.mb_type[mb_pos]; if(IS_INTRA(mbtype) || IS_SEPARATE_DC(mbtype)) r->cbp_luma [mb_pos] = r->deblock_coefs[mb_pos] = 0xFFFF; if(IS_INTRA(mbtype)) r->cbp_chroma[mb_pos] = 0xFF; } mb_pos = row * s->mb_stride; for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){ int y_h_deblock, y_v_deblock; int c_v_deblock[2], c_h_deblock[2]; int clip_left; int avail[4]; int y_to_deblock, c_to_deblock[2]; q = s->current_picture_ptr->f.qscale_table[mb_pos]; alpha = rv40_alpha_tab[q]; beta = rv40_beta_tab [q]; betaY = betaC = beta * 3; if(s->width * s->height <= 176*144) betaY += beta; avail[0] = 1; avail[1] = row; avail[2] = mb_x; avail[3] = row < s->mb_height - 1; for(i = 0; i < 4; i++){ if(avail[i]){ int pos = mb_pos + neighbour_offs_x[i] + neighbour_offs_y[i]*s->mb_stride; mvmasks[i] = r->deblock_coefs[pos]; mbtype [i] = s->current_picture_ptr->f.mb_type[pos]; cbp [i] = r->cbp_luma[pos]; uvcbp[i][0] = r->cbp_chroma[pos] & 0xF; uvcbp[i][1] = r->cbp_chroma[pos] >> 4; }else{ mvmasks[i] = 0; mbtype [i] = mbtype[0]; cbp [i] = 0; uvcbp[i][0] = uvcbp[i][1] = 0; } mb_strong[i] = IS_INTRA(mbtype[i]) || IS_SEPARATE_DC(mbtype[i]); clip[i] = rv40_filter_clip_tbl[mb_strong[i] + 1][q]; } y_to_deblock = mvmasks[POS_CUR] | (mvmasks[POS_BOTTOM] << 16); y_h_deblock = y_to_deblock | ((cbp[POS_CUR] << 4) & ~MASK_Y_TOP_ROW) | ((cbp[POS_TOP] & MASK_Y_LAST_ROW) >> 12); y_v_deblock = y_to_deblock | ((cbp[POS_CUR] << 1) & ~MASK_Y_LEFT_COL) | ((cbp[POS_LEFT] & MASK_Y_RIGHT_COL) >> 3); if(!mb_x) y_v_deblock &= ~MASK_Y_LEFT_COL; if(!row) y_h_deblock &= ~MASK_Y_TOP_ROW; if(row == s->mb_height - 1 || (mb_strong[POS_CUR] || mb_strong[POS_BOTTOM])) y_h_deblock &= ~(MASK_Y_TOP_ROW << 16); for(i = 0; i < 2; i++){ c_to_deblock[i] = (uvcbp[POS_BOTTOM][i] << 4) | uvcbp[POS_CUR][i]; c_v_deblock[i] = c_to_deblock[i] | ((uvcbp[POS_CUR] [i] << 1) & ~MASK_C_LEFT_COL) | ((uvcbp[POS_LEFT][i] & MASK_C_RIGHT_COL) >> 1); c_h_deblock[i] = c_to_deblock[i] | ((uvcbp[POS_TOP][i] & MASK_C_LAST_ROW) >> 2) | (uvcbp[POS_CUR][i] << 2); if(!mb_x) c_v_deblock[i] &= ~MASK_C_LEFT_COL; if(!row) c_h_deblock[i] &= ~MASK_C_TOP_ROW; if(row == s->mb_height - 1 || mb_strong[POS_CUR] || mb_strong[POS_BOTTOM]) c_h_deblock[i] &= ~(MASK_C_TOP_ROW << 4); } for(j = 0; j < 16; j += 4){ Y = s->current_picture_ptr->f.data[0] + mb_x*16 + (row*16 + j) * s->linesize; for(i = 0; i < 4; i++, Y += 4){ int ij = i + j; int clip_cur = y_to_deblock & (MASK_CUR << ij) ? clip[POS_CUR] : 0; int dither = j ? ij : i*4; if(y_h_deblock & (MASK_BOTTOM << ij)){ rv40_adaptive_loop_filter(&r->rdsp, Y+4*s->linesize, s->linesize, dither, y_to_deblock & (MASK_BOTTOM << ij) ? clip[POS_CUR] : 0, clip_cur, alpha, beta, betaY, 0, 0, 0); } if(y_v_deblock & (MASK_CUR << ij) && (i || !(mb_strong[POS_CUR] || mb_strong[POS_LEFT]))){ if(!i) clip_left = mvmasks[POS_LEFT] & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0; else clip_left = y_to_deblock & (MASK_CUR << (ij-1)) ? clip[POS_CUR] : 0; rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither, clip_cur, clip_left, alpha, beta, betaY, 0, 0, 1); } if(!j && y_h_deblock & (MASK_CUR << i) && (mb_strong[POS_CUR] || mb_strong[POS_TOP])){ rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither, clip_cur, mvmasks[POS_TOP] & (MASK_TOP << i) ? clip[POS_TOP] : 0, alpha, beta, betaY, 0, 1, 0); } if(y_v_deblock & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] || mb_strong[POS_LEFT])){ clip_left = mvmasks[POS_LEFT] & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0; rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither, clip_cur, clip_left, alpha, beta, betaY, 0, 1, 1); } } } for(k = 0; k < 2; k++){ for(j = 0; j < 2; j++){ C = s->current_picture_ptr->f.data[k + 1] + mb_x*8 + (row*8 + j*4) * s->uvlinesize; for(i = 0; i < 2; i++, C += 4){ int ij = i + j*2; int clip_cur = c_to_deblock[k] & (MASK_CUR << ij) ? clip[POS_CUR] : 0; if(c_h_deblock[k] & (MASK_CUR << (ij+2))){ int clip_bot = c_to_deblock[k] & (MASK_CUR << (ij+2)) ? clip[POS_CUR] : 0; rv40_adaptive_loop_filter(&r->rdsp, C+4*s->uvlinesize, s->uvlinesize, i*8, clip_bot, clip_cur, alpha, beta, betaC, 1, 0, 0); } if((c_v_deblock[k] & (MASK_CUR << ij)) && (i || !(mb_strong[POS_CUR] || mb_strong[POS_LEFT]))){ if(!i) clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0; else clip_left = c_to_deblock[k] & (MASK_CUR << (ij-1)) ? clip[POS_CUR] : 0; rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, j*8, clip_cur, clip_left, alpha, beta, betaC, 1, 0, 1); } if(!j && c_h_deblock[k] & (MASK_CUR << ij) && (mb_strong[POS_CUR] || mb_strong[POS_TOP])){ int clip_top = uvcbp[POS_TOP][k] & (MASK_CUR << (ij+2)) ? clip[POS_TOP] : 0; rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, i*8, clip_cur, clip_top, alpha, beta, betaC, 1, 1, 0); } if(c_v_deblock[k] & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] || mb_strong[POS_LEFT])){ clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0; rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, j*8, clip_cur, clip_left, alpha, beta, betaC, 1, 1, 1); } } } } } }
['static void rv40_loop_filter(RV34DecContext *r, int row)\n{\n MpegEncContext *s = &r->s;\n int mb_pos, mb_x;\n int i, j, k;\n uint8_t *Y, *C;\n int alpha, beta, betaY, betaC;\n int q;\n int mbtype[4];\n int mb_strong[4];\n int clip[4];\n int cbp[4];\n int uvcbp[4][2];\n int mvmasks[4];\n mb_pos = row * s->mb_stride;\n for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){\n int mbtype = s->current_picture_ptr->f.mb_type[mb_pos];\n if(IS_INTRA(mbtype) || IS_SEPARATE_DC(mbtype))\n r->cbp_luma [mb_pos] = r->deblock_coefs[mb_pos] = 0xFFFF;\n if(IS_INTRA(mbtype))\n r->cbp_chroma[mb_pos] = 0xFF;\n }\n mb_pos = row * s->mb_stride;\n for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){\n int y_h_deblock, y_v_deblock;\n int c_v_deblock[2], c_h_deblock[2];\n int clip_left;\n int avail[4];\n int y_to_deblock, c_to_deblock[2];\n q = s->current_picture_ptr->f.qscale_table[mb_pos];\n alpha = rv40_alpha_tab[q];\n beta = rv40_beta_tab [q];\n betaY = betaC = beta * 3;\n if(s->width * s->height <= 176*144)\n betaY += beta;\n avail[0] = 1;\n avail[1] = row;\n avail[2] = mb_x;\n avail[3] = row < s->mb_height - 1;\n for(i = 0; i < 4; i++){\n if(avail[i]){\n int pos = mb_pos + neighbour_offs_x[i] + neighbour_offs_y[i]*s->mb_stride;\n mvmasks[i] = r->deblock_coefs[pos];\n mbtype [i] = s->current_picture_ptr->f.mb_type[pos];\n cbp [i] = r->cbp_luma[pos];\n uvcbp[i][0] = r->cbp_chroma[pos] & 0xF;\n uvcbp[i][1] = r->cbp_chroma[pos] >> 4;\n }else{\n mvmasks[i] = 0;\n mbtype [i] = mbtype[0];\n cbp [i] = 0;\n uvcbp[i][0] = uvcbp[i][1] = 0;\n }\n mb_strong[i] = IS_INTRA(mbtype[i]) || IS_SEPARATE_DC(mbtype[i]);\n clip[i] = rv40_filter_clip_tbl[mb_strong[i] + 1][q];\n }\n y_to_deblock = mvmasks[POS_CUR]\n | (mvmasks[POS_BOTTOM] << 16);\n y_h_deblock = y_to_deblock\n | ((cbp[POS_CUR] << 4) & ~MASK_Y_TOP_ROW)\n | ((cbp[POS_TOP] & MASK_Y_LAST_ROW) >> 12);\n y_v_deblock = y_to_deblock\n | ((cbp[POS_CUR] << 1) & ~MASK_Y_LEFT_COL)\n | ((cbp[POS_LEFT] & MASK_Y_RIGHT_COL) >> 3);\n if(!mb_x)\n y_v_deblock &= ~MASK_Y_LEFT_COL;\n if(!row)\n y_h_deblock &= ~MASK_Y_TOP_ROW;\n if(row == s->mb_height - 1 || (mb_strong[POS_CUR] || mb_strong[POS_BOTTOM]))\n y_h_deblock &= ~(MASK_Y_TOP_ROW << 16);\n for(i = 0; i < 2; i++){\n c_to_deblock[i] = (uvcbp[POS_BOTTOM][i] << 4) | uvcbp[POS_CUR][i];\n c_v_deblock[i] = c_to_deblock[i]\n | ((uvcbp[POS_CUR] [i] << 1) & ~MASK_C_LEFT_COL)\n | ((uvcbp[POS_LEFT][i] & MASK_C_RIGHT_COL) >> 1);\n c_h_deblock[i] = c_to_deblock[i]\n | ((uvcbp[POS_TOP][i] & MASK_C_LAST_ROW) >> 2)\n | (uvcbp[POS_CUR][i] << 2);\n if(!mb_x)\n c_v_deblock[i] &= ~MASK_C_LEFT_COL;\n if(!row)\n c_h_deblock[i] &= ~MASK_C_TOP_ROW;\n if(row == s->mb_height - 1 || mb_strong[POS_CUR] || mb_strong[POS_BOTTOM])\n c_h_deblock[i] &= ~(MASK_C_TOP_ROW << 4);\n }\n for(j = 0; j < 16; j += 4){\n Y = s->current_picture_ptr->f.data[0] + mb_x*16 + (row*16 + j) * s->linesize;\n for(i = 0; i < 4; i++, Y += 4){\n int ij = i + j;\n int clip_cur = y_to_deblock & (MASK_CUR << ij) ? clip[POS_CUR] : 0;\n int dither = j ? ij : i*4;\n if(y_h_deblock & (MASK_BOTTOM << ij)){\n rv40_adaptive_loop_filter(&r->rdsp, Y+4*s->linesize,\n s->linesize, dither,\n y_to_deblock & (MASK_BOTTOM << ij) ? clip[POS_CUR] : 0,\n clip_cur, alpha, beta, betaY,\n 0, 0, 0);\n }\n if(y_v_deblock & (MASK_CUR << ij) && (i || !(mb_strong[POS_CUR] || mb_strong[POS_LEFT]))){\n if(!i)\n clip_left = mvmasks[POS_LEFT] & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0;\n else\n clip_left = y_to_deblock & (MASK_CUR << (ij-1)) ? clip[POS_CUR] : 0;\n rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,\n clip_cur,\n clip_left,\n alpha, beta, betaY, 0, 0, 1);\n }\n if(!j && y_h_deblock & (MASK_CUR << i) && (mb_strong[POS_CUR] || mb_strong[POS_TOP])){\n rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,\n clip_cur,\n mvmasks[POS_TOP] & (MASK_TOP << i) ? clip[POS_TOP] : 0,\n alpha, beta, betaY, 0, 1, 0);\n }\n if(y_v_deblock & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] || mb_strong[POS_LEFT])){\n clip_left = mvmasks[POS_LEFT] & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0;\n rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,\n clip_cur,\n clip_left,\n alpha, beta, betaY, 0, 1, 1);\n }\n }\n }\n for(k = 0; k < 2; k++){\n for(j = 0; j < 2; j++){\n C = s->current_picture_ptr->f.data[k + 1] + mb_x*8 + (row*8 + j*4) * s->uvlinesize;\n for(i = 0; i < 2; i++, C += 4){\n int ij = i + j*2;\n int clip_cur = c_to_deblock[k] & (MASK_CUR << ij) ? clip[POS_CUR] : 0;\n if(c_h_deblock[k] & (MASK_CUR << (ij+2))){\n int clip_bot = c_to_deblock[k] & (MASK_CUR << (ij+2)) ? clip[POS_CUR] : 0;\n rv40_adaptive_loop_filter(&r->rdsp, C+4*s->uvlinesize, s->uvlinesize, i*8,\n clip_bot,\n clip_cur,\n alpha, beta, betaC, 1, 0, 0);\n }\n if((c_v_deblock[k] & (MASK_CUR << ij)) && (i || !(mb_strong[POS_CUR] || mb_strong[POS_LEFT]))){\n if(!i)\n clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0;\n else\n clip_left = c_to_deblock[k] & (MASK_CUR << (ij-1)) ? clip[POS_CUR] : 0;\n rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, j*8,\n clip_cur,\n clip_left,\n alpha, beta, betaC, 1, 0, 1);\n }\n if(!j && c_h_deblock[k] & (MASK_CUR << ij) && (mb_strong[POS_CUR] || mb_strong[POS_TOP])){\n int clip_top = uvcbp[POS_TOP][k] & (MASK_CUR << (ij+2)) ? clip[POS_TOP] : 0;\n rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, i*8,\n clip_cur,\n clip_top,\n alpha, beta, betaC, 1, 1, 0);\n }\n if(c_v_deblock[k] & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] || mb_strong[POS_LEFT])){\n clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0;\n rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, j*8,\n clip_cur,\n clip_left,\n alpha, beta, betaC, 1, 1, 1);\n }\n }\n }\n }\n }\n}']
35,702
0
https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/crypto/bn/bn_ctx.c/#L273
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int test_mod_exp_zero(void)\n{\n BIGNUM *a = NULL, *p = NULL, *m = NULL;\n BIGNUM *r = NULL;\n BN_ULONG one_word = 1;\n BN_CTX *ctx = BN_CTX_new();\n int ret = 1, failed = 0;\n if (!TEST_ptr(m = BN_new())\n || !TEST_ptr(a = BN_new())\n || !TEST_ptr(p = BN_new())\n || !TEST_ptr(r = BN_new()))\n goto err;\n BN_one(m);\n BN_one(a);\n BN_zero(p);\n if (!TEST_true(BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)))\n goto err;\n if (!TEST_true(BN_mod_exp(r, a, p, m, ctx)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_recp(r, a, p, m, ctx)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_simple(r, a, p, m, ctx)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_mont(r, a, p, m, ctx, NULL)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)))\n goto err;\n if (!TEST_BN_eq_zero(r)) {\n TEST_error("BN_mod_exp_mont_word failed: "\n "1 ** 0 mod 1 = r (should be 0)");\n BN_print_var(r);\n goto err;\n }\n ret = !failed;\n err:\n BN_free(r);\n BN_free(a);\n BN_free(p);\n BN_free(m);\n BN_CTX_free(ctx);\n return ret;\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_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_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 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_is_one(m)) {\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}', '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,703
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_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[],\n BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *u;\n bn_check_top(a);\n if (!p[0]) {\n BN_zero(r);\n return 1;\n }\n BN_CTX_start(ctx);\n if ((u = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_set_bit(u, p[0] - 1))\n goto err;\n ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);\n bn_check_top(r);\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}', '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}', '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,704
0
https://github.com/openssl/openssl/blob/5d1c09de1f2736e1d4b1877206d08455ec75f558/crypto/bn/bn_ctx.c/#L276
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int test_modexp_mont5(void)\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 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 parse_bigBN(&n, bn1strings);\n parse_bigBN(&a, bn2strings);\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 {\n static const char *ahex[] = {\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",\n "9544D954000000006C0000000000000000000000000000000000000000000000",\n "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",\n "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",\n "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",\n "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",\n "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",\n NULL\n };\n static const char *nhex[] = {\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",\n "00000010000000006C0000000000000000000000000000000000000000000000",\n "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",\n "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n NULL\n };\n parse_bigBN(&a, ahex);\n parse_bigBN(&n, nhex);\n }\n BN_free(b);\n b = BN_dup(a);\n BN_MONT_CTX_set(mont, n, ctx);\n if (!TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))\n || !TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))\n || !TEST_BN_eq(c, d))\n goto err;\n BN_hex2bn(&a,\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");\n BN_hex2bn(&n,\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");\n BN_MONT_CTX_set(mont, n, ctx);\n if (!TEST_false(BN_mod_mul_montgomery(d, a, a, mont, ctx)))\n goto err;\n BN_hex2bn(&a,\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF");\n BN_hex2bn(&b,\n "2020202020202020202020202020202020202020202020202020202020202020"\n "2020202020202020202020202020202020202020202020202020202020202020"\n "20202020202020FF202020202020202020202020202020202020202020202020"\n "2020202020202020202020202020202020202020202020202020202020202020");\n BN_hex2bn(&n,\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020FF");\n BN_MONT_CTX_set(mont, n, ctx);\n BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont);\n BN_mod_exp_mont(d, a, b, n, ctx, mont);\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, 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_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 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.flags = BN_FLG_STATIC_DATA;\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,705
0
https://github.com/libav/libav/blob/645d26520a1a1900a89f2811eb78a5d637ca7877/libavcodec/h264_direct.c/#L264
void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){ MpegEncContext * const s = &h->s; int b8_stride = h->b8_stride; int b4_stride = h->b_stride; int mb_xy = h->mb_xy; int mb_type_col[2]; const int16_t (*l1mv0)[2], (*l1mv1)[2]; const int8_t *l1ref0, *l1ref1; const int is_b8x8 = IS_8X8(*mb_type); unsigned int sub_mb_type; int i8, i4; assert(h->ref_list[1][0].reference&3); #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM) if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ if(!IS_INTERLACED(*mb_type)){ mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride; b8_stride = 0; }else{ mb_xy += h->col_fieldoff; } goto single_col; }else{ if(IS_INTERLACED(*mb_type)){ mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride; mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy]; mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride]; b8_stride *= 3; b4_stride *= 6; sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) && !is_b8x8){ *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; }else{ *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1; } }else{ single_col: mb_type_col[0] = mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy]; sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){ *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){ *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16)); }else{ if(!h->sps.direct_8x8_inference_flag){ sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; } *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1; } } } l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]]; l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]]; l1ref0 = &h->ref_list[1][0].ref_index [0][h->mb2b8_xy[mb_xy]]; l1ref1 = &h->ref_list[1][0].ref_index [1][h->mb2b8_xy[mb_xy]]; if(!b8_stride){ if(s->mb_y&1){ l1ref0 += h->b8_stride; l1ref1 += h->b8_stride; l1mv0 += 2*b4_stride; l1mv1 += 2*b4_stride; } } if(h->direct_spatial_mv_pred){ int ref[2]; int mv[2][2]; int list; for(list=0; list<2; list++){ int left_ref = h->ref_cache[list][scan8[0] - 1]; int top_ref = h->ref_cache[list][scan8[0] - 8]; int refc = h->ref_cache[list][scan8[0] - 8 + 4]; const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4]; if(refc == PART_NOT_AVAILABLE){ refc = h->ref_cache[list][scan8[0] - 8 - 1]; C = h-> mv_cache[list][scan8[0] - 8 - 1]; } ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc); if(ref[list] >= 0){ const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ]; const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ]; int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]); if(match_count > 1){ mv[list][0]= mid_pred(A[0], B[0], C[0]); mv[list][1]= mid_pred(A[1], B[1], C[1]); }else { assert(match_count==1); if(left_ref==ref[list]){ mv[list][0]= A[0]; mv[list][1]= A[1]; }else if(top_ref==ref[list]){ mv[list][0]= B[0]; mv[list][1]= B[1]; }else{ mv[list][0]= C[0]; mv[list][1]= C[1]; } } }else{ int mask= ~(MB_TYPE_L0 << (2*list)); mv[list][0] = mv[list][1] = 0; ref[list] = -1; if(!is_b8x8) *mb_type &= mask; sub_mb_type &= mask; } } if(ref[0] < 0 && ref[1] < 0){ ref[0] = ref[1] = 0; if(!is_b8x8) *mb_type |= MB_TYPE_L0L1; sub_mb_type |= MB_TYPE_L0L1; } if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){ int n=0; for(i8=0; i8<4; i8++){ int x8 = i8&1; int y8 = i8>>1; int xy8 = x8+y8*b8_stride; int xy4 = 3*x8+y8*b4_stride; int a,b; if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8])) continue; h->sub_mb_type[i8] = sub_mb_type; fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1); fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1); if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1) || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){ a=b=0; if(ref[0] > 0) a= pack16to32(mv[0][0],mv[0][1]); if(ref[1] > 0) b= pack16to32(mv[1][0],mv[1][1]); n++; }else{ a= pack16to32(mv[0][0],mv[0][1]); b= pack16to32(mv[1][0],mv[1][1]); } fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4); fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4); } if(!is_b8x8 && !(n&3)) *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2; }else if(IS_16X16(*mb_type)){ int a,b; fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1); fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1); if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1) || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1 && h->x264_build>33U))){ a=b=0; if(ref[0] > 0) a= pack16to32(mv[0][0],mv[0][1]); if(ref[1] > 0) b= pack16to32(mv[1][0],mv[1][1]); }else{ a= pack16to32(mv[0][0],mv[0][1]); b= pack16to32(mv[1][0],mv[1][1]); } fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4); fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4); }else{ int n=0; for(i8=0; i8<4; i8++){ const int x8 = i8&1; const int y8 = i8>>1; if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8])) continue; h->sub_mb_type[i8] = sub_mb_type; fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4); fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4); fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1); fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1); if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( l1ref0[x8 + y8*b8_stride] == 0 || (l1ref0[x8 + y8*b8_stride] < 0 && l1ref1[x8 + y8*b8_stride] == 0 && h->x264_build>33U))){ const int16_t (*l1mv)[2]= l1ref0[x8 + y8*b8_stride] == 0 ? l1mv0 : l1mv1; if(IS_SUB_8X8(sub_mb_type)){ const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride]; if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){ if(ref[0] == 0) fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4); if(ref[1] == 0) fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4); n+=4; } }else{ int m=0; for(i4=0; i4<4; i4++){ const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride]; if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){ if(ref[0] == 0) *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0; if(ref[1] == 0) *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0; m++; } } if(!(m&3)) h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8; n+=m; } } } if(!is_b8x8 && !(n&15)) *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2; } }else{ const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]}; const int *dist_scale_factor = h->dist_scale_factor; int ref_offset; if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){ map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0]; map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1]; dist_scale_factor =h->dist_scale_factor_field[s->mb_y&1]; } ref_offset = (h->ref_list[1][0].mbaff<<4) & (mb_type_col[0]>>3); if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){ int y_shift = 2*!IS_INTERLACED(*mb_type); assert(h->sps.direct_8x8_inference_flag); for(i8=0; i8<4; i8++){ const int x8 = i8&1; const int y8 = i8>>1; int ref0, scale; const int16_t (*l1mv)[2]= l1mv0; if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8])) continue; h->sub_mb_type[i8] = sub_mb_type; fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1); if(IS_INTRA(mb_type_col[y8])){ fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1); fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4); fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4); continue; } ref0 = l1ref0[x8 + y8*b8_stride]; if(ref0 >= 0) ref0 = map_col_to_list0[0][ref0 + ref_offset]; else{ ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset]; l1mv= l1mv1; } scale = dist_scale_factor[ref0]; fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1); { const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride]; int my_col = (mv_col[1]<<y_shift)/2; int mx = (scale * mv_col[0] + 128) >> 8; int my = (scale * my_col + 128) >> 8; fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4); fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4); } } return; } if(IS_16X16(*mb_type)){ int ref, mv0, mv1; fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1); if(IS_INTRA(mb_type_col[0])){ ref=mv0=mv1=0; }else{ const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset] : map_col_to_list0[1][l1ref1[0] + ref_offset]; const int scale = dist_scale_factor[ref0]; const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0]; int mv_l0[2]; mv_l0[0] = (scale * mv_col[0] + 128) >> 8; mv_l0[1] = (scale * mv_col[1] + 128) >> 8; ref= ref0; mv0= pack16to32(mv_l0[0],mv_l0[1]); mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]); } fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1); fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4); fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4); }else{ for(i8=0; i8<4; i8++){ const int x8 = i8&1; const int y8 = i8>>1; int ref0, scale; const int16_t (*l1mv)[2]= l1mv0; if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8])) continue; h->sub_mb_type[i8] = sub_mb_type; fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1); if(IS_INTRA(mb_type_col[0])){ fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1); fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4); fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4); continue; } ref0 = l1ref0[x8 + y8*b8_stride]; if(ref0 >= 0) ref0 = map_col_to_list0[0][ref0 + ref_offset]; else{ ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset]; l1mv= l1mv1; } scale = dist_scale_factor[ref0]; fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1); if(IS_SUB_8X8(sub_mb_type)){ const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride]; int mx = (scale * mv_col[0] + 128) >> 8; int my = (scale * mv_col[1] + 128) >> 8; fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4); fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4); }else for(i4=0; i4<4; i4++){ const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride]; int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]]; mv_l0[0] = (scale * mv_col[0] + 128) >> 8; mv_l0[1] = (scale * mv_col[1] + 128) >> 8; *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]); } } } } }
['void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){\n MpegEncContext * const s = &h->s;\n int b8_stride = h->b8_stride;\n int b4_stride = h->b_stride;\n int mb_xy = h->mb_xy;\n int mb_type_col[2];\n const int16_t (*l1mv0)[2], (*l1mv1)[2];\n const int8_t *l1ref0, *l1ref1;\n const int is_b8x8 = IS_8X8(*mb_type);\n unsigned int sub_mb_type;\n int i8, i4;\n assert(h->ref_list[1][0].reference&3);\n#define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)\n if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){\n if(!IS_INTERLACED(*mb_type)){\n mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;\n b8_stride = 0;\n }else{\n mb_xy += h->col_fieldoff;\n }\n goto single_col;\n }else{\n if(IS_INTERLACED(*mb_type)){\n mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;\n mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];\n mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];\n b8_stride *= 3;\n b4_stride *= 6;\n sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;\n if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)\n && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)\n && !is_b8x8){\n *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2;\n }else{\n *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;\n }\n }else{\nsingle_col:\n mb_type_col[0] =\n mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];\n sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;\n if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){\n *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;\n }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){\n *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));\n }else{\n if(!h->sps.direct_8x8_inference_flag){\n sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;\n }\n *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;\n }\n }\n }\n l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];\n l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];\n l1ref0 = &h->ref_list[1][0].ref_index [0][h->mb2b8_xy[mb_xy]];\n l1ref1 = &h->ref_list[1][0].ref_index [1][h->mb2b8_xy[mb_xy]];\n if(!b8_stride){\n if(s->mb_y&1){\n l1ref0 += h->b8_stride;\n l1ref1 += h->b8_stride;\n l1mv0 += 2*b4_stride;\n l1mv1 += 2*b4_stride;\n }\n }\n if(h->direct_spatial_mv_pred){\n int ref[2];\n int mv[2][2];\n int list;\n for(list=0; list<2; list++){\n int left_ref = h->ref_cache[list][scan8[0] - 1];\n int top_ref = h->ref_cache[list][scan8[0] - 8];\n int refc = h->ref_cache[list][scan8[0] - 8 + 4];\n const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];\n if(refc == PART_NOT_AVAILABLE){\n refc = h->ref_cache[list][scan8[0] - 8 - 1];\n C = h-> mv_cache[list][scan8[0] - 8 - 1];\n }\n ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);\n if(ref[list] >= 0){\n const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];\n const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];\n int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);\n if(match_count > 1){\n mv[list][0]= mid_pred(A[0], B[0], C[0]);\n mv[list][1]= mid_pred(A[1], B[1], C[1]);\n }else {\n assert(match_count==1);\n if(left_ref==ref[list]){\n mv[list][0]= A[0];\n mv[list][1]= A[1];\n }else if(top_ref==ref[list]){\n mv[list][0]= B[0];\n mv[list][1]= B[1];\n }else{\n mv[list][0]= C[0];\n mv[list][1]= C[1];\n }\n }\n }else{\n int mask= ~(MB_TYPE_L0 << (2*list));\n mv[list][0] = mv[list][1] = 0;\n ref[list] = -1;\n if(!is_b8x8)\n *mb_type &= mask;\n sub_mb_type &= mask;\n }\n }\n if(ref[0] < 0 && ref[1] < 0){\n ref[0] = ref[1] = 0;\n if(!is_b8x8)\n *mb_type |= MB_TYPE_L0L1;\n sub_mb_type |= MB_TYPE_L0L1;\n }\n if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){\n int n=0;\n for(i8=0; i8<4; i8++){\n int x8 = i8&1;\n int y8 = i8>>1;\n int xy8 = x8+y8*b8_stride;\n int xy4 = 3*x8+y8*b4_stride;\n int a,b;\n if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))\n continue;\n h->sub_mb_type[i8] = sub_mb_type;\n fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);\n fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);\n if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref\n && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)\n || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){\n a=b=0;\n if(ref[0] > 0)\n a= pack16to32(mv[0][0],mv[0][1]);\n if(ref[1] > 0)\n b= pack16to32(mv[1][0],mv[1][1]);\n n++;\n }else{\n a= pack16to32(mv[0][0],mv[0][1]);\n b= pack16to32(mv[1][0],mv[1][1]);\n }\n fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);\n fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);\n }\n if(!is_b8x8 && !(n&3))\n *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;\n }else if(IS_16X16(*mb_type)){\n int a,b;\n fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);\n fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);\n if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref\n && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)\n || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1\n && h->x264_build>33U))){\n a=b=0;\n if(ref[0] > 0)\n a= pack16to32(mv[0][0],mv[0][1]);\n if(ref[1] > 0)\n b= pack16to32(mv[1][0],mv[1][1]);\n }else{\n a= pack16to32(mv[0][0],mv[0][1]);\n b= pack16to32(mv[1][0],mv[1][1]);\n }\n fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);\n fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);\n }else{\n int n=0;\n for(i8=0; i8<4; i8++){\n const int x8 = i8&1;\n const int y8 = i8>>1;\n if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))\n continue;\n h->sub_mb_type[i8] = sub_mb_type;\n fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);\n fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);\n fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);\n fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);\n if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( l1ref0[x8 + y8*b8_stride] == 0\n || (l1ref0[x8 + y8*b8_stride] < 0 && l1ref1[x8 + y8*b8_stride] == 0\n && h->x264_build>33U))){\n const int16_t (*l1mv)[2]= l1ref0[x8 + y8*b8_stride] == 0 ? l1mv0 : l1mv1;\n if(IS_SUB_8X8(sub_mb_type)){\n const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];\n if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){\n if(ref[0] == 0)\n fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);\n if(ref[1] == 0)\n fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);\n n+=4;\n }\n }else{\n int m=0;\n for(i4=0; i4<4; i4++){\n const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];\n if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){\n if(ref[0] == 0)\n *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;\n if(ref[1] == 0)\n *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;\n m++;\n }\n }\n if(!(m&3))\n h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;\n n+=m;\n }\n }\n }\n if(!is_b8x8 && !(n&15))\n *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;\n }\n }else{\n const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};\n const int *dist_scale_factor = h->dist_scale_factor;\n int ref_offset;\n if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){\n map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];\n map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];\n dist_scale_factor =h->dist_scale_factor_field[s->mb_y&1];\n }\n ref_offset = (h->ref_list[1][0].mbaff<<4) & (mb_type_col[0]>>3);\n if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){\n int y_shift = 2*!IS_INTERLACED(*mb_type);\n assert(h->sps.direct_8x8_inference_flag);\n for(i8=0; i8<4; i8++){\n const int x8 = i8&1;\n const int y8 = i8>>1;\n int ref0, scale;\n const int16_t (*l1mv)[2]= l1mv0;\n if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))\n continue;\n h->sub_mb_type[i8] = sub_mb_type;\n fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);\n if(IS_INTRA(mb_type_col[y8])){\n fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);\n fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);\n fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);\n continue;\n }\n ref0 = l1ref0[x8 + y8*b8_stride];\n if(ref0 >= 0)\n ref0 = map_col_to_list0[0][ref0 + ref_offset];\n else{\n ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];\n l1mv= l1mv1;\n }\n scale = dist_scale_factor[ref0];\n fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);\n {\n const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];\n int my_col = (mv_col[1]<<y_shift)/2;\n int mx = (scale * mv_col[0] + 128) >> 8;\n int my = (scale * my_col + 128) >> 8;\n fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);\n fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);\n }\n }\n return;\n }\n if(IS_16X16(*mb_type)){\n int ref, mv0, mv1;\n fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);\n if(IS_INTRA(mb_type_col[0])){\n ref=mv0=mv1=0;\n }else{\n const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]\n : map_col_to_list0[1][l1ref1[0] + ref_offset];\n const int scale = dist_scale_factor[ref0];\n const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];\n int mv_l0[2];\n mv_l0[0] = (scale * mv_col[0] + 128) >> 8;\n mv_l0[1] = (scale * mv_col[1] + 128) >> 8;\n ref= ref0;\n mv0= pack16to32(mv_l0[0],mv_l0[1]);\n mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);\n }\n fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);\n fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);\n fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);\n }else{\n for(i8=0; i8<4; i8++){\n const int x8 = i8&1;\n const int y8 = i8>>1;\n int ref0, scale;\n const int16_t (*l1mv)[2]= l1mv0;\n if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))\n continue;\n h->sub_mb_type[i8] = sub_mb_type;\n fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);\n if(IS_INTRA(mb_type_col[0])){\n fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);\n fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);\n fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);\n continue;\n }\n ref0 = l1ref0[x8 + y8*b8_stride];\n if(ref0 >= 0)\n ref0 = map_col_to_list0[0][ref0 + ref_offset];\n else{\n ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];\n l1mv= l1mv1;\n }\n scale = dist_scale_factor[ref0];\n fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);\n if(IS_SUB_8X8(sub_mb_type)){\n const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];\n int mx = (scale * mv_col[0] + 128) >> 8;\n int my = (scale * mv_col[1] + 128) >> 8;\n fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);\n fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);\n }else\n for(i4=0; i4<4; i4++){\n const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];\n int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];\n mv_l0[0] = (scale * mv_col[0] + 128) >> 8;\n mv_l0[1] = (scale * mv_col[1] + 128) >> 8;\n *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =\n pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);\n }\n }\n }\n }\n}']
35,706
0
https://github.com/openssl/openssl/blob/01b7851aa27aa144372f5484da916be042d9aa4f/crypto/buffer/buf_str.c/#L98
char *BUF_strndup(const char *str, size_t siz) { char *ret; if (str == NULL) return NULL; siz = BUF_strnlen(str, siz); if (siz >= INT_MAX) return NULL; ret = OPENSSL_malloc(siz + 1); if (ret == NULL) { BUFerr(BUF_F_BUF_STRNDUP, ERR_R_MALLOC_FAILURE); return NULL; } memcpy(ret, str, siz); ret[siz] = '\0'; return (ret); }
['int setup_ui_method(void)\n{\n ui_method = UI_create_method("OpenSSL application user interface");\n UI_method_set_opener(ui_method, ui_open);\n UI_method_set_reader(ui_method, ui_read);\n UI_method_set_writer(ui_method, ui_write);\n UI_method_set_closer(ui_method, ui_close);\n return 0;\n}', 'UI_METHOD *UI_create_method(char *name)\n{\n UI_METHOD *ui_method = OPENSSL_zalloc(sizeof(*ui_method));\n if (ui_method)\n ui_method->name = BUF_strdup(name);\n return ui_method;\n}', 'char *BUF_strdup(const char *str)\n{\n if (str == NULL)\n return NULL;\n return BUF_strndup(str, strlen(str));\n}', "char *BUF_strndup(const char *str, size_t siz)\n{\n char *ret;\n if (str == NULL)\n return NULL;\n siz = BUF_strnlen(str, siz);\n if (siz >= INT_MAX)\n return NULL;\n ret = OPENSSL_malloc(siz + 1);\n if (ret == NULL) {\n BUFerr(BUF_F_BUF_STRNDUP, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n memcpy(ret, str, siz);\n ret[siz] = '\\0';\n return (ret);\n}", "size_t BUF_strnlen(const char *str, size_t maxlen)\n{\n const char *p;\n for (p = str; maxlen-- != 0 && *p != '\\0'; ++p) ;\n return p - str;\n}"]
35,707
0
https://github.com/nginx/nginx/blob/18b36e5035ec5b96fbc44311df5e5dd426d4e93e/src/core/ngx_slab.c/#L464
void ngx_slab_free_locked(ngx_slab_pool_t *pool, void *p) { size_t size; uintptr_t slab, m, *bitmap; ngx_uint_t n, type, slot, shift, map; ngx_slab_page_t *slots, *page; ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, ngx_cycle->log, 0, "slab free: %p", p); if ((u_char *) p < pool->start || (u_char *) p > pool->end) { ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): outside of pool"); goto fail; } n = ((u_char *) p - pool->start) >> ngx_pagesize_shift; page = &pool->pages[n]; slab = page->slab; type = page->prev & NGX_SLAB_PAGE_MASK; switch (type) { case NGX_SLAB_SMALL: shift = slab & NGX_SLAB_SHIFT_MASK; size = 1 << shift; if ((uintptr_t) p & (size - 1)) { goto wrong_chunk; } n = ((uintptr_t) p & (ngx_pagesize - 1)) >> shift; m = (uintptr_t) 1 << (n & (sizeof(uintptr_t) * 8 - 1)); n /= (sizeof(uintptr_t) * 8); bitmap = (uintptr_t *) ((uintptr_t) p & ~(ngx_pagesize - 1)); if (bitmap[n] & m) { if (page->next == NULL) { slots = (ngx_slab_page_t *) ((u_char *) pool + sizeof(ngx_slab_pool_t)); slot = shift - pool->min_shift; page->next = slots[slot].next; slots[slot].next = page; page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_SMALL; page->next->prev = (uintptr_t) page | NGX_SLAB_SMALL; } bitmap[n] &= ~m; n = (1 << (ngx_pagesize_shift - shift)) / 8 / (1 << shift); if (n == 0) { n = 1; } if (bitmap[0] & ~(((uintptr_t) 1 << n) - 1)) { goto done; } map = (1 << (ngx_pagesize_shift - shift)) / (sizeof(uintptr_t) * 8); for (n = 1; n < map; n++) { if (bitmap[n]) { goto done; } } ngx_slab_free_pages(pool, page, 1); goto done; } goto chunk_already_free; case NGX_SLAB_EXACT: m = (uintptr_t) 1 << (((uintptr_t) p & (ngx_pagesize - 1)) >> ngx_slab_exact_shift); size = ngx_slab_exact_size; if ((uintptr_t) p & (size - 1)) { goto wrong_chunk; } if (slab & m) { if (slab == NGX_SLAB_BUSY) { slots = (ngx_slab_page_t *) ((u_char *) pool + sizeof(ngx_slab_pool_t)); slot = ngx_slab_exact_shift - pool->min_shift; page->next = slots[slot].next; slots[slot].next = page; page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_EXACT; page->next->prev = (uintptr_t) page | NGX_SLAB_EXACT; } page->slab &= ~m; if (page->slab) { goto done; } ngx_slab_free_pages(pool, page, 1); goto done; } goto chunk_already_free; case NGX_SLAB_BIG: shift = slab & NGX_SLAB_SHIFT_MASK; size = 1 << shift; if ((uintptr_t) p & (size - 1)) { goto wrong_chunk; } m = (uintptr_t) 1 << ((((uintptr_t) p & (ngx_pagesize - 1)) >> shift) + NGX_SLAB_MAP_SHIFT); if (slab & m) { if (page->next == NULL) { slots = (ngx_slab_page_t *) ((u_char *) pool + sizeof(ngx_slab_pool_t)); slot = shift - pool->min_shift; page->next = slots[slot].next; slots[slot].next = page; page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_BIG; page->next->prev = (uintptr_t) page | NGX_SLAB_BIG; } page->slab &= ~m; if (page->slab & NGX_SLAB_MAP_MASK) { goto done; } ngx_slab_free_pages(pool, page, 1); goto done; } goto chunk_already_free; case NGX_SLAB_PAGE: if ((uintptr_t) p & (ngx_pagesize - 1)) { goto wrong_chunk; } if (slab == NGX_SLAB_PAGE_FREE) { ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): page is already free"); goto fail; } if (slab == NGX_SLAB_PAGE_BUSY) { ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): pointer to wrong page"); goto fail; } n = ((u_char *) p - pool->start) >> ngx_pagesize_shift; size = slab & ~NGX_SLAB_PAGE_START; ngx_slab_free_pages(pool, &pool->pages[n], size); ngx_slab_junk(p, size << ngx_pagesize_shift); return; } return; done: ngx_slab_junk(p, size); return; wrong_chunk: ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): pointer to wrong chunk"); goto fail; chunk_already_free: ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): chunk is already free"); fail: return; }
['void\nngx_http_file_cache_free(ngx_http_request_t *r, ngx_temp_file_t *tf)\n{\n ngx_http_cache_t *c;\n ngx_http_file_cache_t *cache;\n ngx_http_file_cache_node_t *fcn;\n c = r->cache;\n if (c->updated) {\n return;\n }\n c->updated = 1;\n cache = c->file_cache;\n ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http file cache free");\n ngx_shmtx_lock(&cache->shpool->mutex);\n fcn = c->node;\n fcn->count--;\n fcn->updating = 0;\n if (c->error) {\n fcn->valid_sec = c->valid_sec;\n fcn->valid_msec = c->valid_msec;\n fcn->error = c->error;\n } else if (!fcn->exists && fcn->count == 0) {\n ngx_queue_remove(&fcn->queue);\n ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);\n ngx_slab_free_locked(cache->shpool, fcn);\n c->node = NULL;\n }\n ngx_shmtx_unlock(&cache->shpool->mutex);\n if (c->temp_file) {\n if (tf && tf->file.fd != NGX_INVALID_FILE) {\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http file cache incomplete: \\"%s\\"",\n tf->file.name.data);\n if (ngx_delete_file(tf->file.name.data) == NGX_FILE_ERROR) {\n ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,\n ngx_delete_file_n " \\"%s\\" failed",\n tf->file.name.data);\n }\n }\n }\n}', 'void\nngx_slab_free_locked(ngx_slab_pool_t *pool, void *p)\n{\n size_t size;\n uintptr_t slab, m, *bitmap;\n ngx_uint_t n, type, slot, shift, map;\n ngx_slab_page_t *slots, *page;\n ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, ngx_cycle->log, 0, "slab free: %p", p);\n if ((u_char *) p < pool->start || (u_char *) p > pool->end) {\n ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): outside of pool");\n goto fail;\n }\n n = ((u_char *) p - pool->start) >> ngx_pagesize_shift;\n page = &pool->pages[n];\n slab = page->slab;\n type = page->prev & NGX_SLAB_PAGE_MASK;\n switch (type) {\n case NGX_SLAB_SMALL:\n shift = slab & NGX_SLAB_SHIFT_MASK;\n size = 1 << shift;\n if ((uintptr_t) p & (size - 1)) {\n goto wrong_chunk;\n }\n n = ((uintptr_t) p & (ngx_pagesize - 1)) >> shift;\n m = (uintptr_t) 1 << (n & (sizeof(uintptr_t) * 8 - 1));\n n /= (sizeof(uintptr_t) * 8);\n bitmap = (uintptr_t *) ((uintptr_t) p & ~(ngx_pagesize - 1));\n if (bitmap[n] & m) {\n if (page->next == NULL) {\n slots = (ngx_slab_page_t *)\n ((u_char *) pool + sizeof(ngx_slab_pool_t));\n slot = shift - pool->min_shift;\n page->next = slots[slot].next;\n slots[slot].next = page;\n page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_SMALL;\n page->next->prev = (uintptr_t) page | NGX_SLAB_SMALL;\n }\n bitmap[n] &= ~m;\n n = (1 << (ngx_pagesize_shift - shift)) / 8 / (1 << shift);\n if (n == 0) {\n n = 1;\n }\n if (bitmap[0] & ~(((uintptr_t) 1 << n) - 1)) {\n goto done;\n }\n map = (1 << (ngx_pagesize_shift - shift)) / (sizeof(uintptr_t) * 8);\n for (n = 1; n < map; n++) {\n if (bitmap[n]) {\n goto done;\n }\n }\n ngx_slab_free_pages(pool, page, 1);\n goto done;\n }\n goto chunk_already_free;\n case NGX_SLAB_EXACT:\n m = (uintptr_t) 1 <<\n (((uintptr_t) p & (ngx_pagesize - 1)) >> ngx_slab_exact_shift);\n size = ngx_slab_exact_size;\n if ((uintptr_t) p & (size - 1)) {\n goto wrong_chunk;\n }\n if (slab & m) {\n if (slab == NGX_SLAB_BUSY) {\n slots = (ngx_slab_page_t *)\n ((u_char *) pool + sizeof(ngx_slab_pool_t));\n slot = ngx_slab_exact_shift - pool->min_shift;\n page->next = slots[slot].next;\n slots[slot].next = page;\n page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_EXACT;\n page->next->prev = (uintptr_t) page | NGX_SLAB_EXACT;\n }\n page->slab &= ~m;\n if (page->slab) {\n goto done;\n }\n ngx_slab_free_pages(pool, page, 1);\n goto done;\n }\n goto chunk_already_free;\n case NGX_SLAB_BIG:\n shift = slab & NGX_SLAB_SHIFT_MASK;\n size = 1 << shift;\n if ((uintptr_t) p & (size - 1)) {\n goto wrong_chunk;\n }\n m = (uintptr_t) 1 << ((((uintptr_t) p & (ngx_pagesize - 1)) >> shift)\n + NGX_SLAB_MAP_SHIFT);\n if (slab & m) {\n if (page->next == NULL) {\n slots = (ngx_slab_page_t *)\n ((u_char *) pool + sizeof(ngx_slab_pool_t));\n slot = shift - pool->min_shift;\n page->next = slots[slot].next;\n slots[slot].next = page;\n page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_BIG;\n page->next->prev = (uintptr_t) page | NGX_SLAB_BIG;\n }\n page->slab &= ~m;\n if (page->slab & NGX_SLAB_MAP_MASK) {\n goto done;\n }\n ngx_slab_free_pages(pool, page, 1);\n goto done;\n }\n goto chunk_already_free;\n case NGX_SLAB_PAGE:\n if ((uintptr_t) p & (ngx_pagesize - 1)) {\n goto wrong_chunk;\n }\n if (slab == NGX_SLAB_PAGE_FREE) {\n ngx_slab_error(pool, NGX_LOG_ALERT,\n "ngx_slab_free(): page is already free");\n goto fail;\n }\n if (slab == NGX_SLAB_PAGE_BUSY) {\n ngx_slab_error(pool, NGX_LOG_ALERT,\n "ngx_slab_free(): pointer to wrong page");\n goto fail;\n }\n n = ((u_char *) p - pool->start) >> ngx_pagesize_shift;\n size = slab & ~NGX_SLAB_PAGE_START;\n ngx_slab_free_pages(pool, &pool->pages[n], size);\n ngx_slab_junk(p, size << ngx_pagesize_shift);\n return;\n }\n return;\ndone:\n ngx_slab_junk(p, size);\n return;\nwrong_chunk:\n ngx_slab_error(pool, NGX_LOG_ALERT,\n "ngx_slab_free(): pointer to wrong chunk");\n goto fail;\nchunk_already_free:\n ngx_slab_error(pool, NGX_LOG_ALERT,\n "ngx_slab_free(): chunk is already free");\nfail:\n return;\n}']
35,708
0
https://github.com/libav/libav/blob/60728e8bab8d2a5f6bbb4baa7d53142dbc6047ed/libavformat/utils.c/#L2636
void avformat_free_context(AVFormatContext *s) { int i, j; AVStream *st; av_opt_free(s); if (s->iformat && s->iformat->priv_class && s->priv_data) av_opt_free(s->priv_data); for (i = 0; i < s->nb_streams; i++) { st = s->streams[i]; for (j = 0; j < st->nb_side_data; j++) av_freep(&st->side_data[j].data); av_freep(&st->side_data); st->nb_side_data = 0; if (st->parser) { av_parser_close(st->parser); } if (st->attached_pic.data) av_free_packet(&st->attached_pic); av_dict_free(&st->metadata); av_freep(&st->probe_data.buf); av_free(st->index_entries); av_free(st->codec->extradata); av_free(st->codec->subtitle_header); av_free(st->codec); av_free(st->priv_data); av_free(st->info); av_free(st); } for (i = s->nb_programs - 1; i >= 0; i--) { av_dict_free(&s->programs[i]->metadata); av_freep(&s->programs[i]->stream_index); av_freep(&s->programs[i]); } av_freep(&s->programs); av_freep(&s->priv_data); while (s->nb_chapters--) { av_dict_free(&s->chapters[s->nb_chapters]->metadata); av_free(s->chapters[s->nb_chapters]); } av_freep(&s->chapters); av_dict_free(&s->metadata); av_freep(&s->streams); av_freep(&s->internal); av_free(s); }
['static int avi_read_close(AVFormatContext *s)\n{\n int i;\n AVIContext *avi = s->priv_data;\n for (i = 0; i < s->nb_streams; i++) {\n AVStream *st = s->streams[i];\n AVIStream *ast = st->priv_data;\n if (ast) {\n if (ast->sub_ctx) {\n av_freep(&ast->sub_ctx->pb);\n avformat_close_input(&ast->sub_ctx);\n }\n av_free(ast->sub_buffer);\n av_free_packet(&ast->sub_pkt);\n }\n }\n av_free(avi->dv_demux);\n return 0;\n}', 'void av_freep(void *arg)\n{\n void **ptr = (void **)arg;\n av_free(*ptr);\n *ptr = NULL;\n}', 'void avformat_close_input(AVFormatContext **ps)\n{\n AVFormatContext *s = *ps;\n AVIOContext *pb = s->pb;\n if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||\n (s->flags & AVFMT_FLAG_CUSTOM_IO))\n pb = NULL;\n flush_packet_queue(s);\n if (s->iformat)\n if (s->iformat->read_close)\n s->iformat->read_close(s);\n avformat_free_context(s);\n *ps = NULL;\n avio_close(pb);\n}', 'void avformat_free_context(AVFormatContext *s)\n{\n int i, j;\n AVStream *st;\n av_opt_free(s);\n if (s->iformat && s->iformat->priv_class && s->priv_data)\n av_opt_free(s->priv_data);\n for (i = 0; i < s->nb_streams; i++) {\n st = s->streams[i];\n for (j = 0; j < st->nb_side_data; j++)\n av_freep(&st->side_data[j].data);\n av_freep(&st->side_data);\n st->nb_side_data = 0;\n if (st->parser) {\n av_parser_close(st->parser);\n }\n if (st->attached_pic.data)\n av_free_packet(&st->attached_pic);\n av_dict_free(&st->metadata);\n av_freep(&st->probe_data.buf);\n av_free(st->index_entries);\n av_free(st->codec->extradata);\n av_free(st->codec->subtitle_header);\n av_free(st->codec);\n av_free(st->priv_data);\n av_free(st->info);\n av_free(st);\n }\n for (i = s->nb_programs - 1; i >= 0; i--) {\n av_dict_free(&s->programs[i]->metadata);\n av_freep(&s->programs[i]->stream_index);\n av_freep(&s->programs[i]);\n }\n av_freep(&s->programs);\n av_freep(&s->priv_data);\n while (s->nb_chapters--) {\n av_dict_free(&s->chapters[s->nb_chapters]->metadata);\n av_free(s->chapters[s->nb_chapters]);\n }\n av_freep(&s->chapters);\n av_dict_free(&s->metadata);\n av_freep(&s->streams);\n av_freep(&s->internal);\n av_free(s);\n}']
35,709
0
https://github.com/openssl/openssl/blob/ed5538dc2bf723388aba3aea332bf554c514dcfe/crypto/ui/ui_lib.c/#L421
UI_METHOD *UI_create_method(char *name) { UI_METHOD *ui_method = (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD)); if (ui_method) memset(ui_method, 0, sizeof(*ui_method)); ui_method->name = BUF_strdup(name); return ui_method; }
['UI_METHOD *UI_create_method(char *name)\n\t{\n\tUI_METHOD *ui_method = (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD));\n\tif (ui_method)\n\t\tmemset(ui_method, 0, sizeof(*ui_method));\n\tui_method->name = BUF_strdup(name);\n\treturn ui_method;\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *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_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\treturn ret;\n\t}', 'char *BUF_strdup(const char *str)\n\t{\n\tchar *ret;\n\tint n;\n\tif (str == NULL) return(NULL);\n\tn=strlen(str);\n\tret=OPENSSL_malloc(n+1);\n\tif (ret == NULL)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_STRDUP,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tmemcpy(ret,str,n+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}']
35,710
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_sqr.c/#L168
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) { int i, j, max; const BN_ULONG *ap; BN_ULONG *rp; max = n * 2; ap = a; rp = r; rp[0] = rp[max - 1] = 0; rp++; j = n; if (--j > 0) { ap++; rp[j] = bn_mul_words(rp, ap, j, ap[-1]); rp += 2; } for (i = n - 2; i > 0; i--) { j--; ap++; rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]); rp += 2; } bn_add_words(r, r, r, max); bn_sqr_words(tmp, a, n); bn_add_words(r, r, tmp, max); }
['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 BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return -1;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\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 (!aa || !val[0])\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_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_sqr(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 || !tmp)\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 if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (rr != r)\n BN_copy(r, rr);\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_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
35,711
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 probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,\n const BIGNUM *rem, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *t1, *qadd, *q;\n bits--;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n qadd = BN_CTX_get(ctx);\n if (qadd == NULL)\n goto err;\n if (!BN_rshift1(qadd, padd))\n goto err;\n if (!BN_priv_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))\n goto err;\n if (!BN_mod(t1, q, qadd, ctx))\n goto err;\n if (!BN_sub(q, q, t1))\n goto err;\n if (rem == NULL) {\n if (!BN_add_word(q, 1))\n goto err;\n } else {\n if (!BN_rshift1(t1, rem))\n goto err;\n if (!BN_add(q, q, t1))\n goto err;\n }\n if (!BN_lshift1(p, q))\n goto err;\n if (!BN_add_word(p, 1))\n goto err;\n loop:\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);\n BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);\n if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)\n goto err;\n if (pmod == 0 || qmod == 0) {\n if (!BN_add(p, p, padd))\n goto err;\n if (!BN_add(q, q, qadd))\n goto err;\n goto loop;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(p);\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_rshift1(BIGNUM *r, const BIGNUM *a)\n{\n BN_ULONG *ap, *rp, t, c;\n int i, j;\n bn_check_top(r);\n bn_check_top(a);\n if (BN_is_zero(a)) {\n BN_zero(r);\n return 1;\n }\n i = a->top;\n ap = a->d;\n j = i - (ap[i - 1] == 1);\n if (a != r) {\n if (bn_wexpand(r, j) == NULL)\n return (0);\n r->neg = a->neg;\n }\n rp = r->d;\n t = ap[--i];\n c = (t & 1) ? BN_TBIT : 0;\n if (t >>= 1)\n rp[i] = t;\n while (i > 0) {\n t = ap[--i];\n rp[i] = ((t >> 1) & BN_MASK2) | c;\n c = (t & 1) ? BN_TBIT : 0;\n }\n r->top = j;\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,712
0
https://github.com/openssl/openssl/blob/d4b009d5f88875ac0e3ac0b2b9689ed16a4c88dc/crypto/lhash/lhash.c/#L209
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 dtls1_get_record(SSL *s)\n{\n int ssl_major, ssl_minor;\n int i, n;\n SSL3_RECORD *rr;\n unsigned char *p = NULL;\n unsigned short version;\n DTLS1_BITMAP *bitmap;\n unsigned int is_next_epoch;\n rr = RECORD_LAYER_get_rrec(&s->rlayer);\n if (dtls1_process_buffered_records(s) < 0)\n return -1;\n if (dtls1_get_processed_record(s))\n return 1;\n again:\n if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||\n (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {\n n = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,\n SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0);\n if (n <= 0)\n return (n);\n if (RECORD_LAYER_get_packet_length(&s->rlayer) != DTLS1_RT_HEADER_LENGTH) {\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);\n p = RECORD_LAYER_get_packet(&s->rlayer);\n if (s->msg_callback)\n s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,\n s, s->msg_callback_arg);\n rr->type = *(p++);\n ssl_major = *(p++);\n ssl_minor = *(p++);\n version = (ssl_major << 8) | ssl_minor;\n n2s(p, rr->epoch);\n memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);\n p += 6;\n n2s(p, rr->length);\n if (!s->first_packet) {\n if (version != s->version) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n }\n if ((version & 0xff00) != (s->version & 0xff00)) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n }\n if (rr->length >\n RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {\n i = rr->length;\n n = ssl3_read_n(s, i, i, 1);\n if (n != i) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n }\n RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);\n bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);\n if (bitmap == NULL) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n#ifndef OPENSSL_NO_SCTP\n if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {\n#endif\n if (!dtls1_record_replay_check(s, bitmap)) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n#ifndef OPENSSL_NO_SCTP\n }\n#endif\n if (rr->length == 0)\n goto again;\n if (is_next_epoch) {\n if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {\n if (dtls1_buffer_record\n (s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),\n rr->seq_num) < 0)\n return -1;\n dtls1_record_bitmap_update(s, bitmap);\n }\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n if (!dtls1_process_record(s)) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n dtls1_record_bitmap_update(s, bitmap);\n return (1);\n}', '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)) < 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, 0);\n if (enc_err == 0) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto err;\n }\n#ifdef TLS_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, 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)) {\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->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_w_lock(CRYPTO_LOCK_SSL_CTX);\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 if (lck)\n CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n if (ret) {\n r->not_resumable = 1;\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, r);\n SSL_SESSION_free(r);\n }\n } else\n ret = 0;\n return (ret);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *lh_delete(_LHASH *lh, const void *data)\n{\n unsigned long hash;\n LHASH_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,713
0
https://github.com/libav/libav/blob/7684a36113fa12c88ba80b5498f05849a6b58632/libavcodec/twinvq.c/#L984
static void transpose_perm(int16_t *out, int16_t *in, int num_vect, const uint8_t line_len[2], int length_div) { int i, j; int cont = 0; for (i = 0; i < num_vect; i++) for (j = 0; j < line_len[i >= length_div]; j++) out[cont++] = in[j * num_vect + i]; }
['static av_cold void init_bitstream_params(TwinVQContext *tctx)\n{\n const TwinVQModeTab *mtab = tctx->mtab;\n int n_ch = tctx->avctx->channels;\n int total_fr_bits = tctx->avctx->bit_rate * mtab->size /\n tctx->avctx->sample_rate;\n int lsp_bits_per_block = n_ch * (mtab->lsp_bit0 + mtab->lsp_bit1 +\n mtab->lsp_split * mtab->lsp_bit2);\n int ppc_bits = n_ch * (mtab->pgain_bit + mtab->ppc_shape_bit +\n mtab->ppc_period_bit);\n int bsize_no_main_cb[3], bse_bits[3], i;\n enum TwinVQFrameType frametype;\n for (i = 0; i < 3; i++)\n bse_bits[i] = n_ch *\n (mtab->fmode[i].bark_n_coef *\n mtab->fmode[i].bark_n_bit + 1);\n bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +\n WINDOW_TYPE_BITS + n_ch * GAIN_BITS;\n for (i = 0; i < 2; i++)\n bsize_no_main_cb[i] =\n lsp_bits_per_block + n_ch * GAIN_BITS + WINDOW_TYPE_BITS +\n mtab->fmode[i].sub * (bse_bits[i] + n_ch * SUB_GAIN_BITS);\n for (i = 0; i < 4; i++) {\n int bit_size, vect_size;\n int rounded_up, rounded_down, num_rounded_down, num_rounded_up;\n if (i == 3) {\n bit_size = n_ch * mtab->ppc_shape_bit;\n vect_size = n_ch * mtab->ppc_shape_len;\n } else {\n bit_size = total_fr_bits - bsize_no_main_cb[i];\n vect_size = n_ch * mtab->size;\n }\n tctx->n_div[i] = (bit_size + 13) / 14;\n rounded_up = (bit_size + tctx->n_div[i] - 1) /\n tctx->n_div[i];\n rounded_down = (bit_size) / tctx->n_div[i];\n num_rounded_down = rounded_up * tctx->n_div[i] - bit_size;\n num_rounded_up = tctx->n_div[i] - num_rounded_down;\n tctx->bits_main_spec[0][i][0] = (rounded_up + 1) / 2;\n tctx->bits_main_spec[1][i][0] = rounded_up / 2;\n tctx->bits_main_spec[0][i][1] = (rounded_down + 1) / 2;\n tctx->bits_main_spec[1][i][1] = rounded_down / 2;\n tctx->bits_main_spec_change[i] = num_rounded_up;\n rounded_up = (vect_size + tctx->n_div[i] - 1) /\n tctx->n_div[i];\n rounded_down = (vect_size) / tctx->n_div[i];\n num_rounded_down = rounded_up * tctx->n_div[i] - vect_size;\n num_rounded_up = tctx->n_div[i] - num_rounded_down;\n tctx->length[i][0] = rounded_up;\n tctx->length[i][1] = rounded_down;\n tctx->length_change[i] = num_rounded_up;\n }\n for (frametype = FT_SHORT; frametype <= FT_PPC; frametype++)\n construct_perm_table(tctx, frametype);\n}', 'static av_cold void construct_perm_table(TwinVQContext *tctx,\n enum TwinVQFrameType ftype)\n{\n int block_size, size;\n const TwinVQModeTab *mtab = tctx->mtab;\n int16_t *tmp_perm = (int16_t *)tctx->tmp_buf;\n if (ftype == FT_PPC) {\n size = tctx->avctx->channels;\n block_size = mtab->ppc_shape_len;\n } else {\n size = tctx->avctx->channels * mtab->fmode[ftype].sub;\n block_size = mtab->size / mtab->fmode[ftype].sub;\n }\n permutate_in_line(tmp_perm, tctx->n_div[ftype], size,\n block_size, tctx->length[ftype],\n tctx->length_change[ftype], ftype);\n transpose_perm(tctx->permut[ftype], tmp_perm, tctx->n_div[ftype],\n tctx->length[ftype], tctx->length_change[ftype]);\n linear_perm(tctx->permut[ftype], tctx->permut[ftype], size,\n size * block_size);\n}', 'static void transpose_perm(int16_t *out, int16_t *in, int num_vect,\n const uint8_t line_len[2], int length_div)\n{\n int i, j;\n int cont = 0;\n for (i = 0; i < num_vect; i++)\n for (j = 0; j < line_len[i >= length_div]; j++)\n out[cont++] = in[j * num_vect + i];\n}']
35,714
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_gf2m.c/#L818
int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[], BN_CTX *ctx) { BIGNUM *field; int ret = 0; bn_check_top(xx); BN_CTX_start(ctx); if ((field = BN_CTX_get(ctx)) == NULL) goto err; if (!BN_GF2m_arr2poly(p, field)) goto err; ret = BN_GF2m_mod_inv(r, xx, field, ctx); bn_check_top(r); err: BN_CTX_end(ctx); return ret; }
['int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],\n BN_CTX *ctx)\n{\n BIGNUM *field;\n int ret = 0;\n bn_check_top(xx);\n BN_CTX_start(ctx);\n if ((field = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_GF2m_arr2poly(p, field))\n goto err;\n ret = BN_GF2m_mod_inv(r, xx, field, ctx);\n bn_check_top(r);\n err:\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}', 'static int BN_STACK_push(BN_STACK *st, unsigned int idx)\n{\n if (st->depth == st->size) {\n unsigned int newsize =\n st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES;\n unsigned int *newitems = OPENSSL_malloc(sizeof(*newitems) * newsize);\n if (newitems == NULL)\n return 0;\n if (st->depth)\n memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth);\n OPENSSL_free(st->indexes);\n st->indexes = newitems;\n st->size = newsize;\n }\n st->indexes[(st->depth)++] = idx;\n return 1;\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#ifdef 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}', 'void CRYPTO_free(void *str)\n{\n#ifdef CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0);\n free(str);\n CRYPTO_mem_debug_free(str, 1);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\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}']
35,715
0
https://github.com/openssl/openssl/blob/b66411f6cda6970c01283ddde6d8063c57b3b7d9/crypto/bn/bn_print.c/#L171
int BN_hex2bn(BIGNUM **bn, const char *a) { BIGNUM *ret = NULL; BN_ULONG l = 0; int neg = 0, h, m, i, j, k, c; int num; if ((a == NULL) || (*a == '\0')) return (0); if (*a == '-') { neg = 1; a++; } for (i = 0; i <= (INT_MAX/4) && isxdigit((unsigned char)a[i]); i++) continue; if (i == 0 || i > INT_MAX/4) goto err; num = i + neg; if (bn == NULL) return (num); if (*bn == NULL) { if ((ret = BN_new()) == NULL) return (0); } else { ret = *bn; BN_zero(ret); } if (bn_expand(ret, i * 4) == NULL) goto err; j = i; m = 0; h = 0; while (j > 0) { m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j; l = 0; for (;;) { c = a[j - m]; k = OPENSSL_hexchar2int(c); if (k < 0) k = 0; l = (l << 4) | k; if (--m <= 0) { ret->d[h++] = l; break; } } j -= (BN_BYTES * 2); } ret->top = h; bn_correct_top(ret); *bn = ret; bn_check_top(ret); if (ret->top != 0) ret->neg = neg; return (num); err: if (*bn == NULL) BN_free(ret); return (0); }
['static int run_srp_kat(void)\n{\n int ret = 0;\n BIGNUM *s = NULL;\n BIGNUM *v = NULL;\n BIGNUM *a = NULL;\n BIGNUM *b = NULL;\n BIGNUM *u = NULL;\n BIGNUM *x = NULL;\n BIGNUM *Apub = NULL;\n BIGNUM *Bpub = NULL;\n BIGNUM *Kclient = NULL;\n BIGNUM *Kserver = NULL;\n const SRP_gN *GN;\n if (!TEST_ptr(GN = SRP_get_default_gN("1024")))\n goto err;\n BN_hex2bn(&s, "BEB25379D1A8581EB5A727673A2441EE");\n if (!TEST_true(SRP_create_verifier_BN("alice", "password123", &s, &v, GN->N,\n GN->g)))\n goto err;\n if (!TEST_true(check_bn("v", v,\n "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D812"\n "9BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5"\n "C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5"\n "EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78"\n "E955A5E29E7AB245DB2BE315E2099AFB")))\n goto err;\n BN_hex2bn(&b, "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1"\n "05284D20");\n Bpub = SRP_Calc_B(b, GN->N, GN->g, v);\n if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))\n goto err;\n if (!TEST_true(check_bn("B", Bpub,\n "BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011"\n "BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC99"\n "6C6DA04453728610D0C6DDB58B318885D7D82C7F8DEB75CE7BD4FBAA"\n "37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAE"\n "EB4012B7D7665238A8E3FB004B117B58")))\n goto err;\n BN_hex2bn(&a, "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD"\n "DA2D4393");\n Apub = SRP_Calc_A(a, GN->N, GN->g);\n if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))\n goto err;\n if (!TEST_true(check_bn("A", Apub,\n "61D5E490F6F1B79547B0704C436F523DD0E560F0C64115BB72557EC4"\n "4352E8903211C04692272D8B2D1A5358A2CF1B6E0BFCF99F921530EC"\n "8E39356179EAE45E42BA92AEACED825171E1E8B9AF6D9C03E1327F44"\n "BE087EF06530E69F66615261EEF54073CA11CF5858F0EDFDFE15EFEA"\n "B349EF5D76988A3672FAC47B0769447B")))\n goto err;\n u = SRP_Calc_u(Apub, Bpub, GN->N);\n if (!TEST_true(check_bn("u", u,\n "CE38B9593487DA98554ED47D70A7AE5F462EF019")))\n goto err;\n x = SRP_Calc_x(s, "alice", "password123");\n Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);\n if (!TEST_true(check_bn("Client\'s key", Kclient,\n "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"\n "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"\n "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"\n "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"\n "C346D7E474B29EDE8A469FFECA686E5A")))\n goto err;\n Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);\n if (!TEST_true(check_bn("Server\'s key", Kserver,\n "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"\n "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"\n "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"\n "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"\n "C346D7E474B29EDE8A469FFECA686E5A")))\n goto err;\n ret = 1;\nerr:\n BN_clear_free(Kclient);\n BN_clear_free(Kserver);\n BN_clear_free(x);\n BN_free(u);\n BN_free(Apub);\n BN_clear_free(a);\n BN_free(Bpub);\n BN_clear_free(b);\n BN_free(s);\n BN_clear_free(v);\n return ret;\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}"]
35,716
0
https://github.com/libav/libav/blob/a158446b2842143a1ea0a284952571435c9ad3c4/ffmpeg.c/#L3497
static void new_audio_stream(AVFormatContext *oc) { AVStream *st; AVCodecContext *audio_enc; enum CodecID codec_id; st = av_new_stream(oc, streamid_map[oc->nb_streams]); if (!st) { fprintf(stderr, "Could not alloc stream\n"); av_exit(1); } avcodec_get_context_defaults2(st->codec, AVMEDIA_TYPE_AUDIO); bitstream_filters[nb_output_files][oc->nb_streams - 1]= audio_bitstream_filters; audio_bitstream_filters= NULL; avcodec_thread_init(st->codec, thread_count); audio_enc = st->codec; audio_enc->codec_type = AVMEDIA_TYPE_AUDIO; if(audio_codec_tag) audio_enc->codec_tag= audio_codec_tag; if (oc->oformat->flags & AVFMT_GLOBALHEADER) { audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER; avcodec_opts[AVMEDIA_TYPE_AUDIO]->flags|= CODEC_FLAG_GLOBAL_HEADER; } if (audio_stream_copy) { st->stream_copy = 1; audio_enc->channels = audio_channels; audio_enc->sample_rate = audio_sample_rate; } else { AVCodec *codec; set_context_opts(audio_enc, avcodec_opts[AVMEDIA_TYPE_AUDIO], AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM); if (audio_codec_name) { codec_id = find_codec_or_die(audio_codec_name, AVMEDIA_TYPE_AUDIO, 1, audio_enc->strict_std_compliance); codec = avcodec_find_encoder_by_name(audio_codec_name); output_codecs[nb_ocodecs] = codec; } else { codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_AUDIO); codec = avcodec_find_encoder(codec_id); } audio_enc->codec_id = codec_id; if (audio_qscale > QSCALE_NONE) { audio_enc->flags |= CODEC_FLAG_QSCALE; audio_enc->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale; } audio_enc->channels = audio_channels; audio_enc->sample_fmt = audio_sample_fmt; audio_enc->sample_rate = audio_sample_rate; audio_enc->channel_layout = channel_layout; if (avcodec_channel_layout_num_channels(channel_layout) != audio_channels) audio_enc->channel_layout = 0; choose_sample_fmt(st, codec); choose_sample_rate(st, codec); } nb_ocodecs++; audio_enc->time_base= (AVRational){1, audio_sample_rate}; if (audio_language) { av_metadata_set2(&st->metadata, "language", audio_language, 0); av_freep(&audio_language); } audio_disable = 0; av_freep(&audio_codec_name); audio_stream_copy = 0; }
['static void new_audio_stream(AVFormatContext *oc)\n{\n AVStream *st;\n AVCodecContext *audio_enc;\n enum CodecID codec_id;\n st = av_new_stream(oc, streamid_map[oc->nb_streams]);\n if (!st) {\n fprintf(stderr, "Could not alloc stream\\n");\n av_exit(1);\n }\n avcodec_get_context_defaults2(st->codec, AVMEDIA_TYPE_AUDIO);\n bitstream_filters[nb_output_files][oc->nb_streams - 1]= audio_bitstream_filters;\n audio_bitstream_filters= NULL;\n avcodec_thread_init(st->codec, thread_count);\n audio_enc = st->codec;\n audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;\n if(audio_codec_tag)\n audio_enc->codec_tag= audio_codec_tag;\n if (oc->oformat->flags & AVFMT_GLOBALHEADER) {\n audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;\n avcodec_opts[AVMEDIA_TYPE_AUDIO]->flags|= CODEC_FLAG_GLOBAL_HEADER;\n }\n if (audio_stream_copy) {\n st->stream_copy = 1;\n audio_enc->channels = audio_channels;\n audio_enc->sample_rate = audio_sample_rate;\n } else {\n AVCodec *codec;\n set_context_opts(audio_enc, avcodec_opts[AVMEDIA_TYPE_AUDIO], AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM);\n if (audio_codec_name) {\n codec_id = find_codec_or_die(audio_codec_name, AVMEDIA_TYPE_AUDIO, 1,\n audio_enc->strict_std_compliance);\n codec = avcodec_find_encoder_by_name(audio_codec_name);\n output_codecs[nb_ocodecs] = codec;\n } else {\n codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_AUDIO);\n codec = avcodec_find_encoder(codec_id);\n }\n audio_enc->codec_id = codec_id;\n if (audio_qscale > QSCALE_NONE) {\n audio_enc->flags |= CODEC_FLAG_QSCALE;\n audio_enc->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale;\n }\n audio_enc->channels = audio_channels;\n audio_enc->sample_fmt = audio_sample_fmt;\n audio_enc->sample_rate = audio_sample_rate;\n audio_enc->channel_layout = channel_layout;\n if (avcodec_channel_layout_num_channels(channel_layout) != audio_channels)\n audio_enc->channel_layout = 0;\n choose_sample_fmt(st, codec);\n choose_sample_rate(st, codec);\n }\n nb_ocodecs++;\n audio_enc->time_base= (AVRational){1, audio_sample_rate};\n if (audio_language) {\n av_metadata_set2(&st->metadata, "language", audio_language, 0);\n av_freep(&audio_language);\n }\n audio_disable = 0;\n av_freep(&audio_codec_name);\n audio_stream_copy = 0;\n}', 'AVStream *av_new_stream(AVFormatContext *s, int id)\n{\n AVStream *st;\n int i;\n if (s->nb_streams >= MAX_STREAMS){\n av_log(s, AV_LOG_ERROR, "Too many streams\\n");\n return NULL;\n }\n st = av_mallocz(sizeof(AVStream));\n if (!st)\n return NULL;\n st->codec= avcodec_alloc_context();\n if (s->iformat) {\n st->codec->bit_rate = 0;\n }\n st->index = s->nb_streams;\n st->id = id;\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 av_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,717
0
https://github.com/libav/libav/blob/490a022d86ef1c506a79744c5a95368af356fc69/libavcodec/smacker.c/#L290
static int decode_header_trees(SmackVContext *smk) { GetBitContext gb; int mmap_size, mclr_size, full_size, type_size; mmap_size = AV_RL32(smk->avctx->extradata); mclr_size = AV_RL32(smk->avctx->extradata + 4); full_size = AV_RL32(smk->avctx->extradata + 8); type_size = AV_RL32(smk->avctx->extradata + 12); init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8); if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n"); smk->mmap_tbl = av_malloc(sizeof(int) * 2); smk->mmap_tbl[0] = 0; smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1; } else { smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size); } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n"); smk->mclr_tbl = av_malloc(sizeof(int) * 2); smk->mclr_tbl[0] = 0; smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1; } else { smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size); } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n"); smk->full_tbl = av_malloc(sizeof(int) * 2); smk->full_tbl[0] = 0; smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1; } else { smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size); } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n"); smk->type_tbl = av_malloc(sizeof(int) * 2); smk->type_tbl[0] = 0; smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1; } else { smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size); } return 0; }
['static int decode_header_trees(SmackVContext *smk) {\n GetBitContext gb;\n int mmap_size, mclr_size, full_size, type_size;\n mmap_size = AV_RL32(smk->avctx->extradata);\n mclr_size = AV_RL32(smk->avctx->extradata + 4);\n full_size = AV_RL32(smk->avctx->extradata + 8);\n type_size = AV_RL32(smk->avctx->extradata + 12);\n init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\\n");\n smk->mmap_tbl = av_malloc(sizeof(int) * 2);\n smk->mmap_tbl[0] = 0;\n smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\\n");\n smk->mclr_tbl = av_malloc(sizeof(int) * 2);\n smk->mclr_tbl[0] = 0;\n smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\\n");\n smk->full_tbl = av_malloc(sizeof(int) * 2);\n smk->full_tbl[0] = 0;\n smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\\n");\n smk->type_tbl = av_malloc(sizeof(int) * 2);\n smk->type_tbl[0] = 0;\n smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);\n }\n return 0;\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 s->buffer_end = buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index = 0;\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer & ~3);\n s->bit_count = 32 + 8*((intptr_t)buffer & 3);\n skip_bits_long(s, 0);\n#endif\n}', 'static inline unsigned int get_bits1(GetBitContext *s){\n#ifdef ALT_BITSTREAM_READER\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 index++;\n s->index = index;\n return result;\n#else\n return get_bits(s, 1);\n#endif\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-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,718
0
https://github.com/openssl/openssl/blob/7141ba31969d0b378d08104a51f8f99b9187b9d5/crypto/evp/evp_enc.c/#L288
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 enc_write(BIO *b, const char *in, int inl)\n{\n int ret = 0, n, i;\n BIO_ENC_CTX *ctx;\n BIO *next;\n ctx = BIO_get_data(b);\n next = BIO_next(b);\n if ((ctx == NULL) || (next == NULL))\n return 0;\n ret = inl;\n BIO_clear_retry_flags(b);\n n = ctx->buf_len - ctx->buf_off;\n while (n > 0) {\n i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);\n if (i <= 0) {\n BIO_copy_next_retry(b);\n return (i);\n }\n ctx->buf_off += i;\n n -= i;\n }\n if ((in == NULL) || (inl <= 0))\n return (0);\n ctx->buf_off = 0;\n while (inl > 0) {\n n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;\n if (!EVP_CipherUpdate(ctx->cipher,\n ctx->buf, &ctx->buf_len,\n (const unsigned char *)in, n)) {\n BIO_clear_retry_flags(b);\n ctx->ok = 0;\n return 0;\n }\n inl -= n;\n in += n;\n ctx->buf_off = 0;\n n = ctx->buf_len;\n while (n > 0) {\n i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);\n if (i <= 0) {\n BIO_copy_next_retry(b);\n return (ret == inl) ? i : ret - inl;\n }\n n -= i;\n ctx->buf_off += i;\n }\n ctx->buf_len = 0;\n ctx->buf_off = 0;\n }\n BIO_copy_next_retry(b);\n return (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 assert(!overlapped);\n return overlapped;\n}']
35,719
0
https://github.com/openssl/openssl/blob/9b340281873643d2b8a33047dc8bfa607f7e0c3c/crypto/lhash/lhash.c/#L191
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 int test_fatalerr(void)\n{\n SSL_CTX *sctx = NULL, *cctx = NULL;\n SSL *sssl = NULL, *cssl = NULL;\n const char *msg = "Dummy";\n BIO *wbio = NULL;\n int ret = 0, len;\n char buf[80];\n unsigned char dummyrec[] = {\n 0x17, 0x03, 0x03, 0x00, 0x05, \'D\', \'u\', \'m\', \'m\', \'y\'\n };\n if (!TEST_true(create_ssl_ctx_pair(TLS_method(), TLS_method(),\n TLS1_VERSION, TLS_MAX_VERSION,\n &sctx, &cctx, cert, privkey)))\n goto err;\n if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA"))\n || !TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-SHA"))\n || !TEST_true(SSL_CTX_set_ciphersuites(sctx,\n "TLS_AES_128_GCM_SHA256"))\n || !TEST_true(SSL_CTX_set_ciphersuites(cctx,\n "TLS_AES_256_GCM_SHA384"))\n || !TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL,\n NULL)))\n goto err;\n wbio = SSL_get_wbio(cssl);\n if (!TEST_ptr(wbio)) {\n printf("Unexpected NULL bio received\\n");\n goto err;\n }\n if (!TEST_false(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)))\n goto err;\n ERR_clear_error();\n if (!TEST_int_gt(BIO_write(wbio, dummyrec, sizeof(dummyrec)), 0))\n goto err;\n if (!TEST_int_le(len = SSL_read(sssl, buf, sizeof(buf) - 1), 0)) {\n buf[len] = \'\\0\';\n TEST_error("Unexpected success reading data: %s\\n", buf);\n goto err;\n }\n if (!TEST_int_le(SSL_write(sssl, msg, strlen(msg)), 0))\n goto err;\n ret = 1;\n err:\n SSL_free(sssl);\n SSL_free(cssl);\n SSL_CTX_free(sctx);\n SSL_CTX_free(cctx);\n return ret;\n}', 'int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,\n SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)\n{\n SSL *serverssl = NULL, *clientssl = NULL;\n BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;\n if (*sssl != NULL)\n serverssl = *sssl;\n else if (!TEST_ptr(serverssl = SSL_new(serverctx)))\n goto error;\n if (*cssl != NULL)\n clientssl = *cssl;\n else if (!TEST_ptr(clientssl = SSL_new(clientctx)))\n goto error;\n if (SSL_is_dtls(clientssl)) {\n if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))\n || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))\n goto error;\n } else {\n if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))\n || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))\n goto error;\n }\n if (s_to_c_fbio != NULL\n && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))\n goto error;\n if (c_to_s_fbio != NULL\n && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))\n goto error;\n BIO_set_mem_eof_return(s_to_c_bio, -1);\n BIO_set_mem_eof_return(c_to_s_bio, -1);\n SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);\n BIO_up_ref(s_to_c_bio);\n BIO_up_ref(c_to_s_bio);\n SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);\n *sssl = serverssl;\n *cssl = clientssl;\n return 1;\n error:\n SSL_free(serverssl);\n SSL_free(clientssl);\n BIO_free(s_to_c_bio);\n BIO_free(c_to_s_bio);\n BIO_free(s_to_c_fbio);\n BIO_free(c_to_s_fbio);\n return 0;\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->references = 1;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n OPENSSL_free(s);\n s = NULL;\n goto err;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\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->max_early_data = ctx->max_early_data;\n s->recv_max_early_data = ctx->recv_max_early_data;\n s->num_tickets = ctx->num_tickets;\n s->pha_enabled = ctx->pha_enabled;\n s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);\n if (s->tls13_ciphersuites == NULL)\n goto err;\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->record_padding_cb = ctx->record_padding_cb;\n s->record_padding_arg = ctx->record_padding_arg;\n s->block_padding = ctx->block_padding;\n s->sid_ctx_length = ctx->sid_ctx_length;\n if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))\n goto err;\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->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;\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->ext.debug_cb = 0;\n s->ext.debug_arg = NULL;\n s->ext.ticket_expected = 0;\n s->ext.status_type = ctx->ext.status_type;\n s->ext.status_expected = 0;\n s->ext.ocsp.ids = NULL;\n s->ext.ocsp.exts = NULL;\n s->ext.ocsp.resp = NULL;\n s->ext.ocsp.resp_len = 0;\n SSL_CTX_up_ref(ctx);\n s->session_ctx = ctx;\n#ifndef OPENSSL_NO_EC\n if (ctx->ext.ecpointformats) {\n s->ext.ecpointformats =\n OPENSSL_memdup(ctx->ext.ecpointformats,\n ctx->ext.ecpointformats_len);\n if (!s->ext.ecpointformats)\n goto err;\n s->ext.ecpointformats_len =\n ctx->ext.ecpointformats_len;\n }\n if (ctx->ext.supportedgroups) {\n s->ext.supportedgroups =\n OPENSSL_memdup(ctx->ext.supportedgroups,\n ctx->ext.supportedgroups_len\n * sizeof(*ctx->ext.supportedgroups));\n if (!s->ext.supportedgroups)\n goto err;\n s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n s->ext.npn = NULL;\n#endif\n if (s->ctx->ext.alpn) {\n s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);\n if (s->ext.alpn == NULL)\n goto err;\n memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);\n s->ext.alpn_len = s->ctx->ext.alpn_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 s->key_update = SSL_KEY_UPDATE_NONE;\n s->allow_early_data_cb = ctx->allow_early_data_cb;\n s->allow_early_data_cb_data = ctx->allow_early_data_cb_data;\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->psk_find_session_cb = ctx->psk_find_session_cb;\n s->psk_use_session_cb = ctx->psk_use_session_cb;\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_DOWN_REF(&s->references, &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 RECORD_LAYER_release(&s->rlayer);\n ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n s->wbio = NULL;\n BIO_free_all(s->rbio);\n s->rbio = NULL;\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 sk_SSL_CIPHER_free(s->tls13_ciphersuites);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n SSL_SESSION_free(s->psksession);\n OPENSSL_free(s->psksession_id);\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->ext.hostname);\n SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->ext.ecpointformats);\n OPENSSL_free(s->ext.supportedgroups);\n#endif\n sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->ext.scts);\n#endif\n OPENSSL_free(s->ext.ocsp.resp);\n OPENSSL_free(s->ext.alpn);\n OPENSSL_free(s->ext.tls13_cookie);\n OPENSSL_free(s->clienthello);\n OPENSSL_free(s->pha_context);\n EVP_MD_CTX_free(s->pha_dgst);\n sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);\n sk_X509_NAME_pop_free(s->client_ca_names, 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 SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->ext.npn);\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_DOWN_REF(&a->references, &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 sk_SSL_CIPHER_free(a->tls13_ciphersuites);\n ssl_cert_free(a->cert);\n sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);\n sk_X509_NAME_pop_free(a->client_ca_names, 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->ext.ecpointformats);\n OPENSSL_free(a->ext.supportedgroups);\n#endif\n OPENSSL_free(a->ext.alpn);\n OPENSSL_secure_free(a->ext.secure);\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,720
0
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/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; }
['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 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 (!d || !r || !val[0])\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_mul_montgomery(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#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n int num = mont->N.top;\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 bn_correct_top(r);\n return (1);\n }\n }\n#endif\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 bn_check_top(r);\n ret = 1;\n err:\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 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}', '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}']
35,721
0
https://github.com/libav/libav/blob/4601e76a4323deec30cc6b1d2cf1f192feedccbc/libavcodec/v210enc.c/#L45
static av_cold int encode_init(AVCodecContext *avctx) { if (avctx->width & 1) { av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n"); return -1; } if (avctx->pix_fmt != PIX_FMT_YUV422P16) { av_log(avctx, AV_LOG_ERROR, "v210 needs YUV422P16\n"); return -1; } if (avctx->bits_per_raw_sample != 10) av_log(avctx, AV_LOG_WARNING, "bits per raw sample: %d != 10-bit\n", avctx->bits_per_raw_sample); avctx->coded_frame = avcodec_alloc_frame(); avctx->coded_frame->key_frame = 1; avctx->coded_frame->pict_type = FF_I_TYPE; return 0; }
['static av_cold int encode_init(AVCodecContext *avctx)\n{\n if (avctx->width & 1) {\n av_log(avctx, AV_LOG_ERROR, "v210 needs even width\\n");\n return -1;\n }\n if (avctx->pix_fmt != PIX_FMT_YUV422P16) {\n av_log(avctx, AV_LOG_ERROR, "v210 needs YUV422P16\\n");\n return -1;\n }\n if (avctx->bits_per_raw_sample != 10)\n av_log(avctx, AV_LOG_WARNING, "bits per raw sample: %d != 10-bit\\n",\n avctx->bits_per_raw_sample);\n avctx->coded_frame = avcodec_alloc_frame();\n avctx->coded_frame->key_frame = 1;\n avctx->coded_frame->pict_type = FF_I_TYPE;\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 = 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,722
0
https://github.com/libav/libav/blob/834259528b6cf593bf9544e3183b84b9b7880641/libavcodec/golomb.h/#L130
static inline unsigned svq3_get_ue_golomb(GetBitContext *gb) { uint32_t buf; OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); if (buf & 0xAA800000) { buf >>= 32 - 8; LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]); CLOSE_READER(re, gb); return ff_interleaved_ue_golomb_vlc_code[buf]; } else { unsigned ret = 1; do { buf >>= 32 - 8; LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8)); if (ff_interleaved_golomb_vlc_len[buf] != 9) { ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1; ret |= ff_interleaved_dirac_golomb_vlc_code[buf]; break; } ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf]; UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); } while (HAVE_BITS_REMAINING(re, gb)); CLOSE_READER(re, gb); return ret - 1; } }
['static inline unsigned svq3_get_ue_golomb(GetBitContext *gb)\n{\n uint32_t buf;\n OPEN_READER(re, gb);\n UPDATE_CACHE(re, gb);\n buf = GET_CACHE(re, gb);\n if (buf & 0xAA800000) {\n buf >>= 32 - 8;\n LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);\n CLOSE_READER(re, gb);\n return ff_interleaved_ue_golomb_vlc_code[buf];\n } else {\n unsigned ret = 1;\n do {\n buf >>= 32 - 8;\n LAST_SKIP_BITS(re, gb,\n FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));\n if (ff_interleaved_golomb_vlc_len[buf] != 9) {\n ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;\n ret |= ff_interleaved_dirac_golomb_vlc_code[buf];\n break;\n }\n ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];\n UPDATE_CACHE(re, gb);\n buf = GET_CACHE(re, gb);\n } while (HAVE_BITS_REMAINING(re, gb));\n CLOSE_READER(re, gb);\n return ret - 1;\n }\n}']
35,723
0
https://github.com/nginx/nginx/blob/8ce8f6667f3f14c004148138c0aec3dff79c350b/src/core/ngx_string.h/#L70
static ngx_inline u_char * ngx_strlchr(u_char *p, u_char *last, u_char c) { while (p < last) { if (*p == c) { return p; } p++; } return NULL; }
['ngx_int_t\nngx_http_core_try_files_phase(ngx_http_request_t *r,\n ngx_http_phase_handler_t *ph)\n{\n size_t len, root, alias, reserve, allocated;\n u_char *p, *name;\n ngx_str_t path, args;\n ngx_uint_t test_dir;\n ngx_http_try_file_t *tf;\n ngx_open_file_info_t of;\n ngx_http_script_code_pt code;\n ngx_http_script_engine_t e;\n ngx_http_core_loc_conf_t *clcf;\n ngx_http_script_len_code_pt lcode;\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "try files phase: %ui", r->phase_handler);\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (clcf->try_files == NULL) {\n r->phase_handler++;\n return NGX_AGAIN;\n }\n allocated = 0;\n root = 0;\n name = NULL;\n path.data = NULL;\n tf = clcf->try_files;\n alias = clcf->alias;\n for ( ;; ) {\n if (tf->lengths) {\n ngx_memzero(&e, sizeof(ngx_http_script_engine_t));\n e.ip = tf->lengths->elts;\n e.request = r;\n len = 1;\n while (*(uintptr_t *) e.ip) {\n lcode = *(ngx_http_script_len_code_pt *) e.ip;\n len += lcode(&e);\n }\n } else {\n len = tf->name.len;\n }\n reserve = ngx_abs((ssize_t) (len - r->uri.len)) + alias + 16;\n if (reserve > allocated) {\n if (ngx_http_map_uri_to_path(r, &path, &root, reserve) == NULL) {\n ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_OK;\n }\n name = path.data + root;\n allocated = path.len - root - (r->uri.len - alias);\n }\n if (tf->values == NULL) {\n ngx_memcpy(name, tf->name.data, tf->name.len);\n path.len = (name + tf->name.len - 1) - path.data;\n } else {\n e.ip = tf->values->elts;\n e.pos = name;\n e.flushed = 1;\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 path.len = e.pos - path.data;\n *e.pos = \'\\0\';\n if (alias && ngx_strncmp(name, clcf->name.data, alias) == 0) {\n ngx_memmove(name, name + alias, len - alias);\n path.len -= alias;\n }\n }\n test_dir = tf->test_dir;\n tf++;\n ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "trying to use %s: \\"%s\\" \\"%s\\"",\n test_dir ? "dir" : "file", name, path.data);\n if (tf->lengths == NULL && tf->name.len == 0) {\n if (tf->code) {\n ngx_http_finalize_request(r, tf->code);\n return NGX_OK;\n }\n path.len -= root;\n path.data += root;\n if (path.data[0] == \'@\') {\n (void) ngx_http_named_location(r, &path);\n } else {\n ngx_http_split_args(r, &path, &args);\n (void) ngx_http_internal_redirect(r, &path, &args);\n }\n ngx_http_finalize_request(r, NGX_DONE);\n return NGX_OK;\n }\n ngx_memzero(&of, sizeof(ngx_open_file_info_t));\n of.read_ahead = clcf->read_ahead;\n of.directio = clcf->directio;\n of.valid = clcf->open_file_cache_valid;\n of.min_uses = clcf->open_file_cache_min_uses;\n of.test_only = 1;\n of.errors = clcf->open_file_cache_errors;\n of.events = clcf->open_file_cache_events;\n#if (NGX_HAVE_OPENAT)\n of.disable_symlinks = clcf->disable_symlinks;\n#endif\n if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)\n != NGX_OK)\n {\n if (of.err != NGX_ENOENT\n && of.err != NGX_ENOTDIR\n && of.err != NGX_ENAMETOOLONG)\n {\n ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,\n "%s \\"%s\\" failed", of.failed, path.data);\n }\n continue;\n }\n if (of.is_dir && !test_dir) {\n continue;\n }\n path.len -= root;\n path.data += root;\n if (!alias) {\n r->uri = path;\n#if (NGX_PCRE)\n } else if (clcf->regex) {\n if (!test_dir) {\n r->uri = path;\n r->add_uri_to_alias = 1;\n }\n#endif\n } else {\n r->uri.len = alias + path.len;\n r->uri.data = ngx_pnalloc(r->pool, r->uri.len);\n if (r->uri.data == NULL) {\n ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_OK;\n }\n p = ngx_copy(r->uri.data, clcf->name.data, alias);\n ngx_memcpy(p, name, path.len);\n }\n ngx_http_set_exten(r);\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "try file uri: \\"%V\\"", &r->uri);\n r->phase_handler++;\n return NGX_AGAIN;\n }\n}', 'ngx_int_t\nngx_open_cached_file(ngx_open_file_cache_t *cache, ngx_str_t *name,\n ngx_open_file_info_t *of, ngx_pool_t *pool)\n{\n time_t now;\n uint32_t hash;\n ngx_int_t rc;\n ngx_file_info_t fi;\n ngx_pool_cleanup_t *cln;\n ngx_cached_open_file_t *file;\n ngx_pool_cleanup_file_t *clnf;\n ngx_open_file_cache_cleanup_t *ofcln;\n of->fd = NGX_INVALID_FILE;\n of->err = 0;\n if (cache == NULL) {\n if (of->test_only) {\n if (ngx_file_info_wrapper(name, of, &fi) == NGX_FILE_ERROR) {\n return NGX_ERROR;\n }\n of->uniq = ngx_file_uniq(&fi);\n of->mtime = ngx_file_mtime(&fi);\n of->size = ngx_file_size(&fi);\n of->fs_size = ngx_file_fs_size(&fi);\n of->is_dir = ngx_is_dir(&fi);\n of->is_file = ngx_is_file(&fi);\n of->is_link = ngx_is_link(&fi);\n of->is_exec = ngx_is_exec(&fi);\n return NGX_OK;\n }\n cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));\n if (cln == NULL) {\n return NGX_ERROR;\n }\n rc = ngx_open_and_stat_file(name, of, pool->log);\n if (rc == NGX_OK && !of->is_dir) {\n cln->handler = ngx_pool_cleanup_file;\n clnf = cln->data;\n clnf->fd = of->fd;\n clnf->name = name->data;\n clnf->log = pool->log;\n }\n return rc;\n }\n cln = ngx_pool_cleanup_add(pool, sizeof(ngx_open_file_cache_cleanup_t));\n if (cln == NULL) {\n return NGX_ERROR;\n }\n now = ngx_time();\n hash = ngx_crc32_long(name->data, name->len);\n file = ngx_open_file_lookup(cache, name, hash);\n if (file) {\n file->uses++;\n ngx_queue_remove(&file->queue);\n if (file->fd == NGX_INVALID_FILE && file->err == 0 && !file->is_dir) {\n rc = ngx_open_and_stat_file(name, of, pool->log);\n if (rc != NGX_OK && (of->err == 0 || !of->errors)) {\n goto failed;\n }\n goto add_event;\n }\n if (file->use_event\n || (file->event == NULL\n && (of->uniq == 0 || of->uniq == file->uniq)\n && now - file->created < of->valid\n#if (NGX_HAVE_OPENAT)\n && of->disable_symlinks == file->disable_symlinks\n#endif\n ))\n {\n if (file->err == 0) {\n of->fd = file->fd;\n of->uniq = file->uniq;\n of->mtime = file->mtime;\n of->size = file->size;\n of->is_dir = file->is_dir;\n of->is_file = file->is_file;\n of->is_link = file->is_link;\n of->is_exec = file->is_exec;\n of->is_directio = file->is_directio;\n if (!file->is_dir) {\n file->count++;\n ngx_open_file_add_event(cache, file, of, pool->log);\n }\n } else {\n of->err = file->err;\n#if (NGX_HAVE_OPENAT)\n of->failed = file->disable_symlinks ? ngx_openat_file_n\n : ngx_open_file_n;\n#else\n of->failed = ngx_open_file_n;\n#endif\n }\n goto found;\n }\n ngx_log_debug4(NGX_LOG_DEBUG_CORE, pool->log, 0,\n "retest open file: %s, fd:%d, c:%d, e:%d",\n file->name, file->fd, file->count, file->err);\n if (file->is_dir) {\n of->test_dir = 1;\n }\n of->fd = file->fd;\n of->uniq = file->uniq;\n rc = ngx_open_and_stat_file(name, of, pool->log);\n if (rc != NGX_OK && (of->err == 0 || !of->errors)) {\n goto failed;\n }\n if (of->is_dir) {\n if (file->is_dir || file->err) {\n goto update;\n }\n } else if (of->err == 0) {\n if (file->is_dir || file->err) {\n goto add_event;\n }\n if (of->uniq == file->uniq) {\n if (file->event) {\n file->use_event = 1;\n }\n of->is_directio = file->is_directio;\n goto update;\n }\n } else {\n if (file->err || file->is_dir) {\n goto update;\n }\n }\n if (file->count == 0) {\n ngx_open_file_del_event(file);\n if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {\n ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,\n ngx_close_file_n " \\"%V\\" failed", name);\n }\n goto add_event;\n }\n ngx_rbtree_delete(&cache->rbtree, &file->node);\n cache->current--;\n file->close = 1;\n goto create;\n }\n rc = ngx_open_and_stat_file(name, of, pool->log);\n if (rc != NGX_OK && (of->err == 0 || !of->errors)) {\n goto failed;\n }\ncreate:\n if (cache->current >= cache->max) {\n ngx_expire_old_cached_files(cache, 0, pool->log);\n }\n file = ngx_alloc(sizeof(ngx_cached_open_file_t), pool->log);\n if (file == NULL) {\n goto failed;\n }\n file->name = ngx_alloc(name->len + 1, pool->log);\n if (file->name == NULL) {\n ngx_free(file);\n file = NULL;\n goto failed;\n }\n ngx_cpystrn(file->name, name->data, name->len + 1);\n file->node.key = hash;\n ngx_rbtree_insert(&cache->rbtree, &file->node);\n cache->current++;\n file->uses = 1;\n file->count = 0;\n file->use_event = 0;\n file->event = NULL;\nadd_event:\n ngx_open_file_add_event(cache, file, of, pool->log);\nupdate:\n file->fd = of->fd;\n file->err = of->err;\n#if (NGX_HAVE_OPENAT)\n file->disable_symlinks = of->disable_symlinks;\n#endif\n if (of->err == 0) {\n file->uniq = of->uniq;\n file->mtime = of->mtime;\n file->size = of->size;\n file->close = 0;\n file->is_dir = of->is_dir;\n file->is_file = of->is_file;\n file->is_link = of->is_link;\n file->is_exec = of->is_exec;\n file->is_directio = of->is_directio;\n if (!of->is_dir) {\n file->count++;\n }\n }\n file->created = now;\nfound:\n file->accessed = now;\n ngx_queue_insert_head(&cache->expire_queue, &file->queue);\n ngx_log_debug5(NGX_LOG_DEBUG_CORE, pool->log, 0,\n "cached open file: %s, fd:%d, c:%d, e:%d, u:%d",\n file->name, file->fd, file->count, file->err, file->uses);\n if (of->err == 0) {\n if (!of->is_dir) {\n cln->handler = ngx_open_file_cleanup;\n ofcln = cln->data;\n ofcln->cache = cache;\n ofcln->file = file;\n ofcln->min_uses = of->min_uses;\n ofcln->log = pool->log;\n }\n return NGX_OK;\n }\n return NGX_ERROR;\nfailed:\n if (file) {\n ngx_rbtree_delete(&cache->rbtree, &file->node);\n cache->current--;\n if (file->count == 0) {\n if (file->fd != NGX_INVALID_FILE) {\n if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {\n ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,\n ngx_close_file_n " \\"%s\\" failed",\n file->name);\n }\n }\n ngx_free(file->name);\n ngx_free(file);\n } else {\n file->close = 1;\n }\n }\n if (of->fd != NGX_INVALID_FILE) {\n if (ngx_close_file(of->fd) == NGX_FILE_ERROR) {\n ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,\n ngx_close_file_n " \\"%V\\" failed", name);\n }\n }\n return NGX_ERROR;\n}', 'static ngx_int_t\nngx_file_info_wrapper(ngx_str_t *name, ngx_open_file_info_t *of,\n ngx_file_info_t *fi)\n{\n ngx_int_t rc;\n#if !(NGX_HAVE_OPENAT)\n rc = ngx_file_info(name->data, fi);\n if (rc == NGX_FILE_ERROR) {\n of->err = ngx_errno;\n of->failed = ngx_file_info_n;\n return NGX_FILE_ERROR;\n }\n return rc;\n#else\n ngx_fd_t fd;\n if (of->disable_symlinks == NGX_DISABLE_SYMLINKS_OFF) {\n rc = ngx_file_info(name->data, fi);\n if (rc == NGX_FILE_ERROR) {\n of->err = ngx_errno;\n of->failed = ngx_file_info_n;\n return NGX_FILE_ERROR;\n }\n return rc;\n }\n fd = ngx_open_file_wrapper(name, of, NGX_FILE_RDONLY|NGX_FILE_NONBLOCK,\n NGX_FILE_OPEN, 0);\n if (fd == NGX_FILE_ERROR) {\n return NGX_FILE_ERROR;\n }\n if (ngx_fd_info(fd, fi) == NGX_FILE_ERROR) {\n of->err = ngx_errno;\n of->failed = ngx_fd_info_n;\n ngx_close_file(fd);\n return NGX_FILE_ERROR;\n }\n ngx_close_file(fd);\n return NGX_OK;\n#endif\n}', 'static ngx_fd_t\nngx_open_file_wrapper(ngx_str_t *name, ngx_open_file_info_t *of,\n ngx_int_t mode, ngx_int_t create, ngx_int_t access)\n{\n ngx_fd_t fd;\n#if !(NGX_HAVE_OPENAT)\n fd = ngx_open_file(name->data, mode, create, access);\n if (fd == NGX_FILE_ERROR) {\n of->err = ngx_errno;\n of->failed = ngx_open_file_n;\n return NGX_FILE_ERROR;\n }\n return fd;\n#else\n u_char *p, *cp, *end;\n ngx_fd_t at_fd;\n if (of->disable_symlinks == NGX_DISABLE_SYMLINKS_OFF) {\n fd = ngx_open_file(name->data, mode, create, access);\n if (fd == NGX_FILE_ERROR) {\n of->err = ngx_errno;\n of->failed = ngx_open_file_n;\n return NGX_FILE_ERROR;\n }\n return fd;\n }\n at_fd = ngx_openat_file(AT_FDCWD, "/", NGX_FILE_RDONLY|NGX_FILE_NONBLOCK,\n NGX_FILE_OPEN, 0);\n if (at_fd == NGX_FILE_ERROR) {\n of->err = ngx_errno;\n of->failed = ngx_openat_file_n;\n return NGX_FILE_ERROR;\n }\n end = name->data + name->len;\n p = name->data + 1;\n for ( ;; ) {\n cp = ngx_strlchr(p, end, \'/\');\n if (cp == NULL) {\n break;\n }\n *cp = \'\\0\';\n if (of->disable_symlinks == NGX_DISABLE_SYMLINKS_NOTOWNER) {\n fd = ngx_openat_file_owner(at_fd, p,\n NGX_FILE_RDONLY|NGX_FILE_NONBLOCK,\n NGX_FILE_OPEN, 0);\n } else {\n fd = ngx_openat_file(at_fd, p,\n NGX_FILE_RDONLY|NGX_FILE_NONBLOCK|NGX_FILE_NOFOLLOW,\n NGX_FILE_OPEN, 0);\n }\n *cp = \'/\';\n ngx_close_file(at_fd);\n if (fd == NGX_FILE_ERROR) {\n of->err = ngx_errno;\n of->failed = ngx_openat_file_n;\n return NGX_FILE_ERROR;\n }\n p = cp + 1;\n at_fd = fd;\n }\n if (of->disable_symlinks == NGX_DISABLE_SYMLINKS_NOTOWNER) {\n fd = ngx_openat_file_owner(at_fd, p, mode, create, access);\n } else {\n fd = ngx_openat_file(at_fd, p, mode|NGX_FILE_NOFOLLOW, create, access);\n }\n if (fd == NGX_FILE_ERROR) {\n of->err = ngx_errno;\n of->failed = ngx_openat_file_n;\n }\n ngx_close_file(at_fd);\n return fd;\n#endif\n}', 'static ngx_inline u_char *\nngx_strlchr(u_char *p, u_char *last, u_char c)\n{\n while (p < last) {\n if (*p == c) {\n return p;\n }\n p++;\n }\n return NULL;\n}']
35,724
1
https://github.com/openssl/openssl/blob/4af6e2432bf7beded7f219c2aae1495b125d5686/crypto/x509/x509_vfy.c/#L441
static int internal_verify(X509_STORE_CTX *ctx) { int i,ok=0,n; X509 *xs,*xi; EVP_PKEY *pkey=NULL; int (*cb)(); cb=ctx->verify_cb; if (cb == NULL) cb=null_callback; n=sk_X509_num(ctx->chain); ctx->error_depth=n-1; n--; xi=sk_X509_value(ctx->chain,n); if (X509_NAME_cmp(X509_get_subject_name(xi), X509_get_issuer_name(xi)) == 0) xs=xi; else { if (n <= 0) { ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE; ctx->current_cert=xi; ok=cb(0,ctx); goto end; } else { n--; ctx->error_depth=n; xs=sk_X509_value(ctx->chain,n); } } while (n >= 0) { ctx->error_depth=n; if (!xs->valid) { if ((pkey=X509_get_pubkey(xi)) == NULL) { ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; ctx->current_cert=xi; ok=(*cb)(0,ctx); if (!ok) goto end; } if (X509_verify(xs,pkey) <= 0) { ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE; ctx->current_cert=xs; ok=(*cb)(0,ctx); if (!ok) { EVP_PKEY_free(pkey); goto end; } } EVP_PKEY_free(pkey); pkey=NULL; i=X509_cmp_current_time(X509_get_notBefore(xs)); if (i == 0) { ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD; ctx->current_cert=xs; ok=(*cb)(0,ctx); if (!ok) goto end; } if (i > 0) { ctx->error=X509_V_ERR_CERT_NOT_YET_VALID; ctx->current_cert=xs; ok=(*cb)(0,ctx); if (!ok) goto end; } xs->valid=1; } i=X509_cmp_current_time(X509_get_notAfter(xs)); if (i == 0) { ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD; ctx->current_cert=xs; ok=(*cb)(0,ctx); if (!ok) goto end; } if (i < 0) { ctx->error=X509_V_ERR_CERT_HAS_EXPIRED; ctx->current_cert=xs; ok=(*cb)(0,ctx); if (!ok) goto end; } ctx->current_cert=xs; ok=(*cb)(1,ctx); if (!ok) goto end; n--; if (n >= 0) { xi=xs; xs=sk_X509_value(ctx->chain,n); } } ok=1; end: return(ok); }
['static int internal_verify(X509_STORE_CTX *ctx)\n\t{\n\tint i,ok=0,n;\n\tX509 *xs,*xi;\n\tEVP_PKEY *pkey=NULL;\n\tint (*cb)();\n\tcb=ctx->verify_cb;\n\tif (cb == NULL) cb=null_callback;\n\tn=sk_X509_num(ctx->chain);\n\tctx->error_depth=n-1;\n\tn--;\n\txi=sk_X509_value(ctx->chain,n);\n\tif (X509_NAME_cmp(X509_get_subject_name(xi),\n\t\tX509_get_issuer_name(xi)) == 0)\n\t\txs=xi;\n\telse\n\t\t{\n\t\tif (n <= 0)\n\t\t\t{\n\t\t\tctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;\n\t\t\tctx->current_cert=xi;\n\t\t\tok=cb(0,ctx);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tn--;\n\t\t\tctx->error_depth=n;\n\t\t\txs=sk_X509_value(ctx->chain,n);\n\t\t\t}\n\t\t}\n\twhile (n >= 0)\n\t\t{\n\t\tctx->error_depth=n;\n\t\tif (!xs->valid)\n\t\t\t{\n\t\t\tif ((pkey=X509_get_pubkey(xi)) == NULL)\n\t\t\t\t{\n\t\t\t\tctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;\n\t\t\t\tctx->current_cert=xi;\n\t\t\t\tok=(*cb)(0,ctx);\n\t\t\t\tif (!ok) goto end;\n\t\t\t\t}\n\t\t\tif (X509_verify(xs,pkey) <= 0)\n\t\t\t\t{\n\t\t\t\tctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE;\n\t\t\t\tctx->current_cert=xs;\n\t\t\t\tok=(*cb)(0,ctx);\n\t\t\t\tif (!ok)\n\t\t\t\t\t{\n\t\t\t\t\tEVP_PKEY_free(pkey);\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\tEVP_PKEY_free(pkey);\n\t\t\tpkey=NULL;\n\t\t\ti=X509_cmp_current_time(X509_get_notBefore(xs));\n\t\t\tif (i == 0)\n\t\t\t\t{\n\t\t\t\tctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;\n\t\t\t\tctx->current_cert=xs;\n\t\t\t\tok=(*cb)(0,ctx);\n\t\t\t\tif (!ok) goto end;\n\t\t\t\t}\n\t\t\tif (i > 0)\n\t\t\t\t{\n\t\t\t\tctx->error=X509_V_ERR_CERT_NOT_YET_VALID;\n\t\t\t\tctx->current_cert=xs;\n\t\t\t\tok=(*cb)(0,ctx);\n\t\t\t\tif (!ok) goto end;\n\t\t\t\t}\n\t\t\txs->valid=1;\n\t\t\t}\n\t\ti=X509_cmp_current_time(X509_get_notAfter(xs));\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;\n\t\t\tctx->current_cert=xs;\n\t\t\tok=(*cb)(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\tif (i < 0)\n\t\t\t{\n\t\t\tctx->error=X509_V_ERR_CERT_HAS_EXPIRED;\n\t\t\tctx->current_cert=xs;\n\t\t\tok=(*cb)(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\tctx->current_cert=xs;\n\t\tok=(*cb)(1,ctx);\n\t\tif (!ok) goto end;\n\t\tn--;\n\t\tif (n >= 0)\n\t\t\t{\n\t\t\txi=xs;\n\t\t\txs=sk_X509_value(ctx->chain,n);\n\t\t\t}\n\t\t}\n\tok=1;\nend:\n\treturn(ok);\n\t}', 'int sk_num(const STACK *st)\n{\n\tif(st == NULL) return -1;\n\treturn st->num;\n}', 'char *sk_value(const STACK *st, int i)\n{\n\tif(st == NULL) return NULL;\n\treturn st->data[i];\n}', 'X509_NAME *X509_get_subject_name(X509 *a)\n\t{\n\treturn(a->cert_info->subject);\n\t}']
35,725
0
https://github.com/libav/libav/blob/28240a60c1b5ce276e947ba013271ec009adc078/libavcodec/ansi.c/#L229
static int execute_code(AVCodecContext * avctx, int c) { AnsiContext *s = avctx->priv_data; int ret, i, width, height; switch(c) { case 'A': s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0); break; case 'B': s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height); break; case 'C': s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH); break; case 'D': s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0); break; case 'H': case 'f': s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0; s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0; break; case 'h': case 'l': if (s->nb_args < 2) s->args[0] = DEFAULT_SCREEN_MODE; switch(s->args[0]) { case 0: case 1: case 4: case 5: case 13: case 19: s->font = ff_cga_font; s->font_height = 8; width = 40<<3; height = 25<<3; break; case 2: case 3: s->font = ff_vga16_font; s->font_height = 16; width = 80<<3; height = 25<<4; break; case 6: case 14: s->font = ff_cga_font; s->font_height = 8; width = 80<<3; height = 25<<3; break; case 7: break; case 15: case 16: s->font = ff_cga_font; s->font_height = 8; width = 80<<3; height = 43<<3; break; case 17: case 18: s->font = ff_cga_font; s->font_height = 8; width = 80<<3; height = 60<<4; break; default: avpriv_request_sample(avctx, "Unsupported screen mode"); } if (width != avctx->width || height != avctx->height) { av_frame_unref(s->frame); ret = ff_set_dimensions(avctx, width, height); if (ret < 0) return ret; ret = ff_get_buffer(avctx, s->frame, AV_GET_BUFFER_FLAG_REF); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } s->frame->pict_type = AV_PICTURE_TYPE_I; s->frame->palette_has_changed = 1; memcpy(s->frame->data[1], ff_cga_palette, 16 * 4); erase_screen(avctx); } else if (c == 'l') { erase_screen(avctx); } break; case 'J': switch (s->args[0]) { case 0: erase_line(avctx, s->x, avctx->width - s->x); if (s->y < avctx->height - s->font_height) memset(s->frame->data[0] + (s->y + s->font_height)*s->frame->linesize[0], DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame->linesize[0]); break; case 1: erase_line(avctx, 0, s->x); if (s->y > 0) memset(s->frame->data[0], DEFAULT_BG_COLOR, s->y * s->frame->linesize[0]); break; case 2: erase_screen(avctx); } break; case 'K': switch(s->args[0]) { case 0: erase_line(avctx, s->x, avctx->width - s->x); break; case 1: erase_line(avctx, 0, s->x); break; case 2: erase_line(avctx, 0, avctx->width); } break; case 'm': if (s->nb_args == 0) { s->nb_args = 1; s->args[0] = 0; } for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) { int m = s->args[i]; if (m == 0) { s->attributes = 0; s->fg = DEFAULT_FG_COLOR; s->bg = DEFAULT_BG_COLOR; } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) { s->attributes |= 1 << (m - 1); } else if (m >= 30 && m <= 38) { s->fg = ansi_to_cga[m - 30]; } else if (m == 39) { s->fg = ansi_to_cga[DEFAULT_FG_COLOR]; } else if (m >= 40 && m <= 47) { s->bg = ansi_to_cga[m - 40]; } else if (m == 49) { s->fg = ansi_to_cga[DEFAULT_BG_COLOR]; } else { avpriv_request_sample(avctx, "Unsupported rendition parameter"); } } break; case 'n': case 'R': break; case 's': s->sx = s->x; s->sy = s->y; break; case 'u': s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH); s->y = av_clip(s->sy, 0, avctx->height - s->font_height); break; default: avpriv_request_sample(avctx, "Unknown escape code"); break; } return 0; }
['static int execute_code(AVCodecContext * avctx, int c)\n{\n AnsiContext *s = avctx->priv_data;\n int ret, i, width, height;\n switch(c) {\n case \'A\':\n s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);\n break;\n case \'B\':\n s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);\n break;\n case \'C\':\n s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);\n break;\n case \'D\':\n s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);\n break;\n case \'H\':\n case \'f\':\n s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;\n s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;\n break;\n case \'h\':\n case \'l\':\n if (s->nb_args < 2)\n s->args[0] = DEFAULT_SCREEN_MODE;\n switch(s->args[0]) {\n case 0: case 1: case 4: case 5: case 13: case 19:\n s->font = ff_cga_font;\n s->font_height = 8;\n width = 40<<3;\n height = 25<<3;\n break;\n case 2: case 3:\n s->font = ff_vga16_font;\n s->font_height = 16;\n width = 80<<3;\n height = 25<<4;\n break;\n case 6: case 14:\n s->font = ff_cga_font;\n s->font_height = 8;\n width = 80<<3;\n height = 25<<3;\n break;\n case 7:\n break;\n case 15: case 16:\n s->font = ff_cga_font;\n s->font_height = 8;\n width = 80<<3;\n height = 43<<3;\n break;\n case 17: case 18:\n s->font = ff_cga_font;\n s->font_height = 8;\n width = 80<<3;\n height = 60<<4;\n break;\n default:\n avpriv_request_sample(avctx, "Unsupported screen mode");\n }\n if (width != avctx->width || height != avctx->height) {\n av_frame_unref(s->frame);\n ret = ff_set_dimensions(avctx, width, height);\n if (ret < 0)\n return ret;\n ret = ff_get_buffer(avctx, s->frame, AV_GET_BUFFER_FLAG_REF);\n if (ret < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n s->frame->pict_type = AV_PICTURE_TYPE_I;\n s->frame->palette_has_changed = 1;\n memcpy(s->frame->data[1], ff_cga_palette, 16 * 4);\n erase_screen(avctx);\n } else if (c == \'l\') {\n erase_screen(avctx);\n }\n break;\n case \'J\':\n switch (s->args[0]) {\n case 0:\n erase_line(avctx, s->x, avctx->width - s->x);\n if (s->y < avctx->height - s->font_height)\n memset(s->frame->data[0] + (s->y + s->font_height)*s->frame->linesize[0],\n DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame->linesize[0]);\n break;\n case 1:\n erase_line(avctx, 0, s->x);\n if (s->y > 0)\n memset(s->frame->data[0], DEFAULT_BG_COLOR, s->y * s->frame->linesize[0]);\n break;\n case 2:\n erase_screen(avctx);\n }\n break;\n case \'K\':\n switch(s->args[0]) {\n case 0:\n erase_line(avctx, s->x, avctx->width - s->x);\n break;\n case 1:\n erase_line(avctx, 0, s->x);\n break;\n case 2:\n erase_line(avctx, 0, avctx->width);\n }\n break;\n case \'m\':\n if (s->nb_args == 0) {\n s->nb_args = 1;\n s->args[0] = 0;\n }\n for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {\n int m = s->args[i];\n if (m == 0) {\n s->attributes = 0;\n s->fg = DEFAULT_FG_COLOR;\n s->bg = DEFAULT_BG_COLOR;\n } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {\n s->attributes |= 1 << (m - 1);\n } else if (m >= 30 && m <= 38) {\n s->fg = ansi_to_cga[m - 30];\n } else if (m == 39) {\n s->fg = ansi_to_cga[DEFAULT_FG_COLOR];\n } else if (m >= 40 && m <= 47) {\n s->bg = ansi_to_cga[m - 40];\n } else if (m == 49) {\n s->fg = ansi_to_cga[DEFAULT_BG_COLOR];\n } else {\n avpriv_request_sample(avctx, "Unsupported rendition parameter");\n }\n }\n break;\n case \'n\':\n case \'R\':\n break;\n case \'s\':\n s->sx = s->x;\n s->sy = s->y;\n break;\n case \'u\':\n s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);\n s->y = av_clip(s->sy, 0, avctx->height - s->font_height);\n break;\n default:\n avpriv_request_sample(avctx, "Unknown escape code");\n break;\n }\n return 0;\n}']
35,726
0
https://github.com/openssl/openssl/blob/25670f3e87d3a9e7ea8ffb2b717a288e2b3024f5/apps/apps.c/#L1845
int pkey_ctrl_string(EVP_PKEY_CTX *ctx, const char *value) { int rv; char *stmp, *vtmp = NULL; stmp = OPENSSL_strdup(value); if (!stmp) return -1; vtmp = strchr(stmp, ':'); if (vtmp) { *vtmp = 0; vtmp++; } rv = EVP_PKEY_CTX_ctrl_str(ctx, stmp, vtmp); OPENSSL_free(stmp); return rv; }
["int pkey_ctrl_string(EVP_PKEY_CTX *ctx, const char *value)\n{\n int rv;\n char *stmp, *vtmp = NULL;\n stmp = OPENSSL_strdup(value);\n if (!stmp)\n return -1;\n vtmp = strchr(stmp, ':');\n if (vtmp) {\n *vtmp = 0;\n vtmp++;\n }\n rv = EVP_PKEY_CTX_ctrl_str(ctx, stmp, vtmp);\n OPENSSL_free(stmp);\n return rv;\n}", 'char *CRYPTO_strdup(const char *str, const char* file, int line)\n{\n char *ret;\n size_t size;\n if (str == NULL)\n return NULL;\n size = strlen(str) + 1;\n ret = CRYPTO_malloc(size, file, line);\n if (ret != NULL)\n memcpy(ret, str, size);\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}', 'int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,\n const char *name, const char *value)\n{\n if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {\n EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);\n return -2;\n }\n if (strcmp(name, "digest") == 0) {\n const EVP_MD *md;\n if (value == NULL || (md = EVP_get_digestbyname(value)) == NULL) {\n EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_INVALID_DIGEST);\n return 0;\n }\n return EVP_PKEY_CTX_set_signature_md(ctx, md);\n }\n return ctx->pmeth->ctrl_str(ctx, name, value);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\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}']
35,727
0
https://github.com/nginx/nginx/blob/4fe0a09942f8aed90f84c77969847980e9aadd98/src/core/ngx_string.c/#L922
ngx_int_t ngx_atoi(u_char *line, size_t n) { ngx_int_t value, cutoff, cutlim; if (n == 0) { return NGX_ERROR; } cutoff = NGX_MAX_INT_T_VALUE / 10; cutlim = NGX_MAX_INT_T_VALUE % 10; for (value = 0; n--; line++) { if (*line < '0' || *line > '9') { return NGX_ERROR; } if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) { return NGX_ERROR; } value = value * 10 + (*line - '0'); } return value; }
['static char *\nngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)\n{\n char *rv;\n ngx_str_t *value;\n ngx_cidr_t cidr;\n ngx_http_geo_conf_ctx_t *ctx;\n ctx = cf->ctx;\n value = cf->args->elts;\n if (cf->args->nelts == 1) {\n if (ngx_strcmp(value[0].data, "ranges") == 0) {\n if (ctx->tree\n#if (NGX_HAVE_INET6)\n || ctx->tree6\n#endif\n )\n {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "the \\"ranges\\" directive must be "\n "the first directive inside \\"geo\\" block");\n goto failed;\n }\n ctx->ranges = 1;\n rv = NGX_CONF_OK;\n goto done;\n }\n else if (ngx_strcmp(value[0].data, "proxy_recursive") == 0) {\n ctx->proxy_recursive = 1;\n rv = NGX_CONF_OK;\n goto done;\n }\n }\n if (cf->args->nelts != 2) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid number of the geo parameters");\n goto failed;\n }\n if (ngx_strcmp(value[0].data, "include") == 0) {\n rv = ngx_http_geo_include(cf, ctx, &value[1]);\n goto done;\n } else if (ngx_strcmp(value[0].data, "proxy") == 0) {\n if (ngx_http_geo_cidr_value(cf, &value[1], &cidr) != NGX_OK) {\n goto failed;\n }\n rv = ngx_http_geo_add_proxy(cf, ctx, &cidr);\n goto done;\n }\n if (ctx->ranges) {\n rv = ngx_http_geo_range(cf, ctx, value);\n } else {\n rv = ngx_http_geo_cidr(cf, ctx, value);\n }\ndone:\n ngx_reset_pool(cf->pool);\n return rv;\nfailed:\n ngx_reset_pool(cf->pool);\n return NGX_CONF_ERROR;\n}', 'static ngx_int_t\nngx_http_geo_cidr_value(ngx_conf_t *cf, ngx_str_t *net, ngx_cidr_t *cidr)\n{\n ngx_int_t rc;\n if (ngx_strcmp(net->data, "255.255.255.255") == 0) {\n cidr->family = AF_INET;\n cidr->u.in.addr = 0xffffffff;\n cidr->u.in.mask = 0xffffffff;\n return NGX_OK;\n }\n rc = ngx_ptocidr(net, cidr);\n if (rc == NGX_ERROR) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid network \\"%V\\"", net);\n return NGX_ERROR;\n }\n if (rc == NGX_DONE) {\n ngx_conf_log_error(NGX_LOG_WARN, cf, 0,\n "low address bits of %V are meaningless", net);\n }\n return NGX_OK;\n}', "ngx_int_t\nngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr)\n{\n u_char *addr, *mask, *last;\n size_t len;\n ngx_int_t shift;\n#if (NGX_HAVE_INET6)\n ngx_int_t rc;\n ngx_uint_t s, i;\n#endif\n addr = text->data;\n last = addr + text->len;\n mask = ngx_strlchr(addr, last, '/');\n len = (mask ? mask : last) - addr;\n cidr->u.in.addr = ngx_inet_addr(addr, len);\n if (cidr->u.in.addr != INADDR_NONE) {\n cidr->family = AF_INET;\n if (mask == NULL) {\n cidr->u.in.mask = 0xffffffff;\n return NGX_OK;\n }\n#if (NGX_HAVE_INET6)\n } else if (ngx_inet6_addr(addr, len, cidr->u.in6.addr.s6_addr) == NGX_OK) {\n cidr->family = AF_INET6;\n if (mask == NULL) {\n ngx_memset(cidr->u.in6.mask.s6_addr, 0xff, 16);\n return NGX_OK;\n }\n#endif\n } else {\n return NGX_ERROR;\n }\n mask++;\n shift = ngx_atoi(mask, last - mask);\n if (shift == NGX_ERROR) {\n return NGX_ERROR;\n }\n switch (cidr->family) {\n#if (NGX_HAVE_INET6)\n case AF_INET6:\n if (shift > 128) {\n return NGX_ERROR;\n }\n addr = cidr->u.in6.addr.s6_addr;\n mask = cidr->u.in6.mask.s6_addr;\n rc = NGX_OK;\n for (i = 0; i < 16; i++) {\n s = (shift > 8) ? 8 : shift;\n shift -= s;\n mask[i] = (u_char) (0xffu << (8 - s));\n if (addr[i] != (addr[i] & mask[i])) {\n rc = NGX_DONE;\n addr[i] &= mask[i];\n }\n }\n return rc;\n#endif\n default:\n if (shift > 32) {\n return NGX_ERROR;\n }\n if (shift) {\n cidr->u.in.mask = htonl((uint32_t) (0xffffffffu << (32 - shift)));\n } else {\n cidr->u.in.mask = 0;\n }\n if (cidr->u.in.addr == (cidr->u.in.addr & cidr->u.in.mask)) {\n return NGX_OK;\n }\n cidr->u.in.addr &= cidr->u.in.mask;\n return NGX_DONE;\n }\n}", 'static ngx_inline u_char *\nngx_strlchr(u_char *p, u_char *last, u_char c)\n{\n while (p < last) {\n if (*p == c) {\n return p;\n }\n p++;\n }\n return NULL;\n}', "ngx_int_t\nngx_atoi(u_char *line, size_t n)\n{\n ngx_int_t value, cutoff, cutlim;\n if (n == 0) {\n return NGX_ERROR;\n }\n cutoff = NGX_MAX_INT_T_VALUE / 10;\n cutlim = NGX_MAX_INT_T_VALUE % 10;\n for (value = 0; n--; line++) {\n if (*line < '0' || *line > '9') {\n return NGX_ERROR;\n }\n if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {\n return NGX_ERROR;\n }\n value = value * 10 + (*line - '0');\n }\n return value;\n}"]
35,728
0
https://github.com/libav/libav/blob/6482bd8831904ab7cd0a126687f871ff9c52b055/libavcodec/alac.c/#L378
static int alac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { ALACContext *alac = avctx->priv_data; int channels; int hassize; unsigned int readsamplesize; int is_compressed; uint8_t interlacing_shift; uint8_t interlacing_leftweight; int i, ch, ret; init_get_bits(&alac->gb, avpkt->data, avpkt->size * 8); channels = get_bits(&alac->gb, 3) + 1; if (channels != avctx->channels) { av_log(avctx, AV_LOG_ERROR, "frame header channel count mismatch\n"); return AVERROR_INVALIDDATA; } skip_bits(&alac->gb, 4); skip_bits(&alac->gb, 12); hassize = get_bits1(&alac->gb); alac->extra_bits = get_bits(&alac->gb, 2) << 3; is_compressed = !get_bits1(&alac->gb); if (hassize) { uint32_t output_samples = get_bits_long(&alac->gb, 32); if (!output_samples || output_samples > alac->max_samples_per_frame) { av_log(avctx, AV_LOG_ERROR, "invalid samples per frame: %d\n", output_samples); return AVERROR_INVALIDDATA; } alac->nb_samples = output_samples; } else alac->nb_samples = alac->max_samples_per_frame; alac->frame.nb_samples = alac->nb_samples; if ((ret = avctx->get_buffer(avctx, &alac->frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } readsamplesize = alac->sample_size - alac->extra_bits + channels - 1; if (readsamplesize > MIN_CACHE_BITS) { av_log(avctx, AV_LOG_ERROR, "readsamplesize too big (%d)\n", readsamplesize); return -1; } if (is_compressed) { int16_t predictor_coef_table[MAX_CHANNELS][32]; int predictor_coef_num[MAX_CHANNELS]; int prediction_type[MAX_CHANNELS]; int prediction_quantitization[MAX_CHANNELS]; int ricemodifier[MAX_CHANNELS]; interlacing_shift = get_bits(&alac->gb, 8); interlacing_leftweight = get_bits(&alac->gb, 8); for (ch = 0; ch < channels; ch++) { prediction_type[ch] = get_bits(&alac->gb, 4); prediction_quantitization[ch] = get_bits(&alac->gb, 4); ricemodifier[ch] = get_bits(&alac->gb, 3); predictor_coef_num[ch] = get_bits(&alac->gb, 5); for (i = 0; i < predictor_coef_num[ch]; i++) predictor_coef_table[ch][i] = get_sbits(&alac->gb, 16); } if (alac->extra_bits) { for (i = 0; i < alac->nb_samples; i++) { for (ch = 0; ch < channels; ch++) alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits); } } for (ch = 0; ch < channels; ch++) { bastardized_rice_decompress(alac, alac->predict_error_buffer[ch], alac->nb_samples, readsamplesize, ricemodifier[ch] * alac->rice_history_mult / 4); if (prediction_type[ch] == 15) { predictor_decompress_fir_adapt(alac->predict_error_buffer[ch], alac->predict_error_buffer[ch], alac->nb_samples, readsamplesize, NULL, 31, 0); } else if (prediction_type[ch] > 0) { av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n", prediction_type[ch]); } predictor_decompress_fir_adapt(alac->predict_error_buffer[ch], alac->output_samples_buffer[ch], alac->nb_samples, readsamplesize, predictor_coef_table[ch], predictor_coef_num[ch], prediction_quantitization[ch]); } } else { for (i = 0; i < alac->nb_samples; i++) { for (ch = 0; ch < channels; ch++) { alac->output_samples_buffer[ch][i] = get_sbits_long(&alac->gb, alac->sample_size); } } alac->extra_bits = 0; interlacing_shift = 0; interlacing_leftweight = 0; } if (get_bits(&alac->gb, 3) != 7) av_log(avctx, AV_LOG_ERROR, "Error : Wrong End Of Frame\n"); if (channels == 2 && interlacing_leftweight) { decorrelate_stereo(alac->output_samples_buffer, alac->nb_samples, interlacing_shift, interlacing_leftweight); } if (alac->extra_bits) { append_extra_bits(alac->output_samples_buffer, alac->extra_bits_buffer, alac->extra_bits, alac->channels, alac->nb_samples); } switch(alac->sample_size) { case 16: { int16_t *outbuffer = (int16_t *)alac->frame.data[0]; for (i = 0; i < alac->nb_samples; i++) { *outbuffer++ = alac->output_samples_buffer[0][i]; if (channels == 2) *outbuffer++ = alac->output_samples_buffer[1][i]; }} break; case 24: { int32_t *outbuffer = (int32_t *)alac->frame.data[0]; for (i = 0; i < alac->nb_samples; i++) { *outbuffer++ = alac->output_samples_buffer[0][i] << 8; if (channels == 2) *outbuffer++ = alac->output_samples_buffer[1][i] << 8; }} break; case 32: if (channels == 2) { int32_t *outbuffer = (int32_t *)alac->frame.data[0]; for (i = 0; i < alac->nb_samples; i++) { *outbuffer++ = alac->output_samples_buffer[0][i]; *outbuffer++ = alac->output_samples_buffer[1][i]; } } else { memcpy(alac->frame.data[0], alac->output_samples_buffer[0], alac->nb_samples * sizeof(*alac->output_samples_buffer[0])); } break; } if (avpkt->size * 8 - get_bits_count(&alac->gb) > 8) av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n", avpkt->size * 8 - get_bits_count(&alac->gb)); *got_frame_ptr = 1; *(AVFrame *)data = alac->frame; return avpkt->size; }
['static int alac_decode_frame(AVCodecContext *avctx, void *data,\n int *got_frame_ptr, AVPacket *avpkt)\n{\n ALACContext *alac = avctx->priv_data;\n int channels;\n int hassize;\n unsigned int readsamplesize;\n int is_compressed;\n uint8_t interlacing_shift;\n uint8_t interlacing_leftweight;\n int i, ch, ret;\n init_get_bits(&alac->gb, avpkt->data, avpkt->size * 8);\n channels = get_bits(&alac->gb, 3) + 1;\n if (channels != avctx->channels) {\n av_log(avctx, AV_LOG_ERROR, "frame header channel count mismatch\\n");\n return AVERROR_INVALIDDATA;\n }\n skip_bits(&alac->gb, 4);\n skip_bits(&alac->gb, 12);\n hassize = get_bits1(&alac->gb);\n alac->extra_bits = get_bits(&alac->gb, 2) << 3;\n is_compressed = !get_bits1(&alac->gb);\n if (hassize) {\n uint32_t output_samples = get_bits_long(&alac->gb, 32);\n if (!output_samples || output_samples > alac->max_samples_per_frame) {\n av_log(avctx, AV_LOG_ERROR, "invalid samples per frame: %d\\n",\n output_samples);\n return AVERROR_INVALIDDATA;\n }\n alac->nb_samples = output_samples;\n } else\n alac->nb_samples = alac->max_samples_per_frame;\n alac->frame.nb_samples = alac->nb_samples;\n if ((ret = avctx->get_buffer(avctx, &alac->frame)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n readsamplesize = alac->sample_size - alac->extra_bits + channels - 1;\n if (readsamplesize > MIN_CACHE_BITS) {\n av_log(avctx, AV_LOG_ERROR, "readsamplesize too big (%d)\\n", readsamplesize);\n return -1;\n }\n if (is_compressed) {\n int16_t predictor_coef_table[MAX_CHANNELS][32];\n int predictor_coef_num[MAX_CHANNELS];\n int prediction_type[MAX_CHANNELS];\n int prediction_quantitization[MAX_CHANNELS];\n int ricemodifier[MAX_CHANNELS];\n interlacing_shift = get_bits(&alac->gb, 8);\n interlacing_leftweight = get_bits(&alac->gb, 8);\n for (ch = 0; ch < channels; ch++) {\n prediction_type[ch] = get_bits(&alac->gb, 4);\n prediction_quantitization[ch] = get_bits(&alac->gb, 4);\n ricemodifier[ch] = get_bits(&alac->gb, 3);\n predictor_coef_num[ch] = get_bits(&alac->gb, 5);\n for (i = 0; i < predictor_coef_num[ch]; i++)\n predictor_coef_table[ch][i] = get_sbits(&alac->gb, 16);\n }\n if (alac->extra_bits) {\n for (i = 0; i < alac->nb_samples; i++) {\n for (ch = 0; ch < channels; ch++)\n alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits);\n }\n }\n for (ch = 0; ch < channels; ch++) {\n bastardized_rice_decompress(alac,\n alac->predict_error_buffer[ch],\n alac->nb_samples,\n readsamplesize,\n ricemodifier[ch] * alac->rice_history_mult / 4);\n if (prediction_type[ch] == 15) {\n predictor_decompress_fir_adapt(alac->predict_error_buffer[ch],\n alac->predict_error_buffer[ch],\n alac->nb_samples, readsamplesize,\n NULL, 31, 0);\n } else if (prediction_type[ch] > 0) {\n av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\\n",\n prediction_type[ch]);\n }\n predictor_decompress_fir_adapt(alac->predict_error_buffer[ch],\n alac->output_samples_buffer[ch],\n alac->nb_samples, readsamplesize,\n predictor_coef_table[ch],\n predictor_coef_num[ch],\n prediction_quantitization[ch]);\n }\n } else {\n for (i = 0; i < alac->nb_samples; i++) {\n for (ch = 0; ch < channels; ch++) {\n alac->output_samples_buffer[ch][i] = get_sbits_long(&alac->gb,\n alac->sample_size);\n }\n }\n alac->extra_bits = 0;\n interlacing_shift = 0;\n interlacing_leftweight = 0;\n }\n if (get_bits(&alac->gb, 3) != 7)\n av_log(avctx, AV_LOG_ERROR, "Error : Wrong End Of Frame\\n");\n if (channels == 2 && interlacing_leftweight) {\n decorrelate_stereo(alac->output_samples_buffer, alac->nb_samples,\n interlacing_shift, interlacing_leftweight);\n }\n if (alac->extra_bits) {\n append_extra_bits(alac->output_samples_buffer, alac->extra_bits_buffer,\n alac->extra_bits, alac->channels, alac->nb_samples);\n }\n switch(alac->sample_size) {\n case 16: {\n int16_t *outbuffer = (int16_t *)alac->frame.data[0];\n for (i = 0; i < alac->nb_samples; i++) {\n *outbuffer++ = alac->output_samples_buffer[0][i];\n if (channels == 2)\n *outbuffer++ = alac->output_samples_buffer[1][i];\n }}\n break;\n case 24: {\n int32_t *outbuffer = (int32_t *)alac->frame.data[0];\n for (i = 0; i < alac->nb_samples; i++) {\n *outbuffer++ = alac->output_samples_buffer[0][i] << 8;\n if (channels == 2)\n *outbuffer++ = alac->output_samples_buffer[1][i] << 8;\n }}\n break;\n case 32:\n if (channels == 2) {\n int32_t *outbuffer = (int32_t *)alac->frame.data[0];\n for (i = 0; i < alac->nb_samples; i++) {\n *outbuffer++ = alac->output_samples_buffer[0][i];\n *outbuffer++ = alac->output_samples_buffer[1][i];\n }\n } else {\n memcpy(alac->frame.data[0], alac->output_samples_buffer[0],\n alac->nb_samples * sizeof(*alac->output_samples_buffer[0]));\n }\n break;\n }\n if (avpkt->size * 8 - get_bits_count(&alac->gb) > 8)\n av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\\n",\n avpkt->size * 8 - get_bits_count(&alac->gb));\n *got_frame_ptr = 1;\n *(AVFrame *)data = alac->frame;\n return avpkt->size;\n}']
35,729
0
https://github.com/libav/libav/blob/e4e30256f87f177decf59b59e923d05ef64147df/libavcodec/mpegvideo_enc.c/#L1975
static av_always_inline void encode_mb_internal(MpegEncContext *s, int motion_x, int motion_y, int mb_block_height, int mb_block_count) { int16_t weight[8][64]; DCTELEM orig[8][64]; const int mb_x = s->mb_x; const int mb_y = s->mb_y; int i; int skip_dct[8]; int dct_offset = s->linesize * 8; uint8_t *ptr_y, *ptr_cb, *ptr_cr; int wrap_y, wrap_c; for (i = 0; i < mb_block_count; i++) skip_dct[i] = s->skipdct; if (s->adaptive_quant) { const int last_qp = s->qscale; const int mb_xy = mb_x + mb_y * s->mb_stride; s->lambda = s->lambda_table[mb_xy]; update_qscale(s); if (!(s->flags & CODEC_FLAG_QP_RD)) { s->qscale = s->current_picture_ptr->f.qscale_table[mb_xy]; s->dquant = s->qscale - last_qp; if (s->out_format == FMT_H263) { s->dquant = av_clip(s->dquant, -2, 2); if (s->codec_id == CODEC_ID_MPEG4) { if (!s->mb_intra) { if (s->pict_type == AV_PICTURE_TYPE_B) { if (s->dquant & 1 || s->mv_dir & MV_DIRECT) s->dquant = 0; } if (s->mv_type == MV_TYPE_8X8) s->dquant = 0; } } } } ff_set_qscale(s, last_qp + s->dquant); } else if (s->flags & CODEC_FLAG_QP_RD) ff_set_qscale(s, s->qscale + s->dquant); wrap_y = s->linesize; wrap_c = s->uvlinesize; ptr_y = s->new_picture.f.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16; ptr_cb = s->new_picture.f.data[1] + (mb_y * mb_block_height * wrap_c) + mb_x * 8; ptr_cr = s->new_picture.f.data[2] + (mb_y * mb_block_height * wrap_c) + mb_x * 8; if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) { uint8_t *ebuf = s->edge_emu_buffer + 32; s->dsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16, mb_y * 16, s->width, s->height); ptr_y = ebuf; s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb, wrap_c, 8, mb_block_height, mb_x * 8, mb_y * 8, s->width >> 1, s->height >> 1); ptr_cb = ebuf + 18 * wrap_y; s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr, wrap_c, 8, mb_block_height, mb_x * 8, mb_y * 8, s->width >> 1, s->height >> 1); ptr_cr = ebuf + 18 * wrap_y + 8; } if (s->mb_intra) { if (s->flags & CODEC_FLAG_INTERLACED_DCT) { int progressive_score, interlaced_score; s->interlaced_dct = 0; progressive_score = s->dsp.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) + s->dsp.ildct_cmp[4](s, ptr_y + wrap_y * 8, NULL, wrap_y, 8) - 400; if (progressive_score > 0) { interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y, NULL, wrap_y * 2, 8) + s->dsp.ildct_cmp[4](s, ptr_y + wrap_y, NULL, wrap_y * 2, 8); if (progressive_score > interlaced_score) { s->interlaced_dct = 1; dct_offset = wrap_y; wrap_y <<= 1; if (s->chroma_format == CHROMA_422) wrap_c <<= 1; } } } s->dsp.get_pixels(s->block[0], ptr_y , wrap_y); s->dsp.get_pixels(s->block[1], ptr_y + 8 , wrap_y); s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y); s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8 , wrap_y); if (s->flags & CODEC_FLAG_GRAY) { skip_dct[4] = 1; skip_dct[5] = 1; } else { s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c); s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c); if (!s->chroma_y_shift) { s->dsp.get_pixels(s->block[6], ptr_cb + (dct_offset >> 1), wrap_c); s->dsp.get_pixels(s->block[7], ptr_cr + (dct_offset >> 1), wrap_c); } } } else { op_pixels_func (*op_pix)[4]; qpel_mc_func (*op_qpix)[16]; uint8_t *dest_y, *dest_cb, *dest_cr; dest_y = s->dest[0]; dest_cb = s->dest[1]; dest_cr = s->dest[2]; if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) { op_pix = s->dsp.put_pixels_tab; op_qpix = s->dsp.put_qpel_pixels_tab; } else { op_pix = s->dsp.put_no_rnd_pixels_tab; op_qpix = s->dsp.put_no_rnd_qpel_pixels_tab; } if (s->mv_dir & MV_DIR_FORWARD) { MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix); op_pix = s->dsp.avg_pixels_tab; op_qpix = s->dsp.avg_qpel_pixels_tab; } if (s->mv_dir & MV_DIR_BACKWARD) { MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix); } if (s->flags & CODEC_FLAG_INTERLACED_DCT) { int progressive_score, interlaced_score; s->interlaced_dct = 0; progressive_score = s->dsp.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) + s->dsp.ildct_cmp[0](s, dest_y + wrap_y * 8, ptr_y + wrap_y * 8, wrap_y, 8) - 400; if (s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400; if (progressive_score > 0) { interlaced_score = s->dsp.ildct_cmp[0](s, dest_y, ptr_y, wrap_y * 2, 8) + s->dsp.ildct_cmp[0](s, dest_y + wrap_y, ptr_y + wrap_y, wrap_y * 2, 8); if (progressive_score > interlaced_score) { s->interlaced_dct = 1; dct_offset = wrap_y; wrap_y <<= 1; if (s->chroma_format == CHROMA_422) wrap_c <<= 1; } } } s->dsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y); s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y); s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset, dest_y + dct_offset, wrap_y); s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y); if (s->flags & CODEC_FLAG_GRAY) { skip_dct[4] = 1; skip_dct[5] = 1; } else { s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c); s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c); if (!s->chroma_y_shift) { s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1), dest_cb + (dct_offset >> 1), wrap_c); s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1), dest_cr + (dct_offset >> 1), wrap_c); } } if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] < 2 * s->qscale * s->qscale) { if (s->dsp.sad[1](NULL, ptr_y , dest_y, wrap_y, 8) < 20 * s->qscale) skip_dct[0] = 1; if (s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale) skip_dct[1] = 1; if (s->dsp.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset, wrap_y, 8) < 20 * s->qscale) skip_dct[2] = 1; if (s->dsp.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y, 8) < 20 * s->qscale) skip_dct[3] = 1; if (s->dsp.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale) skip_dct[4] = 1; if (s->dsp.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale) skip_dct[5] = 1; if (!s->chroma_y_shift) { if (s->dsp.sad[1](NULL, ptr_cb + (dct_offset >> 1), dest_cb + (dct_offset >> 1), wrap_c, 8) < 20 * s->qscale) skip_dct[6] = 1; if (s->dsp.sad[1](NULL, ptr_cr + (dct_offset >> 1), dest_cr + (dct_offset >> 1), wrap_c, 8) < 20 * s->qscale) skip_dct[7] = 1; } } } if (s->avctx->quantizer_noise_shaping) { if (!skip_dct[0]) get_visual_weight(weight[0], ptr_y , wrap_y); if (!skip_dct[1]) get_visual_weight(weight[1], ptr_y + 8, wrap_y); if (!skip_dct[2]) get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y); if (!skip_dct[3]) get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y); if (!skip_dct[4]) get_visual_weight(weight[4], ptr_cb , wrap_c); if (!skip_dct[5]) get_visual_weight(weight[5], ptr_cr , wrap_c); if (!s->chroma_y_shift) { if (!skip_dct[6]) get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1), wrap_c); if (!skip_dct[7]) get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1), wrap_c); } memcpy(orig[0], s->block[0], sizeof(DCTELEM) * 64 * mb_block_count); } assert(s->out_format != FMT_MJPEG || s->qscale == 8); { for (i = 0; i < mb_block_count; i++) { if (!skip_dct[i]) { int overflow; s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow); if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]); } else s->block_last_index[i] = -1; } if (s->avctx->quantizer_noise_shaping) { for (i = 0; i < mb_block_count; i++) { if (!skip_dct[i]) { s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale); } } } if (s->luma_elim_threshold && !s->mb_intra) for (i = 0; i < 4; i++) dct_single_coeff_elimination(s, i, s->luma_elim_threshold); if (s->chroma_elim_threshold && !s->mb_intra) for (i = 4; i < mb_block_count; i++) dct_single_coeff_elimination(s, i, s->chroma_elim_threshold); if (s->flags & CODEC_FLAG_CBP_RD) { for (i = 0; i < mb_block_count; i++) { if (s->block_last_index[i] == -1) s->coded_score[i] = INT_MAX / 256; } } } if ((s->flags & CODEC_FLAG_GRAY) && s->mb_intra) { s->block_last_index[4] = s->block_last_index[5] = 0; s->block[4][0] = s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale; } if (s->alternate_scan && s->dct_quantize != dct_quantize_c) { for (i = 0; i < mb_block_count; i++) { int j; if (s->block_last_index[i] > 0) { for (j = 63; j > 0; j--) { if (s->block[i][s->intra_scantable.permutated[j]]) break; } s->block_last_index[i] = j; } } } switch(s->codec_id){ case CODEC_ID_MPEG1VIDEO: case CODEC_ID_MPEG2VIDEO: if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) mpeg1_encode_mb(s, s->block, motion_x, motion_y); break; case CODEC_ID_MPEG4: if (CONFIG_MPEG4_ENCODER) mpeg4_encode_mb(s, s->block, motion_x, motion_y); break; case CODEC_ID_MSMPEG4V2: case CODEC_ID_MSMPEG4V3: case CODEC_ID_WMV1: if (CONFIG_MSMPEG4_ENCODER) msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break; case CODEC_ID_WMV2: if (CONFIG_WMV2_ENCODER) ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break; case CODEC_ID_H261: if (CONFIG_H261_ENCODER) ff_h261_encode_mb(s, s->block, motion_x, motion_y); break; case CODEC_ID_H263: case CODEC_ID_H263P: case CODEC_ID_FLV1: case CODEC_ID_RV10: case CODEC_ID_RV20: if (CONFIG_H263_ENCODER) h263_encode_mb(s, s->block, motion_x, motion_y); break; case CODEC_ID_MJPEG: if (CONFIG_MJPEG_ENCODER) ff_mjpeg_encode_mb(s, s->block); break; default: assert(0); } }
['static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)\n{\n if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 6);\n else encode_mb_internal(s, motion_x, motion_y, 16, 8);\n}', 'static av_always_inline void encode_mb_internal(MpegEncContext *s,\n int motion_x, int motion_y,\n int mb_block_height,\n int mb_block_count)\n{\n int16_t weight[8][64];\n DCTELEM orig[8][64];\n const int mb_x = s->mb_x;\n const int mb_y = s->mb_y;\n int i;\n int skip_dct[8];\n int dct_offset = s->linesize * 8;\n uint8_t *ptr_y, *ptr_cb, *ptr_cr;\n int wrap_y, wrap_c;\n for (i = 0; i < mb_block_count; i++)\n skip_dct[i] = s->skipdct;\n if (s->adaptive_quant) {\n const int last_qp = s->qscale;\n const int mb_xy = mb_x + mb_y * s->mb_stride;\n s->lambda = s->lambda_table[mb_xy];\n update_qscale(s);\n if (!(s->flags & CODEC_FLAG_QP_RD)) {\n s->qscale = s->current_picture_ptr->f.qscale_table[mb_xy];\n s->dquant = s->qscale - last_qp;\n if (s->out_format == FMT_H263) {\n s->dquant = av_clip(s->dquant, -2, 2);\n if (s->codec_id == CODEC_ID_MPEG4) {\n if (!s->mb_intra) {\n if (s->pict_type == AV_PICTURE_TYPE_B) {\n if (s->dquant & 1 || s->mv_dir & MV_DIRECT)\n s->dquant = 0;\n }\n if (s->mv_type == MV_TYPE_8X8)\n s->dquant = 0;\n }\n }\n }\n }\n ff_set_qscale(s, last_qp + s->dquant);\n } else if (s->flags & CODEC_FLAG_QP_RD)\n ff_set_qscale(s, s->qscale + s->dquant);\n wrap_y = s->linesize;\n wrap_c = s->uvlinesize;\n ptr_y = s->new_picture.f.data[0] +\n (mb_y * 16 * wrap_y) + mb_x * 16;\n ptr_cb = s->new_picture.f.data[1] +\n (mb_y * mb_block_height * wrap_c) + mb_x * 8;\n ptr_cr = s->new_picture.f.data[2] +\n (mb_y * mb_block_height * wrap_c) + mb_x * 8;\n if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {\n uint8_t *ebuf = s->edge_emu_buffer + 32;\n s->dsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16,\n mb_y * 16, s->width, s->height);\n ptr_y = ebuf;\n s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb, wrap_c, 8,\n mb_block_height, mb_x * 8, mb_y * 8,\n s->width >> 1, s->height >> 1);\n ptr_cb = ebuf + 18 * wrap_y;\n s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr, wrap_c, 8,\n mb_block_height, mb_x * 8, mb_y * 8,\n s->width >> 1, s->height >> 1);\n ptr_cr = ebuf + 18 * wrap_y + 8;\n }\n if (s->mb_intra) {\n if (s->flags & CODEC_FLAG_INTERLACED_DCT) {\n int progressive_score, interlaced_score;\n s->interlaced_dct = 0;\n progressive_score = s->dsp.ildct_cmp[4](s, ptr_y,\n NULL, wrap_y, 8) +\n s->dsp.ildct_cmp[4](s, ptr_y + wrap_y * 8,\n NULL, wrap_y, 8) - 400;\n if (progressive_score > 0) {\n interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y,\n NULL, wrap_y * 2, 8) +\n s->dsp.ildct_cmp[4](s, ptr_y + wrap_y,\n NULL, wrap_y * 2, 8);\n if (progressive_score > interlaced_score) {\n s->interlaced_dct = 1;\n dct_offset = wrap_y;\n wrap_y <<= 1;\n if (s->chroma_format == CHROMA_422)\n wrap_c <<= 1;\n }\n }\n }\n s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);\n s->dsp.get_pixels(s->block[1], ptr_y + 8 , wrap_y);\n s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);\n s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8 , wrap_y);\n if (s->flags & CODEC_FLAG_GRAY) {\n skip_dct[4] = 1;\n skip_dct[5] = 1;\n } else {\n s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);\n s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);\n if (!s->chroma_y_shift) {\n s->dsp.get_pixels(s->block[6],\n ptr_cb + (dct_offset >> 1), wrap_c);\n s->dsp.get_pixels(s->block[7],\n ptr_cr + (dct_offset >> 1), wrap_c);\n }\n }\n } else {\n op_pixels_func (*op_pix)[4];\n qpel_mc_func (*op_qpix)[16];\n uint8_t *dest_y, *dest_cb, *dest_cr;\n dest_y = s->dest[0];\n dest_cb = s->dest[1];\n dest_cr = s->dest[2];\n if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {\n op_pix = s->dsp.put_pixels_tab;\n op_qpix = s->dsp.put_qpel_pixels_tab;\n } else {\n op_pix = s->dsp.put_no_rnd_pixels_tab;\n op_qpix = s->dsp.put_no_rnd_qpel_pixels_tab;\n }\n if (s->mv_dir & MV_DIR_FORWARD) {\n MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data,\n op_pix, op_qpix);\n op_pix = s->dsp.avg_pixels_tab;\n op_qpix = s->dsp.avg_qpel_pixels_tab;\n }\n if (s->mv_dir & MV_DIR_BACKWARD) {\n MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data,\n op_pix, op_qpix);\n }\n if (s->flags & CODEC_FLAG_INTERLACED_DCT) {\n int progressive_score, interlaced_score;\n s->interlaced_dct = 0;\n progressive_score = s->dsp.ildct_cmp[0](s, dest_y,\n ptr_y, wrap_y,\n 8) +\n s->dsp.ildct_cmp[0](s, dest_y + wrap_y * 8,\n ptr_y + wrap_y * 8, wrap_y,\n 8) - 400;\n if (s->avctx->ildct_cmp == FF_CMP_VSSE)\n progressive_score -= 400;\n if (progressive_score > 0) {\n interlaced_score = s->dsp.ildct_cmp[0](s, dest_y,\n ptr_y,\n wrap_y * 2, 8) +\n s->dsp.ildct_cmp[0](s, dest_y + wrap_y,\n ptr_y + wrap_y,\n wrap_y * 2, 8);\n if (progressive_score > interlaced_score) {\n s->interlaced_dct = 1;\n dct_offset = wrap_y;\n wrap_y <<= 1;\n if (s->chroma_format == CHROMA_422)\n wrap_c <<= 1;\n }\n }\n }\n s->dsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);\n s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);\n s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset,\n dest_y + dct_offset, wrap_y);\n s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,\n dest_y + dct_offset + 8, wrap_y);\n if (s->flags & CODEC_FLAG_GRAY) {\n skip_dct[4] = 1;\n skip_dct[5] = 1;\n } else {\n s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);\n s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);\n if (!s->chroma_y_shift) {\n s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),\n dest_cb + (dct_offset >> 1), wrap_c);\n s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),\n dest_cr + (dct_offset >> 1), wrap_c);\n }\n }\n if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <\n 2 * s->qscale * s->qscale) {\n if (s->dsp.sad[1](NULL, ptr_y , dest_y,\n wrap_y, 8) < 20 * s->qscale)\n skip_dct[0] = 1;\n if (s->dsp.sad[1](NULL, ptr_y + 8,\n dest_y + 8, wrap_y, 8) < 20 * s->qscale)\n skip_dct[1] = 1;\n if (s->dsp.sad[1](NULL, ptr_y + dct_offset,\n dest_y + dct_offset, wrap_y, 8) < 20 * s->qscale)\n skip_dct[2] = 1;\n if (s->dsp.sad[1](NULL, ptr_y + dct_offset + 8,\n dest_y + dct_offset + 8,\n wrap_y, 8) < 20 * s->qscale)\n skip_dct[3] = 1;\n if (s->dsp.sad[1](NULL, ptr_cb, dest_cb,\n wrap_c, 8) < 20 * s->qscale)\n skip_dct[4] = 1;\n if (s->dsp.sad[1](NULL, ptr_cr, dest_cr,\n wrap_c, 8) < 20 * s->qscale)\n skip_dct[5] = 1;\n if (!s->chroma_y_shift) {\n if (s->dsp.sad[1](NULL, ptr_cb + (dct_offset >> 1),\n dest_cb + (dct_offset >> 1),\n wrap_c, 8) < 20 * s->qscale)\n skip_dct[6] = 1;\n if (s->dsp.sad[1](NULL, ptr_cr + (dct_offset >> 1),\n dest_cr + (dct_offset >> 1),\n wrap_c, 8) < 20 * s->qscale)\n skip_dct[7] = 1;\n }\n }\n }\n if (s->avctx->quantizer_noise_shaping) {\n if (!skip_dct[0])\n get_visual_weight(weight[0], ptr_y , wrap_y);\n if (!skip_dct[1])\n get_visual_weight(weight[1], ptr_y + 8, wrap_y);\n if (!skip_dct[2])\n get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);\n if (!skip_dct[3])\n get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);\n if (!skip_dct[4])\n get_visual_weight(weight[4], ptr_cb , wrap_c);\n if (!skip_dct[5])\n get_visual_weight(weight[5], ptr_cr , wrap_c);\n if (!s->chroma_y_shift) {\n if (!skip_dct[6])\n get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1),\n wrap_c);\n if (!skip_dct[7])\n get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1),\n wrap_c);\n }\n memcpy(orig[0], s->block[0], sizeof(DCTELEM) * 64 * mb_block_count);\n }\n assert(s->out_format != FMT_MJPEG || s->qscale == 8);\n {\n for (i = 0; i < mb_block_count; i++) {\n if (!skip_dct[i]) {\n int overflow;\n s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);\n if (overflow)\n clip_coeffs(s, s->block[i], s->block_last_index[i]);\n } else\n s->block_last_index[i] = -1;\n }\n if (s->avctx->quantizer_noise_shaping) {\n for (i = 0; i < mb_block_count; i++) {\n if (!skip_dct[i]) {\n s->block_last_index[i] =\n dct_quantize_refine(s, s->block[i], weight[i],\n orig[i], i, s->qscale);\n }\n }\n }\n if (s->luma_elim_threshold && !s->mb_intra)\n for (i = 0; i < 4; i++)\n dct_single_coeff_elimination(s, i, s->luma_elim_threshold);\n if (s->chroma_elim_threshold && !s->mb_intra)\n for (i = 4; i < mb_block_count; i++)\n dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);\n if (s->flags & CODEC_FLAG_CBP_RD) {\n for (i = 0; i < mb_block_count; i++) {\n if (s->block_last_index[i] == -1)\n s->coded_score[i] = INT_MAX / 256;\n }\n }\n }\n if ((s->flags & CODEC_FLAG_GRAY) && s->mb_intra) {\n s->block_last_index[4] =\n s->block_last_index[5] = 0;\n s->block[4][0] =\n s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;\n }\n if (s->alternate_scan && s->dct_quantize != dct_quantize_c) {\n for (i = 0; i < mb_block_count; i++) {\n int j;\n if (s->block_last_index[i] > 0) {\n for (j = 63; j > 0; j--) {\n if (s->block[i][s->intra_scantable.permutated[j]])\n break;\n }\n s->block_last_index[i] = j;\n }\n }\n }\n switch(s->codec_id){\n case CODEC_ID_MPEG1VIDEO:\n case CODEC_ID_MPEG2VIDEO:\n if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)\n mpeg1_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case CODEC_ID_MPEG4:\n if (CONFIG_MPEG4_ENCODER)\n mpeg4_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case CODEC_ID_MSMPEG4V2:\n case CODEC_ID_MSMPEG4V3:\n case CODEC_ID_WMV1:\n if (CONFIG_MSMPEG4_ENCODER)\n msmpeg4_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case CODEC_ID_WMV2:\n if (CONFIG_WMV2_ENCODER)\n ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case CODEC_ID_H261:\n if (CONFIG_H261_ENCODER)\n ff_h261_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case CODEC_ID_H263:\n case CODEC_ID_H263P:\n case CODEC_ID_FLV1:\n case CODEC_ID_RV10:\n case CODEC_ID_RV20:\n if (CONFIG_H263_ENCODER)\n h263_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case CODEC_ID_MJPEG:\n if (CONFIG_MJPEG_ENCODER)\n ff_mjpeg_encode_mb(s, s->block);\n break;\n default:\n assert(0);\n }\n}']
35,730
0
https://github.com/openssl/openssl/blob/47ddf355b46eae8c846e411f44531e928e04adf5/crypto/lhash/lhash.c/#L240
void *lh_delete(LHASH *lh, const void *data) { unsigned long hash; LHASH_NODE *nn,**rn; const 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((void *)ret); }
['int ssl3_connect(SSL *s)\n\t{\n\tBUF_MEM *buf;\n\tunsigned long Time=time(NULL),l;\n\tlong num1;\n\tvoid (*cb)()=NULL;\n\tint ret= -1;\n\tint new_state,state,skip=0;;\n\tRAND_add(&Time,sizeof(Time),0);\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_RENEGOTIATE:\n\t\t\ts->new_session=1;\n\t\t\ts->state=SSL_ST_CONNECT;\n\t\t\ts->ctx->stats.sess_connect_renegotiate++;\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\tif ((s->version & 0xff00 ) != 0x0300)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_CONNECT, SSL_R_INTERNAL_ERROR);\n\t\t\t\tret = -1;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\ts->type=SSL_ST_CONNECT;\n\t\t\tif (s->init_buf == NULL)\n\t\t\t\t{\n\t\t\t\tif ((buf=BUF_MEM_new()) == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tret= -1;\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tif (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))\n\t\t\t\t\t{\n\t\t\t\t\tret= -1;\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\ts->init_buf=buf;\n\t\t\t\t}\n\t\t\tif (!ssl3_setup_buffers(s)) { ret= -1; goto end; }\n\t\t\tif (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }\n\t\t\tssl3_init_finished_mac(s);\n\t\t\ts->state=SSL3_ST_CW_CLNT_HELLO_A;\n\t\t\ts->ctx->stats.sess_connect++;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_CLNT_HELLO_A:\n\t\tcase SSL3_ST_CW_CLNT_HELLO_B:\n\t\t\ts->shutdown=0;\n\t\t\tret=ssl3_client_hello(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CR_SRVR_HELLO_A;\n\t\t\ts->init_num=0;\n\t\t\tif (s->bbio != s->wbio)\n\t\t\t\ts->wbio=BIO_push(s->bbio,s->wbio);\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_SRVR_HELLO_A:\n\t\tcase SSL3_ST_CR_SRVR_HELLO_B:\n\t\t\tret=ssl3_get_server_hello(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (s->hit)\n\t\t\t\ts->state=SSL3_ST_CR_FINISHED_A;\n\t\t\telse\n\t\t\t\ts->state=SSL3_ST_CR_CERT_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_CERT_A:\n\t\tcase SSL3_ST_CR_CERT_B:\n\t\t\tif (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))\n\t\t\t\t{\n\t\t\t\tret=ssl3_get_server_certificate(s);\n\t\t\t\tif (ret <= 0) goto end;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tskip=1;\n\t\t\ts->state=SSL3_ST_CR_KEY_EXCH_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_KEY_EXCH_A:\n\t\tcase SSL3_ST_CR_KEY_EXCH_B:\n\t\t\tret=ssl3_get_key_exchange(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CR_CERT_REQ_A;\n\t\t\ts->init_num=0;\n\t\t\tif (!ssl3_check_cert_and_algorithm(s))\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\tbreak;\n\t\tcase SSL3_ST_CR_CERT_REQ_A:\n\t\tcase SSL3_ST_CR_CERT_REQ_B:\n\t\t\tret=ssl3_get_certificate_request(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CR_SRVR_DONE_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_SRVR_DONE_A:\n\t\tcase SSL3_ST_CR_SRVR_DONE_B:\n\t\t\tret=ssl3_get_server_done(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (s->s3->tmp.cert_req)\n\t\t\t\ts->state=SSL3_ST_CW_CERT_A;\n\t\t\telse\n\t\t\t\ts->state=SSL3_ST_CW_KEY_EXCH_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_CERT_A:\n\t\tcase SSL3_ST_CW_CERT_B:\n\t\tcase SSL3_ST_CW_CERT_C:\n\t\tcase SSL3_ST_CW_CERT_D:\n\t\t\tret=ssl3_send_client_certificate(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CW_KEY_EXCH_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_KEY_EXCH_A:\n\t\tcase SSL3_ST_CW_KEY_EXCH_B:\n\t\t\tret=ssl3_send_client_key_exchange(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tl=s->s3->tmp.new_cipher->algorithms;\n\t\t\tif (s->s3->tmp.cert_req == 1)\n\t\t\t\t{\n\t\t\t\ts->state=SSL3_ST_CW_CERT_VRFY_A;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts->state=SSL3_ST_CW_CHANGE_A;\n\t\t\t\ts->s3->change_cipher_spec=0;\n\t\t\t\t}\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_CERT_VRFY_A:\n\t\tcase SSL3_ST_CW_CERT_VRFY_B:\n\t\t\tret=ssl3_send_client_verify(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CW_CHANGE_A;\n\t\t\ts->init_num=0;\n\t\t\ts->s3->change_cipher_spec=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_CHANGE_A:\n\t\tcase SSL3_ST_CW_CHANGE_B:\n\t\t\tret=ssl3_send_change_cipher_spec(s,\n\t\t\t\tSSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CW_FINISHED_A;\n\t\t\ts->init_num=0;\n\t\t\ts->session->cipher=s->s3->tmp.new_cipher;\n\t\t\tif (s->s3->tmp.new_compression == NULL)\n\t\t\t\ts->session->compress_meth=0;\n\t\t\telse\n\t\t\t\ts->session->compress_meth=\n\t\t\t\t\ts->s3->tmp.new_compression->id;\n\t\t\tif (!s->method->ssl3_enc->setup_key_block(s))\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 (!s->method->ssl3_enc->change_cipher_state(s,\n\t\t\t\tSSL3_CHANGE_CIPHER_CLIENT_WRITE))\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\tbreak;\n\t\tcase SSL3_ST_CW_FINISHED_A:\n\t\tcase SSL3_ST_CW_FINISHED_B:\n\t\t\tret=ssl3_send_finished(s,\n\t\t\t\tSSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,\n\t\t\t\ts->method->ssl3_enc->client_finished_label,\n\t\t\t\ts->method->ssl3_enc->client_finished_label_len);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CW_FLUSH;\n\t\t\ts->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;\n\t\t\tif (s->hit)\n\t\t\t\t{\n\t\t\t\ts->s3->tmp.next_state=SSL_ST_OK;\n\t\t\t\tif (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)\n\t\t\t\t\t{\n\t\t\t\t\ts->state=SSL_ST_OK;\n\t\t\t\t\ts->s3->flags|=SSL3_FLAGS_POP_BUFFER;\n\t\t\t\t\ts->s3->delay_buf_pop_ret=0;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;\n\t\t\t\t}\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_FINISHED_A:\n\t\tcase SSL3_ST_CR_FINISHED_B:\n\t\t\tret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,\n\t\t\t\tSSL3_ST_CR_FINISHED_B);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (s->hit)\n\t\t\t\ts->state=SSL3_ST_CW_CHANGE_A;\n\t\t\telse\n\t\t\t\ts->state=SSL_ST_OK;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_FLUSH:\n\t\t\tnum1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);\n\t\t\tif (num1 > 0)\n\t\t\t\t{\n\t\t\t\ts->rwstate=SSL_WRITING;\n\t\t\t\tnum1=BIO_flush(s->wbio);\n\t\t\t\tif (num1 <= 0) { ret= -1; goto end; }\n\t\t\t\ts->rwstate=SSL_NOTHING;\n\t\t\t\t}\n\t\t\ts->state=s->s3->tmp.next_state;\n\t\t\tbreak;\n\t\tcase SSL_ST_OK:\n\t\t\tssl3_cleanup_key_block(s);\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\tif (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))\n\t\t\t\tssl_free_wbio_buffer(s);\n\t\t\ts->init_num=0;\n\t\t\ts->new_session=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->handshake_func=ssl3_connect;\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_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);\n\t\t\tret= -1;\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (!s->s3->tmp.reuse_message && !skip)\n\t\t\t{\n\t\t\tif (s->debug)\n\t\t\t\t{\n\t\t\t\tif ((ret=BIO_flush(s->wbio)) <= 0)\n\t\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tif ((cb != NULL) && (s->state != state))\n\t\t\t\t{\n\t\t\t\tnew_state=s->state;\n\t\t\t\ts->state=state;\n\t\t\t\tcb(s,SSL_CB_CONNECT_LOOP,1);\n\t\t\t\ts->state=new_state;\n\t\t\t\t}\n\t\t\t}\n\t\tskip=0;\n\t\t}\nend:\n\tif (cb != NULL)\n\t\tcb(s,SSL_CB_CONNECT_EXIT,ret);\n\ts->in_handshake--;\n\treturn(ret);\n\t}', 'static int ssl3_get_key_exchange(SSL *s)\n\t{\n#ifndef OPENSSL_NO_RSA\n\tunsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];\n#endif\n\tEVP_MD_CTX md_ctx;\n\tunsigned char *param,*p;\n\tint al,i,j,param_len,ok;\n\tlong n,alg;\n\tEVP_PKEY *pkey=NULL;\n#ifndef OPENSSL_NO_RSA\n\tRSA *rsa=NULL;\n#endif\n#ifndef OPENSSL_NO_DH\n\tDH *dh=NULL;\n#endif\n\tn=ssl3_get_message(s,\n\t\tSSL3_ST_CR_KEY_EXCH_A,\n\t\tSSL3_ST_CR_KEY_EXCH_B,\n\t\t-1,\n\t\t1024*8,\n\t\t&ok);\n\tif (!ok) return((int)n);\n\tif (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)\n\t\t{\n\t\ts->s3->tmp.reuse_message=1;\n\t\treturn(1);\n\t\t}\n\tparam=p=(unsigned char *)s->init_buf->data;\n\tif (s->session->sess_cert != NULL)\n\t\t{\n#ifndef OPENSSL_NO_RSA\n\t\tif (s->session->sess_cert->peer_rsa_tmp != NULL)\n\t\t\t{\n\t\t\tRSA_free(s->session->sess_cert->peer_rsa_tmp);\n\t\t\ts->session->sess_cert->peer_rsa_tmp=NULL;\n\t\t\t}\n#endif\n#ifndef OPENSSL_NO_DH\n\t\tif (s->session->sess_cert->peer_dh_tmp)\n\t\t\t{\n\t\t\tDH_free(s->session->sess_cert->peer_dh_tmp);\n\t\t\ts->session->sess_cert->peer_dh_tmp=NULL;\n\t\t\t}\n#endif\n\t\t}\n\telse\n\t\t{\n\t\ts->session->sess_cert=ssl_sess_cert_new();\n\t\t}\n\tparam_len=0;\n\talg=s->s3->tmp.new_cipher->algorithms;\n#ifndef OPENSSL_NO_RSA\n\tif (alg & SSL_kRSA)\n\t\t{\n\t\tif ((rsa=RSA_new()) == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto err;\n\t\t\t}\n\t\tn2s(p,i);\n\t\tparam_len=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(rsa->n=BN_bin2bn(p,i,rsa->n)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn2s(p,i);\n\t\tparam_len+=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(rsa->e=BN_bin2bn(p,i,rsa->e)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn-=param_len;\n\t\tif (alg & SSL_aRSA)\n\t\t\tpkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);\n\t\telse\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);\n\t\t\tgoto err;\n\t\t\t}\n\t\ts->session->sess_cert->peer_rsa_tmp=rsa;\n\t\trsa=NULL;\n\t\t}\n#else\n\tif (0)\n\t\t;\n#endif\n#ifndef OPENSSL_NO_DH\n\telse if (alg & SSL_kEDH)\n\t\t{\n\t\tif ((dh=DH_new()) == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tn2s(p,i);\n\t\tparam_len=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(dh->p=BN_bin2bn(p,i,NULL)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn2s(p,i);\n\t\tparam_len+=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(dh->g=BN_bin2bn(p,i,NULL)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn2s(p,i);\n\t\tparam_len+=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(dh->pub_key=BN_bin2bn(p,i,NULL)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn-=param_len;\n#ifndef OPENSSL_NO_RSA\n\t\tif (alg & SSL_aRSA)\n\t\t\tpkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);\n#else\n\t\tif (0)\n\t\t\t;\n#endif\n#ifndef OPENSSL_NO_DSA\n\t\telse if (alg & SSL_aDSS)\n\t\t\tpkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);\n#endif\n\t\ts->session->sess_cert->peer_dh_tmp=dh;\n\t\tdh=NULL;\n\t\t}\n\telse if ((alg & SSL_kDHr) || (alg & SSL_kDHd))\n\t\t{\n\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);\n\t\tgoto f_err;\n\t\t}\n#endif\n\tif (alg & SSL_aFZA)\n\t\t{\n\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);\n\t\tgoto f_err;\n\t\t}\n\tif (pkey != NULL)\n\t\t{\n\t\tn2s(p,i);\n\t\tn-=2;\n\t\tj=EVP_PKEY_size(pkey);\n\t\tif ((i != n) || (n > j) || (n <= 0))\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n#ifndef OPENSSL_NO_RSA\n\t\tif (pkey->type == EVP_PKEY_RSA)\n\t\t\t{\n\t\t\tint num;\n\t\t\tj=0;\n\t\t\tq=md_buf;\n\t\t\tfor (num=2; num > 0; num--)\n\t\t\t\t{\n\t\t\t\tEVP_DigestInit(&md_ctx,(num == 2)\n\t\t\t\t\t?s->ctx->md5:s->ctx->sha1);\n\t\t\t\tEVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\tEVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\tEVP_DigestUpdate(&md_ctx,param,param_len);\n\t\t\t\tEVP_DigestFinal(&md_ctx,q,(unsigned int *)&i);\n\t\t\t\tq+=i;\n\t\t\t\tj+=i;\n\t\t\t\t}\n\t\t\ti=RSA_verify(NID_md5_sha1, md_buf, j, p, n,\n\t\t\t\t\t\t\t\tpkey->pkey.rsa);\n\t\t\tif (i < 0)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECRYPT_ERROR;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\tif (i == 0)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECRYPT_ERROR;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n#ifndef OPENSSL_NO_DSA\n\t\t\tif (pkey->type == EVP_PKEY_DSA)\n\t\t\t{\n\t\t\tEVP_VerifyInit(&md_ctx,EVP_dss1());\n\t\t\tEVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);\n\t\t\tEVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);\n\t\t\tEVP_VerifyUpdate(&md_ctx,param,param_len);\n\t\t\tif (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECRYPT_ERROR;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tif (!(alg & SSL_aNULL))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (n != 0)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\tEVP_PKEY_free(pkey);\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\tEVP_PKEY_free(pkey);\n#ifndef OPENSSL_NO_RSA\n\tif (rsa != NULL)\n\t\tRSA_free(rsa);\n#endif\n#ifndef OPENSSL_NO_DH\n\tif (dh != NULL)\n\t\tDH_free(dh);\n#endif\n\treturn(-1);\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\tint skip_message;\n\t\tdo\n\t\t\t{\n\t\t\twhile (s->init_num < 4)\n\t\t\t\t{\n\t\t\t\ti=ssl3_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],\n\t\t\t\t\t4 - s->init_num, 0);\n\t\t\t\tif (i <= 0)\n\t\t\t\t\t{\n\t\t\t\t\ts->rwstate=SSL_READING;\n\t\t\t\t\t*ok = 0;\n\t\t\t\t\treturn i;\n\t\t\t\t\t}\n\t\t\t\ts->init_num+=i;\n\t\t\t\t}\n\t\t\tskip_message = 0;\n\t\t\tif (!s->server)\n\t\t\t\tif (p[0] == SSL3_MT_HELLO_REQUEST)\n\t\t\t\t\tif (p[1] == 0 && p[2] == 0 &&p[3] == 0)\n\t\t\t\t\t\tskip_message = 1;\n\t\t\t}\n\t\twhile (skip_message);\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\tif ((mt < 0) && (*p == SSL3_MT_CLIENT_HELLO) &&\n\t\t\t\t\t(st1 == SSL3_ST_SR_CERT_A) &&\n\t\t\t\t\t(stn == SSL3_ST_SR_CERT_B))\n\t\t\t{\n\t\t\tssl3_init_finished_mac(s);\n\t\t\t}\n\t\tssl3_finish_mac(s, (unsigned char *)s->init_buf->data, 4);\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\twhile (n > 0)\n\t\t{\n\t\ti=ssl3_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],n,0);\n\t\tif (i <= 0)\n\t\t\t{\n\t\t\ts->rwstate=SSL_READING;\n\t\t\t*ok = 0;\n\t\t\treturn i;\n\t\t\t}\n\t\ts->init_num += i;\n\t\tn -= i;\n\t\t}\n\tssl3_finish_mac(s, (unsigned char *)s->init_buf->data, s->init_num);\n\t*ok=1;\n\treturn s->init_num;\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\t*ok=0;\n\treturn(-1);\n\t}', 'static int ssl3_check_cert_and_algorithm(SSL *s)\n\t{\n\tint i,idx;\n\tlong algs;\n\tEVP_PKEY *pkey=NULL;\n\tSESS_CERT *sc;\n#ifndef OPENSSL_NO_RSA\n\tRSA *rsa;\n#endif\n#ifndef OPENSSL_NO_DH\n\tDH *dh;\n#endif\n\tsc=s->session->sess_cert;\n\tif (sc == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_INTERNAL_ERROR);\n\t\tgoto err;\n\t\t}\n\talgs=s->s3->tmp.new_cipher->algorithms;\n\tif (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))\n\t\treturn(1);\n#ifndef OPENSSL_NO_RSA\n\trsa=s->session->sess_cert->peer_rsa_tmp;\n#endif\n#ifndef OPENSSL_NO_DH\n\tdh=s->session->sess_cert->peer_dh_tmp;\n#endif\n\tidx=sc->peer_cert_type;\n\tpkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);\n\ti=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);\n\tEVP_PKEY_free(pkey);\n\tif ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);\n\t\tgoto f_err;\n\t\t}\n#ifndef OPENSSL_NO_DSA\n\telse if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);\n\t\tgoto f_err;\n\t\t}\n#endif\n#ifndef OPENSSL_NO_RSA\n\tif ((algs & SSL_kRSA) &&\n\t\t!(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);\n\t\tgoto f_err;\n\t\t}\n#endif\n#ifndef OPENSSL_NO_DH\n\tif ((algs & SSL_kEDH) &&\n\t\t!(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);\n\t\tgoto f_err;\n\t\t}\n\telse if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);\n\t\tgoto f_err;\n\t\t}\n#ifndef OPENSSL_NO_DSA\n\telse if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);\n\t\tgoto f_err;\n\t\t}\n#endif\n#endif\n\tif (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))\n\t\t{\n#ifndef OPENSSL_NO_RSA\n\t\tif (algs & SSL_kRSA)\n\t\t\t{\n\t\t\tif (rsa == NULL\n\t\t\t || RSA_size(rsa) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n#ifndef OPENSSL_NO_DH\n\t\t\tif (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))\n\t\t\t {\n\t\t\t if (dh == NULL\n\t\t\t\t|| DH_size(dh) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);\nerr:\n\treturn(0);\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,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, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tconst void *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((void *)ret);\n\t}']
35,731
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bn/bn_asm.c/#L535
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 test_mod_exp(BIO *bp, BN_CTX *ctx)\n\t{\n\tBIGNUM *a,*b,*c,*d,*e;\n\tint i;\n\ta=BN_new();\n\tb=BN_new();\n\tc=BN_new();\n\td=BN_new();\n\te=BN_new();\n\tBN_rand(c,30,0,1);\n\tfor (i=0; i<6; i++)\n\t\t{\n\t\tBN_rand(a,20+i*5,0,0);\n\t\tBN_rand(b,2+i,0,0);\n\t\tif (!BN_mod_exp(d,a,b,c,ctx))\n\t\t\treturn(00);\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,b);\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,d);\n\t\t\tBIO_puts(bp,"\\n");\n\t\t\t}\n\t\t}\n\tBN_free(a);\n\tBN_free(b);\n\tBN_free(c);\n\tBN_free(d);\n\tBN_free(e);\n\treturn(1);\n\t}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n\t{\n\tunsigned char *buf=NULL;\n\tint ret=0,bit,bytes,mask;\n\ttime_t tim;\n\tbytes=(bits+7)/8;\n\tbit=(bits-1)%8;\n\tmask=0xff<<bit;\n\tbuf=(unsigned char *)Malloc(bytes);\n\tif (buf == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_RAND,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\ttime(&tim);\n\tRAND_seed(&tim,sizeof(tim));\n\tRAND_bytes(buf,(int)bytes);\n\tif (top)\n\t\t{\n\t\tif (bit == 0)\n\t\t\t{\n\t\t\tbuf[0]=1;\n\t\t\tbuf[1]|=0x80;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbuf[0]|=(3<<(bit-1));\n\t\t\tbuf[0]&= ~(mask<<1);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tbuf[0]|=(1<<bit);\n\t\tbuf[0]&= ~(mask<<1);\n\t\t}\n\tif (bottom)\n\t\tbuf[bytes-1]|=1;\n\tif (!BN_bin2bn(buf,bytes,rnd)) goto err;\n\tret=1;\nerr:\n\tif (buf != NULL)\n\t\t{\n\t\tmemset(buf,0,bytes);\n\t\tFree(buf);\n\t\t}\n\treturn(ret);\n\t}', 'int BN_mod_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx)\n\t{\n\tint ret;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n#ifdef MONT_MUL_MOD\n\tif (BN_is_odd(m))\n\t\t{ ret=BN_mod_exp_mont(r,a,p,m,ctx,NULL); }\n\telse\n#endif\n#ifdef RECP_MUL_MOD\n\t\t{ ret=BN_mod_exp_recp(r,a,p,m,ctx); }\n#else\n\t\t{ ret=BN_mod_exp_simple(r,a,p,m,ctx); }\n#endif\n\treturn(ret);\n\t}', 'int BN_mod_exp_mont(BIGNUM *rr, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx,\n\t BN_MONT_CTX *in_mont)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1,ts=0;\n\tBIGNUM *d,*aa,*r;\n\tBIGNUM val[TABLE_SIZE];\n\tBN_MONT_CTX *mont=NULL;\n\tbn_check_top(a);\n\tbn_check_top(p);\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\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tBN_one(r);\n\t\treturn(1);\n\t\t}\n#if 1\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n#endif\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]);\n\tts=1;\n\tif (BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tBN_mod(&(val[0]),a,m,ctx);\n\t\taa= &(val[0]);\n\t\t}\n\telse\n\t\taa=a;\n\tif (!BN_to_montgomery(&(val[0]),aa,mont,ctx)) goto err;\n\tif (!BN_mod_mul_montgomery(d,&(val[0]),&(val[0]),mont,ctx)) goto err;\n\tif (bits <= 20)\n\t\twindow=1;\n\telse if (bits >= 256)\n\t\twindow=5;\n\telse if (bits >= 128)\n\t\twindow=4;\n\telse\n\t\twindow=3;\n\tj=1<<(window-1);\n\tfor (i=1; i<j; i++)\n\t\t{\n\t\tBN_init(&(val[i]));\n\t\tif (!BN_mod_mul_montgomery(&(val[i]),&(val[i-1]),d,mont,ctx))\n\t\t\tgoto err;\n\t\t}\n\tts=i;\n\tstart=1;\n\twvalue=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\tif (BN_is_bit_set(p,wstart) == 0)\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\tgoto err;\n\t\t\t\t}\n\t\t\tif (wstart == 0) break;\n\t\t\twstart--;\n\t\t\tcontinue;\n\t\t\t}\n\t\tj=wstart;\n\t\twvalue=1;\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\tif (BN_is_bit_set(p,wstart-i))\n\t\t\t\t{\n\t\t\t\twvalue<<=(i-wend);\n\t\t\t\twvalue|=1;\n\t\t\t\twend=i;\n\t\t\t\t}\n\t\t\t}\n\t\tj=wend+1;\n\t\tif (!start)\n\t\t\tfor (i=0; i<j; i++)\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 (!BN_mod_mul_montgomery(r,r,&(val[wvalue>>1]),mont,ctx))\n\t\t\tgoto err;\n\t\twstart-=wend+1;\n\t\twvalue=0;\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\tBN_clear_free(&(val[i]));\n\treturn(ret);\n\t}', 'int BN_mod(BIGNUM *rem, BIGNUM *m, BIGNUM *d, BN_CTX *ctx)\n\t{\n#if 0\n\tint i,nm,nd;\n\tBIGNUM *dv;\n\tif (BN_ucmp(m,d) < 0)\n\t\treturn((BN_copy(rem,m) == NULL)?0:1);\n\tdv= &(ctx->bn[ctx->tos]);\n\tif (!BN_copy(rem,m)) return(0);\n\tnm=BN_num_bits(rem);\n\tnd=BN_num_bits(d);\n\tif (!BN_lshift(dv,d,nm-nd)) return(0);\n\tfor (i=nm-nd; i>=0; i--)\n\t\t{\n\t\tif (BN_cmp(rem,dv) >= 0)\n\t\t\t{\n\t\t\tif (!BN_sub(rem,rem,dv)) return(0);\n\t\t\t}\n\t\tif (!BN_rshift1(dv,dv)) return(0);\n\t\t}\n\treturn(1);\n#else\n\treturn(BN_div(NULL,rem,m,d,ctx));\n#endif\n\t}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, BIGNUM *num, BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,j,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\tbn_check_top(num);\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 (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\ttmp= &(ctx->bn[ctx->tos]);\n\ttmp->neg=0;\n\tsnum= &(ctx->bn[ctx->tos+1]);\n\tsdiv= &(ctx->bn[ctx->tos+2]);\n\tif (dv == NULL)\n\t\tres= &(ctx->bn[ctx->tos+3]);\n\telse\tres=dv;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tBN_lshift(sdiv,divisor,norm_shift);\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tBN_lshift(snum,num,norm_shift);\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\tBN_init(&wnum);\n\twnum.d=\t &(snum->d[loop]);\n\twnum.top= div_n;\n\twnum.max= snum->max+1;\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;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tif (!BN_usub(&wnum,&wnum,sdiv)) goto err;\n\t\t*resp=1;\n\t\tres->d[res->top-1]=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tresp--;\n\tfor (i=0; i<loop-1; i++)\n\t\t{\n\t\tBN_ULONG q,n0,n1;\n\t\tBN_ULONG l0;\n\t\twnum.d--; wnum.top++;\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\tq=bn_div_words(n0,n1,d0);\n\t\t{\n#ifdef BN_LLONG\n\t\tBN_ULLONG t1,t2,rem;\n\t\tt1=((BN_ULLONG)n0<<BN_BITS2)|n1;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\trem=t1-(BN_ULLONG)q*d0;\n\t\t\tif ((rem>>BN_BITS2) ||\n\t\t\t\t(t2 <= ((BN_ULLONG)(rem<<BN_BITS2)+wnump[-2])))\n\t\t\t\tbreak;\n\t\t\tq--;\n\t\t\t}\n#else\n\t\tBN_ULONG t1l,t1h,t2l,t2h,t3l,t3h,ql,qh,t3t;\n\t\tt1h=n0;\n\t\tt1l=n1;\n\t\tfor (;;)\n\t\t\t{\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\tt3t=LBITS(d0); t3h=HBITS(d0);\n\t\t\tmul64(t3t,t3h,ql,qh);\n\t\t\tt3l=(t1l-t3t)&BN_MASK2;\n\t\t\tif (t3l > t1l) t3h++;\n\t\t\tt3h=(t1h-t3h)&BN_MASK2;\n\t\t\tif (t3h) break;\n\t\t\tif (t2h < t3l) break;\n\t\t\tif ((t2h == t3l) && (t2l <= wnump[-2])) break;\n\t\t\tq--;\n\t\t\t}\n#endif\n\t\t}\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\tfor (j=div_n+1; j>0; j--)\n\t\t\tif (tmp->d[j-1]) break;\n\t\ttmp->top=j;\n\t\tj=wnum.top;\n\t\tBN_sub(&wnum,&wnum,tmp);\n\t\tsnum->top=snum->top+wnum.top-j;\n\t\tif (wnum.neg)\n\t\t\t{\n\t\t\tq--;\n\t\t\tj=wnum.top;\n\t\t\tBN_add(&wnum,&wnum,sdiv);\n\t\t\tsnum->top+=wnum.top-j;\n\t\t\t}\n\t\t*(resp--)=q;\n\t\twnump--;\n\t\t}\n\tif (rm != NULL)\n\t\t{\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\trm->neg=num->neg;\n\t\t}\n\treturn(1);\nerr:\n\treturn(0);\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,732
0
https://github.com/openssl/openssl/blob/84c15db551ce1d167b901a3bde2b21880b084384/crypto/bn/bn_asm.c/#L161
BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w) { BN_ULONG c=0; BN_ULONG bl,bh; bn_check_num(num); if (num <= 0) return((BN_ULONG)0); bl=LBITS(w); bh=HBITS(w); for (;;) { mul_add(rp[0],ap[0],bl,bh,c); if (--num == 0) break; mul_add(rp[1],ap[1],bl,bh,c); if (--num == 0) break; mul_add(rp[2],ap[2],bl,bh,c); if (--num == 0) break; mul_add(rp[3],ap[3],bl,bh,c); if (--num == 0) break; ap+=4; rp+=4; } return(c); }
['int test_mont(BIO *bp, BN_CTX *ctx)\n\t{\n\tBIGNUM a,b,c,d,A,B;\n\tBIGNUM n;\n\tint i;\n\tint j;\n\tBN_MONT_CTX *mont;\n\tBN_init(&a);\n\tBN_init(&b);\n\tBN_init(&c);\n\tBN_init(&d);\n\tBN_init(&A);\n\tBN_init(&B);\n\tBN_init(&n);\n\tmont=BN_MONT_CTX_new();\n\tBN_rand(&a,100,0,0);\n\tBN_rand(&b,100,0,0);\n\tfor (i=0; i<10; i++)\n\t\t{\n\t\tBN_rand(&n,(100%BN_BITS2+1)*BN_BITS2*i*BN_BITS2,0,1);\n\t\tBN_MONT_CTX_set(mont,&n,ctx);\n\t\tBN_to_montgomery(&A,&a,mont,ctx);\n\t\tBN_to_montgomery(&B,&b,mont,ctx);\n\t\tif (bp == NULL)\n\t\t\tfor (j=0; j<100; j++)\n\t\t\t\tBN_mod_mul_montgomery(&c,&A,&B,mont,ctx);\n\t\tBN_mod_mul_montgomery(&c,&A,&B,mont,ctx);\n\t\tBN_from_montgomery(&A,&c,mont,ctx);\n\t\tif (bp != NULL)\n\t\t\t{\n\t\t\tif (!results)\n\t\t\t\t{\n#ifdef undef\nfprintf(stderr,"%d * %d %% %d\\n",\nBN_num_bits(&a),\nBN_num_bits(&b),\nBN_num_bits(mont->N));\n#endif\n\t\t\t\tBN_print(bp,&a);\n\t\t\t\tBIO_puts(bp," * ");\n\t\t\t\tBN_print(bp,&b);\n\t\t\t\tBIO_puts(bp," % ");\n\t\t\t\tBN_print(bp,&(mont->N));\n\t\t\t\tBIO_puts(bp," - ");\n\t\t\t\t}\n\t\t\tBN_print(bp,&A);\n\t\t\tBIO_puts(bp,"\\n");\n\t\t\t}\n\t\tBN_mod_mul(&d,&a,&b,&n,ctx);\n\t\tBN_sub(&d,&d,&A);\n\t\tif(!BN_is_zero(&d))\n\t\t {\n\t\t BIO_puts(bp,"Montgomery multiplication test failed!\\n");\n\t\t return 0;\n\t\t }\n\t\t}\n\tBN_MONT_CTX_free(mont);\n\tBN_free(&a);\n\tBN_free(&b);\n\tBN_free(&c);\n\tBN_free(&d);\n\tBN_free(&A);\n\tBN_free(&B);\n\tBN_free(&n);\n\treturn(1);\n\t}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n\t{\n\tunsigned char *buf=NULL;\n\tint ret=0,bit,bytes,mask;\n\ttime_t tim;\n\tbytes=(bits+7)/8;\n\tbit=(bits-1)%8;\n\tmask=0xff<<bit;\n\tbuf=(unsigned char *)Malloc(bytes);\n\tif (buf == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_RAND,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\ttime(&tim);\n\tRAND_seed(&tim,sizeof(tim));\n\tRAND_bytes(buf,(int)bytes);\n\tif (top)\n\t\t{\n\t\tif (bit == 0)\n\t\t\t{\n\t\t\tbuf[0]=1;\n\t\t\tbuf[1]|=0x80;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbuf[0]|=(3<<(bit-1));\n\t\t\tbuf[0]&= ~(mask<<1);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tbuf[0]|=(1<<bit);\n\t\tbuf[0]&= ~(mask<<1);\n\t\t}\n\tif (bottom)\n\t\tbuf[bytes-1]|=1;\n\tif (!BN_bin2bn(buf,bytes,rnd)) goto err;\n\tret=1;\nerr:\n\tif (buf != NULL)\n\t\t{\n\t\tmemset(buf,0,bytes);\n\t\tFree(buf);\n\t\t}\n\treturn(ret);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,\n\t\t\t BN_MONT_CTX *mont, 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_sqr(BIGNUM *r, BIGNUM *a, BN_CTX *ctx)\n\t{\n\tint max,al;\n\tBIGNUM *tmp,*rr;\n#ifdef BN_COUNT\nprintf("BN_sqr %d * %d\\n",a->top,a->top);\n#endif\n\tbn_check_top(a);\n\ttmp= &(ctx->bn[ctx->tos]);\n\trr=(a != r)?r: (&ctx->bn[ctx->tos+1]);\n\tal=a->top;\n\tif (al <= 0)\n\t\t{\n\t\tr->top=0;\n\t\treturn(1);\n\t\t}\n\tmax=(al+al);\n\tif (bn_wexpand(rr,max+1) == NULL) return(0);\n\tr->neg=0;\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(a,k*2) == NULL) return(0);\n\t\t\t\tif (bn_wexpand(tmp,k*2) == NULL) return(0);\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) return(0);\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) return(0);\n\t\tbn_sqr_normal(rr->d,a->d,al,tmp->d);\n#endif\n\t\t}\n\trr->top=max;\n\tif ((max > 0) && (rr->d[max-1] == 0)) rr->top--;\n\tif (rr != r) BN_copy(r,rr);\n\treturn(1);\n\t}', 'int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx)\n\t{\n\tBIGNUM *t;\n\tint r=0;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(m);\n\tt= &(ctx->bn[ctx->tos++]);\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_mod(ret,t,m,ctx)) goto err;\n\tr=1;\nerr:\n\tctx->tos--;\n\treturn(r);\n\t}', 'void bn_sqr_normal(BN_ULONG *r, BN_ULONG *a, int n, BN_ULONG *tmp)\n\t{\n\tint i,j,max;\n\tBN_ULONG *ap,*rp;\n\tmax=n*2;\n\tap=a;\n\trp=r;\n\trp[0]=rp[max-1]=0;\n\trp++;\n\tj=n;\n\tif (--j > 0)\n\t\t{\n\t\tap++;\n\t\trp[j]=bn_mul_words(rp,ap,j,ap[-1]);\n\t\trp+=2;\n\t\t}\n\tfor (i=n-2; i>0; i--)\n\t\t{\n\t\tj--;\n\t\tap++;\n\t\trp[j]=bn_mul_add_words(rp,ap,j,ap[-1]);\n\t\trp+=2;\n\t\t}\n\tbn_add_words(r,r,r,max);\n\tbn_sqr_words(tmp,a,n);\n\tbn_add_words(r,r,tmp,max);\n\t}', 'BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w)\n\t{\n\tBN_ULONG c=0;\n\tBN_ULONG bl,bh;\n\tbn_check_num(num);\n\tif (num <= 0) return((BN_ULONG)0);\n\tbl=LBITS(w);\n\tbh=HBITS(w);\n\tfor (;;)\n\t\t{\n\t\tmul_add(rp[0],ap[0],bl,bh,c);\n\t\tif (--num == 0) break;\n\t\tmul_add(rp[1],ap[1],bl,bh,c);\n\t\tif (--num == 0) break;\n\t\tmul_add(rp[2],ap[2],bl,bh,c);\n\t\tif (--num == 0) break;\n\t\tmul_add(rp[3],ap[3],bl,bh,c);\n\t\tif (--num == 0) break;\n\t\tap+=4;\n\t\trp+=4;\n\t\t}\n\treturn(c);\n\t}']
35,733
0
https://github.com/libav/libav/blob/556f8a066cb33241bf29e85d7e24c9acf7ea9043/libavcodec/h264_loopfilter.c/#L690
void ff_h264_filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) { MpegEncContext * const s = &h->s; const int mb_xy= mb_x + mb_y*s->mb_stride; const int mb_type = s->current_picture.mb_type[mb_xy]; const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4; int first_vertical_edge_done = 0; av_unused int dir; int chroma = !(CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY)); if (FRAME_MBAFF && IS_INTERLACED(mb_type^h->left_type[LTOP]) && h->left_type[LTOP]) { DECLARE_ALIGNED(8, int16_t, bS)[8]; int qp[2]; int bqp[2]; int rqp[2]; int mb_qp, mbn0_qp, mbn1_qp; int i; first_vertical_edge_done = 1; if( IS_INTRA(mb_type) ) { AV_WN64A(&bS[0], 0x0004000400040004ULL); AV_WN64A(&bS[4], 0x0004000400040004ULL); } else { static const uint8_t offset[2][2][8]={ { {3+4*0, 3+4*0, 3+4*0, 3+4*0, 3+4*1, 3+4*1, 3+4*1, 3+4*1}, {3+4*2, 3+4*2, 3+4*2, 3+4*2, 3+4*3, 3+4*3, 3+4*3, 3+4*3}, },{ {3+4*0, 3+4*1, 3+4*2, 3+4*3, 3+4*0, 3+4*1, 3+4*2, 3+4*3}, {3+4*0, 3+4*1, 3+4*2, 3+4*3, 3+4*0, 3+4*1, 3+4*2, 3+4*3}, } }; const uint8_t *off= offset[MB_FIELD][mb_y&1]; for( i = 0; i < 8; i++ ) { int j= MB_FIELD ? i>>2 : i&1; int mbn_xy = h->left_mb_xy[LEFT(j)]; int mbn_type= h->left_type[LEFT(j)]; if( IS_INTRA( mbn_type ) ) bS[i] = 4; else{ bS[i] = 1 + !!(h->non_zero_count_cache[12+8*(i>>1)] | ((!h->pps.cabac && IS_8x8DCT(mbn_type)) ? (h->cbp_table[mbn_xy] & (((MB_FIELD ? (i&2) : (mb_y&1)) ? 8 : 2) << 12)) : h->non_zero_count[mbn_xy][ off[i] ])); } } } mb_qp = s->current_picture.qscale_table[mb_xy]; mbn0_qp = s->current_picture.qscale_table[h->left_mb_xy[0]]; mbn1_qp = s->current_picture.qscale_table[h->left_mb_xy[1]]; qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1; bqp[0] = ( get_chroma_qp( h, 0, mb_qp ) + get_chroma_qp( h, 0, mbn0_qp ) + 1 ) >> 1; rqp[0] = ( get_chroma_qp( h, 1, mb_qp ) + get_chroma_qp( h, 1, mbn0_qp ) + 1 ) >> 1; qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1; bqp[1] = ( get_chroma_qp( h, 0, mb_qp ) + get_chroma_qp( h, 0, mbn1_qp ) + 1 ) >> 1; rqp[1] = ( get_chroma_qp( h, 1, mb_qp ) + get_chroma_qp( h, 1, mbn1_qp ) + 1 ) >> 1; tprintf(s->avctx, "filter mb:%d/%d MBAFF, QPy:%d/%d, QPb:%d/%d QPr:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], bqp[0], bqp[1], rqp[0], rqp[1], linesize, uvlinesize); { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); } if(MB_FIELD){ filter_mb_mbaff_edgev ( h, img_y , linesize, bS , 1, qp [0] ); filter_mb_mbaff_edgev ( h, img_y + 8* linesize, linesize, bS+4, 1, qp [1] ); if (chroma){ if (CHROMA444) { filter_mb_mbaff_edgev ( h, img_cb, uvlinesize, bS , 1, bqp[0] ); filter_mb_mbaff_edgev ( h, img_cb + 8*uvlinesize, uvlinesize, bS+4, 1, bqp[1] ); filter_mb_mbaff_edgev ( h, img_cr, uvlinesize, bS , 1, rqp[0] ); filter_mb_mbaff_edgev ( h, img_cr + 8*uvlinesize, uvlinesize, bS+4, 1, rqp[1] ); }else{ filter_mb_mbaff_edgecv( h, img_cb, uvlinesize, bS , 1, bqp[0] ); filter_mb_mbaff_edgecv( h, img_cb + 4*uvlinesize, uvlinesize, bS+4, 1, bqp[1] ); filter_mb_mbaff_edgecv( h, img_cr, uvlinesize, bS , 1, rqp[0] ); filter_mb_mbaff_edgecv( h, img_cr + 4*uvlinesize, uvlinesize, bS+4, 1, rqp[1] ); } } }else{ filter_mb_mbaff_edgev ( h, img_y , 2* linesize, bS , 2, qp [0] ); filter_mb_mbaff_edgev ( h, img_y + linesize, 2* linesize, bS+1, 2, qp [1] ); if (chroma){ if (CHROMA444) { filter_mb_mbaff_edgev ( h, img_cb, 2*uvlinesize, bS , 2, bqp[0] ); filter_mb_mbaff_edgev ( h, img_cb + uvlinesize, 2*uvlinesize, bS+1, 2, bqp[1] ); filter_mb_mbaff_edgev ( h, img_cr, 2*uvlinesize, bS , 2, rqp[0] ); filter_mb_mbaff_edgev ( h, img_cr + uvlinesize, 2*uvlinesize, bS+1, 2, rqp[1] ); }else{ filter_mb_mbaff_edgecv( h, img_cb, 2*uvlinesize, bS , 2, bqp[0] ); filter_mb_mbaff_edgecv( h, img_cb + uvlinesize, 2*uvlinesize, bS+1, 2, bqp[1] ); filter_mb_mbaff_edgecv( h, img_cr, 2*uvlinesize, bS , 2, rqp[0] ); filter_mb_mbaff_edgecv( h, img_cr + uvlinesize, 2*uvlinesize, bS+1, 2, rqp[1] ); } } } } #if CONFIG_SMALL for( dir = 0; dir < 2; dir++ ) filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, dir ? 0 : first_vertical_edge_done, chroma, CHROMA444, dir); #else filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, first_vertical_edge_done, chroma, CHROMA444, 0); filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, 0, chroma, CHROMA444, 1); #endif }
['void ff_h264_filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {\n MpegEncContext * const s = &h->s;\n const int mb_xy= mb_x + mb_y*s->mb_stride;\n const int mb_type = s->current_picture.mb_type[mb_xy];\n const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;\n int first_vertical_edge_done = 0;\n av_unused int dir;\n int chroma = !(CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));\n if (FRAME_MBAFF\n && IS_INTERLACED(mb_type^h->left_type[LTOP])\n && h->left_type[LTOP]) {\n DECLARE_ALIGNED(8, int16_t, bS)[8];\n int qp[2];\n int bqp[2];\n int rqp[2];\n int mb_qp, mbn0_qp, mbn1_qp;\n int i;\n first_vertical_edge_done = 1;\n if( IS_INTRA(mb_type) ) {\n AV_WN64A(&bS[0], 0x0004000400040004ULL);\n AV_WN64A(&bS[4], 0x0004000400040004ULL);\n } else {\n static const uint8_t offset[2][2][8]={\n {\n {3+4*0, 3+4*0, 3+4*0, 3+4*0, 3+4*1, 3+4*1, 3+4*1, 3+4*1},\n {3+4*2, 3+4*2, 3+4*2, 3+4*2, 3+4*3, 3+4*3, 3+4*3, 3+4*3},\n },{\n {3+4*0, 3+4*1, 3+4*2, 3+4*3, 3+4*0, 3+4*1, 3+4*2, 3+4*3},\n {3+4*0, 3+4*1, 3+4*2, 3+4*3, 3+4*0, 3+4*1, 3+4*2, 3+4*3},\n }\n };\n const uint8_t *off= offset[MB_FIELD][mb_y&1];\n for( i = 0; i < 8; i++ ) {\n int j= MB_FIELD ? i>>2 : i&1;\n int mbn_xy = h->left_mb_xy[LEFT(j)];\n int mbn_type= h->left_type[LEFT(j)];\n if( IS_INTRA( mbn_type ) )\n bS[i] = 4;\n else{\n bS[i] = 1 + !!(h->non_zero_count_cache[12+8*(i>>1)] |\n ((!h->pps.cabac && IS_8x8DCT(mbn_type)) ?\n (h->cbp_table[mbn_xy] & (((MB_FIELD ? (i&2) : (mb_y&1)) ? 8 : 2) << 12))\n :\n h->non_zero_count[mbn_xy][ off[i] ]));\n }\n }\n }\n mb_qp = s->current_picture.qscale_table[mb_xy];\n mbn0_qp = s->current_picture.qscale_table[h->left_mb_xy[0]];\n mbn1_qp = s->current_picture.qscale_table[h->left_mb_xy[1]];\n qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;\n bqp[0] = ( get_chroma_qp( h, 0, mb_qp ) +\n get_chroma_qp( h, 0, mbn0_qp ) + 1 ) >> 1;\n rqp[0] = ( get_chroma_qp( h, 1, mb_qp ) +\n get_chroma_qp( h, 1, mbn0_qp ) + 1 ) >> 1;\n qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;\n bqp[1] = ( get_chroma_qp( h, 0, mb_qp ) +\n get_chroma_qp( h, 0, mbn1_qp ) + 1 ) >> 1;\n rqp[1] = ( get_chroma_qp( h, 1, mb_qp ) +\n get_chroma_qp( h, 1, mbn1_qp ) + 1 ) >> 1;\n tprintf(s->avctx, "filter mb:%d/%d MBAFF, QPy:%d/%d, QPb:%d/%d QPr:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], bqp[0], bqp[1], rqp[0], rqp[1], linesize, uvlinesize);\n { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\\n"); }\n if(MB_FIELD){\n filter_mb_mbaff_edgev ( h, img_y , linesize, bS , 1, qp [0] );\n filter_mb_mbaff_edgev ( h, img_y + 8* linesize, linesize, bS+4, 1, qp [1] );\n if (chroma){\n if (CHROMA444) {\n filter_mb_mbaff_edgev ( h, img_cb, uvlinesize, bS , 1, bqp[0] );\n filter_mb_mbaff_edgev ( h, img_cb + 8*uvlinesize, uvlinesize, bS+4, 1, bqp[1] );\n filter_mb_mbaff_edgev ( h, img_cr, uvlinesize, bS , 1, rqp[0] );\n filter_mb_mbaff_edgev ( h, img_cr + 8*uvlinesize, uvlinesize, bS+4, 1, rqp[1] );\n }else{\n filter_mb_mbaff_edgecv( h, img_cb, uvlinesize, bS , 1, bqp[0] );\n filter_mb_mbaff_edgecv( h, img_cb + 4*uvlinesize, uvlinesize, bS+4, 1, bqp[1] );\n filter_mb_mbaff_edgecv( h, img_cr, uvlinesize, bS , 1, rqp[0] );\n filter_mb_mbaff_edgecv( h, img_cr + 4*uvlinesize, uvlinesize, bS+4, 1, rqp[1] );\n }\n }\n }else{\n filter_mb_mbaff_edgev ( h, img_y , 2* linesize, bS , 2, qp [0] );\n filter_mb_mbaff_edgev ( h, img_y + linesize, 2* linesize, bS+1, 2, qp [1] );\n if (chroma){\n if (CHROMA444) {\n filter_mb_mbaff_edgev ( h, img_cb, 2*uvlinesize, bS , 2, bqp[0] );\n filter_mb_mbaff_edgev ( h, img_cb + uvlinesize, 2*uvlinesize, bS+1, 2, bqp[1] );\n filter_mb_mbaff_edgev ( h, img_cr, 2*uvlinesize, bS , 2, rqp[0] );\n filter_mb_mbaff_edgev ( h, img_cr + uvlinesize, 2*uvlinesize, bS+1, 2, rqp[1] );\n }else{\n filter_mb_mbaff_edgecv( h, img_cb, 2*uvlinesize, bS , 2, bqp[0] );\n filter_mb_mbaff_edgecv( h, img_cb + uvlinesize, 2*uvlinesize, bS+1, 2, bqp[1] );\n filter_mb_mbaff_edgecv( h, img_cr, 2*uvlinesize, bS , 2, rqp[0] );\n filter_mb_mbaff_edgecv( h, img_cr + uvlinesize, 2*uvlinesize, bS+1, 2, rqp[1] );\n }\n }\n }\n }\n#if CONFIG_SMALL\n for( dir = 0; dir < 2; dir++ )\n filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, dir ? 0 : first_vertical_edge_done, chroma, CHROMA444, dir);\n#else\n filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, first_vertical_edge_done, chroma, CHROMA444, 0);\n filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, 0, chroma, CHROMA444, 1);\n#endif\n}']
35,734
0
https://github.com/libav/libav/blob/7fa70598e83cca650717d02ac96bcf55e9f97c19/libavformat/oggdec.c/#L188
static int ogg_new_buf(struct ogg *ogg, int idx) { struct ogg_stream *os = ogg->streams + idx; uint8_t *nb = av_malloc(os->bufsize); int size = os->bufpos - os->pstart; if(os->buf){ memcpy(nb, os->buf + os->pstart, size); av_free(os->buf); } os->buf = nb; os->bufpos = size; os->pstart = 0; return 0; }
['static int\nogg_new_buf(struct ogg *ogg, int idx)\n{\n struct ogg_stream *os = ogg->streams + idx;\n uint8_t *nb = av_malloc(os->bufsize);\n int size = os->bufpos - os->pstart;\n if(os->buf){\n memcpy(nb, os->buf + os->pstart, size);\n av_free(os->buf);\n }\n os->buf = nb;\n os->bufpos = size;\n os->pstart = 0;\n return 0;\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 posix_memalign(&ptr,16,size);\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
35,735
0
https://github.com/libav/libav/blob/63380b5e54f64abdde4a8b6bce0d60f1fa4a22a1/libavcodec/wma.c/#L486
int ff_wma_run_level_decode(AVCodecContext* avctx, GetBitContext* gb, VLC *vlc, const float *level_table, const uint16_t *run_table, int version, WMACoef *ptr, int offset, int num_coefs, int block_len, int frame_len_bits, int coef_nb_bits) { int code, level, sign; const uint32_t *ilvl = (const uint32_t*)level_table; uint32_t *iptr = (uint32_t*)ptr; const unsigned int coef_mask = block_len - 1; for (; offset < num_coefs; offset++) { code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX); if (code > 1) { offset += run_table[code]; sign = get_bits1(gb) - 1; iptr[offset & coef_mask] = ilvl[code] ^ sign<<31; } else if (code == 1) { break; } else { if (!version) { level = get_bits(gb, coef_nb_bits); offset += get_bits(gb, frame_len_bits); } else { level = ff_wma_get_large_val(gb); if (get_bits1(gb)) { if (get_bits1(gb)) { if (get_bits1(gb)) { av_log(avctx,AV_LOG_ERROR, "broken escape sequence\n"); return -1; } else offset += get_bits(gb, frame_len_bits) + 4; } else offset += get_bits(gb, 2) + 1; } } sign = get_bits1(gb) - 1; ptr[offset & coef_mask] = (level^sign) - sign; } } if (offset > num_coefs) { av_log(avctx, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n"); return -1; } return 0; }
['int ff_wma_run_level_decode(AVCodecContext* avctx, GetBitContext* gb,\n VLC *vlc,\n const float *level_table, const uint16_t *run_table,\n int version, WMACoef *ptr, int offset,\n int num_coefs, int block_len, int frame_len_bits,\n int coef_nb_bits)\n{\n int code, level, sign;\n const uint32_t *ilvl = (const uint32_t*)level_table;\n uint32_t *iptr = (uint32_t*)ptr;\n const unsigned int coef_mask = block_len - 1;\n for (; offset < num_coefs; offset++) {\n code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX);\n if (code > 1) {\n offset += run_table[code];\n sign = get_bits1(gb) - 1;\n iptr[offset & coef_mask] = ilvl[code] ^ sign<<31;\n } else if (code == 1) {\n break;\n } else {\n if (!version) {\n level = get_bits(gb, coef_nb_bits);\n offset += get_bits(gb, frame_len_bits);\n } else {\n level = ff_wma_get_large_val(gb);\n if (get_bits1(gb)) {\n if (get_bits1(gb)) {\n if (get_bits1(gb)) {\n av_log(avctx,AV_LOG_ERROR,\n "broken escape sequence\\n");\n return -1;\n } else\n offset += get_bits(gb, frame_len_bits) + 4;\n } else\n offset += get_bits(gb, 2) + 1;\n }\n }\n sign = get_bits1(gb) - 1;\n ptr[offset & coef_mask] = (level^sign) - sign;\n }\n }\n if (offset > num_coefs) {\n av_log(avctx, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\\n");\n return -1;\n }\n return 0;\n}', 'static inline unsigned int get_bits1(GetBitContext *s){\n#ifdef ALT_BITSTREAM_READER\n int index= s->index;\n uint8_t result= s->buffer[ index>>3 ];\n#ifdef ALT_BITSTREAM_READER_LE\n result>>= (index&0x07);\n result&= 1;\n#else\n result<<= (index&0x07);\n result>>= 8 - 1;\n#endif\n index++;\n s->index= index;\n return result;\n#else\n return get_bits(s, 1);\n#endif\n}']
35,736
0
https://github.com/openssl/openssl/blob/3fd5ece39b59d938d0cc84b8e5148d19044d15cf/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); }
['static int test_handshake_secrets(void)\n{\n SSL_CTX *ctx = NULL;\n SSL *s = NULL;\n int ret = 0;\n size_t hashsize;\n unsigned char out_master_secret[EVP_MAX_MD_SIZE];\n size_t master_secret_length;\n ctx = SSL_CTX_new(TLS_method());\n if (!TEST_ptr(ctx))\n goto err;\n s = SSL_new(ctx);\n if (!TEST_ptr(s ))\n goto err;\n s->session = SSL_SESSION_new();\n if (!TEST_ptr(s->session))\n goto err;\n if (!TEST_true(tls13_generate_secret(s, ssl_handshake_md(s), NULL, NULL, 0,\n (unsigned char *)&s->early_secret))) {\n TEST_info("Early secret generation failed");\n goto err;\n }\n if (!TEST_mem_eq(s->early_secret, sizeof(early_secret),\n early_secret, sizeof(early_secret))) {\n TEST_info("Early secret does not match");\n goto err;\n }\n if (!TEST_true(tls13_generate_handshake_secret(s, ecdhe_secret,\n sizeof(ecdhe_secret)))) {\n TEST_info("Hanshake secret generation failed");\n goto err;\n }\n if (!TEST_mem_eq(s->handshake_secret, sizeof(handshake_secret),\n handshake_secret, sizeof(handshake_secret)))\n goto err;\n hashsize = EVP_MD_size(ssl_handshake_md(s));\n if (!TEST_size_t_eq(sizeof(client_hts), hashsize))\n goto err;\n if (!TEST_size_t_eq(sizeof(client_hts_key), KEYLEN))\n goto err;\n if (!TEST_size_t_eq(sizeof(client_hts_iv), IVLEN))\n goto err;\n if (!TEST_true(test_secret(s, s->handshake_secret,\n (unsigned char *)client_hts_label,\n strlen(client_hts_label), client_hts,\n client_hts_key, client_hts_iv))) {\n TEST_info("Client handshake secret test failed");\n goto err;\n }\n if (!TEST_size_t_eq(sizeof(server_hts), hashsize))\n goto err;\n if (!TEST_size_t_eq(sizeof(server_hts_key), KEYLEN))\n goto err;\n if (!TEST_size_t_eq(sizeof(server_hts_iv), IVLEN))\n goto err;\n if (!TEST_true(test_secret(s, s->handshake_secret,\n (unsigned char *)server_hts_label,\n strlen(server_hts_label), server_hts,\n server_hts_key, server_hts_iv))) {\n TEST_info("Server handshake secret test failed");\n goto err;\n }\n full_hash = 1;\n if (!TEST_true(tls13_generate_master_secret(s, out_master_secret,\n s->handshake_secret, hashsize,\n &master_secret_length))) {\n TEST_info("Master secret generation failed");\n goto err;\n }\n if (!TEST_mem_eq(out_master_secret, master_secret_length,\n master_secret, sizeof(master_secret))) {\n TEST_info("Master secret does not match");\n goto err;\n }\n if (!TEST_size_t_eq(sizeof(client_ats), hashsize))\n goto err;\n if (!TEST_size_t_eq(sizeof(client_ats_key), KEYLEN))\n goto err;\n if (!TEST_size_t_eq(sizeof(client_ats_iv), IVLEN))\n goto err;\n if (!TEST_true(test_secret(s, out_master_secret,\n (unsigned char *)client_ats_label,\n strlen(client_ats_label), client_ats,\n client_ats_key, client_ats_iv))) {\n TEST_info("Client application data secret test failed");\n goto err;\n }\n if (!TEST_size_t_eq(sizeof(server_ats), hashsize))\n goto err;\n if (!TEST_size_t_eq(sizeof(server_ats_key), KEYLEN))\n goto err;\n if (!TEST_size_t_eq(sizeof(server_ats_iv), IVLEN))\n goto err;\n if (!TEST_true(test_secret(s, out_master_secret,\n (unsigned char *)server_ats_label,\n strlen(server_ats_label), server_ats,\n server_ats_key, server_ats_iv))) {\n TEST_info("Server application data secret test failed");\n goto err;\n }\n ret = 1;\n err:\n SSL_free(s);\n SSL_CTX_free(ctx);\n return ret;\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->dane.flags = ctx->dane.flags;\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->max_early_data = ctx->max_early_data;\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->ext.debug_cb = 0;\n s->ext.debug_arg = NULL;\n s->ext.ticket_expected = 0;\n s->ext.status_type = ctx->ext.status_type;\n s->ext.status_expected = 0;\n s->ext.ocsp.ids = NULL;\n s->ext.ocsp.exts = NULL;\n s->ext.ocsp.resp = NULL;\n s->ext.ocsp.resp_len = 0;\n SSL_CTX_up_ref(ctx);\n s->session_ctx = ctx;\n#ifndef OPENSSL_NO_EC\n if (ctx->ext.ecpointformats) {\n s->ext.ecpointformats =\n OPENSSL_memdup(ctx->ext.ecpointformats,\n ctx->ext.ecpointformats_len);\n if (!s->ext.ecpointformats)\n goto err;\n s->ext.ecpointformats_len =\n ctx->ext.ecpointformats_len;\n }\n if (ctx->ext.supportedgroups) {\n s->ext.supportedgroups =\n OPENSSL_memdup(ctx->ext.supportedgroups,\n ctx->ext.supportedgroups_len);\n if (!s->ext.supportedgroups)\n goto err;\n s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n s->ext.npn = NULL;\n#endif\n if (s->ctx->ext.alpn) {\n s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);\n if (s->ext.alpn == NULL)\n goto err;\n memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);\n s->ext.alpn_len = s->ctx->ext.alpn_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 s->key_update = SSL_KEY_UPDATE_NONE;\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_DOWN_REF(&s->references, &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 ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n BIO_free_all(s->rbio);\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->ext.hostname);\n SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->ext.ecpointformats);\n OPENSSL_free(s->ext.supportedgroups);\n#endif\n sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->ext.scts);\n#endif\n OPENSSL_free(s->ext.ocsp.resp);\n OPENSSL_free(s->ext.alpn);\n OPENSSL_free(s->ext.tls13_cookie);\n OPENSSL_free(s->clienthello);\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->ext.npn);\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}', 'int ssl_clear_bad_session(SSL *s)\n{\n if ((s->session != NULL) &&\n !(s->shutdown & SSL_SENT_SHUTDOWN) &&\n !(SSL_in_init(s) || SSL_in_before(s))) {\n SSL_CTX_remove_session(s->session_ctx, s->session);\n return (1);\n } else\n return (0);\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,737
0
https://github.com/openssl/openssl/blob/ff64702b3d83d4c77756e0fd7b624e2165dbbdf0/crypto/packet.c/#L52
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) { if (!ossl_assert(pkt->subs != NULL && len != 0)) return 0; if (pkt->maxsize - pkt->written < len) return 0; if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) { size_t newlen; size_t reflen; reflen = (len > pkt->buf->length) ? len : pkt->buf->length; if (reflen > SIZE_MAX / 2) { newlen = SIZE_MAX; } else { newlen = reflen * 2; if (newlen < DEFAULT_BUF_SIZE) newlen = DEFAULT_BUF_SIZE; } if (BUF_MEM_grow(pkt->buf, newlen) == 0) return 0; } if (allocbytes != NULL) *allocbytes = WPACKET_get_curr(pkt); return 1; }
['EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL *s, WPACKET *pkt,\n unsigned int context, X509 *x,\n size_t chainidx)\n{\n if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session))\n return EXT_RETURN_NOT_SENT;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode)\n || !WPACKET_close(pkt)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_TLS_CONSTRUCT_STOC_MAXFRAGMENTLEN, ERR_R_INTERNAL_ERROR);\n return EXT_RETURN_FAIL;\n }\n return EXT_RETURN_SENT;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n WPACKET_SUB *sub;\n unsigned char *lenchars;\n if (!ossl_assert(pkt->subs != NULL))\n return 0;\n if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {\n SSLerr(SSL_F_WPACKET_START_SUB_PACKET_LEN__, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n sub->parent = pkt->subs;\n pkt->subs = sub;\n sub->pwritten = pkt->written + lenbytes;\n sub->lenbytes = lenbytes;\n if (lenbytes == 0) {\n sub->packet_len = 0;\n return 1;\n }\n sub->packet_len = pkt->written;\n if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n return 0;\n return 1;\n}', 'int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)\n{\n unsigned char *data;\n if (!ossl_assert(size <= sizeof(unsigned int))\n || !WPACKET_allocate_bytes(pkt, size, &data)\n || !put_value(data, val, size))\n return 0;\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!ossl_assert(pkt->subs != NULL && len != 0))\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {\n size_t newlen;\n size_t reflen;\n reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n if (reflen > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = reflen * 2;\n if (newlen < DEFAULT_BUF_SIZE)\n newlen = DEFAULT_BUF_SIZE;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n if (allocbytes != NULL)\n *allocbytes = WPACKET_get_curr(pkt);\n return 1;\n}']
35,738
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavutil/rc4.c/#L44
void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen) { int i, j; uint8_t x, y; uint8_t state[256]; for (i = 0; i < 256; i++) state[i] = i; y = 0; for (j = 0, i = 0; i < 256; i++, j++) { if (j == keylen) j = 0; y += state[i] + key[j]; FFSWAP(uint8_t, state[i], state[y]); } x = 1; y = state[1]; while (datalen-- > 0) { uint8_t sum = state[x] + state[y]; FFSWAP(uint8_t, state[x], state[y]); *data++ ^= state[sum]; x++; y += state[x]; } }
['void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen) {\n int i, j;\n uint8_t x, y;\n uint8_t state[256];\n for (i = 0; i < 256; i++)\n state[i] = i;\n y = 0;\n for (j = 0, i = 0; i < 256; i++, j++) {\n if (j == keylen) j = 0;\n y += state[i] + key[j];\n FFSWAP(uint8_t, state[i], state[y]);\n }\n x = 1; y = state[1];\n while (datalen-- > 0) {\n uint8_t sum = state[x] + state[y];\n FFSWAP(uint8_t, state[x], state[y]);\n *data++ ^= state[sum];\n x++;\n y += state[x];\n }\n}']
35,739
0
https://github.com/openssl/openssl/blob/9b95f1df4b575daa8e59722454202fd6dc7027b2/crypto/bn/bn_asm.c/#L401
BN_ULONG bn_sub_words(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n) { BN_ULONG t1,t2; int c=0; assert(n >= 0); if (n <= 0) return((BN_ULONG)0); for (;;) { t1=a[0]; t2=b[0]; r[0]=(t1-t2-c)&BN_MASK2; if (t1 != t2) c=(t1 < t2); if (--n <= 0) break; t1=a[1]; t2=b[1]; r[1]=(t1-t2-c)&BN_MASK2; if (t1 != t2) c=(t1 < t2); if (--n <= 0) break; t1=a[2]; t2=b[2]; r[2]=(t1-t2-c)&BN_MASK2; if (t1 != t2) c=(t1 < t2); if (--n <= 0) break; t1=a[3]; t2=b[3]; r[3]=(t1-t2-c)&BN_MASK2; if (t1 != t2) c=(t1 < t2); if (--n <= 0) break; a+=4; b+=4; r+=4; } return(c); }
['void bn_sqr_recursive(BN_ULONG *r, BN_ULONG *a, int n2, BN_ULONG *t)\n\t{\n\tint n=n2/2;\n\tint zero,c1;\n\tBN_ULONG ln,lo,*p;\n#ifdef BN_COUNT\nprintf(" bn_sqr_recursive %d * %d\\n",n2,n2);\n#endif\n\tif (n2 == 4)\n\t\t{\n#ifndef BN_SQR_COMBA\n\t\tbn_sqr_normal(r,a,4,t);\n#else\n\t\tbn_sqr_comba4(r,a);\n#endif\n\t\treturn;\n\t\t}\n\telse if (n2 == 8)\n\t\t{\n#ifndef BN_SQR_COMBA\n\t\tbn_sqr_normal(r,a,8,t);\n#else\n\t\tbn_sqr_comba8(r,a);\n#endif\n\t\treturn;\n\t\t}\n\tif (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL)\n\t\t{\n\t\tbn_sqr_normal(r,a,n2,t);\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_words(a,&(a[n]),n);\n\tzero=0;\n\tif (c1 > 0)\n\t\tbn_sub_words(t,a,&(a[n]),n);\n\telse if (c1 < 0)\n\t\tbn_sub_words(t,&(a[n]),a,n);\n\telse\n\t\tzero=1;\n\tp= &(t[n2*2]);\n\tif (!zero)\n\t\tbn_sqr_recursive(&(t[n2]),t,n,p);\n\telse\n\t\tmemset(&(t[n2]),0,n*sizeof(BN_ULONG));\n\tbn_sqr_recursive(r,a,n,p);\n\tbn_sqr_recursive(&(r[n2]),&(a[n]),n,p);\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\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}', 'BN_ULONG bn_add_words(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)\n {\n\tBN_ULONG c,l,t;\n\tassert(n >= 0);\n\tif (n <= 0) return((BN_ULONG)0);\n\tc=0;\n\tfor (;;)\n\t\t{\n\t\tt=a[0];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[0])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[0]=l;\n\t\tif (--n <= 0) break;\n\t\tt=a[1];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[1])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[1]=l;\n\t\tif (--n <= 0) break;\n\t\tt=a[2];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[2])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[2]=l;\n\t\tif (--n <= 0) break;\n\t\tt=a[3];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[3])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[3]=l;\n\t\tif (--n <= 0) break;\n\t\ta+=4;\n\t\tb+=4;\n\t\tr+=4;\n\t\t}\n\treturn((BN_ULONG)c);\n\t}', 'BN_ULONG bn_sub_words(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)\n {\n\tBN_ULONG t1,t2;\n\tint c=0;\n\tassert(n >= 0);\n\tif (n <= 0) return((BN_ULONG)0);\n\tfor (;;)\n\t\t{\n\t\tt1=a[0]; t2=b[0];\n\t\tr[0]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tif (--n <= 0) break;\n\t\tt1=a[1]; t2=b[1];\n\t\tr[1]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tif (--n <= 0) break;\n\t\tt1=a[2]; t2=b[2];\n\t\tr[2]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tif (--n <= 0) break;\n\t\tt1=a[3]; t2=b[3];\n\t\tr[3]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tif (--n <= 0) break;\n\t\ta+=4;\n\t\tb+=4;\n\t\tr+=4;\n\t\t}\n\treturn(c);\n\t}']
35,740
0
https://github.com/openssl/openssl/blob/3ec9e4ec46eb4356bc106db5e0e33148c693c8f0/crypto/lhash/lhash.c/#L139
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; }
['SSL *SSL_dup(SSL *s)\n{\n STACK_OF(X509_NAME) *sk;\n X509_NAME *xn;\n SSL *ret;\n int i;\n if (!SSL_in_init(s) || !SSL_in_before(s)) {\n CRYPTO_UP_REF(&s->references, &i, s->lock);\n return s;\n }\n if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)\n return NULL;\n if (s->session != NULL) {\n if (!SSL_copy_session_id(ret, s))\n goto err;\n } else {\n if (!SSL_set_ssl_method(ret, s->method))\n goto err;\n if (s->cert != NULL) {\n ssl_cert_free(ret->cert);\n ret->cert = ssl_cert_dup(s->cert);\n if (ret->cert == NULL)\n goto err;\n }\n if (!SSL_set_session_id_context(ret, s->sid_ctx,\n (int)s->sid_ctx_length))\n goto err;\n }\n if (!ssl_dane_dup(ret, s))\n goto err;\n ret->version = s->version;\n ret->options = s->options;\n ret->mode = s->mode;\n SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));\n SSL_set_read_ahead(ret, SSL_get_read_ahead(s));\n ret->msg_callback = s->msg_callback;\n ret->msg_callback_arg = s->msg_callback_arg;\n SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));\n SSL_set_verify_depth(ret, SSL_get_verify_depth(s));\n ret->generate_session_id = s->generate_session_id;\n SSL_set_info_callback(ret, SSL_get_info_callback(s));\n if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))\n goto err;\n if (s->rbio != NULL) {\n if (!BIO_dup_state(s->rbio, (char *)&ret->rbio))\n goto err;\n }\n if (s->wbio != NULL) {\n if (s->wbio != s->rbio) {\n if (!BIO_dup_state(s->wbio, (char *)&ret->wbio))\n goto err;\n } else {\n BIO_up_ref(ret->rbio);\n ret->wbio = ret->rbio;\n }\n }\n ret->server = s->server;\n if (s->handshake_func) {\n if (s->server)\n SSL_set_accept_state(ret);\n else\n SSL_set_connect_state(ret);\n }\n ret->shutdown = s->shutdown;\n ret->hit = s->hit;\n ret->default_passwd_callback = s->default_passwd_callback;\n ret->default_passwd_callback_userdata = s->default_passwd_callback_userdata;\n X509_VERIFY_PARAM_inherit(ret->param, s->param);\n if (s->cipher_list != NULL) {\n if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)\n goto err;\n }\n if (s->cipher_list_by_id != NULL)\n if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id))\n == NULL)\n goto err;\n if (s->ca_names != NULL) {\n if ((sk = sk_X509_NAME_dup(s->ca_names)) == NULL)\n goto err;\n ret->ca_names = sk;\n for (i = 0; i < sk_X509_NAME_num(sk); i++) {\n xn = sk_X509_NAME_value(sk, i);\n if (sk_X509_NAME_set(sk, i, X509_NAME_dup(xn)) == NULL) {\n X509_NAME_free(xn);\n goto err;\n }\n }\n }\n return ret;\n err:\n SSL_free(ret);\n return NULL;\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->references = 1;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n OPENSSL_free(s);\n s = NULL;\n goto err;\n }\n if (RAND_get_rand_method() == RAND_OpenSSL()) {\n s->drbg =\n RAND_DRBG_new(0, 0, RAND_DRBG_get0_public());\n if (s->drbg == NULL\n || RAND_DRBG_instantiate(s->drbg,\n (const unsigned char *) SSL_version_str,\n sizeof(SSL_version_str) - 1) == 0)\n goto err;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\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->max_early_data = ctx->max_early_data;\n s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);\n if (s->tls13_ciphersuites == NULL)\n goto err;\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->record_padding_cb = ctx->record_padding_cb;\n s->record_padding_arg = ctx->record_padding_arg;\n s->block_padding = ctx->block_padding;\n s->sid_ctx_length = ctx->sid_ctx_length;\n if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))\n goto err;\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->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;\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->ext.debug_cb = 0;\n s->ext.debug_arg = NULL;\n s->ext.ticket_expected = 0;\n s->ext.status_type = ctx->ext.status_type;\n s->ext.status_expected = 0;\n s->ext.ocsp.ids = NULL;\n s->ext.ocsp.exts = NULL;\n s->ext.ocsp.resp = NULL;\n s->ext.ocsp.resp_len = 0;\n SSL_CTX_up_ref(ctx);\n s->session_ctx = ctx;\n#ifndef OPENSSL_NO_EC\n if (ctx->ext.ecpointformats) {\n s->ext.ecpointformats =\n OPENSSL_memdup(ctx->ext.ecpointformats,\n ctx->ext.ecpointformats_len);\n if (!s->ext.ecpointformats)\n goto err;\n s->ext.ecpointformats_len =\n ctx->ext.ecpointformats_len;\n }\n if (ctx->ext.supportedgroups) {\n s->ext.supportedgroups =\n OPENSSL_memdup(ctx->ext.supportedgroups,\n ctx->ext.supportedgroups_len\n * sizeof(*ctx->ext.supportedgroups));\n if (!s->ext.supportedgroups)\n goto err;\n s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n s->ext.npn = NULL;\n#endif\n if (s->ctx->ext.alpn) {\n s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);\n if (s->ext.alpn == NULL)\n goto err;\n memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);\n s->ext.alpn_len = s->ctx->ext.alpn_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 s->key_update = SSL_KEY_UPDATE_NONE;\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->psk_find_session_cb = ctx->psk_find_session_cb;\n s->psk_use_session_cb = ctx->psk_use_session_cb;\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_DOWN_REF(&s->references, &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 ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n BIO_free_all(s->rbio);\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 sk_SSL_CIPHER_free(s->tls13_ciphersuites);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n SSL_SESSION_free(s->psksession);\n OPENSSL_free(s->psksession_id);\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->ext.hostname);\n SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->ext.ecpointformats);\n OPENSSL_free(s->ext.supportedgroups);\n#endif\n sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->ext.scts);\n#endif\n OPENSSL_free(s->ext.ocsp.resp);\n OPENSSL_free(s->ext.alpn);\n OPENSSL_free(s->ext.tls13_cookie);\n OPENSSL_free(s->clienthello);\n OPENSSL_free(s->pha_context);\n EVP_MD_CTX_free(s->pha_dgst);\n sk_X509_NAME_pop_free(s->ca_names, 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->ext.npn);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n RAND_DRBG_free(s->drbg);\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', 'int ssl_clear_bad_session(SSL *s)\n{\n if ((s->session != NULL) &&\n !(s->shutdown & SSL_SENT_SHUTDOWN) &&\n !(SSL_in_init(s) || SSL_in_before(s))) {\n SSL_CTX_remove_session(s->session_ctx, s->session);\n return 1;\n } else\n return 0;\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)) != NULL) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, r);\n SSL_SESSION_list_remove(ctx, r);\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,741
0
https://github.com/libav/libav/blob/f40f329e9219a8dd7e585345a8ea294fa66562b9/libavcodec/mpegaudiodec.c/#L896
void RENAME(ff_mpa_synth_filter)(MPA_INT *synth_buf_ptr, int *synth_buf_offset, MPA_INT *window, int *dither_state, OUT_INT *samples, int incr, INTFLOAT sb_samples[SBLIMIT]) { register MPA_INT *synth_buf; register const MPA_INT *w, *w2, *p; int j, offset; OUT_INT *samples2; #if CONFIG_FLOAT float sum, sum2; #elif FRAC_BITS <= 15 int32_t tmp[32]; int sum, sum2; #else int64_t sum, sum2; #endif offset = *synth_buf_offset; synth_buf = synth_buf_ptr + offset; #if FRAC_BITS <= 15 && !CONFIG_FLOAT dct32(tmp, sb_samples); for(j=0;j<32;j++) { synth_buf[j] = av_clip_int16(tmp[j]); } #else dct32(synth_buf, sb_samples); #endif memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf)); samples2 = samples + 31 * incr; w = window; w2 = window + 31; sum = *dither_state; p = synth_buf + 16; SUM8(MACS, sum, w, p); p = synth_buf + 48; SUM8(MLSS, sum, w + 32, p); *samples = round_sample(&sum); samples += incr; w++; for(j=1;j<16;j++) { sum2 = 0; p = synth_buf + 16 + j; SUM8P2(sum, MACS, sum2, MLSS, w, w2, p); p = synth_buf + 48 - j; SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p); *samples = round_sample(&sum); samples += incr; sum += sum2; *samples2 = round_sample(&sum); samples2 -= incr; w++; w2--; } p = synth_buf + 32; SUM8(MLSS, sum, w + 32, p); *samples = round_sample(&sum); *dither_state= sum; offset = (offset - 32) & 511; *synth_buf_offset = offset; }
['static void mpc_synth(MPCContext *c, int16_t *out)\n{\n int dither_state = 0;\n int i, ch;\n OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE], *samples_ptr;\n for(ch = 0; ch < 2; ch++){\n samples_ptr = samples + ch;\n for(i = 0; i < SAMPLES_PER_BAND; i++) {\n ff_mpa_synth_filter(c->synth_buf[ch], &(c->synth_buf_offset[ch]),\n ff_mpa_synth_window, &dither_state,\n samples_ptr, 2,\n c->sb_samples[ch][i]);\n samples_ptr += 64;\n }\n }\n for(i = 0; i < MPC_FRAME_SIZE*2; i++)\n *out++=samples[i];\n}', 'void RENAME(ff_mpa_synth_filter)(MPA_INT *synth_buf_ptr, int *synth_buf_offset,\n MPA_INT *window, int *dither_state,\n OUT_INT *samples, int incr,\n INTFLOAT sb_samples[SBLIMIT])\n{\n register MPA_INT *synth_buf;\n register const MPA_INT *w, *w2, *p;\n int j, offset;\n OUT_INT *samples2;\n#if CONFIG_FLOAT\n float sum, sum2;\n#elif FRAC_BITS <= 15\n int32_t tmp[32];\n int sum, sum2;\n#else\n int64_t sum, sum2;\n#endif\n offset = *synth_buf_offset;\n synth_buf = synth_buf_ptr + offset;\n#if FRAC_BITS <= 15 && !CONFIG_FLOAT\n dct32(tmp, sb_samples);\n for(j=0;j<32;j++) {\n synth_buf[j] = av_clip_int16(tmp[j]);\n }\n#else\n dct32(synth_buf, sb_samples);\n#endif\n memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));\n samples2 = samples + 31 * incr;\n w = window;\n w2 = window + 31;\n sum = *dither_state;\n p = synth_buf + 16;\n SUM8(MACS, sum, w, p);\n p = synth_buf + 48;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n w++;\n for(j=1;j<16;j++) {\n sum2 = 0;\n p = synth_buf + 16 + j;\n SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);\n p = synth_buf + 48 - j;\n SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n sum += sum2;\n *samples2 = round_sample(&sum);\n samples2 -= incr;\n w++;\n w2--;\n }\n p = synth_buf + 32;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n *dither_state= sum;\n offset = (offset - 32) & 511;\n *synth_buf_offset = offset;\n}']
35,742
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bn/bn_lib.c/#L246
int BN_num_bits(BIGNUM *a) { BN_ULONG l; int i; bn_check_top(a); if (a->top == 0) return(0); l=a->d[a->top-1]; i=(a->top-1)*BN_BITS2; if (l == 0) { #if !defined(NO_STDIO) && !defined(WIN16) fprintf(stderr,"BAD TOP VALUE\n"); #endif abort(); } return(i+BN_num_bits_word(l)); }
['EVP_PKEY *EVP_PKCS82PKEY (PKCS8_PRIV_KEY_INFO *p8)\n{\n\tEVP_PKEY *pkey;\n\tRSA *rsa;\n\tDSA *dsa;\n\tASN1_INTEGER *dsapriv;\n\tX509_ALGOR *a;\n\tSTACK *ndsa;\n\tBN_CTX *ctx;\n\tunsigned char *p;\n\tint plen, pkeylen;\n\tchar obj_tmp[80];\n\tswitch (p8->broken) {\n\t\tcase PKCS8_OK:\n\t\tp = p8->pkey->value.octet_string->data;\n\t\tpkeylen = p8->pkey->value.octet_string->length;\n\t\tbreak;\n\t\tcase PKCS8_NO_OCTET:\n\t\tp = p8->pkey->value.sequence->data;\n\t\tpkeylen = p8->pkey->value.sequence->length;\n\t\tbreak;\n\t\tdefault:\n\t\tEVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE);\n\t\treturn NULL;\n\t\tbreak;\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\tswitch (OBJ_obj2nid(a->algorithm))\n\t{\n\t\tcase NID_rsaEncryption:\n\t\tif (!(rsa = d2i_RSAPrivateKey (NULL, &p, 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\t\tcase NID_dsa:\n\t\tif(*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED)) {\n\t\t if(!(ndsa = ASN1_seq_unpack(p, pkeylen,\n\t\t\t\t\t(char *(*)())d2i_ASN1_INTEGER,\n\t\t\t\t\t\t\t ASN1_STRING_free))) {\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\treturn NULL;\n\t\t }\n\t\t if(sk_num(ndsa) != 2 ) {\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\tsk_pop_free(ndsa, ASN1_STRING_free);\n\t\t\treturn NULL;\n\t\t }\n\t\t dsapriv = (ASN1_INTEGER *) sk_pop(ndsa);\n\t\t sk_pop_free(ndsa, ASN1_STRING_free);\n\t\t} else if (!(dsapriv=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) {\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\treturn NULL;\n\t\t}\n\t\tif (a->parameter->type != V_ASN1_SEQUENCE) {\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_NO_DSA_PARAMETERS);\n\t\t\treturn NULL;\n\t\t}\n\t\tp = a->parameter->value.sequence->data;\n\t\tplen = a->parameter->value.sequence->length;\n\t\tif (!(dsa = d2i_DSAparams (NULL, &p, plen))) {\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);\n\t\t\treturn NULL;\n\t\t}\n\t\tif (!(dsa->priv_key = ASN1_INTEGER_to_BN(dsapriv, NULL))) {\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_DECODE_ERROR);\n\t\t\tDSA_free (dsa);\n\t\t\treturn NULL;\n\t\t}\n\t\tif (!(dsa->pub_key = BN_new())) {\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);\n\t\t\tDSA_free (dsa);\n\t\t\treturn NULL;\n\t\t}\n\t\tif (!(ctx = BN_CTX_new())) {\n\t\t\tEVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);\n\t\t\tDSA_free (dsa);\n\t\t\treturn NULL;\n\t\t}\n\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\tEVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_PUBKEY_ERROR);\n\t\t\tBN_CTX_free (ctx);\n\t\t\tDSA_free (dsa);\n\t\t\treturn NULL;\n\t\t}\n\t\tEVP_PKEY_assign_DSA (pkey, dsa);\n\t\tBN_CTX_free (ctx);\n\t\tbreak;\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}', 'BIGNUM *BN_new(void)\n\t{\n\tBIGNUM *ret;\n\tif ((ret=(BIGNUM *)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->max=0;\n\tret->d=NULL;\n\treturn(ret);\n\t}', 'int BN_mod_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx)\n\t{\n\tint ret;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n#ifdef MONT_MUL_MOD\n\tif (BN_is_odd(m))\n\t\t{ ret=BN_mod_exp_mont(r,a,p,m,ctx,NULL); }\n\telse\n#endif\n#ifdef RECP_MUL_MOD\n\t\t{ ret=BN_mod_exp_recp(r,a,p,m,ctx); }\n#else\n\t\t{ ret=BN_mod_exp_simple(r,a,p,m,ctx); }\n#endif\n\treturn(ret);\n\t}', 'int BN_mod_exp_mont(BIGNUM *rr, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx,\n\t BN_MONT_CTX *in_mont)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1,ts=0;\n\tBIGNUM *d,*aa,*r;\n\tBIGNUM val[TABLE_SIZE];\n\tBN_MONT_CTX *mont=NULL;\n\tbn_check_top(a);\n\tbn_check_top(p);\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\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tBN_one(r);\n\t\treturn(1);\n\t\t}\n#if 1\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n#endif\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]);\n\tts=1;\n\tif (BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tBN_mod(&(val[0]),a,m,ctx);\n\t\taa= &(val[0]);\n\t\t}\n\telse\n\t\taa=a;\n\tif (!BN_to_montgomery(&(val[0]),aa,mont,ctx)) goto err;\n\tif (!BN_mod_mul_montgomery(d,&(val[0]),&(val[0]),mont,ctx)) goto err;\n\tif (bits <= 20)\n\t\twindow=1;\n\telse if (bits >= 256)\n\t\twindow=5;\n\telse if (bits >= 128)\n\t\twindow=4;\n\telse\n\t\twindow=3;\n\tj=1<<(window-1);\n\tfor (i=1; i<j; i++)\n\t\t{\n\t\tBN_init(&(val[i]));\n\t\tif (!BN_mod_mul_montgomery(&(val[i]),&(val[i-1]),d,mont,ctx))\n\t\t\tgoto err;\n\t\t}\n\tts=i;\n\tstart=1;\n\twvalue=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\tif (BN_is_bit_set(p,wstart) == 0)\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\tgoto err;\n\t\t\t\t}\n\t\t\tif (wstart == 0) break;\n\t\t\twstart--;\n\t\t\tcontinue;\n\t\t\t}\n\t\tj=wstart;\n\t\twvalue=1;\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\tif (BN_is_bit_set(p,wstart-i))\n\t\t\t\t{\n\t\t\t\twvalue<<=(i-wend);\n\t\t\t\twvalue|=1;\n\t\t\t\twend=i;\n\t\t\t\t}\n\t\t\t}\n\t\tj=wend+1;\n\t\tif (!start)\n\t\t\tfor (i=0; i<j; i++)\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 (!BN_mod_mul_montgomery(r,r,&(val[wvalue>>1]),mont,ctx))\n\t\t\tgoto err;\n\t\twstart-=wend+1;\n\t\twvalue=0;\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\tBN_clear_free(&(val[i]));\n\treturn(ret);\n\t}', 'int BN_mod(BIGNUM *rem, BIGNUM *m, BIGNUM *d, BN_CTX *ctx)\n\t{\n#if 0\n\tint i,nm,nd;\n\tBIGNUM *dv;\n\tif (BN_ucmp(m,d) < 0)\n\t\treturn((BN_copy(rem,m) == NULL)?0:1);\n\tdv= &(ctx->bn[ctx->tos]);\n\tif (!BN_copy(rem,m)) return(0);\n\tnm=BN_num_bits(rem);\n\tnd=BN_num_bits(d);\n\tif (!BN_lshift(dv,d,nm-nd)) return(0);\n\tfor (i=nm-nd; i>=0; i--)\n\t\t{\n\t\tif (BN_cmp(rem,dv) >= 0)\n\t\t\t{\n\t\t\tif (!BN_sub(rem,rem,dv)) return(0);\n\t\t\t}\n\t\tif (!BN_rshift1(dv,dv)) return(0);\n\t\t}\n\treturn(1);\n#else\n\treturn(BN_div(NULL,rem,m,d,ctx));\n#endif\n\t}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, BIGNUM *num, BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,j,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\tbn_check_top(num);\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 (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\ttmp= &(ctx->bn[ctx->tos]);\n\ttmp->neg=0;\n\tsnum= &(ctx->bn[ctx->tos+1]);\n\tsdiv= &(ctx->bn[ctx->tos+2]);\n\tif (dv == NULL)\n\t\tres= &(ctx->bn[ctx->tos+3]);\n\telse\tres=dv;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tBN_lshift(sdiv,divisor,norm_shift);\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tBN_lshift(snum,num,norm_shift);\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\tBN_init(&wnum);\n\twnum.d=\t &(snum->d[loop]);\n\twnum.top= div_n;\n\twnum.max= snum->max+1;\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;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tif (!BN_usub(&wnum,&wnum,sdiv)) goto err;\n\t\t*resp=1;\n\t\tres->d[res->top-1]=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tresp--;\n\tfor (i=0; i<loop-1; i++)\n\t\t{\n\t\tBN_ULONG q,n0,n1;\n\t\tBN_ULONG l0;\n\t\twnum.d--; wnum.top++;\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\tq=bn_div_words(n0,n1,d0);\n\t\t{\n#ifdef BN_LLONG\n\t\tBN_ULLONG t1,t2,rem;\n\t\tt1=((BN_ULLONG)n0<<BN_BITS2)|n1;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\trem=t1-(BN_ULLONG)q*d0;\n\t\t\tif ((rem>>BN_BITS2) ||\n\t\t\t\t(t2 <= ((BN_ULLONG)(rem<<BN_BITS2)+wnump[-2])))\n\t\t\t\tbreak;\n\t\t\tq--;\n\t\t\t}\n#else\n\t\tBN_ULONG t1l,t1h,t2l,t2h,t3l,t3h,ql,qh,t3t;\n\t\tt1h=n0;\n\t\tt1l=n1;\n\t\tfor (;;)\n\t\t\t{\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\tt3t=LBITS(d0); t3h=HBITS(d0);\n\t\t\tmul64(t3t,t3h,ql,qh);\n\t\t\tt3l=(t1l-t3t)&BN_MASK2;\n\t\t\tif (t3l > t1l) t3h++;\n\t\t\tt3h=(t1h-t3h)&BN_MASK2;\n\t\t\tif (t3h) break;\n\t\t\tif (t2h < t3l) break;\n\t\t\tif ((t2h == t3l) && (t2l <= wnump[-2])) break;\n\t\t\tq--;\n\t\t\t}\n#endif\n\t\t}\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\tfor (j=div_n+1; j>0; j--)\n\t\t\tif (tmp->d[j-1]) break;\n\t\ttmp->top=j;\n\t\tj=wnum.top;\n\t\tBN_sub(&wnum,&wnum,tmp);\n\t\tsnum->top=snum->top+wnum.top-j;\n\t\tif (wnum.neg)\n\t\t\t{\n\t\t\tq--;\n\t\t\tj=wnum.top;\n\t\t\tBN_add(&wnum,&wnum,sdiv);\n\t\t\tsnum->top+=wnum.top-j;\n\t\t\t}\n\t\t*(resp--)=q;\n\t\twnump--;\n\t\t}\n\tif (rm != NULL)\n\t\t{\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\trm->neg=num->neg;\n\t\t}\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_num_bits(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}']
35,743
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L296
static void pred4x4_vertical_left_rv40(uint8_t *src, uint8_t *topright, int stride, const int l0, const int l1, const int l2, const int l3, const int l4){ LOAD_TOP_EDGE LOAD_TOP_RIGHT_EDGE src[0+0*stride]=(2*t0 + 2*t1 + l1 + 2*l2 + l3 + 4)>>3; src[1+0*stride]= src[0+2*stride]=(t1 + t2 + 1)>>1; src[2+0*stride]= src[1+2*stride]=(t2 + t3 + 1)>>1; src[3+0*stride]= src[2+2*stride]=(t3 + t4+ 1)>>1; src[3+2*stride]=(t4 + t5+ 1)>>1; src[0+1*stride]=(t0 + 2*t1 + t2 + l2 + 2*l3 + l4 + 4)>>3; src[1+1*stride]= src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2; src[2+1*stride]= src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2; src[3+1*stride]= src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2; src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2; }
['static void pred4x4_vertical_left_rv40(uint8_t *src, uint8_t *topright, int stride,\n const int l0, const int l1, const int l2, const int l3, const int l4){\n LOAD_TOP_EDGE\n LOAD_TOP_RIGHT_EDGE\n src[0+0*stride]=(2*t0 + 2*t1 + l1 + 2*l2 + l3 + 4)>>3;\n src[1+0*stride]=\n src[0+2*stride]=(t1 + t2 + 1)>>1;\n src[2+0*stride]=\n src[1+2*stride]=(t2 + t3 + 1)>>1;\n src[3+0*stride]=\n src[2+2*stride]=(t3 + t4+ 1)>>1;\n src[3+2*stride]=(t4 + t5+ 1)>>1;\n src[0+1*stride]=(t0 + 2*t1 + t2 + l2 + 2*l3 + l4 + 4)>>3;\n src[1+1*stride]=\n src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;\n src[2+1*stride]=\n src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;\n src[3+1*stride]=\n src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;\n src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;\n}']
35,744
0
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/test/bntest.c/#L983
int test_mod_exp(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *b, *c, *d, *e; int i; a = BN_new(); b = BN_new(); c = BN_new(); d = BN_new(); e = BN_new(); BN_one(a); BN_one(b); BN_zero(c); if (BN_mod_exp(d, a, b, c, ctx)) { fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\n"); return 0; } BN_bntest_rand(c, 30, 0, 1); for (i = 0; i < num2; i++) { BN_bntest_rand(a, 20 + i * 5, 0, 0); BN_bntest_rand(b, 2 + i, 0, 0); if (!BN_mod_exp(d, a, b, c, ctx)) return (0); if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " ^ "); BN_print(bp, b); BIO_puts(bp, " % "); BN_print(bp, c); BIO_puts(bp, " - "); } BN_print(bp, d); BIO_puts(bp, "\n"); } BN_exp(e, a, b, ctx); BN_sub(e, e, d); BN_div(a, b, e, c, ctx); if (!BN_is_zero(b)) { fprintf(stderr, "Modulo exponentiation test failed!\n"); return 0; } } BN_hex2bn(&a, "050505050505"); BN_hex2bn(&b, "02"); BN_hex2bn(&c, "4141414141414141414141274141414141414141414141414141414141414141" "4141414141414141414141414141414141414141414141414141414141414141" "4141414141414141414141800000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000001"); BN_mod_exp(d, a, b, c, ctx); BN_mul(e, a, a, ctx); if (BN_cmp(d, e)) { fprintf(stderr, "BN_mod_exp and BN_mul produce different results!\n"); return 0; } BN_free(a); BN_free(b); BN_free(c); BN_free(d); BN_free(e); return (1); }
['int test_mod_exp(BIO *bp, BN_CTX *ctx)\n{\n BIGNUM *a, *b, *c, *d, *e;\n int i;\n a = BN_new();\n b = BN_new();\n c = BN_new();\n d = BN_new();\n e = BN_new();\n BN_one(a);\n BN_one(b);\n BN_zero(c);\n if (BN_mod_exp(d, a, b, c, ctx)) {\n fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\\n");\n return 0;\n }\n BN_bntest_rand(c, 30, 0, 1);\n for (i = 0; i < num2; i++) {\n BN_bntest_rand(a, 20 + i * 5, 0, 0);\n BN_bntest_rand(b, 2 + i, 0, 0);\n if (!BN_mod_exp(d, a, b, c, ctx))\n return (0);\n if (bp != NULL) {\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " ^ ");\n BN_print(bp, b);\n BIO_puts(bp, " % ");\n BN_print(bp, c);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, d);\n BIO_puts(bp, "\\n");\n }\n BN_exp(e, a, b, ctx);\n BN_sub(e, e, d);\n BN_div(a, b, e, c, ctx);\n if (!BN_is_zero(b)) {\n fprintf(stderr, "Modulo exponentiation test failed!\\n");\n return 0;\n }\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 BN_mod_exp(d, a, b, c, ctx);\n BN_mul(e, a, a, ctx);\n if (BN_cmp(d, e)) {\n fprintf(stderr, "BN_mod_exp and BN_mul produce different results!\\n");\n return 0;\n }\n BN_free(a);\n BN_free(b);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n return (1);\n}', 'BIGNUM *BN_new(void)\n{\n BIGNUM *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->flags = BN_FLG_MALLOCED;\n bn_check_top(ret);\n return (ret);\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 (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}', '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}']
35,745
0
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/bn/bn_ctx.c/#L273
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n BN_MONT_CTX *mont = NULL;\n int b, bits, ret = 0;\n int r_is_one;\n BN_ULONG w, next_w;\n BIGNUM *d, *r, *t;\n BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n (BN_mul_word(r, (w)) && \\\n ( \\\n (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return -1;\n }\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n if (m->top == 1)\n a %= m->d[0];\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 if (a == 0) {\n BN_zero(rr);\n ret = 1;\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (d == NULL || r == NULL || t == 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 r_is_one = 1;\n w = a;\n for (b = bits - 2; b >= 0; b--) {\n next_w = w * w;\n if ((next_w / w) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = 1;\n }\n w = next_w;\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (BN_is_bit_set(p, b)) {\n next_w = w * a;\n if ((next_w / a) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = a;\n }\n w = next_w;\n }\n }\n if (w != 1) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n }\n if (r_is_one) {\n if (!BN_one(rr))\n goto err;\n } else {\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n }\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}', '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_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(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#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n int num = mont->N.top;\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 bn_correct_top(r);\n return (1);\n }\n }\n#endif\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 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 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 || !tmp)\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 if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (rr != r)\n BN_copy(r, rr);\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_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,746
1
https://github.com/openssl/openssl/blob/04761b557a53f026630dd5916b2b8522d94579db/crypto/lhash/lhash.c/#L221
static int 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->num_nodes--; lh->p = 0; return 0; } 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; } return 1; }
['STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)\n{\n BIO *in = BIO_new(BIO_s_file());\n X509 *x = NULL;\n X509_NAME *xn = NULL;\n STACK_OF(X509_NAME) *ret = NULL;\n LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);\n if ((name_hash == NULL) || (in == NULL)) {\n SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!BIO_read_filename(in, file))\n goto err;\n for (;;) {\n if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)\n break;\n if (ret == NULL) {\n ret = sk_X509_NAME_new_null();\n if (ret == NULL) {\n SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n }\n if ((xn = X509_get_subject_name(x)) == NULL)\n goto err;\n xn = X509_NAME_dup(xn);\n if (xn == NULL)\n goto err;\n if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {\n X509_NAME_free(xn);\n xn = NULL;\n } else {\n lh_X509_NAME_insert(name_hash, xn);\n if (!sk_X509_NAME_push(ret, xn))\n goto err;\n }\n }\n goto done;\n err:\n X509_NAME_free(xn);\n sk_X509_NAME_pop_free(ret, X509_NAME_free);\n ret = NULL;\n done:\n BIO_free(in);\n X509_free(x);\n lh_X509_NAME_free(name_hash);\n if (ret != NULL)\n ERR_clear_error();\n return ret;\n}', 'DEFINE_LHASH_OF(X509_NAME)', '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 return NULL;\n if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)\n goto err;\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;\nerr:\n OPENSSL_free(ret->b);\n OPENSSL_free(ret);\n return NULL;\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)) && !expand(lh))\n return NULL;\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 int 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->num_nodes--;\n lh->p = 0;\n return 0;\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 return 1;\n}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n INCREMENT(realloc_count);\n if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)\n return realloc_impl(str, num, file, line);\n FAILTEST();\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 (void)(file); (void)(line);\n#endif\n return realloc(str, num);\n}']
35,747
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 void decode_exp_lsp(WMACodecContext *s, int ch)\n{\n float lsp_coefs[NB_LSP_COEFS];\n int val, i;\n for (i = 0; i < NB_LSP_COEFS; i++) {\n if (i == 0 || i >= 8)\n val = bitstream_read(&s->bc, 3);\n else\n val = bitstream_read(&s->bc, 4);\n lsp_coefs[i] = ff_wma_lsp_codebook[i][val];\n }\n wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],\n s->block_len, lsp_coefs);\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,748
0
https://github.com/libav/libav/blob/ab839054e662d3227e1f795ba1dfd01fe1cf305c/libswscale/utils.c/#L207
int sws_isSupportedInput(enum AVPixelFormat pix_fmt) { return (unsigned)pix_fmt < AV_PIX_FMT_NB ? format_entries[pix_fmt].is_supported_in : 0; }
['int show_pix_fmts(void *optctx, const char *opt, const char *arg)\n{\n const AVPixFmtDescriptor *pix_desc = NULL;\n printf("Pixel formats:\\n"\n "I.... = Supported Input format for conversion\\n"\n ".O... = Supported Output format for conversion\\n"\n "..H.. = Hardware accelerated format\\n"\n "...P. = Paletted format\\n"\n "....B = Bitstream format\\n"\n "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\\n"\n "-----\\n");\n#if !CONFIG_SWSCALE\n# define sws_isSupportedInput(x) 0\n# define sws_isSupportedOutput(x) 0\n#endif\n while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {\n enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);\n printf("%c%c%c%c%c %-16s %d %2d\\n",\n sws_isSupportedInput (pix_fmt) ? \'I\' : \'.\',\n sws_isSupportedOutput(pix_fmt) ? \'O\' : \'.\',\n pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? \'H\' : \'.\',\n pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? \'P\' : \'.\',\n pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? \'B\' : \'.\',\n pix_desc->name,\n pix_desc->nb_components,\n av_get_bits_per_pixel(pix_desc));\n }\n return 0;\n}', 'enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)\n{\n if (desc < av_pix_fmt_descriptors ||\n desc >= av_pix_fmt_descriptors + FF_ARRAY_ELEMS(av_pix_fmt_descriptors))\n return AV_PIX_FMT_NONE;\n return desc - av_pix_fmt_descriptors;\n}', 'int sws_isSupportedInput(enum AVPixelFormat pix_fmt)\n{\n return (unsigned)pix_fmt < AV_PIX_FMT_NB ?\n format_entries[pix_fmt].is_supported_in : 0;\n}']
35,749
0
https://github.com/openssl/openssl/blob/1e552869960659b4baac425bbf46cf6d3bd5e7e4/crypto/lhash/lhash.c/#L282
void lh_doall_arg(LHASH *lh, void (*func)(), void *arg) { int i; LHASH_NODE *a,*n; for (i=lh->num_nodes-1; i>=0; i--) { a=lh->b[i]; while (a != NULL) { n=a->next; func(a->data,arg); a=n; } } }
['void SSL_free(SSL *s)\n\t{\n\tint i;\n\tif(s == NULL)\n\t return;\n\ti=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);\n#ifdef REF_PRINT\n\tREF_PRINT("SSL",s);\n#endif\n\tif (i > 0) return;\n#ifdef REF_CHECK\n\tif (i < 0)\n\t\t{\n\t\tfprintf(stderr,"SSL_free, bad reference count\\n");\n\t\tabort();\n\t\t}\n#endif\n\tCRYPTO_free_ex_data(ssl_meth,(char *)s,&s->ex_data);\n\tif (s->bbio != NULL)\n\t\t{\n\t\tif (s->bbio == s->wbio)\n\t\t\t{\n\t\t\ts->wbio=BIO_pop(s->wbio);\n\t\t\t}\n\t\tBIO_free(s->bbio);\n\t\ts->bbio=NULL;\n\t\t}\n\tif (s->rbio != NULL)\n\t\tBIO_free_all(s->rbio);\n\tif ((s->wbio != NULL) && (s->wbio != s->rbio))\n\t\tBIO_free_all(s->wbio);\n\tif (s->init_buf != NULL) BUF_MEM_free(s->init_buf);\n\tif (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);\n\tif (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);\n\tif (s->session != NULL)\n\t\t{\n\t\tssl_clear_bad_session(s);\n\t\tSSL_SESSION_free(s->session);\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tif (s->cert != NULL) ssl_cert_free(s->cert);\n\tif (s->ctx) SSL_CTX_free(s->ctx);\n\tif (s->client_CA != NULL)\n\t\tsk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);\n\tif (s->method != NULL) s->method->ssl_free(s);\n\tOPENSSL_free(s);\n\t}', 'void SSL_CTX_free(SSL_CTX *a)\n\t{\n\tint i;\n\tif (a == NULL) return;\n\ti=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);\n#ifdef REF_PRINT\n\tREF_PRINT("SSL_CTX",a);\n#endif\n\tif (i > 0) return;\n#ifdef REF_CHECK\n\tif (i < 0)\n\t\t{\n\t\tfprintf(stderr,"SSL_CTX_free, bad reference count\\n");\n\t\tabort();\n\t\t}\n#endif\n\tCRYPTO_free_ex_data(ssl_ctx_meth,(char *)a,&a->ex_data);\n\tif (a->sessions != NULL)\n\t\t{\n\t\tSSL_CTX_flush_sessions(a,0);\n\t\tlh_free(a->sessions);\n\t\t}\n\tif (a->cert_store != NULL)\n\t\tX509_STORE_free(a->cert_store);\n\tif (a->cipher_list != NULL)\n\t\tsk_SSL_CIPHER_free(a->cipher_list);\n\tif (a->cipher_list_by_id != NULL)\n\t\tsk_SSL_CIPHER_free(a->cipher_list_by_id);\n\tif (a->cert != NULL)\n\t\tssl_cert_free(a->cert);\n\tif (a->client_CA != NULL)\n\t\tsk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);\n\tif (a->extra_certs != NULL)\n\t\tsk_X509_pop_free(a->extra_certs,X509_free);\n\tif (a->comp_methods != NULL)\n\t\tsk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);\n\tOPENSSL_free(a);\n\t}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n\t{\n\tunsigned long i;\n\tTIMEOUT_PARAM tp;\n\ttp.ctx=s;\n\ttp.cache=s->sessions;\n\tif (tp.cache == NULL) return;\n\ttp.time=t;\n\tCRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\ti=tp.cache->down_load;\n\ttp.cache->down_load=0;\n\tlh_doall_arg(tp.cache,(void (*)())timeout,&tp);\n\ttp.cache->down_load=i;\n\tCRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t}', 'void lh_doall_arg(LHASH *lh, void (*func)(), void *arg)\n\t{\n\tint i;\n\tLHASH_NODE *a,*n;\n\tfor (i=lh->num_nodes-1; i>=0; i--)\n\t\t{\n\t\ta=lh->b[i];\n\t\twhile (a != NULL)\n\t\t\t{\n\t\t\tn=a->next;\n\t\t\tfunc(a->data,arg);\n\t\t\ta=n;\n\t\t\t}\n\t\t}\n\t}']
35,750
0
https://github.com/libav/libav/blob/d9cf5f516974c64e01846ca685301014b38cf224/libavcodec/smacker.c/#L291
static int decode_header_trees(SmackVContext *smk) { GetBitContext gb; int mmap_size, mclr_size, full_size, type_size; mmap_size = AV_RL32(smk->avctx->extradata); mclr_size = AV_RL32(smk->avctx->extradata + 4); full_size = AV_RL32(smk->avctx->extradata + 8); type_size = AV_RL32(smk->avctx->extradata + 12); init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8); if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n"); smk->mmap_tbl = av_malloc(sizeof(int) * 2); smk->mmap_tbl[0] = 0; smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1; } else { if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size)) return -1; } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n"); smk->mclr_tbl = av_malloc(sizeof(int) * 2); smk->mclr_tbl[0] = 0; smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1; } else { if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size)) return -1; } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n"); smk->full_tbl = av_malloc(sizeof(int) * 2); smk->full_tbl[0] = 0; smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1; } else { if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size)) return -1; } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n"); smk->type_tbl = av_malloc(sizeof(int) * 2); smk->type_tbl[0] = 0; smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1; } else { if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size)) return -1; } return 0; }
['static int decode_header_trees(SmackVContext *smk) {\n GetBitContext gb;\n int mmap_size, mclr_size, full_size, type_size;\n mmap_size = AV_RL32(smk->avctx->extradata);\n mclr_size = AV_RL32(smk->avctx->extradata + 4);\n full_size = AV_RL32(smk->avctx->extradata + 8);\n type_size = AV_RL32(smk->avctx->extradata + 12);\n init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\\n");\n smk->mmap_tbl = av_malloc(sizeof(int) * 2);\n smk->mmap_tbl[0] = 0;\n smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;\n } else {\n if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))\n return -1;\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\\n");\n smk->mclr_tbl = av_malloc(sizeof(int) * 2);\n smk->mclr_tbl[0] = 0;\n smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;\n } else {\n if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))\n return -1;\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\\n");\n smk->full_tbl = av_malloc(sizeof(int) * 2);\n smk->full_tbl[0] = 0;\n smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;\n } else {\n if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))\n return -1;\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\\n");\n smk->type_tbl = av_malloc(sizeof(int) * 2);\n smk->type_tbl[0] = 0;\n smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;\n } else {\n if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))\n return -1;\n }\n return 0;\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) {\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}', '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,751
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)]; }
['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 (test == NULL)\n goto err;\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 if (q == NULL)\n goto err;\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}', '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_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,752
0
https://github.com/openssl/openssl/blob/fa9bb6201e1d16ba8ccab938833d140ef81a7f73/crypto/mem.c/#L241
void CRYPTO_free(void *str, const char *file, int line) { if (free_impl != NULL && free_impl != &CRYPTO_free) { free_impl(str, file, line); return; } #ifndef OPENSSL_NO_CRYPTO_MDEBUG if (call_malloc_debug) { CRYPTO_mem_debug_free(str, 0, file, line); free(str); CRYPTO_mem_debug_free(str, 1, file, line); } else { free(str); } #else free(str); #endif }
['int ECDH_KDF_X9_62(unsigned char *out, size_t outlen,\n const unsigned char *Z, size_t Zlen,\n const unsigned char *sinfo, size_t sinfolen,\n const EVP_MD *md)\n{\n EVP_MD_CTX *mctx = NULL;\n int rv = 0;\n unsigned int i;\n size_t mdlen;\n unsigned char ctr[4];\n if (sinfolen > ECDH_KDF_MAX || outlen > ECDH_KDF_MAX\n || Zlen > ECDH_KDF_MAX)\n return 0;\n mctx = EVP_MD_CTX_new();\n if (mctx == NULL)\n return 0;\n mdlen = EVP_MD_size(md);\n for (i = 1;; i++) {\n unsigned char mtmp[EVP_MAX_MD_SIZE];\n EVP_DigestInit_ex(mctx, md, NULL);\n ctr[3] = i & 0xFF;\n ctr[2] = (i >> 8) & 0xFF;\n ctr[1] = (i >> 16) & 0xFF;\n ctr[0] = (i >> 24) & 0xFF;\n if (!EVP_DigestUpdate(mctx, Z, Zlen))\n goto err;\n if (!EVP_DigestUpdate(mctx, ctr, sizeof(ctr)))\n goto err;\n if (!EVP_DigestUpdate(mctx, sinfo, sinfolen))\n goto err;\n if (outlen >= mdlen) {\n if (!EVP_DigestFinal(mctx, out, NULL))\n goto err;\n outlen -= mdlen;\n if (outlen == 0)\n break;\n out += mdlen;\n } else {\n if (!EVP_DigestFinal(mctx, mtmp, NULL))\n goto err;\n memcpy(out, mtmp, outlen);\n OPENSSL_cleanse(mtmp, mdlen);\n break;\n }\n }\n rv = 1;\n err:\n EVP_MD_CTX_free(mctx);\n return rv;\n}', 'int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)\n{\n int ret;\n ret = EVP_DigestFinal_ex(ctx, md, size);\n EVP_MD_CTX_reset(ctx);\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 && 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 EVP_PKEY_CTX_free(ctx->pctx);\n#ifndef OPENSSL_NO_ENGINE\n if (ctx->engine)\n ENGINE_finish(ctx->engine);\n#endif\n memset(ctx, 0, 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 if (ctx->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 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}']
35,753
0
https://github.com/openssl/openssl/blob/16bce0e08b16b28a1953795bde3f913957b08ef2/ssl/packet.c/#L49
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) { assert(pkt->subs != NULL && len != 0); if (pkt->subs == NULL || len == 0) return 0; if (pkt->maxsize - pkt->written < len) return 0; if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) { size_t newlen; size_t reflen; reflen = (len > pkt->buf->length) ? len : pkt->buf->length; if (reflen > SIZE_MAX / 2) { newlen = SIZE_MAX; } else { newlen = reflen * 2; if (newlen < DEFAULT_BUF_SIZE) newlen = DEFAULT_BUF_SIZE; } if (BUF_MEM_grow(pkt->buf, newlen) == 0) return 0; } *allocbytes = GETBUF(pkt) + pkt->curr; return 1; }
['int ssl_add_serverhello_renegotiate_ext(SSL *s, WPACKET *pkt)\n{\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u8(pkt)\n || !WPACKET_memcpy(pkt, s->s3->previous_client_finished,\n s->s3->previous_client_finished_len)\n || !WPACKET_memcpy(pkt, s->s3->previous_server_finished,\n s->s3->previous_server_finished_len)\n || !WPACKET_close(pkt)\n || !WPACKET_close(pkt))\n return 0;\n return 1;\n}', 'int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)\n{\n unsigned char *dest;\n if (len == 0)\n return 1;\n if (!WPACKET_allocate_bytes(pkt, len, &dest))\n return 0;\n memcpy(dest, src, len);\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n assert(pkt->subs != NULL && len != 0);\n if (pkt->subs == NULL || len == 0)\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {\n size_t newlen;\n size_t reflen;\n reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n if (reflen > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = reflen * 2;\n if (newlen < DEFAULT_BUF_SIZE)\n newlen = DEFAULT_BUF_SIZE;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n *allocbytes = GETBUF(pkt) + pkt->curr;\n return 1;\n}']
35,754
0
https://github.com/openssl/openssl/blob/3ec9e4ec46eb4356bc106db5e0e33148c693c8f0/crypto/lhash/lhash.c/#L139
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; }
['static int test_tls13_psk(int idx)\n{\n SSL_CTX *sctx = NULL, *cctx = NULL;\n SSL *serverssl = NULL, *clientssl = NULL;\n const SSL_CIPHER *cipher = NULL;\n const unsigned char key[] = {\n 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,\n 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,\n 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,\n 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f\n };\n int testresult = 0;\n if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),\n TLS_client_method(), &sctx,\n &cctx, cert, privkey)))\n goto end;\n if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256")))\n goto end;\n if (idx == 0 || idx == 1) {\n SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);\n SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);\n }\n if (idx == 1 || idx == 2) {\n SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);\n SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);\n }\n srvid = pskid;\n use_session_cb_cnt = 0;\n find_session_cb_cnt = 0;\n psk_client_cb_cnt = 0;\n psk_server_cb_cnt = 0;\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,\n NULL, NULL))\n || !TEST_true(create_ssl_connection(serverssl, clientssl,\n SSL_ERROR_NONE))\n || !TEST_false(SSL_session_reused(clientssl))\n || !TEST_false(SSL_session_reused(serverssl)))\n goto end;\n if (idx == 0 || idx == 1) {\n if (!TEST_true(use_session_cb_cnt == 1)\n || !TEST_true(find_session_cb_cnt == 0)\n || !TEST_true(psk_client_cb_cnt == idx)\n || !TEST_true(psk_server_cb_cnt == 0))\n goto end;\n } else {\n if (!TEST_true(use_session_cb_cnt == 0)\n || !TEST_true(find_session_cb_cnt == 0)\n || !TEST_true(psk_client_cb_cnt == 1)\n || !TEST_true(psk_server_cb_cnt == 0))\n goto end;\n }\n shutdown_ssl_connection(serverssl, clientssl);\n serverssl = clientssl = NULL;\n use_session_cb_cnt = psk_client_cb_cnt = 0;\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,\n NULL, NULL)))\n goto end;\n cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);\n clientpsk = SSL_SESSION_new();\n if (!TEST_ptr(clientpsk)\n || !TEST_ptr(cipher)\n || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,\n sizeof(key)))\n || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))\n || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,\n TLS1_3_VERSION))\n || !TEST_true(SSL_SESSION_up_ref(clientpsk)))\n goto end;\n serverpsk = clientpsk;\n if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))\n || !TEST_true(SSL_session_reused(clientssl))\n || !TEST_true(SSL_session_reused(serverssl)))\n goto end;\n if (idx == 0 || idx == 1) {\n if (!TEST_true(use_session_cb_cnt == 1)\n || !TEST_true(find_session_cb_cnt == 1)\n || !TEST_true(psk_client_cb_cnt == 0)\n || !TEST_true(psk_server_cb_cnt == 0))\n goto end;\n } else {\n if (!TEST_true(use_session_cb_cnt == 0)\n || !TEST_true(find_session_cb_cnt == 0)\n || !TEST_true(psk_client_cb_cnt == 1)\n || !TEST_true(psk_server_cb_cnt == 1))\n goto end;\n }\n shutdown_ssl_connection(serverssl, clientssl);\n serverssl = clientssl = NULL;\n use_session_cb_cnt = find_session_cb_cnt = 0;\n psk_client_cb_cnt = psk_server_cb_cnt = 0;\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,\n NULL, NULL)))\n goto end;\n if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))\n goto end;\n if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))\n || !TEST_true(SSL_session_reused(clientssl))\n || !TEST_true(SSL_session_reused(serverssl)))\n goto end;\n if (idx == 0 || idx == 1) {\n if (!TEST_true(use_session_cb_cnt == 2)\n || !TEST_true(find_session_cb_cnt == 2)\n || !TEST_true(psk_client_cb_cnt == 0)\n || !TEST_true(psk_server_cb_cnt == 0))\n goto end;\n } else {\n if (!TEST_true(use_session_cb_cnt == 0)\n || !TEST_true(find_session_cb_cnt == 0)\n || !TEST_true(psk_client_cb_cnt == 2)\n || !TEST_true(psk_server_cb_cnt == 2))\n goto end;\n }\n shutdown_ssl_connection(serverssl, clientssl);\n serverssl = clientssl = NULL;\n use_session_cb_cnt = find_session_cb_cnt = 0;\n psk_client_cb_cnt = psk_server_cb_cnt = 0;\n srvid = "Dummy Identity";\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,\n NULL, NULL))\n || !TEST_true(create_ssl_connection(serverssl, clientssl,\n SSL_ERROR_NONE))\n || !TEST_false(SSL_session_reused(clientssl))\n || !TEST_false(SSL_session_reused(serverssl)))\n goto end;\n if (idx == 0 || idx == 1) {\n if (!TEST_true(use_session_cb_cnt == 1)\n || !TEST_true(find_session_cb_cnt == 1)\n || !TEST_true(psk_client_cb_cnt == 0)\n || !TEST_true(psk_server_cb_cnt == idx))\n goto end;\n } else {\n if (!TEST_true(use_session_cb_cnt == 0)\n || !TEST_true(find_session_cb_cnt == 0)\n || !TEST_true(psk_client_cb_cnt == 1)\n || !TEST_true(psk_server_cb_cnt == 1))\n goto end;\n }\n shutdown_ssl_connection(serverssl, clientssl);\n serverssl = clientssl = NULL;\n testresult = 1;\n end:\n SSL_SESSION_free(clientpsk);\n SSL_SESSION_free(serverpsk);\n clientpsk = serverpsk = NULL;\n SSL_free(serverssl);\n SSL_free(clientssl);\n SSL_CTX_free(sctx);\n SSL_CTX_free(cctx);\n return testresult;\n}', 'int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,\n SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)\n{\n SSL *serverssl = NULL, *clientssl = NULL;\n BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;\n if (*sssl != NULL)\n serverssl = *sssl;\n else if (!TEST_ptr(serverssl = SSL_new(serverctx)))\n goto error;\n if (*cssl != NULL)\n clientssl = *cssl;\n else if (!TEST_ptr(clientssl = SSL_new(clientctx)))\n goto error;\n if (SSL_is_dtls(clientssl)) {\n if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))\n || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))\n goto error;\n } else {\n if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))\n || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))\n goto error;\n }\n if (s_to_c_fbio != NULL\n && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))\n goto error;\n if (c_to_s_fbio != NULL\n && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))\n goto error;\n BIO_set_mem_eof_return(s_to_c_bio, -1);\n BIO_set_mem_eof_return(c_to_s_bio, -1);\n SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);\n BIO_up_ref(s_to_c_bio);\n BIO_up_ref(c_to_s_bio);\n SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);\n *sssl = serverssl;\n *cssl = clientssl;\n return 1;\n error:\n SSL_free(serverssl);\n SSL_free(clientssl);\n BIO_free(s_to_c_bio);\n BIO_free(c_to_s_bio);\n BIO_free(s_to_c_fbio);\n BIO_free(c_to_s_fbio);\n return 0;\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->references = 1;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n OPENSSL_free(s);\n s = NULL;\n goto err;\n }\n if (RAND_get_rand_method() == RAND_OpenSSL()) {\n s->drbg =\n RAND_DRBG_new(0, 0, RAND_DRBG_get0_public());\n if (s->drbg == NULL\n || RAND_DRBG_instantiate(s->drbg,\n (const unsigned char *) SSL_version_str,\n sizeof(SSL_version_str) - 1) == 0)\n goto err;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\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->max_early_data = ctx->max_early_data;\n s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);\n if (s->tls13_ciphersuites == NULL)\n goto err;\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->record_padding_cb = ctx->record_padding_cb;\n s->record_padding_arg = ctx->record_padding_arg;\n s->block_padding = ctx->block_padding;\n s->sid_ctx_length = ctx->sid_ctx_length;\n if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))\n goto err;\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->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;\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->ext.debug_cb = 0;\n s->ext.debug_arg = NULL;\n s->ext.ticket_expected = 0;\n s->ext.status_type = ctx->ext.status_type;\n s->ext.status_expected = 0;\n s->ext.ocsp.ids = NULL;\n s->ext.ocsp.exts = NULL;\n s->ext.ocsp.resp = NULL;\n s->ext.ocsp.resp_len = 0;\n SSL_CTX_up_ref(ctx);\n s->session_ctx = ctx;\n#ifndef OPENSSL_NO_EC\n if (ctx->ext.ecpointformats) {\n s->ext.ecpointformats =\n OPENSSL_memdup(ctx->ext.ecpointformats,\n ctx->ext.ecpointformats_len);\n if (!s->ext.ecpointformats)\n goto err;\n s->ext.ecpointformats_len =\n ctx->ext.ecpointformats_len;\n }\n if (ctx->ext.supportedgroups) {\n s->ext.supportedgroups =\n OPENSSL_memdup(ctx->ext.supportedgroups,\n ctx->ext.supportedgroups_len\n * sizeof(*ctx->ext.supportedgroups));\n if (!s->ext.supportedgroups)\n goto err;\n s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n s->ext.npn = NULL;\n#endif\n if (s->ctx->ext.alpn) {\n s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);\n if (s->ext.alpn == NULL)\n goto err;\n memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);\n s->ext.alpn_len = s->ctx->ext.alpn_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 s->key_update = SSL_KEY_UPDATE_NONE;\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->psk_find_session_cb = ctx->psk_find_session_cb;\n s->psk_use_session_cb = ctx->psk_use_session_cb;\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_DOWN_REF(&s->references, &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 ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n BIO_free_all(s->rbio);\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 sk_SSL_CIPHER_free(s->tls13_ciphersuites);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n SSL_SESSION_free(s->psksession);\n OPENSSL_free(s->psksession_id);\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->ext.hostname);\n SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->ext.ecpointformats);\n OPENSSL_free(s->ext.supportedgroups);\n#endif\n sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->ext.scts);\n#endif\n OPENSSL_free(s->ext.ocsp.resp);\n OPENSSL_free(s->ext.alpn);\n OPENSSL_free(s->ext.tls13_cookie);\n OPENSSL_free(s->clienthello);\n OPENSSL_free(s->pha_context);\n EVP_MD_CTX_free(s->pha_dgst);\n sk_X509_NAME_pop_free(s->ca_names, 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->ext.npn);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n RAND_DRBG_free(s->drbg);\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', 'int ssl_clear_bad_session(SSL *s)\n{\n if ((s->session != NULL) &&\n !(s->shutdown & SSL_SENT_SHUTDOWN) &&\n !(SSL_in_init(s) || SSL_in_before(s))) {\n SSL_CTX_remove_session(s->session_ctx, s->session);\n return 1;\n } else\n return 0;\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)) != NULL) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, r);\n SSL_SESSION_list_remove(ctx, r);\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,755
0
https://github.com/openssl/openssl/blob/b8f1c116a357285ccb4905cd88c83f5076bafb52/apps/apps.c/#L1993
unsigned char *next_protos_parse(size_t *outlen, const char *in) { size_t len; unsigned char *out; size_t i, start = 0; len = strlen(in); if (len >= 65535) return NULL; out = app_malloc(strlen(in) + 1, "NPN buffer"); for (i = 0; i <= len; ++i) { if (i == len || in[i] == ',') { if (i - start > 255) { OPENSSL_free(out); return NULL; } out[start] = i - start; start = i + 1; } else out[i + 1] = in[i]; } *outlen = len + 1; return out; }
['int s_server_main(int argc, char *argv[])\n{\n ENGINE *e = NULL;\n EVP_PKEY *s_key = NULL, *s_dkey = NULL;\n SSL_CONF_CTX *cctx = NULL;\n const SSL_METHOD *meth = TLS_server_method();\n SSL_EXCERT *exc = NULL;\n STACK_OF(OPENSSL_STRING) *ssl_args = NULL;\n STACK_OF(X509) *s_chain = NULL, *s_dchain = NULL;\n STACK_OF(X509_CRL) *crls = NULL;\n X509 *s_cert = NULL, *s_dcert = NULL;\n X509_VERIFY_PARAM *vpm = NULL;\n char *CApath = NULL, *CAfile = NULL, *chCApath = NULL, *chCAfile = NULL;\n#ifndef OPENSSL_NO_DH\n char *dhfile = NULL;\n#endif\n char *dpassarg = NULL, *dpass = NULL, *inrand = NULL;\n char *passarg = NULL, *pass = NULL, *vfyCApath = NULL, *vfyCAfile = NULL;\n char *crl_file = NULL, *prog;\n#ifndef OPENSSL_NO_PSK\n char *p;\n#endif\n#ifdef AF_UNIX\n int unlink_unix_path = 0;\n#endif\n do_server_cb server_cb;\n int vpmtouched = 0, build_chain = 0, no_cache = 0, ext_cache = 0;\n#ifndef OPENSSL_NO_DH\n int no_dhe = 0;\n#endif\n int nocert = 0, ret = 1;\n int noCApath = 0, noCAfile = 0;\n int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;\n int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;\n int rev = 0, naccept = -1, sdebug = 0;\n int socket_family = AF_UNSPEC, socket_type = SOCK_STREAM;\n int state = 0, crl_format = FORMAT_PEM, crl_download = 0;\n char *host = NULL;\n char *port = BUF_strdup(PORT);\n unsigned char *context = NULL;\n OPTION_CHOICE o;\n EVP_PKEY *s_key2 = NULL;\n X509 *s_cert2 = NULL;\n tlsextctx tlsextcbp = { NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING };\n const char *ssl_config = NULL;\n int read_buf_len = 0;\n#ifndef OPENSSL_NO_NEXTPROTONEG\n const char *next_proto_neg_in = NULL;\n tlsextnextprotoctx next_proto = { NULL, 0 };\n#endif\n const char *alpn_in = NULL;\n tlsextalpnctx alpn_ctx = { NULL, 0 };\n#ifndef OPENSSL_NO_PSK\n static char *psk_identity_hint = NULL;\n#endif\n#ifndef OPENSSL_NO_SRP\n char *srpuserseed = NULL;\n char *srp_verifier_file = NULL;\n#endif\n int min_version = 0, max_version = 0;\n local_argc = argc;\n local_argv = argv;\n s_server_init();\n cctx = SSL_CONF_CTX_new();\n vpm = X509_VERIFY_PARAM_new();\n if (cctx == NULL || vpm == NULL)\n goto end;\n SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CMDLINE);\n prog = opt_init(argc, argv, s_server_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(s_server_options);\n ret = 0;\n goto end;\n case OPT_4:\n#ifdef AF_UNIX\n if (socket_family == AF_UNIX) {\n OPENSSL_free(host); host = NULL;\n OPENSSL_free(port); port = NULL;\n }\n#endif\n socket_family = AF_INET;\n break;\n case OPT_6:\n if (1) {\n#ifdef AF_INET6\n#ifdef AF_UNIX\n if (socket_family == AF_UNIX) {\n OPENSSL_free(host); host = NULL;\n OPENSSL_free(port); port = NULL;\n }\n#endif\n socket_family = AF_INET6;\n } else {\n#endif\n BIO_printf(bio_err, "%s: IPv6 domain sockets unsupported\\n", prog);\n goto end;\n }\n break;\n case OPT_PORT:\n#ifdef AF_UNIX\n if (socket_family == AF_UNIX) {\n socket_family = AF_UNSPEC;\n }\n#endif\n OPENSSL_free(port); port = NULL;\n OPENSSL_free(host); host = NULL;\n if (BIO_parse_hostserv(opt_arg(), NULL, &port, BIO_PARSE_PRIO_SERV) < 1) {\n BIO_printf(bio_err,\n "%s: -port argument malformed or ambiguous\\n",\n port);\n goto end;\n }\n break;\n case OPT_ACCEPT:\n#ifdef AF_UNIX\n if (socket_family == AF_UNIX) {\n socket_family = AF_UNSPEC;\n }\n#endif\n OPENSSL_free(port); port = NULL;\n OPENSSL_free(host); host = NULL;\n if (BIO_parse_hostserv(opt_arg(), &host, &port, BIO_PARSE_PRIO_SERV) < 1) {\n BIO_printf(bio_err,\n "%s: -accept argument malformed or ambiguous\\n",\n port);\n goto end;\n }\n break;\n#ifdef AF_UNIX\n case OPT_UNIX:\n socket_family = AF_UNIX;\n OPENSSL_free(host); host = BUF_strdup(opt_arg());\n OPENSSL_free(port); port = NULL;\n break;\n case OPT_UNLINK:\n unlink_unix_path = 1;\n break;\n#endif\n case OPT_NACCEPT:\n naccept = atol(opt_arg());\n break;\n case OPT_VERIFY:\n s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;\n verify_depth = atoi(opt_arg());\n if (!s_quiet)\n BIO_printf(bio_err, "verify depth is %d\\n", verify_depth);\n break;\n case OPT_UPPER_V_VERIFY:\n s_server_verify =\n SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |\n SSL_VERIFY_CLIENT_ONCE;\n verify_depth = atoi(opt_arg());\n if (!s_quiet)\n BIO_printf(bio_err,\n "verify depth is %d, must return a certificate\\n",\n verify_depth);\n break;\n case OPT_CONTEXT:\n context = (unsigned char *)opt_arg();\n break;\n case OPT_CERT:\n s_cert_file = opt_arg();\n break;\n case OPT_CRL:\n crl_file = opt_arg();\n break;\n case OPT_CRL_DOWNLOAD:\n crl_download = 1;\n break;\n case OPT_SERVERINFO:\n s_serverinfo_file = opt_arg();\n break;\n case OPT_CERTFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &s_cert_format))\n goto opthelp;\n break;\n case OPT_KEY:\n s_key_file = opt_arg();\n break;\n case OPT_KEYFORM:\n if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_key_format))\n goto opthelp;\n break;\n case OPT_PASS:\n passarg = opt_arg();\n break;\n case OPT_CERT_CHAIN:\n s_chain_file = opt_arg();\n break;\n case OPT_DHPARAM:\n#ifndef OPENSSL_NO_DH\n dhfile = opt_arg();\n#endif\n break;\n case OPT_DCERTFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &s_dcert_format))\n goto opthelp;\n break;\n case OPT_DCERT:\n s_dcert_file = opt_arg();\n break;\n case OPT_DKEYFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &s_dkey_format))\n goto opthelp;\n break;\n case OPT_DPASS:\n dpassarg = opt_arg();\n break;\n case OPT_DKEY:\n s_dkey_file = opt_arg();\n break;\n case OPT_DCERT_CHAIN:\n s_dchain_file = opt_arg();\n break;\n case OPT_NOCERT:\n nocert = 1;\n break;\n case OPT_CAPATH:\n CApath = opt_arg();\n break;\n case OPT_NOCAPATH:\n noCApath = 1;\n break;\n case OPT_CHAINCAPATH:\n chCApath = opt_arg();\n break;\n case OPT_VERIFYCAPATH:\n vfyCApath = opt_arg();\n break;\n case OPT_NO_CACHE:\n no_cache = 1;\n break;\n case OPT_EXT_CACHE:\n ext_cache = 1;\n break;\n case OPT_CRLFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &crl_format))\n goto opthelp;\n break;\n case OPT_S_CASES:\n if (ssl_args == NULL)\n ssl_args = sk_OPENSSL_STRING_new_null();\n if (ssl_args == NULL\n || !sk_OPENSSL_STRING_push(ssl_args, opt_flag())\n || !sk_OPENSSL_STRING_push(ssl_args, opt_arg())) {\n BIO_printf(bio_err, "%s: Memory allocation failure\\n", prog);\n goto end;\n }\n break;\n case OPT_V_CASES:\n if (!opt_verify(o, vpm))\n goto end;\n vpmtouched++;\n break;\n case OPT_X_CASES:\n if (!args_excert(o, &exc))\n goto end;\n break;\n case OPT_VERIFY_RET_ERROR:\n verify_return_error = 1;\n break;\n case OPT_VERIFY_QUIET:\n verify_quiet = 1;\n break;\n case OPT_BUILD_CHAIN:\n build_chain = 1;\n break;\n case OPT_CAFILE:\n CAfile = opt_arg();\n break;\n case OPT_NOCAFILE:\n noCAfile = 1;\n break;\n case OPT_CHAINCAFILE:\n chCAfile = opt_arg();\n break;\n case OPT_VERIFYCAFILE:\n vfyCAfile = opt_arg();\n break;\n case OPT_NBIO:\n s_nbio = 1;\n break;\n case OPT_NBIO_TEST:\n s_nbio = s_nbio_test = 1;\n break;\n case OPT_IGN_EOF:\n s_ign_eof = 1;\n break;\n case OPT_NO_IGN_EOF:\n s_ign_eof = 0;\n break;\n case OPT_DEBUG:\n s_debug = 1;\n break;\n case OPT_TLSEXTDEBUG:\n s_tlsextdebug = 1;\n break;\n case OPT_STATUS:\n s_tlsextstatus = 1;\n break;\n case OPT_STATUS_VERBOSE:\n s_tlsextstatus = tlscstatp.verbose = 1;\n break;\n case OPT_STATUS_TIMEOUT:\n s_tlsextstatus = 1;\n tlscstatp.timeout = atoi(opt_arg());\n break;\n case OPT_STATUS_URL:\n#ifndef OPENSSL_NO_OCSP\n s_tlsextstatus = 1;\n if (!OCSP_parse_url(opt_arg(),\n &tlscstatp.host,\n &tlscstatp.port,\n &tlscstatp.path, &tlscstatp.use_ssl)) {\n BIO_printf(bio_err, "Error parsing URL\\n");\n goto end;\n }\n#endif\n break;\n case OPT_MSG:\n s_msg = 1;\n break;\n case OPT_MSGFILE:\n bio_s_msg = BIO_new_file(opt_arg(), "w");\n break;\n case OPT_TRACE:\n#ifndef OPENSSL_NO_SSL_TRACE\n s_msg = 2;\n#endif\n break;\n case OPT_SECURITY_DEBUG:\n sdebug = 1;\n break;\n case OPT_SECURITY_DEBUG_VERBOSE:\n sdebug = 2;\n break;\n case OPT_STATE:\n state = 1;\n break;\n case OPT_CRLF:\n s_crlf = 1;\n break;\n case OPT_QUIET:\n s_quiet = 1;\n break;\n case OPT_BRIEF:\n s_quiet = s_brief = verify_quiet = 1;\n break;\n case OPT_NO_DHE:\n#ifndef OPENSSL_NO_DH\n no_dhe = 1;\n#endif\n break;\n case OPT_NO_RESUME_EPHEMERAL:\n no_resume_ephemeral = 1;\n break;\n case OPT_PSK_HINT:\n#ifndef OPENSSL_NO_PSK\n psk_identity_hint = opt_arg();\n#endif\n break;\n case OPT_PSK:\n#ifndef OPENSSL_NO_PSK\n for (p = psk_key = opt_arg(); *p; p++) {\n if (isxdigit(_UC(*p)))\n continue;\n BIO_printf(bio_err, "Not a hex number \'%s\'\\n", *argv);\n goto end;\n }\n#endif\n break;\n case OPT_SRPVFILE:\n#ifndef OPENSSL_NO_SRP\n srp_verifier_file = opt_arg();\n if (min_version < TLS1_VERSION)\n min_version = TLS1_VERSION;\n#endif\n break;\n case OPT_SRPUSERSEED:\n#ifndef OPENSSL_NO_SRP\n srpuserseed = opt_arg();\n if (min_version < TLS1_VERSION)\n min_version = TLS1_VERSION;\n#endif\n break;\n case OPT_REV:\n rev = 1;\n break;\n case OPT_WWW:\n www = 1;\n break;\n case OPT_UPPER_WWW:\n www = 2;\n break;\n case OPT_HTTP:\n www = 3;\n break;\n case OPT_SSL_CONFIG:\n ssl_config = opt_arg();\n break;\n case OPT_SSL3:\n min_version = SSL3_VERSION;\n max_version = SSL3_VERSION;\n break;\n case OPT_TLS1_2:\n min_version = TLS1_2_VERSION;\n max_version = TLS1_2_VERSION;\n break;\n case OPT_TLS1_1:\n min_version = TLS1_1_VERSION;\n max_version = TLS1_1_VERSION;\n break;\n case OPT_TLS1:\n min_version = TLS1_VERSION;\n max_version = TLS1_VERSION;\n break;\n case OPT_DTLS:\n#ifndef OPENSSL_NO_DTLS\n meth = DTLS_server_method();\n socket_type = SOCK_DGRAM;\n#endif\n break;\n case OPT_DTLS1:\n#ifndef OPENSSL_NO_DTLS\n meth = DTLS_server_method();\n min_version = DTLS1_VERSION;\n max_version = DTLS1_VERSION;\n socket_type = SOCK_DGRAM;\n#endif\n break;\n case OPT_DTLS1_2:\n#ifndef OPENSSL_NO_DTLS\n meth = DTLS_server_method();\n min_version = DTLS1_2_VERSION;\n max_version = DTLS1_2_VERSION;\n socket_type = SOCK_DGRAM;\n#endif\n break;\n case OPT_TIMEOUT:\n#ifndef OPENSSL_NO_DTLS\n enable_timeouts = 1;\n#endif\n break;\n case OPT_MTU:\n#ifndef OPENSSL_NO_DTLS\n socket_mtu = atol(opt_arg());\n#endif\n break;\n case OPT_CHAIN:\n#ifndef OPENSSL_NO_DTLS\n cert_chain = 1;\n#endif\n break;\n case OPT_LISTEN:\n#ifndef OPENSSL_NO_DTLS\n dtlslisten = 1;\n#endif\n break;\n case OPT_ID_PREFIX:\n session_id_prefix = opt_arg();\n break;\n case OPT_ENGINE:\n e = setup_engine(opt_arg(), 1);\n break;\n case OPT_RAND:\n inrand = opt_arg();\n break;\n case OPT_SERVERNAME:\n tlsextcbp.servername = opt_arg();\n break;\n case OPT_SERVERNAME_FATAL:\n tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL;\n break;\n case OPT_CERT2:\n s_cert_file2 = opt_arg();\n break;\n case OPT_KEY2:\n s_key_file2 = opt_arg();\n break;\n case OPT_NEXTPROTONEG:\n# ifndef OPENSSL_NO_NEXTPROTONEG\n next_proto_neg_in = opt_arg();\n#endif\n break;\n case OPT_ALPN:\n alpn_in = opt_arg();\n break;\n case OPT_SRTP_PROFILES:\n#ifndef OPENSSL_NO_SRTP\n srtp_profiles = opt_arg();\n#endif\n break;\n case OPT_KEYMATEXPORT:\n keymatexportlabel = opt_arg();\n break;\n case OPT_KEYMATEXPORTLEN:\n keymatexportlen = atoi(opt_arg());\n break;\n case OPT_ASYNC:\n async = 1;\n break;\n case OPT_SPLIT_SEND_FRAG:\n split_send_fragment = atoi(opt_arg());\n if (split_send_fragment == 0) {\n split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH + 1;\n }\n break;\n case OPT_MAX_PIPELINES:\n max_pipelines = atoi(opt_arg());\n break;\n case OPT_READ_BUF:\n read_buf_len = atoi(opt_arg());\n break;\n }\n }\n argc = opt_num_rest();\n argv = opt_rest();\n#ifndef OPENSSL_NO_DTLS\n if (www && socket_type == SOCK_DGRAM) {\n BIO_printf(bio_err, "Can\'t use -HTTP, -www or -WWW with DTLS\\n");\n goto end;\n }\n if (dtlslisten && socket_type != SOCK_DGRAM) {\n BIO_printf(bio_err, "Can only use -listen with DTLS\\n");\n goto end;\n }\n#endif\n#ifdef AF_UNIX\n if (socket_family == AF_UNIX && socket_type != SOCK_STREAM) {\n BIO_printf(bio_err,\n "Can\'t use unix sockets and datagrams together\\n");\n goto end;\n }\n#endif\n if (split_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {\n BIO_printf(bio_err, "Bad split send fragment size\\n");\n goto end;\n }\n if (max_pipelines > SSL_MAX_PIPELINES) {\n BIO_printf(bio_err, "Bad max pipelines value\\n");\n goto end;\n }\n if (!app_passwd(passarg, dpassarg, &pass, &dpass)) {\n BIO_printf(bio_err, "Error getting password\\n");\n goto end;\n }\n if (s_key_file == NULL)\n s_key_file = s_cert_file;\n if (s_key_file2 == NULL)\n s_key_file2 = s_cert_file2;\n if (!load_excert(&exc))\n goto end;\n if (nocert == 0) {\n s_key = load_key(s_key_file, s_key_format, 0, pass, e,\n "server certificate private key file");\n if (!s_key) {\n ERR_print_errors(bio_err);\n goto end;\n }\n s_cert = load_cert(s_cert_file, s_cert_format,\n "server certificate file");\n if (!s_cert) {\n ERR_print_errors(bio_err);\n goto end;\n }\n if (s_chain_file) {\n if (!load_certs(s_chain_file, &s_chain, FORMAT_PEM, NULL,\n "server certificate chain"))\n goto end;\n }\n if (tlsextcbp.servername) {\n s_key2 = load_key(s_key_file2, s_key_format, 0, pass, e,\n "second server certificate private key file");\n if (!s_key2) {\n ERR_print_errors(bio_err);\n goto end;\n }\n s_cert2 = load_cert(s_cert_file2, s_cert_format,\n "second server certificate file");\n if (!s_cert2) {\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n }\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n if (next_proto_neg_in) {\n size_t len;\n next_proto.data = next_protos_parse(&len, next_proto_neg_in);\n if (next_proto.data == NULL)\n goto end;\n next_proto.len = len;\n } else {\n next_proto.data = NULL;\n }\n#endif\n alpn_ctx.data = NULL;\n if (alpn_in) {\n size_t len;\n alpn_ctx.data = next_protos_parse(&len, alpn_in);\n if (alpn_ctx.data == NULL)\n goto end;\n alpn_ctx.len = len;\n }\n if (crl_file) {\n X509_CRL *crl;\n crl = load_crl(crl_file, crl_format);\n if (!crl) {\n BIO_puts(bio_err, "Error loading CRL\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n crls = sk_X509_CRL_new_null();\n if (!crls || !sk_X509_CRL_push(crls, crl)) {\n BIO_puts(bio_err, "Error adding CRL\\n");\n ERR_print_errors(bio_err);\n X509_CRL_free(crl);\n goto end;\n }\n }\n if (s_dcert_file) {\n if (s_dkey_file == NULL)\n s_dkey_file = s_dcert_file;\n s_dkey = load_key(s_dkey_file, s_dkey_format,\n 0, dpass, e, "second certificate private key file");\n if (!s_dkey) {\n ERR_print_errors(bio_err);\n goto end;\n }\n s_dcert = load_cert(s_dcert_file, s_dcert_format,\n "second server certificate file");\n if (!s_dcert) {\n ERR_print_errors(bio_err);\n goto end;\n }\n if (s_dchain_file) {\n if (!load_certs(s_dchain_file, &s_dchain, FORMAT_PEM, NULL,\n "second server certificate chain"))\n goto end;\n }\n }\n if (!app_RAND_load_file(NULL, 1) && inrand == NULL\n && !RAND_status()) {\n BIO_printf(bio_err,\n "warning, not much extra random data, consider using the -rand option\\n");\n }\n if (inrand != NULL)\n BIO_printf(bio_err, "%ld semi-random bytes loaded\\n",\n app_RAND_load_files(inrand));\n if (bio_s_out == NULL) {\n if (s_quiet && !s_debug) {\n bio_s_out = BIO_new(BIO_s_null());\n if (s_msg && !bio_s_msg)\n bio_s_msg = dup_bio_out(FORMAT_TEXT);\n } else {\n if (bio_s_out == NULL)\n bio_s_out = dup_bio_out(FORMAT_TEXT);\n }\n }\n#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)\n if (nocert)\n#endif\n {\n s_cert_file = NULL;\n s_key_file = NULL;\n s_dcert_file = NULL;\n s_dkey_file = NULL;\n s_cert_file2 = NULL;\n s_key_file2 = NULL;\n }\n ctx = SSL_CTX_new(meth);\n if (ctx == NULL) {\n ERR_print_errors(bio_err);\n goto end;\n }\n if (sdebug)\n ssl_ctx_security_debug(ctx, sdebug);\n if (ssl_config) {\n if (SSL_CTX_config(ctx, ssl_config) == 0) {\n BIO_printf(bio_err, "Error using configuration \\"%s\\"\\n",\n ssl_config);\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n if (SSL_CTX_set_min_proto_version(ctx, min_version) == 0)\n goto end;\n if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)\n goto end;\n if (session_id_prefix) {\n if (strlen(session_id_prefix) >= 32)\n BIO_printf(bio_err,\n "warning: id_prefix is too long, only one new session will be possible\\n");\n if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) {\n BIO_printf(bio_err, "error setting \'id_prefix\'\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n BIO_printf(bio_err, "id_prefix \'%s\' set.\\n", session_id_prefix);\n }\n SSL_CTX_set_quiet_shutdown(ctx, 1);\n if (exc)\n ssl_ctx_set_excert(ctx, exc);\n if (state)\n SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);\n if (no_cache)\n SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);\n else if (ext_cache)\n init_session_cache_ctx(ctx);\n else\n SSL_CTX_sess_set_cache_size(ctx, 128);\n if (async) {\n SSL_CTX_set_mode(ctx, SSL_MODE_ASYNC);\n }\n if (split_send_fragment > 0) {\n SSL_CTX_set_split_send_fragment(ctx, split_send_fragment);\n }\n if (max_pipelines > 0) {\n SSL_CTX_set_max_pipelines(ctx, max_pipelines);\n }\n if (read_buf_len > 0) {\n SSL_CTX_set_default_read_buffer_len(ctx, read_buf_len);\n }\n#ifndef OPENSSL_NO_SRTP\n if (srtp_profiles != NULL) {\n if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) {\n BIO_printf(bio_err, "Error setting SRTP profile\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n#endif\n if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {\n ERR_print_errors(bio_err);\n goto end;\n }\n if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) {\n BIO_printf(bio_err, "Error setting verify params\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n ssl_ctx_add_crls(ctx, crls, 0);\n if (!config_ctx(cctx, ssl_args, ctx))\n goto end;\n if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile,\n crls, crl_download)) {\n BIO_printf(bio_err, "Error loading store locations\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n if (s_cert2) {\n ctx2 = SSL_CTX_new(meth);\n if (ctx2 == NULL) {\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n if (ctx2) {\n BIO_printf(bio_s_out, "Setting secondary ctx parameters\\n");\n if (sdebug)\n ssl_ctx_security_debug(ctx, sdebug);\n if (session_id_prefix) {\n if (strlen(session_id_prefix) >= 32)\n BIO_printf(bio_err,\n "warning: id_prefix is too long, only one new session will be possible\\n");\n if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) {\n BIO_printf(bio_err, "error setting \'id_prefix\'\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n BIO_printf(bio_err, "id_prefix \'%s\' set.\\n", session_id_prefix);\n }\n SSL_CTX_set_quiet_shutdown(ctx2, 1);\n if (exc)\n ssl_ctx_set_excert(ctx2, exc);\n if (state)\n SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback);\n if (no_cache)\n SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF);\n else if (ext_cache)\n init_session_cache_ctx(ctx2);\n else\n SSL_CTX_sess_set_cache_size(ctx2, 128);\n if (async)\n SSL_CTX_set_mode(ctx2, SSL_MODE_ASYNC);\n if ((!SSL_CTX_load_verify_locations(ctx2, CAfile, CApath)) ||\n (!SSL_CTX_set_default_verify_paths(ctx2))) {\n ERR_print_errors(bio_err);\n }\n if (vpmtouched && !SSL_CTX_set1_param(ctx2, vpm)) {\n BIO_printf(bio_err, "Error setting verify params\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n ssl_ctx_add_crls(ctx2, crls, 0);\n if (!config_ctx(cctx, ssl_args, ctx2))\n goto end;\n }\n#ifndef OPENSSL_NO_NEXTPROTONEG\n if (next_proto.data)\n SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb,\n &next_proto);\n#endif\n if (alpn_ctx.data)\n SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx);\n#ifndef OPENSSL_NO_DH\n if (!no_dhe) {\n DH *dh = NULL;\n if (dhfile)\n dh = load_dh_param(dhfile);\n else if (s_cert_file)\n dh = load_dh_param(s_cert_file);\n if (dh != NULL) {\n BIO_printf(bio_s_out, "Setting temp DH parameters\\n");\n } else {\n BIO_printf(bio_s_out, "Using default temp DH parameters\\n");\n }\n (void)BIO_flush(bio_s_out);\n if (dh == NULL)\n SSL_CTX_set_dh_auto(ctx, 1);\n else if (!SSL_CTX_set_tmp_dh(ctx, dh)) {\n BIO_puts(bio_err, "Error setting temp DH parameters\\n");\n ERR_print_errors(bio_err);\n DH_free(dh);\n goto end;\n }\n if (ctx2) {\n if (!dhfile) {\n DH *dh2 = load_dh_param(s_cert_file2);\n if (dh2 != NULL) {\n BIO_printf(bio_s_out, "Setting temp DH parameters\\n");\n (void)BIO_flush(bio_s_out);\n DH_free(dh);\n dh = dh2;\n }\n }\n if (dh == NULL)\n SSL_CTX_set_dh_auto(ctx2, 1);\n else if (!SSL_CTX_set_tmp_dh(ctx2, dh)) {\n BIO_puts(bio_err, "Error setting temp DH parameters\\n");\n ERR_print_errors(bio_err);\n DH_free(dh);\n goto end;\n }\n }\n DH_free(dh);\n }\n#endif\n if (!set_cert_key_stuff(ctx, s_cert, s_key, s_chain, build_chain))\n goto end;\n if (s_serverinfo_file != NULL\n && !SSL_CTX_use_serverinfo_file(ctx, s_serverinfo_file)) {\n ERR_print_errors(bio_err);\n goto end;\n }\n if (ctx2 && !set_cert_key_stuff(ctx2, s_cert2, s_key2, NULL, build_chain))\n goto end;\n if (s_dcert != NULL) {\n if (!set_cert_key_stuff(ctx, s_dcert, s_dkey, s_dchain, build_chain))\n goto end;\n }\n if (no_resume_ephemeral) {\n SSL_CTX_set_not_resumable_session_callback(ctx,\n not_resumable_sess_cb);\n if (ctx2)\n SSL_CTX_set_not_resumable_session_callback(ctx2,\n not_resumable_sess_cb);\n }\n#ifndef OPENSSL_NO_PSK\n if (psk_key != NULL) {\n if (s_debug)\n BIO_printf(bio_s_out,\n "PSK key given, setting server callback\\n");\n SSL_CTX_set_psk_server_callback(ctx, psk_server_cb);\n }\n if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint)) {\n BIO_printf(bio_err, "error setting PSK identity hint to context\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n#endif\n SSL_CTX_set_verify(ctx, s_server_verify, verify_callback);\n if (!SSL_CTX_set_session_id_context(ctx,\n (void *)&s_server_session_id_context,\n sizeof s_server_session_id_context)) {\n BIO_printf(bio_err, "error setting session id context\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);\n SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);\n if (ctx2) {\n SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback);\n if (!SSL_CTX_set_session_id_context(ctx2,\n (void *)&s_server_session_id_context,\n sizeof s_server_session_id_context)) {\n BIO_printf(bio_err, "error setting session id context\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n tlsextcbp.biodebug = bio_s_out;\n SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);\n SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);\n SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);\n SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);\n }\n#ifndef OPENSSL_NO_SRP\n if (srp_verifier_file != NULL) {\n srp_callback_parm.vb = SRP_VBASE_new(srpuserseed);\n srp_callback_parm.user = NULL;\n srp_callback_parm.login = NULL;\n if ((ret =\n SRP_VBASE_init(srp_callback_parm.vb,\n srp_verifier_file)) != SRP_NO_ERROR) {\n BIO_printf(bio_err,\n "Cannot initialize SRP verifier file \\"%s\\":ret=%d\\n",\n srp_verifier_file, ret);\n goto end;\n }\n SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, verify_callback);\n SSL_CTX_set_srp_cb_arg(ctx, &srp_callback_parm);\n SSL_CTX_set_srp_username_callback(ctx, ssl_srp_server_param_cb);\n } else\n#endif\n if (CAfile != NULL) {\n SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile));\n if (ctx2)\n SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile));\n }\n#ifndef OPENSSL_NO_OCSP\n if (s_tlsextstatus) {\n SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);\n SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);\n if (ctx2) {\n SSL_CTX_set_tlsext_status_cb(ctx2, cert_status_cb);\n SSL_CTX_set_tlsext_status_arg(ctx2, &tlscstatp);\n }\n }\n#endif\n BIO_printf(bio_s_out, "ACCEPT\\n");\n (void)BIO_flush(bio_s_out);\n if (rev)\n server_cb = rev_body;\n else if (www)\n server_cb = www_body;\n else\n server_cb = sv_body;\n#ifdef AF_UNIX\n if (socket_family == AF_UNIX\n && unlink_unix_path)\n unlink(host);\n#endif\n do_server(&accept_socket, host, port, socket_family, socket_type,\n server_cb, context, naccept);\n print_stats(bio_s_out, ctx);\n ret = 0;\n end:\n SSL_CTX_free(ctx);\n X509_free(s_cert);\n sk_X509_CRL_pop_free(crls, X509_CRL_free);\n X509_free(s_dcert);\n EVP_PKEY_free(s_key);\n EVP_PKEY_free(s_dkey);\n sk_X509_pop_free(s_chain, X509_free);\n sk_X509_pop_free(s_dchain, X509_free);\n OPENSSL_free(pass);\n OPENSSL_free(dpass);\n OPENSSL_free(host);\n OPENSSL_free(port);\n X509_VERIFY_PARAM_free(vpm);\n free_sessions();\n OPENSSL_free(tlscstatp.host);\n OPENSSL_free(tlscstatp.port);\n OPENSSL_free(tlscstatp.path);\n SSL_CTX_free(ctx2);\n X509_free(s_cert2);\n EVP_PKEY_free(s_key2);\n BIO_free(serverinfo_in);\n#ifndef OPENSSL_NO_NEXTPROTONEG\n OPENSSL_free(next_proto.data);\n#endif\n OPENSSL_free(alpn_ctx.data);\n ssl_excert_free(exc);\n sk_OPENSSL_STRING_free(ssl_args);\n SSL_CONF_CTX_free(cctx);\n BIO_free(bio_s_out);\n bio_s_out = NULL;\n BIO_free(bio_s_msg);\n bio_s_msg = NULL;\n return (ret);\n}', 'X509 *load_cert(const char *file, int format, const char *cert_descrip)\n{\n X509 *x = NULL;\n BIO *cert;\n if (format == FORMAT_HTTP) {\n#if !defined(OPENSSL_NO_OCSP) && !defined(OPENSSL_NO_SOCK)\n load_cert_crl_http(file, &x, NULL);\n#endif\n return x;\n }\n if (file == NULL) {\n unbuffer(stdin);\n cert = dup_bio_in(format);\n } else\n cert = bio_open_default(file, \'r\', format);\n if (cert == NULL)\n goto end;\n if (format == FORMAT_ASN1)\n x = d2i_X509_bio(cert, NULL);\n else if (format == FORMAT_PEM)\n x = PEM_read_bio_X509_AUX(cert, NULL,\n (pem_password_cb *)password_callback, NULL);\n else if (format == FORMAT_PKCS12) {\n if (!load_pkcs12(cert, cert_descrip, NULL, NULL, NULL, &x, NULL))\n goto end;\n } else {\n BIO_printf(bio_err, "bad input format specified for %s\\n", cert_descrip);\n goto end;\n }\n end:\n if (x == NULL) {\n BIO_printf(bio_err, "unable to load certificate\\n");\n ERR_print_errors(bio_err);\n }\n BIO_free(cert);\n return (x);\n}', 'static int load_cert_crl_http(const char *url, X509 **pcert, X509_CRL **pcrl)\n{\n char *host = NULL, *port = NULL, *path = NULL;\n BIO *bio = NULL;\n OCSP_REQ_CTX *rctx = NULL;\n int use_ssl, rv = 0;\n if (!OCSP_parse_url(url, &host, &port, &path, &use_ssl))\n goto err;\n if (use_ssl) {\n BIO_puts(bio_err, "https not supported\\n");\n goto err;\n }\n bio = BIO_new_connect(host);\n if (!bio || !BIO_set_conn_port(bio, port))\n goto err;\n rctx = OCSP_REQ_CTX_new(bio, 1024);\n if (rctx == NULL)\n goto err;\n if (!OCSP_REQ_CTX_http(rctx, "GET", path))\n goto err;\n if (!OCSP_REQ_CTX_add1_header(rctx, "Host", host))\n goto err;\n if (pcert) {\n do {\n rv = X509_http_nbio(rctx, pcert);\n } while (rv == -1);\n } else {\n do {\n rv = X509_CRL_http_nbio(rctx, pcrl);\n } while (rv == -1);\n }\n err:\n OPENSSL_free(host);\n OPENSSL_free(path);\n OPENSSL_free(port);\n if (bio)\n BIO_free_all(bio);\n OCSP_REQ_CTX_free(rctx);\n if (rv != 1) {\n BIO_printf(bio_err, "Error loading %s from %s\\n",\n pcert ? "certificate" : "CRL", url);\n ERR_print_errors(bio_err);\n }\n return rv;\n}', 'int OCSP_parse_url(const char *url, char **phost, char **pport, char **ppath,\n int *pssl)\n{\n char *p, *buf;\n char *host, *port;\n *phost = NULL;\n *pport = NULL;\n *ppath = NULL;\n buf = OPENSSL_strdup(url);\n if (!buf)\n goto mem_err;\n p = strchr(buf, \':\');\n if (!p)\n goto parse_err;\n *(p++) = \'\\0\';\n if (strcmp(buf, "http") == 0) {\n *pssl = 0;\n port = "80";\n } else if (strcmp(buf, "https") == 0) {\n *pssl = 1;\n port = "443";\n } else\n goto parse_err;\n if ((p[0] != \'/\') || (p[1] != \'/\'))\n goto parse_err;\n p += 2;\n host = p;\n p = strchr(p, \'/\');\n if (!p)\n *ppath = OPENSSL_strdup("/");\n else {\n *ppath = OPENSSL_strdup(p);\n *p = \'\\0\';\n }\n if (!*ppath)\n goto mem_err;\n p = host;\n if (host[0] == \'[\') {\n host++;\n p = strchr(host, \']\');\n if (!p)\n goto parse_err;\n *p = \'\\0\';\n p++;\n }\n if ((p = strchr(p, \':\'))) {\n *p = 0;\n port = p + 1;\n }\n *pport = OPENSSL_strdup(port);\n if (!*pport)\n goto mem_err;\n *phost = OPENSSL_strdup(host);\n if (!*phost)\n goto mem_err;\n OPENSSL_free(buf);\n return 1;\n mem_err:\n OCSPerr(OCSP_F_OCSP_PARSE_URL, ERR_R_MALLOC_FAILURE);\n goto err;\n parse_err:\n OCSPerr(OCSP_F_OCSP_PARSE_URL, OCSP_R_ERROR_PARSING_URL);\n err:\n OPENSSL_free(buf);\n OPENSSL_free(*ppath);\n *ppath = NULL;\n OPENSSL_free(*pport);\n *pport = NULL;\n OPENSSL_free(*phost);\n *phost = NULL;\n return 0;\n}', 'unsigned char *next_protos_parse(size_t *outlen, const char *in)\n{\n size_t len;\n unsigned char *out;\n size_t i, start = 0;\n len = strlen(in);\n if (len >= 65535)\n return NULL;\n out = app_malloc(strlen(in) + 1, "NPN buffer");\n for (i = 0; i <= len; ++i) {\n if (i == len || in[i] == \',\') {\n if (i - start > 255) {\n OPENSSL_free(out);\n return NULL;\n }\n out[start] = i - start;\n start = i + 1;\n } else\n out[i + 1] = in[i];\n }\n *outlen = len + 1;\n return out;\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 (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,756
0
https://github.com/libav/libav/blob/8f935b9271052be8f97d655081b94b68b6c23bfb/libavformat/gxfenc.c/#L197
static int gxf_write_mpeg_auxiliary(AVIOContext *pb, AVStream *st) { GXFStreamContext *sc = st->priv_data; char buffer[1024]; int size, starting_line; if (sc->iframes) { sc->p_per_gop = sc->pframes / sc->iframes; if (sc->pframes % sc->iframes) sc->p_per_gop++; if (sc->pframes) { sc->b_per_i_or_p = sc->bframes / sc->pframes; if (sc->bframes % sc->pframes) sc->b_per_i_or_p++; } if (sc->p_per_gop > 9) sc->p_per_gop = 9; if (sc->b_per_i_or_p > 9) sc->b_per_i_or_p = 9; } if (st->codec->height == 512 || st->codec->height == 608) starting_line = 7; else if (st->codec->height == 480) starting_line = 20; else starting_line = 23; size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n" "Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n", (float)st->codec->bit_rate, sc->p_per_gop, sc->b_per_i_or_p, st->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, sc->first_gop_closed == 1, starting_line, (st->codec->height + 15) / 16); avio_w8(pb, TRACK_MPG_AUX); avio_w8(pb, size + 1); avio_write(pb, (uint8_t *)buffer, size + 1); return size + 3; }
['static int gxf_write_mpeg_auxiliary(AVIOContext *pb, AVStream *st)\n{\n GXFStreamContext *sc = st->priv_data;\n char buffer[1024];\n int size, starting_line;\n if (sc->iframes) {\n sc->p_per_gop = sc->pframes / sc->iframes;\n if (sc->pframes % sc->iframes)\n sc->p_per_gop++;\n if (sc->pframes) {\n sc->b_per_i_or_p = sc->bframes / sc->pframes;\n if (sc->bframes % sc->pframes)\n sc->b_per_i_or_p++;\n }\n if (sc->p_per_gop > 9)\n sc->p_per_gop = 9;\n if (sc->b_per_i_or_p > 9)\n sc->b_per_i_or_p = 9;\n }\n if (st->codec->height == 512 || st->codec->height == 608)\n starting_line = 7;\n else if (st->codec->height == 480)\n starting_line = 20;\n else\n starting_line = 23;\n size = snprintf(buffer, 1024, "Ver 1\\nBr %.6f\\nIpg 1\\nPpi %d\\nBpiop %d\\n"\n "Pix 0\\nCf %d\\nCg %d\\nSl %d\\nnl16 %d\\nVi 1\\nf1 1\\n",\n (float)st->codec->bit_rate, sc->p_per_gop, sc->b_per_i_or_p,\n st->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, sc->first_gop_closed == 1,\n starting_line, (st->codec->height + 15) / 16);\n avio_w8(pb, TRACK_MPG_AUX);\n avio_w8(pb, size + 1);\n avio_write(pb, (uint8_t *)buffer, size + 1);\n return size + 3;\n}']
35,757
0
https://github.com/openssl/openssl/blob/c922ebe23247ff9ee07310fa30647623c0547cd9/ssl/packet.c/#L49
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) { assert(pkt->subs != NULL && len != 0); if (pkt->subs == NULL || len == 0) return 0; if (pkt->maxsize - pkt->written < len) return 0; if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) { size_t newlen; size_t reflen; reflen = (len > pkt->buf->length) ? len : pkt->buf->length; if (reflen > SIZE_MAX / 2) { newlen = SIZE_MAX; } else { newlen = reflen * 2; if (newlen < DEFAULT_BUF_SIZE) newlen = DEFAULT_BUF_SIZE; } if (BUF_MEM_grow(pkt->buf, newlen) == 0) return 0; } if (allocbytes != NULL) *allocbytes = WPACKET_get_curr(pkt); return 1; }
['int tls_construct_stoc_server_name(SSL *s, WPACKET *pkt, int *al)\n{\n if (s->hit || s->servername_done != 1\n || s->session->tlsext_hostname == NULL)\n return 1;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)\n || !WPACKET_put_bytes_u16(pkt, 0)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_STOC_SERVER_NAME, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n return 1;\n}', 'int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)\n{\n unsigned char *data;\n assert(size <= sizeof(unsigned int));\n if (size > sizeof(unsigned int)\n || !WPACKET_allocate_bytes(pkt, size, &data)\n || !put_value(data, val, size))\n return 0;\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n assert(pkt->subs != NULL && len != 0);\n if (pkt->subs == NULL || len == 0)\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {\n size_t newlen;\n size_t reflen;\n reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n if (reflen > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = reflen * 2;\n if (newlen < DEFAULT_BUF_SIZE)\n newlen = DEFAULT_BUF_SIZE;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n if (allocbytes != NULL)\n *allocbytes = WPACKET_get_curr(pkt);\n return 1;\n}']
35,758
0
https://github.com/openssl/openssl/blob/49619ab008a651e49e7524c73ba6fb4b3c96f67f/test/ssltest_old.c/#L351
static int cb_server_alpn(SSL *s, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { unsigned char *protos; size_t protos_len; char* alpn_str = arg; protos = next_protos_parse(&protos_len, alpn_str); if (protos == NULL) { fprintf(stderr, "failed to parser ALPN server protocol string: %s\n", alpn_str); abort(); } if (SSL_select_next_proto ((unsigned char **)out, outlen, protos, protos_len, in, inlen) != OPENSSL_NPN_NEGOTIATED) { OPENSSL_free(protos); return SSL_TLSEXT_ERR_NOACK; } alpn_selected = OPENSSL_malloc(*outlen); memcpy(alpn_selected, *out, *outlen); *out = alpn_selected; OPENSSL_free(protos); return SSL_TLSEXT_ERR_OK; }
['static int cb_server_alpn(SSL *s, const unsigned char **out,\n unsigned char *outlen, const unsigned char *in,\n unsigned int inlen, void *arg)\n{\n unsigned char *protos;\n size_t protos_len;\n char* alpn_str = arg;\n protos = next_protos_parse(&protos_len, alpn_str);\n if (protos == NULL) {\n fprintf(stderr, "failed to parser ALPN server protocol string: %s\\n",\n alpn_str);\n abort();\n }\n if (SSL_select_next_proto\n ((unsigned char **)out, outlen, protos, protos_len, in,\n inlen) != OPENSSL_NPN_NEGOTIATED) {\n OPENSSL_free(protos);\n return SSL_TLSEXT_ERR_NOACK;\n }\n alpn_selected = OPENSSL_malloc(*outlen);\n memcpy(alpn_selected, *out, *outlen);\n *out = alpn_selected;\n OPENSSL_free(protos);\n return SSL_TLSEXT_ERR_OK;\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}']
35,759
0
https://gitlab.com/libtiff/libtiff/blob/6c63f17749ad8fc2e1e94e056b18cbc557ab59f1/tools/tiff2ps.c/#L2298
int PS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h) { uint16 fillorder; int use_rawdata, tiled_image, breaklen = MAXLINE; uint32 chunk_no, num_chunks; uint64 *bc; unsigned char *buf_data, *cp; tsize_t chunk_size, byte_count; #if defined( EXP_ASCII85ENCODER ) tsize_t ascii85_l; uint8 * ascii85_p = 0; #endif PS_Lvl2colorspace(fd, tif); use_rawdata = PS_Lvl2ImageDict(fd, tif, w, h); #ifdef ENABLE_BROKEN_BEGINENDDATA fputs("%%BeginData:\n", fd); #endif fputs("exec\n", fd); tiled_image = TIFFIsTiled(tif); if (tiled_image) { num_chunks = TIFFNumberOfTiles(tif); TIFFGetField(tif, TIFFTAG_TILEBYTECOUNTS, &bc); } else { num_chunks = TIFFNumberOfStrips(tif); TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc); } if (use_rawdata) { chunk_size = (tsize_t) bc[0]; for (chunk_no = 1; chunk_no < num_chunks; chunk_no++) if ((tsize_t) bc[chunk_no] > chunk_size) chunk_size = (tsize_t) bc[chunk_no]; } else { if (tiled_image) chunk_size = TIFFTileSize(tif); else chunk_size = TIFFStripSize(tif); } buf_data = (unsigned char *)limitMalloc(chunk_size); if (!buf_data) { TIFFError(filename, "Can't alloc %lu bytes for %s.", (unsigned long) chunk_size, tiled_image ? "tiles" : "strips"); return(FALSE); } #if defined( EXP_ASCII85ENCODER ) if ( ascii85 ) { ascii85_p = limitMalloc( (chunk_size+(chunk_size/2)) + 8 ); if ( !ascii85_p ) { _TIFFfree( buf_data ); TIFFError( filename, "Cannot allocate ASCII85 encoding buffer." ); return ( FALSE ); } } #endif TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder); for (chunk_no = 0; chunk_no < num_chunks; chunk_no++) { if (ascii85) Ascii85Init(); else breaklen = MAXLINE; if (use_rawdata) { if (tiled_image) byte_count = TIFFReadRawTile(tif, chunk_no, buf_data, chunk_size); else byte_count = TIFFReadRawStrip(tif, chunk_no, buf_data, chunk_size); if (fillorder == FILLORDER_LSB2MSB) TIFFReverseBits(buf_data, byte_count); } else { if (tiled_image) byte_count = TIFFReadEncodedTile(tif, chunk_no, buf_data, chunk_size); else byte_count = TIFFReadEncodedStrip(tif, chunk_no, buf_data, chunk_size); } if (byte_count < 0) { TIFFError(filename, "Can't read %s %d.", tiled_image ? "tile" : "strip", chunk_no); if (ascii85) Ascii85Put('\0', fd); } if (bitspersample == 16 && !TIFFIsBigEndian(tif)) { PS_FlipBytes(buf_data, byte_count); } if (alpha) { int adjust, i, j = 0; int ncomps = samplesperpixel - extrasamples; for (i = 0; (i + ncomps) < byte_count; i+=samplesperpixel) { adjust = 255 - buf_data[i + ncomps]; switch (ncomps) { case 1: buf_data[j++] = buf_data[i] + adjust; break; case 2: buf_data[j++] = buf_data[i] + adjust; buf_data[j++] = buf_data[i+1] + adjust; break; case 3: buf_data[j++] = buf_data[i] + adjust; buf_data[j++] = buf_data[i+1] + adjust; buf_data[j++] = buf_data[i+2] + adjust; break; } } byte_count -= j; } if (ascii85) { #if defined( EXP_ASCII85ENCODER ) ascii85_l = Ascii85EncodeBlock(ascii85_p, 1, buf_data, byte_count ); if ( ascii85_l > 0 ) fwrite( ascii85_p, ascii85_l, 1, fd ); #else for (cp = buf_data; byte_count > 0; byte_count--) Ascii85Put(*cp++, fd); #endif } else { for (cp = buf_data; byte_count > 0; byte_count--) { putc(hex[((*cp)>>4)&0xf], fd); putc(hex[(*cp)&0xf], fd); cp++; if (--breaklen <= 0) { putc('\n', fd); breaklen = MAXLINE; } } } if ( !ascii85 ) { if ( level2 || level3 ) putc( '>', fd ); putc('\n', fd); } #if !defined( EXP_ASCII85ENCODER ) else Ascii85Flush(fd); #endif } #if defined( EXP_ASCII85ENCODER ) if ( ascii85_p ) _TIFFfree( ascii85_p ); #endif _TIFFfree(buf_data); #ifdef ENABLE_BROKEN_BEGINENDDATA fputs("%%EndData\n", fd); #endif return(TRUE); }
['int\nPS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h)\n{\n\tuint16 fillorder;\n\tint use_rawdata, tiled_image, breaklen = MAXLINE;\n\tuint32 chunk_no, num_chunks;\n uint64 *bc;\n\tunsigned char *buf_data, *cp;\n\ttsize_t chunk_size, byte_count;\n#if defined( EXP_ASCII85ENCODER )\n\ttsize_t\t\t\tascii85_l;\n\tuint8\t\t*\tascii85_p = 0;\n#endif\n\tPS_Lvl2colorspace(fd, tif);\n\tuse_rawdata = PS_Lvl2ImageDict(fd, tif, w, h);\n#ifdef ENABLE_BROKEN_BEGINENDDATA\n\tfputs("%%BeginData:\\n", fd);\n#endif\n\tfputs("exec\\n", fd);\n\ttiled_image = TIFFIsTiled(tif);\n\tif (tiled_image) {\n\t\tnum_chunks = TIFFNumberOfTiles(tif);\n\t\tTIFFGetField(tif, TIFFTAG_TILEBYTECOUNTS, &bc);\n\t} else {\n\t\tnum_chunks = TIFFNumberOfStrips(tif);\n\t\tTIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc);\n\t}\n\tif (use_rawdata) {\n\t\tchunk_size = (tsize_t) bc[0];\n\t\tfor (chunk_no = 1; chunk_no < num_chunks; chunk_no++)\n\t\t\tif ((tsize_t) bc[chunk_no] > chunk_size)\n\t\t\t\tchunk_size = (tsize_t) bc[chunk_no];\n\t} else {\n\t\tif (tiled_image)\n\t\t\tchunk_size = TIFFTileSize(tif);\n\t\telse\n\t\t\tchunk_size = TIFFStripSize(tif);\n\t}\n\tbuf_data = (unsigned char *)limitMalloc(chunk_size);\n\tif (!buf_data) {\n\t\tTIFFError(filename, "Can\'t alloc %lu bytes for %s.",\n\t\t\t(unsigned long) chunk_size, tiled_image ? "tiles" : "strips");\n\t\treturn(FALSE);\n\t}\n#if defined( EXP_ASCII85ENCODER )\n\tif ( ascii85 ) {\n\t ascii85_p = limitMalloc( (chunk_size+(chunk_size/2)) + 8 );\n\t if ( !ascii85_p ) {\n\t\t_TIFFfree( buf_data );\n\t\tTIFFError( filename, "Cannot allocate ASCII85 encoding buffer." );\n\t\treturn ( FALSE );\n\t }\n\t}\n#endif\n\tTIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder);\n\tfor (chunk_no = 0; chunk_no < num_chunks; chunk_no++) {\n\t\tif (ascii85)\n\t\t\tAscii85Init();\n\t\telse\n\t\t\tbreaklen = MAXLINE;\n\t\tif (use_rawdata) {\n\t\t\tif (tiled_image)\n\t\t\t\tbyte_count = TIFFReadRawTile(tif, chunk_no,\n\t\t\t\t\t\t buf_data, chunk_size);\n\t\t\telse\n\t\t\t\tbyte_count = TIFFReadRawStrip(tif, chunk_no,\n\t\t\t\t\t\t buf_data, chunk_size);\n\t\t\tif (fillorder == FILLORDER_LSB2MSB)\n\t\t\t TIFFReverseBits(buf_data, byte_count);\n\t\t} else {\n\t\t\tif (tiled_image)\n\t\t\t\tbyte_count = TIFFReadEncodedTile(tif,\n\t\t\t\t\t\tchunk_no, buf_data,\n\t\t\t\t\t\tchunk_size);\n\t\t\telse\n\t\t\t\tbyte_count = TIFFReadEncodedStrip(tif,\n\t\t\t\t\t\tchunk_no, buf_data,\n\t\t\t\t\t\tchunk_size);\n\t\t}\n\t\tif (byte_count < 0) {\n\t\t\tTIFFError(filename, "Can\'t read %s %d.",\n\t\t\t\ttiled_image ? "tile" : "strip", chunk_no);\n\t\t\tif (ascii85)\n\t\t\t\tAscii85Put(\'\\0\', fd);\n\t\t}\n\t\tif (bitspersample == 16 && !TIFFIsBigEndian(tif)) {\n\t\t\tPS_FlipBytes(buf_data, byte_count);\n\t\t}\n\t\tif (alpha) {\n\t\t\tint adjust, i, j = 0;\n\t\t\tint ncomps = samplesperpixel - extrasamples;\n\t\t\tfor (i = 0; (i + ncomps) < byte_count; i+=samplesperpixel) {\n\t\t\t\tadjust = 255 - buf_data[i + ncomps];\n\t\t\t\tswitch (ncomps) {\n\t\t\t\t\tcase 1:\n\t\t\t\t\t\tbuf_data[j++] = buf_data[i] + adjust;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 2:\n\t\t\t\t\t\tbuf_data[j++] = buf_data[i] + adjust;\n\t\t\t\t\t\tbuf_data[j++] = buf_data[i+1] + adjust;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 3:\n\t\t\t\t\t\tbuf_data[j++] = buf_data[i] + adjust;\n\t\t\t\t\t\tbuf_data[j++] = buf_data[i+1] + adjust;\n\t\t\t\t\t\tbuf_data[j++] = buf_data[i+2] + adjust;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbyte_count -= j;\n\t\t}\n\t\tif (ascii85) {\n#if defined( EXP_ASCII85ENCODER )\n\t\t\tascii85_l = Ascii85EncodeBlock(ascii85_p, 1, buf_data, byte_count );\n\t\t\tif ( ascii85_l > 0 )\n\t\t\t\tfwrite( ascii85_p, ascii85_l, 1, fd );\n#else\n\t\t\tfor (cp = buf_data; byte_count > 0; byte_count--)\n\t\t\t\tAscii85Put(*cp++, fd);\n#endif\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfor (cp = buf_data; byte_count > 0; byte_count--) {\n\t\t\t\tputc(hex[((*cp)>>4)&0xf], fd);\n\t\t\t\tputc(hex[(*cp)&0xf], fd);\n\t\t\t\tcp++;\n\t\t\t\tif (--breaklen <= 0) {\n\t\t\t\t\tputc(\'\\n\', fd);\n\t\t\t\t\tbreaklen = MAXLINE;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif ( !ascii85 ) {\n\t\t\tif ( level2 || level3 )\n\t\t\t\tputc( \'>\', fd );\n\t\t\tputc(\'\\n\', fd);\n\t\t}\n#if !defined( EXP_ASCII85ENCODER )\n\t\telse\n\t\t\tAscii85Flush(fd);\n#endif\n\t}\n#if defined( EXP_ASCII85ENCODER )\n\tif ( ascii85_p )\n\t _TIFFfree( ascii85_p );\n#endif\n\t_TIFFfree(buf_data);\n#ifdef ENABLE_BROKEN_BEGINENDDATA\n\tfputs("%%EndData\\n", fd);\n#endif\n\treturn(TRUE);\n}', 'static void* limitMalloc(tmsize_t s)\n{\n\tif (maxMalloc && (s > maxMalloc)) {\n\t\tfprintf(stderr, "MemoryLimitError: allocation of " TIFF_UINT64_FORMAT " bytes is forbidden. Limit is " TIFF_UINT64_FORMAT ".\\n",\n\t\t (uint64)s, (uint64)maxMalloc);\n\t\tfprintf(stderr, " use -M option to change limit.\\n");\n\t\treturn NULL;\n\t}\n\treturn _TIFFmalloc(s);\n}']
35,760
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_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 BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return -1;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\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 (!aa || !val[0])\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}', '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 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}', '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,761
0
https://github.com/libav/libav/blob/f255a28d140a64ea4c1a5060061863aec993b5ea/libavcodec/mp3_header_compress_bsf.c/#L67
static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe){ uint32_t header, extraheader; int mode_extension, header_size; if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){ av_log(avctx, AV_LOG_ERROR, "not standards compliant\n"); return -1; } header = AV_RB32(buf); mode_extension= (header>>4)&3; if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){ output_unchanged: *poutbuf= (uint8_t *) buf; *poutbuf_size= buf_size; av_log(avctx, AV_LOG_INFO, "cannot compress %08X\n", header); return 0; } if(avctx->extradata_size == 0){ avctx->extradata_size=15; avctx->extradata= av_malloc(avctx->extradata_size); strcpy(avctx->extradata, "FFCMP3 0.0"); memcpy(avctx->extradata+11, buf, 4); } if(avctx->extradata_size != 15){ av_log(avctx, AV_LOG_ERROR, "Extradata invalid\n"); return -1; } extraheader = AV_RB32(avctx->extradata+11); if((extraheader&MP3_MASK) != (header&MP3_MASK)) goto output_unchanged; header_size= (header&0x10000) ? 4 : 6; *poutbuf_size= buf_size - header_size; *poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE); memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE); if(avctx->channels==2){ if((header & (3<<19)) != 3<<19){ (*poutbuf)[1] &= 0x3F; (*poutbuf)[1] |= mode_extension<<6; FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]); }else{ (*poutbuf)[1] &= 0x8F; (*poutbuf)[1] |= mode_extension<<4; } } return 1; }
['static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,\n uint8_t **poutbuf, int *poutbuf_size,\n const uint8_t *buf, int buf_size, int keyframe){\n uint32_t header, extraheader;\n int mode_extension, header_size;\n if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){\n av_log(avctx, AV_LOG_ERROR, "not standards compliant\\n");\n return -1;\n }\n header = AV_RB32(buf);\n mode_extension= (header>>4)&3;\n if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){\noutput_unchanged:\n *poutbuf= (uint8_t *) buf;\n *poutbuf_size= buf_size;\n av_log(avctx, AV_LOG_INFO, "cannot compress %08X\\n", header);\n return 0;\n }\n if(avctx->extradata_size == 0){\n avctx->extradata_size=15;\n avctx->extradata= av_malloc(avctx->extradata_size);\n strcpy(avctx->extradata, "FFCMP3 0.0");\n memcpy(avctx->extradata+11, buf, 4);\n }\n if(avctx->extradata_size != 15){\n av_log(avctx, AV_LOG_ERROR, "Extradata invalid\\n");\n return -1;\n }\n extraheader = AV_RB32(avctx->extradata+11);\n if((extraheader&MP3_MASK) != (header&MP3_MASK))\n goto output_unchanged;\n header_size= (header&0x10000) ? 4 : 6;\n *poutbuf_size= buf_size - header_size;\n *poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);\n memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);\n if(avctx->channels==2){\n if((header & (3<<19)) != 3<<19){\n (*poutbuf)[1] &= 0x3F;\n (*poutbuf)[1] |= mode_extension<<6;\n FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]);\n }else{\n (*poutbuf)[1] &= 0x8F;\n (*poutbuf)[1] |= mode_extension<<4;\n }\n }\n return 1;\n}', 'static av_always_inline av_const uint32_t av_bswap32(uint32_t x)\n{\n x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);\n x= (x>>16) | (x<<16);\n return x;\n}', 'static inline int ff_mpa_check_header(uint32_t header){\n if ((header & 0xffe00000) != 0xffe00000)\n return -1;\n if ((header & (3<<17)) == 0)\n return -1;\n if ((header & (0xf<<12)) == 0xf<<12)\n return -1;\n if ((header & (3<<10)) == 3<<10)\n return -1;\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) )\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,762
0
https://github.com/libav/libav/blob/e5b0fc170f85b00f7dd0ac514918fb5c95253d39/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_rle(uint8_t *bitmap, int linesize, int w, int h,\n const uint8_t *buf, int start, int buf_size, int is_8bit)\n{\n BitstreamContext bc;\n int bit_len;\n int x, y, len, color;\n uint8_t *d;\n bit_len = (buf_size - start) * 8;\n bitstream_init(&bc, buf + start, bit_len);\n x = 0;\n y = 0;\n d = bitmap;\n for(;;) {\n if (bitstream_tell(&bc) > bit_len)\n return -1;\n if (is_8bit)\n len = decode_run_8bit(&bc, &color);\n else\n len = decode_run_2bit(&bc, &color);\n len = FFMIN(len, w - x);\n memset(d + x, color, len);\n x += len;\n if (x >= w) {\n y++;\n if (y >= h)\n break;\n d += linesize;\n x = 0;\n bitstream_align(&bc);\n }\n }\n return 0;\n}', 'static inline int bitstream_init(BitstreamContext *bc, const uint8_t *buffer,\n unsigned bit_size)\n{\n unsigned buffer_size;\n if (bit_size > INT_MAX - 7 || !buffer) {\n buffer =\n bc->buffer =\n bc->ptr = NULL;\n bc->bits_left = 0;\n return AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n bc->buffer = buffer;\n bc->buffer_end = buffer + buffer_size;\n bc->ptr = bc->buffer;\n bc->size_in_bits = bit_size;\n bc->bits_left = 0;\n bc->bits = 0;\n refill_64(bc);\n return 0;\n}', 'static int decode_run_8bit(BitstreamContext *bc, int *color)\n{\n int len;\n int has_run = bitstream_read_bit(bc);\n if (bitstream_read_bit(bc))\n *color = bitstream_read(bc, 8);\n else\n *color = bitstream_read(bc, 2);\n if (has_run) {\n if (bitstream_read_bit(bc)) {\n len = bitstream_read(bc, 7);\n if (len == 0)\n len = INT_MAX;\n else\n len += 9;\n } else\n len = bitstream_read(bc, 3) + 2;\n } else\n len = 1;\n return len;\n}', 'static inline unsigned bitstream_read_bit(BitstreamContext *bc)\n{\n if (!bc->bits_left)\n refill_64(bc);\n return get_val(bc, 1);\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,763
0
https://github.com/openssl/openssl/blob/6e9fa57c6ddde7df49983251373a05cd663aac22/crypto/pem/pvkfmt.c/#L788
static int i2b_PVK(unsigned char **out, EVP_PKEY *pk, int enclevel, pem_password_cb *cb, void *u) { int outlen = 24, pklen; unsigned char *p, *salt = NULL; EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new(); if (enclevel) outlen += PVK_SALTLEN; pklen = do_i2b(NULL, pk, 0); if (pklen < 0) return -1; outlen += pklen; if (!out) return outlen; if (*out) p = *out; else { p = OPENSSL_malloc(outlen); if (p == NULL) { PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE); return -1; } *out = p; } write_ledword(&p, MS_PVKMAGIC); write_ledword(&p, 0); if (EVP_PKEY_id(pk) == EVP_PKEY_DSA) write_ledword(&p, MS_KEYTYPE_SIGN); else write_ledword(&p, MS_KEYTYPE_KEYX); write_ledword(&p, enclevel ? 1 : 0); write_ledword(&p, enclevel ? PVK_SALTLEN : 0); write_ledword(&p, pklen); if (enclevel) { if (RAND_bytes(p, PVK_SALTLEN) <= 0) goto error; salt = p; p += PVK_SALTLEN; } do_i2b(&p, pk, 0); if (enclevel == 0) return outlen; else { char psbuf[PEM_BUFSIZE]; unsigned char keybuf[20]; int enctmplen, inlen; if (cb) inlen = cb(psbuf, PEM_BUFSIZE, 1, u); else inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u); if (inlen <= 0) { PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ); goto error; } if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN, (unsigned char *)psbuf, inlen)) goto error; if (enclevel == 1) memset(keybuf + 5, 0, 11); p = salt + PVK_SALTLEN + 8; if (!EVP_EncryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL)) goto error; OPENSSL_cleanse(keybuf, 20); if (!EVP_DecryptUpdate(cctx, p, &enctmplen, p, pklen - 8)) goto error; if (!EVP_DecryptFinal_ex(cctx, p + enctmplen, &enctmplen)) goto error; } EVP_CIPHER_CTX_free(cctx); return outlen; error: EVP_CIPHER_CTX_free(cctx); return -1; }
['static int i2b_PVK(unsigned char **out, EVP_PKEY *pk, int enclevel,\n pem_password_cb *cb, void *u)\n{\n int outlen = 24, pklen;\n unsigned char *p, *salt = NULL;\n EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();\n if (enclevel)\n outlen += PVK_SALTLEN;\n pklen = do_i2b(NULL, pk, 0);\n if (pklen < 0)\n return -1;\n outlen += pklen;\n if (!out)\n return outlen;\n if (*out)\n p = *out;\n else {\n p = OPENSSL_malloc(outlen);\n if (p == NULL) {\n PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n *out = p;\n }\n write_ledword(&p, MS_PVKMAGIC);\n write_ledword(&p, 0);\n if (EVP_PKEY_id(pk) == EVP_PKEY_DSA)\n write_ledword(&p, MS_KEYTYPE_SIGN);\n else\n write_ledword(&p, MS_KEYTYPE_KEYX);\n write_ledword(&p, enclevel ? 1 : 0);\n write_ledword(&p, enclevel ? PVK_SALTLEN : 0);\n write_ledword(&p, pklen);\n if (enclevel) {\n if (RAND_bytes(p, PVK_SALTLEN) <= 0)\n goto error;\n salt = p;\n p += PVK_SALTLEN;\n }\n do_i2b(&p, pk, 0);\n if (enclevel == 0)\n return outlen;\n else {\n char psbuf[PEM_BUFSIZE];\n unsigned char keybuf[20];\n int enctmplen, inlen;\n if (cb)\n inlen = cb(psbuf, PEM_BUFSIZE, 1, u);\n else\n inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);\n if (inlen <= 0) {\n PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ);\n goto error;\n }\n if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,\n (unsigned char *)psbuf, inlen))\n goto error;\n if (enclevel == 1)\n memset(keybuf + 5, 0, 11);\n p = salt + PVK_SALTLEN + 8;\n if (!EVP_EncryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))\n goto error;\n OPENSSL_cleanse(keybuf, 20);\n if (!EVP_DecryptUpdate(cctx, p, &enctmplen, p, pklen - 8))\n goto error;\n if (!EVP_DecryptFinal_ex(cctx, p + enctmplen, &enctmplen))\n goto error;\n }\n EVP_CIPHER_CTX_free(cctx);\n return outlen;\n error:\n EVP_CIPHER_CTX_free(cctx);\n return -1;\n}', 'EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_CIPHER_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,764
0
https://github.com/openssl/openssl/blob/d65c3615f6c658478503f4862f8055203a98038c/crypto/bn/bn_lib.c/#L289
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 RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e,\n BN_GENCB *cb)\n{\n int ok = 0;\n BIGNUM *Xp = NULL, *Xq = NULL;\n BN_CTX *ctx = NULL;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto error;\n BN_CTX_start(ctx);\n Xp = BN_CTX_get(ctx);\n Xq = BN_CTX_get(ctx);\n if (!BN_X931_generate_Xpq(Xp, Xq, bits, ctx))\n goto error;\n rsa->p = BN_new();\n rsa->q = BN_new();\n if (rsa->p == NULL || rsa->q == NULL)\n goto error;\n if (!BN_X931_generate_prime_ex(rsa->p, NULL, NULL, NULL, NULL, Xp,\n e, ctx, cb))\n goto error;\n if (!BN_X931_generate_prime_ex(rsa->q, NULL, NULL, NULL, NULL, Xq,\n e, ctx, cb))\n goto error;\n if (!RSA_X931_derive_ex(rsa, NULL, NULL, NULL, NULL,\n NULL, NULL, NULL, NULL, NULL, NULL, e, cb))\n goto error;\n ok = 1;\n error:\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n if (ok)\n return 1;\n return 0;\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_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx)\n{\n BIGNUM *t;\n int i;\n if ((nbits < 1024) || (nbits & 0xff))\n return 0;\n nbits >>= 1;\n if (!BN_rand(Xp, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY))\n goto err;\n BN_CTX_start(ctx);\n t = BN_CTX_get(ctx);\n for (i = 0; i < 1000; i++) {\n if (!BN_rand(Xq, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY))\n goto err;\n BN_sub(t, Xp, Xq);\n if (BN_num_bits(t) > (nbits - 100))\n break;\n }\n BN_CTX_end(ctx);\n if (i < 1000)\n return 1;\n return 0;\n err:\n BN_CTX_end(ctx);\n return 0;\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 pseudorand, 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 (pseudorand == 2) {\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}', '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,765
0
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_ctx.c/#L276
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n BIGNUM *r1, *m1, *vrfy, *r2, *m[RSA_MAX_PRIME_NUM - 2];\n int ret = 0, i, ex_primes = 0, smooth = 0;\n RSA_PRIME_INFO *pinfo;\n BN_CTX_start(ctx);\n r1 = BN_CTX_get(ctx);\n r2 = BN_CTX_get(ctx);\n m1 = BN_CTX_get(ctx);\n vrfy = BN_CTX_get(ctx);\n if (vrfy == NULL)\n goto err;\n if (rsa->version == RSA_ASN1_VERSION_MULTI\n && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0\n || ex_primes > RSA_MAX_PRIME_NUM - 2))\n goto err;\n if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {\n BIGNUM *factor = BN_new();\n if (factor == NULL)\n goto err;\n if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),\n BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,\n factor, ctx))\n || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),\n BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,\n factor, ctx))) {\n BN_free(factor);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);\n if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {\n BN_free(factor);\n goto err;\n }\n }\n BN_free(factor);\n smooth = (ex_primes == 0)\n && (rsa->meth->bn_mod_exp == BN_mod_exp_mont)\n && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));\n }\n if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,\n rsa->n, ctx))\n goto err;\n if (smooth) {\n if (\n !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)\n || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)\n || !BN_mod_exp_mont_consttime(m1, m1, rsa->dmq1, rsa->q, ctx,\n rsa->_method_mod_q)\n || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)\n || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n || !BN_mod_exp_mont_consttime(r1, r1, rsa->dmp1, rsa->p, ctx,\n rsa->_method_mod_p)\n || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)\n || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,\n ctx)\n || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)\n || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))\n goto err;\n goto tail;\n }\n {\n BIGNUM *c = BN_new();\n if (c == NULL)\n goto err;\n BN_with_flags(c, I, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, c, rsa->q, ctx)) {\n BN_free(c);\n goto err;\n }\n {\n BIGNUM *dmq1 = BN_new();\n if (dmq1 == NULL) {\n BN_free(c);\n goto err;\n }\n BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,\n rsa->_method_mod_q)) {\n BN_free(c);\n BN_free(dmq1);\n goto err;\n }\n BN_free(dmq1);\n }\n if (!BN_mod(r1, c, rsa->p, ctx)) {\n BN_free(c);\n goto err;\n }\n BN_free(c);\n }\n {\n BIGNUM *dmp1 = BN_new();\n if (dmp1 == NULL)\n goto err;\n BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,\n rsa->_method_mod_p)) {\n BN_free(dmp1);\n goto err;\n }\n BN_free(dmp1);\n }\n if (ex_primes > 0) {\n BIGNUM *di = BN_new(), *cc = BN_new();\n if (cc == NULL || di == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n if ((m[i] = BN_CTX_get(ctx)) == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(cc, I, BN_FLG_CONSTTIME);\n BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, cc, pinfo->r, ctx)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n }\n BN_free(cc);\n BN_free(di);\n }\n if (!BN_sub(r0, r0, m1))\n goto err;\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->iqmp, ctx))\n goto err;\n {\n BIGNUM *pr1 = BN_new();\n if (pr1 == NULL)\n goto err;\n BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);\n if (!BN_mod(r0, pr1, rsa->p, ctx)) {\n BN_free(pr1);\n goto err;\n }\n BN_free(pr1);\n }\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->q, ctx))\n goto err;\n if (!BN_add(r0, r1, m1))\n goto err;\n if (ex_primes > 0) {\n BIGNUM *pr2 = BN_new();\n if (pr2 == NULL)\n goto err;\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n if (!BN_sub(r1, m[i], r0)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r2, r1, pinfo->t, ctx)) {\n BN_free(pr2);\n goto err;\n }\n BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, pr2, pinfo->r, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (BN_is_negative(r1))\n if (!BN_add(r1, r1, pinfo->r)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r1, r1, pinfo->pp, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_add(r0, r0, r1)) {\n BN_free(pr2);\n goto err;\n }\n }\n BN_free(pr2);\n }\n tail:\n if (rsa->e && rsa->n) {\n if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {\n if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n } else {\n bn_correct_top(r0);\n if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n }\n if (!BN_sub(vrfy, vrfy, I))\n goto err;\n if (BN_is_zero(vrfy)) {\n bn_correct_top(r0);\n ret = 1;\n goto err;\n }\n if (!BN_mod(vrfy, vrfy, rsa->n, ctx))\n goto err;\n if (BN_is_negative(vrfy))\n if (!BN_add(vrfy, vrfy, rsa->n))\n goto err;\n if (!BN_is_zero(vrfy)) {\n BIGNUM *d = BN_new();\n if (d == NULL)\n goto err;\n BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,\n rsa->_method_mod_n)) {\n BN_free(d);\n goto err;\n }\n BN_free(d);\n }\n }\n bn_correct_top(r0);\n ret = 1;\n err:\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_from_mont_fixed_top(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n int retn = 0;\n#ifdef MONT_WORD\n BIGNUM *t;\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) && BN_copy(t, a)) {\n retn = bn_from_montgomery_word(ret, t, mont);\n }\n BN_CTX_end(ctx);\n#else\n BIGNUM *t1, *t2;\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 (!BN_copy(t1, a))\n goto err;\n BN_mask_bits(t1, mont->ri);\n if (!BN_mul(t2, t1, &mont->Ni, ctx))\n goto err;\n BN_mask_bits(t2, mont->ri);\n if (!BN_mul(t1, t2, &mont->N, ctx))\n goto err;\n if (!BN_add(t2, a, t1))\n goto err;\n if (!BN_rshift(ret, t2, mont->ri))\n goto err;\n if (BN_ucmp(ret, &(mont->N)) >= 0) {\n if (!BN_usub(ret, ret, &(mont->N)))\n goto err;\n }\n retn = 1;\n bn_check_top(ret);\n err:\n BN_CTX_end(ctx);\n#endif\n return retn;\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}', '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_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}', '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}', '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}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
35,766
0
https://github.com/libav/libav/blob/606cc8afa1cb782311f68560c8f9bad978cdcc32/libavcodec/pngdec.c/#L236
static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type, uint8_t *src, uint8_t *last, int size, int bpp) { int i, p, r, g, b, a; switch (filter_type) { case PNG_FILTER_VALUE_NONE: memcpy(dst, src, size); break; case PNG_FILTER_VALUE_SUB: for (i = 0; i < bpp; i++) dst[i] = src[i]; if (bpp == 4) { p = *(int *)dst; for (; i < size; i += bpp) { int s = *(int *)(src + i); p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080); *(int *)(dst + i) = p; } } else { #define OP_SUB(x, s, l) x + s UNROLL_FILTER(OP_SUB); } break; case PNG_FILTER_VALUE_UP: dsp->add_bytes_l2(dst, src, last, size); break; case PNG_FILTER_VALUE_AVG: for (i = 0; i < bpp; i++) { p = (last[i] >> 1); dst[i] = p + src[i]; } #define OP_AVG(x, s, l) (((x + l) >> 1) + s) & 0xff UNROLL_FILTER(OP_AVG); break; case PNG_FILTER_VALUE_PAETH: for (i = 0; i < bpp; i++) { p = last[i]; dst[i] = p + src[i]; } if (bpp > 1 && size > 4) { int w = bpp == 4 ? size : size - 3; dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp); i = w; } ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp); break; } }
['static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,\n uint8_t *src, uint8_t *last, int size, int bpp)\n{\n int i, p, r, g, b, a;\n switch (filter_type) {\n case PNG_FILTER_VALUE_NONE:\n memcpy(dst, src, size);\n break;\n case PNG_FILTER_VALUE_SUB:\n for (i = 0; i < bpp; i++)\n dst[i] = src[i];\n if (bpp == 4) {\n p = *(int *)dst;\n for (; i < size; i += bpp) {\n int s = *(int *)(src + i);\n p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);\n *(int *)(dst + i) = p;\n }\n } else {\n#define OP_SUB(x, s, l) x + s\n UNROLL_FILTER(OP_SUB);\n }\n break;\n case PNG_FILTER_VALUE_UP:\n dsp->add_bytes_l2(dst, src, last, size);\n break;\n case PNG_FILTER_VALUE_AVG:\n for (i = 0; i < bpp; i++) {\n p = (last[i] >> 1);\n dst[i] = p + src[i];\n }\n#define OP_AVG(x, s, l) (((x + l) >> 1) + s) & 0xff\n UNROLL_FILTER(OP_AVG);\n break;\n case PNG_FILTER_VALUE_PAETH:\n for (i = 0; i < bpp; i++) {\n p = last[i];\n dst[i] = p + src[i];\n }\n if (bpp > 1 && size > 4) {\n int w = bpp == 4 ? size : size - 3;\n dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp);\n i = w;\n }\n ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);\n break;\n }\n}']
35,767
0
https://github.com/nginx/nginx/blob/2b2def789199b1fc661fbac38ff6bf7fdccb7bbb/src/http/modules/ngx_http_userid_filter_module.c/#L475
static ngx_int_t ngx_http_userid_create_uid(ngx_http_request_t *r, ngx_http_userid_ctx_t *ctx, ngx_http_userid_conf_t *conf) { ngx_connection_t *c; struct sockaddr_in *sin; ngx_http_variable_value_t *vv; #if (NGX_HAVE_INET6) u_char *p; struct sockaddr_in6 *sin6; #endif if (ctx->uid_set[3] != 0) { return NGX_OK; } if (ctx->uid_got[3] != 0) { vv = ngx_http_get_indexed_variable(r, ngx_http_userid_reset_index); if (vv->len == 0 || (vv->len == 1 && vv->data[0] == '0')) { if (conf->mark == '\0' || (ctx->cookie.len > 23 && ctx->cookie.data[22] == conf->mark && ctx->cookie.data[23] == '=')) { return NGX_OK; } ctx->uid_set[0] = ctx->uid_got[0]; ctx->uid_set[1] = ctx->uid_got[1]; ctx->uid_set[2] = ctx->uid_got[2]; ctx->uid_set[3] = ctx->uid_got[3]; return NGX_OK; } else { ctx->reset = 1; if (vv->len == 3 && ngx_strncmp(vv->data, "log", 3) == 0) { ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, "userid cookie \"%V=%08XD%08XD%08XD%08XD\" was reset", &conf->name, ctx->uid_got[0], ctx->uid_got[1], ctx->uid_got[2], ctx->uid_got[3]); } } } if (conf->enable == NGX_HTTP_USERID_V1) { if (conf->service == NGX_CONF_UNSET) { ctx->uid_set[0] = 0; } else { ctx->uid_set[0] = conf->service; } ctx->uid_set[1] = (uint32_t) ngx_time(); ctx->uid_set[2] = start_value; ctx->uid_set[3] = sequencer_v1; sequencer_v1 += 0x100; } else { if (conf->service == NGX_CONF_UNSET) { c = r->connection; if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) { return NGX_ERROR; } switch (c->local_sockaddr->sa_family) { #if (NGX_HAVE_INET6) case AF_INET6: sin6 = (struct sockaddr_in6 *) c->local_sockaddr; p = (u_char *) &ctx->uid_set[0]; *p++ = sin6->sin6_addr.s6_addr[12]; *p++ = sin6->sin6_addr.s6_addr[13]; *p++ = sin6->sin6_addr.s6_addr[14]; *p = sin6->sin6_addr.s6_addr[15]; break; #endif default: sin = (struct sockaddr_in *) c->local_sockaddr; ctx->uid_set[0] = sin->sin_addr.s_addr; break; } } else { ctx->uid_set[0] = htonl(conf->service); } ctx->uid_set[1] = htonl((uint32_t) ngx_time()); ctx->uid_set[2] = htonl(start_value); ctx->uid_set[3] = htonl(sequencer_v2); sequencer_v2 += 0x100; if (sequencer_v2 < 0x03030302) { sequencer_v2 = 0x03030302; } } return NGX_OK; }
['static ngx_int_t\nngx_http_userid_create_uid(ngx_http_request_t *r, ngx_http_userid_ctx_t *ctx,\n ngx_http_userid_conf_t *conf)\n{\n ngx_connection_t *c;\n struct sockaddr_in *sin;\n ngx_http_variable_value_t *vv;\n#if (NGX_HAVE_INET6)\n u_char *p;\n struct sockaddr_in6 *sin6;\n#endif\n if (ctx->uid_set[3] != 0) {\n return NGX_OK;\n }\n if (ctx->uid_got[3] != 0) {\n vv = ngx_http_get_indexed_variable(r, ngx_http_userid_reset_index);\n if (vv->len == 0 || (vv->len == 1 && vv->data[0] == \'0\')) {\n if (conf->mark == \'\\0\'\n || (ctx->cookie.len > 23\n && ctx->cookie.data[22] == conf->mark\n && ctx->cookie.data[23] == \'=\'))\n {\n return NGX_OK;\n }\n ctx->uid_set[0] = ctx->uid_got[0];\n ctx->uid_set[1] = ctx->uid_got[1];\n ctx->uid_set[2] = ctx->uid_got[2];\n ctx->uid_set[3] = ctx->uid_got[3];\n return NGX_OK;\n } else {\n ctx->reset = 1;\n if (vv->len == 3 && ngx_strncmp(vv->data, "log", 3) == 0) {\n ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,\n "userid cookie \\"%V=%08XD%08XD%08XD%08XD\\" was reset",\n &conf->name, ctx->uid_got[0], ctx->uid_got[1],\n ctx->uid_got[2], ctx->uid_got[3]);\n }\n }\n }\n if (conf->enable == NGX_HTTP_USERID_V1) {\n if (conf->service == NGX_CONF_UNSET) {\n ctx->uid_set[0] = 0;\n } else {\n ctx->uid_set[0] = conf->service;\n }\n ctx->uid_set[1] = (uint32_t) ngx_time();\n ctx->uid_set[2] = start_value;\n ctx->uid_set[3] = sequencer_v1;\n sequencer_v1 += 0x100;\n } else {\n if (conf->service == NGX_CONF_UNSET) {\n c = r->connection;\n if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {\n return NGX_ERROR;\n }\n switch (c->local_sockaddr->sa_family) {\n#if (NGX_HAVE_INET6)\n case AF_INET6:\n sin6 = (struct sockaddr_in6 *) c->local_sockaddr;\n p = (u_char *) &ctx->uid_set[0];\n *p++ = sin6->sin6_addr.s6_addr[12];\n *p++ = sin6->sin6_addr.s6_addr[13];\n *p++ = sin6->sin6_addr.s6_addr[14];\n *p = sin6->sin6_addr.s6_addr[15];\n break;\n#endif\n default:\n sin = (struct sockaddr_in *) c->local_sockaddr;\n ctx->uid_set[0] = sin->sin_addr.s_addr;\n break;\n }\n } else {\n ctx->uid_set[0] = htonl(conf->service);\n }\n ctx->uid_set[1] = htonl((uint32_t) ngx_time());\n ctx->uid_set[2] = htonl(start_value);\n ctx->uid_set[3] = htonl(sequencer_v2);\n sequencer_v2 += 0x100;\n if (sequencer_v2 < 0x03030302) {\n sequencer_v2 = 0x03030302;\n }\n }\n return NGX_OK;\n}', 'ngx_http_variable_value_t *\nngx_http_get_indexed_variable(ngx_http_request_t *r, ngx_uint_t index)\n{\n ngx_http_variable_t *v;\n ngx_http_core_main_conf_t *cmcf;\n cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);\n if (cmcf->variables.nelts <= index) {\n ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,\n "unknown variable index: %ui", index);\n return NULL;\n }\n if (r->variables[index].not_found || r->variables[index].valid) {\n return &r->variables[index];\n }\n v = cmcf->variables.elts;\n if (v[index].get_handler(r, &r->variables[index], v[index].data)\n == NGX_OK)\n {\n if (v[index].flags & NGX_HTTP_VAR_NOCACHEABLE) {\n r->variables[index].no_cacheable = 1;\n }\n return &r->variables[index];\n }\n r->variables[index].valid = 0;\n r->variables[index].not_found = 1;\n return NULL;\n}']
35,768
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_ctx.c/#L328
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,\n const EC_POINT *b, BN_CTX *ctx)\n{\n BIGNUM *aX, *aY, *bX, *bY;\n BN_CTX *new_ctx = NULL;\n int ret = -1;\n if (EC_POINT_is_at_infinity(group, a)) {\n return EC_POINT_is_at_infinity(group, b) ? 0 : 1;\n }\n if (EC_POINT_is_at_infinity(group, b))\n return 1;\n if (a->Z_is_one && b->Z_is_one) {\n return ((BN_cmp(a->X, b->X) == 0) && BN_cmp(a->Y, b->Y) == 0) ? 0 : 1;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return -1;\n }\n BN_CTX_start(ctx);\n aX = BN_CTX_get(ctx);\n aY = BN_CTX_get(ctx);\n bX = BN_CTX_get(ctx);\n bY = BN_CTX_get(ctx);\n if (bY == NULL)\n goto err;\n if (!EC_POINT_get_affine_coordinates_GF2m(group, a, aX, aY, ctx))\n goto err;\n if (!EC_POINT_get_affine_coordinates_GF2m(group, b, bX, bY, ctx))\n goto err;\n ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1;\n err:\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(new_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}', '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,769
0
https://github.com/openssl/openssl/blob/2d5d70b15559f9813054ddb11b30b816daf62ebe/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 (dif) memcpy(rp, ap, sizeof(*rp) * dif); r->top = max; r->neg = 0; bn_correct_top(r); return (1); }
['int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const BIGNUM *m)\n{\n if (!BN_uadd(r, a, b))\n return 0;\n if (BN_ucmp(r, m) >= 0)\n return BN_usub(r, r, m);\n return 1;\n}', 'int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n const BN_ULONG *ap, *bp;\n BN_ULONG *rp, carry, t1, t2;\n const BIGNUM *tmp;\n bn_check_top(a);\n bn_check_top(b);\n if (a->top < b->top) {\n tmp = a;\n a = b;\n b = tmp;\n }\n max = a->top;\n min = b->top;\n dif = max - min;\n if (bn_wexpand(r, max + 1) == NULL)\n return 0;\n r->top = max;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n carry = bn_add_words(rp, ap, bp, min);\n rp += min;\n ap += min;\n bp += min;\n if (carry) {\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 + 1) & BN_MASK2;\n *(rp++) = t2;\n if (t2) {\n carry = 0;\n break;\n }\n }\n if (carry) {\n *rp = 1;\n r->top++;\n }\n }\n if (dif && rp != ap)\n while (dif--)\n *(rp++) = *(ap++);\n r->neg = 0;\n bn_check_top(r);\n return 1;\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_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 (dif)\n memcpy(rp, ap, sizeof(*rp) * dif);\n r->top = max;\n r->neg = 0;\n bn_correct_top(r);\n return (1);\n}']
35,770
0
https://github.com/openssl/openssl/blob/a5a95f8d65c2c616ebee13ae4b33eacde34bb2d3/crypto/bn/bn_lib.c/#L289
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 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 pseudorand, 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 || (bits == 1 && top > 0)) {\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n }\n if (bits == 0) {\n BN_zero(rnd);\n return 1;\n }\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 (pseudorand == 2) {\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);\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, *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,771
0
https://github.com/openssl/openssl/blob/9dd4ac8cf17f2afd636e85ae0111d1df4104a475/crypto/bn/bn_rand.c/#L82
static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom) { unsigned char *buf = NULL; int ret = 0, bit, bytes, mask; time_t tim; if (bits == 0) { if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY) goto toosmall; BN_zero(rnd); return 1; } if (bits < 0 || (bits == 1 && top > 0)) goto toosmall; bytes = (bits + 7) / 8; bit = (bits - 1) % 8; mask = 0xff << (bit + 1); buf = OPENSSL_malloc(bytes); if (buf == NULL) { BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE); goto err; } time(&tim); RAND_add(&tim, sizeof(tim), 0.0); if (RAND_bytes(buf, bytes) <= 0) goto err; if (pseudorand == 2) { int i; unsigned char c; for (i = 0; i < bytes; i++) { if (RAND_bytes(&c, 1) <= 0) goto err; if (c >= 128 && i > 0) buf[i] = buf[i - 1]; else if (c < 42) buf[i] = 0; else if (c < 84) buf[i] = 255; } } if (top >= 0) { if (top) { if (bit == 0) { buf[0] = 1; buf[1] |= 0x80; } else { buf[0] |= (3 << (bit - 1)); } } else { buf[0] |= (1 << bit); } } buf[0] &= ~mask; if (bottom) buf[bytes - 1] |= 1; if (!BN_bin2bn(buf, bytes, rnd)) goto err; ret = 1; err: OPENSSL_clear_free(buf, bytes); bn_check_top(rnd); return (ret); toosmall: BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL); return 0; }
['static int test_gf2m_mod()\n{\n static int p0[] = { 163, 7, 6, 3, 0, -1 };\n static int p1[] = { 193, 15, 0, -1 };\n BIGNUM *a, *b[2], *c, *d, *e;\n int i, j, st = 0;\n a = BN_new();\n b[0] = BN_new();\n b[1] = BN_new();\n c = BN_new();\n d = BN_new();\n e = BN_new();\n BN_GF2m_arr2poly(p0, b[0]);\n BN_GF2m_arr2poly(p1, b[1]);\n for (i = 0; i < NUM0; i++) {\n BN_bntest_rand(a, 1024, 0, 0);\n for (j = 0; j < 2; j++) {\n BN_GF2m_mod(c, a, b[j]);\n BN_GF2m_add(d, a, c);\n BN_GF2m_mod(e, d, b[j]);\n if (!BN_is_zero(e)) {\n printf("GF(2^m) modulo test failed!\\n");\n goto err;\n }\n }\n }\n st = 1;\n err:\n BN_free(a);\n BN_free(b[0]);\n BN_free(b[1]);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n return st;\n}', 'int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(2, rnd, bits, top, bottom);\n}', 'static int bnrand(int pseudorand, 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 (pseudorand == 2) {\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}', '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}']
35,772
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/adxenc.c/#L120
static av_cold int adx_encode_init(AVCodecContext *avctx) { if (avctx->channels > 2) return -1; avctx->frame_size = 32; avctx->coded_frame= avcodec_alloc_frame(); avctx->coded_frame->key_frame= 1; av_log(avctx, AV_LOG_DEBUG, "adx encode init\n"); return 0; }
['static av_cold int adx_encode_init(AVCodecContext *avctx)\n{\n if (avctx->channels > 2)\n return -1;\n avctx->frame_size = 32;\n avctx->coded_frame= avcodec_alloc_frame();\n avctx->coded_frame->key_frame= 1;\n av_log(avctx, AV_LOG_DEBUG, "adx encode init\\n");\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,773
0
https://github.com/libav/libav/blob/1ef8ff4534706de0b1da3442f380be58a650acf2/libavcodec/mjpegdec.c/#L885
static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al, const uint8_t *mb_bitmask, const AVFrame *reference) { int i, mb_x, mb_y; uint8_t *data[MAX_COMPONENTS]; const uint8_t *reference_data[MAX_COMPONENTS]; int linesize[MAX_COMPONENTS]; GetBitContext mb_bitmask_gb; if (mb_bitmask) init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height); if (s->flipped && s->avctx->flags & CODEC_FLAG_EMU_EDGE) { av_log(s->avctx, AV_LOG_ERROR, "Can not flip image with CODEC_FLAG_EMU_EDGE set!\n"); s->flipped = 0; } for (i = 0; i < nb_components; i++) { int c = s->comp_index[i]; data[c] = s->picture_ptr->data[c]; reference_data[c] = reference ? reference->data[c] : NULL; linesize[c] = s->linesize[c]; s->coefs_finished[c] |= 1; if (s->flipped) { int offset = (linesize[c] * (s->v_scount[i] * (8 * s->mb_height - ((s->height / s->v_max) & 7)) - 1)); data[c] += offset; reference_data[c] += offset; linesize[c] *= -1; } } for (mb_y = 0; mb_y < s->mb_height; mb_y++) { for (mb_x = 0; mb_x < s->mb_width; mb_x++) { const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb); if (s->restart_interval && !s->restart_count) s->restart_count = s->restart_interval; if (get_bits_count(&s->gb)>s->gb.size_in_bits) { av_log(s->avctx, AV_LOG_ERROR, "overread %d\n", get_bits_count(&s->gb) - s->gb.size_in_bits); return -1; } for (i = 0; i < nb_components; i++) { uint8_t *ptr; int n, h, v, x, y, c, j; int block_offset; n = s->nb_blocks[i]; c = s->comp_index[i]; h = s->h_scount[i]; v = s->v_scount[i]; x = 0; y = 0; for (j = 0; j < n; j++) { block_offset = (((linesize[c] * (v * mb_y + y) * 8) + (h * mb_x + x) * 8) >> s->avctx->lowres); if (s->interlaced && s->bottom_field) block_offset += linesize[c] >> 1; ptr = data[c] + block_offset; if (!s->progressive) { if (copy_mb) mjpeg_copy_block(ptr, reference_data[c] + block_offset, linesize[c], s->avctx->lowres); else { s->dsp.clear_block(s->block); if (decode_block(s, s->block, i, s->dc_index[i], s->ac_index[i], s->quant_matrixes[s->quant_index[c]]) < 0) { av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); return -1; } s->dsp.idct_put(ptr, linesize[c], s->block); } } else { int block_idx = s->block_stride[c] * (v * mb_y + y) + (h * mb_x + x); DCTELEM *block = s->blocks[c][block_idx]; if (Ah) block[0] += get_bits1(&s->gb) * s->quant_matrixes[s->quant_index[c]][0] << Al; else if (decode_dc_progressive(s, block, i, s->dc_index[i], s->quant_matrixes[s->quant_index[c]], Al) < 0) { av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); return -1; } } if (++x == h) { x = 0; y++; } } } if (s->restart_interval) { s->restart_count--; i = 8 + ((-get_bits_count(&s->gb)) & 7); if (show_bits(&s->gb, i) == (1 << i) - 1) { int pos = get_bits_count(&s->gb); align_get_bits(&s->gb); while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF) skip_bits(&s->gb, 8); if ((get_bits(&s->gb, 8) & 0xF8) == 0xD0) { for (i = 0; i < nb_components; i++) s->last_dc[i] = 1024; } else skip_bits_long(&s->gb, pos - get_bits_count(&s->gb)); } } } } return 0; }
['static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,\n int Al, const uint8_t *mb_bitmask,\n const AVFrame *reference)\n{\n int i, mb_x, mb_y;\n uint8_t *data[MAX_COMPONENTS];\n const uint8_t *reference_data[MAX_COMPONENTS];\n int linesize[MAX_COMPONENTS];\n GetBitContext mb_bitmask_gb;\n if (mb_bitmask)\n init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);\n if (s->flipped && s->avctx->flags & CODEC_FLAG_EMU_EDGE) {\n av_log(s->avctx, AV_LOG_ERROR,\n "Can not flip image with CODEC_FLAG_EMU_EDGE set!\\n");\n s->flipped = 0;\n }\n for (i = 0; i < nb_components; i++) {\n int c = s->comp_index[i];\n data[c] = s->picture_ptr->data[c];\n reference_data[c] = reference ? reference->data[c] : NULL;\n linesize[c] = s->linesize[c];\n s->coefs_finished[c] |= 1;\n if (s->flipped) {\n int offset = (linesize[c] * (s->v_scount[i] *\n (8 * s->mb_height - ((s->height / s->v_max) & 7)) - 1));\n data[c] += offset;\n reference_data[c] += offset;\n linesize[c] *= -1;\n }\n }\n for (mb_y = 0; mb_y < s->mb_height; mb_y++) {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);\n if (s->restart_interval && !s->restart_count)\n s->restart_count = s->restart_interval;\n if (get_bits_count(&s->gb)>s->gb.size_in_bits) {\n av_log(s->avctx, AV_LOG_ERROR, "overread %d\\n",\n get_bits_count(&s->gb) - s->gb.size_in_bits);\n return -1;\n }\n for (i = 0; i < nb_components; i++) {\n uint8_t *ptr;\n int n, h, v, x, y, c, j;\n int block_offset;\n n = s->nb_blocks[i];\n c = s->comp_index[i];\n h = s->h_scount[i];\n v = s->v_scount[i];\n x = 0;\n y = 0;\n for (j = 0; j < n; j++) {\n block_offset = (((linesize[c] * (v * mb_y + y) * 8) +\n (h * mb_x + x) * 8) >> s->avctx->lowres);\n if (s->interlaced && s->bottom_field)\n block_offset += linesize[c] >> 1;\n ptr = data[c] + block_offset;\n if (!s->progressive) {\n if (copy_mb)\n mjpeg_copy_block(ptr, reference_data[c] + block_offset,\n linesize[c], s->avctx->lowres);\n else {\n s->dsp.clear_block(s->block);\n if (decode_block(s, s->block, i,\n s->dc_index[i], s->ac_index[i],\n s->quant_matrixes[s->quant_index[c]]) < 0) {\n av_log(s->avctx, AV_LOG_ERROR,\n "error y=%d x=%d\\n", mb_y, mb_x);\n return -1;\n }\n s->dsp.idct_put(ptr, linesize[c], s->block);\n }\n } else {\n int block_idx = s->block_stride[c] * (v * mb_y + y) +\n (h * mb_x + x);\n DCTELEM *block = s->blocks[c][block_idx];\n if (Ah)\n block[0] += get_bits1(&s->gb) *\n s->quant_matrixes[s->quant_index[c]][0] << Al;\n else if (decode_dc_progressive(s, block, i, s->dc_index[i],\n s->quant_matrixes[s->quant_index[c]],\n Al) < 0) {\n av_log(s->avctx, AV_LOG_ERROR,\n "error y=%d x=%d\\n", mb_y, mb_x);\n return -1;\n }\n }\n if (++x == h) {\n x = 0;\n y++;\n }\n }\n }\n if (s->restart_interval) {\n s->restart_count--;\n i = 8 + ((-get_bits_count(&s->gb)) & 7);\n if (show_bits(&s->gb, i) == (1 << i) - 1) {\n int pos = get_bits_count(&s->gb);\n align_get_bits(&s->gb);\n while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)\n skip_bits(&s->gb, 8);\n if ((get_bits(&s->gb, 8) & 0xF8) == 0xD0) {\n for (i = 0; i < nb_components; i++)\n s->last_dc[i] = 1024;\n } else\n skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));\n }\n }\n }\n }\n return 0;\n}']
35,774
0
https://github.com/libav/libav/blob/1a5fdf9519d7f4b9a1bdda623516c37744f569bd/libavcodec/g2meet.c/#L550
static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c, GetByteContext *gb) { int i, j, k; uint8_t *dst; uint32_t bits; uint32_t cur_size, cursor_w, cursor_h, cursor_stride; uint32_t cursor_hot_x, cursor_hot_y; int cursor_fmt, err; cur_size = bytestream2_get_be32(gb); cursor_w = bytestream2_get_byte(gb); cursor_h = bytestream2_get_byte(gb); cursor_hot_x = bytestream2_get_byte(gb); cursor_hot_y = bytestream2_get_byte(gb); cursor_fmt = bytestream2_get_byte(gb); cursor_stride = FFALIGN(cursor_w, 32) * 4; if (cursor_w < 1 || cursor_w > 256 || cursor_h < 1 || cursor_h > 256) { av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %dx%d\n", cursor_w, cursor_h); return AVERROR_INVALIDDATA; } if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) { av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %d,%d\n", cursor_hot_x, cursor_hot_y); cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1); cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1); } if (cur_size - 9 > bytestream2_get_bytes_left(gb) || c->cursor_w * c->cursor_h / 4 > cur_size) { av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %d/%d\n", cur_size, bytestream2_get_bytes_left(gb)); return AVERROR_INVALIDDATA; } if (cursor_fmt != 1 && cursor_fmt != 32) { avpriv_report_missing_feature(avctx, "Cursor format %d", cursor_fmt); return AVERROR_PATCHWELCOME; } if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) { av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n"); return err; } c->cursor_w = cursor_w; c->cursor_h = cursor_h; c->cursor_hot_x = cursor_hot_x; c->cursor_hot_y = cursor_hot_y; c->cursor_fmt = cursor_fmt; c->cursor_stride = cursor_stride; dst = c->cursor; switch (c->cursor_fmt) { case 1: for (j = 0; j < c->cursor_h; j++) { for (i = 0; i < c->cursor_w; i += 32) { bits = bytestream2_get_be32(gb); for (k = 0; k < 32; k++) { dst[0] = !!(bits & 0x80000000); dst += 4; bits <<= 1; } } dst += c->cursor_stride - c->cursor_w * 4; } dst = c->cursor; for (j = 0; j < c->cursor_h; j++) { for (i = 0; i < c->cursor_w; i += 32) { bits = bytestream2_get_be32(gb); for (k = 0; k < 32; k++) { int mask_bit = !!(bits & 0x80000000); switch (dst[0] * 2 + mask_bit) { case 0: dst[0] = 0xFF; dst[1] = 0x00; dst[2] = 0x00; dst[3] = 0x00; break; case 1: dst[0] = 0xFF; dst[1] = 0xFF; dst[2] = 0xFF; dst[3] = 0xFF; break; default: dst[0] = 0x00; dst[1] = 0x00; dst[2] = 0x00; dst[3] = 0x00; } dst += 4; bits <<= 1; } } dst += c->cursor_stride - c->cursor_w * 4; } break; case 32: bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3)); for (j = 0; j < c->cursor_h; j++) { for (i = 0; i < c->cursor_w; i++) { int val = bytestream2_get_be32(gb); *dst++ = val >> 0; *dst++ = val >> 8; *dst++ = val >> 16; *dst++ = val >> 24; } dst += c->cursor_stride - c->cursor_w * 4; } break; default: return AVERROR_PATCHWELCOME; } return 0; }
['static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c,\n GetByteContext *gb)\n{\n int i, j, k;\n uint8_t *dst;\n uint32_t bits;\n uint32_t cur_size, cursor_w, cursor_h, cursor_stride;\n uint32_t cursor_hot_x, cursor_hot_y;\n int cursor_fmt, err;\n cur_size = bytestream2_get_be32(gb);\n cursor_w = bytestream2_get_byte(gb);\n cursor_h = bytestream2_get_byte(gb);\n cursor_hot_x = bytestream2_get_byte(gb);\n cursor_hot_y = bytestream2_get_byte(gb);\n cursor_fmt = bytestream2_get_byte(gb);\n cursor_stride = FFALIGN(cursor_w, 32) * 4;\n if (cursor_w < 1 || cursor_w > 256 ||\n cursor_h < 1 || cursor_h > 256) {\n av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %dx%d\\n",\n cursor_w, cursor_h);\n return AVERROR_INVALIDDATA;\n }\n if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {\n av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %d,%d\\n",\n cursor_hot_x, cursor_hot_y);\n cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1);\n cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1);\n }\n if (cur_size - 9 > bytestream2_get_bytes_left(gb) ||\n c->cursor_w * c->cursor_h / 4 > cur_size) {\n av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %d/%d\\n",\n cur_size, bytestream2_get_bytes_left(gb));\n return AVERROR_INVALIDDATA;\n }\n if (cursor_fmt != 1 && cursor_fmt != 32) {\n avpriv_report_missing_feature(avctx, "Cursor format %d",\n cursor_fmt);\n return AVERROR_PATCHWELCOME;\n }\n if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\\n");\n return err;\n }\n c->cursor_w = cursor_w;\n c->cursor_h = cursor_h;\n c->cursor_hot_x = cursor_hot_x;\n c->cursor_hot_y = cursor_hot_y;\n c->cursor_fmt = cursor_fmt;\n c->cursor_stride = cursor_stride;\n dst = c->cursor;\n switch (c->cursor_fmt) {\n case 1:\n for (j = 0; j < c->cursor_h; j++) {\n for (i = 0; i < c->cursor_w; i += 32) {\n bits = bytestream2_get_be32(gb);\n for (k = 0; k < 32; k++) {\n dst[0] = !!(bits & 0x80000000);\n dst += 4;\n bits <<= 1;\n }\n }\n dst += c->cursor_stride - c->cursor_w * 4;\n }\n dst = c->cursor;\n for (j = 0; j < c->cursor_h; j++) {\n for (i = 0; i < c->cursor_w; i += 32) {\n bits = bytestream2_get_be32(gb);\n for (k = 0; k < 32; k++) {\n int mask_bit = !!(bits & 0x80000000);\n switch (dst[0] * 2 + mask_bit) {\n case 0:\n dst[0] = 0xFF; dst[1] = 0x00;\n dst[2] = 0x00; dst[3] = 0x00;\n break;\n case 1:\n dst[0] = 0xFF; dst[1] = 0xFF;\n dst[2] = 0xFF; dst[3] = 0xFF;\n break;\n default:\n dst[0] = 0x00; dst[1] = 0x00;\n dst[2] = 0x00; dst[3] = 0x00;\n }\n dst += 4;\n bits <<= 1;\n }\n }\n dst += c->cursor_stride - c->cursor_w * 4;\n }\n break;\n case 32:\n bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));\n for (j = 0; j < c->cursor_h; j++) {\n for (i = 0; i < c->cursor_w; i++) {\n int val = bytestream2_get_be32(gb);\n *dst++ = val >> 0;\n *dst++ = val >> 8;\n *dst++ = val >> 16;\n *dst++ = val >> 24;\n }\n dst += c->cursor_stride - c->cursor_w * 4;\n }\n break;\n default:\n return AVERROR_PATCHWELCOME;\n }\n return 0;\n}']
35,775
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/lhash/lhash.c/#L370
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_server_certificate(SSL *s)\n\t{\n\tint al,i,ok,ret= -1;\n\tunsigned long n,nc,llen,l;\n\tX509 *x=NULL;\n\tunsigned char *p,*d,*q;\n\tSTACK_OF(X509) *sk=NULL;\n\tCERT *c;\n\tEVP_PKEY *pkey=NULL;\n\tn=ssl3_get_message(s,\n\t\tSSL3_ST_CR_CERT_A,\n\t\tSSL3_ST_CR_CERT_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\tif (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)\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)\n\t\t{\n\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);\n\t\tgoto f_err;\n\t\t}\n\td=p=(unsigned char *)s->init_buf->data;\n\tif ((sk=sk_X509_new_null()) == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tn2l3(p,llen);\n\tif (llen+3 != n)\n\t\t{\n\t\tal=SSL_AD_DECODE_ERROR;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);\n\t\tgoto f_err;\n\t\t}\n\tfor (nc=0; nc<llen; )\n\t\t{\n\t\tn2l3(p,l);\n\t\tif ((l+nc+3) > llen)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tq=p;\n\t\tx=d2i_X509(NULL,&q,l);\n\t\tif (x == NULL)\n\t\t\t{\n\t\t\tal=SSL_AD_BAD_CERTIFICATE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (q != (p+l))\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!sk_X509_push(sk,x))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto err;\n\t\t\t}\n\t\tx=NULL;\n\t\tnc+=l+3;\n\t\tp=q;\n\t\t}\n\ti=ssl_verify_cert_chain(s,sk);\n if ((s->verify_mode != SSL_VERIFY_NONE) && (!i))\n\t\t{\n\t\tal=ssl_verify_alarm_type(s->verify_result);\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);\n\t\tgoto f_err;\n\t\t}\n\tc=ssl_cert_new();\n\tif (c == NULL) goto err;\n\tif (s->session->cert) ssl_cert_free(s->session->cert);\n\ts->session->cert=c;\n\tc->cert_chain=sk;\n\tx=sk_X509_value(sk,0);\n\tsk=NULL;\n\tpkey=X509_get_pubkey(x);\n\tif ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))\n\t\t{\n\t\tx=NULL;\n\t\tal=SSL3_AL_FATAL;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);\n\t\tgoto f_err;\n\t\t}\n\ti=ssl_cert_type(x,pkey);\n\tif (i < 0)\n\t\t{\n\t\tx=NULL;\n\t\tal=SSL3_AL_FATAL;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNKNOWN_CERTIFICATE_TYPE);\n\t\tgoto f_err;\n\t\t}\n\tc->cert_type=i;\n\tCRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);\n\tif (c->pkeys[i].x509 != NULL)\n\t\tX509_free(c->pkeys[i].x509);\n\tc->pkeys[i].x509=x;\n\tc->key= &(c->pkeys[i]);\n\tif ((s->session != NULL) && (s->session->peer != NULL))\n\t\tX509_free(s->session->peer);\n\tCRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);\n\ts->session->peer=x;\n\tx=NULL;\n\tret=1;\n\tif (0)\n\t\t{\nf_err:\n\t\tssl3_send_alert(s,SSL3_AL_FATAL,al);\n\t\t}\nerr:\n\tEVP_PKEY_free(pkey);\n\tX509_free(x);\n\tsk_X509_pop_free(sk,X509_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,\n\t\t\t(char *)&(p[s->init_num]),\n\t\t\t4-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,\n\t\t\t(char *)&(p[s->init_num]),(int)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\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tCRYPTO_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\tCRYPTO_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,776
0
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/x509v3/v3_utl.c/#L202
ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value) { BIGNUM *bn = NULL; ASN1_INTEGER *aint; int isneg, ishex; int ret; if (value == NULL) { X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE); return NULL; } bn = BN_new(); if (bn == NULL) { X509V3err(X509V3_F_S2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE); return NULL; } if (value[0] == '-') { value++; isneg = 1; } else isneg = 0; if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) { value += 2; ishex = 1; } else ishex = 0; if (ishex) ret = BN_hex2bn(&bn, value); else ret = BN_dec2bn(&bn, value); if (!ret || value[ret]) { BN_free(bn); X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR); return NULL; } if (isneg && BN_is_zero(bn)) isneg = 0; aint = BN_to_ASN1_INTEGER(bn, NULL); BN_free(bn); if (!aint) { X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_TO_ASN1_INTEGER_ERROR); return NULL; } if (isneg) aint->type |= V_ASN1_NEG; return aint; }
["ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)\n{\n BIGNUM *bn = NULL;\n ASN1_INTEGER *aint;\n int isneg, ishex;\n int ret;\n if (value == NULL) {\n X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE);\n return NULL;\n }\n bn = BN_new();\n if (bn == NULL) {\n X509V3err(X509V3_F_S2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n if (value[0] == '-') {\n value++;\n isneg = 1;\n } else\n isneg = 0;\n if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {\n value += 2;\n ishex = 1;\n } else\n ishex = 0;\n if (ishex)\n ret = BN_hex2bn(&bn, value);\n else\n ret = BN_dec2bn(&bn, value);\n if (!ret || value[ret]) {\n BN_free(bn);\n X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR);\n return NULL;\n }\n if (isneg && BN_is_zero(bn))\n isneg = 0;\n aint = BN_to_ASN1_INTEGER(bn, NULL);\n BN_free(bn);\n if (!aint) {\n X509V3err(X509V3_F_S2I_ASN1_INTEGER,\n X509V3_R_BN_TO_ASN1_INTEGER_ERROR);\n return NULL;\n }\n if (isneg)\n aint->type |= V_ASN1_NEG;\n return aint;\n}", 'BIGNUM *BN_new(void)\n{\n BIGNUM *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->flags = BN_FLG_MALLOCED;\n bn_check_top(ret);\n return (ret);\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 (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,777
0
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/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_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,\n int len, int peek)\n{\n int al, i, j, ret;\n unsigned int n;\n SSL3_RECORD *rr;\n void (*cb) (const SSL *ssl, int type2, int val) = NULL;\n if (!SSL3_BUFFER_is_initialised(&s->rlayer.rbuf)) {\n if (!ssl3_setup_buffers(s))\n return (-1);\n }\n if ((type && (type != SSL3_RT_APPLICATION_DATA) &&\n (type != SSL3_RT_HANDSHAKE)) ||\n (peek && (type != SSL3_RT_APPLICATION_DATA))) {\n SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);\n return -1;\n }\n if ((ret = have_handshake_fragment(s, type, buf, len)))\n return ret;\n#ifndef OPENSSL_NO_SCTP\n if ((!ossl_statem_get_in_handshake(s) && SSL_in_init(s)) ||\n (BIO_dgram_is_sctp(SSL_get_rbio(s))\n && ossl_statem_in_sctp_read_sock(s)\n && s->s3->in_read_app_data != 2))\n#else\n if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s))\n#endif\n {\n i = s->handshake_func(s);\n if (i < 0)\n return (i);\n if (i == 0) {\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);\n return (-1);\n }\n }\n start:\n s->rwstate = SSL_NOTHING;\n rr = s->rlayer.rrec;\n if (SSL_is_init_finished(s) && SSL3_RECORD_get_length(rr) == 0) {\n pitem *item;\n item = pqueue_pop(s->rlayer.d->buffered_app_data.q);\n if (item) {\n#ifndef OPENSSL_NO_SCTP\n if (BIO_dgram_is_sctp(SSL_get_rbio(s))) {\n DTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *)item->data;\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO,\n sizeof(rdata->recordinfo), &rdata->recordinfo);\n }\n#endif\n dtls1_copy_record(s, item);\n OPENSSL_free(item->data);\n pitem_free(item);\n }\n }\n if (dtls1_handle_timeout(s) > 0)\n goto start;\n if ((SSL3_RECORD_get_length(rr) == 0)\n || (s->rlayer.rstate == SSL_ST_READ_BODY)) {\n ret = dtls1_get_record(s);\n if (ret <= 0) {\n ret = dtls1_read_failed(s, ret);\n if (ret <= 0)\n return (ret);\n else\n goto start;\n }\n }\n if (s->s3->change_cipher_spec\n && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {\n if (dtls1_buffer_record(s, &(s->rlayer.d->buffered_app_data),\n SSL3_RECORD_get_seq_num(rr)) < 0) {\n SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);\n return -1;\n }\n SSL3_RECORD_set_length(rr, 0);\n goto start;\n }\n if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {\n SSL3_RECORD_set_length(rr, 0);\n s->rwstate = SSL_NOTHING;\n return (0);\n }\n if (type == SSL3_RECORD_get_type(rr)\n || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC\n && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {\n if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&\n (s->enc_read_ctx == NULL)) {\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);\n goto f_err;\n }\n if (recvd_type != NULL)\n *recvd_type = SSL3_RECORD_get_type(rr);\n if (len <= 0)\n return (len);\n if ((unsigned int)len > SSL3_RECORD_get_length(rr))\n n = SSL3_RECORD_get_length(rr);\n else\n n = (unsigned int)len;\n memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);\n if (!peek) {\n SSL3_RECORD_add_length(rr, -n);\n SSL3_RECORD_add_off(rr, n);\n if (SSL3_RECORD_get_length(rr) == 0) {\n s->rlayer.rstate = SSL_ST_READ_HEADER;\n SSL3_RECORD_set_off(rr, 0);\n }\n }\n#ifndef OPENSSL_NO_SCTP\n if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n SSL3_RECORD_get_type(rr) == SSL3_RT_APPLICATION_DATA &&\n ossl_statem_in_sctp_read_sock(s)) {\n s->rwstate = SSL_READING;\n BIO_clear_retry_flags(SSL_get_rbio(s));\n BIO_set_retry_read(SSL_get_rbio(s));\n }\n if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n s->d1->shutdown_received\n && !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {\n s->shutdown |= SSL_RECEIVED_SHUTDOWN;\n return (0);\n }\n#endif\n return (n);\n }\n {\n unsigned int k, dest_maxlen = 0;\n unsigned char *dest = NULL;\n unsigned int *dest_len = NULL;\n if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) {\n dest_maxlen = sizeof s->rlayer.d->handshake_fragment;\n dest = s->rlayer.d->handshake_fragment;\n dest_len = &s->rlayer.d->handshake_fragment_len;\n } else if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {\n dest_maxlen = sizeof(s->rlayer.d->alert_fragment);\n dest = s->rlayer.d->alert_fragment;\n dest_len = &s->rlayer.d->alert_fragment_len;\n }\n#ifndef OPENSSL_NO_HEARTBEATS\n else if (SSL3_RECORD_get_type(rr) == DTLS1_RT_HEARTBEAT) {\n if (dtls1_process_heartbeat(s, SSL3_RECORD_get_data(rr),\n SSL3_RECORD_get_length(rr)) < 0) {\n return -1;\n }\n SSL3_RECORD_set_length(rr, 0);\n s->rwstate = SSL_READING;\n BIO_clear_retry_flags(SSL_get_rbio(s));\n BIO_set_retry_read(SSL_get_rbio(s));\n return (-1);\n }\n#endif\n else if (SSL3_RECORD_get_type(rr) != SSL3_RT_CHANGE_CIPHER_SPEC) {\n if (SSL3_RECORD_get_type(rr) == SSL3_RT_APPLICATION_DATA) {\n BIO *bio;\n s->s3->in_read_app_data = 2;\n bio = SSL_get_rbio(s);\n s->rwstate = SSL_READING;\n BIO_clear_retry_flags(bio);\n BIO_set_retry_read(bio);\n return (-1);\n }\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);\n goto f_err;\n }\n if (dest_maxlen > 0) {\n if (SSL3_RECORD_get_length(rr) < dest_maxlen) {\n#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE\n FIX ME\n#endif\n s->rlayer.rstate = SSL_ST_READ_HEADER;\n SSL3_RECORD_set_length(rr, 0);\n goto start;\n }\n for (k = 0; k < dest_maxlen; k++) {\n dest[k] = SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)];\n SSL3_RECORD_add_off(rr, 1);\n SSL3_RECORD_add_length(rr, -1);\n }\n *dest_len = dest_maxlen;\n }\n }\n if ((!s->server) &&\n (s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&\n (s->rlayer.d->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&\n (s->session != NULL) && (s->session->cipher != NULL)) {\n s->rlayer.d->handshake_fragment_len = 0;\n if ((s->rlayer.d->handshake_fragment[1] != 0) ||\n (s->rlayer.d->handshake_fragment[2] != 0) ||\n (s->rlayer.d->handshake_fragment[3] != 0)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);\n goto f_err;\n }\n if (s->msg_callback)\n s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,\n s->rlayer.d->handshake_fragment, 4, s,\n s->msg_callback_arg);\n if (SSL_is_init_finished(s) &&\n !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&\n !s->s3->renegotiate) {\n s->d1->handshake_read_seq++;\n s->new_session = 1;\n ssl3_renegotiate(s);\n if (ssl3_renegotiate_check(s)) {\n i = s->handshake_func(s);\n if (i < 0)\n return (i);\n if (i == 0) {\n SSLerr(SSL_F_DTLS1_READ_BYTES,\n SSL_R_SSL_HANDSHAKE_FAILURE);\n return (-1);\n }\n if (!(s->mode & SSL_MODE_AUTO_RETRY)) {\n if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {\n BIO *bio;\n s->rwstate = SSL_READING;\n bio = SSL_get_rbio(s);\n BIO_clear_retry_flags(bio);\n BIO_set_retry_read(bio);\n return (-1);\n }\n }\n }\n }\n goto start;\n }\n if (s->rlayer.d->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH) {\n int alert_level = s->rlayer.d->alert_fragment[0];\n int alert_descr = s->rlayer.d->alert_fragment[1];\n s->rlayer.d->alert_fragment_len = 0;\n if (s->msg_callback)\n s->msg_callback(0, s->version, SSL3_RT_ALERT,\n s->rlayer.d->alert_fragment, 2, s,\n s->msg_callback_arg);\n if (s->info_callback != NULL)\n cb = s->info_callback;\n else if (s->ctx->info_callback != NULL)\n cb = s->ctx->info_callback;\n if (cb != NULL) {\n j = (alert_level << 8) | alert_descr;\n cb(s, SSL_CB_READ_ALERT, j);\n }\n if (alert_level == SSL3_AL_WARNING) {\n s->s3->warn_alert = alert_descr;\n if (alert_descr == SSL_AD_CLOSE_NOTIFY) {\n#ifndef OPENSSL_NO_SCTP\n if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {\n s->d1->shutdown_received = 1;\n s->rwstate = SSL_READING;\n BIO_clear_retry_flags(SSL_get_rbio(s));\n BIO_set_retry_read(SSL_get_rbio(s));\n return -1;\n }\n#endif\n s->shutdown |= SSL_RECEIVED_SHUTDOWN;\n return (0);\n }\n#if 0\n if (alert_descr == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) {\n unsigned short seq;\n unsigned int frag_off;\n unsigned char *p = &(s->rlayer.d->alert_fragment[2]);\n n2s(p, seq);\n n2l3(p, frag_off);\n dtls1_retransmit_message(s,\n dtls1_get_queue_priority\n (frag->msg_header.seq, 0), frag_off,\n &found);\n if (!found && SSL_in_init(s)) {\n ssl3_send_alert(s, SSL3_AL_WARNING,\n DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);\n }\n }\n#endif\n } else if (alert_level == SSL3_AL_FATAL) {\n char tmp[16];\n s->rwstate = SSL_NOTHING;\n s->s3->fatal_alert = alert_descr;\n SSLerr(SSL_F_DTLS1_READ_BYTES,\n SSL_AD_REASON_OFFSET + alert_descr);\n BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);\n ERR_add_error_data(2, "SSL alert number ", tmp);\n s->shutdown |= SSL_RECEIVED_SHUTDOWN;\n SSL_CTX_remove_session(s->ctx, s->session);\n return (0);\n } else {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);\n goto f_err;\n }\n goto start;\n }\n if (s->shutdown & SSL_SENT_SHUTDOWN) {\n s->rwstate = SSL_NOTHING;\n SSL3_RECORD_set_length(rr, 0);\n return (0);\n }\n if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {\n SSL3_RECORD_set_length(rr, 0);\n goto start;\n }\n if ((s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&\n !ossl_statem_get_in_handshake(s)) {\n struct hm_header_st msg_hdr;\n dtls1_get_message_header(rr->data, &msg_hdr);\n if (SSL3_RECORD_get_epoch(rr) != s->rlayer.d->r_epoch) {\n SSL3_RECORD_set_length(rr, 0);\n goto start;\n }\n if (msg_hdr.type == SSL3_MT_FINISHED) {\n if (dtls1_check_timeout_num(s) < 0)\n return -1;\n dtls1_retransmit_buffered_messages(s);\n SSL3_RECORD_set_length(rr, 0);\n goto start;\n }\n if (SSL_is_init_finished(s) &&\n !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {\n ossl_statem_set_in_init(s, 1);\n s->renegotiate = 1;\n s->new_session = 1;\n }\n i = s->handshake_func(s);\n if (i < 0)\n return (i);\n if (i == 0) {\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);\n return (-1);\n }\n if (!(s->mode & SSL_MODE_AUTO_RETRY)) {\n if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {\n BIO *bio;\n s->rwstate = SSL_READING;\n bio = SSL_get_rbio(s);\n BIO_clear_retry_flags(bio);\n BIO_set_retry_read(bio);\n return (-1);\n }\n }\n goto start;\n }\n switch (SSL3_RECORD_get_type(rr)) {\n default:\n if (s->version == TLS1_VERSION) {\n SSL3_RECORD_set_length(rr, 0);\n goto start;\n }\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);\n goto f_err;\n case SSL3_RT_CHANGE_CIPHER_SPEC:\n case SSL3_RT_ALERT:\n case SSL3_RT_HANDSHAKE:\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);\n goto f_err;\n case SSL3_RT_APPLICATION_DATA:\n if (s->s3->in_read_app_data &&\n (s->s3->total_renegotiations != 0) &&\n ossl_statem_app_data_allowed(s)) {\n s->s3->in_read_app_data = 2;\n return (-1);\n } else {\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);\n goto f_err;\n }\n }\n f_err:\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\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 if (lck)\n CRYPTO_THREAD_unlock(ctx->lock);\n if (ret) {\n r->not_resumable = 1;\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, r);\n SSL_SESSION_free(r);\n }\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,778
0
https://github.com/openssl/openssl/blob/4b8515baa6edef1a771f9e4e3fbc0395b4a629e8/crypto/bn/bn_gf2m.c/#L628
int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp; int ret = 0; bn_check_top(a); bn_check_top(p); BN_CTX_start(ctx); b = BN_CTX_get(ctx); c = BN_CTX_get(ctx); u = BN_CTX_get(ctx); v = BN_CTX_get(ctx); if (v == NULL) goto err; if (!BN_GF2m_mod(u, a, p)) goto err; if (BN_is_zero(u)) goto err; if (!BN_copy(v, p)) goto err; # if 0 if (!BN_one(b)) goto err; while (1) { while (!BN_is_odd(u)) { if (BN_is_zero(u)) goto err; if (!BN_rshift1(u, u)) goto err; if (BN_is_odd(b)) { if (!BN_GF2m_add(b, b, p)) goto err; } if (!BN_rshift1(b, b)) goto err; } if (BN_abs_is_word(u, 1)) break; if (BN_num_bits(u) < BN_num_bits(v)) { tmp = u; u = v; v = tmp; tmp = b; b = c; c = tmp; } if (!BN_GF2m_add(u, u, v)) goto err; if (!BN_GF2m_add(b, b, c)) goto err; } # else { int i; int ubits = BN_num_bits(u); int vbits = BN_num_bits(v); int top = p->top; BN_ULONG *udp, *bdp, *vdp, *cdp; if (!bn_wexpand(u, top)) goto err; udp = u->d; for (i = u->top; i < top; i++) udp[i] = 0; u->top = top; if (!bn_wexpand(b, top)) goto err; bdp = b->d; bdp[0] = 1; for (i = 1; i < top; i++) bdp[i] = 0; b->top = top; if (!bn_wexpand(c, top)) goto err; cdp = c->d; for (i = 0; i < top; i++) cdp[i] = 0; c->top = top; vdp = v->d; while (1) { while (ubits && !(udp[0] & 1)) { BN_ULONG u0, u1, b0, b1, mask; u0 = udp[0]; b0 = bdp[0]; mask = (BN_ULONG)0 - (b0 & 1); b0 ^= p->d[0] & mask; for (i = 0; i < top - 1; i++) { u1 = udp[i + 1]; udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2; u0 = u1; b1 = bdp[i + 1] ^ (p->d[i + 1] & mask); bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2; b0 = b1; } udp[i] = u0 >> 1; bdp[i] = b0 >> 1; ubits--; } if (ubits <= BN_BITS2) { if (udp[0] == 0) goto err; if (udp[0] == 1) break; } if (ubits < vbits) { i = ubits; ubits = vbits; vbits = i; tmp = u; u = v; v = tmp; tmp = b; b = c; c = tmp; udp = vdp; vdp = v->d; bdp = cdp; cdp = c->d; } for (i = 0; i < top; i++) { udp[i] ^= vdp[i]; bdp[i] ^= cdp[i]; } if (ubits == vbits) { BN_ULONG ul; int utop = (ubits - 1) / BN_BITS2; while ((ul = udp[utop]) == 0 && utop) utop--; ubits = utop * BN_BITS2 + BN_num_bits_word(ul); } } bn_correct_top(b); } # endif if (!BN_copy(r, b)) goto err; bn_check_top(r); ret = 1; err: # ifdef BN_DEBUG bn_correct_top(c); bn_correct_top(u); bn_correct_top(v); # endif BN_CTX_end(ctx); return ret; }
['static int test_gf2m_moddiv()\n{\n BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;\n BIGNUM *e = NULL, *f = NULL;\n int i, j, st = 0;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(b[0] = BN_new())\n || !TEST_ptr(b[1] = BN_new())\n || !TEST_ptr(c = BN_new())\n || !TEST_ptr(d = BN_new())\n || !TEST_ptr(e = BN_new())\n || !TEST_ptr(f = BN_new()))\n goto err;\n BN_GF2m_arr2poly(p0, b[0]);\n BN_GF2m_arr2poly(p1, b[1]);\n for (i = 0; i < NUM0; i++) {\n BN_bntest_rand(a, 512, 0, 0);\n BN_bntest_rand(c, 512, 0, 0);\n for (j = 0; j < 2; j++) {\n BN_GF2m_mod_div(d, a, c, b[j], ctx);\n BN_GF2m_mod_mul(e, d, c, b[j], ctx);\n BN_GF2m_mod_div(f, a, e, b[j], ctx);\n if (!TEST_BN_eq_one(f))\n goto err;\n }\n }\n st = 1;\n err:\n BN_free(a);\n BN_free(b[0]);\n BN_free(b[1]);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n BN_free(f);\n return st;\n}', 'int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,\n const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *xinv = NULL;\n int ret = 0;\n bn_check_top(y);\n bn_check_top(x);\n bn_check_top(p);\n BN_CTX_start(ctx);\n xinv = BN_CTX_get(ctx);\n if (xinv == NULL)\n goto err;\n if (!BN_GF2m_mod_inv(xinv, x, p, ctx))\n goto err;\n if (!BN_GF2m_mod_mul(r, y, xinv, p, 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_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(p);\n BN_CTX_start(ctx);\n b = BN_CTX_get(ctx);\n c = BN_CTX_get(ctx);\n u = BN_CTX_get(ctx);\n v = BN_CTX_get(ctx);\n if (v == NULL)\n goto err;\n if (!BN_GF2m_mod(u, a, p))\n goto err;\n if (BN_is_zero(u))\n goto err;\n if (!BN_copy(v, p))\n goto err;\n# if 0\n if (!BN_one(b))\n goto err;\n while (1) {\n while (!BN_is_odd(u)) {\n if (BN_is_zero(u))\n goto err;\n if (!BN_rshift1(u, u))\n goto err;\n if (BN_is_odd(b)) {\n if (!BN_GF2m_add(b, b, p))\n goto err;\n }\n if (!BN_rshift1(b, b))\n goto err;\n }\n if (BN_abs_is_word(u, 1))\n break;\n if (BN_num_bits(u) < BN_num_bits(v)) {\n tmp = u;\n u = v;\n v = tmp;\n tmp = b;\n b = c;\n c = tmp;\n }\n if (!BN_GF2m_add(u, u, v))\n goto err;\n if (!BN_GF2m_add(b, b, c))\n goto err;\n }\n# else\n {\n int i;\n int ubits = BN_num_bits(u);\n int vbits = BN_num_bits(v);\n int top = p->top;\n BN_ULONG *udp, *bdp, *vdp, *cdp;\n if (!bn_wexpand(u, top))\n goto err;\n udp = u->d;\n for (i = u->top; i < top; i++)\n udp[i] = 0;\n u->top = top;\n if (!bn_wexpand(b, top))\n goto err;\n bdp = b->d;\n bdp[0] = 1;\n for (i = 1; i < top; i++)\n bdp[i] = 0;\n b->top = top;\n if (!bn_wexpand(c, top))\n goto err;\n cdp = c->d;\n for (i = 0; i < top; i++)\n cdp[i] = 0;\n c->top = top;\n vdp = v->d;\n while (1) {\n while (ubits && !(udp[0] & 1)) {\n BN_ULONG u0, u1, b0, b1, mask;\n u0 = udp[0];\n b0 = bdp[0];\n mask = (BN_ULONG)0 - (b0 & 1);\n b0 ^= p->d[0] & mask;\n for (i = 0; i < top - 1; i++) {\n u1 = udp[i + 1];\n udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;\n u0 = u1;\n b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);\n bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;\n b0 = b1;\n }\n udp[i] = u0 >> 1;\n bdp[i] = b0 >> 1;\n ubits--;\n }\n if (ubits <= BN_BITS2) {\n if (udp[0] == 0)\n goto err;\n if (udp[0] == 1)\n break;\n }\n if (ubits < vbits) {\n i = ubits;\n ubits = vbits;\n vbits = i;\n tmp = u;\n u = v;\n v = tmp;\n tmp = b;\n b = c;\n c = tmp;\n udp = vdp;\n vdp = v->d;\n bdp = cdp;\n cdp = c->d;\n }\n for (i = 0; i < top; i++) {\n udp[i] ^= vdp[i];\n bdp[i] ^= cdp[i];\n }\n if (ubits == vbits) {\n BN_ULONG ul;\n int utop = (ubits - 1) / BN_BITS2;\n while ((ul = udp[utop]) == 0 && utop)\n utop--;\n ubits = utop * BN_BITS2 + BN_num_bits_word(ul);\n }\n }\n bn_correct_top(b);\n }\n# endif\n if (!BN_copy(r, b))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n# ifdef BN_DEBUG\n bn_correct_top(c);\n bn_correct_top(u);\n bn_correct_top(v);\n# endif\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}']
35,779
0
https://github.com/openssl/openssl/blob/02cba628daa7fea959c561531a8a984756bdf41c/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 ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,\n size_t len, int peek, size_t *readbytes)\n{\n int al, i, j, ret;\n size_t n, curr_rec, num_recs, totalbytes;\n SSL3_RECORD *rr;\n SSL3_BUFFER *rbuf;\n void (*cb) (const SSL *ssl, int type2, int val) = NULL;\n rbuf = &s->rlayer.rbuf;\n if (!SSL3_BUFFER_is_initialised(rbuf)) {\n if (!ssl3_setup_read_buffer(s))\n return -1;\n }\n if ((type && (type != SSL3_RT_APPLICATION_DATA)\n && (type != SSL3_RT_HANDSHAKE)) || (peek\n && (type !=\n SSL3_RT_APPLICATION_DATA))) {\n SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);\n return -1;\n }\n if ((type == SSL3_RT_HANDSHAKE) && (s->rlayer.handshake_fragment_len > 0))\n {\n unsigned char *src = s->rlayer.handshake_fragment;\n unsigned char *dst = buf;\n unsigned int k;\n n = 0;\n while ((len > 0) && (s->rlayer.handshake_fragment_len > 0)) {\n *dst++ = *src++;\n len--;\n s->rlayer.handshake_fragment_len--;\n n++;\n }\n for (k = 0; k < s->rlayer.handshake_fragment_len; k++)\n s->rlayer.handshake_fragment[k] = *src++;\n if (recvd_type != NULL)\n *recvd_type = SSL3_RT_HANDSHAKE;\n *readbytes = n;\n return 1;\n }\n if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s)) {\n i = s->handshake_func(s);\n if (i < 0)\n return i;\n if (i == 0) {\n SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);\n return -1;\n }\n }\n start:\n s->rwstate = SSL_NOTHING;\n rr = s->rlayer.rrec;\n num_recs = RECORD_LAYER_get_numrpipes(&s->rlayer);\n do {\n if (num_recs == 0) {\n ret = ssl3_get_record(s);\n if (ret <= 0)\n return ret;\n num_recs = RECORD_LAYER_get_numrpipes(&s->rlayer);\n if (num_recs == 0) {\n al = SSL_AD_INTERNAL_ERROR;\n SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);\n goto f_err;\n }\n }\n for (curr_rec = 0;\n curr_rec < num_recs && SSL3_RECORD_is_read(&rr[curr_rec]);\n curr_rec++) ;\n if (curr_rec == num_recs) {\n RECORD_LAYER_set_numrpipes(&s->rlayer, 0);\n num_recs = 0;\n curr_rec = 0;\n }\n } while (num_recs == 0);\n rr = &rr[curr_rec];\n if (SSL3_RECORD_get_type(rr) != SSL3_RT_ALERT\n && SSL3_RECORD_get_length(rr) != 0)\n s->rlayer.alert_count = 0;\n if (s->s3->change_cipher_spec\n && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);\n goto f_err;\n }\n if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {\n SSL3_RECORD_set_length(rr, 0);\n s->rwstate = SSL_NOTHING;\n return 0;\n }\n if (type == SSL3_RECORD_get_type(rr)\n || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC\n && type == SSL3_RT_HANDSHAKE && recvd_type != NULL\n && !SSL_IS_TLS13(s))) {\n if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&\n (s->enc_read_ctx == NULL)) {\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);\n goto f_err;\n }\n if (type == SSL3_RT_HANDSHAKE\n && SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC\n && s->rlayer.handshake_fragment_len > 0) {\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);\n goto f_err;\n }\n if (recvd_type != NULL)\n *recvd_type = SSL3_RECORD_get_type(rr);\n if (len == 0)\n return 0;\n totalbytes = 0;\n do {\n if (len - totalbytes > SSL3_RECORD_get_length(rr))\n n = SSL3_RECORD_get_length(rr);\n else\n n = len - totalbytes;\n memcpy(buf, &(rr->data[rr->off]), n);\n buf += n;\n if (peek) {\n if (SSL3_RECORD_get_length(rr) == 0)\n SSL3_RECORD_set_read(rr);\n } else {\n SSL3_RECORD_sub_length(rr, n);\n SSL3_RECORD_add_off(rr, n);\n if (SSL3_RECORD_get_length(rr) == 0) {\n s->rlayer.rstate = SSL_ST_READ_HEADER;\n SSL3_RECORD_set_off(rr, 0);\n SSL3_RECORD_set_read(rr);\n }\n }\n if (SSL3_RECORD_get_length(rr) == 0\n || (peek && n == SSL3_RECORD_get_length(rr))) {\n curr_rec++;\n rr++;\n }\n totalbytes += n;\n } while (type == SSL3_RT_APPLICATION_DATA && curr_rec < num_recs\n && totalbytes < len);\n if (totalbytes == 0) {\n goto start;\n }\n if (!peek && curr_rec == num_recs\n && (s->mode & SSL_MODE_RELEASE_BUFFERS)\n && SSL3_BUFFER_get_left(rbuf) == 0)\n ssl3_release_read_buffer(s);\n *readbytes = totalbytes;\n return 1;\n }\n if (rr->rec_version == SSL2_VERSION) {\n al = SSL_AD_INTERNAL_ERROR;\n SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);\n goto f_err;\n }\n if (s->method->version == TLS_ANY_VERSION\n && (s->server || rr->type != SSL3_RT_ALERT)) {\n s->version = rr->rec_version;\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_MESSAGE);\n goto f_err;\n }\n {\n size_t dest_maxlen = 0;\n unsigned char *dest = NULL;\n size_t *dest_len = NULL;\n if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) {\n dest_maxlen = sizeof s->rlayer.handshake_fragment;\n dest = s->rlayer.handshake_fragment;\n dest_len = &s->rlayer.handshake_fragment_len;\n } else if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {\n dest_maxlen = sizeof s->rlayer.alert_fragment;\n dest = s->rlayer.alert_fragment;\n dest_len = &s->rlayer.alert_fragment_len;\n }\n if (dest_maxlen > 0) {\n n = dest_maxlen - *dest_len;\n if (SSL3_RECORD_get_length(rr) < n)\n n = SSL3_RECORD_get_length(rr);\n while (n-- > 0) {\n dest[(*dest_len)++] =\n SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)];\n SSL3_RECORD_add_off(rr, 1);\n SSL3_RECORD_add_length(rr, -1);\n }\n if (*dest_len < dest_maxlen) {\n SSL3_RECORD_set_read(rr);\n goto start;\n }\n }\n }\n if (s->server &&\n SSL_is_init_finished(s) &&\n !s->s3->send_connection_binding &&\n (s->version > SSL3_VERSION) &&\n !SSL_IS_TLS13(s) &&\n (s->rlayer.handshake_fragment_len >= 4) &&\n (s->rlayer.handshake_fragment[0] == SSL3_MT_CLIENT_HELLO) &&\n (s->session != NULL) && (s->session->cipher != NULL) &&\n !(s->ctx->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {\n SSL3_RECORD_set_length(rr, 0);\n SSL3_RECORD_set_read(rr);\n ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);\n goto start;\n }\n if (s->rlayer.alert_fragment_len >= 2) {\n int alert_level = s->rlayer.alert_fragment[0];\n int alert_descr = s->rlayer.alert_fragment[1];\n s->rlayer.alert_fragment_len = 0;\n if (s->msg_callback)\n s->msg_callback(0, s->version, SSL3_RT_ALERT,\n s->rlayer.alert_fragment, 2, s,\n s->msg_callback_arg);\n if (s->info_callback != NULL)\n cb = s->info_callback;\n else if (s->ctx->info_callback != NULL)\n cb = s->ctx->info_callback;\n if (cb != NULL) {\n j = (alert_level << 8) | alert_descr;\n cb(s, SSL_CB_READ_ALERT, j);\n }\n if (alert_level == SSL3_AL_WARNING) {\n s->s3->warn_alert = alert_descr;\n SSL3_RECORD_set_read(rr);\n s->rlayer.alert_count++;\n if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_TOO_MANY_WARN_ALERTS);\n goto f_err;\n }\n if (alert_descr == SSL_AD_CLOSE_NOTIFY) {\n s->shutdown |= SSL_RECEIVED_SHUTDOWN;\n return 0;\n }\n else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {\n al = SSL_AD_HANDSHAKE_FAILURE;\n SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_NO_RENEGOTIATION);\n goto f_err;\n }\n#ifdef SSL_AD_MISSING_SRP_USERNAME\n else if (alert_descr == SSL_AD_MISSING_SRP_USERNAME)\n return (0);\n#endif\n } else if (alert_level == SSL3_AL_FATAL) {\n char tmp[16];\n s->rwstate = SSL_NOTHING;\n s->s3->fatal_alert = alert_descr;\n SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);\n BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);\n ERR_add_error_data(2, "SSL alert number ", tmp);\n s->shutdown |= SSL_RECEIVED_SHUTDOWN;\n SSL3_RECORD_set_read(rr);\n SSL_CTX_remove_session(s->session_ctx, s->session);\n return 0;\n } else {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);\n goto f_err;\n }\n goto start;\n }\n if (s->shutdown & SSL_SENT_SHUTDOWN) {\n s->rwstate = SSL_NOTHING;\n SSL3_RECORD_set_length(rr, 0);\n SSL3_RECORD_set_read(rr);\n return 0;\n }\n if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);\n goto f_err;\n }\n if ((s->rlayer.handshake_fragment_len >= 4)\n && !ossl_statem_get_in_handshake(s)) {\n assert(SSL_is_init_finished(s));\n if (!SSL_is_init_finished(s)) {\n al = SSL_AD_INTERNAL_ERROR;\n SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);\n goto f_err;\n }\n ossl_statem_set_in_init(s, 1);\n i = s->handshake_func(s);\n if (i < 0)\n return i;\n if (i == 0) {\n SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);\n return -1;\n }\n if (!(s->mode & SSL_MODE_AUTO_RETRY)) {\n if (SSL3_BUFFER_get_left(rbuf) == 0) {\n BIO *bio;\n s->rwstate = SSL_READING;\n bio = SSL_get_rbio(s);\n BIO_clear_retry_flags(bio);\n BIO_set_retry_read(bio);\n return -1;\n }\n }\n goto start;\n }\n switch (SSL3_RECORD_get_type(rr)) {\n default:\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);\n goto f_err;\n case SSL3_RT_CHANGE_CIPHER_SPEC:\n case SSL3_RT_ALERT:\n case SSL3_RT_HANDSHAKE:\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);\n goto f_err;\n case SSL3_RT_APPLICATION_DATA:\n if (ossl_statem_app_data_allowed(s)) {\n s->s3->in_read_app_data = 2;\n return -1;\n } else {\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);\n goto f_err;\n }\n }\n f_err:\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\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,780
0
https://github.com/libav/libav/blob/dad7a9c7c0ae8ebc56f2e3a24e6fa4da5c2cd491/libavcodec/bitstream.h/#L68
static inline void refill_32(BitstreamContext *bc) { if (bc->ptr >= bc->buffer_end) return; #ifdef BITSTREAM_READER_LE bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits; #else bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left); #endif bc->ptr += 4; bc->bits_left += 32; }
['static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)\n{\n int i, ret;\n int skip = 0;\n int len, toks, pos;\n TM2Codes codes;\n GetByteContext gb;\n bytestream2_init(&gb, buf, buf_size);\n len = bytestream2_get_be32(&gb);\n skip = len * 4 + 4;\n if (len == 0)\n return 4;\n if (len >= INT_MAX / 4 - 1 || len < 0 || skip > buf_size) {\n av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\\n");\n return AVERROR_INVALIDDATA;\n }\n toks = bytestream2_get_be32(&gb);\n if (toks & 1) {\n len = bytestream2_get_be32(&gb);\n if (len == TM2_ESCAPE) {\n len = bytestream2_get_be32(&gb);\n }\n if (len > 0) {\n pos = bytestream2_tell(&gb);\n if (skip <= pos)\n return AVERROR_INVALIDDATA;\n bitstream_init8(&ctx->bc, buf + pos, skip - pos);\n if ((ret = tm2_read_deltas(ctx, stream_id)) < 0)\n return ret;\n bytestream2_skip(&gb, ((bitstream_tell(&ctx->bc) + 31) >> 5) << 2);\n }\n }\n len = bytestream2_get_be32(&gb);\n if (len == TM2_ESCAPE) {\n bytestream2_skip(&gb, 8);\n } else {\n bytestream2_skip(&gb, 4);\n }\n pos = bytestream2_tell(&gb);\n if (skip <= pos)\n return AVERROR_INVALIDDATA;\n bitstream_init8(&ctx->bc, buf + pos, skip - pos);\n if ((ret = tm2_build_huff_table(ctx, &codes)) < 0)\n return ret;\n bytestream2_skip(&gb, ((bitstream_tell(&ctx->bc) + 31) >> 5) << 2);\n toks >>= 1;\n if ((toks < 0) || (toks > 0xFFFFFF)) {\n av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\\n", toks);\n tm2_free_codes(&codes);\n return AVERROR_INVALIDDATA;\n }\n ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));\n ctx->tok_lens[stream_id] = toks;\n len = bytestream2_get_be32(&gb);\n if (len > 0) {\n pos = bytestream2_tell(&gb);\n if (skip <= pos)\n return AVERROR_INVALIDDATA;\n bitstream_init8(&ctx->bc, buf + pos, skip - pos);\n for (i = 0; i < toks; i++) {\n if (bitstream_bits_left(&ctx->bc) <= 0) {\n av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\\n", toks);\n return AVERROR_INVALIDDATA;\n }\n ctx->tokens[stream_id][i] = tm2_get_token(&ctx->bc, &codes);\n if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {\n av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\\n",\n ctx->tokens[stream_id][i], stream_id, i);\n return AVERROR_INVALIDDATA;\n }\n }\n } else {\n for (i = 0; i < toks; i++) {\n ctx->tokens[stream_id][i] = codes.recode[0];\n if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {\n av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\\n",\n ctx->tokens[stream_id][i], stream_id, i);\n return AVERROR_INVALIDDATA;\n }\n }\n }\n tm2_free_codes(&codes);\n return skip;\n}', 'static inline int bitstream_init8(BitstreamContext *bc, const uint8_t *buffer,\n unsigned byte_size)\n{\n if (byte_size > INT_MAX / 8)\n return AVERROR_INVALIDDATA;\n return bitstream_init(bc, buffer, byte_size * 8);\n}', 'static int tm2_read_deltas(TM2Context *ctx, int stream_id)\n{\n int d, mb;\n int i, v;\n d = bitstream_read(&ctx->bc, 9);\n mb = bitstream_read(&ctx->bc, 5);\n if ((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {\n av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\\n", d, mb);\n return AVERROR_INVALIDDATA;\n }\n for (i = 0; i < d; i++) {\n v = bitstream_read(&ctx->bc, mb);\n if (v & (1 << (mb - 1)))\n ctx->deltas[stream_id][i] = v - (1 << mb);\n else\n ctx->deltas[stream_id][i] = v;\n }\n for (; i < TM2_DELTAS; i++)\n ctx->deltas[stream_id][i] = 0;\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 refill_32(BitstreamContext *bc)\n{\n if (bc->ptr >= bc->buffer_end)\n return;\n#ifdef BITSTREAM_READER_LE\n bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits;\n#else\n bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left);\n#endif\n bc->ptr += 4;\n bc->bits_left += 32;\n}']
35,781
0
https://github.com/openssl/openssl/blob/5d1c09de1f2736e1d4b1877206d08455ec75f558/crypto/bn/bn_mont.c/#L125
static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont) { BIGNUM *n; BN_ULONG *ap, *np, *rp, n0, v, carry; int nl, max, i; n = &(mont->N); nl = n->top; if (nl == 0) { ret->top = 0; return 1; } max = (2 * nl); if (bn_wexpand(r, max) == NULL) return 0; r->neg ^= n->neg; np = n->d; rp = r->d; i = max - r->top; if (i) memset(&rp[r->top], 0, sizeof(*rp) * i); r->top = max; r->flags |= BN_FLG_FIXED_TOP; n0 = mont->n0[0]; for (carry = 0, i = 0; i < nl; i++, rp++) { v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2); v = (v + carry + rp[nl]) & BN_MASK2; carry |= (v != rp[nl]); carry &= (v <= rp[nl]); rp[nl] = v; } if (bn_wexpand(ret, nl) == NULL) return 0; ret->top = nl; ret->flags |= BN_FLG_FIXED_TOP; ret->neg = r->neg; rp = ret->d; ap = &(r->d[nl]); carry -= bn_sub_words(rp, ap, np, nl); for (i = 0; i < nl; i++) { rp[i] = (carry & ap[i]) | (~carry & rp[i]); ap[i] = 0; } return 1; }
['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}', 'static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)\n{\n BIGNUM *n;\n BN_ULONG *ap, *np, *rp, n0, v, carry;\n int nl, max, i;\n n = &(mont->N);\n nl = n->top;\n if (nl == 0) {\n ret->top = 0;\n return 1;\n }\n max = (2 * nl);\n if (bn_wexpand(r, max) == NULL)\n return 0;\n r->neg ^= n->neg;\n np = n->d;\n rp = r->d;\n i = max - r->top;\n if (i)\n memset(&rp[r->top], 0, sizeof(*rp) * i);\n r->top = max;\n r->flags |= BN_FLG_FIXED_TOP;\n n0 = mont->n0[0];\n for (carry = 0, i = 0; i < nl; i++, rp++) {\n v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);\n v = (v + carry + rp[nl]) & BN_MASK2;\n carry |= (v != rp[nl]);\n carry &= (v <= rp[nl]);\n rp[nl] = v;\n }\n if (bn_wexpand(ret, nl) == NULL)\n return 0;\n ret->top = nl;\n ret->flags |= BN_FLG_FIXED_TOP;\n ret->neg = r->neg;\n rp = ret->d;\n ap = &(r->d[nl]);\n carry -= bn_sub_words(rp, ap, np, nl);\n for (i = 0; i < nl; i++) {\n rp[i] = (carry & ap[i]) | (~carry & rp[i]);\n ap[i] = 0;\n }\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
35,782
0
https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/crypto/bn/bn_ctx.c/#L276
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,\n const EVP_MD *evpmd, const unsigned char *seed_in,\n size_t seed_len, unsigned char *seed_out,\n int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)\n{\n int ok = 0;\n unsigned char seed[SHA256_DIGEST_LENGTH];\n unsigned char md[SHA256_DIGEST_LENGTH];\n unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];\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 = qbits >> 3;\n int counter = 0;\n int r = 0;\n BN_CTX *ctx = NULL;\n unsigned int h = 2;\n if (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH &&\n qsize != SHA256_DIGEST_LENGTH)\n return 0;\n if (evpmd == NULL) {\n if (qsize == SHA_DIGEST_LENGTH)\n evpmd = EVP_sha1();\n else if (qsize == SHA224_DIGEST_LENGTH)\n evpmd = EVP_sha224();\n else\n evpmd = EVP_sha256();\n } else {\n qsize = EVP_MD_size(evpmd);\n }\n if (bits < 512)\n bits = 512;\n bits = (bits + 63) / 64 * 64;\n if (seed_in != NULL) {\n if (seed_len < (size_t)qsize) {\n DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN, DSA_R_SEED_LEN_SMALL);\n return 0;\n }\n if (seed_len > (size_t)qsize) {\n seed_len = qsize;\n }\n memcpy(seed, seed_in, seed_len);\n }\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if ((ctx = BN_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 q = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n c = BN_CTX_get(ctx);\n p = BN_CTX_get(ctx);\n test = BN_CTX_get(ctx);\n if (test == NULL)\n goto err;\n if (!BN_lshift(test, BN_value_one(), bits - 1))\n goto err;\n for (;;) {\n for (;;) {\n int use_random_seed = (seed_in == NULL);\n if (!BN_GENCB_call(cb, 0, m++))\n goto err;\n if (use_random_seed) {\n if (RAND_bytes(seed, qsize) <= 0)\n goto err;\n } else {\n seed_in = NULL;\n }\n memcpy(buf, seed, qsize);\n memcpy(buf2, seed, qsize);\n for (i = qsize - 1; i >= 0; i--) {\n buf[i]++;\n if (buf[i] != 0)\n break;\n }\n if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL))\n goto err;\n if (!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL))\n goto err;\n for (i = 0; i < qsize; i++)\n md[i] ^= buf2[i];\n md[0] |= 0x80;\n md[qsize - 1] |= 0x01;\n if (!BN_bin2bn(md, qsize, q))\n goto err;\n r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,\n use_random_seed, cb);\n if (r > 0)\n break;\n if (r != 0)\n goto err;\n }\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 = (bits - 1) / 160;\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 = qsize - 1; i >= 0; i--) {\n buf[i]++;\n if (buf[i] != 0)\n break;\n }\n if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL))\n goto err;\n if (!BN_bin2bn(md, qsize, r0))\n goto err;\n if (!BN_lshift(r0, r0, (qsize << 3) * k))\n goto err;\n if (!BN_add(W, W, r0))\n goto err;\n }\n if (!BN_mask_bits(W, bits - 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 >= 4096)\n break;\n }\n }\n end:\n if (!BN_GENCB_call(cb, 2, 1))\n goto err;\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 (!BN_set_word(test, h))\n goto err;\n if (!BN_MONT_CTX_set(mont, p, ctx))\n goto err;\n for (;;) {\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 (!BN_add(test, test, BN_value_one()))\n goto err;\n h++;\n }\n if (!BN_GENCB_call(cb, 3, 1))\n goto err;\n ok = 1;\n err:\n if (ok) {\n BN_free(ret->p);\n BN_free(ret->q);\n BN_free(ret->g);\n ret->p = BN_dup(p);\n ret->q = BN_dup(q);\n ret->g = BN_dup(g);\n if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {\n ok = 0;\n goto err;\n }\n if (counter_ret != NULL)\n *counter_ret = counter;\n if (h_ret != NULL)\n *h_ret = h;\n if (seed_out)\n memcpy(seed_out, seed, qsize);\n }\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n BN_MONT_CTX_free(mont);\n return ok;\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_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 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,783
0
https://github.com/openssl/openssl/blob/8de2910b5cbfa6169b54d1267abf654da773f00a/crypto/bn/bn_ctx.c/#L143
void BN_CTX_end(BN_CTX *ctx) { if (ctx == NULL) return; assert(ctx->depth > 0); if (ctx->depth == 0) BN_CTX_start(ctx); ctx->too_many = 0; ctx->depth--; if (ctx->depth < BN_CTX_NUM_POS) ctx->tos = ctx->pos[ctx->depth]; }
['int DSA_generate_key(DSA *dsa)\n\t{\n\tint ok=0;\n\tunsigned int i;\n\tBN_CTX *ctx=NULL;\n\tBIGNUM *pub_key=NULL,*priv_key=NULL;\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tif (dsa->priv_key == NULL)\n\t\t{\n\t\tif ((priv_key=BN_new()) == NULL) goto err;\n\t\t}\n\telse\n\t\tpriv_key=dsa->priv_key;\n\ti=BN_num_bits(dsa->q);\n\tfor (;;)\n\t\t{\n\t\tif (!BN_rand(priv_key,i,0,0))\n\t\t\tgoto err;\n\t\tif (BN_cmp(priv_key,dsa->q) >= 0)\n\t\t\tBN_sub(priv_key,priv_key,dsa->q);\n\t\tif (!BN_is_zero(priv_key)) break;\n\t\t}\n\tif (dsa->pub_key == NULL)\n\t\t{\n\t\tif ((pub_key=BN_new()) == NULL) goto err;\n\t\t}\n\telse\n\t\tpub_key=dsa->pub_key;\n\tif (!BN_mod_exp(pub_key,dsa->g,priv_key,dsa->p,ctx)) goto err;\n\tdsa->priv_key=priv_key;\n\tdsa->pub_key=pub_key;\n\tok=1;\nerr:\n\tif ((pub_key != NULL) && (dsa->pub_key == NULL)) BN_free(pub_key);\n\tif ((priv_key != NULL) && (dsa->priv_key == NULL)) BN_free(priv_key);\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\treturn(ok);\n\t}', 'BN_CTX *BN_CTX_new(void)\n\t{\n\tBN_CTX *ret;\n\tret=(BN_CTX *)OPENSSL_malloc(sizeof(BN_CTX));\n\tif (ret == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tBN_CTX_init(ret);\n\tret->flags=BN_FLG_MALLOCED;\n\treturn(ret);\n\t}', 'void BN_CTX_init(BN_CTX *ctx)\n\t{\n\tint i;\n\tctx->tos = 0;\n\tctx->flags = 0;\n\tctx->depth = 0;\n\tctx->too_many = 0;\n\tfor (i = 0; i < BN_CTX_NUM; i++)\n\t\tBN_init(&(ctx->bn[i]));\n\t}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n\t BN_CTX *ctx)\n\t{\n\tint ret;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n#ifdef MONT_MUL_MOD\n\tif (BN_is_odd(m))\n\t\t{\n\t\tif (a->top == 1)\n\t\t\t{\n\t\t\tBN_ULONG A = a->d[0];\n\t\t\tret=BN_mod_exp_mont_word(r,A,p,m,ctx,NULL);\n\t\t\t}\n\t\telse\n\t\t\tret=BN_mod_exp_mont(r,a,p,m,ctx,NULL);\n\t\t}\n\telse\n#endif\n#ifdef RECP_MUL_MOD\n\t\t{ ret=BN_mod_exp_recp(r,a,p,m,ctx); }\n#else\n\t\t{ ret=BN_mod_exp_simple(r,a,p,m,ctx); }\n#endif\n\treturn(ret);\n\t}', 'int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tBN_MONT_CTX *mont = NULL;\n\tint b, bits, ret=0;\n\tint r_is_one;\n\tBN_ULONG w, next_w;\n\tBIGNUM *d, *r, *t;\n\tBIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n\t\t(BN_mul_word(r, (w)) && \\\n\t\t( \\\n\t\t\t(BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n\t\t(BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n\tbn_check_top(p);\n\tbn_check_top(m);\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT_WORD,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\tbits = BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tBN_one(rr);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\td = BN_CTX_get(ctx);\n\tr = BN_CTX_get(ctx);\n\tt = BN_CTX_get(ctx);\n\tif (d == NULL || r == NULL || t == NULL) goto err;\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\tr_is_one = 1;\n\tw = a;\n\tfor (b = bits-2; b >= 0; b--)\n\t\t{\n\t\tnext_w = w*w;\n\t\tif ((next_w/w) != w)\n\t\t\t{\n\t\t\tif (r_is_one)\n\t\t\t\t{\n\t\t\t\tif (!BN_TO_MONTGOMERY_WORD(r, w, mont)) goto err;\n\t\t\t\tr_is_one = 0;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_MOD_MUL_WORD(r, w, m)) goto err;\n\t\t\t\t}\n\t\t\tnext_w = 1;\n\t\t\t}\n\t\tw = next_w;\n\t\tif (!r_is_one)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(r, r, r, mont, ctx)) goto err;\n\t\t\t}\n\t\tif (BN_is_bit_set(p, b))\n\t\t\t{\n\t\t\tnext_w = w*a;\n\t\t\tif ((next_w/a) != w)\n\t\t\t\t{\n\t\t\t\tif (r_is_one)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_TO_MONTGOMERY_WORD(r, w, mont)) goto err;\n\t\t\t\t\tr_is_one = 0;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_MOD_MUL_WORD(r, w, m)) goto err;\n\t\t\t\t\t}\n\t\t\t\tnext_w = a;\n\t\t\t\t}\n\t\t\tw = next_w;\n\t\t\t}\n\t\t}\n\tif (w != 1)\n\t\t{\n\t\tif (r_is_one)\n\t\t\t{\n\t\t\tif (!BN_TO_MONTGOMERY_WORD(r, w, mont)) goto err;\n\t\t\tr_is_one = 0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_MOD_MUL_WORD(r, w, m)) goto err;\n\t\t\t}\n\t\t}\n\tif (r_is_one)\n\t\t{\n\t\tif (!BN_one(rr)) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_from_montgomery(rr, r, mont, ctx)) goto err;\n\t\t}\n\tret = 1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tif (ctx->depth < BN_CTX_NUM_POS)\n\t\tctx->pos[ctx->depth] = ctx->tos;\n\tctx->depth++;\n\t}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n\t{\n\tif (ctx->depth > BN_CTX_NUM_POS || ctx->tos >= BN_CTX_NUM)\n\t\t{\n\t\tif (!ctx->too_many)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_CTX_GET,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\t\tctx->too_many = 1;\n\t\t\t}\n\t\treturn NULL;\n\t\t}\n\treturn (&(ctx->bn[ctx->tos++]));\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tif (ctx == NULL) return;\n\tassert(ctx->depth > 0);\n\tif (ctx->depth == 0)\n\t\tBN_CTX_start(ctx);\n\tctx->too_many = 0;\n\tctx->depth--;\n\tif (ctx->depth < BN_CTX_NUM_POS)\n\t\tctx->tos = ctx->pos[ctx->depth];\n\t}']
35,784
0
https://github.com/openssl/openssl/blob/27a3d9f9aa1ca6137ffd13a23775709c6f1ef567/crypto/bn/bntest.c/#L1192
int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx) { BIGNUM *a,*b[2],*c,*d,*e,*f,*g,*h; int i, j, ret = 0; int p0[] = {163,7,6,3,0,-1}; int p1[] = {193,15,0,-1}; a=BN_new(); b[0]=BN_new(); b[1]=BN_new(); c=BN_new(); d=BN_new(); e=BN_new(); f=BN_new(); g=BN_new(); h=BN_new(); BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); for (i=0; i<num0; i++) { BN_bntest_rand(a, 1024, 0, 0); BN_bntest_rand(c, 1024, 0, 0); BN_bntest_rand(d, 1024, 0, 0); for (j=0; j < 2; j++) { BN_GF2m_mod_mul(e, a, c, b[j], ctx); #if 0 if (bp != NULL) { if (!results) { BN_print(bp,a); BIO_puts(bp," * "); BN_print(bp,c); BIO_puts(bp," % "); BN_print(bp,b[j]); BIO_puts(bp," - "); BN_print(bp,e); BIO_puts(bp,"\n"); } } #endif BN_GF2m_add(f, a, d); BN_GF2m_mod_mul(g, f, c, b[j], ctx); BN_GF2m_mod_mul(h, d, c, b[j], ctx); BN_GF2m_add(f, e, g); BN_GF2m_add(f, f, h); if(!BN_is_zero(f)) { fprintf(stderr,"GF(2^m) modular multiplication test failed!\n"); goto err; } } } ret = 1; err: BN_free(a); BN_free(b[0]); BN_free(b[1]); BN_free(c); BN_free(d); BN_free(e); BN_free(f); BN_free(g); BN_free(h); return ret; }
['int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx)\n\t{\n\tBIGNUM *a,*b[2],*c,*d,*e,*f,*g,*h;\n\tint i, j, ret = 0;\n\tint p0[] = {163,7,6,3,0,-1};\n\tint p1[] = {193,15,0,-1};\n\ta=BN_new();\n\tb[0]=BN_new();\n\tb[1]=BN_new();\n\tc=BN_new();\n\td=BN_new();\n\te=BN_new();\n\tf=BN_new();\n\tg=BN_new();\n\th=BN_new();\n\tBN_GF2m_arr2poly(p0, b[0]);\n\tBN_GF2m_arr2poly(p1, b[1]);\n\tfor (i=0; i<num0; i++)\n\t\t{\n\t\tBN_bntest_rand(a, 1024, 0, 0);\n\t\tBN_bntest_rand(c, 1024, 0, 0);\n\t\tBN_bntest_rand(d, 1024, 0, 0);\n\t\tfor (j=0; j < 2; j++)\n\t\t\t{\n\t\t\tBN_GF2m_mod_mul(e, a, c, b[j], ctx);\n#if 0\n\t\t\tif (bp != NULL)\n\t\t\t\t{\n\t\t\t\tif (!results)\n\t\t\t\t\t{\n\t\t\t\t\tBN_print(bp,a);\n\t\t\t\t\tBIO_puts(bp," * ");\n\t\t\t\t\tBN_print(bp,c);\n\t\t\t\t\tBIO_puts(bp," % ");\n\t\t\t\t\tBN_print(bp,b[j]);\n\t\t\t\t\tBIO_puts(bp," - ");\n\t\t\t\t\tBN_print(bp,e);\n\t\t\t\t\tBIO_puts(bp,"\\n");\n\t\t\t\t\t}\n\t\t\t\t}\n#endif\n\t\t\tBN_GF2m_add(f, a, d);\n\t\t\tBN_GF2m_mod_mul(g, f, c, b[j], ctx);\n\t\t\tBN_GF2m_mod_mul(h, d, c, b[j], ctx);\n\t\t\tBN_GF2m_add(f, e, g);\n\t\t\tBN_GF2m_add(f, f, h);\n\t\t\tif(!BN_is_zero(f))\n\t\t\t\t{\n\t\t\t\tfprintf(stderr,"GF(2^m) modular multiplication test failed!\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tret = 1;\n err:\n\tBN_free(a);\n\tBN_free(b[0]);\n\tBN_free(b[1]);\n\tBN_free(c);\n\tBN_free(d);\n\tBN_free(e);\n\tBN_free(f);\n\tBN_free(g);\n\tBN_free(h);\n\treturn ret;\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\tbn_check_top(ret);\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\tif (num <= 0) return 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_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_flags[es->top]=0;\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 BN_GF2m_arr2poly(const int p[], BIGNUM *a)\n\t{\n\tint i;\n\tbn_check_top(a);\n\tBN_zero(a);\n\tfor (i = 0; p[i] != -1; i++)\n\t\t{\n\t\tif (BN_set_bit(a, p[i]) == 0)\n\t\t\treturn 0;\n\t\t}\n\tbn_check_top(a);\n\treturn 1;\n\t}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tbn_check_top(a);\n\tif (bn_expand(a,(int)sizeof(BN_ULONG)*8) == NULL) return(0);\n\ta->neg = 0;\n\ta->d[0] = w;\n\ta->top = (w ? 1 : 0);\n\tbn_check_top(a);\n\treturn(1);\n\t}']
35,785
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/lhash/lhash.c/#L296
void lh_doall_arg(LHASH *lh, void (*func)(), char *arg) { int i; LHASH_NODE *a,*n; for (i=lh->num_nodes-1; i>=0; i--) { a=lh->b[i]; while (a != NULL) { n=a->next; func(a->data,arg); a=n; } } }
['void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n\t{\n\tunsigned long i;\n\tTIMEOUT_PARAM tp;\n\ttp.ctx=s;\n\ttp.cache=s->sessions;\n\tif (tp.cache == NULL) return;\n\ttp.time=t;\n\tCRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\ti=tp.cache->down_load;\n\ttp.cache->down_load=0;\n\tlh_doall_arg(tp.cache,(void (*)())timeout,(char *)&tp);\n\ttp.cache->down_load=i;\n\tCRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t}', 'void lh_doall_arg(LHASH *lh, void (*func)(), char *arg)\n\t{\n\tint i;\n\tLHASH_NODE *a,*n;\n\tfor (i=lh->num_nodes-1; i>=0; i--)\n\t\t{\n\t\ta=lh->b[i];\n\t\twhile (a != NULL)\n\t\t\t{\n\t\t\tn=a->next;\n\t\t\tfunc(a->data,arg);\n\t\t\ta=n;\n\t\t\t}\n\t\t}\n\t}']
35,786
0
https://github.com/openssl/openssl/blob/f8c0218f09e190a2efb28302f6c9737efe151d27/apps/lib/opt.c/#L497
int opt_verify(int opt, X509_VERIFY_PARAM *vpm) { int i; ossl_intmax_t t = 0; ASN1_OBJECT *otmp; X509_PURPOSE *xptmp; const X509_VERIFY_PARAM *vtmp; OPENSSL_assert(vpm != NULL); OPENSSL_assert(opt > OPT_V__FIRST); OPENSSL_assert(opt < OPT_V__LAST); switch ((enum range)opt) { case OPT_V__FIRST: case OPT_V__LAST: return 0; case OPT_V_POLICY: otmp = OBJ_txt2obj(opt_arg(), 0); if (otmp == NULL) { opt_printf_stderr("%s: Invalid Policy %s\n", prog, opt_arg()); return 0; } X509_VERIFY_PARAM_add0_policy(vpm, otmp); break; case OPT_V_PURPOSE: i = X509_PURPOSE_get_by_sname(opt_arg()); if (i < 0) { opt_printf_stderr("%s: Invalid purpose %s\n", prog, opt_arg()); return 0; } xptmp = X509_PURPOSE_get0(i); i = X509_PURPOSE_get_id(xptmp); if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) { opt_printf_stderr("%s: Internal error setting purpose %s\n", prog, opt_arg()); return 0; } break; case OPT_V_VERIFY_NAME: vtmp = X509_VERIFY_PARAM_lookup(opt_arg()); if (vtmp == NULL) { opt_printf_stderr("%s: Invalid verify name %s\n", prog, opt_arg()); return 0; } X509_VERIFY_PARAM_set1(vpm, vtmp); break; case OPT_V_VERIFY_DEPTH: i = atoi(opt_arg()); if (i >= 0) X509_VERIFY_PARAM_set_depth(vpm, i); break; case OPT_V_VERIFY_AUTH_LEVEL: i = atoi(opt_arg()); if (i >= 0) X509_VERIFY_PARAM_set_auth_level(vpm, i); break; case OPT_V_ATTIME: if (!opt_imax(opt_arg(), &t)) return 0; if (t != (time_t)t) { opt_printf_stderr("%s: epoch time out of range %s\n", prog, opt_arg()); return 0; } X509_VERIFY_PARAM_set_time(vpm, (time_t)t); break; case OPT_V_VERIFY_HOSTNAME: if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0)) return 0; break; case OPT_V_VERIFY_EMAIL: if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0)) return 0; break; case OPT_V_VERIFY_IP: if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg())) return 0; break; case OPT_V_IGNORE_CRITICAL: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL); break; case OPT_V_ISSUER_CHECKS: break; case OPT_V_CRL_CHECK: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK); break; case OPT_V_CRL_CHECK_ALL: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL); break; case OPT_V_POLICY_CHECK: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK); break; case OPT_V_EXPLICIT_POLICY: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY); break; case OPT_V_INHIBIT_ANY: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY); break; case OPT_V_INHIBIT_MAP: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP); break; case OPT_V_X509_STRICT: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT); break; case OPT_V_EXTENDED_CRL: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT); break; case OPT_V_USE_DELTAS: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS); break; case OPT_V_POLICY_PRINT: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY); break; case OPT_V_CHECK_SS_SIG: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE); break; case OPT_V_TRUSTED_FIRST: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST); break; case OPT_V_SUITEB_128_ONLY: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY); break; case OPT_V_SUITEB_128: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS); break; case OPT_V_SUITEB_192: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS); break; case OPT_V_PARTIAL_CHAIN: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN); break; case OPT_V_NO_ALT_CHAINS: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS); break; case OPT_V_NO_CHECK_TIME: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME); break; case OPT_V_ALLOW_PROXY_CERTS: X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS); break; } return 1; }
['int opt_verify(int opt, X509_VERIFY_PARAM *vpm)\n{\n int i;\n ossl_intmax_t t = 0;\n ASN1_OBJECT *otmp;\n X509_PURPOSE *xptmp;\n const X509_VERIFY_PARAM *vtmp;\n OPENSSL_assert(vpm != NULL);\n OPENSSL_assert(opt > OPT_V__FIRST);\n OPENSSL_assert(opt < OPT_V__LAST);\n switch ((enum range)opt) {\n case OPT_V__FIRST:\n case OPT_V__LAST:\n return 0;\n case OPT_V_POLICY:\n otmp = OBJ_txt2obj(opt_arg(), 0);\n if (otmp == NULL) {\n opt_printf_stderr("%s: Invalid Policy %s\\n", prog, opt_arg());\n return 0;\n }\n X509_VERIFY_PARAM_add0_policy(vpm, otmp);\n break;\n case OPT_V_PURPOSE:\n i = X509_PURPOSE_get_by_sname(opt_arg());\n if (i < 0) {\n opt_printf_stderr("%s: Invalid purpose %s\\n", prog, opt_arg());\n return 0;\n }\n xptmp = X509_PURPOSE_get0(i);\n i = X509_PURPOSE_get_id(xptmp);\n if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) {\n opt_printf_stderr("%s: Internal error setting purpose %s\\n",\n prog, opt_arg());\n return 0;\n }\n break;\n case OPT_V_VERIFY_NAME:\n vtmp = X509_VERIFY_PARAM_lookup(opt_arg());\n if (vtmp == NULL) {\n opt_printf_stderr("%s: Invalid verify name %s\\n",\n prog, opt_arg());\n return 0;\n }\n X509_VERIFY_PARAM_set1(vpm, vtmp);\n break;\n case OPT_V_VERIFY_DEPTH:\n i = atoi(opt_arg());\n if (i >= 0)\n X509_VERIFY_PARAM_set_depth(vpm, i);\n break;\n case OPT_V_VERIFY_AUTH_LEVEL:\n i = atoi(opt_arg());\n if (i >= 0)\n X509_VERIFY_PARAM_set_auth_level(vpm, i);\n break;\n case OPT_V_ATTIME:\n if (!opt_imax(opt_arg(), &t))\n return 0;\n if (t != (time_t)t) {\n opt_printf_stderr("%s: epoch time out of range %s\\n",\n prog, opt_arg());\n return 0;\n }\n X509_VERIFY_PARAM_set_time(vpm, (time_t)t);\n break;\n case OPT_V_VERIFY_HOSTNAME:\n if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0))\n return 0;\n break;\n case OPT_V_VERIFY_EMAIL:\n if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0))\n return 0;\n break;\n case OPT_V_VERIFY_IP:\n if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg()))\n return 0;\n break;\n case OPT_V_IGNORE_CRITICAL:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL);\n break;\n case OPT_V_ISSUER_CHECKS:\n break;\n case OPT_V_CRL_CHECK:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK);\n break;\n case OPT_V_CRL_CHECK_ALL:\n X509_VERIFY_PARAM_set_flags(vpm,\n X509_V_FLAG_CRL_CHECK |\n X509_V_FLAG_CRL_CHECK_ALL);\n break;\n case OPT_V_POLICY_CHECK:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK);\n break;\n case OPT_V_EXPLICIT_POLICY:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY);\n break;\n case OPT_V_INHIBIT_ANY:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY);\n break;\n case OPT_V_INHIBIT_MAP:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP);\n break;\n case OPT_V_X509_STRICT:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT);\n break;\n case OPT_V_EXTENDED_CRL:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT);\n break;\n case OPT_V_USE_DELTAS:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS);\n break;\n case OPT_V_POLICY_PRINT:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY);\n break;\n case OPT_V_CHECK_SS_SIG:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE);\n break;\n case OPT_V_TRUSTED_FIRST:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST);\n break;\n case OPT_V_SUITEB_128_ONLY:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY);\n break;\n case OPT_V_SUITEB_128:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS);\n break;\n case OPT_V_SUITEB_192:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS);\n break;\n case OPT_V_PARTIAL_CHAIN:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);\n break;\n case OPT_V_NO_ALT_CHAINS:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS);\n break;\n case OPT_V_NO_CHECK_TIME:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME);\n break;\n case OPT_V_ALLOW_PROXY_CERTS:\n X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS);\n break;\n }\n return 1;\n}', 'char *opt_arg(void)\n{\n return arg;\n}', 'X509_PURPOSE *X509_PURPOSE_get0(int idx)\n{\n if (idx < 0)\n return NULL;\n if (idx < (int)X509_PURPOSE_COUNT)\n return xstandard + idx;\n return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);\n}', 'DEFINE_STACK_OF(X509_PURPOSE)', 'void *OPENSSL_sk_value(const OPENSSL_STACK *st, int i)\n{\n if (st == NULL || i < 0 || i >= st->num)\n return NULL;\n return (void *)st->data[i];\n}', 'int X509_PURPOSE_get_id(const X509_PURPOSE *xp)\n{\n return xp->purpose;\n}']
35,787
0
https://github.com/openssl/openssl/blob/55442b8a5b719f54578083fae0fcc814b599cd84/crypto/bn/bn_sqr.c/#L120
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) { int i, j, max; const BN_ULONG *ap; BN_ULONG *rp; max = n * 2; ap = a; rp = r; rp[0] = rp[max - 1] = 0; rp++; j = n; if (--j > 0) { ap++; rp[j] = bn_mul_words(rp, ap, j, ap[-1]); rp += 2; } for (i = n - 2; i > 0; i--) { j--; ap++; rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]); rp += 2; } bn_add_words(r, r, r, max); bn_sqr_words(tmp, a, n); bn_add_words(r, r, tmp, max); }
['static\nECDSA_SIG *SM2_sig_gen(const EC_KEY *key, const BIGNUM *e)\n{\n const BIGNUM *dA = EC_KEY_get0_private_key(key);\n const EC_GROUP *group = EC_KEY_get0_group(key);\n const BIGNUM *order = EC_GROUP_get0_order(group);\n ECDSA_SIG *sig = NULL;\n EC_POINT *kG = NULL;\n BN_CTX *ctx = NULL;\n BIGNUM *k = NULL;\n BIGNUM *rk = NULL;\n BIGNUM *r = NULL;\n BIGNUM *s = NULL;\n BIGNUM *x1 = NULL;\n BIGNUM *tmp = NULL;\n kG = EC_POINT_new(group);\n if (kG == NULL)\n goto done;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto done;\n BN_CTX_start(ctx);\n k = BN_CTX_get(ctx);\n rk = BN_CTX_get(ctx);\n x1 = BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto done;\n r = BN_new();\n s = BN_new();\n if (r == NULL || s == NULL)\n goto done;\n for (;;) {\n BN_priv_rand_range(k, order);\n if (EC_POINT_mul(group, kG, k, NULL, NULL, ctx) == 0)\n goto done;\n if (EC_POINT_get_affine_coordinates_GFp(group, kG, x1, NULL, ctx) == 0)\n goto done;\n if (BN_mod_add(r, e, x1, order, ctx) == 0)\n goto done;\n if (BN_is_zero(r))\n continue;\n BN_add(rk, r, k);\n if (BN_cmp(rk, order) == 0)\n continue;\n BN_add(s, dA, BN_value_one());\n BN_mod_inverse(s, s, order, ctx);\n BN_mod_mul(tmp, dA, r, order, ctx);\n BN_sub(tmp, k, tmp);\n BN_mod_mul(s, s, tmp, order, ctx);\n sig = ECDSA_SIG_new();\n if (sig == NULL)\n goto done;\n ECDSA_SIG_set0(sig, r, s);\n break;\n }\n done:\n if (sig == NULL) {\n BN_free(r);\n BN_free(s);\n }\n BN_CTX_free(ctx);\n EC_POINT_free(kG);\n return sig;\n}', 'int BN_add(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_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}', '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 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 if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\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_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
35,788
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/des/des_enc.c/#L143
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; }
['void des_random_key(unsigned char *ret)\n\t{\n\tdes_key_schedule ks;\n\tstatic DES_LONG c=0;\n\tstatic unsigned short pid=0;\n\tstatic des_cblock data={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};\n\tdes_cblock key;\n\tunsigned char *p;\n\tDES_LONG t;\n\tint i;\n#ifdef MSDOS\n\tpid=1;\n#else\n\tif (!pid) pid=getpid();\n#endif\n\tp=key;\n\tif (seed)\n\t\t{\n\t\tfor (i=0; i<8; i++)\n\t\t\t{\n\t\t\tdata[i] ^= init[i];\n\t\t\tinit[i]=0;\n\t\t\t}\n\t\tseed=0;\n\t\t}\n\tt=(DES_LONG)time(NULL);\n\tl2c(t,p);\n\tt=(DES_LONG)((pid)|((c++)<<16));\n\tl2c(t,p);\n\tdes_set_odd_parity(data);\n\tdes_set_key(data,ks);\n\tdes_cbc_cksum(key,key,sizeof(key),ks,data);\n\tdes_set_odd_parity(key);\n\tdes_set_key(key,ks);\n\tdes_cbc_cksum(key,data,sizeof(key),ks,key);\n\tmemcpy(ret,data,sizeof(key));\n\tmemset(key,0,sizeof(key));\n\tmemset(ks,0,sizeof(ks));\n\tt=0;\n\t}', 'DES_LONG des_cbc_cksum(const unsigned char *in, des_cblock out, long length,\n\t des_key_schedule schedule, const des_cblock iv)\n\t{\n\tregister DES_LONG tout0,tout1,tin0,tin1;\n\tregister long l=length;\n\tDES_LONG tin[2];\n\tc2l(iv,tout0);\n\tc2l(iv,tout1);\n\tfor (; l>0; l-=8)\n\t\t{\n\t\tif (l >= 8)\n\t\t\t{\n\t\t\tc2l(in,tin0);\n\t\t\tc2l(in,tin1);\n\t\t\t}\n\t\telse\n\t\t\tc2ln(in,tin0,tin1,l);\n\t\ttin0^=tout0; tin[0]=tin0;\n\t\ttin1^=tout1; tin[1]=tin1;\n\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);\n\t\ttout0=tin[0];\n\t\ttout1=tin[1];\n\t\t}\n\tif (out != NULL)\n\t\t{\n\t\tl2c(tout0,out);\n\t\tl2c(tout1,out);\n\t\t}\n\ttout0=tin0=tin1=tin[0]=tin[1]=0;\n\treturn(tout1);\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,789
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 BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],\n BN_CTX *ctx)\n{\n BIGNUM *field;\n int ret = 0;\n bn_check_top(xx);\n BN_CTX_start(ctx);\n if ((field = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_GF2m_arr2poly(p, field))\n goto err;\n ret = BN_GF2m_mod_inv(r, xx, field, ctx);\n bn_check_top(r);\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}', '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,790
0
https://github.com/libav/libav/blob/6cecd63005b29a1dc3a5104e6ac85fd112705122/ffmpeg.c/#L3174
static void new_audio_stream(AVFormatContext *oc) { AVStream *st; AVCodecContext *audio_enc; int codec_id; st = av_new_stream(oc, oc->nb_streams); if (!st) { fprintf(stderr, "Could not alloc stream\n"); av_exit(1); } avcodec_get_context_defaults2(st->codec, CODEC_TYPE_AUDIO); bitstream_filters[nb_output_files][oc->nb_streams - 1]= audio_bitstream_filters; audio_bitstream_filters= NULL; if(thread_count>1) avcodec_thread_init(st->codec, thread_count); audio_enc = st->codec; audio_enc->codec_type = CODEC_TYPE_AUDIO; if(audio_codec_tag) audio_enc->codec_tag= audio_codec_tag; if (oc->oformat->flags & AVFMT_GLOBALHEADER) { audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER; avcodec_opts[CODEC_TYPE_AUDIO]->flags|= CODEC_FLAG_GLOBAL_HEADER; } if (audio_stream_copy) { st->stream_copy = 1; audio_enc->channels = audio_channels; } else { AVCodec *codec; set_context_opts(audio_enc, avcodec_opts[CODEC_TYPE_AUDIO], AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM); if (audio_codec_name) { codec_id = find_codec_or_die(audio_codec_name, CODEC_TYPE_AUDIO, 1); codec = avcodec_find_encoder_by_name(audio_codec_name); output_codecs[nb_ocodecs] = codec; } else { codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, CODEC_TYPE_AUDIO); codec = avcodec_find_encoder(codec_id); } audio_enc->codec_id = codec_id; if (audio_qscale > QSCALE_NONE) { audio_enc->flags |= CODEC_FLAG_QSCALE; audio_enc->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale; } audio_enc->thread_count = thread_count; audio_enc->channels = audio_channels; audio_enc->sample_fmt = audio_sample_fmt; audio_enc->channel_layout = channel_layout; if(codec && codec->sample_fmts){ const enum SampleFormat *p= codec->sample_fmts; for(; *p!=-1; p++){ if(*p == audio_enc->sample_fmt) break; } if(*p == -1) audio_enc->sample_fmt = codec->sample_fmts[0]; } } nb_ocodecs++; audio_enc->sample_rate = audio_sample_rate; audio_enc->time_base= (AVRational){1, audio_sample_rate}; if (audio_language) { av_metadata_set(&st->metadata, "language", audio_language); av_free(audio_language); audio_language = NULL; } audio_disable = 0; av_freep(&audio_codec_name); audio_stream_copy = 0; }
['static void new_audio_stream(AVFormatContext *oc)\n{\n AVStream *st;\n AVCodecContext *audio_enc;\n int codec_id;\n st = av_new_stream(oc, oc->nb_streams);\n if (!st) {\n fprintf(stderr, "Could not alloc stream\\n");\n av_exit(1);\n }\n avcodec_get_context_defaults2(st->codec, CODEC_TYPE_AUDIO);\n bitstream_filters[nb_output_files][oc->nb_streams - 1]= audio_bitstream_filters;\n audio_bitstream_filters= NULL;\n if(thread_count>1)\n avcodec_thread_init(st->codec, thread_count);\n audio_enc = st->codec;\n audio_enc->codec_type = CODEC_TYPE_AUDIO;\n if(audio_codec_tag)\n audio_enc->codec_tag= audio_codec_tag;\n if (oc->oformat->flags & AVFMT_GLOBALHEADER) {\n audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;\n avcodec_opts[CODEC_TYPE_AUDIO]->flags|= CODEC_FLAG_GLOBAL_HEADER;\n }\n if (audio_stream_copy) {\n st->stream_copy = 1;\n audio_enc->channels = audio_channels;\n } else {\n AVCodec *codec;\n set_context_opts(audio_enc, avcodec_opts[CODEC_TYPE_AUDIO], AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM);\n if (audio_codec_name) {\n codec_id = find_codec_or_die(audio_codec_name, CODEC_TYPE_AUDIO, 1);\n codec = avcodec_find_encoder_by_name(audio_codec_name);\n output_codecs[nb_ocodecs] = codec;\n } else {\n codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, CODEC_TYPE_AUDIO);\n codec = avcodec_find_encoder(codec_id);\n }\n audio_enc->codec_id = codec_id;\n if (audio_qscale > QSCALE_NONE) {\n audio_enc->flags |= CODEC_FLAG_QSCALE;\n audio_enc->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale;\n }\n audio_enc->thread_count = thread_count;\n audio_enc->channels = audio_channels;\n audio_enc->sample_fmt = audio_sample_fmt;\n audio_enc->channel_layout = channel_layout;\n if(codec && codec->sample_fmts){\n const enum SampleFormat *p= codec->sample_fmts;\n for(; *p!=-1; p++){\n if(*p == audio_enc->sample_fmt)\n break;\n }\n if(*p == -1)\n audio_enc->sample_fmt = codec->sample_fmts[0];\n }\n }\n nb_ocodecs++;\n audio_enc->sample_rate = audio_sample_rate;\n audio_enc->time_base= (AVRational){1, audio_sample_rate};\n if (audio_language) {\n av_metadata_set(&st->metadata, "language", audio_language);\n av_free(audio_language);\n audio_language = NULL;\n }\n audio_disable = 0;\n av_freep(&audio_codec_name);\n audio_stream_copy = 0;\n}', 'AVStream *av_new_stream(AVFormatContext *s, int id)\n{\n AVStream *st;\n int i;\n if (s->nb_streams >= MAX_STREAMS)\n return NULL;\n st = av_mallocz(sizeof(AVStream));\n if (!st)\n return NULL;\n st->codec= avcodec_alloc_context();\n if (s->iformat) {\n st->codec->bit_rate = 0;\n }\n st->index = s->nb_streams;\n st->id = id;\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 av_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,791
0
https://github.com/openssl/openssl/blob/aa24c4a736b095bfaa0698bf87e61bec7b5d8691/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 ssl3_connect(SSL *s)\n\t{\n\tBUF_MEM *buf=NULL;\n\tunsigned long Time=(unsigned long)time(NULL);\n\tvoid (*cb)(const SSL *ssl,int type,int val)=NULL;\n\tint ret= -1;\n\tint new_state,state,skip=0;\n\tRAND_add(&Time,sizeof(Time),0);\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\ts->in_handshake++;\n\tif (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);\n\tfor (;;)\n\t\t{\n\t\tstate=s->state;\n\t\tswitch(s->state)\n\t\t\t{\n\t\tcase SSL_ST_RENEGOTIATE:\n\t\t\ts->renegotiate=1;\n\t\t\ts->state=SSL_ST_CONNECT;\n\t\t\ts->ctx->stats.sess_connect_renegotiate++;\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\tif ((s->version & 0xff00 ) != 0x0300)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);\n\t\t\t\tret = -1;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\ts->type=SSL_ST_CONNECT;\n\t\t\tif (s->init_buf == NULL)\n\t\t\t\t{\n\t\t\t\tif ((buf=BUF_MEM_new()) == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tret= -1;\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tif (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))\n\t\t\t\t\t{\n\t\t\t\t\tret= -1;\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\ts->init_buf=buf;\n\t\t\t\tbuf=NULL;\n\t\t\t\t}\n\t\t\tif (!ssl3_setup_buffers(s)) { ret= -1; goto end; }\n\t\t\tif (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }\n\t\t\tssl3_init_finished_mac(s);\n\t\t\ts->state=SSL3_ST_CW_CLNT_HELLO_A;\n\t\t\ts->ctx->stats.sess_connect++;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_CLNT_HELLO_A:\n\t\tcase SSL3_ST_CW_CLNT_HELLO_B:\n\t\t\ts->shutdown=0;\n\t\t\tret=ssl3_client_hello(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CR_SRVR_HELLO_A;\n\t\t\ts->init_num=0;\n\t\t\tif (s->bbio != s->wbio)\n\t\t\t\ts->wbio=BIO_push(s->bbio,s->wbio);\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_SRVR_HELLO_A:\n\t\tcase SSL3_ST_CR_SRVR_HELLO_B:\n\t\t\tret=ssl3_get_server_hello(s);\n#ifndef OPENSSL_NO_SRP\n\t\t\tif ((ret == 0) && (s->s3->warn_alert == SSL_AD_MISSING_SRP_USERNAME))\n\t\t\t\t{\n\t\t\t\tif (!SRP_have_to_put_srp_username(s))\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_MISSING_SRP_USERNAME);\n\t\t\t\t\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_USER_CANCELLED);\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\ts->state=SSL3_ST_CW_CLNT_HELLO_A;\n\t\t\t\tif (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }\n\t\t\t\tbreak;\n\t\t\t\t}\n#endif\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (s->hit)\n\t\t\t\ts->state=SSL3_ST_CR_FINISHED_A;\n\t\t\telse\n\t\t\t\ts->state=SSL3_ST_CR_CERT_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_CERT_A:\n\t\tcase SSL3_ST_CR_CERT_B:\n#ifndef OPENSSL_NO_TLSEXT\n\t\t\tret=ssl3_check_finished(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (ret == 2)\n\t\t\t\t{\n\t\t\t\ts->hit = 1;\n\t\t\t\tif (s->tlsext_ticket_expected)\n\t\t\t\t\ts->state=SSL3_ST_CR_SESSION_TICKET_A;\n\t\t\t\telse\n\t\t\t\t\ts->state=SSL3_ST_CR_FINISHED_A;\n\t\t\t\ts->init_num=0;\n\t\t\t\tbreak;\n\t\t\t\t}\n#endif\n\t\t\tif (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&\n\t\t\t !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))\n\t\t\t\t{\n\t\t\t\tret=ssl3_get_server_certificate(s);\n\t\t\t\tif (ret <= 0) goto end;\n#ifndef OPENSSL_NO_TLSEXT\n\t\t\t\tif (s->tlsext_status_expected)\n\t\t\t\t\ts->state=SSL3_ST_CR_CERT_STATUS_A;\n\t\t\t\telse\n\t\t\t\t\ts->state=SSL3_ST_CR_KEY_EXCH_A;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tskip = 1;\n\t\t\t\ts->state=SSL3_ST_CR_KEY_EXCH_A;\n\t\t\t\t}\n#else\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tskip=1;\n\t\t\ts->state=SSL3_ST_CR_KEY_EXCH_A;\n#endif\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_KEY_EXCH_A:\n\t\tcase SSL3_ST_CR_KEY_EXCH_B:\n\t\t\tret=ssl3_get_key_exchange(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CR_CERT_REQ_A;\n\t\t\ts->init_num=0;\n\t\t\tif (!ssl3_check_cert_and_algorithm(s))\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\tbreak;\n\t\tcase SSL3_ST_CR_CERT_REQ_A:\n\t\tcase SSL3_ST_CR_CERT_REQ_B:\n\t\t\tret=ssl3_get_certificate_request(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CR_SRVR_DONE_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_SRVR_DONE_A:\n\t\tcase SSL3_ST_CR_SRVR_DONE_B:\n\t\t\tret=ssl3_get_server_done(s);\n\t\t\tif (ret <= 0) goto end;\n#ifndef OPENSSL_NO_SRP\n\t\t\tif (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP)\n\t\t\t\t{\n\t\t\t\tif ((ret = SRP_Calc_A_param(s))<=0)\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_SRP_A_CALC);\n\t\t\t\t\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\t}\n#endif\n\t\t\tif (s->s3->tmp.cert_req)\n\t\t\t\ts->state=SSL3_ST_CW_CERT_A;\n\t\t\telse\n\t\t\t\ts->state=SSL3_ST_CW_KEY_EXCH_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_CERT_A:\n\t\tcase SSL3_ST_CW_CERT_B:\n\t\tcase SSL3_ST_CW_CERT_C:\n\t\tcase SSL3_ST_CW_CERT_D:\n\t\t\tret=ssl3_send_client_certificate(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CW_KEY_EXCH_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_KEY_EXCH_A:\n\t\tcase SSL3_ST_CW_KEY_EXCH_B:\n\t\t\tret=ssl3_send_client_key_exchange(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (s->s3->tmp.cert_req == 1)\n\t\t\t\t{\n\t\t\t\ts->state=SSL3_ST_CW_CERT_VRFY_A;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts->state=SSL3_ST_CW_CHANGE_A;\n\t\t\t\ts->s3->change_cipher_spec=0;\n\t\t\t\t}\n\t\t\tif (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)\n\t\t\t\t{\n\t\t\t\ts->state=SSL3_ST_CW_CHANGE_A;\n\t\t\t\ts->s3->change_cipher_spec=0;\n\t\t\t\t}\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_CERT_VRFY_A:\n\t\tcase SSL3_ST_CW_CERT_VRFY_B:\n\t\t\tret=ssl3_send_client_verify(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CW_CHANGE_A;\n\t\t\ts->init_num=0;\n\t\t\ts->s3->change_cipher_spec=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_CHANGE_A:\n\t\tcase SSL3_ST_CW_CHANGE_B:\n\t\t\tret=ssl3_send_change_cipher_spec(s,\n\t\t\t\tSSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);\n\t\t\tif (ret <= 0) goto end;\n#if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)\n\t\t\ts->state=SSL3_ST_CW_FINISHED_A;\n#else\n\t\t\tif (s->next_proto_negotiated)\n\t\t\t\ts->state=SSL3_ST_CW_NEXT_PROTO_A;\n\t\t\telse\n\t\t\t\ts->state=SSL3_ST_CW_FINISHED_A;\n#endif\n\t\t\ts->init_num=0;\n\t\t\ts->session->cipher=s->s3->tmp.new_cipher;\n#ifdef OPENSSL_NO_COMP\n\t\t\ts->session->compress_meth=0;\n#else\n\t\t\tif (s->s3->tmp.new_compression == NULL)\n\t\t\t\ts->session->compress_meth=0;\n\t\t\telse\n\t\t\t\ts->session->compress_meth=\n\t\t\t\t\ts->s3->tmp.new_compression->id;\n#endif\n\t\t\tif (!s->method->ssl3_enc->setup_key_block(s))\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 (!s->method->ssl3_enc->change_cipher_state(s,\n\t\t\t\tSSL3_CHANGE_CIPHER_CLIENT_WRITE))\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\tbreak;\n#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)\n\t\tcase SSL3_ST_CW_NEXT_PROTO_A:\n\t\tcase SSL3_ST_CW_NEXT_PROTO_B:\n\t\t\tret=ssl3_send_next_proto(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CW_FINISHED_A;\n\t\t\tbreak;\n#endif\n\t\tcase SSL3_ST_CW_FINISHED_A:\n\t\tcase SSL3_ST_CW_FINISHED_B:\n\t\t\tret=ssl3_send_finished(s,\n\t\t\t\tSSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,\n\t\t\t\ts->method->ssl3_enc->client_finished_label,\n\t\t\t\ts->method->ssl3_enc->client_finished_label_len);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CW_FLUSH;\n\t\t\ts->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;\n\t\t\tif (s->hit)\n\t\t\t\t{\n\t\t\t\ts->s3->tmp.next_state=SSL_ST_OK;\n\t\t\t\tif (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)\n\t\t\t\t\t{\n\t\t\t\t\ts->state=SSL_ST_OK;\n\t\t\t\t\ts->s3->flags|=SSL3_FLAGS_POP_BUFFER;\n\t\t\t\t\ts->s3->delay_buf_pop_ret=0;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n#ifndef OPENSSL_NO_TLSEXT\n\t\t\t\tif (s->tlsext_ticket_expected)\n\t\t\t\t\ts->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;\n\t\t\t\telse\n#endif\n\t\t\t\ts->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;\n\t\t\t\t}\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n#ifndef OPENSSL_NO_TLSEXT\n\t\tcase SSL3_ST_CR_SESSION_TICKET_A:\n\t\tcase SSL3_ST_CR_SESSION_TICKET_B:\n\t\t\tret=ssl3_get_new_session_ticket(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CR_FINISHED_A;\n\t\t\ts->init_num=0;\n\t\tbreak;\n\t\tcase SSL3_ST_CR_CERT_STATUS_A:\n\t\tcase SSL3_ST_CR_CERT_STATUS_B:\n\t\t\tret=ssl3_get_cert_status(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CR_KEY_EXCH_A;\n\t\t\ts->init_num=0;\n\t\tbreak;\n#endif\n\t\tcase SSL3_ST_CR_FINISHED_A:\n\t\tcase SSL3_ST_CR_FINISHED_B:\n\t\t\tret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,\n\t\t\t\tSSL3_ST_CR_FINISHED_B);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (s->hit)\n\t\t\t\ts->state=SSL3_ST_CW_CHANGE_A;\n\t\t\telse\n\t\t\t\ts->state=SSL_ST_OK;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_FLUSH:\n\t\t\ts->rwstate=SSL_WRITING;\n\t\t\tif (BIO_flush(s->wbio) <= 0)\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->rwstate=SSL_NOTHING;\n\t\t\ts->state=s->s3->tmp.next_state;\n\t\t\tbreak;\n\t\tcase SSL_ST_OK:\n\t\t\tssl3_cleanup_key_block(s);\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\tif (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))\n\t\t\t\tssl_free_wbio_buffer(s);\n\t\t\ts->init_num=0;\n\t\t\ts->renegotiate=0;\n\t\t\ts->new_session=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->handshake_func=ssl3_connect;\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_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);\n\t\t\tret= -1;\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (!s->s3->tmp.reuse_message && !skip)\n\t\t\t{\n\t\t\tif (s->debug)\n\t\t\t\t{\n\t\t\t\tif ((ret=BIO_flush(s->wbio)) <= 0)\n\t\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tif ((cb != NULL) && (s->state != state))\n\t\t\t\t{\n\t\t\t\tnew_state=s->state;\n\t\t\t\ts->state=state;\n\t\t\t\tcb(s,SSL_CB_CONNECT_LOOP,1);\n\t\t\t\ts->state=new_state;\n\t\t\t\t}\n\t\t\t}\n\t\tskip=0;\n\t\t}\nend:\n\ts->in_handshake--;\n\tif (buf != NULL)\n\t\tBUF_MEM_free(buf);\n\tif (cb != NULL)\n\t\tcb(s,SSL_CB_CONNECT_EXIT,ret);\n\treturn(ret);\n\t}', 'int ssl3_get_server_hello(SSL *s)\n\t{\n\tSTACK_OF(SSL_CIPHER) *sk;\n\tconst SSL_CIPHER *c;\n\tunsigned char *p,*d;\n\tint i,al,ok;\n\tunsigned int j;\n\tlong n;\n#ifndef OPENSSL_NO_COMP\n\tSSL_COMP *comp;\n#endif\n\tn=s->method->ssl_get_message(s,\n\t\tSSL3_ST_CR_SRVR_HELLO_A,\n\t\tSSL3_ST_CR_SRVR_HELLO_B,\n\t\t-1,\n\t\t20000,\n\t\t&ok);\n\tif (!ok) return((int)n);\n\tif ( SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)\n\t\t{\n\t\tif ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)\n\t\t\t{\n\t\t\tif ( s->d1->send_cookie == 0)\n\t\t\t\t{\n\t\t\t\ts->s3->tmp.reuse_message = 1;\n\t\t\t\treturn 1;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)\n\t\t{\n\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);\n\t\tgoto f_err;\n\t\t}\n\td=p=(unsigned char *)s->init_msg;\n\tif ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);\n\t\ts->version=(s->version&0xff00)|p[1];\n\t\tal=SSL_AD_PROTOCOL_VERSION;\n\t\tgoto f_err;\n\t\t}\n\tp+=2;\n\tmemcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);\n\tp+=SSL3_RANDOM_SIZE;\n\tj= *(p++);\n\tif ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))\n\t\t{\n\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);\n\t\tgoto f_err;\n\t\t}\n#ifndef OPENSSL_NO_TLSEXT\n\tif (s->version >= TLS1_VERSION && s->tls_session_secret_cb)\n\t\t{\n\t\tSSL_CIPHER *pref_cipher=NULL;\n\t\ts->session->master_key_length=sizeof(s->session->master_key);\n\t\tif (s->tls_session_secret_cb(s, s->session->master_key,\n\t\t\t\t\t &s->session->master_key_length,\n\t\t\t\t\t NULL, &pref_cipher,\n\t\t\t\t\t s->tls_session_secret_cb_arg))\n\t\t\t{\n\t\t\ts->session->cipher = pref_cipher ?\n\t\t\t\tpref_cipher : ssl_get_cipher_by_char(s, p+j);\n\t\t\t}\n\t\t}\n#endif\n\tif (j != 0 && j == s->session->session_id_length\n\t && memcmp(p,s->session->session_id,j) == 0)\n\t {\n\t if(s->sid_ctx_length != s->session->sid_ctx_length\n\t || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))\n\t\t{\n\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);\n\t\tgoto f_err;\n\t\t}\n\t s->hit=1;\n\t }\n\telse\n\t\t{\n\t\ts->hit=0;\n\t\tif (s->session->session_id_length > 0)\n\t\t\t{\n\t\t\tif (!ssl_get_new_session(s,0))\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_INTERNAL_ERROR;\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\ts->session->session_id_length=j;\n\t\tmemcpy(s->session->session_id,p,j);\n\t\t}\n\tp+=j;\n\tc=ssl_get_cipher_by_char(s,p);\n\tif (c == NULL)\n\t\t{\n\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);\n\t\tgoto f_err;\n\t\t}\n\tp+=ssl_put_cipher_by_char(s,NULL,NULL);\n\tsk=ssl_get_ciphers_by_id(s);\n\ti=sk_SSL_CIPHER_find(sk,c);\n\tif (i < 0)\n\t\t{\n\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);\n\t\tgoto f_err;\n\t\t}\n\tif (s->session->cipher)\n\t\ts->session->cipher_id = s->session->cipher->id;\n\tif (s->hit && (s->session->cipher_id != c->id))\n\t\t{\n#if 0\n\t\tif (!(s->options &\n\t\t\tSSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))\n#endif\n\t\t\t{\n\t\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\ts->s3->tmp.new_cipher=c;\n\tif (!ssl3_digest_cached_records(s))\n\t\tgoto f_err;\n#ifdef OPENSSL_NO_COMP\n\tif (*(p++) != 0)\n\t\t{\n\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);\n\t\tgoto f_err;\n\t\t}\n\tif (s->session->compress_meth != 0)\n\t\t{\n\t\tal=SSL_AD_INTERNAL_ERROR;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_INCONSISTENT_COMPRESSION);\n\t\tgoto f_err;\n\t\t}\n#else\n\tj= *(p++);\n\tif (s->hit && j != s->session->compress_meth)\n\t\t{\n\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);\n\t\tgoto f_err;\n\t\t}\n\tif (j == 0)\n\t\tcomp=NULL;\n\telse if (s->options & SSL_OP_NO_COMPRESSION)\n\t\t{\n\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_COMPRESSION_DISABLED);\n\t\tgoto f_err;\n\t\t}\n\telse\n\t\tcomp=ssl3_comp_find(s->ctx->comp_methods,j);\n\tif ((j != 0) && (comp == NULL))\n\t\t{\n\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);\n\t\tgoto f_err;\n\t\t}\n\telse\n\t\t{\n\t\ts->s3->tmp.new_compression=comp;\n\t\t}\n#endif\n#ifndef OPENSSL_NO_TLSEXT\n\tif (s->version >= SSL3_VERSION)\n\t\t{\n\t\tif (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (ssl_check_serverhello_tlsext(s) <= 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n#endif\n\tif (p != (d+n))\n\t\t{\n\t\tal=SSL_AD_DECODE_ERROR;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);\n\t\tgoto err;\n\t\t}\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\treturn(-1);\n\t}', 'int ssl_check_serverhello_tlsext(SSL *s)\n\t{\n\tint ret=SSL_TLSEXT_ERR_NOACK;\n\tint al = SSL_AD_UNRECOGNIZED_NAME;\n#ifndef OPENSSL_NO_EC\n\tunsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;\n\tunsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;\n\tif ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&\n\t (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&\n\t ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))\n\t\t{\n\t\tsize_t i;\n\t\tunsigned char *list;\n\t\tint found_uncompressed = 0;\n\t\tlist = s->session->tlsext_ecpointformatlist;\n\t\tfor (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)\n\t\t\t{\n\t\t\tif (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)\n\t\t\t\t{\n\t\t\t\tfound_uncompressed = 1;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tif (!found_uncompressed)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);\n\t\t\treturn -1;\n\t\t\t}\n\t\t}\n\tret = SSL_TLSEXT_ERR_OK;\n#endif\n\tif (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)\n\t\tret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);\n\telse if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)\n\t\tret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);\n#ifdef TLSEXT_TYPE_opaque_prf_input\n\tif (s->s3->server_opaque_prf_input_len > 0)\n\t\t{\n\t\tif (s->s3->server_opaque_prf_input == NULL)\n\t\t\t{\n\t\t\tret = SSL_TLSEXT_ERR_ALERT_FATAL;\n\t\t\tal = SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t}\n\t\tif (s->s3->client_opaque_prf_input == NULL ||\n\t\t s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)\n\t\t\t{\n\t\t\tret = SSL_TLSEXT_ERR_ALERT_FATAL;\n\t\t\tal = SSL_AD_ILLEGAL_PARAMETER;\n\t\t\t}\n\t\t}\n#endif\n\tif ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)\n\t\t\t&& s->ctx && s->ctx->tlsext_status_cb)\n\t\t{\n\t\tint r;\n\t\tif (s->tlsext_ocsp_resp)\n\t\t\t{\n\t\t\tOPENSSL_free(s->tlsext_ocsp_resp);\n\t\t\ts->tlsext_ocsp_resp = NULL;\n\t\t\t}\n\t\ts->tlsext_ocsp_resplen = -1;\n\t\tr = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);\n\t\tif (r == 0)\n\t\t\t{\n\t\t\tal = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;\n\t\t\tret = SSL_TLSEXT_ERR_ALERT_FATAL;\n\t\t\t}\n\t\tif (r < 0)\n\t\t\t{\n\t\t\tal = SSL_AD_INTERNAL_ERROR;\n\t\t\tret = SSL_TLSEXT_ERR_ALERT_FATAL;\n\t\t\t}\n\t\t}\n\tswitch (ret)\n\t\t{\n\t\tcase SSL_TLSEXT_ERR_ALERT_FATAL:\n\t\t\tssl3_send_alert(s,SSL3_AL_FATAL,al);\n\t\t\treturn -1;\n\t\tcase SSL_TLSEXT_ERR_ALERT_WARNING:\n\t\t\tssl3_send_alert(s,SSL3_AL_WARNING,al);\n\t\t\treturn 1;\n\t\tcase SSL_TLSEXT_ERR_NOACK:\n\t\t\ts->servername_done=0;\n\t\t\tdefault:\n\t\treturn 1;\n\t\t}\n\t}', 'int ssl3_send_alert(SSL *s, int level, int desc)\n\t{\n\tdesc=s->method->ssl3_enc->alert_value(desc);\n\tif (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)\n\t\tdesc = SSL_AD_HANDSHAKE_FAILURE;\n\tif (desc < 0) return -1;\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\treturn s->method->ssl_dispatch_alert(s);\n\treturn -1;\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,792
0
https://github.com/libav/libav/blob/f4f3300c09bb13eb7922e60888b55e3e0fb325e7/libavcodec/vp6.c/#L227
static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[], const uint8_t *map, unsigned size, VLC *vlc) { Node nodes[2*VP6_MAX_HUFF_SIZE], *tmp = &nodes[size]; int a, b, i; tmp[0].count = 256; for (i=0; i<size-1; i++) { a = tmp[i].count * coeff_model[i] >> 8; b = tmp[i].count * (255 - coeff_model[i]) >> 8; nodes[map[2*i ]].count = a + !a; nodes[map[2*i+1]].count = b + !b; } free_vlc(vlc); return ff_huff_build_tree(s->avctx, vlc, size, nodes, vp6_huff_cmp, FF_HUFFMAN_FLAG_HNODE_FIRST); }
['static void vp6_parse_coeff_models(VP56Context *s)\n{\n VP56RangeCoder *c = &s->c;\n VP56Model *model = s->modelp;\n int def_prob[11];\n int node, cg, ctx, pos;\n int ct;\n int pt;\n memset(def_prob, 0x80, sizeof(def_prob));\n for (pt=0; pt<2; pt++)\n for (node=0; node<11; node++)\n if (vp56_rac_get_prob(c, vp6_dccv_pct[pt][node])) {\n def_prob[node] = vp56_rac_gets_nn(c, 7);\n model->coeff_dccv[pt][node] = def_prob[node];\n } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {\n model->coeff_dccv[pt][node] = def_prob[node];\n }\n if (vp56_rac_get(c)) {\n for (pos=1; pos<64; pos++)\n if (vp56_rac_get_prob(c, vp6_coeff_reorder_pct[pos]))\n model->coeff_reorder[pos] = vp56_rac_gets(c, 4);\n vp6_coeff_order_table_init(s);\n }\n for (cg=0; cg<2; cg++)\n for (node=0; node<14; node++)\n if (vp56_rac_get_prob(c, vp6_runv_pct[cg][node]))\n model->coeff_runv[cg][node] = vp56_rac_gets_nn(c, 7);\n for (ct=0; ct<3; ct++)\n for (pt=0; pt<2; pt++)\n for (cg=0; cg<6; cg++)\n for (node=0; node<11; node++)\n if (vp56_rac_get_prob(c, vp6_ract_pct[ct][pt][cg][node])) {\n def_prob[node] = vp56_rac_gets_nn(c, 7);\n model->coeff_ract[pt][ct][cg][node] = def_prob[node];\n } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {\n model->coeff_ract[pt][ct][cg][node] = def_prob[node];\n }\n if (s->use_huffman) {\n for (pt=0; pt<2; pt++) {\n vp6_build_huff_tree(s, model->coeff_dccv[pt],\n vp6_huff_coeff_map, 12, &s->dccv_vlc[pt]);\n vp6_build_huff_tree(s, model->coeff_runv[pt],\n vp6_huff_run_map, 9, &s->runv_vlc[pt]);\n for (ct=0; ct<3; ct++)\n for (cg = 0; cg < 6; cg++)\n vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg],\n vp6_huff_coeff_map, 12,\n &s->ract_vlc[pt][ct][cg]);\n }\n memset(s->nb_null, 0, sizeof(s->nb_null));\n } else {\n for (pt=0; pt<2; pt++)\n for (ctx=0; ctx<3; ctx++)\n for (node=0; node<5; node++)\n model->coeff_dcct[pt][ctx][node] = av_clip(((model->coeff_dccv[pt][node] * vp6_dccv_lc[ctx][node][0] + 128) >> 8) + vp6_dccv_lc[ctx][node][1], 1, 255);\n }\n}', 'static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[],\n const uint8_t *map, unsigned size, VLC *vlc)\n{\n Node nodes[2*VP6_MAX_HUFF_SIZE], *tmp = &nodes[size];\n int a, b, i;\n tmp[0].count = 256;\n for (i=0; i<size-1; i++) {\n a = tmp[i].count * coeff_model[i] >> 8;\n b = tmp[i].count * (255 - coeff_model[i]) >> 8;\n nodes[map[2*i ]].count = a + !a;\n nodes[map[2*i+1]].count = b + !b;\n }\n free_vlc(vlc);\n return ff_huff_build_tree(s->avctx, vlc, size, nodes, vp6_huff_cmp,\n FF_HUFFMAN_FLAG_HNODE_FIRST);\n}']
35,793
0
https://github.com/libav/libav/blob/67ce33162aa93bee1a5f9e8d6f00060329fa67da/libavcodec/pcm.c/#L124
static av_cold int pcm_encode_init(AVCodecContext *avctx) { avctx->frame_size = 1; switch(avctx->codec->id) { case CODEC_ID_PCM_ALAW: build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5); break; case CODEC_ID_PCM_MULAW: build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff); break; default: break; } avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id); avctx->block_align = avctx->channels * avctx->bits_per_coded_sample/8; avctx->coded_frame= avcodec_alloc_frame(); avctx->coded_frame->key_frame= 1; return 0; }
['static av_cold int pcm_encode_init(AVCodecContext *avctx)\n{\n avctx->frame_size = 1;\n switch(avctx->codec->id) {\n case CODEC_ID_PCM_ALAW:\n build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);\n break;\n case CODEC_ID_PCM_MULAW:\n build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);\n break;\n default:\n break;\n }\n avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);\n avctx->block_align = avctx->channels * avctx->bits_per_coded_sample/8;\n avctx->coded_frame= avcodec_alloc_frame();\n avctx->coded_frame->key_frame= 1;\n return 0;\n}', 'int av_get_bits_per_sample(enum CodecID codec_id){\n switch(codec_id){\n case CODEC_ID_ADPCM_SBPRO_2:\n return 2;\n case CODEC_ID_ADPCM_SBPRO_3:\n return 3;\n case CODEC_ID_ADPCM_SBPRO_4:\n case CODEC_ID_ADPCM_CT:\n return 4;\n case CODEC_ID_PCM_ALAW:\n case CODEC_ID_PCM_MULAW:\n case CODEC_ID_PCM_S8:\n case CODEC_ID_PCM_U8:\n case CODEC_ID_PCM_ZORK:\n return 8;\n case CODEC_ID_PCM_S16BE:\n case CODEC_ID_PCM_S16LE:\n case CODEC_ID_PCM_S16LE_PLANAR:\n case CODEC_ID_PCM_U16BE:\n case CODEC_ID_PCM_U16LE:\n return 16;\n case CODEC_ID_PCM_S24DAUD:\n case CODEC_ID_PCM_S24BE:\n case CODEC_ID_PCM_S24LE:\n case CODEC_ID_PCM_U24BE:\n case CODEC_ID_PCM_U24LE:\n return 24;\n case CODEC_ID_PCM_S32BE:\n case CODEC_ID_PCM_S32LE:\n case CODEC_ID_PCM_U32BE:\n case CODEC_ID_PCM_U32LE:\n case CODEC_ID_PCM_F32BE:\n case CODEC_ID_PCM_F32LE:\n return 32;\n case CODEC_ID_PCM_F64BE:\n case CODEC_ID_PCM_F64LE:\n return 64;\n default:\n return 0;\n }\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,794
0
https://github.com/openssl/openssl/blob/019bfef89964105cdf9256b6a6bc0aa7790bd020/crypto/x509/x509_vfy.c/#L169
int X509_verify_cert(X509_STORE_CTX *ctx) { X509 *x,*xtmp,*chain_ss=NULL; X509_NAME *xn; int bad_chain = 0; X509_VERIFY_PARAM *param = ctx->param; int depth,i,ok=0; int num; int (*cb)(int xok,X509_STORE_CTX *xctx); STACK_OF(X509) *sktmp=NULL; if (ctx->cert == NULL) { X509err(X509_F_X509_VERIFY_CERT,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY); return -1; } cb=ctx->verify_cb; if (ctx->chain == NULL) { if ( ((ctx->chain=sk_X509_new_null()) == NULL) || (!sk_X509_push(ctx->chain,ctx->cert))) { X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); goto end; } CRYPTO_add(&ctx->cert->references,1,CRYPTO_LOCK_X509); ctx->last_untrusted=1; } if (ctx->untrusted != NULL && (sktmp=sk_X509_dup(ctx->untrusted)) == NULL) { X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); goto end; } num=sk_X509_num(ctx->chain); x=sk_X509_value(ctx->chain,num-1); depth=param->depth; for (;;) { if (depth < num) break; xn=X509_get_issuer_name(x); if (ctx->check_issued(ctx, x,x)) break; if (ctx->untrusted != NULL) { xtmp=find_issuer(ctx, sktmp,x); if (xtmp != NULL) { if (!sk_X509_push(ctx->chain,xtmp)) { X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); goto end; } CRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509); sk_X509_delete_ptr(sktmp,xtmp); ctx->last_untrusted++; x=xtmp; num++; continue; } } break; } i=sk_X509_num(ctx->chain); x=sk_X509_value(ctx->chain,i-1); xn = X509_get_subject_name(x); if (ctx->check_issued(ctx, x, x)) { if (sk_X509_num(ctx->chain) == 1) { ok = ctx->get_issuer(&xtmp, ctx, x); if ((ok <= 0) || X509_cmp(x, xtmp)) { ctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT; ctx->current_cert=x; ctx->error_depth=i-1; if (ok == 1) X509_free(xtmp); bad_chain = 1; ok=cb(0,ctx); if (!ok) goto end; } else { X509_free(x); x = xtmp; sk_X509_set(ctx->chain, i - 1, x); ctx->last_untrusted=0; } } else { chain_ss=sk_X509_pop(ctx->chain); ctx->last_untrusted--; num--; x=sk_X509_value(ctx->chain,num-1); } } for (;;) { if (depth < num) break; xn=X509_get_issuer_name(x); if (ctx->check_issued(ctx,x,x)) break; ok = ctx->get_issuer(&xtmp, ctx, x); if (ok < 0) return ok; if (ok == 0) break; x = xtmp; if (!sk_X509_push(ctx->chain,x)) { X509_free(xtmp); X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); return 0; } num++; } xn=X509_get_issuer_name(x); if (!ctx->check_issued(ctx,x,x)) { if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) { if (ctx->last_untrusted >= num) ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY; else ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT; ctx->current_cert=x; } else { sk_X509_push(ctx->chain,chain_ss); num++; ctx->last_untrusted=num; ctx->current_cert=chain_ss; ctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; chain_ss=NULL; } ctx->error_depth=num-1; bad_chain = 1; ok=cb(0,ctx); if (!ok) goto end; } ok = check_chain_extensions(ctx); if (!ok) goto end; if (param->trust > 0) ok = check_trust(ctx); if (!ok) goto end; X509_get_pubkey_parameters(NULL,ctx->chain); ok = ctx->check_revocation(ctx); if(!ok) goto end; if (ctx->verify != NULL) ok=ctx->verify(ctx); else ok=internal_verify(ctx); if(!ok) goto end; if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK)) ok = ctx->check_policy(ctx); if(!ok) goto end; if (0) { end: X509_get_pubkey_parameters(NULL,ctx->chain); } if (sktmp != NULL) sk_X509_free(sktmp); if (chain_ss != NULL) X509_free(chain_ss); return ok; }
['int X509_verify_cert(X509_STORE_CTX *ctx)\n\t{\n\tX509 *x,*xtmp,*chain_ss=NULL;\n\tX509_NAME *xn;\n\tint bad_chain = 0;\n\tX509_VERIFY_PARAM *param = ctx->param;\n\tint depth,i,ok=0;\n\tint num;\n\tint (*cb)(int xok,X509_STORE_CTX *xctx);\n\tSTACK_OF(X509) *sktmp=NULL;\n\tif (ctx->cert == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_VERIFY_CERT,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);\n\t\treturn -1;\n\t\t}\n\tcb=ctx->verify_cb;\n\tif (ctx->chain == NULL)\n\t\t{\n\t\tif (\t((ctx->chain=sk_X509_new_null()) == NULL) ||\n\t\t\t(!sk_X509_push(ctx->chain,ctx->cert)))\n\t\t\t{\n\t\t\tX509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto end;\n\t\t\t}\n\t\tCRYPTO_add(&ctx->cert->references,1,CRYPTO_LOCK_X509);\n\t\tctx->last_untrusted=1;\n\t\t}\n\tif (ctx->untrusted != NULL\n\t && (sktmp=sk_X509_dup(ctx->untrusted)) == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);\n\t\tgoto end;\n\t\t}\n\tnum=sk_X509_num(ctx->chain);\n\tx=sk_X509_value(ctx->chain,num-1);\n\tdepth=param->depth;\n\tfor (;;)\n\t\t{\n\t\tif (depth < num) break;\n\t\txn=X509_get_issuer_name(x);\n\t\tif (ctx->check_issued(ctx, x,x)) break;\n\t\tif (ctx->untrusted != NULL)\n\t\t\t{\n\t\t\txtmp=find_issuer(ctx, sktmp,x);\n\t\t\tif (xtmp != NULL)\n\t\t\t\t{\n\t\t\t\tif (!sk_X509_push(ctx->chain,xtmp))\n\t\t\t\t\t{\n\t\t\t\t\tX509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tCRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509);\n\t\t\t\tsk_X509_delete_ptr(sktmp,xtmp);\n\t\t\t\tctx->last_untrusted++;\n\t\t\t\tx=xtmp;\n\t\t\t\tnum++;\n\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\tbreak;\n\t\t}\n\ti=sk_X509_num(ctx->chain);\n\tx=sk_X509_value(ctx->chain,i-1);\n\txn = X509_get_subject_name(x);\n\tif (ctx->check_issued(ctx, x, x))\n\t\t{\n\t\tif (sk_X509_num(ctx->chain) == 1)\n\t\t\t{\n\t\t\tok = ctx->get_issuer(&xtmp, ctx, x);\n\t\t\tif ((ok <= 0) || X509_cmp(x, xtmp))\n\t\t\t\t{\n\t\t\t\tctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;\n\t\t\t\tctx->current_cert=x;\n\t\t\t\tctx->error_depth=i-1;\n\t\t\t\tif (ok == 1) X509_free(xtmp);\n\t\t\t\tbad_chain = 1;\n\t\t\t\tok=cb(0,ctx);\n\t\t\t\tif (!ok) goto end;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tX509_free(x);\n\t\t\t\tx = xtmp;\n\t\t\t\tsk_X509_set(ctx->chain, i - 1, x);\n\t\t\t\tctx->last_untrusted=0;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tchain_ss=sk_X509_pop(ctx->chain);\n\t\t\tctx->last_untrusted--;\n\t\t\tnum--;\n\t\t\tx=sk_X509_value(ctx->chain,num-1);\n\t\t\t}\n\t\t}\n\tfor (;;)\n\t\t{\n\t\tif (depth < num) break;\n\t\txn=X509_get_issuer_name(x);\n\t\tif (ctx->check_issued(ctx,x,x)) break;\n\t\tok = ctx->get_issuer(&xtmp, ctx, x);\n\t\tif (ok < 0) return ok;\n\t\tif (ok == 0) break;\n\t\tx = xtmp;\n\t\tif (!sk_X509_push(ctx->chain,x))\n\t\t\t{\n\t\t\tX509_free(xtmp);\n\t\t\tX509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);\n\t\t\treturn 0;\n\t\t\t}\n\t\tnum++;\n\t\t}\n\txn=X509_get_issuer_name(x);\n\tif (!ctx->check_issued(ctx,x,x))\n\t\t{\n\t\tif ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss))\n\t\t\t{\n\t\t\tif (ctx->last_untrusted >= num)\n\t\t\t\tctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;\n\t\t\telse\n\t\t\t\tctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;\n\t\t\tctx->current_cert=x;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tsk_X509_push(ctx->chain,chain_ss);\n\t\t\tnum++;\n\t\t\tctx->last_untrusted=num;\n\t\t\tctx->current_cert=chain_ss;\n\t\t\tctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;\n\t\t\tchain_ss=NULL;\n\t\t\t}\n\t\tctx->error_depth=num-1;\n\t\tbad_chain = 1;\n\t\tok=cb(0,ctx);\n\t\tif (!ok) goto end;\n\t\t}\n\tok = check_chain_extensions(ctx);\n\tif (!ok) goto end;\n\tif (param->trust > 0) ok = check_trust(ctx);\n\tif (!ok) goto end;\n\tX509_get_pubkey_parameters(NULL,ctx->chain);\n\tok = ctx->check_revocation(ctx);\n\tif(!ok) goto end;\n\tif (ctx->verify != NULL)\n\t\tok=ctx->verify(ctx);\n\telse\n\t\tok=internal_verify(ctx);\n\tif(!ok) goto end;\n\tif (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))\n\t\tok = ctx->check_policy(ctx);\n\tif(!ok) goto end;\n\tif (0)\n\t\t{\nend:\n\t\tX509_get_pubkey_parameters(NULL,ctx->chain);\n\t\t}\n\tif (sktmp != NULL) sk_X509_free(sktmp);\n\tif (chain_ss != NULL) X509_free(chain_ss);\n\treturn ok;\n\t}', 'int sk_num(const STACK *st)\n{\n\tif(st == NULL) return -1;\n\treturn st->num;\n}', 'char *sk_value(const STACK *st, int i)\n{\n\tif(!st || (i < 0) || (i >= st->num)) return NULL;\n\treturn st->data[i];\n}', 'X509_NAME *X509_get_issuer_name(X509 *a)\n\t{\n\treturn(a->cert_info->issuer);\n\t}', 'int sk_push(STACK *st, char *data)\n\t{\n\treturn(sk_insert(st,data,st->num));\n\t}', 'int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,\n\t int line)\n\t{\n\tint ret = 0;\n\tif (add_lock_callback != NULL)\n\t\t{\n#ifdef LOCK_DEBUG\n\t\tint before= *pointer;\n#endif\n\t\tret=add_lock_callback(pointer,amount,type,file,line);\n#ifdef LOCK_DEBUG\n\t\tfprintf(stderr,"ladd:%08lx/%0xp:%2d+%2d->%2d %-18s %s:%d\\n",\n\t\t\tCRYPTO_thread_id(), CRYPTO_thread_idptr(),\n\t\t\tbefore,amount,ret,\n\t\t\tCRYPTO_get_lock_name(type),\n\t\t\tfile,line);\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tCRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,file,line);\n\t\tret= *pointer+amount;\n#ifdef LOCK_DEBUG\n\t\tfprintf(stderr,"ladd:%08lx/%0xp:%2d+%2d->%2d %-18s %s:%d\\n",\n\t\t\tCRYPTO_thread_id(), CRYPTO_thread_idptr(),\n\t\t\t*pointer,amount,ret,\n\t\t\tCRYPTO_get_lock_name(type),\n\t\t\tfile,line);\n#endif\n\t\t*pointer=ret;\n\t\tCRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,file,line);\n\t\t}\n\treturn(ret);\n\t}', '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/%08p:(%s)%s %-18s %s:%d\\n",\n\t\t\tCRYPTO_thread_id(), CRYPTO_thread_idptr(), 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\tif (dynlock_lock_callback != NULL)\n\t\t\t{\n\t\t\tstruct CRYPTO_dynlock_value *pointer\n\t\t\t\t= CRYPTO_get_dynlock_value(type);\n\t\t\tOPENSSL_assert(pointer != NULL);\n\t\t\tdynlock_lock_callback(mode, pointer, file, line);\n\t\t\tCRYPTO_destroy_dynlockid(type);\n\t\t\t}\n\t\t}\n\telse\n\t\tif (locking_callback != NULL)\n\t\t\tlocking_callback(mode,type,file,line);\n\t}', 'char *sk_delete_ptr(STACK *st, char *p)\n\t{\n\tint i;\n\tfor (i=0; i<st->num; i++)\n\t\tif (st->data[i] == p)\n\t\t\treturn(sk_delete(st,i));\n\treturn(NULL);\n\t}']
35,795
0
https://github.com/libav/libav/blob/06ed4873e6e6aed8ec7cc24285d610ef4060880e/libavformat/matroskadec.c/#L834
static int ebml_parse_elem(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, void *data) { static const uint64_t max_lengths[EBML_TYPE_COUNT] = { [EBML_UINT] = 8, [EBML_FLOAT] = 8, [EBML_STR] = 0x1000000, [EBML_UTF8] = 0x1000000, [EBML_BIN] = 0x10000000, }; AVIOContext *pb = matroska->ctx->pb; uint32_t id = syntax->id; uint64_t length; int res; data = (char *)data + syntax->data_offset; if (syntax->list_elem_size) { EbmlList *list = data; list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size); data = (char*)list->elem + list->nb_elem*syntax->list_elem_size; memset(data, 0, syntax->list_elem_size); list->nb_elem++; } if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) { matroska->current_id = 0; if ((res = ebml_read_length(matroska, pb, &length)) < 0) return res; if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) { av_log(matroska->ctx, AV_LOG_ERROR, "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for syntax element %i\n", length, max_lengths[syntax->type], syntax->type); return AVERROR_INVALIDDATA; } } switch (syntax->type) { case EBML_UINT: res = ebml_read_uint (pb, length, data); break; case EBML_FLOAT: res = ebml_read_float (pb, length, data); break; case EBML_STR: case EBML_UTF8: res = ebml_read_ascii (pb, length, data); break; case EBML_BIN: res = ebml_read_binary(pb, length, data); break; case EBML_NEST: if ((res=ebml_read_master(matroska, length)) < 0) return res; if (id == MATROSKA_ID_SEGMENT) matroska->segment_start = url_ftell(matroska->ctx->pb); return ebml_parse_nest(matroska, syntax->def.n, data); case EBML_PASS: return ebml_parse_id(matroska, syntax->def.n, id, data); case EBML_STOP: return 1; default: return avio_seek(pb,length,SEEK_CUR)<0 ? AVERROR(EIO) : 0; } if (res == AVERROR_INVALIDDATA) av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n"); else if (res == AVERROR(EIO)) av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n"); return res; }
['static int ebml_parse_elem(MatroskaDemuxContext *matroska,\n EbmlSyntax *syntax, void *data)\n{\n static const uint64_t max_lengths[EBML_TYPE_COUNT] = {\n [EBML_UINT] = 8,\n [EBML_FLOAT] = 8,\n [EBML_STR] = 0x1000000,\n [EBML_UTF8] = 0x1000000,\n [EBML_BIN] = 0x10000000,\n };\n AVIOContext *pb = matroska->ctx->pb;\n uint32_t id = syntax->id;\n uint64_t length;\n int res;\n data = (char *)data + syntax->data_offset;\n if (syntax->list_elem_size) {\n EbmlList *list = data;\n list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);\n data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;\n memset(data, 0, syntax->list_elem_size);\n list->nb_elem++;\n }\n if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) {\n matroska->current_id = 0;\n if ((res = ebml_read_length(matroska, pb, &length)) < 0)\n return res;\n if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) {\n av_log(matroska->ctx, AV_LOG_ERROR,\n "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for syntax element %i\\n",\n length, max_lengths[syntax->type], syntax->type);\n return AVERROR_INVALIDDATA;\n }\n }\n switch (syntax->type) {\n case EBML_UINT: res = ebml_read_uint (pb, length, data); break;\n case EBML_FLOAT: res = ebml_read_float (pb, length, data); break;\n case EBML_STR:\n case EBML_UTF8: res = ebml_read_ascii (pb, length, data); break;\n case EBML_BIN: res = ebml_read_binary(pb, length, data); break;\n case EBML_NEST: if ((res=ebml_read_master(matroska, length)) < 0)\n return res;\n if (id == MATROSKA_ID_SEGMENT)\n matroska->segment_start = url_ftell(matroska->ctx->pb);\n return ebml_parse_nest(matroska, syntax->def.n, data);\n case EBML_PASS: return ebml_parse_id(matroska, syntax->def.n, id, data);\n case EBML_STOP: return 1;\n default: return avio_seek(pb,length,SEEK_CUR)<0 ? AVERROR(EIO) : 0;\n }\n if (res == AVERROR_INVALIDDATA)\n av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\\n");\n else if (res == AVERROR(EIO))\n av_log(matroska->ctx, AV_LOG_ERROR, "Read error\\n");\n return res;\n}']
35,796
0
https://gitlab.com/libtiff/libtiff/blob/400ae6f2b13bae921d835278a36c0d2435bcb519/tools/tiff2pdf.c/#L2434
tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ tsize_t written=0; unsigned char* buffer=NULL; unsigned char* samplebuffer=NULL; tsize_t bufferoffset=0; tsize_t samplebufferoffset=0; tsize_t read=0; tstrip_t i=0; tstrip_t j=0; tstrip_t stripcount=0; tsize_t stripsize=0; tsize_t sepstripcount=0; tsize_t sepstripsize=0; #ifdef OJPEG_SUPPORT toff_t inputoffset=0; uint16 h_samp=1; uint16 v_samp=1; uint16 ri=1; uint32 rows=0; #endif #ifdef JPEG_SUPPORT unsigned char* jpt; float* xfloatp; uint64* sbc; unsigned char* stripbuffer; tsize_t striplength=0; uint32 max_striplength=0; #endif if (t2p->t2p_error != T2P_ERR_OK) return(0); if(t2p->pdf_transcode == T2P_TRANSCODE_RAW){ #ifdef CCITT_SUPPORT if(t2p->pdf_compression == T2P_COMPRESS_G4){ buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if (buffer == NULL) { TIFFError(TIFF2PDF_MODULE, "Can't allocate %lu bytes of memory for " "t2p_readwrite_pdf_image, %s", (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } memset(buffer, 0, t2p->tiff_datasize); if (TIFFReadRawStrip(input, 0, (tdata_t) buffer, t2p->tiff_datasize) < 0) { TIFFError(TIFF2PDF_MODULE, "TIFFReadRawStrip() failed"); _TIFFfree(buffer); return(0); } if (t2p->tiff_fillorder==FILLORDER_LSB2MSB){ TIFFReverseBits(buffer, t2p->tiff_datasize); } t2pWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); _TIFFfree(buffer); return(t2p->tiff_datasize); } #endif #ifdef ZIP_SUPPORT if (t2p->pdf_compression == T2P_COMPRESS_ZIP) { buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer == NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } memset(buffer, 0, t2p->tiff_datasize); if (TIFFReadRawStrip(input, 0, (tdata_t) buffer, t2p->tiff_datasize) < 0) { TIFFError(TIFF2PDF_MODULE, "TIFFReadRawStrip() failed"); _TIFFfree(buffer); return(0); } if (t2p->tiff_fillorder==FILLORDER_LSB2MSB) { TIFFReverseBits(buffer, t2p->tiff_datasize); } t2pWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); _TIFFfree(buffer); return(t2p->tiff_datasize); } #endif #ifdef OJPEG_SUPPORT if(t2p->tiff_compression == COMPRESSION_OJPEG) { if(t2p->tiff_dataoffset != 0) { buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer == NULL) { TIFFError(TIFF2PDF_MODULE, "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } memset(buffer, 0, t2p->tiff_datasize); if(t2p->pdf_ojpegiflength==0){ inputoffset=t2pSeekFile(input, 0, SEEK_CUR); t2pSeekFile(input, t2p->tiff_dataoffset, SEEK_SET); t2pReadFile(input, (tdata_t) buffer, t2p->tiff_datasize); t2pSeekFile(input, inputoffset, SEEK_SET); t2pWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); _TIFFfree(buffer); return(t2p->tiff_datasize); } else { inputoffset=t2pSeekFile(input, 0, SEEK_CUR); t2pSeekFile(input, t2p->tiff_dataoffset, SEEK_SET); bufferoffset = t2pReadFile(input, (tdata_t) buffer, t2p->pdf_ojpegiflength); t2p->pdf_ojpegiflength = 0; t2pSeekFile(input, inputoffset, SEEK_SET); TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &h_samp, &v_samp); buffer[bufferoffset++]= 0xff; buffer[bufferoffset++]= 0xdd; buffer[bufferoffset++]= 0x00; buffer[bufferoffset++]= 0x04; h_samp*=8; v_samp*=8; ri=(t2p->tiff_width+h_samp-1) / h_samp; TIFFGetField(input, TIFFTAG_ROWSPERSTRIP, &rows); ri*=(rows+v_samp-1)/v_samp; buffer[bufferoffset++]= (ri>>8) & 0xff; buffer[bufferoffset++]= ri & 0xff; stripcount=TIFFNumberOfStrips(input); for(i=0;i<stripcount;i++){ if(i != 0 ){ buffer[bufferoffset++]=0xff; buffer[bufferoffset++]=(0xd0 | ((i-1)%8)); } bufferoffset+=TIFFReadRawStrip(input, i, (tdata_t) &(((unsigned char*)buffer)[bufferoffset]), -1); } t2pWriteFile(output, (tdata_t) buffer, bufferoffset); _TIFFfree(buffer); return(bufferoffset); } } else { if(! t2p->pdf_ojpegdata){ TIFFError(TIFF2PDF_MODULE, "No support for OJPEG image %s with bad tables", TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } memset(buffer, 0, t2p->tiff_datasize); _TIFFmemcpy(buffer, t2p->pdf_ojpegdata, t2p->pdf_ojpegdatalength); bufferoffset=t2p->pdf_ojpegdatalength; stripcount=TIFFNumberOfStrips(input); for(i=0;i<stripcount;i++){ tsize_t retTIFFReadRawStrip; if(i != 0){ buffer[bufferoffset++]=0xff; buffer[bufferoffset++]=(0xd0 | ((i-1)%8)); } retTIFFReadRawStrip = TIFFReadRawStrip(input, i, (tdata_t) &(((unsigned char*)buffer)[bufferoffset]), -1); if (retTIFFReadRawStrip < 0) { TIFFError(TIFF2PDF_MODULE, "TIFFReadRawStrip()"); _TIFFfree(buffer); t2p->t2p_error = T2P_ERR_ERROR; return(0); } bufferoffset += retTIFFReadRawStrip; } if( ! ( (buffer[bufferoffset-1]==0xd9) && (buffer[bufferoffset-2]==0xff) ) ){ buffer[bufferoffset++]=0xff; buffer[bufferoffset++]=0xd9; } t2pWriteFile(output, (tdata_t) buffer, bufferoffset); _TIFFfree(buffer); return(bufferoffset); #if 0 TIFFError(TIFF2PDF_MODULE, "No support for OJPEG image %s with no JPEG File Interchange offset", TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); #endif } } #endif #ifdef JPEG_SUPPORT if(t2p->tiff_compression == COMPRESSION_JPEG) { uint32 count = 0; buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } memset(buffer, 0, t2p->tiff_datasize); if (TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) { if(count > 4) { _TIFFmemcpy(buffer, jpt, count); bufferoffset += count - 2; } } stripcount=TIFFNumberOfStrips(input); TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc); for(i=0;i<stripcount;i++){ if(sbc[i]>max_striplength) max_striplength=sbc[i]; } stripbuffer = (unsigned char*) _TIFFmalloc(max_striplength); if(stripbuffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", max_striplength, TIFFFileName(input)); _TIFFfree(buffer); t2p->t2p_error = T2P_ERR_ERROR; return(0); } memset(stripbuffer, 0, max_striplength); for(i=0;i<stripcount;i++){ striplength=TIFFReadRawStrip(input, i, (tdata_t) stripbuffer, -1); if (striplength < 0) { TIFFError(TIFF2PDF_MODULE, "TIFFReadRawStrip() failed"); _TIFFfree(samplebuffer); _TIFFfree(buffer); t2p->t2p_error = T2P_ERR_ERROR; return(0); } if(!t2p_process_jpeg_strip( stripbuffer, &striplength, buffer, t2p->tiff_datasize, &bufferoffset, i, t2p->tiff_length)){ TIFFError(TIFF2PDF_MODULE, "Can't process JPEG data in input file %s", TIFFFileName(input)); _TIFFfree(samplebuffer); _TIFFfree(buffer); t2p->t2p_error = T2P_ERR_ERROR; return(0); } } buffer[bufferoffset++]=0xff; buffer[bufferoffset++]=0xd9; t2pWriteFile(output, (tdata_t) buffer, bufferoffset); _TIFFfree(stripbuffer); _TIFFfree(buffer); return(bufferoffset); } #endif (void)0; } if(t2p->pdf_sample==T2P_SAMPLE_NOTHING){ buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } memset(buffer, 0, t2p->tiff_datasize); stripsize=TIFFStripSize(input); stripcount=TIFFNumberOfStrips(input); for(i=0;i<stripcount;i++){ read = TIFFReadEncodedStrip(input, i, (tdata_t) &buffer[bufferoffset], TIFFmin(stripsize, t2p->tiff_datasize - bufferoffset)); if(read==-1){ TIFFError(TIFF2PDF_MODULE, "Error on decoding strip %u of %s", i, TIFFFileName(input)); _TIFFfree(buffer); t2p->t2p_error=T2P_ERR_ERROR; return(0); } bufferoffset+=read; } } else { if(t2p->pdf_sample & T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG){ sepstripsize=TIFFStripSize(input); sepstripcount=TIFFNumberOfStrips(input); stripsize=sepstripsize*t2p->tiff_samplesperpixel; stripcount=sepstripcount/t2p->tiff_samplesperpixel; buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } memset(buffer, 0, t2p->tiff_datasize); samplebuffer = (unsigned char*) _TIFFmalloc(stripsize); if(samplebuffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; _TIFFfree(buffer); return(0); } for(i=0;i<stripcount;i++){ samplebufferoffset=0; for(j=0;j<t2p->tiff_samplesperpixel;j++){ read = TIFFReadEncodedStrip(input, i + j*stripcount, (tdata_t) &(samplebuffer[samplebufferoffset]), TIFFmin(sepstripsize, stripsize - samplebufferoffset)); if(read==-1){ TIFFError(TIFF2PDF_MODULE, "Error on decoding strip %u of %s", i + j*stripcount, TIFFFileName(input)); _TIFFfree(buffer); t2p->t2p_error=T2P_ERR_ERROR; return(0); } samplebufferoffset+=read; } t2p_sample_planar_separate_to_contig( t2p, &(buffer[bufferoffset]), samplebuffer, samplebufferoffset); bufferoffset+=samplebufferoffset; } _TIFFfree(samplebuffer); goto dataready; } buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } memset(buffer, 0, t2p->tiff_datasize); stripsize=TIFFStripSize(input); stripcount=TIFFNumberOfStrips(input); for(i=0;i<stripcount;i++){ read = TIFFReadEncodedStrip(input, i, (tdata_t) &buffer[bufferoffset], TIFFmin(stripsize, t2p->tiff_datasize - bufferoffset)); if(read==-1){ TIFFError(TIFF2PDF_MODULE, "Error on decoding strip %u of %s", i, TIFFFileName(input)); _TIFFfree(samplebuffer); _TIFFfree(buffer); t2p->t2p_error=T2P_ERR_ERROR; return(0); } bufferoffset+=read; } if(t2p->pdf_sample & T2P_SAMPLE_REALIZE_PALETTE){ samplebuffer=(unsigned char*)_TIFFrealloc( (tdata_t) buffer, t2p->tiff_datasize * t2p->tiff_samplesperpixel); if(samplebuffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; _TIFFfree(buffer); return(0); } else { buffer=samplebuffer; t2p->tiff_datasize *= t2p->tiff_samplesperpixel; } t2p_sample_realize_palette(t2p, buffer); } if(t2p->pdf_sample & T2P_SAMPLE_RGBA_TO_RGB){ t2p->tiff_datasize=t2p_sample_rgba_to_rgb( (tdata_t)buffer, t2p->tiff_width*t2p->tiff_length); } if(t2p->pdf_sample & T2P_SAMPLE_RGBAA_TO_RGB){ t2p->tiff_datasize=t2p_sample_rgbaa_to_rgb( (tdata_t)buffer, t2p->tiff_width*t2p->tiff_length); } if(t2p->pdf_sample & T2P_SAMPLE_YCBCR_TO_RGB){ samplebuffer=(unsigned char*)_TIFFrealloc( (tdata_t)buffer, t2p->tiff_width*t2p->tiff_length*4); if(samplebuffer==NULL){ TIFFError(TIFF2PDF_MODULE, "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; _TIFFfree(buffer); return(0); } else { buffer=samplebuffer; } if(!TIFFReadRGBAImageOriented( input, t2p->tiff_width, t2p->tiff_length, (uint32*)buffer, ORIENTATION_TOPLEFT, 0)){ TIFFError(TIFF2PDF_MODULE, "Can't use TIFFReadRGBAImageOriented to extract RGB image from %s", TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } t2p->tiff_datasize=t2p_sample_abgr_to_rgb( (tdata_t) buffer, t2p->tiff_width*t2p->tiff_length); } if(t2p->pdf_sample & T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED){ t2p->tiff_datasize=t2p_sample_lab_signed_to_unsigned( (tdata_t)buffer, t2p->tiff_width*t2p->tiff_length); } } dataready: t2p_disable(output); TIFFSetField(output, TIFFTAG_PHOTOMETRIC, t2p->tiff_photometric); TIFFSetField(output, TIFFTAG_BITSPERSAMPLE, t2p->tiff_bitspersample); TIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, t2p->tiff_samplesperpixel); TIFFSetField(output, TIFFTAG_IMAGEWIDTH, t2p->tiff_width); TIFFSetField(output, TIFFTAG_IMAGELENGTH, t2p->tiff_length); TIFFSetField(output, TIFFTAG_ROWSPERSTRIP, t2p->tiff_length); TIFFSetField(output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(output, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); switch(t2p->pdf_compression){ case T2P_COMPRESS_NONE: TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_NONE); break; #ifdef CCITT_SUPPORT case T2P_COMPRESS_G4: TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); break; #endif #ifdef JPEG_SUPPORT case T2P_COMPRESS_JPEG: if(t2p->tiff_photometric==PHOTOMETRIC_YCBCR) { uint16 hor = 0, ver = 0; if (TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &hor, &ver) !=0 ) { if(hor != 0 && ver != 0){ TIFFSetField(output, TIFFTAG_YCBCRSUBSAMPLING, hor, ver); } } if(TIFFGetField(input, TIFFTAG_REFERENCEBLACKWHITE, &xfloatp)!=0){ TIFFSetField(output, TIFFTAG_REFERENCEBLACKWHITE, xfloatp); } } if(TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_JPEG)==0){ TIFFError(TIFF2PDF_MODULE, "Unable to use JPEG compression for input %s and output %s", TIFFFileName(input), TIFFFileName(output)); _TIFFfree(buffer); t2p->t2p_error = T2P_ERR_ERROR; return(0); } TIFFSetField(output, TIFFTAG_JPEGTABLESMODE, 0); if(t2p->pdf_colorspace & (T2P_CS_RGB | T2P_CS_LAB)){ TIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR); if(t2p->tiff_photometric != PHOTOMETRIC_YCBCR){ TIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB); } else { TIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RAW); } } if(t2p->pdf_colorspace & T2P_CS_GRAY){ (void)0; } if(t2p->pdf_colorspace & T2P_CS_CMYK){ (void)0; } if(t2p->pdf_defaultcompressionquality != 0){ TIFFSetField(output, TIFFTAG_JPEGQUALITY, t2p->pdf_defaultcompressionquality); } break; #endif #ifdef ZIP_SUPPORT case T2P_COMPRESS_ZIP: TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); if(t2p->pdf_defaultcompressionquality%100 != 0){ TIFFSetField(output, TIFFTAG_PREDICTOR, t2p->pdf_defaultcompressionquality % 100); } if(t2p->pdf_defaultcompressionquality/100 != 0){ TIFFSetField(output, TIFFTAG_ZIPQUALITY, (t2p->pdf_defaultcompressionquality / 100)); } break; #endif default: break; } t2p_enable(output); t2p->outputwritten = 0; #ifdef JPEG_SUPPORT if(t2p->pdf_compression == T2P_COMPRESS_JPEG && t2p->tiff_photometric == PHOTOMETRIC_YCBCR){ bufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0, buffer, stripsize * stripcount); } else #endif { bufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0, buffer, t2p->tiff_datasize); } if (buffer != NULL) { _TIFFfree(buffer); buffer=NULL; } if (bufferoffset == (tsize_t)-1) { TIFFError(TIFF2PDF_MODULE, "Error writing encoded strip to output PDF %s", TIFFFileName(output)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } written = t2p->outputwritten; 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(TIFFSafeMultiply(tmsize_t,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\twritten += t2p_write_pdf_transfer_stream(t2p, output, i);\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\twritten += t2p_write_pdf_xobject_palettecs_stream(t2p, output);\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\twritten += t2p_write_pdf_xobject_icccs_stream(t2p, output);\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\tif (t2p->tiff_maxdatasize && (t2p->tiff_datasize > t2p->tiff_maxdatasize)) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Allocation of " TIFF_UINT64_FORMAT " bytes is forbidden. Limit is " TIFF_UINT64_FORMAT ". Use -m option to change limit",\n\t\t\t\t\t(uint64)t2p->tiff_datasize, (uint64)t2p->tiff_maxdatasize);\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn (0);\n\t\t\t}\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}', 'tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){\n\ttsize_t written=0;\n\tunsigned char* buffer=NULL;\n\tunsigned char* samplebuffer=NULL;\n\ttsize_t bufferoffset=0;\n\ttsize_t samplebufferoffset=0;\n\ttsize_t read=0;\n\ttstrip_t i=0;\n\ttstrip_t j=0;\n\ttstrip_t stripcount=0;\n\ttsize_t stripsize=0;\n\ttsize_t sepstripcount=0;\n\ttsize_t sepstripsize=0;\n#ifdef OJPEG_SUPPORT\n\ttoff_t inputoffset=0;\n\tuint16 h_samp=1;\n\tuint16 v_samp=1;\n\tuint16 ri=1;\n\tuint32 rows=0;\n#endif\n#ifdef JPEG_SUPPORT\n\tunsigned char* jpt;\n\tfloat* xfloatp;\n\tuint64* sbc;\n\tunsigned char* stripbuffer;\n\ttsize_t striplength=0;\n\tuint32 max_striplength=0;\n#endif\n\tif (t2p->t2p_error != T2P_ERR_OK)\n\t\treturn(0);\n\tif(t2p->pdf_transcode == T2P_TRANSCODE_RAW){\n#ifdef CCITT_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_G4){\n\t\t\tbuffer = (unsigned char*)\n\t\t\t\t_TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif (buffer == NULL) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory for "\n\t\t\t\t\t"t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tmemset(buffer, 0, t2p->tiff_datasize);\n\t\t\tif (TIFFReadRawStrip(input, 0, (tdata_t) buffer,\n\t\t\t\t\t t2p->tiff_datasize) < 0) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"TIFFReadRawStrip() failed");\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tif (t2p->tiff_fillorder==FILLORDER_LSB2MSB){\n\t\t\t\t\tTIFFReverseBits(buffer,\n\t\t\t\t\t\t\tt2p->tiff_datasize);\n\t\t\t}\n\t\t\tt2pWriteFile(output, (tdata_t) buffer,\n\t\t\t\t t2p->tiff_datasize);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(t2p->tiff_datasize);\n\t\t}\n#endif\n#ifdef ZIP_SUPPORT\n\t\tif (t2p->pdf_compression == T2P_COMPRESS_ZIP) {\n\t\t\tbuffer = (unsigned char*)\n\t\t\t\t_TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer == NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tmemset(buffer, 0, t2p->tiff_datasize);\n\t\t\tif (TIFFReadRawStrip(input, 0, (tdata_t) buffer,\n\t\t\t\t\t t2p->tiff_datasize) < 0) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"TIFFReadRawStrip() failed");\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tif (t2p->tiff_fillorder==FILLORDER_LSB2MSB) {\n\t\t\t\t\tTIFFReverseBits(buffer,\n\t\t\t\t\t\t\tt2p->tiff_datasize);\n\t\t\t}\n\t\t\tt2pWriteFile(output, (tdata_t) buffer,\n\t\t\t\t t2p->tiff_datasize);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(t2p->tiff_datasize);\n\t\t}\n#endif\n#ifdef OJPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_OJPEG) {\n\t\t\tif(t2p->tiff_dataoffset != 0) {\n\t\t\t\tbuffer = (unsigned char*)\n\t\t\t\t\t_TIFFmalloc(t2p->tiff_datasize);\n\t\t\t\tif(buffer == NULL) {\n\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\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(0);\n\t\t\t\t}\n\t\t\t\tmemset(buffer, 0, t2p->tiff_datasize);\n\t\t\t\tif(t2p->pdf_ojpegiflength==0){\n\t\t\t\t\tinputoffset=t2pSeekFile(input, 0,\n\t\t\t\t\t\t\t\t SEEK_CUR);\n\t\t\t\t\tt2pSeekFile(input,\n\t\t\t\t\t\t t2p->tiff_dataoffset,\n\t\t\t\t\t\t SEEK_SET);\n\t\t\t\t\tt2pReadFile(input, (tdata_t) buffer,\n\t\t\t\t\t\t t2p->tiff_datasize);\n\t\t\t\t\tt2pSeekFile(input, inputoffset,\n\t\t\t\t\t\t SEEK_SET);\n\t\t\t\t\tt2pWriteFile(output, (tdata_t) buffer,\n\t\t\t\t\t\t t2p->tiff_datasize);\n\t\t\t\t\t_TIFFfree(buffer);\n\t\t\t\t\treturn(t2p->tiff_datasize);\n\t\t\t\t} else {\n\t\t\t\t\tinputoffset=t2pSeekFile(input, 0,\n\t\t\t\t\t\t\t\t SEEK_CUR);\n\t\t\t\t\tt2pSeekFile(input,\n\t\t\t\t\t\t t2p->tiff_dataoffset,\n\t\t\t\t\t\t SEEK_SET);\n\t\t\t\t\tbufferoffset = t2pReadFile(input,\n\t\t\t\t\t\t(tdata_t) buffer,\n\t\t\t\t\t\tt2p->pdf_ojpegiflength);\n\t\t\t\t\tt2p->pdf_ojpegiflength = 0;\n\t\t\t\t\tt2pSeekFile(input, inputoffset,\n\t\t\t\t\t\t SEEK_SET);\n\t\t\t\t\tTIFFGetField(input,\n\t\t\t\t\t\t TIFFTAG_YCBCRSUBSAMPLING,\n\t\t\t\t\t\t &h_samp, &v_samp);\n\t\t\t\t\tbuffer[bufferoffset++]= 0xff;\n\t\t\t\t\tbuffer[bufferoffset++]= 0xdd;\n\t\t\t\t\tbuffer[bufferoffset++]= 0x00;\n\t\t\t\t\tbuffer[bufferoffset++]= 0x04;\n\t\t\t\t\th_samp*=8;\n\t\t\t\t\tv_samp*=8;\n\t\t\t\t\tri=(t2p->tiff_width+h_samp-1) / h_samp;\n\t\t\t\t\tTIFFGetField(input,\n\t\t\t\t\t\t TIFFTAG_ROWSPERSTRIP,\n\t\t\t\t\t\t &rows);\n\t\t\t\t\tri*=(rows+v_samp-1)/v_samp;\n\t\t\t\t\tbuffer[bufferoffset++]= (ri>>8) & 0xff;\n\t\t\t\t\tbuffer[bufferoffset++]= ri & 0xff;\n\t\t\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\t\t\tif(i != 0 ){\n\t\t\t\t\t\t\tbuffer[bufferoffset++]=0xff;\n\t\t\t\t\t\t\tbuffer[bufferoffset++]=(0xd0 | ((i-1)%8));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbufferoffset+=TIFFReadRawStrip(input,\n\t\t\t\t\t\t\ti,\n\t\t\t\t\t\t\t(tdata_t) &(((unsigned char*)buffer)[bufferoffset]),\n\t\t\t\t\t\t\t-1);\n\t\t\t\t\t}\n\t\t\t\t\tt2pWriteFile(output, (tdata_t) buffer, bufferoffset);\n\t\t\t\t\t_TIFFfree(buffer);\n\t\t\t\t\treturn(bufferoffset);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif(! t2p->pdf_ojpegdata){\n\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t\t"No support for OJPEG image %s with bad tables",\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(0);\n\t\t\t\t}\n\t\t\t\tbuffer = (unsigned char*)\n\t\t\t\t\t_TIFFmalloc(t2p->tiff_datasize);\n\t\t\t\tif(buffer==NULL){\n\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\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(0);\n\t\t\t\t}\n\t\t\t\tmemset(buffer, 0, t2p->tiff_datasize);\n\t\t\t\t_TIFFmemcpy(buffer, t2p->pdf_ojpegdata, t2p->pdf_ojpegdatalength);\n\t\t\t\tbufferoffset=t2p->pdf_ojpegdatalength;\n\t\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\t\ttsize_t retTIFFReadRawStrip;\n\t\t\t\t\tif(i != 0){\n\t\t\t\t\t\tbuffer[bufferoffset++]=0xff;\n\t\t\t\t\t\tbuffer[bufferoffset++]=(0xd0 | ((i-1)%8));\n\t\t\t\t\t}\n\t\t\t\t\tretTIFFReadRawStrip = TIFFReadRawStrip(input,\n\t\t\t\t\t\ti,\n\t\t\t\t\t\t(tdata_t) &(((unsigned char*)buffer)[bufferoffset]),\n\t\t\t\t\t\t-1);\n\t\t\t\t\tif (retTIFFReadRawStrip < 0) {\n\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE, "TIFFReadRawStrip()");\n\t\t\t\t\t\t_TIFFfree(buffer);\n\t\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\treturn(0);\n\t\t\t\t\t}\n\t\t\t\t\tbufferoffset += retTIFFReadRawStrip;\n\t\t\t\t}\n\t\t\t\tif( ! ( (buffer[bufferoffset-1]==0xd9) && (buffer[bufferoffset-2]==0xff) ) ){\n\t\t\t\t\t\tbuffer[bufferoffset++]=0xff;\n\t\t\t\t\t\tbuffer[bufferoffset++]=0xd9;\n\t\t\t\t}\n\t\t\t\tt2pWriteFile(output, (tdata_t) buffer, bufferoffset);\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\treturn(bufferoffset);\n#if 0\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"No support for OJPEG image %s with no JPEG File Interchange offset",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n#endif\n\t\t\t}\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\tbuffer = (unsigned char*)\n\t\t\t\t_TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tmemset(buffer, 0, t2p->tiff_datasize);\n\t\t\tif (TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) {\n\t\t\t\tif(count > 4) {\n\t\t\t\t\t_TIFFmemcpy(buffer, jpt, count);\n\t\t\t\t\tbufferoffset += count - 2;\n\t\t\t\t}\n\t\t\t}\n\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\tTIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tif(sbc[i]>max_striplength) max_striplength=sbc[i];\n\t\t\t}\n\t\t\tstripbuffer = (unsigned char*)\n\t\t\t\t_TIFFmalloc(max_striplength);\n\t\t\tif(stripbuffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %u bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\tmax_striplength,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tmemset(stripbuffer, 0, max_striplength);\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tstriplength=TIFFReadRawStrip(input, i, (tdata_t) stripbuffer, -1);\n\t\t\t\tif (striplength < 0) {\n\t\t\t\t\tTIFFError(TIFF2PDF_MODULE, "TIFFReadRawStrip() failed");\n\t\t\t\t\t_TIFFfree(samplebuffer);\n\t\t\t\t\t_TIFFfree(buffer);\n\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\treturn(0);\n\t\t\t\t}\n\t\t\t\tif(!t2p_process_jpeg_strip(\n\t\t\t\t\tstripbuffer,\n\t\t\t\t\t&striplength,\n\t\t\t\t\tbuffer,\n t2p->tiff_datasize,\n\t\t\t\t\t&bufferoffset,\n\t\t\t\t\ti,\n\t\t\t\t\tt2p->tiff_length)){\n\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t"Can\'t process JPEG data in input file %s",\n\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t_TIFFfree(samplebuffer);\n\t\t\t\t\t\t_TIFFfree(buffer);\n\t\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\treturn(0);\n\t\t\t\t}\n\t\t\t}\n\t\t\tbuffer[bufferoffset++]=0xff;\n\t\t\tbuffer[bufferoffset++]=0xd9;\n\t\t\tt2pWriteFile(output, (tdata_t) buffer, bufferoffset);\n\t\t\t_TIFFfree(stripbuffer);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(bufferoffset);\n\t\t}\n#endif\n\t\t(void)0;\n\t}\n\tif(t2p->pdf_sample==T2P_SAMPLE_NOTHING){\n\t\tbuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\tif(buffer==NULL){\n\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\tTIFFFileName(input));\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn(0);\n\t\t}\n\t\tmemset(buffer, 0, t2p->tiff_datasize);\n\t\tstripsize=TIFFStripSize(input);\n\t\tstripcount=TIFFNumberOfStrips(input);\n\t\tfor(i=0;i<stripcount;i++){\n\t\t\tread =\n\t\t\t\tTIFFReadEncodedStrip(input,\n\t\t\t\ti,\n\t\t\t\t(tdata_t) &buffer[bufferoffset],\n\t\t\t\tTIFFmin(stripsize, t2p->tiff_datasize - bufferoffset));\n\t\t\tif(read==-1){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Error on decoding strip %u of %s",\n\t\t\t\t\ti,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tbufferoffset+=read;\n\t\t}\n\t} else {\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG){\n\t\t\tsepstripsize=TIFFStripSize(input);\n\t\t\tsepstripcount=TIFFNumberOfStrips(input);\n\t\t\tstripsize=sepstripsize*t2p->tiff_samplesperpixel;\n\t\t\tstripcount=sepstripcount/t2p->tiff_samplesperpixel;\n\t\t\tbuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tmemset(buffer, 0, t2p->tiff_datasize);\n\t\t\tsamplebuffer = (unsigned char*) _TIFFmalloc(stripsize);\n\t\t\tif(samplebuffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n _TIFFfree(buffer);\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tsamplebufferoffset=0;\n\t\t\t\tfor(j=0;j<t2p->tiff_samplesperpixel;j++){\n\t\t\t\t\tread =\n\t\t\t\t\t\tTIFFReadEncodedStrip(input,\n\t\t\t\t\t\t\ti + j*stripcount,\n\t\t\t\t\t\t\t(tdata_t) &(samplebuffer[samplebufferoffset]),\n\t\t\t\t\t\t\tTIFFmin(sepstripsize, stripsize - samplebufferoffset));\n\t\t\t\t\tif(read==-1){\n\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t\t\t"Error on decoding strip %u of %s",\n\t\t\t\t\t\t\ti + j*stripcount,\n\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\t_TIFFfree(buffer);\n\t\t\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\t\t\t\treturn(0);\n\t\t\t\t\t}\n\t\t\t\t\tsamplebufferoffset+=read;\n\t\t\t\t}\n\t\t\t\tt2p_sample_planar_separate_to_contig(\n\t\t\t\t\tt2p,\n\t\t\t\t\t&(buffer[bufferoffset]),\n\t\t\t\t\tsamplebuffer,\n\t\t\t\t\tsamplebufferoffset);\n\t\t\t\tbufferoffset+=samplebufferoffset;\n\t\t\t}\n\t\t\t_TIFFfree(samplebuffer);\n\t\t\tgoto dataready;\n\t\t}\n\t\tbuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\tif(buffer==NULL){\n\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\tTIFFFileName(input));\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn(0);\n\t\t}\n\t\tmemset(buffer, 0, t2p->tiff_datasize);\n\t\tstripsize=TIFFStripSize(input);\n\t\tstripcount=TIFFNumberOfStrips(input);\n\t\tfor(i=0;i<stripcount;i++){\n\t\t\tread =\n\t\t\t\tTIFFReadEncodedStrip(input,\n\t\t\t\ti,\n\t\t\t\t(tdata_t) &buffer[bufferoffset],\n\t\t\t\tTIFFmin(stripsize, t2p->tiff_datasize - bufferoffset));\n\t\t\tif(read==-1){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Error on decoding strip %u of %s",\n\t\t\t\t\ti,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t_TIFFfree(samplebuffer);\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tbufferoffset+=read;\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_REALIZE_PALETTE){\n\t\t\tsamplebuffer=(unsigned char*)_TIFFrealloc(\n\t\t\t\t(tdata_t) buffer,\n\t\t\t\tt2p->tiff_datasize * t2p->tiff_samplesperpixel);\n\t\t\tif(samplebuffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\treturn(0);\n\t\t\t} else {\n\t\t\t\tbuffer=samplebuffer;\n\t\t\t\tt2p->tiff_datasize *= t2p->tiff_samplesperpixel;\n\t\t\t}\n\t\t\tt2p_sample_realize_palette(t2p, buffer);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_RGBA_TO_RGB){\n\t\t\tt2p->tiff_datasize=t2p_sample_rgba_to_rgb(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_width*t2p->tiff_length);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_RGBAA_TO_RGB){\n\t\t\tt2p->tiff_datasize=t2p_sample_rgbaa_to_rgb(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_width*t2p->tiff_length);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_YCBCR_TO_RGB){\n\t\t\tsamplebuffer=(unsigned char*)_TIFFrealloc(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_width*t2p->tiff_length*4);\n\t\t\tif(samplebuffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\treturn(0);\n\t\t\t} else {\n\t\t\t\tbuffer=samplebuffer;\n\t\t\t}\n\t\t\tif(!TIFFReadRGBAImageOriented(\n\t\t\t\tinput,\n\t\t\t\tt2p->tiff_width,\n\t\t\t\tt2p->tiff_length,\n\t\t\t\t(uint32*)buffer,\n\t\t\t\tORIENTATION_TOPLEFT,\n\t\t\t\t0)){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t use TIFFReadRGBAImageOriented to extract RGB image from %s",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tt2p->tiff_datasize=t2p_sample_abgr_to_rgb(\n\t\t\t\t(tdata_t) buffer,\n\t\t\t\tt2p->tiff_width*t2p->tiff_length);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED){\n\t\t\tt2p->tiff_datasize=t2p_sample_lab_signed_to_unsigned(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_width*t2p->tiff_length);\n\t\t}\n\t}\ndataready:\n\tt2p_disable(output);\n\tTIFFSetField(output, TIFFTAG_PHOTOMETRIC, t2p->tiff_photometric);\n\tTIFFSetField(output, TIFFTAG_BITSPERSAMPLE, t2p->tiff_bitspersample);\n\tTIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, t2p->tiff_samplesperpixel);\n\tTIFFSetField(output, TIFFTAG_IMAGEWIDTH, t2p->tiff_width);\n\tTIFFSetField(output, TIFFTAG_IMAGELENGTH, t2p->tiff_length);\n\tTIFFSetField(output, TIFFTAG_ROWSPERSTRIP, t2p->tiff_length);\n\tTIFFSetField(output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);\n\tTIFFSetField(output, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);\n\tswitch(t2p->pdf_compression){\n\tcase T2P_COMPRESS_NONE:\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_NONE);\n\t\tbreak;\n#ifdef CCITT_SUPPORT\n\tcase T2P_COMPRESS_G4:\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);\n\t\tbreak;\n#endif\n#ifdef JPEG_SUPPORT\n\tcase T2P_COMPRESS_JPEG:\n\t\tif(t2p->tiff_photometric==PHOTOMETRIC_YCBCR) {\n\t\t\tuint16 hor = 0, ver = 0;\n\t\t\tif (TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &hor, &ver) !=0 ) {\n\t\t\t\tif(hor != 0 && ver != 0){\n\t\t\t\t\tTIFFSetField(output, TIFFTAG_YCBCRSUBSAMPLING, hor, ver);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(TIFFGetField(input, TIFFTAG_REFERENCEBLACKWHITE, &xfloatp)!=0){\n\t\t\t\tTIFFSetField(output, TIFFTAG_REFERENCEBLACKWHITE, xfloatp);\n\t\t\t}\n\t\t}\n\t\tif(TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_JPEG)==0){\n\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t"Unable to use JPEG compression for input %s and output %s",\n\t\t\t\tTIFFFileName(input),\n\t\t\t\tTIFFFileName(output));\n\t\t\t_TIFFfree(buffer);\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn(0);\n\t\t}\n\t\tTIFFSetField(output, TIFFTAG_JPEGTABLESMODE, 0);\n\t\tif(t2p->pdf_colorspace & (T2P_CS_RGB | T2P_CS_LAB)){\n\t\t\tTIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);\n\t\t\tif(t2p->tiff_photometric != PHOTOMETRIC_YCBCR){\n\t\t\t\tTIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);\n\t\t\t} else {\n\t\t\t\tTIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RAW);\n\t\t\t}\n\t\t}\n\t\tif(t2p->pdf_colorspace & T2P_CS_GRAY){\n\t\t\t(void)0;\n\t\t}\n\t\tif(t2p->pdf_colorspace & T2P_CS_CMYK){\n\t\t\t(void)0;\n\t\t}\n\t\tif(t2p->pdf_defaultcompressionquality != 0){\n\t\t\tTIFFSetField(output,\n\t\t\t\tTIFFTAG_JPEGQUALITY,\n\t\t\t\tt2p->pdf_defaultcompressionquality);\n\t\t}\n\t\tbreak;\n#endif\n#ifdef ZIP_SUPPORT\n\tcase T2P_COMPRESS_ZIP:\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);\n\t\tif(t2p->pdf_defaultcompressionquality%100 != 0){\n\t\t\tTIFFSetField(output,\n\t\t\t\tTIFFTAG_PREDICTOR,\n\t\t\t\tt2p->pdf_defaultcompressionquality % 100);\n\t\t}\n\t\tif(t2p->pdf_defaultcompressionquality/100 != 0){\n\t\t\tTIFFSetField(output,\n\t\t\t\tTIFFTAG_ZIPQUALITY,\n\t\t\t\t(t2p->pdf_defaultcompressionquality / 100));\n\t\t}\n\t\tbreak;\n#endif\n\tdefault:\n\t\tbreak;\n\t}\n\tt2p_enable(output);\n\tt2p->outputwritten = 0;\n#ifdef JPEG_SUPPORT\n\tif(t2p->pdf_compression == T2P_COMPRESS_JPEG\n\t && t2p->tiff_photometric == PHOTOMETRIC_YCBCR){\n\t\tbufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0,\n\t\t\t\t\t\t buffer,\n\t\t\t\t\t\t stripsize * stripcount);\n\t} else\n#endif\n {\n\t\tbufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0,\n\t\t\t\t\t\t buffer,\n\t\t\t\t\t\t t2p->tiff_datasize);\n\t}\n\tif (buffer != NULL) {\n\t\t_TIFFfree(buffer);\n\t\tbuffer=NULL;\n\t}\n\tif (bufferoffset == (tsize_t)-1) {\n\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t "Error writing encoded strip to output PDF %s",\n\t\t\t TIFFFileName(output));\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn(0);\n\t}\n\twritten = t2p->outputwritten;\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}']
35,797
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bn/bn_lib.c/#L377
BIGNUM *bn_expand2(BIGNUM *b, int words) { BN_ULONG *A,*B,*a; int i,j; bn_check_top(b); if (words > b->max) { bn_check_top(b); if (BN_get_flags(b,BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return(NULL); } a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1)); if (A == NULL) { BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE); return(NULL); } memset(A,0x5c,sizeof(BN_ULONG)*(words+1)); #if 1 B=b->d; if (B != NULL) { for (i=b->top&(~7); i>0; i-=8) { A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3]; A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7]; A+=8; B+=8; } switch (b->top&7) { case 7: A[6]=B[6]; case 6: A[5]=B[5]; case 5: A[4]=B[4]; case 4: A[3]=B[3]; case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } Free(b->d); } b->d=a; b->max=words; B= &(b->d[b->top]); j=(b->max - b->top) & ~7; for (i=0; i<j; i+=8) { B[0]=0; B[1]=0; B[2]=0; B[3]=0; B[4]=0; B[5]=0; B[6]=0; B[7]=0; B+=8; } j=(b->max - b->top) & 7; for (i=0; i<j; i++) { B[0]=0; B++; } #else memcpy(a->d,b->d,sizeof(b->d[0])*b->top); #endif } return(b); }
['BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int strong, BIGNUM *add,\n\t BIGNUM *rem, void (*callback)(P_I_I_P), char *cb_arg)\n\t{\n\tBIGNUM *rnd=NULL;\n\tBIGNUM t;\n\tint i,j,c1=0;\n\tBN_CTX *ctx;\n\tctx=BN_CTX_new();\n\tif (ctx == NULL) goto err;\n\tif (ret == NULL)\n\t\t{\n\t\tif ((rnd=BN_new()) == NULL) goto err;\n\t\t}\n\telse\n\t\trnd=ret;\n\tBN_init(&t);\nloop:\n\tif (add == NULL)\n\t\t{\n\t\tif (!probable_prime(rnd,bits)) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (strong)\n\t\t\t{\n\t\t\tif (!probable_prime_dh_strong(rnd,bits,add,rem,ctx))\n\t\t\t\t goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!probable_prime_dh(rnd,bits,add,rem,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (callback != NULL) callback(0,c1++,cb_arg);\n\tif (!strong)\n\t\t{\n\t\ti=BN_is_prime(rnd,BN_prime_checks,callback,ctx,cb_arg);\n\t\tif (i == -1) goto err;\n\t\tif (i == 0) goto loop;\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_rshift1(&t,rnd)) goto err;\n\t\tfor (i=0; i<BN_prime_checks; i++)\n\t\t\t{\n\t\t\tj=BN_is_prime(rnd,1,callback,ctx,cb_arg);\n\t\t\tif (j == -1) goto err;\n\t\t\tif (j == 0) goto loop;\n\t\t\tj=BN_is_prime(&t,1,callback,ctx,cb_arg);\n\t\t\tif (j == -1) goto err;\n\t\t\tif (j == 0) goto loop;\n\t\t\tif (callback != NULL) callback(2,c1-1,cb_arg);\n\t\t\t}\n\t\t}\n\tret=rnd;\nerr:\n\tif ((ret == NULL) && (rnd != NULL)) BN_free(rnd);\n\tBN_free(&t);\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\treturn(ret);\n\t}', 'static int probable_prime_dh_strong(BIGNUM *p, int bits, BIGNUM *padd,\n\t BIGNUM *rem, BN_CTX *ctx)\n\t{\n\tint i,ret=0;\n\tBIGNUM *t1,*qadd=NULL,*q=NULL;\n\tbits--;\n\tt1= &(ctx->bn[ctx->tos++]);\n\tq= &(ctx->bn[ctx->tos++]);\n\tqadd= &(ctx->bn[ctx->tos++]);\n\tif (!BN_rshift1(qadd,padd)) goto err;\n\tif (!BN_rand(q,bits,0,1)) goto err;\n\tif (!BN_mod(t1,q,qadd,ctx)) goto err;\n\tif (!BN_sub(q,q,t1)) goto err;\n\tif (rem == NULL)\n\t\t{ if (!BN_add_word(q,1)) goto err; }\n\telse\n\t\t{\n\t\tif (!BN_rshift1(t1,rem)) goto err;\n\t\tif (!BN_add(q,q,t1)) goto err;\n\t\t}\n\tif (!BN_lshift1(p,q)) goto err;\n\tif (!BN_add_word(p,1)) goto err;\n\tloop: for (i=1; i<NUMPRIMES; i++)\n\t\t{\n\t\tif (\t(BN_mod_word(p,(BN_LONG)primes[i]) == 0) ||\n\t\t\t(BN_mod_word(q,(BN_LONG)primes[i]) == 0))\n\t\t\t{\n\t\t\tif (!BN_add(p,p,padd)) goto err;\n\t\t\tif (!BN_add(q,q,qadd)) goto err;\n\t\t\tgoto loop;\n\t\t\t}\n\t\t}\n\tret=1;\nerr:\n\tctx->tos-=3;\n\treturn(ret);\n\t}', 'int BN_rshift1(BIGNUM *r, BIGNUM *a)\n\t{\n\tBN_ULONG *ap,*rp,t,c;\n\tint i;\n\tif (BN_is_zero(a))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\tif (a != r)\n\t\t{\n\t\tif (bn_wexpand(r,a->top) == NULL) return(0);\n\t\tr->top=a->top;\n\t\tr->neg=a->neg;\n\t\t}\n\tap=a->d;\n\trp=r->d;\n\tc=0;\n\tfor (i=a->top-1; i>=0; i--)\n\t\t{\n\t\tt=ap[i];\n\t\trp[i]=((t>>1)&BN_MASK2)|c;\n\t\tc=(t&1)?BN_TBIT:0;\n\t\t}\n\tbn_fix_top(r);\n\treturn(1);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*B,*a;\n\tint i,j;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\nmemset(A,0x5c,sizeof(BN_ULONG)*(words+1));\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tB= &(b->d[b->top]);\n\t\tj=(b->max - b->top) & ~7;\n\t\tfor (i=0; i<j; i+=8)\n\t\t\t{\n\t\t\tB[0]=0; B[1]=0; B[2]=0; B[3]=0;\n\t\t\tB[4]=0; B[5]=0; B[6]=0; B[7]=0;\n\t\t\tB+=8;\n\t\t\t}\n\t\tj=(b->max - b->top) & 7;\n\t\tfor (i=0; i<j; i++)\n\t\t\t{\n\t\t\tB[0]=0;\n\t\t\tB++;\n\t\t\t}\n#else\n\t\t\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\t\t}\n\treturn(b);\n\t}']
35,798
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 dsa_do_verify(const unsigned char *dgst, int dgst_len,\n DSA_SIG *sig, DSA *dsa)\n{\n BN_CTX *ctx;\n BIGNUM *u1, *u2, *t1;\n BN_MONT_CTX *mont = NULL;\n const BIGNUM *r, *s;\n int ret = -1, i;\n if (!dsa->p || !dsa->q || !dsa->g) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);\n return -1;\n }\n i = BN_num_bits(dsa->q);\n if (i != 160 && i != 224 && i != 256) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);\n return -1;\n }\n if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);\n return -1;\n }\n u1 = BN_new();\n u2 = BN_new();\n t1 = BN_new();\n ctx = BN_CTX_new();\n if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)\n goto err;\n DSA_SIG_get0(sig, &r, &s);\n if (BN_is_zero(r) || BN_is_negative(r) ||\n BN_ucmp(r, dsa->q) >= 0) {\n ret = 0;\n goto err;\n }\n if (BN_is_zero(s) || BN_is_negative(s) ||\n BN_ucmp(s, dsa->q) >= 0) {\n ret = 0;\n goto err;\n }\n if ((BN_mod_inverse(u2, s, dsa->q, ctx)) == NULL)\n goto err;\n if (dgst_len > (i >> 3))\n dgst_len = (i >> 3);\n if (BN_bin2bn(dgst, dgst_len, u1) == NULL)\n goto err;\n if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))\n goto err;\n if (!BN_mod_mul(u2, r, u2, dsa->q, ctx))\n goto err;\n if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {\n mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,\n dsa->lock, dsa->p, ctx);\n if (!mont)\n goto err;\n }\n if (dsa->meth->dsa_mod_exp != NULL) {\n if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, u2,\n dsa->p, ctx, mont))\n goto err;\n } else {\n if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,\n mont))\n goto err;\n }\n if (!BN_mod(u1, t1, dsa->q, ctx))\n goto err;\n ret = (BN_ucmp(u1, r) == 0);\n err:\n if (ret < 0)\n DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);\n BN_CTX_free(ctx);\n BN_free(u1);\n BN_free(u2);\n BN_free(t1);\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}', '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,799
0
https://github.com/openssl/openssl/blob/d8028b202bfe337200a0cc89b80983ea1838cb30/ssl/packet.c/#L49
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) { assert(pkt->subs != NULL && len != 0); if (pkt->subs == NULL || len == 0) return 0; if (pkt->maxsize - pkt->written < len) return 0; if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) { size_t newlen; size_t reflen; reflen = (len > pkt->buf->length) ? len : pkt->buf->length; if (reflen > SIZE_MAX / 2) { newlen = SIZE_MAX; } else { newlen = reflen * 2; if (newlen < DEFAULT_BUF_SIZE) newlen = DEFAULT_BUF_SIZE; } if (BUF_MEM_grow(pkt->buf, newlen) == 0) return 0; } if (allocbytes != NULL) *allocbytes = WPACKET_get_curr(pkt); return 1; }
['EXT_RETURN tls_construct_ctos_renegotiate(SSL *s, WPACKET *pkt,\n unsigned int context, X509 *x,\n size_t chainidx, int *al)\n{\n if (!s->renegotiate)\n return EXT_RETURN_NOT_SENT;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_sub_memcpy_u8(pkt, s->s3->previous_client_finished,\n s->s3->previous_client_finished_len)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_RENEGOTIATE, ERR_R_INTERNAL_ERROR);\n return EXT_RETURN_FAIL;\n }\n return EXT_RETURN_SENT;\n}', 'int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)\n{\n unsigned char *data;\n assert(size <= sizeof(unsigned int));\n if (size > sizeof(unsigned int)\n || !WPACKET_allocate_bytes(pkt, size, &data)\n || !put_value(data, val, size))\n return 0;\n return 1;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n WPACKET_SUB *sub;\n unsigned char *lenchars;\n assert(pkt->subs != NULL);\n if (pkt->subs == NULL)\n return 0;\n sub = OPENSSL_zalloc(sizeof(*sub));\n if (sub == NULL)\n return 0;\n sub->parent = pkt->subs;\n pkt->subs = sub;\n sub->pwritten = pkt->written + lenbytes;\n sub->lenbytes = lenbytes;\n if (lenbytes == 0) {\n sub->packet_len = 0;\n return 1;\n }\n if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n return 0;\n sub->packet_len = lenchars - GETBUF(pkt);\n return 1;\n}', 'int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,\n size_t lenbytes)\n{\n if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)\n || !WPACKET_memcpy(pkt, src, len)\n || !WPACKET_close(pkt))\n return 0;\n return 1;\n}', 'int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)\n{\n unsigned char *dest;\n if (len == 0)\n return 1;\n if (!WPACKET_allocate_bytes(pkt, len, &dest))\n return 0;\n memcpy(dest, src, len);\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n assert(pkt->subs != NULL && len != 0);\n if (pkt->subs == NULL || len == 0)\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {\n size_t newlen;\n size_t reflen;\n reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n if (reflen > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = reflen * 2;\n if (newlen < DEFAULT_BUF_SIZE)\n newlen = DEFAULT_BUF_SIZE;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n if (allocbytes != NULL)\n *allocbytes = WPACKET_get_curr(pkt);\n return 1;\n}']
35,800
0
https://github.com/libav/libav/blob/93d06bd1df73861c73bc12c659aecdf404d1de2a/ffmpeg.c/#L3628
static void new_data_stream(AVFormatContext *oc, int file_idx) { AVStream *st; AVCodec *codec=NULL; AVCodecContext *data_enc; st = av_new_stream(oc, oc->nb_streams < nb_streamid_map ? streamid_map[oc->nb_streams] : 0); if (!st) { fprintf(stderr, "Could not alloc stream\n"); ffmpeg_exit(1); } new_output_stream(oc, file_idx); data_enc = st->codec; output_codecs = grow_array(output_codecs, sizeof(*output_codecs), &nb_output_codecs, nb_output_codecs + 1); if (!data_stream_copy) { fprintf(stderr, "Data stream encoding not supported yet (only streamcopy)\n"); ffmpeg_exit(1); } avcodec_get_context_defaults3(st->codec, codec); data_enc->codec_type = AVMEDIA_TYPE_DATA; if (data_codec_tag) data_enc->codec_tag= data_codec_tag; if (oc->oformat->flags & AVFMT_GLOBALHEADER) { data_enc->flags |= CODEC_FLAG_GLOBAL_HEADER; avcodec_opts[AVMEDIA_TYPE_DATA]->flags |= CODEC_FLAG_GLOBAL_HEADER; } if (data_stream_copy) { st->stream_copy = 1; } data_disable = 0; av_freep(&data_codec_name); data_stream_copy = 0; }
['static void new_data_stream(AVFormatContext *oc, int file_idx)\n{\n AVStream *st;\n AVCodec *codec=NULL;\n AVCodecContext *data_enc;\n st = av_new_stream(oc, oc->nb_streams < nb_streamid_map ? streamid_map[oc->nb_streams] : 0);\n if (!st) {\n fprintf(stderr, "Could not alloc stream\\n");\n ffmpeg_exit(1);\n }\n new_output_stream(oc, file_idx);\n data_enc = st->codec;\n output_codecs = grow_array(output_codecs, sizeof(*output_codecs), &nb_output_codecs, nb_output_codecs + 1);\n if (!data_stream_copy) {\n fprintf(stderr, "Data stream encoding not supported yet (only streamcopy)\\n");\n ffmpeg_exit(1);\n }\n avcodec_get_context_defaults3(st->codec, codec);\n data_enc->codec_type = AVMEDIA_TYPE_DATA;\n if (data_codec_tag)\n data_enc->codec_tag= data_codec_tag;\n if (oc->oformat->flags & AVFMT_GLOBALHEADER) {\n data_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;\n avcodec_opts[AVMEDIA_TYPE_DATA]->flags |= CODEC_FLAG_GLOBAL_HEADER;\n }\n if (data_stream_copy) {\n st->stream_copy = 1;\n }\n data_disable = 0;\n av_freep(&data_codec_name);\n data_stream_copy = 0;\n}', 'AVStream *av_new_stream(AVFormatContext *s, int id)\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_context();\n if (s->iformat) {\n st->codec->bit_rate = 0;\n }\n st->index = s->nb_streams;\n st->id = id;\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 av_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}']