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 |
|---|---|---|---|---|
2,201 | 0 | https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L290 | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
bn_check_top(b);
if (a == b)
return a;
if (bn_wexpand(a, b->top) == NULL)
return NULL;
if (b->top > 0)
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
a->neg = b->neg;
a->top = b->top;
a->flags |= b->flags & BN_FLG_FIXED_TOP;
bn_check_top(a);
return a;
} | ['static int srp_Verify_N_and_g(const BIGNUM *N, const BIGNUM *g)\n{\n BN_CTX *bn_ctx = BN_CTX_new();\n BIGNUM *p = BN_new();\n BIGNUM *r = BN_new();\n int ret =\n g != NULL && N != NULL && bn_ctx != NULL && BN_is_odd(N) &&\n BN_is_prime_ex(N, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) == 1 &&\n p != NULL && BN_rshift1(p, N) &&\n BN_is_prime_ex(p, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) == 1 &&\n r != NULL &&\n BN_mod_exp(r, g, p, N, bn_ctx) &&\n BN_add_word(r, 1) && BN_cmp(r, N) == 0;\n BN_free(r);\n BN_free(p);\n BN_CTX_free(bn_ctx);\n return ret;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n BN_GENCB *cb)\n{\n return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);\n}', 'int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, j, ret = -1;\n int k;\n BN_CTX *ctx = NULL;\n BIGNUM *A1, *A1_odd, *A3, *check;\n BN_MONT_CTX *mont = NULL;\n if (BN_is_word(a, 2) || BN_is_word(a, 3))\n return 1;\n if (!BN_is_odd(a) || BN_cmp(a, BN_value_one()) <= 0)\n return 0;\n if (checks == BN_prime_checks)\n checks = BN_prime_checks_for_size(BN_num_bits(a));\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG mod = BN_mod_word(a, primes[i]);\n if (mod == (BN_ULONG)-1)\n goto err;\n if (mod == 0)\n return BN_is_word(a, primes[i]);\n }\n if (!BN_GENCB_call(cb, 1, -1))\n goto err;\n }\n if (ctx_passed != NULL)\n ctx = ctx_passed;\n else if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n A1 = BN_CTX_get(ctx);\n A3 = BN_CTX_get(ctx);\n A1_odd = BN_CTX_get(ctx);\n check = BN_CTX_get(ctx);\n if (check == NULL)\n goto err;\n if (!BN_copy(A1, a) || !BN_sub_word(A1, 1))\n goto err;\n if (!BN_copy(A3, a) || !BN_sub_word(A3, 3))\n goto err;\n k = 1;\n while (!BN_is_bit_set(A1, k))\n k++;\n if (!BN_rshift(A1_odd, A1, k))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, a, ctx))\n goto err;\n for (i = 0; i < checks; i++) {\n if (!BN_priv_rand_range(check, A3) || !BN_add_word(check, 2))\n goto err;\n j = witness(check, a, A1, A1_odd, k, ctx, mont);\n if (j == -1)\n goto err;\n if (j) {\n ret = 0;\n goto err;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n if (ctx_passed == NULL)\n BN_CTX_free(ctx);\n }\n BN_MONT_CTX_free(mont);\n return ret;\n}', 'int BN_is_word(const BIGNUM *a, const BN_ULONG w)\n{\n return BN_abs_is_word(a, w) && (!w || !a->neg);\n}', 'int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)\n{\n return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));\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}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_RECP_CTX_free(&recp);\n bn_check_top(r);\n return ret;\n}', 'int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!BN_copy(&(recp->N), d))\n return 0;\n BN_zero(&(recp->Nr));\n recp->num_bits = BN_num_bits(d);\n recp->shift = 0;\n return 1;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->neg = b->neg;\n a->top = b->top;\n a->flags |= b->flags & BN_FLG_FIXED_TOP;\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}'] |
2,202 | 0 | https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/motion_est_template.c/#L149 | static int hpel_motion_search(MpegEncContext * s,
int *mx_ptr, int *my_ptr, int dmin,
int src_index, int ref_index,
int size, int h)
{
MotionEstContext * const c= &s->me;
const int mx = *mx_ptr;
const int my = *my_ptr;
const int penalty_factor= c->sub_penalty_factor;
me_cmp_func cmp_sub, chroma_cmp_sub;
int bx=2*mx, by=2*my;
LOAD_COMMON
int flags= c->sub_flags;
cmp_sub= s->dsp.me_sub_cmp[size];
chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
if(c->skip){
*mx_ptr = 0;
*my_ptr = 0;
return dmin;
}
if(c->avctx->me_cmp != c->avctx->me_sub_cmp){
dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
if(mx || my || size>0)
dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
}
if (mx > xmin && mx < xmax &&
my > ymin && my < ymax) {
int d= dmin;
const int index= (my<<ME_MAP_SHIFT) + mx;
const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
+ (mv_penalty[bx - pred_x] + mv_penalty[by-2 - pred_y])*c->penalty_factor;
const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]
+ (mv_penalty[bx-2 - pred_x] + mv_penalty[by - pred_y])*c->penalty_factor;
const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]
+ (mv_penalty[bx+2 - pred_x] + mv_penalty[by - pred_y])*c->penalty_factor;
const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
+ (mv_penalty[bx - pred_x] + mv_penalty[by+2 - pred_y])*c->penalty_factor;
#if 1
int key;
int map_generation= c->map_generation;
#ifndef NDEBUG
uint32_t *map= c->map;
#endif
key= ((my-1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
assert(map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
key= ((my+1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
assert(map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
key= ((my)<<ME_MAP_MV_BITS) + (mx+1) + map_generation;
assert(map[(index+1)&(ME_MAP_SIZE-1)] == key);
key= ((my)<<ME_MAP_MV_BITS) + (mx-1) + map_generation;
assert(map[(index-1)&(ME_MAP_SIZE-1)] == key);
#endif
if(t<=b){
CHECK_HALF_MV(0, 1, mx ,my-1)
if(l<=r){
CHECK_HALF_MV(1, 1, mx-1, my-1)
if(t+r<=b+l){
CHECK_HALF_MV(1, 1, mx , my-1)
}else{
CHECK_HALF_MV(1, 1, mx-1, my )
}
CHECK_HALF_MV(1, 0, mx-1, my )
}else{
CHECK_HALF_MV(1, 1, mx , my-1)
if(t+l<=b+r){
CHECK_HALF_MV(1, 1, mx-1, my-1)
}else{
CHECK_HALF_MV(1, 1, mx , my )
}
CHECK_HALF_MV(1, 0, mx , my )
}
}else{
if(l<=r){
if(t+l<=b+r){
CHECK_HALF_MV(1, 1, mx-1, my-1)
}else{
CHECK_HALF_MV(1, 1, mx , my )
}
CHECK_HALF_MV(1, 0, mx-1, my)
CHECK_HALF_MV(1, 1, mx-1, my)
}else{
if(t+r<=b+l){
CHECK_HALF_MV(1, 1, mx , my-1)
}else{
CHECK_HALF_MV(1, 1, mx-1, my)
}
CHECK_HALF_MV(1, 0, mx , my)
CHECK_HALF_MV(1, 1, mx , my)
}
CHECK_HALF_MV(0, 1, mx , my)
}
assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2);
}
*mx_ptr = bx;
*my_ptr = by;
return dmin;
} | ['static int hpel_motion_search(MpegEncContext * s,\n int *mx_ptr, int *my_ptr, int dmin,\n int src_index, int ref_index,\n int size, int h)\n{\n MotionEstContext * const c= &s->me;\n const int mx = *mx_ptr;\n const int my = *my_ptr;\n const int penalty_factor= c->sub_penalty_factor;\n me_cmp_func cmp_sub, chroma_cmp_sub;\n int bx=2*mx, by=2*my;\n LOAD_COMMON\n int flags= c->sub_flags;\n cmp_sub= s->dsp.me_sub_cmp[size];\n chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];\n if(c->skip){\n *mx_ptr = 0;\n *my_ptr = 0;\n return dmin;\n }\n if(c->avctx->me_cmp != c->avctx->me_sub_cmp){\n dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);\n if(mx || my || size>0)\n dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;\n }\n if (mx > xmin && mx < xmax &&\n my > ymin && my < ymax) {\n int d= dmin;\n const int index= (my<<ME_MAP_SHIFT) + mx;\n const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]\n + (mv_penalty[bx - pred_x] + mv_penalty[by-2 - pred_y])*c->penalty_factor;\n const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]\n + (mv_penalty[bx-2 - pred_x] + mv_penalty[by - pred_y])*c->penalty_factor;\n const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]\n + (mv_penalty[bx+2 - pred_x] + mv_penalty[by - pred_y])*c->penalty_factor;\n const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]\n + (mv_penalty[bx - pred_x] + mv_penalty[by+2 - pred_y])*c->penalty_factor;\n#if 1\n int key;\n int map_generation= c->map_generation;\n#ifndef NDEBUG\n uint32_t *map= c->map;\n#endif\n key= ((my-1)<<ME_MAP_MV_BITS) + (mx) + map_generation;\n assert(map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);\n key= ((my+1)<<ME_MAP_MV_BITS) + (mx) + map_generation;\n assert(map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);\n key= ((my)<<ME_MAP_MV_BITS) + (mx+1) + map_generation;\n assert(map[(index+1)&(ME_MAP_SIZE-1)] == key);\n key= ((my)<<ME_MAP_MV_BITS) + (mx-1) + map_generation;\n assert(map[(index-1)&(ME_MAP_SIZE-1)] == key);\n#endif\n if(t<=b){\n CHECK_HALF_MV(0, 1, mx ,my-1)\n if(l<=r){\n CHECK_HALF_MV(1, 1, mx-1, my-1)\n if(t+r<=b+l){\n CHECK_HALF_MV(1, 1, mx , my-1)\n }else{\n CHECK_HALF_MV(1, 1, mx-1, my )\n }\n CHECK_HALF_MV(1, 0, mx-1, my )\n }else{\n CHECK_HALF_MV(1, 1, mx , my-1)\n if(t+l<=b+r){\n CHECK_HALF_MV(1, 1, mx-1, my-1)\n }else{\n CHECK_HALF_MV(1, 1, mx , my )\n }\n CHECK_HALF_MV(1, 0, mx , my )\n }\n }else{\n if(l<=r){\n if(t+l<=b+r){\n CHECK_HALF_MV(1, 1, mx-1, my-1)\n }else{\n CHECK_HALF_MV(1, 1, mx , my )\n }\n CHECK_HALF_MV(1, 0, mx-1, my)\n CHECK_HALF_MV(1, 1, mx-1, my)\n }else{\n if(t+r<=b+l){\n CHECK_HALF_MV(1, 1, mx , my-1)\n }else{\n CHECK_HALF_MV(1, 1, mx-1, my)\n }\n CHECK_HALF_MV(1, 0, mx , my)\n CHECK_HALF_MV(1, 1, mx , my)\n }\n CHECK_HALF_MV(0, 1, mx , my)\n }\n assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2);\n }\n *mx_ptr = bx;\n *my_ptr = by;\n return dmin;\n}'] |
2,203 | 0 | https://github.com/libav/libav/blob/452a398fd6bdca3f301c5c8af3bc241bc16a777e/libavcodec/motion_est_template.c/#L804 | static int full_search(MpegEncContext * s, int *best, int dmin,
int src_index, int ref_index, int const penalty_factor,
int size, int h, int flags)
{
MotionEstContext * const c= &s->me;
me_cmp_func cmpf, chroma_cmpf;
LOAD_COMMON
LOAD_COMMON2
int map_generation= c->map_generation;
int x,y, d;
const int dia_size= c->dia_size&0xFF;
cmpf= s->dsp.me_cmp[size];
chroma_cmpf= s->dsp.me_cmp[size+1];
for(y=FFMAX(-dia_size, ymin); y<=FFMIN(dia_size,ymax); y++){
for(x=FFMAX(-dia_size, xmin); x<=FFMIN(dia_size,xmax); x++){
CHECK_MV(x, y);
}
}
x= best[0];
y= best[1];
d= dmin;
CHECK_CLIPPED_MV(x , y);
CHECK_CLIPPED_MV(x+1, y);
CHECK_CLIPPED_MV(x, y+1);
CHECK_CLIPPED_MV(x-1, y);
CHECK_CLIPPED_MV(x, y-1);
best[0]= x;
best[1]= y;
return d;
} | ['static int full_search(MpegEncContext * s, int *best, int dmin,\n int src_index, int ref_index, int const penalty_factor,\n int size, int h, int flags)\n{\n MotionEstContext * const c= &s->me;\n me_cmp_func cmpf, chroma_cmpf;\n LOAD_COMMON\n LOAD_COMMON2\n int map_generation= c->map_generation;\n int x,y, d;\n const int dia_size= c->dia_size&0xFF;\n cmpf= s->dsp.me_cmp[size];\n chroma_cmpf= s->dsp.me_cmp[size+1];\n for(y=FFMAX(-dia_size, ymin); y<=FFMIN(dia_size,ymax); y++){\n for(x=FFMAX(-dia_size, xmin); x<=FFMIN(dia_size,xmax); x++){\n CHECK_MV(x, y);\n }\n }\n x= best[0];\n y= best[1];\n d= dmin;\n CHECK_CLIPPED_MV(x , y);\n CHECK_CLIPPED_MV(x+1, y);\n CHECK_CLIPPED_MV(x, y+1);\n CHECK_CLIPPED_MV(x-1, y);\n CHECK_CLIPPED_MV(x, y-1);\n best[0]= x;\n best[1]= y;\n return d;\n}'] |
2,204 | 0 | https://github.com/openssl/openssl/blob/55525742f4c2bf416013fc3a75ec642775d97f80/crypto/bn/bn_ctx.c/#L353 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM local_A, local_B;\n\tBIGNUM *pA, *pB;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tpB = &local_B;\n\t\tBN_with_flags(pB, B, BN_FLG_CONSTTIME);\n\t\tif (!BN_nnmod(B, pB, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tBIGNUM *tmp;\n\t\tpA = &local_A;\n\t\tBN_with_flags(pA, A, BN_FLG_CONSTTIME);\n\t\tif (!BN_div(D,M,pA,B,ctx)) goto err;\n\t\ttmp=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=tmp;\n\t\tsign = -sign;\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_START,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_div_no_branch(dv, rm, num, divisor, ctx);\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\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\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\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\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\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_NO_BRANCH,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}'] |
2,205 | 0 | https://github.com/libav/libav/blob/b5c1c16247ab7d166c84eaf4564e49a1535fdaaf/libavcodec/vc1_block.c/#L2660 | static void vc1_decode_i_blocks(VC1Context *v)
{
int k, j;
MpegEncContext *s = &v->s;
int cbp, val;
uint8_t *coded_val;
int mb_pos;
switch (v->y_ac_table_index) {
case 0:
v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
break;
case 1:
v->codingset = CS_HIGH_MOT_INTRA;
break;
case 2:
v->codingset = CS_MID_RATE_INTRA;
break;
}
switch (v->c_ac_table_index) {
case 0:
v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
break;
case 1:
v->codingset2 = CS_HIGH_MOT_INTER;
break;
case 2:
v->codingset2 = CS_MID_RATE_INTER;
break;
}
s->y_dc_scale = s->y_dc_scale_table[v->pq];
s->c_dc_scale = s->c_dc_scale_table[v->pq];
s->mb_x = s->mb_y = 0;
s->mb_intra = 1;
s->first_slice_line = 1;
for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
s->mb_x = 0;
init_block_index(v);
for (; s->mb_x < v->end_mb_x; s->mb_x++) {
uint8_t *dst[6];
ff_update_block_index(s);
dst[0] = s->dest[0];
dst[1] = dst[0] + 8;
dst[2] = s->dest[0] + s->linesize * 8;
dst[3] = dst[2] + 8;
dst[4] = s->dest[1];
dst[5] = s->dest[2];
s->bdsp.clear_blocks(s->block[0]);
mb_pos = s->mb_x + s->mb_y * s->mb_width;
s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
s->current_picture.qscale_table[mb_pos] = v->pq;
s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
v->s.ac_pred = get_bits1(&v->s.gb);
for (k = 0; k < 6; k++) {
val = ((cbp >> (5 - k)) & 1);
if (k < 4) {
int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
val = val ^ pred;
*coded_val = val;
}
cbp |= val << (5 - k);
vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
if (k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
continue;
v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
if (v->pq >= 9 && v->overlap) {
if (v->rangeredfrm)
for (j = 0; j < 64; j++)
s->block[k][j] <<= 1;
s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
k & 4 ? s->uvlinesize
: s->linesize);
} else {
if (v->rangeredfrm)
for (j = 0; j < 64; j++)
s->block[k][j] = (s->block[k][j] - 64) << 1;
s->idsp.put_pixels_clamped(s->block[k], dst[k],
k & 4 ? s->uvlinesize
: s->linesize);
}
}
if (v->pq >= 9 && v->overlap) {
if (s->mb_x) {
v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
}
}
v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
if (!s->first_slice_line) {
v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
}
}
v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
}
if (v->s.loop_filter)
ff_vc1_loop_filter_iblk(v, v->pq);
if (get_bits_count(&s->gb) > v->bits) {
ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
get_bits_count(&s->gb), v->bits);
return;
}
}
if (!v->s.loop_filter)
ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
else if (s->mb_y)
ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
s->first_slice_line = 0;
}
if (v->s.loop_filter)
ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
} | ['static void vc1_decode_i_blocks(VC1Context *v)\n{\n int k, j;\n MpegEncContext *s = &v->s;\n int cbp, val;\n uint8_t *coded_val;\n int mb_pos;\n switch (v->y_ac_table_index) {\n case 0:\n v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;\n break;\n case 1:\n v->codingset = CS_HIGH_MOT_INTRA;\n break;\n case 2:\n v->codingset = CS_MID_RATE_INTRA;\n break;\n }\n switch (v->c_ac_table_index) {\n case 0:\n v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;\n break;\n case 1:\n v->codingset2 = CS_HIGH_MOT_INTER;\n break;\n case 2:\n v->codingset2 = CS_MID_RATE_INTER;\n break;\n }\n s->y_dc_scale = s->y_dc_scale_table[v->pq];\n s->c_dc_scale = s->c_dc_scale_table[v->pq];\n s->mb_x = s->mb_y = 0;\n s->mb_intra = 1;\n s->first_slice_line = 1;\n for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {\n s->mb_x = 0;\n init_block_index(v);\n for (; s->mb_x < v->end_mb_x; s->mb_x++) {\n uint8_t *dst[6];\n ff_update_block_index(s);\n dst[0] = s->dest[0];\n dst[1] = dst[0] + 8;\n dst[2] = s->dest[0] + s->linesize * 8;\n dst[3] = dst[2] + 8;\n dst[4] = s->dest[1];\n dst[5] = s->dest[2];\n s->bdsp.clear_blocks(s->block[0]);\n mb_pos = s->mb_x + s->mb_y * s->mb_width;\n s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;\n s->current_picture.qscale_table[mb_pos] = v->pq;\n s->current_picture.motion_val[1][s->block_index[0]][0] = 0;\n s->current_picture.motion_val[1][s->block_index[0]][1] = 0;\n cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);\n v->s.ac_pred = get_bits1(&v->s.gb);\n for (k = 0; k < 6; k++) {\n val = ((cbp >> (5 - k)) & 1);\n if (k < 4) {\n int pred = vc1_coded_block_pred(&v->s, k, &coded_val);\n val = val ^ pred;\n *coded_val = val;\n }\n cbp |= val << (5 - k);\n vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);\n if (k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))\n continue;\n v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);\n if (v->pq >= 9 && v->overlap) {\n if (v->rangeredfrm)\n for (j = 0; j < 64; j++)\n s->block[k][j] <<= 1;\n s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],\n k & 4 ? s->uvlinesize\n : s->linesize);\n } else {\n if (v->rangeredfrm)\n for (j = 0; j < 64; j++)\n s->block[k][j] = (s->block[k][j] - 64) << 1;\n s->idsp.put_pixels_clamped(s->block[k], dst[k],\n k & 4 ? s->uvlinesize\n : s->linesize);\n }\n }\n if (v->pq >= 9 && v->overlap) {\n if (s->mb_x) {\n v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);\n v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);\n if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {\n v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);\n v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);\n }\n }\n v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);\n v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);\n if (!s->first_slice_line) {\n v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);\n v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);\n if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {\n v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);\n v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);\n }\n }\n v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);\n v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);\n }\n if (v->s.loop_filter)\n ff_vc1_loop_filter_iblk(v, v->pq);\n if (get_bits_count(&s->gb) > v->bits) {\n ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);\n av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\\n",\n get_bits_count(&s->gb), v->bits);\n return;\n }\n }\n if (!v->s.loop_filter)\n ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);\n else if (s->mb_y)\n ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);\n s->first_slice_line = 0;\n }\n if (v->s.loop_filter)\n ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);\n ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);\n}', 'static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,\n int coded, int codingset)\n{\n GetBitContext *gb = &v->s.gb;\n MpegEncContext *s = &v->s;\n int dc_pred_dir = 0;\n int i;\n int16_t *dc_val;\n int16_t *ac_val, *ac_val2;\n int dcdiff;\n if (n < 4) {\n dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);\n } else {\n dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);\n }\n if (dcdiff < 0) {\n av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\\n");\n return -1;\n }\n if (dcdiff) {\n if (dcdiff == 119 ) {\n if (v->pq == 1) dcdiff = get_bits(gb, 10);\n else if (v->pq == 2) dcdiff = get_bits(gb, 9);\n else dcdiff = get_bits(gb, 8);\n } else {\n if (v->pq == 1)\n dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;\n else if (v->pq == 2)\n dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;\n }\n if (get_bits1(gb))\n dcdiff = -dcdiff;\n }\n dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);\n *dc_val = dcdiff;\n if (n < 4) {\n block[0] = dcdiff * s->y_dc_scale;\n } else {\n block[0] = dcdiff * s->c_dc_scale;\n }\n if (!coded) {\n goto not_coded;\n }\n i = 1;\n {\n int last = 0, skip, value;\n const uint8_t *zz_table;\n int scale;\n int k;\n scale = v->pq * 2 + v->halfpq;\n if (v->s.ac_pred) {\n if (!dc_pred_dir)\n zz_table = v->zz_8x8[2];\n else\n zz_table = v->zz_8x8[3];\n } else\n zz_table = v->zz_8x8[1];\n ac_val = s->ac_val[0][0] + s->block_index[n] * 16;\n ac_val2 = ac_val;\n if (dc_pred_dir)\n ac_val -= 16;\n else\n ac_val -= 16 * s->block_wrap[n];\n while (!last) {\n vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);\n i += skip;\n if (i > 63)\n break;\n block[zz_table[i++]] = value;\n }\n if (s->ac_pred) {\n if (dc_pred_dir) {\n for (k = 1; k < 8; k++)\n block[k << v->left_blk_sh] += ac_val[k];\n } else {\n for (k = 1; k < 8; k++)\n block[k << v->top_blk_sh] += ac_val[k + 8];\n }\n }\n for (k = 1; k < 8; k++) {\n ac_val2[k] = block[k << v->left_blk_sh];\n ac_val2[k + 8] = block[k << v->top_blk_sh];\n }\n for (k = 1; k < 64; k++)\n if (block[k]) {\n block[k] *= scale;\n if (!v->pquantizer)\n block[k] += (block[k] < 0) ? -v->pq : v->pq;\n }\n if (s->ac_pred) i = 63;\n }\nnot_coded:\n if (!coded) {\n int k, scale;\n ac_val = s->ac_val[0][0] + s->block_index[n] * 16;\n ac_val2 = ac_val;\n i = 0;\n scale = v->pq * 2 + v->halfpq;\n memset(ac_val2, 0, 16 * 2);\n if (dc_pred_dir) {\n ac_val -= 16;\n if (s->ac_pred)\n memcpy(ac_val2, ac_val, 8 * 2);\n } else {\n ac_val -= 16 * s->block_wrap[n];\n if (s->ac_pred)\n memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);\n }\n if (s->ac_pred) {\n if (dc_pred_dir) {\n for (k = 1; k < 8; k++) {\n block[k << v->left_blk_sh] = ac_val[k] * scale;\n if (!v->pquantizer && block[k << v->left_blk_sh])\n block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;\n }\n } else {\n for (k = 1; k < 8; k++) {\n block[k << v->top_blk_sh] = ac_val[k + 8] * scale;\n if (!v->pquantizer && block[k << v->top_blk_sh])\n block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;\n }\n }\n i = 63;\n }\n }\n s->block_last_index[n] = i;\n return 0;\n}'] |
2,206 | 0 | https://github.com/openssl/openssl/blob/ff7b6ce9db329eb48775bb81e0ecbbd2a9b23c1c/crypto/bn/bn_ctx.c/#L355 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,\n\t\t\t\t BIGNUM **kinvp, BIGNUM **rp,\n\t\t\t\t const unsigned char *dgst, int dlen)\n\t{\n\tBN_CTX *ctx;\n\tBIGNUM k,kq,*K,*kinv=NULL,*r=NULL;\n\tint ret=0;\n\tif (!dsa->p || !dsa->q || !dsa->g)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_SIGN_SETUP,DSA_R_MISSING_PARAMETERS);\n\t\treturn 0;\n\t\t}\n\tBN_init(&k);\n\tBN_init(&kq);\n\tif (ctx_in == NULL)\n\t\t{\n\t\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\t\t}\n\telse\n\t\tctx=ctx_in;\n\tif ((r=BN_new()) == NULL) goto err;\n\tdo\n\t\t{\n#ifndef OPENSSL_NO_SHA512\n\t\tif (dgst != NULL)\n\t\t\t{\n\t\t\tif (!BN_generate_dsa_nonce(&k, dsa->q, dsa->priv_key, dgst,\n\t\t\t\t\t\t dlen, ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\telse\n#endif\n\t\t\tif (!BN_rand_range(&k, dsa->q)) goto err;\n\t\t} while (BN_is_zero(&k));\n\tif ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0)\n\t\t{\n\t\tBN_set_flags(&k, BN_FLG_CONSTTIME);\n\t\t}\n\tif (dsa->flags & DSA_FLAG_CACHE_MONT_P)\n\t\t{\n\t\tif (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,\n\t\t\t\t\t\tCRYPTO_LOCK_DSA,\n\t\t\t\t\t\tdsa->p, ctx))\n\t\t\tgoto err;\n\t\t}\n\tif ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0)\n\t\t{\n\t\tif (!BN_copy(&kq, &k)) goto err;\n\t\tif (!BN_add(&kq, &kq, dsa->q)) goto err;\n\t\tif (BN_num_bits(&kq) <= BN_num_bits(dsa->q))\n\t\t\t{\n\t\t\tif (!BN_add(&kq, &kq, dsa->q)) goto err;\n\t\t\t}\n\t\tK = &kq;\n\t\t}\n\telse\n\t\t{\n\t\tK = &k;\n\t\t}\n\tDSA_BN_MOD_EXP(goto err, dsa, r, dsa->g, K, dsa->p, ctx,\n\t\t\tdsa->method_mont_p);\n\tif (!BN_mod(r,r,dsa->q,ctx)) goto err;\n\tif ((kinv=BN_mod_inverse(NULL,&k,dsa->q,ctx)) == NULL) goto err;\n\tif (*kinvp != NULL) BN_clear_free(*kinvp);\n\t*kinvp=kinv;\n\tkinv=NULL;\n\tif (*rp != NULL) BN_clear_free(*rp);\n\t*rp=r;\n\tret=1;\nerr:\n\tif (!ret)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_SIGN_SETUP,ERR_R_BN_LIB);\n\t\tif (r != NULL)\n\t\t\tBN_clear_free(r);\n\t\t}\n\tif (ctx_in == NULL) BN_CTX_free(ctx);\n\tBN_clear_free(&k);\n\tBN_clear_free(&kq);\n\treturn(ret);\n\t}', 'int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range, const BIGNUM* priv,\n\t\t\t const unsigned char *message, size_t message_len,\n\t\t\t BN_CTX *ctx)\n\t{\n\tSHA512_CTX sha;\n\tunsigned char random_bytes[64];\n\tunsigned char digest[SHA512_DIGEST_LENGTH];\n\tunsigned done, todo;\n\tconst unsigned num_k_bytes = BN_num_bytes(range) + 8;\n\tunsigned char private_bytes[96];\n\tunsigned char *k_bytes;\n\tint ret = 0;\n\tk_bytes = OPENSSL_malloc(num_k_bytes);\n\tif (!k_bytes)\n\t\tgoto err;\n\ttodo = sizeof(priv->d[0])*priv->top;\n\tif (todo > sizeof(private_bytes))\n\t\t{\n\t\tBNerr(BN_F_BN_GENERATE_DSA_NONCE, BN_R_PRIVATE_KEY_TOO_LARGE);\n\t\tgoto err;\n\t\t}\n\tmemcpy(private_bytes, priv->d, todo);\n\tmemset(private_bytes + todo, 0, sizeof(private_bytes) - todo);\n\tfor (done = 0; done < num_k_bytes;) {\n\t\tif (RAND_bytes(random_bytes, sizeof(random_bytes)) != 1)\n\t\t\tgoto err;\n\t\tSHA512_Init(&sha);\n\t\tSHA512_Update(&sha, &done, sizeof(done));\n\t\tSHA512_Update(&sha, private_bytes, sizeof(private_bytes));\n\t\tSHA512_Update(&sha, message, message_len);\n\t\tSHA512_Update(&sha, random_bytes, sizeof(random_bytes));\n\t\tSHA512_Final(digest, &sha);\n\t\ttodo = num_k_bytes - done;\n\t\tif (todo > SHA512_DIGEST_LENGTH)\n\t\t\ttodo = SHA512_DIGEST_LENGTH;\n\t\tmemcpy(k_bytes + done, digest, todo);\n\t\tdone += todo;\n\t}\n\tif (!BN_bin2bn(k_bytes, num_k_bytes, out))\n\t\tgoto err;\n\tif (BN_mod(out, out, range, ctx) != 1)\n\t\tgoto err;\n\tret = 1;\nerr:\n\tif (k_bytes)\n\t\tOPENSSL_free(k_bytes);\n\treturn ret;\n\t}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tint no_branch=0;\n\tif (num->top > 0 && num->d[num->top - 1] == 0)\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_NOT_INITIALIZED);\n\t\treturn 0;\n\t\t}\n\tbn_check_top(num);\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\tno_branch=1;\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (!no_branch && BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n\t\tgoto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (no_branch)\n\t\t{\n\t\tif (snum->top <= sdiv->top+1)\n\t\t\t{\n\t\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\t\tsnum->top = sdiv->top + 2;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\t\tsnum->d[snum->top] = 0;\n\t\t\tsnum->top ++;\n\t\t\t}\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-no_branch;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (!no_branch)\n\t\t{\n\t\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t\t{\n\t\t\tbn_clear_top2max(&wnum);\n\t\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t\t*resp=1;\n\t\t\t}\n\t\telse\n\t\t\tres->top--;\n\t\t}\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tif (no_branch)\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_START,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}'] |
2,207 | 0 | https://gitlab.com/libtiff/libtiff/blob/7af4d827dd8a002e529508105a90d8f97dce91dd/tools/tiff2pdf.c/#L1077 | void t2p_read_tiff_init(T2P* t2p, TIFF* input){
tdir_t directorycount=0;
tdir_t i=0;
uint16 pagen=0;
uint16 paged=0;
uint16 xuint16=0;
directorycount=TIFFNumberOfDirectories(input);
t2p->tiff_pages = (T2P_PAGE*) _TIFFmalloc(directorycount * sizeof(T2P_PAGE));
if(t2p->tiff_pages==NULL){
TIFFError(
TIFF2PDF_MODULE,
"Can't allocate %lu bytes of memory for tiff_pages array, %s",
(unsigned long) directorycount * sizeof(T2P_PAGE),
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
_TIFFmemset( t2p->tiff_pages, 0x00, directorycount * sizeof(T2P_PAGE));
t2p->tiff_tiles = (T2P_TILES*) _TIFFmalloc(directorycount * sizeof(T2P_TILES));
if(t2p->tiff_tiles==NULL){
TIFFError(
TIFF2PDF_MODULE,
"Can't allocate %lu bytes of memory for tiff_tiles array, %s",
(unsigned long) directorycount * sizeof(T2P_TILES),
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
_TIFFmemset( t2p->tiff_tiles, 0x00, directorycount * sizeof(T2P_TILES));
for(i=0;i<directorycount;i++){
uint32 subfiletype = 0;
if(!TIFFSetDirectory(input, i)){
TIFFError(
TIFF2PDF_MODULE,
"Can't set directory %u of input file %s",
i,
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
if(TIFFGetField(input, TIFFTAG_PAGENUMBER, &pagen, &paged)){
if((pagen>paged) && (paged != 0)){
t2p->tiff_pages[t2p->tiff_pagecount].page_number =
paged;
} else {
t2p->tiff_pages[t2p->tiff_pagecount].page_number =
pagen;
}
goto ispage2;
}
if(TIFFGetField(input, TIFFTAG_SUBFILETYPE, &subfiletype)){
if ( ((subfiletype & FILETYPE_PAGE) != 0)
|| (subfiletype == 0)){
goto ispage;
} else {
goto isnotpage;
}
}
if(TIFFGetField(input, TIFFTAG_OSUBFILETYPE, &subfiletype)){
if ((subfiletype == OFILETYPE_IMAGE)
|| (subfiletype == OFILETYPE_PAGE)
|| (subfiletype == 0) ){
goto ispage;
} else {
goto isnotpage;
}
}
ispage:
t2p->tiff_pages[t2p->tiff_pagecount].page_number=t2p->tiff_pagecount;
ispage2:
t2p->tiff_pages[t2p->tiff_pagecount].page_directory=i;
if(TIFFIsTiled(input)){
t2p->tiff_pages[t2p->tiff_pagecount].page_tilecount =
TIFFNumberOfTiles(input);
}
t2p->tiff_pagecount++;
isnotpage:
(void)0;
}
qsort((void*) t2p->tiff_pages, t2p->tiff_pagecount,
sizeof(T2P_PAGE), t2p_cmp_t2p_page);
for(i=0;i<t2p->tiff_pagecount;i++){
t2p->pdf_xrefcount += 5;
TIFFSetDirectory(input, t2p->tiff_pages[i].page_directory );
if((TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &xuint16)
&& (xuint16==PHOTOMETRIC_PALETTE))
|| TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)) {
t2p->tiff_pages[i].page_extra++;
t2p->pdf_xrefcount++;
}
#ifdef ZIP_SUPPORT
if (TIFFGetField(input, TIFFTAG_COMPRESSION, &xuint16)) {
if( (xuint16== COMPRESSION_DEFLATE ||
xuint16== COMPRESSION_ADOBE_DEFLATE) &&
((t2p->tiff_pages[i].page_tilecount != 0)
|| TIFFNumberOfStrips(input)==1) &&
(t2p->pdf_nopassthrough==0) ){
if(t2p->pdf_minorversion<2){t2p->pdf_minorversion=2;}
}
}
#endif
if (TIFFGetField(input, TIFFTAG_TRANSFERFUNCTION,
&(t2p->tiff_transferfunction[0]),
&(t2p->tiff_transferfunction[1]),
&(t2p->tiff_transferfunction[2]))) {
if(t2p->tiff_transferfunction[1] !=
t2p->tiff_transferfunction[0]) {
t2p->tiff_transferfunctioncount = 3;
t2p->tiff_pages[i].page_extra += 4;
t2p->pdf_xrefcount += 4;
} else {
t2p->tiff_transferfunctioncount = 1;
t2p->tiff_pages[i].page_extra += 2;
t2p->pdf_xrefcount += 2;
}
if(t2p->pdf_minorversion < 2)
t2p->pdf_minorversion = 2;
} else {
t2p->tiff_transferfunctioncount=0;
}
if( TIFFGetField(
input,
TIFFTAG_ICCPROFILE,
&(t2p->tiff_iccprofilelength),
&(t2p->tiff_iccprofile)) != 0){
t2p->tiff_pages[i].page_extra++;
t2p->pdf_xrefcount++;
if(t2p->pdf_minorversion<3){t2p->pdf_minorversion=3;}
}
t2p->tiff_tiles[i].tiles_tilecount=
t2p->tiff_pages[i].page_tilecount;
if( (TIFFGetField(input, TIFFTAG_PLANARCONFIG, &xuint16) != 0)
&& (xuint16 == PLANARCONFIG_SEPARATE ) ){
TIFFGetField(input, TIFFTAG_SAMPLESPERPIXEL, &xuint16);
t2p->tiff_tiles[i].tiles_tilecount/= xuint16;
}
if( t2p->tiff_tiles[i].tiles_tilecount > 0){
t2p->pdf_xrefcount +=
(t2p->tiff_tiles[i].tiles_tilecount -1)*2;
TIFFGetField(input,
TIFFTAG_TILEWIDTH,
&( t2p->tiff_tiles[i].tiles_tilewidth) );
TIFFGetField(input,
TIFFTAG_TILELENGTH,
&( t2p->tiff_tiles[i].tiles_tilelength) );
t2p->tiff_tiles[i].tiles_tiles =
(T2P_TILE*) _TIFFmalloc(
t2p->tiff_tiles[i].tiles_tilecount
* sizeof(T2P_TILE) );
if( t2p->tiff_tiles[i].tiles_tiles == NULL){
TIFFError(
TIFF2PDF_MODULE,
"Can't allocate %lu bytes of memory for t2p_read_tiff_init, %s",
(unsigned long) t2p->tiff_tiles[i].tiles_tilecount * sizeof(T2P_TILE),
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
}
}
return;
} | ['void t2p_read_tiff_init(T2P* t2p, TIFF* input){\n\ttdir_t directorycount=0;\n\ttdir_t i=0;\n\tuint16 pagen=0;\n\tuint16 paged=0;\n\tuint16 xuint16=0;\n\tdirectorycount=TIFFNumberOfDirectories(input);\n\tt2p->tiff_pages = (T2P_PAGE*) _TIFFmalloc(directorycount * sizeof(T2P_PAGE));\n\tif(t2p->tiff_pages==NULL){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"Can\'t allocate %lu bytes of memory for tiff_pages array, %s",\n\t\t\t(unsigned long) directorycount * sizeof(T2P_PAGE),\n\t\t\tTIFFFileName(input));\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn;\n\t}\n\t_TIFFmemset( t2p->tiff_pages, 0x00, directorycount * sizeof(T2P_PAGE));\n\tt2p->tiff_tiles = (T2P_TILES*) _TIFFmalloc(directorycount * sizeof(T2P_TILES));\n\tif(t2p->tiff_tiles==NULL){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"Can\'t allocate %lu bytes of memory for tiff_tiles array, %s",\n\t\t\t(unsigned long) directorycount * sizeof(T2P_TILES),\n\t\t\tTIFFFileName(input));\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn;\n\t}\n\t_TIFFmemset( t2p->tiff_tiles, 0x00, directorycount * sizeof(T2P_TILES));\n\tfor(i=0;i<directorycount;i++){\n\t\tuint32 subfiletype = 0;\n\t\tif(!TIFFSetDirectory(input, i)){\n\t\t\tTIFFError(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"Can\'t set directory %u of input file %s",\n\t\t\t\ti,\n\t\t\t\tTIFFFileName(input));\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn;\n\t\t}\n\t\tif(TIFFGetField(input, TIFFTAG_PAGENUMBER, &pagen, &paged)){\n\t\t\tif((pagen>paged) && (paged != 0)){\n\t\t\t\tt2p->tiff_pages[t2p->tiff_pagecount].page_number =\n\t\t\t\t\tpaged;\n\t\t\t} else {\n\t\t\t\tt2p->tiff_pages[t2p->tiff_pagecount].page_number =\n\t\t\t\t\tpagen;\n\t\t\t}\n\t\t\tgoto ispage2;\n\t\t}\n\t\tif(TIFFGetField(input, TIFFTAG_SUBFILETYPE, &subfiletype)){\n\t\t\tif ( ((subfiletype & FILETYPE_PAGE) != 0)\n || (subfiletype == 0)){\n\t\t\t\tgoto ispage;\n\t\t\t} else {\n\t\t\t\tgoto isnotpage;\n\t\t\t}\n\t\t}\n\t\tif(TIFFGetField(input, TIFFTAG_OSUBFILETYPE, &subfiletype)){\n\t\t\tif ((subfiletype == OFILETYPE_IMAGE)\n\t\t\t\t|| (subfiletype == OFILETYPE_PAGE)\n\t\t\t\t|| (subfiletype == 0) ){\n\t\t\t\tgoto ispage;\n\t\t\t} else {\n\t\t\t\tgoto isnotpage;\n\t\t\t}\n\t\t}\n\t\tispage:\n\t\tt2p->tiff_pages[t2p->tiff_pagecount].page_number=t2p->tiff_pagecount;\n\t\tispage2:\n\t\tt2p->tiff_pages[t2p->tiff_pagecount].page_directory=i;\n\t\tif(TIFFIsTiled(input)){\n\t\t\tt2p->tiff_pages[t2p->tiff_pagecount].page_tilecount =\n\t\t\t\tTIFFNumberOfTiles(input);\n\t\t}\n\t\tt2p->tiff_pagecount++;\n\t\tisnotpage:\n\t\t(void)0;\n\t}\n\tqsort((void*) t2p->tiff_pages, t2p->tiff_pagecount,\n sizeof(T2P_PAGE), t2p_cmp_t2p_page);\n\tfor(i=0;i<t2p->tiff_pagecount;i++){\n\t\tt2p->pdf_xrefcount += 5;\n\t\tTIFFSetDirectory(input, t2p->tiff_pages[i].page_directory );\n\t\tif((TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &xuint16)\n && (xuint16==PHOTOMETRIC_PALETTE))\n\t\t || TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)) {\n\t\t\tt2p->tiff_pages[i].page_extra++;\n\t\t\tt2p->pdf_xrefcount++;\n\t\t}\n#ifdef ZIP_SUPPORT\n\t\tif (TIFFGetField(input, TIFFTAG_COMPRESSION, &xuint16)) {\n if( (xuint16== COMPRESSION_DEFLATE ||\n xuint16== COMPRESSION_ADOBE_DEFLATE) &&\n ((t2p->tiff_pages[i].page_tilecount != 0)\n || TIFFNumberOfStrips(input)==1) &&\n (t2p->pdf_nopassthrough==0)\t){\n if(t2p->pdf_minorversion<2){t2p->pdf_minorversion=2;}\n }\n }\n#endif\n\t\tif (TIFFGetField(input, TIFFTAG_TRANSFERFUNCTION,\n &(t2p->tiff_transferfunction[0]),\n &(t2p->tiff_transferfunction[1]),\n &(t2p->tiff_transferfunction[2]))) {\n\t\t\tif(t2p->tiff_transferfunction[1] !=\n\t\t\t t2p->tiff_transferfunction[0]) {\n\t\t\t\tt2p->tiff_transferfunctioncount = 3;\n\t\t\t\tt2p->tiff_pages[i].page_extra += 4;\n\t\t\t\tt2p->pdf_xrefcount += 4;\n\t\t\t} else {\n\t\t\t\tt2p->tiff_transferfunctioncount = 1;\n\t\t\t\tt2p->tiff_pages[i].page_extra += 2;\n\t\t\t\tt2p->pdf_xrefcount += 2;\n\t\t\t}\n\t\t\tif(t2p->pdf_minorversion < 2)\n\t\t\t\tt2p->pdf_minorversion = 2;\n } else {\n\t\t\tt2p->tiff_transferfunctioncount=0;\n\t\t}\n\t\tif( TIFFGetField(\n\t\t\tinput,\n\t\t\tTIFFTAG_ICCPROFILE,\n\t\t\t&(t2p->tiff_iccprofilelength),\n\t\t\t&(t2p->tiff_iccprofile)) != 0){\n\t\t\tt2p->tiff_pages[i].page_extra++;\n\t\t\tt2p->pdf_xrefcount++;\n\t\t\tif(t2p->pdf_minorversion<3){t2p->pdf_minorversion=3;}\n\t\t}\n\t\tt2p->tiff_tiles[i].tiles_tilecount=\n\t\t\tt2p->tiff_pages[i].page_tilecount;\n\t\tif( (TIFFGetField(input, TIFFTAG_PLANARCONFIG, &xuint16) != 0)\n\t\t\t&& (xuint16 == PLANARCONFIG_SEPARATE ) ){\n\t\t\t\tTIFFGetField(input, TIFFTAG_SAMPLESPERPIXEL, &xuint16);\n\t\t\t\tt2p->tiff_tiles[i].tiles_tilecount/= xuint16;\n\t\t}\n\t\tif( t2p->tiff_tiles[i].tiles_tilecount > 0){\n\t\t\tt2p->pdf_xrefcount +=\n\t\t\t\t(t2p->tiff_tiles[i].tiles_tilecount -1)*2;\n\t\t\tTIFFGetField(input,\n\t\t\t\tTIFFTAG_TILEWIDTH,\n\t\t\t\t&( t2p->tiff_tiles[i].tiles_tilewidth) );\n\t\t\tTIFFGetField(input,\n\t\t\t\tTIFFTAG_TILELENGTH,\n\t\t\t\t&( t2p->tiff_tiles[i].tiles_tilelength) );\n\t\t\tt2p->tiff_tiles[i].tiles_tiles =\n\t\t\t(T2P_TILE*) _TIFFmalloc(\n\t\t\t\tt2p->tiff_tiles[i].tiles_tilecount\n\t\t\t\t* sizeof(T2P_TILE) );\n\t\t\tif( t2p->tiff_tiles[i].tiles_tiles == NULL){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory for t2p_read_tiff_init, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_tiles[i].tiles_tilecount * sizeof(T2P_TILE),\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\treturn;\n}', 'uint16\nTIFFNumberOfDirectories(TIFF* tif)\n{\n\tstatic const char module[] = "TIFFNumberOfDirectories";\n\tuint64 nextdir;\n\tuint16 n;\n\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\tnextdir = tif->tif_header.classic.tiff_diroff;\n\telse\n\t\tnextdir = tif->tif_header.big.tiff_diroff;\n\tn = 0;\n\twhile (nextdir != 0 && TIFFAdvanceDirectory(tif, &nextdir, NULL))\n {\n\t\tif(++n == 0)\n {\n TIFFErrorExt(tif->tif_clientdata, module,\n "Directory count exceeded 65535 limit, giving up on counting.");\n return (65535);\n }\n }\n\treturn (n);\n}', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n if (s == 0)\n return ((void *) NULL);\n\treturn (malloc((size_t) s));\n}'] |
2,208 | 0 | https://github.com/openssl/openssl/blob/de2f409ef9de775df6db2c7de69b7bb0df21e380/crypto/evp/m_sigver.c/#L129 | int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
size_t *siglen)
{
int sctx = 0, r = 0;
EVP_PKEY_CTX *pctx = ctx->pctx;
if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
if (!sigret)
return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
else {
EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_dup(ctx->pctx);
if (!dctx)
return 0;
r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
EVP_PKEY_CTX_free(dctx);
}
return r;
}
if (pctx->pmeth->signctx)
sctx = 1;
else
sctx = 0;
if (sigret) {
unsigned char md[EVP_MAX_MD_SIZE];
unsigned int mdlen = 0;
if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
if (sctx)
r = ctx->pctx->pmeth->signctx(ctx->pctx, sigret, siglen, ctx);
else
r = EVP_DigestFinal_ex(ctx, md, &mdlen);
} else {
EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
if (tmp_ctx == NULL)
return 0;
if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
EVP_MD_CTX_free(tmp_ctx);
return 0;
}
if (sctx)
r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
sigret, siglen, tmp_ctx);
else
r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
EVP_MD_CTX_free(tmp_ctx);
}
if (sctx || !r)
return r;
if (EVP_PKEY_sign(ctx->pctx, sigret, siglen, md, mdlen) <= 0)
return 0;
} else {
if (sctx) {
if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
return 0;
} else {
int s = EVP_MD_size(ctx->digest);
if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
return 0;
}
}
return 1;
} | ['int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,\n size_t *siglen)\n{\n int sctx = 0, r = 0;\n EVP_PKEY_CTX *pctx = ctx->pctx;\n if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {\n if (!sigret)\n return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);\n if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)\n r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);\n else {\n EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_dup(ctx->pctx);\n if (!dctx)\n return 0;\n r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);\n EVP_PKEY_CTX_free(dctx);\n }\n return r;\n }\n if (pctx->pmeth->signctx)\n sctx = 1;\n else\n sctx = 0;\n if (sigret) {\n unsigned char md[EVP_MAX_MD_SIZE];\n unsigned int mdlen = 0;\n if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {\n if (sctx)\n r = ctx->pctx->pmeth->signctx(ctx->pctx, sigret, siglen, ctx);\n else\n r = EVP_DigestFinal_ex(ctx, md, &mdlen);\n } else {\n EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();\n if (tmp_ctx == NULL)\n return 0;\n if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {\n EVP_MD_CTX_free(tmp_ctx);\n return 0;\n }\n if (sctx)\n r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,\n sigret, siglen, tmp_ctx);\n else\n r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);\n EVP_MD_CTX_free(tmp_ctx);\n }\n if (sctx || !r)\n return r;\n if (EVP_PKEY_sign(ctx->pctx, sigret, siglen, md, mdlen) <= 0)\n return 0;\n } else {\n if (sctx) {\n if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)\n return 0;\n } else {\n int s = EVP_MD_size(ctx->digest);\n if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)\n return 0;\n }\n }\n return 1;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)\n{\n unsigned char *tmp_buf;\n if ((in == NULL) || (in->digest == NULL)) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);\n return 0;\n }\n#ifndef OPENSSL_NO_ENGINE\n if (in->engine && !ENGINE_init(in->engine)) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);\n return 0;\n }\n#endif\n if (out->digest == in->digest) {\n tmp_buf = out->md_data;\n EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);\n } else\n tmp_buf = NULL;\n EVP_MD_CTX_reset(out);\n memcpy(out, in, sizeof(*out));\n out->md_data = NULL;\n out->pctx = NULL;\n if (in->md_data && out->digest->ctx_size) {\n if (tmp_buf)\n out->md_data = tmp_buf;\n else {\n out->md_data = OPENSSL_malloc(out->digest->ctx_size);\n if (out->md_data == NULL) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n }\n memcpy(out->md_data, in->md_data, out->digest->ctx_size);\n }\n out->update = in->update;\n if (in->pctx) {\n out->pctx = EVP_PKEY_CTX_dup(in->pctx);\n if (!out->pctx) {\n EVP_MD_CTX_reset(out);\n return 0;\n }\n }\n if (out->digest->copy)\n return out->digest->copy(out, in);\n return 1;\n}', 'void ERR_put_error(int lib, int func, int reason, const char *file, int line)\n{\n ERR_STATE *es;\n#ifdef _OSD_POSIX\n if (strncmp(file, "*POSIX(", sizeof("*POSIX(") - 1) == 0) {\n char *end;\n file += sizeof("*POSIX(") - 1;\n end = &file[strlen(file) - 1];\n if (*end == \')\')\n *end = \'\\0\';\n if ((end = strrchr(file, \'/\')) != NULL)\n file = &end[1];\n }\n#endif\n es = ERR_get_state();\n if (es == NULL)\n return;\n es->top = (es->top + 1) % ERR_NUM_ERRORS;\n if (es->top == es->bottom)\n es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;\n es->err_flags[es->top] = 0;\n es->err_buffer[es->top] = ERR_PACK(lib, func, reason);\n es->err_file[es->top] = file;\n es->err_line[es->top] = line;\n err_clear_data(es, es->top);\n}', 'ERR_STATE *ERR_get_state(void)\n{\n ERR_STATE *state = NULL;\n if (!RUN_ONCE(&err_init, err_do_init))\n return NULL;\n state = CRYPTO_THREAD_get_local(&err_thread_local);\n if (state == NULL) {\n state = OPENSSL_zalloc(sizeof(*state));\n if (state == NULL)\n return NULL;\n if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE)\n || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {\n ERR_STATE_free(state);\n return NULL;\n }\n OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);\n }\n return state;\n}', 'int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))\n{\n if (pthread_once(once, init) != 0)\n return 0;\n return 1;\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx);\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}'] |
2,209 | 0 | https://github.com/libav/libav/blob/452a398fd6bdca3f301c5c8af3bc241bc16a777e/libavcodec/mpegaudiodec.c/#L918 | void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
MPA_INT *window, int *dither_state,
OUT_INT *samples, int incr,
int32_t sb_samples[SBLIMIT])
{
int32_t tmp[32];
register MPA_INT *synth_buf;
register const MPA_INT *w, *w2, *p;
int j, offset, v;
OUT_INT *samples2;
#if FRAC_BITS <= 15
int sum, sum2;
#else
int64_t sum, sum2;
#endif
dct32(tmp, sb_samples);
offset = *synth_buf_offset;
synth_buf = synth_buf_ptr + offset;
for(j=0;j<32;j++) {
v = tmp[j];
#if FRAC_BITS <= 15
v = av_clip_int16(v);
#endif
synth_buf[j] = v;
}
memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
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 mpa_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 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 int32_t sb_samples[SBLIMIT])\n{\n int32_t tmp[32];\n register MPA_INT *synth_buf;\n register const MPA_INT *w, *w2, *p;\n int j, offset, v;\n OUT_INT *samples2;\n#if FRAC_BITS <= 15\n int sum, sum2;\n#else\n int64_t sum, sum2;\n#endif\n dct32(tmp, sb_samples);\n offset = *synth_buf_offset;\n synth_buf = synth_buf_ptr + offset;\n for(j=0;j<32;j++) {\n v = tmp[j];\n#if FRAC_BITS <= 15\n v = av_clip_int16(v);\n#endif\n synth_buf[j] = v;\n }\n memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));\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}'] |
2,210 | 0 | https://github.com/openssl/openssl/blob/cacd830f02736aeb6ee26a742c61b3df39b62195/engines/e_ncipher.c/#L873 | static EVP_PKEY *hwcrhk_load_pubkey(ENGINE *eng, const char *key_id,
UI_METHOD *ui_method, void *callback_data)
{
EVP_PKEY *res = NULL;
#ifndef OPENSSL_NO_RSA
res = hwcrhk_load_privkey(eng, key_id,
ui_method, callback_data);
#endif
if (res)
switch(res->type)
{
#ifndef OPENSSL_NO_RSA
case EVP_PKEY_RSA:
{
RSA *rsa = NULL;
CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY);
rsa = res->pkey.rsa;
res->pkey.rsa = RSA_new();
res->pkey.rsa->n = rsa->n;
res->pkey.rsa->e = rsa->e;
rsa->n = NULL;
rsa->e = NULL;
CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
RSA_free(rsa);
}
break;
#endif
default:
HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PUBKEY,
HWCRHK_R_CTRL_COMMAND_NOT_IMPLEMENTED);
goto err;
}
return res;
err:
if (res)
EVP_PKEY_free(res);
return NULL;
} | ['static EVP_PKEY *hwcrhk_load_pubkey(ENGINE *eng, const char *key_id,\n\tUI_METHOD *ui_method, void *callback_data)\n\t{\n\tEVP_PKEY *res = NULL;\n#ifndef OPENSSL_NO_RSA\n res = hwcrhk_load_privkey(eng, key_id,\n ui_method, callback_data);\n#endif\n\tif (res)\n\t\tswitch(res->type)\n\t\t\t{\n#ifndef OPENSSL_NO_RSA\n\t\tcase EVP_PKEY_RSA:\n\t\t\t{\n\t\t\tRSA *rsa = NULL;\n\t\t\tCRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY);\n\t\t\trsa = res->pkey.rsa;\n\t\t\tres->pkey.rsa = RSA_new();\n\t\t\tres->pkey.rsa->n = rsa->n;\n\t\t\tres->pkey.rsa->e = rsa->e;\n\t\t\trsa->n = NULL;\n\t\t\trsa->e = NULL;\n\t\t\tCRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);\n\t\t\tRSA_free(rsa);\n\t\t\t}\n\t\t\tbreak;\n#endif\n\t\tdefault:\n\t\t\tHWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PUBKEY,\n\t\t\t\tHWCRHK_R_CTRL_COMMAND_NOT_IMPLEMENTED);\n\t\t\tgoto err;\n\t\t\t}\n\treturn res;\n err:\n\tif (res)\n\t\tEVP_PKEY_free(res);\n\treturn NULL;\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:(%s)%s %-18s %s:%d\\n",\n\t\t\tCRYPTO_thread_id(), rw_text, operation_text,\n\t\t\tCRYPTO_get_lock_name(type), file, line);\n\t\t}\n#endif\n\tif (type < 0)\n\t\t{\n\t\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}', 'RSA *RSA_new(void)\n\t{\n\tRSA *r=RSA_new_method(NULL);\n\treturn r;\n\t}', 'RSA *RSA_new_method(ENGINE *engine)\n\t{\n\tRSA *ret;\n\tret=(RSA *)OPENSSL_malloc(sizeof(RSA));\n\tif (ret == NULL)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t\t}\n\tret->meth = RSA_get_default_method();\n#ifndef OPENSSL_NO_ENGINE\n\tif (engine)\n\t\t{\n\t\tif (!ENGINE_init(engine))\n\t\t\t{\n\t\t\tRSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);\n\t\t\tOPENSSL_free(ret);\n\t\t\treturn NULL;\n\t\t\t}\n\t\tret->engine = engine;\n\t\t}\n\telse\n\t\tret->engine = ENGINE_get_default_RSA();\n\tif(ret->engine)\n\t\t{\n\t\tret->meth = ENGINE_get_RSA(ret->engine);\n\t\tif(!ret->meth)\n\t\t\t{\n\t\t\tRSAerr(RSA_F_RSA_NEW_METHOD,\n\t\t\t\tERR_R_ENGINE_LIB);\n\t\t\tENGINE_finish(ret->engine);\n\t\t\tOPENSSL_free(ret);\n\t\t\treturn NULL;\n\t\t\t}\n\t\t}\n#endif\n\tret->pad=0;\n\tret->version=0;\n\tret->n=NULL;\n\tret->e=NULL;\n\tret->d=NULL;\n\tret->p=NULL;\n\tret->q=NULL;\n\tret->dmp1=NULL;\n\tret->dmq1=NULL;\n\tret->iqmp=NULL;\n\tret->references=1;\n\tret->_method_mod_n=NULL;\n\tret->_method_mod_p=NULL;\n\tret->_method_mod_q=NULL;\n\tret->blinding=NULL;\n\tret->bignum_data=NULL;\n\tret->flags=ret->meth->flags;\n\tCRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);\n\tif ((ret->meth->init != NULL) && !ret->meth->init(ret))\n\t\t{\n#ifndef OPENSSL_NO_ENGINE\n\t\tif (ret->engine)\n\t\t\tENGINE_finish(ret->engine);\n#endif\n\t\tCRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);\n\t\tOPENSSL_free(ret);\n\t\tret=NULL;\n\t\t}\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}'] |
2,211 | 0 | https://github.com/openssl/openssl/blob/848113a30b431c2fe21ae8de2a366b9b6146fb92/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 int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,\n X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)\n{\n const ASN1_OBJECT *aoid;\n int atype;\n const void *aval;\n int rv = 0;\n EVP_PKEY *pkpeer = NULL;\n EC_KEY *ecpeer = NULL;\n const unsigned char *p;\n int plen;\n X509_ALGOR_get0(&aoid, &atype, &aval, alg);\n if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)\n goto err;\n if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {\n const EC_GROUP *grp;\n EVP_PKEY *pk;\n pk = EVP_PKEY_CTX_get0_pkey(pctx);\n if (!pk)\n goto err;\n grp = EC_KEY_get0_group(pk->pkey.ec);\n ecpeer = EC_KEY_new();\n if (ecpeer == NULL)\n goto err;\n if (!EC_KEY_set_group(ecpeer, grp))\n goto err;\n } else {\n ecpeer = eckey_type2param(atype, aval);\n if (!ecpeer)\n goto err;\n }\n plen = ASN1_STRING_length(pubkey);\n p = ASN1_STRING_get0_data(pubkey);\n if (!p || !plen)\n goto err;\n if (!o2i_ECPublicKey(&ecpeer, &p, plen))\n goto err;\n pkpeer = EVP_PKEY_new();\n if (pkpeer == NULL)\n goto err;\n EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);\n if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)\n rv = 1;\n err:\n EC_KEY_free(ecpeer);\n EVP_PKEY_free(pkpeer);\n return rv;\n}', 'int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)\n{\n if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)\n return 0;\n EC_GROUP_free(key->group);\n key->group = EC_GROUP_dup(group);\n return (key->group == NULL) ? 0 : 1;\n}', 'EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)\n{\n EC_GROUP *t = NULL;\n int ok = 0;\n if (a == NULL)\n return NULL;\n if ((t = EC_GROUP_new(a->meth)) == NULL)\n return NULL;\n if (!EC_GROUP_copy(t, a))\n goto err;\n ok = 1;\n err:\n if (!ok) {\n EC_GROUP_free(t);\n return NULL;\n }\n return t;\n}', 'int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)\n{\n if (dest->meth->group_copy == 0) {\n ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n if (dest->meth != src->meth) {\n ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n if (dest == src)\n return 1;\n dest->curve_name = src->curve_name;\n dest->pre_comp_type = src->pre_comp_type;\n switch (src->pre_comp_type) {\n case PCT_none:\n dest->pre_comp.ec = NULL;\n break;\n case PCT_nistz256:\n#ifdef ECP_NISTZ256_ASM\n dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);\n#endif\n break;\n#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128\n case PCT_nistp224:\n dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);\n break;\n case PCT_nistp256:\n dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);\n break;\n case PCT_nistp521:\n dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);\n break;\n#else\n case PCT_nistp224:\n case PCT_nistp256:\n case PCT_nistp521:\n break;\n#endif\n case PCT_ec:\n dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);\n break;\n }\n if (src->mont_data != NULL) {\n if (dest->mont_data == NULL) {\n dest->mont_data = BN_MONT_CTX_new();\n if (dest->mont_data == NULL)\n return 0;\n }\n if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))\n return 0;\n } else {\n BN_MONT_CTX_free(dest->mont_data);\n dest->mont_data = NULL;\n }\n if (src->generator != NULL) {\n if (dest->generator == NULL) {\n dest->generator = EC_POINT_new(dest);\n if (dest->generator == NULL)\n return 0;\n }\n if (!EC_POINT_copy(dest->generator, src->generator))\n return 0;\n } else {\n EC_POINT_clear_free(dest->generator);\n dest->generator = NULL;\n }\n if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {\n if (!BN_copy(dest->order, src->order))\n return 0;\n if (!BN_copy(dest->cofactor, src->cofactor))\n return 0;\n }\n dest->asn1_flag = src->asn1_flag;\n dest->asn1_form = src->asn1_form;\n if (src->seed) {\n OPENSSL_free(dest->seed);\n if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {\n ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n if (!memcpy(dest->seed, src->seed, src->seed_len))\n return 0;\n dest->seed_len = src->seed_len;\n } else {\n OPENSSL_free(dest->seed);\n dest->seed = NULL;\n dest->seed_len = 0;\n }\n return dest->meth->group_copy(dest, src);\n}', 'EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)\n{\n EC_KEY *ret = NULL;\n if (a == NULL || (*a) == NULL || (*a)->group == NULL) {\n ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);\n return 0;\n }\n ret = *a;\n if (!EC_KEY_oct2key(ret, *in, len, NULL)) {\n ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);\n return 0;\n }\n *in += len;\n return ret;\n}', 'int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,\n BN_CTX *ctx)\n{\n if (key == NULL || key->group == NULL)\n return 0;\n if (key->pub_key == NULL)\n key->pub_key = EC_POINT_new(key->group);\n if (key->pub_key == NULL)\n return 0;\n if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)\n return 0;\n if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)\n key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);\n return 1;\n}', 'int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,\n const unsigned char *buf, size_t len, BN_CTX *ctx)\n{\n if (group->meth->oct2point == 0\n && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {\n ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n if (!ec_point_is_compat(point, group)) {\n ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {\n if (group->meth->field_type == NID_X9_62_prime_field)\n return ec_GFp_simple_oct2point(group, point, buf, len, ctx);\n else\n#ifdef OPENSSL_NO_EC2M\n {\n ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);\n return 0;\n }\n#else\n return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);\n#endif\n }\n return group->meth->oct2point(group, point, buf, len, ctx);\n}', 'int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,\n const unsigned char *buf, size_t len,\n BN_CTX *ctx)\n{\n point_conversion_form_t form;\n int y_bit;\n BN_CTX *new_ctx = NULL;\n BIGNUM *x, *y, *yxi;\n size_t field_len, enc_len;\n int ret = 0;\n if (len == 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);\n return 0;\n }\n form = buf[0];\n y_bit = form & 1;\n form = form & ~1U;\n if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)\n && (form != POINT_CONVERSION_UNCOMPRESSED)\n && (form != POINT_CONVERSION_HYBRID)) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if (form == 0) {\n if (len != 1) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n return EC_POINT_set_to_infinity(group, point);\n }\n field_len = (EC_GROUP_get_degree(group) + 7) / 8;\n enc_len =\n (form ==\n POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;\n if (len != enc_len) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n yxi = BN_CTX_get(ctx);\n if (yxi == NULL)\n goto err;\n if (!BN_bin2bn(buf + 1, field_len, x))\n goto err;\n if (BN_ucmp(x, group->field) >= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n if (form == POINT_CONVERSION_COMPRESSED) {\n if (!EC_POINT_set_compressed_coordinates_GF2m\n (group, point, x, y_bit, ctx))\n goto err;\n } else {\n if (!BN_bin2bn(buf + 1 + field_len, field_len, y))\n goto err;\n if (BN_ucmp(y, group->field) >= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n if (form == POINT_CONVERSION_HYBRID) {\n if (!group->meth->field_div(group, yxi, y, x, ctx))\n goto err;\n if (y_bit != BN_is_odd(yxi)) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n }\n if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx))\n goto err;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,\n EC_POINT *point, const BIGNUM *x,\n int y_bit, BN_CTX *ctx)\n{\n if (group->meth->point_set_compressed_coordinates == 0\n && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {\n ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,\n ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n if (!ec_point_is_compat(point, group)) {\n ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,\n EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {\n if (group->meth->field_type == NID_X9_62_prime_field)\n return ec_GFp_simple_set_compressed_coordinates(group, point, x,\n y_bit, ctx);\n else\n return ec_GF2m_simple_set_compressed_coordinates(group, point, x,\n y_bit, ctx);\n }\n return group->meth->point_set_compressed_coordinates(group, point, x,\n y_bit, ctx);\n}', 'int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x_, int y_bit,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp1, *tmp2, *x, *y;\n int ret = 0;\n ERR_clear_error();\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n y_bit = (y_bit != 0);\n BN_CTX_start(ctx);\n tmp1 = BN_CTX_get(ctx);\n tmp2 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_nnmod(x, x_, group->field, ctx))\n goto err;\n if (group->meth->field_decode == 0) {\n if (!group->meth->field_sqr(group, tmp2, x_, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))\n goto err;\n } else {\n if (!BN_mod_sqr(tmp2, x_, group->field, ctx))\n goto err;\n if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))\n goto err;\n }\n if (group->a_is_minus3) {\n if (!BN_mod_lshift1_quick(tmp2, x, group->field))\n goto err;\n if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))\n goto err;\n if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->a, ctx))\n goto err;\n if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))\n goto err;\n } else {\n if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))\n goto err;\n }\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n }\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->b, ctx))\n goto err;\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))\n goto err;\n }\n if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {\n unsigned long err = ERR_peek_last_error();\n if (ERR_GET_LIB(err) == ERR_LIB_BN\n && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {\n ERR_clear_error();\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n } else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_BN_LIB);\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n if (BN_is_zero(y)) {\n int kron;\n kron = BN_kronecker(x, group->field, ctx);\n if (kron == -2)\n goto err;\n if (kron == 1)\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSION_BIT);\n else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n goto err;\n }\n if (!BN_usub(y, group->field, y))\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n 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}', 'BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *ret = in;\n int err = 1;\n int r;\n BIGNUM *A, *b, *q, *t, *x, *y;\n int e, i, j;\n if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {\n if (BN_abs_is_word(p, 2)) {\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {\n if (ret != in)\n BN_free(ret);\n return NULL;\n }\n bn_check_top(ret);\n return ret;\n }\n BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n return NULL;\n }\n if (BN_is_zero(a) || BN_is_one(a)) {\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_set_word(ret, BN_is_one(a))) {\n if (ret != in)\n BN_free(ret);\n return NULL;\n }\n bn_check_top(ret);\n return ret;\n }\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto end;\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_nnmod(A, a, p, ctx))\n goto end;\n e = 1;\n while (!BN_is_bit_set(p, e))\n e++;\n if (e == 1) {\n if (!BN_rshift(q, p, 2))\n goto end;\n q->neg = 0;\n if (!BN_add_word(q, 1))\n goto end;\n if (!BN_mod_exp(ret, A, q, p, ctx))\n goto end;\n err = 0;\n goto vrfy;\n }\n if (e == 2) {\n if (!BN_mod_lshift1_quick(t, A, p))\n goto end;\n if (!BN_rshift(q, p, 3))\n goto end;\n q->neg = 0;\n if (!BN_mod_exp(b, t, q, p, ctx))\n goto end;\n if (!BN_mod_sqr(y, b, p, ctx))\n goto end;\n if (!BN_mod_mul(t, t, y, p, ctx))\n goto end;\n if (!BN_sub_word(t, 1))\n goto end;\n if (!BN_mod_mul(x, A, b, p, ctx))\n goto end;\n if (!BN_mod_mul(x, x, t, p, ctx))\n goto end;\n if (!BN_copy(ret, x))\n goto end;\n err = 0;\n goto vrfy;\n }\n if (!BN_copy(q, p))\n goto end;\n q->neg = 0;\n i = 2;\n do {\n if (i < 22) {\n if (!BN_set_word(y, i))\n goto end;\n } else {\n if (!BN_priv_rand(y, BN_num_bits(p), 0, 0))\n goto end;\n if (BN_ucmp(y, p) >= 0) {\n if (!(p->neg ? BN_add : BN_sub) (y, y, p))\n goto end;\n }\n if (BN_is_zero(y))\n if (!BN_set_word(y, i))\n goto end;\n }\n r = BN_kronecker(y, q, ctx);\n if (r < -1)\n goto end;\n if (r == 0) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n goto end;\n }\n }\n while (r == 1 && ++i < 82);\n if (r != -1) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);\n goto end;\n }\n if (!BN_rshift(q, q, e))\n goto end;\n if (!BN_mod_exp(y, y, q, p, ctx))\n goto end;\n if (BN_is_one(y)) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n goto end;\n }\n if (!BN_rshift1(t, q))\n goto end;\n if (BN_is_zero(t)) {\n if (!BN_nnmod(t, A, p, ctx))\n goto end;\n if (BN_is_zero(t)) {\n BN_zero(ret);\n err = 0;\n goto end;\n } else if (!BN_one(x))\n goto end;\n } else {\n if (!BN_mod_exp(x, A, t, p, ctx))\n goto end;\n if (BN_is_zero(x)) {\n BN_zero(ret);\n err = 0;\n goto end;\n }\n }\n if (!BN_mod_sqr(b, x, p, ctx))\n goto end;\n if (!BN_mod_mul(b, b, A, p, ctx))\n goto end;\n if (!BN_mod_mul(x, x, A, p, ctx))\n goto end;\n while (1) {\n if (BN_is_one(b)) {\n if (!BN_copy(ret, x))\n goto end;\n err = 0;\n goto vrfy;\n }\n i = 1;\n if (!BN_mod_sqr(t, b, p, ctx))\n goto end;\n while (!BN_is_one(t)) {\n i++;\n if (i == e) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);\n goto end;\n }\n if (!BN_mod_mul(t, t, t, p, ctx))\n goto end;\n }\n if (!BN_copy(t, y))\n goto end;\n for (j = e - i - 1; j > 0; j--) {\n if (!BN_mod_sqr(t, t, p, ctx))\n goto end;\n }\n if (!BN_mod_mul(y, t, t, p, ctx))\n goto end;\n if (!BN_mod_mul(x, x, t, p, ctx))\n goto end;\n if (!BN_mod_mul(b, b, y, p, ctx))\n goto end;\n e = i;\n }\n vrfy:\n if (!err) {\n if (!BN_mod_sqr(x, ret, p, ctx))\n err = 1;\n if (!err && 0 != BN_cmp(x, A)) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);\n err = 1;\n }\n }\n end:\n if (err) {\n if (ret != in)\n BN_clear_free(ret);\n ret = NULL;\n }\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)\n{\n return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));\n}', 'int BN_is_bit_set(const BIGNUM *a, int n)\n{\n int i, j;\n bn_check_top(a);\n if (n < 0)\n return 0;\n i = n / BN_BITS2;\n j = n % BN_BITS2;\n if (a->top <= i)\n return 0;\n return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));\n}', 'int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, j, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l, tmp;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n rb = n % BN_BITS2;\n lb = BN_BITS2 - rb;\n if (nw >= a->top || a->top == 0) {\n BN_zero(r);\n return 1;\n }\n i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;\n if (r != a) {\n if (bn_wexpand(r, i) == NULL)\n return 0;\n r->neg = a->neg;\n } else {\n if (n == 0)\n return 1;\n }\n f = &(a->d[nw]);\n t = r->d;\n j = a->top - nw;\n r->top = i;\n if (rb == 0) {\n for (i = j; i != 0; i--)\n *(t++) = *(f++);\n } else {\n l = *(f++);\n for (i = j - 1; i != 0; i--) {\n tmp = (l >> rb) & BN_MASK2;\n l = *(f++);\n *(t++) = (tmp | (l << lb)) & BN_MASK2;\n }\n if ((l = (l >> rb) & BN_MASK2))\n *(t) = l;\n }\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_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 d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (BN_is_zero(aa)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n bn_correct_top(r);\n } else\n#endif\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue, 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_montgomery(&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_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits >= 0) {\n if (bits < stride)\n stride = bits + 1;\n bits -= stride;\n wvalue = bn_get_bits(p, bits + 1);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7)\n while (bits >= 0) {\n for (wvalue = 0, i = 0; i < 5; i++, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n } else {\n while (bits >= 0) {\n wvalue = bn_get_bits5(p->d, bits - 4);\n bits -= 5;\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n 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_mod_mul_montgomery(&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_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top,\n unsigned char *buf, int idx,\n int window)\n{\n int i, j;\n int width = 1 << window;\n volatile BN_ULONG *table = (volatile BN_ULONG *)buf;\n if (bn_wexpand(b, top) == NULL)\n return 0;\n if (window <= 3) {\n for (i = 0; i < top; i++, table += width) {\n BN_ULONG acc = 0;\n for (j = 0; j < width; j++) {\n acc |= table[j] &\n ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));\n }\n b->d[i] = acc;\n }\n } else {\n int xstride = 1 << (window - 2);\n BN_ULONG y0, y1, y2, y3;\n i = idx >> (window - 2);\n idx &= xstride - 1;\n y0 = (BN_ULONG)0 - (constant_time_eq_int(i,0)&1);\n y1 = (BN_ULONG)0 - (constant_time_eq_int(i,1)&1);\n y2 = (BN_ULONG)0 - (constant_time_eq_int(i,2)&1);\n y3 = (BN_ULONG)0 - (constant_time_eq_int(i,3)&1);\n for (i = 0; i < top; i++, table += width) {\n BN_ULONG acc = 0;\n for (j = 0; j < xstride; j++) {\n acc |= ( (table[j + 0 * xstride] & y0) |\n (table[j + 1 * xstride] & y1) |\n (table[j + 2 * xstride] & y2) |\n (table[j + 3 * xstride] & y3) )\n & ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));\n }\n b->d[i] = acc;\n }\n }\n b->top = top;\n bn_correct_top(b);\n return 1;\n}', 'void bn_correct_top(BIGNUM *a)\n{\n BN_ULONG *ftl;\n int tmp_top = a->top;\n if (tmp_top > 0) {\n for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {\n ftl--;\n if (*ftl != 0)\n break;\n }\n a->top = tmp_top;\n }\n if (a->top == 0)\n a->neg = 0;\n bn_pollute(a);\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 == 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}'] |
2,212 | 0 | https://github.com/openssl/openssl/blob/b90506e995d44dee0ef4dd0324b56b59154256c2/ssl/packet.c/#L46 | 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->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 = (unsigned char *)pkt->buf->data + pkt->curr;
return 1;
} | ['int tls_construct_next_proto(SSL *s, WPACKET *pkt)\n{\n size_t len, padding_len;\n unsigned char *padding = NULL;\n len = s->next_proto_negotiated_len;\n padding_len = 32 - ((len + 2) % 32);\n if (!WPACKET_sub_memcpy_u8(pkt, s->next_proto_negotiated, len)\n || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_NEXT_PROTO, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n memset(padding, 0, padding_len);\n return 1;\n err:\n ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);\n return 0;\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_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 - (unsigned char *)pkt->buf->data;\n return 1;\n}', 'int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,\n unsigned char **allocbytes, size_t lenbytes)\n{\n if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)\n || !WPACKET_allocate_bytes(pkt, len, allocbytes)\n || !WPACKET_close(pkt))\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->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 = (unsigned char *)pkt->buf->data + pkt->curr;\n return 1;\n}'] |
2,213 | 0 | https://github.com/libav/libav/blob/452a398fd6bdca3f301c5c8af3bc241bc16a777e/libavcodec/motion_est_template.c/#L806 | static int full_search(MpegEncContext * s, int *best, int dmin,
int src_index, int ref_index, int const penalty_factor,
int size, int h, int flags)
{
MotionEstContext * const c= &s->me;
me_cmp_func cmpf, chroma_cmpf;
LOAD_COMMON
LOAD_COMMON2
int map_generation= c->map_generation;
int x,y, d;
const int dia_size= c->dia_size&0xFF;
cmpf= s->dsp.me_cmp[size];
chroma_cmpf= s->dsp.me_cmp[size+1];
for(y=FFMAX(-dia_size, ymin); y<=FFMIN(dia_size,ymax); y++){
for(x=FFMAX(-dia_size, xmin); x<=FFMIN(dia_size,xmax); x++){
CHECK_MV(x, y);
}
}
x= best[0];
y= best[1];
d= dmin;
CHECK_CLIPPED_MV(x , y);
CHECK_CLIPPED_MV(x+1, y);
CHECK_CLIPPED_MV(x, y+1);
CHECK_CLIPPED_MV(x-1, y);
CHECK_CLIPPED_MV(x, y-1);
best[0]= x;
best[1]= y;
return d;
} | ['static int full_search(MpegEncContext * s, int *best, int dmin,\n int src_index, int ref_index, int const penalty_factor,\n int size, int h, int flags)\n{\n MotionEstContext * const c= &s->me;\n me_cmp_func cmpf, chroma_cmpf;\n LOAD_COMMON\n LOAD_COMMON2\n int map_generation= c->map_generation;\n int x,y, d;\n const int dia_size= c->dia_size&0xFF;\n cmpf= s->dsp.me_cmp[size];\n chroma_cmpf= s->dsp.me_cmp[size+1];\n for(y=FFMAX(-dia_size, ymin); y<=FFMIN(dia_size,ymax); y++){\n for(x=FFMAX(-dia_size, xmin); x<=FFMIN(dia_size,xmax); x++){\n CHECK_MV(x, y);\n }\n }\n x= best[0];\n y= best[1];\n d= dmin;\n CHECK_CLIPPED_MV(x , y);\n CHECK_CLIPPED_MV(x+1, y);\n CHECK_CLIPPED_MV(x, y+1);\n CHECK_CLIPPED_MV(x-1, y);\n CHECK_CLIPPED_MV(x, y-1);\n best[0]= x;\n best[1]= y;\n return d;\n}'] |
2,214 | 0 | https://github.com/libav/libav/blob/1efa772e20be5869817b2370a557bb14e7ce2fff/libavformat/mov.c/#L275 | static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
int64_t total_size = 0;
MOVAtom a;
int i;
if (atom.size < 0)
atom.size = INT64_MAX;
while (total_size + 8 < atom.size && !url_feof(pb)) {
int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
a.size = atom.size;
a.type=0;
if(atom.size >= 8) {
a.size = get_be32(pb);
a.type = get_le32(pb);
}
av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
total_size += 8;
if (a.size == 1) {
a.size = get_be64(pb) - 8;
total_size += 8;
}
if (a.size == 0) {
a.size = atom.size - total_size;
if (a.size <= 8)
break;
}
a.size -= 8;
if(a.size < 0)
break;
a.size = FFMIN(a.size, atom.size - total_size);
for (i = 0; mov_default_parse_table[i].type; i++)
if (mov_default_parse_table[i].type == a.type) {
parse = mov_default_parse_table[i].parse;
break;
}
if (!parse && (atom.type == MKTAG('u','d','t','a') ||
atom.type == MKTAG('i','l','s','t')))
parse = mov_read_udta_string;
if (!parse) {
url_fskip(pb, a.size);
} else {
int64_t start_pos = url_ftell(pb);
int64_t left;
int err = parse(c, pb, a);
if (err < 0)
return err;
if (c->found_moov && c->found_mdat &&
(url_is_streamed(pb) || start_pos + a.size == url_fsize(pb)))
return 0;
left = a.size - url_ftell(pb) + start_pos;
if (left > 0)
url_fskip(pb, left);
}
total_size += a.size;
}
if (total_size < atom.size && atom.size < 0x7ffff)
url_fskip(pb, atom.size - total_size);
return 0;
} | ['static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)\n{\n int64_t total_size = 0;\n MOVAtom a;\n int i;\n if (atom.size < 0)\n atom.size = INT64_MAX;\n while (total_size + 8 < atom.size && !url_feof(pb)) {\n int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;\n a.size = atom.size;\n a.type=0;\n if(atom.size >= 8) {\n a.size = get_be32(pb);\n a.type = get_le32(pb);\n }\n av_dlog(c->fc, "type: %08x \'%.4s\' parent:\'%.4s\' sz: %"PRId64" %"PRId64" %"PRId64"\\n",\n a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);\n total_size += 8;\n if (a.size == 1) {\n a.size = get_be64(pb) - 8;\n total_size += 8;\n }\n if (a.size == 0) {\n a.size = atom.size - total_size;\n if (a.size <= 8)\n break;\n }\n a.size -= 8;\n if(a.size < 0)\n break;\n a.size = FFMIN(a.size, atom.size - total_size);\n for (i = 0; mov_default_parse_table[i].type; i++)\n if (mov_default_parse_table[i].type == a.type) {\n parse = mov_default_parse_table[i].parse;\n break;\n }\n if (!parse && (atom.type == MKTAG(\'u\',\'d\',\'t\',\'a\') ||\n atom.type == MKTAG(\'i\',\'l\',\'s\',\'t\')))\n parse = mov_read_udta_string;\n if (!parse) {\n url_fskip(pb, a.size);\n } else {\n int64_t start_pos = url_ftell(pb);\n int64_t left;\n int err = parse(c, pb, a);\n if (err < 0)\n return err;\n if (c->found_moov && c->found_mdat &&\n (url_is_streamed(pb) || start_pos + a.size == url_fsize(pb)))\n return 0;\n left = a.size - url_ftell(pb) + start_pos;\n if (left > 0)\n url_fskip(pb, left);\n }\n total_size += a.size;\n }\n if (total_size < atom.size && atom.size < 0x7ffff)\n url_fskip(pb, atom.size - total_size);\n return 0;\n}'] |
2,215 | 0 | https://github.com/openssl/openssl/blob/55525742f4c2bf416013fc3a75ec642775d97f80/crypto/bn/bn_mont.c/#L281 | static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
{
BIGNUM *n;
BN_ULONG *ap,*np,*rp,n0,v,*nrp;
int al,nl,max,i,x,ri;
n= &(mont->N);
al=ri=mont->ri/BN_BITS2;
nl=n->top;
if ((al == 0) || (nl == 0)) { ret->top=0; return(1); }
max=(nl+al+1);
if (bn_wexpand(r,max) == NULL) return(0);
r->neg^=n->neg;
np=n->d;
rp=r->d;
nrp= &(r->d[nl]);
#if 1
for (i=r->top; i<max; i++)
r->d[i]=0;
#else
memset(&(r->d[r->top]),0,(max-r->top)*sizeof(BN_ULONG));
#endif
r->top=max;
n0=mont->n0[0];
#ifdef BN_COUNT
fprintf(stderr,"word BN_from_montgomery_word %d * %d\n",nl,nl);
#endif
for (i=0; i<nl; i++)
{
#ifdef __TANDEM
{
long long t1;
long long t2;
long long t3;
t1 = rp[0] * (n0 & 0177777);
t2 = 037777600000l;
t2 = n0 & t2;
t3 = rp[0] & 0177777;
t2 = (t3 * t2) & BN_MASK2;
t1 = t1 + t2;
v=bn_mul_add_words(rp,np,nl,(BN_ULONG) t1);
}
#else
v=bn_mul_add_words(rp,np,nl,(rp[0]*n0)&BN_MASK2);
#endif
nrp++;
rp++;
if (((nrp[-1]+=v)&BN_MASK2) >= v)
continue;
else
{
if (((++nrp[0])&BN_MASK2) != 0) continue;
if (((++nrp[1])&BN_MASK2) != 0) continue;
for (x=2; (((++nrp[x])&BN_MASK2) == 0); x++) ;
}
}
bn_correct_top(r);
if (r->top < ri)
{
ret->top=0;
return(1);
}
al=r->top-ri;
#define BRANCH_FREE 1
#if BRANCH_FREE
if (bn_wexpand(ret,ri) == NULL) return(0);
x=0-(((al-ri)>>(sizeof(al)*8-1))&1);
ret->top=x=(ri&~x)|(al&x);
ret->neg=r->neg;
rp=ret->d;
ap=&(r->d[ri]);
nrp=ap;
if ((mont->N.d[ri-1]>>(BN_BITS2-2))!=0)
{
size_t m1,m2;
v=bn_sub_words(rp,ap,mont->N.d,ri);
m1=0-(size_t)(((al-ri)>>(sizeof(al)*8-1))&1);
m2=0-(size_t)(((ri-al)>>(sizeof(al)*8-1))&1);
m1|=m2;
m1|=(0-(size_t)v);
m1&=~m2;
nrp=(BN_ULONG *)(((size_t)rp&~m1)|((size_t)ap&m1));
}
for (i=0,ri-=4; i<ri; i+=4)
{
BN_ULONG t1,t2,t3,t4;
t1=nrp[i+0];
t2=nrp[i+1];
t3=nrp[i+2]; ap[i+0]=0;
t4=nrp[i+3]; ap[i+1]=0;
rp[i+0]=t1; ap[i+2]=0;
rp[i+1]=t2; ap[i+3]=0;
rp[i+2]=t3;
rp[i+3]=t4;
}
for (ri+=4; i<ri; i++)
rp[i]=nrp[i], ap[i]=0;
#else
if (bn_wexpand(ret,al) == NULL) return(0);
ret->top=al;
ret->neg=r->neg;
rp=ret->d;
ap=&(r->d[ri]);
al-=4;
for (i=0; i<al; i+=4)
{
BN_ULONG t1,t2,t3,t4;
t1=ap[i+0];
t2=ap[i+1];
t3=ap[i+2];
t4=ap[i+3];
rp[i+0]=t1;
rp[i+1]=t2;
rp[i+2]=t3;
rp[i+3]=t4;
}
al+=4;
for (; i<al; i++)
rp[i]=ap[i];
if (BN_ucmp(ret, &(mont->N)) >= 0)
{
if (!BN_usub(ret,ret,&(mont->N))) return(0);
}
#endif
bn_check_top(ret);
return(1);
} | ['static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)\n\t{\n\tBIGNUM *n;\n\tBN_ULONG *ap,*np,*rp,n0,v,*nrp;\n\tint al,nl,max,i,x,ri;\n\tn= &(mont->N);\n\tal=ri=mont->ri/BN_BITS2;\n\tnl=n->top;\n\tif ((al == 0) || (nl == 0)) { ret->top=0; return(1); }\n\tmax=(nl+al+1);\n\tif (bn_wexpand(r,max) == NULL) return(0);\n\tr->neg^=n->neg;\n\tnp=n->d;\n\trp=r->d;\n\tnrp= &(r->d[nl]);\n#if 1\n\tfor (i=r->top; i<max; i++)\n\t\tr->d[i]=0;\n#else\n\tmemset(&(r->d[r->top]),0,(max-r->top)*sizeof(BN_ULONG));\n#endif\n\tr->top=max;\n\tn0=mont->n0[0];\n#ifdef BN_COUNT\n\tfprintf(stderr,"word BN_from_montgomery_word %d * %d\\n",nl,nl);\n#endif\n\tfor (i=0; i<nl; i++)\n\t\t{\n#ifdef __TANDEM\n {\n long long t1;\n long long t2;\n long long t3;\n t1 = rp[0] * (n0 & 0177777);\n t2 = 037777600000l;\n t2 = n0 & t2;\n t3 = rp[0] & 0177777;\n t2 = (t3 * t2) & BN_MASK2;\n t1 = t1 + t2;\n v=bn_mul_add_words(rp,np,nl,(BN_ULONG) t1);\n }\n#else\n\t\tv=bn_mul_add_words(rp,np,nl,(rp[0]*n0)&BN_MASK2);\n#endif\n\t\tnrp++;\n\t\trp++;\n\t\tif (((nrp[-1]+=v)&BN_MASK2) >= v)\n\t\t\tcontinue;\n\t\telse\n\t\t\t{\n\t\t\tif (((++nrp[0])&BN_MASK2) != 0) continue;\n\t\t\tif (((++nrp[1])&BN_MASK2) != 0) continue;\n\t\t\tfor (x=2; (((++nrp[x])&BN_MASK2) == 0); x++) ;\n\t\t\t}\n\t\t}\n\tbn_correct_top(r);\n\tif (r->top < ri)\n\t\t{\n\t\tret->top=0;\n\t\treturn(1);\n\t\t}\n\tal=r->top-ri;\n#define BRANCH_FREE 1\n#if BRANCH_FREE\n\tif (bn_wexpand(ret,ri) == NULL) return(0);\n\tx=0-(((al-ri)>>(sizeof(al)*8-1))&1);\n\tret->top=x=(ri&~x)|(al&x);\n\tret->neg=r->neg;\n\trp=ret->d;\n\tap=&(r->d[ri]);\n\tnrp=ap;\n\tif ((mont->N.d[ri-1]>>(BN_BITS2-2))!=0)\n\t\t{\n\t\tsize_t m1,m2;\n\t\tv=bn_sub_words(rp,ap,mont->N.d,ri);\n\t\tm1=0-(size_t)(((al-ri)>>(sizeof(al)*8-1))&1);\n\t\tm2=0-(size_t)(((ri-al)>>(sizeof(al)*8-1))&1);\n\t\tm1|=m2;\n\t\tm1|=(0-(size_t)v);\n\t\tm1&=~m2;\n\t\tnrp=(BN_ULONG *)(((size_t)rp&~m1)|((size_t)ap&m1));\n\t\t}\n\tfor (i=0,ri-=4; i<ri; i+=4)\n\t\t{\n\t\tBN_ULONG t1,t2,t3,t4;\n\t\tt1=nrp[i+0];\n\t\tt2=nrp[i+1];\n\t\tt3=nrp[i+2];\tap[i+0]=0;\n\t\tt4=nrp[i+3];\tap[i+1]=0;\n\t\trp[i+0]=t1;\tap[i+2]=0;\n\t\trp[i+1]=t2;\tap[i+3]=0;\n\t\trp[i+2]=t3;\n\t\trp[i+3]=t4;\n\t\t}\n\tfor (ri+=4; i<ri; i++)\n\t\trp[i]=nrp[i], ap[i]=0;\n#else\n\tif (bn_wexpand(ret,al) == NULL) return(0);\n\tret->top=al;\n\tret->neg=r->neg;\n\trp=ret->d;\n\tap=&(r->d[ri]);\n\tal-=4;\n\tfor (i=0; i<al; i+=4)\n\t\t{\n\t\tBN_ULONG t1,t2,t3,t4;\n\t\tt1=ap[i+0];\n\t\tt2=ap[i+1];\n\t\tt3=ap[i+2];\n\t\tt4=ap[i+3];\n\t\trp[i+0]=t1;\n\t\trp[i+1]=t2;\n\t\trp[i+2]=t3;\n\t\trp[i+3]=t4;\n\t\t}\n\tal+=4;\n\tfor (; i<al; i++)\n\t\trp[i]=ap[i];\n\tif (BN_ucmp(ret, &(mont->N)) >= 0)\n\t\t{\n\t\tif (!BN_usub(ret,ret,&(mont->N))) return(0);\n\t\t}\n#endif\n\tbn_check_top(ret);\n\treturn(1);\n\t}'] |
2,216 | 0 | https://github.com/libav/libav/blob/a082ac412520cd5d812bd57e5ccdad2af557125b/libavcodec/proresenc.c/#L558 | static int find_slice_quant(AVCodecContext *avctx, const AVFrame *pic,
int trellis_node, int x, int y, int mbs_per_slice)
{
ProresContext *ctx = avctx->priv_data;
int i, q, pq, xp, yp;
const uint16_t *src;
int slice_width_factor = av_log2(mbs_per_slice);
int num_cblocks[MAX_PLANES], pwidth;
int plane_factor[MAX_PLANES], is_chroma[MAX_PLANES];
const int min_quant = ctx->profile_info->min_quant;
const int max_quant = ctx->profile_info->max_quant;
int error, bits, bits_limit;
int mbs, prev, cur, new_score;
int slice_bits[TRELLIS_WIDTH], slice_score[TRELLIS_WIDTH];
mbs = x + mbs_per_slice;
for (i = 0; i < ctx->num_planes; i++) {
is_chroma[i] = (i == 1 || i == 2);
plane_factor[i] = slice_width_factor + 2;
if (is_chroma[i])
plane_factor[i] += ctx->chroma_factor - 3;
if (!is_chroma[i] || ctx->chroma_factor == CFACTOR_Y444) {
xp = x << 4;
yp = y << 4;
num_cblocks[i] = 4;
pwidth = avctx->width;
} else {
xp = x << 3;
yp = y << 4;
num_cblocks[i] = 2;
pwidth = avctx->width >> 1;
}
src = (const uint16_t*)(pic->data[i] + yp * pic->linesize[i]) + xp;
get_slice_data(ctx, src, pic->linesize[i], xp, yp,
pwidth, avctx->height, ctx->blocks[i],
mbs_per_slice, num_cblocks[i]);
}
for (q = min_quant; q <= max_quant; q++) {
ctx->nodes[trellis_node + q].prev_node = -1;
ctx->nodes[trellis_node + q].quant = q;
}
for (q = min_quant; q <= max_quant; q++) {
bits = 0;
error = 0;
for (i = 0; i < ctx->num_planes; i++) {
bits += estimate_slice_plane(ctx, &error, i,
src, pic->linesize[i],
mbs_per_slice,
num_cblocks[i], plane_factor[i],
ctx->quants[q]);
}
if (bits > 65000 * 8) {
error = SCORE_LIMIT;
break;
}
slice_bits[q] = bits;
slice_score[q] = error;
}
bits_limit = mbs * ctx->bits_per_mb;
for (pq = min_quant; pq <= max_quant; pq++) {
prev = trellis_node - TRELLIS_WIDTH + pq;
for (q = min_quant; q <= max_quant; q++) {
cur = trellis_node + q;
bits = ctx->nodes[prev].bits + slice_bits[q];
error = slice_score[q];
if (bits > bits_limit)
error = SCORE_LIMIT;
if (ctx->nodes[prev].score < SCORE_LIMIT && error < SCORE_LIMIT)
new_score = ctx->nodes[prev].score + error;
else
new_score = SCORE_LIMIT;
if (ctx->nodes[cur].prev_node == -1 ||
ctx->nodes[cur].score >= new_score) {
ctx->nodes[cur].bits = bits;
ctx->nodes[cur].score = new_score;
ctx->nodes[cur].prev_node = prev;
}
}
}
error = ctx->nodes[trellis_node + min_quant].score;
pq = trellis_node + min_quant;
for (q = min_quant + 1; q <= max_quant; q++) {
if (ctx->nodes[trellis_node + q].score <= error) {
error = ctx->nodes[trellis_node + q].score;
pq = trellis_node + q;
}
}
return pq;
} | ['static int find_slice_quant(AVCodecContext *avctx, const AVFrame *pic,\n int trellis_node, int x, int y, int mbs_per_slice)\n{\n ProresContext *ctx = avctx->priv_data;\n int i, q, pq, xp, yp;\n const uint16_t *src;\n int slice_width_factor = av_log2(mbs_per_slice);\n int num_cblocks[MAX_PLANES], pwidth;\n int plane_factor[MAX_PLANES], is_chroma[MAX_PLANES];\n const int min_quant = ctx->profile_info->min_quant;\n const int max_quant = ctx->profile_info->max_quant;\n int error, bits, bits_limit;\n int mbs, prev, cur, new_score;\n int slice_bits[TRELLIS_WIDTH], slice_score[TRELLIS_WIDTH];\n mbs = x + mbs_per_slice;\n for (i = 0; i < ctx->num_planes; i++) {\n is_chroma[i] = (i == 1 || i == 2);\n plane_factor[i] = slice_width_factor + 2;\n if (is_chroma[i])\n plane_factor[i] += ctx->chroma_factor - 3;\n if (!is_chroma[i] || ctx->chroma_factor == CFACTOR_Y444) {\n xp = x << 4;\n yp = y << 4;\n num_cblocks[i] = 4;\n pwidth = avctx->width;\n } else {\n xp = x << 3;\n yp = y << 4;\n num_cblocks[i] = 2;\n pwidth = avctx->width >> 1;\n }\n src = (const uint16_t*)(pic->data[i] + yp * pic->linesize[i]) + xp;\n get_slice_data(ctx, src, pic->linesize[i], xp, yp,\n pwidth, avctx->height, ctx->blocks[i],\n mbs_per_slice, num_cblocks[i]);\n }\n for (q = min_quant; q <= max_quant; q++) {\n ctx->nodes[trellis_node + q].prev_node = -1;\n ctx->nodes[trellis_node + q].quant = q;\n }\n for (q = min_quant; q <= max_quant; q++) {\n bits = 0;\n error = 0;\n for (i = 0; i < ctx->num_planes; i++) {\n bits += estimate_slice_plane(ctx, &error, i,\n src, pic->linesize[i],\n mbs_per_slice,\n num_cblocks[i], plane_factor[i],\n ctx->quants[q]);\n }\n if (bits > 65000 * 8) {\n error = SCORE_LIMIT;\n break;\n }\n slice_bits[q] = bits;\n slice_score[q] = error;\n }\n bits_limit = mbs * ctx->bits_per_mb;\n for (pq = min_quant; pq <= max_quant; pq++) {\n prev = trellis_node - TRELLIS_WIDTH + pq;\n for (q = min_quant; q <= max_quant; q++) {\n cur = trellis_node + q;\n bits = ctx->nodes[prev].bits + slice_bits[q];\n error = slice_score[q];\n if (bits > bits_limit)\n error = SCORE_LIMIT;\n if (ctx->nodes[prev].score < SCORE_LIMIT && error < SCORE_LIMIT)\n new_score = ctx->nodes[prev].score + error;\n else\n new_score = SCORE_LIMIT;\n if (ctx->nodes[cur].prev_node == -1 ||\n ctx->nodes[cur].score >= new_score) {\n ctx->nodes[cur].bits = bits;\n ctx->nodes[cur].score = new_score;\n ctx->nodes[cur].prev_node = prev;\n }\n }\n }\n error = ctx->nodes[trellis_node + min_quant].score;\n pq = trellis_node + min_quant;\n for (q = min_quant + 1; q <= max_quant; q++) {\n if (ctx->nodes[trellis_node + q].score <= error) {\n error = ctx->nodes[trellis_node + q].score;\n pq = trellis_node + q;\n }\n }\n return pq;\n}'] |
2,217 | 0 | https://gitlab.com/libtiff/libtiff/blob/69ce2652ef2feae25a4569eb57b837dde0a1bd71/libtiff/tif_dirread.c/#L6192 | static int _TIFFFetchStrileValue(TIFF* tif,
uint32 strile,
TIFFDirEntry* dirent,
uint64** parray)
{
static const char module[] = "_TIFFFetchStrileValue";
TIFFDirectory *td = &tif->tif_dir;
if( strile >= dirent->tdir_count )
{
return 0;
}
if( strile >= td->td_stripoffsetbyteallocsize )
{
uint32 nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;
uint32 nStripArrayAllocNew;
uint64 nArraySize64;
size_t nArraySize;
uint64* offsetArray;
uint64* bytecountArray;
if( strile > 1000000 )
{
uint64 filesize = TIFFGetFileSize(tif);
if( strile > filesize / sizeof(uint32) )
{
TIFFErrorExt(tif->tif_clientdata, module, "File too short");
return 0;
}
}
if( td->td_stripoffsetbyteallocsize == 0 &&
td->td_nstrips < 1024 * 1024 )
{
nStripArrayAllocNew = td->td_nstrips;
}
else
{
#define TIFF_MAX(a,b) (((a)>(b)) ? (a) : (b))
#define TIFF_MIN(a,b) (((a)<(b)) ? (a) : (b))
nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U );
if( nStripArrayAllocNew < 0xFFFFFFFFU / 2 )
nStripArrayAllocNew *= 2;
nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);
}
assert( strile < nStripArrayAllocNew );
nArraySize64 = (uint64)sizeof(uint64) * nStripArrayAllocNew;
nArraySize = (size_t)(nArraySize64);
#if SIZEOF_SIZE_T == 4
if( nArraySize != nArraySize64 )
{
TIFFErrorExt(tif->tif_clientdata, module,
"Cannot allocate strip offset and bytecount arrays");
return 0;
}
#endif
offsetArray = (uint64*)(
_TIFFrealloc( td->td_stripoffset_p, nArraySize ) );
bytecountArray = (uint64*)(
_TIFFrealloc( td->td_stripbytecount_p, nArraySize ) );
if( offsetArray )
td->td_stripoffset_p = offsetArray;
if( bytecountArray )
td->td_stripbytecount_p = bytecountArray;
if( offsetArray && bytecountArray )
{
td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;
memset(td->td_stripoffset_p + nStripArrayAllocBefore,
0xFF,
(td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64) );
memset(td->td_stripbytecount_p + nStripArrayAllocBefore,
0xFF,
(td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64) );
}
else
{
TIFFErrorExt(tif->tif_clientdata, module,
"Cannot allocate strip offset and bytecount arrays");
_TIFFfree(td->td_stripoffset_p);
td->td_stripoffset_p = NULL;
_TIFFfree(td->td_stripbytecount_p);
td->td_stripbytecount_p = NULL;
td->td_stripoffsetbyteallocsize = 0;
}
}
if( *parray == NULL || strile >= td->td_stripoffsetbyteallocsize )
return 0;
if( ~((*parray)[strile]) == 0 )
{
if( !_TIFFPartialReadStripArray( tif, dirent, strile, *parray ) )
{
(*parray)[strile] = 0;
return 0;
}
}
return 1;
} | ['void TIFFBuildOverviews( TIFF *hTIFF, int nOverviews, int * panOvList,\n int bUseSubIFDs, const char *pszResampleMethod,\n int (*pfnProgress)( double, void * ),\n void * pProgressData )\n{\n TIFFOvrCache\t**papoRawBIs;\n uint32\t\tnXSize, nYSize, nBlockXSize, nBlockYSize;\n uint16\t\tnBitsPerPixel, nPhotometric, nCompressFlag, nSamples,\n nPlanarConfig, nSampleFormat;\n int bSubsampled;\n uint16 nHorSubsampling, nVerSubsampling;\n int\t\t\tbTiled, nSXOff, nSYOff, i;\n unsigned char\t*pabySrcTile;\n uint16\t\t*panRedMap, *panGreenMap, *panBlueMap;\n TIFFErrorHandler pfnWarning;\n (void) pfnProgress;\n (void) pProgressData;\n TIFFGetField( hTIFF, TIFFTAG_IMAGEWIDTH, &nXSize );\n TIFFGetField( hTIFF, TIFFTAG_IMAGELENGTH, &nYSize );\n TIFFGetField( hTIFF, TIFFTAG_BITSPERSAMPLE, &nBitsPerPixel );\n TIFFGetField( hTIFF, TIFFTAG_SAMPLESPERPIXEL, &nSamples );\n TIFFGetFieldDefaulted( hTIFF, TIFFTAG_PLANARCONFIG, &nPlanarConfig );\n TIFFGetFieldDefaulted( hTIFF, TIFFTAG_PHOTOMETRIC, &nPhotometric );\n TIFFGetFieldDefaulted( hTIFF, TIFFTAG_COMPRESSION, &nCompressFlag );\n TIFFGetFieldDefaulted( hTIFF, TIFFTAG_SAMPLEFORMAT, &nSampleFormat );\n if( nPhotometric == PHOTOMETRIC_YCBCR || nPhotometric == PHOTOMETRIC_ITULAB )\n {\n if( nBitsPerPixel != 8 || nSamples != 3 || nPlanarConfig != PLANARCONFIG_CONTIG ||\n nSampleFormat != SAMPLEFORMAT_UINT)\n {\n TIFFErrorExt( TIFFClientdata(hTIFF), "TIFFBuildOverviews",\n "File `%s\' has an unsupported subsampling configuration.\\n",\n TIFFFileName(hTIFF) );\n return;\n }\n bSubsampled = 1;\n TIFFGetField( hTIFF, TIFFTAG_YCBCRSUBSAMPLING, &nHorSubsampling, &nVerSubsampling );\n }\n else\n {\n if( nBitsPerPixel < 8 )\n {\n TIFFErrorExt( TIFFClientdata(hTIFF), "TIFFBuildOverviews",\n "File `%s\' has samples of %d bits per sample. Sample\\n"\n "sizes of less than 8 bits per sample are not supported.\\n",\n TIFFFileName(hTIFF), nBitsPerPixel );\n return;\n }\n bSubsampled = 0;\n nHorSubsampling = 1;\n nVerSubsampling = 1;\n }\n pfnWarning = TIFFSetWarningHandler( NULL );\n if( TIFFGetField( hTIFF, TIFFTAG_ROWSPERSTRIP, &(nBlockYSize) ) )\n {\n nBlockXSize = nXSize;\n bTiled = FALSE;\n }\n else\n {\n TIFFGetField( hTIFF, TIFFTAG_TILEWIDTH, &nBlockXSize );\n TIFFGetField( hTIFF, TIFFTAG_TILELENGTH, &nBlockYSize );\n bTiled = TRUE;\n }\n if( TIFFGetField( hTIFF, TIFFTAG_COLORMAP,\n &panRedMap, &panGreenMap, &panBlueMap ) )\n {\n uint16\t\t*panRed2, *panGreen2, *panBlue2;\n int nColorCount = 1 << nBitsPerPixel;\n panRed2 = (uint16 *) _TIFFmalloc(2*nColorCount);\n panGreen2 = (uint16 *) _TIFFmalloc(2*nColorCount);\n panBlue2 = (uint16 *) _TIFFmalloc(2*nColorCount);\n memcpy( panRed2, panRedMap, 2 * nColorCount );\n memcpy( panGreen2, panGreenMap, 2 * nColorCount );\n memcpy( panBlue2, panBlueMap, 2 * nColorCount );\n panRedMap = panRed2;\n panGreenMap = panGreen2;\n panBlueMap = panBlue2;\n }\n else\n {\n panRedMap = panGreenMap = panBlueMap = NULL;\n }\n papoRawBIs = (TIFFOvrCache **) _TIFFmalloc(nOverviews*sizeof(void*));\n for( i = 0; i < nOverviews; i++ )\n {\n uint32 nOXSize, nOYSize, nOBlockXSize, nOBlockYSize;\n toff_t nDirOffset;\n nOXSize = (nXSize + panOvList[i] - 1) / panOvList[i];\n nOYSize = (nYSize + panOvList[i] - 1) / panOvList[i];\n nOBlockXSize = MIN(nBlockXSize,nOXSize);\n nOBlockYSize = MIN(nBlockYSize,nOYSize);\n if( bTiled )\n {\n if( (nOBlockXSize % 16) != 0 )\n nOBlockXSize = nOBlockXSize + 16 - (nOBlockXSize % 16);\n if( (nOBlockYSize % 16) != 0 )\n nOBlockYSize = nOBlockYSize + 16 - (nOBlockYSize % 16);\n }\n nDirOffset = TIFF_WriteOverview( hTIFF, nOXSize, nOYSize,\n nBitsPerPixel, nPlanarConfig,\n nSamples, nOBlockXSize, nOBlockYSize,\n bTiled, nCompressFlag, nPhotometric,\n nSampleFormat,\n panRedMap, panGreenMap, panBlueMap,\n bUseSubIFDs,\n nHorSubsampling, nVerSubsampling );\n papoRawBIs[i] = TIFFCreateOvrCache( hTIFF, nDirOffset );\n }\n if( panRedMap != NULL )\n {\n _TIFFfree( panRedMap );\n _TIFFfree( panGreenMap );\n _TIFFfree( panBlueMap );\n }\n if( bTiled )\n pabySrcTile = (unsigned char *) _TIFFmalloc(TIFFTileSize(hTIFF));\n else\n pabySrcTile = (unsigned char *) _TIFFmalloc(TIFFStripSize(hTIFF));\n for( nSYOff = 0; nSYOff < (int) nYSize; nSYOff += nBlockYSize )\n {\n for( nSXOff = 0; nSXOff < (int) nXSize; nSXOff += nBlockXSize )\n {\n TIFF_ProcessFullResBlock( hTIFF, nPlanarConfig,\n bSubsampled,nHorSubsampling,nVerSubsampling,\n nOverviews, panOvList,\n nBitsPerPixel, nSamples, papoRawBIs,\n nSXOff, nSYOff, pabySrcTile,\n nBlockXSize, nBlockYSize,\n nSampleFormat, pszResampleMethod );\n }\n }\n _TIFFfree( pabySrcTile );\n for( i = 0; i < nOverviews; i++ )\n {\n TIFFDestroyOvrCache( papoRawBIs[i] );\n }\n if( papoRawBIs != NULL )\n _TIFFfree( papoRawBIs );\n TIFFSetWarningHandler( pfnWarning );\n}', 'uint32 TIFF_WriteOverview( TIFF *hTIFF, uint32 nXSize, uint32 nYSize,\n int nBitsPerPixel, int nPlanarConfig, int nSamples,\n int nBlockXSize, int nBlockYSize,\n int bTiled, int nCompressFlag, int nPhotometric,\n int nSampleFormat,\n unsigned short *panRed,\n unsigned short *panGreen,\n unsigned short *panBlue,\n int bUseSubIFDs,\n int nHorSubsampling, int nVerSubsampling )\n{\n toff_t\tnBaseDirOffset;\n toff_t\tnOffset;\n tdir_t\tiNumDir;\n (void) bUseSubIFDs;\n nBaseDirOffset = TIFFCurrentDirOffset( hTIFF );\n TIFFCreateDirectory( hTIFF );\n TIFFSetField( hTIFF, TIFFTAG_IMAGEWIDTH, nXSize );\n TIFFSetField( hTIFF, TIFFTAG_IMAGELENGTH, nYSize );\n if( nSamples == 1 )\n TIFFSetField( hTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );\n else\n TIFFSetField( hTIFF, TIFFTAG_PLANARCONFIG, nPlanarConfig );\n TIFFSetField( hTIFF, TIFFTAG_BITSPERSAMPLE, nBitsPerPixel );\n TIFFSetField( hTIFF, TIFFTAG_SAMPLESPERPIXEL, nSamples );\n TIFFSetField( hTIFF, TIFFTAG_COMPRESSION, nCompressFlag );\n TIFFSetField( hTIFF, TIFFTAG_PHOTOMETRIC, nPhotometric );\n TIFFSetField( hTIFF, TIFFTAG_SAMPLEFORMAT, nSampleFormat );\n if( bTiled )\n {\n TIFFSetField( hTIFF, TIFFTAG_TILEWIDTH, nBlockXSize );\n TIFFSetField( hTIFF, TIFFTAG_TILELENGTH, nBlockYSize );\n }\n else\n TIFFSetField( hTIFF, TIFFTAG_ROWSPERSTRIP, nBlockYSize );\n TIFFSetField( hTIFF, TIFFTAG_SUBFILETYPE, FILETYPE_REDUCEDIMAGE );\n if( nPhotometric == PHOTOMETRIC_YCBCR || nPhotometric == PHOTOMETRIC_ITULAB )\n {\n TIFFSetField( hTIFF, TIFFTAG_YCBCRSUBSAMPLING, nHorSubsampling, nVerSubsampling);\n }\n if( panRed != NULL )\n {\n TIFFSetField( hTIFF, TIFFTAG_COLORMAP, panRed, panGreen, panBlue );\n }\n if( TIFFWriteCheck( hTIFF, bTiled, "TIFFBuildOverviews" ) == 0 )\n return 0;\n TIFFWriteDirectory( hTIFF );\n iNumDir = TIFFNumberOfDirectories(hTIFF);\n if( iNumDir > TIFF_DIR_MAX )\n {\n TIFFErrorExt( TIFFClientdata(hTIFF),\n "TIFF_WriteOverview",\n "File `%s\' has too many directories.\\n",\n TIFFFileName(hTIFF) );\n exit(-1);\n }\n TIFFSetDirectory( hTIFF, (tdir_t) (iNumDir - 1) );\n nOffset = TIFFCurrentDirOffset( hTIFF );\n TIFFSetSubDirectory( hTIFF, nBaseDirOffset );\n return nOffset;\n}', 'int\nTIFFWriteCheck(TIFF* tif, int tiles, const char* module)\n{\n\tif (tif->tif_mode == O_RDONLY) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module, "File not open for writing");\n\t\treturn (0);\n\t}\n\tif (tiles ^ isTiled(tif)) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module, tiles ?\n\t\t "Can not write tiles to a striped image" :\n\t\t "Can not write scanlines to a tiled image");\n\t\treturn (0);\n\t}\n _TIFFFillStriles( tif );\n\tif (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "Must set \\"ImageWidth\\" before writing data");\n\t\treturn (0);\n\t}\n\tif (tif->tif_dir.td_samplesperpixel == 1) {\n\t\tif (!TIFFFieldSet(tif, FIELD_PLANARCONFIG))\n tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;\n\t} else {\n\t\tif (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Must set \\"PlanarConfiguration\\" before writing data");\n\t\t\treturn (0);\n\t\t}\n\t}\n\tif (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif)) {\n\t\ttif->tif_dir.td_nstrips = 0;\n\t\tTIFFErrorExt(tif->tif_clientdata, module, "No space for %s arrays",\n\t\t isTiled(tif) ? "tile" : "strip");\n\t\treturn (0);\n\t}\n\tif (isTiled(tif))\n\t{\n\t\ttif->tif_tilesize = TIFFTileSize(tif);\n\t\tif (tif->tif_tilesize == 0)\n\t\t\treturn (0);\n\t}\n\telse\n\t\ttif->tif_tilesize = (tmsize_t)(-1);\n\ttif->tif_scanlinesize = TIFFScanlineSize(tif);\n\tif (tif->tif_scanlinesize == 0)\n\t\treturn (0);\n\ttif->tif_flags |= TIFF_BEENWRITING;\n\treturn (1);\n}', 'TIFFOvrCache *TIFFCreateOvrCache( TIFF *hTIFF, toff_t nDirOffset )\n{\n TIFFOvrCache\t*psCache;\n toff_t\t\tnBaseDirOffset;\n psCache = (TIFFOvrCache *) _TIFFmalloc(sizeof(TIFFOvrCache));\n psCache->nDirOffset = nDirOffset;\n psCache->hTIFF = hTIFF;\n nBaseDirOffset = TIFFCurrentDirOffset( psCache->hTIFF );\n TIFFSetSubDirectory( hTIFF, nDirOffset );\n TIFFGetField( hTIFF, TIFFTAG_IMAGEWIDTH, &(psCache->nXSize) );\n TIFFGetField( hTIFF, TIFFTAG_IMAGELENGTH, &(psCache->nYSize) );\n TIFFGetField( hTIFF, TIFFTAG_BITSPERSAMPLE, &(psCache->nBitsPerPixel) );\n TIFFGetField( hTIFF, TIFFTAG_SAMPLESPERPIXEL, &(psCache->nSamples) );\n TIFFGetField( hTIFF, TIFFTAG_PLANARCONFIG, &(psCache->nPlanarConfig) );\n if( !TIFFIsTiled( hTIFF ) )\n {\n TIFFGetField( hTIFF, TIFFTAG_ROWSPERSTRIP, &(psCache->nBlockYSize) );\n psCache->nBlockXSize = psCache->nXSize;\n psCache->nBytesPerBlock = TIFFStripSize(hTIFF);\n psCache->bTiled = FALSE;\n }\n else\n {\n TIFFGetField( hTIFF, TIFFTAG_TILEWIDTH, &(psCache->nBlockXSize) );\n TIFFGetField( hTIFF, TIFFTAG_TILELENGTH, &(psCache->nBlockYSize) );\n psCache->nBytesPerBlock = TIFFTileSize(hTIFF);\n psCache->bTiled = TRUE;\n }\n psCache->nBlocksPerRow = (psCache->nXSize + psCache->nBlockXSize - 1)\n \t\t/ psCache->nBlockXSize;\n psCache->nBlocksPerColumn = (psCache->nYSize + psCache->nBlockYSize - 1)\n \t\t/ psCache->nBlockYSize;\n if (psCache->nPlanarConfig == PLANARCONFIG_SEPARATE)\n psCache->nBytesPerRow = psCache->nBytesPerBlock\n * psCache->nBlocksPerRow * psCache->nSamples;\n else\n psCache->nBytesPerRow =\n psCache->nBytesPerBlock * psCache->nBlocksPerRow;\n psCache->pabyRow1Blocks =\n (unsigned char *) _TIFFmalloc(psCache->nBytesPerRow);\n psCache->pabyRow2Blocks =\n (unsigned char *) _TIFFmalloc(psCache->nBytesPerRow);\n if ( psCache->pabyRow1Blocks == NULL\n || psCache->pabyRow2Blocks == NULL )\n {\n\t\tTIFFErrorExt( hTIFF->tif_clientdata, hTIFF->tif_name,\n\t\t\t\t\t "Can\'t allocate memory for overview cache." );\n if (psCache->pabyRow1Blocks) _TIFFfree(psCache->pabyRow1Blocks);\n if (psCache->pabyRow2Blocks) _TIFFfree(psCache->pabyRow2Blocks);\n _TIFFfree( psCache );\n return NULL;\n }\n _TIFFmemset( psCache->pabyRow1Blocks, 0, psCache->nBytesPerRow );\n _TIFFmemset( psCache->pabyRow2Blocks, 0, psCache->nBytesPerRow );\n psCache->nBlockOffset = 0;\n TIFFSetSubDirectory( psCache->hTIFF, nBaseDirOffset );\n return psCache;\n}', 'int\nTIFFSetSubDirectory(TIFF* tif, uint64 diroff)\n{\n\ttif->tif_nextdiroff = diroff;\n\ttif->tif_dirnumber = 0;\n\treturn (TIFFReadDirectory(tif));\n}', 'int\nTIFFReadDirectory(TIFF* tif)\n{\n\tstatic const char module[] = "TIFFReadDirectory";\n\tTIFFDirEntry* dir;\n\tuint16 dircount;\n\tTIFFDirEntry* dp;\n\tuint16 di;\n\tconst TIFFField* fip;\n\tuint32 fii=FAILED_FII;\n toff_t nextdiroff;\n int bitspersample_read = FALSE;\n int color_channels;\n\ttif->tif_diroff=tif->tif_nextdiroff;\n\tif (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))\n\t\treturn 0;\n\t(*tif->tif_cleanup)(tif);\n\ttif->tif_curdir++;\n nextdiroff = tif->tif_nextdiroff;\n\tdircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);\n\tif (!dircount)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,\n\t\t "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);\n\t\treturn 0;\n\t}\n\tTIFFReadDirectoryCheckOrder(tif,dir,dircount);\n\t{\n\t\tTIFFDirEntry* ma;\n\t\tuint16 mb;\n\t\tfor (ma=dir, mb=0; mb<dircount; ma++, mb++)\n\t\t{\n\t\t\tTIFFDirEntry* na;\n\t\t\tuint16 nb;\n\t\t\tfor (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)\n\t\t\t{\n\t\t\t\tif (ma->tdir_tag==na->tdir_tag)\n\t\t\t\t\tna->tdir_tag=IGNORE;\n\t\t\t}\n\t\t}\n\t}\n\ttif->tif_flags &= ~TIFF_BEENWRITING;\n\ttif->tif_flags &= ~TIFF_BUF4WRITE;\n\ttif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;\n\tTIFFFreeDirectory(tif);\n\tTIFFDefaultDirectory(tif);\n\tTIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);\n\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);\n\tif (dp)\n\t{\n\t\tif (!TIFFFetchNormalTag(tif,dp,0))\n\t\t\tgoto bad;\n\t\tdp->tdir_tag=IGNORE;\n\t}\n\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);\n\tif (dp)\n\t{\n\t\tuint16 value;\n\t\tenum TIFFReadDirEntryErr err;\n\t\terr=TIFFReadDirEntryShort(tif,dp,&value);\n\t\tif (err==TIFFReadDirEntryErrCount)\n\t\t\terr=TIFFReadDirEntryPersampleShort(tif,dp,&value);\n\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t{\n\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);\n\t\t\tgoto bad;\n\t\t}\n\t\tif (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))\n\t\t\tgoto bad;\n\t\tdp->tdir_tag=IGNORE;\n\t}\n\telse\n\t{\n\t\tif (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))\n\t\t\tgoto bad;\n\t}\n\tfor (di=0, dp=dir; di<dircount; di++, dp++)\n\t{\n\t\tif (dp->tdir_tag!=IGNORE)\n\t\t{\n\t\t\tTIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);\n\t\t\tif (fii == FAILED_FII)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t\t "Unknown field with tag %d (0x%x) encountered",\n\t\t\t\t dp->tdir_tag,dp->tdir_tag);\n\t\t\t\tif (!_TIFFMergeFields(tif,\n\t\t\t\t\t_TIFFCreateAnonField(tif,\n\t\t\t\t\t\tdp->tdir_tag,\n\t\t\t\t\t\t(TIFFDataType) dp->tdir_type),\n\t\t\t\t\t1)) {\n\t\t\t\t\tTIFFWarningExt(tif->tif_clientdata,\n\t\t\t\t\t module,\n\t\t\t\t\t "Registering anonymous field with tag %d (0x%x) failed",\n\t\t\t\t\t dp->tdir_tag,\n\t\t\t\t\t dp->tdir_tag);\n\t\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\t\t} else {\n\t\t\t\t\tTIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);\n\t\t\t\t\tassert(fii != FAILED_FII);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dp->tdir_tag!=IGNORE)\n\t\t{\n\t\t\tfip=tif->tif_fields[fii];\n\t\t\tif (fip->field_bit==FIELD_IGNORE)\n\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\telse\n\t\t\t{\n\t\t\t\tswitch (dp->tdir_tag)\n\t\t\t\t{\n\t\t\t\t\tcase TIFFTAG_STRIPOFFSETS:\n\t\t\t\t\tcase TIFFTAG_STRIPBYTECOUNTS:\n\t\t\t\t\tcase TIFFTAG_TILEOFFSETS:\n\t\t\t\t\tcase TIFFTAG_TILEBYTECOUNTS:\n\t\t\t\t\t\tTIFFSetFieldBit(tif,fip->field_bit);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase TIFFTAG_IMAGEWIDTH:\n\t\t\t\t\tcase TIFFTAG_IMAGELENGTH:\n\t\t\t\t\tcase TIFFTAG_IMAGEDEPTH:\n\t\t\t\t\tcase TIFFTAG_TILELENGTH:\n\t\t\t\t\tcase TIFFTAG_TILEWIDTH:\n\t\t\t\t\tcase TIFFTAG_TILEDEPTH:\n\t\t\t\t\tcase TIFFTAG_PLANARCONFIG:\n\t\t\t\t\tcase TIFFTAG_ROWSPERSTRIP:\n\t\t\t\t\tcase TIFFTAG_EXTRASAMPLES:\n\t\t\t\t\t\tif (!TIFFFetchNormalTag(tif,dp,0))\n\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\t\t\t\tbreak;\n default:\n if( !_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag) )\n dp->tdir_tag=IGNORE;\n break;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&\n\t (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))\n\t{\n if (!_TIFFFillStriles(tif))\n goto bad;\n\t\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);\n\t\tif ((dp!=0)&&(dp->tdir_count==1))\n\t\t{\n\t\t\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,\n\t\t\t TIFFTAG_STRIPBYTECOUNTS);\n\t\t\tif ((dp!=0)&&(dp->tdir_count==1))\n\t\t\t{\n\t\t\t\ttif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "Planarconfig tag value assumed incorrect, "\n\t\t\t\t "assuming data is contig instead of chunky");\n\t\t\t}\n\t\t}\n\t}\n\tif (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))\n\t{\n\t\tMissingRequired(tif,"ImageLength");\n\t\tgoto bad;\n\t}\n\tif (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {\n\t\ttif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);\n\t\ttif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;\n\t\ttif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;\n\t\ttif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;\n\t\ttif->tif_flags &= ~TIFF_ISTILED;\n\t} else {\n\t\ttif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);\n\t\ttif->tif_flags |= TIFF_ISTILED;\n\t}\n\tif (!tif->tif_dir.td_nstrips) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "Cannot handle zero number of %s",\n\t\t isTiled(tif) ? "tiles" : "strips");\n\t\tgoto bad;\n\t}\n\ttif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;\n\tif (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\ttif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;\n\tif (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {\n#ifdef OJPEG_SUPPORT\n\t\tif ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&\n\t\t (isTiled(tif)==0) &&\n\t\t (tif->tif_dir.td_nstrips==1)) {\n\t\t\tTIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);\n\t\t} else\n#endif\n {\n\t\t\tMissingRequired(tif,\n\t\t\t\tisTiled(tif) ? "TileOffsets" : "StripOffsets");\n\t\t\tgoto bad;\n\t\t}\n\t}\n\tfor (di=0, dp=dir; di<dircount; di++, dp++)\n\t{\n\t\tswitch (dp->tdir_tag)\n\t\t{\n\t\t\tcase IGNORE:\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_MINSAMPLEVALUE:\n\t\t\tcase TIFFTAG_MAXSAMPLEVALUE:\n\t\t\tcase TIFFTAG_BITSPERSAMPLE:\n\t\t\tcase TIFFTAG_DATATYPE:\n\t\t\tcase TIFFTAG_SAMPLEFORMAT:\n\t\t\t\t{\n\t\t\t\t\tuint16 value;\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\terr=TIFFReadDirEntryShort(tif,dp,&value);\n\t\t\t\t\tif (err==TIFFReadDirEntryErrCount)\n\t\t\t\t\t\terr=TIFFReadDirEntryPersampleShort(tif,dp,&value);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t}\n\t\t\t\t\tif (!TIFFSetField(tif,dp->tdir_tag,value))\n\t\t\t\t\t\tgoto bad;\n if( dp->tdir_tag == TIFFTAG_BITSPERSAMPLE )\n bitspersample_read = TRUE;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_SMINSAMPLEVALUE:\n\t\t\tcase TIFFTAG_SMAXSAMPLEVALUE:\n\t\t\t\t{\n\t\t\t\t\tdouble *data = NULL;\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\tuint32 saved_flags;\n\t\t\t\t\tint m;\n\t\t\t\t\tif (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)\n\t\t\t\t\t\terr = TIFFReadDirEntryErrCount;\n\t\t\t\t\telse\n\t\t\t\t\t\terr = TIFFReadDirEntryDoubleArray(tif, dp, &data);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t}\n\t\t\t\t\tsaved_flags = tif->tif_flags;\n\t\t\t\t\ttif->tif_flags |= TIFF_PERSAMPLE;\n\t\t\t\t\tm = TIFFSetField(tif,dp->tdir_tag,data);\n\t\t\t\t\ttif->tif_flags = saved_flags;\n\t\t\t\t\t_TIFFfree(data);\n\t\t\t\t\tif (!m)\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_STRIPOFFSETS:\n\t\t\tcase TIFFTAG_TILEOFFSETS:\n _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),\n dp, sizeof(TIFFDirEntry) );\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_STRIPBYTECOUNTS:\n\t\t\tcase TIFFTAG_TILEBYTECOUNTS:\n _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),\n dp, sizeof(TIFFDirEntry) );\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_COLORMAP:\n\t\t\tcase TIFFTAG_TRANSFERFUNCTION:\n\t\t\t\t{\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\tuint32 countpersample;\n\t\t\t\t\tuint32 countrequired;\n\t\t\t\t\tuint32 incrementpersample;\n\t\t\t\t\tuint16* value=NULL;\n if( !bitspersample_read )\n {\n fip = TIFFFieldWithTag(tif,dp->tdir_tag);\n TIFFWarningExt(tif->tif_clientdata,module,\n "Ignoring %s since BitsPerSample tag not found",\n fip ? fip->field_name : "unknown tagname");\n continue;\n }\n\t\t\t\t\tif (tif->tif_dir.td_bitspersample > 24)\n\t\t\t\t\t{\n\t\t\t\t\t fip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t TIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t\t\t"Ignoring %s because BitsPerSample=%d>24",\n\t\t\t\t\t\tfip ? fip->field_name : "unknown tagname",\n\t\t\t\t\t\ttif->tif_dir.td_bitspersample);\n\t\t\t\t\t continue;\n\t\t\t\t\t}\n\t\t\t\t\tcountpersample=(1U<<tif->tif_dir.td_bitspersample);\n\t\t\t\t\tif ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))\n\t\t\t\t\t{\n\t\t\t\t\t\tcountrequired=countpersample;\n\t\t\t\t\t\tincrementpersample=0;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tcountrequired=3*countpersample;\n\t\t\t\t\t\tincrementpersample=countpersample;\n\t\t\t\t\t}\n\t\t\t\t\tif (dp->tdir_count!=(uint64)countrequired)\n\t\t\t\t\t\terr=TIFFReadDirEntryErrCount;\n\t\t\t\t\telse\n\t\t\t\t\t\terr=TIFFReadDirEntryShortArray(tif,dp,&value);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n {\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);\n }\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tTIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);\n\t\t\t\t\t\t_TIFFfree(value);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_OSUBFILETYPE:\n\t\t\t\t{\n\t\t\t\t\tuint16 valueo;\n\t\t\t\t\tuint32 value;\n\t\t\t\t\tif (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tswitch (valueo)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcase OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;\n\t\t\t\t\t\t\tcase OFILETYPE_PAGE: value=FILETYPE_PAGE; break;\n\t\t\t\t\t\t\tdefault: value=0; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (value!=0)\n\t\t\t\t\t\t\tTIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\t(void) TIFFFetchNormalTag(tif, dp, TRUE);\n\t\t\t\tbreak;\n\t\t}\n\t}\n if( tif->tif_mode == O_RDWR &&\n tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0 )\n {\n TIFFSetupStrips(tif);\n }\n else if( !(tif->tif_flags&TIFF_DEFERSTRILELOAD) )\n {\n if( tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 )\n {\n if (!TIFFFetchStripThing(tif,&(tif->tif_dir.td_stripoffset_entry),\n tif->tif_dir.td_nstrips,\n &tif->tif_dir.td_stripoffset_p))\n {\n goto bad;\n }\n }\n if( tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 )\n {\n if (!TIFFFetchStripThing(tif,&(tif->tif_dir.td_stripbytecount_entry),\n tif->tif_dir.td_nstrips,\n &tif->tif_dir.td_stripbytecount_p))\n {\n goto bad;\n }\n }\n }\n\tif (tif->tif_dir.td_compression==COMPRESSION_OJPEG)\n\t{\n\t\tif (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))\n\t\t{\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Photometric tag is missing, assuming data is YCbCr");\n\t\t\tif (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))\n\t\t\t\tgoto bad;\n\t\t}\n\t\telse if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)\n\t\t{\n\t\t\ttif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Photometric tag value assumed incorrect, "\n\t\t\t "assuming data is YCbCr instead of RGB");\n\t\t}\n\t\tif (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))\n\t\t{\n\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t "BitsPerSample tag is missing, assuming 8 bits per sample");\n\t\t\tif (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))\n\t\t\t\tgoto bad;\n\t\t}\n\t\tif (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))\n\t\t{\n\t\t\tif (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "SamplesPerPixel tag is missing, "\n\t\t\t\t "assuming correct SamplesPerPixel value is 3");\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "SamplesPerPixel tag is missing, "\n\t\t\t\t "applying correct SamplesPerPixel value of 3");\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\telse if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)\n\t\t\t\t || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))\n\t\t\t{\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t}\n\t}\n color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);\n if (color_channels && tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples > color_channels) {\n uint16 old_extrasamples;\n uint16 *new_sampleinfo;\n TIFFWarningExt(tif->tif_clientdata,module, "Sum of Photometric type-related "\n "color channels and ExtraSamples doesn\'t match SamplesPerPixel. "\n "Defining non-color channels as ExtraSamples.");\n old_extrasamples = tif->tif_dir.td_extrasamples;\n tif->tif_dir.td_extrasamples = (uint16) (tif->tif_dir.td_samplesperpixel - color_channels);\n new_sampleinfo = (uint16*) _TIFFcalloc(tif->tif_dir.td_extrasamples, sizeof(uint16));\n if (!new_sampleinfo) {\n TIFFErrorExt(tif->tif_clientdata, module, "Failed to allocate memory for "\n "temporary new sampleinfo array (%d 16 bit elements)",\n tif->tif_dir.td_extrasamples);\n goto bad;\n }\n memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo, old_extrasamples * sizeof(uint16));\n _TIFFsetShortArray(&tif->tif_dir.td_sampleinfo, new_sampleinfo, tif->tif_dir.td_extrasamples);\n _TIFFfree(new_sampleinfo);\n }\n\tif (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&\n\t !TIFFFieldSet(tif, FIELD_COLORMAP)) {\n\t\tif ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)\n\t\t\ttif->tif_dir.td_photometric = PHOTOMETRIC_RGB;\n\t\telse if (tif->tif_dir.td_bitspersample>=8)\n\t\t\ttif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;\n\t\telse {\n\t\t\tMissingRequired(tif, "Colormap");\n\t\t\tgoto bad;\n\t\t}\n\t}\n\tif (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)\n\t{\n\t\tif (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {\n\t\t\tif ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&\n\t\t\t tif->tif_dir.td_nstrips > 1) ||\n\t\t\t (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&\n\t\t\t tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {\n\t\t\t MissingRequired(tif, "StripByteCounts");\n\t\t\t goto bad;\n\t\t\t}\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t\t"TIFF directory is missing required "\n\t\t\t\t"\\"StripByteCounts\\" field, calculating from imagelength");\n\t\t\tif (EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n\t\t} else if (tif->tif_dir.td_nstrips == 1\n && !(tif->tif_flags&TIFF_ISTILED)\n\t\t\t && ByteCountLooksBad(tif)) {\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Bogus \\"StripByteCounts\\" field, ignoring and calculating from imagelength");\n\t\t\tif(EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n\t\t} else if (!(tif->tif_flags&TIFF_DEFERSTRILELOAD)\n\t\t\t && tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG\n\t\t\t && tif->tif_dir.td_nstrips > 2\n\t\t\t && tif->tif_dir.td_compression == COMPRESSION_NONE\n\t\t\t && TIFFGetStrileByteCount(tif, 0) != TIFFGetStrileByteCount(tif, 1)\n\t\t\t && TIFFGetStrileByteCount(tif, 0) != 0\n\t\t\t && TIFFGetStrileByteCount(tif, 1) != 0 ) {\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Wrong \\"StripByteCounts\\" field, ignoring and calculating from imagelength");\n\t\t\tif (EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n\t\t}\n\t}\n\tif (dir)\n\t{\n\t\t_TIFFfree(dir);\n\t\tdir=NULL;\n\t}\n\tif (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))\n\t{\n\t\tif (tif->tif_dir.td_bitspersample>=16)\n\t\t\ttif->tif_dir.td_maxsamplevalue=0xFFFF;\n\t\telse\n\t\t\ttif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);\n\t}\n#ifdef STRIPBYTECOUNTSORTED_UNUSED\n\tif (!(tif->tif_flags&TIFF_DEFERSTRILELOAD) && tif->tif_dir.td_nstrips > 1) {\n\t\tuint32 strip;\n\t\ttif->tif_dir.td_stripbytecountsorted = 1;\n\t\tfor (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {\n\t\t\tif (TIFFGetStrileOffset(tif, strip - 1) >\n\t\t\t TIFFGetStrileOffset(tif, strip)) {\n\t\t\t\ttif->tif_dir.td_stripbytecountsorted = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n#endif\n\t(*tif->tif_fixuptags)(tif);\n\tif ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&\n\t (tif->tif_dir.td_nstrips==1)&&\n\t (tif->tif_dir.td_compression==COMPRESSION_NONE)&&\n\t ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))\n {\n ChopUpSingleUncompressedStrip(tif);\n }\n if( tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&\n tif->tif_dir.td_compression == COMPRESSION_NONE &&\n (tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP &&\n TIFFStripSize64(tif) > 0x7FFFFFFFUL )\n {\n TryChopUpUncompressedBigTiff(tif);\n }\n\ttif->tif_flags &= ~TIFF_DIRTYDIRECT;\n\ttif->tif_flags &= ~TIFF_DIRTYSTRIP;\n\ttif->tif_row = (uint32) -1;\n\ttif->tif_curstrip = (uint32) -1;\n\ttif->tif_col = (uint32) -1;\n\ttif->tif_curtile = (uint32) -1;\n\ttif->tif_tilesize = (tmsize_t) -1;\n\ttif->tif_scanlinesize = TIFFScanlineSize(tif);\n\tif (!tif->tif_scanlinesize) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "Cannot handle zero scanline size");\n\t\treturn (0);\n\t}\n\tif (isTiled(tif)) {\n\t\ttif->tif_tilesize = TIFFTileSize(tif);\n\t\tif (!tif->tif_tilesize) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Cannot handle zero tile size");\n\t\t\treturn (0);\n\t\t}\n\t} else {\n\t\tif (!TIFFStripSize(tif)) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Cannot handle zero strip size");\n\t\t\treturn (0);\n\t\t}\n\t}\n\treturn (1);\nbad:\n\tif (dir)\n\t\t_TIFFfree(dir);\n\treturn (0);\n}', 'void\nTIFFFreeDirectory(TIFF* tif)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tint i;\n\t_TIFFmemset(td->td_fieldsset, 0, FIELD_SETLONGS);\n\tCleanupField(td_sminsamplevalue);\n\tCleanupField(td_smaxsamplevalue);\n\tCleanupField(td_colormap[0]);\n\tCleanupField(td_colormap[1]);\n\tCleanupField(td_colormap[2]);\n\tCleanupField(td_sampleinfo);\n\tCleanupField(td_subifd);\n\tCleanupField(td_inknames);\n\tCleanupField(td_refblackwhite);\n\tCleanupField(td_transferfunction[0]);\n\tCleanupField(td_transferfunction[1]);\n\tCleanupField(td_transferfunction[2]);\n\tCleanupField(td_stripoffset_p);\n\tCleanupField(td_stripbytecount_p);\n td->td_stripoffsetbyteallocsize = 0;\n\tTIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);\n\tTIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);\n\tfor( i = 0; i < td->td_customValueCount; i++ ) {\n\t\tif (td->td_customValues[i].value)\n\t\t\t_TIFFfree(td->td_customValues[i].value);\n\t}\n\ttd->td_customValueCount = 0;\n\tCleanupField(td_customValues);\n _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));\n _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));\n}', 'uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)\n{\n return TIFFGetStrileByteCountWithErr(tif, strile, NULL);\n}', 'uint64 TIFFGetStrileByteCountWithErr(TIFF *tif, uint32 strile, int *pbErr)\n{\n TIFFDirectory *td = &tif->tif_dir;\n return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,\n &(td->td_stripbytecount_entry),\n &(td->td_stripbytecount_p), pbErr);\n}', 'static uint64 _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32 strile,\n TIFFDirEntry* dirent,\n uint64** parray,\n int *pbErr)\n{\n TIFFDirectory *td = &tif->tif_dir;\n if( pbErr )\n *pbErr = 0;\n if( (tif->tif_flags&TIFF_DEFERSTRILELOAD) && !(tif->tif_flags&TIFF_CHOPPEDUPARRAYS) )\n {\n if( !(tif->tif_flags&TIFF_LAZYSTRILELOAD) ||\n dirent->tdir_count <= 4 )\n {\n if( !_TIFFFillStriles(tif) )\n {\n if( pbErr )\n *pbErr = 1;\n }\n }\n else\n {\n if( !_TIFFFetchStrileValue(tif, strile, dirent, parray) )\n {\n if( pbErr )\n *pbErr = 1;\n return 0;\n }\n }\n }\n if( *parray == NULL || strile >= td->td_nstrips )\n {\n if( pbErr )\n *pbErr = 1;\n return 0;\n }\n return (*parray)[strile];\n}', 'static int _TIFFFetchStrileValue(TIFF* tif,\n uint32 strile,\n TIFFDirEntry* dirent,\n uint64** parray)\n{\n static const char module[] = "_TIFFFetchStrileValue";\n TIFFDirectory *td = &tif->tif_dir;\n if( strile >= dirent->tdir_count )\n {\n return 0;\n }\n if( strile >= td->td_stripoffsetbyteallocsize )\n {\n uint32 nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;\n uint32 nStripArrayAllocNew;\n uint64 nArraySize64;\n size_t nArraySize;\n uint64* offsetArray;\n uint64* bytecountArray;\n if( strile > 1000000 )\n {\n uint64 filesize = TIFFGetFileSize(tif);\n if( strile > filesize / sizeof(uint32) )\n {\n TIFFErrorExt(tif->tif_clientdata, module, "File too short");\n return 0;\n }\n }\n if( td->td_stripoffsetbyteallocsize == 0 &&\n td->td_nstrips < 1024 * 1024 )\n {\n nStripArrayAllocNew = td->td_nstrips;\n }\n else\n {\n#define TIFF_MAX(a,b) (((a)>(b)) ? (a) : (b))\n#define TIFF_MIN(a,b) (((a)<(b)) ? (a) : (b))\n nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U );\n if( nStripArrayAllocNew < 0xFFFFFFFFU / 2 )\n nStripArrayAllocNew *= 2;\n nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);\n }\n assert( strile < nStripArrayAllocNew );\n nArraySize64 = (uint64)sizeof(uint64) * nStripArrayAllocNew;\n nArraySize = (size_t)(nArraySize64);\n#if SIZEOF_SIZE_T == 4\n if( nArraySize != nArraySize64 )\n {\n TIFFErrorExt(tif->tif_clientdata, module,\n "Cannot allocate strip offset and bytecount arrays");\n return 0;\n }\n#endif\n offsetArray = (uint64*)(\n _TIFFrealloc( td->td_stripoffset_p, nArraySize ) );\n bytecountArray = (uint64*)(\n _TIFFrealloc( td->td_stripbytecount_p, nArraySize ) );\n if( offsetArray )\n td->td_stripoffset_p = offsetArray;\n if( bytecountArray )\n td->td_stripbytecount_p = bytecountArray;\n if( offsetArray && bytecountArray )\n {\n td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;\n memset(td->td_stripoffset_p + nStripArrayAllocBefore,\n 0xFF,\n (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64) );\n memset(td->td_stripbytecount_p + nStripArrayAllocBefore,\n 0xFF,\n (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64) );\n }\n else\n {\n TIFFErrorExt(tif->tif_clientdata, module,\n "Cannot allocate strip offset and bytecount arrays");\n _TIFFfree(td->td_stripoffset_p);\n td->td_stripoffset_p = NULL;\n _TIFFfree(td->td_stripbytecount_p);\n td->td_stripbytecount_p = NULL;\n td->td_stripoffsetbyteallocsize = 0;\n }\n }\n if( *parray == NULL || strile >= td->td_stripoffsetbyteallocsize )\n return 0;\n if( ~((*parray)[strile]) == 0 )\n {\n if( !_TIFFPartialReadStripArray( tif, dirent, strile, *parray ) )\n {\n (*parray)[strile] = 0;\n return 0;\n }\n }\n return 1;\n}'] |
2,218 | 0 | https://github.com/libav/libav/blob/0bf511d579c7b21f1244eec688abf571ca1235bd/libavfilter/vf_unsharp.c/#L192 | static int config_props(AVFilterLink *link)
{
UnsharpContext *unsharp = link->dst->priv;
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
unsharp->hsub = desc->log2_chroma_w;
unsharp->vsub = desc->log2_chroma_h;
init_filter_param(link->dst, &unsharp->luma, "luma", link->w);
init_filter_param(link->dst, &unsharp->chroma, "chroma", SHIFTUP(link->w, unsharp->hsub));
return 0;
} | ['static int config_props(AVFilterLink *link)\n{\n UnsharpContext *unsharp = link->dst->priv;\n const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);\n unsharp->hsub = desc->log2_chroma_w;\n unsharp->vsub = desc->log2_chroma_h;\n init_filter_param(link->dst, &unsharp->luma, "luma", link->w);\n init_filter_param(link->dst, &unsharp->chroma, "chroma", SHIFTUP(link->w, unsharp->hsub));\n return 0;\n}', 'const AVPixFmtDescriptor *av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)\n{\n if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)\n return NULL;\n return &av_pix_fmt_descriptors[pix_fmt];\n}'] |
2,219 | 0 | https://github.com/libav/libav/blob/a4cd2ad89aa67533ff69de49612c747825c3f96f/avserver.c/#L4060 | static int parse_ffconfig(const char *filename)
{
FILE *f;
char line[1024];
char cmd[64];
char arg[1024];
const char *p;
int val, errors, line_num;
FFStream **last_stream, *stream, *redirect;
FFStream **last_feed, *feed, *s;
AVCodecContext audio_enc, video_enc;
enum AVCodecID audio_id, video_id;
f = fopen(filename, "r");
if (!f) {
perror(filename);
return -1;
}
errors = 0;
line_num = 0;
first_stream = NULL;
last_stream = &first_stream;
first_feed = NULL;
last_feed = &first_feed;
stream = NULL;
feed = NULL;
redirect = NULL;
audio_id = AV_CODEC_ID_NONE;
video_id = AV_CODEC_ID_NONE;
#define ERROR(...) report_config_error(filename, line_num, &errors, __VA_ARGS__)
for(;;) {
if (fgets(line, sizeof(line), f) == NULL)
break;
line_num++;
p = line;
while (isspace(*p))
p++;
if (*p == '\0' || *p == '#')
continue;
get_arg(cmd, sizeof(cmd), &p);
if (!av_strcasecmp(cmd, "Port")) {
get_arg(arg, sizeof(arg), &p);
val = atoi(arg);
if (val < 1 || val > 65536) {
ERROR("Invalid_port: %s\n", arg);
}
my_http_addr.sin_port = htons(val);
} else if (!av_strcasecmp(cmd, "BindAddress")) {
get_arg(arg, sizeof(arg), &p);
if (resolve_host(&my_http_addr.sin_addr, arg) != 0) {
ERROR("%s:%d: Invalid host/IP address: %s\n", arg);
}
} else if (!av_strcasecmp(cmd, "RTSPPort")) {
get_arg(arg, sizeof(arg), &p);
val = atoi(arg);
if (val < 1 || val > 65536) {
ERROR("%s:%d: Invalid port: %s\n", arg);
}
my_rtsp_addr.sin_port = htons(atoi(arg));
} else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
get_arg(arg, sizeof(arg), &p);
if (resolve_host(&my_rtsp_addr.sin_addr, arg) != 0) {
ERROR("Invalid host/IP address: %s\n", arg);
}
} else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
get_arg(arg, sizeof(arg), &p);
val = atoi(arg);
if (val < 1 || val > 65536) {
ERROR("Invalid MaxHTTPConnections: %s\n", arg);
}
nb_max_http_connections = val;
} else if (!av_strcasecmp(cmd, "MaxClients")) {
get_arg(arg, sizeof(arg), &p);
val = atoi(arg);
if (val < 1 || val > nb_max_http_connections) {
ERROR("Invalid MaxClients: %s\n", arg);
} else {
nb_max_connections = val;
}
} else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
int64_t llval;
get_arg(arg, sizeof(arg), &p);
llval = atoll(arg);
if (llval < 10 || llval > 10000000) {
ERROR("Invalid MaxBandwidth: %s\n", arg);
} else
max_bandwidth = llval;
} else if (!av_strcasecmp(cmd, "CustomLog")) {
if (!avserver_debug)
get_arg(logfilename, sizeof(logfilename), &p);
} else if (!av_strcasecmp(cmd, "<Feed")) {
char *q;
if (stream || feed) {
ERROR("Already in a tag\n");
} else {
feed = av_mallocz(sizeof(FFStream));
get_arg(feed->filename, sizeof(feed->filename), &p);
q = strrchr(feed->filename, '>');
if (*q)
*q = '\0';
for (s = first_feed; s; s = s->next) {
if (!strcmp(feed->filename, s->filename)) {
ERROR("Feed '%s' already registered\n", s->filename);
}
}
feed->fmt = av_guess_format("ffm", NULL, NULL);
snprintf(feed->feed_filename, sizeof(feed->feed_filename),
"/tmp/%s.ffm", feed->filename);
feed->feed_max_size = 5 * 1024 * 1024;
feed->is_feed = 1;
feed->feed = feed;
*last_stream = feed;
last_stream = &feed->next;
*last_feed = feed;
last_feed = &feed->next_feed;
}
} else if (!av_strcasecmp(cmd, "Launch")) {
if (feed) {
int i;
feed->child_argv = av_mallocz(64 * sizeof(char *));
for (i = 0; i < 62; i++) {
get_arg(arg, sizeof(arg), &p);
if (!arg[0])
break;
feed->child_argv[i] = av_strdup(arg);
}
feed->child_argv[i] = av_malloc(30 + strlen(feed->filename));
snprintf(feed->child_argv[i], 30+strlen(feed->filename),
"http://%s:%d/%s",
(my_http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
inet_ntoa(my_http_addr.sin_addr),
ntohs(my_http_addr.sin_port), feed->filename);
}
} else if (!av_strcasecmp(cmd, "ReadOnlyFile")) {
if (feed) {
get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
feed->readonly = 1;
} else if (stream) {
get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
}
} else if (!av_strcasecmp(cmd, "File")) {
if (feed) {
get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
} else if (stream)
get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
} else if (!av_strcasecmp(cmd, "Truncate")) {
if (feed) {
get_arg(arg, sizeof(arg), &p);
feed->truncate = strtod(arg, NULL);
}
} else if (!av_strcasecmp(cmd, "FileMaxSize")) {
if (feed) {
char *p1;
double fsize;
get_arg(arg, sizeof(arg), &p);
p1 = arg;
fsize = strtod(p1, &p1);
switch(toupper(*p1)) {
case 'K':
fsize *= 1024;
break;
case 'M':
fsize *= 1024 * 1024;
break;
case 'G':
fsize *= 1024 * 1024 * 1024;
break;
}
feed->feed_max_size = (int64_t)fsize;
if (feed->feed_max_size < FFM_PACKET_SIZE*4) {
ERROR("Feed max file size is too small, must be at least %d\n", FFM_PACKET_SIZE*4);
}
}
} else if (!av_strcasecmp(cmd, "</Feed>")) {
if (!feed) {
ERROR("No corresponding <Feed> for </Feed>\n");
}
feed = NULL;
} else if (!av_strcasecmp(cmd, "<Stream")) {
char *q;
if (stream || feed) {
ERROR("Already in a tag\n");
} else {
FFStream *s;
stream = av_mallocz(sizeof(FFStream));
get_arg(stream->filename, sizeof(stream->filename), &p);
q = strrchr(stream->filename, '>');
if (*q)
*q = '\0';
for (s = first_stream; s; s = s->next) {
if (!strcmp(stream->filename, s->filename)) {
ERROR("Stream '%s' already registered\n", s->filename);
}
}
stream->fmt = avserver_guess_format(NULL, stream->filename, NULL);
avcodec_get_context_defaults3(&video_enc, NULL);
avcodec_get_context_defaults3(&audio_enc, NULL);
audio_id = AV_CODEC_ID_NONE;
video_id = AV_CODEC_ID_NONE;
if (stream->fmt) {
audio_id = stream->fmt->audio_codec;
video_id = stream->fmt->video_codec;
}
*last_stream = stream;
last_stream = &stream->next;
}
} else if (!av_strcasecmp(cmd, "Feed")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
FFStream *sfeed;
sfeed = first_feed;
while (sfeed != NULL) {
if (!strcmp(sfeed->filename, arg))
break;
sfeed = sfeed->next_feed;
}
if (!sfeed)
ERROR("feed '%s' not defined\n", arg);
else
stream->feed = sfeed;
}
} else if (!av_strcasecmp(cmd, "Format")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
if (!strcmp(arg, "status")) {
stream->stream_type = STREAM_TYPE_STATUS;
stream->fmt = NULL;
} else {
stream->stream_type = STREAM_TYPE_LIVE;
if (!strcmp(arg, "jpeg"))
strcpy(arg, "mjpeg");
stream->fmt = avserver_guess_format(arg, NULL, NULL);
if (!stream->fmt) {
ERROR("Unknown Format: %s\n", arg);
}
}
if (stream->fmt) {
audio_id = stream->fmt->audio_codec;
video_id = stream->fmt->video_codec;
}
}
} else if (!av_strcasecmp(cmd, "InputFormat")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
stream->ifmt = av_find_input_format(arg);
if (!stream->ifmt) {
ERROR("Unknown input format: %s\n", arg);
}
}
} else if (!av_strcasecmp(cmd, "FaviconURL")) {
if (stream && stream->stream_type == STREAM_TYPE_STATUS) {
get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
} else {
ERROR("FaviconURL only permitted for status streams\n");
}
} else if (!av_strcasecmp(cmd, "Author")) {
if (stream)
get_arg(stream->author, sizeof(stream->author), &p);
} else if (!av_strcasecmp(cmd, "Comment")) {
if (stream)
get_arg(stream->comment, sizeof(stream->comment), &p);
} else if (!av_strcasecmp(cmd, "Copyright")) {
if (stream)
get_arg(stream->copyright, sizeof(stream->copyright), &p);
} else if (!av_strcasecmp(cmd, "Title")) {
if (stream)
get_arg(stream->title, sizeof(stream->title), &p);
} else if (!av_strcasecmp(cmd, "Preroll")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
stream->prebuffer = atof(arg) * 1000;
} else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
if (stream)
stream->send_on_key = 1;
} else if (!av_strcasecmp(cmd, "AudioCodec")) {
get_arg(arg, sizeof(arg), &p);
audio_id = opt_audio_codec(arg);
if (audio_id == AV_CODEC_ID_NONE) {
ERROR("Unknown AudioCodec: %s\n", arg);
}
} else if (!av_strcasecmp(cmd, "VideoCodec")) {
get_arg(arg, sizeof(arg), &p);
video_id = opt_video_codec(arg);
if (video_id == AV_CODEC_ID_NONE) {
ERROR("Unknown VideoCodec: %s\n", arg);
}
} else if (!av_strcasecmp(cmd, "MaxTime")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
stream->max_time = atof(arg) * 1000;
} else if (!av_strcasecmp(cmd, "AudioBitRate")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
audio_enc.bit_rate = lrintf(atof(arg) * 1000);
} else if (!av_strcasecmp(cmd, "AudioChannels")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
audio_enc.channels = atoi(arg);
} else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
audio_enc.sample_rate = atoi(arg);
} else if (!av_strcasecmp(cmd, "AudioQuality")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
}
} else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
if (stream) {
int minrate, maxrate;
get_arg(arg, sizeof(arg), &p);
if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
video_enc.rc_min_rate = minrate * 1000;
video_enc.rc_max_rate = maxrate * 1000;
} else {
ERROR("Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n", arg);
}
}
} else if (!av_strcasecmp(cmd, "Debug")) {
if (stream) {
get_arg(arg, sizeof(arg), &p);
video_enc.debug = strtol(arg,0,0);
}
} else if (!av_strcasecmp(cmd, "Strict")) {
if (stream) {
get_arg(arg, sizeof(arg), &p);
video_enc.strict_std_compliance = atoi(arg);
}
} else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
if (stream) {
get_arg(arg, sizeof(arg), &p);
video_enc.rc_buffer_size = atoi(arg) * 8*1024;
}
} else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
if (stream) {
get_arg(arg, sizeof(arg), &p);
video_enc.bit_rate_tolerance = atoi(arg) * 1000;
}
} else if (!av_strcasecmp(cmd, "VideoBitRate")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
video_enc.bit_rate = atoi(arg) * 1000;
}
} else if (!av_strcasecmp(cmd, "VideoSize")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
av_parse_video_size(&video_enc.width, &video_enc.height, arg);
if ((video_enc.width % 16) != 0 ||
(video_enc.height % 16) != 0) {
ERROR("Image size must be a multiple of 16\n");
}
}
} else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
AVRational frame_rate;
if (av_parse_video_rate(&frame_rate, arg) < 0) {
ERROR("Incorrect frame rate: %s\n", arg);
} else {
video_enc.time_base.num = frame_rate.den;
video_enc.time_base.den = frame_rate.num;
}
}
} else if (!av_strcasecmp(cmd, "VideoGopSize")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
video_enc.gop_size = atoi(arg);
} else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
if (stream)
video_enc.gop_size = 1;
} else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
if (stream)
video_enc.mb_decision = FF_MB_DECISION_BITS;
} else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
if (stream) {
video_enc.mb_decision = FF_MB_DECISION_BITS;
video_enc.flags |= CODEC_FLAG_4MV;
}
} else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
!av_strcasecmp(cmd, "AVOptionAudio")) {
char arg2[1024];
AVCodecContext *avctx;
int type;
get_arg(arg, sizeof(arg), &p);
get_arg(arg2, sizeof(arg2), &p);
if (!av_strcasecmp(cmd, "AVOptionVideo")) {
avctx = &video_enc;
type = AV_OPT_FLAG_VIDEO_PARAM;
} else {
avctx = &audio_enc;
type = AV_OPT_FLAG_AUDIO_PARAM;
}
if (avserver_opt_default(arg, arg2, avctx, type|AV_OPT_FLAG_ENCODING_PARAM)) {
ERROR("AVOption error: %s %s\n", arg, arg2);
}
} else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
!av_strcasecmp(cmd, "AVPresetAudio")) {
AVCodecContext *avctx;
int type;
get_arg(arg, sizeof(arg), &p);
if (!av_strcasecmp(cmd, "AVPresetVideo")) {
avctx = &video_enc;
video_enc.codec_id = video_id;
type = AV_OPT_FLAG_VIDEO_PARAM;
} else {
avctx = &audio_enc;
audio_enc.codec_id = audio_id;
type = AV_OPT_FLAG_AUDIO_PARAM;
}
if (avserver_opt_preset(arg, avctx, type|AV_OPT_FLAG_ENCODING_PARAM, &audio_id, &video_id)) {
ERROR("AVPreset error: %s\n", arg);
}
} else if (!av_strcasecmp(cmd, "VideoTag")) {
get_arg(arg, sizeof(arg), &p);
if ((strlen(arg) == 4) && stream)
video_enc.codec_tag = MKTAG(arg[0], arg[1], arg[2], arg[3]);
} else if (!av_strcasecmp(cmd, "BitExact")) {
if (stream)
video_enc.flags |= CODEC_FLAG_BITEXACT;
} else if (!av_strcasecmp(cmd, "DctFastint")) {
if (stream)
video_enc.dct_algo = FF_DCT_FASTINT;
} else if (!av_strcasecmp(cmd, "IdctSimple")) {
if (stream)
video_enc.idct_algo = FF_IDCT_SIMPLE;
} else if (!av_strcasecmp(cmd, "Qscale")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
video_enc.flags |= CODEC_FLAG_QSCALE;
video_enc.global_quality = FF_QP2LAMBDA * atoi(arg);
}
} else if (!av_strcasecmp(cmd, "VideoQDiff")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
video_enc.max_qdiff = atoi(arg);
if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {
ERROR("VideoQDiff out of range\n");
}
}
} else if (!av_strcasecmp(cmd, "VideoQMax")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
video_enc.qmax = atoi(arg);
if (video_enc.qmax < 1 || video_enc.qmax > 31) {
ERROR("VideoQMax out of range\n");
}
}
} else if (!av_strcasecmp(cmd, "VideoQMin")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
video_enc.qmin = atoi(arg);
if (video_enc.qmin < 1 || video_enc.qmin > 31) {
ERROR("VideoQMin out of range\n");
}
}
} else if (!av_strcasecmp(cmd, "LumaElim")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
video_enc.luma_elim_threshold = atoi(arg);
} else if (!av_strcasecmp(cmd, "ChromaElim")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
video_enc.chroma_elim_threshold = atoi(arg);
} else if (!av_strcasecmp(cmd, "LumiMask")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
video_enc.lumi_masking = atof(arg);
} else if (!av_strcasecmp(cmd, "DarkMask")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
video_enc.dark_masking = atof(arg);
} else if (!av_strcasecmp(cmd, "NoVideo")) {
video_id = AV_CODEC_ID_NONE;
} else if (!av_strcasecmp(cmd, "NoAudio")) {
audio_id = AV_CODEC_ID_NONE;
} else if (!av_strcasecmp(cmd, "ACL")) {
parse_acl_row(stream, feed, NULL, p, filename, line_num);
} else if (!av_strcasecmp(cmd, "DynamicACL")) {
if (stream) {
get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), &p);
}
} else if (!av_strcasecmp(cmd, "RTSPOption")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
av_freep(&stream->rtsp_option);
stream->rtsp_option = av_strdup(arg);
}
} else if (!av_strcasecmp(cmd, "MulticastAddress")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
if (resolve_host(&stream->multicast_ip, arg) != 0) {
ERROR("Invalid host/IP address: %s\n", arg);
}
stream->is_multicast = 1;
stream->loop = 1;
}
} else if (!av_strcasecmp(cmd, "MulticastPort")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
stream->multicast_port = atoi(arg);
} else if (!av_strcasecmp(cmd, "MulticastTTL")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
stream->multicast_ttl = atoi(arg);
} else if (!av_strcasecmp(cmd, "NoLoop")) {
if (stream)
stream->loop = 0;
} else if (!av_strcasecmp(cmd, "</Stream>")) {
if (!stream) {
ERROR("No corresponding <Stream> for </Stream>\n");
} else {
if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
if (audio_id != AV_CODEC_ID_NONE) {
audio_enc.codec_type = AVMEDIA_TYPE_AUDIO;
audio_enc.codec_id = audio_id;
add_codec(stream, &audio_enc);
}
if (video_id != AV_CODEC_ID_NONE) {
video_enc.codec_type = AVMEDIA_TYPE_VIDEO;
video_enc.codec_id = video_id;
add_codec(stream, &video_enc);
}
}
stream = NULL;
}
} else if (!av_strcasecmp(cmd, "<Redirect")) {
char *q;
if (stream || feed || redirect) {
ERROR("Already in a tag\n");
} else {
redirect = av_mallocz(sizeof(FFStream));
*last_stream = redirect;
last_stream = &redirect->next;
get_arg(redirect->filename, sizeof(redirect->filename), &p);
q = strrchr(redirect->filename, '>');
if (*q)
*q = '\0';
redirect->stream_type = STREAM_TYPE_REDIRECT;
}
} else if (!av_strcasecmp(cmd, "URL")) {
if (redirect)
get_arg(redirect->feed_filename, sizeof(redirect->feed_filename), &p);
} else if (!av_strcasecmp(cmd, "</Redirect>")) {
if (!redirect) {
ERROR("No corresponding <Redirect> for </Redirect>\n");
} else {
if (!redirect->feed_filename[0]) {
ERROR("No URL found for <Redirect>\n");
}
redirect = NULL;
}
} else if (!av_strcasecmp(cmd, "LoadModule")) {
get_arg(arg, sizeof(arg), &p);
#if HAVE_DLOPEN
load_module(arg);
#else
ERROR("Module support not compiled into this version: '%s'\n", arg);
#endif
} else {
ERROR("Incorrect keyword: '%s'\n", cmd);
}
}
#undef ERROR
fclose(f);
if (errors)
return -1;
else
return 0;
} | ['static int parse_ffconfig(const char *filename)\n{\n FILE *f;\n char line[1024];\n char cmd[64];\n char arg[1024];\n const char *p;\n int val, errors, line_num;\n FFStream **last_stream, *stream, *redirect;\n FFStream **last_feed, *feed, *s;\n AVCodecContext audio_enc, video_enc;\n enum AVCodecID audio_id, video_id;\n f = fopen(filename, "r");\n if (!f) {\n perror(filename);\n return -1;\n }\n errors = 0;\n line_num = 0;\n first_stream = NULL;\n last_stream = &first_stream;\n first_feed = NULL;\n last_feed = &first_feed;\n stream = NULL;\n feed = NULL;\n redirect = NULL;\n audio_id = AV_CODEC_ID_NONE;\n video_id = AV_CODEC_ID_NONE;\n#define ERROR(...) report_config_error(filename, line_num, &errors, __VA_ARGS__)\n for(;;) {\n if (fgets(line, sizeof(line), f) == NULL)\n break;\n line_num++;\n p = line;\n while (isspace(*p))\n p++;\n if (*p == \'\\0\' || *p == \'#\')\n continue;\n get_arg(cmd, sizeof(cmd), &p);\n if (!av_strcasecmp(cmd, "Port")) {\n get_arg(arg, sizeof(arg), &p);\n val = atoi(arg);\n if (val < 1 || val > 65536) {\n ERROR("Invalid_port: %s\\n", arg);\n }\n my_http_addr.sin_port = htons(val);\n } else if (!av_strcasecmp(cmd, "BindAddress")) {\n get_arg(arg, sizeof(arg), &p);\n if (resolve_host(&my_http_addr.sin_addr, arg) != 0) {\n ERROR("%s:%d: Invalid host/IP address: %s\\n", arg);\n }\n } else if (!av_strcasecmp(cmd, "RTSPPort")) {\n get_arg(arg, sizeof(arg), &p);\n val = atoi(arg);\n if (val < 1 || val > 65536) {\n ERROR("%s:%d: Invalid port: %s\\n", arg);\n }\n my_rtsp_addr.sin_port = htons(atoi(arg));\n } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {\n get_arg(arg, sizeof(arg), &p);\n if (resolve_host(&my_rtsp_addr.sin_addr, arg) != 0) {\n ERROR("Invalid host/IP address: %s\\n", arg);\n }\n } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {\n get_arg(arg, sizeof(arg), &p);\n val = atoi(arg);\n if (val < 1 || val > 65536) {\n ERROR("Invalid MaxHTTPConnections: %s\\n", arg);\n }\n nb_max_http_connections = val;\n } else if (!av_strcasecmp(cmd, "MaxClients")) {\n get_arg(arg, sizeof(arg), &p);\n val = atoi(arg);\n if (val < 1 || val > nb_max_http_connections) {\n ERROR("Invalid MaxClients: %s\\n", arg);\n } else {\n nb_max_connections = val;\n }\n } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {\n int64_t llval;\n get_arg(arg, sizeof(arg), &p);\n llval = atoll(arg);\n if (llval < 10 || llval > 10000000) {\n ERROR("Invalid MaxBandwidth: %s\\n", arg);\n } else\n max_bandwidth = llval;\n } else if (!av_strcasecmp(cmd, "CustomLog")) {\n if (!avserver_debug)\n get_arg(logfilename, sizeof(logfilename), &p);\n } else if (!av_strcasecmp(cmd, "<Feed")) {\n char *q;\n if (stream || feed) {\n ERROR("Already in a tag\\n");\n } else {\n feed = av_mallocz(sizeof(FFStream));\n get_arg(feed->filename, sizeof(feed->filename), &p);\n q = strrchr(feed->filename, \'>\');\n if (*q)\n *q = \'\\0\';\n for (s = first_feed; s; s = s->next) {\n if (!strcmp(feed->filename, s->filename)) {\n ERROR("Feed \'%s\' already registered\\n", s->filename);\n }\n }\n feed->fmt = av_guess_format("ffm", NULL, NULL);\n snprintf(feed->feed_filename, sizeof(feed->feed_filename),\n "/tmp/%s.ffm", feed->filename);\n feed->feed_max_size = 5 * 1024 * 1024;\n feed->is_feed = 1;\n feed->feed = feed;\n *last_stream = feed;\n last_stream = &feed->next;\n *last_feed = feed;\n last_feed = &feed->next_feed;\n }\n } else if (!av_strcasecmp(cmd, "Launch")) {\n if (feed) {\n int i;\n feed->child_argv = av_mallocz(64 * sizeof(char *));\n for (i = 0; i < 62; i++) {\n get_arg(arg, sizeof(arg), &p);\n if (!arg[0])\n break;\n feed->child_argv[i] = av_strdup(arg);\n }\n feed->child_argv[i] = av_malloc(30 + strlen(feed->filename));\n snprintf(feed->child_argv[i], 30+strlen(feed->filename),\n "http://%s:%d/%s",\n (my_http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :\n inet_ntoa(my_http_addr.sin_addr),\n ntohs(my_http_addr.sin_port), feed->filename);\n }\n } else if (!av_strcasecmp(cmd, "ReadOnlyFile")) {\n if (feed) {\n get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);\n feed->readonly = 1;\n } else if (stream) {\n get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);\n }\n } else if (!av_strcasecmp(cmd, "File")) {\n if (feed) {\n get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);\n } else if (stream)\n get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);\n } else if (!av_strcasecmp(cmd, "Truncate")) {\n if (feed) {\n get_arg(arg, sizeof(arg), &p);\n feed->truncate = strtod(arg, NULL);\n }\n } else if (!av_strcasecmp(cmd, "FileMaxSize")) {\n if (feed) {\n char *p1;\n double fsize;\n get_arg(arg, sizeof(arg), &p);\n p1 = arg;\n fsize = strtod(p1, &p1);\n switch(toupper(*p1)) {\n case \'K\':\n fsize *= 1024;\n break;\n case \'M\':\n fsize *= 1024 * 1024;\n break;\n case \'G\':\n fsize *= 1024 * 1024 * 1024;\n break;\n }\n feed->feed_max_size = (int64_t)fsize;\n if (feed->feed_max_size < FFM_PACKET_SIZE*4) {\n ERROR("Feed max file size is too small, must be at least %d\\n", FFM_PACKET_SIZE*4);\n }\n }\n } else if (!av_strcasecmp(cmd, "</Feed>")) {\n if (!feed) {\n ERROR("No corresponding <Feed> for </Feed>\\n");\n }\n feed = NULL;\n } else if (!av_strcasecmp(cmd, "<Stream")) {\n char *q;\n if (stream || feed) {\n ERROR("Already in a tag\\n");\n } else {\n FFStream *s;\n stream = av_mallocz(sizeof(FFStream));\n get_arg(stream->filename, sizeof(stream->filename), &p);\n q = strrchr(stream->filename, \'>\');\n if (*q)\n *q = \'\\0\';\n for (s = first_stream; s; s = s->next) {\n if (!strcmp(stream->filename, s->filename)) {\n ERROR("Stream \'%s\' already registered\\n", s->filename);\n }\n }\n stream->fmt = avserver_guess_format(NULL, stream->filename, NULL);\n avcodec_get_context_defaults3(&video_enc, NULL);\n avcodec_get_context_defaults3(&audio_enc, NULL);\n audio_id = AV_CODEC_ID_NONE;\n video_id = AV_CODEC_ID_NONE;\n if (stream->fmt) {\n audio_id = stream->fmt->audio_codec;\n video_id = stream->fmt->video_codec;\n }\n *last_stream = stream;\n last_stream = &stream->next;\n }\n } else if (!av_strcasecmp(cmd, "Feed")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream) {\n FFStream *sfeed;\n sfeed = first_feed;\n while (sfeed != NULL) {\n if (!strcmp(sfeed->filename, arg))\n break;\n sfeed = sfeed->next_feed;\n }\n if (!sfeed)\n ERROR("feed \'%s\' not defined\\n", arg);\n else\n stream->feed = sfeed;\n }\n } else if (!av_strcasecmp(cmd, "Format")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream) {\n if (!strcmp(arg, "status")) {\n stream->stream_type = STREAM_TYPE_STATUS;\n stream->fmt = NULL;\n } else {\n stream->stream_type = STREAM_TYPE_LIVE;\n if (!strcmp(arg, "jpeg"))\n strcpy(arg, "mjpeg");\n stream->fmt = avserver_guess_format(arg, NULL, NULL);\n if (!stream->fmt) {\n ERROR("Unknown Format: %s\\n", arg);\n }\n }\n if (stream->fmt) {\n audio_id = stream->fmt->audio_codec;\n video_id = stream->fmt->video_codec;\n }\n }\n } else if (!av_strcasecmp(cmd, "InputFormat")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream) {\n stream->ifmt = av_find_input_format(arg);\n if (!stream->ifmt) {\n ERROR("Unknown input format: %s\\n", arg);\n }\n }\n } else if (!av_strcasecmp(cmd, "FaviconURL")) {\n if (stream && stream->stream_type == STREAM_TYPE_STATUS) {\n get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);\n } else {\n ERROR("FaviconURL only permitted for status streams\\n");\n }\n } else if (!av_strcasecmp(cmd, "Author")) {\n if (stream)\n get_arg(stream->author, sizeof(stream->author), &p);\n } else if (!av_strcasecmp(cmd, "Comment")) {\n if (stream)\n get_arg(stream->comment, sizeof(stream->comment), &p);\n } else if (!av_strcasecmp(cmd, "Copyright")) {\n if (stream)\n get_arg(stream->copyright, sizeof(stream->copyright), &p);\n } else if (!av_strcasecmp(cmd, "Title")) {\n if (stream)\n get_arg(stream->title, sizeof(stream->title), &p);\n } else if (!av_strcasecmp(cmd, "Preroll")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream)\n stream->prebuffer = atof(arg) * 1000;\n } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {\n if (stream)\n stream->send_on_key = 1;\n } else if (!av_strcasecmp(cmd, "AudioCodec")) {\n get_arg(arg, sizeof(arg), &p);\n audio_id = opt_audio_codec(arg);\n if (audio_id == AV_CODEC_ID_NONE) {\n ERROR("Unknown AudioCodec: %s\\n", arg);\n }\n } else if (!av_strcasecmp(cmd, "VideoCodec")) {\n get_arg(arg, sizeof(arg), &p);\n video_id = opt_video_codec(arg);\n if (video_id == AV_CODEC_ID_NONE) {\n ERROR("Unknown VideoCodec: %s\\n", arg);\n }\n } else if (!av_strcasecmp(cmd, "MaxTime")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream)\n stream->max_time = atof(arg) * 1000;\n } else if (!av_strcasecmp(cmd, "AudioBitRate")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream)\n audio_enc.bit_rate = lrintf(atof(arg) * 1000);\n } else if (!av_strcasecmp(cmd, "AudioChannels")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream)\n audio_enc.channels = atoi(arg);\n } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream)\n audio_enc.sample_rate = atoi(arg);\n } else if (!av_strcasecmp(cmd, "AudioQuality")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream) {\n }\n } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {\n if (stream) {\n int minrate, maxrate;\n get_arg(arg, sizeof(arg), &p);\n if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {\n video_enc.rc_min_rate = minrate * 1000;\n video_enc.rc_max_rate = maxrate * 1000;\n } else {\n ERROR("Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\\n", arg);\n }\n }\n } else if (!av_strcasecmp(cmd, "Debug")) {\n if (stream) {\n get_arg(arg, sizeof(arg), &p);\n video_enc.debug = strtol(arg,0,0);\n }\n } else if (!av_strcasecmp(cmd, "Strict")) {\n if (stream) {\n get_arg(arg, sizeof(arg), &p);\n video_enc.strict_std_compliance = atoi(arg);\n }\n } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {\n if (stream) {\n get_arg(arg, sizeof(arg), &p);\n video_enc.rc_buffer_size = atoi(arg) * 8*1024;\n }\n } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {\n if (stream) {\n get_arg(arg, sizeof(arg), &p);\n video_enc.bit_rate_tolerance = atoi(arg) * 1000;\n }\n } else if (!av_strcasecmp(cmd, "VideoBitRate")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream) {\n video_enc.bit_rate = atoi(arg) * 1000;\n }\n } else if (!av_strcasecmp(cmd, "VideoSize")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream) {\n av_parse_video_size(&video_enc.width, &video_enc.height, arg);\n if ((video_enc.width % 16) != 0 ||\n (video_enc.height % 16) != 0) {\n ERROR("Image size must be a multiple of 16\\n");\n }\n }\n } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream) {\n AVRational frame_rate;\n if (av_parse_video_rate(&frame_rate, arg) < 0) {\n ERROR("Incorrect frame rate: %s\\n", arg);\n } else {\n video_enc.time_base.num = frame_rate.den;\n video_enc.time_base.den = frame_rate.num;\n }\n }\n } else if (!av_strcasecmp(cmd, "VideoGopSize")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream)\n video_enc.gop_size = atoi(arg);\n } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {\n if (stream)\n video_enc.gop_size = 1;\n } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {\n if (stream)\n video_enc.mb_decision = FF_MB_DECISION_BITS;\n } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {\n if (stream) {\n video_enc.mb_decision = FF_MB_DECISION_BITS;\n video_enc.flags |= CODEC_FLAG_4MV;\n }\n } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||\n !av_strcasecmp(cmd, "AVOptionAudio")) {\n char arg2[1024];\n AVCodecContext *avctx;\n int type;\n get_arg(arg, sizeof(arg), &p);\n get_arg(arg2, sizeof(arg2), &p);\n if (!av_strcasecmp(cmd, "AVOptionVideo")) {\n avctx = &video_enc;\n type = AV_OPT_FLAG_VIDEO_PARAM;\n } else {\n avctx = &audio_enc;\n type = AV_OPT_FLAG_AUDIO_PARAM;\n }\n if (avserver_opt_default(arg, arg2, avctx, type|AV_OPT_FLAG_ENCODING_PARAM)) {\n ERROR("AVOption error: %s %s\\n", arg, arg2);\n }\n } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||\n !av_strcasecmp(cmd, "AVPresetAudio")) {\n AVCodecContext *avctx;\n int type;\n get_arg(arg, sizeof(arg), &p);\n if (!av_strcasecmp(cmd, "AVPresetVideo")) {\n avctx = &video_enc;\n video_enc.codec_id = video_id;\n type = AV_OPT_FLAG_VIDEO_PARAM;\n } else {\n avctx = &audio_enc;\n audio_enc.codec_id = audio_id;\n type = AV_OPT_FLAG_AUDIO_PARAM;\n }\n if (avserver_opt_preset(arg, avctx, type|AV_OPT_FLAG_ENCODING_PARAM, &audio_id, &video_id)) {\n ERROR("AVPreset error: %s\\n", arg);\n }\n } else if (!av_strcasecmp(cmd, "VideoTag")) {\n get_arg(arg, sizeof(arg), &p);\n if ((strlen(arg) == 4) && stream)\n video_enc.codec_tag = MKTAG(arg[0], arg[1], arg[2], arg[3]);\n } else if (!av_strcasecmp(cmd, "BitExact")) {\n if (stream)\n video_enc.flags |= CODEC_FLAG_BITEXACT;\n } else if (!av_strcasecmp(cmd, "DctFastint")) {\n if (stream)\n video_enc.dct_algo = FF_DCT_FASTINT;\n } else if (!av_strcasecmp(cmd, "IdctSimple")) {\n if (stream)\n video_enc.idct_algo = FF_IDCT_SIMPLE;\n } else if (!av_strcasecmp(cmd, "Qscale")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream) {\n video_enc.flags |= CODEC_FLAG_QSCALE;\n video_enc.global_quality = FF_QP2LAMBDA * atoi(arg);\n }\n } else if (!av_strcasecmp(cmd, "VideoQDiff")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream) {\n video_enc.max_qdiff = atoi(arg);\n if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {\n ERROR("VideoQDiff out of range\\n");\n }\n }\n } else if (!av_strcasecmp(cmd, "VideoQMax")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream) {\n video_enc.qmax = atoi(arg);\n if (video_enc.qmax < 1 || video_enc.qmax > 31) {\n ERROR("VideoQMax out of range\\n");\n }\n }\n } else if (!av_strcasecmp(cmd, "VideoQMin")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream) {\n video_enc.qmin = atoi(arg);\n if (video_enc.qmin < 1 || video_enc.qmin > 31) {\n ERROR("VideoQMin out of range\\n");\n }\n }\n } else if (!av_strcasecmp(cmd, "LumaElim")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream)\n video_enc.luma_elim_threshold = atoi(arg);\n } else if (!av_strcasecmp(cmd, "ChromaElim")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream)\n video_enc.chroma_elim_threshold = atoi(arg);\n } else if (!av_strcasecmp(cmd, "LumiMask")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream)\n video_enc.lumi_masking = atof(arg);\n } else if (!av_strcasecmp(cmd, "DarkMask")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream)\n video_enc.dark_masking = atof(arg);\n } else if (!av_strcasecmp(cmd, "NoVideo")) {\n video_id = AV_CODEC_ID_NONE;\n } else if (!av_strcasecmp(cmd, "NoAudio")) {\n audio_id = AV_CODEC_ID_NONE;\n } else if (!av_strcasecmp(cmd, "ACL")) {\n parse_acl_row(stream, feed, NULL, p, filename, line_num);\n } else if (!av_strcasecmp(cmd, "DynamicACL")) {\n if (stream) {\n get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), &p);\n }\n } else if (!av_strcasecmp(cmd, "RTSPOption")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream) {\n av_freep(&stream->rtsp_option);\n stream->rtsp_option = av_strdup(arg);\n }\n } else if (!av_strcasecmp(cmd, "MulticastAddress")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream) {\n if (resolve_host(&stream->multicast_ip, arg) != 0) {\n ERROR("Invalid host/IP address: %s\\n", arg);\n }\n stream->is_multicast = 1;\n stream->loop = 1;\n }\n } else if (!av_strcasecmp(cmd, "MulticastPort")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream)\n stream->multicast_port = atoi(arg);\n } else if (!av_strcasecmp(cmd, "MulticastTTL")) {\n get_arg(arg, sizeof(arg), &p);\n if (stream)\n stream->multicast_ttl = atoi(arg);\n } else if (!av_strcasecmp(cmd, "NoLoop")) {\n if (stream)\n stream->loop = 0;\n } else if (!av_strcasecmp(cmd, "</Stream>")) {\n if (!stream) {\n ERROR("No corresponding <Stream> for </Stream>\\n");\n } else {\n if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {\n if (audio_id != AV_CODEC_ID_NONE) {\n audio_enc.codec_type = AVMEDIA_TYPE_AUDIO;\n audio_enc.codec_id = audio_id;\n add_codec(stream, &audio_enc);\n }\n if (video_id != AV_CODEC_ID_NONE) {\n video_enc.codec_type = AVMEDIA_TYPE_VIDEO;\n video_enc.codec_id = video_id;\n add_codec(stream, &video_enc);\n }\n }\n stream = NULL;\n }\n } else if (!av_strcasecmp(cmd, "<Redirect")) {\n char *q;\n if (stream || feed || redirect) {\n ERROR("Already in a tag\\n");\n } else {\n redirect = av_mallocz(sizeof(FFStream));\n *last_stream = redirect;\n last_stream = &redirect->next;\n get_arg(redirect->filename, sizeof(redirect->filename), &p);\n q = strrchr(redirect->filename, \'>\');\n if (*q)\n *q = \'\\0\';\n redirect->stream_type = STREAM_TYPE_REDIRECT;\n }\n } else if (!av_strcasecmp(cmd, "URL")) {\n if (redirect)\n get_arg(redirect->feed_filename, sizeof(redirect->feed_filename), &p);\n } else if (!av_strcasecmp(cmd, "</Redirect>")) {\n if (!redirect) {\n ERROR("No corresponding <Redirect> for </Redirect>\\n");\n } else {\n if (!redirect->feed_filename[0]) {\n ERROR("No URL found for <Redirect>\\n");\n }\n redirect = NULL;\n }\n } else if (!av_strcasecmp(cmd, "LoadModule")) {\n get_arg(arg, sizeof(arg), &p);\n#if HAVE_DLOPEN\n load_module(arg);\n#else\n ERROR("Module support not compiled into this version: \'%s\'\\n", arg);\n#endif\n } else {\n ERROR("Incorrect keyword: \'%s\'\\n", cmd);\n }\n }\n#undef ERROR\n fclose(f);\n if (errors)\n return -1;\n else\n return 0;\n}'] |
2,220 | 0 | https://github.com/openssl/openssl/blob/54d00677f305375eee65a0c9edb5f0980c5f020f/crypto/bn/bn_lib.c/#L232 | static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
} | ['int BN_GF2m_mod_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 ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'int BN_GF2m_arr2poly(const int p[], BIGNUM *a)\n{\n int i;\n bn_check_top(a);\n BN_zero(a);\n for (i = 0; p[i] != -1; i++) {\n if (BN_set_bit(a, p[i]) == 0)\n return 0;\n }\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 if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}'] |
2,221 | 0 | https://github.com/openssl/openssl/blob/55525742f4c2bf416013fc3a75ec642775d97f80/crypto/bn/bn_ctx.c/#L353 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)\n\t{\n\tBN_CTX *ctx;\n\tBIGNUM k,kq,*K,*kinv=NULL,*r=NULL;\n\tint ret=0;\n\tif (!dsa->p || !dsa->q || !dsa->g)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_SIGN_SETUP,DSA_R_MISSING_PARAMETERS);\n\t\treturn 0;\n\t\t}\n\tBN_init(&k);\n\tBN_init(&kq);\n\tif (ctx_in == NULL)\n\t\t{\n\t\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\t\t}\n\telse\n\t\tctx=ctx_in;\n\tif ((r=BN_new()) == NULL) goto err;\n\tdo\n\t\tif (!BN_rand_range(&k, dsa->q)) goto err;\n\twhile (BN_is_zero(&k));\n\tif ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0)\n\t\t{\n\t\tBN_set_flags(&k, BN_FLG_CONSTTIME);\n\t\t}\n\tif (dsa->flags & DSA_FLAG_CACHE_MONT_P)\n\t\t{\n\t\tif (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,\n\t\t\t\t\t\tCRYPTO_LOCK_DSA,\n\t\t\t\t\t\tdsa->p, ctx))\n\t\t\tgoto err;\n\t\t}\n\tif ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0)\n\t\t{\n\t\tif (!BN_copy(&kq, &k)) goto err;\n\t\tif (!BN_add(&kq, &kq, dsa->q)) goto err;\n\t\tif (BN_num_bits(&kq) <= BN_num_bits(dsa->q))\n\t\t\t{\n\t\t\tif (!BN_add(&kq, &kq, dsa->q)) goto err;\n\t\t\t}\n\t\tK = &kq;\n\t\t}\n\telse\n\t\t{\n\t\tK = &k;\n\t\t}\n\tDSA_BN_MOD_EXP(goto err, dsa, r, dsa->g, K, dsa->p, ctx,\n\t\t\tdsa->method_mont_p);\n\tif (!BN_mod(r,r,dsa->q,ctx)) goto err;\n\tif ((kinv=BN_mod_inverse(NULL,&k,dsa->q,ctx)) == NULL) goto err;\n\tif (*kinvp != NULL) BN_clear_free(*kinvp);\n\t*kinvp=kinv;\n\tkinv=NULL;\n\tif (*rp != NULL) BN_clear_free(*rp);\n\t*rp=r;\n\tret=1;\nerr:\n\tif (!ret)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_SIGN_SETUP,ERR_R_BN_LIB);\n\t\tif (r != NULL)\n\t\t\tBN_clear_free(r);\n\t\t}\n\tif (ctx_in == NULL) BN_CTX_free(ctx);\n\tBN_clear_free(&k);\n\tBN_clear_free(&kq);\n\treturn(ret);\n\t}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1;\n\tBIGNUM *d,*r;\n\tconst BIGNUM *aa;\n\tBIGNUM *val[TABLE_SIZE];\n\tBN_MONT_CTX *mont=NULL;\n\tif (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)\n\t\t{\n\t\treturn BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n\t\t}\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\tif (!BN_is_odd(m))\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\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tret = BN_one(rr);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\td = BN_CTX_get(ctx);\n\tr = BN_CTX_get(ctx);\n\tval[0] = BN_CTX_get(ctx);\n\tif (!d || !r || !val[0]) 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\tif (a->neg || BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tif (!BN_nnmod(val[0],a,m,ctx))\n\t\t\tgoto err;\n\t\taa= val[0];\n\t\t}\n\telse\n\t\taa=a;\n\tif (BN_is_zero(aa))\n\t\t{\n\t\tBN_zero(rr);\n\t\tret = 1;\n\t\tgoto err;\n\t\t}\n\tif (!BN_to_montgomery(val[0],aa,mont,ctx)) goto err;\n\twindow = BN_window_bits_for_exponent_size(bits);\n\tif (window > 1)\n\t\t{\n\t\tif (!BN_mod_mul_montgomery(d,val[0],val[0],mont,ctx)) goto err;\n\t\tj=1<<(window-1);\n\t\tfor (i=1; i<j; i++)\n\t\t\t{\n\t\t\tif(((val[i] = BN_CTX_get(ctx)) == NULL) ||\n\t\t\t\t\t!BN_mod_mul_montgomery(val[i],val[i-1],\n\t\t\t\t\t\td,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tstart=1;\n\twvalue=0;\n\twstart=bits-1;\n\twend=0;\n\tif (!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\tif (!BN_from_montgomery(rr,r,mont,ctx)) goto err;\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tBN_CTX_end(ctx);\n\tbn_check_top(rr);\n\treturn(ret);\n\t}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,bits,ret=0,idx,window,wvalue;\n\tint top;\n \tBIGNUM *r;\n\tconst BIGNUM *aa;\n\tBN_MONT_CTX *mont=NULL;\n\tint numPowers;\n\tunsigned char *powerbufFree=NULL;\n\tint powerbufLen = 0;\n\tunsigned char *powerbuf=NULL;\n\tBIGNUM *computeTemp=NULL, *am=NULL;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\ttop = m->top;\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME,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\tret = BN_one(rr);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\tr = BN_CTX_get(ctx);\n\tif (r == 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\twindow = BN_window_bits_for_ctime_exponent_size(bits);\n\tnumPowers = 1 << window;\n\tpowerbufLen = sizeof(m->d[0])*top*numPowers;\n\tif ((powerbufFree=(unsigned char*)OPENSSL_malloc(powerbufLen+MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL)\n\t\tgoto err;\n\tpowerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n\tmemset(powerbuf, 0, powerbufLen);\n \tif (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;\n\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(r, top, powerbuf, 0, numPowers)) goto err;\n\tcomputeTemp = BN_CTX_get(ctx);\n\tam = BN_CTX_get(ctx);\n\tif (computeTemp==NULL || am==NULL) goto err;\n\tif (a->neg || BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tif (!BN_mod(am,a,m,ctx))\n\t\t\tgoto err;\n\t\taa= am;\n\t\t}\n\telse\n\t\taa=a;\n\tif (!BN_to_montgomery(am,aa,mont,ctx)) goto err;\n\tif (!BN_copy(computeTemp, am)) goto err;\n\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(am, top, powerbuf, 1, numPowers)) goto err;\n\tif (window > 1)\n\t\t{\n\t\tfor (i=2; i<numPowers; i++)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(computeTemp,am,computeTemp,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(computeTemp, top, powerbuf, i, numPowers)) goto err;\n\t\t\t}\n\t\t}\n \tbits = ((bits+window-1)/window)*window;\n \tidx=bits-1;\n \twhile (idx >= 0)\n \t\t{\n \t\twvalue=0;\n \t\tfor (i=0; i<window; i++,idx--)\n \t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\tgoto err;\n\t\t\twvalue = (wvalue<<1)+BN_is_bit_set(p,idx);\n \t\t\t}\n\t\tif (!MOD_EXP_CTIME_COPY_FROM_PREBUF(computeTemp, top, powerbuf, wvalue, numPowers)) goto err;\n \t\tif (!BN_mod_mul_montgomery(r,r,computeTemp,mont,ctx)) goto err;\n \t\t}\n\tif (!BN_from_montgomery(rr,r,mont,ctx)) goto err;\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tif (powerbuf!=NULL)\n\t\t{\n\t\tOPENSSL_cleanse(powerbuf,powerbufLen);\n\t\tOPENSSL_free(powerbufFree);\n\t\t}\n \tif (am!=NULL) BN_clear(am);\n \tif (computeTemp!=NULL) BN_clear(computeTemp);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_START,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tint ret = 0;\n\tBIGNUM *Ri,*R;\n\tBN_CTX_start(ctx);\n\tif((Ri = BN_CTX_get(ctx)) == NULL) goto err;\n\tR= &(mont->RR);\n\tif (!BN_copy(&(mont->N),mod)) goto err;\n\tmont->N.neg = 0;\n#ifdef MONT_WORD\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\ttmod.d=buf;\n\t\ttmod.dmax=2;\n\t\ttmod.neg=0;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n#if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,2*BN_BITS2))) goto err;\n\t\t\t\t\t\t\t\ttmod.top=0;\n\t\tif ((buf[0] = mod->d[0]))\t\t\ttmod.top=1;\n\t\tif ((buf[1] = mod->top>1 ? mod->d[1] : 0))\ttmod.top=2;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,2*BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bn_expand(Ri,(int)sizeof(BN_ULONG)*2) == NULL)\n\t\t\t\tgoto err;\n\t\t\tRi->neg=0;\n\t\t\tRi->d[0]=BN_MASK2;\n\t\t\tRi->d[1]=BN_MASK2;\n\t\t\tRi->top=2;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n#else\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,BN_BITS2))) goto err;\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.top = buf[0] != 0 ? 1 : 0;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_set_word(Ri,BN_MASK2)) goto err;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = 0;\n#endif\n\t\t}\n#else\n\t\t{\n\t\tmont->ri=BN_num_bits(&mont->N);\n\t\tBN_zero(R);\n\t\tif (!BN_set_bit(R,mont->ri)) goto err;\n\t\tif ((BN_mod_inverse(Ri,R,&mont->N,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,mont->ri)) goto err;\n\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\tif (!BN_div(&(mont->Ni),NULL,Ri,&mont->N,ctx)) goto err;\n\t\t}\n#endif\n\tBN_zero(&(mont->RR));\n\tif (!BN_set_bit(&(mont->RR),mont->ri*2)) goto err;\n\tif (!BN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx)) goto err;\n\tret = 1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn ret;\n\t}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tif ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_mod_inverse_no_branch(in, a, n, ctx);\n\t\t}\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tif (!BN_nnmod(B, B, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\tif (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))\n\t\t{\n\t\tint shift;\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(B, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(X))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(X, X, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(X, X)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(B, B, shift)) goto err;\n\t\t\t\t}\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(A, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(Y))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(Y, Y, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(Y, Y)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(A, A, shift)) goto err;\n\t\t\t\t}\n\t\t\tif (BN_ucmp(B, A) >= 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(X, X, Y)) goto err;\n\t\t\t\tif (!BN_usub(B, B, A)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(Y, Y, X)) goto err;\n\t\t\t\tif (!BN_usub(A, A, B)) goto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tBIGNUM *tmp;\n\t\t\tif (BN_num_bits(A) == BN_num_bits(B))\n\t\t\t\t{\n\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t}\n\t\t\telse if (BN_num_bits(A) == BN_num_bits(B) + 1)\n\t\t\t\t{\n\t\t\t\tif (!BN_lshift1(T,B)) goto err;\n\t\t\t\tif (BN_ucmp(A,T) < 0)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_sub(M,A,T)) goto err;\n\t\t\t\t\tif (!BN_add(D,T,B)) goto err;\n\t\t\t\t\tif (BN_ucmp(A,D) < 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,2)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,3)) goto err;\n\t\t\t\t\t\tif (!BN_sub(M,M,B)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\t\t\t}\n\t\t\ttmp=A;\n\t\t\tA=B;\n\t\t\tB=M;\n\t\t\tif (BN_is_one(D))\n\t\t\t\t{\n\t\t\t\tif (!BN_add(tmp,X,Y)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (BN_is_word(D,2))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift1(tmp,X)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (BN_is_word(D,4))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift(tmp,X,2)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (D->top == 1)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_copy(tmp,X)) goto err;\n\t\t\t\t\tif (!BN_mul_word(tmp,D->d[0])) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\t\t\t}\n\t\t\tM=Y;\n\t\t\tY=X;\n\t\t\tX=tmp;\n\t\t\tsign = -sign;\n\t\t\t}\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM local_A, local_B;\n\tBIGNUM *pA, *pB;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tpB = &local_B;\n\t\tBN_with_flags(pB, B, BN_FLG_CONSTTIME);\n\t\tif (!BN_nnmod(B, pB, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tBIGNUM *tmp;\n\t\tpA = &local_A;\n\t\tBN_with_flags(pA, A, BN_FLG_CONSTTIME);\n\t\tif (!BN_div(D,M,pA,B,ctx)) goto err;\n\t\ttmp=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=tmp;\n\t\tsign = -sign;\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n\tif (!(BN_mod(r,m,d,ctx)))\n\t\treturn 0;\n\tif (!r->neg)\n\t\treturn 1;\n\treturn (d->neg ? BN_sub : BN_add)(r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_div_no_branch(dv, rm, num, divisor, ctx);\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\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\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\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\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\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_NO_BRANCH,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}'] |
2,222 | 0 | https://github.com/libav/libav/blob/7fa70598e83cca650717d02ac96bcf55e9f97c19/libavcodec/qdm2.c/#L1221 | static void qdm2_decode_super_block (QDM2Context *q)
{
GetBitContext gb;
QDM2SubPacket header, *packet;
int i, packet_bytes, sub_packet_size, sub_packets_D;
unsigned int next_index = 0;
memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1));
memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid));
memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2));
q->sub_packets_B = 0;
sub_packets_D = 0;
average_quantized_coeffs(q);
init_get_bits(&gb, q->compressed_data, q->compressed_size*8);
qdm2_decode_sub_packet_header(&gb, &header);
if (header.type < 2 || header.type >= 8) {
q->has_errors = 1;
av_log(NULL,AV_LOG_ERROR,"bad superblock type\n");
return;
}
q->superblocktype_2_3 = (header.type == 2 || header.type == 3);
packet_bytes = (q->compressed_size - get_bits_count(&gb) / 8);
init_get_bits(&gb, header.data, header.size*8);
if (header.type == 2 || header.type == 4 || header.type == 5) {
int csum = 257 * get_bits(&gb, 8) + 2 * get_bits(&gb, 8);
csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);
if (csum != 0) {
q->has_errors = 1;
av_log(NULL,AV_LOG_ERROR,"bad packet checksum\n");
return;
}
}
q->sub_packet_list_B[0].packet = NULL;
q->sub_packet_list_D[0].packet = NULL;
for (i = 0; i < 6; i++)
if (--q->fft_level_exp[i] < 0)
q->fft_level_exp[i] = 0;
for (i = 0; packet_bytes > 0; i++) {
int j;
q->sub_packet_list_A[i].next = NULL;
if (i > 0) {
q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];
init_get_bits(&gb, header.data, header.size*8);
skip_bits(&gb, next_index*8);
if (next_index >= header.size)
break;
}
packet = &q->sub_packets[i];
qdm2_decode_sub_packet_header(&gb, packet);
next_index = packet->size + get_bits_count(&gb) / 8;
sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;
if (packet->type == 0)
break;
if (sub_packet_size > packet_bytes) {
if (packet->type != 10 && packet->type != 11 && packet->type != 12)
break;
packet->size += packet_bytes - sub_packet_size;
}
packet_bytes -= sub_packet_size;
q->sub_packet_list_A[i].packet = packet;
if (packet->type == 8) {
SAMPLES_NEEDED_2("packet type 8");
return;
} else if (packet->type >= 9 && packet->type <= 12) {
QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);
} else if (packet->type == 13) {
for (j = 0; j < 6; j++)
q->fft_level_exp[j] = get_bits(&gb, 6);
} else if (packet->type == 14) {
for (j = 0; j < 6; j++)
q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2);
} else if (packet->type == 15) {
SAMPLES_NEEDED_2("packet type 15")
return;
} else if (packet->type >= 16 && packet->type < 48 && !fft_subpackets[packet->type - 16]) {
QDM2_LIST_ADD(q->sub_packet_list_B, q->sub_packets_B, packet);
}
}
if (q->sub_packet_list_D[0].packet != NULL) {
process_synthesis_subpackets(q, q->sub_packet_list_D);
q->do_synth_filter = 1;
} else if (q->do_synth_filter) {
process_subpacket_10(q, NULL, 0);
process_subpacket_11(q, NULL, 0);
process_subpacket_12(q, NULL, 0);
}
} | ['static void qdm2_decode_super_block (QDM2Context *q)\n{\n GetBitContext gb;\n QDM2SubPacket header, *packet;\n int i, packet_bytes, sub_packet_size, sub_packets_D;\n unsigned int next_index = 0;\n memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1));\n memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid));\n memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2));\n q->sub_packets_B = 0;\n sub_packets_D = 0;\n average_quantized_coeffs(q);\n init_get_bits(&gb, q->compressed_data, q->compressed_size*8);\n qdm2_decode_sub_packet_header(&gb, &header);\n if (header.type < 2 || header.type >= 8) {\n q->has_errors = 1;\n av_log(NULL,AV_LOG_ERROR,"bad superblock type\\n");\n return;\n }\n q->superblocktype_2_3 = (header.type == 2 || header.type == 3);\n packet_bytes = (q->compressed_size - get_bits_count(&gb) / 8);\n init_get_bits(&gb, header.data, header.size*8);\n if (header.type == 2 || header.type == 4 || header.type == 5) {\n int csum = 257 * get_bits(&gb, 8) + 2 * get_bits(&gb, 8);\n csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);\n if (csum != 0) {\n q->has_errors = 1;\n av_log(NULL,AV_LOG_ERROR,"bad packet checksum\\n");\n return;\n }\n }\n q->sub_packet_list_B[0].packet = NULL;\n q->sub_packet_list_D[0].packet = NULL;\n for (i = 0; i < 6; i++)\n if (--q->fft_level_exp[i] < 0)\n q->fft_level_exp[i] = 0;\n for (i = 0; packet_bytes > 0; i++) {\n int j;\n q->sub_packet_list_A[i].next = NULL;\n if (i > 0) {\n q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];\n init_get_bits(&gb, header.data, header.size*8);\n skip_bits(&gb, next_index*8);\n if (next_index >= header.size)\n break;\n }\n packet = &q->sub_packets[i];\n qdm2_decode_sub_packet_header(&gb, packet);\n next_index = packet->size + get_bits_count(&gb) / 8;\n sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;\n if (packet->type == 0)\n break;\n if (sub_packet_size > packet_bytes) {\n if (packet->type != 10 && packet->type != 11 && packet->type != 12)\n break;\n packet->size += packet_bytes - sub_packet_size;\n }\n packet_bytes -= sub_packet_size;\n q->sub_packet_list_A[i].packet = packet;\n if (packet->type == 8) {\n SAMPLES_NEEDED_2("packet type 8");\n return;\n } else if (packet->type >= 9 && packet->type <= 12) {\n QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);\n } else if (packet->type == 13) {\n for (j = 0; j < 6; j++)\n q->fft_level_exp[j] = get_bits(&gb, 6);\n } else if (packet->type == 14) {\n for (j = 0; j < 6; j++)\n q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2);\n } else if (packet->type == 15) {\n SAMPLES_NEEDED_2("packet type 15")\n return;\n } else if (packet->type >= 16 && packet->type < 48 && !fft_subpackets[packet->type - 16]) {\n QDM2_LIST_ADD(q->sub_packet_list_B, q->sub_packets_B, packet);\n }\n }\n if (q->sub_packet_list_D[0].packet != NULL) {\n process_synthesis_subpackets(q, q->sub_packet_list_D);\n q->do_synth_filter = 1;\n } else if (q->do_synth_filter) {\n process_subpacket_10(q, NULL, 0);\n process_subpacket_11(q, NULL, 0);\n process_subpacket_12(q, NULL, 0);\n }\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 LIBMPEG2_BITSTREAM_READER\n s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));\n s->bit_count = 16 + 8*((intptr_t)buffer&1);\n skip_bits_long(s, 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 void qdm2_decode_sub_packet_header (GetBitContext *gb, QDM2SubPacket *sub_packet)\n{\n sub_packet->type = get_bits (gb, 8);\n if (sub_packet->type == 0) {\n sub_packet->size = 0;\n sub_packet->data = NULL;\n } else {\n sub_packet->size = get_bits (gb, 8);\n if (sub_packet->type & 0x80) {\n sub_packet->size <<= 8;\n sub_packet->size |= get_bits (gb, 8);\n sub_packet->type &= 0x7f;\n }\n if (sub_packet->type == 0x7f)\n sub_packet->type |= (get_bits (gb, 8) << 8);\n sub_packet->data = &gb->buffer[get_bits_count(gb) / 8];\n }\n av_log(NULL,AV_LOG_DEBUG,"Subpacket: type=%d size=%d start_offs=%x\\n",\n sub_packet->type, sub_packet->size, get_bits_count(gb) / 8);\n}', 'static inline unsigned int get_bits(GetBitContext *s, int n){\n register int tmp;\n OPEN_READER(re, s)\n UPDATE_CACHE(re, s)\n tmp= SHOW_UBITS(re, s, n);\n LAST_SKIP_BITS(re, s, n)\n CLOSE_READER(re, s)\n return tmp;\n}'] |
2,223 | 0 | https://github.com/libav/libav/blob/5bf2ac2b37ae17df7f2bd541801bec8c049b8d2c/libavformat/utils.c/#L2670 | void avformat_free_context(AVFormatContext *s)
{
int i;
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];
if (st->parser) {
av_parser_close(st->parser);
av_free_packet(&st->cur_pkt);
}
av_dict_free(&st->metadata);
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_free(s);
} | ['static av_cold void uninit(AVFilterContext *ctx)\n{\n MovieContext *movie = ctx->priv;\n av_free(movie->file_name);\n av_free(movie->format_name);\n if (movie->codec_ctx)\n avcodec_close(movie->codec_ctx);\n if (movie->format_ctx)\n avformat_close_input(&movie->format_ctx);\n avfilter_unref_buffer(movie->picref);\n av_freep(&movie->frame);\n}', 'int avcodec_close(AVCodecContext *avctx)\n{\n if (ff_lockmgr_cb) {\n if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))\n return -1;\n }\n entangled_thread_counter++;\n if(entangled_thread_counter != 1){\n av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\\n");\n entangled_thread_counter--;\n return -1;\n }\n if (HAVE_THREADS && avctx->thread_opaque)\n ff_thread_free(avctx);\n if (avctx->codec && avctx->codec->close)\n avctx->codec->close(avctx);\n avcodec_default_free_buffers(avctx);\n avctx->coded_frame = NULL;\n av_freep(&avctx->internal);\n if (avctx->codec && avctx->codec->priv_class)\n av_opt_free(avctx->priv_data);\n av_opt_free(avctx);\n av_freep(&avctx->priv_data);\n if(avctx->codec && avctx->codec->encode)\n av_freep(&avctx->extradata);\n avctx->codec = NULL;\n avctx->active_thread_type = 0;\n entangled_thread_counter--;\n if (ff_lockmgr_cb) {\n (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);\n }\n return 0;\n}', 'void avcodec_default_free_buffers(AVCodecContext *avctx)\n{\n switch (avctx->codec_type) {\n case AVMEDIA_TYPE_VIDEO:\n video_free_buffers(avctx);\n break;\n case AVMEDIA_TYPE_AUDIO:\n audio_free_buffers(avctx);\n break;\n default:\n break;\n }\n}', 'void avformat_close_input(AVFormatContext **ps)\n{\n AVFormatContext *s = *ps;\n AVIOContext *pb = (s->iformat->flags & AVFMT_NOFILE) || (s->flags & AVFMT_FLAG_CUSTOM_IO) ?\n NULL : s->pb;\n flush_packet_queue(s);\n if (s->iformat->read_close)\n s->iformat->read_close(s);\n avformat_free_context(s);\n *ps = NULL;\n if (pb)\n avio_close(pb);\n}', 'void avformat_free_context(AVFormatContext *s)\n{\n int i;\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 if (st->parser) {\n av_parser_close(st->parser);\n av_free_packet(&st->cur_pkt);\n }\n av_dict_free(&st->metadata);\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_free(s);\n}'] |
2,224 | 0 | https://github.com/openssl/openssl/blob/207c7df746ca5c3cad6ce71e6cf2263d4183d8be/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];
} | ['static int RSA_eay_public_encrypt(int flen, unsigned char *from,\n\t unsigned char *to, RSA *rsa, int padding)\n\t{\n\tBIGNUM f,ret;\n\tint i,j,k,num=0,r= -1;\n\tunsigned char *buf=NULL;\n\tBN_CTX *ctx=NULL;\n\tBN_init(&f);\n\tBN_init(&ret);\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tnum=BN_num_bytes(rsa->n);\n\tif ((buf=(unsigned char *)Malloc(num)) == NULL)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tswitch (padding)\n\t\t{\n\tcase RSA_PKCS1_PADDING:\n\t\ti=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);\n\t\tbreak;\n#ifndef NO_SHA\n\tcase RSA_PKCS1_OAEP_PADDING:\n\t i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);\n\t\tbreak;\n#endif\n\tcase RSA_SSLV23_PADDING:\n\t\ti=RSA_padding_add_SSLv23(buf,num,from,flen);\n\t\tbreak;\n\tcase RSA_NO_PADDING:\n\t\ti=RSA_padding_add_none(buf,num,from,flen);\n\t\tbreak;\n\tdefault:\n\t\tRSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);\n\t\tgoto err;\n\t\t}\n\tif (i <= 0) goto err;\n\tif (BN_bin2bn(buf,num,&f) == NULL) goto err;\n\tif ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))\n\t\t{\n\t\tif ((rsa->_method_mod_n=BN_MONT_CTX_new()) != NULL)\n\t\t\tif (!BN_MONT_CTX_set(rsa->_method_mod_n,rsa->n,ctx))\n\t\t\t goto err;\n\t\t}\n\tif (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,\n\t\trsa->_method_mod_n)) goto err;\n\tj=BN_num_bytes(&ret);\n\ti=BN_bn2bin(&ret,&(to[num-j]));\n\tfor (k=0; k<(num-i); k++)\n\t\tto[k]=0;\n\tr=num;\nerr:\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\tBN_clear_free(&f);\n\tBN_clear_free(&ret);\n\tif (buf != NULL)\n\t\t{\n\t\tmemset(buf,0,num);\n\t\tFree(buf);\n\t\t}\n\treturn(r);\n\t}', 'BN_CTX *BN_CTX_new(void)\n\t{\n\tBN_CTX *ret;\n\tret=(BN_CTX *)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_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tBIGNUM Ri,*R;\n\tBN_init(&Ri);\n\tR= &(mont->RR);\n\tBN_copy(&(mont->N),mod);\n#ifdef MONT_WORD\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,BN_BITS2);\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.d=buf;\n\t\ttmod.top=1;\n\t\ttmod.max=2;\n\t\ttmod.neg=mod->neg;\n\t\tif ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,BN_BITS2);\n\t\tif (!BN_is_zero(&Ri))\n\t\t\tBN_sub_word(&Ri,1);\n\t\telse\n\t\t\tBN_set_word(&Ri,BN_MASK2);\n\t\tBN_div(&Ri,NULL,&Ri,&tmod,ctx);\n\t\tmont->n0=Ri.d[0];\n\t\tBN_free(&Ri);\n\t\t}\n#else\n\t\t{\n\t\tmont->ri=BN_num_bits(mod);\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,mont->ri);\n\t\tif ((BN_mod_inverse(&Ri,R,mod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,mont->ri);\n\t\tBN_sub_word(&Ri,1);\n\t\tBN_div(&(mont->Ni),NULL,&Ri,mod,ctx);\n\t\tBN_free(&Ri);\n\t\t}\n#endif\n\tBN_zero(&(mont->RR));\n\tBN_set_bit(&(mont->RR),mont->ri*2);\n\tBN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx);\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'BIGNUM *BN_mod_inverse(BIGNUM *in, BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*R=NULL;\n\tBIGNUM *T,*ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tif (Y == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_zero(X);\n\tBN_one(Y);\n\tif (BN_copy(A,a) == NULL) goto err;\n\tif (BN_copy(B,n) == NULL) goto err;\n\tsign=1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\tT=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(T,D,X,ctx)) goto err;\n\t\tif (!BN_add(T,T,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=T;\n\t\tsign= -sign;\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{ if (!BN_mod(R,Y,n,ctx)) goto err; }\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\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}'] |
2,225 | 0 | https://github.com/libav/libav/blob/a7da517f6a5c472f46f67dd33bb6b95ccc919923/libavcodec/vp8.c/#L1264 | static av_always_inline
int decode_block_coeffs_internal(VP56RangeCoder *r, int16_t block[16],
uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
int i, uint8_t *token_prob, int16_t qmul[2],
const uint8_t scan[16], int vp7)
{
VP56RangeCoder c = *r;
goto skip_eob;
do {
int coeff;
restart:
if (!vp56_rac_get_prob_branchy(&c, token_prob[0]))
break;
skip_eob:
if (!vp56_rac_get_prob_branchy(&c, token_prob[1])) {
if (++i == 16)
break;
token_prob = probs[i][0];
if (vp7)
goto restart;
goto skip_eob;
}
if (!vp56_rac_get_prob_branchy(&c, token_prob[2])) {
coeff = 1;
token_prob = probs[i + 1][1];
} else {
if (!vp56_rac_get_prob_branchy(&c, token_prob[3])) {
coeff = vp56_rac_get_prob_branchy(&c, token_prob[4]);
if (coeff)
coeff += vp56_rac_get_prob(&c, token_prob[5]);
coeff += 2;
} else {
if (!vp56_rac_get_prob_branchy(&c, token_prob[6])) {
if (!vp56_rac_get_prob_branchy(&c, token_prob[7])) {
coeff = 5 + vp56_rac_get_prob(&c, vp8_dct_cat1_prob[0]);
} else {
coeff = 7;
coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[0]) << 1;
coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[1]);
}
} else {
int a = vp56_rac_get_prob(&c, token_prob[8]);
int b = vp56_rac_get_prob(&c, token_prob[9 + a]);
int cat = (a << 1) + b;
coeff = 3 + (8 << cat);
coeff += vp8_rac_get_coeff(&c, ff_vp8_dct_cat_prob[cat]);
}
}
token_prob = probs[i + 1][2];
}
block[scan[i]] = (vp8_rac_get(&c) ? -coeff : coeff) * qmul[!!i];
} while (++i < 16);
*r = c;
return i;
} | ['static av_always_inline\nint decode_block_coeffs(VP56RangeCoder *c, int16_t block[16],\n uint8_t probs[16][3][NUM_DCT_TOKENS - 1],\n int i, int zero_nhood, int16_t qmul[2],\n const uint8_t scan[16], int vp7)\n{\n uint8_t *token_prob = probs[i][zero_nhood];\n if (!vp56_rac_get_prob_branchy(c, token_prob[0]))\n return 0;\n return vp7 ? vp7_decode_block_coeffs_internal(c, block, probs, i,\n token_prob, qmul, scan)\n : vp8_decode_block_coeffs_internal(c, block, probs, i,\n token_prob, qmul);\n}', 'static int vp8_decode_block_coeffs_internal(VP56RangeCoder *r,\n int16_t block[16],\n uint8_t probs[16][3][NUM_DCT_TOKENS - 1],\n int i, uint8_t *token_prob,\n int16_t qmul[2])\n{\n return decode_block_coeffs_internal(r, block, probs, i,\n token_prob, qmul, ff_zigzag_scan, IS_VP8);\n}', 'static av_always_inline\nint decode_block_coeffs_internal(VP56RangeCoder *r, int16_t block[16],\n uint8_t probs[16][3][NUM_DCT_TOKENS - 1],\n int i, uint8_t *token_prob, int16_t qmul[2],\n const uint8_t scan[16], int vp7)\n{\n VP56RangeCoder c = *r;\n goto skip_eob;\n do {\n int coeff;\nrestart:\n if (!vp56_rac_get_prob_branchy(&c, token_prob[0]))\n break;\nskip_eob:\n if (!vp56_rac_get_prob_branchy(&c, token_prob[1])) {\n if (++i == 16)\n break;\n token_prob = probs[i][0];\n if (vp7)\n goto restart;\n goto skip_eob;\n }\n if (!vp56_rac_get_prob_branchy(&c, token_prob[2])) {\n coeff = 1;\n token_prob = probs[i + 1][1];\n } else {\n if (!vp56_rac_get_prob_branchy(&c, token_prob[3])) {\n coeff = vp56_rac_get_prob_branchy(&c, token_prob[4]);\n if (coeff)\n coeff += vp56_rac_get_prob(&c, token_prob[5]);\n coeff += 2;\n } else {\n if (!vp56_rac_get_prob_branchy(&c, token_prob[6])) {\n if (!vp56_rac_get_prob_branchy(&c, token_prob[7])) {\n coeff = 5 + vp56_rac_get_prob(&c, vp8_dct_cat1_prob[0]);\n } else {\n coeff = 7;\n coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[0]) << 1;\n coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[1]);\n }\n } else {\n int a = vp56_rac_get_prob(&c, token_prob[8]);\n int b = vp56_rac_get_prob(&c, token_prob[9 + a]);\n int cat = (a << 1) + b;\n coeff = 3 + (8 << cat);\n coeff += vp8_rac_get_coeff(&c, ff_vp8_dct_cat_prob[cat]);\n }\n }\n token_prob = probs[i + 1][2];\n }\n block[scan[i]] = (vp8_rac_get(&c) ? -coeff : coeff) * qmul[!!i];\n } while (++i < 16);\n *r = c;\n return i;\n}', 'static av_always_inline int vp56_rac_get_prob(VP56RangeCoder *c, uint8_t prob)\n{\n unsigned int code_word = vp56_rac_renorm(c);\n unsigned int low = 1 + (((c->high - 1) * prob) >> 8);\n unsigned int low_shift = low << 16;\n int bit = code_word >= low_shift;\n c->high = bit ? c->high - low : low;\n c->code_word = bit ? code_word - low_shift : code_word;\n return bit;\n}'] |
2,226 | 0 | https://github.com/openssl/openssl/blob/cca3ea1e71ae90163de515f4d63d92c31e572b07/ssl/d1_both.c/#L995 | int
dtls1_buffer_message(SSL *s, int is_ccs)
{
pitem *item;
hm_fragment *frag;
unsigned char seq64be[8];
OPENSSL_assert(s->init_off == 0);
frag = dtls1_hm_fragment_new(s->init_num);
memcpy(frag->fragment, s->init_buf->data, s->init_num);
if ( is_ccs)
{
OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
((s->version==DTLS1_VERSION)?DTLS1_CCS_HEADER_LENGTH:3) == (unsigned int)s->init_num);
}
else
{
OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num);
}
frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
frag->msg_header.seq = s->d1->w_msg_hdr.seq;
frag->msg_header.type = s->d1->w_msg_hdr.type;
frag->msg_header.frag_off = 0;
frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
frag->msg_header.is_ccs = is_ccs;
frag->msg_header.saved_retransmit_state.enc_write_ctx = s->enc_write_ctx;
frag->msg_header.saved_retransmit_state.write_hash = s->write_hash;
frag->msg_header.saved_retransmit_state.compress = s->compress;
frag->msg_header.saved_retransmit_state.session = s->session;
frag->msg_header.saved_retransmit_state.epoch = s->d1->w_epoch;
memset(seq64be,0,sizeof(seq64be));
seq64be[6] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq,
frag->msg_header.is_ccs)>>8);
seq64be[7] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq,
frag->msg_header.is_ccs));
item = pitem_new(seq64be, frag);
if ( item == NULL)
{
dtls1_hm_fragment_free(frag);
return 0;
}
#if 0
fprintf( stderr, "buffered messge: \ttype = %xx\n", msg_buf->type);
fprintf( stderr, "\t\t\t\t\tlen = %d\n", msg_buf->len);
fprintf( stderr, "\t\t\t\t\tseq_num = %d\n", msg_buf->seq_num);
#endif
pqueue_insert(s->d1->sent_messages, item);
return 1;
} | ['int\ndtls1_buffer_message(SSL *s, int is_ccs)\n\t{\n\tpitem *item;\n\thm_fragment *frag;\n\tunsigned char seq64be[8];\n\tOPENSSL_assert(s->init_off == 0);\n\tfrag = dtls1_hm_fragment_new(s->init_num);\n\tmemcpy(frag->fragment, s->init_buf->data, s->init_num);\n\tif ( is_ccs)\n\t\t{\n\t\tOPENSSL_assert(s->d1->w_msg_hdr.msg_len +\n\t\t\t ((s->version==DTLS1_VERSION)?DTLS1_CCS_HEADER_LENGTH:3) == (unsigned int)s->init_num);\n\t\t}\n\telse\n\t\t{\n\t\tOPENSSL_assert(s->d1->w_msg_hdr.msg_len +\n\t\t\tDTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num);\n\t\t}\n\tfrag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;\n\tfrag->msg_header.seq = s->d1->w_msg_hdr.seq;\n\tfrag->msg_header.type = s->d1->w_msg_hdr.type;\n\tfrag->msg_header.frag_off = 0;\n\tfrag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;\n\tfrag->msg_header.is_ccs = is_ccs;\n\tfrag->msg_header.saved_retransmit_state.enc_write_ctx = s->enc_write_ctx;\n\tfrag->msg_header.saved_retransmit_state.write_hash = s->write_hash;\n\tfrag->msg_header.saved_retransmit_state.compress = s->compress;\n\tfrag->msg_header.saved_retransmit_state.session = s->session;\n\tfrag->msg_header.saved_retransmit_state.epoch = s->d1->w_epoch;\n\tmemset(seq64be,0,sizeof(seq64be));\n\tseq64be[6] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t frag->msg_header.is_ccs)>>8);\n\tseq64be[7] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t frag->msg_header.is_ccs));\n\titem = pitem_new(seq64be, frag);\n\tif ( item == NULL)\n\t\t{\n\t\tdtls1_hm_fragment_free(frag);\n\t\treturn 0;\n\t\t}\n#if 0\n\tfprintf( stderr, "buffered messge: \\ttype = %xx\\n", msg_buf->type);\n\tfprintf( stderr, "\\t\\t\\t\\t\\tlen = %d\\n", msg_buf->len);\n\tfprintf( stderr, "\\t\\t\\t\\t\\tseq_num = %d\\n", msg_buf->seq_num);\n#endif\n\tpqueue_insert(s->d1->sent_messages, item);\n\treturn 1;\n\t}', 'static hm_fragment *\ndtls1_hm_fragment_new(unsigned long frag_len)\n\t{\n\thm_fragment *frag = NULL;\n\tunsigned char *buf = NULL;\n\tfrag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment));\n\tif ( frag == NULL)\n\t\treturn NULL;\n\tif (frag_len)\n\t\t{\n\t\tbuf = (unsigned char *)OPENSSL_malloc(frag_len);\n\t\tif ( buf == NULL)\n\t\t\t{\n\t\t\tOPENSSL_free(frag);\n\t\t\treturn NULL;\n\t\t\t}\n\t\t}\n\tfrag->fragment = buf;\n\treturn frag;\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\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#ifndef OPENSSL_CPUID_OBJ\n if(ret && (num > 2048))\n\t{\textern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\t}\n#endif\n\treturn ret;\n\t}'] |
2,227 | 0 | https://github.com/libav/libav/blob/1de53d006b754c8ecab2f31a223acfaea15924f4/libavcodec/h264_mvpred.h/#L570 | static void fill_decode_caches(H264Context *h, int mb_type)
{
MpegEncContext *const s = &h->s;
int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
int topleft_type, top_type, topright_type, left_type[LEFT_MBS];
const uint8_t *left_block = h->left_block;
int i;
uint8_t *nnz;
uint8_t *nnz_cache;
topleft_xy = h->topleft_mb_xy;
top_xy = h->top_mb_xy;
topright_xy = h->topright_mb_xy;
left_xy[LTOP] = h->left_mb_xy[LTOP];
left_xy[LBOT] = h->left_mb_xy[LBOT];
topleft_type = h->topleft_type;
top_type = h->top_type;
topright_type = h->topright_type;
left_type[LTOP] = h->left_type[LTOP];
left_type[LBOT] = h->left_type[LBOT];
if (!IS_SKIP(mb_type)) {
if (IS_INTRA(mb_type)) {
int type_mask = h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;
h->topleft_samples_available =
h->top_samples_available =
h->left_samples_available = 0xFFFF;
h->topright_samples_available = 0xEEEA;
if (!(top_type & type_mask)) {
h->topleft_samples_available = 0xB3FF;
h->top_samples_available = 0x33FF;
h->topright_samples_available = 0x26EA;
}
if (IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[LTOP])) {
if (IS_INTERLACED(mb_type)) {
if (!(left_type[LTOP] & type_mask)) {
h->topleft_samples_available &= 0xDFFF;
h->left_samples_available &= 0x5FFF;
}
if (!(left_type[LBOT] & type_mask)) {
h->topleft_samples_available &= 0xFF5F;
h->left_samples_available &= 0xFF5F;
}
} else {
int left_typei = s->current_picture.f.mb_type[left_xy[LTOP] + s->mb_stride];
assert(left_xy[LTOP] == left_xy[LBOT]);
if (!((left_typei & type_mask) && (left_type[LTOP] & type_mask))) {
h->topleft_samples_available &= 0xDF5F;
h->left_samples_available &= 0x5F5F;
}
}
} else {
if (!(left_type[LTOP] & type_mask)) {
h->topleft_samples_available &= 0xDF5F;
h->left_samples_available &= 0x5F5F;
}
}
if (!(topleft_type & type_mask))
h->topleft_samples_available &= 0x7FFF;
if (!(topright_type & type_mask))
h->topright_samples_available &= 0xFBFF;
if (IS_INTRA4x4(mb_type)) {
if (IS_INTRA4x4(top_type)) {
AV_COPY32(h->intra4x4_pred_mode_cache + 4 + 8 * 0, h->intra4x4_pred_mode + h->mb2br_xy[top_xy]);
} else {
h->intra4x4_pred_mode_cache[4 + 8 * 0] =
h->intra4x4_pred_mode_cache[5 + 8 * 0] =
h->intra4x4_pred_mode_cache[6 + 8 * 0] =
h->intra4x4_pred_mode_cache[7 + 8 * 0] = 2 - 3 * !(top_type & type_mask);
}
for (i = 0; i < 2; i++) {
if (IS_INTRA4x4(left_type[LEFT(i)])) {
int8_t *mode = h->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]];
h->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] = mode[6 - left_block[0 + 2 * i]];
h->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = mode[6 - left_block[1 + 2 * i]];
} else {
h->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] =
h->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = 2 - 3 * !(left_type[LEFT(i)] & type_mask);
}
}
}
}
nnz_cache = h->non_zero_count_cache;
if (top_type) {
nnz = h->non_zero_count[top_xy];
AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[4 * 3]);
if (!s->chroma_y_shift) {
AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 7]);
AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 11]);
} else {
AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 5]);
AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);
}
} else {
uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;
AV_WN32A(&nnz_cache[4 + 8 * 0], top_empty);
AV_WN32A(&nnz_cache[4 + 8 * 5], top_empty);
AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);
}
for (i = 0; i < 2; i++) {
if (left_type[LEFT(i)]) {
nnz = h->non_zero_count[left_xy[LEFT(i)]];
nnz_cache[3 + 8 * 1 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i]];
nnz_cache[3 + 8 * 2 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i]];
if (CHROMA444) {
nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 4 * 4];
nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 4 * 4];
nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 8 * 4];
nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 8 * 4];
} else if (CHROMA422) {
nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 4 * 4];
nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 4 * 4];
nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 8 * 4];
nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 8 * 4];
} else {
nnz_cache[3 + 8 * 6 + 8 * i] = nnz[left_block[8 + 4 + 2 * i]];
nnz_cache[3 + 8 * 11 + 8 * i] = nnz[left_block[8 + 5 + 2 * i]];
}
} else {
nnz_cache[3 + 8 * 1 + 2 * 8 * i] =
nnz_cache[3 + 8 * 2 + 2 * 8 * i] =
nnz_cache[3 + 8 * 6 + 2 * 8 * i] =
nnz_cache[3 + 8 * 7 + 2 * 8 * i] =
nnz_cache[3 + 8 * 11 + 2 * 8 * i] =
nnz_cache[3 + 8 * 12 + 2 * 8 * i] = CABAC && !IS_INTRA(mb_type) ? 0 : 64;
}
}
if (CABAC) {
if (top_type)
h->top_cbp = h->cbp_table[top_xy];
else
h->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
if (left_type[LTOP]) {
h->left_cbp = (h->cbp_table[left_xy[LTOP]] & 0x7F0) |
((h->cbp_table[left_xy[LTOP]] >> (left_block[0] & (~1))) & 2) |
(((h->cbp_table[left_xy[LBOT]] >> (left_block[2] & (~1))) & 2) << 2);
} else {
h->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
}
}
}
if (IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)) {
int list;
int b_stride = h->b_stride;
for (list = 0; list < h->list_count; list++) {
int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
int8_t *ref = s->current_picture.f.ref_index[list];
int16_t(*mv_cache)[2] = &h->mv_cache[list][scan8[0]];
int16_t(*mv)[2] = s->current_picture.f.motion_val[list];
if (!USES_LIST(mb_type, list))
continue;
assert(!(IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred));
if (USES_LIST(top_type, list)) {
const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
AV_COPY128(mv_cache[0 - 1 * 8], mv[b_xy + 0]);
ref_cache[0 - 1 * 8] =
ref_cache[1 - 1 * 8] = ref[4 * top_xy + 2];
ref_cache[2 - 1 * 8] =
ref_cache[3 - 1 * 8] = ref[4 * top_xy + 3];
} else {
AV_ZERO128(mv_cache[0 - 1 * 8]);
AV_WN32A(&ref_cache[0 - 1 * 8],
((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE) & 0xFF) * 0x01010101u);
}
if (mb_type & (MB_TYPE_16x8 | MB_TYPE_8x8)) {
for (i = 0; i < 2; i++) {
int cache_idx = -1 + i * 2 * 8;
if (USES_LIST(left_type[LEFT(i)], list)) {
const int b_xy = h->mb2b_xy[left_xy[LEFT(i)]] + 3;
const int b8_xy = 4 * left_xy[LEFT(i)] + 1;
AV_COPY32(mv_cache[cache_idx],
mv[b_xy + b_stride * left_block[0 + i * 2]]);
AV_COPY32(mv_cache[cache_idx + 8],
mv[b_xy + b_stride * left_block[1 + i * 2]]);
ref_cache[cache_idx] = ref[b8_xy + (left_block[0 + i * 2] & ~1)];
ref_cache[cache_idx + 8] = ref[b8_xy + (left_block[1 + i * 2] & ~1)];
} else {
AV_ZERO32(mv_cache[cache_idx]);
AV_ZERO32(mv_cache[cache_idx + 8]);
ref_cache[cache_idx] =
ref_cache[cache_idx + 8] = (left_type[LEFT(i)]) ? LIST_NOT_USED
: PART_NOT_AVAILABLE;
}
}
} else {
if (USES_LIST(left_type[LTOP], list)) {
const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
const int b8_xy = 4 * left_xy[LTOP] + 1;
AV_COPY32(mv_cache[-1], mv[b_xy + b_stride * left_block[0]]);
ref_cache[-1] = ref[b8_xy + (left_block[0] & ~1)];
} else {
AV_ZERO32(mv_cache[-1]);
ref_cache[-1] = left_type[LTOP] ? LIST_NOT_USED
: PART_NOT_AVAILABLE;
}
}
if (USES_LIST(topright_type, list)) {
const int b_xy = h->mb2b_xy[topright_xy] + 3 * b_stride;
AV_COPY32(mv_cache[4 - 1 * 8], mv[b_xy]);
ref_cache[4 - 1 * 8] = ref[4 * topright_xy + 2];
} else {
AV_ZERO32(mv_cache[4 - 1 * 8]);
ref_cache[4 - 1 * 8] = topright_type ? LIST_NOT_USED
: PART_NOT_AVAILABLE;
}
if (ref_cache[4 - 1 * 8] < 0) {
if (USES_LIST(topleft_type, list)) {
const int b_xy = h->mb2b_xy[topleft_xy] + 3 + b_stride +
(h->topleft_partition & 2 * b_stride);
const int b8_xy = 4 * topleft_xy + 1 + (h->topleft_partition & 2);
AV_COPY32(mv_cache[-1 - 1 * 8], mv[b_xy]);
ref_cache[-1 - 1 * 8] = ref[b8_xy];
} else {
AV_ZERO32(mv_cache[-1 - 1 * 8]);
ref_cache[-1 - 1 * 8] = topleft_type ? LIST_NOT_USED
: PART_NOT_AVAILABLE;
}
}
if ((mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2)) && !FRAME_MBAFF)
continue;
if (!(mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2))) {
uint8_t(*mvd_cache)[2] = &h->mvd_cache[list][scan8[0]];
uint8_t(*mvd)[2] = h->mvd_table[list];
ref_cache[2 + 8 * 0] =
ref_cache[2 + 8 * 2] = PART_NOT_AVAILABLE;
AV_ZERO32(mv_cache[2 + 8 * 0]);
AV_ZERO32(mv_cache[2 + 8 * 2]);
if (CABAC) {
if (USES_LIST(top_type, list)) {
const int b_xy = h->mb2br_xy[top_xy];
AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);
} else {
AV_ZERO64(mvd_cache[0 - 1 * 8]);
}
if (USES_LIST(left_type[LTOP], list)) {
const int b_xy = h->mb2br_xy[left_xy[LTOP]] + 6;
AV_COPY16(mvd_cache[-1 + 0 * 8], mvd[b_xy - left_block[0]]);
AV_COPY16(mvd_cache[-1 + 1 * 8], mvd[b_xy - left_block[1]]);
} else {
AV_ZERO16(mvd_cache[-1 + 0 * 8]);
AV_ZERO16(mvd_cache[-1 + 1 * 8]);
}
if (USES_LIST(left_type[LBOT], list)) {
const int b_xy = h->mb2br_xy[left_xy[LBOT]] + 6;
AV_COPY16(mvd_cache[-1 + 2 * 8], mvd[b_xy - left_block[2]]);
AV_COPY16(mvd_cache[-1 + 3 * 8], mvd[b_xy - left_block[3]]);
} else {
AV_ZERO16(mvd_cache[-1 + 2 * 8]);
AV_ZERO16(mvd_cache[-1 + 3 * 8]);
}
AV_ZERO16(mvd_cache[2 + 8 * 0]);
AV_ZERO16(mvd_cache[2 + 8 * 2]);
if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
uint8_t *direct_cache = &h->direct_cache[scan8[0]];
uint8_t *direct_table = h->direct_table;
fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16 >> 1, 1);
if (IS_DIRECT(top_type)) {
AV_WN32A(&direct_cache[-1 * 8],
0x01010101u * (MB_TYPE_DIRECT2 >> 1));
} else if (IS_8X8(top_type)) {
int b8_xy = 4 * top_xy;
direct_cache[0 - 1 * 8] = direct_table[b8_xy + 2];
direct_cache[2 - 1 * 8] = direct_table[b8_xy + 3];
} else {
AV_WN32A(&direct_cache[-1 * 8],
0x01010101 * (MB_TYPE_16x16 >> 1));
}
if (IS_DIRECT(left_type[LTOP]))
direct_cache[-1 + 0 * 8] = MB_TYPE_DIRECT2 >> 1;
else if (IS_8X8(left_type[LTOP]))
direct_cache[-1 + 0 * 8] = direct_table[4 * left_xy[LTOP] + 1 + (left_block[0] & ~1)];
else
direct_cache[-1 + 0 * 8] = MB_TYPE_16x16 >> 1;
if (IS_DIRECT(left_type[LBOT]))
direct_cache[-1 + 2 * 8] = MB_TYPE_DIRECT2 >> 1;
else if (IS_8X8(left_type[LBOT]))
direct_cache[-1 + 2 * 8] = direct_table[4 * left_xy[LBOT] + 1 + (left_block[2] & ~1)];
else
direct_cache[-1 + 2 * 8] = MB_TYPE_16x16 >> 1;
}
}
}
#define MAP_MVS \
MAP_F2F(scan8[0] - 1 - 1 * 8, topleft_type) \
MAP_F2F(scan8[0] + 0 - 1 * 8, top_type) \
MAP_F2F(scan8[0] + 1 - 1 * 8, top_type) \
MAP_F2F(scan8[0] + 2 - 1 * 8, top_type) \
MAP_F2F(scan8[0] + 3 - 1 * 8, top_type) \
MAP_F2F(scan8[0] + 4 - 1 * 8, topright_type) \
MAP_F2F(scan8[0] - 1 + 0 * 8, left_type[LTOP]) \
MAP_F2F(scan8[0] - 1 + 1 * 8, left_type[LTOP]) \
MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT]) \
MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])
if (FRAME_MBAFF) {
if (MB_FIELD) {
#define MAP_F2F(idx, mb_type) \
if (!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0) { \
h->ref_cache[list][idx] <<= 1; \
h->mv_cache[list][idx][1] /= 2; \
h->mvd_cache[list][idx][1] >>= 1; \
}
MAP_MVS
} else {
#undef MAP_F2F
#define MAP_F2F(idx, mb_type) \
if (IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0) { \
h->ref_cache[list][idx] >>= 1; \
h->mv_cache[list][idx][1] <<= 1; \
h->mvd_cache[list][idx][1] <<= 1; \
}
MAP_MVS
#undef MAP_F2F
}
}
}
}
h->neighbor_transform_size = !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[LTOP]);
} | ['int ff_h264_decode_mb_cabac(H264Context *h) {\n MpegEncContext * const s = &h->s;\n int mb_xy;\n int mb_type, partition_count, cbp = 0;\n int dct8x8_allowed= h->pps.transform_8x8_mode;\n int decode_chroma = h->sps.chroma_format_idc == 1 || h->sps.chroma_format_idc == 2;\n const int pixel_shift = h->pixel_shift;\n mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;\n tprintf(s->avctx, "pic:%d mb:%d/%d\\n", h->frame_num, s->mb_x, s->mb_y);\n if( h->slice_type_nos != AV_PICTURE_TYPE_I ) {\n int skip;\n if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )\n skip = h->next_mb_skipped;\n else\n skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );\n if( skip ) {\n if( FRAME_MBAFF && (s->mb_y&1)==0 ){\n s->current_picture.f.mb_type[mb_xy] = MB_TYPE_SKIP;\n h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );\n if(!h->next_mb_skipped)\n h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);\n }\n decode_mb_skip(h);\n h->cbp_table[mb_xy] = 0;\n h->chroma_pred_mode_table[mb_xy] = 0;\n h->last_qscale_diff = 0;\n return 0;\n }\n }\n if(FRAME_MBAFF){\n if( (s->mb_y&1) == 0 )\n h->mb_mbaff =\n h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);\n }\n h->prev_mb_skipped = 0;\n fill_decode_neighbors(h, -(MB_FIELD));\n if( h->slice_type_nos == AV_PICTURE_TYPE_B ) {\n int ctx = 0;\n assert(h->slice_type_nos == AV_PICTURE_TYPE_B);\n if( !IS_DIRECT( h->left_type[LTOP]-1 ) )\n ctx++;\n if( !IS_DIRECT( h->top_type-1 ) )\n ctx++;\n if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) ){\n mb_type= 0;\n }else if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {\n mb_type= 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );\n }else{\n int bits;\n bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;\n bits+= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;\n bits+= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;\n bits+= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );\n if( bits < 8 ){\n mb_type= bits + 3;\n }else if( bits == 13 ){\n mb_type= decode_cabac_intra_mb_type(h, 32, 0);\n goto decode_intra_mb;\n }else if( bits == 14 ){\n mb_type= 11;\n }else if( bits == 15 ){\n mb_type= 22;\n }else{\n bits= ( bits<<1 ) + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );\n mb_type= bits - 4;\n }\n }\n partition_count= b_mb_type_info[mb_type].partition_count;\n mb_type= b_mb_type_info[mb_type].type;\n } else if( h->slice_type_nos == AV_PICTURE_TYPE_P ) {\n if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {\n if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {\n mb_type= 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );\n } else {\n mb_type= 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );\n }\n partition_count= p_mb_type_info[mb_type].partition_count;\n mb_type= p_mb_type_info[mb_type].type;\n } else {\n mb_type= decode_cabac_intra_mb_type(h, 17, 0);\n goto decode_intra_mb;\n }\n } else {\n mb_type= decode_cabac_intra_mb_type(h, 3, 1);\n if(h->slice_type == AV_PICTURE_TYPE_SI && mb_type)\n mb_type--;\n assert(h->slice_type_nos == AV_PICTURE_TYPE_I);\ndecode_intra_mb:\n partition_count = 0;\n cbp= i_mb_type_info[mb_type].cbp;\n h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;\n mb_type= i_mb_type_info[mb_type].type;\n }\n if(MB_FIELD)\n mb_type |= MB_TYPE_INTERLACED;\n h->slice_table[ mb_xy ]= h->slice_num;\n if(IS_INTRA_PCM(mb_type)) {\n const int mb_size = ff_h264_mb_sizes[h->sps.chroma_format_idc] *\n h->sps.bit_depth_luma >> 3;\n const uint8_t *ptr;\n ptr= h->cabac.bytestream;\n if(h->cabac.low&0x1) ptr--;\n if(CABAC_BITS==16){\n if(h->cabac.low&0x1FF) ptr--;\n }\n if ((int) (h->cabac.bytestream_end - ptr) < mb_size)\n return -1;\n memcpy(h->mb, ptr, mb_size); ptr+=mb_size;\n ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);\n h->cbp_table[mb_xy] = 0xf7ef;\n h->chroma_pred_mode_table[mb_xy] = 0;\n s->current_picture.f.qscale_table[mb_xy] = 0;\n memset(h->non_zero_count[mb_xy], 16, 48);\n s->current_picture.f.mb_type[mb_xy] = mb_type;\n h->last_qscale_diff = 0;\n return 0;\n }\n if(MB_MBAFF){\n h->ref_count[0] <<= 1;\n h->ref_count[1] <<= 1;\n }\n fill_decode_caches(h, mb_type);\n if( IS_INTRA( mb_type ) ) {\n int i, pred_mode;\n if( IS_INTRA4x4( mb_type ) ) {\n if( dct8x8_allowed && get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] ) ) {\n mb_type |= MB_TYPE_8x8DCT;\n for( i = 0; i < 16; i+=4 ) {\n int pred = pred_intra_mode( h, i );\n int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );\n fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );\n }\n } else {\n for( i = 0; i < 16; i++ ) {\n int pred = pred_intra_mode( h, i );\n h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );\n }\n }\n write_back_intra_pred_mode(h);\n if( ff_h264_check_intra4x4_pred_mode(h) < 0 ) return -1;\n } else {\n h->intra16x16_pred_mode= ff_h264_check_intra_pred_mode( h, h->intra16x16_pred_mode, 0 );\n if( h->intra16x16_pred_mode < 0 ) return -1;\n }\n if(decode_chroma){\n h->chroma_pred_mode_table[mb_xy] =\n pred_mode = decode_cabac_mb_chroma_pre_mode( h );\n pred_mode= ff_h264_check_intra_pred_mode( h, pred_mode, 1 );\n if( pred_mode < 0 ) return -1;\n h->chroma_pred_mode= pred_mode;\n } else {\n h->chroma_pred_mode= DC_128_PRED8x8;\n }\n } else if( partition_count == 4 ) {\n int i, j, sub_partition_count[4], list, ref[2][4];\n if( h->slice_type_nos == AV_PICTURE_TYPE_B ) {\n for( i = 0; i < 4; i++ ) {\n h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );\n sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;\n h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;\n }\n if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |\n h->sub_mb_type[2] | h->sub_mb_type[3]) ) {\n ff_h264_pred_direct_motion(h, &mb_type);\n h->ref_cache[0][scan8[4]] =\n h->ref_cache[1][scan8[4]] =\n h->ref_cache[0][scan8[12]] =\n h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;\n for( i = 0; i < 4; i++ )\n fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, (h->sub_mb_type[i]>>1)&0xFF, 1 );\n }\n } else {\n for( i = 0; i < 4; i++ ) {\n h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );\n sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;\n h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;\n }\n }\n for( list = 0; list < h->list_count; list++ ) {\n for( i = 0; i < 4; i++ ) {\n if(IS_DIRECT(h->sub_mb_type[i])) continue;\n if(IS_DIR(h->sub_mb_type[i], 0, list)){\n if( h->ref_count[list] > 1 ){\n ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );\n if(ref[list][i] >= (unsigned)h->ref_count[list]){\n av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\\n", ref[list][i], h->ref_count[list]);\n return -1;\n }\n }else\n ref[list][i] = 0;\n } else {\n ref[list][i] = -1;\n }\n h->ref_cache[list][ scan8[4*i]+1 ]=\n h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];\n }\n }\n if(dct8x8_allowed)\n dct8x8_allowed = get_dct8x8_allowed(h);\n for(list=0; list<h->list_count; list++){\n for(i=0; i<4; i++){\n h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];\n if(IS_DIRECT(h->sub_mb_type[i])){\n fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 2);\n continue;\n }\n if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){\n const int sub_mb_type= h->sub_mb_type[i];\n const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;\n for(j=0; j<sub_partition_count[i]; j++){\n int mpx, mpy;\n int mx, my;\n const int index= 4*i + block_width*j;\n int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];\n uint8_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];\n pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);\n DECODE_CABAC_MB_MVD( h, list, index)\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n if(IS_SUB_8X8(sub_mb_type)){\n mv_cache[ 1 ][0]=\n mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;\n mv_cache[ 1 ][1]=\n mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;\n mvd_cache[ 1 ][0]=\n mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mpx;\n mvd_cache[ 1 ][1]=\n mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= mpy;\n }else if(IS_SUB_8X4(sub_mb_type)){\n mv_cache[ 1 ][0]= mx;\n mv_cache[ 1 ][1]= my;\n mvd_cache[ 1 ][0]= mpx;\n mvd_cache[ 1 ][1]= mpy;\n }else if(IS_SUB_4X8(sub_mb_type)){\n mv_cache[ 8 ][0]= mx;\n mv_cache[ 8 ][1]= my;\n mvd_cache[ 8 ][0]= mpx;\n mvd_cache[ 8 ][1]= mpy;\n }\n mv_cache[ 0 ][0]= mx;\n mv_cache[ 0 ][1]= my;\n mvd_cache[ 0 ][0]= mpx;\n mvd_cache[ 0 ][1]= mpy;\n }\n }else{\n fill_rectangle(h->mv_cache [list][ scan8[4*i] ], 2, 2, 8, 0, 4);\n fill_rectangle(h->mvd_cache[list][ scan8[4*i] ], 2, 2, 8, 0, 2);\n }\n }\n }\n } else if( IS_DIRECT(mb_type) ) {\n ff_h264_pred_direct_motion(h, &mb_type);\n fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 2);\n fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 2);\n dct8x8_allowed &= h->sps.direct_8x8_inference_flag;\n } else {\n int list, i;\n if(IS_16X16(mb_type)){\n for(list=0; list<h->list_count; list++){\n if(IS_DIR(mb_type, 0, list)){\n int ref;\n if(h->ref_count[list] > 1){\n ref= decode_cabac_mb_ref(h, list, 0);\n if(ref >= (unsigned)h->ref_count[list]){\n av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\\n", ref, h->ref_count[list]);\n return -1;\n }\n }else\n ref=0;\n fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);\n }\n }\n for(list=0; list<h->list_count; list++){\n if(IS_DIR(mb_type, 0, list)){\n int mx,my,mpx,mpy;\n pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);\n DECODE_CABAC_MB_MVD( h, list, 0)\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack8to16(mpx,mpy), 2);\n fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);\n }\n }\n }\n else if(IS_16X8(mb_type)){\n for(list=0; list<h->list_count; list++){\n for(i=0; i<2; i++){\n if(IS_DIR(mb_type, i, list)){\n int ref;\n if(h->ref_count[list] > 1){\n ref= decode_cabac_mb_ref( h, list, 8*i );\n if(ref >= (unsigned)h->ref_count[list]){\n av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\\n", ref, h->ref_count[list]);\n return -1;\n }\n }else\n ref=0;\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);\n }else\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);\n }\n }\n for(list=0; list<h->list_count; list++){\n for(i=0; i<2; i++){\n if(IS_DIR(mb_type, i, list)){\n int mx,my,mpx,mpy;\n pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);\n DECODE_CABAC_MB_MVD( h, list, 8*i)\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack8to16(mpx,mpy), 2);\n fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);\n }else{\n fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 2);\n fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);\n }\n }\n }\n }else{\n assert(IS_8X16(mb_type));\n for(list=0; list<h->list_count; list++){\n for(i=0; i<2; i++){\n if(IS_DIR(mb_type, i, list)){\n int ref;\n if(h->ref_count[list] > 1){\n ref= decode_cabac_mb_ref( h, list, 4*i );\n if(ref >= (unsigned)h->ref_count[list]){\n av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\\n", ref, h->ref_count[list]);\n return -1;\n }\n }else\n ref=0;\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);\n }else\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);\n }\n }\n for(list=0; list<h->list_count; list++){\n for(i=0; i<2; i++){\n if(IS_DIR(mb_type, i, list)){\n int mx,my,mpx,mpy;\n pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);\n DECODE_CABAC_MB_MVD( h, list, 4*i)\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack8to16(mpx,mpy), 2);\n fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);\n }else{\n fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 2);\n fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);\n }\n }\n }\n }\n }\n if( IS_INTER( mb_type ) ) {\n h->chroma_pred_mode_table[mb_xy] = 0;\n write_back_motion( h, mb_type );\n }\n if( !IS_INTRA16x16( mb_type ) ) {\n cbp = decode_cabac_mb_cbp_luma( h );\n if(decode_chroma)\n cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;\n }\n h->cbp_table[mb_xy] = h->cbp = cbp;\n if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {\n mb_type |= MB_TYPE_8x8DCT * get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );\n }\n if (CHROMA444 && IS_8x8DCT(mb_type)){\n int i;\n uint8_t *nnz_cache = h->non_zero_count_cache;\n for (i = 0; i < 2; i++){\n if (h->left_type[LEFT(i)] && !IS_8x8DCT(h->left_type[LEFT(i)])){\n nnz_cache[3+8* 1 + 2*8*i]=\n nnz_cache[3+8* 2 + 2*8*i]=\n nnz_cache[3+8* 6 + 2*8*i]=\n nnz_cache[3+8* 7 + 2*8*i]=\n nnz_cache[3+8*11 + 2*8*i]=\n nnz_cache[3+8*12 + 2*8*i]= IS_INTRA(mb_type) ? 64 : 0;\n }\n }\n if (h->top_type && !IS_8x8DCT(h->top_type)){\n uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;\n AV_WN32A(&nnz_cache[4+8* 0], top_empty);\n AV_WN32A(&nnz_cache[4+8* 5], top_empty);\n AV_WN32A(&nnz_cache[4+8*10], top_empty);\n }\n }\n s->current_picture.f.mb_type[mb_xy] = mb_type;\n if( cbp || IS_INTRA16x16( mb_type ) ) {\n const uint8_t *scan, *scan8x8;\n const uint32_t *qmul;\n if(IS_INTERLACED(mb_type)){\n scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;\n scan= s->qscale ? h->field_scan : h->field_scan_q0;\n }else{\n scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;\n scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;\n }\n if(get_cabac_noinline( &h->cabac, &h->cabac_state[60 + (h->last_qscale_diff != 0)])){\n int val = 1;\n int ctx= 2;\n const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);\n while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {\n ctx= 3;\n val++;\n if(val > 2*max_qp){\n av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\\n", s->mb_x, s->mb_y);\n return -1;\n }\n }\n if( val&0x01 )\n val= (val + 1)>>1 ;\n else\n val= -((val + 1)>>1);\n h->last_qscale_diff = val;\n s->qscale += val;\n if(((unsigned)s->qscale) > max_qp){\n if(s->qscale<0) s->qscale+= max_qp+1;\n else s->qscale-= max_qp+1;\n }\n h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);\n h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);\n }else\n h->last_qscale_diff=0;\n decode_cabac_luma_residual(h, scan, scan8x8, pixel_shift, mb_type, cbp, 0);\n if(CHROMA444){\n decode_cabac_luma_residual(h, scan, scan8x8, pixel_shift, mb_type, cbp, 1);\n decode_cabac_luma_residual(h, scan, scan8x8, pixel_shift, mb_type, cbp, 2);\n } else if (CHROMA422) {\n if( cbp&0x30 ){\n int c;\n for( c = 0; c < 2; c++ ) {\n decode_cabac_residual_dc_422(h, h->mb + ((256 + 16*16*c) << pixel_shift), 3,\n CHROMA_DC_BLOCK_INDEX + c,\n chroma422_dc_scan, 8);\n }\n }\n if( cbp&0x20 ) {\n int c, i, i8x8;\n for( c = 0; c < 2; c++ ) {\n DCTELEM *mb = h->mb + (16*(16 + 16*c) << pixel_shift);\n qmul = h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[c]];\n for (i8x8 = 0; i8x8 < 2; i8x8++) {\n for (i = 0; i < 4; i++) {\n const int index = 16 + 16 * c + 8*i8x8 + i;\n decode_cabac_residual_nondc(h, mb, 4, index, scan + 1, qmul, 15);\n mb += 16<<pixel_shift;\n }\n }\n }\n } else {\n fill_rectangle(&h->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1);\n fill_rectangle(&h->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1);\n }\n } else {\n if( cbp&0x30 ){\n int c;\n for( c = 0; c < 2; c++ ) {\n decode_cabac_residual_dc(h, h->mb + ((256 + 16*16*c) << pixel_shift), 3, CHROMA_DC_BLOCK_INDEX+c, chroma_dc_scan, 4);\n }\n }\n if( cbp&0x20 ) {\n int c, i;\n for( c = 0; c < 2; c++ ) {\n qmul = h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[c]];\n for( i = 0; i < 4; i++ ) {\n const int index = 16 + 16 * c + i;\n decode_cabac_residual_nondc(h, h->mb + (16*index << pixel_shift), 4, index, scan + 1, qmul, 15);\n }\n }\n } else {\n fill_rectangle(&h->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1);\n fill_rectangle(&h->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1);\n }\n }\n } else {\n fill_rectangle(&h->non_zero_count_cache[scan8[ 0]], 4, 4, 8, 0, 1);\n fill_rectangle(&h->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1);\n fill_rectangle(&h->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1);\n h->last_qscale_diff = 0;\n }\n s->current_picture.f.qscale_table[mb_xy] = s->qscale;\n write_back_non_zero_count(h);\n if(MB_MBAFF){\n h->ref_count[0] >>= 1;\n h->ref_count[1] >>= 1;\n }\n return 0;\n}', 'static void fill_decode_neighbors(H264Context *h, int mb_type)\n{\n MpegEncContext *const s = &h->s;\n const int mb_xy = h->mb_xy;\n int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];\n static const uint8_t left_block_options[4][32] = {\n { 0, 1, 2, 3, 7, 10, 8, 11, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 5 * 4, 1 + 9 * 4 },\n { 2, 2, 3, 3, 8, 11, 8, 11, 3 + 2 * 4, 3 + 2 * 4, 3 + 3 * 4, 3 + 3 * 4, 1 + 5 * 4, 1 + 9 * 4, 1 + 5 * 4, 1 + 9 * 4 },\n { 0, 0, 1, 1, 7, 10, 7, 10, 3 + 0 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 1 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 4 * 4, 1 + 8 * 4 },\n { 0, 2, 0, 2, 7, 10, 7, 10, 3 + 0 * 4, 3 + 2 * 4, 3 + 0 * 4, 3 + 2 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 4 * 4, 1 + 8 * 4 }\n };\n h->topleft_partition = -1;\n top_xy = mb_xy - (s->mb_stride << MB_FIELD);\n topleft_xy = top_xy - 1;\n topright_xy = top_xy + 1;\n left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;\n h->left_block = left_block_options[0];\n if (FRAME_MBAFF) {\n const int left_mb_field_flag = IS_INTERLACED(s->current_picture.f.mb_type[mb_xy - 1]);\n const int curr_mb_field_flag = IS_INTERLACED(mb_type);\n if (s->mb_y & 1) {\n if (left_mb_field_flag != curr_mb_field_flag) {\n left_xy[LBOT] = left_xy[LTOP] = mb_xy - s->mb_stride - 1;\n if (curr_mb_field_flag) {\n left_xy[LBOT] += s->mb_stride;\n h->left_block = left_block_options[3];\n } else {\n topleft_xy += s->mb_stride;\n h->topleft_partition = 0;\n h->left_block = left_block_options[1];\n }\n }\n } else {\n if (curr_mb_field_flag) {\n topleft_xy += s->mb_stride & (((s->current_picture.f.mb_type[top_xy - 1] >> 7) & 1) - 1);\n topright_xy += s->mb_stride & (((s->current_picture.f.mb_type[top_xy + 1] >> 7) & 1) - 1);\n top_xy += s->mb_stride & (((s->current_picture.f.mb_type[top_xy] >> 7) & 1) - 1);\n }\n if (left_mb_field_flag != curr_mb_field_flag) {\n if (curr_mb_field_flag) {\n left_xy[LBOT] += s->mb_stride;\n h->left_block = left_block_options[3];\n } else {\n h->left_block = left_block_options[2];\n }\n }\n }\n }\n h->topleft_mb_xy = topleft_xy;\n h->top_mb_xy = top_xy;\n h->topright_mb_xy = topright_xy;\n h->left_mb_xy[LTOP] = left_xy[LTOP];\n h->left_mb_xy[LBOT] = left_xy[LBOT];\n h->topleft_type = s->current_picture.f.mb_type[topleft_xy];\n h->top_type = s->current_picture.f.mb_type[top_xy];\n h->topright_type = s->current_picture.f.mb_type[topright_xy];\n h->left_type[LTOP] = s->current_picture.f.mb_type[left_xy[LTOP]];\n h->left_type[LBOT] = s->current_picture.f.mb_type[left_xy[LBOT]];\n if (FMO) {\n if (h->slice_table[topleft_xy] != h->slice_num)\n h->topleft_type = 0;\n if (h->slice_table[top_xy] != h->slice_num)\n h->top_type = 0;\n if (h->slice_table[left_xy[LTOP]] != h->slice_num)\n h->left_type[LTOP] = h->left_type[LBOT] = 0;\n } else {\n if (h->slice_table[topleft_xy] != h->slice_num) {\n h->topleft_type = 0;\n if (h->slice_table[top_xy] != h->slice_num)\n h->top_type = 0;\n if (h->slice_table[left_xy[LTOP]] != h->slice_num)\n h->left_type[LTOP] = h->left_type[LBOT] = 0;\n }\n }\n if (h->slice_table[topright_xy] != h->slice_num)\n h->topright_type = 0;\n}', 'static void fill_decode_caches(H264Context *h, int mb_type)\n{\n MpegEncContext *const s = &h->s;\n int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];\n int topleft_type, top_type, topright_type, left_type[LEFT_MBS];\n const uint8_t *left_block = h->left_block;\n int i;\n uint8_t *nnz;\n uint8_t *nnz_cache;\n topleft_xy = h->topleft_mb_xy;\n top_xy = h->top_mb_xy;\n topright_xy = h->topright_mb_xy;\n left_xy[LTOP] = h->left_mb_xy[LTOP];\n left_xy[LBOT] = h->left_mb_xy[LBOT];\n topleft_type = h->topleft_type;\n top_type = h->top_type;\n topright_type = h->topright_type;\n left_type[LTOP] = h->left_type[LTOP];\n left_type[LBOT] = h->left_type[LBOT];\n if (!IS_SKIP(mb_type)) {\n if (IS_INTRA(mb_type)) {\n int type_mask = h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;\n h->topleft_samples_available =\n h->top_samples_available =\n h->left_samples_available = 0xFFFF;\n h->topright_samples_available = 0xEEEA;\n if (!(top_type & type_mask)) {\n h->topleft_samples_available = 0xB3FF;\n h->top_samples_available = 0x33FF;\n h->topright_samples_available = 0x26EA;\n }\n if (IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[LTOP])) {\n if (IS_INTERLACED(mb_type)) {\n if (!(left_type[LTOP] & type_mask)) {\n h->topleft_samples_available &= 0xDFFF;\n h->left_samples_available &= 0x5FFF;\n }\n if (!(left_type[LBOT] & type_mask)) {\n h->topleft_samples_available &= 0xFF5F;\n h->left_samples_available &= 0xFF5F;\n }\n } else {\n int left_typei = s->current_picture.f.mb_type[left_xy[LTOP] + s->mb_stride];\n assert(left_xy[LTOP] == left_xy[LBOT]);\n if (!((left_typei & type_mask) && (left_type[LTOP] & type_mask))) {\n h->topleft_samples_available &= 0xDF5F;\n h->left_samples_available &= 0x5F5F;\n }\n }\n } else {\n if (!(left_type[LTOP] & type_mask)) {\n h->topleft_samples_available &= 0xDF5F;\n h->left_samples_available &= 0x5F5F;\n }\n }\n if (!(topleft_type & type_mask))\n h->topleft_samples_available &= 0x7FFF;\n if (!(topright_type & type_mask))\n h->topright_samples_available &= 0xFBFF;\n if (IS_INTRA4x4(mb_type)) {\n if (IS_INTRA4x4(top_type)) {\n AV_COPY32(h->intra4x4_pred_mode_cache + 4 + 8 * 0, h->intra4x4_pred_mode + h->mb2br_xy[top_xy]);\n } else {\n h->intra4x4_pred_mode_cache[4 + 8 * 0] =\n h->intra4x4_pred_mode_cache[5 + 8 * 0] =\n h->intra4x4_pred_mode_cache[6 + 8 * 0] =\n h->intra4x4_pred_mode_cache[7 + 8 * 0] = 2 - 3 * !(top_type & type_mask);\n }\n for (i = 0; i < 2; i++) {\n if (IS_INTRA4x4(left_type[LEFT(i)])) {\n int8_t *mode = h->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]];\n h->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] = mode[6 - left_block[0 + 2 * i]];\n h->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = mode[6 - left_block[1 + 2 * i]];\n } else {\n h->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] =\n h->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = 2 - 3 * !(left_type[LEFT(i)] & type_mask);\n }\n }\n }\n }\n nnz_cache = h->non_zero_count_cache;\n if (top_type) {\n nnz = h->non_zero_count[top_xy];\n AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[4 * 3]);\n if (!s->chroma_y_shift) {\n AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 7]);\n AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 11]);\n } else {\n AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 5]);\n AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);\n }\n } else {\n uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;\n AV_WN32A(&nnz_cache[4 + 8 * 0], top_empty);\n AV_WN32A(&nnz_cache[4 + 8 * 5], top_empty);\n AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);\n }\n for (i = 0; i < 2; i++) {\n if (left_type[LEFT(i)]) {\n nnz = h->non_zero_count[left_xy[LEFT(i)]];\n nnz_cache[3 + 8 * 1 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i]];\n nnz_cache[3 + 8 * 2 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i]];\n if (CHROMA444) {\n nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 4 * 4];\n nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 4 * 4];\n nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 8 * 4];\n nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 8 * 4];\n } else if (CHROMA422) {\n nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 4 * 4];\n nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 4 * 4];\n nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 8 * 4];\n nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 8 * 4];\n } else {\n nnz_cache[3 + 8 * 6 + 8 * i] = nnz[left_block[8 + 4 + 2 * i]];\n nnz_cache[3 + 8 * 11 + 8 * i] = nnz[left_block[8 + 5 + 2 * i]];\n }\n } else {\n nnz_cache[3 + 8 * 1 + 2 * 8 * i] =\n nnz_cache[3 + 8 * 2 + 2 * 8 * i] =\n nnz_cache[3 + 8 * 6 + 2 * 8 * i] =\n nnz_cache[3 + 8 * 7 + 2 * 8 * i] =\n nnz_cache[3 + 8 * 11 + 2 * 8 * i] =\n nnz_cache[3 + 8 * 12 + 2 * 8 * i] = CABAC && !IS_INTRA(mb_type) ? 0 : 64;\n }\n }\n if (CABAC) {\n if (top_type)\n h->top_cbp = h->cbp_table[top_xy];\n else\n h->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;\n if (left_type[LTOP]) {\n h->left_cbp = (h->cbp_table[left_xy[LTOP]] & 0x7F0) |\n ((h->cbp_table[left_xy[LTOP]] >> (left_block[0] & (~1))) & 2) |\n (((h->cbp_table[left_xy[LBOT]] >> (left_block[2] & (~1))) & 2) << 2);\n } else {\n h->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;\n }\n }\n }\n if (IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)) {\n int list;\n int b_stride = h->b_stride;\n for (list = 0; list < h->list_count; list++) {\n int8_t *ref_cache = &h->ref_cache[list][scan8[0]];\n int8_t *ref = s->current_picture.f.ref_index[list];\n int16_t(*mv_cache)[2] = &h->mv_cache[list][scan8[0]];\n int16_t(*mv)[2] = s->current_picture.f.motion_val[list];\n if (!USES_LIST(mb_type, list))\n continue;\n assert(!(IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred));\n if (USES_LIST(top_type, list)) {\n const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;\n AV_COPY128(mv_cache[0 - 1 * 8], mv[b_xy + 0]);\n ref_cache[0 - 1 * 8] =\n ref_cache[1 - 1 * 8] = ref[4 * top_xy + 2];\n ref_cache[2 - 1 * 8] =\n ref_cache[3 - 1 * 8] = ref[4 * top_xy + 3];\n } else {\n AV_ZERO128(mv_cache[0 - 1 * 8]);\n AV_WN32A(&ref_cache[0 - 1 * 8],\n ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE) & 0xFF) * 0x01010101u);\n }\n if (mb_type & (MB_TYPE_16x8 | MB_TYPE_8x8)) {\n for (i = 0; i < 2; i++) {\n int cache_idx = -1 + i * 2 * 8;\n if (USES_LIST(left_type[LEFT(i)], list)) {\n const int b_xy = h->mb2b_xy[left_xy[LEFT(i)]] + 3;\n const int b8_xy = 4 * left_xy[LEFT(i)] + 1;\n AV_COPY32(mv_cache[cache_idx],\n mv[b_xy + b_stride * left_block[0 + i * 2]]);\n AV_COPY32(mv_cache[cache_idx + 8],\n mv[b_xy + b_stride * left_block[1 + i * 2]]);\n ref_cache[cache_idx] = ref[b8_xy + (left_block[0 + i * 2] & ~1)];\n ref_cache[cache_idx + 8] = ref[b8_xy + (left_block[1 + i * 2] & ~1)];\n } else {\n AV_ZERO32(mv_cache[cache_idx]);\n AV_ZERO32(mv_cache[cache_idx + 8]);\n ref_cache[cache_idx] =\n ref_cache[cache_idx + 8] = (left_type[LEFT(i)]) ? LIST_NOT_USED\n : PART_NOT_AVAILABLE;\n }\n }\n } else {\n if (USES_LIST(left_type[LTOP], list)) {\n const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;\n const int b8_xy = 4 * left_xy[LTOP] + 1;\n AV_COPY32(mv_cache[-1], mv[b_xy + b_stride * left_block[0]]);\n ref_cache[-1] = ref[b8_xy + (left_block[0] & ~1)];\n } else {\n AV_ZERO32(mv_cache[-1]);\n ref_cache[-1] = left_type[LTOP] ? LIST_NOT_USED\n : PART_NOT_AVAILABLE;\n }\n }\n if (USES_LIST(topright_type, list)) {\n const int b_xy = h->mb2b_xy[topright_xy] + 3 * b_stride;\n AV_COPY32(mv_cache[4 - 1 * 8], mv[b_xy]);\n ref_cache[4 - 1 * 8] = ref[4 * topright_xy + 2];\n } else {\n AV_ZERO32(mv_cache[4 - 1 * 8]);\n ref_cache[4 - 1 * 8] = topright_type ? LIST_NOT_USED\n : PART_NOT_AVAILABLE;\n }\n if (ref_cache[4 - 1 * 8] < 0) {\n if (USES_LIST(topleft_type, list)) {\n const int b_xy = h->mb2b_xy[topleft_xy] + 3 + b_stride +\n (h->topleft_partition & 2 * b_stride);\n const int b8_xy = 4 * topleft_xy + 1 + (h->topleft_partition & 2);\n AV_COPY32(mv_cache[-1 - 1 * 8], mv[b_xy]);\n ref_cache[-1 - 1 * 8] = ref[b8_xy];\n } else {\n AV_ZERO32(mv_cache[-1 - 1 * 8]);\n ref_cache[-1 - 1 * 8] = topleft_type ? LIST_NOT_USED\n : PART_NOT_AVAILABLE;\n }\n }\n if ((mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2)) && !FRAME_MBAFF)\n continue;\n if (!(mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2))) {\n uint8_t(*mvd_cache)[2] = &h->mvd_cache[list][scan8[0]];\n uint8_t(*mvd)[2] = h->mvd_table[list];\n ref_cache[2 + 8 * 0] =\n ref_cache[2 + 8 * 2] = PART_NOT_AVAILABLE;\n AV_ZERO32(mv_cache[2 + 8 * 0]);\n AV_ZERO32(mv_cache[2 + 8 * 2]);\n if (CABAC) {\n if (USES_LIST(top_type, list)) {\n const int b_xy = h->mb2br_xy[top_xy];\n AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);\n } else {\n AV_ZERO64(mvd_cache[0 - 1 * 8]);\n }\n if (USES_LIST(left_type[LTOP], list)) {\n const int b_xy = h->mb2br_xy[left_xy[LTOP]] + 6;\n AV_COPY16(mvd_cache[-1 + 0 * 8], mvd[b_xy - left_block[0]]);\n AV_COPY16(mvd_cache[-1 + 1 * 8], mvd[b_xy - left_block[1]]);\n } else {\n AV_ZERO16(mvd_cache[-1 + 0 * 8]);\n AV_ZERO16(mvd_cache[-1 + 1 * 8]);\n }\n if (USES_LIST(left_type[LBOT], list)) {\n const int b_xy = h->mb2br_xy[left_xy[LBOT]] + 6;\n AV_COPY16(mvd_cache[-1 + 2 * 8], mvd[b_xy - left_block[2]]);\n AV_COPY16(mvd_cache[-1 + 3 * 8], mvd[b_xy - left_block[3]]);\n } else {\n AV_ZERO16(mvd_cache[-1 + 2 * 8]);\n AV_ZERO16(mvd_cache[-1 + 3 * 8]);\n }\n AV_ZERO16(mvd_cache[2 + 8 * 0]);\n AV_ZERO16(mvd_cache[2 + 8 * 2]);\n if (h->slice_type_nos == AV_PICTURE_TYPE_B) {\n uint8_t *direct_cache = &h->direct_cache[scan8[0]];\n uint8_t *direct_table = h->direct_table;\n fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16 >> 1, 1);\n if (IS_DIRECT(top_type)) {\n AV_WN32A(&direct_cache[-1 * 8],\n 0x01010101u * (MB_TYPE_DIRECT2 >> 1));\n } else if (IS_8X8(top_type)) {\n int b8_xy = 4 * top_xy;\n direct_cache[0 - 1 * 8] = direct_table[b8_xy + 2];\n direct_cache[2 - 1 * 8] = direct_table[b8_xy + 3];\n } else {\n AV_WN32A(&direct_cache[-1 * 8],\n 0x01010101 * (MB_TYPE_16x16 >> 1));\n }\n if (IS_DIRECT(left_type[LTOP]))\n direct_cache[-1 + 0 * 8] = MB_TYPE_DIRECT2 >> 1;\n else if (IS_8X8(left_type[LTOP]))\n direct_cache[-1 + 0 * 8] = direct_table[4 * left_xy[LTOP] + 1 + (left_block[0] & ~1)];\n else\n direct_cache[-1 + 0 * 8] = MB_TYPE_16x16 >> 1;\n if (IS_DIRECT(left_type[LBOT]))\n direct_cache[-1 + 2 * 8] = MB_TYPE_DIRECT2 >> 1;\n else if (IS_8X8(left_type[LBOT]))\n direct_cache[-1 + 2 * 8] = direct_table[4 * left_xy[LBOT] + 1 + (left_block[2] & ~1)];\n else\n direct_cache[-1 + 2 * 8] = MB_TYPE_16x16 >> 1;\n }\n }\n }\n#define MAP_MVS \\\n MAP_F2F(scan8[0] - 1 - 1 * 8, topleft_type) \\\n MAP_F2F(scan8[0] + 0 - 1 * 8, top_type) \\\n MAP_F2F(scan8[0] + 1 - 1 * 8, top_type) \\\n MAP_F2F(scan8[0] + 2 - 1 * 8, top_type) \\\n MAP_F2F(scan8[0] + 3 - 1 * 8, top_type) \\\n MAP_F2F(scan8[0] + 4 - 1 * 8, topright_type) \\\n MAP_F2F(scan8[0] - 1 + 0 * 8, left_type[LTOP]) \\\n MAP_F2F(scan8[0] - 1 + 1 * 8, left_type[LTOP]) \\\n MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT]) \\\n MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])\n if (FRAME_MBAFF) {\n if (MB_FIELD) {\n#define MAP_F2F(idx, mb_type) \\\n if (!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0) { \\\n h->ref_cache[list][idx] <<= 1; \\\n h->mv_cache[list][idx][1] /= 2; \\\n h->mvd_cache[list][idx][1] >>= 1; \\\n }\n MAP_MVS\n } else {\n#undef MAP_F2F\n#define MAP_F2F(idx, mb_type) \\\n if (IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0) { \\\n h->ref_cache[list][idx] >>= 1; \\\n h->mv_cache[list][idx][1] <<= 1; \\\n h->mvd_cache[list][idx][1] <<= 1; \\\n }\n MAP_MVS\n#undef MAP_F2F\n }\n }\n }\n }\n h->neighbor_transform_size = !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[LTOP]);\n}'] |
2,228 | 0 | https://github.com/openssl/openssl/blob/b8f1c116a357285ccb4905cd88c83f5076bafb52/ssl/ssl_sess.c/#L203 | SSL_SESSION *SSL_SESSION_new(void)
{
SSL_SESSION *ss;
ss = OPENSSL_zalloc(sizeof(*ss));
if (ss == NULL) {
SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
return NULL;
}
ss->verify_result = 1;
ss->references = 1;
ss->timeout = 60 * 5 + 4;
ss->time = (unsigned long)time(NULL);
ss->lock = CRYPTO_THREAD_lock_new();
if (ss->lock == NULL) {
SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
OPENSSL_free(ss);
return NULL;
}
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) {
CRYPTO_THREAD_lock_free(ss->lock);
OPENSSL_free(ss);
return NULL;
}
return ss;
} | ['SSL_SESSION *SSL_SESSION_new(void)\n{\n SSL_SESSION *ss;\n ss = OPENSSL_zalloc(sizeof(*ss));\n if (ss == NULL) {\n SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n ss->verify_result = 1;\n ss->references = 1;\n ss->timeout = 60 * 5 + 4;\n ss->time = (unsigned long)time(NULL);\n ss->lock = CRYPTO_THREAD_lock_new();\n if (ss->lock == NULL) {\n SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(ss);\n return NULL;\n }\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) {\n CRYPTO_THREAD_lock_free(ss->lock);\n OPENSSL_free(ss);\n return NULL;\n }\n return ss;\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}', 'CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)\n{\n CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t));\n if (lock == NULL)\n return NULL;\n if (pthread_rwlock_init(lock, NULL) != 0) {\n OPENSSL_free(lock);\n return NULL;\n }\n return lock;\n}', 'int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)\n{\n int mx, i;\n void *ptr;\n EX_CALLBACK **storage = NULL;\n EX_CALLBACK *stack[10];\n EX_CALLBACKS *ip = get_and_lock(class_index);\n if (ip == NULL)\n return 0;\n ad->sk = NULL;\n mx = sk_EX_CALLBACK_num(ip->meth);\n if (mx > 0) {\n if (mx < (int)OSSL_NELEM(stack))\n storage = stack;\n else\n storage = OPENSSL_malloc(sizeof(*storage) * mx);\n if (storage != NULL)\n for (i = 0; i < mx; i++)\n storage[i] = sk_EX_CALLBACK_value(ip->meth, i);\n }\n CRYPTO_THREAD_unlock(ex_data_lock);\n if (mx > 0 && storage == NULL) {\n CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n for (i = 0; i < mx; i++) {\n if (storage[i] && storage[i]->new_func) {\n ptr = CRYPTO_get_ex_data(ad, i);\n storage[i]->new_func(obj, ptr, ad, i,\n storage[i]->argl, storage[i]->argp);\n }\n }\n if (storage != stack)\n OPENSSL_free(storage);\n return 1;\n}', 'static EX_CALLBACKS *get_and_lock(int class_index)\n{\n EX_CALLBACKS *ip;\n if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {\n CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);\n return NULL;\n }\n CRYPTO_THREAD_run_once(&ex_data_init, do_ex_data_init);\n if (ex_data_lock == NULL) {\n return NULL;\n }\n ip = &ex_data[class_index];\n CRYPTO_THREAD_write_lock(ex_data_lock);\n return ip;\n}', 'int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))\n{\n if (pthread_once(once, init) != 0)\n return 0;\n return 1;\n}', 'void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)\n{\n if (lock == NULL)\n return;\n pthread_rwlock_destroy(lock);\n OPENSSL_free(lock);\n return;\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}'] |
2,229 | 0 | https://github.com/libav/libav/blob/6ea220cbeec8863e2006a03b73bed52db2b13ee7/libavformat/movenc.c/#L1744 | static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
{
AVFormatContext *ctx = track->rtp_ctx;
char buf[1000] = "";
int len;
ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
NULL, NULL, 0, 0, ctx);
av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
len = strlen(buf);
avio_wb32(pb, len + 24);
ffio_wfourcc(pb, "udta");
avio_wb32(pb, len + 16);
ffio_wfourcc(pb, "hnti");
avio_wb32(pb, len + 8);
ffio_wfourcc(pb, "sdp ");
avio_write(pb, buf, len);
return len + 24;
} | ['static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)\n{\n AVFormatContext *ctx = track->rtp_ctx;\n char buf[1000] = "";\n int len;\n ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,\n NULL, NULL, 0, 0, ctx);\n av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\\r\\n", track->track_id);\n len = strlen(buf);\n avio_wb32(pb, len + 24);\n ffio_wfourcc(pb, "udta");\n avio_wb32(pb, len + 16);\n ffio_wfourcc(pb, "hnti");\n avio_wb32(pb, len + 8);\n ffio_wfourcc(pb, "sdp ");\n avio_write(pb, buf, len);\n return len + 24;\n}', 'void ff_sdp_write_media(char *buff, int size, AVStream *st, int idx,\n const char *dest_addr, const char *dest_type,\n int port, int ttl, AVFormatContext *fmt)\n{\n AVCodecParameters *p = st->codecpar;\n const char *type;\n int payload_type;\n payload_type = ff_rtp_get_payload_type(fmt, st->codecpar, idx);\n switch (p->codec_type) {\n case AVMEDIA_TYPE_VIDEO : type = "video" ; break;\n case AVMEDIA_TYPE_AUDIO : type = "audio" ; break;\n case AVMEDIA_TYPE_SUBTITLE: type = "text" ; break;\n default : type = "application"; break;\n }\n av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\\r\\n", type, port, payload_type);\n sdp_write_address(buff, size, dest_addr, dest_type, ttl);\n if (p->bit_rate) {\n av_strlcatf(buff, size, "b=AS:%d\\r\\n", p->bit_rate / 1000);\n }\n sdp_write_media_attributes(buff, size, p, payload_type, fmt);\n}', 'size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)\n{\n int len = strlen(dst);\n va_list vl;\n va_start(vl, fmt);\n len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);\n va_end(vl);\n return len;\n}', 'static void sdp_write_address(char *buff, int size, const char *dest_addr,\n const char *dest_type, int ttl)\n{\n if (dest_addr) {\n if (!dest_type)\n dest_type = "IP4";\n if (ttl > 0 && !strcmp(dest_type, "IP4")) {\n av_strlcatf(buff, size, "c=IN %s %s/%d\\r\\n", dest_type, dest_addr, ttl);\n } else {\n av_strlcatf(buff, size, "c=IN %s %s\\r\\n", dest_type, dest_addr);\n }\n }\n}'] |
2,230 | 0 | https://github.com/libav/libav/blob/e259eadcabe188988c0a9696707791f3497738c2/ffmpeg.c/#L3707 | static void opt_output_file(const char *filename)
{
AVFormatContext *oc;
int err, use_video, use_audio, use_subtitle;
int input_has_video, input_has_audio, input_has_subtitle;
AVFormatParameters params, *ap = ¶ms;
AVOutputFormat *file_oformat;
if (!strcmp(filename, "-"))
filename = "pipe:";
oc = avformat_alloc_context();
if (!oc) {
print_error(filename, AVERROR(ENOMEM));
ffmpeg_exit(1);
}
if (last_asked_format) {
file_oformat = av_guess_format(last_asked_format, NULL, NULL);
if (!file_oformat) {
fprintf(stderr, "Requested output format '%s' is not a suitable output format\n", last_asked_format);
ffmpeg_exit(1);
}
last_asked_format = NULL;
} else {
file_oformat = av_guess_format(NULL, filename, NULL);
if (!file_oformat) {
fprintf(stderr, "Unable to find a suitable output format for '%s'\n",
filename);
ffmpeg_exit(1);
}
}
oc->oformat = file_oformat;
av_strlcpy(oc->filename, filename, sizeof(oc->filename));
if (!strcmp(file_oformat->name, "ffm") &&
av_strstart(filename, "http:", NULL)) {
int err = read_ffserver_streams(oc, filename);
if (err < 0) {
print_error(filename, err);
ffmpeg_exit(1);
}
} else {
use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;
use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;
use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;
if (nb_input_files > 0) {
check_audio_video_sub_inputs(&input_has_video, &input_has_audio,
&input_has_subtitle);
if (!input_has_video)
use_video = 0;
if (!input_has_audio)
use_audio = 0;
if (!input_has_subtitle)
use_subtitle = 0;
}
if (audio_disable) {
use_audio = 0;
}
if (video_disable) {
use_video = 0;
}
if (subtitle_disable) {
use_subtitle = 0;
}
if (use_video) {
new_video_stream(oc, nb_output_files);
}
if (use_audio) {
new_audio_stream(oc, nb_output_files);
}
if (use_subtitle) {
new_subtitle_stream(oc, nb_output_files);
}
oc->timestamp = recording_timestamp;
for(; metadata_count>0; metadata_count--){
av_metadata_set2(&oc->metadata, metadata[metadata_count-1].key,
metadata[metadata_count-1].value, 0);
}
av_metadata_conv(oc, oc->oformat->metadata_conv, NULL);
}
output_files[nb_output_files++] = oc;
if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
if (!av_filename_number_test(oc->filename)) {
print_error(oc->filename, AVERROR_NUMEXPECTED);
ffmpeg_exit(1);
}
}
if (!(oc->oformat->flags & AVFMT_NOFILE)) {
if (!file_overwrite &&
(strchr(filename, ':') == NULL ||
filename[1] == ':' ||
av_strstart(filename, "file:", NULL))) {
if (url_exist(filename)) {
if (!using_stdin) {
fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
fflush(stderr);
if (!read_yesno()) {
fprintf(stderr, "Not overwriting - exiting\n");
ffmpeg_exit(1);
}
}
else {
fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
ffmpeg_exit(1);
}
}
}
if ((err = url_fopen(&oc->pb, filename, URL_WRONLY)) < 0) {
print_error(filename, err);
ffmpeg_exit(1);
}
}
memset(ap, 0, sizeof(*ap));
if (av_set_parameters(oc, ap) < 0) {
fprintf(stderr, "%s: Invalid encoding parameters\n",
oc->filename);
ffmpeg_exit(1);
}
oc->preload= (int)(mux_preload*AV_TIME_BASE);
oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);
oc->loop_output = loop_output;
oc->flags |= AVFMT_FLAG_NONBLOCK;
set_context_opts(oc, avformat_opts, AV_OPT_FLAG_ENCODING_PARAM, NULL);
nb_streamid_map = 0;
} | ['static void opt_output_file(const char *filename)\n{\n AVFormatContext *oc;\n int err, use_video, use_audio, use_subtitle;\n int input_has_video, input_has_audio, input_has_subtitle;\n AVFormatParameters params, *ap = ¶ms;\n AVOutputFormat *file_oformat;\n if (!strcmp(filename, "-"))\n filename = "pipe:";\n oc = avformat_alloc_context();\n if (!oc) {\n print_error(filename, AVERROR(ENOMEM));\n ffmpeg_exit(1);\n }\n if (last_asked_format) {\n file_oformat = av_guess_format(last_asked_format, NULL, NULL);\n if (!file_oformat) {\n fprintf(stderr, "Requested output format \'%s\' is not a suitable output format\\n", last_asked_format);\n ffmpeg_exit(1);\n }\n last_asked_format = NULL;\n } else {\n file_oformat = av_guess_format(NULL, filename, NULL);\n if (!file_oformat) {\n fprintf(stderr, "Unable to find a suitable output format for \'%s\'\\n",\n filename);\n ffmpeg_exit(1);\n }\n }\n oc->oformat = file_oformat;\n av_strlcpy(oc->filename, filename, sizeof(oc->filename));\n if (!strcmp(file_oformat->name, "ffm") &&\n av_strstart(filename, "http:", NULL)) {\n int err = read_ffserver_streams(oc, filename);\n if (err < 0) {\n print_error(filename, err);\n ffmpeg_exit(1);\n }\n } else {\n use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;\n use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;\n use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;\n if (nb_input_files > 0) {\n check_audio_video_sub_inputs(&input_has_video, &input_has_audio,\n &input_has_subtitle);\n if (!input_has_video)\n use_video = 0;\n if (!input_has_audio)\n use_audio = 0;\n if (!input_has_subtitle)\n use_subtitle = 0;\n }\n if (audio_disable) {\n use_audio = 0;\n }\n if (video_disable) {\n use_video = 0;\n }\n if (subtitle_disable) {\n use_subtitle = 0;\n }\n if (use_video) {\n new_video_stream(oc, nb_output_files);\n }\n if (use_audio) {\n new_audio_stream(oc, nb_output_files);\n }\n if (use_subtitle) {\n new_subtitle_stream(oc, nb_output_files);\n }\n oc->timestamp = recording_timestamp;\n for(; metadata_count>0; metadata_count--){\n av_metadata_set2(&oc->metadata, metadata[metadata_count-1].key,\n metadata[metadata_count-1].value, 0);\n }\n av_metadata_conv(oc, oc->oformat->metadata_conv, NULL);\n }\n output_files[nb_output_files++] = oc;\n if (oc->oformat->flags & AVFMT_NEEDNUMBER) {\n if (!av_filename_number_test(oc->filename)) {\n print_error(oc->filename, AVERROR_NUMEXPECTED);\n ffmpeg_exit(1);\n }\n }\n if (!(oc->oformat->flags & AVFMT_NOFILE)) {\n if (!file_overwrite &&\n (strchr(filename, \':\') == NULL ||\n filename[1] == \':\' ||\n av_strstart(filename, "file:", NULL))) {\n if (url_exist(filename)) {\n if (!using_stdin) {\n fprintf(stderr,"File \'%s\' already exists. Overwrite ? [y/N] ", filename);\n fflush(stderr);\n if (!read_yesno()) {\n fprintf(stderr, "Not overwriting - exiting\\n");\n ffmpeg_exit(1);\n }\n }\n else {\n fprintf(stderr,"File \'%s\' already exists. Exiting.\\n", filename);\n ffmpeg_exit(1);\n }\n }\n }\n if ((err = url_fopen(&oc->pb, filename, URL_WRONLY)) < 0) {\n print_error(filename, err);\n ffmpeg_exit(1);\n }\n }\n memset(ap, 0, sizeof(*ap));\n if (av_set_parameters(oc, ap) < 0) {\n fprintf(stderr, "%s: Invalid encoding parameters\\n",\n oc->filename);\n ffmpeg_exit(1);\n }\n oc->preload= (int)(mux_preload*AV_TIME_BASE);\n oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);\n oc->loop_output = loop_output;\n oc->flags |= AVFMT_FLAG_NONBLOCK;\n set_context_opts(oc, avformat_opts, AV_OPT_FLAG_ENCODING_PARAM, NULL);\n nb_streamid_map = 0;\n}', 'AVFormatContext *avformat_alloc_context(void)\n{\n AVFormatContext *ic;\n ic = av_malloc(sizeof(AVFormatContext));\n if (!ic) return ic;\n avformat_get_context_defaults(ic);\n ic->av_class = &av_format_context_class;\n return ic;\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}', 'size_t av_strlcpy(char *dst, const char *src, size_t size)\n{\n size_t len = 0;\n while (++len < size && *src)\n *dst++ = *src++;\n if (len <= size)\n *dst = 0;\n return len + strlen(src) - 1;\n}'] |
2,231 | 0 | https://github.com/openssl/openssl/blob/0eab41fb78cf4d7c76e563fd677ab6c32fc28bb0/apps/ts.c/#L1083 | static X509_STORE *create_cert_store(char *ca_path, char *ca_file)
{
X509_STORE *cert_ctx = NULL;
X509_LOOKUP *lookup = NULL;
int i;
cert_ctx = X509_STORE_new();
X509_STORE_set_verify_cb_func(cert_ctx, verify_cb);
if (ca_path)
{
lookup = X509_STORE_add_lookup(cert_ctx,
X509_LOOKUP_hash_dir());
if (lookup == NULL)
{
BIO_printf(bio_err, "memory allocation failure\n");
goto err;
}
i = X509_LOOKUP_add_dir(lookup, ca_path, X509_FILETYPE_PEM);
if (!i)
{
BIO_printf(bio_err, "Error loading directory %s\n",
ca_path);
goto err;
}
}
if (ca_file)
{
lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
if (lookup == NULL)
{
BIO_printf(bio_err, "memory allocation failure\n");
goto err;
}
i = X509_LOOKUP_load_file(lookup, ca_file, X509_FILETYPE_PEM);
if (!i)
{
BIO_printf(bio_err, "Error loading file %s\n", ca_file);
goto err;
}
}
return cert_ctx;
err:
X509_STORE_free(cert_ctx);
return NULL;
} | ['static X509_STORE *create_cert_store(char *ca_path, char *ca_file)\n\t{\n\tX509_STORE *cert_ctx = NULL;\n\tX509_LOOKUP *lookup = NULL;\n\tint i;\n\tcert_ctx = X509_STORE_new();\n\tX509_STORE_set_verify_cb_func(cert_ctx, verify_cb);\n\tif (ca_path)\n\t\t{\n\t\tlookup = X509_STORE_add_lookup(cert_ctx,\n\t\t\t\t\t X509_LOOKUP_hash_dir());\n\t\tif (lookup == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "memory allocation failure\\n");\n\t\t\tgoto err;\n\t\t\t}\n\t\ti = X509_LOOKUP_add_dir(lookup, ca_path, X509_FILETYPE_PEM);\n\t\tif (!i)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "Error loading directory %s\\n",\n\t\t\t\t ca_path);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (ca_file)\n\t\t{\n\t\tlookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());\n\t\tif (lookup == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "memory allocation failure\\n");\n\t\t\tgoto err;\n\t\t\t}\n\t\ti = X509_LOOKUP_load_file(lookup, ca_file, X509_FILETYPE_PEM);\n\t\tif (!i)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "Error loading file %s\\n", ca_file);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\treturn cert_ctx;\n err:\n\tX509_STORE_free(cert_ctx);\n\treturn NULL;\n\t}', 'X509_STORE *X509_STORE_new(void)\n\t{\n\tX509_STORE *ret;\n\tif ((ret=(X509_STORE *)OPENSSL_malloc(sizeof(X509_STORE))) == NULL)\n\t\treturn NULL;\n\tret->objs = sk_X509_OBJECT_new(x509_object_cmp);\n\tret->cache=1;\n\tret->get_cert_methods=sk_X509_LOOKUP_new_null();\n\tret->verify=0;\n\tret->verify_cb=0;\n\tif ((ret->param = X509_VERIFY_PARAM_new()) == NULL)\n\t\treturn NULL;\n\tret->get_issuer = 0;\n\tret->check_issued = 0;\n\tret->check_revocation = 0;\n\tret->get_crl = 0;\n\tret->check_crl = 0;\n\tret->cert_crl = 0;\n\tret->lookup_certs = 0;\n\tret->lookup_crls = 0;\n\tret->cleanup = 0;\n\tCRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE, ret, &ret->ex_data);\n\tret->references=1;\n\treturn ret;\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\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#ifndef OPENSSL_CPUID_OBJ\n if(ret && (num > 2048))\n\t{\textern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\t}\n#endif\n\treturn ret;\n\t}'] |
2,232 | 0 | https://github.com/libav/libav/blob/1db9da523815beb8e9fdcbc63205b3473616c6f0/libavcodec/mpegaudiodec.c/#L2565 | static int decode_init_mp3on4(AVCodecContext * avctx)
{
MP3On4DecodeContext *s = avctx->priv_data;
MPEG4AudioConfig cfg;
int i;
if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
return -1;
}
ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
if (!cfg.chan_config || cfg.chan_config > 7) {
av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
return -1;
}
s->frames = mp3Frames[cfg.chan_config];
s->coff = chan_offset[cfg.chan_config];
avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
if (cfg.sample_rate < 16000)
s->syncword = 0xffe00000;
else
s->syncword = 0xfff00000;
s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
avctx->priv_data = s->mp3decctx[0];
decode_init(avctx);
avctx->priv_data = s;
s->mp3decctx[0]->adu_mode = 1;
for (i = 1; i < s->frames; i++) {
s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
s->mp3decctx[i]->adu_mode = 1;
s->mp3decctx[i]->avctx = avctx;
}
return 0;
} | ['static int decode_init_mp3on4(AVCodecContext * avctx)\n{\n MP3On4DecodeContext *s = avctx->priv_data;\n MPEG4AudioConfig cfg;\n int i;\n if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {\n av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\\n");\n return -1;\n }\n ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);\n if (!cfg.chan_config || cfg.chan_config > 7) {\n av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\\n");\n return -1;\n }\n s->frames = mp3Frames[cfg.chan_config];\n s->coff = chan_offset[cfg.chan_config];\n avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];\n if (cfg.sample_rate < 16000)\n s->syncword = 0xffe00000;\n else\n s->syncword = 0xfff00000;\n s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));\n avctx->priv_data = s->mp3decctx[0];\n decode_init(avctx);\n avctx->priv_data = s;\n s->mp3decctx[0]->adu_mode = 1;\n for (i = 1; i < s->frames; i++) {\n s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));\n s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;\n s->mp3decctx[i]->adu_mode = 1;\n s->mp3decctx[i]->avctx = avctx;\n }\n return 0;\n}', 'void *av_mallocz(unsigned int size)\n{\n void *ptr = av_malloc(size);\n if (ptr)\n memset(ptr, 0, size);\n return ptr;\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}'] |
2,233 | 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_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 || !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}'] |
2,234 | 0 | https://github.com/libav/libav/blob/3ae0e721c7b6e0483801b9039b3d140e3b68b7f5/libavcodec/rawenc.c/#L44 | static av_cold int raw_encode_init(AVCodecContext *avctx)
{
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
#if FF_API_CODED_FRAME
FF_DISABLE_DEPRECATION_WARNINGS
avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
avctx->coded_frame->key_frame = 1;
FF_ENABLE_DEPRECATION_WARNINGS
#endif
avctx->bits_per_coded_sample = av_get_bits_per_pixel(desc);
if(!avctx->codec_tag)
avctx->codec_tag = avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
return 0;
} | ['static av_cold int raw_encode_init(AVCodecContext *avctx)\n{\n const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);\n#if FF_API_CODED_FRAME\nFF_DISABLE_DEPRECATION_WARNINGS\n avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;\n avctx->coded_frame->key_frame = 1;\nFF_ENABLE_DEPRECATION_WARNINGS\n#endif\n avctx->bits_per_coded_sample = av_get_bits_per_pixel(desc);\n if(!avctx->codec_tag)\n avctx->codec_tag = avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);\n return 0;\n}', 'const AVPixFmtDescriptor *av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)\n{\n if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)\n return NULL;\n return &av_pix_fmt_descriptors[pix_fmt];\n}', 'int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)\n{\n int c, bits = 0;\n int log2_pixels = pixdesc->log2_chroma_w + pixdesc->log2_chroma_h;\n for (c = 0; c < pixdesc->nb_components; c++) {\n int s = c == 1 || c == 2 ? 0 : log2_pixels;\n bits += (pixdesc->comp[c].depth_minus1 + 1) << s;\n }\n return bits >> log2_pixels;\n}'] |
2,235 | 0 | https://github.com/openssl/openssl/blob/fa3f83552f53447deced45579865cec9f55a947e/crypto/cms/cms_pwri.c/#L225 | static int kek_unwrap_key(unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen,
EVP_CIPHER_CTX *ctx)
{
size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
unsigned char *tmp;
int outl, rv = 0;
if (inlen < 2 * blocklen) {
return 0;
}
if (inlen % blocklen) {
return 0;
}
tmp = OPENSSL_malloc(inlen);
if (tmp == NULL)
return 0;
if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,
in + inlen - 2 * blocklen, blocklen * 2)
|| !EVP_DecryptUpdate(ctx, tmp, &outl,
tmp + inlen - blocklen, blocklen)
|| !EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen)
|| !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)
|| !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen))
goto err;
if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {
goto err;
}
if (inlen < (size_t)(tmp[0] - 4)) {
goto err;
}
*outlen = (size_t)tmp[0];
memcpy(out, tmp + 4, *outlen);
rv = 1;
err:
OPENSSL_clear_free(tmp, inlen);
return rv;
} | ['static int kek_unwrap_key(unsigned char *out, size_t *outlen,\n const unsigned char *in, size_t inlen,\n EVP_CIPHER_CTX *ctx)\n{\n size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);\n unsigned char *tmp;\n int outl, rv = 0;\n if (inlen < 2 * blocklen) {\n return 0;\n }\n if (inlen % blocklen) {\n return 0;\n }\n tmp = OPENSSL_malloc(inlen);\n if (tmp == NULL)\n return 0;\n if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,\n in + inlen - 2 * blocklen, blocklen * 2)\n || !EVP_DecryptUpdate(ctx, tmp, &outl,\n tmp + inlen - blocklen, blocklen)\n || !EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen)\n || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)\n || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen))\n goto err;\n if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {\n goto err;\n }\n if (inlen < (size_t)(tmp[0] - 4)) {\n goto err;\n }\n *outlen = (size_t)tmp[0];\n memcpy(out, tmp + 4, *outlen);\n rv = 1;\n err:\n OPENSSL_clear_free(tmp, inlen);\n return rv;\n}', 'int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)\n{\n return ctx->cipher->block_size;\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}', 'void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)\n{\n if (str == NULL)\n return;\n if (num)\n OPENSSL_cleanse(str, num);\n CRYPTO_free(str, file, line);\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}'] |
2,236 | 0 | https://github.com/openssl/openssl/blob/de3955f66225e42bfae710c50b51c98aa4616ac1/apps/ca.c/#L2174 | static int get_certificate_status(const char *serial, CA_DB *db)
{
char *row[DB_NUMBER], **rrow;
int ok = -1, i;
size_t serial_len = strlen(serial);
for (i = 0; i < DB_NUMBER; i++)
row[i] = NULL;
row[DB_serial] = app_malloc(serial_len + 2, "row serial#");
if (serial_len % 2) {
row[DB_serial][0] = '0';
memcpy(row[DB_serial] + 1, serial, serial_len);
row[DB_serial][serial_len + 1] = '\0';
} else {
memcpy(row[DB_serial], serial, serial_len);
row[DB_serial][serial_len] = '\0';
}
make_uppercase(row[DB_serial]);
ok = 1;
rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
if (rrow == NULL) {
BIO_printf(bio_err, "Serial %s not present in db.\n", row[DB_serial]);
ok = -1;
goto end;
} else if (rrow[DB_type][0] == DB_TYPE_VAL) {
BIO_printf(bio_err, "%s=Valid (%c)\n",
row[DB_serial], rrow[DB_type][0]);
goto end;
} else if (rrow[DB_type][0] == DB_TYPE_REV) {
BIO_printf(bio_err, "%s=Revoked (%c)\n",
row[DB_serial], rrow[DB_type][0]);
goto end;
} else if (rrow[DB_type][0] == DB_TYPE_EXP) {
BIO_printf(bio_err, "%s=Expired (%c)\n",
row[DB_serial], rrow[DB_type][0]);
goto end;
} else if (rrow[DB_type][0] == DB_TYPE_SUSP) {
BIO_printf(bio_err, "%s=Suspended (%c)\n",
row[DB_serial], rrow[DB_type][0]);
goto end;
} else {
BIO_printf(bio_err, "%s=Unknown (%c).\n",
row[DB_serial], rrow[DB_type][0]);
ok = -1;
}
end:
for (i = 0; i < DB_NUMBER; i++) {
OPENSSL_free(row[i]);
}
return ok;
} | ['static int get_certificate_status(const char *serial, CA_DB *db)\n{\n char *row[DB_NUMBER], **rrow;\n int ok = -1, i;\n size_t serial_len = strlen(serial);\n for (i = 0; i < DB_NUMBER; i++)\n row[i] = NULL;\n row[DB_serial] = app_malloc(serial_len + 2, "row serial#");\n if (serial_len % 2) {\n row[DB_serial][0] = \'0\';\n memcpy(row[DB_serial] + 1, serial, serial_len);\n row[DB_serial][serial_len + 1] = \'\\0\';\n } else {\n memcpy(row[DB_serial], serial, serial_len);\n row[DB_serial][serial_len] = \'\\0\';\n }\n make_uppercase(row[DB_serial]);\n ok = 1;\n rrow = TXT_DB_get_by_index(db->db, DB_serial, row);\n if (rrow == NULL) {\n BIO_printf(bio_err, "Serial %s not present in db.\\n", row[DB_serial]);\n ok = -1;\n goto end;\n } else if (rrow[DB_type][0] == DB_TYPE_VAL) {\n BIO_printf(bio_err, "%s=Valid (%c)\\n",\n row[DB_serial], rrow[DB_type][0]);\n goto end;\n } else if (rrow[DB_type][0] == DB_TYPE_REV) {\n BIO_printf(bio_err, "%s=Revoked (%c)\\n",\n row[DB_serial], rrow[DB_type][0]);\n goto end;\n } else if (rrow[DB_type][0] == DB_TYPE_EXP) {\n BIO_printf(bio_err, "%s=Expired (%c)\\n",\n row[DB_serial], rrow[DB_type][0]);\n goto end;\n } else if (rrow[DB_type][0] == DB_TYPE_SUSP) {\n BIO_printf(bio_err, "%s=Suspended (%c)\\n",\n row[DB_serial], rrow[DB_type][0]);\n goto end;\n } else {\n BIO_printf(bio_err, "%s=Unknown (%c).\\n",\n row[DB_serial], rrow[DB_type][0]);\n ok = -1;\n }\n end:\n for (i = 0; i < DB_NUMBER; i++) {\n OPENSSL_free(row[i]);\n }\n return ok;\n}', 'void* app_malloc(int sz, const char *what)\n{\n void *vp = OPENSSL_malloc(sz);\n return vp;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE)\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}'] |
2,237 | 0 | https://github.com/openssl/openssl/blob/23dd0c9f8dc6f7edf4b872d13e5644dfbbee585b/apps/apps.c/#L2471 | BIO *bio_open_owner(const char *filename, int format, int private)
{
FILE *fp = NULL;
BIO *b = NULL;
int fd = -1, bflags, mode, textmode;
if (!private || filename == NULL || strcmp(filename, "-") == 0)
return bio_open_default(filename, 'w', format);
mode = O_WRONLY;
#ifdef O_CREAT
mode |= O_CREAT;
#endif
#ifdef O_TRUNC
mode |= O_TRUNC;
#endif
textmode = istext(format);
if (!textmode) {
#ifdef O_BINARY
mode |= O_BINARY;
#elif defined(_O_BINARY)
mode |= _O_BINARY;
#endif
}
#ifdef OPENSSL_SYS_VMS
if (!textmode)
fd = open(filename, mode, 0600, "ctx=bin");
else
#endif
fd = open(filename, mode, 0600);
if (fd < 0)
goto err;
fp = fdopen(fd, modestr('w', format));
if (fp == NULL)
goto err;
bflags = BIO_CLOSE;
if (textmode)
bflags |= BIO_FP_TEXT;
b = BIO_new_fp(fp, bflags);
if (b)
return b;
err:
BIO_printf(bio_err, "%s: Can't open \"%s\" for writing, %s\n",
opt_getprog(), filename, strerror(errno));
ERR_print_errors(bio_err);
if (fp)
fclose(fp);
else if (fd >= 0)
close(fd);
return NULL;
} | ['BIO *bio_open_owner(const char *filename, int format, int private)\n{\n FILE *fp = NULL;\n BIO *b = NULL;\n int fd = -1, bflags, mode, textmode;\n if (!private || filename == NULL || strcmp(filename, "-") == 0)\n return bio_open_default(filename, \'w\', format);\n mode = O_WRONLY;\n#ifdef O_CREAT\n mode |= O_CREAT;\n#endif\n#ifdef O_TRUNC\n mode |= O_TRUNC;\n#endif\n textmode = istext(format);\n if (!textmode) {\n#ifdef O_BINARY\n mode |= O_BINARY;\n#elif defined(_O_BINARY)\n mode |= _O_BINARY;\n#endif\n }\n#ifdef OPENSSL_SYS_VMS\n if (!textmode)\n fd = open(filename, mode, 0600, "ctx=bin");\n else\n#endif\n fd = open(filename, mode, 0600);\n if (fd < 0)\n goto err;\n fp = fdopen(fd, modestr(\'w\', format));\n if (fp == NULL)\n goto err;\n bflags = BIO_CLOSE;\n if (textmode)\n bflags |= BIO_FP_TEXT;\n b = BIO_new_fp(fp, bflags);\n if (b)\n return b;\n err:\n BIO_printf(bio_err, "%s: Can\'t open \\"%s\\" for writing, %s\\n",\n opt_getprog(), filename, strerror(errno));\n ERR_print_errors(bio_err);\n if (fp)\n fclose(fp);\n else if (fd >= 0)\n close(fd);\n return NULL;\n}', 'static int istext(int format)\n{\n return (format & B_FORMAT_TEXT) == B_FORMAT_TEXT;\n}', 'static const char *modestr(char mode, int format)\n{\n OPENSSL_assert(mode == \'a\' || mode == \'r\' || mode == \'w\');\n switch (mode) {\n case \'a\':\n return istext(format) ? "a" : "ab";\n case \'r\':\n return istext(format) ? "r" : "rb";\n case \'w\':\n return istext(format) ? "w" : "wb";\n }\n return NULL;\n}'] |
2,238 | 0 | https://github.com/openssl/openssl/blob/24b8e4b2c835d6bf52c2768d4d4a78ed7d7e85bb/ssl/t1_lib.c/#L316 | int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
{
const tls_curve_info *cinfo;
if (curve[0])
return 1;
if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))
return 0;
cinfo = &nid_list[curve[1] - 1];
# ifdef OPENSSL_NO_EC2M
if (cinfo->flags & TLS_CURVE_CHAR2)
return 0;
# endif
return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
} | ['int tls_construct_client_supported_groups(SSL *s, WPACKET *pkt, int *al)\n{\n const unsigned char *pcurves = NULL, *pcurvestmp;\n size_t num_curves = 0, i;\n if (!use_ecc(s))\n return 1;\n pcurves = s->tlsext_supportedgroupslist;\n if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SUPPORTED_GROUPS,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n pcurvestmp = pcurves;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SUPPORTED_GROUPS,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n for (i = 0; i < num_curves; i++, pcurvestmp += 2) {\n if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {\n if (!WPACKET_put_bytes_u8(pkt, pcurvestmp[0])\n || !WPACKET_put_bytes_u8(pkt, pcurvestmp[1])) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SUPPORTED_GROUPS,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n }\n if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SUPPORTED_GROUPS,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n return 1;\n}', 'int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)\n{\n const tls_curve_info *cinfo;\n if (curve[0])\n return 1;\n if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))\n return 0;\n cinfo = &nid_list[curve[1] - 1];\n# ifdef OPENSSL_NO_EC2M\n if (cinfo->flags & TLS_CURVE_CHAR2)\n return 0;\n# endif\n return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);\n}'] |
2,239 | 0 | https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_mont.c/#L106 | 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;
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->neg = r->neg;
rp = ret->d;
ap = &(r->d[nl]);
# define BRANCH_FREE 1
# if BRANCH_FREE
{
BN_ULONG *nrp;
size_t m;
v = bn_sub_words(rp, ap, np, nl) - carry;
m = (0 - (size_t)v);
nrp =
(BN_ULONG *)(((PTR_SIZE_INT) rp & ~m) | ((PTR_SIZE_INT) ap & m));
for (i = 0, nl -= 4; i < nl; i += 4) {
BN_ULONG t1, t2, t3, t4;
t1 = nrp[i + 0];
t2 = nrp[i + 1];
t3 = nrp[i + 2];
ap[i + 0] = 0;
t4 = nrp[i + 3];
ap[i + 1] = 0;
rp[i + 0] = t1;
ap[i + 2] = 0;
rp[i + 1] = t2;
ap[i + 3] = 0;
rp[i + 2] = t3;
rp[i + 3] = t4;
}
for (nl += 4; i < nl; i++)
rp[i] = nrp[i], ap[i] = 0;
}
# else
if (bn_sub_words(rp, ap, np, nl) - carry)
memcpy(rp, ap, nl * sizeof(BN_ULONG));
# endif
bn_correct_top(r);
bn_correct_top(ret);
bn_check_top(ret);
return 1;
} | ['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}', '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 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->neg = r->neg;\n rp = ret->d;\n ap = &(r->d[nl]);\n# define BRANCH_FREE 1\n# if BRANCH_FREE\n {\n BN_ULONG *nrp;\n size_t m;\n v = bn_sub_words(rp, ap, np, nl) - carry;\n m = (0 - (size_t)v);\n nrp =\n (BN_ULONG *)(((PTR_SIZE_INT) rp & ~m) | ((PTR_SIZE_INT) ap & m));\n for (i = 0, nl -= 4; i < nl; i += 4) {\n BN_ULONG t1, t2, t3, t4;\n t1 = nrp[i + 0];\n t2 = nrp[i + 1];\n t3 = nrp[i + 2];\n ap[i + 0] = 0;\n t4 = nrp[i + 3];\n ap[i + 1] = 0;\n rp[i + 0] = t1;\n ap[i + 2] = 0;\n rp[i + 1] = t2;\n ap[i + 3] = 0;\n rp[i + 2] = t3;\n rp[i + 3] = t4;\n }\n for (nl += 4; i < nl; i++)\n rp[i] = nrp[i], ap[i] = 0;\n }\n# else\n if (bn_sub_words(rp, ap, np, nl) - carry)\n memcpy(rp, ap, nl * sizeof(BN_ULONG));\n# endif\n bn_correct_top(r);\n bn_correct_top(ret);\n bn_check_top(ret);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
2,240 | 0 | https://github.com/openssl/openssl/blob/54b5fd537f7a7ac1874359fd42a4721b6839f7a1/crypto/x509v3/pcy_tree.c/#L531 | static int tree_evaluate(X509_POLICY_TREE *tree)
{
int ret, i;
X509_POLICY_LEVEL *curr = tree->levels + 1;
const X509_POLICY_CACHE *cache;
for(i = 1; i < tree->nlevel; i++, curr++)
{
cache = policy_cache_set(curr->cert);
if (!tree_link_nodes(curr, cache))
return 0;
if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
&& !tree_link_any(curr, cache, tree))
return 0;
ret = tree_prune(tree, curr);
if (ret != 1)
return ret;
}
return 1;
} | ['static int tree_evaluate(X509_POLICY_TREE *tree)\n\t{\n\tint ret, i;\n\tX509_POLICY_LEVEL *curr = tree->levels + 1;\n\tconst X509_POLICY_CACHE *cache;\n\tfor(i = 1; i < tree->nlevel; i++, curr++)\n\t\t{\n\t\tcache = policy_cache_set(curr->cert);\n\t\tif (!tree_link_nodes(curr, cache))\n\t\t\treturn 0;\n\t\tif (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)\n\t\t\t&& !tree_link_any(curr, cache, tree))\n\t\t\treturn 0;\n\t\tret = tree_prune(tree, curr);\n\t\tif (ret != 1)\n\t\t\treturn ret;\n\t\t}\n\treturn 1;\n\t}', 'const X509_POLICY_CACHE *policy_cache_set(X509 *x)\n\t{\n\tif (x->policy_cache == NULL)\n\t\t{\n\t\tCRYPTO_w_lock(CRYPTO_LOCK_X509);\n\t\t\tpolicy_cache_new(x);\n\t\tCRYPTO_w_unlock(CRYPTO_LOCK_X509);\n\t\t}\n\treturn x->policy_cache;\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}', 'static int policy_cache_new(X509 *x)\n\t{\n\tX509_POLICY_CACHE *cache;\n\tASN1_INTEGER *ext_any = NULL;\n\tPOLICY_CONSTRAINTS *ext_pcons = NULL;\n\tCERTIFICATEPOLICIES *ext_cpols = NULL;\n\tPOLICY_MAPPINGS *ext_pmaps = NULL;\n\tint i;\n\tcache = OPENSSL_malloc(sizeof(X509_POLICY_CACHE));\n\tif (!cache)\n\t\treturn 0;\n\tcache->anyPolicy = NULL;\n\tcache->data = NULL;\n\tcache->maps = NULL;\n\tcache->any_skip = -1;\n\tcache->explicit_skip = -1;\n\tcache->map_skip = -1;\n\tx->policy_cache = cache;\n\text_pcons = X509_get_ext_d2i(x, NID_policy_constraints, &i, NULL);\n\tif (!ext_pcons)\n\t\t{\n\t\tif (i != -1)\n\t\t\tgoto bad_cache;\n\t\t}\n\telse\n\t\t{\n\t\tif (!ext_pcons->requireExplicitPolicy\n\t\t\t&& !ext_pcons->inhibitPolicyMapping)\n\t\t\tgoto bad_cache;\n\t\tif (!policy_cache_set_int(&cache->explicit_skip,\n\t\t\text_pcons->requireExplicitPolicy))\n\t\t\tgoto bad_cache;\n\t\tif (!policy_cache_set_int(&cache->map_skip,\n\t\t\text_pcons->inhibitPolicyMapping))\n\t\t\tgoto bad_cache;\n\t\t}\n\text_cpols = X509_get_ext_d2i(x, NID_certificate_policies, &i, NULL);\n\tif (!ext_cpols)\n\t\t{\n\t\tif (i != -1)\n\t\t\tgoto bad_cache;\n\t\treturn 1;\n\t\t}\n\ti = policy_cache_create(x, ext_cpols, i);\n\tif (i <= 0)\n\t\treturn i;\n\text_pmaps = X509_get_ext_d2i(x, NID_policy_mappings, &i, NULL);\n\tif (!ext_pmaps)\n\t\t{\n\t\tif (i != -1)\n\t\t\tgoto bad_cache;\n\t\t}\n\telse\n\t\t{\n\t\ti = policy_cache_set_mapping(x, ext_pmaps);\n\t\tif (i <= 0)\n\t\t\tgoto bad_cache;\n\t\t}\n\text_any = X509_get_ext_d2i(x, NID_inhibit_any_policy, &i, NULL);\n\tif (!ext_any)\n\t\t{\n\t\tif (i != -1)\n\t\t\tgoto bad_cache;\n\t\t}\n\telse if (!policy_cache_set_int(&cache->any_skip, ext_any))\n\t\t\tgoto bad_cache;\n\tif (0)\n\t\t{\n\t\tbad_cache:\n\t\tx->ex_flags |= EXFLAG_INVALID_POLICY;\n\t\t}\n\tif(ext_pcons)\n\t\tPOLICY_CONSTRAINTS_free(ext_pcons);\n\tif (ext_any)\n\t\tASN1_INTEGER_free(ext_any);\n\treturn 1;\n}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\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#ifndef OPENSSL_CPUID_OBJ\n if(ret && (num > 2048))\n\t{\textern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\t}\n#endif\n\treturn ret;\n\t}', 'static int tree_link_nodes(X509_POLICY_LEVEL *curr,\n\t\t\t\tconst X509_POLICY_CACHE *cache)\n\t{\n\tint i;\n\tX509_POLICY_LEVEL *last;\n\tX509_POLICY_DATA *data;\n\tX509_POLICY_NODE *parent;\n\tlast = curr - 1;\n\tfor (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++)\n\t\t{\n\t\tdata = sk_X509_POLICY_DATA_value(cache->data, i);\n\t\tif ((data->flags & POLICY_DATA_FLAG_MAPPED_ANY)\n\t\t\t&& !(curr->flags & X509_V_FLAG_INHIBIT_ANY))\n\t\t\tcontinue;\n\t\tparent = level_find_node(last, data->valid_policy);\n\t\tif (!parent)\n\t\t\tparent = last->anyPolicy;\n\t\tif (parent && !level_add_node(curr, data, parent, NULL))\n\t\t\t\treturn 0;\n\t\t}\n\treturn 1;\n\t}'] |
2,241 | 0 | https://github.com/openssl/openssl/blob/24b8e4b2c835d6bf52c2768d4d4a78ed7d7e85bb/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_server_renegotiate(SSL *s, WPACKET *pkt, int *al)\n{\n if (!s->s3->send_connection_binding)\n return 1;\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 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_RENEGOTIATE, ERR_R_INTERNAL_ERROR);\n return 0;\n }\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_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}'] |
2,242 | 0 | https://github.com/openssl/openssl/blob/a44a208442ecf8f576c9e364f8b46b6661c7d2de/crypto/evp/digest.c/#L363 | int EVP_Digest(const void *data, size_t count,
unsigned char *md, unsigned int *size, const EVP_MD *type,
ENGINE *impl)
{
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
int ret;
if (ctx == NULL)
return 0;
EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
ret = EVP_DigestInit_ex(ctx, type, impl)
&& EVP_DigestUpdate(ctx, data, count)
&& EVP_DigestFinal_ex(ctx, md, size);
EVP_MD_CTX_free(ctx);
return ret;
} | ['int EVP_Digest(const void *data, size_t count,\n unsigned char *md, unsigned int *size, const EVP_MD *type,\n ENGINE *impl)\n{\n EVP_MD_CTX *ctx = EVP_MD_CTX_new();\n int ret;\n if (ctx == NULL)\n return 0;\n EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);\n ret = EVP_DigestInit_ex(ctx, type, impl)\n && EVP_DigestUpdate(ctx, data, count)\n && EVP_DigestFinal_ex(ctx, md, size);\n EVP_MD_CTX_free(ctx);\n return ret;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_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#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 EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)\n{\n ctx->flags |= flags;\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx);\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}'] |
2,243 | 0 | https://github.com/openssl/openssl/blob/09c4b4e0b7c795daa99aeca27f83fcac007db33d/crypto/asn1/a_strex.c/#L478 | static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n,
int indent, unsigned long flags)
{
int i, prev = -1, orflags, cnt;
int fn_opt, fn_nid;
ASN1_OBJECT *fn;
ASN1_STRING *val;
X509_NAME_ENTRY *ent;
char objtmp[80];
const char *objbuf;
int outlen, len;
char *sep_dn, *sep_mv, *sep_eq;
int sep_dn_len, sep_mv_len, sep_eq_len;
if(indent < 0) indent = 0;
outlen = indent;
if(!do_indent(io_ch, arg, indent)) return -1;
switch (flags & XN_FLAG_SEP_MASK)
{
case XN_FLAG_SEP_MULTILINE:
sep_dn = "\n";
sep_dn_len = 1;
sep_mv = " + ";
sep_mv_len = 3;
break;
case XN_FLAG_SEP_COMMA_PLUS:
sep_dn = ",";
sep_dn_len = 1;
sep_mv = "+";
sep_mv_len = 1;
indent = 0;
break;
case XN_FLAG_SEP_CPLUS_SPC:
sep_dn = ", ";
sep_dn_len = 2;
sep_mv = " + ";
sep_mv_len = 3;
indent = 0;
break;
case XN_FLAG_SEP_SPLUS_SPC:
sep_dn = "; ";
sep_dn_len = 2;
sep_mv = " + ";
sep_mv_len = 3;
indent = 0;
break;
default:
return -1;
}
if(flags & XN_FLAG_SPC_EQ) {
sep_eq = " = ";
sep_eq_len = 3;
} else {
sep_eq = "=";
sep_eq_len = 1;
}
fn_opt = flags & XN_FLAG_FN_MASK;
cnt = X509_NAME_entry_count(n);
for(i = 0; i < cnt; i++) {
if(flags & XN_FLAG_DN_REV)
ent = X509_NAME_get_entry(n, cnt - i - 1);
else ent = X509_NAME_get_entry(n, i);
if(prev != -1) {
if(prev == ent->set) {
if(!io_ch(arg, sep_mv, sep_mv_len)) return -1;
outlen += sep_mv_len;
} else {
if(!io_ch(arg, sep_dn, sep_dn_len)) return -1;
outlen += sep_dn_len;
if(!do_indent(io_ch, arg, indent)) return -1;
outlen += indent;
}
}
prev = ent->set;
fn = X509_NAME_ENTRY_get_object(ent);
val = X509_NAME_ENTRY_get_data(ent);
fn_nid = OBJ_obj2nid(fn);
if(fn_opt != XN_FLAG_FN_NONE) {
int objlen, fld_len;
if((fn_opt == XN_FLAG_FN_OID) || (fn_nid==NID_undef) ) {
OBJ_obj2txt(objtmp, 80, fn, 1);
objbuf = objtmp;
} else {
if(fn_opt == XN_FLAG_FN_SN) {
fld_len = FN_WIDTH_SN;
objbuf = OBJ_nid2sn(fn_nid);
} else if(fn_opt == XN_FLAG_FN_LN) {
fld_len = FN_WIDTH_LN;
objbuf = OBJ_nid2ln(fn_nid);
} else objbuf = "";
}
objlen = strlen(objbuf);
if(!io_ch(arg, objbuf, objlen)) return -1;
if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
if (!do_indent(io_ch, arg, fld_len - objlen)) return -1;
outlen += fld_len - objlen;
}
if(!io_ch(arg, sep_eq, sep_eq_len)) return -1;
outlen += objlen + sep_eq_len;
}
if((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
orflags = ASN1_STRFLGS_DUMP_ALL;
else orflags = 0;
len = do_print_ex(io_ch, arg, flags | orflags, val);
if(len < 0) return -1;
outlen += len;
}
return outlen;
} | ['static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n,\n\t\t\t\tint indent, unsigned long flags)\n{\n\tint i, prev = -1, orflags, cnt;\n\tint fn_opt, fn_nid;\n\tASN1_OBJECT *fn;\n\tASN1_STRING *val;\n\tX509_NAME_ENTRY *ent;\n\tchar objtmp[80];\n\tconst char *objbuf;\n\tint outlen, len;\n\tchar *sep_dn, *sep_mv, *sep_eq;\n\tint sep_dn_len, sep_mv_len, sep_eq_len;\n\tif(indent < 0) indent = 0;\n\toutlen = indent;\n\tif(!do_indent(io_ch, arg, indent)) return -1;\n\tswitch (flags & XN_FLAG_SEP_MASK)\n\t{\n\t\tcase XN_FLAG_SEP_MULTILINE:\n\t\tsep_dn = "\\n";\n\t\tsep_dn_len = 1;\n\t\tsep_mv = " + ";\n\t\tsep_mv_len = 3;\n\t\tbreak;\n\t\tcase XN_FLAG_SEP_COMMA_PLUS:\n\t\tsep_dn = ",";\n\t\tsep_dn_len = 1;\n\t\tsep_mv = "+";\n\t\tsep_mv_len = 1;\n\t\tindent = 0;\n\t\tbreak;\n\t\tcase XN_FLAG_SEP_CPLUS_SPC:\n\t\tsep_dn = ", ";\n\t\tsep_dn_len = 2;\n\t\tsep_mv = " + ";\n\t\tsep_mv_len = 3;\n\t\tindent = 0;\n\t\tbreak;\n\t\tcase XN_FLAG_SEP_SPLUS_SPC:\n\t\tsep_dn = "; ";\n\t\tsep_dn_len = 2;\n\t\tsep_mv = " + ";\n\t\tsep_mv_len = 3;\n\t\tindent = 0;\n\t\tbreak;\n\t\tdefault:\n\t\treturn -1;\n\t}\n\tif(flags & XN_FLAG_SPC_EQ) {\n\t\tsep_eq = " = ";\n\t\tsep_eq_len = 3;\n\t} else {\n\t\tsep_eq = "=";\n\t\tsep_eq_len = 1;\n\t}\n\tfn_opt = flags & XN_FLAG_FN_MASK;\n\tcnt = X509_NAME_entry_count(n);\n\tfor(i = 0; i < cnt; i++) {\n\t\tif(flags & XN_FLAG_DN_REV)\n\t\t\t\tent = X509_NAME_get_entry(n, cnt - i - 1);\n\t\telse ent = X509_NAME_get_entry(n, i);\n\t\tif(prev != -1) {\n\t\t\tif(prev == ent->set) {\n\t\t\t\tif(!io_ch(arg, sep_mv, sep_mv_len)) return -1;\n\t\t\t\toutlen += sep_mv_len;\n\t\t\t} else {\n\t\t\t\tif(!io_ch(arg, sep_dn, sep_dn_len)) return -1;\n\t\t\t\toutlen += sep_dn_len;\n\t\t\t\tif(!do_indent(io_ch, arg, indent)) return -1;\n\t\t\t\toutlen += indent;\n\t\t\t}\n\t\t}\n\t\tprev = ent->set;\n\t\tfn = X509_NAME_ENTRY_get_object(ent);\n\t\tval = X509_NAME_ENTRY_get_data(ent);\n\t\tfn_nid = OBJ_obj2nid(fn);\n\t\tif(fn_opt != XN_FLAG_FN_NONE) {\n\t\t\tint objlen, fld_len;\n\t\t\tif((fn_opt == XN_FLAG_FN_OID) || (fn_nid==NID_undef) ) {\n\t\t\t\tOBJ_obj2txt(objtmp, 80, fn, 1);\n\t\t\t\tobjbuf = objtmp;\n\t\t\t} else {\n\t\t\t\tif(fn_opt == XN_FLAG_FN_SN) {\n\t\t\t\t\tfld_len = FN_WIDTH_SN;\n\t\t\t\t\tobjbuf = OBJ_nid2sn(fn_nid);\n\t\t\t\t} else if(fn_opt == XN_FLAG_FN_LN) {\n\t\t\t\t\tfld_len = FN_WIDTH_LN;\n\t\t\t\t\tobjbuf = OBJ_nid2ln(fn_nid);\n\t\t\t\t} else objbuf = "";\n\t\t\t}\n\t\t\tobjlen = strlen(objbuf);\n\t\t\tif(!io_ch(arg, objbuf, objlen)) return -1;\n\t\t\tif ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {\n\t\t\t\tif (!do_indent(io_ch, arg, fld_len - objlen)) return -1;\n\t\t\t\toutlen += fld_len - objlen;\n\t\t\t}\n\t\t\tif(!io_ch(arg, sep_eq, sep_eq_len)) return -1;\n\t\t\toutlen += objlen + sep_eq_len;\n\t\t}\n\t\tif((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))\n\t\t\t\t\torflags = ASN1_STRFLGS_DUMP_ALL;\n\t\telse orflags = 0;\n\t\tlen = do_print_ex(io_ch, arg, flags | orflags, val);\n\t\tif(len < 0) return -1;\n\t\toutlen += len;\n\t}\n\treturn outlen;\n}'] |
2,244 | 0 | https://github.com/openssl/openssl/blob/475965f2ef8eaeb67f065a7eabd3a781da76305b/crypto/blake2/blake2_impl.h/#L33 | static ossl_inline uint32_t load32(const uint8_t *src)
{
const union {
long one;
char little;
} is_endian = { 1 };
if (is_endian.little) {
uint32_t w;
memcpy(&w, src, sizeof(w));
return w;
} else {
uint32_t w = ((uint32_t)src[0])
| ((uint32_t)src[1] << 8)
| ((uint32_t)src[2] << 16)
| ((uint32_t)src[3] << 24);
return w;
}
} | ['int BLAKE2s_Init(BLAKE2S_CTX *c)\n{\n BLAKE2S_PARAM P[1];\n P->digest_length = BLAKE2S_DIGEST_LENGTH;\n P->key_length = 0;\n P->fanout = 1;\n P->depth = 1;\n store32(P->leaf_length, 0);\n store48(P->node_offset, 0);\n P->node_depth = 0;\n P->inner_length = 0;\n memset(P->salt, 0, sizeof(P->salt));\n memset(P->personal, 0, sizeof(P->personal));\n blake2s_init_param(c, P);\n return 1;\n}', 'static void blake2s_init_param(BLAKE2S_CTX *S, const BLAKE2S_PARAM *P)\n{\n const uint8_t *p = (const uint8_t *)(P);\n size_t i;\n OPENSSL_assert(sizeof(BLAKE2S_PARAM) == 32);\n blake2s_init0(S);\n for (i = 0; i < 8; ++i) {\n S->h[i] ^= load32(&p[i*4]);\n }\n}', 'static ossl_inline uint32_t load32(const uint8_t *src)\n{\n const union {\n long one;\n char little;\n } is_endian = { 1 };\n if (is_endian.little) {\n uint32_t w;\n memcpy(&w, src, sizeof(w));\n return w;\n } else {\n uint32_t w = ((uint32_t)src[0])\n | ((uint32_t)src[1] << 8)\n | ((uint32_t)src[2] << 16)\n | ((uint32_t)src[3] << 24);\n return w;\n }\n}'] |
2,245 | 0 | https://github.com/libav/libav/blob/8bd1956462e862315a78ca6442c5b54c5dd1f826/libavfilter/avfilter.c/#L73 | void avfilter_unref_buffer(AVFilterBufferRef *ref)
{
if (!(--ref->buf->refcount))
ref->buf->free(ref->buf);
av_free(ref->video);
av_free(ref->audio);
av_free(ref);
} | ['static void end_frame(AVFilterLink *inlink)\n{\n GradFunContext *gf = inlink->dst->priv;\n AVFilterBufferRef *inpic = inlink->cur_buf;\n AVFilterLink *outlink = inlink->dst->outputs[0];\n AVFilterBufferRef *outpic = outlink->out_buf;\n int p;\n for (p = 0; p < 4 && inpic->data[p]; p++) {\n int w = inlink->w;\n int h = inlink->h;\n int r = gf->radius;\n if (p) {\n w = gf->chroma_w;\n h = gf->chroma_h;\n r = gf->chroma_r;\n }\n if (FFMIN(w, h) > 2 * r)\n filter(gf, outpic->data[p], inpic->data[p], w, h, outpic->linesize[p], inpic->linesize[p], r);\n else if (outpic->data[p] != inpic->data[p])\n av_image_copy_plane(outpic->data[p], outpic->linesize[p], inpic->data[p], inpic->linesize[p], w, h);\n }\n avfilter_draw_slice(outlink, 0, inlink->h, 1);\n avfilter_end_frame(outlink);\n avfilter_unref_buffer(inpic);\n if (outpic != inpic)\n avfilter_unref_buffer(outpic);\n}', 'void avfilter_end_frame(AVFilterLink *link)\n{\n void (*end_frame)(AVFilterLink *);\n if (!(end_frame = link->dstpad->end_frame))\n end_frame = avfilter_default_end_frame;\n end_frame(link);\n if (link->src_buf) {\n avfilter_unref_buffer(link->src_buf);\n link->src_buf = NULL;\n }\n}', 'void avfilter_unref_buffer(AVFilterBufferRef *ref)\n{\n if (!(--ref->buf->refcount))\n ref->buf->free(ref->buf);\n av_free(ref->video);\n av_free(ref->audio);\n av_free(ref);\n}'] |
2,246 | 0 | https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L263 | static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
const int lt= src[-1-1*stride];
LOAD_TOP_EDGE
LOAD_LEFT_EDGE
src[0+0*stride]=
src[1+2*stride]=(lt + t0 + 1)>>1;
src[1+0*stride]=
src[2+2*stride]=(t0 + t1 + 1)>>1;
src[2+0*stride]=
src[3+2*stride]=(t1 + t2 + 1)>>1;
src[3+0*stride]=(t2 + t3 + 1)>>1;
src[0+1*stride]=
src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
src[1+1*stride]=
src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
src[2+1*stride]=
src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
} | ['static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){\n const int lt= src[-1-1*stride];\n LOAD_TOP_EDGE\n LOAD_LEFT_EDGE\n src[0+0*stride]=\n src[1+2*stride]=(lt + t0 + 1)>>1;\n src[1+0*stride]=\n src[2+2*stride]=(t0 + t1 + 1)>>1;\n src[2+0*stride]=\n src[3+2*stride]=(t1 + t2 + 1)>>1;\n src[3+0*stride]=(t2 + t3 + 1)>>1;\n src[0+1*stride]=\n src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;\n src[1+1*stride]=\n src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;\n src[2+1*stride]=\n src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;\n src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;\n src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;\n src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;\n}'] |
2,247 | 1 | https://github.com/libav/libav/blob/a5ef830b1226273c35d4baa1c2e4e7e1c9de7c7d/libavcodec/atrac3.c/#L932 | static av_cold int atrac3_decode_init(AVCodecContext *avctx)
{
int i, ret;
int version, delay, samples_per_frame, frame_factor;
const uint8_t *edata_ptr = avctx->extradata;
ATRAC3Context *q = avctx->priv_data;
if (avctx->channels <= 0 || avctx->channels > 2) {
av_log(avctx, AV_LOG_ERROR, "Channel configuration error!\n");
return AVERROR(EINVAL);
}
if (avctx->extradata_size == 14) {
av_log(avctx, AV_LOG_DEBUG, "[0-1] %d\n",
bytestream_get_le16(&edata_ptr));
edata_ptr += 4;
q->coding_mode = bytestream_get_le16(&edata_ptr);
av_log(avctx, AV_LOG_DEBUG,"[8-9] %d\n",
bytestream_get_le16(&edata_ptr));
frame_factor = bytestream_get_le16(&edata_ptr);
av_log(avctx, AV_LOG_DEBUG,"[12-13] %d\n",
bytestream_get_le16(&edata_ptr));
samples_per_frame = SAMPLES_PER_FRAME * avctx->channels;
version = 4;
delay = 0x88E;
q->coding_mode = q->coding_mode ? JOINT_STEREO : STEREO;
q->scrambled_stream = 0;
if (avctx->block_align != 96 * avctx->channels * frame_factor &&
avctx->block_align != 152 * avctx->channels * frame_factor &&
avctx->block_align != 192 * avctx->channels * frame_factor) {
av_log(avctx, AV_LOG_ERROR, "Unknown frame/channel/frame_factor "
"configuration %d/%d/%d\n", avctx->block_align,
avctx->channels, frame_factor);
return AVERROR_INVALIDDATA;
}
} else if (avctx->extradata_size == 10) {
version = bytestream_get_be32(&edata_ptr);
samples_per_frame = bytestream_get_be16(&edata_ptr);
delay = bytestream_get_be16(&edata_ptr);
q->coding_mode = bytestream_get_be16(&edata_ptr);
q->scrambled_stream = 1;
} else {
av_log(NULL, AV_LOG_ERROR, "Unknown extradata size %d.\n",
avctx->extradata_size);
}
if (version != 4) {
av_log(avctx, AV_LOG_ERROR, "Version %d != 4.\n", version);
return AVERROR_INVALIDDATA;
}
if (samples_per_frame != SAMPLES_PER_FRAME &&
samples_per_frame != SAMPLES_PER_FRAME * 2) {
av_log(avctx, AV_LOG_ERROR, "Unknown amount of samples per frame %d.\n",
samples_per_frame);
return AVERROR_INVALIDDATA;
}
if (delay != 0x88E) {
av_log(avctx, AV_LOG_ERROR, "Unknown amount of delay %x != 0x88E.\n",
delay);
return AVERROR_INVALIDDATA;
}
if (q->coding_mode == STEREO)
av_log(avctx, AV_LOG_DEBUG, "Normal stereo detected.\n");
else if (q->coding_mode == JOINT_STEREO)
av_log(avctx, AV_LOG_DEBUG, "Joint stereo detected.\n");
else {
av_log(avctx, AV_LOG_ERROR, "Unknown channel coding mode %x!\n",
q->coding_mode);
return AVERROR_INVALIDDATA;
}
if (avctx->block_align >= UINT_MAX / 2)
return AVERROR(EINVAL);
q->decoded_bytes_buffer = av_mallocz(FFALIGN(avctx->block_align, 4) +
FF_INPUT_BUFFER_PADDING_SIZE);
if (q->decoded_bytes_buffer == NULL)
return AVERROR(ENOMEM);
avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
if ((ret = ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0 / 32768)) < 0) {
av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
av_freep(&q->decoded_bytes_buffer);
return ret;
}
q->weighting_delay[0] = 0;
q->weighting_delay[1] = 7;
q->weighting_delay[2] = 0;
q->weighting_delay[3] = 7;
q->weighting_delay[4] = 0;
q->weighting_delay[5] = 7;
for (i = 0; i < 4; i++) {
q->matrix_coeff_index_prev[i] = 3;
q->matrix_coeff_index_now[i] = 3;
q->matrix_coeff_index_next[i] = 3;
}
avpriv_float_dsp_init(&q->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
ff_fmt_convert_init(&q->fmt_conv, avctx);
q->units = av_mallocz(sizeof(*q->units) * avctx->channels);
if (!q->units) {
atrac3_decode_close(avctx);
return AVERROR(ENOMEM);
}
avcodec_get_frame_defaults(&q->frame);
avctx->coded_frame = &q->frame;
return 0;
} | ['static av_cold int atrac3_decode_init(AVCodecContext *avctx)\n{\n int i, ret;\n int version, delay, samples_per_frame, frame_factor;\n const uint8_t *edata_ptr = avctx->extradata;\n ATRAC3Context *q = avctx->priv_data;\n if (avctx->channels <= 0 || avctx->channels > 2) {\n av_log(avctx, AV_LOG_ERROR, "Channel configuration error!\\n");\n return AVERROR(EINVAL);\n }\n if (avctx->extradata_size == 14) {\n av_log(avctx, AV_LOG_DEBUG, "[0-1] %d\\n",\n bytestream_get_le16(&edata_ptr));\n edata_ptr += 4;\n q->coding_mode = bytestream_get_le16(&edata_ptr);\n av_log(avctx, AV_LOG_DEBUG,"[8-9] %d\\n",\n bytestream_get_le16(&edata_ptr));\n frame_factor = bytestream_get_le16(&edata_ptr);\n av_log(avctx, AV_LOG_DEBUG,"[12-13] %d\\n",\n bytestream_get_le16(&edata_ptr));\n samples_per_frame = SAMPLES_PER_FRAME * avctx->channels;\n version = 4;\n delay = 0x88E;\n q->coding_mode = q->coding_mode ? JOINT_STEREO : STEREO;\n q->scrambled_stream = 0;\n if (avctx->block_align != 96 * avctx->channels * frame_factor &&\n avctx->block_align != 152 * avctx->channels * frame_factor &&\n avctx->block_align != 192 * avctx->channels * frame_factor) {\n av_log(avctx, AV_LOG_ERROR, "Unknown frame/channel/frame_factor "\n "configuration %d/%d/%d\\n", avctx->block_align,\n avctx->channels, frame_factor);\n return AVERROR_INVALIDDATA;\n }\n } else if (avctx->extradata_size == 10) {\n version = bytestream_get_be32(&edata_ptr);\n samples_per_frame = bytestream_get_be16(&edata_ptr);\n delay = bytestream_get_be16(&edata_ptr);\n q->coding_mode = bytestream_get_be16(&edata_ptr);\n q->scrambled_stream = 1;\n } else {\n av_log(NULL, AV_LOG_ERROR, "Unknown extradata size %d.\\n",\n avctx->extradata_size);\n }\n if (version != 4) {\n av_log(avctx, AV_LOG_ERROR, "Version %d != 4.\\n", version);\n return AVERROR_INVALIDDATA;\n }\n if (samples_per_frame != SAMPLES_PER_FRAME &&\n samples_per_frame != SAMPLES_PER_FRAME * 2) {\n av_log(avctx, AV_LOG_ERROR, "Unknown amount of samples per frame %d.\\n",\n samples_per_frame);\n return AVERROR_INVALIDDATA;\n }\n if (delay != 0x88E) {\n av_log(avctx, AV_LOG_ERROR, "Unknown amount of delay %x != 0x88E.\\n",\n delay);\n return AVERROR_INVALIDDATA;\n }\n if (q->coding_mode == STEREO)\n av_log(avctx, AV_LOG_DEBUG, "Normal stereo detected.\\n");\n else if (q->coding_mode == JOINT_STEREO)\n av_log(avctx, AV_LOG_DEBUG, "Joint stereo detected.\\n");\n else {\n av_log(avctx, AV_LOG_ERROR, "Unknown channel coding mode %x!\\n",\n q->coding_mode);\n return AVERROR_INVALIDDATA;\n }\n if (avctx->block_align >= UINT_MAX / 2)\n return AVERROR(EINVAL);\n q->decoded_bytes_buffer = av_mallocz(FFALIGN(avctx->block_align, 4) +\n FF_INPUT_BUFFER_PADDING_SIZE);\n if (q->decoded_bytes_buffer == NULL)\n return AVERROR(ENOMEM);\n avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;\n if ((ret = ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0 / 32768)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\\n");\n av_freep(&q->decoded_bytes_buffer);\n return ret;\n }\n q->weighting_delay[0] = 0;\n q->weighting_delay[1] = 7;\n q->weighting_delay[2] = 0;\n q->weighting_delay[3] = 7;\n q->weighting_delay[4] = 0;\n q->weighting_delay[5] = 7;\n for (i = 0; i < 4; i++) {\n q->matrix_coeff_index_prev[i] = 3;\n q->matrix_coeff_index_now[i] = 3;\n q->matrix_coeff_index_next[i] = 3;\n }\n avpriv_float_dsp_init(&q->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);\n ff_fmt_convert_init(&q->fmt_conv, avctx);\n q->units = av_mallocz(sizeof(*q->units) * avctx->channels);\n if (!q->units) {\n atrac3_decode_close(avctx);\n return AVERROR(ENOMEM);\n }\n avcodec_get_frame_defaults(&q->frame);\n avctx->coded_frame = &q->frame;\n return 0;\n}'] |
2,248 | 0 | https://github.com/openssl/openssl/blob/dd616752a1ad3a4cae469ea9ceac50141debe32b/apps/ca.c/#L2916 | int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, const char *str)
{
char *tmp = NULL;
char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
int reason_code = -1;
int ret = 0;
unsigned int i;
ASN1_OBJECT *hold = NULL;
ASN1_GENERALIZEDTIME *comp_time = NULL;
tmp = BUF_strdup(str);
p = strchr(tmp, ',');
rtime_str = tmp;
if (p)
{
*p = '\0';
p++;
reason_str = p;
p = strchr(p, ',');
if (p)
{
*p = '\0';
arg_str = p + 1;
}
}
if (prevtm)
{
*prevtm = ASN1_UTCTIME_new();
if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str))
{
BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str);
goto err;
}
}
if (reason_str)
{
for (i = 0; i < NUM_REASONS; i++)
{
if(!strcasecmp(reason_str, crl_reasons[i]))
{
reason_code = i;
break;
}
}
if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS)
{
BIO_printf(bio_err, "invalid reason code %s\n", reason_str);
goto err;
}
if (reason_code == 7)
reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
else if (reason_code == 8)
{
if (!arg_str)
{
BIO_printf(bio_err, "missing hold instruction\n");
goto err;
}
reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
hold = OBJ_txt2obj(arg_str, 0);
if (!hold)
{
BIO_printf(bio_err, "invalid object identifier %s\n", arg_str);
goto err;
}
if (phold) *phold = hold;
}
else if ((reason_code == 9) || (reason_code == 10))
{
if (!arg_str)
{
BIO_printf(bio_err, "missing compromised time\n");
goto err;
}
comp_time = ASN1_GENERALIZEDTIME_new();
if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str))
{
BIO_printf(bio_err, "invalid compromised time %s\n", arg_str);
goto err;
}
if (reason_code == 9)
reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
else
reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
}
}
if (preason) *preason = reason_code;
if (pinvtm) *pinvtm = comp_time;
else ASN1_GENERALIZEDTIME_free(comp_time);
ret = 1;
err:
if (tmp) OPENSSL_free(tmp);
if (!phold) ASN1_OBJECT_free(hold);
if (!pinvtm) ASN1_GENERALIZEDTIME_free(comp_time);
return ret;
} | ['int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, const char *str)\n\t{\n\tchar *tmp = NULL;\n\tchar *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;\n\tint reason_code = -1;\n\tint ret = 0;\n\tunsigned int i;\n\tASN1_OBJECT *hold = NULL;\n\tASN1_GENERALIZEDTIME *comp_time = NULL;\n\ttmp = BUF_strdup(str);\n\tp = strchr(tmp, \',\');\n\trtime_str = tmp;\n\tif (p)\n\t\t{\n\t\t*p = \'\\0\';\n\t\tp++;\n\t\treason_str = p;\n\t\tp = strchr(p, \',\');\n\t\tif (p)\n\t\t\t{\n\t\t\t*p = \'\\0\';\n\t\t\targ_str = p + 1;\n\t\t\t}\n\t\t}\n\tif (prevtm)\n\t\t{\n\t\t*prevtm = ASN1_UTCTIME_new();\n\t\tif (!ASN1_UTCTIME_set_string(*prevtm, rtime_str))\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "invalid revocation date %s\\n", rtime_str);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (reason_str)\n\t\t{\n\t\tfor (i = 0; i < NUM_REASONS; i++)\n\t\t\t{\n\t\t\tif(!strcasecmp(reason_str, crl_reasons[i]))\n\t\t\t\t{\n\t\t\t\treason_code = i;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tif (reason_code == OCSP_REVOKED_STATUS_NOSTATUS)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "invalid reason code %s\\n", reason_str);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (reason_code == 7)\n\t\t\treason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;\n\t\telse if (reason_code == 8)\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing hold instruction\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\treason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;\n\t\t\thold = OBJ_txt2obj(arg_str, 0);\n\t\t\tif (!hold)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid object identifier %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (phold) *phold = hold;\n\t\t\t}\n\t\telse if ((reason_code == 9) || (reason_code == 10))\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing compromised time\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tcomp_time = ASN1_GENERALIZEDTIME_new();\n\t\t\tif (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid compromised time %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (reason_code == 9)\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;\n\t\t\telse\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;\n\t\t\t}\n\t\t}\n\tif (preason) *preason = reason_code;\n\tif (pinvtm) *pinvtm = comp_time;\n\telse ASN1_GENERALIZEDTIME_free(comp_time);\n\tret = 1;\n\terr:\n\tif (tmp) OPENSSL_free(tmp);\n\tif (!phold) ASN1_OBJECT_free(hold);\n\tif (!pinvtm) ASN1_GENERALIZEDTIME_free(comp_time);\n\treturn ret;\n\t}', 'char *BUF_strdup(const char *str)\n\t{\n\tif (str == NULL) return(NULL);\n\treturn BUF_strndup(str, strlen(str));\n\t}'] |
2,249 | 0 | https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_lib.c/#L250 | int BN_num_bits(const BIGNUM *a)
{
int i = a->top - 1;
bn_check_top(a);
if (BN_is_zero(a)) return 0;
return ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));
} | ['static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,\n\t\tBIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,\n\t\tBN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tBIGNUM t;\n\tint to_return = 0;\n\tBN_init(&t);\n\tif (!atalla_mod_exp(rr,a1,p1,m,ctx)) goto end;\n\tif (!atalla_mod_exp(&t,a2,p2,m,ctx)) goto end;\n\tif (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;\n\tto_return = 1;\nend:\n\tBN_free(&t);\n\treturn to_return;\n\t}', 'static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n\t\t\tconst BIGNUM *m, BN_CTX *ctx)\n\t{\n\tBIGNUM *modulus;\n\tBIGNUM *exponent;\n\tBIGNUM *argument;\n\tBIGNUM *result;\n\tRSAPrivateKey keydata;\n\tint to_return, numbytes;\n\tmodulus = exponent = argument = result = NULL;\n\tto_return = 0;\n\tif(!atalla_dso)\n\t\t{\n\t\tATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_NOT_LOADED);\n\t\tgoto err;\n\t\t}\n\tBN_CTX_start(ctx);\n\tmodulus = BN_CTX_get(ctx);\n\texponent = BN_CTX_get(ctx);\n\targument = BN_CTX_get(ctx);\n\tresult = BN_CTX_get(ctx);\n\tif (!result)\n\t\t{\n\t\tATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_CTX_FULL);\n\t\tgoto err;\n\t\t}\n\tif(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||\n\t !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top))\n\t\t{\n\t\tATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_EXPAND_FAIL);\n\t\tgoto err;\n\t\t}\n\tmemset(&keydata, 0,sizeof keydata);\n\tnumbytes = BN_num_bytes(m);\n\tmemset(exponent->d, 0, numbytes);\n\tmemset(modulus->d, 0, numbytes);\n\tBN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));\n\tBN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));\n\tkeydata.privateExponent.data = (unsigned char *)exponent->d;\n\tkeydata.privateExponent.len = numbytes;\n\tkeydata.modulus.data = (unsigned char *)modulus->d;\n\tkeydata.modulus.len = numbytes;\n\tmemset(argument->d, 0, numbytes);\n\tmemset(result->d, 0, numbytes);\n\tBN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));\n\tif(p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,\n\t\t\t(unsigned char *)argument->d,\n\t\t\tkeydata.modulus.len) != 0)\n\t\t{\n\t\tATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_REQUEST_FAILED);\n\t\tgoto err;\n\t\t}\n\tBN_bin2bn((unsigned char *)result->d, numbytes, r);\n\tto_return = 1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn to_return;\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tint i = a->top - 1;\n\tbn_check_top(a);\n\tif (BN_is_zero(a)) return 0;\n\treturn ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));\n\t}'] |
2,250 | 0 | https://github.com/openssl/openssl/blob/6bc62a620e715f7580651ca932eab052aa527886/crypto/bn/bn_ctx.c/#L268 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int ec_GFp_mont_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,\n BN_CTX *ctx)\n{\n BIGNUM *e = NULL;\n BN_CTX *new_ctx = NULL;\n int ret = 0;\n if (group->field_data1 == NULL)\n return 0;\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)\n return 0;\n BN_CTX_start(ctx);\n if ((e = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_set_word(e, 2))\n goto err;\n if (!BN_sub(e, group->field, e))\n goto err;\n if (!BN_mod_exp_mont(r, a, e, group->field, ctx, group->field_data1))\n goto err;\n if (BN_is_zero(r)) {\n ECerr(EC_F_EC_GFP_MONT_FIELD_INV, EC_R_CANNOT_INVERT);\n goto err;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_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 d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(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_mul_mont_fixed_top(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 r->flags |= BN_FLG_FIXED_TOP;\n } else\n#endif\n if (!bn_to_mont_fixed_top(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_mul_mont_fixed_top(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_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue, 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 if (a->neg || BN_ucmp(a, m) >= 0) {\n BIGNUM *reduced = BN_CTX_get(ctx);\n if (reduced == NULL\n || !BN_nnmod(reduced, a, m, ctx)) {\n goto err;\n }\n a = reduced;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!bn_to_mont_fixed_top(&tmp, BN_value_one(), mont, ctx))\n goto err;\n 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_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)\n mont->RR.d[i] = 0;\n mont->RR.top = ret;\n mont->RR.flags |= BN_FLG_FIXED_TOP;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {\n if (pnoinv != NULL)\n *pnoinv = 1;\n return NULL;\n }\n if (pnoinv != NULL)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n {\n BIGNUM local_B;\n bn_init(&local_B);\n BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, &local_B, A, ctx))\n goto err;\n }\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n {\n BIGNUM local_A;\n bn_init(&local_A);\n BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, &local_A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
2,251 | 0 | https://github.com/openssl/openssl/blob/ac5a110621ca48f0bebd5b4d76d081de403da29e/crypto/bio/bio_cb.c/#L82 | long BIO_debug_callback(BIO *bio, int cmd, const char *argp,
int argi, long argl, long ret)
{
BIO *b;
char buf[256];
char *p;
long r = 1;
int len;
size_t p_maxlen;
if (BIO_CB_RETURN & cmd)
r = ret;
len = BIO_snprintf(buf,sizeof buf,"BIO[%p]: ",(void *)bio);
p = buf + len;
p_maxlen = sizeof(buf) - len;
switch (cmd) {
case BIO_CB_FREE:
BIO_snprintf(p, p_maxlen, "Free - %s\n", bio->method->name);
break;
case BIO_CB_READ:
if (bio->method->type & BIO_TYPE_DESCRIPTOR)
BIO_snprintf(p, p_maxlen, "read(%d,%lu) - %s fd=%d\n",
bio->num, (unsigned long)argi,
bio->method->name, bio->num);
else
BIO_snprintf(p, p_maxlen, "read(%d,%lu) - %s\n",
bio->num, (unsigned long)argi, bio->method->name);
break;
case BIO_CB_WRITE:
if (bio->method->type & BIO_TYPE_DESCRIPTOR)
BIO_snprintf(p, p_maxlen, "write(%d,%lu) - %s fd=%d\n",
bio->num, (unsigned long)argi,
bio->method->name, bio->num);
else
BIO_snprintf(p, p_maxlen, "write(%d,%lu) - %s\n",
bio->num, (unsigned long)argi, bio->method->name);
break;
case BIO_CB_PUTS:
BIO_snprintf(p, p_maxlen, "puts() - %s\n", bio->method->name);
break;
case BIO_CB_GETS:
BIO_snprintf(p, p_maxlen, "gets(%lu) - %s\n", (unsigned long)argi,
bio->method->name);
break;
case BIO_CB_CTRL:
BIO_snprintf(p, p_maxlen, "ctrl(%lu) - %s\n", (unsigned long)argi,
bio->method->name);
break;
case BIO_CB_RETURN | BIO_CB_READ:
BIO_snprintf(p, p_maxlen, "read return %ld\n", ret);
break;
case BIO_CB_RETURN | BIO_CB_WRITE:
BIO_snprintf(p, p_maxlen, "write return %ld\n", ret);
break;
case BIO_CB_RETURN | BIO_CB_GETS:
BIO_snprintf(p, p_maxlen, "gets return %ld\n", ret);
break;
case BIO_CB_RETURN | BIO_CB_PUTS:
BIO_snprintf(p, p_maxlen, "puts return %ld\n", ret);
break;
case BIO_CB_RETURN | BIO_CB_CTRL:
BIO_snprintf(p, p_maxlen, "ctrl return %ld\n", ret);
break;
default:
BIO_snprintf(p, p_maxlen, "bio callback - unknown type (%d)\n", cmd);
break;
}
b = (BIO *)bio->cb_arg;
if (b != NULL)
BIO_write(b, buf, strlen(buf));
#if !defined(OPENSSL_NO_STDIO)
else
fputs(buf, stderr);
#endif
return (r);
} | ['long BIO_debug_callback(BIO *bio, int cmd, const char *argp,\n int argi, long argl, long ret)\n{\n BIO *b;\n char buf[256];\n char *p;\n long r = 1;\n int len;\n size_t p_maxlen;\n if (BIO_CB_RETURN & cmd)\n r = ret;\n len = BIO_snprintf(buf,sizeof buf,"BIO[%p]: ",(void *)bio);\n p = buf + len;\n p_maxlen = sizeof(buf) - len;\n switch (cmd) {\n case BIO_CB_FREE:\n BIO_snprintf(p, p_maxlen, "Free - %s\\n", bio->method->name);\n break;\n case BIO_CB_READ:\n if (bio->method->type & BIO_TYPE_DESCRIPTOR)\n BIO_snprintf(p, p_maxlen, "read(%d,%lu) - %s fd=%d\\n",\n bio->num, (unsigned long)argi,\n bio->method->name, bio->num);\n else\n BIO_snprintf(p, p_maxlen, "read(%d,%lu) - %s\\n",\n bio->num, (unsigned long)argi, bio->method->name);\n break;\n case BIO_CB_WRITE:\n if (bio->method->type & BIO_TYPE_DESCRIPTOR)\n BIO_snprintf(p, p_maxlen, "write(%d,%lu) - %s fd=%d\\n",\n bio->num, (unsigned long)argi,\n bio->method->name, bio->num);\n else\n BIO_snprintf(p, p_maxlen, "write(%d,%lu) - %s\\n",\n bio->num, (unsigned long)argi, bio->method->name);\n break;\n case BIO_CB_PUTS:\n BIO_snprintf(p, p_maxlen, "puts() - %s\\n", bio->method->name);\n break;\n case BIO_CB_GETS:\n BIO_snprintf(p, p_maxlen, "gets(%lu) - %s\\n", (unsigned long)argi,\n bio->method->name);\n break;\n case BIO_CB_CTRL:\n BIO_snprintf(p, p_maxlen, "ctrl(%lu) - %s\\n", (unsigned long)argi,\n bio->method->name);\n break;\n case BIO_CB_RETURN | BIO_CB_READ:\n BIO_snprintf(p, p_maxlen, "read return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_WRITE:\n BIO_snprintf(p, p_maxlen, "write return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_GETS:\n BIO_snprintf(p, p_maxlen, "gets return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_PUTS:\n BIO_snprintf(p, p_maxlen, "puts return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_CTRL:\n BIO_snprintf(p, p_maxlen, "ctrl return %ld\\n", ret);\n break;\n default:\n BIO_snprintf(p, p_maxlen, "bio callback - unknown type (%d)\\n", cmd);\n break;\n }\n b = (BIO *)bio->cb_arg;\n if (b != NULL)\n BIO_write(b, buf, strlen(buf));\n#if !defined(OPENSSL_NO_STDIO)\n else\n fputs(buf, stderr);\n#endif\n return (r);\n}', 'int BIO_snprintf(char *buf, size_t n, const char *format, ...)\n{\n va_list args;\n int ret;\n va_start(args, format);\n ret = BIO_vsnprintf(buf, n, format, args);\n va_end(args);\n return (ret);\n}', 'int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)\n{\n size_t retlen;\n int truncated;\n _dopr(&buf, NULL, &n, &retlen, &truncated, format, args);\n if (truncated)\n return -1;\n else\n return (retlen <= INT_MAX) ? (int)retlen : -1;\n}'] |
2,252 | 0 | https://github.com/nginx/nginx/blob/70f7141074896fb1ff3e5fc08407ea0f64f2076b/src/core/ngx_sha1.c/#L256 | static const u_char *
ngx_sha1_body(ngx_sha1_t *ctx, const u_char *data, size_t size)
{
uint32_t a, b, c, d, e, temp;
uint32_t saved_a, saved_b, saved_c, saved_d, saved_e;
uint32_t words[80];
ngx_uint_t i;
const u_char *p;
p = data;
a = ctx->a;
b = ctx->b;
c = ctx->c;
d = ctx->d;
e = ctx->e;
do {
saved_a = a;
saved_b = b;
saved_c = c;
saved_d = d;
saved_e = e;
for (i = 0; i < 16; i++) {
words[i] = GET(i);
}
for (i = 16; i < 80; i++) {
words[i] = ROTATE(1, words[i - 3] ^ words[i - 8] ^ words[i - 14]
^ words[i - 16]);
}
STEP(F1, a, b, c, d, e, words[0], 0x5a827999);
STEP(F1, a, b, c, d, e, words[1], 0x5a827999);
STEP(F1, a, b, c, d, e, words[2], 0x5a827999);
STEP(F1, a, b, c, d, e, words[3], 0x5a827999);
STEP(F1, a, b, c, d, e, words[4], 0x5a827999);
STEP(F1, a, b, c, d, e, words[5], 0x5a827999);
STEP(F1, a, b, c, d, e, words[6], 0x5a827999);
STEP(F1, a, b, c, d, e, words[7], 0x5a827999);
STEP(F1, a, b, c, d, e, words[8], 0x5a827999);
STEP(F1, a, b, c, d, e, words[9], 0x5a827999);
STEP(F1, a, b, c, d, e, words[10], 0x5a827999);
STEP(F1, a, b, c, d, e, words[11], 0x5a827999);
STEP(F1, a, b, c, d, e, words[12], 0x5a827999);
STEP(F1, a, b, c, d, e, words[13], 0x5a827999);
STEP(F1, a, b, c, d, e, words[14], 0x5a827999);
STEP(F1, a, b, c, d, e, words[15], 0x5a827999);
STEP(F1, a, b, c, d, e, words[16], 0x5a827999);
STEP(F1, a, b, c, d, e, words[17], 0x5a827999);
STEP(F1, a, b, c, d, e, words[18], 0x5a827999);
STEP(F1, a, b, c, d, e, words[19], 0x5a827999);
STEP(F2, a, b, c, d, e, words[20], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[21], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[22], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[23], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[24], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[25], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[26], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[27], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[28], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[29], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[30], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[31], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[32], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[33], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[34], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[35], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[36], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[37], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[38], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[39], 0x6ed9eba1);
STEP(F3, a, b, c, d, e, words[40], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[41], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[42], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[43], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[44], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[45], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[46], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[47], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[48], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[49], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[50], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[51], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[52], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[53], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[54], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[55], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[56], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[57], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[58], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[59], 0x8f1bbcdc);
STEP(F2, a, b, c, d, e, words[60], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[61], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[62], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[63], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[64], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[65], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[66], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[67], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[68], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[69], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[70], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[71], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[72], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[73], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[74], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[75], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[76], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[77], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[78], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[79], 0xca62c1d6);
a += saved_a;
b += saved_b;
c += saved_c;
d += saved_d;
e += saved_e;
p += 64;
} while (size -= 64);
ctx->a = a;
ctx->b = b;
ctx->c = c;
ctx->d = d;
ctx->e = e;
return p;
} | ['static const u_char *\nngx_sha1_body(ngx_sha1_t *ctx, const u_char *data, size_t size)\n{\n uint32_t a, b, c, d, e, temp;\n uint32_t saved_a, saved_b, saved_c, saved_d, saved_e;\n uint32_t words[80];\n ngx_uint_t i;\n const u_char *p;\n p = data;\n a = ctx->a;\n b = ctx->b;\n c = ctx->c;\n d = ctx->d;\n e = ctx->e;\n do {\n saved_a = a;\n saved_b = b;\n saved_c = c;\n saved_d = d;\n saved_e = e;\n for (i = 0; i < 16; i++) {\n words[i] = GET(i);\n }\n for (i = 16; i < 80; i++) {\n words[i] = ROTATE(1, words[i - 3] ^ words[i - 8] ^ words[i - 14]\n ^ words[i - 16]);\n }\n STEP(F1, a, b, c, d, e, words[0], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[1], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[2], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[3], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[4], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[5], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[6], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[7], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[8], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[9], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[10], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[11], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[12], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[13], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[14], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[15], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[16], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[17], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[18], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[19], 0x5a827999);\n STEP(F2, a, b, c, d, e, words[20], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[21], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[22], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[23], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[24], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[25], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[26], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[27], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[28], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[29], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[30], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[31], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[32], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[33], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[34], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[35], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[36], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[37], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[38], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[39], 0x6ed9eba1);\n STEP(F3, a, b, c, d, e, words[40], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[41], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[42], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[43], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[44], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[45], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[46], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[47], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[48], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[49], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[50], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[51], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[52], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[53], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[54], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[55], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[56], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[57], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[58], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[59], 0x8f1bbcdc);\n STEP(F2, a, b, c, d, e, words[60], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[61], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[62], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[63], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[64], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[65], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[66], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[67], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[68], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[69], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[70], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[71], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[72], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[73], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[74], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[75], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[76], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[77], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[78], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[79], 0xca62c1d6);\n a += saved_a;\n b += saved_b;\n c += saved_c;\n d += saved_d;\n e += saved_e;\n p += 64;\n } while (size -= 64);\n ctx->a = a;\n ctx->b = b;\n ctx->c = c;\n ctx->d = d;\n ctx->e = e;\n return p;\n}'] |
2,253 | 0 | https://github.com/openssl/openssl/blob/9f519addc09b2005fa8c6cde36e3267de02577bb/crypto/bn/bn_shift.c/#L158 | int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
r->neg = a->neg;
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return (0);
lb = n % BN_BITS2;
rb = BN_BITS2 - lb;
f = a->d;
t = r->d;
t[a->top + nw] = 0;
if (lb == 0)
for (i = a->top - 1; i >= 0; i--)
t[nw + i] = f[i];
else
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
t[nw + i + 1] |= (l >> rb) & BN_MASK2;
t[nw + i] = (l << lb) & BN_MASK2;
}
memset(t, 0, sizeof(*t) * nw);
r->top = a->top + nw + 1;
bn_correct_top(r);
bn_check_top(r);
return (1);
} | ['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 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(&r, &s, sig);\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 DSA_MOD_EXP(goto err, dsa, t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,\n mont);\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}', '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_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# 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 = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n r->neg = a->neg;\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
2,254 | 0 | https://github.com/openssl/openssl/blob/02cba628daa7fea959c561531a8a984756bdf41c/crypto/bn/bn_shift.c/#L112 | int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return (0);
r->neg = a->neg;
lb = n % BN_BITS2;
rb = BN_BITS2 - lb;
f = a->d;
t = r->d;
t[a->top + nw] = 0;
if (lb == 0)
for (i = a->top - 1; i >= 0; i--)
t[nw + i] = f[i];
else
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
t[nw + i + 1] |= (l >> rb) & BN_MASK2;
t[nw + i] = (l << lb) & BN_MASK2;
}
memset(t, 0, sizeof(*t) * nw);
r->top = a->top + nw + 1;
bn_correct_top(r);
bn_check_top(r);
return (1);
} | ['static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n BIGNUM *r1, *m1, *vrfy;\n int ret = 0;\n BN_CTX_start(ctx);\n r1 = BN_CTX_get(ctx);\n m1 = BN_CTX_get(ctx);\n vrfy = BN_CTX_get(ctx);\n {\n BIGNUM *p = BN_new(), *q = BN_new();\n if (p == NULL || q == NULL) {\n BN_free(p);\n BN_free(q);\n goto err;\n }\n BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);\n BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);\n if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {\n if (!BN_MONT_CTX_set_locked\n (&rsa->_method_mod_p, rsa->lock, p, ctx)\n || !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,\n rsa->lock, q, ctx)) {\n BN_free(p);\n BN_free(q);\n goto err;\n }\n }\n BN_free(p);\n BN_free(q);\n }\n if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n if (!BN_MONT_CTX_set_locked\n (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))\n goto err;\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 (!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 (rsa->e && rsa->n) {\n if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n if (!BN_sub(vrfy, vrfy, I))\n goto err;\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 ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)\n{\n dest->d = b->d;\n dest->top = b->top;\n dest->dmax = b->dmax;\n dest->neg = b->neg;\n dest->flags = ((dest->flags & BN_FLG_MALLOCED)\n | (b->flags & ~BN_FLG_MALLOCED)\n | BN_FLG_STATIC_DATA | flags);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
2,255 | 0 | https://github.com/openssl/openssl/blob/877e8e970c3c94c43ce1db50fdbb8e9b0342b90e/crypto/lhash/lhash.c/#L278 | static void doall_util_fn(LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
LHASH_DOALL_ARG_FN_TYPE func_arg, 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;
if(use_arg)
func_arg(a->data,arg);
else
func(a->data);
a=n;
}
}
} | ['void ssl_update_cache(SSL *s,int mode)\n\t{\n\tint i;\n\tif (s->session->session_id_length == 0) return;\n\ti=s->ctx->session_cache_mode;\n\tif ((i & mode) && (!s->hit)\n\t\t&& ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)\n\t\t || SSL_CTX_add_session(s->ctx,s->session))\n\t\t&& (s->ctx->new_session_cb != NULL))\n\t\t{\n\t\tCRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);\n\t\tif (!s->ctx->new_session_cb(s,s->session))\n\t\t\tSSL_SESSION_free(s->session);\n\t\t}\n\tif ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&\n\t\t((i & mode) == mode))\n\t\t{\n\t\tif ( (((mode & SSL_SESS_CACHE_CLIENT)\n\t\t\t?s->ctx->stats.sess_connect_good\n\t\t\t:s->ctx->stats.sess_accept_good) & 0xff) == 0xff)\n\t\t\t{\n\t\t\tSSL_CTX_flush_sessions(s->ctx,(unsigned long)time(NULL));\n\t\t\t}\n\t\t}\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, LHASH_DOALL_ARG_FN(timeout), &tp);\n\ttp.cache->down_load=i;\n\tCRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t}', 'void lh_doall_arg(LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)\n\t{\n\tdoall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);\n\t}', 'static void doall_util_fn(LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,\n\t\t\t LHASH_DOALL_ARG_FN_TYPE func_arg, 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\tif(use_arg)\n\t\t\t\tfunc_arg(a->data,arg);\n\t\t\telse\n\t\t\t\tfunc(a->data);\n\t\t\ta=n;\n\t\t\t}\n\t\t}\n\t}'] |
2,256 | 0 | https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/asn1/asn1_lib.c/#L218 | static void asn1_put_length(unsigned char **pp, int length)
{
unsigned char *p= *pp;
int i,l;
if (length <= 127)
*(p++)=(unsigned char)length;
else
{
l=length;
for (i=0; l > 0; i++)
l>>=8;
*(p++)=i|0x80;
l=i;
while (i-- > 0)
{
p[i]=length&0xff;
length>>=8;
}
p+=l;
}
*pp=p;
} | ['static int request_certificate(SSL *s)\n\t{\n\tunsigned char *p,*p2,*buf2;\n\tunsigned char *ccd;\n\tint i,j,ctype,ret= -1;\n\tX509 *x509=NULL;\n\tSTACK_OF(X509) *sk=NULL;\n\tccd=s->s2->tmp.ccl;\n\tif (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A)\n\t\t{\n\t\tp=(unsigned char *)s->init_buf->data;\n\t\t*(p++)=SSL2_MT_REQUEST_CERTIFICATE;\n\t\t*(p++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION;\n\t\tRAND_bytes(ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);\n\t\tmemcpy(p,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);\n\t\ts->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_B;\n\t\ts->init_num=SSL2_MIN_CERT_CHALLENGE_LENGTH+2;\n\t\ts->init_off=0;\n\t\t}\n\tif (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B)\n\t\t{\n\t\ti=ssl2_do_write(s);\n\t\tif (i <= 0)\n\t\t\t{\n\t\t\tret=i;\n\t\t\tgoto end;\n\t\t\t}\n\t\ts->init_num=0;\n\t\ts->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_C;\n\t\t}\n\tif (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C)\n\t\t{\n\t\tp=(unsigned char *)s->init_buf->data;\n\t\ti=ssl2_read(s,(char *)&(p[s->init_num]),6-s->init_num);\n\t\tif (i < 3)\n\t\t\t{\n\t\t\tret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);\n\t\t\tgoto end;\n\t\t\t}\n\t\tif ((*p == SSL2_MT_ERROR) && (i >= 3))\n\t\t\t{\n\t\t\tn2s(p,i);\n\t\t\tif (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)\n\t\t\t\t{\n\t\t\t\tssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);\n\t\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tret=1;\n\t\t\tgoto end;\n\t\t\t}\n\t\tif ((*(p++) != SSL2_MT_CLIENT_CERTIFICATE) || (i < 6))\n\t\t\t{\n\t\t\tssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);\n\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_SHORT_READ);\n\t\t\tgoto end;\n\t\t\t}\n\t\tctype= *(p++);\n\t\tif (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)\n\t\t\t{\n\t\t\tssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);\n\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_RESPONSE_ARGUMENT);\n\t\t\tgoto end;\n\t\t\t}\n\t\tn2s(p,i); s->s2->tmp.clen=i;\n\t\tn2s(p,i); s->s2->tmp.rlen=i;\n\t\ts->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_D;\n\t\ts->init_num=0;\n\t\t}\n\tp=(unsigned char *)s->init_buf->data;\n\tj=s->s2->tmp.clen+s->s2->tmp.rlen-s->init_num;\n\ti=ssl2_read(s,(char *)&(p[s->init_num]),j);\n\tif (i < j)\n\t\t{\n\t\tret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);\n\t\tgoto end;\n\t\t}\n\tx509=(X509 *)d2i_X509(NULL,&p,(long)s->s2->tmp.clen);\n\tif (x509 == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_X509_LIB);\n\t\tgoto msg_end;\n\t\t}\n\tif (((sk=sk_X509_new_null()) == NULL) || (!sk_X509_push(sk,x509)))\n\t\t{\n\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);\n\t\tgoto msg_end;\n\t\t}\n\ti=ssl_verify_cert_chain(s,sk);\n\tif (i)\n\t\t{\n\t\tEVP_MD_CTX ctx;\n\t\tEVP_PKEY *pkey=NULL;\n\t\tEVP_VerifyInit(&ctx,s->ctx->rsa_md5);\n\t\tEVP_VerifyUpdate(&ctx,s->s2->key_material,\n\t\t\t(unsigned int)s->s2->key_material_length);\n\t\tEVP_VerifyUpdate(&ctx,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);\n\t\ti=i2d_X509(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);\n\t\tbuf2=(unsigned char *)Malloc((unsigned int)i);\n\t\tif (buf2 == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto msg_end;\n\t\t\t}\n\t\tp2=buf2;\n\t\ti=i2d_X509(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2);\n\t\tEVP_VerifyUpdate(&ctx,buf2,(unsigned int)i);\n\t\tFree(buf2);\n\t\tpkey=X509_get_pubkey(x509);\n\t\tif (pkey == NULL) goto end;\n\t\ti=EVP_VerifyFinal(&ctx,p,s->s2->tmp.rlen,pkey);\n\t\tEVP_PKEY_free(pkey);\n\t\tmemset(&ctx,0,sizeof(ctx));\n\t\tif (i)\n\t\t\t{\n\t\t\tif (s->session->peer != NULL)\n\t\t\t\tX509_free(s->session->peer);\n\t\t\ts->session->peer=x509;\n\t\t\tCRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);\n\t\t\tret=1;\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_CHECKSUM);\n\t\t\tgoto msg_end;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\nmsg_end:\n\t\tssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);\n\t\t}\nend:\n\tsk_X509_free(sk);\n\tX509_free(x509);\n\treturn(ret);\n\t}', 'int i2d_X509(X509 *a, unsigned char **pp)\n\t{\n\tM_ASN1_I2D_vars(a);\n\tM_ASN1_I2D_len(a->cert_info,\ti2d_X509_CINF);\n\tM_ASN1_I2D_len(a->sig_alg,\ti2d_X509_ALGOR);\n\tM_ASN1_I2D_len(a->signature,\ti2d_ASN1_BIT_STRING);\n\tM_ASN1_I2D_seq_total();\n\tM_ASN1_I2D_put(a->cert_info,\ti2d_X509_CINF);\n\tM_ASN1_I2D_put(a->sig_alg,\ti2d_X509_ALGOR);\n\tM_ASN1_I2D_put(a->signature,\ti2d_ASN1_BIT_STRING);\n\tM_ASN1_I2D_finish();\n\t}', 'void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,\n\t int xclass)\n\t{\n\tunsigned char *p= *pp;\n\tint i;\n\ti=(constructed)?V_ASN1_CONSTRUCTED:0;\n\ti|=(xclass&V_ASN1_PRIVATE);\n\tif (tag < 31)\n\t\t*(p++)=i|(tag&V_ASN1_PRIMATIVE_TAG);\n\telse\n\t\t{\n\t\t*(p++)=i|V_ASN1_PRIMATIVE_TAG;\n\t\twhile (tag > 0x7f)\n\t\t\t{\n\t\t\t*(p++)=(tag&0x7f)|0x80;\n\t\t\ttag>>=7;\n\t\t\t}\n\t\t*(p++)=(tag&0x7f);\n\t\t}\n\tif ((constructed == 2) && (length == 0))\n\t\t*(p++)=0x80;\n\telse\n\t\tasn1_put_length(&p,length);\n\t*pp=p;\n\t}', 'int i2d_X509_CINF(X509_CINF *a, unsigned char **pp)\n\t{\n\tint v1=0,v2=0;\n\tM_ASN1_I2D_vars(a);\n\tM_ASN1_I2D_len_EXP_opt(a->version,i2d_ASN1_INTEGER,0,v1);\n\tM_ASN1_I2D_len(a->serialNumber,\t\ti2d_ASN1_INTEGER);\n\tM_ASN1_I2D_len(a->signature,\t\ti2d_X509_ALGOR);\n\tM_ASN1_I2D_len(a->issuer,\t\ti2d_X509_NAME);\n\tM_ASN1_I2D_len(a->validity,\t\ti2d_X509_VAL);\n\tM_ASN1_I2D_len(a->subject,\t\ti2d_X509_NAME);\n\tM_ASN1_I2D_len(a->key,\t\t\ti2d_X509_PUBKEY);\n\tM_ASN1_I2D_len_IMP_opt(a->issuerUID,\ti2d_ASN1_BIT_STRING);\n\tM_ASN1_I2D_len_IMP_opt(a->subjectUID,\ti2d_ASN1_BIT_STRING);\n\tM_ASN1_I2D_len_EXP_SEQUENCE_opt(a->extensions,i2d_X509_EXTENSION,3,V_ASN1_SEQUENCE,v2);\n\tM_ASN1_I2D_seq_total();\n\tM_ASN1_I2D_put_EXP_opt(a->version,i2d_ASN1_INTEGER,0,v1);\n\tM_ASN1_I2D_put(a->serialNumber,\t\ti2d_ASN1_INTEGER);\n\tM_ASN1_I2D_put(a->signature,\t\ti2d_X509_ALGOR);\n\tM_ASN1_I2D_put(a->issuer,\t\ti2d_X509_NAME);\n\tM_ASN1_I2D_put(a->validity,\t\ti2d_X509_VAL);\n\tM_ASN1_I2D_put(a->subject,\t\ti2d_X509_NAME);\n\tM_ASN1_I2D_put(a->key,\t\t\ti2d_X509_PUBKEY);\n\tM_ASN1_I2D_put_IMP_opt(a->issuerUID,\ti2d_ASN1_BIT_STRING,1);\n\tM_ASN1_I2D_put_IMP_opt(a->subjectUID,\ti2d_ASN1_BIT_STRING,2);\n\tM_ASN1_I2D_put_EXP_SEQUENCE_opt(a->extensions,i2d_X509_EXTENSION,3,V_ASN1_SEQUENCE,v2);\n\tM_ASN1_I2D_finish();\n\t}', 'int i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)\n\t{\n\tint pad=0,ret,r,i,t;\n\tunsigned char *p,*pt,*n,pb=0;\n\tif ((a == NULL) || (a->data == NULL)) return(0);\n\tt=a->type;\n\tif (a->length == 0)\n\t\tret=1;\n\telse\n\t\t{\n\t\tret=a->length;\n\t\ti=a->data[0];\n\t\tif ((t == V_ASN1_INTEGER) && (i > 127))\n\t\t\t{\n\t\t\tpad=1;\n\t\t\tpb=0;\n\t\t\t}\n\t\telse if ((t == V_ASN1_NEG_INTEGER) && (i>128))\n\t\t\t{\n\t\t\tpad=1;\n\t\t\tpb=0xFF;\n\t\t\t}\n\t\tret+=pad;\n\t\t}\n\tr=ASN1_object_size(0,ret,V_ASN1_INTEGER);\n\tif (pp == NULL) return(r);\n\tp= *pp;\n\tASN1_put_object(&p,0,ret,V_ASN1_INTEGER,V_ASN1_UNIVERSAL);\n\tif (pad) *(p++)=pb;\n\tif (a->length == 0)\n\t\t*(p++)=0;\n\telse if (t == V_ASN1_INTEGER)\n\t\t{\n\t\tmemcpy(p,a->data,(unsigned int)a->length);\n\t\tp+=a->length;\n\t\t}\n\telse\n\t\t{\n\t\tn=a->data;\n\t\tpt=p;\n\t\tfor (i=a->length; i>0; i--)\n\t\t\t*(p++)= (*(n++)^0xFF)+1;\n\t\tif (!pad) *pt|=0x80;\n\t\t}\n\t*pp=p;\n\treturn(r);\n\t}', 'static void asn1_put_length(unsigned char **pp, int length)\n\t{\n\tunsigned char *p= *pp;\n\tint i,l;\n\tif (length <= 127)\n\t\t*(p++)=(unsigned char)length;\n\telse\n\t\t{\n\t\tl=length;\n\t\tfor (i=0; l > 0; i++)\n\t\t\tl>>=8;\n\t\t*(p++)=i|0x80;\n\t\tl=i;\n\t\twhile (i-- > 0)\n\t\t\t{\n\t\t\tp[i]=length&0xff;\n\t\t\tlength>>=8;\n\t\t\t}\n\t\tp+=l;\n\t\t}\n\t*pp=p;\n\t}'] |
2,257 | 0 | https://github.com/libav/libav/blob/cf6bae6883607f83f3b042b7b9d711197f736e2a/libavcodec/mpegaudiodec.c/#L720 | static void dct32(int32_t *out, int32_t *tab)
{
int tmp0, tmp1;
BF( 0, 31, COS0_0 , 1);
BF(15, 16, COS0_15, 5);
BF( 0, 15, COS1_0 , 1);
BF(16, 31,-COS1_0 , 1);
BF( 7, 24, COS0_7 , 1);
BF( 8, 23, COS0_8 , 1);
BF( 7, 8, COS1_7 , 4);
BF(23, 24,-COS1_7 , 4);
BF( 0, 7, COS2_0 , 1);
BF( 8, 15,-COS2_0 , 1);
BF(16, 23, COS2_0 , 1);
BF(24, 31,-COS2_0 , 1);
BF( 3, 28, COS0_3 , 1);
BF(12, 19, COS0_12, 2);
BF( 3, 12, COS1_3 , 1);
BF(19, 28,-COS1_3 , 1);
BF( 4, 27, COS0_4 , 1);
BF(11, 20, COS0_11, 2);
BF( 4, 11, COS1_4 , 1);
BF(20, 27,-COS1_4 , 1);
BF( 3, 4, COS2_3 , 3);
BF(11, 12,-COS2_3 , 3);
BF(19, 20, COS2_3 , 3);
BF(27, 28,-COS2_3 , 3);
BF( 0, 3, COS3_0 , 1);
BF( 4, 7,-COS3_0 , 1);
BF( 8, 11, COS3_0 , 1);
BF(12, 15,-COS3_0 , 1);
BF(16, 19, COS3_0 , 1);
BF(20, 23,-COS3_0 , 1);
BF(24, 27, COS3_0 , 1);
BF(28, 31,-COS3_0 , 1);
BF( 1, 30, COS0_1 , 1);
BF(14, 17, COS0_14, 3);
BF( 1, 14, COS1_1 , 1);
BF(17, 30,-COS1_1 , 1);
BF( 6, 25, COS0_6 , 1);
BF( 9, 22, COS0_9 , 1);
BF( 6, 9, COS1_6 , 2);
BF(22, 25,-COS1_6 , 2);
BF( 1, 6, COS2_1 , 1);
BF( 9, 14,-COS2_1 , 1);
BF(17, 22, COS2_1 , 1);
BF(25, 30,-COS2_1 , 1);
BF( 2, 29, COS0_2 , 1);
BF(13, 18, COS0_13, 3);
BF( 2, 13, COS1_2 , 1);
BF(18, 29,-COS1_2 , 1);
BF( 5, 26, COS0_5 , 1);
BF(10, 21, COS0_10, 1);
BF( 5, 10, COS1_5 , 2);
BF(21, 26,-COS1_5 , 2);
BF( 2, 5, COS2_2 , 1);
BF(10, 13,-COS2_2 , 1);
BF(18, 21, COS2_2 , 1);
BF(26, 29,-COS2_2 , 1);
BF( 1, 2, COS3_1 , 2);
BF( 5, 6,-COS3_1 , 2);
BF( 9, 10, COS3_1 , 2);
BF(13, 14,-COS3_1 , 2);
BF(17, 18, COS3_1 , 2);
BF(21, 22,-COS3_1 , 2);
BF(25, 26, COS3_1 , 2);
BF(29, 30,-COS3_1 , 2);
BF1( 0, 1, 2, 3);
BF2( 4, 5, 6, 7);
BF1( 8, 9, 10, 11);
BF2(12, 13, 14, 15);
BF1(16, 17, 18, 19);
BF2(20, 21, 22, 23);
BF1(24, 25, 26, 27);
BF2(28, 29, 30, 31);
ADD( 8, 12);
ADD(12, 10);
ADD(10, 14);
ADD(14, 9);
ADD( 9, 13);
ADD(13, 11);
ADD(11, 15);
out[ 0] = tab[0];
out[16] = tab[1];
out[ 8] = tab[2];
out[24] = tab[3];
out[ 4] = tab[4];
out[20] = tab[5];
out[12] = tab[6];
out[28] = tab[7];
out[ 2] = tab[8];
out[18] = tab[9];
out[10] = tab[10];
out[26] = tab[11];
out[ 6] = tab[12];
out[22] = tab[13];
out[14] = tab[14];
out[30] = tab[15];
ADD(24, 28);
ADD(28, 26);
ADD(26, 30);
ADD(30, 25);
ADD(25, 29);
ADD(29, 27);
ADD(27, 31);
out[ 1] = tab[16] + tab[24];
out[17] = tab[17] + tab[25];
out[ 9] = tab[18] + tab[26];
out[25] = tab[19] + tab[27];
out[ 5] = tab[20] + tab[28];
out[21] = tab[21] + tab[29];
out[13] = tab[22] + tab[30];
out[29] = tab[23] + tab[31];
out[ 3] = tab[24] + tab[20];
out[19] = tab[25] + tab[21];
out[11] = tab[26] + tab[22];
out[27] = tab[27] + tab[23];
out[ 7] = tab[28] + tab[18];
out[23] = tab[29] + tab[19];
out[15] = tab[30] + tab[17];
out[31] = tab[31];
} | ['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 mpa_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 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 int32_t 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 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\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(MPA_INT));\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}', 'static void dct32(int32_t *out, int32_t *tab)\n{\n int tmp0, tmp1;\n BF( 0, 31, COS0_0 , 1);\n BF(15, 16, COS0_15, 5);\n BF( 0, 15, COS1_0 , 1);\n BF(16, 31,-COS1_0 , 1);\n BF( 7, 24, COS0_7 , 1);\n BF( 8, 23, COS0_8 , 1);\n BF( 7, 8, COS1_7 , 4);\n BF(23, 24,-COS1_7 , 4);\n BF( 0, 7, COS2_0 , 1);\n BF( 8, 15,-COS2_0 , 1);\n BF(16, 23, COS2_0 , 1);\n BF(24, 31,-COS2_0 , 1);\n BF( 3, 28, COS0_3 , 1);\n BF(12, 19, COS0_12, 2);\n BF( 3, 12, COS1_3 , 1);\n BF(19, 28,-COS1_3 , 1);\n BF( 4, 27, COS0_4 , 1);\n BF(11, 20, COS0_11, 2);\n BF( 4, 11, COS1_4 , 1);\n BF(20, 27,-COS1_4 , 1);\n BF( 3, 4, COS2_3 , 3);\n BF(11, 12,-COS2_3 , 3);\n BF(19, 20, COS2_3 , 3);\n BF(27, 28,-COS2_3 , 3);\n BF( 0, 3, COS3_0 , 1);\n BF( 4, 7,-COS3_0 , 1);\n BF( 8, 11, COS3_0 , 1);\n BF(12, 15,-COS3_0 , 1);\n BF(16, 19, COS3_0 , 1);\n BF(20, 23,-COS3_0 , 1);\n BF(24, 27, COS3_0 , 1);\n BF(28, 31,-COS3_0 , 1);\n BF( 1, 30, COS0_1 , 1);\n BF(14, 17, COS0_14, 3);\n BF( 1, 14, COS1_1 , 1);\n BF(17, 30,-COS1_1 , 1);\n BF( 6, 25, COS0_6 , 1);\n BF( 9, 22, COS0_9 , 1);\n BF( 6, 9, COS1_6 , 2);\n BF(22, 25,-COS1_6 , 2);\n BF( 1, 6, COS2_1 , 1);\n BF( 9, 14,-COS2_1 , 1);\n BF(17, 22, COS2_1 , 1);\n BF(25, 30,-COS2_1 , 1);\n BF( 2, 29, COS0_2 , 1);\n BF(13, 18, COS0_13, 3);\n BF( 2, 13, COS1_2 , 1);\n BF(18, 29,-COS1_2 , 1);\n BF( 5, 26, COS0_5 , 1);\n BF(10, 21, COS0_10, 1);\n BF( 5, 10, COS1_5 , 2);\n BF(21, 26,-COS1_5 , 2);\n BF( 2, 5, COS2_2 , 1);\n BF(10, 13,-COS2_2 , 1);\n BF(18, 21, COS2_2 , 1);\n BF(26, 29,-COS2_2 , 1);\n BF( 1, 2, COS3_1 , 2);\n BF( 5, 6,-COS3_1 , 2);\n BF( 9, 10, COS3_1 , 2);\n BF(13, 14,-COS3_1 , 2);\n BF(17, 18, COS3_1 , 2);\n BF(21, 22,-COS3_1 , 2);\n BF(25, 26, COS3_1 , 2);\n BF(29, 30,-COS3_1 , 2);\n BF1( 0, 1, 2, 3);\n BF2( 4, 5, 6, 7);\n BF1( 8, 9, 10, 11);\n BF2(12, 13, 14, 15);\n BF1(16, 17, 18, 19);\n BF2(20, 21, 22, 23);\n BF1(24, 25, 26, 27);\n BF2(28, 29, 30, 31);\n ADD( 8, 12);\n ADD(12, 10);\n ADD(10, 14);\n ADD(14, 9);\n ADD( 9, 13);\n ADD(13, 11);\n ADD(11, 15);\n out[ 0] = tab[0];\n out[16] = tab[1];\n out[ 8] = tab[2];\n out[24] = tab[3];\n out[ 4] = tab[4];\n out[20] = tab[5];\n out[12] = tab[6];\n out[28] = tab[7];\n out[ 2] = tab[8];\n out[18] = tab[9];\n out[10] = tab[10];\n out[26] = tab[11];\n out[ 6] = tab[12];\n out[22] = tab[13];\n out[14] = tab[14];\n out[30] = tab[15];\n ADD(24, 28);\n ADD(28, 26);\n ADD(26, 30);\n ADD(30, 25);\n ADD(25, 29);\n ADD(29, 27);\n ADD(27, 31);\n out[ 1] = tab[16] + tab[24];\n out[17] = tab[17] + tab[25];\n out[ 9] = tab[18] + tab[26];\n out[25] = tab[19] + tab[27];\n out[ 5] = tab[20] + tab[28];\n out[21] = tab[21] + tab[29];\n out[13] = tab[22] + tab[30];\n out[29] = tab[23] + tab[31];\n out[ 3] = tab[24] + tab[20];\n out[19] = tab[25] + tab[21];\n out[11] = tab[26] + tab[22];\n out[27] = tab[27] + tab[23];\n out[ 7] = tab[28] + tab[18];\n out[23] = tab[29] + tab[19];\n out[15] = tab[30] + tab[17];\n out[31] = tab[31];\n}'] |
2,258 | 0 | https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_ctx.c/#L273 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x_, int y_bit,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp1, *tmp2, *x, *y;\n int ret = 0;\n ERR_clear_error();\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n y_bit = (y_bit != 0);\n BN_CTX_start(ctx);\n tmp1 = BN_CTX_get(ctx);\n tmp2 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_nnmod(x, x_, group->field, ctx))\n goto err;\n if (group->meth->field_decode == 0) {\n if (!group->meth->field_sqr(group, tmp2, x_, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))\n goto err;\n } else {\n if (!BN_mod_sqr(tmp2, x_, group->field, ctx))\n goto err;\n if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))\n goto err;\n }\n if (group->a_is_minus3) {\n if (!BN_mod_lshift1_quick(tmp2, x, group->field))\n goto err;\n if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))\n goto err;\n if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->a, ctx))\n goto err;\n if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))\n goto err;\n } else {\n if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))\n goto err;\n }\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n }\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->b, ctx))\n goto err;\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))\n goto err;\n }\n if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {\n unsigned long err = ERR_peek_last_error();\n if (ERR_GET_LIB(err) == ERR_LIB_BN\n && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {\n ERR_clear_error();\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n } else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_BN_LIB);\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n if (BN_is_zero(y)) {\n int kron;\n kron = BN_kronecker(x, group->field, ctx);\n if (kron == -2)\n goto err;\n if (kron == 1)\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSION_BIT);\n else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n goto err;\n }\n if (!BN_usub(y, group->field, y))\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', '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_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)\n{\n if (!BN_sqr(r, a, ctx))\n return 0;\n return BN_mod(r, r, m, ctx);\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_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}'] |
2,259 | 0 | https://github.com/libav/libav/blob/452a398fd6bdca3f301c5c8af3bc241bc16a777e/libavcodec/adpcm.c/#L202 | static int adpcm_encode_init(AVCodecContext *avctx)
{
if (avctx->channels > 2)
return -1;
if(avctx->trellis && (unsigned)avctx->trellis > 16U){
av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
return -1;
}
switch(avctx->codec->id) {
case CODEC_ID_ADPCM_IMA_WAV:
avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1;
avctx->block_align = BLKSIZE;
break;
case CODEC_ID_ADPCM_IMA_QT:
avctx->frame_size = 64;
avctx->block_align = 34 * avctx->channels;
break;
case CODEC_ID_ADPCM_MS:
avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2;
avctx->block_align = BLKSIZE;
break;
case CODEC_ID_ADPCM_YAMAHA:
avctx->frame_size = BLKSIZE * avctx->channels;
avctx->block_align = BLKSIZE;
break;
case CODEC_ID_ADPCM_SWF:
if (avctx->sample_rate != 11025 &&
avctx->sample_rate != 22050 &&
avctx->sample_rate != 44100) {
av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
return -1;
}
avctx->frame_size = 512 * (avctx->sample_rate / 11025);
break;
default:
return -1;
break;
}
avctx->coded_frame= avcodec_alloc_frame();
avctx->coded_frame->key_frame= 1;
return 0;
} | ['static int adpcm_encode_init(AVCodecContext *avctx)\n{\n if (avctx->channels > 2)\n return -1;\n if(avctx->trellis && (unsigned)avctx->trellis > 16U){\n av_log(avctx, AV_LOG_ERROR, "invalid trellis size\\n");\n return -1;\n }\n switch(avctx->codec->id) {\n case CODEC_ID_ADPCM_IMA_WAV:\n avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1;\n avctx->block_align = BLKSIZE;\n break;\n case CODEC_ID_ADPCM_IMA_QT:\n avctx->frame_size = 64;\n avctx->block_align = 34 * avctx->channels;\n break;\n case CODEC_ID_ADPCM_MS:\n avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2;\n avctx->block_align = BLKSIZE;\n break;\n case CODEC_ID_ADPCM_YAMAHA:\n avctx->frame_size = BLKSIZE * avctx->channels;\n avctx->block_align = BLKSIZE;\n break;\n case CODEC_ID_ADPCM_SWF:\n if (avctx->sample_rate != 11025 &&\n avctx->sample_rate != 22050 &&\n avctx->sample_rate != 44100) {\n av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\\n");\n return -1;\n }\n avctx->frame_size = 512 * (avctx->sample_rate / 11025);\n break;\n default:\n return -1;\n break;\n }\n avctx->coded_frame= avcodec_alloc_frame();\n avctx->coded_frame->key_frame= 1;\n return 0;\n}', 'AVFrame *avcodec_alloc_frame(void){\n AVFrame *pic= av_malloc(sizeof(AVFrame));\n if(pic==NULL) return NULL;\n avcodec_get_frame_defaults(pic);\n return pic;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr;\n#ifdef CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#ifdef CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif defined (HAVE_MEMALIGN)\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}'] |
2,260 | 0 | https://github.com/openssl/openssl/blob/55442b8a5b719f54578083fae0fcc814b599cd84/crypto/bn/bn_lib.c/#L363 | int BN_set_word(BIGNUM *a, BN_ULONG w)
{
bn_check_top(a);
if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
return 0;
a->neg = 0;
a->d[0] = w;
a->top = (w ? 1 : 0);
bn_check_top(a);
return 1;
} | ['int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,\n const EC_POINT *b, BN_CTX *ctx)\n{\n int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,\n const BIGNUM *, BN_CTX *);\n int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);\n const BIGNUM *p;\n BN_CTX *new_ctx = NULL;\n BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;\n int ret = 0;\n if (a == b)\n return EC_POINT_dbl(group, r, a, ctx);\n if (EC_POINT_is_at_infinity(group, a))\n return EC_POINT_copy(r, b);\n if (EC_POINT_is_at_infinity(group, b))\n return EC_POINT_copy(r, a);\n field_mul = group->meth->field_mul;\n field_sqr = group->meth->field_sqr;\n p = group->field;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n n0 = BN_CTX_get(ctx);\n n1 = BN_CTX_get(ctx);\n n2 = BN_CTX_get(ctx);\n n3 = BN_CTX_get(ctx);\n n4 = BN_CTX_get(ctx);\n n5 = BN_CTX_get(ctx);\n n6 = BN_CTX_get(ctx);\n if (n6 == NULL)\n goto end;\n if (b->Z_is_one) {\n if (!BN_copy(n1, a->X))\n goto end;\n if (!BN_copy(n2, a->Y))\n goto end;\n } else {\n if (!field_sqr(group, n0, b->Z, ctx))\n goto end;\n if (!field_mul(group, n1, a->X, n0, ctx))\n goto end;\n if (!field_mul(group, n0, n0, b->Z, ctx))\n goto end;\n if (!field_mul(group, n2, a->Y, n0, ctx))\n goto end;\n }\n if (a->Z_is_one) {\n if (!BN_copy(n3, b->X))\n goto end;\n if (!BN_copy(n4, b->Y))\n goto end;\n } else {\n if (!field_sqr(group, n0, a->Z, ctx))\n goto end;\n if (!field_mul(group, n3, b->X, n0, ctx))\n goto end;\n if (!field_mul(group, n0, n0, a->Z, ctx))\n goto end;\n if (!field_mul(group, n4, b->Y, n0, ctx))\n goto end;\n }\n if (!BN_mod_sub_quick(n5, n1, n3, p))\n goto end;\n if (!BN_mod_sub_quick(n6, n2, n4, p))\n goto end;\n if (BN_is_zero(n5)) {\n if (BN_is_zero(n6)) {\n BN_CTX_end(ctx);\n ret = EC_POINT_dbl(group, r, a, ctx);\n ctx = NULL;\n goto end;\n } else {\n BN_zero(r->Z);\n r->Z_is_one = 0;\n ret = 1;\n goto end;\n }\n }\n if (!BN_mod_add_quick(n1, n1, n3, p))\n goto end;\n if (!BN_mod_add_quick(n2, n2, n4, p))\n goto end;\n if (a->Z_is_one && b->Z_is_one) {\n if (!BN_copy(r->Z, n5))\n goto end;\n } else {\n if (a->Z_is_one) {\n if (!BN_copy(n0, b->Z))\n goto end;\n } else if (b->Z_is_one) {\n if (!BN_copy(n0, a->Z))\n goto end;\n } else {\n if (!field_mul(group, n0, a->Z, b->Z, ctx))\n goto end;\n }\n if (!field_mul(group, r->Z, n0, n5, ctx))\n goto end;\n }\n r->Z_is_one = 0;\n if (!field_sqr(group, n0, n6, ctx))\n goto end;\n if (!field_sqr(group, n4, n5, ctx))\n goto end;\n if (!field_mul(group, n3, n1, n4, ctx))\n goto end;\n if (!BN_mod_sub_quick(r->X, n0, n3, p))\n goto end;\n if (!BN_mod_lshift1_quick(n0, r->X, p))\n goto end;\n if (!BN_mod_sub_quick(n0, n3, n0, p))\n goto end;\n if (!field_mul(group, n0, n0, n6, ctx))\n goto end;\n if (!field_mul(group, n5, n4, n5, ctx))\n goto end;\n if (!field_mul(group, n1, n2, n5, ctx))\n goto end;\n if (!BN_mod_sub_quick(n0, n0, n1, p))\n goto end;\n if (BN_is_odd(n0))\n if (!BN_add(n0, n0, p))\n goto end;\n if (!BN_rshift1(r->Y, n0))\n goto end;\n ret = 1;\n end:\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const BIGNUM *m)\n{\n if (!BN_sub(r, a, b))\n return 0;\n if (r->neg)\n return BN_add(r, r, m);\n return 1;\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}'] |
2,261 | 0 | https://github.com/openssl/openssl/blob/a44a208442ecf8f576c9e364f8b46b6661c7d2de/crypto/cms/cms_pwri.c/#L337 | int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
int en_de)
{
CMS_EncryptedContentInfo *ec;
CMS_PasswordRecipientInfo *pwri;
int r = 0;
X509_ALGOR *algtmp, *kekalg = NULL;
EVP_CIPHER_CTX *kekctx;
const EVP_CIPHER *kekcipher;
unsigned char *key = NULL;
size_t keylen;
ec = cms->d.envelopedData->encryptedContentInfo;
pwri = ri->d.pwri;
kekctx = EVP_CIPHER_CTX_new();
if (!pwri->pass) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_NO_PASSWORD);
return 0;
}
algtmp = pwri->keyEncryptionAlgorithm;
if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
return 0;
}
kekalg = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
algtmp->parameter);
if (kekalg == NULL) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);
return 0;
}
kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
if (!kekcipher) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNKNOWN_CIPHER);
goto err;
}
if (!EVP_CipherInit_ex(kekctx, kekcipher, NULL, NULL, NULL, en_de))
goto err;
EVP_CIPHER_CTX_set_padding(kekctx, 0);
if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) < 0) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
goto err;
}
algtmp = pwri->keyDerivationAlgorithm;
if (EVP_PBE_CipherInit(algtmp->algorithm,
(char *)pwri->pass, pwri->passlen,
algtmp->parameter, kekctx, en_de) < 0) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_EVP_LIB);
goto err;
}
if (en_de) {
if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, kekctx))
goto err;
key = OPENSSL_malloc(keylen);
if (key == NULL)
goto err;
if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, kekctx))
goto err;
pwri->encryptedKey->data = key;
pwri->encryptedKey->length = keylen;
} else {
key = OPENSSL_malloc(pwri->encryptedKey->length);
if (key == NULL) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);
goto err;
}
if (!kek_unwrap_key(key, &keylen,
pwri->encryptedKey->data,
pwri->encryptedKey->length, kekctx)) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNWRAP_FAILURE);
goto err;
}
ec->key = key;
ec->keylen = keylen;
}
r = 1;
err:
EVP_CIPHER_CTX_free(kekctx);
if (!r)
OPENSSL_free(key);
X509_ALGOR_free(kekalg);
return r;
} | ['int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,\n int en_de)\n{\n CMS_EncryptedContentInfo *ec;\n CMS_PasswordRecipientInfo *pwri;\n int r = 0;\n X509_ALGOR *algtmp, *kekalg = NULL;\n EVP_CIPHER_CTX *kekctx;\n const EVP_CIPHER *kekcipher;\n unsigned char *key = NULL;\n size_t keylen;\n ec = cms->d.envelopedData->encryptedContentInfo;\n pwri = ri->d.pwri;\n kekctx = EVP_CIPHER_CTX_new();\n if (!pwri->pass) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_NO_PASSWORD);\n return 0;\n }\n algtmp = pwri->keyEncryptionAlgorithm;\n if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,\n CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);\n return 0;\n }\n kekalg = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),\n algtmp->parameter);\n if (kekalg == NULL) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,\n CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);\n return 0;\n }\n kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);\n if (!kekcipher) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNKNOWN_CIPHER);\n goto err;\n }\n if (!EVP_CipherInit_ex(kekctx, kekcipher, NULL, NULL, NULL, en_de))\n goto err;\n EVP_CIPHER_CTX_set_padding(kekctx, 0);\n if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) < 0) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,\n CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);\n goto err;\n }\n algtmp = pwri->keyDerivationAlgorithm;\n if (EVP_PBE_CipherInit(algtmp->algorithm,\n (char *)pwri->pass, pwri->passlen,\n algtmp->parameter, kekctx, en_de) < 0) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_EVP_LIB);\n goto err;\n }\n if (en_de) {\n if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, kekctx))\n goto err;\n key = OPENSSL_malloc(keylen);\n if (key == NULL)\n goto err;\n if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, kekctx))\n goto err;\n pwri->encryptedKey->data = key;\n pwri->encryptedKey->length = keylen;\n } else {\n key = OPENSSL_malloc(pwri->encryptedKey->length);\n if (key == NULL) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!kek_unwrap_key(key, &keylen,\n pwri->encryptedKey->data,\n pwri->encryptedKey->length, kekctx)) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNWRAP_FAILURE);\n goto err;\n }\n ec->key = key;\n ec->keylen = keylen;\n }\n r = 1;\n err:\n EVP_CIPHER_CTX_free(kekctx);\n if (!r)\n OPENSSL_free(key);\n X509_ALGOR_free(kekalg);\n return r;\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#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}'] |
2,262 | 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 BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,\n const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,\n BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, b, bits1, bits2, ret =\n 0, wpos1, wpos2, window1, window2, wvalue1, wvalue2;\n int r_is_one = 1;\n BIGNUM *d, *r;\n const BIGNUM *a_mod_m;\n BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n bn_check_top(a1);\n bn_check_top(p1);\n bn_check_top(a2);\n bn_check_top(p2);\n bn_check_top(m);\n if (!(m->d[0] & 1)) {\n BNerr(BN_F_BN_MOD_EXP2_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n bits1 = BN_num_bits(p1);\n bits2 = BN_num_bits(p2);\n if ((bits1 == 0) && (bits2 == 0)) {\n ret = BN_one(rr);\n return ret;\n }\n bits = (bits1 > bits2) ? bits1 : bits2;\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val1[0] = BN_CTX_get(ctx);\n val2[0] = BN_CTX_get(ctx);\n if (val2[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n window1 = BN_window_bits_for_exponent_size(bits1);\n window2 = BN_window_bits_for_exponent_size(bits2);\n if (a1->neg || BN_ucmp(a1, m) >= 0) {\n if (!BN_mod(val1[0], a1, m, ctx))\n goto err;\n a_mod_m = val1[0];\n } else\n a_mod_m = a1;\n if (BN_is_zero(a_mod_m)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))\n goto err;\n if (window1 > 1) {\n if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))\n goto err;\n j = 1 << (window1 - 1);\n for (i = 1; i < j; i++) {\n if (((val1[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))\n goto err;\n }\n }\n if (a2->neg || BN_ucmp(a2, m) >= 0) {\n if (!BN_mod(val2[0], a2, m, ctx))\n goto err;\n a_mod_m = val2[0];\n } else\n a_mod_m = a2;\n if (BN_is_zero(a_mod_m)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))\n goto err;\n if (window2 > 1) {\n if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))\n goto err;\n j = 1 << (window2 - 1);\n for (i = 1; i < j; i++) {\n if (((val2[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))\n goto err;\n }\n }\n r_is_one = 1;\n wvalue1 = 0;\n wvalue2 = 0;\n wpos1 = 0;\n wpos2 = 0;\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (b = bits - 1; b >= 0; b--) {\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!wvalue1)\n if (BN_is_bit_set(p1, b)) {\n i = b - window1 + 1;\n while (!BN_is_bit_set(p1, i))\n i++;\n wpos1 = i;\n wvalue1 = 1;\n for (i = b - 1; i >= wpos1; i--) {\n wvalue1 <<= 1;\n if (BN_is_bit_set(p1, i))\n wvalue1++;\n }\n }\n if (!wvalue2)\n if (BN_is_bit_set(p2, b)) {\n i = b - window2 + 1;\n while (!BN_is_bit_set(p2, i))\n i++;\n wpos2 = i;\n wvalue2 = 1;\n for (i = b - 1; i >= wpos2; i--) {\n wvalue2 <<= 1;\n if (BN_is_bit_set(p2, i))\n wvalue2++;\n }\n }\n if (wvalue1 && b == wpos1) {\n if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))\n goto err;\n wvalue1 = 0;\n r_is_one = 0;\n }\n if (wvalue2 && b == wpos2) {\n if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))\n goto err;\n wvalue2 = 0;\n r_is_one = 0;\n }\n }\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}', '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}'] |
2,263 | 0 | https://github.com/libav/libav/blob/38ce707e0273e975aa9be02b8992bbe7a5e258d8/ffmpeg.c/#L1033 | static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
int frame_size)
{
AVCodecContext *enc;
int frame_number;
double ti1, bitrate, avg_bitrate;
if (!vstats_file) {
vstats_file = fopen(vstats_filename, "w");
if (!vstats_file) {
perror("fopen");
av_exit(1);
}
}
enc = ost->st->codec;
if (enc->codec_type == CODEC_TYPE_VIDEO) {
frame_number = ost->frame_number;
fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);
if (enc->flags&CODEC_FLAG_PSNR)
fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
fprintf(vstats_file,"f_size= %6d ", frame_size);
ti1 = ost->sync_opts * av_q2d(enc->time_base);
if (ti1 < 0.01)
ti1 = 0.01;
bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
(double)video_size / 1024, ti1, bitrate, avg_bitrate);
fprintf(vstats_file,"type= %c\n", av_get_pict_type_char(enc->coded_frame->pict_type));
}
} | ['static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,\n int frame_size)\n{\n AVCodecContext *enc;\n int frame_number;\n double ti1, bitrate, avg_bitrate;\n if (!vstats_file) {\n vstats_file = fopen(vstats_filename, "w");\n if (!vstats_file) {\n perror("fopen");\n av_exit(1);\n }\n }\n enc = ost->st->codec;\n if (enc->codec_type == CODEC_TYPE_VIDEO) {\n frame_number = ost->frame_number;\n fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);\n if (enc->flags&CODEC_FLAG_PSNR)\n fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));\n fprintf(vstats_file,"f_size= %6d ", frame_size);\n ti1 = ost->sync_opts * av_q2d(enc->time_base);\n if (ti1 < 0.01)\n ti1 = 0.01;\n bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;\n avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;\n fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",\n (double)video_size / 1024, ti1, bitrate, avg_bitrate);\n fprintf(vstats_file,"type= %c\\n", av_get_pict_type_char(enc->coded_frame->pict_type));\n }\n}'] |
2,264 | 0 | https://github.com/nginx/nginx/blob/bcd78e22e97d4c870b5104d0c540caaa972176ed/src/core/ngx_hash.c/#L383 | ngx_int_t
ngx_hash_init(ngx_hash_init_t *hinit, ngx_hash_key_t *names, ngx_uint_t nelts)
{
u_char *elts;
size_t len;
u_short *test;
ngx_uint_t i, n, key, size, start, bucket_size;
ngx_hash_elt_t *elt, **buckets;
for (n = 0; n < nelts; n++) {
if (hinit->bucket_size < NGX_HASH_ELT_SIZE(&names[n]) + sizeof(void *))
{
ngx_log_error(NGX_LOG_EMERG, hinit->pool->log, 0,
"could not build the %s, you should "
"increase %s_bucket_size: %i",
hinit->name, hinit->name, hinit->bucket_size);
return NGX_ERROR;
}
}
test = ngx_alloc(hinit->max_size * sizeof(u_short), hinit->pool->log);
if (test == NULL) {
return NGX_ERROR;
}
bucket_size = hinit->bucket_size - sizeof(void *);
start = nelts / (bucket_size / (2 * sizeof(void *)));
start = start ? start : 1;
if (hinit->max_size > 10000 && hinit->max_size / nelts < 100) {
start = hinit->max_size - 1000;
}
for (size = start; size < hinit->max_size; size++) {
ngx_memzero(test, size * sizeof(u_short));
for (n = 0; n < nelts; n++) {
if (names[n].key.data == NULL) {
continue;
}
key = names[n].key_hash % size;
test[key] = (u_short) (test[key] + NGX_HASH_ELT_SIZE(&names[n]));
#if 0
ngx_log_error(NGX_LOG_ALERT, hinit->pool->log, 0,
"%ui: %ui %ui \"%V\"",
size, key, test[key], &names[n].key);
#endif
if (test[key] > (u_short) bucket_size) {
goto next;
}
}
goto found;
next:
continue;
}
ngx_log_error(NGX_LOG_EMERG, hinit->pool->log, 0,
"could not build the %s, you should increase "
"either %s_max_size: %i or %s_bucket_size: %i",
hinit->name, hinit->name, hinit->max_size,
hinit->name, hinit->bucket_size);
ngx_free(test);
return NGX_ERROR;
found:
for (i = 0; i < size; i++) {
test[i] = sizeof(void *);
}
for (n = 0; n < nelts; n++) {
if (names[n].key.data == NULL) {
continue;
}
key = names[n].key_hash % size;
test[key] = (u_short) (test[key] + NGX_HASH_ELT_SIZE(&names[n]));
}
len = 0;
for (i = 0; i < size; i++) {
if (test[i] == sizeof(void *)) {
continue;
}
test[i] = (u_short) (ngx_align(test[i], ngx_cacheline_size));
len += test[i];
}
if (hinit->hash == NULL) {
hinit->hash = ngx_pcalloc(hinit->pool, sizeof(ngx_hash_wildcard_t)
+ size * sizeof(ngx_hash_elt_t *));
if (hinit->hash == NULL) {
ngx_free(test);
return NGX_ERROR;
}
buckets = (ngx_hash_elt_t **)
((u_char *) hinit->hash + sizeof(ngx_hash_wildcard_t));
} else {
buckets = ngx_pcalloc(hinit->pool, size * sizeof(ngx_hash_elt_t *));
if (buckets == NULL) {
ngx_free(test);
return NGX_ERROR;
}
}
elts = ngx_palloc(hinit->pool, len + ngx_cacheline_size);
if (elts == NULL) {
ngx_free(test);
return NGX_ERROR;
}
elts = ngx_align_ptr(elts, ngx_cacheline_size);
for (i = 0; i < size; i++) {
if (test[i] == sizeof(void *)) {
continue;
}
buckets[i] = (ngx_hash_elt_t *) elts;
elts += test[i];
}
for (i = 0; i < size; i++) {
test[i] = 0;
}
for (n = 0; n < nelts; n++) {
if (names[n].key.data == NULL) {
continue;
}
key = names[n].key_hash % size;
elt = (ngx_hash_elt_t *) ((u_char *) buckets[key] + test[key]);
elt->value = names[n].value;
elt->len = (u_short) names[n].key.len;
ngx_strlow(elt->name, names[n].key.data, names[n].key.len);
test[key] = (u_short) (test[key] + NGX_HASH_ELT_SIZE(&names[n]));
}
for (i = 0; i < size; i++) {
if (buckets[i] == NULL) {
continue;
}
elt = (ngx_hash_elt_t *) ((u_char *) buckets[i] + test[i]);
elt->value = NULL;
}
ngx_free(test);
hinit->hash->buckets = buckets;
hinit->hash->size = size;
#if 0
for (i = 0; i < size; i++) {
ngx_str_t val;
ngx_uint_t key;
elt = buckets[i];
if (elt == NULL) {
ngx_log_error(NGX_LOG_ALERT, hinit->pool->log, 0,
"%ui: NULL", i);
continue;
}
while (elt->value) {
val.len = elt->len;
val.data = &elt->name[0];
key = hinit->key(val.data, val.len);
ngx_log_error(NGX_LOG_ALERT, hinit->pool->log, 0,
"%ui: %p \"%V\" %ui", i, elt, &val, key);
elt = (ngx_hash_elt_t *) ngx_align_ptr(&elt->name[0] + elt->len,
sizeof(void *));
}
}
#endif
return NGX_OK;
} | ['ngx_int_t\nngx_http_variables_init_vars(ngx_conf_t *cf)\n{\n ngx_uint_t i, n;\n ngx_hash_key_t *key;\n ngx_hash_init_t hash;\n ngx_http_variable_t *v, *av;\n ngx_http_core_main_conf_t *cmcf;\n cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);\n v = cmcf->variables.elts;\n key = cmcf->variables_keys->keys.elts;\n for (i = 0; i < cmcf->variables.nelts; i++) {\n for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) {\n av = key[n].value;\n if (av->get_handler\n && v[i].name.len == key[n].key.len\n && ngx_strncmp(v[i].name.data, key[n].key.data, v[i].name.len)\n == 0)\n {\n v[i].get_handler = av->get_handler;\n v[i].data = av->data;\n av->flags |= NGX_HTTP_VAR_INDEXED;\n v[i].flags = av->flags;\n av->index = i;\n goto next;\n }\n }\n if (ngx_strncmp(v[i].name.data, "http_", 5) == 0) {\n v[i].get_handler = ngx_http_variable_unknown_header_in;\n v[i].data = (uintptr_t) &v[i].name;\n continue;\n }\n if (ngx_strncmp(v[i].name.data, "sent_http_", 10) == 0) {\n v[i].get_handler = ngx_http_variable_unknown_header_out;\n v[i].data = (uintptr_t) &v[i].name;\n continue;\n }\n if (ngx_strncmp(v[i].name.data, "upstream_http_", 14) == 0) {\n v[i].get_handler = ngx_http_upstream_header_variable;\n v[i].data = (uintptr_t) &v[i].name;\n v[i].flags = NGX_HTTP_VAR_NOCACHEABLE;\n continue;\n }\n if (ngx_strncmp(v[i].name.data, "cookie_", 7) == 0) {\n v[i].get_handler = ngx_http_variable_cookie;\n v[i].data = (uintptr_t) &v[i].name;\n continue;\n }\n if (ngx_strncmp(v[i].name.data, "arg_", 4) == 0) {\n v[i].get_handler = ngx_http_variable_argument;\n v[i].data = (uintptr_t) &v[i].name;\n v[i].flags = NGX_HTTP_VAR_NOCACHEABLE;\n continue;\n }\n ngx_log_error(NGX_LOG_EMERG, cf->log, 0,\n "unknown \\"%V\\" variable", &v[i].name);\n return NGX_ERROR;\n next:\n continue;\n }\n for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) {\n av = key[n].value;\n if (av->flags & NGX_HTTP_VAR_NOHASH) {\n key[n].key.data = NULL;\n }\n }\n hash.hash = &cmcf->variables_hash;\n hash.key = ngx_hash_key;\n hash.max_size = cmcf->variables_hash_max_size;\n hash.bucket_size = cmcf->variables_hash_bucket_size;\n hash.name = "variables_hash";\n hash.pool = cf->pool;\n hash.temp_pool = NULL;\n if (ngx_hash_init(&hash, cmcf->variables_keys->keys.elts,\n cmcf->variables_keys->keys.nelts)\n != NGX_OK)\n {\n return NGX_ERROR;\n }\n cmcf->variables_keys = NULL;\n return NGX_OK;\n}', 'ngx_int_t\nngx_hash_init(ngx_hash_init_t *hinit, ngx_hash_key_t *names, ngx_uint_t nelts)\n{\n u_char *elts;\n size_t len;\n u_short *test;\n ngx_uint_t i, n, key, size, start, bucket_size;\n ngx_hash_elt_t *elt, **buckets;\n for (n = 0; n < nelts; n++) {\n if (hinit->bucket_size < NGX_HASH_ELT_SIZE(&names[n]) + sizeof(void *))\n {\n ngx_log_error(NGX_LOG_EMERG, hinit->pool->log, 0,\n "could not build the %s, you should "\n "increase %s_bucket_size: %i",\n hinit->name, hinit->name, hinit->bucket_size);\n return NGX_ERROR;\n }\n }\n test = ngx_alloc(hinit->max_size * sizeof(u_short), hinit->pool->log);\n if (test == NULL) {\n return NGX_ERROR;\n }\n bucket_size = hinit->bucket_size - sizeof(void *);\n start = nelts / (bucket_size / (2 * sizeof(void *)));\n start = start ? start : 1;\n if (hinit->max_size > 10000 && hinit->max_size / nelts < 100) {\n start = hinit->max_size - 1000;\n }\n for (size = start; size < hinit->max_size; size++) {\n ngx_memzero(test, size * sizeof(u_short));\n for (n = 0; n < nelts; n++) {\n if (names[n].key.data == NULL) {\n continue;\n }\n key = names[n].key_hash % size;\n test[key] = (u_short) (test[key] + NGX_HASH_ELT_SIZE(&names[n]));\n#if 0\n ngx_log_error(NGX_LOG_ALERT, hinit->pool->log, 0,\n "%ui: %ui %ui \\"%V\\"",\n size, key, test[key], &names[n].key);\n#endif\n if (test[key] > (u_short) bucket_size) {\n goto next;\n }\n }\n goto found;\n next:\n continue;\n }\n ngx_log_error(NGX_LOG_EMERG, hinit->pool->log, 0,\n "could not build the %s, you should increase "\n "either %s_max_size: %i or %s_bucket_size: %i",\n hinit->name, hinit->name, hinit->max_size,\n hinit->name, hinit->bucket_size);\n ngx_free(test);\n return NGX_ERROR;\nfound:\n for (i = 0; i < size; i++) {\n test[i] = sizeof(void *);\n }\n for (n = 0; n < nelts; n++) {\n if (names[n].key.data == NULL) {\n continue;\n }\n key = names[n].key_hash % size;\n test[key] = (u_short) (test[key] + NGX_HASH_ELT_SIZE(&names[n]));\n }\n len = 0;\n for (i = 0; i < size; i++) {\n if (test[i] == sizeof(void *)) {\n continue;\n }\n test[i] = (u_short) (ngx_align(test[i], ngx_cacheline_size));\n len += test[i];\n }\n if (hinit->hash == NULL) {\n hinit->hash = ngx_pcalloc(hinit->pool, sizeof(ngx_hash_wildcard_t)\n + size * sizeof(ngx_hash_elt_t *));\n if (hinit->hash == NULL) {\n ngx_free(test);\n return NGX_ERROR;\n }\n buckets = (ngx_hash_elt_t **)\n ((u_char *) hinit->hash + sizeof(ngx_hash_wildcard_t));\n } else {\n buckets = ngx_pcalloc(hinit->pool, size * sizeof(ngx_hash_elt_t *));\n if (buckets == NULL) {\n ngx_free(test);\n return NGX_ERROR;\n }\n }\n elts = ngx_palloc(hinit->pool, len + ngx_cacheline_size);\n if (elts == NULL) {\n ngx_free(test);\n return NGX_ERROR;\n }\n elts = ngx_align_ptr(elts, ngx_cacheline_size);\n for (i = 0; i < size; i++) {\n if (test[i] == sizeof(void *)) {\n continue;\n }\n buckets[i] = (ngx_hash_elt_t *) elts;\n elts += test[i];\n }\n for (i = 0; i < size; i++) {\n test[i] = 0;\n }\n for (n = 0; n < nelts; n++) {\n if (names[n].key.data == NULL) {\n continue;\n }\n key = names[n].key_hash % size;\n elt = (ngx_hash_elt_t *) ((u_char *) buckets[key] + test[key]);\n elt->value = names[n].value;\n elt->len = (u_short) names[n].key.len;\n ngx_strlow(elt->name, names[n].key.data, names[n].key.len);\n test[key] = (u_short) (test[key] + NGX_HASH_ELT_SIZE(&names[n]));\n }\n for (i = 0; i < size; i++) {\n if (buckets[i] == NULL) {\n continue;\n }\n elt = (ngx_hash_elt_t *) ((u_char *) buckets[i] + test[i]);\n elt->value = NULL;\n }\n ngx_free(test);\n hinit->hash->buckets = buckets;\n hinit->hash->size = size;\n#if 0\n for (i = 0; i < size; i++) {\n ngx_str_t val;\n ngx_uint_t key;\n elt = buckets[i];\n if (elt == NULL) {\n ngx_log_error(NGX_LOG_ALERT, hinit->pool->log, 0,\n "%ui: NULL", i);\n continue;\n }\n while (elt->value) {\n val.len = elt->len;\n val.data = &elt->name[0];\n key = hinit->key(val.data, val.len);\n ngx_log_error(NGX_LOG_ALERT, hinit->pool->log, 0,\n "%ui: %p \\"%V\\" %ui", i, elt, &val, key);\n elt = (ngx_hash_elt_t *) ngx_align_ptr(&elt->name[0] + elt->len,\n sizeof(void *));\n }\n }\n#endif\n return NGX_OK;\n}', 'void *\nngx_pcalloc(ngx_pool_t *pool, size_t size)\n{\n void *p;\n p = ngx_palloc(pool, size);\n if (p) {\n ngx_memzero(p, size);\n }\n return p;\n}', 'void *\nngx_palloc(ngx_pool_t *pool, size_t size)\n{\n u_char *m;\n ngx_pool_t *p;\n if (size <= pool->max) {\n p = pool->current;\n do {\n m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT);\n if ((size_t) (p->d.end - m) >= size) {\n p->d.last = m + size;\n return m;\n }\n p = p->d.next;\n } while (p);\n return ngx_palloc_block(pool, size);\n }\n return ngx_palloc_large(pool, size);\n}'] |
2,265 | 0 | https://github.com/libav/libav/blob/0836d48a16419faf742e999f565dc50d863d0f55/avconv.c/#L1897 | static void print_sdp(OutputFile *output_files, int n)
{
char sdp[2048];
int i;
AVFormatContext **avc = av_malloc(sizeof(*avc)*n);
if (!avc)
exit_program(1);
for (i = 0; i < n; i++)
avc[i] = output_files[i].ctx;
av_sdp_create(avc, n, sdp, sizeof(sdp));
printf("SDP:\n%s\n", sdp);
fflush(stdout);
av_freep(&avc);
} | ['static void print_sdp(OutputFile *output_files, int n)\n{\n char sdp[2048];\n int i;\n AVFormatContext **avc = av_malloc(sizeof(*avc)*n);\n if (!avc)\n exit_program(1);\n for (i = 0; i < n; i++)\n avc[i] = output_files[i].ctx;\n av_sdp_create(avc, n, sdp, sizeof(sdp));\n printf("SDP:\\n%s\\n", sdp);\n fflush(stdout);\n av_freep(&avc);\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}'] |
2,266 | 0 | https://github.com/nginx/nginx/blob/030a1f959c9c673258fe53f968fab04fc9214b86/src/http/modules/ngx_http_uwsgi_module.c/#L1003 | static ngx_int_t
ngx_http_uwsgi_create_request(ngx_http_request_t *r)
{
u_char ch, *lowcase_key;
size_t key_len, val_len, len, allocated;
ngx_uint_t i, n, hash, skip_empty, header_params;
ngx_buf_t *b;
ngx_chain_t *cl, *body;
ngx_list_part_t *part;
ngx_table_elt_t *header, **ignored;
ngx_http_uwsgi_params_t *params;
ngx_http_script_code_pt code;
ngx_http_script_engine_t e, le;
ngx_http_uwsgi_loc_conf_t *uwcf;
ngx_http_script_len_code_pt lcode;
len = 0;
header_params = 0;
ignored = NULL;
uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module);
#if (NGX_HTTP_CACHE)
params = r->upstream->cacheable ? &uwcf->params_cache : &uwcf->params;
#else
params = &uwcf->params;
#endif
if (params->lengths) {
ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
ngx_http_script_flush_no_cacheable_variables(r, params->flushes);
le.flushed = 1;
le.ip = params->lengths->elts;
le.request = r;
while (*(uintptr_t *) le.ip) {
lcode = *(ngx_http_script_len_code_pt *) le.ip;
key_len = lcode(&le);
lcode = *(ngx_http_script_len_code_pt *) le.ip;
skip_empty = lcode(&le);
for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode (&le)) {
lcode = *(ngx_http_script_len_code_pt *) le.ip;
}
le.ip += sizeof(uintptr_t);
if (skip_empty && val_len == 0) {
continue;
}
len += 2 + key_len + 2 + val_len;
}
}
if (uwcf->upstream.pass_request_headers) {
allocated = 0;
lowcase_key = NULL;
if (params->number) {
n = 0;
part = &r->headers_in.headers.part;
while (part) {
n += part->nelts;
part = part->next;
}
ignored = ngx_palloc(r->pool, n * sizeof(void *));
if (ignored == NULL) {
return NGX_ERROR;
}
}
part = &r->headers_in.headers.part;
header = part->elts;
for (i = 0; ; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
if (params->number) {
if (allocated < header[i].key.len) {
allocated = header[i].key.len + 16;
lowcase_key = ngx_pnalloc(r->pool, allocated);
if (lowcase_key == NULL) {
return NGX_ERROR;
}
}
hash = 0;
for (n = 0; n < header[i].key.len; n++) {
ch = header[i].key.data[n];
if (ch >= 'A' && ch <= 'Z') {
ch |= 0x20;
} else if (ch == '-') {
ch = '_';
}
hash = ngx_hash(hash, ch);
lowcase_key[n] = ch;
}
if (ngx_hash_find(¶ms->hash, hash, lowcase_key, n)) {
ignored[header_params++] = &header[i];
continue;
}
}
len += 2 + sizeof("HTTP_") - 1 + header[i].key.len
+ 2 + header[i].value.len;
}
}
len += uwcf->uwsgi_string.len;
#if 0
if (len > 0 && len < 2) {
ngx_log_error (NGX_LOG_ALERT, r->connection->log, 0,
"uwsgi request is too little: %uz", len);
return NGX_ERROR;
}
#endif
b = ngx_create_temp_buf(r->pool, len + 4);
if (b == NULL) {
return NGX_ERROR;
}
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = b;
*b->last++ = (u_char) uwcf->modifier1;
*b->last++ = (u_char) (len & 0xff);
*b->last++ = (u_char) ((len >> 8) & 0xff);
*b->last++ = (u_char) uwcf->modifier2;
if (params->lengths) {
ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
e.ip = params->values->elts;
e.pos = b->last;
e.request = r;
e.flushed = 1;
le.ip = params->lengths->elts;
while (*(uintptr_t *) le.ip) {
lcode = *(ngx_http_script_len_code_pt *) le.ip;
key_len = (u_char) lcode (&le);
lcode = *(ngx_http_script_len_code_pt *) le.ip;
skip_empty = lcode(&le);
for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
lcode = *(ngx_http_script_len_code_pt *) le.ip;
}
le.ip += sizeof(uintptr_t);
if (skip_empty && val_len == 0) {
e.skip = 1;
while (*(uintptr_t *) e.ip) {
code = *(ngx_http_script_code_pt *) e.ip;
code((ngx_http_script_engine_t *) &e);
}
e.ip += sizeof(uintptr_t);
e.skip = 0;
continue;
}
*e.pos++ = (u_char) (key_len & 0xff);
*e.pos++ = (u_char) ((key_len >> 8) & 0xff);
code = *(ngx_http_script_code_pt *) e.ip;
code((ngx_http_script_engine_t *) & e);
*e.pos++ = (u_char) (val_len & 0xff);
*e.pos++ = (u_char) ((val_len >> 8) & 0xff);
while (*(uintptr_t *) e.ip) {
code = *(ngx_http_script_code_pt *) e.ip;
code((ngx_http_script_engine_t *) & e);
}
e.ip += sizeof(uintptr_t);
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"uwsgi param: \"%*s: %*s\"",
key_len, e.pos - (key_len + 2 + val_len),
val_len, e.pos - val_len);
}
b->last = e.pos;
}
if (uwcf->upstream.pass_request_headers) {
part = &r->headers_in.headers.part;
header = part->elts;
for (i = 0; ; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
for (n = 0; n < header_params; n++) {
if (&header[i] == ignored[n]) {
goto next;
}
}
key_len = sizeof("HTTP_") - 1 + header[i].key.len;
*b->last++ = (u_char) (key_len & 0xff);
*b->last++ = (u_char) ((key_len >> 8) & 0xff);
b->last = ngx_cpymem(b->last, "HTTP_", sizeof("HTTP_") - 1);
for (n = 0; n < header[i].key.len; n++) {
ch = header[i].key.data[n];
if (ch >= 'a' && ch <= 'z') {
ch &= ~0x20;
} else if (ch == '-') {
ch = '_';
}
*b->last++ = ch;
}
val_len = header[i].value.len;
*b->last++ = (u_char) (val_len & 0xff);
*b->last++ = (u_char) ((val_len >> 8) & 0xff);
b->last = ngx_copy(b->last, header[i].value.data, val_len);
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"uwsgi param: \"%*s: %*s\"",
key_len, b->last - (key_len + 2 + val_len),
val_len, b->last - val_len);
next:
continue;
}
}
b->last = ngx_copy(b->last, uwcf->uwsgi_string.data,
uwcf->uwsgi_string.len);
if (r->request_body_no_buffering) {
r->upstream->request_bufs = cl;
} else if (uwcf->upstream.pass_request_body) {
body = r->upstream->request_bufs;
r->upstream->request_bufs = cl;
while (body) {
b = ngx_alloc_buf(r->pool);
if (b == NULL) {
return NGX_ERROR;
}
ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
cl->next = ngx_alloc_chain_link(r->pool);
if (cl->next == NULL) {
return NGX_ERROR;
}
cl = cl->next;
cl->buf = b;
body = body->next;
}
} else {
r->upstream->request_bufs = cl;
}
cl->next = NULL;
return NGX_OK;
} | ['static ngx_int_t\nngx_http_uwsgi_create_request(ngx_http_request_t *r)\n{\n u_char ch, *lowcase_key;\n size_t key_len, val_len, len, allocated;\n ngx_uint_t i, n, hash, skip_empty, header_params;\n ngx_buf_t *b;\n ngx_chain_t *cl, *body;\n ngx_list_part_t *part;\n ngx_table_elt_t *header, **ignored;\n ngx_http_uwsgi_params_t *params;\n ngx_http_script_code_pt code;\n ngx_http_script_engine_t e, le;\n ngx_http_uwsgi_loc_conf_t *uwcf;\n ngx_http_script_len_code_pt lcode;\n len = 0;\n header_params = 0;\n ignored = NULL;\n uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module);\n#if (NGX_HTTP_CACHE)\n params = r->upstream->cacheable ? &uwcf->params_cache : &uwcf->params;\n#else\n params = &uwcf->params;\n#endif\n if (params->lengths) {\n ngx_memzero(&le, sizeof(ngx_http_script_engine_t));\n ngx_http_script_flush_no_cacheable_variables(r, params->flushes);\n le.flushed = 1;\n le.ip = params->lengths->elts;\n le.request = r;\n while (*(uintptr_t *) le.ip) {\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n key_len = lcode(&le);\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n skip_empty = lcode(&le);\n for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode (&le)) {\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n }\n le.ip += sizeof(uintptr_t);\n if (skip_empty && val_len == 0) {\n continue;\n }\n len += 2 + key_len + 2 + val_len;\n }\n }\n if (uwcf->upstream.pass_request_headers) {\n allocated = 0;\n lowcase_key = NULL;\n if (params->number) {\n n = 0;\n part = &r->headers_in.headers.part;\n while (part) {\n n += part->nelts;\n part = part->next;\n }\n ignored = ngx_palloc(r->pool, n * sizeof(void *));\n if (ignored == NULL) {\n return NGX_ERROR;\n }\n }\n part = &r->headers_in.headers.part;\n header = part->elts;\n for (i = 0; ; i++) {\n if (i >= part->nelts) {\n if (part->next == NULL) {\n break;\n }\n part = part->next;\n header = part->elts;\n i = 0;\n }\n if (params->number) {\n if (allocated < header[i].key.len) {\n allocated = header[i].key.len + 16;\n lowcase_key = ngx_pnalloc(r->pool, allocated);\n if (lowcase_key == NULL) {\n return NGX_ERROR;\n }\n }\n hash = 0;\n for (n = 0; n < header[i].key.len; n++) {\n ch = header[i].key.data[n];\n if (ch >= \'A\' && ch <= \'Z\') {\n ch |= 0x20;\n } else if (ch == \'-\') {\n ch = \'_\';\n }\n hash = ngx_hash(hash, ch);\n lowcase_key[n] = ch;\n }\n if (ngx_hash_find(¶ms->hash, hash, lowcase_key, n)) {\n ignored[header_params++] = &header[i];\n continue;\n }\n }\n len += 2 + sizeof("HTTP_") - 1 + header[i].key.len\n + 2 + header[i].value.len;\n }\n }\n len += uwcf->uwsgi_string.len;\n#if 0\n if (len > 0 && len < 2) {\n ngx_log_error (NGX_LOG_ALERT, r->connection->log, 0,\n "uwsgi request is too little: %uz", len);\n return NGX_ERROR;\n }\n#endif\n b = ngx_create_temp_buf(r->pool, len + 4);\n if (b == NULL) {\n return NGX_ERROR;\n }\n cl = ngx_alloc_chain_link(r->pool);\n if (cl == NULL) {\n return NGX_ERROR;\n }\n cl->buf = b;\n *b->last++ = (u_char) uwcf->modifier1;\n *b->last++ = (u_char) (len & 0xff);\n *b->last++ = (u_char) ((len >> 8) & 0xff);\n *b->last++ = (u_char) uwcf->modifier2;\n if (params->lengths) {\n ngx_memzero(&e, sizeof(ngx_http_script_engine_t));\n e.ip = params->values->elts;\n e.pos = b->last;\n e.request = r;\n e.flushed = 1;\n le.ip = params->lengths->elts;\n while (*(uintptr_t *) le.ip) {\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n key_len = (u_char) lcode (&le);\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n skip_empty = lcode(&le);\n for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n }\n le.ip += sizeof(uintptr_t);\n if (skip_empty && val_len == 0) {\n e.skip = 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 e.ip += sizeof(uintptr_t);\n e.skip = 0;\n continue;\n }\n *e.pos++ = (u_char) (key_len & 0xff);\n *e.pos++ = (u_char) ((key_len >> 8) & 0xff);\n code = *(ngx_http_script_code_pt *) e.ip;\n code((ngx_http_script_engine_t *) & e);\n *e.pos++ = (u_char) (val_len & 0xff);\n *e.pos++ = (u_char) ((val_len >> 8) & 0xff);\n while (*(uintptr_t *) e.ip) {\n code = *(ngx_http_script_code_pt *) e.ip;\n code((ngx_http_script_engine_t *) & e);\n }\n e.ip += sizeof(uintptr_t);\n ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "uwsgi param: \\"%*s: %*s\\"",\n key_len, e.pos - (key_len + 2 + val_len),\n val_len, e.pos - val_len);\n }\n b->last = e.pos;\n }\n if (uwcf->upstream.pass_request_headers) {\n part = &r->headers_in.headers.part;\n header = part->elts;\n for (i = 0; ; i++) {\n if (i >= part->nelts) {\n if (part->next == NULL) {\n break;\n }\n part = part->next;\n header = part->elts;\n i = 0;\n }\n for (n = 0; n < header_params; n++) {\n if (&header[i] == ignored[n]) {\n goto next;\n }\n }\n key_len = sizeof("HTTP_") - 1 + header[i].key.len;\n *b->last++ = (u_char) (key_len & 0xff);\n *b->last++ = (u_char) ((key_len >> 8) & 0xff);\n b->last = ngx_cpymem(b->last, "HTTP_", sizeof("HTTP_") - 1);\n for (n = 0; n < header[i].key.len; n++) {\n ch = header[i].key.data[n];\n if (ch >= \'a\' && ch <= \'z\') {\n ch &= ~0x20;\n } else if (ch == \'-\') {\n ch = \'_\';\n }\n *b->last++ = ch;\n }\n val_len = header[i].value.len;\n *b->last++ = (u_char) (val_len & 0xff);\n *b->last++ = (u_char) ((val_len >> 8) & 0xff);\n b->last = ngx_copy(b->last, header[i].value.data, val_len);\n ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "uwsgi param: \\"%*s: %*s\\"",\n key_len, b->last - (key_len + 2 + val_len),\n val_len, b->last - val_len);\n next:\n continue;\n }\n }\n b->last = ngx_copy(b->last, uwcf->uwsgi_string.data,\n uwcf->uwsgi_string.len);\n if (r->request_body_no_buffering) {\n r->upstream->request_bufs = cl;\n } else if (uwcf->upstream.pass_request_body) {\n body = r->upstream->request_bufs;\n r->upstream->request_bufs = cl;\n while (body) {\n b = ngx_alloc_buf(r->pool);\n if (b == NULL) {\n return NGX_ERROR;\n }\n ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));\n cl->next = ngx_alloc_chain_link(r->pool);\n if (cl->next == NULL) {\n return NGX_ERROR;\n }\n cl = cl->next;\n cl->buf = b;\n body = body->next;\n }\n } else {\n r->upstream->request_bufs = cl;\n }\n cl->next = NULL;\n return NGX_OK;\n}', 'ngx_buf_t *\nngx_create_temp_buf(ngx_pool_t *pool, size_t size)\n{\n ngx_buf_t *b;\n b = ngx_calloc_buf(pool);\n if (b == NULL) {\n return NULL;\n }\n b->start = ngx_palloc(pool, size);\n if (b->start == NULL) {\n return NULL;\n }\n b->pos = b->start;\n b->last = b->start;\n b->end = b->last + size;\n b->temporary = 1;\n return b;\n}', 'void *\nngx_palloc(ngx_pool_t *pool, size_t size)\n{\n#if !(NGX_DEBUG_PALLOC)\n if (size <= pool->max) {\n return ngx_palloc_small(pool, size, 1);\n }\n#endif\n return ngx_palloc_large(pool, size);\n}', 'static ngx_inline void *\nngx_palloc_small(ngx_pool_t *pool, size_t size, ngx_uint_t align)\n{\n u_char *m;\n ngx_pool_t *p;\n p = pool->current;\n do {\n m = p->d.last;\n if (align) {\n m = ngx_align_ptr(m, NGX_ALIGNMENT);\n }\n if ((size_t) (p->d.end - m) >= size) {\n p->d.last = m + size;\n return m;\n }\n p = p->d.next;\n } while (p);\n return ngx_palloc_block(pool, size);\n}'] |
2,267 | 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 ec_GF2m_simple_mul(const EC_GROUP *group, EC_POINT *r,\n const BIGNUM *scalar, size_t num,\n const EC_POINT *points[], const BIGNUM *scalars[],\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n int ret = 0;\n size_t i;\n EC_POINT *p = NULL;\n EC_POINT *acc = NULL;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n if ((scalar && (num > 1)) || (num > 2)\n || (num == 0 && EC_GROUP_have_precompute_mult(group))) {\n ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);\n goto err;\n }\n if ((p = EC_POINT_new(group)) == NULL)\n goto err;\n if ((acc = EC_POINT_new(group)) == NULL)\n goto err;\n if (!EC_POINT_set_to_infinity(group, acc))\n goto err;\n if (scalar) {\n if (!ec_GF2m_montgomery_point_multiply\n (group, p, scalar, group->generator, ctx))\n goto err;\n if (BN_is_negative(scalar))\n if (!group->meth->invert(group, p, ctx))\n goto err;\n if (!group->meth->add(group, acc, acc, p, ctx))\n goto err;\n }\n for (i = 0; i < num; i++) {\n if (!ec_GF2m_montgomery_point_multiply\n (group, p, scalars[i], points[i], ctx))\n goto err;\n if (BN_is_negative(scalars[i]))\n if (!group->meth->invert(group, p, ctx))\n goto err;\n if (!group->meth->add(group, acc, acc, p, ctx))\n goto err;\n }\n if (!EC_POINT_copy(r, acc))\n goto err;\n ret = 1;\n err:\n EC_POINT_free(p);\n EC_POINT_free(acc);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'static int ec_GF2m_montgomery_point_multiply(const EC_GROUP *group,\n EC_POINT *r,\n const BIGNUM *scalar,\n const EC_POINT *point,\n BN_CTX *ctx)\n{\n BIGNUM *x1, *x2, *z1, *z2;\n int ret = 0, i, group_top;\n BN_ULONG mask, word;\n if (r == point) {\n ECerr(EC_F_EC_GF2M_MONTGOMERY_POINT_MULTIPLY, EC_R_INVALID_ARGUMENT);\n return 0;\n }\n if ((scalar == NULL) || BN_is_zero(scalar) || (point == NULL) ||\n EC_POINT_is_at_infinity(group, point)) {\n return EC_POINT_set_to_infinity(group, r);\n }\n if (!point->Z_is_one)\n return 0;\n BN_CTX_start(ctx);\n x1 = BN_CTX_get(ctx);\n z1 = BN_CTX_get(ctx);\n if (z1 == NULL)\n goto err;\n x2 = r->X;\n z2 = r->Y;\n group_top = bn_get_top(group->field);\n if (bn_wexpand(x1, group_top) == NULL\n || bn_wexpand(z1, group_top) == NULL\n || bn_wexpand(x2, group_top) == NULL\n || bn_wexpand(z2, group_top) == NULL)\n goto err;\n if (!BN_GF2m_mod_arr(x1, point->X, group->poly))\n goto err;\n if (!BN_one(z1))\n goto err;\n if (!group->meth->field_sqr(group, z2, x1, ctx))\n goto err;\n if (!group->meth->field_sqr(group, x2, z2, ctx))\n goto err;\n if (!BN_GF2m_add(x2, x2, group->b))\n goto err;\n i = bn_get_top(scalar) - 1;\n mask = BN_TBIT;\n word = bn_get_words(scalar)[i];\n while (!(word & mask))\n mask >>= 1;\n mask >>= 1;\n if (!mask) {\n i--;\n mask = BN_TBIT;\n }\n for (; i >= 0; i--) {\n word = bn_get_words(scalar)[i];\n while (mask) {\n BN_consttime_swap(word & mask, x1, x2, group_top);\n BN_consttime_swap(word & mask, z1, z2, group_top);\n if (!gf2m_Madd(group, point->X, x2, z2, x1, z1, ctx))\n goto err;\n if (!gf2m_Mdouble(group, x1, z1, ctx))\n goto err;\n BN_consttime_swap(word & mask, x1, x2, group_top);\n BN_consttime_swap(word & mask, z1, z2, group_top);\n mask >>= 1;\n }\n mask = BN_TBIT;\n }\n i = gf2m_Mxy(group, point->X, point->Y, x1, z1, x2, z2, ctx);\n if (i == 0)\n goto err;\n else if (i == 1) {\n if (!EC_POINT_set_to_infinity(group, r))\n goto err;\n } else {\n if (!BN_one(r->Z))\n goto err;\n r->Z_is_one = 1;\n }\n BN_set_negative(r->X, 0);\n BN_set_negative(r->Y, 0);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', '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}'] |
2,268 | 0 | https://github.com/libav/libav/blob/97ae370078e5ee8e78269fe4d7b4f457e82bd758/libavformat/rtpdec_mpeg4.c/#L186 | static int aac_parse_packet(AVFormatContext *ctx,
PayloadContext *data,
AVStream *st,
AVPacket *pkt,
uint32_t *timestamp,
const uint8_t *buf, int len, int flags)
{
if (rtp_parse_mp4_au(data, buf))
return -1;
buf += data->au_headers_length_bytes + 2;
len -= data->au_headers_length_bytes + 2;
av_new_packet(pkt, data->au_headers[0].size);
memcpy(pkt->data, buf, data->au_headers[0].size);
pkt->stream_index = st->index;
return 0;
} | ['static int aac_parse_packet(AVFormatContext *ctx,\n PayloadContext *data,\n AVStream *st,\n AVPacket *pkt,\n uint32_t *timestamp,\n const uint8_t *buf, int len, int flags)\n{\n if (rtp_parse_mp4_au(data, buf))\n return -1;\n buf += data->au_headers_length_bytes + 2;\n len -= data->au_headers_length_bytes + 2;\n av_new_packet(pkt, data->au_headers[0].size);\n memcpy(pkt->data, buf, data->au_headers[0].size);\n pkt->stream_index = st->index;\n return 0;\n}', 'int av_new_packet(AVPacket *pkt, int size)\n{\n uint8_t *data = NULL;\n if ((unsigned)size < (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)\n data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);\n if (data) {\n memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);\n } else\n size = 0;\n av_init_packet(pkt);\n pkt->data = data;\n pkt->size = size;\n pkt->destruct = av_destruct_packet;\n if (!data)\n return AVERROR(ENOMEM);\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 assert(size);\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_MEMALIGN\n ptr = memalign(32,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'void av_init_packet(AVPacket *pkt)\n{\n pkt->pts = AV_NOPTS_VALUE;\n pkt->dts = AV_NOPTS_VALUE;\n pkt->pos = -1;\n pkt->duration = 0;\n pkt->convergence_duration = 0;\n pkt->flags = 0;\n pkt->stream_index = 0;\n pkt->destruct = NULL;\n pkt->side_data = NULL;\n pkt->side_data_elems = 0;\n}'] |
2,269 | 0 | https://gitlab.com/libtiff/libtiff/blob/cec2d959be52d749846b8343b1c71ec01533c746/tools/tiffcrop.c/#L7616 | static int
createCroppedImage(struct image_data *image, struct crop_mask *crop,
unsigned char **read_buff_ptr, unsigned char **crop_buff_ptr)
{
tsize_t cropsize;
unsigned char *read_buff = NULL;
unsigned char *crop_buff = NULL;
unsigned char *new_buff = NULL;
static tsize_t prev_cropsize = 0;
read_buff = *read_buff_ptr;
crop_buff = read_buff;
*crop_buff_ptr = read_buff;
crop->combined_width = image->width;
crop->combined_length = image->length;
cropsize = crop->bufftotal;
crop_buff = *crop_buff_ptr;
if (!crop_buff)
{
crop_buff = (unsigned char *)_TIFFmalloc(cropsize);
*crop_buff_ptr = crop_buff;
_TIFFmemset(crop_buff, 0, cropsize);
prev_cropsize = cropsize;
}
else
{
if (prev_cropsize < cropsize)
{
new_buff = _TIFFrealloc(crop_buff, cropsize);
if (!new_buff)
{
free (crop_buff);
crop_buff = (unsigned char *)_TIFFmalloc(cropsize);
}
else
crop_buff = new_buff;
_TIFFmemset(crop_buff, 0, cropsize);
}
}
if (!crop_buff)
{
TIFFError("createCroppedImage", "Unable to allocate/reallocate crop buffer");
return (-1);
}
*crop_buff_ptr = crop_buff;
if (crop->crop_mode & CROP_INVERT)
{
switch (crop->photometric)
{
case PHOTOMETRIC_MINISWHITE:
case PHOTOMETRIC_MINISBLACK:
image->photometric = crop->photometric;
break;
case INVERT_DATA_ONLY:
case INVERT_DATA_AND_TAG:
if (invertImage(image->photometric, image->spp, image->bps,
crop->combined_width, crop->combined_length, crop_buff))
{
TIFFError("createCroppedImage",
"Failed to invert colorspace for image or cropped selection");
return (-1);
}
if (crop->photometric == INVERT_DATA_AND_TAG)
{
switch (image->photometric)
{
case PHOTOMETRIC_MINISWHITE:
image->photometric = PHOTOMETRIC_MINISBLACK;
break;
case PHOTOMETRIC_MINISBLACK:
image->photometric = PHOTOMETRIC_MINISWHITE;
break;
default:
break;
}
}
break;
default: break;
}
}
if (crop->crop_mode & CROP_MIRROR)
{
if (mirrorImage(image->spp, image->bps, crop->mirror,
crop->combined_width, crop->combined_length, crop_buff))
{
TIFFError("createCroppedImage", "Failed to mirror image or cropped selection %s",
(crop->rotation == MIRROR_HORIZ) ? "horizontally" : "vertically");
return (-1);
}
}
if (crop->crop_mode & CROP_ROTATE)
{
if (rotateImage(crop->rotation, image, &crop->combined_width,
&crop->combined_length, crop_buff_ptr))
{
TIFFError("createCroppedImage",
"Failed to rotate image or cropped selection by %d degrees", crop->rotation);
return (-1);
}
}
if (crop_buff == read_buff)
*read_buff_ptr = NULL;
return (0);
} | ['static int\ncreateCroppedImage(struct image_data *image, struct crop_mask *crop,\n unsigned char **read_buff_ptr, unsigned char **crop_buff_ptr)\n {\n tsize_t cropsize;\n unsigned char *read_buff = NULL;\n unsigned char *crop_buff = NULL;\n unsigned char *new_buff = NULL;\n static tsize_t prev_cropsize = 0;\n read_buff = *read_buff_ptr;\n crop_buff = read_buff;\n *crop_buff_ptr = read_buff;\n crop->combined_width = image->width;\n crop->combined_length = image->length;\n cropsize = crop->bufftotal;\n crop_buff = *crop_buff_ptr;\n if (!crop_buff)\n {\n crop_buff = (unsigned char *)_TIFFmalloc(cropsize);\n *crop_buff_ptr = crop_buff;\n _TIFFmemset(crop_buff, 0, cropsize);\n prev_cropsize = cropsize;\n }\n else\n {\n if (prev_cropsize < cropsize)\n {\n new_buff = _TIFFrealloc(crop_buff, cropsize);\n if (!new_buff)\n {\n\tfree (crop_buff);\n crop_buff = (unsigned char *)_TIFFmalloc(cropsize);\n }\n else\n crop_buff = new_buff;\n _TIFFmemset(crop_buff, 0, cropsize);\n }\n }\n if (!crop_buff)\n {\n TIFFError("createCroppedImage", "Unable to allocate/reallocate crop buffer");\n return (-1);\n }\n *crop_buff_ptr = crop_buff;\n if (crop->crop_mode & CROP_INVERT)\n {\n switch (crop->photometric)\n {\n case PHOTOMETRIC_MINISWHITE:\n case PHOTOMETRIC_MINISBLACK:\n\t image->photometric = crop->photometric;\n\t break;\n case INVERT_DATA_ONLY:\n case INVERT_DATA_AND_TAG:\n if (invertImage(image->photometric, image->spp, image->bps,\n crop->combined_width, crop->combined_length, crop_buff))\n {\n TIFFError("createCroppedImage",\n "Failed to invert colorspace for image or cropped selection");\n return (-1);\n }\n if (crop->photometric == INVERT_DATA_AND_TAG)\n {\n switch (image->photometric)\n {\n case PHOTOMETRIC_MINISWHITE:\n \t image->photometric = PHOTOMETRIC_MINISBLACK;\n\t break;\n case PHOTOMETRIC_MINISBLACK:\n \t image->photometric = PHOTOMETRIC_MINISWHITE;\n\t break;\n default:\n\t break;\n\t }\n\t }\n break;\n default: break;\n }\n }\n if (crop->crop_mode & CROP_MIRROR)\n {\n if (mirrorImage(image->spp, image->bps, crop->mirror,\n crop->combined_width, crop->combined_length, crop_buff))\n {\n TIFFError("createCroppedImage", "Failed to mirror image or cropped selection %s",\n\t (crop->rotation == MIRROR_HORIZ) ? "horizontally" : "vertically");\n return (-1);\n }\n }\n if (crop->crop_mode & CROP_ROTATE)\n {\n if (rotateImage(crop->rotation, image, &crop->combined_width,\n &crop->combined_length, crop_buff_ptr))\n {\n TIFFError("createCroppedImage",\n "Failed to rotate image or cropped selection by %d degrees", crop->rotation);\n return (-1);\n }\n }\n if (crop_buff == read_buff)\n *read_buff_ptr = NULL;\n return (0);\n }', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n if (s == 0)\n return ((void *) NULL);\n\treturn (malloc((size_t) s));\n}', 'void\n_TIFFmemset(void* p, int v, tmsize_t c)\n{\n\tmemset(p, v, (size_t) c);\n}'] |
2,270 | 0 | https://github.com/openssl/openssl/blob/d9c989fe3f137580ee627c91e01245e78b0b41ff/crypto/lhash/lhash.c/#L220 | static int expand(OPENSSL_LHASH *lh)
{
OPENSSL_LH_NODE **n, **n1, **n2, *np;
unsigned int p, pmax, nni, j;
unsigned long hash;
nni = lh->num_alloc_nodes;
p = lh->p;
pmax = lh->pmax;
if (p + 1 >= pmax) {
j = nni * 2;
n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
if (n == NULL) {
lh->error++;
return 0;
}
lh->b = n;
memset(n + nni, 0, sizeof(*n) * (j - nni));
lh->pmax = nni;
lh->num_alloc_nodes = j;
lh->num_expand_reallocs++;
lh->p = 0;
} else {
lh->p++;
}
lh->num_nodes++;
lh->num_expands++;
n1 = &(lh->b[p]);
n2 = &(lh->b[p + pmax]);
*n2 = NULL;
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;
}
return 1;
} | ['static int test_int_lhash(void)\n{\n static struct {\n int data;\n int null;\n } dels[] = {\n { 65537, 0 },\n { 173, 0 },\n { 999, 1 },\n { 37, 0 },\n { 1, 0 },\n { 34, 1 }\n };\n const unsigned int n_dels = OSSL_NELEM(dels);\n LHASH_OF(int) *h = lh_int_new(&int_hash, &int_cmp);\n unsigned int i;\n int testresult = 0, j, *p;\n if (!TEST_ptr(h))\n goto end;\n for (i = 0; i < n_int_tests; i++)\n if (!TEST_ptr_null(lh_int_insert(h, int_tests + i))) {\n TEST_info("int insert %d", i);\n goto end;\n }\n if (!TEST_int_eq(lh_int_num_items(h), n_int_tests))\n goto end;\n for (i = 0; i < n_int_tests; i++)\n if (!TEST_int_eq(*lh_int_retrieve(h, int_tests + i), int_tests[i])) {\n TEST_info("lhash int retrieve value %d", i);\n goto end;\n }\n for (i = 0; i < n_int_tests; i++)\n if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + i), int_tests + i)) {\n TEST_info("lhash int retrieve address %d", i);\n goto end;\n }\n j = 1;\n if (!TEST_ptr_eq(lh_int_retrieve(h, &j), int_tests + 2))\n goto end;\n j = 13;\n if (!TEST_ptr(p = lh_int_insert(h, &j)))\n goto end;\n if (!TEST_ptr_eq(p, int_tests + 1))\n goto end;\n if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + 1), &j))\n goto end;\n memset(int_found, 0, sizeof(int_found));\n lh_int_doall(h, &int_doall);\n for (i = 0; i < n_int_tests; i++)\n if (!TEST_int_eq(int_found[i], 1)) {\n TEST_info("lhash int doall %d", i);\n goto end;\n }\n memset(int_found, 0, sizeof(int_found));\n lh_int_doall_short(h, int_doall_arg, int_found);\n for (i = 0; i < n_int_tests; i++)\n if (!TEST_int_eq(int_found[i], 1)) {\n TEST_info("lhash int doall arg %d", i);\n goto end;\n }\n for (i = 0; i < n_dels; i++) {\n const int b = lh_int_delete(h, &dels[i].data) == NULL;\n if (!TEST_int_eq(b ^ dels[i].null, 0)) {\n TEST_info("lhash int delete %d", i);\n goto end;\n }\n }\n if (!TEST_int_eq(lh_int_error(h), 0))\n goto end;\n testresult = 1;\nend:\n lh_int_free(h);\n return testresult;\n}', 'DEFINE_LHASH_OF(int)', '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, pmax, nni, j;\n unsigned long hash;\n nni = lh->num_alloc_nodes;\n p = lh->p;\n pmax = lh->pmax;\n if (p + 1 >= pmax) {\n j = nni * 2;\n n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);\n if (n == NULL) {\n lh->error++;\n return 0;\n }\n lh->b = n;\n memset(n + nni, 0, sizeof(*n) * (j - nni));\n lh->pmax = nni;\n lh->num_alloc_nodes = j;\n lh->num_expand_reallocs++;\n lh->p = 0;\n } else {\n lh->p++;\n }\n lh->num_nodes++;\n lh->num_expands++;\n n1 = &(lh->b[p]);\n n2 = &(lh->b[p + pmax]);\n *n2 = NULL;\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 return 1;\n}'] |
2,271 | 0 | https://github.com/openssl/openssl/blob/7788638777934141e235d0add9341e852ac4e80b/crypto/hmac/hmac.c/#L183 | int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
{
HMAC_CTX_init(dctx);
if (!EVP_MD_CTX_copy_ex(&dctx->i_ctx, &sctx->i_ctx))
goto err;
if (!EVP_MD_CTX_copy_ex(&dctx->o_ctx, &sctx->o_ctx))
goto err;
if (!EVP_MD_CTX_copy_ex(&dctx->md_ctx, &sctx->md_ctx))
goto err;
memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK);
dctx->key_length = sctx->key_length;
dctx->md = sctx->md;
return 1;
err:
return 0;
} | ['int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)\n{\n HMAC_CTX_init(dctx);\n if (!EVP_MD_CTX_copy_ex(&dctx->i_ctx, &sctx->i_ctx))\n goto err;\n if (!EVP_MD_CTX_copy_ex(&dctx->o_ctx, &sctx->o_ctx))\n goto err;\n if (!EVP_MD_CTX_copy_ex(&dctx->md_ctx, &sctx->md_ctx))\n goto err;\n memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK);\n dctx->key_length = sctx->key_length;\n dctx->md = sctx->md;\n return 1;\n err:\n return 0;\n}'] |
2,272 | 0 | https://github.com/openssl/openssl/blob/92eb4c551d7433ba1e74e77001dab2e256f8a870/crypto/bn/bn_ctx.c/#L355 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,\n\t\tconst ECDSA_SIG *sig, EC_KEY *eckey)\n{\n\tint ret = -1, i;\n\tBN_CTX *ctx;\n\tBIGNUM *order, *u1, *u2, *m, *X;\n\tEC_POINT *point = NULL;\n\tconst EC_GROUP *group;\n\tconst EC_POINT *pub_key;\n\tif (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||\n\t (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL)\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);\n\t\treturn -1;\n\t}\n\tctx = BN_CTX_new();\n\tif (!ctx)\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);\n\t\treturn -1;\n\t}\n\tBN_CTX_start(ctx);\n\torder = BN_CTX_get(ctx);\n\tu1 = BN_CTX_get(ctx);\n\tu2 = BN_CTX_get(ctx);\n\tm = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tif (!X)\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n\t\tgoto err;\n\t}\n\tif (!EC_GROUP_get_order(group, order, ctx))\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);\n\t\tgoto err;\n\t}\n\tif (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||\n\t BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||\n\t BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0)\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);\n\t\tret = 0;\n\t\tgoto err;\n\t}\n\tif (!BN_mod_inverse(u2, sig->s, order, ctx))\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n\t\tgoto err;\n\t}\n\ti = BN_num_bits(order);\n\tif (8 * dgst_len > i)\n\t\tdgst_len = (i + 7)/8;\n\tif (!BN_bin2bn(dgst, dgst_len, m))\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n\t\tgoto err;\n\t}\n\tif ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7)))\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n\t\tgoto err;\n\t}\n\tif (!BN_mod_mul(u1, m, u2, order, ctx))\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n\t\tgoto err;\n\t}\n\tif (!BN_mod_mul(u2, sig->r, u2, order, ctx))\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n\t\tgoto err;\n\t}\n\tif ((point = EC_POINT_new(group)) == NULL)\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t}\n\tif (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx))\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);\n\t\tgoto err;\n\t}\n\tif (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)\n\t{\n\t\tif (!EC_POINT_get_affine_coordinates_GFp(group,\n\t\t\tpoint, X, NULL, ctx))\n\t\t{\n\t\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);\n\t\t\tgoto err;\n\t\t}\n\t}\n\telse\n\t{\n\t\tif (!EC_POINT_get_affine_coordinates_GF2m(group,\n\t\t\tpoint, X, NULL, ctx))\n\t\t{\n\t\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);\n\t\t\tgoto err;\n\t\t}\n\t}\n\tif (!BN_nnmod(u1, X, order, ctx))\n\t{\n\t\tECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n\t\tgoto err;\n\t}\n\tret = (BN_ucmp(u1, sig->r) == 0);\nerr:\n\tBN_CTX_end(ctx);\n\tBN_CTX_free(ctx);\n\tif (point)\n\t\tEC_POINT_free(point);\n\treturn ret;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *rv;\n\tint noinv;\n\trv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n\tif (noinv)\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\treturn rv;\n\t}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx, int *pnoinv)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tif (pnoinv)\n\t\t*pnoinv = 0;\n\tif ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_mod_inverse_no_branch(in, a, n, ctx);\n\t\t}\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tif (!BN_nnmod(B, B, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\tif (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))\n\t\t{\n\t\tint shift;\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(B, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(X))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(X, X, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(X, X)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(B, B, shift)) goto err;\n\t\t\t\t}\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(A, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(Y))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(Y, Y, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(Y, Y)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(A, A, shift)) goto err;\n\t\t\t\t}\n\t\t\tif (BN_ucmp(B, A) >= 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(X, X, Y)) goto err;\n\t\t\t\tif (!BN_usub(B, B, A)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(Y, Y, X)) goto err;\n\t\t\t\tif (!BN_usub(A, A, B)) goto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tBIGNUM *tmp;\n\t\t\tif (BN_num_bits(A) == BN_num_bits(B))\n\t\t\t\t{\n\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t}\n\t\t\telse if (BN_num_bits(A) == BN_num_bits(B) + 1)\n\t\t\t\t{\n\t\t\t\tif (!BN_lshift1(T,B)) goto err;\n\t\t\t\tif (BN_ucmp(A,T) < 0)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_sub(M,A,T)) goto err;\n\t\t\t\t\tif (!BN_add(D,T,B)) goto err;\n\t\t\t\t\tif (BN_ucmp(A,D) < 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,2)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,3)) goto err;\n\t\t\t\t\t\tif (!BN_sub(M,M,B)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\t\t\t}\n\t\t\ttmp=A;\n\t\t\tA=B;\n\t\t\tB=M;\n\t\t\tif (BN_is_one(D))\n\t\t\t\t{\n\t\t\t\tif (!BN_add(tmp,X,Y)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (BN_is_word(D,2))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift1(tmp,X)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (BN_is_word(D,4))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift(tmp,X,2)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (D->top == 1)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_copy(tmp,X)) goto err;\n\t\t\t\t\tif (!BN_mul_word(tmp,D->d[0])) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\t\t\t}\n\t\t\tM=Y;\n\t\t\tY=X;\n\t\t\tX=tmp;\n\t\t\tsign = -sign;\n\t\t\t}\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tif (pnoinv)\n\t\t\t*pnoinv = 1;\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_START,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n\tBN_CTX *ctx)\n\t{\n\tBIGNUM *t;\n\tint ret=0;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(m);\n\tBN_CTX_start(ctx);\n\tif ((t = BN_CTX_get(ctx)) == NULL) goto err;\n\tif (a == b)\n\t\t{ if (!BN_sqr(t,a,ctx)) goto err; }\n\telse\n\t\t{ if (!BN_mul(t,a,b,ctx)) goto err; }\n\tif (!BN_nnmod(r,t,m,ctx)) goto err;\n\tbn_check_top(r);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tint top,al,bl;\n\tBIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tint i;\n#endif\n#ifdef BN_RECURSION\n\tBIGNUM *t=NULL;\n\tint j=0,k;\n#endif\n#ifdef BN_COUNT\n\tfprintf(stderr,"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\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\tBN_CTX_start(ctx);\n\tif ((r == a) || (r == b))\n\t\t{\n\t\tif ((rr = BN_CTX_get(ctx)) == NULL) goto err;\n\t\t}\n\telse\n\t\trr = r;\n\trr->neg=a->neg^b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\ti = al-bl;\n#endif\n#ifdef BN_MUL_COMBA\n\tif (i == 0)\n\t\t{\n# if 0\n\t\tif (al == 4)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,8) == NULL) goto err;\n\t\t\trr->top=8;\n\t\t\tbn_mul_comba4(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n# endif\n\t\tif (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) goto err;\n\t\t\trr->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\t}\n#endif\n#ifdef BN_RECURSION\n\tif ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (i >= -1 && i <= 1)\n\t\t\t{\n\t\t\tif (i >= 0)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)al);\n\t\t\t\t}\n\t\t\tif (i == -1)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)bl);\n\t\t\t\t}\n\t\t\tj = 1<<(j-1);\n\t\t\tassert(j <= al || j <= bl);\n\t\t\tk = j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (t == NULL)\n\t\t\t\tgoto err;\n\t\t\tif (al > j || bl > j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#if 0\n\t\tif (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)b;\n\t\t\tif (bn_wexpand(tmp_bn,al) == NULL) goto err;\n\t\t\ttmp_bn->d[bl]=0;\n\t\t\tbl++;\n\t\t\ti--;\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\tBIGNUM *tmp_bn = (BIGNUM *)a;\n\t\t\tif (bn_wexpand(tmp_bn,bl) == NULL) goto err;\n\t\t\ttmp_bn->d[al]=0;\n\t\t\tal++;\n\t\t\ti++;\n\t\t\t}\n\t\tif (i == 0)\n\t\t\t{\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\tt = BN_CTX_get(ctx);\n\t\t\tif (al == j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#endif\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) goto err;\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_correct_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\tret=1;\nerr:\n\tbn_check_top(r);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}'] |
2,273 | 0 | https://github.com/openssl/openssl/blob/0f3e6045898e9aa5d0249e61c874b1f153ae54fa/crypto/lhash/lhash.c/#L359 | static void contract(LHASH *lh)
{
LHASH_NODE **n,*n1,*np;
np=lh->b[lh->p+lh->pmax-1];
lh->b[lh->p+lh->pmax-1]=NULL;
if (lh->p == 0)
{
n=(LHASH_NODE **)Realloc((char *)lh->b,
(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));
if (n == NULL)
{
lh->error++;
return;
}
lh->num_contract_reallocs++;
lh->num_alloc_nodes/=2;
lh->pmax/=2;
lh->p=lh->pmax-1;
lh->b=n;
}
else
lh->p--;
lh->num_nodes--;
lh->num_contracts++;
n1=lh->b[(int)lh->p];
if (n1 == NULL)
lh->b[(int)lh->p]=np;
else
{
while (n1->next != NULL)
n1=n1->next;
n1->next=np;
}
} | ['static long ssl_ctrl(BIO *b, int cmd, long num, char *ptr)\n\t{\n\tSSL **sslp,*ssl;\n\tBIO_SSL *bs;\n\tBIO *dbio,*bio;\n\tlong ret=1;\n\tbs=(BIO_SSL *)b->ptr;\n\tssl=bs->ssl;\n\tif ((ssl == NULL) && (cmd != BIO_C_SET_SSL))\n\t\treturn(0);\n\tswitch (cmd)\n\t\t{\n\tcase BIO_CTRL_RESET:\n\t\tSSL_shutdown(ssl);\n\t\tif (ssl->handshake_func == ssl->method->ssl_connect)\n\t\t\tSSL_set_connect_state(ssl);\n\t\telse if (ssl->handshake_func == ssl->method->ssl_accept)\n\t\t\tSSL_set_accept_state(ssl);\n\t\tSSL_clear(ssl);\n\t\tif (b->next_bio != NULL)\n\t\t\tret=BIO_ctrl(b->next_bio,cmd,num,ptr);\n\t\telse if (ssl->rbio != NULL)\n\t\t\tret=BIO_ctrl(ssl->rbio,cmd,num,ptr);\n\t\telse\n\t\t\tret=1;\n\t\tbreak;\n\tcase BIO_CTRL_INFO:\n\t\tret=0;\n\t\tbreak;\n\tcase BIO_C_SSL_MODE:\n\t\tif (num)\n\t\t\tSSL_set_connect_state(ssl);\n\t\telse\n\t\t\tSSL_set_accept_state(ssl);\n\t\tbreak;\n\tcase BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:\n\t\tret=bs->renegotiate_timeout;\n\t\tif (num < 60) num=5;\n\t\tbs->renegotiate_timeout=(unsigned long)num;\n\t\tbs->last_time=(unsigned long)time(NULL);\n\t\tbreak;\n\tcase BIO_C_SET_SSL_RENEGOTIATE_BYTES:\n\t\tret=bs->renegotiate_count;\n\t\tif ((long)num >=512)\n\t\t\tbs->renegotiate_count=(unsigned long)num;\n\t\tbreak;\n\tcase BIO_C_GET_SSL_NUM_RENEGOTIATES:\n\t\tret=bs->num_renegotiates;\n\t\tbreak;\n\tcase BIO_C_SET_SSL:\n\t\tif (ssl != NULL)\n\t\t\tssl_free(b);\n\t\tb->shutdown=(int)num;\n\t\tssl=(SSL *)ptr;\n\t\t((BIO_SSL *)b->ptr)->ssl=ssl;\n\t\tbio=SSL_get_rbio(ssl);\n\t\tif (bio != NULL)\n\t\t\t{\n\t\t\tif (b->next_bio != NULL)\n\t\t\t\tBIO_push(bio,b->next_bio);\n\t\t\tb->next_bio=bio;\n\t\t\tCRYPTO_add(&bio->references,1,CRYPTO_LOCK_BIO);\n\t\t\t}\n\t\tb->init=1;\n\t\tbreak;\n\tcase BIO_C_GET_SSL:\n\t\tif (ptr != NULL)\n\t\t\t{\n\t\t\tsslp=(SSL **)ptr;\n\t\t\t*sslp=ssl;\n\t\t\t}\n\t\telse\n\t\t\tret=0;\n\t\tbreak;\n\tcase BIO_CTRL_GET_CLOSE:\n\t\tret=b->shutdown;\n\t\tbreak;\n\tcase BIO_CTRL_SET_CLOSE:\n\t\tb->shutdown=(int)num;\n\t\tbreak;\n\tcase BIO_CTRL_WPENDING:\n\t\tret=BIO_ctrl(ssl->wbio,cmd,num,ptr);\n\t\tbreak;\n\tcase BIO_CTRL_PENDING:\n\t\tret=SSL_pending(ssl);\n\t\tif (ret == 0)\n\t\t\tret=BIO_pending(ssl->rbio);\n\t\tbreak;\n\tcase BIO_CTRL_FLUSH:\n\t\tBIO_clear_retry_flags(b);\n\t\tret=BIO_ctrl(ssl->wbio,cmd,num,ptr);\n\t\tBIO_copy_next_retry(b);\n\t\tbreak;\n\tcase BIO_CTRL_PUSH:\n\t\tif ((b->next_bio != NULL) && (b->next_bio != ssl->rbio))\n\t\t\t{\n\t\t\tSSL_set_bio(ssl,b->next_bio,b->next_bio);\n\t\t\tCRYPTO_add(&b->next_bio->references,1,CRYPTO_LOCK_BIO);\n\t\t\t}\n\t\tbreak;\n\tcase BIO_CTRL_POP:\n\t\tif (ssl->rbio != ssl->wbio)\n\t\t\t{\n\t\t\tBIO_free_all(ssl->wbio);\n\t\t\t}\n\t\tssl->wbio=NULL;\n\t\tssl->rbio=NULL;\n\t\tbreak;\n\tcase BIO_C_DO_STATE_MACHINE:\n\t\tBIO_clear_retry_flags(b);\n\t\tb->retry_reason=0;\n\t\tret=(int)SSL_do_handshake(ssl);\n\t\tswitch (SSL_get_error(ssl,(int)ret))\n\t\t\t{\n\t\tcase SSL_ERROR_WANT_READ:\n\t\t\tBIO_set_flags(b,\n\t\t\t\tBIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);\n\t\t\tbreak;\n\t\tcase SSL_ERROR_WANT_WRITE:\n\t\t\tBIO_set_flags(b,\n\t\t\t\tBIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY);\n\t\t\tbreak;\n\t\tcase SSL_ERROR_WANT_CONNECT:\n\t\t\tBIO_set_flags(b,\n\t\t\t\tBIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY);\n\t\t\tb->retry_reason=b->next_bio->retry_reason;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tbreak;\n\t\t\t}\n\t\tbreak;\n\tcase BIO_CTRL_DUP:\n\t\tdbio=(BIO *)ptr;\n\t\tif (((BIO_SSL *)dbio->ptr)->ssl != NULL)\n\t\t\tSSL_free(((BIO_SSL *)dbio->ptr)->ssl);\n\t\t((BIO_SSL *)dbio->ptr)->ssl=SSL_dup(ssl);\n\t\t((BIO_SSL *)dbio->ptr)->renegotiate_count=\n\t\t\t((BIO_SSL *)b->ptr)->renegotiate_count;\n\t\t((BIO_SSL *)dbio->ptr)->byte_count=\n\t\t\t((BIO_SSL *)b->ptr)->byte_count;\n\t\t((BIO_SSL *)dbio->ptr)->renegotiate_timeout=\n\t\t\t((BIO_SSL *)b->ptr)->renegotiate_timeout;\n\t\t((BIO_SSL *)dbio->ptr)->last_time=\n\t\t\t((BIO_SSL *)b->ptr)->last_time;\n\t\tret=(((BIO_SSL *)dbio->ptr)->ssl != NULL);\n\t\tbreak;\n\tcase BIO_C_GET_FD:\n\t\tret=BIO_ctrl(ssl->rbio,cmd,num,ptr);\n\t\tbreak;\n\tcase BIO_CTRL_SET_CALLBACK:\n\t\tSSL_set_info_callback(ssl,(void (*)())ptr);\n\t\tbreak;\n\tcase BIO_CTRL_GET_CALLBACK:\n\t\t{\n\t\tvoid (**fptr)();\n\t\tfptr=(void (**)())ptr;\n\t\t*fptr=SSL_get_info_callback(ssl);\n\t\t}\n\t\tbreak;\n\tdefault:\n\t\tret=BIO_ctrl(ssl->rbio,cmd,num,ptr);\n\t\tbreak;\n\t\t}\n\treturn(ret);\n\t}', 'int SSL_clear(SSL *s)\n\t{\n\tint state;\n\tif (s->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);\n\t\treturn(0);\n\t\t}\n\ts->error=0;\n\ts->hit=0;\n\ts->shutdown=0;\n#if 0\n\tif (s->new_session) return(1);\n#endif\n\tstate=s->state;\n\ts->type=0;\n\ts->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);\n\ts->version=s->method->version;\n\ts->client_version=s->version;\n\ts->rwstate=SSL_NOTHING;\n\ts->rstate=SSL_ST_READ_HEADER;\n\ts->read_ahead=s->ctx->read_ahead;\n\tif (s->init_buf != NULL)\n\t\t{\n\t\tBUF_MEM_free(s->init_buf);\n\t\ts->init_buf=NULL;\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tif (ssl_clear_bad_session(s))\n\t\t{\n\t\tSSL_SESSION_free(s->session);\n\t\ts->session=NULL;\n\t\t}\n\ts->first_packet=0;\n#if 1\n\tif ((s->session == NULL) && (s->method != s->ctx->method))\n\t\t{\n\t\ts->method->ssl_free(s);\n\t\ts->method=s->ctx->method;\n\t\tif (!s->method->ssl_new(s))\n\t\t\treturn(0);\n\t\t}\n\telse\n#endif\n\t\ts->method->ssl_clear(s);\n\treturn(1);\n\t}', 'int ssl_clear_bad_session(SSL *s)\n\t{\n\tif (\t(s->session != NULL) &&\n\t\t!(s->shutdown & SSL_SENT_SHUTDOWN) &&\n\t\t!(SSL_in_init(s) || SSL_in_before(s)))\n\t\t{\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\treturn(1);\n\t\t}\n\telse\n\t\treturn(0);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,(char *)c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'char *lh_delete(LHASH *lh, char *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tchar *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree((char *)nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}', 'static void contract(LHASH *lh)\n\t{\n\tLHASH_NODE **n,*n1,*np;\n\tnp=lh->b[lh->p+lh->pmax-1];\n\tlh->b[lh->p+lh->pmax-1]=NULL;\n\tif (lh->p == 0)\n\t\t{\n\t\tn=(LHASH_NODE **)Realloc((char *)lh->b,\n\t\t\t(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));\n\t\tif (n == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn;\n\t\t\t}\n\t\tlh->num_contract_reallocs++;\n\t\tlh->num_alloc_nodes/=2;\n\t\tlh->pmax/=2;\n\t\tlh->p=lh->pmax-1;\n\t\tlh->b=n;\n\t\t}\n\telse\n\t\tlh->p--;\n\tlh->num_nodes--;\n\tlh->num_contracts++;\n\tn1=lh->b[(int)lh->p];\n\tif (n1 == NULL)\n\t\tlh->b[(int)lh->p]=np;\n\telse\n\t\t{\n\t\twhile (n1->next != NULL)\n\t\t\tn1=n1->next;\n\t\tn1->next=np;\n\t\t}\n\t}'] |
2,274 | 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 rsa_ossl_public_encrypt(int flen, const unsigned char *from,\n unsigned char *to, RSA *rsa, int padding)\n{\n BIGNUM *f, *ret;\n int i, num = 0, r = -1;\n unsigned char *buf = NULL;\n BN_CTX *ctx = NULL;\n if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);\n return -1;\n }\n if (BN_ucmp(rsa->n, rsa->e) <= 0) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);\n return -1;\n }\n if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {\n if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);\n return -1;\n }\n }\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n f = BN_CTX_get(ctx);\n ret = BN_CTX_get(ctx);\n num = BN_num_bytes(rsa->n);\n buf = OPENSSL_malloc(num);\n if (ret == NULL || buf == NULL) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n switch (padding) {\n case RSA_PKCS1_PADDING:\n i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);\n break;\n case RSA_PKCS1_OAEP_PADDING:\n i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);\n break;\n case RSA_SSLV23_PADDING:\n i = RSA_padding_add_SSLv23(buf, num, from, flen);\n break;\n case RSA_NO_PADDING:\n i = RSA_padding_add_none(buf, num, from, flen);\n break;\n default:\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);\n goto err;\n }\n if (i <= 0)\n goto err;\n if (BN_bin2bn(buf, num, f) == NULL)\n goto err;\n if (BN_ucmp(f, rsa->n) >= 0) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT,\n RSA_R_DATA_TOO_LARGE_FOR_MODULUS);\n goto err;\n }\n if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n if (!BN_MONT_CTX_set_locked\n (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))\n goto err;\n if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n r = BN_bn2binpad(ret, to, num);\n err:\n if (ctx != NULL)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n OPENSSL_clear_free(buf, num);\n return r;\n}', '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}', 'BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,\n const BIGNUM *mod, BN_CTX *ctx)\n{\n BN_MONT_CTX *ret;\n CRYPTO_THREAD_read_lock(lock);\n ret = *pmont;\n CRYPTO_THREAD_unlock(lock);\n if (ret)\n return ret;\n ret = BN_MONT_CTX_new();\n if (ret == NULL)\n return NULL;\n if (!BN_MONT_CTX_set(ret, mod, ctx)) {\n BN_MONT_CTX_free(ret);\n return NULL;\n }\n CRYPTO_THREAD_write_lock(lock);\n if (*pmont) {\n BN_MONT_CTX_free(ret);\n ret = *pmont;\n } else\n *pmont = ret;\n CRYPTO_THREAD_unlock(lock);\n return ret;\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}'] |
2,275 | 0 | https://github.com/openssl/openssl/blob/7d461736f7bd3af3c2f266f8541034ecf6f41ed9/crypto/bn/bn_lib.c/#L322 | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
bn_check_top(b);
if (a == b)
return a;
if (bn_wexpand(a, b->top) == NULL)
return NULL;
if (b->top > 0)
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
a->top = b->top;
a->neg = b->neg;
bn_check_top(a);
return a;
} | ['int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x,\n const BIGNUM *y, BN_CTX *ctx)\n{\n int ret = 0;\n if (x == NULL || y == NULL) {\n ECerr(EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES,\n ERR_R_PASSED_NULL_PARAMETER);\n return 0;\n }\n if (!BN_copy(point->X, x))\n goto err;\n BN_set_negative(point->X, 0);\n if (!BN_copy(point->Y, y))\n goto err;\n BN_set_negative(point->Y, 0);\n if (!BN_copy(point->Z, BN_value_one()))\n goto err;\n BN_set_negative(point->Z, 0);\n point->Z_is_one = 1;\n ret = 1;\n err:\n return ret;\n}', 'const BIGNUM *BN_value_one(void)\n{\n static const BN_ULONG data_one = 1L;\n static const BIGNUM const_one =\n { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };\n return &const_one;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n 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}'] |
2,276 | 0 | https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_lib.c/#L291 | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
bn_check_top(b);
if (a == b)
return a;
if (bn_wexpand(a, b->top) == NULL)
return NULL;
if (b->top > 0)
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
a->neg = b->neg;
a->top = b->top;
a->flags |= b->flags & BN_FLG_FIXED_TOP;
bn_check_top(a);
return a;
} | ['int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)\n{\n BIGNUM *i, *j, *k, *l, *m;\n BN_CTX *ctx;\n int ret = 1, ex_primes = 0, idx;\n RSA_PRIME_INFO *pinfo;\n if (key->p == NULL || key->q == NULL || key->n == NULL\n || key->e == NULL || key->d == NULL) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);\n return 0;\n }\n if (key->version == RSA_ASN1_VERSION_MULTI) {\n ex_primes = sk_RSA_PRIME_INFO_num(key->prime_infos);\n if (ex_primes <= 0\n || (ex_primes + 2) > rsa_multip_cap(BN_num_bits(key->n))) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_INVALID_MULTI_PRIME_KEY);\n return 0;\n }\n }\n i = BN_new();\n j = BN_new();\n k = BN_new();\n l = BN_new();\n m = BN_new();\n ctx = BN_CTX_new();\n if (i == NULL || j == NULL || k == NULL || l == NULL\n || m == NULL || ctx == NULL) {\n ret = -1;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (BN_is_one(key->e)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);\n }\n if (!BN_is_odd(key->e)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);\n }\n if (BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);\n }\n if (BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (BN_is_prime_ex(pinfo->r, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_R_NOT_PRIME);\n }\n }\n if (!BN_mul(i, key->p, key->q, ctx)) {\n ret = -1;\n goto err;\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_mul(i, i, pinfo->r, ctx)) {\n ret = -1;\n goto err;\n }\n }\n if (BN_cmp(i, key->n) != 0) {\n ret = 0;\n if (ex_primes)\n RSAerr(RSA_F_RSA_CHECK_KEY_EX,\n RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES);\n else\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);\n }\n if (!BN_sub(i, key->p, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_sub(j, key->q, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mul(l, i, j, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_gcd(m, i, j, ctx)) {\n ret = -1;\n goto err;\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_sub(k, pinfo->r, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mul(l, l, k, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_gcd(m, m, k, ctx)) {\n ret = -1;\n goto err;\n }\n }\n if (!BN_div(k, NULL, l, m, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_mod_mul(i, key->d, key->e, k, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_is_one(i)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);\n }\n if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {\n if (!BN_sub(i, key->p, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmp1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_sub(i, key->q, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmq1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, key->q, key->p, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, key->iqmp) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);\n }\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_sub(i, pinfo->r, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, pinfo->d) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, pinfo->pp, pinfo->r, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, pinfo->t) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R);\n }\n }\n err:\n BN_free(i);\n BN_free(j);\n BN_free(k);\n BN_free(l);\n BN_free(m);\n BN_CTX_free(ctx);\n return ret;\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n BN_ULONG t1, t2, borrow, *rp;\n const BN_ULONG *ap, *bp;\n bn_check_top(a);\n bn_check_top(b);\n max = a->top;\n min = b->top;\n dif = max - min;\n if (dif < 0) {\n BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n return 0;\n }\n if (bn_wexpand(r, max) == NULL)\n return 0;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n borrow = bn_sub_words(rp, ap, bp, min);\n ap += min;\n rp += min;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 - borrow) & BN_MASK2;\n *(rp++) = t2;\n borrow &= (t1 == 0);\n }\n while (max && *--rp == 0)\n max--;\n r->top = max;\n r->neg = 0;\n bn_pollute(r);\n return 1;\n}', 'int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)\n{\n BIGNUM *a, *b, *t;\n int ret = 0;\n bn_check_top(in_a);\n bn_check_top(in_b);\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (b == NULL)\n goto err;\n if (BN_copy(a, in_a) == NULL)\n goto err;\n if (BN_copy(b, in_b) == NULL)\n goto err;\n a->neg = 0;\n b->neg = 0;\n if (BN_cmp(a, b) < 0) {\n t = a;\n a = b;\n b = t;\n }\n t = euclid(a, b);\n if (t == NULL)\n goto err;\n if (BN_copy(r, t) == NULL)\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->neg = b->neg;\n a->top = b->top;\n a->flags |= b->flags & BN_FLG_FIXED_TOP;\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}'] |
2,277 | 0 | https://github.com/openssl/openssl/blob/64c3da230f557e85422f76c9e3c45fac9b16466c/crypto/bn/bn_print.c/#L197 | 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; isxdigit((unsigned char) a[i]); i++)
;
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];
if ((c >= '0') && (c <= '9')) k=c-'0';
else if ((c >= 'a') && (c <= 'f')) k=c-'a'+10;
else if ((c >= 'A') && (c <= 'F')) k=c-'A'+10;
else k=0;
l=(l<<4)|k;
if (--m <= 0)
{
ret->d[h++]=l;
break;
}
}
j-=(BN_BYTES*2);
}
ret->top=h;
bn_fix_top(ret);
ret->neg=neg;
*bn=ret;
return(num);
err:
if (*bn == NULL) BN_free(ret);
return(0);
} | ['EC_GROUP *EC_GROUP_new_by_name(int name)\n\t{\n\tEC_GROUP *ret = NULL;\n\tswitch (name)\n\t\t{\n\tcase EC_GROUP_NO_CURVE:\n\t\treturn NULL;\n\tcase EC_GROUP_SECG_PRIME_112R1:\n\tcase EC_GROUP_WTLS_6:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_112R1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_112R2:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_112R2);\n\t\tbreak;\n\tcase EC_GROUP_WTLS_8:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_WTLS_8);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_128R1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_128R1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_128R2:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_128R2);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_160K1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_160K1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_160R1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_160R1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_160R2:\n\tcase EC_GROUP_WTLS_7:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_160R2);\n\t\tbreak;\n\tcase EC_GROUP_WTLS_9:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_WTLS_9);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_192K1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_192K1);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_PRIME_192V1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_NIST_PRIME_192);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_PRIME_192V2:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_X9_62_PRIME_192V2);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_PRIME_192V3:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_X9_62_PRIME_192V3);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_224K1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_224K1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_224R1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_NIST_PRIME_224);\n\t\tbreak;\n\tcase EC_GROUP_WTLS_12:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_WTLS_12);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_PRIME_239V1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_X9_62_PRIME_239V1);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_PRIME_239V2:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_X9_62_PRIME_239V2);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_PRIME_239V3:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_X9_62_PRIME_239V3);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_256K1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_256K1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_256R1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_X9_62_PRIME_256V1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_384R1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_NIST_PRIME_384);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_521R1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_NIST_PRIME_521);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_113R1:\n\tcase EC_GROUP_WTLS_4:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_113R1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_113R2:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_113R2);\n\t\tbreak;\n\tcase EC_GROUP_WTLS_1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_WTLS_1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_131R1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_131R1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_131R2:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_131R2);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_163K1:\n\tcase EC_GROUP_WTLS_3:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_163K1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_163R1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_163R1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_163R2:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_163R2);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_163V1:\n\tcase EC_GROUP_WTLS_5:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_163V1);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_163V2:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_163V2);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_163V3:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_163V3);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_176V1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_176V1);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_191V1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_191V1);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_191V2:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_191V2);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_191V3:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_191V3);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_193R1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_193R1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_193R2:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_193R2);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_208W1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_208W1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_233K1:\n\tcase EC_GROUP_WTLS_10:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_233K1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_233R1:\n\tcase EC_GROUP_WTLS_11:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_233R1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_239K1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_239K1);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_239V1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_239V1);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_239V2:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_239V2);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_239V3:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_239V3);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_272W1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_272W1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_283K1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_283K1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_283R1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_283R1);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_304W1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_304W1);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_359V1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_359V1);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_368W1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_368W1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_409K1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_409K1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_409R1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_409R1);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_CHAR2_431R1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_X9_62_CHAR2_431R1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_571K1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_571K1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_CHAR2_571R1:\n\t\tret = ec_group_new_GF2m_from_hex(_EC_GROUP_SECG_CHAR2_571R1);\n\t\tbreak;\n\t\t}\n\tif (ret == NULL)\n\t\t{\n\t\tECerr(EC_F_EC_GROUP_NEW_BY_NAME, EC_R_UNKNOWN_GROUP);\n\t\treturn NULL;\n\t\t}\n\tEC_GROUP_set_nid(ret, name);\n\treturn ret;\n\t}', 'static EC_GROUP *ec_group_new_GFp_from_hex(const char *prime_in,\n\t const char *a_in, const char *b_in,\n\t const char *x_in, const int y_bit, const char *order_in, const BN_ULONG cofac_in)\n\t{\n\tEC_GROUP *group=NULL;\n\tEC_POINT *P=NULL;\n\tBN_CTX\t *ctx=NULL;\n\tBIGNUM \t *prime=NULL,*a=NULL,*b=NULL,*x=NULL,*order=NULL;\n\tint\t ok=0;\n\tif ((ctx = BN_CTX_new()) == NULL) goto bn_err;\n\tif ((prime = BN_new()) == NULL || (a = BN_new()) == NULL || (b = BN_new()) == NULL ||\n\t\t(x = BN_new()) == NULL || (order = BN_new()) == NULL) goto bn_err;\n\tif (!BN_hex2bn(&prime, prime_in)) goto bn_err;\n\tif (!BN_hex2bn(&a, a_in)) goto bn_err;\n\tif (!BN_hex2bn(&b, b_in)) goto bn_err;\n\tif ((group = EC_GROUP_new_curve_GFp(prime, a, b, ctx)) == NULL) goto err;\n\tif ((P = EC_POINT_new(group)) == NULL) goto err;\n\tif (!BN_hex2bn(&x, x_in)) goto bn_err;\n\tif (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, y_bit, ctx)) goto err;\n\tif (!BN_hex2bn(&order, order_in)) goto bn_err;\n\tif (!BN_set_word(x, cofac_in)) goto bn_err;\n\tif (!EC_GROUP_set_generator(group, P, order, x)) goto err;\n\tok=1;\nbn_err:\n\tif (!ok)\n\t\tECerr(EC_F_EC_GROUP_NEW_GFP_FROM_HEX, ERR_R_BN_LIB);\nerr:\n\tif (!ok)\n\t\t{\n\t\tEC_GROUP_free(group);\n\t\tgroup = NULL;\n\t\t}\n\tif (P) \t EC_POINT_free(P);\n\tif (ctx) BN_CTX_free(ctx);\n\tif (prime) BN_free(prime);\n\tif (a) BN_free(a);\n\tif (b) BN_free(b);\n\tif (order) BN_free(order);\n\tif (x) BN_free(x);\n\treturn(group);\n\t}', "int BN_hex2bn(BIGNUM **bn, const char *a)\n\t{\n\tBIGNUM *ret=NULL;\n\tBN_ULONG l=0;\n\tint neg=0,h,m,i,j,k,c;\n\tint num;\n\tif ((a == NULL) || (*a == '\\0')) return(0);\n\tif (*a == '-') { neg=1; a++; }\n\tfor (i=0; isxdigit((unsigned char) a[i]); i++)\n\t\t;\n\tnum=i+neg;\n\tif (bn == NULL) return(num);\n\tif (*bn == NULL)\n\t\t{\n\t\tif ((ret=BN_new()) == NULL) return(0);\n\t\t}\n\telse\n\t\t{\n\t\tret= *bn;\n\t\tBN_zero(ret);\n\t\t}\n\tif (bn_expand(ret,i*4) == NULL) goto err;\n\tj=i;\n\tm=0;\n\th=0;\n\twhile (j > 0)\n\t\t{\n\t\tm=((BN_BYTES*2) <= j)?(BN_BYTES*2):j;\n\t\tl=0;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tc=a[j-m];\n\t\t\tif ((c >= '0') && (c <= '9')) k=c-'0';\n\t\t\telse if ((c >= 'a') && (c <= 'f')) k=c-'a'+10;\n\t\t\telse if ((c >= 'A') && (c <= 'F')) k=c-'A'+10;\n\t\t\telse k=0;\n\t\t\tl=(l<<4)|k;\n\t\t\tif (--m <= 0)\n\t\t\t\t{\n\t\t\t\tret->d[h++]=l;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tj-=(BN_BYTES*2);\n\t\t}\n\tret->top=h;\n\tbn_fix_top(ret);\n\tret->neg=neg;\n\t*bn=ret;\n\treturn(num);\nerr:\n\tif (*bn == NULL) BN_free(ret);\n\treturn(0);\n\t}"] |
2,278 | 0 | https://github.com/openssl/openssl/blob/9639515871c73722de3fff04d3c50d54aa6b1477/apps/x509.c/#L1059 | static int sign(X509 *x, EVP_PKEY *pkey, int days, const EVP_MD *digest,
LHASH *conf, char *section)
{
EVP_PKEY *pktmp;
pktmp = X509_get_pubkey(x);
EVP_PKEY_copy_parameters(pktmp,pkey);
EVP_PKEY_save_parameters(pktmp,1);
EVP_PKEY_free(pktmp);
if (!X509_set_issuer_name(x,X509_get_subject_name(x))) goto err;
if (X509_gmtime_adj(X509_get_notBefore(x),0) == NULL) goto err;
if (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)
goto err;
if (!X509_set_pubkey(x,pkey)) goto err;
if(conf) {
X509V3_CTX ctx;
X509_set_version(x,2);
X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
X509V3_set_conf_lhash(&ctx, conf);
if(!X509V3_EXT_add_conf(conf, &ctx, section, x)) goto err;
}
if (!X509_sign(x,pkey,digest)) goto err;
return(1);
err:
ERR_print_errors(bio_err);
return(0);
} | ['static int sign(X509 *x, EVP_PKEY *pkey, int days, const EVP_MD *digest,\n\t\t\t\t\t\tLHASH *conf, char *section)\n\t{\n\tEVP_PKEY *pktmp;\n\tpktmp = X509_get_pubkey(x);\n\tEVP_PKEY_copy_parameters(pktmp,pkey);\n\tEVP_PKEY_save_parameters(pktmp,1);\n\tEVP_PKEY_free(pktmp);\n\tif (!X509_set_issuer_name(x,X509_get_subject_name(x))) goto err;\n\tif (X509_gmtime_adj(X509_get_notBefore(x),0) == NULL) goto err;\n\tif (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)\n\t\tgoto err;\n\tif (!X509_set_pubkey(x,pkey)) goto err;\n\tif(conf) {\n\t\tX509V3_CTX ctx;\n\t\tX509_set_version(x,2);\n X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);\n X509V3_set_conf_lhash(&ctx, conf);\n if(!X509V3_EXT_add_conf(conf, &ctx, section, x)) goto err;\n\t}\n\tif (!X509_sign(x,pkey,digest)) goto err;\n\treturn(1);\nerr:\n\tERR_print_errors(bio_err);\n\treturn(0);\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}'] |
2,279 | 0 | https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/test/bntest.c/#L498 | int test_div(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_zero(b);
if (BN_div(d, c, a, b, ctx)) {
fprintf(stderr, "Division by zero succeeded!\n");
return 0;
}
for (i = 0; i < num0 + num1; i++) {
if (i < num1) {
BN_bntest_rand(a, 400, 0, 0);
BN_copy(b, a);
BN_lshift(a, a, i);
BN_add_word(a, i);
} else
BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);
a->neg = rand_neg();
b->neg = rand_neg();
BN_div(d, c, a, b, ctx);
if (bp != NULL) {
if (!results) {
BN_print(bp, a);
BIO_puts(bp, " / ");
BN_print(bp, b);
BIO_puts(bp, " - ");
}
BN_print(bp, d);
BIO_puts(bp, "\n");
if (!results) {
BN_print(bp, a);
BIO_puts(bp, " % ");
BN_print(bp, b);
BIO_puts(bp, " - ");
}
BN_print(bp, c);
BIO_puts(bp, "\n");
}
BN_mul(e, d, b, ctx);
BN_add(d, e, c);
BN_sub(d, d, a);
if (!BN_is_zero(d)) {
fprintf(stderr, "Division test failed!\n");
return 0;
}
}
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
BN_free(e);
return (1);
} | ['int test_div(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_zero(b);\n if (BN_div(d, c, a, b, ctx)) {\n fprintf(stderr, "Division by zero succeeded!\\n");\n return 0;\n }\n for (i = 0; i < num0 + num1; i++) {\n if (i < num1) {\n BN_bntest_rand(a, 400, 0, 0);\n BN_copy(b, a);\n BN_lshift(a, a, i);\n BN_add_word(a, i);\n } else\n BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);\n a->neg = rand_neg();\n b->neg = rand_neg();\n BN_div(d, c, a, b, ctx);\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 }\n BN_print(bp, d);\n BIO_puts(bp, "\\n");\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " % ");\n BN_print(bp, b);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, c);\n BIO_puts(bp, "\\n");\n }\n BN_mul(e, d, b, ctx);\n BN_add(d, e, c);\n BN_sub(d, d, a);\n if (!BN_is_zero(d)) {\n fprintf(stderr, "Division test failed!\\n");\n return 0;\n }\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}', 'void BN_free(BIGNUM *a)\n{\n if (a == NULL)\n return;\n bn_check_top(a);\n if (!BN_get_flags(a, BN_FLG_STATIC_DATA))\n bn_free_d(a);\n if (a->flags & BN_FLG_MALLOCED)\n OPENSSL_free(a);\n else {\n#if OPENSSL_API_COMPAT < 0x00908000L\n a->flags |= BN_FLG_FREE;\n#endif\n a->d = NULL;\n }\n}', 'int BN_get_flags(const BIGNUM *b, int n)\n{\n return b->flags & n;\n}'] |
2,280 | 0 | https://github.com/libav/libav/blob/e3ec6fe7bb2a622a863e3912181717a659eb1bad/libavcodec/h264_refs.c/#L152 | int ff_h264_fill_default_ref_list(H264Context *h, H264SliceContext *sl)
{
int i, len;
if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
H264Picture *sorted[32];
int cur_poc, list;
int lens[2];
if (FIELD_PICTURE(h))
cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD];
else
cur_poc = h->cur_pic_ptr->poc;
for (list = 0; list < 2; list++) {
len = add_sorted(sorted, h->short_ref, h->short_ref_count, cur_poc, 1 ^ list);
len += add_sorted(sorted + len, h->short_ref, h->short_ref_count, cur_poc, 0 ^ list);
assert(len <= 32);
len = build_def_list(h->default_ref_list[list], FF_ARRAY_ELEMS(h->default_ref_list[0]),
sorted, len, 0, h->picture_structure);
len += build_def_list(h->default_ref_list[list] + len,
FF_ARRAY_ELEMS(h->default_ref_list[0]) - len,
h->long_ref, 16, 1, h->picture_structure);
if (len < sl->ref_count[list])
memset(&h->default_ref_list[list][len], 0, sizeof(H264Ref) * (sl->ref_count[list] - len));
lens[list] = len;
}
if (lens[0] == lens[1] && lens[1] > 1) {
for (i = 0; i < lens[0] &&
h->default_ref_list[0][i].parent->f.buf[0]->buffer ==
h->default_ref_list[1][i].parent->f.buf[0]->buffer; i++);
if (i == lens[0]) {
FFSWAP(H264Ref, h->default_ref_list[1][0], h->default_ref_list[1][1]);
}
}
} else {
len = build_def_list(h->default_ref_list[0], FF_ARRAY_ELEMS(h->default_ref_list[0]),
h->short_ref, h->short_ref_count, 0, h->picture_structure);
len += build_def_list(h->default_ref_list[0] + len,
FF_ARRAY_ELEMS(h->default_ref_list[0]) - len,
h-> long_ref, 16, 1, h->picture_structure);
if (len < sl->ref_count[0])
memset(&h->default_ref_list[0][len], 0, sizeof(H264Ref) * (sl->ref_count[0] - len));
}
#ifdef TRACE
for (i = 0; i < sl->ref_count[0]; i++) {
tprintf(h->avctx, "List0: %s fn:%d 0x%p\n",
(h->default_ref_list[0][i].long_ref ? "LT" : "ST"),
h->default_ref_list[0][i].pic_id,
h->default_ref_list[0][i].f.data[0]);
}
if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
for (i = 0; i < sl->ref_count[1]; i++) {
tprintf(h->avctx, "List1: %s fn:%d 0x%p\n",
(h->default_ref_list[1][i].long_ref ? "LT" : "ST"),
h->default_ref_list[1][i].pic_id,
h->default_ref_list[1][i].f.data[0]);
}
}
#endif
return 0;
} | ['int ff_h264_fill_default_ref_list(H264Context *h, H264SliceContext *sl)\n{\n int i, len;\n if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {\n H264Picture *sorted[32];\n int cur_poc, list;\n int lens[2];\n if (FIELD_PICTURE(h))\n cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD];\n else\n cur_poc = h->cur_pic_ptr->poc;\n for (list = 0; list < 2; list++) {\n len = add_sorted(sorted, h->short_ref, h->short_ref_count, cur_poc, 1 ^ list);\n len += add_sorted(sorted + len, h->short_ref, h->short_ref_count, cur_poc, 0 ^ list);\n assert(len <= 32);\n len = build_def_list(h->default_ref_list[list], FF_ARRAY_ELEMS(h->default_ref_list[0]),\n sorted, len, 0, h->picture_structure);\n len += build_def_list(h->default_ref_list[list] + len,\n FF_ARRAY_ELEMS(h->default_ref_list[0]) - len,\n h->long_ref, 16, 1, h->picture_structure);\n if (len < sl->ref_count[list])\n memset(&h->default_ref_list[list][len], 0, sizeof(H264Ref) * (sl->ref_count[list] - len));\n lens[list] = len;\n }\n if (lens[0] == lens[1] && lens[1] > 1) {\n for (i = 0; i < lens[0] &&\n h->default_ref_list[0][i].parent->f.buf[0]->buffer ==\n h->default_ref_list[1][i].parent->f.buf[0]->buffer; i++);\n if (i == lens[0]) {\n FFSWAP(H264Ref, h->default_ref_list[1][0], h->default_ref_list[1][1]);\n }\n }\n } else {\n len = build_def_list(h->default_ref_list[0], FF_ARRAY_ELEMS(h->default_ref_list[0]),\n h->short_ref, h->short_ref_count, 0, h->picture_structure);\n len += build_def_list(h->default_ref_list[0] + len,\n FF_ARRAY_ELEMS(h->default_ref_list[0]) - len,\n h-> long_ref, 16, 1, h->picture_structure);\n if (len < sl->ref_count[0])\n memset(&h->default_ref_list[0][len], 0, sizeof(H264Ref) * (sl->ref_count[0] - len));\n }\n#ifdef TRACE\n for (i = 0; i < sl->ref_count[0]; i++) {\n tprintf(h->avctx, "List0: %s fn:%d 0x%p\\n",\n (h->default_ref_list[0][i].long_ref ? "LT" : "ST"),\n h->default_ref_list[0][i].pic_id,\n h->default_ref_list[0][i].f.data[0]);\n }\n if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {\n for (i = 0; i < sl->ref_count[1]; i++) {\n tprintf(h->avctx, "List1: %s fn:%d 0x%p\\n",\n (h->default_ref_list[1][i].long_ref ? "LT" : "ST"),\n h->default_ref_list[1][i].pic_id,\n h->default_ref_list[1][i].f.data[0]);\n }\n }\n#endif\n return 0;\n}'] |
2,281 | 0 | https://github.com/nginx/nginx/blob/8e9f1df637160ec20990601976e140b8d3cffc0b/src/core/ngx_file.c/#L248 | void
ngx_create_hashed_filename(ngx_path_t *path, u_char *file, size_t len)
{
size_t i, level;
ngx_uint_t n;
i = path->name.len + 1;
file[path->name.len + path->len] = '/';
for (n = 0; n < NGX_MAX_PATH_LEVEL; n++) {
level = path->level[n];
if (level == 0) {
break;
}
len -= level;
file[i - 1] = '/';
ngx_memcpy(&file[i], &file[len], level);
i += level + 1;
}
} | ['ngx_int_t\nngx_create_temp_file(ngx_file_t *file, ngx_path_t *path, ngx_pool_t *pool,\n ngx_uint_t persistent, ngx_uint_t clean, ngx_uint_t access)\n{\n size_t levels;\n u_char *p;\n uint32_t n;\n ngx_err_t err;\n ngx_str_t name;\n ngx_uint_t prefix;\n ngx_pool_cleanup_t *cln;\n ngx_pool_cleanup_file_t *clnf;\n if (file->name.len) {\n name = file->name;\n levels = 0;\n prefix = 1;\n } else {\n name = path->name;\n levels = path->len;\n prefix = 0;\n }\n file->name.len = name.len + 1 + levels + 10;\n file->name.data = ngx_pnalloc(pool, file->name.len + 1);\n if (file->name.data == NULL) {\n return NGX_ERROR;\n }\n#if 0\n for (i = 0; i < file->name.len; i++) {\n file->name.data[i] = \'X\';\n }\n#endif\n p = ngx_cpymem(file->name.data, name.data, name.len);\n if (prefix) {\n *p = \'.\';\n }\n p += 1 + levels;\n n = (uint32_t) ngx_next_temp_number(0);\n cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));\n if (cln == NULL) {\n return NGX_ERROR;\n }\n for ( ;; ) {\n (void) ngx_sprintf(p, "%010uD%Z", n);\n if (!prefix) {\n ngx_create_hashed_filename(path, file->name.data, file->name.len);\n }\n ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,\n "hashed path: %s", file->name.data);\n file->fd = ngx_open_tempfile(file->name.data, persistent, access);\n ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,\n "temp fd:%d", file->fd);\n if (file->fd != NGX_INVALID_FILE) {\n cln->handler = clean ? ngx_pool_delete_file : ngx_pool_cleanup_file;\n clnf = cln->data;\n clnf->fd = file->fd;\n clnf->name = file->name.data;\n clnf->log = pool->log;\n return NGX_OK;\n }\n err = ngx_errno;\n if (err == NGX_EEXIST_FILE) {\n n = (uint32_t) ngx_next_temp_number(1);\n continue;\n }\n if ((path->level[0] == 0) || (err != NGX_ENOPATH)) {\n ngx_log_error(NGX_LOG_CRIT, file->log, err,\n ngx_open_tempfile_n " \\"%s\\" failed",\n file->name.data);\n return NGX_ERROR;\n }\n if (ngx_create_path(file, path) == NGX_ERROR) {\n return NGX_ERROR;\n }\n }\n}', "void\nngx_create_hashed_filename(ngx_path_t *path, u_char *file, size_t len)\n{\n size_t i, level;\n ngx_uint_t n;\n i = path->name.len + 1;\n file[path->name.len + path->len] = '/';\n for (n = 0; n < NGX_MAX_PATH_LEVEL; n++) {\n level = path->level[n];\n if (level == 0) {\n break;\n }\n len -= level;\n file[i - 1] = '/';\n ngx_memcpy(&file[i], &file[len], level);\n i += level + 1;\n }\n}"] |
2,282 | 0 | https://github.com/libav/libav/blob/99ca7c94c4540ee0f5639efe669caaa27608cc70/libavcodec/dvbsubdec.c/#L947 | static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
DVBSubContext *ctx = avctx->priv_data;
const uint8_t *buf_end = buf + buf_size;
int clut_id;
DVBSubCLUT *clut;
int entry_id, depth , full_range;
int y, cr, cb, alpha;
int r, g, b, r_add, g_add, b_add;
#ifdef DEBUG_PACKET_CONTENTS
int i;
av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
for (i=0; i < buf_size; i++) {
av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
if (i % 16 == 15)
av_log(avctx, AV_LOG_INFO, "\n");
}
if (i % 16)
av_log(avctx, AV_LOG_INFO, "\n");
#endif
clut_id = *buf++;
buf += 1;
clut = get_clut(ctx, clut_id);
if (!clut) {
clut = av_malloc(sizeof(DVBSubCLUT));
memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
clut->id = clut_id;
clut->next = ctx->clut_list;
ctx->clut_list = clut;
}
while (buf + 4 < buf_end) {
entry_id = *buf++;
depth = (*buf) & 0xe0;
if (depth == 0) {
av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
return;
}
full_range = (*buf++) & 1;
if (full_range) {
y = *buf++;
cr = *buf++;
cb = *buf++;
alpha = *buf++;
} else {
y = buf[0] & 0xfc;
cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
cb = (buf[1] << 2) & 0xf0;
alpha = (buf[1] << 6) & 0xc0;
buf += 2;
}
if (y == 0)
alpha = 0xff;
YUV_TO_RGB1_CCIR(cb, cr);
YUV_TO_RGB2_CCIR(r, g, b, y);
dprintf(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
if (depth & 0x80)
clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
if (depth & 0x40)
clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
if (depth & 0x20)
clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
}
} | ['static void dvbsub_parse_clut_segment(AVCodecContext *avctx,\n const uint8_t *buf, int buf_size)\n{\n DVBSubContext *ctx = avctx->priv_data;\n const uint8_t *buf_end = buf + buf_size;\n int clut_id;\n DVBSubCLUT *clut;\n int entry_id, depth , full_range;\n int y, cr, cb, alpha;\n int r, g, b, r_add, g_add, b_add;\n#ifdef DEBUG_PACKET_CONTENTS\n int i;\n av_log(avctx, AV_LOG_INFO, "DVB clut packet:\\n");\n for (i=0; i < buf_size; i++) {\n av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);\n if (i % 16 == 15)\n av_log(avctx, AV_LOG_INFO, "\\n");\n }\n if (i % 16)\n av_log(avctx, AV_LOG_INFO, "\\n");\n#endif\n clut_id = *buf++;\n buf += 1;\n clut = get_clut(ctx, clut_id);\n if (!clut) {\n clut = av_malloc(sizeof(DVBSubCLUT));\n memcpy(clut, &default_clut, sizeof(DVBSubCLUT));\n clut->id = clut_id;\n clut->next = ctx->clut_list;\n ctx->clut_list = clut;\n }\n while (buf + 4 < buf_end) {\n entry_id = *buf++;\n depth = (*buf) & 0xe0;\n if (depth == 0) {\n av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\\n", *buf);\n return;\n }\n full_range = (*buf++) & 1;\n if (full_range) {\n y = *buf++;\n cr = *buf++;\n cb = *buf++;\n alpha = *buf++;\n } else {\n y = buf[0] & 0xfc;\n cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;\n cb = (buf[1] << 2) & 0xf0;\n alpha = (buf[1] << 6) & 0xc0;\n buf += 2;\n }\n if (y == 0)\n alpha = 0xff;\n YUV_TO_RGB1_CCIR(cb, cr);\n YUV_TO_RGB2_CCIR(r, g, b, y);\n dprintf(avctx, "clut %d := (%d,%d,%d,%d)\\n", entry_id, r, g, b, alpha);\n if (depth & 0x80)\n clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);\n if (depth & 0x40)\n clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);\n if (depth & 0x20)\n clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);\n }\n}', 'static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)\n{\n DVBSubCLUT *ptr = ctx->clut_list;\n while (ptr && ptr->id != clut_id) {\n ptr = ptr->next;\n }\n return ptr;\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}'] |
2,283 | 0 | https://github.com/libav/libav/blob/a6783b8961a0c68b0b76a35f03538778e67c3ec9/libavcodec/movsub_bsf.c/#L31 | static int text2movsub(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size, int keyframe){
if (buf_size > 0xffff) return 0;
*poutbuf_size = buf_size + 2;
*poutbuf = av_malloc(*poutbuf_size + FF_INPUT_BUFFER_PADDING_SIZE);
AV_WB16(*poutbuf, buf_size);
memcpy(*poutbuf + 2, buf, buf_size);
return 1;
} | ['static int text2movsub(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 if (buf_size > 0xffff) return 0;\n *poutbuf_size = buf_size + 2;\n *poutbuf = av_malloc(*poutbuf_size + FF_INPUT_BUFFER_PADDING_SIZE);\n AV_WB16(*poutbuf, buf_size);\n memcpy(*poutbuf + 2, buf, buf_size);\n return 1;\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}', 'static av_always_inline av_const uint16_t bswap_16(uint16_t x)\n{\n x= (x>>8) | (x<<8);\n return x;\n}'] |
2,284 | 0 | https://github.com/libav/libav/blob/9e12002f114d7e0b0ef69519518cdc0391e5e198/libavcodec/utils.c/#L317 | static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
{
AVCodecInternal *avci = avctx->internal;
InternalBuffer *buf;
int buf_size, ret;
buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
frame->nb_samples, avctx->sample_fmt,
32);
if (buf_size < 0)
return AVERROR(EINVAL);
if (!avci->buffer) {
avci->buffer = av_mallocz(sizeof(InternalBuffer));
if (!avci->buffer)
return AVERROR(ENOMEM);
}
buf = avci->buffer;
if (buf->extended_data) {
if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
av_free(buf->extended_data[0]);
if (buf->extended_data != buf->data)
av_free(&buf->extended_data);
buf->extended_data = NULL;
buf->data[0] = NULL;
}
if (buf->nb_channels != avctx->channels) {
if (buf->extended_data != buf->data)
av_free(buf->extended_data);
buf->extended_data = NULL;
}
}
if (!buf->extended_data) {
if (!buf->data[0]) {
if (!(buf->data[0] = av_mallocz(buf_size)))
return AVERROR(ENOMEM);
buf->audio_data_size = buf_size;
}
if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
avctx->sample_fmt, buf->data[0],
buf->audio_data_size, 32)))
return ret;
if (frame->extended_data == frame->data)
buf->extended_data = buf->data;
else
buf->extended_data = frame->extended_data;
memcpy(buf->data, frame->data, sizeof(frame->data));
buf->linesize[0] = frame->linesize[0];
buf->nb_channels = avctx->channels;
} else {
frame->extended_data = buf->extended_data;
frame->linesize[0] = buf->linesize[0];
memcpy(frame->data, buf->data, sizeof(frame->data));
}
frame->type = FF_BUFFER_TYPE_INTERNAL;
if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
else frame->pkt_pts = AV_NOPTS_VALUE;
frame->reordered_opaque = avctx->reordered_opaque;
if (avctx->debug & FF_DEBUG_BUFFERS)
av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
"internal audio buffer used\n", frame);
return 0;
} | ['static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)\n{\n AVCodecInternal *avci = avctx->internal;\n InternalBuffer *buf;\n int buf_size, ret;\n buf_size = av_samples_get_buffer_size(NULL, avctx->channels,\n frame->nb_samples, avctx->sample_fmt,\n 32);\n if (buf_size < 0)\n return AVERROR(EINVAL);\n if (!avci->buffer) {\n avci->buffer = av_mallocz(sizeof(InternalBuffer));\n if (!avci->buffer)\n return AVERROR(ENOMEM);\n }\n buf = avci->buffer;\n if (buf->extended_data) {\n if (buf->extended_data[0] && buf_size > buf->audio_data_size) {\n av_free(buf->extended_data[0]);\n if (buf->extended_data != buf->data)\n av_free(&buf->extended_data);\n buf->extended_data = NULL;\n buf->data[0] = NULL;\n }\n if (buf->nb_channels != avctx->channels) {\n if (buf->extended_data != buf->data)\n av_free(buf->extended_data);\n buf->extended_data = NULL;\n }\n }\n if (!buf->extended_data) {\n if (!buf->data[0]) {\n if (!(buf->data[0] = av_mallocz(buf_size)))\n return AVERROR(ENOMEM);\n buf->audio_data_size = buf_size;\n }\n if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,\n avctx->sample_fmt, buf->data[0],\n buf->audio_data_size, 32)))\n return ret;\n if (frame->extended_data == frame->data)\n buf->extended_data = buf->data;\n else\n buf->extended_data = frame->extended_data;\n memcpy(buf->data, frame->data, sizeof(frame->data));\n buf->linesize[0] = frame->linesize[0];\n buf->nb_channels = avctx->channels;\n } else {\n frame->extended_data = buf->extended_data;\n frame->linesize[0] = buf->linesize[0];\n memcpy(frame->data, buf->data, sizeof(frame->data));\n }\n frame->type = FF_BUFFER_TYPE_INTERNAL;\n if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;\n else frame->pkt_pts = AV_NOPTS_VALUE;\n frame->reordered_opaque = avctx->reordered_opaque;\n if (avctx->debug & FF_DEBUG_BUFFERS)\n av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "\n "internal audio buffer used\\n", frame);\n return 0;\n}', 'void av_free(void *ptr)\n{\n#if CONFIG_MEMALIGN_HACK\n if (ptr)\n free((char*)ptr - ((char*)ptr)[-1]);\n#else\n free(ptr);\n#endif\n}'] |
2,285 | 1 | https://github.com/openssl/openssl/blob/b3618f44a7b8504bfb0a64e8a33e6b8e56d4d516/crypto/bn/bn_ctx.c/#L273 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int test_sqr(BIO *bp, BN_CTX *ctx)\n{\n BIGNUM *a, *c, *d, *e;\n int i, ret = 0;\n a = BN_new();\n c = BN_new();\n d = BN_new();\n e = BN_new();\n if (a == NULL || c == NULL || d == NULL || e == NULL) {\n goto err;\n }\n for (i = 0; i < num0; i++) {\n BN_bntest_rand(a, 40 + i * 10, 0, 0);\n a->neg = rand_neg();\n BN_sqr(c, a, ctx);\n if (bp != NULL) {\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " * ");\n BN_print(bp, a);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, c);\n BIO_puts(bp, "\\n");\n }\n BN_div(d, e, c, a, ctx);\n BN_sub(d, d, a);\n if (!BN_is_zero(d) || !BN_is_zero(e)) {\n fprintf(stderr, "Square test failed!\\n");\n goto err;\n }\n }\n BN_hex2bn(&a,\n "80000000000000008000000000000001"\n "FFFFFFFFFFFFFFFE0000000000000000");\n BN_sqr(c, a, ctx);\n if (bp != NULL) {\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " * ");\n BN_print(bp, a);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, c);\n BIO_puts(bp, "\\n");\n }\n BN_mul(d, a, a, ctx);\n if (BN_cmp(c, d)) {\n fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "\n "different results!\\n");\n goto err;\n }\n BN_hex2bn(&a,\n "80000000000000000000000080000001"\n "FFFFFFFE000000000000000000000000");\n BN_sqr(c, a, ctx);\n if (bp != NULL) {\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " * ");\n BN_print(bp, a);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, c);\n BIO_puts(bp, "\\n");\n }\n BN_mul(d, a, a, ctx);\n if (BN_cmp(c, d)) {\n fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "\n "different results!\\n");\n goto err;\n }\n ret = 1;\n err:\n BN_free(a);\n BN_free(c);\n BN_free(d);\n BN_free(e);\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 (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void BN_CTX_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}'] |
2,286 | 0 | https://github.com/openssl/openssl/blob/edbcba6c6bd4587e96fb4e4d07a6c24920897579/apps/x509.c/#L1209 | static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, const EVP_MD *digest,
LHASH *conf, char *section)
{
EVP_PKEY *pktmp;
pktmp = X509_get_pubkey(x);
EVP_PKEY_copy_parameters(pktmp,pkey);
EVP_PKEY_save_parameters(pktmp,1);
EVP_PKEY_free(pktmp);
if (!X509_set_issuer_name(x,X509_get_subject_name(x))) goto err;
if (X509_gmtime_adj(X509_get_notBefore(x),0) == NULL) goto err;
if (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)
goto err;
if (!X509_set_pubkey(x,pkey)) goto err;
if(clrext) {
while(X509_get_ext_count(x) > 0) X509_delete_ext(x, 0);
}
if(conf) {
X509V3_CTX ctx;
X509_set_version(x,2);
X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
X509V3_set_conf_lhash(&ctx, conf);
if(!X509V3_EXT_add_conf(conf, &ctx, section, x)) goto err;
}
if (!X509_sign(x,pkey,digest)) goto err;
return(1);
err:
ERR_print_errors(bio_err);
return(0);
} | ['static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, const EVP_MD *digest,\n\t\t\t\t\t\tLHASH *conf, char *section)\n\t{\n\tEVP_PKEY *pktmp;\n\tpktmp = X509_get_pubkey(x);\n\tEVP_PKEY_copy_parameters(pktmp,pkey);\n\tEVP_PKEY_save_parameters(pktmp,1);\n\tEVP_PKEY_free(pktmp);\n\tif (!X509_set_issuer_name(x,X509_get_subject_name(x))) goto err;\n\tif (X509_gmtime_adj(X509_get_notBefore(x),0) == NULL) goto err;\n\tif (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)\n\t\tgoto err;\n\tif (!X509_set_pubkey(x,pkey)) goto err;\n\tif(clrext) {\n\t\twhile(X509_get_ext_count(x) > 0) X509_delete_ext(x, 0);\n\t}\n\tif(conf) {\n\t\tX509V3_CTX ctx;\n\t\tX509_set_version(x,2);\n X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);\n X509V3_set_conf_lhash(&ctx, conf);\n if(!X509V3_EXT_add_conf(conf, &ctx, section, x)) goto err;\n\t}\n\tif (!X509_sign(x,pkey,digest)) goto err;\n\treturn(1);\nerr:\n\tERR_print_errors(bio_err);\n\treturn(0);\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}', 'int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)\n\t{\n#ifndef NO_DSA\n\tif (pkey->type == EVP_PKEY_DSA)\n\t\t{\n\t\tint ret=pkey->save_parameters=mode;\n\t\tif (mode >= 0)\n\t\t\tpkey->save_parameters=mode;\n\t\treturn(ret);\n\t\t}\n#endif\n\treturn(0);\n\t}'] |
2,287 | 0 | https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L228 | static void pred4x4_down_left_rv40_nodown_c(uint8_t *src, uint8_t *topright, int stride){
LOAD_TOP_EDGE
LOAD_TOP_RIGHT_EDGE
LOAD_LEFT_EDGE
src[0+0*stride]=(t0 + t2 + 2*t1 + 2 + l0 + l2 + 2*l1 + 2)>>3;
src[1+0*stride]=
src[0+1*stride]=(t1 + t3 + 2*t2 + 2 + l1 + l3 + 2*l2 + 2)>>3;
src[2+0*stride]=
src[1+1*stride]=
src[0+2*stride]=(t2 + t4 + 2*t3 + 2 + l2 + 3*l3 + 2)>>3;
src[3+0*stride]=
src[2+1*stride]=
src[1+2*stride]=
src[0+3*stride]=(t3 + t5 + 2*t4 + 2 + l3*4 + 2)>>3;
src[3+1*stride]=
src[2+2*stride]=
src[1+3*stride]=(t4 + t6 + 2*t5 + 2 + l3*4 + 2)>>3;
src[3+2*stride]=
src[2+3*stride]=(t5 + t7 + 2*t6 + 2 + l3*4 + 2)>>3;
src[3+3*stride]=(t6 + t7 + 1 + 2*l3 + 1)>>2;
} | ['static void pred4x4_down_left_rv40_nodown_c(uint8_t *src, uint8_t *topright, int stride){\n LOAD_TOP_EDGE\n LOAD_TOP_RIGHT_EDGE\n LOAD_LEFT_EDGE\n src[0+0*stride]=(t0 + t2 + 2*t1 + 2 + l0 + l2 + 2*l1 + 2)>>3;\n src[1+0*stride]=\n src[0+1*stride]=(t1 + t3 + 2*t2 + 2 + l1 + l3 + 2*l2 + 2)>>3;\n src[2+0*stride]=\n src[1+1*stride]=\n src[0+2*stride]=(t2 + t4 + 2*t3 + 2 + l2 + 3*l3 + 2)>>3;\n src[3+0*stride]=\n src[2+1*stride]=\n src[1+2*stride]=\n src[0+3*stride]=(t3 + t5 + 2*t4 + 2 + l3*4 + 2)>>3;\n src[3+1*stride]=\n src[2+2*stride]=\n src[1+3*stride]=(t4 + t6 + 2*t5 + 2 + l3*4 + 2)>>3;\n src[3+2*stride]=\n src[2+3*stride]=(t5 + t7 + 2*t6 + 2 + l3*4 + 2)>>3;\n src[3+3*stride]=(t6 + t7 + 1 + 2*l3 + 1)>>2;\n}'] |
2,288 | 0 | https://github.com/libav/libav/blob/d6e49096c0c3c10ffb176761b0da150c93bedbf6/libavformat/mpegts.c/#L2137 | static int mpegts_read_header(AVFormatContext *s)
{
MpegTSContext *ts = s->priv_data;
AVIOContext *pb = s->pb;
uint8_t buf[5 * 1024];
int len;
int64_t pos;
pos = avio_tell(pb);
len = avio_read(pb, buf, sizeof(buf));
if (len < 0)
return len;
if (len != sizeof(buf))
return AVERROR_BUG;
ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
if (ts->raw_packet_size <= 0)
return AVERROR_INVALIDDATA;
ts->stream = s;
ts->auto_guess = 0;
if (s->iformat == &ff_mpegts_demuxer) {
if (avio_seek(pb, pos, SEEK_SET) < 0 && pb->seekable)
av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\n");
mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
handle_packets(ts, s->probesize / ts->raw_packet_size);
ts->auto_guess = 1;
av_log(ts->stream, AV_LOG_TRACE, "tuning done\n");
s->ctx_flags |= AVFMTCTX_NOHEADER;
} else {
AVStream *st;
int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
int64_t pcrs[2], pcr_h;
int packet_count[2];
uint8_t packet[TS_PACKET_SIZE];
const uint8_t *data;
st = avformat_new_stream(s, NULL);
if (!st)
return AVERROR(ENOMEM);
avpriv_set_pts_info(st, 60, 1, 27000000);
st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
st->codecpar->codec_id = AV_CODEC_ID_MPEG2TS;
pcr_pid = -1;
nb_pcrs = 0;
nb_packets = 0;
for (;;) {
ret = read_packet(s, packet, ts->raw_packet_size, &data);
if (ret < 0)
return ret;
pid = AV_RB16(data + 1) & 0x1fff;
if ((pcr_pid == -1 || pcr_pid == pid) &&
parse_pcr(&pcr_h, &pcr_l, data) == 0) {
finished_reading_packet(s, ts->raw_packet_size);
pcr_pid = pid;
packet_count[nb_pcrs] = nb_packets;
pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
nb_pcrs++;
if (nb_pcrs >= 2)
break;
} else {
finished_reading_packet(s, ts->raw_packet_size);
}
nb_packets++;
}
ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
s->bit_rate = TS_PACKET_SIZE * 8 * 27e6 / ts->pcr_incr;
st->codecpar->bit_rate = s->bit_rate;
st->start_time = ts->cur_pcr;
av_log(ts->stream, AV_LOG_TRACE, "start=%0.3f pcr=%0.3f incr=%d\n",
st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
}
avio_seek(pb, pos, SEEK_SET);
return 0;
} | ['static int mpegts_read_header(AVFormatContext *s)\n{\n MpegTSContext *ts = s->priv_data;\n AVIOContext *pb = s->pb;\n uint8_t buf[5 * 1024];\n int len;\n int64_t pos;\n pos = avio_tell(pb);\n len = avio_read(pb, buf, sizeof(buf));\n if (len < 0)\n return len;\n if (len != sizeof(buf))\n return AVERROR_BUG;\n ts->raw_packet_size = get_packet_size(buf, sizeof(buf));\n if (ts->raw_packet_size <= 0)\n return AVERROR_INVALIDDATA;\n ts->stream = s;\n ts->auto_guess = 0;\n if (s->iformat == &ff_mpegts_demuxer) {\n if (avio_seek(pb, pos, SEEK_SET) < 0 && pb->seekable)\n av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\\n");\n mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);\n mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);\n handle_packets(ts, s->probesize / ts->raw_packet_size);\n ts->auto_guess = 1;\n av_log(ts->stream, AV_LOG_TRACE, "tuning done\\n");\n s->ctx_flags |= AVFMTCTX_NOHEADER;\n } else {\n AVStream *st;\n int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;\n int64_t pcrs[2], pcr_h;\n int packet_count[2];\n uint8_t packet[TS_PACKET_SIZE];\n const uint8_t *data;\n st = avformat_new_stream(s, NULL);\n if (!st)\n return AVERROR(ENOMEM);\n avpriv_set_pts_info(st, 60, 1, 27000000);\n st->codecpar->codec_type = AVMEDIA_TYPE_DATA;\n st->codecpar->codec_id = AV_CODEC_ID_MPEG2TS;\n pcr_pid = -1;\n nb_pcrs = 0;\n nb_packets = 0;\n for (;;) {\n ret = read_packet(s, packet, ts->raw_packet_size, &data);\n if (ret < 0)\n return ret;\n pid = AV_RB16(data + 1) & 0x1fff;\n if ((pcr_pid == -1 || pcr_pid == pid) &&\n parse_pcr(&pcr_h, &pcr_l, data) == 0) {\n finished_reading_packet(s, ts->raw_packet_size);\n pcr_pid = pid;\n packet_count[nb_pcrs] = nb_packets;\n pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;\n nb_pcrs++;\n if (nb_pcrs >= 2)\n break;\n } else {\n finished_reading_packet(s, ts->raw_packet_size);\n }\n nb_packets++;\n }\n ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);\n ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];\n s->bit_rate = TS_PACKET_SIZE * 8 * 27e6 / ts->pcr_incr;\n st->codecpar->bit_rate = s->bit_rate;\n st->start_time = ts->cur_pcr;\n av_log(ts->stream, AV_LOG_TRACE, "start=%0.3f pcr=%0.3f incr=%d\\n",\n st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);\n }\n avio_seek(pb, pos, SEEK_SET);\n return 0;\n}'] |
2,289 | 0 | https://github.com/openssl/openssl/blob/6bc62a620e715f7580651ca932eab052aa527886/crypto/bn/bn_ctx.c/#L268 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,\n const ECDSA_SIG *sig, EC_KEY *eckey)\n{\n int ret = -1, i;\n BN_CTX *ctx;\n const BIGNUM *order;\n BIGNUM *u1, *u2, *m, *X;\n EC_POINT *point = NULL;\n const EC_GROUP *group;\n const EC_POINT *pub_key;\n if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||\n (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);\n return -1;\n }\n if (!EC_KEY_can_sign(eckey)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n return -1;\n }\n ctx = BN_CTX_new();\n if (ctx == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n BN_CTX_start(ctx);\n u1 = BN_CTX_get(ctx);\n u2 = BN_CTX_get(ctx);\n m = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n if (X == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n order = EC_GROUP_get0_order(group);\n if (order == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);\n goto err;\n }\n if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||\n BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||\n BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);\n ret = 0;\n goto err;\n }\n if (!ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n i = BN_num_bits(order);\n if (8 * dgst_len > i)\n dgst_len = (i + 7) / 8;\n if (!BN_bin2bn(dgst, dgst_len, m)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(u1, m, u2, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if ((point = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);\n goto err;\n }\n if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);\n goto err;\n }\n if (!BN_nnmod(u1, X, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n ret = (BN_ucmp(u1, sig->r) == 0);\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n EC_POINT_free(point);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = bn_mul_fixed_top(r, a, b, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
2,290 | 0 | https://github.com/libav/libav/blob/8a49d2bcbe7573bb4b765728b2578fac0d19763f/libavfilter/buffer.c/#L79 | void avfilter_unref_buffer(AVFilterBufferRef *ref)
{
if (!ref)
return;
if (!(--ref->buf->refcount))
ref->buf->free(ref->buf);
if (ref->extended_data != ref->data)
av_freep(&ref->extended_data);
av_free(ref->video);
av_free(ref->audio);
av_free(ref);
} | ['static void compat_free_buffer(void *opaque, uint8_t *data)\n{\n AVFilterBufferRef *buf = opaque;\n avfilter_unref_buffer(buf);\n}', 'void avfilter_unref_buffer(AVFilterBufferRef *ref)\n{\n if (!ref)\n return;\n if (!(--ref->buf->refcount))\n ref->buf->free(ref->buf);\n if (ref->extended_data != ref->data)\n av_freep(&ref->extended_data);\n av_free(ref->video);\n av_free(ref->audio);\n av_free(ref);\n}'] |
2,291 | 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 ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *a, *b, *order, *tmp_1, *tmp_2;\n const BIGNUM *p = group->field;\n BN_CTX *new_ctx = NULL;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL) {\n ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT,\n ERR_R_MALLOC_FAILURE);\n goto err;\n }\n }\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n tmp_1 = BN_CTX_get(ctx);\n tmp_2 = BN_CTX_get(ctx);\n order = BN_CTX_get(ctx);\n if (order == NULL)\n goto err;\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, a, group->a, ctx))\n goto err;\n if (!group->meth->field_decode(group, b, group->b, ctx))\n goto err;\n } else {\n if (!BN_copy(a, group->a))\n goto err;\n if (!BN_copy(b, group->b))\n goto err;\n }\n if (BN_is_zero(a)) {\n if (BN_is_zero(b))\n goto err;\n } else if (!BN_is_zero(b)) {\n if (!BN_mod_sqr(tmp_1, a, p, ctx))\n goto err;\n if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx))\n goto err;\n if (!BN_lshift(tmp_1, tmp_2, 2))\n goto err;\n if (!BN_mod_sqr(tmp_2, b, p, ctx))\n goto err;\n if (!BN_mul_word(tmp_2, 27))\n goto err;\n if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx))\n goto err;\n if (BN_is_zero(a))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL)\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}', 'int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)\n{\n if (!BN_sqr(r, a, ctx))\n return 0;\n return BN_mod(r, r, m, ctx);\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}'] |
2,292 | 0 | https://github.com/libav/libav/blob/366ba2dee1f2b17825b42e2164d3b9879f0271b1/libavcodec/h264_mvpred.h/#L564 | static void fill_decode_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
{
int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
int topleft_type, top_type, topright_type, left_type[LEFT_MBS];
const uint8_t *left_block = sl->left_block;
int i;
uint8_t *nnz;
uint8_t *nnz_cache;
topleft_xy = sl->topleft_mb_xy;
top_xy = sl->top_mb_xy;
topright_xy = sl->topright_mb_xy;
left_xy[LTOP] = sl->left_mb_xy[LTOP];
left_xy[LBOT] = sl->left_mb_xy[LBOT];
topleft_type = sl->topleft_type;
top_type = sl->top_type;
topright_type = sl->topright_type;
left_type[LTOP] = sl->left_type[LTOP];
left_type[LBOT] = sl->left_type[LBOT];
if (!IS_SKIP(mb_type)) {
if (IS_INTRA(mb_type)) {
int type_mask = h->ps.pps->constrained_intra_pred ? IS_INTRA(-1) : -1;
sl->topleft_samples_available =
sl->top_samples_available =
sl->left_samples_available = 0xFFFF;
sl->topright_samples_available = 0xEEEA;
if (!(top_type & type_mask)) {
sl->topleft_samples_available = 0xB3FF;
sl->top_samples_available = 0x33FF;
sl->topright_samples_available = 0x26EA;
}
if (IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[LTOP])) {
if (IS_INTERLACED(mb_type)) {
if (!(left_type[LTOP] & type_mask)) {
sl->topleft_samples_available &= 0xDFFF;
sl->left_samples_available &= 0x5FFF;
}
if (!(left_type[LBOT] & type_mask)) {
sl->topleft_samples_available &= 0xFF5F;
sl->left_samples_available &= 0xFF5F;
}
} else {
int left_typei = h->cur_pic.mb_type[left_xy[LTOP] + h->mb_stride];
assert(left_xy[LTOP] == left_xy[LBOT]);
if (!((left_typei & type_mask) && (left_type[LTOP] & type_mask))) {
sl->topleft_samples_available &= 0xDF5F;
sl->left_samples_available &= 0x5F5F;
}
}
} else {
if (!(left_type[LTOP] & type_mask)) {
sl->topleft_samples_available &= 0xDF5F;
sl->left_samples_available &= 0x5F5F;
}
}
if (!(topleft_type & type_mask))
sl->topleft_samples_available &= 0x7FFF;
if (!(topright_type & type_mask))
sl->topright_samples_available &= 0xFBFF;
if (IS_INTRA4x4(mb_type)) {
if (IS_INTRA4x4(top_type)) {
AV_COPY32(sl->intra4x4_pred_mode_cache + 4 + 8 * 0, sl->intra4x4_pred_mode + h->mb2br_xy[top_xy]);
} else {
sl->intra4x4_pred_mode_cache[4 + 8 * 0] =
sl->intra4x4_pred_mode_cache[5 + 8 * 0] =
sl->intra4x4_pred_mode_cache[6 + 8 * 0] =
sl->intra4x4_pred_mode_cache[7 + 8 * 0] = 2 - 3 * !(top_type & type_mask);
}
for (i = 0; i < 2; i++) {
if (IS_INTRA4x4(left_type[LEFT(i)])) {
int8_t *mode = sl->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]];
sl->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] = mode[6 - left_block[0 + 2 * i]];
sl->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = mode[6 - left_block[1 + 2 * i]];
} else {
sl->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] =
sl->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = 2 - 3 * !(left_type[LEFT(i)] & type_mask);
}
}
}
}
nnz_cache = sl->non_zero_count_cache;
if (top_type) {
nnz = h->non_zero_count[top_xy];
AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[4 * 3]);
if (!h->chroma_y_shift) {
AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 7]);
AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 11]);
} else {
AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 5]);
AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);
}
} else {
uint32_t top_empty = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 0x40404040;
AV_WN32A(&nnz_cache[4 + 8 * 0], top_empty);
AV_WN32A(&nnz_cache[4 + 8 * 5], top_empty);
AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);
}
for (i = 0; i < 2; i++) {
if (left_type[LEFT(i)]) {
nnz = h->non_zero_count[left_xy[LEFT(i)]];
nnz_cache[3 + 8 * 1 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i]];
nnz_cache[3 + 8 * 2 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i]];
if (CHROMA444(h)) {
nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 4 * 4];
nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 4 * 4];
nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 8 * 4];
nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 8 * 4];
} else if (CHROMA422(h)) {
nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 4 * 4];
nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 4 * 4];
nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 8 * 4];
nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 8 * 4];
} else {
nnz_cache[3 + 8 * 6 + 8 * i] = nnz[left_block[8 + 4 + 2 * i]];
nnz_cache[3 + 8 * 11 + 8 * i] = nnz[left_block[8 + 5 + 2 * i]];
}
} else {
nnz_cache[3 + 8 * 1 + 2 * 8 * i] =
nnz_cache[3 + 8 * 2 + 2 * 8 * i] =
nnz_cache[3 + 8 * 6 + 2 * 8 * i] =
nnz_cache[3 + 8 * 7 + 2 * 8 * i] =
nnz_cache[3 + 8 * 11 + 2 * 8 * i] =
nnz_cache[3 + 8 * 12 + 2 * 8 * i] = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 64;
}
}
if (CABAC(h)) {
if (top_type)
sl->top_cbp = h->cbp_table[top_xy];
else
sl->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
if (left_type[LTOP]) {
sl->left_cbp = (h->cbp_table[left_xy[LTOP]] & 0x7F0) |
((h->cbp_table[left_xy[LTOP]] >> (left_block[0] & (~1))) & 2) |
(((h->cbp_table[left_xy[LBOT]] >> (left_block[2] & (~1))) & 2) << 2);
} else {
sl->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
}
}
}
if (IS_INTER(mb_type) || (IS_DIRECT(mb_type) && sl->direct_spatial_mv_pred)) {
int list;
int b_stride = h->b_stride;
for (list = 0; list < sl->list_count; list++) {
int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
int8_t *ref = h->cur_pic.ref_index[list];
int16_t(*mv_cache)[2] = &sl->mv_cache[list][scan8[0]];
int16_t(*mv)[2] = h->cur_pic.motion_val[list];
if (!USES_LIST(mb_type, list))
continue;
assert(!(IS_DIRECT(mb_type) && !sl->direct_spatial_mv_pred));
if (USES_LIST(top_type, list)) {
const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
AV_COPY128(mv_cache[0 - 1 * 8], mv[b_xy + 0]);
ref_cache[0 - 1 * 8] =
ref_cache[1 - 1 * 8] = ref[4 * top_xy + 2];
ref_cache[2 - 1 * 8] =
ref_cache[3 - 1 * 8] = ref[4 * top_xy + 3];
} else {
AV_ZERO128(mv_cache[0 - 1 * 8]);
AV_WN32A(&ref_cache[0 - 1 * 8],
((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE) & 0xFF) * 0x01010101u);
}
if (mb_type & (MB_TYPE_16x8 | MB_TYPE_8x8)) {
for (i = 0; i < 2; i++) {
int cache_idx = -1 + i * 2 * 8;
if (USES_LIST(left_type[LEFT(i)], list)) {
const int b_xy = h->mb2b_xy[left_xy[LEFT(i)]] + 3;
const int b8_xy = 4 * left_xy[LEFT(i)] + 1;
AV_COPY32(mv_cache[cache_idx],
mv[b_xy + b_stride * left_block[0 + i * 2]]);
AV_COPY32(mv_cache[cache_idx + 8],
mv[b_xy + b_stride * left_block[1 + i * 2]]);
ref_cache[cache_idx] = ref[b8_xy + (left_block[0 + i * 2] & ~1)];
ref_cache[cache_idx + 8] = ref[b8_xy + (left_block[1 + i * 2] & ~1)];
} else {
AV_ZERO32(mv_cache[cache_idx]);
AV_ZERO32(mv_cache[cache_idx + 8]);
ref_cache[cache_idx] =
ref_cache[cache_idx + 8] = (left_type[LEFT(i)]) ? LIST_NOT_USED
: PART_NOT_AVAILABLE;
}
}
} else {
if (USES_LIST(left_type[LTOP], list)) {
const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
const int b8_xy = 4 * left_xy[LTOP] + 1;
AV_COPY32(mv_cache[-1], mv[b_xy + b_stride * left_block[0]]);
ref_cache[-1] = ref[b8_xy + (left_block[0] & ~1)];
} else {
AV_ZERO32(mv_cache[-1]);
ref_cache[-1] = left_type[LTOP] ? LIST_NOT_USED
: PART_NOT_AVAILABLE;
}
}
if (USES_LIST(topright_type, list)) {
const int b_xy = h->mb2b_xy[topright_xy] + 3 * b_stride;
AV_COPY32(mv_cache[4 - 1 * 8], mv[b_xy]);
ref_cache[4 - 1 * 8] = ref[4 * topright_xy + 2];
} else {
AV_ZERO32(mv_cache[4 - 1 * 8]);
ref_cache[4 - 1 * 8] = topright_type ? LIST_NOT_USED
: PART_NOT_AVAILABLE;
}
if (ref_cache[4 - 1 * 8] < 0) {
if (USES_LIST(topleft_type, list)) {
const int b_xy = h->mb2b_xy[topleft_xy] + 3 + b_stride +
(sl->topleft_partition & 2 * b_stride);
const int b8_xy = 4 * topleft_xy + 1 + (sl->topleft_partition & 2);
AV_COPY32(mv_cache[-1 - 1 * 8], mv[b_xy]);
ref_cache[-1 - 1 * 8] = ref[b8_xy];
} else {
AV_ZERO32(mv_cache[-1 - 1 * 8]);
ref_cache[-1 - 1 * 8] = topleft_type ? LIST_NOT_USED
: PART_NOT_AVAILABLE;
}
}
if ((mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2)) && !FRAME_MBAFF(h))
continue;
if (!(mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2))) {
uint8_t(*mvd_cache)[2] = &sl->mvd_cache[list][scan8[0]];
uint8_t(*mvd)[2] = sl->mvd_table[list];
ref_cache[2 + 8 * 0] =
ref_cache[2 + 8 * 2] = PART_NOT_AVAILABLE;
AV_ZERO32(mv_cache[2 + 8 * 0]);
AV_ZERO32(mv_cache[2 + 8 * 2]);
if (CABAC(h)) {
if (USES_LIST(top_type, list)) {
const int b_xy = h->mb2br_xy[top_xy];
AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);
} else {
AV_ZERO64(mvd_cache[0 - 1 * 8]);
}
if (USES_LIST(left_type[LTOP], list)) {
const int b_xy = h->mb2br_xy[left_xy[LTOP]] + 6;
AV_COPY16(mvd_cache[-1 + 0 * 8], mvd[b_xy - left_block[0]]);
AV_COPY16(mvd_cache[-1 + 1 * 8], mvd[b_xy - left_block[1]]);
} else {
AV_ZERO16(mvd_cache[-1 + 0 * 8]);
AV_ZERO16(mvd_cache[-1 + 1 * 8]);
}
if (USES_LIST(left_type[LBOT], list)) {
const int b_xy = h->mb2br_xy[left_xy[LBOT]] + 6;
AV_COPY16(mvd_cache[-1 + 2 * 8], mvd[b_xy - left_block[2]]);
AV_COPY16(mvd_cache[-1 + 3 * 8], mvd[b_xy - left_block[3]]);
} else {
AV_ZERO16(mvd_cache[-1 + 2 * 8]);
AV_ZERO16(mvd_cache[-1 + 3 * 8]);
}
AV_ZERO16(mvd_cache[2 + 8 * 0]);
AV_ZERO16(mvd_cache[2 + 8 * 2]);
if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
uint8_t *direct_cache = &sl->direct_cache[scan8[0]];
uint8_t *direct_table = h->direct_table;
fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16 >> 1, 1);
if (IS_DIRECT(top_type)) {
AV_WN32A(&direct_cache[-1 * 8],
0x01010101u * (MB_TYPE_DIRECT2 >> 1));
} else if (IS_8X8(top_type)) {
int b8_xy = 4 * top_xy;
direct_cache[0 - 1 * 8] = direct_table[b8_xy + 2];
direct_cache[2 - 1 * 8] = direct_table[b8_xy + 3];
} else {
AV_WN32A(&direct_cache[-1 * 8],
0x01010101 * (MB_TYPE_16x16 >> 1));
}
if (IS_DIRECT(left_type[LTOP]))
direct_cache[-1 + 0 * 8] = MB_TYPE_DIRECT2 >> 1;
else if (IS_8X8(left_type[LTOP]))
direct_cache[-1 + 0 * 8] = direct_table[4 * left_xy[LTOP] + 1 + (left_block[0] & ~1)];
else
direct_cache[-1 + 0 * 8] = MB_TYPE_16x16 >> 1;
if (IS_DIRECT(left_type[LBOT]))
direct_cache[-1 + 2 * 8] = MB_TYPE_DIRECT2 >> 1;
else if (IS_8X8(left_type[LBOT]))
direct_cache[-1 + 2 * 8] = direct_table[4 * left_xy[LBOT] + 1 + (left_block[2] & ~1)];
else
direct_cache[-1 + 2 * 8] = MB_TYPE_16x16 >> 1;
}
}
}
#define MAP_MVS \
MAP_F2F(scan8[0] - 1 - 1 * 8, topleft_type) \
MAP_F2F(scan8[0] + 0 - 1 * 8, top_type) \
MAP_F2F(scan8[0] + 1 - 1 * 8, top_type) \
MAP_F2F(scan8[0] + 2 - 1 * 8, top_type) \
MAP_F2F(scan8[0] + 3 - 1 * 8, top_type) \
MAP_F2F(scan8[0] + 4 - 1 * 8, topright_type) \
MAP_F2F(scan8[0] - 1 + 0 * 8, left_type[LTOP]) \
MAP_F2F(scan8[0] - 1 + 1 * 8, left_type[LTOP]) \
MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT]) \
MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])
if (FRAME_MBAFF(h)) {
if (MB_FIELD(sl)) {
#define MAP_F2F(idx, mb_type) \
if (!IS_INTERLACED(mb_type) && sl->ref_cache[list][idx] >= 0) { \
sl->ref_cache[list][idx] <<= 1; \
sl->mv_cache[list][idx][1] /= 2; \
sl->mvd_cache[list][idx][1] >>= 1; \
}
MAP_MVS
} else {
#undef MAP_F2F
#define MAP_F2F(idx, mb_type) \
if (IS_INTERLACED(mb_type) && sl->ref_cache[list][idx] >= 0) { \
sl->ref_cache[list][idx] >>= 1; \
sl->mv_cache[list][idx][1] <<= 1; \
sl->mvd_cache[list][idx][1] <<= 1; \
}
MAP_MVS
#undef MAP_F2F
}
}
}
}
sl->neighbor_transform_size = !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[LTOP]);
} | ['static void av_unused decode_mb_skip(const H264Context *h, H264SliceContext *sl)\n{\n const int mb_xy = sl->mb_xy;\n int mb_type = 0;\n memset(h->non_zero_count[mb_xy], 0, 48);\n if (MB_FIELD(sl))\n mb_type |= MB_TYPE_INTERLACED;\n if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {\n mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 | MB_TYPE_SKIP;\n if (sl->direct_spatial_mv_pred) {\n fill_decode_neighbors(h, sl, mb_type);\n fill_decode_caches(h, sl, mb_type);\n }\n ff_h264_pred_direct_motion(h, sl, &mb_type);\n mb_type |= MB_TYPE_SKIP;\n } else {\n mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P1L0 | MB_TYPE_SKIP;\n fill_decode_neighbors(h, sl, mb_type);\n pred_pskip_motion(h, sl);\n }\n write_back_motion(h, sl, mb_type);\n h->cur_pic.mb_type[mb_xy] = mb_type;\n h->cur_pic.qscale_table[mb_xy] = sl->qscale;\n h->slice_table[mb_xy] = sl->slice_num;\n sl->prev_mb_skipped = 1;\n}', 'static void fill_decode_neighbors(const H264Context *h, H264SliceContext *sl, int mb_type)\n{\n const int mb_xy = sl->mb_xy;\n int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];\n static const uint8_t left_block_options[4][32] = {\n { 0, 1, 2, 3, 7, 10, 8, 11, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 5 * 4, 1 + 9 * 4 },\n { 2, 2, 3, 3, 8, 11, 8, 11, 3 + 2 * 4, 3 + 2 * 4, 3 + 3 * 4, 3 + 3 * 4, 1 + 5 * 4, 1 + 9 * 4, 1 + 5 * 4, 1 + 9 * 4 },\n { 0, 0, 1, 1, 7, 10, 7, 10, 3 + 0 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 1 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 4 * 4, 1 + 8 * 4 },\n { 0, 2, 0, 2, 7, 10, 7, 10, 3 + 0 * 4, 3 + 2 * 4, 3 + 0 * 4, 3 + 2 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 4 * 4, 1 + 8 * 4 }\n };\n sl->topleft_partition = -1;\n top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));\n topleft_xy = top_xy - 1;\n topright_xy = top_xy + 1;\n left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;\n sl->left_block = left_block_options[0];\n if (FRAME_MBAFF(h)) {\n const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);\n const int curr_mb_field_flag = IS_INTERLACED(mb_type);\n if (sl->mb_y & 1) {\n if (left_mb_field_flag != curr_mb_field_flag) {\n left_xy[LBOT] = left_xy[LTOP] = mb_xy - h->mb_stride - 1;\n if (curr_mb_field_flag) {\n left_xy[LBOT] += h->mb_stride;\n sl->left_block = left_block_options[3];\n } else {\n topleft_xy += h->mb_stride;\n sl->topleft_partition = 0;\n sl->left_block = left_block_options[1];\n }\n }\n } else {\n if (curr_mb_field_flag) {\n topleft_xy += h->mb_stride & (((h->cur_pic.mb_type[top_xy - 1] >> 7) & 1) - 1);\n topright_xy += h->mb_stride & (((h->cur_pic.mb_type[top_xy + 1] >> 7) & 1) - 1);\n top_xy += h->mb_stride & (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);\n }\n if (left_mb_field_flag != curr_mb_field_flag) {\n if (curr_mb_field_flag) {\n left_xy[LBOT] += h->mb_stride;\n sl->left_block = left_block_options[3];\n } else {\n sl->left_block = left_block_options[2];\n }\n }\n }\n }\n sl->topleft_mb_xy = topleft_xy;\n sl->top_mb_xy = top_xy;\n sl->topright_mb_xy = topright_xy;\n sl->left_mb_xy[LTOP] = left_xy[LTOP];\n sl->left_mb_xy[LBOT] = left_xy[LBOT];\n sl->topleft_type = h->cur_pic.mb_type[topleft_xy];\n sl->top_type = h->cur_pic.mb_type[top_xy];\n sl->topright_type = h->cur_pic.mb_type[topright_xy];\n sl->left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];\n sl->left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];\n if (FMO) {\n if (h->slice_table[topleft_xy] != sl->slice_num)\n sl->topleft_type = 0;\n if (h->slice_table[top_xy] != sl->slice_num)\n sl->top_type = 0;\n if (h->slice_table[left_xy[LTOP]] != sl->slice_num)\n sl->left_type[LTOP] = sl->left_type[LBOT] = 0;\n } else {\n if (h->slice_table[topleft_xy] != sl->slice_num) {\n sl->topleft_type = 0;\n if (h->slice_table[top_xy] != sl->slice_num)\n sl->top_type = 0;\n if (h->slice_table[left_xy[LTOP]] != sl->slice_num)\n sl->left_type[LTOP] = sl->left_type[LBOT] = 0;\n }\n }\n if (h->slice_table[topright_xy] != sl->slice_num)\n sl->topright_type = 0;\n}', 'static void fill_decode_caches(const H264Context *h, H264SliceContext *sl, int mb_type)\n{\n int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];\n int topleft_type, top_type, topright_type, left_type[LEFT_MBS];\n const uint8_t *left_block = sl->left_block;\n int i;\n uint8_t *nnz;\n uint8_t *nnz_cache;\n topleft_xy = sl->topleft_mb_xy;\n top_xy = sl->top_mb_xy;\n topright_xy = sl->topright_mb_xy;\n left_xy[LTOP] = sl->left_mb_xy[LTOP];\n left_xy[LBOT] = sl->left_mb_xy[LBOT];\n topleft_type = sl->topleft_type;\n top_type = sl->top_type;\n topright_type = sl->topright_type;\n left_type[LTOP] = sl->left_type[LTOP];\n left_type[LBOT] = sl->left_type[LBOT];\n if (!IS_SKIP(mb_type)) {\n if (IS_INTRA(mb_type)) {\n int type_mask = h->ps.pps->constrained_intra_pred ? IS_INTRA(-1) : -1;\n sl->topleft_samples_available =\n sl->top_samples_available =\n sl->left_samples_available = 0xFFFF;\n sl->topright_samples_available = 0xEEEA;\n if (!(top_type & type_mask)) {\n sl->topleft_samples_available = 0xB3FF;\n sl->top_samples_available = 0x33FF;\n sl->topright_samples_available = 0x26EA;\n }\n if (IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[LTOP])) {\n if (IS_INTERLACED(mb_type)) {\n if (!(left_type[LTOP] & type_mask)) {\n sl->topleft_samples_available &= 0xDFFF;\n sl->left_samples_available &= 0x5FFF;\n }\n if (!(left_type[LBOT] & type_mask)) {\n sl->topleft_samples_available &= 0xFF5F;\n sl->left_samples_available &= 0xFF5F;\n }\n } else {\n int left_typei = h->cur_pic.mb_type[left_xy[LTOP] + h->mb_stride];\n assert(left_xy[LTOP] == left_xy[LBOT]);\n if (!((left_typei & type_mask) && (left_type[LTOP] & type_mask))) {\n sl->topleft_samples_available &= 0xDF5F;\n sl->left_samples_available &= 0x5F5F;\n }\n }\n } else {\n if (!(left_type[LTOP] & type_mask)) {\n sl->topleft_samples_available &= 0xDF5F;\n sl->left_samples_available &= 0x5F5F;\n }\n }\n if (!(topleft_type & type_mask))\n sl->topleft_samples_available &= 0x7FFF;\n if (!(topright_type & type_mask))\n sl->topright_samples_available &= 0xFBFF;\n if (IS_INTRA4x4(mb_type)) {\n if (IS_INTRA4x4(top_type)) {\n AV_COPY32(sl->intra4x4_pred_mode_cache + 4 + 8 * 0, sl->intra4x4_pred_mode + h->mb2br_xy[top_xy]);\n } else {\n sl->intra4x4_pred_mode_cache[4 + 8 * 0] =\n sl->intra4x4_pred_mode_cache[5 + 8 * 0] =\n sl->intra4x4_pred_mode_cache[6 + 8 * 0] =\n sl->intra4x4_pred_mode_cache[7 + 8 * 0] = 2 - 3 * !(top_type & type_mask);\n }\n for (i = 0; i < 2; i++) {\n if (IS_INTRA4x4(left_type[LEFT(i)])) {\n int8_t *mode = sl->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]];\n sl->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] = mode[6 - left_block[0 + 2 * i]];\n sl->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = mode[6 - left_block[1 + 2 * i]];\n } else {\n sl->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] =\n sl->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = 2 - 3 * !(left_type[LEFT(i)] & type_mask);\n }\n }\n }\n }\n nnz_cache = sl->non_zero_count_cache;\n if (top_type) {\n nnz = h->non_zero_count[top_xy];\n AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[4 * 3]);\n if (!h->chroma_y_shift) {\n AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 7]);\n AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 11]);\n } else {\n AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 5]);\n AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);\n }\n } else {\n uint32_t top_empty = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 0x40404040;\n AV_WN32A(&nnz_cache[4 + 8 * 0], top_empty);\n AV_WN32A(&nnz_cache[4 + 8 * 5], top_empty);\n AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);\n }\n for (i = 0; i < 2; i++) {\n if (left_type[LEFT(i)]) {\n nnz = h->non_zero_count[left_xy[LEFT(i)]];\n nnz_cache[3 + 8 * 1 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i]];\n nnz_cache[3 + 8 * 2 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i]];\n if (CHROMA444(h)) {\n nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 4 * 4];\n nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 4 * 4];\n nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 8 * 4];\n nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 8 * 4];\n } else if (CHROMA422(h)) {\n nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 4 * 4];\n nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 4 * 4];\n nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 8 * 4];\n nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 8 * 4];\n } else {\n nnz_cache[3 + 8 * 6 + 8 * i] = nnz[left_block[8 + 4 + 2 * i]];\n nnz_cache[3 + 8 * 11 + 8 * i] = nnz[left_block[8 + 5 + 2 * i]];\n }\n } else {\n nnz_cache[3 + 8 * 1 + 2 * 8 * i] =\n nnz_cache[3 + 8 * 2 + 2 * 8 * i] =\n nnz_cache[3 + 8 * 6 + 2 * 8 * i] =\n nnz_cache[3 + 8 * 7 + 2 * 8 * i] =\n nnz_cache[3 + 8 * 11 + 2 * 8 * i] =\n nnz_cache[3 + 8 * 12 + 2 * 8 * i] = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 64;\n }\n }\n if (CABAC(h)) {\n if (top_type)\n sl->top_cbp = h->cbp_table[top_xy];\n else\n sl->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;\n if (left_type[LTOP]) {\n sl->left_cbp = (h->cbp_table[left_xy[LTOP]] & 0x7F0) |\n ((h->cbp_table[left_xy[LTOP]] >> (left_block[0] & (~1))) & 2) |\n (((h->cbp_table[left_xy[LBOT]] >> (left_block[2] & (~1))) & 2) << 2);\n } else {\n sl->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;\n }\n }\n }\n if (IS_INTER(mb_type) || (IS_DIRECT(mb_type) && sl->direct_spatial_mv_pred)) {\n int list;\n int b_stride = h->b_stride;\n for (list = 0; list < sl->list_count; list++) {\n int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];\n int8_t *ref = h->cur_pic.ref_index[list];\n int16_t(*mv_cache)[2] = &sl->mv_cache[list][scan8[0]];\n int16_t(*mv)[2] = h->cur_pic.motion_val[list];\n if (!USES_LIST(mb_type, list))\n continue;\n assert(!(IS_DIRECT(mb_type) && !sl->direct_spatial_mv_pred));\n if (USES_LIST(top_type, list)) {\n const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;\n AV_COPY128(mv_cache[0 - 1 * 8], mv[b_xy + 0]);\n ref_cache[0 - 1 * 8] =\n ref_cache[1 - 1 * 8] = ref[4 * top_xy + 2];\n ref_cache[2 - 1 * 8] =\n ref_cache[3 - 1 * 8] = ref[4 * top_xy + 3];\n } else {\n AV_ZERO128(mv_cache[0 - 1 * 8]);\n AV_WN32A(&ref_cache[0 - 1 * 8],\n ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE) & 0xFF) * 0x01010101u);\n }\n if (mb_type & (MB_TYPE_16x8 | MB_TYPE_8x8)) {\n for (i = 0; i < 2; i++) {\n int cache_idx = -1 + i * 2 * 8;\n if (USES_LIST(left_type[LEFT(i)], list)) {\n const int b_xy = h->mb2b_xy[left_xy[LEFT(i)]] + 3;\n const int b8_xy = 4 * left_xy[LEFT(i)] + 1;\n AV_COPY32(mv_cache[cache_idx],\n mv[b_xy + b_stride * left_block[0 + i * 2]]);\n AV_COPY32(mv_cache[cache_idx + 8],\n mv[b_xy + b_stride * left_block[1 + i * 2]]);\n ref_cache[cache_idx] = ref[b8_xy + (left_block[0 + i * 2] & ~1)];\n ref_cache[cache_idx + 8] = ref[b8_xy + (left_block[1 + i * 2] & ~1)];\n } else {\n AV_ZERO32(mv_cache[cache_idx]);\n AV_ZERO32(mv_cache[cache_idx + 8]);\n ref_cache[cache_idx] =\n ref_cache[cache_idx + 8] = (left_type[LEFT(i)]) ? LIST_NOT_USED\n : PART_NOT_AVAILABLE;\n }\n }\n } else {\n if (USES_LIST(left_type[LTOP], list)) {\n const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;\n const int b8_xy = 4 * left_xy[LTOP] + 1;\n AV_COPY32(mv_cache[-1], mv[b_xy + b_stride * left_block[0]]);\n ref_cache[-1] = ref[b8_xy + (left_block[0] & ~1)];\n } else {\n AV_ZERO32(mv_cache[-1]);\n ref_cache[-1] = left_type[LTOP] ? LIST_NOT_USED\n : PART_NOT_AVAILABLE;\n }\n }\n if (USES_LIST(topright_type, list)) {\n const int b_xy = h->mb2b_xy[topright_xy] + 3 * b_stride;\n AV_COPY32(mv_cache[4 - 1 * 8], mv[b_xy]);\n ref_cache[4 - 1 * 8] = ref[4 * topright_xy + 2];\n } else {\n AV_ZERO32(mv_cache[4 - 1 * 8]);\n ref_cache[4 - 1 * 8] = topright_type ? LIST_NOT_USED\n : PART_NOT_AVAILABLE;\n }\n if (ref_cache[4 - 1 * 8] < 0) {\n if (USES_LIST(topleft_type, list)) {\n const int b_xy = h->mb2b_xy[topleft_xy] + 3 + b_stride +\n (sl->topleft_partition & 2 * b_stride);\n const int b8_xy = 4 * topleft_xy + 1 + (sl->topleft_partition & 2);\n AV_COPY32(mv_cache[-1 - 1 * 8], mv[b_xy]);\n ref_cache[-1 - 1 * 8] = ref[b8_xy];\n } else {\n AV_ZERO32(mv_cache[-1 - 1 * 8]);\n ref_cache[-1 - 1 * 8] = topleft_type ? LIST_NOT_USED\n : PART_NOT_AVAILABLE;\n }\n }\n if ((mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2)) && !FRAME_MBAFF(h))\n continue;\n if (!(mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2))) {\n uint8_t(*mvd_cache)[2] = &sl->mvd_cache[list][scan8[0]];\n uint8_t(*mvd)[2] = sl->mvd_table[list];\n ref_cache[2 + 8 * 0] =\n ref_cache[2 + 8 * 2] = PART_NOT_AVAILABLE;\n AV_ZERO32(mv_cache[2 + 8 * 0]);\n AV_ZERO32(mv_cache[2 + 8 * 2]);\n if (CABAC(h)) {\n if (USES_LIST(top_type, list)) {\n const int b_xy = h->mb2br_xy[top_xy];\n AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);\n } else {\n AV_ZERO64(mvd_cache[0 - 1 * 8]);\n }\n if (USES_LIST(left_type[LTOP], list)) {\n const int b_xy = h->mb2br_xy[left_xy[LTOP]] + 6;\n AV_COPY16(mvd_cache[-1 + 0 * 8], mvd[b_xy - left_block[0]]);\n AV_COPY16(mvd_cache[-1 + 1 * 8], mvd[b_xy - left_block[1]]);\n } else {\n AV_ZERO16(mvd_cache[-1 + 0 * 8]);\n AV_ZERO16(mvd_cache[-1 + 1 * 8]);\n }\n if (USES_LIST(left_type[LBOT], list)) {\n const int b_xy = h->mb2br_xy[left_xy[LBOT]] + 6;\n AV_COPY16(mvd_cache[-1 + 2 * 8], mvd[b_xy - left_block[2]]);\n AV_COPY16(mvd_cache[-1 + 3 * 8], mvd[b_xy - left_block[3]]);\n } else {\n AV_ZERO16(mvd_cache[-1 + 2 * 8]);\n AV_ZERO16(mvd_cache[-1 + 3 * 8]);\n }\n AV_ZERO16(mvd_cache[2 + 8 * 0]);\n AV_ZERO16(mvd_cache[2 + 8 * 2]);\n if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {\n uint8_t *direct_cache = &sl->direct_cache[scan8[0]];\n uint8_t *direct_table = h->direct_table;\n fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16 >> 1, 1);\n if (IS_DIRECT(top_type)) {\n AV_WN32A(&direct_cache[-1 * 8],\n 0x01010101u * (MB_TYPE_DIRECT2 >> 1));\n } else if (IS_8X8(top_type)) {\n int b8_xy = 4 * top_xy;\n direct_cache[0 - 1 * 8] = direct_table[b8_xy + 2];\n direct_cache[2 - 1 * 8] = direct_table[b8_xy + 3];\n } else {\n AV_WN32A(&direct_cache[-1 * 8],\n 0x01010101 * (MB_TYPE_16x16 >> 1));\n }\n if (IS_DIRECT(left_type[LTOP]))\n direct_cache[-1 + 0 * 8] = MB_TYPE_DIRECT2 >> 1;\n else if (IS_8X8(left_type[LTOP]))\n direct_cache[-1 + 0 * 8] = direct_table[4 * left_xy[LTOP] + 1 + (left_block[0] & ~1)];\n else\n direct_cache[-1 + 0 * 8] = MB_TYPE_16x16 >> 1;\n if (IS_DIRECT(left_type[LBOT]))\n direct_cache[-1 + 2 * 8] = MB_TYPE_DIRECT2 >> 1;\n else if (IS_8X8(left_type[LBOT]))\n direct_cache[-1 + 2 * 8] = direct_table[4 * left_xy[LBOT] + 1 + (left_block[2] & ~1)];\n else\n direct_cache[-1 + 2 * 8] = MB_TYPE_16x16 >> 1;\n }\n }\n }\n#define MAP_MVS \\\n MAP_F2F(scan8[0] - 1 - 1 * 8, topleft_type) \\\n MAP_F2F(scan8[0] + 0 - 1 * 8, top_type) \\\n MAP_F2F(scan8[0] + 1 - 1 * 8, top_type) \\\n MAP_F2F(scan8[0] + 2 - 1 * 8, top_type) \\\n MAP_F2F(scan8[0] + 3 - 1 * 8, top_type) \\\n MAP_F2F(scan8[0] + 4 - 1 * 8, topright_type) \\\n MAP_F2F(scan8[0] - 1 + 0 * 8, left_type[LTOP]) \\\n MAP_F2F(scan8[0] - 1 + 1 * 8, left_type[LTOP]) \\\n MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT]) \\\n MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])\n if (FRAME_MBAFF(h)) {\n if (MB_FIELD(sl)) {\n#define MAP_F2F(idx, mb_type) \\\n if (!IS_INTERLACED(mb_type) && sl->ref_cache[list][idx] >= 0) { \\\n sl->ref_cache[list][idx] <<= 1; \\\n sl->mv_cache[list][idx][1] /= 2; \\\n sl->mvd_cache[list][idx][1] >>= 1; \\\n }\n MAP_MVS\n } else {\n#undef MAP_F2F\n#define MAP_F2F(idx, mb_type) \\\n if (IS_INTERLACED(mb_type) && sl->ref_cache[list][idx] >= 0) { \\\n sl->ref_cache[list][idx] >>= 1; \\\n sl->mv_cache[list][idx][1] <<= 1; \\\n sl->mvd_cache[list][idx][1] <<= 1; \\\n }\n MAP_MVS\n#undef MAP_F2F\n }\n }\n }\n }\n sl->neighbor_transform_size = !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[LTOP]);\n}'] |
2,293 | 0 | https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_lib.c/#L250 | int BN_num_bits(const BIGNUM *a)
{
int i = a->top - 1;
bn_check_top(a);
if (BN_is_zero(a)) return 0;
return ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));
} | ['EC_POINT *EC_POINT_bn2point(const EC_GROUP *group,\n const BIGNUM *bn,\n EC_POINT *point,\n BN_CTX *ctx)\n\t{\n\tsize_t buf_len=0;\n\tunsigned char *buf;\n\tEC_POINT *ret;\n\tif ((buf_len = BN_num_bytes(bn)) == 0) return NULL;\n\tbuf = OPENSSL_malloc(buf_len);\n\tif (buf == NULL)\n\t\treturn NULL;\n\tif (!BN_bn2bin(bn, buf))\n\t\t{\n\t\tOPENSSL_free(buf);\n\t\treturn NULL;\n\t\t}\n\tif (point == NULL)\n\t\t{\n\t\tif ((ret = EC_POINT_new(group)) == NULL)\n\t\t\t{\n\t\t\tOPENSSL_free(buf);\n\t\t\treturn NULL;\n\t\t\t}\n\t\t}\n\telse\n\t\tret = point;\n\tif (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx))\n\t\t{\n\t\tif (point == NULL)\n\t\t\tEC_POINT_clear_free(ret);\n\t\tOPENSSL_free(buf);\n\t\treturn NULL;\n\t\t}\n\tOPENSSL_free(buf);\n\treturn ret;\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tint i = a->top - 1;\n\tbn_check_top(a);\n\tif (BN_is_zero(a)) return 0;\n\treturn ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));\n\t}', 'int BN_bn2bin(const BIGNUM *a, unsigned char *to)\n\t{\n\tint n,i;\n\tBN_ULONG l;\n\tbn_check_top(a);\n\tn=i=BN_num_bytes(a);\n\twhile (i--)\n\t\t{\n\t\tl=a->d[i/BN_BYTES];\n\t\t*(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff;\n\t\t}\n\treturn(n);\n\t}'] |
2,294 | 0 | https://github.com/libav/libav/blob/df84d7d9bdf6b8e6896c711880f130b72738c828/libavcodec/mpegaudiodec.c/#L737 | static void dct32(int32_t *out, int32_t *tab)
{
int tmp0, tmp1;
BF( 0, 31, COS0_0 , 1);
BF(15, 16, COS0_15, 5);
BF( 0, 15, COS1_0 , 1);
BF(16, 31,-COS1_0 , 1);
BF( 7, 24, COS0_7 , 1);
BF( 8, 23, COS0_8 , 1);
BF( 7, 8, COS1_7 , 4);
BF(23, 24,-COS1_7 , 4);
BF( 0, 7, COS2_0 , 1);
BF( 8, 15,-COS2_0 , 1);
BF(16, 23, COS2_0 , 1);
BF(24, 31,-COS2_0 , 1);
BF( 3, 28, COS0_3 , 1);
BF(12, 19, COS0_12, 2);
BF( 3, 12, COS1_3 , 1);
BF(19, 28,-COS1_3 , 1);
BF( 4, 27, COS0_4 , 1);
BF(11, 20, COS0_11, 2);
BF( 4, 11, COS1_4 , 1);
BF(20, 27,-COS1_4 , 1);
BF( 3, 4, COS2_3 , 3);
BF(11, 12,-COS2_3 , 3);
BF(19, 20, COS2_3 , 3);
BF(27, 28,-COS2_3 , 3);
BF( 0, 3, COS3_0 , 1);
BF( 4, 7,-COS3_0 , 1);
BF( 8, 11, COS3_0 , 1);
BF(12, 15,-COS3_0 , 1);
BF(16, 19, COS3_0 , 1);
BF(20, 23,-COS3_0 , 1);
BF(24, 27, COS3_0 , 1);
BF(28, 31,-COS3_0 , 1);
BF( 1, 30, COS0_1 , 1);
BF(14, 17, COS0_14, 3);
BF( 1, 14, COS1_1 , 1);
BF(17, 30,-COS1_1 , 1);
BF( 6, 25, COS0_6 , 1);
BF( 9, 22, COS0_9 , 1);
BF( 6, 9, COS1_6 , 2);
BF(22, 25,-COS1_6 , 2);
BF( 1, 6, COS2_1 , 1);
BF( 9, 14,-COS2_1 , 1);
BF(17, 22, COS2_1 , 1);
BF(25, 30,-COS2_1 , 1);
BF( 2, 29, COS0_2 , 1);
BF(13, 18, COS0_13, 3);
BF( 2, 13, COS1_2 , 1);
BF(18, 29,-COS1_2 , 1);
BF( 5, 26, COS0_5 , 1);
BF(10, 21, COS0_10, 1);
BF( 5, 10, COS1_5 , 2);
BF(21, 26,-COS1_5 , 2);
BF( 2, 5, COS2_2 , 1);
BF(10, 13,-COS2_2 , 1);
BF(18, 21, COS2_2 , 1);
BF(26, 29,-COS2_2 , 1);
BF( 1, 2, COS3_1 , 2);
BF( 5, 6,-COS3_1 , 2);
BF( 9, 10, COS3_1 , 2);
BF(13, 14,-COS3_1 , 2);
BF(17, 18, COS3_1 , 2);
BF(21, 22,-COS3_1 , 2);
BF(25, 26, COS3_1 , 2);
BF(29, 30,-COS3_1 , 2);
BF1( 0, 1, 2, 3);
BF2( 4, 5, 6, 7);
BF1( 8, 9, 10, 11);
BF2(12, 13, 14, 15);
BF1(16, 17, 18, 19);
BF2(20, 21, 22, 23);
BF1(24, 25, 26, 27);
BF2(28, 29, 30, 31);
ADD( 8, 12);
ADD(12, 10);
ADD(10, 14);
ADD(14, 9);
ADD( 9, 13);
ADD(13, 11);
ADD(11, 15);
out[ 0] = tab[0];
out[16] = tab[1];
out[ 8] = tab[2];
out[24] = tab[3];
out[ 4] = tab[4];
out[20] = tab[5];
out[12] = tab[6];
out[28] = tab[7];
out[ 2] = tab[8];
out[18] = tab[9];
out[10] = tab[10];
out[26] = tab[11];
out[ 6] = tab[12];
out[22] = tab[13];
out[14] = tab[14];
out[30] = tab[15];
ADD(24, 28);
ADD(28, 26);
ADD(26, 30);
ADD(30, 25);
ADD(25, 29);
ADD(29, 27);
ADD(27, 31);
out[ 1] = tab[16] + tab[24];
out[17] = tab[17] + tab[25];
out[ 9] = tab[18] + tab[26];
out[25] = tab[19] + tab[27];
out[ 5] = tab[20] + tab[28];
out[21] = tab[21] + tab[29];
out[13] = tab[22] + tab[30];
out[29] = tab[23] + tab[31];
out[ 3] = tab[24] + tab[20];
out[19] = tab[25] + tab[21];
out[11] = tab[26] + tab[22];
out[27] = tab[27] + tab[23];
out[ 7] = tab[28] + tab[18];
out[23] = tab[29] + tab[19];
out[15] = tab[30] + tab[17];
out[31] = tab[31];
} | ['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 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 int32_t 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 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\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(MPA_INT));\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}', 'static void dct32(int32_t *out, int32_t *tab)\n{\n int tmp0, tmp1;\n BF( 0, 31, COS0_0 , 1);\n BF(15, 16, COS0_15, 5);\n BF( 0, 15, COS1_0 , 1);\n BF(16, 31,-COS1_0 , 1);\n BF( 7, 24, COS0_7 , 1);\n BF( 8, 23, COS0_8 , 1);\n BF( 7, 8, COS1_7 , 4);\n BF(23, 24,-COS1_7 , 4);\n BF( 0, 7, COS2_0 , 1);\n BF( 8, 15,-COS2_0 , 1);\n BF(16, 23, COS2_0 , 1);\n BF(24, 31,-COS2_0 , 1);\n BF( 3, 28, COS0_3 , 1);\n BF(12, 19, COS0_12, 2);\n BF( 3, 12, COS1_3 , 1);\n BF(19, 28,-COS1_3 , 1);\n BF( 4, 27, COS0_4 , 1);\n BF(11, 20, COS0_11, 2);\n BF( 4, 11, COS1_4 , 1);\n BF(20, 27,-COS1_4 , 1);\n BF( 3, 4, COS2_3 , 3);\n BF(11, 12,-COS2_3 , 3);\n BF(19, 20, COS2_3 , 3);\n BF(27, 28,-COS2_3 , 3);\n BF( 0, 3, COS3_0 , 1);\n BF( 4, 7,-COS3_0 , 1);\n BF( 8, 11, COS3_0 , 1);\n BF(12, 15,-COS3_0 , 1);\n BF(16, 19, COS3_0 , 1);\n BF(20, 23,-COS3_0 , 1);\n BF(24, 27, COS3_0 , 1);\n BF(28, 31,-COS3_0 , 1);\n BF( 1, 30, COS0_1 , 1);\n BF(14, 17, COS0_14, 3);\n BF( 1, 14, COS1_1 , 1);\n BF(17, 30,-COS1_1 , 1);\n BF( 6, 25, COS0_6 , 1);\n BF( 9, 22, COS0_9 , 1);\n BF( 6, 9, COS1_6 , 2);\n BF(22, 25,-COS1_6 , 2);\n BF( 1, 6, COS2_1 , 1);\n BF( 9, 14,-COS2_1 , 1);\n BF(17, 22, COS2_1 , 1);\n BF(25, 30,-COS2_1 , 1);\n BF( 2, 29, COS0_2 , 1);\n BF(13, 18, COS0_13, 3);\n BF( 2, 13, COS1_2 , 1);\n BF(18, 29,-COS1_2 , 1);\n BF( 5, 26, COS0_5 , 1);\n BF(10, 21, COS0_10, 1);\n BF( 5, 10, COS1_5 , 2);\n BF(21, 26,-COS1_5 , 2);\n BF( 2, 5, COS2_2 , 1);\n BF(10, 13,-COS2_2 , 1);\n BF(18, 21, COS2_2 , 1);\n BF(26, 29,-COS2_2 , 1);\n BF( 1, 2, COS3_1 , 2);\n BF( 5, 6,-COS3_1 , 2);\n BF( 9, 10, COS3_1 , 2);\n BF(13, 14,-COS3_1 , 2);\n BF(17, 18, COS3_1 , 2);\n BF(21, 22,-COS3_1 , 2);\n BF(25, 26, COS3_1 , 2);\n BF(29, 30,-COS3_1 , 2);\n BF1( 0, 1, 2, 3);\n BF2( 4, 5, 6, 7);\n BF1( 8, 9, 10, 11);\n BF2(12, 13, 14, 15);\n BF1(16, 17, 18, 19);\n BF2(20, 21, 22, 23);\n BF1(24, 25, 26, 27);\n BF2(28, 29, 30, 31);\n ADD( 8, 12);\n ADD(12, 10);\n ADD(10, 14);\n ADD(14, 9);\n ADD( 9, 13);\n ADD(13, 11);\n ADD(11, 15);\n out[ 0] = tab[0];\n out[16] = tab[1];\n out[ 8] = tab[2];\n out[24] = tab[3];\n out[ 4] = tab[4];\n out[20] = tab[5];\n out[12] = tab[6];\n out[28] = tab[7];\n out[ 2] = tab[8];\n out[18] = tab[9];\n out[10] = tab[10];\n out[26] = tab[11];\n out[ 6] = tab[12];\n out[22] = tab[13];\n out[14] = tab[14];\n out[30] = tab[15];\n ADD(24, 28);\n ADD(28, 26);\n ADD(26, 30);\n ADD(30, 25);\n ADD(25, 29);\n ADD(29, 27);\n ADD(27, 31);\n out[ 1] = tab[16] + tab[24];\n out[17] = tab[17] + tab[25];\n out[ 9] = tab[18] + tab[26];\n out[25] = tab[19] + tab[27];\n out[ 5] = tab[20] + tab[28];\n out[21] = tab[21] + tab[29];\n out[13] = tab[22] + tab[30];\n out[29] = tab[23] + tab[31];\n out[ 3] = tab[24] + tab[20];\n out[19] = tab[25] + tab[21];\n out[11] = tab[26] + tab[22];\n out[27] = tab[27] + tab[23];\n out[ 7] = tab[28] + tab[18];\n out[23] = tab[29] + tab[19];\n out[15] = tab[30] + tab[17];\n out[31] = tab[31];\n}'] |
2,295 | 0 | https://github.com/openssl/openssl/blob/0f3ffbd1581fad58095fedcc32b0da42a486b8b7/test/evp_extra_test.c/#L266 | static int test_EVP_DigestSignInit(void)
{
int ret = 0;
EVP_PKEY *pkey = NULL;
unsigned char *sig = NULL;
size_t sig_len = 0;
EVP_MD_CTX *md_ctx, *md_ctx_verify = NULL;
if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())
|| !TEST_ptr(md_ctx_verify = EVP_MD_CTX_new())
|| !TEST_ptr(pkey = load_example_rsa_key()))
goto out;
if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, EVP_sha256(), NULL, pkey))
|| !TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
goto out;
if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len))
|| !TEST_size_t_eq(sig_len, (size_t)EVP_PKEY_size(pkey)))
goto out;
if (!TEST_ptr(sig = OPENSSL_malloc(sig_len))
|| !TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
goto out;
if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sha256(),
NULL, pkey))
|| !TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify,
kMsg, sizeof(kMsg)))
|| !TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))
goto out;
ret = 1;
out:
EVP_MD_CTX_free(md_ctx);
EVP_MD_CTX_free(md_ctx_verify);
EVP_PKEY_free(pkey);
OPENSSL_free(sig);
return ret;
} | ['static int test_EVP_DigestSignInit(void)\n{\n int ret = 0;\n EVP_PKEY *pkey = NULL;\n unsigned char *sig = NULL;\n size_t sig_len = 0;\n EVP_MD_CTX *md_ctx, *md_ctx_verify = NULL;\n if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())\n || !TEST_ptr(md_ctx_verify = EVP_MD_CTX_new())\n || !TEST_ptr(pkey = load_example_rsa_key()))\n goto out;\n if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, EVP_sha256(), NULL, pkey))\n || !TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))\n goto out;\n if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len))\n || !TEST_size_t_eq(sig_len, (size_t)EVP_PKEY_size(pkey)))\n goto out;\n if (!TEST_ptr(sig = OPENSSL_malloc(sig_len))\n || !TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))\n goto out;\n if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sha256(),\n NULL, pkey))\n || !TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify,\n kMsg, sizeof(kMsg)))\n || !TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))\n goto out;\n ret = 1;\n out:\n EVP_MD_CTX_free(md_ctx);\n EVP_MD_CTX_free(md_ctx_verify);\n EVP_PKEY_free(pkey);\n OPENSSL_free(sig);\n return ret;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int test_ptr(const char *file, int line, const char *s, const void *p)\n{\n if (p != NULL)\n return 1;\n test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);\n return 0;\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx);\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}'] |
2,296 | 0 | https://github.com/openssl/openssl/blob/5850cc75ea0c1581a9034390f1ca77cadc596238/crypto/bn/bn_ctx.c/#L328 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,\n EC_POINT *points[], BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp, *tmp_Z;\n BIGNUM **prod_Z = NULL;\n size_t i;\n int ret = 0;\n if (num == 0)\n return 1;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n tmp_Z = BN_CTX_get(ctx);\n if (tmp == NULL || tmp_Z == NULL)\n goto err;\n prod_Z = OPENSSL_malloc(num * sizeof prod_Z[0]);\n if (prod_Z == NULL)\n goto err;\n for (i = 0; i < num; i++) {\n prod_Z[i] = BN_new();\n if (prod_Z[i] == NULL)\n goto err;\n }\n if (!BN_is_zero(points[0]->Z)) {\n if (!BN_copy(prod_Z[0], points[0]->Z))\n goto err;\n } else {\n if (group->meth->field_set_to_one != 0) {\n if (!group->meth->field_set_to_one(group, prod_Z[0], ctx))\n goto err;\n } else {\n if (!BN_one(prod_Z[0]))\n goto err;\n }\n }\n for (i = 1; i < num; i++) {\n if (!BN_is_zero(points[i]->Z)) {\n if (!group->\n meth->field_mul(group, prod_Z[i], prod_Z[i - 1], points[i]->Z,\n ctx))\n goto err;\n } else {\n if (!BN_copy(prod_Z[i], prod_Z[i - 1]))\n goto err;\n }\n }\n if (!BN_mod_inverse(tmp, prod_Z[num - 1], group->field, ctx)) {\n ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);\n goto err;\n }\n if (group->meth->field_encode != 0) {\n if (!group->meth->field_encode(group, tmp, tmp, ctx))\n goto err;\n if (!group->meth->field_encode(group, tmp, tmp, ctx))\n goto err;\n }\n for (i = num - 1; i > 0; --i) {\n if (!BN_is_zero(points[i]->Z)) {\n if (!group->\n meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp, tmp, points[i]->Z, ctx))\n goto err;\n if (!BN_copy(points[i]->Z, tmp_Z))\n goto err;\n }\n }\n if (!BN_is_zero(points[0]->Z)) {\n if (!BN_copy(points[0]->Z, tmp))\n goto err;\n }\n for (i = 0; i < num; i++) {\n EC_POINT *p = points[i];\n if (!BN_is_zero(p->Z)) {\n if (!group->meth->field_sqr(group, tmp, p->Z, ctx))\n goto err;\n if (!group->meth->field_mul(group, p->X, p->X, tmp, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp, tmp, p->Z, ctx))\n goto err;\n if (!group->meth->field_mul(group, p->Y, p->Y, tmp, ctx))\n goto err;\n if (group->meth->field_set_to_one != 0) {\n if (!group->meth->field_set_to_one(group, p->Z, ctx))\n goto err;\n } else {\n if (!BN_one(p->Z))\n goto err;\n }\n p->Z_is_one = 1;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n if (prod_Z != NULL) {\n for (i = 0; i < num; i++) {\n if (prod_Z[i] == NULL)\n break;\n BN_clear_free(prod_Z[i]);\n }\n OPENSSL_free(prod_Z);\n }\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}', '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) <= (BN_BITS <= 32 ? 450 : 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 local_A, local_B;\n BIGNUM *pA, *pB;\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 pB = &local_B;\n local_B.flags = 0;\n BN_with_flags(pB, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, pB, A, ctx))\n goto err;\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n pA = &local_A;\n local_A.flags = 0;\n BN_with_flags(pA, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, pA, B, ctx))\n goto err;\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 tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
2,297 | 0 | https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/x509/x509_lu.c/#L241 | void X509_STORE_free(X509_STORE *vfy)
{
int i;
STACK *sk;
X509_LOOKUP *lu;
if(vfy == NULL)
return;
sk=vfy->get_cert_methods;
for (i=0; i<sk_num(sk); i++)
{
lu=(X509_LOOKUP *)sk_value(sk,i);
X509_LOOKUP_shutdown(lu);
X509_LOOKUP_free(lu);
}
sk_free(sk);
CRYPTO_free_ex_data(x509_store_meth,(char *)vfy,&vfy->ex_data);
lh_doall(vfy->certs,cleanup);
lh_free(vfy->certs);
Free(vfy);
} | ['int MAIN(int argc, char **argv)\n\t{\n\tint i,ret=1;\n\tchar *CApath=NULL,*CAfile=NULL;\n\tX509_STORE *cert_ctx=NULL;\n\tX509_LOOKUP *lookup=NULL;\n\tcert_ctx=X509_STORE_new();\n\tif (cert_ctx == NULL) goto end;\n\tX509_STORE_set_verify_cb_func(cert_ctx,cb);\n\tERR_load_crypto_strings();\n\tapps_startup();\n\tif (bio_err == NULL)\n\t\tif ((bio_err=BIO_new(BIO_s_file())) != NULL)\n\t\t\tBIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);\n\targc--;\n\targv++;\n\tfor (;;)\n\t\t{\n\t\tif (argc >= 1)\n\t\t\t{\n\t\t\tif (strcmp(*argv,"-CApath") == 0)\n\t\t\t\t{\n\t\t\t\tif (argc-- < 1) goto end;\n\t\t\t\tCApath= *(++argv);\n\t\t\t\t}\n\t\t\telse if (strcmp(*argv,"-CAfile") == 0)\n\t\t\t\t{\n\t\t\t\tif (argc-- < 1) goto end;\n\t\t\t\tCAfile= *(++argv);\n\t\t\t\t}\n\t\t\telse if (strcmp(*argv,"-help") == 0)\n\t\t\t\tgoto end;\n\t\t\telse if (strcmp(*argv,"-verbose") == 0)\n\t\t\t\tv_verbose=1;\n\t\t\telse if (argv[0][0] == \'-\')\n\t\t\t\tgoto end;\n\t\t\telse\n\t\t\t\tbreak;\n\t\t\targc--;\n\t\t\targv++;\n\t\t\t}\n\t\telse\n\t\t\tbreak;\n\t\t}\n\tlookup=X509_STORE_add_lookup(cert_ctx,X509_LOOKUP_file());\n\tif (lookup == NULL) abort();\n\tif (!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM))\n\t\tX509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);\n\tlookup=X509_STORE_add_lookup(cert_ctx,X509_LOOKUP_hash_dir());\n\tif (lookup == NULL) abort();\n\tif (!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM))\n\t\tX509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);\n\tERR_clear_error();\n\tif (argc < 1) check(cert_ctx,NULL);\n\telse\n\t\tfor (i=0; i<argc; i++)\n\t\t\tcheck(cert_ctx,argv[i]);\n\tret=0;\nend:\n\tif (ret == 1)\n\t\tBIO_printf(bio_err,"usage: verify [-verbose] [-CApath path] [-CAfile file] cert1 cert2 ...\\n");\n\tif (cert_ctx != NULL) X509_STORE_free(cert_ctx);\n\tEXIT(ret);\n\t}', 'X509_STORE *X509_STORE_new(void)\n\t{\n\tX509_STORE *ret;\n\tif ((ret=(X509_STORE *)Malloc(sizeof(X509_STORE))) == NULL)\n\t\treturn(NULL);\n\tret->certs=lh_new(x509_object_hash,x509_object_cmp);\n\tret->cache=1;\n\tret->get_cert_methods=sk_new_null();\n\tret->verify=NULL;\n\tret->verify_cb=NULL;\n\tmemset(&ret->ex_data,0,sizeof(CRYPTO_EX_DATA));\n\tret->references=1;\n\tret->depth=0;\n\treturn(ret);\n\t}', 'STACK *sk_new(int (*c)())\n\t{\n\tSTACK *ret;\n\tint i;\n\tif ((ret=(STACK *)Malloc(sizeof(STACK))) == NULL)\n\t\tgoto err0;\n\tif ((ret->data=(char **)Malloc(sizeof(char *)*MIN_NODES)) == NULL)\n\t\tgoto err1;\n\tfor (i=0; i<MIN_NODES; i++)\n\t\tret->data[i]=NULL;\n\tret->comp=c;\n\tret->num_alloc=MIN_NODES;\n\tret->num=0;\n\tret->sorted=0;\n\treturn(ret);\nerr1:\n\tFree((char *)ret);\nerr0:\n\treturn(NULL);\n\t}', 'void X509_STORE_free(X509_STORE *vfy)\n\t{\n\tint i;\n\tSTACK *sk;\n\tX509_LOOKUP *lu;\n\tif(vfy == NULL)\n\t return;\n\tsk=vfy->get_cert_methods;\n\tfor (i=0; i<sk_num(sk); i++)\n\t\t{\n\t\tlu=(X509_LOOKUP *)sk_value(sk,i);\n\t\tX509_LOOKUP_shutdown(lu);\n\t\tX509_LOOKUP_free(lu);\n\t\t}\n\tsk_free(sk);\n\tCRYPTO_free_ex_data(x509_store_meth,(char *)vfy,&vfy->ex_data);\n\tlh_doall(vfy->certs,cleanup);\n\tlh_free(vfy->certs);\n\tFree(vfy);\n\t}'] |
2,298 | 0 | https://gitlab.com/libtiff/libtiff/blob/69ce2652ef2feae25a4569eb57b837dde0a1bd71/libtiff/tif_dirread.c/#L6192 | static int _TIFFFetchStrileValue(TIFF* tif,
uint32 strile,
TIFFDirEntry* dirent,
uint64** parray)
{
static const char module[] = "_TIFFFetchStrileValue";
TIFFDirectory *td = &tif->tif_dir;
if( strile >= dirent->tdir_count )
{
return 0;
}
if( strile >= td->td_stripoffsetbyteallocsize )
{
uint32 nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;
uint32 nStripArrayAllocNew;
uint64 nArraySize64;
size_t nArraySize;
uint64* offsetArray;
uint64* bytecountArray;
if( strile > 1000000 )
{
uint64 filesize = TIFFGetFileSize(tif);
if( strile > filesize / sizeof(uint32) )
{
TIFFErrorExt(tif->tif_clientdata, module, "File too short");
return 0;
}
}
if( td->td_stripoffsetbyteallocsize == 0 &&
td->td_nstrips < 1024 * 1024 )
{
nStripArrayAllocNew = td->td_nstrips;
}
else
{
#define TIFF_MAX(a,b) (((a)>(b)) ? (a) : (b))
#define TIFF_MIN(a,b) (((a)<(b)) ? (a) : (b))
nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U );
if( nStripArrayAllocNew < 0xFFFFFFFFU / 2 )
nStripArrayAllocNew *= 2;
nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);
}
assert( strile < nStripArrayAllocNew );
nArraySize64 = (uint64)sizeof(uint64) * nStripArrayAllocNew;
nArraySize = (size_t)(nArraySize64);
#if SIZEOF_SIZE_T == 4
if( nArraySize != nArraySize64 )
{
TIFFErrorExt(tif->tif_clientdata, module,
"Cannot allocate strip offset and bytecount arrays");
return 0;
}
#endif
offsetArray = (uint64*)(
_TIFFrealloc( td->td_stripoffset_p, nArraySize ) );
bytecountArray = (uint64*)(
_TIFFrealloc( td->td_stripbytecount_p, nArraySize ) );
if( offsetArray )
td->td_stripoffset_p = offsetArray;
if( bytecountArray )
td->td_stripbytecount_p = bytecountArray;
if( offsetArray && bytecountArray )
{
td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;
memset(td->td_stripoffset_p + nStripArrayAllocBefore,
0xFF,
(td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64) );
memset(td->td_stripbytecount_p + nStripArrayAllocBefore,
0xFF,
(td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64) );
}
else
{
TIFFErrorExt(tif->tif_clientdata, module,
"Cannot allocate strip offset and bytecount arrays");
_TIFFfree(td->td_stripoffset_p);
td->td_stripoffset_p = NULL;
_TIFFfree(td->td_stripbytecount_p);
td->td_stripbytecount_p = NULL;
td->td_stripoffsetbyteallocsize = 0;
}
}
if( *parray == NULL || strile >= td->td_stripoffsetbyteallocsize )
return 0;
if( ~((*parray)[strile]) == 0 )
{
if( !_TIFFPartialReadStripArray( tif, dirent, strile, *parray ) )
{
(*parray)[strile] = 0;
return 0;
}
}
return 1;
} | ['int\nmain(int argc, char* argv[])\n{\n\tTIFF *in, *out;\n\tif (argc < 2) {\n fprintf(stderr, "%s\\n\\n", TIFFGetVersion());\n\t\tfprintf(stderr, "usage: tiffsplit input.tif [prefix]\\n");\n\t\treturn (-3);\n\t}\n\tif (argc > 2) {\n\t\tstrncpy(fname, argv[2], sizeof(fname));\n\t\tfname[sizeof(fname) - 1] = \'\\0\';\n\t}\n\tin = TIFFOpen(argv[1], "r");\n\tif (in != NULL) {\n\t\tdo {\n\t\t\tsize_t path_len;\n\t\t\tchar *path;\n\t\t\tnewfilename();\n\t\t\tpath_len = strlen(fname) + sizeof(TIFF_SUFFIX);\n\t\t\tpath = (char *) _TIFFmalloc(path_len);\n\t\t\tstrncpy(path, fname, path_len);\n\t\t\tpath[path_len - 1] = \'\\0\';\n\t\t\tstrncat(path, TIFF_SUFFIX, path_len - strlen(path) - 1);\n\t\t\tout = TIFFOpen(path, TIFFIsBigEndian(in)?"wb":"wl");\n\t\t\t_TIFFfree(path);\n\t\t\tif (out == NULL)\n\t\t\t\treturn (-2);\n\t\t\tif (!tiffcp(in, out))\n\t\t\t\treturn (-1);\n\t\t\tTIFFClose(out);\n\t\t} while (TIFFReadDirectory(in));\n\t\t(void) TIFFClose(in);\n\t}\n\treturn (0);\n}', 'static int\ntiffcp(TIFF* in, TIFF* out)\n{\n\tuint16 bitspersample, samplesperpixel, compression, shortv, *shortav;\n\tuint32 w, l;\n\tfloat floatv;\n\tchar *stringv;\n\tuint32 longv;\n\tCopyField(TIFFTAG_SUBFILETYPE, longv);\n\tCopyField(TIFFTAG_TILEWIDTH, w);\n\tCopyField(TIFFTAG_TILELENGTH, l);\n\tCopyField(TIFFTAG_IMAGEWIDTH, w);\n\tCopyField(TIFFTAG_IMAGELENGTH, l);\n\tCopyField(TIFFTAG_BITSPERSAMPLE, bitspersample);\n\tCopyField(TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);\n\tCopyField(TIFFTAG_COMPRESSION, compression);\n\tif (compression == COMPRESSION_JPEG) {\n\t\tuint32 count = 0;\n\t\tvoid *table = NULL;\n\t\tif (TIFFGetField(in, TIFFTAG_JPEGTABLES, &count, &table)\n\t\t && count > 0 && table) {\n\t\t TIFFSetField(out, TIFFTAG_JPEGTABLES, count, table);\n\t\t}\n\t}\n CopyField(TIFFTAG_PHOTOMETRIC, shortv);\n\tCopyField(TIFFTAG_PREDICTOR, shortv);\n\tCopyField(TIFFTAG_THRESHHOLDING, shortv);\n\tCopyField(TIFFTAG_FILLORDER, shortv);\n\tCopyField(TIFFTAG_ORIENTATION, shortv);\n\tCopyField(TIFFTAG_MINSAMPLEVALUE, shortv);\n\tCopyField(TIFFTAG_MAXSAMPLEVALUE, shortv);\n\tCopyField(TIFFTAG_XRESOLUTION, floatv);\n\tCopyField(TIFFTAG_YRESOLUTION, floatv);\n\tCopyField(TIFFTAG_GROUP3OPTIONS, longv);\n\tCopyField(TIFFTAG_GROUP4OPTIONS, longv);\n\tCopyField(TIFFTAG_RESOLUTIONUNIT, shortv);\n\tCopyField(TIFFTAG_PLANARCONFIG, shortv);\n\tCopyField(TIFFTAG_ROWSPERSTRIP, longv);\n\tCopyField(TIFFTAG_XPOSITION, floatv);\n\tCopyField(TIFFTAG_YPOSITION, floatv);\n\tCopyField(TIFFTAG_IMAGEDEPTH, longv);\n\tCopyField(TIFFTAG_TILEDEPTH, longv);\n\tCopyField(TIFFTAG_SAMPLEFORMAT, shortv);\n\tCopyField2(TIFFTAG_EXTRASAMPLES, shortv, shortav);\n\t{ uint16 *red, *green, *blue;\n\t CopyField3(TIFFTAG_COLORMAP, red, green, blue);\n\t}\n\t{ uint16 shortv2;\n\t CopyField2(TIFFTAG_PAGENUMBER, shortv, shortv2);\n\t}\n\tCopyField(TIFFTAG_ARTIST, stringv);\n\tCopyField(TIFFTAG_IMAGEDESCRIPTION, stringv);\n\tCopyField(TIFFTAG_MAKE, stringv);\n\tCopyField(TIFFTAG_MODEL, stringv);\n\tCopyField(TIFFTAG_SOFTWARE, stringv);\n\tCopyField(TIFFTAG_DATETIME, stringv);\n\tCopyField(TIFFTAG_HOSTCOMPUTER, stringv);\n\tCopyField(TIFFTAG_PAGENAME, stringv);\n\tCopyField(TIFFTAG_DOCUMENTNAME, stringv);\n\tCopyField(TIFFTAG_BADFAXLINES, longv);\n\tCopyField(TIFFTAG_CLEANFAXDATA, longv);\n\tCopyField(TIFFTAG_CONSECUTIVEBADFAXLINES, longv);\n\tCopyField(TIFFTAG_FAXRECVPARAMS, longv);\n\tCopyField(TIFFTAG_FAXRECVTIME, longv);\n\tCopyField(TIFFTAG_FAXSUBADDRESS, stringv);\n\tCopyField(TIFFTAG_FAXDCS, stringv);\n\tif (TIFFIsTiled(in))\n\t\treturn (cpTiles(in, out));\n\telse\n\t\treturn (cpStrips(in, out));\n}', 'static int\ncpTiles(TIFF* in, TIFF* out)\n{\n\ttmsize_t bufsize = TIFFTileSize(in);\n\tunsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);\n\tif (buf) {\n\t\tttile_t t, nt = TIFFNumberOfTiles(in);\n\t\tuint64 *bytecounts;\n\t\tif (!TIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts)) {\n\t\t\tfprintf(stderr, "tiffsplit: tile byte counts are missing\\n");\n _TIFFfree(buf);\n\t\t\treturn (0);\n\t\t}\n\t\tfor (t = 0; t < nt; t++) {\n\t\t\tif (bytecounts[t] > (uint64) bufsize) {\n\t\t\t\tbuf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[t]);\n\t\t\t\tif (!buf)\n\t\t\t\t\treturn (0);\n\t\t\t\tbufsize = (tmsize_t)bytecounts[t];\n\t\t\t}\n\t\t\tif (TIFFReadRawTile(in, t, buf, (tmsize_t)bytecounts[t]) < 0 ||\n\t\t\t TIFFWriteRawTile(out, t, buf, (tmsize_t)bytecounts[t]) < 0) {\n\t\t\t\t_TIFFfree(buf);\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\t_TIFFfree(buf);\n\t\treturn (1);\n\t}\n\treturn (0);\n}', 'int\nTIFFReadDirectory(TIFF* tif)\n{\n\tstatic const char module[] = "TIFFReadDirectory";\n\tTIFFDirEntry* dir;\n\tuint16 dircount;\n\tTIFFDirEntry* dp;\n\tuint16 di;\n\tconst TIFFField* fip;\n\tuint32 fii=FAILED_FII;\n toff_t nextdiroff;\n int bitspersample_read = FALSE;\n int color_channels;\n\ttif->tif_diroff=tif->tif_nextdiroff;\n\tif (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))\n\t\treturn 0;\n\t(*tif->tif_cleanup)(tif);\n\ttif->tif_curdir++;\n nextdiroff = tif->tif_nextdiroff;\n\tdircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);\n\tif (!dircount)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,\n\t\t "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);\n\t\treturn 0;\n\t}\n\tTIFFReadDirectoryCheckOrder(tif,dir,dircount);\n\t{\n\t\tTIFFDirEntry* ma;\n\t\tuint16 mb;\n\t\tfor (ma=dir, mb=0; mb<dircount; ma++, mb++)\n\t\t{\n\t\t\tTIFFDirEntry* na;\n\t\t\tuint16 nb;\n\t\t\tfor (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)\n\t\t\t{\n\t\t\t\tif (ma->tdir_tag==na->tdir_tag)\n\t\t\t\t\tna->tdir_tag=IGNORE;\n\t\t\t}\n\t\t}\n\t}\n\ttif->tif_flags &= ~TIFF_BEENWRITING;\n\ttif->tif_flags &= ~TIFF_BUF4WRITE;\n\ttif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;\n\tTIFFFreeDirectory(tif);\n\tTIFFDefaultDirectory(tif);\n\tTIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);\n\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);\n\tif (dp)\n\t{\n\t\tif (!TIFFFetchNormalTag(tif,dp,0))\n\t\t\tgoto bad;\n\t\tdp->tdir_tag=IGNORE;\n\t}\n\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);\n\tif (dp)\n\t{\n\t\tuint16 value;\n\t\tenum TIFFReadDirEntryErr err;\n\t\terr=TIFFReadDirEntryShort(tif,dp,&value);\n\t\tif (err==TIFFReadDirEntryErrCount)\n\t\t\terr=TIFFReadDirEntryPersampleShort(tif,dp,&value);\n\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t{\n\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);\n\t\t\tgoto bad;\n\t\t}\n\t\tif (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))\n\t\t\tgoto bad;\n\t\tdp->tdir_tag=IGNORE;\n\t}\n\telse\n\t{\n\t\tif (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))\n\t\t\tgoto bad;\n\t}\n\tfor (di=0, dp=dir; di<dircount; di++, dp++)\n\t{\n\t\tif (dp->tdir_tag!=IGNORE)\n\t\t{\n\t\t\tTIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);\n\t\t\tif (fii == FAILED_FII)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t\t "Unknown field with tag %d (0x%x) encountered",\n\t\t\t\t dp->tdir_tag,dp->tdir_tag);\n\t\t\t\tif (!_TIFFMergeFields(tif,\n\t\t\t\t\t_TIFFCreateAnonField(tif,\n\t\t\t\t\t\tdp->tdir_tag,\n\t\t\t\t\t\t(TIFFDataType) dp->tdir_type),\n\t\t\t\t\t1)) {\n\t\t\t\t\tTIFFWarningExt(tif->tif_clientdata,\n\t\t\t\t\t module,\n\t\t\t\t\t "Registering anonymous field with tag %d (0x%x) failed",\n\t\t\t\t\t dp->tdir_tag,\n\t\t\t\t\t dp->tdir_tag);\n\t\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\t\t} else {\n\t\t\t\t\tTIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);\n\t\t\t\t\tassert(fii != FAILED_FII);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dp->tdir_tag!=IGNORE)\n\t\t{\n\t\t\tfip=tif->tif_fields[fii];\n\t\t\tif (fip->field_bit==FIELD_IGNORE)\n\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\telse\n\t\t\t{\n\t\t\t\tswitch (dp->tdir_tag)\n\t\t\t\t{\n\t\t\t\t\tcase TIFFTAG_STRIPOFFSETS:\n\t\t\t\t\tcase TIFFTAG_STRIPBYTECOUNTS:\n\t\t\t\t\tcase TIFFTAG_TILEOFFSETS:\n\t\t\t\t\tcase TIFFTAG_TILEBYTECOUNTS:\n\t\t\t\t\t\tTIFFSetFieldBit(tif,fip->field_bit);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase TIFFTAG_IMAGEWIDTH:\n\t\t\t\t\tcase TIFFTAG_IMAGELENGTH:\n\t\t\t\t\tcase TIFFTAG_IMAGEDEPTH:\n\t\t\t\t\tcase TIFFTAG_TILELENGTH:\n\t\t\t\t\tcase TIFFTAG_TILEWIDTH:\n\t\t\t\t\tcase TIFFTAG_TILEDEPTH:\n\t\t\t\t\tcase TIFFTAG_PLANARCONFIG:\n\t\t\t\t\tcase TIFFTAG_ROWSPERSTRIP:\n\t\t\t\t\tcase TIFFTAG_EXTRASAMPLES:\n\t\t\t\t\t\tif (!TIFFFetchNormalTag(tif,dp,0))\n\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\t\t\t\tbreak;\n default:\n if( !_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag) )\n dp->tdir_tag=IGNORE;\n break;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&\n\t (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))\n\t{\n if (!_TIFFFillStriles(tif))\n goto bad;\n\t\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);\n\t\tif ((dp!=0)&&(dp->tdir_count==1))\n\t\t{\n\t\t\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,\n\t\t\t TIFFTAG_STRIPBYTECOUNTS);\n\t\t\tif ((dp!=0)&&(dp->tdir_count==1))\n\t\t\t{\n\t\t\t\ttif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "Planarconfig tag value assumed incorrect, "\n\t\t\t\t "assuming data is contig instead of chunky");\n\t\t\t}\n\t\t}\n\t}\n\tif (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))\n\t{\n\t\tMissingRequired(tif,"ImageLength");\n\t\tgoto bad;\n\t}\n\tif (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {\n\t\ttif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);\n\t\ttif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;\n\t\ttif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;\n\t\ttif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;\n\t\ttif->tif_flags &= ~TIFF_ISTILED;\n\t} else {\n\t\ttif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);\n\t\ttif->tif_flags |= TIFF_ISTILED;\n\t}\n\tif (!tif->tif_dir.td_nstrips) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "Cannot handle zero number of %s",\n\t\t isTiled(tif) ? "tiles" : "strips");\n\t\tgoto bad;\n\t}\n\ttif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;\n\tif (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\ttif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;\n\tif (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {\n#ifdef OJPEG_SUPPORT\n\t\tif ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&\n\t\t (isTiled(tif)==0) &&\n\t\t (tif->tif_dir.td_nstrips==1)) {\n\t\t\tTIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);\n\t\t} else\n#endif\n {\n\t\t\tMissingRequired(tif,\n\t\t\t\tisTiled(tif) ? "TileOffsets" : "StripOffsets");\n\t\t\tgoto bad;\n\t\t}\n\t}\n\tfor (di=0, dp=dir; di<dircount; di++, dp++)\n\t{\n\t\tswitch (dp->tdir_tag)\n\t\t{\n\t\t\tcase IGNORE:\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_MINSAMPLEVALUE:\n\t\t\tcase TIFFTAG_MAXSAMPLEVALUE:\n\t\t\tcase TIFFTAG_BITSPERSAMPLE:\n\t\t\tcase TIFFTAG_DATATYPE:\n\t\t\tcase TIFFTAG_SAMPLEFORMAT:\n\t\t\t\t{\n\t\t\t\t\tuint16 value;\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\terr=TIFFReadDirEntryShort(tif,dp,&value);\n\t\t\t\t\tif (err==TIFFReadDirEntryErrCount)\n\t\t\t\t\t\terr=TIFFReadDirEntryPersampleShort(tif,dp,&value);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t}\n\t\t\t\t\tif (!TIFFSetField(tif,dp->tdir_tag,value))\n\t\t\t\t\t\tgoto bad;\n if( dp->tdir_tag == TIFFTAG_BITSPERSAMPLE )\n bitspersample_read = TRUE;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_SMINSAMPLEVALUE:\n\t\t\tcase TIFFTAG_SMAXSAMPLEVALUE:\n\t\t\t\t{\n\t\t\t\t\tdouble *data = NULL;\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\tuint32 saved_flags;\n\t\t\t\t\tint m;\n\t\t\t\t\tif (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)\n\t\t\t\t\t\terr = TIFFReadDirEntryErrCount;\n\t\t\t\t\telse\n\t\t\t\t\t\terr = TIFFReadDirEntryDoubleArray(tif, dp, &data);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t}\n\t\t\t\t\tsaved_flags = tif->tif_flags;\n\t\t\t\t\ttif->tif_flags |= TIFF_PERSAMPLE;\n\t\t\t\t\tm = TIFFSetField(tif,dp->tdir_tag,data);\n\t\t\t\t\ttif->tif_flags = saved_flags;\n\t\t\t\t\t_TIFFfree(data);\n\t\t\t\t\tif (!m)\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_STRIPOFFSETS:\n\t\t\tcase TIFFTAG_TILEOFFSETS:\n _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),\n dp, sizeof(TIFFDirEntry) );\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_STRIPBYTECOUNTS:\n\t\t\tcase TIFFTAG_TILEBYTECOUNTS:\n _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),\n dp, sizeof(TIFFDirEntry) );\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_COLORMAP:\n\t\t\tcase TIFFTAG_TRANSFERFUNCTION:\n\t\t\t\t{\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\tuint32 countpersample;\n\t\t\t\t\tuint32 countrequired;\n\t\t\t\t\tuint32 incrementpersample;\n\t\t\t\t\tuint16* value=NULL;\n if( !bitspersample_read )\n {\n fip = TIFFFieldWithTag(tif,dp->tdir_tag);\n TIFFWarningExt(tif->tif_clientdata,module,\n "Ignoring %s since BitsPerSample tag not found",\n fip ? fip->field_name : "unknown tagname");\n continue;\n }\n\t\t\t\t\tif (tif->tif_dir.td_bitspersample > 24)\n\t\t\t\t\t{\n\t\t\t\t\t fip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t TIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t\t\t"Ignoring %s because BitsPerSample=%d>24",\n\t\t\t\t\t\tfip ? fip->field_name : "unknown tagname",\n\t\t\t\t\t\ttif->tif_dir.td_bitspersample);\n\t\t\t\t\t continue;\n\t\t\t\t\t}\n\t\t\t\t\tcountpersample=(1U<<tif->tif_dir.td_bitspersample);\n\t\t\t\t\tif ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))\n\t\t\t\t\t{\n\t\t\t\t\t\tcountrequired=countpersample;\n\t\t\t\t\t\tincrementpersample=0;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tcountrequired=3*countpersample;\n\t\t\t\t\t\tincrementpersample=countpersample;\n\t\t\t\t\t}\n\t\t\t\t\tif (dp->tdir_count!=(uint64)countrequired)\n\t\t\t\t\t\terr=TIFFReadDirEntryErrCount;\n\t\t\t\t\telse\n\t\t\t\t\t\terr=TIFFReadDirEntryShortArray(tif,dp,&value);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n {\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);\n }\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tTIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);\n\t\t\t\t\t\t_TIFFfree(value);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_OSUBFILETYPE:\n\t\t\t\t{\n\t\t\t\t\tuint16 valueo;\n\t\t\t\t\tuint32 value;\n\t\t\t\t\tif (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tswitch (valueo)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcase OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;\n\t\t\t\t\t\t\tcase OFILETYPE_PAGE: value=FILETYPE_PAGE; break;\n\t\t\t\t\t\t\tdefault: value=0; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (value!=0)\n\t\t\t\t\t\t\tTIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\t(void) TIFFFetchNormalTag(tif, dp, TRUE);\n\t\t\t\tbreak;\n\t\t}\n\t}\n if( tif->tif_mode == O_RDWR &&\n tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0 )\n {\n TIFFSetupStrips(tif);\n }\n else if( !(tif->tif_flags&TIFF_DEFERSTRILELOAD) )\n {\n if( tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 )\n {\n if (!TIFFFetchStripThing(tif,&(tif->tif_dir.td_stripoffset_entry),\n tif->tif_dir.td_nstrips,\n &tif->tif_dir.td_stripoffset_p))\n {\n goto bad;\n }\n }\n if( tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 )\n {\n if (!TIFFFetchStripThing(tif,&(tif->tif_dir.td_stripbytecount_entry),\n tif->tif_dir.td_nstrips,\n &tif->tif_dir.td_stripbytecount_p))\n {\n goto bad;\n }\n }\n }\n\tif (tif->tif_dir.td_compression==COMPRESSION_OJPEG)\n\t{\n\t\tif (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))\n\t\t{\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Photometric tag is missing, assuming data is YCbCr");\n\t\t\tif (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))\n\t\t\t\tgoto bad;\n\t\t}\n\t\telse if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)\n\t\t{\n\t\t\ttif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Photometric tag value assumed incorrect, "\n\t\t\t "assuming data is YCbCr instead of RGB");\n\t\t}\n\t\tif (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))\n\t\t{\n\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t "BitsPerSample tag is missing, assuming 8 bits per sample");\n\t\t\tif (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))\n\t\t\t\tgoto bad;\n\t\t}\n\t\tif (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))\n\t\t{\n\t\t\tif (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "SamplesPerPixel tag is missing, "\n\t\t\t\t "assuming correct SamplesPerPixel value is 3");\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "SamplesPerPixel tag is missing, "\n\t\t\t\t "applying correct SamplesPerPixel value of 3");\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\telse if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)\n\t\t\t\t || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))\n\t\t\t{\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t}\n\t}\n color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);\n if (color_channels && tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples > color_channels) {\n uint16 old_extrasamples;\n uint16 *new_sampleinfo;\n TIFFWarningExt(tif->tif_clientdata,module, "Sum of Photometric type-related "\n "color channels and ExtraSamples doesn\'t match SamplesPerPixel. "\n "Defining non-color channels as ExtraSamples.");\n old_extrasamples = tif->tif_dir.td_extrasamples;\n tif->tif_dir.td_extrasamples = (uint16) (tif->tif_dir.td_samplesperpixel - color_channels);\n new_sampleinfo = (uint16*) _TIFFcalloc(tif->tif_dir.td_extrasamples, sizeof(uint16));\n if (!new_sampleinfo) {\n TIFFErrorExt(tif->tif_clientdata, module, "Failed to allocate memory for "\n "temporary new sampleinfo array (%d 16 bit elements)",\n tif->tif_dir.td_extrasamples);\n goto bad;\n }\n memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo, old_extrasamples * sizeof(uint16));\n _TIFFsetShortArray(&tif->tif_dir.td_sampleinfo, new_sampleinfo, tif->tif_dir.td_extrasamples);\n _TIFFfree(new_sampleinfo);\n }\n\tif (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&\n\t !TIFFFieldSet(tif, FIELD_COLORMAP)) {\n\t\tif ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)\n\t\t\ttif->tif_dir.td_photometric = PHOTOMETRIC_RGB;\n\t\telse if (tif->tif_dir.td_bitspersample>=8)\n\t\t\ttif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;\n\t\telse {\n\t\t\tMissingRequired(tif, "Colormap");\n\t\t\tgoto bad;\n\t\t}\n\t}\n\tif (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)\n\t{\n\t\tif (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {\n\t\t\tif ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&\n\t\t\t tif->tif_dir.td_nstrips > 1) ||\n\t\t\t (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&\n\t\t\t tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {\n\t\t\t MissingRequired(tif, "StripByteCounts");\n\t\t\t goto bad;\n\t\t\t}\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t\t"TIFF directory is missing required "\n\t\t\t\t"\\"StripByteCounts\\" field, calculating from imagelength");\n\t\t\tif (EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n\t\t} else if (tif->tif_dir.td_nstrips == 1\n && !(tif->tif_flags&TIFF_ISTILED)\n\t\t\t && ByteCountLooksBad(tif)) {\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Bogus \\"StripByteCounts\\" field, ignoring and calculating from imagelength");\n\t\t\tif(EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n\t\t} else if (!(tif->tif_flags&TIFF_DEFERSTRILELOAD)\n\t\t\t && tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG\n\t\t\t && tif->tif_dir.td_nstrips > 2\n\t\t\t && tif->tif_dir.td_compression == COMPRESSION_NONE\n\t\t\t && TIFFGetStrileByteCount(tif, 0) != TIFFGetStrileByteCount(tif, 1)\n\t\t\t && TIFFGetStrileByteCount(tif, 0) != 0\n\t\t\t && TIFFGetStrileByteCount(tif, 1) != 0 ) {\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Wrong \\"StripByteCounts\\" field, ignoring and calculating from imagelength");\n\t\t\tif (EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n\t\t}\n\t}\n\tif (dir)\n\t{\n\t\t_TIFFfree(dir);\n\t\tdir=NULL;\n\t}\n\tif (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))\n\t{\n\t\tif (tif->tif_dir.td_bitspersample>=16)\n\t\t\ttif->tif_dir.td_maxsamplevalue=0xFFFF;\n\t\telse\n\t\t\ttif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);\n\t}\n#ifdef STRIPBYTECOUNTSORTED_UNUSED\n\tif (!(tif->tif_flags&TIFF_DEFERSTRILELOAD) && tif->tif_dir.td_nstrips > 1) {\n\t\tuint32 strip;\n\t\ttif->tif_dir.td_stripbytecountsorted = 1;\n\t\tfor (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {\n\t\t\tif (TIFFGetStrileOffset(tif, strip - 1) >\n\t\t\t TIFFGetStrileOffset(tif, strip)) {\n\t\t\t\ttif->tif_dir.td_stripbytecountsorted = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n#endif\n\t(*tif->tif_fixuptags)(tif);\n\tif ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&\n\t (tif->tif_dir.td_nstrips==1)&&\n\t (tif->tif_dir.td_compression==COMPRESSION_NONE)&&\n\t ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))\n {\n ChopUpSingleUncompressedStrip(tif);\n }\n if( tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&\n tif->tif_dir.td_compression == COMPRESSION_NONE &&\n (tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP &&\n TIFFStripSize64(tif) > 0x7FFFFFFFUL )\n {\n TryChopUpUncompressedBigTiff(tif);\n }\n\ttif->tif_flags &= ~TIFF_DIRTYDIRECT;\n\ttif->tif_flags &= ~TIFF_DIRTYSTRIP;\n\ttif->tif_row = (uint32) -1;\n\ttif->tif_curstrip = (uint32) -1;\n\ttif->tif_col = (uint32) -1;\n\ttif->tif_curtile = (uint32) -1;\n\ttif->tif_tilesize = (tmsize_t) -1;\n\ttif->tif_scanlinesize = TIFFScanlineSize(tif);\n\tif (!tif->tif_scanlinesize) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "Cannot handle zero scanline size");\n\t\treturn (0);\n\t}\n\tif (isTiled(tif)) {\n\t\ttif->tif_tilesize = TIFFTileSize(tif);\n\t\tif (!tif->tif_tilesize) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Cannot handle zero tile size");\n\t\t\treturn (0);\n\t\t}\n\t} else {\n\t\tif (!TIFFStripSize(tif)) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Cannot handle zero strip size");\n\t\t\treturn (0);\n\t\t}\n\t}\n\treturn (1);\nbad:\n\tif (dir)\n\t\t_TIFFfree(dir);\n\treturn (0);\n}', 'void\nTIFFFreeDirectory(TIFF* tif)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tint i;\n\t_TIFFmemset(td->td_fieldsset, 0, FIELD_SETLONGS);\n\tCleanupField(td_sminsamplevalue);\n\tCleanupField(td_smaxsamplevalue);\n\tCleanupField(td_colormap[0]);\n\tCleanupField(td_colormap[1]);\n\tCleanupField(td_colormap[2]);\n\tCleanupField(td_sampleinfo);\n\tCleanupField(td_subifd);\n\tCleanupField(td_inknames);\n\tCleanupField(td_refblackwhite);\n\tCleanupField(td_transferfunction[0]);\n\tCleanupField(td_transferfunction[1]);\n\tCleanupField(td_transferfunction[2]);\n\tCleanupField(td_stripoffset_p);\n\tCleanupField(td_stripbytecount_p);\n td->td_stripoffsetbyteallocsize = 0;\n\tTIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);\n\tTIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);\n\tfor( i = 0; i < td->td_customValueCount; i++ ) {\n\t\tif (td->td_customValues[i].value)\n\t\t\t_TIFFfree(td->td_customValues[i].value);\n\t}\n\ttd->td_customValueCount = 0;\n\tCleanupField(td_customValues);\n _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));\n _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));\n}', 'static void TryChopUpUncompressedBigTiff( TIFF* tif )\n{\n TIFFDirectory *td = &tif->tif_dir;\n uint32 rowblock;\n uint64 rowblockbytes;\n uint32 i;\n uint64 stripsize;\n uint32 rowblocksperstrip;\n uint32 rowsperstrip;\n uint64 stripbytes;\n uint32 nstrips;\n stripsize = TIFFStripSize64(tif);\n assert( tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG );\n assert( tif->tif_dir.td_compression == COMPRESSION_NONE );\n assert( (tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP );\n assert( stripsize > 0x7FFFFFFFUL );\n if( TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY )\n return;\n if ((td->td_photometric == PHOTOMETRIC_YCBCR)&&\n (!isUpSampled(tif)))\n rowblock = td->td_ycbcrsubsampling[1];\n else\n rowblock = 1;\n rowblockbytes = TIFFVStripSize64(tif, rowblock);\n if( rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL )\n {\n return;\n }\n for( i = 0; i < td->td_nstrips; i++ )\n {\n if( i == td->td_nstrips - 1 )\n {\n if( TIFFGetStrileByteCount(tif, i) < TIFFVStripSize64(\n tif, td->td_imagelength - i * td->td_rowsperstrip ) )\n {\n return;\n }\n }\n else\n {\n if( TIFFGetStrileByteCount(tif, i) != stripsize )\n {\n return;\n }\n if( i > 0 && TIFFGetStrileOffset(tif, i) !=\n TIFFGetStrileOffset(tif, i-1) + TIFFGetStrileByteCount(tif, i-1) )\n {\n return;\n }\n }\n }\n rowblocksperstrip = (uint32) (512 * 1024 * 1024 / rowblockbytes);\n if( rowblocksperstrip == 0 )\n rowblocksperstrip = 1;\n rowsperstrip = rowblocksperstrip * rowblock;\n stripbytes = rowblocksperstrip * rowblockbytes;\n assert( stripbytes <= 0x7FFFFFFFUL );\n nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);\n if( nstrips == 0 )\n return;\n if( tif->tif_mode == O_RDONLY &&\n nstrips > 1000000 &&\n (TIFFGetStrileOffset(tif, td->td_nstrips-1) > TIFFGetFileSize(tif) ||\n TIFFGetStrileOffset(tif, td->td_nstrips-1) +\n TIFFGetStrileByteCount(tif, td->td_nstrips-1) > TIFFGetFileSize(tif)) )\n {\n return;\n }\n allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);\n}', 'uint64 TIFFGetStrileOffset(TIFF *tif, uint32 strile)\n{\n return TIFFGetStrileOffsetWithErr(tif, strile, NULL);\n}', 'uint64 TIFFGetStrileOffsetWithErr(TIFF *tif, uint32 strile, int *pbErr)\n{\n TIFFDirectory *td = &tif->tif_dir;\n return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,\n &(td->td_stripoffset_entry),\n &(td->td_stripoffset_p), pbErr);\n}', 'static uint64 _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32 strile,\n TIFFDirEntry* dirent,\n uint64** parray,\n int *pbErr)\n{\n TIFFDirectory *td = &tif->tif_dir;\n if( pbErr )\n *pbErr = 0;\n if( (tif->tif_flags&TIFF_DEFERSTRILELOAD) && !(tif->tif_flags&TIFF_CHOPPEDUPARRAYS) )\n {\n if( !(tif->tif_flags&TIFF_LAZYSTRILELOAD) ||\n dirent->tdir_count <= 4 )\n {\n if( !_TIFFFillStriles(tif) )\n {\n if( pbErr )\n *pbErr = 1;\n }\n }\n else\n {\n if( !_TIFFFetchStrileValue(tif, strile, dirent, parray) )\n {\n if( pbErr )\n *pbErr = 1;\n return 0;\n }\n }\n }\n if( *parray == NULL || strile >= td->td_nstrips )\n {\n if( pbErr )\n *pbErr = 1;\n return 0;\n }\n return (*parray)[strile];\n}', 'static int _TIFFFetchStrileValue(TIFF* tif,\n uint32 strile,\n TIFFDirEntry* dirent,\n uint64** parray)\n{\n static const char module[] = "_TIFFFetchStrileValue";\n TIFFDirectory *td = &tif->tif_dir;\n if( strile >= dirent->tdir_count )\n {\n return 0;\n }\n if( strile >= td->td_stripoffsetbyteallocsize )\n {\n uint32 nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;\n uint32 nStripArrayAllocNew;\n uint64 nArraySize64;\n size_t nArraySize;\n uint64* offsetArray;\n uint64* bytecountArray;\n if( strile > 1000000 )\n {\n uint64 filesize = TIFFGetFileSize(tif);\n if( strile > filesize / sizeof(uint32) )\n {\n TIFFErrorExt(tif->tif_clientdata, module, "File too short");\n return 0;\n }\n }\n if( td->td_stripoffsetbyteallocsize == 0 &&\n td->td_nstrips < 1024 * 1024 )\n {\n nStripArrayAllocNew = td->td_nstrips;\n }\n else\n {\n#define TIFF_MAX(a,b) (((a)>(b)) ? (a) : (b))\n#define TIFF_MIN(a,b) (((a)<(b)) ? (a) : (b))\n nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U );\n if( nStripArrayAllocNew < 0xFFFFFFFFU / 2 )\n nStripArrayAllocNew *= 2;\n nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);\n }\n assert( strile < nStripArrayAllocNew );\n nArraySize64 = (uint64)sizeof(uint64) * nStripArrayAllocNew;\n nArraySize = (size_t)(nArraySize64);\n#if SIZEOF_SIZE_T == 4\n if( nArraySize != nArraySize64 )\n {\n TIFFErrorExt(tif->tif_clientdata, module,\n "Cannot allocate strip offset and bytecount arrays");\n return 0;\n }\n#endif\n offsetArray = (uint64*)(\n _TIFFrealloc( td->td_stripoffset_p, nArraySize ) );\n bytecountArray = (uint64*)(\n _TIFFrealloc( td->td_stripbytecount_p, nArraySize ) );\n if( offsetArray )\n td->td_stripoffset_p = offsetArray;\n if( bytecountArray )\n td->td_stripbytecount_p = bytecountArray;\n if( offsetArray && bytecountArray )\n {\n td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;\n memset(td->td_stripoffset_p + nStripArrayAllocBefore,\n 0xFF,\n (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64) );\n memset(td->td_stripbytecount_p + nStripArrayAllocBefore,\n 0xFF,\n (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64) );\n }\n else\n {\n TIFFErrorExt(tif->tif_clientdata, module,\n "Cannot allocate strip offset and bytecount arrays");\n _TIFFfree(td->td_stripoffset_p);\n td->td_stripoffset_p = NULL;\n _TIFFfree(td->td_stripbytecount_p);\n td->td_stripbytecount_p = NULL;\n td->td_stripoffsetbyteallocsize = 0;\n }\n }\n if( *parray == NULL || strile >= td->td_stripoffsetbyteallocsize )\n return 0;\n if( ~((*parray)[strile]) == 0 )\n {\n if( !_TIFFPartialReadStripArray( tif, dirent, strile, *parray ) )\n {\n (*parray)[strile] = 0;\n return 0;\n }\n }\n return 1;\n}'] |
2,299 | 0 | https://github.com/openssl/openssl/blob/40a706286febe0279336c96374c607daaa1b1d49/crypto/objects/o_names.c/#L108 | int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *),
int (*cmp_func)(const char *, const char *),
void (*free_func)(const char *, int, const char *))
{
int ret;
int i;
NAME_FUNCS *name_funcs;
if (name_funcs_stack == NULL)
{
MemCheck_off();
name_funcs_stack=sk_NAME_FUNCS_new_null();
MemCheck_on();
}
if ((name_funcs_stack == NULL))
{
return(0);
}
ret=names_type_num;
names_type_num++;
for (i=sk_NAME_FUNCS_num(name_funcs_stack); i<names_type_num; i++)
{
MemCheck_off();
name_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS));
MemCheck_on();
if (!name_funcs)
{
OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX,ERR_R_MALLOC_FAILURE);
return(0);
}
name_funcs->hash_func = lh_strhash;
name_funcs->cmp_func = OPENSSL_strcmp;
name_funcs->free_func = 0;
MemCheck_off();
sk_NAME_FUNCS_push(name_funcs_stack,name_funcs);
MemCheck_on();
}
name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
if (hash_func != NULL)
name_funcs->hash_func = hash_func;
if (cmp_func != NULL)
name_funcs->cmp_func = cmp_func;
if (free_func != NULL)
name_funcs->free_func = free_func;
return(ret);
} | ['int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *),\n\tint (*cmp_func)(const char *, const char *),\n\tvoid (*free_func)(const char *, int, const char *))\n\t{\n\tint ret;\n\tint i;\n\tNAME_FUNCS *name_funcs;\n\tif (name_funcs_stack == NULL)\n\t\t{\n\t\tMemCheck_off();\n\t\tname_funcs_stack=sk_NAME_FUNCS_new_null();\n\t\tMemCheck_on();\n\t\t}\n\tif ((name_funcs_stack == NULL))\n\t\t{\n\t\treturn(0);\n\t\t}\n\tret=names_type_num;\n\tnames_type_num++;\n\tfor (i=sk_NAME_FUNCS_num(name_funcs_stack); i<names_type_num; i++)\n\t\t{\n\t\tMemCheck_off();\n\t\tname_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS));\n\t\tMemCheck_on();\n\t\tif (!name_funcs)\n\t\t\t{\n\t\t\tOBJerr(OBJ_F_OBJ_NAME_NEW_INDEX,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(0);\n\t\t\t}\n\t\tname_funcs->hash_func = lh_strhash;\n\t\tname_funcs->cmp_func = OPENSSL_strcmp;\n\t\tname_funcs->free_func = 0;\n\t\tMemCheck_off();\n\t\tsk_NAME_FUNCS_push(name_funcs_stack,name_funcs);\n\t\tMemCheck_on();\n\t\t}\n\tname_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);\n\tif (hash_func != NULL)\n\t\tname_funcs->hash_func = hash_func;\n\tif (cmp_func != NULL)\n\t\tname_funcs->cmp_func = cmp_func;\n\tif (free_func != NULL)\n\t\tname_funcs->free_func = free_func;\n\treturn(ret);\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}'] |
2,300 | 0 | https://github.com/openssl/openssl/blob/507db4c5313288d55eeb8434b0111201ba363b28/crypto/evp/evp_key.c/#L194 | int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
const unsigned char *salt, const unsigned char *data,
int datal, int count, unsigned char *key,
unsigned char *iv)
{
EVP_MD_CTX *c;
unsigned char md_buf[EVP_MAX_MD_SIZE];
int niv, nkey, addmd = 0;
unsigned int mds = 0, i;
int rv = 0;
nkey = type->key_len;
niv = type->iv_len;
OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH);
OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH);
if (data == NULL)
return (nkey);
c = EVP_MD_CTX_new();
if (c == NULL)
goto err;
for (;;) {
if (!EVP_DigestInit_ex(c, md, NULL))
goto err;
if (addmd++)
if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
goto err;
if (!EVP_DigestUpdate(c, data, datal))
goto err;
if (salt != NULL)
if (!EVP_DigestUpdate(c, salt, PKCS5_SALT_LEN))
goto err;
if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
goto err;
for (i = 1; i < (unsigned int)count; i++) {
if (!EVP_DigestInit_ex(c, md, NULL))
goto err;
if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
goto err;
if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
goto err;
}
i = 0;
if (nkey) {
for (;;) {
if (nkey == 0)
break;
if (i == mds)
break;
if (key != NULL)
*(key++) = md_buf[i];
nkey--;
i++;
}
}
if (niv && (i != mds)) {
for (;;) {
if (niv == 0)
break;
if (i == mds)
break;
if (iv != NULL)
*(iv++) = md_buf[i];
niv--;
i++;
}
}
if ((nkey == 0) && (niv == 0))
break;
}
rv = type->key_len;
err:
EVP_MD_CTX_free(c);
OPENSSL_cleanse(md_buf, sizeof(md_buf));
return rv;
} | ['int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,\n const unsigned char *salt, const unsigned char *data,\n int datal, int count, unsigned char *key,\n unsigned char *iv)\n{\n EVP_MD_CTX *c;\n unsigned char md_buf[EVP_MAX_MD_SIZE];\n int niv, nkey, addmd = 0;\n unsigned int mds = 0, i;\n int rv = 0;\n nkey = type->key_len;\n niv = type->iv_len;\n OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH);\n OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH);\n if (data == NULL)\n return (nkey);\n c = EVP_MD_CTX_new();\n if (c == NULL)\n goto err;\n for (;;) {\n if (!EVP_DigestInit_ex(c, md, NULL))\n goto err;\n if (addmd++)\n if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))\n goto err;\n if (!EVP_DigestUpdate(c, data, datal))\n goto err;\n if (salt != NULL)\n if (!EVP_DigestUpdate(c, salt, PKCS5_SALT_LEN))\n goto err;\n if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))\n goto err;\n for (i = 1; i < (unsigned int)count; i++) {\n if (!EVP_DigestInit_ex(c, md, NULL))\n goto err;\n if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))\n goto err;\n if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))\n goto err;\n }\n i = 0;\n if (nkey) {\n for (;;) {\n if (nkey == 0)\n break;\n if (i == mds)\n break;\n if (key != NULL)\n *(key++) = md_buf[i];\n nkey--;\n i++;\n }\n }\n if (niv && (i != mds)) {\n for (;;) {\n if (niv == 0)\n break;\n if (i == mds)\n break;\n if (iv != NULL)\n *(iv++) = md_buf[i];\n niv--;\n i++;\n }\n }\n if ((nkey == 0) && (niv == 0))\n break;\n }\n rv = type->key_len;\n err:\n EVP_MD_CTX_free(c);\n OPENSSL_cleanse(md_buf, sizeof(md_buf));\n return rv;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(int 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(int num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n if (allow_customize)\n allow_customize = 0;\n if (malloc_debug_func != NULL) {\n if (allow_customize_debug)\n allow_customize_debug = 0;\n malloc_debug_func(NULL, num, file, line, 0);\n }\n ret = malloc_ex_func(num, file, line);\n#ifdef LEVITTE_DEBUG_MEM\n fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n if (malloc_debug_func != NULL)\n malloc_debug_func(ret, num, file, line, 1);\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}'] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.