id
int64
1
36.7k
label
int64
0
1
bug_url
stringlengths
91
134
bug_function
stringlengths
13
72.7k
functions
stringlengths
17
79.2k
35,501
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bn/bn_mul.c/#L641
int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx) { int top,al,bl; BIGNUM *rr; #ifdef BN_RECURSION BIGNUM *t; int i,j,k; #endif #ifdef BN_COUNT printf("BN_mul %d * %d\n",a->top,b->top); #endif bn_check_top(a); bn_check_top(b); bn_check_top(r); al=a->top; bl=b->top; r->neg=a->neg^b->neg; if ((al == 0) || (bl == 0)) { BN_zero(r); return(1); } top=al+bl; if ((r == a) || (r == b)) rr= &(ctx->bn[ctx->tos+1]); else rr=r; #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) if (al == bl) { # ifdef BN_MUL_COMBA if (al == 8) { if (bn_wexpand(rr,16) == NULL) return(0); r->top=16; bn_mul_comba8(rr->d,a->d,b->d); goto end; } else # endif #ifdef BN_RECURSION if (al < BN_MULL_SIZE_NORMAL) #endif { if (bn_wexpand(rr,top) == NULL) return(0); rr->top=top; bn_mul_normal(rr->d,a->d,al,b->d,bl); goto end; } # ifdef BN_RECURSION goto symetric; # endif } #endif #ifdef BN_RECURSION else if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL)) { if (bn_wexpand(rr,top) == NULL) return(0); rr->top=top; bn_mul_normal(rr->d,a->d,al,b->d,bl); goto end; } else { i=(al-bl); if ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA)) { bn_wexpand(b,al); b->d[bl]=0; bl++; goto symetric; } else if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA)) { bn_wexpand(a,bl); a->d[al]=0; al++; goto symetric; } } #endif if (bn_wexpand(rr,top) == NULL) return(0); rr->top=top; bn_mul_normal(rr->d,a->d,al,b->d,bl); #ifdef BN_RECURSION if (0) { symetric: j=BN_num_bits_word((BN_ULONG)al); j=1<<(j-1); k=j+j; t= &(ctx->bn[ctx->tos]); if (al == j) { bn_wexpand(t,k*2); bn_wexpand(rr,k*2); bn_mul_recursive(rr->d,a->d,b->d,al,t->d); } else { bn_wexpand(a,k); bn_wexpand(b,k); bn_wexpand(t,k*4); bn_wexpand(rr,k*4); for (i=a->top; i<k; i++) a->d[i]=0; for (i=b->top; i<k; i++) b->d[i]=0; bn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d); } rr->top=top; } #endif #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) end: #endif bn_fix_top(rr); if (r != rr) BN_copy(r,rr); return(1); }
['int test_mul(BIO *bp)\n\t{\n\tBIGNUM a,b,c;\n\tint i;\n\tint j;\n\tBN_CTX ctx;\n\tBN_CTX_init(&ctx);\n\tBN_init(&a);\n\tBN_init(&b);\n\tBN_init(&c);\n\tBN_rand(&a,200,0,0);\n\tfor (i=0; i<100; i++)\n\t\t{\n\t\tBN_rand(&b,250+i,0,0);\n\t\tBN_rand(&b,200,0,0);\n\t\ta.neg=rand_neg();\n\t\tb.neg=rand_neg();\n\t\tif (bp == NULL)\n\t\t\tfor (j=0; j<100; j++)\n\t\t\t\tBN_mul(&c,&a,&b,&ctx);\n\t\tBN_mul(&c,&a,&b,&ctx);\n\t\tif (bp != NULL)\n\t\t\t{\n\t\t\tif (!results)\n\t\t\t\t{\n\t\t\t\tBN_print(bp,&a);\n\t\t\t\tBIO_puts(bp," * ");\n\t\t\t\tBN_print(bp,&b);\n\t\t\t\tBIO_puts(bp," - ");\n\t\t\t\t}\n\t\t\tBN_print(bp,&c);\n\t\t\tBIO_puts(bp,"\\n");\n\t\t\t}\n\t\t}\n\tBN_free(&a);\n\tBN_free(&b);\n\tBN_free(&c);\n\tBN_CTX_free(&ctx);\n\treturn(1);\n\t}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n\t{\n\tunsigned char *buf=NULL;\n\tint ret=0,bit,bytes,mask;\n\ttime_t tim;\n\tbytes=(bits+7)/8;\n\tbit=(bits-1)%8;\n\tmask=0xff<<bit;\n\tbuf=(unsigned char *)Malloc(bytes);\n\tif (buf == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_RAND,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\ttime(&tim);\n\tRAND_seed(&tim,sizeof(tim));\n\tRAND_bytes(buf,(int)bytes);\n\tif (top)\n\t\t{\n\t\tif (bit == 0)\n\t\t\t{\n\t\t\tbuf[0]=1;\n\t\t\tbuf[1]|=0x80;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbuf[0]|=(3<<(bit-1));\n\t\t\tbuf[0]&= ~(mask<<1);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tbuf[0]|=(1<<bit);\n\t\tbuf[0]&= ~(mask<<1);\n\t\t}\n\tif (bottom)\n\t\tbuf[bytes-1]|=1;\n\tif (!BN_bin2bn(buf,bytes,rnd)) goto err;\n\tret=1;\nerr:\n\tif (buf != NULL)\n\t\t{\n\t\tmemset(buf,0,bytes);\n\t\tFree(buf);\n\t\t}\n\treturn(ret);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}']
35,502
0
https://github.com/openssl/openssl/blob/6bc62a620e715f7580651ca932eab052aa527886/crypto/bn/bn_ctx.c/#L268
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int dsa_do_verify(const unsigned char *dgst, int dgst_len,\n DSA_SIG *sig, DSA *dsa)\n{\n BN_CTX *ctx;\n BIGNUM *u1, *u2, *t1;\n BN_MONT_CTX *mont = NULL;\n const BIGNUM *r, *s;\n int ret = -1, i;\n if (!dsa->p || !dsa->q || !dsa->g) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);\n return -1;\n }\n i = BN_num_bits(dsa->q);\n if (i != 160 && i != 224 && i != 256) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);\n return -1;\n }\n if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);\n return -1;\n }\n u1 = BN_new();\n u2 = BN_new();\n t1 = BN_new();\n ctx = BN_CTX_new();\n if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)\n goto err;\n DSA_SIG_get0(sig, &r, &s);\n if (BN_is_zero(r) || BN_is_negative(r) ||\n BN_ucmp(r, dsa->q) >= 0) {\n ret = 0;\n goto err;\n }\n if (BN_is_zero(s) || BN_is_negative(s) ||\n BN_ucmp(s, dsa->q) >= 0) {\n ret = 0;\n goto err;\n }\n if ((BN_mod_inverse(u2, s, dsa->q, ctx)) == NULL)\n goto err;\n if (dgst_len > (i >> 3))\n dgst_len = (i >> 3);\n if (BN_bin2bn(dgst, dgst_len, u1) == NULL)\n goto err;\n if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))\n goto err;\n if (!BN_mod_mul(u2, r, u2, dsa->q, ctx))\n goto err;\n if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {\n mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,\n dsa->lock, dsa->p, ctx);\n if (!mont)\n goto err;\n }\n if (dsa->meth->dsa_mod_exp != NULL) {\n if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, u2,\n dsa->p, ctx, mont))\n goto err;\n } else {\n if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,\n mont))\n goto err;\n }\n if (!BN_mod(u1, t1, dsa->q, ctx))\n goto err;\n ret = (BN_ucmp(u1, r) == 0);\n err:\n if (ret < 0)\n DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);\n BN_CTX_free(ctx);\n BN_free(u1);\n BN_free(u2);\n BN_free(t1);\n return ret;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (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}', '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_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}']
35,503
0
https://github.com/openssl/openssl/blob/61f5b6f33807306d09bccbc2dcad474d1d04ca40/crypto/bn/bn_lib.c/#L377
BIGNUM *bn_expand2(BIGNUM *b, int words) { BN_ULONG *A,*B,*a; int i,j; bn_check_top(b); if (words > b->max) { bn_check_top(b); if (BN_get_flags(b,BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return(NULL); } a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1)); if (A == NULL) { BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE); return(NULL); } memset(A,0x5c,sizeof(BN_ULONG)*(words+1)); #if 1 B=b->d; if (B != NULL) { for (i=b->top&(~7); i>0; i-=8) { A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3]; A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7]; A+=8; B+=8; } switch (b->top&7) { case 7: A[6]=B[6]; case 6: A[5]=B[5]; case 5: A[4]=B[4]; case 4: A[3]=B[3]; case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } Free(b->d); } b->d=a; b->max=words; B= &(b->d[b->top]); j=(b->max - b->top) & ~7; for (i=0; i<j; i+=8) { B[0]=0; B[1]=0; B[2]=0; B[3]=0; B[4]=0; B[5]=0; B[6]=0; B[7]=0; B+=8; } j=(b->max - b->top) & 7; for (i=0; i<j; i++) { B[0]=0; B++; } #else memcpy(a->d,b->d,sizeof(b->d[0])*b->top); #endif } return(b); }
['DSA *get_dsa1024()\n\t{\n\tDSA *dsa;\n\tif ((dsa=DSA_new()) == NULL) return(NULL);\n\tdsa->p=BN_bin2bn(dsa1024_p,sizeof(dsa1024_p),NULL);\n\tdsa->q=BN_bin2bn(dsa1024_q,sizeof(dsa1024_q),NULL);\n\tdsa->g=BN_bin2bn(dsa1024_g,sizeof(dsa1024_g),NULL);\n\tif ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))\n\t\treturn(NULL);\n\treturn(dsa);\n\t}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n\t{\n\tunsigned int i,m;\n\tunsigned int n;\n\tBN_ULONG l;\n\tif (ret == NULL) ret=BN_new();\n\tif (ret == NULL) return(NULL);\n\tl=0;\n\tn=len;\n\tif (n == 0)\n\t\t{\n\t\tret->top=0;\n\t\treturn(ret);\n\t\t}\n\tif (bn_expand(ret,(int)(n+2)*8) == NULL)\n\t\treturn(NULL);\n\ti=((n-1)/BN_BYTES)+1;\n\tm=((n-1)%(BN_BYTES));\n\tret->top=i;\n\twhile (n-- > 0)\n\t\t{\n\t\tl=(l<<8L)| *(s++);\n\t\tif (m-- == 0)\n\t\t\t{\n\t\t\tret->d[--i]=l;\n\t\t\tl=0;\n\t\t\tm=BN_BYTES-1;\n\t\t\t}\n\t\t}\n\tbn_fix_top(ret);\n\treturn(ret);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*B,*a;\n\tint i,j;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\nmemset(A,0x5c,sizeof(BN_ULONG)*(words+1));\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tB= &(b->d[b->top]);\n\t\tj=(b->max - b->top) & ~7;\n\t\tfor (i=0; i<j; i+=8)\n\t\t\t{\n\t\t\tB[0]=0; B[1]=0; B[2]=0; B[3]=0;\n\t\t\tB[4]=0; B[5]=0; B[6]=0; B[7]=0;\n\t\t\tB+=8;\n\t\t\t}\n\t\tj=(b->max - b->top) & 7;\n\t\tfor (i=0; i<j; i++)\n\t\t\t{\n\t\t\tB[0]=0;\n\t\t\tB++;\n\t\t\t}\n#else\n\t\t\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\t\t}\n\treturn(b);\n\t}']
35,504
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L352
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *A, *a = NULL; const BN_ULONG *B; int i; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b,BN_FLG_SECURE)) a = A = OPENSSL_secure_malloc(words * sizeof(*a)); else a = A = OPENSSL_malloc(words * sizeof(*a)); if (A == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } #ifdef PURIFY memset(a, 0, sizeof(*a) * words); #endif #if 1 B = b->d; if (B != NULL) { for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { BN_ULONG a0, a1, a2, a3; a0 = B[0]; a1 = B[1]; a2 = B[2]; a3 = B[3]; A[0] = a0; A[1] = a1; A[2] = a2; A[3] = a3; } switch (b->top & 3) { case 3: A[2] = B[2]; case 2: A[1] = B[1]; case 1: A[0] = B[0]; case 0: ; } } #else memset(A, 0, sizeof(*A) * words); memcpy(A, b->d, sizeof(b->d[0]) * b->top); #endif return (a); }
['int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,\n const EVP_MD *evpmd, const unsigned char *seed_in,\n size_t seed_len, unsigned char *seed_out,\n int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)\n{\n int ok = 0;\n unsigned char seed[SHA256_DIGEST_LENGTH];\n unsigned char md[SHA256_DIGEST_LENGTH];\n unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];\n BIGNUM *r0, *W, *X, *c, *test;\n BIGNUM *g = NULL, *q = NULL, *p = NULL;\n BN_MONT_CTX *mont = NULL;\n int i, k, n = 0, m = 0, qsize = qbits >> 3;\n int counter = 0;\n int r = 0;\n BN_CTX *ctx = NULL;\n unsigned int h = 2;\n if (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH &&\n qsize != SHA256_DIGEST_LENGTH)\n return 0;\n if (evpmd == NULL)\n evpmd = EVP_sha1();\n if (bits < 512)\n bits = 512;\n bits = (bits + 63) / 64 * 64;\n if (seed_in != NULL) {\n if (seed_len < (size_t)qsize)\n return 0;\n if (seed_len > (size_t)qsize) {\n seed_len = qsize;\n }\n memcpy(seed, seed_in, seed_len);\n }\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n r0 = BN_CTX_get(ctx);\n g = BN_CTX_get(ctx);\n W = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n c = BN_CTX_get(ctx);\n p = BN_CTX_get(ctx);\n test = BN_CTX_get(ctx);\n if (!BN_lshift(test, BN_value_one(), bits - 1))\n goto err;\n for (;;) {\n for (;;) {\n int use_random_seed = (seed_in == NULL);\n if (!BN_GENCB_call(cb, 0, m++))\n goto err;\n if (use_random_seed) {\n if (RAND_bytes(seed, qsize) <= 0)\n goto err;\n } else {\n seed_in = NULL;\n }\n memcpy(buf, seed, qsize);\n memcpy(buf2, seed, qsize);\n for (i = qsize - 1; i >= 0; i--) {\n buf[i]++;\n if (buf[i] != 0)\n break;\n }\n if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL))\n goto err;\n if (!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL))\n goto err;\n for (i = 0; i < qsize; i++)\n md[i] ^= buf2[i];\n md[0] |= 0x80;\n md[qsize - 1] |= 0x01;\n if (!BN_bin2bn(md, qsize, q))\n goto err;\n r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,\n use_random_seed, cb);\n if (r > 0)\n break;\n if (r != 0)\n goto err;\n }\n if (!BN_GENCB_call(cb, 2, 0))\n goto err;\n if (!BN_GENCB_call(cb, 3, 0))\n goto err;\n counter = 0;\n n = (bits - 1) / 160;\n for (;;) {\n if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))\n goto err;\n BN_zero(W);\n for (k = 0; k <= n; k++) {\n for (i = qsize - 1; i >= 0; i--) {\n buf[i]++;\n if (buf[i] != 0)\n break;\n }\n if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL))\n goto err;\n if (!BN_bin2bn(md, qsize, r0))\n goto err;\n if (!BN_lshift(r0, r0, (qsize << 3) * k))\n goto err;\n if (!BN_add(W, W, r0))\n goto err;\n }\n if (!BN_mask_bits(W, bits - 1))\n goto err;\n if (!BN_copy(X, W))\n goto err;\n if (!BN_add(X, X, test))\n goto err;\n if (!BN_lshift1(r0, q))\n goto err;\n if (!BN_mod(c, X, r0, ctx))\n goto err;\n if (!BN_sub(r0, c, BN_value_one()))\n goto err;\n if (!BN_sub(p, X, r0))\n goto err;\n if (BN_cmp(p, test) >= 0) {\n r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);\n if (r > 0)\n goto end;\n if (r != 0)\n goto err;\n }\n counter++;\n if (counter >= 4096)\n break;\n }\n }\n end:\n if (!BN_GENCB_call(cb, 2, 1))\n goto err;\n if (!BN_sub(test, p, BN_value_one()))\n goto err;\n if (!BN_div(r0, NULL, test, q, ctx))\n goto err;\n if (!BN_set_word(test, h))\n goto err;\n if (!BN_MONT_CTX_set(mont, p, ctx))\n goto err;\n for (;;) {\n if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))\n goto err;\n if (!BN_is_one(g))\n break;\n if (!BN_add(test, test, BN_value_one()))\n goto err;\n h++;\n }\n if (!BN_GENCB_call(cb, 3, 1))\n goto err;\n ok = 1;\n err:\n if (ok) {\n BN_free(ret->p);\n BN_free(ret->q);\n BN_free(ret->g);\n ret->p = BN_dup(p);\n ret->q = BN_dup(q);\n ret->g = BN_dup(g);\n if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {\n ok = 0;\n goto err;\n }\n if (counter_ret != NULL)\n *counter_ret = counter;\n if (h_ret != NULL)\n *h_ret = h;\n if (seed_out)\n memcpy(seed_out, seed, qsize);\n }\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n BN_MONT_CTX_free(mont);\n return ok;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_malloc(words * sizeof(*a));\n else\n a = A = OPENSSL_malloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#ifdef PURIFY\n memset(a, 0, sizeof(*a) * words);\n#endif\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
35,505
0
https://github.com/openssl/openssl/blob/8ae173bb57819a23717fd3c8e7c51cb62f4268d0/crypto/x509/x_name.c/#L505
int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase) { char *s, *c, *b; int l, i; l = 80 - 2 - obase; b = X509_NAME_oneline(name, NULL, 0); if (!b) return 0; if (!*b) { OPENSSL_free(b); return 1; } s = b + 1; c = s; for (;;) { if (((*s == '/') && (ossl_isupper(s[1]) && ((s[2] == '=') || (ossl_isupper(s[2]) && (s[3] == '=')) ))) || (*s == '\0')) { i = s - c; if (BIO_write(bp, c, i) != i) goto err; c = s + 1; if (*s != '\0') { if (BIO_write(bp, ", ", 2) != 2) goto err; } l--; } if (*s == '\0') break; s++; l--; } OPENSSL_free(b); return 1; err: X509err(X509_F_X509_NAME_PRINT, ERR_R_BUF_LIB); OPENSSL_free(b); return 0; }
['int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase)\n{\n char *s, *c, *b;\n int l, i;\n l = 80 - 2 - obase;\n b = X509_NAME_oneline(name, NULL, 0);\n if (!b)\n return 0;\n if (!*b) {\n OPENSSL_free(b);\n return 1;\n }\n s = b + 1;\n c = s;\n for (;;) {\n if (((*s == \'/\') &&\n (ossl_isupper(s[1]) && ((s[2] == \'=\') ||\n (ossl_isupper(s[2]) && (s[3] == \'=\'))\n ))) || (*s == \'\\0\'))\n {\n i = s - c;\n if (BIO_write(bp, c, i) != i)\n goto err;\n c = s + 1;\n if (*s != \'\\0\') {\n if (BIO_write(bp, ", ", 2) != 2)\n goto err;\n }\n l--;\n }\n if (*s == \'\\0\')\n break;\n s++;\n l--;\n }\n OPENSSL_free(b);\n return 1;\n err:\n X509err(X509_F_X509_NAME_PRINT, ERR_R_BUF_LIB);\n OPENSSL_free(b);\n return 0;\n}', 'char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)\n{\n const X509_NAME_ENTRY *ne;\n int i;\n int n, lold, l, l1, l2, num, j, type;\n const char *s;\n char *p;\n unsigned char *q;\n BUF_MEM *b = NULL;\n static const char hex[17] = "0123456789ABCDEF";\n int gs_doit[4];\n char tmp_buf[80];\n#ifdef CHARSET_EBCDIC\n unsigned char ebcdic_buf[1024];\n#endif\n if (buf == NULL) {\n if ((b = BUF_MEM_new()) == NULL)\n goto err;\n if (!BUF_MEM_grow(b, 200))\n goto err;\n b->data[0] = \'\\0\';\n len = 200;\n } else if (len == 0) {\n return NULL;\n }\n if (a == NULL) {\n if (b) {\n buf = b->data;\n OPENSSL_free(b);\n }\n strncpy(buf, "NO X509_NAME", len);\n buf[len - 1] = \'\\0\';\n return buf;\n }\n len--;\n l = 0;\n for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {\n ne = sk_X509_NAME_ENTRY_value(a->entries, i);\n n = OBJ_obj2nid(ne->object);\n if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {\n i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);\n s = tmp_buf;\n }\n l1 = strlen(s);\n type = ne->value->type;\n num = ne->value->length;\n if (num > NAME_ONELINE_MAX) {\n X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG);\n goto end;\n }\n q = ne->value->data;\n#ifdef CHARSET_EBCDIC\n if (type == V_ASN1_GENERALSTRING ||\n type == V_ASN1_VISIBLESTRING ||\n type == V_ASN1_PRINTABLESTRING ||\n type == V_ASN1_TELETEXSTRING ||\n type == V_ASN1_IA5STRING) {\n if (num > (int)sizeof(ebcdic_buf))\n num = sizeof(ebcdic_buf);\n ascii2ebcdic(ebcdic_buf, q, num);\n q = ebcdic_buf;\n }\n#endif\n if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;\n for (j = 0; j < num; j++)\n if (q[j] != 0)\n gs_doit[j & 3] = 1;\n if (gs_doit[0] | gs_doit[1] | gs_doit[2])\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;\n else {\n gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;\n gs_doit[3] = 1;\n }\n } else\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;\n for (l2 = j = 0; j < num; j++) {\n if (!gs_doit[j & 3])\n continue;\n l2++;\n#ifndef CHARSET_EBCDIC\n if ((q[j] < \' \') || (q[j] > \'~\'))\n l2 += 3;\n#else\n if ((os_toascii[q[j]] < os_toascii[\' \']) ||\n (os_toascii[q[j]] > os_toascii[\'~\']))\n l2 += 3;\n#endif\n }\n lold = l;\n l += 1 + l1 + 1 + l2;\n if (l > NAME_ONELINE_MAX) {\n X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG);\n goto end;\n }\n if (b != NULL) {\n if (!BUF_MEM_grow(b, l + 1))\n goto err;\n p = &(b->data[lold]);\n } else if (l > len) {\n break;\n } else\n p = &(buf[lold]);\n *(p++) = \'/\';\n memcpy(p, s, (unsigned int)l1);\n p += l1;\n *(p++) = \'=\';\n#ifndef CHARSET_EBCDIC\n q = ne->value->data;\n#endif\n for (j = 0; j < num; j++) {\n if (!gs_doit[j & 3])\n continue;\n#ifndef CHARSET_EBCDIC\n n = q[j];\n if ((n < \' \') || (n > \'~\')) {\n *(p++) = \'\\\\\';\n *(p++) = \'x\';\n *(p++) = hex[(n >> 4) & 0x0f];\n *(p++) = hex[n & 0x0f];\n } else\n *(p++) = n;\n#else\n n = os_toascii[q[j]];\n if ((n < os_toascii[\' \']) || (n > os_toascii[\'~\'])) {\n *(p++) = \'\\\\\';\n *(p++) = \'x\';\n *(p++) = hex[(n >> 4) & 0x0f];\n *(p++) = hex[n & 0x0f];\n } else\n *(p++) = q[j];\n#endif\n }\n *p = \'\\0\';\n }\n if (b != NULL) {\n p = b->data;\n OPENSSL_free(b);\n } else\n p = buf;\n if (i == 0)\n *p = \'\\0\';\n return p;\n err:\n X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE);\n end:\n BUF_MEM_free(b);\n return NULL;\n}']
35,506
0
https://github.com/openssl/openssl/blob/5d1c09de1f2736e1d4b1877206d08455ec75f558/crypto/bn/bn_sqr.c/#L108
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 run_srp(const char *username, const char *client_pass,\n const char *server_pass)\n{\n int ret = 0;\n BIGNUM *s = NULL;\n BIGNUM *v = NULL;\n BIGNUM *a = NULL;\n BIGNUM *b = NULL;\n BIGNUM *u = NULL;\n BIGNUM *x = NULL;\n BIGNUM *Apub = NULL;\n BIGNUM *Bpub = NULL;\n BIGNUM *Kclient = NULL;\n BIGNUM *Kserver = NULL;\n unsigned char rand_tmp[RANDOM_SIZE];\n const SRP_gN *GN;\n if (!TEST_ptr(GN = SRP_get_default_gN("1024")))\n return 0;\n if (!TEST_true(SRP_create_verifier_BN(username, server_pass,\n &s, &v, GN->N, GN->g)))\n goto end;\n test_output_bignum("N", GN->N);\n test_output_bignum("g", GN->g);\n test_output_bignum("Salt", s);\n test_output_bignum("Verifier", v);\n RAND_bytes(rand_tmp, sizeof(rand_tmp));\n b = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);\n if (!TEST_BN_ne_zero(b))\n goto end;\n test_output_bignum("b", b);\n Bpub = SRP_Calc_B(b, GN->N, GN->g, v);\n test_output_bignum("B", Bpub);\n if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))\n goto end;\n RAND_bytes(rand_tmp, sizeof(rand_tmp));\n a = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);\n if (!TEST_BN_ne_zero(a))\n goto end;\n test_output_bignum("a", a);\n Apub = SRP_Calc_A(a, GN->N, GN->g);\n test_output_bignum("A", Apub);\n if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))\n goto end;\n u = SRP_Calc_u(Apub, Bpub, GN->N);\n x = SRP_Calc_x(s, username, client_pass);\n Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);\n test_output_bignum("Client\'s key", Kclient);\n Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);\n test_output_bignum("Server\'s key", Kserver);\n if (!TEST_BN_eq(Kclient, Kserver))\n goto end;\n ret = 1;\nend:\n BN_clear_free(Kclient);\n BN_clear_free(Kserver);\n BN_clear_free(x);\n BN_free(u);\n BN_free(Apub);\n BN_clear_free(a);\n BN_free(Bpub);\n BN_clear_free(b);\n BN_free(s);\n BN_clear_free(v);\n return ret;\n}', 'int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,\n BIGNUM **verifier, const BIGNUM *N,\n const BIGNUM *g)\n{\n int result = 0;\n BIGNUM *x = NULL;\n BN_CTX *bn_ctx = BN_CTX_new();\n unsigned char tmp2[MAX_LEN];\n BIGNUM *salttmp = NULL;\n if ((user == NULL) ||\n (pass == NULL) ||\n (salt == NULL) ||\n (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))\n goto err;\n if (*salt == NULL) {\n if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n goto err;\n salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n } else {\n salttmp = *salt;\n }\n x = SRP_Calc_x(salttmp, user, pass);\n *verifier = BN_new();\n if (*verifier == NULL)\n goto err;\n if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {\n BN_clear_free(*verifier);\n goto err;\n }\n result = 1;\n *salt = salttmp;\n err:\n if (salt != NULL && *salt != salttmp)\n BN_clear_free(salttmp);\n BN_clear_free(x);\n BN_CTX_free(bn_ctx);\n return result;\n}', '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_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#ifdef RSAZ_ENABLED\n if (!a->neg) {\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!bn_to_mont_fixed_top(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(&am, a, m, ctx))\n goto err;\n if (!bn_to_mont_fixed_top(&am, &am, mont, ctx))\n goto err;\n } else if (!bn_to_mont_fixed_top(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits > 0) {\n if (bits < stride)\n stride = bits;\n bits -= stride;\n wvalue = bn_get_bits(p, bits);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7) {\n while (bits > 0) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n } else {\n while (bits > 0) {\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx);\n}', 'int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!bn_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_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 rr->top = max;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
35,507
0
https://github.com/openssl/openssl/blob/d4b009d5f88875ac0e3ac0b2b9689ed16a4c88dc/crypto/objects/o_names.c/#L93
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) { CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); name_funcs_stack = sk_NAME_FUNCS_new_null(); CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE); } 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++) { CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); name_funcs = OPENSSL_zalloc(sizeof(*name_funcs)); CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE); if (name_funcs == NULL) { 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; CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); sk_NAME_FUNCS_push(name_funcs_stack, name_funcs); CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE); } 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 int (*cmp_func) (const char *, const char *),\n void (*free_func) (const char *, int, const char *))\n{\n int ret;\n int i;\n NAME_FUNCS *name_funcs;\n if (name_funcs_stack == NULL) {\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);\n name_funcs_stack = sk_NAME_FUNCS_new_null();\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);\n }\n if (name_funcs_stack == NULL) {\n return (0);\n }\n ret = names_type_num;\n names_type_num++;\n for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);\n name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);\n if (name_funcs == NULL) {\n OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);\n return (0);\n }\n name_funcs->hash_func = lh_strhash;\n name_funcs->cmp_func = OPENSSL_strcmp;\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);\n sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);\n }\n name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);\n if (hash_func != NULL)\n name_funcs->hash_func = hash_func;\n if (cmp_func != NULL)\n name_funcs->cmp_func = cmp_func;\n if (free_func != NULL)\n name_funcs->free_func = free_func;\n return (ret);\n}', 'int CRYPTO_mem_ctrl(int mode)\n{\n#ifdef OPENSSL_NO_CRYPTO_MDEBUG\n return mode - mode;\n#else\n int ret = mh_mode;\n CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);\n switch (mode) {\n default:\n break;\n case CRYPTO_MEM_CHECK_ON:\n mh_mode = CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE;\n num_disable = 0;\n break;\n case CRYPTO_MEM_CHECK_OFF:\n mh_mode = 0;\n num_disable = 0;\n break;\n case CRYPTO_MEM_CHECK_DISABLE:\n if (mh_mode & CRYPTO_MEM_CHECK_ON) {\n CRYPTO_THREADID cur;\n CRYPTO_THREADID_current(&cur);\n if (!num_disable\n || CRYPTO_THREADID_cmp(&disabling_threadid, &cur)) {\n CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);\n CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2);\n CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);\n mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE;\n CRYPTO_THREADID_cpy(&disabling_threadid, &cur);\n }\n num_disable++;\n }\n break;\n case CRYPTO_MEM_CHECK_ENABLE:\n if (mh_mode & CRYPTO_MEM_CHECK_ON) {\n if (num_disable) {\n num_disable--;\n if (num_disable == 0) {\n mh_mode |= CRYPTO_MEM_CHECK_ENABLE;\n CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);\n }\n }\n }\n break;\n }\n CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);\n return (ret);\n#endif\n}', '_STACK *sk_new_null(void)\n{\n return sk_new((int (*)(const void *, const void *))0);\n}', '_STACK *sk_new(int (*c) (const void *, const void *))\n{\n _STACK *ret;\n if ((ret = OPENSSL_zalloc(sizeof(_STACK))) == NULL)\n goto err;\n if ((ret->data = OPENSSL_zalloc(sizeof(*ret->data) * MIN_NODES)) == NULL)\n goto err;\n ret->comp = c;\n ret->num_alloc = MIN_NODES;\n return (ret);\n err:\n OPENSSL_free(ret);\n return (NULL);\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 sk_num(const _STACK *st)\n{\n if (st == NULL)\n return -1;\n return st->num;\n}', 'int sk_push(_STACK *st, void *data)\n{\n return (sk_insert(st, data, st->num));\n}', 'int sk_insert(_STACK *st, void *data, int loc)\n{\n char **s;\n if (st == NULL)\n return 0;\n if (st->num_alloc <= st->num + 1) {\n s = OPENSSL_realloc((char *)st->data,\n (unsigned int)sizeof(char *) * st->num_alloc * 2);\n if (s == NULL)\n return (0);\n st->data = s;\n st->num_alloc *= 2;\n }\n if ((loc >= (int)st->num) || (loc < 0))\n st->data[st->num] = data;\n else {\n memmove(&(st->data[loc + 1]),\n &(st->data[loc]), sizeof(char *) * (st->num - loc));\n st->data[loc] = data;\n }\n st->num++;\n st->sorted = 0;\n return (st->num);\n}', 'void *sk_value(const _STACK *st, int i)\n{\n if (!st || (i < 0) || (i >= st->num))\n return NULL;\n return st->data[i];\n}']
35,508
0
https://github.com/openssl/openssl/blob/1e552869960659b4baac425bbf46cf6d3bd5e7e4/crypto/rc2/rc2_skey.c/#L136
void RC2_set_key(RC2_KEY *key, int len, const unsigned char *data, int bits) { int i,j; unsigned char *k; RC2_INT *ki; unsigned int c,d; k= (unsigned char *)&(key->data[0]); *k=0; if (len > 128) len=128; if (bits <= 0) bits=1024; if (bits > 1024) bits=1024; for (i=0; i<len; i++) k[i]=data[i]; d=k[len-1]; j=0; for (i=len; i < 128; i++,j++) { d=key_table[(k[j]+d)&0xff]; k[i]=d; } j=(bits+7)>>3; i=128-j; c= (0xff>>(-bits & 0x07)); d=key_table[k[i]&c]; k[i]=d; while (i--) { d=key_table[k[i+j]^d]; k[i]=d; } ki= &(key->data[63]); for (i=127; i>=0; i-=2) *(ki--)=((k[i]<<8)|k[i-1])&0xffff; }
['void RC2_set_key(RC2_KEY *key, int len, const unsigned char *data, int bits)\n\t{\n\tint i,j;\n\tunsigned char *k;\n\tRC2_INT *ki;\n\tunsigned int c,d;\n\tk= (unsigned char *)&(key->data[0]);\n\t*k=0;\n\tif (len > 128) len=128;\n\tif (bits <= 0) bits=1024;\n\tif (bits > 1024) bits=1024;\n\tfor (i=0; i<len; i++)\n\t\tk[i]=data[i];\n\td=k[len-1];\n\tj=0;\n\tfor (i=len; i < 128; i++,j++)\n\t\t{\n\t\td=key_table[(k[j]+d)&0xff];\n\t\tk[i]=d;\n\t\t}\n\tj=(bits+7)>>3;\n\ti=128-j;\n\tc= (0xff>>(-bits & 0x07));\n\td=key_table[k[i]&c];\n\tk[i]=d;\n\twhile (i--)\n\t\t{\n\t\td=key_table[k[i+j]^d];\n\t\tk[i]=d;\n\t\t}\n\tki= &(key->data[63]);\n\tfor (i=127; i>=0; i-=2)\n\t\t*(ki--)=((k[i]<<8)|k[i-1])&0xffff;\n\t}']
35,509
0
https://github.com/libav/libav/blob/dad7a9c7c0ae8ebc56f2e3a24e6fa4da5c2cd491/libavcodec/bitstream.h/#L237
static inline void skip_remaining(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE bc->bits >>= n; #else bc->bits <<= n; #endif bc->bits_left -= n; }
['static int decode_seq_header(AVSContext *h)\n{\n int frame_rate_code;\n int width, height;\n h->profile = bitstream_read(&h->bc, 8);\n h->level = bitstream_read(&h->bc, 8);\n bitstream_skip(&h->bc, 1);\n width = bitstream_read(&h->bc, 14);\n height = bitstream_read(&h->bc, 14);\n if ((h->width || h->height) && (h->width != width || h->height != height)) {\n avpriv_report_missing_feature(h->avctx,\n "Width/height changing in CAVS");\n return AVERROR_PATCHWELCOME;\n }\n h->width = width;\n h->height = height;\n bitstream_skip(&h->bc, 2);\n bitstream_skip(&h->bc, 3);\n h->aspect_ratio = bitstream_read(&h->bc, 4);\n frame_rate_code = bitstream_read(&h->bc, 4);\n bitstream_skip(&h->bc, 18);\n bitstream_skip(&h->bc, 1);\n bitstream_skip(&h->bc, 12);\n h->low_delay = bitstream_read_bit(&h->bc);\n h->mb_width = (h->width + 15) >> 4;\n h->mb_height = (h->height + 15) >> 4;\n h->avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code];\n h->avctx->width = h->width;\n h->avctx->height = h->height;\n if (!h->top_qp)\n ff_cavs_init_top_lines(h);\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n <= bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n skip_remaining(bc, bc->bits_left);\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void skip_remaining(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n bc->bits >>= n;\n#else\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n}']
35,510
0
https://github.com/openssl/openssl/blob/0bde1089f895718db2fe2637fda4a0c2ed6df904/crypto/lhash/lhash.c/#L356
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(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; } }
['int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len)\n\t{\n\tint al,i,j,n,ret;\n\tSSL3_RECORD *rr;\n\tvoid (*cb)()=NULL;\n\tBIO *bio;\n\tif (s->s3->rbuf.buf == NULL)\n\t\tif (!ssl3_setup_buffers(s))\n\t\t\treturn(-1);\n\tif (!s->in_handshake && SSL_in_init(s))\n\t\t{\n\t\ti=s->handshake_func(s);\n\t\tif (i < 0) return(i);\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);\n\t\t\treturn(-1);\n\t\t\t}\n\t\t}\nstart:\n\ts->rwstate=SSL_NOTHING;\n\trr= &(s->s3->rrec);\n\tif ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY))\n\t\t{\n\t\tret=ssl3_get_record(s);\n\t\tif (ret <= 0) return(ret);\n\t\t}\n\tif (s->s3->change_cipher_spec && (rr->type != SSL3_RT_HANDSHAKE))\n\t\t{\n\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);\n\t\tgoto err;\n\t\t}\n\tif (s->shutdown & SSL_RECEIVED_SHUTDOWN)\n\t\t{\n\t\trr->length=0;\n\t\ts->rwstate=SSL_NOTHING;\n\t\treturn(0);\n\t\t}\n\tif ((rr->type == SSL3_RT_HANDSHAKE) && (rr->length == 4) &&\n\t\t(rr->data[0] == SSL3_MT_HELLO_REQUEST) &&\n\t\t(s->session != NULL) && (s->session->cipher != NULL))\n\t\t{\n\t\tif ((rr->data[1] != 0) || (rr->data[2] != 0) ||\n\t\t\t(rr->data[3] != 0))\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_HELLO_REQUEST);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (SSL_is_init_finished(s) &&\n\t\t\t!(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&\n\t\t\t!s->s3->renegotiate)\n\t\t\t{\n\t\t\tssl3_renegotiate(s);\n\t\t\tif (ssl3_renegotiate_check(s))\n\t\t\t\t{\n\t\t\t\tn=s->handshake_func(s);\n\t\t\t\tif (n < 0) return(n);\n\t\t\t\tif (n == 0)\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);\n\t\t\t\t\treturn(-1);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\trr->length=0;\n \tgoto start;\n\t\t}\n\tif ((rr->type != type) || (s->shutdown & SSL_SENT_SHUTDOWN))\n\t\t{\n\t\tif (rr->type == SSL3_RT_ALERT)\n\t\t\t{\n\t\t\tif ((rr->length != 2) || (rr->off != 0))\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_ALERT_RECORD);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\ti=rr->data[0];\n\t\t\tn=rr->data[1];\n\t\t\trr->length=0;\n\t\t\tif (s->info_callback != NULL)\n\t\t\t\tcb=s->info_callback;\n\t\t\telse if (s->ctx->info_callback != NULL)\n\t\t\t\tcb=s->ctx->info_callback;\n\t\t\tif (cb != NULL)\n\t\t\t\t{\n\t\t\t\tj=(i<<8)|n;\n\t\t\t\tcb(s,SSL_CB_READ_ALERT,j);\n\t\t\t\t}\n\t\t\tif (i == 1)\n\t\t\t\t{\n\t\t\t\ts->s3->warn_alert=n;\n\t\t\t\tif (n == SSL_AD_CLOSE_NOTIFY)\n\t\t\t\t\t{\n\t\t\t\t\ts->shutdown|=SSL_RECEIVED_SHUTDOWN;\n\t\t\t\t\treturn(0);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse if (i == 2)\n\t\t\t\t{\n\t\t\t\tchar tmp[16];\n\t\t\t\ts->rwstate=SSL_NOTHING;\n\t\t\t\ts->s3->fatal_alert=n;\n\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,\n\t\t\t\t\tSSL_AD_REASON_OFFSET+n);\n\t\t\t\tsprintf(tmp,"%d",n);\n\t\t\t\tERR_add_error_data(2,"SSL alert number ",tmp);\n\t\t\t\ts->shutdown|=SSL_RECEIVED_SHUTDOWN;\n\t\t\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\t\t\treturn(0);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNKNOWN_ALERT_TYPE);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\trr->length=0;\n\t\t\tgoto start;\n\t\t\t}\n\t\tif (s->shutdown & SSL_SENT_SHUTDOWN)\n\t\t\t{\n\t\t\ts->rwstate=SSL_NOTHING;\n\t\t\trr->length=0;\n\t\t\treturn(0);\n\t\t\t}\n\t\tif (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC)\n\t\t\t{\n\t\t\tif (\t(rr->length != 1) || (rr->off != 0) ||\n\t\t\t\t(rr->data[0] != SSL3_MT_CCS))\n\t\t\t\t{\n\t\t\t\ti=SSL_AD_ILLEGAL_PARAMETER;\n\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_CHANGE_CIPHER_SPEC);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\trr->length=0;\n\t\t\ts->s3->change_cipher_spec=1;\n\t\t\tif (!do_change_cipher_spec(s))\n\t\t\t\tgoto err;\n\t\t\telse\n\t\t\t\tgoto start;\n\t\t\t}\n\t\tif ((rr->type == SSL3_RT_HANDSHAKE) &&\n\t\t\t!s->in_handshake)\n\t\t\t{\n\t\t\tif (((s->state&SSL_ST_MASK) == SSL_ST_OK) &&\n\t\t\t\t!(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS))\n\t\t\t\t{\n\t\t\t\ts->state=SSL_ST_BEFORE|(s->server)\n\t\t\t\t\t\t?SSL_ST_ACCEPT\n\t\t\t\t\t\t:SSL_ST_CONNECT;\n\t\t\t\ts->new_session=1;\n\t\t\t\t}\n\t\t\tn=s->handshake_func(s);\n\t\t\tif (n < 0) return(n);\n\t\t\tif (n == 0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);\n\t\t\t\treturn(-1);\n\t\t\t\t}\n\t\t\ts->rwstate=SSL_READING;\n\t\t\tbio=SSL_get_rbio(s);\n\t\t\tBIO_clear_retry_flags(bio);\n\t\t\tBIO_set_retry_read(bio);\n\t\t\treturn(-1);\n\t\t\t}\n\t\tswitch (rr->type)\n\t\t\t{\n\t\tdefault:\n#ifndef NO_TLS\n\t\t\tif (s->version == TLS1_VERSION)\n\t\t\t\t{\n\t\t\t\tgoto start;\n\t\t\t\t}\n#endif\n\t\tcase SSL3_RT_CHANGE_CIPHER_SPEC:\n\t\tcase SSL3_RT_ALERT:\n\t\tcase SSL3_RT_HANDSHAKE:\n\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD);\n\t\t\tgoto f_err;\n\t\tcase SSL3_RT_APPLICATION_DATA:\n\t\t\tif (s->s3->in_read_app_data &&\n\t\t\t\t(s->s3->total_renegotiations != 0) &&\n\t\t\t\t((\n\t\t\t\t (s->state & SSL_ST_CONNECT) &&\n\t\t\t\t (s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&\n\t\t\t\t (s->state <= SSL3_ST_CR_SRVR_HELLO_A)\n\t\t\t\t ) || (\n\t\t\t\t (s->state & SSL_ST_ACCEPT) &&\n\t\t\t\t (s->state <= SSL3_ST_SW_HELLO_REQ_A) &&\n\t\t\t\t (s->state >= SSL3_ST_SR_CLNT_HELLO_A)\n\t\t\t\t )\n\t\t\t\t))\n\t\t\t\t{\n\t\t\t\ts->s3->in_read_app_data=0;\n\t\t\t\treturn(-1);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&\n\t\t(s->enc_read_ctx == NULL))\n\t\t{\n\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_APP_DATA_IN_HANDSHAKE);\n\t\tgoto f_err;\n\t\t}\n\tif (len <= 0) return(len);\n\tif ((unsigned int)len > rr->length)\n\t\tn=rr->length;\n\telse\n\t\tn=len;\n\tmemcpy(buf,&(rr->data[rr->off]),(unsigned int)n);\n\trr->length-=n;\n\trr->off+=n;\n\tif (rr->length == 0)\n\t\t{\n\t\ts->rstate=SSL_ST_READ_HEADER;\n\t\trr->off=0;\n\t\t}\n\tif (type == SSL3_RT_HANDSHAKE)\n\t\tssl3_finish_mac(s,buf,n);\n\treturn(n);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\treturn(-1);\n\t}', 'static int ssl3_get_record(SSL *s)\n\t{\n\tint ssl_major,ssl_minor,al;\n\tint n,i,ret= -1;\n\tSSL3_BUFFER *rb;\n\tSSL3_RECORD *rr;\n\tSSL_SESSION *sess;\n\tunsigned char *p;\n\tunsigned char md[EVP_MAX_MD_SIZE];\n\tshort version;\n\tunsigned int mac_size;\n\tint clear=0,extra;\n\trr= &(s->s3->rrec);\n\trb= &(s->s3->rbuf);\n\tsess=s->session;\n\tif (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)\n\t\textra=SSL3_RT_MAX_EXTRA;\n\telse\n\t\textra=0;\nagain:\n\tif (\t(s->rstate != SSL_ST_READ_BODY) ||\n\t\t(s->packet_length < SSL3_RT_HEADER_LENGTH))\n\t\t{\n\t\tn=ssl3_read_n(s,SSL3_RT_HEADER_LENGTH,\n\t\t\tSSL3_RT_MAX_PACKET_SIZE,0);\n\t\tif (n <= 0) return(n);\n\t\ts->rstate=SSL_ST_READ_BODY;\n\t\tp=s->packet;\n\t\trr->type= *(p++);\n\t\tssl_major= *(p++);\n\t\tssl_minor= *(p++);\n\t\tversion=(ssl_major<<8)|ssl_minor;\n\t\tn2s(p,rr->length);\n\t\tif (s->first_packet)\n\t\t\t{\n\t\t\ts->first_packet=0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (version != s->version)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);\n\t\t\t\ts->version=version;\n\t\t\t\tal=SSL_AD_PROTOCOL_VERSION;\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\tif ((version>>8) != SSL3_VERSION_MAJOR)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (rr->length >\n\t\t\t(unsigned int)SSL3_RT_MAX_ENCRYPTED_LENGTH+extra)\n\t\t\t{\n\t\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PACKET_LENGTH_TOO_LONG);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\tif (rr->length > (s->packet_length-SSL3_RT_HEADER_LENGTH))\n\t\t{\n\t\ti=rr->length;\n\t\tn=ssl3_read_n(s,i,i,1);\n\t\tif (n <= 0) return(n);\n\t\t}\n\ts->rstate=SSL_ST_READ_HEADER;\n\trr->input= &(s->packet[SSL3_RT_HEADER_LENGTH]);\n\tif (rr->length > (unsigned int)SSL3_RT_MAX_ENCRYPTED_LENGTH+extra)\n\t\t{\n\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_ENCRYPTED_LENGTH_TOO_LONG);\n\t\tgoto f_err;\n\t\t}\n\trr->data=rr->input;\n\tif (!s->method->ssl3_enc->enc(s,0))\n\t\t{\n\t\tal=SSL_AD_DECRYPT_ERROR;\n\t\tgoto f_err;\n\t\t}\n#ifdef TLS_DEBUG\nprintf("dec %d\\n",rr->length);\n{ unsigned int z; for (z=0; z<rr->length; z++) printf("%02X%c",rr->data[z],((z+1)%16)?\' \':\'\\n\'); }\nprintf("\\n");\n#endif\n\tif (\t(sess == NULL) ||\n\t\t(s->enc_read_ctx == NULL) ||\n\t\t(s->read_hash == NULL))\n\t\tclear=1;\n\tif (!clear)\n\t\t{\n\t\tmac_size=EVP_MD_size(s->read_hash);\n\t\tif (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra+mac_size)\n\t\t\t{\n\t\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PRE_MAC_LENGTH_TOO_LONG);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (rr->length < mac_size)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_LENGTH_TOO_SHORT);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\trr->length-=mac_size;\n\t\ti=s->method->ssl3_enc->mac(s,md,0);\n\t\tif (memcmp(md,&(rr->data[rr->length]),mac_size) != 0)\n\t\t\t{\n\t\t\tal=SSL_AD_BAD_RECORD_MAC;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_BAD_MAC_DECODE);\n\t\t\tret= -1;\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\tif (s->expand != NULL)\n\t\t{\n\t\tif (rr->length >\n\t\t\t(unsigned int)SSL3_RT_MAX_COMPRESSED_LENGTH+extra)\n\t\t\t{\n\t\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_COMPRESSED_LENGTH_TOO_LONG);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!do_uncompress(s))\n\t\t\t{\n\t\t\tal=SSL_AD_DECOMPRESSION_FAILURE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_BAD_DECOMPRESSION);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\tif (rr->length > (unsigned int)SSL3_RT_MAX_PLAIN_LENGTH+extra)\n\t\t{\n\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_DATA_LENGTH_TOO_LONG);\n\t\tgoto f_err;\n\t\t}\n\trr->off=0;\n\ts->packet_length=0;\n\tif (rr->length == 0) goto again;\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\treturn(ret);\n\t}', 'void ssl3_send_alert(SSL *s, int level, int desc)\n\t{\n\tdesc=s->method->ssl3_enc->alert_value(desc);\n\tif (desc < 0) return;\n\tif ((level == 2) && (s->session != NULL))\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\ts->s3->alert_dispatch=1;\n\ts->s3->send_alert[0]=level;\n\ts->s3->send_alert[1]=desc;\n\tif (s->s3->wbuf.left == 0)\n\t\tssl3_dispatch_alert(s);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'void *lh_delete(LHASH *lh, void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tvoid *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}', '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(lh->b,\n\t\t\t(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));\n\t\tif (n == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn;\n\t\t\t}\n\t\tlh->num_contract_reallocs++;\n\t\tlh->num_alloc_nodes/=2;\n\t\tlh->pmax/=2;\n\t\tlh->p=lh->pmax-1;\n\t\tlh->b=n;\n\t\t}\n\telse\n\t\tlh->p--;\n\tlh->num_nodes--;\n\tlh->num_contracts++;\n\tn1=lh->b[(int)lh->p];\n\tif (n1 == NULL)\n\t\tlh->b[(int)lh->p]=np;\n\telse\n\t\t{\n\t\twhile (n1->next != NULL)\n\t\t\tn1=n1->next;\n\t\tn1->next=np;\n\t\t}\n\t}']
35,511
0
https://github.com/openssl/openssl/blob/38f5c30b311f0e736081e0b64b22e917b651536a/ssl/statem/statem_srvr.c/#L3652
static int tls_construct_hello_retry_request(SSL *s, WPACKET *pkt) { int al; if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT) || !tls_construct_extensions(s, pkt, EXT_TLS1_3_HELLO_RETRY_REQUEST, NULL, 0, &al)) { ssl3_send_alert(s, SSL3_AL_FATAL, al); SSLerr(SSL_F_TLS_CONSTRUCT_HELLO_RETRY_REQUEST, ERR_R_INTERNAL_ERROR); ssl3_send_alert(s, SSL3_AL_FATAL, al); return 0; } SSL_SESSION_free(s->session); s->session = NULL; s->hit = 0; return 1; }
['static int tls_construct_hello_retry_request(SSL *s, WPACKET *pkt)\n{\n int al;\n if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)\n || !tls_construct_extensions(s, pkt, EXT_TLS1_3_HELLO_RETRY_REQUEST,\n NULL, 0, &al)) {\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\n SSLerr(SSL_F_TLS_CONSTRUCT_HELLO_RETRY_REQUEST, ERR_R_INTERNAL_ERROR);\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\n return 0;\n }\n SSL_SESSION_free(s->session);\n s->session = NULL;\n s->hit = 0;\n return 1;\n}']
35,512
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L231
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_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}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
35,513
0
https://github.com/openssl/openssl/blob/b8a3f39b890304757058deb730c855b72c14947b/crypto/ec/curve448/arch_32/f_impl.c/#L41
void gf_mul(gf_s * RESTRICT cs, const gf as, const gf bs) { const uint32_t *a = as->limb, *b = bs->limb; uint32_t *c = cs->limb; uint64_t accum0 = 0, accum1 = 0, accum2 = 0; uint32_t mask = (1 << 28) - 1; uint32_t aa[8], bb[8]; int i, j; for (i = 0; i < 8; i++) { aa[i] = a[i] + a[i + 8]; bb[i] = b[i] + b[i + 8]; } for (j = 0; j < 8; j++) { accum2 = 0; for (i = 0; i < j + 1; i++) { accum2 += widemul(a[j - i], b[i]); accum1 += widemul(aa[j - i], bb[i]); accum0 += widemul(a[8 + j - i], b[8 + i]); } accum1 -= accum2; accum0 += accum2; accum2 = 0; for (i = j + 1; i < 8; i++) { accum0 -= widemul(a[8 + j - i], b[i]); accum2 += widemul(aa[8 + j - i], bb[i]); accum1 += widemul(a[16 + j - i], b[8 + i]); } accum1 += accum2; accum0 += accum2; c[j] = ((uint32_t)(accum0)) & mask; c[j + 8] = ((uint32_t)(accum1)) & mask; accum0 >>= 28; accum1 >>= 28; } accum0 += accum1; accum0 += c[8]; accum1 += c[0]; c[8] = ((uint32_t)(accum0)) & mask; c[0] = ((uint32_t)(accum1)) & mask; accum0 >>= 28; accum1 >>= 28; c[9] += ((uint32_t)(accum0)); c[1] += ((uint32_t)(accum1)); }
['void gf_mul(gf_s * RESTRICT cs, const gf as, const gf bs)\n{\n const uint32_t *a = as->limb, *b = bs->limb;\n uint32_t *c = cs->limb;\n uint64_t accum0 = 0, accum1 = 0, accum2 = 0;\n uint32_t mask = (1 << 28) - 1;\n uint32_t aa[8], bb[8];\n int i, j;\n for (i = 0; i < 8; i++) {\n aa[i] = a[i] + a[i + 8];\n bb[i] = b[i] + b[i + 8];\n }\n for (j = 0; j < 8; j++) {\n accum2 = 0;\n for (i = 0; i < j + 1; i++) {\n accum2 += widemul(a[j - i], b[i]);\n accum1 += widemul(aa[j - i], bb[i]);\n accum0 += widemul(a[8 + j - i], b[8 + i]);\n }\n accum1 -= accum2;\n accum0 += accum2;\n accum2 = 0;\n for (i = j + 1; i < 8; i++) {\n accum0 -= widemul(a[8 + j - i], b[i]);\n accum2 += widemul(aa[8 + j - i], bb[i]);\n accum1 += widemul(a[16 + j - i], b[8 + i]);\n }\n accum1 += accum2;\n accum0 += accum2;\n c[j] = ((uint32_t)(accum0)) & mask;\n c[j + 8] = ((uint32_t)(accum1)) & mask;\n accum0 >>= 28;\n accum1 >>= 28;\n }\n accum0 += accum1;\n accum0 += c[8];\n accum1 += c[0];\n c[8] = ((uint32_t)(accum0)) & mask;\n c[0] = ((uint32_t)(accum1)) & mask;\n accum0 >>= 28;\n accum1 >>= 28;\n c[9] += ((uint32_t)(accum0));\n c[1] += ((uint32_t)(accum1));\n}']
35,514
0
https://github.com/openssl/openssl/blob/8e826a339f8cda20a4311fa88a1de782972cf40d/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; }
['BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,\n const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)\n{\n BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;\n BN_CTX *bn_ctx;\n if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL\n || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n return NULL;\n if ((tmp = BN_new()) == NULL ||\n (tmp2 = BN_new()) == NULL ||\n (tmp3 = BN_new()) == NULL)\n goto err;\n if (!BN_mod_exp(tmp, g, x, N, bn_ctx))\n goto err;\n if ((k = srp_Calc_k(N, g)) == NULL)\n goto err;\n if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))\n goto err;\n if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))\n goto err;\n if (!BN_mul(tmp3, u, x, bn_ctx))\n goto err;\n if (!BN_add(tmp2, a, tmp3))\n goto err;\n K = BN_new();\n if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {\n BN_free(K);\n K = NULL;\n }\n err:\n BN_CTX_free(bn_ctx);\n BN_clear_free(tmp);\n BN_clear_free(tmp2);\n BN_clear_free(tmp3);\n BN_free(k);\n return K;\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_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n BN_MONT_CTX *mont = NULL;\n int b, bits, ret = 0;\n int r_is_one;\n BN_ULONG w, next_w;\n BIGNUM *r, *t;\n BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n (BN_mul_word(r, (w)) && \\\n ( \\\n (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n if (m->top == 1)\n a %= m->d[0];\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n if (a == 0) {\n BN_zero(rr);\n ret = 1;\n return ret;\n }\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n r_is_one = 1;\n w = a;\n for (b = bits - 2; b >= 0; b--) {\n next_w = w * w;\n if ((next_w / w) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = 1;\n }\n w = next_w;\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (BN_is_bit_set(p, b)) {\n next_w = w * a;\n if ((next_w / a) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = a;\n }\n w = next_w;\n }\n }\n if (w != 1) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n }\n if (r_is_one) {\n if (!BN_one(rr))\n goto err;\n } else {\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n }\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return (ret);\n}', '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 (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
35,515
0
https://github.com/libav/libav/blob/6aba117f1273c7704312c6d892c9f552fa0661bb/libavcodec/wmaenc.c/#L61
static int encode_init(AVCodecContext * avctx){ WMACodecContext *s = avctx->priv_data; int i, flags1, flags2; uint8_t *extradata; s->avctx = avctx; if(avctx->channels > MAX_CHANNELS) { av_log(avctx, AV_LOG_ERROR, "too many channels: got %i, need %i or fewer", avctx->channels, MAX_CHANNELS); return AVERROR(EINVAL); } if (avctx->sample_rate > 48000) { av_log(avctx, AV_LOG_ERROR, "sample rate is too high: %d > 48kHz", avctx->sample_rate); return AVERROR(EINVAL); } if(avctx->bit_rate < 24*1000) { av_log(avctx, AV_LOG_ERROR, "bitrate too low: got %i, need 24000 or higher\n", avctx->bit_rate); return AVERROR(EINVAL); } flags1 = 0; flags2 = 1; if (avctx->codec->id == CODEC_ID_WMAV1) { extradata= av_malloc(4); avctx->extradata_size= 4; AV_WL16(extradata, flags1); AV_WL16(extradata+2, flags2); } else if (avctx->codec->id == CODEC_ID_WMAV2) { extradata= av_mallocz(10); avctx->extradata_size= 10; AV_WL32(extradata, flags1); AV_WL16(extradata+4, flags2); }else assert(0); avctx->extradata= extradata; s->use_exp_vlc = flags2 & 0x0001; s->use_bit_reservoir = flags2 & 0x0002; s->use_variable_block_len = flags2 & 0x0004; if (avctx->channels == 2) s->ms_stereo = 1; ff_wma_init(avctx, flags2); for(i = 0; i < s->nb_block_sizes; i++) ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 0, 1.0); s->block_align = avctx->bit_rate * (int64_t)s->frame_len / (avctx->sample_rate * 8); s->block_align = FFMIN(s->block_align, MAX_CODED_SUPERFRAME_SIZE); avctx->block_align = s->block_align; avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate / s->frame_len; avctx->frame_size = avctx->delay = s->frame_len; #if FF_API_OLD_ENCODE_AUDIO avctx->coded_frame = &s->frame; avcodec_get_frame_defaults(avctx->coded_frame); #endif return 0; }
['static int encode_init(AVCodecContext * avctx){\n WMACodecContext *s = avctx->priv_data;\n int i, flags1, flags2;\n uint8_t *extradata;\n s->avctx = avctx;\n if(avctx->channels > MAX_CHANNELS) {\n av_log(avctx, AV_LOG_ERROR, "too many channels: got %i, need %i or fewer",\n avctx->channels, MAX_CHANNELS);\n return AVERROR(EINVAL);\n }\n if (avctx->sample_rate > 48000) {\n av_log(avctx, AV_LOG_ERROR, "sample rate is too high: %d > 48kHz",\n avctx->sample_rate);\n return AVERROR(EINVAL);\n }\n if(avctx->bit_rate < 24*1000) {\n av_log(avctx, AV_LOG_ERROR, "bitrate too low: got %i, need 24000 or higher\\n",\n avctx->bit_rate);\n return AVERROR(EINVAL);\n }\n flags1 = 0;\n flags2 = 1;\n if (avctx->codec->id == CODEC_ID_WMAV1) {\n extradata= av_malloc(4);\n avctx->extradata_size= 4;\n AV_WL16(extradata, flags1);\n AV_WL16(extradata+2, flags2);\n } else if (avctx->codec->id == CODEC_ID_WMAV2) {\n extradata= av_mallocz(10);\n avctx->extradata_size= 10;\n AV_WL32(extradata, flags1);\n AV_WL16(extradata+4, flags2);\n }else\n assert(0);\n avctx->extradata= extradata;\n s->use_exp_vlc = flags2 & 0x0001;\n s->use_bit_reservoir = flags2 & 0x0002;\n s->use_variable_block_len = flags2 & 0x0004;\n if (avctx->channels == 2)\n s->ms_stereo = 1;\n ff_wma_init(avctx, flags2);\n for(i = 0; i < s->nb_block_sizes; i++)\n ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 0, 1.0);\n s->block_align = avctx->bit_rate * (int64_t)s->frame_len /\n (avctx->sample_rate * 8);\n s->block_align = FFMIN(s->block_align, MAX_CODED_SUPERFRAME_SIZE);\n avctx->block_align = s->block_align;\n avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate /\n s->frame_len;\n avctx->frame_size = avctx->delay = s->frame_len;\n#if FF_API_OLD_ENCODE_AUDIO\n avctx->coded_frame = &s->frame;\n avcodec_get_frame_defaults(avctx->coded_frame);\n#endif\n return 0;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-32) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+32);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&31) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,32,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(32,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
35,516
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_gf2m.c/#L631
static int BN_GF2m_mod_inv_vartime(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp; int ret = 0; bn_check_top(a); bn_check_top(p); BN_CTX_start(ctx); b = BN_CTX_get(ctx); c = BN_CTX_get(ctx); u = BN_CTX_get(ctx); v = BN_CTX_get(ctx); if (v == NULL) goto err; if (!BN_GF2m_mod(u, a, p)) goto err; if (BN_is_zero(u)) goto err; if (!BN_copy(v, p)) goto err; # if 0 if (!BN_one(b)) goto err; while (1) { while (!BN_is_odd(u)) { if (BN_is_zero(u)) goto err; if (!BN_rshift1(u, u)) goto err; if (BN_is_odd(b)) { if (!BN_GF2m_add(b, b, p)) goto err; } if (!BN_rshift1(b, b)) goto err; } if (BN_abs_is_word(u, 1)) break; if (BN_num_bits(u) < BN_num_bits(v)) { tmp = u; u = v; v = tmp; tmp = b; b = c; c = tmp; } if (!BN_GF2m_add(u, u, v)) goto err; if (!BN_GF2m_add(b, b, c)) goto err; } # else { int i; int ubits = BN_num_bits(u); int vbits = BN_num_bits(v); int top = p->top; BN_ULONG *udp, *bdp, *vdp, *cdp; if (!bn_wexpand(u, top)) goto err; udp = u->d; for (i = u->top; i < top; i++) udp[i] = 0; u->top = top; if (!bn_wexpand(b, top)) goto err; bdp = b->d; bdp[0] = 1; for (i = 1; i < top; i++) bdp[i] = 0; b->top = top; if (!bn_wexpand(c, top)) goto err; cdp = c->d; for (i = 0; i < top; i++) cdp[i] = 0; c->top = top; vdp = v->d; while (1) { while (ubits && !(udp[0] & 1)) { BN_ULONG u0, u1, b0, b1, mask; u0 = udp[0]; b0 = bdp[0]; mask = (BN_ULONG)0 - (b0 & 1); b0 ^= p->d[0] & mask; for (i = 0; i < top - 1; i++) { u1 = udp[i + 1]; udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2; u0 = u1; b1 = bdp[i + 1] ^ (p->d[i + 1] & mask); bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2; b0 = b1; } udp[i] = u0 >> 1; bdp[i] = b0 >> 1; ubits--; } if (ubits <= BN_BITS2) { if (udp[0] == 0) goto err; if (udp[0] == 1) break; } if (ubits < vbits) { i = ubits; ubits = vbits; vbits = i; tmp = u; u = v; v = tmp; tmp = b; b = c; c = tmp; udp = vdp; vdp = v->d; bdp = cdp; cdp = c->d; } for (i = 0; i < top; i++) { udp[i] ^= vdp[i]; bdp[i] ^= cdp[i]; } if (ubits == vbits) { BN_ULONG ul; int utop = (ubits - 1) / BN_BITS2; while ((ul = udp[utop]) == 0 && utop) utop--; ubits = utop * BN_BITS2 + BN_num_bits_word(ul); } } bn_correct_top(b); } # endif if (!BN_copy(r, b)) goto err; bn_check_top(r); ret = 1; err: # ifdef BN_DEBUG bn_correct_top(c); bn_correct_top(u); bn_correct_top(v); # endif BN_CTX_end(ctx); return ret; }
['int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,\n const int p[], BN_CTX *ctx)\n{\n BIGNUM *field;\n int ret = 0;\n bn_check_top(yy);\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_div(r, yy, xx, field, ctx);\n bn_check_top(r);\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,\n const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *xinv = NULL;\n int ret = 0;\n bn_check_top(y);\n bn_check_top(x);\n bn_check_top(p);\n BN_CTX_start(ctx);\n xinv = BN_CTX_get(ctx);\n if (xinv == NULL)\n goto err;\n if (!BN_GF2m_mod_inv(xinv, x, p, ctx))\n goto err;\n if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *b = NULL;\n int ret = 0;\n BN_CTX_start(ctx);\n if ((b = BN_CTX_get(ctx)) == NULL)\n goto err;\n do {\n if (!BN_priv_rand(b, BN_num_bits(p) - 1,\n BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n goto err;\n } while (BN_is_zero(b));\n if (!BN_GF2m_mod_mul(r, a, b, p, ctx))\n goto err;\n if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx))\n goto err;\n if (!BN_GF2m_mod_mul(r, r, b, p, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\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_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const BIGNUM *p, BN_CTX *ctx)\n{\n int ret = 0;\n const int max = BN_num_bits(p) + 1;\n int *arr = NULL;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(p);\n if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)\n goto err;\n ret = BN_GF2m_poly2arr(p, arr, max);\n if (!ret || ret > max) {\n BNerr(BN_F_BN_GF2M_MOD_MUL, BN_R_INVALID_LENGTH);\n goto err;\n }\n ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);\n bn_check_top(r);\n err:\n OPENSSL_free(arr);\n return ret;\n}', 'static int BN_GF2m_mod_inv_vartime(BIGNUM *r, const BIGNUM *a,\n const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(p);\n BN_CTX_start(ctx);\n b = BN_CTX_get(ctx);\n c = BN_CTX_get(ctx);\n u = BN_CTX_get(ctx);\n v = BN_CTX_get(ctx);\n if (v == NULL)\n goto err;\n if (!BN_GF2m_mod(u, a, p))\n goto err;\n if (BN_is_zero(u))\n goto err;\n if (!BN_copy(v, p))\n goto err;\n# if 0\n if (!BN_one(b))\n goto err;\n while (1) {\n while (!BN_is_odd(u)) {\n if (BN_is_zero(u))\n goto err;\n if (!BN_rshift1(u, u))\n goto err;\n if (BN_is_odd(b)) {\n if (!BN_GF2m_add(b, b, p))\n goto err;\n }\n if (!BN_rshift1(b, b))\n goto err;\n }\n if (BN_abs_is_word(u, 1))\n break;\n if (BN_num_bits(u) < BN_num_bits(v)) {\n tmp = u;\n u = v;\n v = tmp;\n tmp = b;\n b = c;\n c = tmp;\n }\n if (!BN_GF2m_add(u, u, v))\n goto err;\n if (!BN_GF2m_add(b, b, c))\n goto err;\n }\n# else\n {\n int i;\n int ubits = BN_num_bits(u);\n int vbits = BN_num_bits(v);\n int top = p->top;\n BN_ULONG *udp, *bdp, *vdp, *cdp;\n if (!bn_wexpand(u, top))\n goto err;\n udp = u->d;\n for (i = u->top; i < top; i++)\n udp[i] = 0;\n u->top = top;\n if (!bn_wexpand(b, top))\n goto err;\n bdp = b->d;\n bdp[0] = 1;\n for (i = 1; i < top; i++)\n bdp[i] = 0;\n b->top = top;\n if (!bn_wexpand(c, top))\n goto err;\n cdp = c->d;\n for (i = 0; i < top; i++)\n cdp[i] = 0;\n c->top = top;\n vdp = v->d;\n while (1) {\n while (ubits && !(udp[0] & 1)) {\n BN_ULONG u0, u1, b0, b1, mask;\n u0 = udp[0];\n b0 = bdp[0];\n mask = (BN_ULONG)0 - (b0 & 1);\n b0 ^= p->d[0] & mask;\n for (i = 0; i < top - 1; i++) {\n u1 = udp[i + 1];\n udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;\n u0 = u1;\n b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);\n bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;\n b0 = b1;\n }\n udp[i] = u0 >> 1;\n bdp[i] = b0 >> 1;\n ubits--;\n }\n if (ubits <= BN_BITS2) {\n if (udp[0] == 0)\n goto err;\n if (udp[0] == 1)\n break;\n }\n if (ubits < vbits) {\n i = ubits;\n ubits = vbits;\n vbits = i;\n tmp = u;\n u = v;\n v = tmp;\n tmp = b;\n b = c;\n c = tmp;\n udp = vdp;\n vdp = v->d;\n bdp = cdp;\n cdp = c->d;\n }\n for (i = 0; i < top; i++) {\n udp[i] ^= vdp[i];\n bdp[i] ^= cdp[i];\n }\n if (ubits == vbits) {\n BN_ULONG ul;\n int utop = (ubits - 1) / BN_BITS2;\n while ((ul = udp[utop]) == 0 && utop)\n utop--;\n ubits = utop * BN_BITS2 + BN_num_bits_word(ul);\n }\n }\n bn_correct_top(b);\n }\n# endif\n if (!BN_copy(r, b))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n# ifdef BN_DEBUG\n bn_correct_top(c);\n bn_correct_top(u);\n bn_correct_top(v);\n# endif\n BN_CTX_end(ctx);\n return ret;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}']
35,517
0
https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/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 BN_mod_exp_simple(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 *d;\n BIGNUM *val[TABLE_SIZE];\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_SIMPLE, 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 d = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\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(d, val[0], val[0], m, 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(val[i], val[i - 1], d, m, 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(r, r, r, m, 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(r, r, r, m, ctx))\n goto err;\n }\n if (!BN_mod_mul(r, r, val[wvalue >> 1], m, 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_check_top(r);\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_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\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_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}', '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}']
35,518
0
https://github.com/nginx/nginx/blob/7e4f193bb0e1cfa4128052f538dd60519cac02e4/src/core/ngx_palloc.c/#L31
ngx_pool_t * ngx_create_pool(size_t size, ngx_log_t *log) { ngx_pool_t *p; p = ngx_memalign(ngx_pagesize, size, log); if (p == NULL) { return NULL; } p->d.last = (u_char *) p + sizeof(ngx_pool_t); p->d.end = (u_char *) p + size; p->d.next = NULL; p->d.failed = 0; size = size - sizeof(ngx_pool_t); p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; p->current = p; p->chain = NULL; p->large = NULL; p->cleanup = NULL; p->log = log; return p; }
['int ngx_cdecl\nmain(int argc, char *const *argv)\n{\n ngx_int_t i;\n ngx_log_t *log;\n ngx_cycle_t *cycle, init_cycle;\n ngx_core_conf_t *ccf;\n if (ngx_get_options(argc, argv) != NGX_OK) {\n return 1;\n }\n if (ngx_show_version) {\n ngx_log_stderr(0, "nginx version: " NGINX_VER);\n if (ngx_show_help) {\n ngx_log_stderr(0,\n "Usage: nginx [-?hvVt] [-s signal] [-c filename] "\n "[-p prefix] [-g directives]" CRLF CRLF\n "Options:" CRLF\n " -?,-h : this help" CRLF\n " -v : show version and exit" CRLF\n " -V : show version and configure options then exit"\n CRLF\n " -t : test configuration and exit" CRLF\n " -s signal : send signal to a master process: "\n "stop, quit, reopen, reload" CRLF\n#ifdef NGX_PREFIX\n " -p prefix : set prefix path (default: "\n NGX_PREFIX ")" CRLF\n#else\n " -p prefix : set prefix path (default: NONE)" CRLF\n#endif\n " -c filename : set configuration file (default: "\n NGX_CONF_PATH ")" CRLF\n " -g directives : set global directives out of configuration "\n "file" CRLF\n );\n }\n if (ngx_show_configure) {\n#ifdef NGX_COMPILER\n ngx_log_stderr(0, "built by " NGX_COMPILER);\n#endif\n#if (NGX_SSL)\n#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME\n ngx_log_stderr(0, "TLS SNI support enabled");\n#else\n ngx_log_stderr(0, "TLS SNI support disabled");\n#endif\n#endif\n ngx_log_stderr(0, "configure arguments:" NGX_CONFIGURE);\n }\n if (!ngx_test_config) {\n return 0;\n }\n }\n#if (NGX_FREEBSD)\n ngx_debug_init();\n#endif\n ngx_max_sockets = -1;\n ngx_time_init();\n#if (NGX_PCRE)\n ngx_regex_init();\n#endif\n ngx_pid = ngx_getpid();\n log = ngx_log_init(ngx_prefix);\n if (log == NULL) {\n return 1;\n }\n#if (NGX_OPENSSL)\n ngx_ssl_init(log);\n#endif\n ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));\n init_cycle.log = log;\n ngx_cycle = &init_cycle;\n ngx_pagesize = 1024;\n init_cycle.pool = ngx_create_pool(1024, log);\n if (init_cycle.pool == NULL) {\n return 1;\n }\n if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) {\n return 1;\n }\n if (ngx_process_options(&init_cycle) != NGX_OK) {\n return 1;\n }\n if (ngx_os_init(log) != NGX_OK) {\n return 1;\n }\n if (ngx_crc32_table_init() != NGX_OK) {\n return 1;\n }\n if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) {\n return 1;\n }\n ngx_max_module = 0;\n for (i = 0; ngx_modules[i]; i++) {\n ngx_modules[i]->index = ngx_max_module++;\n }\n cycle = ngx_init_cycle(&init_cycle);\n if (cycle == NULL) {\n if (ngx_test_config) {\n ngx_log_stderr(0, "configuration file %s test failed",\n init_cycle.conf_file.data);\n }\n return 1;\n }\n if (ngx_test_config) {\n ngx_log_stderr(0, "configuration file %s test is successful",\n cycle->conf_file.data);\n return 0;\n }\n if (ngx_signal) {\n return ngx_signal_process(cycle, ngx_signal);\n }\n ngx_os_status(cycle->log);\n ngx_cycle = cycle;\n ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);\n if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {\n ngx_process = NGX_PROCESS_MASTER;\n }\n#if !(NGX_WIN32)\n if (ngx_init_signals(cycle->log) != NGX_OK) {\n return 1;\n }\n if (!ngx_inherited && ccf->daemon) {\n if (ngx_daemon(cycle->log) != NGX_OK) {\n return 1;\n }\n ngx_daemonized = 1;\n }\n#endif\n if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {\n return 1;\n }\n if (cycle->log->file->fd != ngx_stderr) {\n if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) {\n ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,\n ngx_set_stderr_n " failed");\n return 1;\n }\n }\n if (log->file->fd != ngx_stderr) {\n if (ngx_close_file(log->file->fd) == NGX_FILE_ERROR) {\n ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,\n ngx_close_file_n " built-in log failed");\n }\n }\n ngx_use_stderr = 0;\n if (ngx_process == NGX_PROCESS_SINGLE) {\n ngx_single_process_cycle(cycle);\n } else {\n ngx_master_process_cycle(cycle);\n }\n return 0;\n}', 'ngx_pool_t *\nngx_create_pool(size_t size, ngx_log_t *log)\n{\n ngx_pool_t *p;\n p = ngx_memalign(ngx_pagesize, size, log);\n if (p == NULL) {\n return NULL;\n }\n p->d.last = (u_char *) p + sizeof(ngx_pool_t);\n p->d.end = (u_char *) p + size;\n p->d.next = NULL;\n p->d.failed = 0;\n size = size - sizeof(ngx_pool_t);\n p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;\n p->current = p;\n p->chain = NULL;\n p->large = NULL;\n p->cleanup = NULL;\n p->log = log;\n return p;\n}']
35,519
0
https://github.com/openssl/openssl/blob/eaeb1870d4ef887a89f0bcec6b5ee1ec7fafa00f/crypto/asn1/p7_s_e.c/#L124
PKCS7_SIGN_ENVELOPE *PKCS7_SIGN_ENVELOPE_new(void) { PKCS7_SIGN_ENVELOPE *ret=NULL; ASN1_CTX c; M_ASN1_New_Malloc(ret,PKCS7_SIGN_ENVELOPE); M_ASN1_New(ret->version,ASN1_INTEGER_new); M_ASN1_New(ret->recipientinfo,sk_PKCS7_RECIP_INFO_new_null); M_ASN1_New(ret->md_algs,sk_X509_ALGOR_new_null); M_ASN1_New(ret->enc_data,PKCS7_ENC_CONTENT_new); ret->cert=NULL; ret->crl=NULL; M_ASN1_New(ret->signer_info,sk_PKCS7_SIGNER_INFO_new_null); return(ret); M_ASN1_New_Error(ASN1_F_PKCS7_SIGN_ENVELOPE_NEW); }
['PKCS7_SIGN_ENVELOPE *PKCS7_SIGN_ENVELOPE_new(void)\n\t{\n\tPKCS7_SIGN_ENVELOPE *ret=NULL;\n\tASN1_CTX c;\n\tM_ASN1_New_Malloc(ret,PKCS7_SIGN_ENVELOPE);\n\tM_ASN1_New(ret->version,ASN1_INTEGER_new);\n\tM_ASN1_New(ret->recipientinfo,sk_PKCS7_RECIP_INFO_new_null);\n\tM_ASN1_New(ret->md_algs,sk_X509_ALGOR_new_null);\n\tM_ASN1_New(ret->enc_data,PKCS7_ENC_CONTENT_new);\n\tret->cert=NULL;\n\tret->crl=NULL;\n\tM_ASN1_New(ret->signer_info,sk_PKCS7_SIGNER_INFO_new_null);\n\treturn(ret);\n\tM_ASN1_New_Error(ASN1_F_PKCS7_SIGN_ENVELOPE_NEW);\n\t}', 'ASN1_STRING *ASN1_STRING_type_new(int type)\n\t{\n\tASN1_STRING *ret;\n\tret=(ASN1_STRING *)Malloc(sizeof(ASN1_STRING));\n\tif (ret == NULL)\n\t\t{\n\t\tASN1err(ASN1_F_ASN1_STRING_TYPE_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->length=0;\n\tret->type=type;\n\tret->data=NULL;\n\tret->flags=0;\n\treturn(ret);\n\t}', 'IMPLEMENT_STACK_OF(PKCS7_RECIP_INFO)', '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}', 'IMPLEMENT_STACK_OF(X509_ALGOR)']
35,520
0
https://github.com/libav/libav/blob/e208e6d209728d332343aa5390ae377ac0a6305c/libavformat/utils.c/#L479
static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt) { if(st->codec->codec_id == AV_CODEC_ID_PROBE){ AVProbeData *pd = &st->probe_data; av_log(s, AV_LOG_DEBUG, "probing stream %d\n", st->index); --st->probe_packets; if (pkt) { int err; if ((err = av_reallocp(&pd->buf, pd->buf_size + pkt->size + AVPROBE_PADDING_SIZE)) < 0) return err; memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size); pd->buf_size += pkt->size; memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE); } else { st->probe_packets = 0; if (!pd->buf_size) { av_log(s, AV_LOG_ERROR, "nothing to probe for stream %d\n", st->index); return 0; } } if (!st->probe_packets || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) { set_codec_from_probe_data(s, st, pd, st->probe_packets > 0 ? AVPROBE_SCORE_MAX/4 : 0); if(st->codec->codec_id != AV_CODEC_ID_PROBE){ pd->buf_size=0; av_freep(&pd->buf); av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index); } } } return 0; }
['static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)\n{\n if(st->codec->codec_id == AV_CODEC_ID_PROBE){\n AVProbeData *pd = &st->probe_data;\n av_log(s, AV_LOG_DEBUG, "probing stream %d\\n", st->index);\n --st->probe_packets;\n if (pkt) {\n int err;\n if ((err = av_reallocp(&pd->buf, pd->buf_size + pkt->size +\n AVPROBE_PADDING_SIZE)) < 0)\n return err;\n memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size);\n pd->buf_size += pkt->size;\n memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);\n } else {\n st->probe_packets = 0;\n if (!pd->buf_size) {\n av_log(s, AV_LOG_ERROR, "nothing to probe for stream %d\\n",\n st->index);\n return 0;\n }\n }\n if (!st->probe_packets ||\n av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {\n set_codec_from_probe_data(s, st, pd, st->probe_packets > 0 ? AVPROBE_SCORE_MAX/4 : 0);\n if(st->codec->codec_id != AV_CODEC_ID_PROBE){\n pd->buf_size=0;\n av_freep(&pd->buf);\n av_log(s, AV_LOG_DEBUG, "probed stream %d\\n", st->index);\n }\n }\n }\n return 0;\n}', 'int av_reallocp(void *ptr, size_t size)\n{\n void **ptrptr = ptr;\n void *ret;\n if (!size) {\n av_freep(ptr);\n return 0;\n }\n ret = av_realloc(*ptrptr, size);\n if (!ret) {\n av_freep(ptr);\n return AVERROR(ENOMEM);\n }\n *ptrptr = ret;\n return 0;\n}', 'void *av_realloc(void *ptr, size_t size)\n{\n#if CONFIG_MEMALIGN_HACK\n int diff;\n#endif\n if (size > (INT_MAX - 16))\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n if (!ptr)\n return av_malloc(size);\n diff = ((char *)ptr)[-1];\n return (char *)realloc((char *)ptr - diff, size + diff) + diff;\n#elif HAVE_ALIGNED_MALLOC\n return _aligned_realloc(ptr, size, 32);\n#else\n return realloc(ptr, size);\n#endif\n}']
35,521
0
https://github.com/openssl/openssl/blob/bb56561adbb6d2728b05e2df08c0575c38a46249/crypto/ec/ec_ameth.c/#L98
static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key) { const EC_GROUP *group; int nid; if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) { ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS); return 0; } if (EC_GROUP_get_asn1_flag(group) && (nid = EC_GROUP_get_curve_name(group))) { *ppval = OBJ_nid2obj(nid); *pptype = V_ASN1_OBJECT; } else { ASN1_STRING *pstr = NULL; pstr = ASN1_STRING_new(); if (pstr == NULL) return 0; pstr->length = i2d_ECParameters(ec_key, &pstr->data); if (pstr->length <= 0) { ASN1_STRING_free(pstr); ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB); return 0; } *ppval = pstr; *pptype = V_ASN1_SEQUENCE; } return 1; }
['static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key)\n{\n const EC_GROUP *group;\n int nid;\n if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {\n ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);\n return 0;\n }\n if (EC_GROUP_get_asn1_flag(group)\n && (nid = EC_GROUP_get_curve_name(group)))\n {\n *ppval = OBJ_nid2obj(nid);\n *pptype = V_ASN1_OBJECT;\n } else {\n ASN1_STRING *pstr = NULL;\n pstr = ASN1_STRING_new();\n if (pstr == NULL)\n return 0;\n pstr->length = i2d_ECParameters(ec_key, &pstr->data);\n if (pstr->length <= 0) {\n ASN1_STRING_free(pstr);\n ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);\n return 0;\n }\n *ppval = pstr;\n *pptype = V_ASN1_SEQUENCE;\n }\n return 1;\n}', 'const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)\n{\n return key->group;\n}', 'int EC_GROUP_get_asn1_flag(const EC_GROUP *group)\n{\n return group->asn1_flag;\n}', 'ASN1_STRING *ASN1_STRING_new(void)\n{\n return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));\n}', 'ASN1_STRING *ASN1_STRING_type_new(int type)\n{\n ASN1_STRING *ret;\n ret = OPENSSL_zalloc(sizeof(*ret));\n if (ret == NULL) {\n ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->type = type;\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 i2d_ECParameters(EC_KEY *a, unsigned char **out)\n{\n if (a == NULL) {\n ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);\n return 0;\n }\n return i2d_ECPKParameters(a->group, out);\n}', 'int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)\n{\n int ret = 0;\n ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);\n if (tmp == NULL) {\n ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);\n return 0;\n }\n if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {\n ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);\n ECPKPARAMETERS_free(tmp);\n return 0;\n }\n ECPKPARAMETERS_free(tmp);\n return (ret);\n}', 'IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)', 'int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)\n{\n return asn1_item_flags_i2d(val, out, it, 0);\n}', 'static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,\n const ASN1_ITEM *it, int flags)\n{\n if (out && !*out) {\n unsigned char *p, *buf;\n int len;\n len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);\n if (len <= 0)\n return len;\n buf = OPENSSL_malloc(len);\n if (buf == NULL)\n return -1;\n p = buf;\n ASN1_item_ex_i2d(&val, &p, it, -1, flags);\n *out = buf;\n return len;\n }\n return ASN1_item_ex_i2d(&val, out, it, -1, flags);\n}', 'void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)\n{\n asn1_item_embed_free(&val, it, 0);\n}', 'void ASN1_STRING_free(ASN1_STRING *a)\n{\n if (a == NULL)\n return;\n if (!(a->flags & ASN1_STRING_FLAG_NDEF))\n OPENSSL_free(a->data);\n if (!(a->flags & ASN1_STRING_FLAG_EMBED))\n OPENSSL_free(a);\n}']
35,522
0
https://github.com/openssl/openssl/blob/a68d8c7b77a3d46d591b89cfd0ecd2a2242e4613/crypto/bn/bn_print.c/#L106
char *BN_bn2dec(const BIGNUM *a) { int i = 0, num, ok = 0; char *buf = NULL; char *p; BIGNUM *t = NULL; BN_ULONG *bn_data = NULL, *lp; int bn_data_num; i = BN_num_bits(a) * 3; num = (i / 10 + i / 1000 + 1) + 1; bn_data_num = num / BN_DEC_NUM + 1; bn_data = OPENSSL_malloc(bn_data_num * sizeof(BN_ULONG)); buf = OPENSSL_malloc(num + 3); if ((buf == NULL) || (bn_data == NULL)) { BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE); goto err; } if ((t = BN_dup(a)) == NULL) goto err; #define BUF_REMAIN (num+3 - (size_t)(p - buf)) p = buf; lp = bn_data; if (BN_is_zero(t)) { *(p++) = '0'; *(p++) = '\0'; } else { if (BN_is_negative(t)) *p++ = '-'; while (!BN_is_zero(t)) { if (lp - bn_data >= bn_data_num) goto err; *lp = BN_div_word(t, BN_DEC_CONV); if (*lp == (BN_ULONG)-1) goto err; lp++; } lp--; BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT1, *lp); while (*p) p++; while (lp != bn_data) { lp--; BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT2, *lp); while (*p) p++; } } ok = 1; err: OPENSSL_free(bn_data); BN_free(t); if (ok) return buf; OPENSSL_free(buf); return NULL; }
["char *BN_bn2dec(const BIGNUM *a)\n{\n int i = 0, num, ok = 0;\n char *buf = NULL;\n char *p;\n BIGNUM *t = NULL;\n BN_ULONG *bn_data = NULL, *lp;\n int bn_data_num;\n i = BN_num_bits(a) * 3;\n num = (i / 10 + i / 1000 + 1) + 1;\n bn_data_num = num / BN_DEC_NUM + 1;\n bn_data = OPENSSL_malloc(bn_data_num * sizeof(BN_ULONG));\n buf = OPENSSL_malloc(num + 3);\n if ((buf == NULL) || (bn_data == NULL)) {\n BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if ((t = BN_dup(a)) == NULL)\n goto err;\n#define BUF_REMAIN (num+3 - (size_t)(p - buf))\n p = buf;\n lp = bn_data;\n if (BN_is_zero(t)) {\n *(p++) = '0';\n *(p++) = '\\0';\n } else {\n if (BN_is_negative(t))\n *p++ = '-';\n while (!BN_is_zero(t)) {\n if (lp - bn_data >= bn_data_num)\n goto err;\n *lp = BN_div_word(t, BN_DEC_CONV);\n if (*lp == (BN_ULONG)-1)\n goto err;\n lp++;\n }\n lp--;\n BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT1, *lp);\n while (*p)\n p++;\n while (lp != bn_data) {\n lp--;\n BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT2, *lp);\n while (*p)\n p++;\n }\n }\n ok = 1;\n err:\n OPENSSL_free(bn_data);\n BN_free(t);\n if (ok)\n return buf;\n OPENSSL_free(buf);\n return NULL;\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}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
35,523
0
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/bn/bn_lib.c/#L271
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,\n BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *a;\n const BIGNUM *ca;\n BN_CTX_start(ctx);\n if ((a = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (y != NULL) {\n if (x == y) {\n if (!BN_sqr(a, x, ctx))\n goto err;\n } else {\n if (!BN_mul(a, x, y, ctx))\n goto err;\n }\n ca = a;\n } else\n ca = x;\n ret = BN_div_recp(NULL, r, ca, recp, ctx);\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return (ret);\n}', '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_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return (1);\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# if 0\n if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)b;\n if (bn_wexpand(tmp_bn, al) == NULL)\n goto err;\n tmp_bn->d[bl] = 0;\n bl++;\n i--;\n } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)a;\n if (bn_wexpand(tmp_bn, bl) == NULL)\n goto err;\n tmp_bn->d[al] = 0;\n al++;\n i++;\n }\n if (i == 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (al == j) {\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, al, t->d);\n } else {\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, al - j, j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# endif\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return (ret);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
35,524
0
https://github.com/libav/libav/blob/1e4dd198aff2f1071b88aba6ae873745e9c18a81/libavcodec/motion_est.c/#L217
static av_always_inline int cmp_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby, const int size, const int h, int ref_index, int src_index, me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel, int chroma){ MotionEstContext * const c= &s->me; const int stride= c->stride; const int uvstride= c->uvstride; const int dxy= subx + (suby<<(1+qpel)); const int hx= subx + (x<<(1+qpel)); const int hy= suby + (y<<(1+qpel)); uint8_t * const * const ref= c->ref[ref_index]; uint8_t * const * const src= c->src[src_index]; int d; int uvdxy; if(dxy){ if(qpel){ c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride); if(chroma){ int cx= hx/2; int cy= hy/2; cx= (cx>>1)|(cx&1); cy= (cy>>1)|(cy&1); uvdxy= (cx&1) + 2*(cy&1); } }else{ c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h); if(chroma) uvdxy= dxy | (x&1) | (2*(y&1)); } d = cmp_func(s, c->temp, src[0], stride, h); }else{ d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h); if(chroma) uvdxy= (x&1) + 2*(y&1); } if(chroma){ uint8_t * const uvtemp= c->temp + 16*stride; c->hpel_put[size+1][uvdxy](uvtemp , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1); c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1); d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h>>1); d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1); } return d; }
['void ff_estimate_b_frame_motion(MpegEncContext * s,\n int mb_x, int mb_y)\n{\n MotionEstContext * const c= &s->me;\n const int penalty_factor= c->mb_penalty_factor;\n int fmin, bmin, dmin, fbmin, bimin, fimin;\n int type=0;\n const int xy = mb_y*s->mb_stride + mb_x;\n init_ref(c, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2);\n get_limits(s, 16*mb_x, 16*mb_y);\n c->skip=0;\n if(s->codec_id == CODEC_ID_MPEG4 && s->next_picture.mbskip_table[xy]){\n int score= direct_search(s, mb_x, mb_y);\n score= ((unsigned)(score*score + 128*256))>>16;\n c->mc_mb_var_sum_temp += score;\n s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score;\n s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;\n return;\n }\n if(c->avctx->me_threshold){\n int vard= check_input_motion(s, mb_x, mb_y, 0);\n if((vard+128)>>8 < c->avctx->me_threshold){\n s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;\n c->mc_mb_var_sum_temp += (vard+128)>>8;\n return;\n }\n if((vard+128)>>8 < c->avctx->mb_threshold){\n type= s->mb_type[mb_y*s->mb_stride + mb_x];\n if(type == CANDIDATE_MB_TYPE_DIRECT){\n direct_search(s, mb_x, mb_y);\n }\n if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){\n c->skip=0;\n ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);\n }\n if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){\n c->skip=0;\n ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);\n }\n if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){\n c->skip=0;\n c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;\n interlaced_search(s, 0,\n s->b_field_mv_table[0], s->b_field_select_table[0],\n s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);\n }\n if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){\n c->skip=0;\n c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;\n interlaced_search(s, 2,\n s->b_field_mv_table[1], s->b_field_select_table[1],\n s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);\n }\n return;\n }\n }\n if (s->codec_id == CODEC_ID_MPEG4)\n dmin= direct_search(s, mb_x, mb_y);\n else\n dmin= INT_MAX;\n c->skip=0;\n fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;\n c->skip=0;\n bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;\n c->skip=0;\n fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;\n if(s->flags & CODEC_FLAG_INTERLACED_ME){\n c->skip=0;\n c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;\n fimin= interlaced_search(s, 0,\n s->b_field_mv_table[0], s->b_field_select_table[0],\n s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);\n c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;\n bimin= interlaced_search(s, 2,\n s->b_field_mv_table[1], s->b_field_select_table[1],\n s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);\n }else\n fimin= bimin= INT_MAX;\n {\n int score= fmin;\n type = CANDIDATE_MB_TYPE_FORWARD;\n if (dmin <= score){\n score = dmin;\n type = CANDIDATE_MB_TYPE_DIRECT;\n }\n if(bmin<score){\n score=bmin;\n type= CANDIDATE_MB_TYPE_BACKWARD;\n }\n if(fbmin<score){\n score=fbmin;\n type= CANDIDATE_MB_TYPE_BIDIR;\n }\n if(fimin<score){\n score=fimin;\n type= CANDIDATE_MB_TYPE_FORWARD_I;\n }\n if(bimin<score){\n score=bimin;\n type= CANDIDATE_MB_TYPE_BACKWARD_I;\n }\n score= ((unsigned)(score*score + 128*256))>>16;\n c->mc_mb_var_sum_temp += score;\n s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score;\n }\n if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){\n type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;\n if(fimin < INT_MAX)\n type |= CANDIDATE_MB_TYPE_FORWARD_I;\n if(bimin < INT_MAX)\n type |= CANDIDATE_MB_TYPE_BACKWARD_I;\n if(fimin < INT_MAX && bimin < INT_MAX){\n type |= CANDIDATE_MB_TYPE_BIDIR_I;\n }\n if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT;\n if(s->codec_id == CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT && s->flags&CODEC_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])\n type |= CANDIDATE_MB_TYPE_DIRECT0;\n#if 0\n if(s->out_format == FMT_MPEG1)\n type |= CANDIDATE_MB_TYPE_INTRA;\n#endif\n }\n s->mb_type[mb_y*s->mb_stride + mb_x]= type;\n}', 'static int interlaced_search(MpegEncContext *s, int ref_index,\n int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)\n{\n MotionEstContext * const c= &s->me;\n const int size=0;\n const int h=8;\n int block;\n int P[10][2];\n uint8_t * const mv_penalty= c->current_mv_penalty;\n int same=1;\n const int stride= 2*s->linesize;\n int dmin_sum= 0;\n const int mot_stride= s->mb_stride;\n const int xy= s->mb_x + s->mb_y*mot_stride;\n c->ymin>>=1;\n c->ymax>>=1;\n c->stride<<=1;\n c->uvstride<<=1;\n init_interlaced_ref(s, ref_index);\n for(block=0; block<2; block++){\n int field_select;\n int best_dmin= INT_MAX;\n int best_field= -1;\n for(field_select=0; field_select<2; field_select++){\n int dmin, mx_i, my_i;\n int16_t (*mv_table)[2]= mv_tables[block][field_select];\n if(user_field_select){\n assert(field_select==0 || field_select==1);\n assert(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);\n if(field_select_tables[block][xy] != field_select)\n continue;\n }\n P_LEFT[0] = mv_table[xy - 1][0];\n P_LEFT[1] = mv_table[xy - 1][1];\n if(P_LEFT[0] > (c->xmax<<1)) P_LEFT[0] = (c->xmax<<1);\n c->pred_x= P_LEFT[0];\n c->pred_y= P_LEFT[1];\n if(!s->first_slice_line){\n P_TOP[0] = mv_table[xy - mot_stride][0];\n P_TOP[1] = mv_table[xy - mot_stride][1];\n P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];\n P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];\n if(P_TOP[1] > (c->ymax<<1)) P_TOP[1] = (c->ymax<<1);\n if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1);\n if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);\n if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);\n P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);\n P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);\n }\n P_MV1[0]= mx;\n P_MV1[1]= my / 2;\n dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);\n dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);\n mv_table[xy][0]= mx_i;\n mv_table[xy][1]= my_i;\n if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){\n int dxy;\n uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;\n dxy = ((my_i & 1) << 1) | (mx_i & 1);\n if(s->no_rounding){\n s->dsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref , stride, h);\n }else{\n s->dsp.put_pixels_tab [size][dxy](c->scratchpad, ref , stride, h);\n }\n dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);\n dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;\n }else\n dmin+= c->mb_penalty_factor;\n dmin += field_select != block;\n if(dmin < best_dmin){\n best_dmin= dmin;\n best_field= field_select;\n }\n }\n {\n int16_t (*mv_table)[2]= mv_tables[block][best_field];\n if(mv_table[xy][0] != mx) same=0;\n if(mv_table[xy][1]&1) same=0;\n if(mv_table[xy][1]*2 != my) same=0;\n if(best_field != block) same=0;\n }\n field_select_tables[block][xy]= best_field;\n dmin_sum += best_dmin;\n }\n c->ymin<<=1;\n c->ymax<<=1;\n c->stride>>=1;\n c->uvstride>>=1;\n if(same)\n return INT_MAX;\n switch(c->avctx->mb_cmp&0xFF){\n case FF_CMP_RD:\n return dmin_sum;\n default:\n return dmin_sum+ 11*c->mb_penalty_factor;\n }\n}', 'static int epzs_motion_search2(MpegEncContext * s,\n int *mx_ptr, int *my_ptr, int P[10][2],\n int src_index, int ref_index, int16_t (*last_mv)[2],\n int ref_mv_scale)\n{\n MotionEstContext * const c= &s->me;\n int best[2]={0, 0};\n int d, dmin;\n int map_generation;\n const int penalty_factor= c->penalty_factor;\n const int size=0;\n const int h=8;\n const int ref_mv_stride= s->mb_stride;\n const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;\n me_cmp_func cmpf, chroma_cmpf;\n LOAD_COMMON\n int flags= c->flags;\n LOAD_COMMON2\n cmpf= s->dsp.me_cmp[size];\n chroma_cmpf= s->dsp.me_cmp[size+1];\n map_generation= update_map_generation(c);\n dmin = 1000000;\n if (s->first_slice_line) {\n CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)\n CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)\n }else{\n CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)\n CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)\n CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)\n CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)\n CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)\n }\n if(dmin>64*4){\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)\n if(s->mb_y+1<s->end_mb_y)\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)\n }\n dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n *mx_ptr= best[0];\n *my_ptr= best[1];\n return dmin;\n}', 'static av_always_inline int diamond_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 MotionEstContext * const c= &s->me;\n if(c->dia_size==-1)\n return funny_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size<-1)\n return sab_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size<2)\n return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size>1024)\n return full_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size>768)\n return umh_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size>512)\n return hex_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags, c->dia_size&0xFF);\n else if(c->dia_size>256)\n return l2s_dia_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else\n return var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n}', '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}', 'static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,\n const int size, const int h, int ref_index, int src_index,\n me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){\n if(av_builtin_constant_p(flags) && av_builtin_constant_p(h) && av_builtin_constant_p(size)\n && av_builtin_constant_p(subx) && av_builtin_constant_p(suby)\n && flags==0 && h==16 && size==0 && subx==0 && suby==0){\n return cmp_simple(s,x,y,ref_index,src_index, cmp_func, chroma_cmp_func);\n }else if(av_builtin_constant_p(subx) && av_builtin_constant_p(suby)\n && subx==0 && suby==0){\n return cmp_fpel_internal(s,x,y,size,h,ref_index,src_index, cmp_func, chroma_cmp_func,flags);\n }else{\n return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags);\n }\n}', 'static int cmp_simple(MpegEncContext *s, const int x, const int y,\n int ref_index, int src_index,\n me_cmp_func cmp_func, me_cmp_func chroma_cmp_func){\n return cmp_inline(s,x,y,0,0,0,16,ref_index,src_index, cmp_func, chroma_cmp_func, 0, 0);\n}', 'static av_always_inline int cmp_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,\n const int size, const int h, int ref_index, int src_index,\n me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel, int chroma){\n MotionEstContext * const c= &s->me;\n const int stride= c->stride;\n const int uvstride= c->uvstride;\n const int dxy= subx + (suby<<(1+qpel));\n const int hx= subx + (x<<(1+qpel));\n const int hy= suby + (y<<(1+qpel));\n uint8_t * const * const ref= c->ref[ref_index];\n uint8_t * const * const src= c->src[src_index];\n int d;\n int uvdxy;\n if(dxy){\n if(qpel){\n c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride);\n if(chroma){\n int cx= hx/2;\n int cy= hy/2;\n cx= (cx>>1)|(cx&1);\n cy= (cy>>1)|(cy&1);\n uvdxy= (cx&1) + 2*(cy&1);\n }\n }else{\n c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);\n if(chroma)\n uvdxy= dxy | (x&1) | (2*(y&1));\n }\n d = cmp_func(s, c->temp, src[0], stride, h);\n }else{\n d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);\n if(chroma)\n uvdxy= (x&1) + 2*(y&1);\n }\n if(chroma){\n uint8_t * const uvtemp= c->temp + 16*stride;\n c->hpel_put[size+1][uvdxy](uvtemp , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);\n c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);\n d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h>>1);\n d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);\n }\n return d;\n}']
35,525
0
https://github.com/libav/libav/blob/1db9da523815beb8e9fdcbc63205b3473616c6f0/libavcodec/g726.c/#L368
static int g726_decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size) { G726Context *c = avctx->priv_data; short *samples = data; GetBitContext gb; init_get_bits(&gb, buf, buf_size * 8); while (get_bits_count(&gb) + c->code_size <= buf_size*8) *samples++ = g726_decode(c, get_bits(&gb, c->code_size)); if(buf_size*8 != get_bits_count(&gb)) av_log(avctx, AV_LOG_ERROR, "Frame invalidly split, missing parser?\n"); *data_size = (uint8_t*)samples - (uint8_t*)data; return buf_size; }
['static int g726_decode_frame(AVCodecContext *avctx,\n void *data, int *data_size,\n const uint8_t *buf, int buf_size)\n{\n G726Context *c = avctx->priv_data;\n short *samples = data;\n GetBitContext gb;\n init_get_bits(&gb, buf, buf_size * 8);\n while (get_bits_count(&gb) + c->code_size <= buf_size*8)\n *samples++ = g726_decode(c, get_bits(&gb, c->code_size));\n if(buf_size*8 != get_bits_count(&gb))\n av_log(avctx, AV_LOG_ERROR, "Frame invalidly split, missing parser?\\n");\n *data_size = (uint8_t*)samples - (uint8_t*)data;\n return buf_size;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size= (bit_size+7)>>3;\n if(buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer= buffer;\n s->size_in_bits= bit_size;\n 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 inline int get_bits_count(GetBitContext *s){\n return s->index;\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}']
35,526
0
https://github.com/openssl/openssl/blob/54d00677f305375eee65a0c9edb5f0980c5f020f/crypto/bn/bn_shift.c/#L242
int bn_rshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n) { int i, top, nw; unsigned int lb, rb; BN_ULONG *t, *f; BN_ULONG l, m, mask; bn_check_top(r); bn_check_top(a); assert(n >= 0); nw = n / BN_BITS2; if (nw >= a->top) { BN_zero(r); return 1; } rb = (unsigned int)n % BN_BITS2; lb = BN_BITS2 - rb; lb %= BN_BITS2; mask = (BN_ULONG)0 - lb; mask |= mask >> 8; top = a->top - nw; if (r != a && bn_wexpand(r, top) == NULL) return 0; t = &(r->d[0]); f = &(a->d[nw]); l = f[0]; for (i = 0; i < top - 1; i++) { m = f[i + 1]; t[i] = (l >> rb) | ((m << lb) & mask); l = m; } t[i] = l >> rb; r->neg = a->neg; r->top = top; r->flags |= BN_FLG_FIXED_TOP; return 1; }
['int bn_probable_prime_dh(BIGNUM *rnd, int bits,\n const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *t1;\n BN_CTX_start(ctx);\n if ((t1 = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))\n goto err;\n if (!BN_mod(t1, rnd, add, ctx))\n goto err;\n if (!BN_sub(rnd, rnd, t1))\n goto err;\n if (rem == NULL) {\n if (!BN_add_word(rnd, 1))\n goto err;\n } else {\n if (!BN_add(rnd, rnd, rem))\n goto err;\n }\n loop:\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);\n if (mod == (BN_ULONG)-1)\n goto err;\n if (mod <= 1) {\n if (!BN_add(rnd, rnd, add))\n goto err;\n goto loop;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(rnd);\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}', '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_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}', 'int bn_rshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, top, nw;\n unsigned int lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l, m, mask;\n bn_check_top(r);\n bn_check_top(a);\n assert(n >= 0);\n nw = n / BN_BITS2;\n if (nw >= a->top) {\n BN_zero(r);\n return 1;\n }\n rb = (unsigned int)n % BN_BITS2;\n lb = BN_BITS2 - rb;\n lb %= BN_BITS2;\n mask = (BN_ULONG)0 - lb;\n mask |= mask >> 8;\n top = a->top - nw;\n if (r != a && bn_wexpand(r, top) == NULL)\n return 0;\n t = &(r->d[0]);\n f = &(a->d[nw]);\n l = f[0];\n for (i = 0; i < top - 1; i++) {\n m = f[i + 1];\n t[i] = (l >> rb) | ((m << lb) & mask);\n l = m;\n }\n t[i] = l >> rb;\n r->neg = a->neg;\n r->top = top;\n r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n}']
35,527
0
https://github.com/libav/libav/blob/1e76a1da0534f3a7cdaf7811059beaff874e0504/libavcodec/vp3.c/#L738
static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb) { int j, k, sb_x, sb_y; int coding_mode; int motion_x[4]; int motion_y[4]; int last_motion_x = 0; int last_motion_y = 0; int prior_last_motion_x = 0; int prior_last_motion_y = 0; int current_macroblock; int current_fragment; Vp3Fragment *frag; if (s->keyframe) return 0; coding_mode = get_bits1(gb); for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) { for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) { for (j = 0; j < 4; j++) { int mb_x = 2*sb_x + (j>>1); int mb_y = 2*sb_y + (((j>>1)+j)&1); current_macroblock = mb_y * s->macroblock_width + mb_x; if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height || (s->macroblock_coding[current_macroblock] == MODE_COPY)) continue; switch (s->macroblock_coding[current_macroblock]) { case MODE_INTER_PLUS_MV: case MODE_GOLDEN_MV: if (coding_mode == 0) { motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; } else { motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)]; motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)]; } if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) { prior_last_motion_x = last_motion_x; prior_last_motion_y = last_motion_y; last_motion_x = motion_x[0]; last_motion_y = motion_y[0]; } break; case MODE_INTER_FOURMV: prior_last_motion_x = last_motion_x; prior_last_motion_y = last_motion_y; for (k = 0; k < 4; k++) { current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X; if (s->all_fragments[current_fragment].coding_method != MODE_COPY) { if (coding_mode == 0) { motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; } else { motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)]; motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)]; } last_motion_x = motion_x[k]; last_motion_y = motion_y[k]; } else { motion_x[k] = 0; motion_y[k] = 0; } } break; case MODE_INTER_LAST_MV: motion_x[0] = last_motion_x; motion_y[0] = last_motion_y; break; case MODE_INTER_PRIOR_LAST: motion_x[0] = prior_last_motion_x; motion_y[0] = prior_last_motion_y; prior_last_motion_x = last_motion_x; prior_last_motion_y = last_motion_y; last_motion_x = motion_x[0]; last_motion_y = motion_y[0]; break; default: motion_x[0] = 0; motion_y[0] = 0; break; } for (k = 0; k < 4; k++) { current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X; if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { s->all_fragments[current_fragment].motion_x = motion_x[k]; s->all_fragments[current_fragment].motion_y = motion_y[k]; } else { s->all_fragments[current_fragment].motion_x = motion_x[0]; s->all_fragments[current_fragment].motion_y = motion_y[0]; } } #define SET_CHROMA_MV(mx, my) \ frag[s->fragment_start[1]].motion_x = mx; \ frag[s->fragment_start[1]].motion_y = my; \ frag[s->fragment_start[2]].motion_x = mx; \ frag[s->fragment_start[2]].motion_y = my if (s->chroma_y_shift) { if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2); motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2); } motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1); motion_y[0] = (motion_y[0]>>1) | (motion_y[0]&1); frag = s->all_fragments + mb_y*s->fragment_width[1] + mb_x; SET_CHROMA_MV(motion_x[0], motion_y[0]); } else if (s->chroma_x_shift) { if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1); motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1); motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1); motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1); } else { motion_x[1] = motion_x[0]; motion_y[1] = motion_y[0]; } motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1); motion_x[1] = (motion_x[1]>>1) | (motion_x[1]&1); frag = s->all_fragments + 2*mb_y*s->fragment_width[1] + mb_x; for (k = 0; k < 2; k++) { SET_CHROMA_MV(motion_x[k], motion_y[k]); frag += s->fragment_width[1]; } } else { for (k = 0; k < 4; k++) { frag = s->all_fragments + BLOCK_Y*s->fragment_width[1] + BLOCK_X; if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { SET_CHROMA_MV(motion_x[k], motion_y[k]); } else { SET_CHROMA_MV(motion_x[0], motion_y[0]); } } } } } } return 0; }
['static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)\n{\n int j, k, sb_x, sb_y;\n int coding_mode;\n int motion_x[4];\n int motion_y[4];\n int last_motion_x = 0;\n int last_motion_y = 0;\n int prior_last_motion_x = 0;\n int prior_last_motion_y = 0;\n int current_macroblock;\n int current_fragment;\n Vp3Fragment *frag;\n if (s->keyframe)\n return 0;\n coding_mode = get_bits1(gb);\n for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {\n for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {\n for (j = 0; j < 4; j++) {\n int mb_x = 2*sb_x + (j>>1);\n int mb_y = 2*sb_y + (((j>>1)+j)&1);\n current_macroblock = mb_y * s->macroblock_width + mb_x;\n if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height ||\n (s->macroblock_coding[current_macroblock] == MODE_COPY))\n continue;\n switch (s->macroblock_coding[current_macroblock]) {\n case MODE_INTER_PLUS_MV:\n case MODE_GOLDEN_MV:\n if (coding_mode == 0) {\n motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];\n motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];\n } else {\n motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];\n motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];\n }\n if (s->macroblock_coding[current_macroblock] ==\n MODE_INTER_PLUS_MV) {\n prior_last_motion_x = last_motion_x;\n prior_last_motion_y = last_motion_y;\n last_motion_x = motion_x[0];\n last_motion_y = motion_y[0];\n }\n break;\n case MODE_INTER_FOURMV:\n prior_last_motion_x = last_motion_x;\n prior_last_motion_y = last_motion_y;\n for (k = 0; k < 4; k++) {\n current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X;\n if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {\n if (coding_mode == 0) {\n motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];\n motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];\n } else {\n motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];\n motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];\n }\n last_motion_x = motion_x[k];\n last_motion_y = motion_y[k];\n } else {\n motion_x[k] = 0;\n motion_y[k] = 0;\n }\n }\n break;\n case MODE_INTER_LAST_MV:\n motion_x[0] = last_motion_x;\n motion_y[0] = last_motion_y;\n break;\n case MODE_INTER_PRIOR_LAST:\n motion_x[0] = prior_last_motion_x;\n motion_y[0] = prior_last_motion_y;\n prior_last_motion_x = last_motion_x;\n prior_last_motion_y = last_motion_y;\n last_motion_x = motion_x[0];\n last_motion_y = motion_y[0];\n break;\n default:\n motion_x[0] = 0;\n motion_y[0] = 0;\n break;\n }\n for (k = 0; k < 4; k++) {\n current_fragment =\n BLOCK_Y*s->fragment_width[0] + BLOCK_X;\n if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {\n s->all_fragments[current_fragment].motion_x = motion_x[k];\n s->all_fragments[current_fragment].motion_y = motion_y[k];\n } else {\n s->all_fragments[current_fragment].motion_x = motion_x[0];\n s->all_fragments[current_fragment].motion_y = motion_y[0];\n }\n }\n#define SET_CHROMA_MV(mx, my) \\\n frag[s->fragment_start[1]].motion_x = mx; \\\n frag[s->fragment_start[1]].motion_y = my; \\\n frag[s->fragment_start[2]].motion_x = mx; \\\n frag[s->fragment_start[2]].motion_y = my\n if (s->chroma_y_shift) {\n if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {\n motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2);\n motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2);\n }\n motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);\n motion_y[0] = (motion_y[0]>>1) | (motion_y[0]&1);\n frag = s->all_fragments + mb_y*s->fragment_width[1] + mb_x;\n SET_CHROMA_MV(motion_x[0], motion_y[0]);\n } else if (s->chroma_x_shift) {\n if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {\n motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);\n motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);\n motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);\n motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);\n } else {\n motion_x[1] = motion_x[0];\n motion_y[1] = motion_y[0];\n }\n motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);\n motion_x[1] = (motion_x[1]>>1) | (motion_x[1]&1);\n frag = s->all_fragments + 2*mb_y*s->fragment_width[1] + mb_x;\n for (k = 0; k < 2; k++) {\n SET_CHROMA_MV(motion_x[k], motion_y[k]);\n frag += s->fragment_width[1];\n }\n } else {\n for (k = 0; k < 4; k++) {\n frag = s->all_fragments + BLOCK_Y*s->fragment_width[1] + BLOCK_X;\n if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {\n SET_CHROMA_MV(motion_x[k], motion_y[k]);\n } else {\n SET_CHROMA_MV(motion_x[0], motion_y[0]);\n }\n }\n }\n }\n }\n }\n return 0;\n}']
35,528
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/apps/s_client.c/#L722
static void print_stuff(BIO *bio, SSL *s, int full) { X509 *peer=NULL; char *p; static char *space=" "; char buf[BUFSIZ]; STACK_OF(X509) *sk; STACK_OF(X509_NAME) *sk2; SSL_CIPHER *c; X509_NAME *xn; int j,i; if (full) { sk=SSL_get_peer_cert_chain(s); if (sk != NULL) { BIO_printf(bio,"---\nCertificate chain\n"); for (i=0; i<sk_X509_num(sk); i++) { X509_NAME_oneline(X509_get_subject_name( sk_X509_value(sk,i)),buf,BUFSIZ); BIO_printf(bio,"%2d s:%s\n",i,buf); X509_NAME_oneline(X509_get_issuer_name( sk_X509_value(sk,i)),buf,BUFSIZ); BIO_printf(bio," i:%s\n",buf); if (c_showcerts) PEM_write_bio_X509(bio,sk_X509_value(sk,i)); } } BIO_printf(bio,"---\n"); peer=SSL_get_peer_certificate(s); if (peer != NULL) { BIO_printf(bio,"Server certificate\n"); if (!c_showcerts) PEM_write_bio_X509(bio,peer); X509_NAME_oneline(X509_get_subject_name(peer), buf,BUFSIZ); BIO_printf(bio,"subject=%s\n",buf); X509_NAME_oneline(X509_get_issuer_name(peer), buf,BUFSIZ); BIO_printf(bio,"issuer=%s\n",buf); } else BIO_printf(bio,"no peer certificate available\n"); sk2=SSL_get_client_CA_list(s); if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0)) { BIO_printf(bio,"---\nAcceptable client certificate CA names\n"); for (i=0; i<sk_X509_NAME_num(sk2); i++) { xn=sk_X509_NAME_value(sk2,i); X509_NAME_oneline(xn,buf,sizeof(buf)); BIO_write(bio,buf,strlen(buf)); BIO_write(bio,"\n",1); } } else { BIO_printf(bio,"---\nNo client certificate CA names sent\n"); } p=SSL_get_shared_ciphers(s,buf,BUFSIZ); if (p != NULL) { BIO_printf(bio,"---\nCiphers common between both SSL endpoints:\n"); j=i=0; while (*p) { if (*p == ':') { BIO_write(bio,space,15-j%25); i++; j=0; BIO_write(bio,((i%3)?" ":"\n"),1); } else { BIO_write(bio,p,1); j++; } p++; } BIO_write(bio,"\n",1); } BIO_printf(bio,"---\nSSL handshake has read %ld bytes and written %ld bytes\n", BIO_number_read(SSL_get_rbio(s)), BIO_number_written(SSL_get_wbio(s))); } BIO_printf(bio,((s->hit)?"---\nReused, ":"---\nNew, ")); c=SSL_get_current_cipher(s); BIO_printf(bio,"%s, Cipher is %s\n", SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c)); if (peer != NULL) { EVP_PKEY *pktmp; pktmp = X509_get_pubkey(peer); BIO_printf(bio,"Server public key is %d bit\n", EVP_PKEY_bits(pktmp)); EVP_PKEY_free(pktmp); } SSL_SESSION_print(bio,SSL_get_session(s)); BIO_printf(bio,"---\n"); if (peer != NULL) X509_free(peer); }
['static void print_stuff(BIO *bio, SSL *s, int full)\n\t{\n\tX509 *peer=NULL;\n\tchar *p;\n\tstatic char *space=" ";\n\tchar buf[BUFSIZ];\n\tSTACK_OF(X509) *sk;\n\tSTACK_OF(X509_NAME) *sk2;\n\tSSL_CIPHER *c;\n\tX509_NAME *xn;\n\tint j,i;\n\tif (full)\n\t\t{\n\t\tsk=SSL_get_peer_cert_chain(s);\n\t\tif (sk != NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio,"---\\nCertificate chain\\n");\n\t\t\tfor (i=0; i<sk_X509_num(sk); i++)\n\t\t\t\t{\n\t\t\t\tX509_NAME_oneline(X509_get_subject_name(\n\t\t\t\t\tsk_X509_value(sk,i)),buf,BUFSIZ);\n\t\t\t\tBIO_printf(bio,"%2d s:%s\\n",i,buf);\n\t\t\t\tX509_NAME_oneline(X509_get_issuer_name(\n\t\t\t\t\tsk_X509_value(sk,i)),buf,BUFSIZ);\n\t\t\t\tBIO_printf(bio," i:%s\\n",buf);\n\t\t\t\tif (c_showcerts)\n\t\t\t\t\tPEM_write_bio_X509(bio,sk_X509_value(sk,i));\n\t\t\t\t}\n\t\t\t}\n\t\tBIO_printf(bio,"---\\n");\n\t\tpeer=SSL_get_peer_certificate(s);\n\t\tif (peer != NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio,"Server certificate\\n");\n\t\t\tif (!c_showcerts)\n\t\t\t\tPEM_write_bio_X509(bio,peer);\n\t\t\tX509_NAME_oneline(X509_get_subject_name(peer),\n\t\t\t\tbuf,BUFSIZ);\n\t\t\tBIO_printf(bio,"subject=%s\\n",buf);\n\t\t\tX509_NAME_oneline(X509_get_issuer_name(peer),\n\t\t\t\tbuf,BUFSIZ);\n\t\t\tBIO_printf(bio,"issuer=%s\\n",buf);\n\t\t\t}\n\t\telse\n\t\t\tBIO_printf(bio,"no peer certificate available\\n");\n\t\tsk2=SSL_get_client_CA_list(s);\n\t\tif ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0))\n\t\t\t{\n\t\t\tBIO_printf(bio,"---\\nAcceptable client certificate CA names\\n");\n\t\t\tfor (i=0; i<sk_X509_NAME_num(sk2); i++)\n\t\t\t\t{\n\t\t\t\txn=sk_X509_NAME_value(sk2,i);\n\t\t\t\tX509_NAME_oneline(xn,buf,sizeof(buf));\n\t\t\t\tBIO_write(bio,buf,strlen(buf));\n\t\t\t\tBIO_write(bio,"\\n",1);\n\t\t\t\t}\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio,"---\\nNo client certificate CA names sent\\n");\n\t\t\t}\n\t\tp=SSL_get_shared_ciphers(s,buf,BUFSIZ);\n\t\tif (p != NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio,"---\\nCiphers common between both SSL endpoints:\\n");\n\t\t\tj=i=0;\n\t\t\twhile (*p)\n\t\t\t\t{\n\t\t\t\tif (*p == \':\')\n\t\t\t\t\t{\n\t\t\t\t\tBIO_write(bio,space,15-j%25);\n\t\t\t\t\ti++;\n\t\t\t\t\tj=0;\n\t\t\t\t\tBIO_write(bio,((i%3)?" ":"\\n"),1);\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tBIO_write(bio,p,1);\n\t\t\t\t\tj++;\n\t\t\t\t\t}\n\t\t\t\tp++;\n\t\t\t\t}\n\t\t\tBIO_write(bio,"\\n",1);\n\t\t\t}\n\t\tBIO_printf(bio,"---\\nSSL handshake has read %ld bytes and written %ld bytes\\n",\n\t\t\tBIO_number_read(SSL_get_rbio(s)),\n\t\t\tBIO_number_written(SSL_get_wbio(s)));\n\t\t}\n\tBIO_printf(bio,((s->hit)?"---\\nReused, ":"---\\nNew, "));\n\tc=SSL_get_current_cipher(s);\n\tBIO_printf(bio,"%s, Cipher is %s\\n",\n\t\tSSL_CIPHER_get_version(c),\n\t\tSSL_CIPHER_get_name(c));\n\tif (peer != NULL) {\n\t\tEVP_PKEY *pktmp;\n\t\tpktmp = X509_get_pubkey(peer);\n\t\tBIO_printf(bio,"Server public key is %d bit\\n",\n\t\t\t\t\t\t\t EVP_PKEY_bits(pktmp));\n\t\tEVP_PKEY_free(pktmp);\n\t}\n\tSSL_SESSION_print(bio,SSL_get_session(s));\n\tBIO_printf(bio,"---\\n");\n\tif (peer != NULL)\n\t\tX509_free(peer);\n\t}']
35,529
0
https://github.com/openssl/openssl/blob/fb9c3ff565aa11b08646e0f9f28fc082ed365cbd/test/evp_test.c/#L1781
static int encode_test_run(EVP_TEST *t) { ENCODE_DATA *expected = t->data; unsigned char *encode_out = NULL, *decode_out = NULL; int output_len, chunk_len; EVP_ENCODE_CTX *decode_ctx; if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) { t->err = "INTERNAL_ERROR"; goto err; } if (expected->encoding == BASE64_CANONICAL_ENCODING) { EVP_ENCODE_CTX *encode_ctx; if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new()) || !TEST_ptr(encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len)))) goto err; EVP_EncodeInit(encode_ctx); EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len, expected->input, expected->input_len); output_len = chunk_len; EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len); output_len += chunk_len; EVP_ENCODE_CTX_free(encode_ctx); if (!memory_err_compare(t, "BAD_ENCODING", expected->output, expected->output_len, encode_out, output_len)) goto err; } if (!TEST_ptr(decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len)))) goto err; EVP_DecodeInit(decode_ctx); if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output, expected->output_len) < 0) { t->err = "DECODE_ERROR"; goto err; } output_len = chunk_len; if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) { t->err = "DECODE_ERROR"; goto err; } output_len += chunk_len; if (expected->encoding != BASE64_INVALID_ENCODING && !memory_err_compare(t, "BAD_DECODING", expected->input, expected->input_len, decode_out, output_len)) { t->err = "BAD_DECODING"; goto err; } t->err = NULL; err: OPENSSL_free(encode_out); OPENSSL_free(decode_out); EVP_ENCODE_CTX_free(decode_ctx); return 1; }
['static int encode_test_run(EVP_TEST *t)\n{\n ENCODE_DATA *expected = t->data;\n unsigned char *encode_out = NULL, *decode_out = NULL;\n int output_len, chunk_len;\n EVP_ENCODE_CTX *decode_ctx;\n if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {\n t->err = "INTERNAL_ERROR";\n goto err;\n }\n if (expected->encoding == BASE64_CANONICAL_ENCODING) {\n EVP_ENCODE_CTX *encode_ctx;\n if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())\n || !TEST_ptr(encode_out =\n OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))\n goto err;\n EVP_EncodeInit(encode_ctx);\n EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,\n expected->input, expected->input_len);\n output_len = chunk_len;\n EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);\n output_len += chunk_len;\n EVP_ENCODE_CTX_free(encode_ctx);\n if (!memory_err_compare(t, "BAD_ENCODING",\n expected->output, expected->output_len,\n encode_out, output_len))\n goto err;\n }\n if (!TEST_ptr(decode_out =\n OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))\n goto err;\n EVP_DecodeInit(decode_ctx);\n if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,\n expected->output_len) < 0) {\n t->err = "DECODE_ERROR";\n goto err;\n }\n output_len = chunk_len;\n if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {\n t->err = "DECODE_ERROR";\n goto err;\n }\n output_len += chunk_len;\n if (expected->encoding != BASE64_INVALID_ENCODING\n && !memory_err_compare(t, "BAD_DECODING",\n expected->input, expected->input_len,\n decode_out, output_len)) {\n t->err = "BAD_DECODING";\n goto err;\n }\n t->err = NULL;\n err:\n OPENSSL_free(encode_out);\n OPENSSL_free(decode_out);\n EVP_ENCODE_CTX_free(decode_ctx);\n return 1;\n}', 'EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_ENCODE_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int test_ptr(const char *file, int line, const char *s, const void *p)\n{\n if (p != NULL)\n return 1;\n test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);\n return 0;\n}']
35,530
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bn/bn_lib.c/#L377
BIGNUM *bn_expand2(BIGNUM *b, int words) { BN_ULONG *A,*B,*a; int i,j; bn_check_top(b); if (words > b->max) { bn_check_top(b); if (BN_get_flags(b,BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return(NULL); } a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1)); if (A == NULL) { BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE); return(NULL); } memset(A,0x5c,sizeof(BN_ULONG)*(words+1)); #if 1 B=b->d; if (B != NULL) { for (i=b->top&(~7); i>0; i-=8) { A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3]; A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7]; A+=8; B+=8; } switch (b->top&7) { case 7: A[6]=B[6]; case 6: A[5]=B[5]; case 5: A[4]=B[4]; case 4: A[3]=B[3]; case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } Free(b->d); } b->d=a; b->max=words; B= &(b->d[b->top]); j=(b->max - b->top) & ~7; for (i=0; i<j; i+=8) { B[0]=0; B[1]=0; B[2]=0; B[3]=0; B[4]=0; B[5]=0; B[6]=0; B[7]=0; B+=8; } j=(b->max - b->top) & 7; for (i=0; i<j; i++) { B[0]=0; B++; } #else memcpy(a->d,b->d,sizeof(b->d[0])*b->top); #endif } return(b); }
['int PKCS12_key_gen_uni (unsigned char *pass, int passlen, unsigned char *salt,\n\t int saltlen, int id, int iter, int n, unsigned char *out,\n\t const EVP_MD *md_type)\n{\n\tunsigned char *B, *D, *I, *p, *Ai;\n\tint Slen, Plen, Ilen;\n\tint i, j, u, v;\n\tBIGNUM *Ij, *Bpl1;\n\tEVP_MD_CTX ctx;\n#ifdef DEBUG_KEYGEN\n\tunsigned char *tmpout = out;\n\tint tmpn = n;\n\tBIO_printf (bio_err, "KEYGEN DEBUG\\n");\n\tBIO_printf (bio_err, "ID %d, ITER %d\\n", id, iter);\n\tBIO_printf (bio_err, "Password (length %d):\\n", passlen);\n\th__dump (pass, passlen);\n\tBIO_printf (bio_err, "Salt (length %d):\\n", saltlen);\n\th__dump (salt, saltlen);\n\tBIO_printf (bio_err, "ID %d, ITER %d\\n\\n", id, iter);\n#endif\n\tv = EVP_MD_block_size (md_type);\n\tu = EVP_MD_size (md_type);\n\tD = Malloc (v);\n\tAi = Malloc (u);\n\tB = Malloc (v + 1);\n\tSlen = v * ((saltlen+v-1)/v);\n\tPlen = v * ((passlen+v-1)/v);\n\tIlen = Slen + Plen;\n\tI = Malloc (Ilen);\n\tIj = BN_new();\n\tBpl1 = BN_new();\n\tif (!D || !Ai || !B || !I || !Ij || !Bpl1) {\n\t\tPKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI,ERR_R_MALLOC_FAILURE);\n\t\treturn 0;\n\t}\n\tfor (i = 0; i < v; i++) D[i] = id;\n\tp = I;\n\tfor (i = 0; i < Slen; i++) *p++ = salt[i % saltlen];\n\tfor (i = 0; i < Plen; i++) *p++ = pass[i % passlen];\n\tfor (;;) {\n\t\tEVP_DigestInit (&ctx, md_type);\n\t\tEVP_DigestUpdate (&ctx, D, v);\n\t\tEVP_DigestUpdate (&ctx, I, Ilen);\n\t\tEVP_DigestFinal (&ctx, Ai, NULL);\n\t\tfor (j = 1; j < iter; j++) {\n\t\t\tEVP_DigestInit (&ctx, md_type);\n\t\t\tEVP_DigestUpdate (&ctx, Ai, u);\n\t\t\tEVP_DigestFinal (&ctx, Ai, NULL);\n\t\t}\n\t\tmemcpy (out, Ai, min (n, u));\n\t\tif (u >= n) {\n\t\t\tFree (Ai);\n\t\t\tFree (B);\n\t\t\tFree (D);\n\t\t\tFree (I);\n\t\t\tBN_free (Ij);\n\t\t\tBN_free (Bpl1);\n#ifdef DEBUG_KEYGEN\n\t\t\tBIO_printf (bio_err, "Output KEY (length %d)\\n", tmpn);\n\t\t\th__dump (tmpout, tmpn);\n#endif\n\t\t\treturn 1;\n\t\t}\n\t\tn -= u;\n\t\tout += u;\n\t\tfor (j = 0; j < v; j++) B[j] = Ai[j % u];\n\t\tBN_bin2bn (B, v, Bpl1);\n\t\tBN_add_word (Bpl1, 1);\n\t\tfor (j = 0; j < Ilen ; j+=v) {\n\t\t\tBN_bin2bn (I + j, v, Ij);\n\t\t\tBN_add (Ij, Ij, Bpl1);\n\t\t\tBN_bn2bin (Ij, B);\n\t\t\tif (BN_num_bytes (Ij) > v) {\n\t\t\t\tBN_bn2bin (Ij, B);\n\t\t\t\tmemcpy (I + j, B + 1, v);\n\t\t\t} else BN_bn2bin (Ij, I + j);\n\t\t}\n\t}\n\treturn 0;\n}', 'BIGNUM *BN_bin2bn(unsigned char *s, int len, BIGNUM *ret)\n\t{\n\tunsigned int i,m;\n\tunsigned int n;\n\tBN_ULONG l;\n\tif (ret == NULL) ret=BN_new();\n\tif (ret == NULL) return(NULL);\n\tl=0;\n\tn=len;\n\tif (n == 0)\n\t\t{\n\t\tret->top=0;\n\t\treturn(ret);\n\t\t}\n\tif (bn_expand(ret,(int)(n+2)*8) == NULL)\n\t\treturn(NULL);\n\ti=((n-1)/BN_BYTES)+1;\n\tm=((n-1)%(BN_BYTES));\n\tret->top=i;\n\twhile (n-- > 0)\n\t\t{\n\t\tl=(l<<8L)| *(s++);\n\t\tif (m-- == 0)\n\t\t\t{\n\t\t\tret->d[--i]=l;\n\t\t\tl=0;\n\t\t\tm=BN_BYTES-1;\n\t\t\t}\n\t\t}\n\tbn_fix_top(ret);\n\treturn(ret);\n\t}', 'int BN_add_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tBN_ULONG l;\n\tint i;\n\tif (a->neg)\n\t\t{\n\t\ta->neg=0;\n\t\ti=BN_sub_word(a,w);\n\t\tif (!BN_is_zero(a))\n\t\t\ta->neg=1;\n\t\treturn(i);\n\t\t}\n\tw&=BN_MASK2;\n\tif (bn_wexpand(a,a->top+1) == NULL) return(0);\n\ti=0;\n\tfor (;;)\n\t\t{\n\t\tl=(a->d[i]+(BN_ULONG)w)&BN_MASK2;\n\t\ta->d[i]=l;\n\t\tif (w > l)\n\t\t\tw=1;\n\t\telse\n\t\t\tbreak;\n\t\ti++;\n\t\t}\n\tif (i >= a->top)\n\t\ta->top++;\n\treturn(1);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*B,*a;\n\tint i,j;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\nmemset(A,0x5c,sizeof(BN_ULONG)*(words+1));\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tB= &(b->d[b->top]);\n\t\tj=(b->max - b->top) & ~7;\n\t\tfor (i=0; i<j; i+=8)\n\t\t\t{\n\t\t\tB[0]=0; B[1]=0; B[2]=0; B[3]=0;\n\t\t\tB[4]=0; B[5]=0; B[6]=0; B[7]=0;\n\t\t\tB+=8;\n\t\t\t}\n\t\tj=(b->max - b->top) & 7;\n\t\tfor (i=0; i<j; i++)\n\t\t\t{\n\t\t\tB[0]=0;\n\t\t\tB++;\n\t\t\t}\n#else\n\t\t\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\t\t}\n\treturn(b);\n\t}']
35,531
0
https://github.com/libav/libav/blob/fd7b11d027da7cc350d867d22d4c6bbe6022d8df/libavcodec/wmavoice.c/#L1644
static int check_bits_for_superframe(GetBitContext *orig_gb, WMAVoiceContext *s) { GetBitContext s_gb, *gb = &s_gb; int n, need_bits, bd_idx; const struct frame_type_desc *frame_desc; init_get_bits(gb, orig_gb->buffer, orig_gb->size_in_bits); skip_bits_long(gb, get_bits_count(orig_gb)); assert(get_bits_left(gb) == get_bits_left(orig_gb)); if (get_bits_left(gb) < 14) return 1; if (!get_bits1(gb)) return -1; if (get_bits1(gb)) skip_bits(gb, 12); if (s->has_residual_lsps) { if (get_bits_left(gb) < s->sframe_lsp_bitsize) return 1; skip_bits_long(gb, s->sframe_lsp_bitsize); } for (n = 0; n < MAX_FRAMES; n++) { int aw_idx_is_ext = 0; if (!s->has_residual_lsps) { if (get_bits_left(gb) < s->frame_lsp_bitsize) return 1; skip_bits_long(gb, s->frame_lsp_bitsize); } bd_idx = s->vbm_tree[get_vlc2(gb, frame_type_vlc.table, 6, 3)]; if (bd_idx < 0) return -1; frame_desc = &frame_descs[bd_idx]; if (frame_desc->acb_type == ACB_TYPE_ASYMMETRIC) { if (get_bits_left(gb) < s->pitch_nbits) return 1; skip_bits_long(gb, s->pitch_nbits); } if (frame_desc->fcb_type == FCB_TYPE_SILENCE) { skip_bits(gb, 8); } else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) { int tmp = get_bits(gb, 6); if (tmp >= 0x36) { skip_bits(gb, 2); aw_idx_is_ext = 1; } } if (frame_desc->acb_type == ACB_TYPE_HAMMING) { need_bits = s->block_pitch_nbits + (frame_desc->n_blocks - 1) * s->block_delta_pitch_nbits; } else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) { need_bits = 2 * !aw_idx_is_ext; } else need_bits = 0; need_bits += frame_desc->frame_size; if (get_bits_left(gb) < need_bits) return 1; skip_bits_long(gb, need_bits); } return 0; }
['static int check_bits_for_superframe(GetBitContext *orig_gb,\n WMAVoiceContext *s)\n{\n GetBitContext s_gb, *gb = &s_gb;\n int n, need_bits, bd_idx;\n const struct frame_type_desc *frame_desc;\n init_get_bits(gb, orig_gb->buffer, orig_gb->size_in_bits);\n skip_bits_long(gb, get_bits_count(orig_gb));\n assert(get_bits_left(gb) == get_bits_left(orig_gb));\n if (get_bits_left(gb) < 14)\n return 1;\n if (!get_bits1(gb))\n return -1;\n if (get_bits1(gb)) skip_bits(gb, 12);\n if (s->has_residual_lsps) {\n if (get_bits_left(gb) < s->sframe_lsp_bitsize)\n return 1;\n skip_bits_long(gb, s->sframe_lsp_bitsize);\n }\n for (n = 0; n < MAX_FRAMES; n++) {\n int aw_idx_is_ext = 0;\n if (!s->has_residual_lsps) {\n if (get_bits_left(gb) < s->frame_lsp_bitsize) return 1;\n skip_bits_long(gb, s->frame_lsp_bitsize);\n }\n bd_idx = s->vbm_tree[get_vlc2(gb, frame_type_vlc.table, 6, 3)];\n if (bd_idx < 0)\n return -1;\n frame_desc = &frame_descs[bd_idx];\n if (frame_desc->acb_type == ACB_TYPE_ASYMMETRIC) {\n if (get_bits_left(gb) < s->pitch_nbits)\n return 1;\n skip_bits_long(gb, s->pitch_nbits);\n }\n if (frame_desc->fcb_type == FCB_TYPE_SILENCE) {\n skip_bits(gb, 8);\n } else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) {\n int tmp = get_bits(gb, 6);\n if (tmp >= 0x36) {\n skip_bits(gb, 2);\n aw_idx_is_ext = 1;\n }\n }\n if (frame_desc->acb_type == ACB_TYPE_HAMMING) {\n need_bits = s->block_pitch_nbits +\n (frame_desc->n_blocks - 1) * s->block_delta_pitch_nbits;\n } else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) {\n need_bits = 2 * !aw_idx_is_ext;\n } else\n need_bits = 0;\n need_bits += frame_desc->frame_size;\n if (get_bits_left(gb) < need_bits)\n return 1;\n skip_bits_long(gb, need_bits);\n }\n return 0;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size= (bit_size+7)>>3;\n if(buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer= buffer;\n s->size_in_bits= bit_size;\n s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index=0;\n#elif defined 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 inline int get_bits_count(const GetBitContext *s){\n return s->index;\n}', 'static inline void skip_bits_long(GetBitContext *s, int n){\n s->index += n;\n}', 'static inline int get_bits_left(GetBitContext *gb)\n{\n return gb->size_in_bits - get_bits_count(gb);\n}', 'static inline unsigned int get_bits1(GetBitContext *s){\n#ifdef ALT_BITSTREAM_READER\n unsigned int index= s->index;\n uint8_t result= s->buffer[ index>>3 ];\n#ifdef ALT_BITSTREAM_READER_LE\n result>>= (index&0x07);\n result&= 1;\n#else\n result<<= (index&0x07);\n result>>= 8 - 1;\n#endif\n index++;\n s->index= index;\n return result;\n#else\n return get_bits(s, 1);\n#endif\n}']
35,532
0
https://github.com/libav/libav/blob/84ae8936f642042fe7ad3466ce3a26dec77016f1/libavcodec/smacker.c/#L287
static int decode_header_trees(SmackVContext *smk) { GetBitContext gb; int mmap_size, mclr_size, full_size, type_size; mmap_size = AV_RL32(smk->avctx->extradata); mclr_size = AV_RL32(smk->avctx->extradata + 4); full_size = AV_RL32(smk->avctx->extradata + 8); type_size = AV_RL32(smk->avctx->extradata + 12); init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8); if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n"); smk->mmap_tbl = av_malloc(sizeof(int) * 2); smk->mmap_tbl[0] = 0; smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1; } else { smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size); } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n"); smk->mclr_tbl = av_malloc(sizeof(int) * 2); smk->mclr_tbl[0] = 0; smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1; } else { smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size); } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n"); smk->full_tbl = av_malloc(sizeof(int) * 2); smk->full_tbl[0] = 0; smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1; } else { smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size); } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n"); smk->type_tbl = av_malloc(sizeof(int) * 2); smk->type_tbl[0] = 0; smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1; } else { smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size); } return 0; }
['static int decode_header_trees(SmackVContext *smk) {\n GetBitContext gb;\n int mmap_size, mclr_size, full_size, type_size;\n mmap_size = AV_RL32(smk->avctx->extradata);\n mclr_size = AV_RL32(smk->avctx->extradata + 4);\n full_size = AV_RL32(smk->avctx->extradata + 8);\n type_size = AV_RL32(smk->avctx->extradata + 12);\n init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\\n");\n smk->mmap_tbl = av_malloc(sizeof(int) * 2);\n smk->mmap_tbl[0] = 0;\n smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\\n");\n smk->mclr_tbl = av_malloc(sizeof(int) * 2);\n smk->mclr_tbl[0] = 0;\n smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\\n");\n smk->full_tbl = av_malloc(sizeof(int) * 2);\n smk->full_tbl[0] = 0;\n smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\\n");\n smk->type_tbl = av_malloc(sizeof(int) * 2);\n smk->type_tbl[0] = 0;\n smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);\n }\n return 0;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size = (bit_size+7)>>3;\n if (buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n s->buffer_end = buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index = 0;\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer & ~3);\n s->bit_count = 32 + 8*((intptr_t)buffer & 3);\n skip_bits_long(s, 0);\n#endif\n}', 'static inline unsigned int get_bits1(GetBitContext *s){\n#ifdef ALT_BITSTREAM_READER\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef ALT_BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n index++;\n s->index = index;\n return result;\n#else\n return get_bits(s, 1);\n#endif\n}']
35,533
0
https://github.com/libav/libav/blob/5228bcd8705523cee43e351e1a113e12aefcf837/libavutil/mathematics.c/#L148
int64_t av_compare_mod(uint64_t a, uint64_t b, uint64_t mod){ int64_t c= (a-b) & (mod-1); if(c > (mod>>1)) c-= mod; return c; }
['int av_read_frame(AVFormatContext *s, AVPacket *pkt)\n{\n AVPacketList *pktl;\n int eof=0;\n const int genpts= s->flags & AVFMT_FLAG_GENPTS;\n for(;;){\n pktl = s->packet_buffer;\n if (pktl) {\n AVPacket *next_pkt= &pktl->pkt;\n if(genpts && next_pkt->dts != AV_NOPTS_VALUE){\n int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;\n while(pktl && next_pkt->pts == AV_NOPTS_VALUE){\n if( pktl->pkt.stream_index == next_pkt->stream_index\n && (0 > av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)))\n && av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) {\n next_pkt->pts= pktl->pkt.dts;\n }\n pktl= pktl->next;\n }\n pktl = s->packet_buffer;\n }\n if( next_pkt->pts != AV_NOPTS_VALUE\n || next_pkt->dts == AV_NOPTS_VALUE\n || !genpts || eof){\n *pkt = *next_pkt;\n s->packet_buffer = pktl->next;\n av_free(pktl);\n return 0;\n }\n }\n if(genpts){\n int ret= av_read_frame_internal(s, pkt);\n if(ret<0){\n if(pktl && ret != AVERROR(EAGAIN)){\n eof=1;\n continue;\n }else\n return ret;\n }\n if(av_dup_packet(add_to_pktbuf(&s->packet_buffer, pkt,\n &s->packet_buffer_end)) < 0)\n return AVERROR(ENOMEM);\n }else{\n assert(!s->packet_buffer);\n return av_read_frame_internal(s, pkt);\n }\n }\n}', 'int64_t av_compare_mod(uint64_t a, uint64_t b, uint64_t mod){\n int64_t c= (a-b) & (mod-1);\n if(c > (mod>>1))\n c-= mod;\n return c;\n}']
35,534
0
https://github.com/libav/libav/blob/a1c1c7801918c46da5525cfddb99f3467c522b02/libavcodec/rv40.c/#L373
static inline void rv40_adaptive_loop_filter(uint8_t *src, const int step, const int stride, const int dmode, const int lim_q1, const int lim_p1, const int alpha, const int beta, const int beta2, const int chroma, const int edge) { int diff_p1p0[4], diff_q1q0[4], diff_p1p2[4], diff_q1q2[4]; int sum_p1p0 = 0, sum_q1q0 = 0, sum_p1p2 = 0, sum_q1q2 = 0; uint8_t *ptr; int flag_strong0 = 1, flag_strong1 = 1; int filter_p1, filter_q1; int i; int lims; for(i = 0, ptr = src; i < 4; i++, ptr += stride){ diff_p1p0[i] = ptr[-2*step] - ptr[-1*step]; diff_q1q0[i] = ptr[ 1*step] - ptr[ 0*step]; sum_p1p0 += diff_p1p0[i]; sum_q1q0 += diff_q1q0[i]; } filter_p1 = FFABS(sum_p1p0) < (beta<<2); filter_q1 = FFABS(sum_q1q0) < (beta<<2); if(!filter_p1 && !filter_q1) return; for(i = 0, ptr = src; i < 4; i++, ptr += stride){ diff_p1p2[i] = ptr[-2*step] - ptr[-3*step]; diff_q1q2[i] = ptr[ 1*step] - ptr[ 2*step]; sum_p1p2 += diff_p1p2[i]; sum_q1q2 += diff_q1q2[i]; } if(edge){ flag_strong0 = filter_p1 && (FFABS(sum_p1p2) < beta2); flag_strong1 = filter_q1 && (FFABS(sum_q1q2) < beta2); }else{ flag_strong0 = flag_strong1 = 0; } lims = filter_p1 + filter_q1 + ((lim_q1 + lim_p1) >> 1) + 1; if(flag_strong0 && flag_strong1){ for(i = 0; i < 4; i++, src += stride){ int sflag, p0, q0, p1, q1; int t = src[0*step] - src[-1*step]; if(!t) continue; sflag = (alpha * FFABS(t)) >> 7; if(sflag > 1) continue; p0 = (25*src[-3*step] + 26*src[-2*step] + 26*src[-1*step] + 26*src[ 0*step] + 25*src[ 1*step] + rv40_dither_l[dmode + i]) >> 7; q0 = (25*src[-2*step] + 26*src[-1*step] + 26*src[ 0*step] + 26*src[ 1*step] + 25*src[ 2*step] + rv40_dither_r[dmode + i]) >> 7; if(sflag){ p0 = av_clip(p0, src[-1*step] - lims, src[-1*step] + lims); q0 = av_clip(q0, src[ 0*step] - lims, src[ 0*step] + lims); } p1 = (25*src[-4*step] + 26*src[-3*step] + 26*src[-2*step] + 26*p0 + 25*src[ 0*step] + rv40_dither_l[dmode + i]) >> 7; q1 = (25*src[-1*step] + 26*q0 + 26*src[ 1*step] + 26*src[ 2*step] + 25*src[ 3*step] + rv40_dither_r[dmode + i]) >> 7; if(sflag){ p1 = av_clip(p1, src[-2*step] - lims, src[-2*step] + lims); q1 = av_clip(q1, src[ 1*step] - lims, src[ 1*step] + lims); } src[-2*step] = p1; src[-1*step] = p0; src[ 0*step] = q0; src[ 1*step] = q1; if(!chroma){ src[-3*step] = (25*src[-1*step] + 26*src[-2*step] + 51*src[-3*step] + 26*src[-4*step] + 64) >> 7; src[ 2*step] = (25*src[ 0*step] + 26*src[ 1*step] + 51*src[ 2*step] + 26*src[ 3*step] + 64) >> 7; } } }else if(filter_p1 && filter_q1){ for(i = 0; i < 4; i++, src += stride) rv40_weak_loop_filter(src, step, 1, 1, alpha, beta, lims, lim_q1, lim_p1, diff_p1p0[i], diff_q1q0[i], diff_p1p2[i], diff_q1q2[i]); }else{ for(i = 0; i < 4; i++, src += stride) rv40_weak_loop_filter(src, step, filter_p1, filter_q1, alpha, beta, lims>>1, lim_q1>>1, lim_p1>>1, diff_p1p0[i], diff_q1q0[i], diff_p1p2[i], diff_q1q2[i]); } }
['static inline void rv40_adaptive_loop_filter(uint8_t *src, const int step,\n const int stride, const int dmode,\n const int lim_q1, const int lim_p1,\n const int alpha,\n const int beta, const int beta2,\n const int chroma, const int edge)\n{\n int diff_p1p0[4], diff_q1q0[4], diff_p1p2[4], diff_q1q2[4];\n int sum_p1p0 = 0, sum_q1q0 = 0, sum_p1p2 = 0, sum_q1q2 = 0;\n uint8_t *ptr;\n int flag_strong0 = 1, flag_strong1 = 1;\n int filter_p1, filter_q1;\n int i;\n int lims;\n for(i = 0, ptr = src; i < 4; i++, ptr += stride){\n diff_p1p0[i] = ptr[-2*step] - ptr[-1*step];\n diff_q1q0[i] = ptr[ 1*step] - ptr[ 0*step];\n sum_p1p0 += diff_p1p0[i];\n sum_q1q0 += diff_q1q0[i];\n }\n filter_p1 = FFABS(sum_p1p0) < (beta<<2);\n filter_q1 = FFABS(sum_q1q0) < (beta<<2);\n if(!filter_p1 && !filter_q1)\n return;\n for(i = 0, ptr = src; i < 4; i++, ptr += stride){\n diff_p1p2[i] = ptr[-2*step] - ptr[-3*step];\n diff_q1q2[i] = ptr[ 1*step] - ptr[ 2*step];\n sum_p1p2 += diff_p1p2[i];\n sum_q1q2 += diff_q1q2[i];\n }\n if(edge){\n flag_strong0 = filter_p1 && (FFABS(sum_p1p2) < beta2);\n flag_strong1 = filter_q1 && (FFABS(sum_q1q2) < beta2);\n }else{\n flag_strong0 = flag_strong1 = 0;\n }\n lims = filter_p1 + filter_q1 + ((lim_q1 + lim_p1) >> 1) + 1;\n if(flag_strong0 && flag_strong1){\n for(i = 0; i < 4; i++, src += stride){\n int sflag, p0, q0, p1, q1;\n int t = src[0*step] - src[-1*step];\n if(!t) continue;\n sflag = (alpha * FFABS(t)) >> 7;\n if(sflag > 1) continue;\n p0 = (25*src[-3*step] + 26*src[-2*step]\n + 26*src[-1*step]\n + 26*src[ 0*step] + 25*src[ 1*step] + rv40_dither_l[dmode + i]) >> 7;\n q0 = (25*src[-2*step] + 26*src[-1*step]\n + 26*src[ 0*step]\n + 26*src[ 1*step] + 25*src[ 2*step] + rv40_dither_r[dmode + i]) >> 7;\n if(sflag){\n p0 = av_clip(p0, src[-1*step] - lims, src[-1*step] + lims);\n q0 = av_clip(q0, src[ 0*step] - lims, src[ 0*step] + lims);\n }\n p1 = (25*src[-4*step] + 26*src[-3*step]\n + 26*src[-2*step]\n + 26*p0 + 25*src[ 0*step] + rv40_dither_l[dmode + i]) >> 7;\n q1 = (25*src[-1*step] + 26*q0\n + 26*src[ 1*step]\n + 26*src[ 2*step] + 25*src[ 3*step] + rv40_dither_r[dmode + i]) >> 7;\n if(sflag){\n p1 = av_clip(p1, src[-2*step] - lims, src[-2*step] + lims);\n q1 = av_clip(q1, src[ 1*step] - lims, src[ 1*step] + lims);\n }\n src[-2*step] = p1;\n src[-1*step] = p0;\n src[ 0*step] = q0;\n src[ 1*step] = q1;\n if(!chroma){\n src[-3*step] = (25*src[-1*step] + 26*src[-2*step] + 51*src[-3*step] + 26*src[-4*step] + 64) >> 7;\n src[ 2*step] = (25*src[ 0*step] + 26*src[ 1*step] + 51*src[ 2*step] + 26*src[ 3*step] + 64) >> 7;\n }\n }\n }else if(filter_p1 && filter_q1){\n for(i = 0; i < 4; i++, src += stride)\n rv40_weak_loop_filter(src, step, 1, 1, alpha, beta, lims, lim_q1, lim_p1,\n diff_p1p0[i], diff_q1q0[i], diff_p1p2[i], diff_q1q2[i]);\n }else{\n for(i = 0; i < 4; i++, src += stride)\n rv40_weak_loop_filter(src, step, filter_p1, filter_q1,\n alpha, beta, lims>>1, lim_q1>>1, lim_p1>>1,\n diff_p1p0[i], diff_q1q0[i], diff_p1p2[i], diff_q1q2[i]);\n }\n}']
35,535
0
https://github.com/openssl/openssl/blob/c15e95a61dacfc326cf9cdf05935ae8c6c97bcf6/crypto/lhash/lhash.c/#L208
void *lh_delete(_LHASH *lh, const void *data) { unsigned long hash; LHASH_NODE *nn, **rn; void *ret; lh->error = 0; rn = getrn(lh, data, &hash); if (*rn == NULL) { lh->num_no_delete++; return (NULL); } else { nn = *rn; *rn = nn->next; ret = nn->data; OPENSSL_free(nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) contract(lh); return (ret); }
['MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)\n{\n if (PACKET_remaining(pkt) > 0) {\n ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);\n SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);\n ossl_statem_set_error(s);\n return MSG_PROCESS_ERROR;\n }\n#ifndef OPENSSL_NO_SRP\n if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {\n if (SRP_Calc_A_param(s) <= 0) {\n SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);\n ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);\n ossl_statem_set_error(s);\n return MSG_PROCESS_ERROR;\n }\n }\n#endif\n if (!ssl3_check_cert_and_algorithm(s)) {\n ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);\n ossl_statem_set_error(s);\n return MSG_PROCESS_ERROR;\n }\n if (s->tlsext_status_type != -1 && s->ctx->tlsext_status_cb != NULL) {\n int ret;\n ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);\n if (ret == 0) {\n ssl3_send_alert(s, SSL3_AL_FATAL,\n SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE);\n SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE,\n SSL_R_INVALID_STATUS_RESPONSE);\n return MSG_PROCESS_ERROR;\n }\n if (ret < 0) {\n ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);\n SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, ERR_R_MALLOC_FAILURE);\n return MSG_PROCESS_ERROR;\n }\n }\n#ifndef OPENSSL_NO_SCTP\n if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))\n && s->renegotiate != 0)\n return MSG_PROCESS_CONTINUE_PROCESSING;\n else\n#endif\n return MSG_PROCESS_FINISHED_READING;\n}', 'int ssl3_check_cert_and_algorithm(SSL *s)\n{\n int i;\n#ifndef OPENSSL_NO_EC\n int idx;\n#endif\n long alg_k, alg_a;\n EVP_PKEY *pkey = NULL;\n int al = SSL_AD_HANDSHAKE_FAILURE;\n alg_k = s->s3->tmp.new_cipher->algorithm_mkey;\n alg_a = s->s3->tmp.new_cipher->algorithm_auth;\n if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))\n return (1);\n#ifndef OPENSSL_NO_EC\n idx = s->session->peer_type;\n if (idx == SSL_PKEY_ECC) {\n if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {\n SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);\n goto f_err;\n } else {\n return 1;\n }\n } else if (alg_a & SSL_aECDSA) {\n SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,\n SSL_R_MISSING_ECDSA_SIGNING_CERT);\n goto f_err;\n }\n#endif\n pkey = X509_get0_pubkey(s->session->peer);\n i = X509_certificate_type(s->session->peer, pkey);\n if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {\n SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,\n SSL_R_MISSING_RSA_SIGNING_CERT);\n goto f_err;\n }\n#ifndef OPENSSL_NO_DSA\n else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {\n SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,\n SSL_R_MISSING_DSA_SIGNING_CERT);\n goto f_err;\n }\n#endif\n#ifndef OPENSSL_NO_RSA\n if (alg_k & (SSL_kRSA | SSL_kRSAPSK) &&\n !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {\n SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,\n SSL_R_MISSING_RSA_ENCRYPTING_CERT);\n goto f_err;\n }\n#endif\n#ifndef OPENSSL_NO_DH\n if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {\n al = SSL_AD_INTERNAL_ERROR;\n SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);\n goto f_err;\n }\n#endif\n return (1);\n f_err:\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\n return (0);\n}', 'int ssl3_send_alert(SSL *s, int level, int desc)\n{\n desc = s->method->ssl3_enc->alert_value(desc);\n if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)\n desc = SSL_AD_HANDSHAKE_FAILURE;\n if (desc < 0)\n return -1;\n if ((level == SSL3_AL_FATAL) && (s->session != NULL))\n SSL_CTX_remove_session(s->ctx, s->session);\n s->s3->alert_dispatch = 1;\n s->s3->send_alert[0] = level;\n s->s3->send_alert[1] = desc;\n if (!RECORD_LAYER_write_pending(&s->rlayer)) {\n return s->method->ssl_dispatch_alert(s);\n }\n return -1;\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, c);\n SSL_SESSION_list_remove(ctx, c);\n }\n if (lck)\n CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n if (ret) {\n r->not_resumable = 1;\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, r);\n SSL_SESSION_free(r);\n }\n } else\n ret = 0;\n return (ret);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *lh_delete(_LHASH *lh, const void *data)\n{\n unsigned long hash;\n LHASH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return (NULL);\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return (ret);\n}']
35,536
0
https://github.com/openssl/openssl/blob/16bce0e08b16b28a1953795bde3f913957b08ef2/ssl/packet.c/#L49
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) { assert(pkt->subs != NULL && len != 0); if (pkt->subs == NULL || len == 0) return 0; if (pkt->maxsize - pkt->written < len) return 0; if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) { size_t newlen; size_t reflen; reflen = (len > pkt->buf->length) ? len : pkt->buf->length; if (reflen > SIZE_MAX / 2) { newlen = SIZE_MAX; } else { newlen = reflen * 2; if (newlen < DEFAULT_BUF_SIZE) newlen = DEFAULT_BUF_SIZE; } if (BUF_MEM_grow(pkt->buf, newlen) == 0) return 0; } *allocbytes = GETBUF(pkt) + pkt->curr; return 1; }
['int tls_construct_server_hello(SSL *s, WPACKET *pkt)\n{\n int compm, al = SSL_AD_INTERNAL_ERROR;\n size_t sl, len;\n if (!WPACKET_put_bytes_u16(pkt, s->version)\n || !WPACKET_memcpy(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (s->session->not_resumable ||\n (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)\n && !s->hit))\n s->session->session_id_length = 0;\n sl = s->session->session_id_length;\n if (sl > sizeof(s->session->session_id)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n#ifdef OPENSSL_NO_COMP\n compm = 0;\n#else\n if (s->s3->tmp.new_compression == NULL)\n compm = 0;\n else\n compm = s->s3->tmp.new_compression->id;\n#endif\n if (!WPACKET_sub_memcpy_u8(pkt, s->session->session_id, sl)\n || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)\n || !WPACKET_put_bytes_u8(pkt, compm)\n || !ssl_prepare_serverhello_tlsext(s)\n || !ssl_add_serverhello_tlsext(s, pkt, &al)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n return 1;\n err:\n ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);\n return 0;\n}', 'int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)\n{\n unsigned char *data;\n assert(size <= sizeof(unsigned int));\n if (size > sizeof(unsigned int)\n || !WPACKET_allocate_bytes(pkt, size, &data)\n || !put_value(data, val, size))\n return 0;\n return 1;\n}', 'int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)\n{\n unsigned char *dest;\n if (len == 0)\n return 1;\n if (!WPACKET_allocate_bytes(pkt, len, &dest))\n return 0;\n memcpy(dest, src, len);\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n assert(pkt->subs != NULL && len != 0);\n if (pkt->subs == NULL || len == 0)\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {\n size_t newlen;\n size_t reflen;\n reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n if (reflen > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = reflen * 2;\n if (newlen < DEFAULT_BUF_SIZE)\n newlen = DEFAULT_BUF_SIZE;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n *allocbytes = GETBUF(pkt) + pkt->curr;\n return 1;\n}']
35,537
0
https://github.com/openssl/openssl/blob/3ad4af89cf7380aa94d1995e05e713d59e1c469a/crypto/err/err.c/#L779
ERR_STATE *ERR_get_state(void) { ERR_STATE *state = NULL; CRYPTO_THREAD_run_once(&err_init, err_do_init); state = CRYPTO_THREAD_get_local(&err_thread_local); if (state == NULL) { state = OPENSSL_zalloc(sizeof(*state)); if (state == NULL) return NULL; if (!CRYPTO_THREAD_set_local(&err_thread_local, state)) { ERR_STATE_free(state); return NULL; } OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE); } return state; }
['ERR_STATE *ERR_get_state(void)\n{\n ERR_STATE *state = NULL;\n CRYPTO_THREAD_run_once(&err_init, err_do_init);\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 (!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 ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE);\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 *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)\n{\n return pthread_getspecific(*key);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n 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}', 'int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)\n{\n if (pthread_setspecific(*key, val) != 0)\n return 0;\n return 1;\n}']
35,538
0
https://github.com/openssl/openssl/blob/2864df8f9d3264e19b49a246e272fb513f4c1be3/crypto/bn/bn_ctx.c/#L270
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int file_quotient(STANZA *s)\n{\n BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;\n BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;\n BN_ULONG b_word, ret_word;\n int st = 0;\n if (!TEST_ptr(a = getBN(s, "A"))\n || !TEST_ptr(b = getBN(s, "B"))\n || !TEST_ptr(quotient = getBN(s, "Quotient"))\n || !TEST_ptr(remainder = getBN(s, "Remainder"))\n || !TEST_ptr(ret = BN_new())\n || !TEST_ptr(ret2 = BN_new())\n || !TEST_ptr(nnmod = BN_new()))\n goto err;\n if (!TEST_true(BN_div(ret, ret2, a, b, ctx))\n || !equalBN("A / B", quotient, ret)\n || !equalBN("A % B", remainder, ret2)\n || !TEST_true(BN_mul(ret, quotient, b, ctx))\n || !TEST_true(BN_add(ret, ret, remainder))\n || !equalBN("Quotient * B + Remainder", a, ret))\n goto err;\n b_word = BN_get_word(b);\n if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {\n BN_ULONG remainder_word = BN_get_word(remainder);\n assert(remainder_word != (BN_ULONG)-1);\n if (!TEST_ptr(BN_copy(ret, a)))\n goto err;\n ret_word = BN_div_word(ret, b_word);\n if (ret_word != remainder_word) {\n#ifdef BN_DEC_FMT1\n TEST_error(\n "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,\n ret_word, remainder_word);\n#else\n TEST_error("Got A %% B (word) mismatch");\n#endif\n goto err;\n }\n if (!equalBN ("A / B (word)", quotient, ret))\n goto err;\n ret_word = BN_mod_word(a, b_word);\n if (ret_word != remainder_word) {\n#ifdef BN_DEC_FMT1\n TEST_error(\n "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",\n ret_word, remainder_word);\n#else\n TEST_error("Got A %% B (word) mismatch");\n#endif\n goto err;\n }\n }\n if (!BN_is_negative(b)) {\n if (!TEST_true(BN_copy(nnmod, remainder))\n || (BN_is_negative(nnmod)\n && !TEST_true(BN_add(nnmod, nnmod, b)))\n || !TEST_true(BN_nnmod(ret, a, b, ctx))\n || !equalBN("A % B (non-negative)", nnmod, ret))\n goto err;\n }\n st = 1;\n err:\n BN_free(a);\n BN_free(b);\n BN_free(quotient);\n BN_free(remainder);\n BN_free(ret);\n BN_free(ret2);\n BN_free(nnmod);\n return st;\n}', '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_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
35,539
0
https://github.com/nginx/nginx/blob/89bd5f038a570694763fd968ab471a91139b2f7f/src/core/ngx_string.c/#L1180
static ngx_int_t ngx_decode_base64_internal(ngx_str_t *dst, ngx_str_t *src, const u_char *basis) { size_t len; u_char *d, *s; for (len = 0; len < src->len; len++) { if (src->data[len] == '=') { break; } if (basis[src->data[len]] == 77) { return NGX_ERROR; } } if (len % 4 == 1) { return NGX_ERROR; } s = src->data; d = dst->data; while (len > 3) { *d++ = (u_char) (basis[s[0]] << 2 | basis[s[1]] >> 4); *d++ = (u_char) (basis[s[1]] << 4 | basis[s[2]] >> 2); *d++ = (u_char) (basis[s[2]] << 6 | basis[s[3]]); s += 4; len -= 4; } if (len > 1) { *d++ = (u_char) (basis[s[0]] << 2 | basis[s[1]] >> 4); } if (len > 2) { *d++ = (u_char) (basis[s[1]] << 4 | basis[s[2]] >> 2); } dst->len = d - dst->data; return NGX_OK; }
['static ngx_int_t\nngx_crypt_ssha(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)\n{\n size_t len;\n ngx_int_t rc;\n ngx_str_t encoded, decoded;\n ngx_sha1_t sha1;\n encoded.data = salt + sizeof("{SSHA}") - 1;\n encoded.len = ngx_strlen(encoded.data);\n len = ngx_max(ngx_base64_decoded_length(encoded.len), 20);\n decoded.data = ngx_pnalloc(pool, len);\n if (decoded.data == NULL) {\n return NGX_ERROR;\n }\n rc = ngx_decode_base64(&decoded, &encoded);\n if (rc != NGX_OK || decoded.len < 20) {\n decoded.len = 20;\n }\n ngx_sha1_init(&sha1);\n ngx_sha1_update(&sha1, key, ngx_strlen(key));\n ngx_sha1_update(&sha1, decoded.data + 20, decoded.len - 20);\n ngx_sha1_final(decoded.data, &sha1);\n len = sizeof("{SSHA}") - 1 + ngx_base64_encoded_length(decoded.len) + 1;\n *encrypted = ngx_pnalloc(pool, len);\n if (*encrypted == NULL) {\n return NGX_ERROR;\n }\n encoded.data = ngx_cpymem(*encrypted, "{SSHA}", sizeof("{SSHA}") - 1);\n ngx_encode_base64(&encoded, &decoded);\n encoded.data[encoded.len] = \'\\0\';\n return NGX_OK;\n}', 'void *\nngx_pnalloc(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 = p->d.last;\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}', 'ngx_int_t\nngx_decode_base64(ngx_str_t *dst, ngx_str_t *src)\n{\n static u_char basis64[] = {\n 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,\n 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,\n 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77, 77, 63,\n 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,\n 77, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,\n 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 77, 77, 77, 77, 77,\n 77, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,\n 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 77, 77, 77, 77, 77,\n 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,\n 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,\n 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,\n 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,\n 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,\n 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,\n 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,\n 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77\n };\n return ngx_decode_base64_internal(dst, src, basis64);\n}', "static ngx_int_t\nngx_decode_base64_internal(ngx_str_t *dst, ngx_str_t *src, const u_char *basis)\n{\n size_t len;\n u_char *d, *s;\n for (len = 0; len < src->len; len++) {\n if (src->data[len] == '=') {\n break;\n }\n if (basis[src->data[len]] == 77) {\n return NGX_ERROR;\n }\n }\n if (len % 4 == 1) {\n return NGX_ERROR;\n }\n s = src->data;\n d = dst->data;\n while (len > 3) {\n *d++ = (u_char) (basis[s[0]] << 2 | basis[s[1]] >> 4);\n *d++ = (u_char) (basis[s[1]] << 4 | basis[s[2]] >> 2);\n *d++ = (u_char) (basis[s[2]] << 6 | basis[s[3]]);\n s += 4;\n len -= 4;\n }\n if (len > 1) {\n *d++ = (u_char) (basis[s[0]] << 2 | basis[s[1]] >> 4);\n }\n if (len > 2) {\n *d++ = (u_char) (basis[s[1]] << 4 | basis[s[2]] >> 2);\n }\n dst->len = d - dst->data;\n return NGX_OK;\n}"]
35,540
0
https://github.com/libav/libav/blob/9f54e461fecec7a97ec1b97ae4468135ea770609/libavformat/movenc.c/#L2072
static void mov_create_chapter_track(AVFormatContext *s, int tracknum) { MOVMuxContext *mov = s->priv_data; MOVTrack *track = &mov->tracks[tracknum]; AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY }; int i, len; track->mode = mov->mode; track->tag = MKTAG('t','e','x','t'); track->timescale = MOV_TIMESCALE; track->enc = avcodec_alloc_context(); track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE; for (i = 0; i < s->nb_chapters; i++) { AVChapter *c = s->chapters[i]; AVMetadataTag *t; int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE}); pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE}); pkt.duration = end - pkt.dts; if ((t = av_metadata_get(c->metadata, "title", NULL, 0))) { len = strlen(t->value); pkt.size = len+2; pkt.data = av_malloc(pkt.size); AV_WB16(pkt.data, len); memcpy(pkt.data+2, t->value, len); ff_mov_write_packet(s, &pkt); av_freep(&pkt.data); } } }
['static void mov_create_chapter_track(AVFormatContext *s, int tracknum)\n{\n MOVMuxContext *mov = s->priv_data;\n MOVTrack *track = &mov->tracks[tracknum];\n AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };\n int i, len;\n track->mode = mov->mode;\n track->tag = MKTAG(\'t\',\'e\',\'x\',\'t\');\n track->timescale = MOV_TIMESCALE;\n track->enc = avcodec_alloc_context();\n track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;\n for (i = 0; i < s->nb_chapters; i++) {\n AVChapter *c = s->chapters[i];\n AVMetadataTag *t;\n int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});\n pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});\n pkt.duration = end - pkt.dts;\n if ((t = av_metadata_get(c->metadata, "title", NULL, 0))) {\n len = strlen(t->value);\n pkt.size = len+2;\n pkt.data = av_malloc(pkt.size);\n AV_WB16(pkt.data, len);\n memcpy(pkt.data+2, t->value, len);\n ff_mov_write_packet(s, &pkt);\n av_freep(&pkt.data);\n }\n }\n}', 'AVCodecContext *avcodec_alloc_context(void){\n return avcodec_alloc_context2(AVMEDIA_TYPE_UNKNOWN);\n}', 'AVCodecContext *avcodec_alloc_context2(enum AVMediaType codec_type){\n AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));\n if(avctx==NULL) return NULL;\n avcodec_get_context_defaults2(avctx, codec_type);\n return avctx;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-32) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+32);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&31) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,32,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(32,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){\n int64_t b= bq.num * (int64_t)cq.den;\n int64_t c= cq.num * (int64_t)bq.den;\n return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);\n}', 'static av_always_inline av_const uint16_t av_bswap16(uint16_t x)\n{\n x= (x>>8) | (x<<8);\n return x;\n}']
35,541
0
https://github.com/openssl/openssl/blob/183733f882056ea3e6fe95e665b85fcc6a45dcb4/crypto/cms/cms_pwri.c/#L352
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 (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 OBJ_obj2nid(const ASN1_OBJECT *a)\n{\n const unsigned int *op;\n ADDED_OBJ ad, *adp;\n if (a == NULL)\n return (NID_undef);\n if (a->nid != 0)\n return (a->nid);\n if (a->length == 0)\n return NID_undef;\n if (added != NULL) {\n ad.type = ADDED_DATA;\n ad.obj = (ASN1_OBJECT *)a;\n adp = lh_ADDED_OBJ_retrieve(added, &ad);\n if (adp != NULL)\n return (adp->obj->nid);\n }\n op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);\n if (op == NULL)\n return (NID_undef);\n return (nid_objs[*op].nid);\n}', 'DEFINE_LHASH_OF(ADDED_OBJ)', 'void *lh_retrieve(_LHASH *lh, const void *data)\n{\n unsigned long hash;\n LHASH_NODE **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_retrieve_miss++;\n return (NULL);\n } else {\n ret = (*rn)->data;\n lh->num_retrieve++;\n }\n return (ret);\n}', 'void *ASN1_TYPE_unpack_sequence(const ASN1_ITEM *it, const ASN1_TYPE *t)\n{\n if (t == NULL || t->type != V_ASN1_SEQUENCE || t->value.sequence == NULL)\n return NULL;\n return ASN1_item_unpack(t->value.sequence, it);\n}']
35,542
0
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/err/err.c/#L752
int ERR_set_mark(void) { ERR_STATE *es; es = ERR_get_state(); if (es->bottom == es->top) return 0; es->err_flags[es->top] |= ERR_FLAG_MARK; return 1; }
['int ERR_set_mark(void)\n{\n ERR_STATE *es;\n es = ERR_get_state();\n if (es->bottom == es->top)\n return 0;\n es->err_flags[es->top] |= ERR_FLAG_MARK;\n return 1;\n}', 'ERR_STATE *ERR_get_state(void)\n{\n ERR_STATE *state = NULL;\n CRYPTO_THREAD_run_once(&err_init, err_do_init);\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 (!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 ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE);\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 *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)\n{\n return pthread_getspecific(*key);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
35,543
0
https://github.com/libav/libav/blob/452a398fd6bdca3f301c5c8af3bc241bc16a777e/libavcodec/motion_est.c/#L176
static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby, const int size, const int h, int ref_index, int src_index, me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){ MotionEstContext * const c= &s->me; const int stride= c->stride; const int uvstride= c->uvstride; const int qpel= flags&FLAG_QPEL; const int chroma= flags&FLAG_CHROMA; const int dxy= subx + (suby<<(1+qpel)); const int hx= subx + (x<<(1+qpel)); const int hy= suby + (y<<(1+qpel)); uint8_t * const * const ref= c->ref[ref_index]; uint8_t * const * const src= c->src[src_index]; int d; if(flags&FLAG_DIRECT){ assert(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)); if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){ const int time_pp= s->pp_time; const int time_pb= s->pb_time; const int mask= 2*qpel+1; if(s->mv_type==MV_TYPE_8X8){ int i; for(i=0; i<4; i++){ int fx = c->direct_basis_mv[i][0] + hx; int fy = c->direct_basis_mv[i][1] + hy; int bx = hx ? fx - c->co_located_mv[i][0] : c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(qpel+4)); int by = hy ? fy - c->co_located_mv[i][1] : c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(qpel+4)); int fxy= (fx&mask) + ((fy&mask)<<(qpel+1)); int bxy= (bx&mask) + ((by&mask)<<(qpel+1)); uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1); if(qpel){ c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride); c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride); }else{ c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8); c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8); } } }else{ int fx = c->direct_basis_mv[0][0] + hx; int fy = c->direct_basis_mv[0][1] + hy; int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp); int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp); int fxy= (fx&mask) + ((fy&mask)<<(qpel+1)); int bxy= (bx&mask) + ((by&mask)<<(qpel+1)); if(qpel){ c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride); c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride); c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride); c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride); c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride); c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride); c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride); c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride); }else{ assert((fx>>1) + 16*s->mb_x >= -16); assert((fy>>1) + 16*s->mb_y >= -16); assert((fx>>1) + 16*s->mb_x <= s->width); assert((fy>>1) + 16*s->mb_y <= s->height); assert((bx>>1) + 16*s->mb_x >= -16); assert((by>>1) + 16*s->mb_y >= -16); assert((bx>>1) + 16*s->mb_x <= s->width); assert((by>>1) + 16*s->mb_y <= s->height); c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16); c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16); } } d = cmp_func(s, c->temp, src[0], stride, 16); }else d= 256*256*256*32; }else{ int uvdxy; if(dxy){ if(qpel){ c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride); if(chroma){ int cx= hx/2; int cy= hy/2; cx= (cx>>1)|(cx&1); cy= (cy>>1)|(cy&1); uvdxy= (cx&1) + 2*(cy&1); } }else{ c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h); if(chroma) uvdxy= dxy | (x&1) | (2*(y&1)); } d = cmp_func(s, c->temp, src[0], stride, h); }else{ d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h); if(chroma) uvdxy= (x&1) + 2*(y&1); } if(chroma){ uint8_t * const uvtemp= c->temp + 16*stride; c->hpel_put[size+1][uvdxy](uvtemp , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1); c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1); d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h>>1); d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1); } } #if 0 if(full_pel){ const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1); score_map[index]= d; } d += (c->mv_penalty[hx - c->pred_x] + c->mv_penalty[hy - c->pred_y])*c->penalty_factor; #endif return d; }
['void ff_estimate_b_frame_motion(MpegEncContext * s,\n int mb_x, int mb_y)\n{\n MotionEstContext * const c= &s->me;\n const int penalty_factor= c->mb_penalty_factor;\n int fmin, bmin, dmin, fbmin, bimin, fimin;\n int type=0;\n const int xy = mb_y*s->mb_stride + mb_x;\n init_ref(c, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2);\n get_limits(s, 16*mb_x, 16*mb_y);\n c->skip=0;\n if(s->codec_id == CODEC_ID_MPEG4 && s->next_picture.mbskip_table[xy]){\n int score= direct_search(s, mb_x, mb_y);\n score= ((unsigned)(score*score + 128*256))>>16;\n c->mc_mb_var_sum_temp += score;\n s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score;\n s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;\n return;\n }\n if(c->avctx->me_threshold){\n int vard= check_input_motion(s, mb_x, mb_y, 0);\n if((vard+128)>>8 < c->avctx->me_threshold){\n s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;\n c->mc_mb_var_sum_temp += (vard+128)>>8;\n return;\n }\n if((vard+128)>>8 < c->avctx->mb_threshold){\n type= s->mb_type[mb_y*s->mb_stride + mb_x];\n if(type == CANDIDATE_MB_TYPE_DIRECT){\n direct_search(s, mb_x, mb_y);\n }\n if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){\n c->skip=0;\n ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);\n }\n if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){\n c->skip=0;\n ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);\n }\n if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){\n c->skip=0;\n c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;\n interlaced_search(s, 0,\n s->b_field_mv_table[0], s->b_field_select_table[0],\n s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);\n }\n if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){\n c->skip=0;\n c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;\n interlaced_search(s, 2,\n s->b_field_mv_table[1], s->b_field_select_table[1],\n s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);\n }\n return;\n }\n }\n if (s->codec_id == CODEC_ID_MPEG4)\n dmin= direct_search(s, mb_x, mb_y);\n else\n dmin= INT_MAX;\n c->skip=0;\n fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;\n c->skip=0;\n bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;\n c->skip=0;\n fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;\n if(s->flags & CODEC_FLAG_INTERLACED_ME){\n c->skip=0;\n c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;\n fimin= interlaced_search(s, 0,\n s->b_field_mv_table[0], s->b_field_select_table[0],\n s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);\n c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;\n bimin= interlaced_search(s, 2,\n s->b_field_mv_table[1], s->b_field_select_table[1],\n s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);\n }else\n fimin= bimin= INT_MAX;\n {\n int score= fmin;\n type = CANDIDATE_MB_TYPE_FORWARD;\n if (dmin <= score){\n score = dmin;\n type = CANDIDATE_MB_TYPE_DIRECT;\n }\n if(bmin<score){\n score=bmin;\n type= CANDIDATE_MB_TYPE_BACKWARD;\n }\n if(fbmin<score){\n score=fbmin;\n type= CANDIDATE_MB_TYPE_BIDIR;\n }\n if(fimin<score){\n score=fimin;\n type= CANDIDATE_MB_TYPE_FORWARD_I;\n }\n if(bimin<score){\n score=bimin;\n type= CANDIDATE_MB_TYPE_BACKWARD_I;\n }\n score= ((unsigned)(score*score + 128*256))>>16;\n c->mc_mb_var_sum_temp += score;\n s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score;\n }\n if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){\n type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;\n if(fimin < INT_MAX)\n type |= CANDIDATE_MB_TYPE_FORWARD_I;\n if(bimin < INT_MAX)\n type |= CANDIDATE_MB_TYPE_BACKWARD_I;\n if(fimin < INT_MAX && bimin < INT_MAX){\n type |= CANDIDATE_MB_TYPE_BIDIR_I;\n }\n if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT;\n if(s->codec_id == CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT && s->flags&CODEC_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])\n type |= CANDIDATE_MB_TYPE_DIRECT0;\n#if 0\n if(s->out_format == FMT_MPEG1)\n type |= CANDIDATE_MB_TYPE_INTRA;\n#endif\n }\n s->mb_type[mb_y*s->mb_stride + mb_x]= type;\n}', 'static int ff_estimate_motion_b(MpegEncContext * s,\n int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)\n{\n MotionEstContext * const c= &s->me;\n int mx, my, dmin;\n int P[10][2];\n const int shift= 1+s->quarter_sample;\n const int mot_stride = s->mb_stride;\n const int mot_xy = mb_y*mot_stride + mb_x;\n uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;\n int mv_scale;\n c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);\n c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);\n c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);\n c->current_mv_penalty= mv_penalty;\n get_limits(s, 16*mb_x, 16*mb_y);\n switch(s->me_method) {\n case ME_ZERO:\n default:\n no_motion_search(s, &mx, &my);\n dmin = 0;\n mx-= mb_x*16;\n my-= mb_y*16;\n break;\n case ME_X1:\n case ME_EPZS:\n {\n P_LEFT[0] = mv_table[mot_xy - 1][0];\n P_LEFT[1] = mv_table[mot_xy - 1][1];\n if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);\n if (!s->first_slice_line) {\n P_TOP[0] = mv_table[mot_xy - mot_stride ][0];\n P_TOP[1] = mv_table[mot_xy - mot_stride ][1];\n P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0];\n P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1];\n if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1]= (c->ymax<<shift);\n if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);\n if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);\n P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);\n P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);\n }\n c->pred_x= P_LEFT[0];\n c->pred_y= P_LEFT[1];\n }\n if(mv_table == s->b_forw_mv_table){\n mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);\n }else{\n mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);\n }\n dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);\n break;\n }\n dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);\n if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)\n dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);\n mv_table[mot_xy][0]= mx;\n mv_table[mot_xy][1]= my;\n return dmin;\n}', 'inline int ff_epzs_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr,\n int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],\n int ref_mv_scale, int size, int h)\n{\n MotionEstContext * const c= &s->me;\n if(c->flags==0 && h==16 && size==0){\n return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0, 0, 16);\n }else{\n return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, c->flags, size, h);\n }\n}', 'static av_always_inline int epzs_motion_search_internal(MpegEncContext * s, int *mx_ptr, int *my_ptr,\n int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],\n int ref_mv_scale, int flags, int size, int h)\n{\n MotionEstContext * const c= &s->me;\n int best[2]={0, 0};\n int d;\n int dmin;\n int map_generation;\n int penalty_factor;\n const int ref_mv_stride= s->mb_stride;\n const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride;\n me_cmp_func cmpf, chroma_cmpf;\n LOAD_COMMON\n LOAD_COMMON2\n if(c->pre_pass){\n penalty_factor= c->pre_penalty_factor;\n cmpf= s->dsp.me_pre_cmp[size];\n chroma_cmpf= s->dsp.me_pre_cmp[size+1];\n }else{\n penalty_factor= c->penalty_factor;\n cmpf= s->dsp.me_cmp[size];\n chroma_cmpf= s->dsp.me_cmp[size+1];\n }\n map_generation= update_map_generation(c);\n assert(cmpf);\n dmin= cmp(s, 0, 0, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\n map[0]= map_generation;\n score_map[0]= dmin;\n if((s->pict_type == FF_B_TYPE && !(c->flags & FLAG_DIRECT)) || s->flags&CODEC_FLAG_MV0)\n dmin += (mv_penalty[pred_x] + mv_penalty[pred_y])*penalty_factor;\n if (s->first_slice_line) {\n CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)\n }else{\n if(dmin<((h*h*s->avctx->mv0_threshold)>>8)\n && ( P_LEFT[0] |P_LEFT[1]\n |P_TOP[0] |P_TOP[1]\n |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){\n *mx_ptr= 0;\n *my_ptr= 0;\n c->skip=1;\n return dmin;\n }\n CHECK_MV( P_MEDIAN[0] >>shift , P_MEDIAN[1] >>shift)\n CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift) , (P_MEDIAN[1]>>shift)-1)\n CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift) , (P_MEDIAN[1]>>shift)+1)\n CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)-1, (P_MEDIAN[1]>>shift) )\n CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)+1, (P_MEDIAN[1]>>shift) )\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)\n CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)\n CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)\n CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)\n }\n if(dmin>h*h*4){\n if(c->pre_pass){\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)\n if(!s->first_slice_line)\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)\n }else{\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)\n if(s->mb_y+1<s->end_mb_y)\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)\n }\n }\n if(c->avctx->last_predictor_count){\n const int count= c->avctx->last_predictor_count;\n const int xstart= FFMAX(0, s->mb_x - count);\n const int ystart= FFMAX(0, s->mb_y - count);\n const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);\n const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);\n int mb_y;\n for(mb_y=ystart; mb_y<yend; mb_y++){\n int mb_x;\n for(mb_x=xstart; mb_x<xend; mb_x++){\n const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;\n int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;\n int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;\n if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;\n CHECK_MV(mx,my)\n }\n }\n }\n dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n *mx_ptr= best[0];\n *my_ptr= best[1];\n return dmin;\n}', 'static av_always_inline int diamond_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 MotionEstContext * const c= &s->me;\n if(c->dia_size==-1)\n return funny_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size<-1)\n return sab_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size<2)\n return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size>1024)\n return full_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size>768)\n return umh_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size>512)\n return hex_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags, c->dia_size&0xFF);\n else if(c->dia_size>256)\n return l2s_dia_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else\n return var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n}', 'static int l2s_dia_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,i,d;\n int dia_size= c->dia_size&0xFF;\n const int dec= dia_size & (dia_size-1);\n static const int hex[8][2]={{-2, 0}, {-1,-1}, { 0,-2}, { 1,-1},\n { 2, 0}, { 1, 1}, { 0, 2}, {-1, 1}};\n cmpf= s->dsp.me_cmp[size];\n chroma_cmpf= s->dsp.me_cmp[size+1];\n for(; dia_size; dia_size= dec ? dia_size-1 : dia_size>>1){\n do{\n x= best[0];\n y= best[1];\n for(i=0; i<8; i++){\n CHECK_CLIPPED_MV(x+hex[i][0]*dia_size, y+hex[i][1]*dia_size);\n }\n }while(best[0] != x || best[1] != y);\n }\n x= best[0];\n y= best[1];\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 return dmin;\n}', 'static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,\n const int size, const int h, int ref_index, int src_index,\n me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){\n MotionEstContext * const c= &s->me;\n const int stride= c->stride;\n const int uvstride= c->uvstride;\n const int qpel= flags&FLAG_QPEL;\n const int chroma= flags&FLAG_CHROMA;\n const int dxy= subx + (suby<<(1+qpel));\n const int hx= subx + (x<<(1+qpel));\n const int hy= suby + (y<<(1+qpel));\n uint8_t * const * const ref= c->ref[ref_index];\n uint8_t * const * const src= c->src[src_index];\n int d;\n if(flags&FLAG_DIRECT){\n assert(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1));\n if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){\n const int time_pp= s->pp_time;\n const int time_pb= s->pb_time;\n const int mask= 2*qpel+1;\n if(s->mv_type==MV_TYPE_8X8){\n int i;\n for(i=0; i<4; i++){\n int fx = c->direct_basis_mv[i][0] + hx;\n int fy = c->direct_basis_mv[i][1] + hy;\n int bx = hx ? fx - c->co_located_mv[i][0] : c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(qpel+4));\n int by = hy ? fy - c->co_located_mv[i][1] : c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(qpel+4));\n int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));\n int bxy= (bx&mask) + ((by&mask)<<(qpel+1));\n uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);\n if(qpel){\n c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);\n c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);\n }else{\n c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);\n c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);\n }\n }\n }else{\n int fx = c->direct_basis_mv[0][0] + hx;\n int fy = c->direct_basis_mv[0][1] + hy;\n int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);\n int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);\n int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));\n int bxy= (bx&mask) + ((by&mask)<<(qpel+1));\n if(qpel){\n c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride);\n c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride);\n c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride);\n c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);\n c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride);\n c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride);\n c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride);\n c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);\n }else{\n assert((fx>>1) + 16*s->mb_x >= -16);\n assert((fy>>1) + 16*s->mb_y >= -16);\n assert((fx>>1) + 16*s->mb_x <= s->width);\n assert((fy>>1) + 16*s->mb_y <= s->height);\n assert((bx>>1) + 16*s->mb_x >= -16);\n assert((by>>1) + 16*s->mb_y >= -16);\n assert((bx>>1) + 16*s->mb_x <= s->width);\n assert((by>>1) + 16*s->mb_y <= s->height);\n c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);\n c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);\n }\n }\n d = cmp_func(s, c->temp, src[0], stride, 16);\n }else\n d= 256*256*256*32;\n }else{\n int uvdxy;\n if(dxy){\n if(qpel){\n c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride);\n if(chroma){\n int cx= hx/2;\n int cy= hy/2;\n cx= (cx>>1)|(cx&1);\n cy= (cy>>1)|(cy&1);\n uvdxy= (cx&1) + 2*(cy&1);\n }\n }else{\n c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);\n if(chroma)\n uvdxy= dxy | (x&1) | (2*(y&1));\n }\n d = cmp_func(s, c->temp, src[0], stride, h);\n }else{\n d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);\n if(chroma)\n uvdxy= (x&1) + 2*(y&1);\n }\n if(chroma){\n uint8_t * const uvtemp= c->temp + 16*stride;\n c->hpel_put[size+1][uvdxy](uvtemp , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);\n c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);\n d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h>>1);\n d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);\n }\n }\n#if 0\n if(full_pel){\n const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\n score_map[index]= d;\n }\n d += (c->mv_penalty[hx - c->pred_x] + c->mv_penalty[hy - c->pred_y])*c->penalty_factor;\n#endif\n return d;\n}']
35,544
0
https://github.com/openssl/openssl/blob/4dfc8f1f0b3ff85adfdca3a37be5df7928092f07/apps/speed.c/#L2494
static int do_multi(int multi) { int n; int fd[2]; int *fds; static char sep[]=":"; fds=malloc(multi*sizeof *fds); for(n=0 ; n < multi ; ++n) { pipe(fd); if(fork()) { close(fd[1]); fds[n]=fd[0]; } else { close(fd[0]); close(1); dup(fd[1]); close(fd[1]); mr=1; usertime=0; return 0; } printf("Forked child %d\n",n); } for(n=0 ; n < multi ; ++n) { FILE *f; char buf[1024]; char *p; f=fdopen(fds[n],"r"); while(fgets(buf,sizeof buf,f)) { p=strchr(buf,'\n'); if(p) *p='\0'; if(buf[0] != '+') { fprintf(stderr,"Don't understand line '%s' from child %d\n", buf,n); continue; } printf("Got: %s from %d\n",buf,n); if(!strncmp(buf,"+F:",3)) { int alg; int j; p=buf+3; alg=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); for(j=0 ; j < SIZE_NUM ; ++j) results[alg][j]+=atof(sstrsep(&p,sep)); } else if(!strncmp(buf,"+F2:",4)) { int k; double d; p=buf+4; k=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); d=atof(sstrsep(&p,sep)); if(n) rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d); else rsa_results[k][0]=d; d=atof(sstrsep(&p,sep)); if(n) rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d); else rsa_results[k][1]=d; } else if(!strncmp(buf,"+F2:",4)) { int k; double d; p=buf+4; k=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); d=atof(sstrsep(&p,sep)); if(n) rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d); else rsa_results[k][0]=d; d=atof(sstrsep(&p,sep)); if(n) rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d); else rsa_results[k][1]=d; } else if(!strncmp(buf,"+F3:",4)) { int k; double d; p=buf+4; k=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); d=atof(sstrsep(&p,sep)); if(n) dsa_results[k][0]=1/(1/dsa_results[k][0]+1/d); else dsa_results[k][0]=d; d=atof(sstrsep(&p,sep)); if(n) dsa_results[k][1]=1/(1/dsa_results[k][1]+1/d); else dsa_results[k][1]=d; } #ifndef OPENSSL_NO_ECDSA else if(!strncmp(buf,"+F4:",4)) { int k; double d; p=buf+4; k=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); d=atof(sstrsep(&p,sep)); if(n) ecdsa_results[k][0]=1/(1/ecdsa_results[k][0]+1/d); else ecdsa_results[k][0]=d; d=atof(sstrsep(&p,sep)); if(n) ecdsa_results[k][1]=1/(1/ecdsa_results[k][1]+1/d); else ecdsa_results[k][1]=d; } #endif #ifndef OPENSSL_NO_ECDH else if(!strncmp(buf,"+F5:",4)) { int k; double d; p=buf+4; k=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); d=atof(sstrsep(&p,sep)); if(n) ecdh_results[k][0]=1/(1/ecdh_results[k][0]+1/d); else ecdh_results[k][0]=d; } #endif else if(!strncmp(buf,"+H:",3)) { } else fprintf(stderr,"Unknown type '%s' from child %d\n",buf,n); } } return 1; }
['static int do_multi(int multi)\n\t{\n\tint n;\n\tint fd[2];\n\tint *fds;\n\tstatic char sep[]=":";\n\tfds=malloc(multi*sizeof *fds);\n\tfor(n=0 ; n < multi ; ++n)\n\t\t{\n\t\tpipe(fd);\n\t\tif(fork())\n\t\t\t{\n\t\t\tclose(fd[1]);\n\t\t\tfds[n]=fd[0];\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tclose(fd[0]);\n\t\t\tclose(1);\n\t\t\tdup(fd[1]);\n\t\t\tclose(fd[1]);\n\t\t\tmr=1;\n\t\t\tusertime=0;\n\t\t\treturn 0;\n\t\t\t}\n\t\tprintf("Forked child %d\\n",n);\n\t\t}\n\tfor(n=0 ; n < multi ; ++n)\n\t\t{\n\t\tFILE *f;\n\t\tchar buf[1024];\n\t\tchar *p;\n\t\tf=fdopen(fds[n],"r");\n\t\twhile(fgets(buf,sizeof buf,f))\n\t\t\t{\n\t\t\tp=strchr(buf,\'\\n\');\n\t\t\tif(p)\n\t\t\t\t*p=\'\\0\';\n\t\t\tif(buf[0] != \'+\')\n\t\t\t\t{\n\t\t\t\tfprintf(stderr,"Don\'t understand line \'%s\' from child %d\\n",\n\t\t\t\t\t\tbuf,n);\n\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\tprintf("Got: %s from %d\\n",buf,n);\n\t\t\tif(!strncmp(buf,"+F:",3))\n\t\t\t\t{\n\t\t\t\tint alg;\n\t\t\t\tint j;\n\t\t\t\tp=buf+3;\n\t\t\t\talg=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\tfor(j=0 ; j < SIZE_NUM ; ++j)\n\t\t\t\t\tresults[alg][j]+=atof(sstrsep(&p,sep));\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F2:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][1]=d;\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F2:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][1]=d;\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F3:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tdsa_results[k][0]=1/(1/dsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tdsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tdsa_results[k][1]=1/(1/dsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\tdsa_results[k][1]=d;\n\t\t\t\t}\n#ifndef OPENSSL_NO_ECDSA\n\t\t\telse if(!strncmp(buf,"+F4:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdsa_results[k][0]=1/(1/ecdsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdsa_results[k][1]=1/(1/ecdsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdsa_results[k][1]=d;\n\t\t\t\t}\n#endif\n#ifndef OPENSSL_NO_ECDH\n\t\t\telse if(!strncmp(buf,"+F5:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdh_results[k][0]=1/(1/ecdh_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdh_results[k][0]=d;\n\t\t\t\t}\n#endif\n\t\t\telse if(!strncmp(buf,"+H:",3))\n\t\t\t\t{\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tfprintf(stderr,"Unknown type \'%s\' from child %d\\n",buf,n);\n\t\t\t}\n\t\t}\n\treturn 1;\n\t}']
35,545
0
https://github.com/libav/libav/blob/e5b0fc170f85b00f7dd0ac514918fb5c95253d39/libavcodec/bitstream.h/#L139
static inline uint64_t get_val(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1); bc->bits >>= n; #else uint64_t ret = bc->bits >> (64 - n); bc->bits <<= n; #endif bc->bits_left -= n; return ret; }
['static inline void decode4x4(BitstreamContext *bc, uint8_t *dst, int linesize)\n{\n int i, j, v[2];\n switch (bitstream_read(bc, 2)) {\n case 1:\n v[0] = bitstream_read(bc, 8);\n for (j = 0; j < 4; j++)\n memset(dst + j * linesize, v[0], 4);\n break;\n case 2:\n v[0] = bitstream_read(bc, 8);\n v[1] = bitstream_read(bc, 8);\n for (j = 2; j >= 0; j -= 2) {\n for (i = 0; i < 4; i++)\n dst[j * linesize + i] = v[bitstream_read_bit(bc)];\n for (i = 0; i < 4; i++)\n dst[(j + 1) * linesize + i] = v[bitstream_read_bit(bc)];\n }\n break;\n case 3:\n for (j = 0; j < 4; j += 2)\n for (i = 0; i < 4; i += 2)\n decode2x2(bc, dst + j * linesize + i, linesize);\n }\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline unsigned bitstream_read_bit(BitstreamContext *bc)\n{\n if (!bc->bits_left)\n refill_64(bc);\n return get_val(bc, 1);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
35,546
0
https://github.com/libav/libav/blob/556f8a066cb33241bf29e85d7e24c9acf7ea9043/libavcodec/h264.h/#L935
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.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(CHROMA444){ 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{ 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.ref_index[list]; int16_t (*mv_cache)[2] = &h->mv_cache[list][scan8[0]]; int16_t (*mv)[2] = s->current_picture.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)*0x01010101); } 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; } } } if(FRAME_MBAFF){ #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(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 #undef MAP_F2F }else{ #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.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 static const uint16_t mb_sizes[4] = {256,384,512,768};\n const int mb_size = mb_sizes[h->sps.chroma_format_idc]*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 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.qscale_table[mb_xy]= 0;\n memset(h->non_zero_count[mb_xy], 16, 48);\n s->current_picture.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 );\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 );\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.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 {\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.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 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.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.mb_type[top_xy - 1]>>7)&1)-1);\n topright_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy + 1]>>7)&1)-1);\n top_xy += s->mb_stride & (((s->current_picture.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.mb_type[topleft_xy] ;\n h->top_type = s->current_picture.mb_type[top_xy] ;\n h->topright_type= s->current_picture.mb_type[topright_xy];\n h->left_type[LTOP] = s->current_picture.mb_type[left_xy[LTOP]] ;\n h->left_type[LBOT] = s->current_picture.mb_type[left_xy[LBOT]] ;\n if(FMO){\n if(h->slice_table[topleft_xy ] != h->slice_num) h->topleft_type = 0;\n if(h->slice_table[top_xy ] != h->slice_num) h->top_type = 0;\n if(h->slice_table[left_xy[LTOP] ] != h->slice_num) 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) h->top_type = 0;\n if(h->slice_table[left_xy[LTOP] ] != h->slice_num) h->left_type[LTOP] = h->left_type[LBOT] = 0;\n }\n }\n if(h->slice_table[topright_xy] != h->slice_num) h->topright_type= 0;\n}', 'static void fill_decode_caches(H264Context *h, int mb_type){\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.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(CHROMA444){\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{\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 }\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.ref_index[list];\n int16_t (*mv_cache)[2] = &h->mv_cache[list][scan8[0]];\n int16_t (*mv)[2] = s->current_picture.motion_val[list];\n if(!USES_LIST(mb_type, list)){\n continue;\n }\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], ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101);\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 ], mv[b_xy + b_stride*left_block[0+i*2]]);\n AV_COPY32(mv_cache[cache_idx+8], 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 : 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 : 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 : 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 + (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 : 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], 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], 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 if(FRAME_MBAFF){\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(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#undef MAP_F2F\n }else{\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}']
35,547
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L352
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *A, *a = NULL; const BN_ULONG *B; int i; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b,BN_FLG_SECURE)) a = A = OPENSSL_secure_malloc(words * sizeof(*a)); else a = A = OPENSSL_malloc(words * sizeof(*a)); if (A == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } #ifdef PURIFY memset(a, 0, sizeof(*a) * words); #endif #if 1 B = b->d; if (B != NULL) { for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { BN_ULONG a0, a1, a2, a3; a0 = B[0]; a1 = B[1]; a2 = B[2]; a3 = B[3]; A[0] = a0; A[1] = a1; A[2] = a2; A[3] = a3; } switch (b->top & 3) { case 3: A[2] = B[2]; case 2: A[1] = B[1]; case 1: A[0] = B[0]; case 0: ; } } #else memset(A, 0, sizeof(*A) * words); memcpy(A, b->d, sizeof(b->d[0]) * b->top); #endif return (a); }
['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_pseudo_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}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return (ret);\n}', 'int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n BN_MONT_CTX *mont = NULL;\n int b, bits, ret = 0;\n int r_is_one;\n BN_ULONG w, next_w;\n BIGNUM *d, *r, *t;\n BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n (BN_mul_word(r, (w)) && \\\n ( \\\n (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return -1;\n }\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n if (m->top == 1)\n a %= m->d[0];\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n if (a == 0) {\n BN_zero(rr);\n ret = 1;\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (d == NULL || r == NULL || t == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n r_is_one = 1;\n w = a;\n for (b = bits - 2; b >= 0; b--) {\n next_w = w * w;\n if ((next_w / w) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = 1;\n }\n w = next_w;\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (BN_is_bit_set(p, b)) {\n next_w = w * a;\n if ((next_w / a) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = a;\n }\n w = next_w;\n }\n }\n if (w != 1) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n }\n if (r_is_one) {\n if (!BN_one(rr))\n goto err;\n } else {\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n }\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return (ret);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_malloc(words * sizeof(*a));\n else\n a = A = OPENSSL_malloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#ifdef PURIFY\n memset(a, 0, sizeof(*a) * words);\n#endif\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
35,548
0
https://github.com/openssl/openssl/blob/8ccf2ffbd6a98d3750b715787c80d5d2b76d054b/crypto/evp/exchange.c/#L194
int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, EVP_KEYEXCH *exchange) { int ret; void *provkey = NULL; ctx->operation = EVP_PKEY_OP_DERIVE; if (ctx->engine != NULL) goto legacy; if (exchange != NULL) { if (!EVP_KEYEXCH_up_ref(exchange)) goto err; } else { int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id; if (ctx->pkey != NULL) { switch (ctx->pkey->type) { case EVP_PKEY_DH: break; default: goto legacy; } exchange = EVP_KEYEXCH_fetch(NULL, OBJ_nid2sn(nid), NULL); } else { goto legacy; } if (exchange == NULL) { EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR); goto err; } } if (ctx->exchprovctx != NULL && ctx->exchange != NULL) ctx->exchange->freectx(ctx->exchprovctx); EVP_KEYEXCH_free(ctx->exchange); ctx->exchange = exchange; if (ctx->pkey != NULL) { provkey = evp_keymgmt_export_to_provider(ctx->pkey, exchange->keymgmt); if (provkey == NULL) { EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR); goto err; } } ctx->exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov)); if (ctx->exchprovctx == NULL) { EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR); goto err; } ret = exchange->init(ctx->exchprovctx, provkey); return ret ? 1 : 0; err: ctx->operation = EVP_PKEY_OP_UNDEFINED; return 0; legacy: if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) { EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); return -2; } if (ctx->pmeth->derive_init == NULL) return 1; ret = ctx->pmeth->derive_init(ctx); if (ret <= 0) ctx->operation = EVP_PKEY_OP_UNDEFINED; return ret; }
['int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, EVP_KEYEXCH *exchange)\n{\n int ret;\n void *provkey = NULL;\n ctx->operation = EVP_PKEY_OP_DERIVE;\n if (ctx->engine != NULL)\n goto legacy;\n if (exchange != NULL) {\n if (!EVP_KEYEXCH_up_ref(exchange))\n goto err;\n } else {\n int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;\n if (ctx->pkey != NULL) {\n switch (ctx->pkey->type) {\n case EVP_PKEY_DH:\n break;\n default:\n goto legacy;\n }\n exchange = EVP_KEYEXCH_fetch(NULL, OBJ_nid2sn(nid), NULL);\n } else {\n goto legacy;\n }\n if (exchange == NULL) {\n EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);\n goto err;\n }\n }\n if (ctx->exchprovctx != NULL && ctx->exchange != NULL)\n ctx->exchange->freectx(ctx->exchprovctx);\n EVP_KEYEXCH_free(ctx->exchange);\n ctx->exchange = exchange;\n if (ctx->pkey != NULL) {\n provkey = evp_keymgmt_export_to_provider(ctx->pkey, exchange->keymgmt);\n if (provkey == NULL) {\n EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);\n goto err;\n }\n }\n ctx->exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));\n if (ctx->exchprovctx == NULL) {\n EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);\n goto err;\n }\n ret = exchange->init(ctx->exchprovctx, provkey);\n return ret ? 1 : 0;\n err:\n ctx->operation = EVP_PKEY_OP_UNDEFINED;\n return 0;\n legacy:\n if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {\n EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX,\n EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);\n return -2;\n }\n if (ctx->pmeth->derive_init == NULL)\n return 1;\n ret = ctx->pmeth->derive_init(ctx);\n if (ret <= 0)\n ctx->operation = EVP_PKEY_OP_UNDEFINED;\n return ret;\n}', 'const char *OBJ_nid2sn(int n)\n{\n ADDED_OBJ ad, *adp;\n ASN1_OBJECT ob;\n if ((n >= 0) && (n < NUM_NID)) {\n if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {\n OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);\n return NULL;\n }\n return nid_objs[n].sn;\n } else if (added == NULL)\n return NULL;\n else {\n ad.type = ADDED_NID;\n ad.obj = &ob;\n ob.nid = n;\n adp = lh_ADDED_OBJ_retrieve(added, &ad);\n if (adp != NULL)\n return adp->obj->sn;\n else {\n OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);\n return NULL;\n }\n }\n}', 'void ERR_put_error(int lib, int func, int reason, const char *file, int line)\n{\n c_put_error(NULL, ERR_PACK(lib, func, reason), file, line);\n ERR_add_error_data(1, "(in the FIPS module)");\n}']
35,549
0
https://gitlab.com/libtiff/libtiff/blob/d85a64b6d6379c9f9b8de6ff3a26e380b0fc3e18/libtiff/tif_strip.c/#L67
uint32 TIFFNumberOfStrips(TIFF* tif) { TIFFDirectory *td = &tif->tif_dir; uint32 nstrips; nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 : TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip)); if (td->td_planarconfig == PLANARCONFIG_SEPARATE) nstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel, "TIFFNumberOfStrips"); return (nstrips); }
['tsize_t t2p_write_pdf(T2P* t2p, TIFF* input, TIFF* output){\n\ttsize_t written=0;\n\tttile_t i2=0;\n\ttsize_t streamlen=0;\n\tuint16 i=0;\n\tt2p_read_tiff_init(t2p, input);\n\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\tt2p->pdf_xrefoffsets= (uint32*) _TIFFmalloc(t2p->pdf_xrefcount * sizeof(uint32) );\n\tif(t2p->pdf_xrefoffsets==NULL){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"Can\'t allocate %u bytes of memory for t2p_write_pdf",\n\t\t\t(unsigned int) (t2p->pdf_xrefcount * sizeof(uint32)) );\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn(written);\n\t}\n\tt2p->pdf_xrefcount=0;\n\tt2p->pdf_catalog=1;\n\tt2p->pdf_info=2;\n\tt2p->pdf_pages=3;\n\twritten += t2p_write_pdf_header(t2p, output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_catalog=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_catalog(t2p, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_info=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_info(t2p, input, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_pages=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_pages(t2p, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tfor(t2p->pdf_page=0;t2p->pdf_page<t2p->tiff_pagecount;t2p->pdf_page++){\n\t\tt2p_read_tiff_data(t2p, input);\n\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_page(t2p->pdf_xrefcount, t2p, output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\twritten += t2p_write_pdf_stream_dict(0, t2p->pdf_xrefcount+1, output);\n\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\twritten += t2p_write_pdf_stream_start(output);\n\t\tstreamlen=written;\n\t\twritten += t2p_write_pdf_page_content_stream(t2p, output);\n\t\tstreamlen=written-streamlen;\n\t\twritten += t2p_write_pdf_stream_end(output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tif(t2p->tiff_transferfunctioncount != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_transfer(t2p, output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\tfor(i=0; i < t2p->tiff_transferfunctioncount; i++){\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\t\twritten += t2p_write_pdf_transfer_dict(t2p, output, i);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\t\tstreamlen=written;\n\t\t\t\twritten += t2p_write_pdf_transfer_stream(t2p, output, i);\n\t\t\t\tstreamlen=written-streamlen;\n\t\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t}\n\t\t}\n\t\tif( (t2p->pdf_colorspace & T2P_CS_PALETTE) != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\tt2p->pdf_palettecs=t2p->pdf_xrefcount;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_stream_dict(t2p->pdf_palettesize, 0, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\tstreamlen=written;\n\t\t\twritten += t2p_write_pdf_xobject_palettecs_stream(t2p, output);\n\t\t\tstreamlen=written-streamlen;\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t\tif( (t2p->pdf_colorspace & T2P_CS_ICCBASED) != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\tt2p->pdf_icccs=t2p->pdf_xrefcount;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_icccs_dict(t2p, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\tstreamlen=written;\n\t\t\twritten += t2p_write_pdf_xobject_icccs_stream(t2p, output);\n\t\t\tstreamlen=written-streamlen;\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t\tif(t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount !=0){\n\t\t\tfor(i2=0;i2<t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount;i2++){\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\t\twritten += t2p_write_pdf_xobject_stream_dict(\n\t\t\t\t\ti2+1,\n\t\t\t\t\tt2p,\n\t\t\t\t\toutput);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\t\tstreamlen=written;\n\t\t\t\tt2p_read_tiff_size_tile(t2p, input, i2);\n\t\t\t\twritten += t2p_readwrite_pdf_image_tile(t2p, input, output, i2);\n\t\t\t\tt2p_write_advance_directory(t2p, output);\n\t\t\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\t\t\tstreamlen=written-streamlen;\n\t\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t}\n\t\t} else {\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_stream_dict(\n\t\t\t\t0,\n\t\t\t\tt2p,\n\t\t\t\toutput);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\tstreamlen=written;\n\t\t\tt2p_read_tiff_size(t2p, input);\n\t\t\twritten += t2p_readwrite_pdf_image(t2p, input, output);\n\t\t\tt2p_write_advance_directory(t2p, output);\n\t\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\t\tstreamlen=written-streamlen;\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t}\n\tt2p->pdf_startxref = written;\n\twritten += t2p_write_pdf_xreftable(t2p, output);\n\twritten += t2p_write_pdf_trailer(t2p, output);\n\tt2p_disable(output);\n\treturn(written);\n}', 'void t2p_read_tiff_data(T2P* t2p, TIFF* input){\n\tint i=0;\n\tuint16* r;\n\tuint16* g;\n\tuint16* b;\n\tuint16* a;\n\tuint16 xuint16;\n\tuint16* xuint16p;\n\tfloat* xfloatp;\n\tt2p->pdf_transcode = T2P_TRANSCODE_ENCODE;\n\tt2p->pdf_sample = T2P_SAMPLE_NOTHING;\n t2p->pdf_switchdecode = t2p->pdf_colorspace_invert;\n\tTIFFSetDirectory(input, t2p->tiff_pages[t2p->pdf_page].page_directory);\n\tTIFFGetField(input, TIFFTAG_IMAGEWIDTH, &(t2p->tiff_width));\n\tif(t2p->tiff_width == 0){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"No support for %s with zero width",\n\t\t\tTIFFFileName(input)\t);\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn;\n\t}\n\tTIFFGetField(input, TIFFTAG_IMAGELENGTH, &(t2p->tiff_length));\n\tif(t2p->tiff_length == 0){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"No support for %s with zero length",\n\t\t\tTIFFFileName(input)\t);\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn;\n\t}\n if(TIFFGetField(input, TIFFTAG_COMPRESSION, &(t2p->tiff_compression)) == 0){\n TIFFError(\n TIFF2PDF_MODULE,\n "No support for %s with no compression tag",\n TIFFFileName(input) );\n t2p->t2p_error = T2P_ERR_ERROR;\n return;\n }\n if( TIFFIsCODECConfigured(t2p->tiff_compression) == 0){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"No support for %s with compression type %u: not configured",\n\t\t\tTIFFFileName(input),\n\t\t\tt2p->tiff_compression\n\t\t\t);\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn;\n\t}\n\tTIFFGetFieldDefaulted(input, TIFFTAG_BITSPERSAMPLE, &(t2p->tiff_bitspersample));\n\tswitch(t2p->tiff_bitspersample){\n\t\tcase 1:\n\t\tcase 2:\n\t\tcase 4:\n\t\tcase 8:\n\t\t\tbreak;\n\t\tcase 0:\n\t\t\tTIFFWarning(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"Image %s has 0 bits per sample, assuming 1",\n\t\t\t\tTIFFFileName(input));\n\t\t\tt2p->tiff_bitspersample=1;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tTIFFError(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"No support for %s with %u bits per sample",\n\t\t\t\tTIFFFileName(input),\n\t\t\t\tt2p->tiff_bitspersample);\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn;\n\t}\n\tTIFFGetFieldDefaulted(input, TIFFTAG_SAMPLESPERPIXEL, &(t2p->tiff_samplesperpixel));\n\tif(t2p->tiff_samplesperpixel>4){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"No support for %s with %u samples per pixel",\n\t\t\tTIFFFileName(input),\n\t\t\tt2p->tiff_samplesperpixel);\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn;\n\t}\n\tif(t2p->tiff_samplesperpixel==0){\n\t\tTIFFWarning(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"Image %s has 0 samples per pixel, assuming 1",\n\t\t\tTIFFFileName(input));\n\t\tt2p->tiff_samplesperpixel=1;\n\t}\n\tif(TIFFGetField(input, TIFFTAG_SAMPLEFORMAT, &xuint16) != 0 ){\n\t\tswitch(xuint16){\n\t\t\tcase 0:\n\t\t\tcase 1:\n\t\t\tcase 4:\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"No support for %s with sample format %u",\n\t\t\t\t\tTIFFFileName(input),\n\t\t\t\t\txuint16);\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t\tbreak;\n\t\t}\n\t}\n\tTIFFGetFieldDefaulted(input, TIFFTAG_FILLORDER, &(t2p->tiff_fillorder));\n if(TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &(t2p->tiff_photometric)) == 0){\n TIFFError(\n TIFF2PDF_MODULE,\n "No support for %s with no photometric interpretation tag",\n TIFFFileName(input) );\n t2p->t2p_error = T2P_ERR_ERROR;\n return;\n }\n\tswitch(t2p->tiff_photometric){\n\t\tcase PHOTOMETRIC_MINISWHITE:\n\t\tcase PHOTOMETRIC_MINISBLACK:\n\t\t\tif (t2p->tiff_bitspersample==1){\n\t\t\t\tt2p->pdf_colorspace=T2P_CS_BILEVEL;\n\t\t\t\tif(t2p->tiff_photometric==PHOTOMETRIC_MINISWHITE){\n\t\t\t\t\tt2p->pdf_switchdecode ^= 1;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tt2p->pdf_colorspace=T2P_CS_GRAY;\n\t\t\t\tif(t2p->tiff_photometric==PHOTOMETRIC_MINISWHITE){\n\t\t\t\t\tt2p->pdf_switchdecode ^= 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_RGB:\n\t\t\tt2p->pdf_colorspace=T2P_CS_RGB;\n\t\t\tif(t2p->tiff_samplesperpixel == 3){\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif(TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)){\n\t\t\t\tif(xuint16==1)\n\t\t\t\t\tgoto photometric_palette;\n\t\t\t}\n\t\t\tif(t2p->tiff_samplesperpixel > 3) {\n\t\t\t\tif(t2p->tiff_samplesperpixel == 4) {\n\t\t\t\t\tt2p->pdf_colorspace = T2P_CS_RGB;\n\t\t\t\t\tif(TIFFGetField(input,\n\t\t\t\t\t\t\tTIFFTAG_EXTRASAMPLES,\n\t\t\t\t\t\t\t&xuint16, &xuint16p)\n\t\t\t\t\t && xuint16 == 1) {\n\t\t\t\t\t\tif(xuint16p[0] == EXTRASAMPLE_ASSOCALPHA){\n\t\t\t\t\t\t\tt2p->pdf_sample=T2P_SAMPLE_RGBAA_TO_RGB;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(xuint16p[0] == EXTRASAMPLE_UNASSALPHA){\n\t\t\t\t\t\t\tt2p->pdf_sample=T2P_SAMPLE_RGBA_TO_RGB;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tTIFFWarning(\n\t\t\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t\t\t"RGB image %s has 4 samples per pixel, assuming RGBA",\n\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tt2p->pdf_colorspace=T2P_CS_CMYK;\n\t\t\t\t\tt2p->pdf_switchdecode ^= 1;\n\t\t\t\t\tTIFFWarning(\n\t\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t\t"RGB image %s has 4 samples per pixel, assuming inverse CMYK",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\tbreak;\n\t\t\t\t} else {\n\t\t\t\t\tTIFFError(\n\t\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t\t"No support for RGB image %s with %u samples per pixel",\n\t\t\t\t\t\tTIFFFileName(input),\n\t\t\t\t\t\tt2p->tiff_samplesperpixel);\n\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"No support for RGB image %s with %u samples per pixel",\n\t\t\t\t\tTIFFFileName(input),\n\t\t\t\t\tt2p->tiff_samplesperpixel);\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\tbreak;\n\t\t\t}\n\t\tcase PHOTOMETRIC_PALETTE:\n\t\t\tphotometric_palette:\n\t\t\tif(t2p->tiff_samplesperpixel!=1){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"No support for palettized image %s with not one sample per pixel",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tt2p->pdf_colorspace=T2P_CS_RGB | T2P_CS_PALETTE;\n\t\t\tt2p->pdf_palettesize=0x0001<<t2p->tiff_bitspersample;\n\t\t\tif(!TIFFGetField(input, TIFFTAG_COLORMAP, &r, &g, &b)){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Palettized image %s has no color map",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif(t2p->pdf_palette != NULL){\n\t\t\t\t_TIFFfree(t2p->pdf_palette);\n\t\t\t\tt2p->pdf_palette=NULL;\n\t\t\t}\n\t\t\tt2p->pdf_palette = (unsigned char*)\n\t\t\t\t_TIFFmalloc(t2p->pdf_palettesize*3);\n\t\t\tif(t2p->pdf_palette==NULL){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %u bytes of memory for t2p_read_tiff_image, %s",\n\t\t\t\t\tt2p->pdf_palettesize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor(i=0;i<t2p->pdf_palettesize;i++){\n\t\t\t\tt2p->pdf_palette[(i*3)] = (unsigned char) (r[i]>>8);\n\t\t\t\tt2p->pdf_palette[(i*3)+1]= (unsigned char) (g[i]>>8);\n\t\t\t\tt2p->pdf_palette[(i*3)+2]= (unsigned char) (b[i]>>8);\n\t\t\t}\n\t\t\tt2p->pdf_palettesize *= 3;\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_SEPARATED:\n\t\t\tif(TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)){\n\t\t\t\tif(xuint16==1){\n\t\t\t\t\t\tgoto photometric_palette_cmyk;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif( TIFFGetField(input, TIFFTAG_INKSET, &xuint16) ){\n\t\t\t\tif(xuint16 != INKSET_CMYK){\n\t\t\t\t\tTIFFError(\n\t\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t\t"No support for %s because its inkset is not CMYK",\n\t\t\t\t\t\tTIFFFileName(input) );\n\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(t2p->tiff_samplesperpixel==4){\n\t\t\t\tt2p->pdf_colorspace=T2P_CS_CMYK;\n\t\t\t} else {\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"No support for %s because it has %u samples per pixel",\n\t\t\t\t\tTIFFFileName(input),\n\t\t\t\t\tt2p->tiff_samplesperpixel);\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tbreak;\n\t\t\tphotometric_palette_cmyk:\n\t\t\tif(t2p->tiff_samplesperpixel!=1){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"No support for palettized CMYK image %s with not one sample per pixel",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tt2p->pdf_colorspace=T2P_CS_CMYK | T2P_CS_PALETTE;\n\t\t\tt2p->pdf_palettesize=0x0001<<t2p->tiff_bitspersample;\n\t\t\tif(!TIFFGetField(input, TIFFTAG_COLORMAP, &r, &g, &b, &a)){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Palettized image %s has no color map",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif(t2p->pdf_palette != NULL){\n\t\t\t\t_TIFFfree(t2p->pdf_palette);\n\t\t\t\tt2p->pdf_palette=NULL;\n\t\t\t}\n\t\t\tt2p->pdf_palette = (unsigned char*)\n\t\t\t\t_TIFFmalloc(t2p->pdf_palettesize*4);\n\t\t\tif(t2p->pdf_palette==NULL){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %u bytes of memory for t2p_read_tiff_image, %s",\n\t\t\t\t\tt2p->pdf_palettesize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor(i=0;i<t2p->pdf_palettesize;i++){\n\t\t\t\tt2p->pdf_palette[(i*4)] = (unsigned char) (r[i]>>8);\n\t\t\t\tt2p->pdf_palette[(i*4)+1]= (unsigned char) (g[i]>>8);\n\t\t\t\tt2p->pdf_palette[(i*4)+2]= (unsigned char) (b[i]>>8);\n\t\t\t\tt2p->pdf_palette[(i*4)+3]= (unsigned char) (a[i]>>8);\n\t\t\t}\n\t\t\tt2p->pdf_palettesize *= 4;\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_YCBCR:\n\t\t\tt2p->pdf_colorspace=T2P_CS_RGB;\n\t\t\tif(t2p->tiff_samplesperpixel==1){\n\t\t\t\tt2p->pdf_colorspace=T2P_CS_GRAY;\n\t\t\t\tt2p->tiff_photometric=PHOTOMETRIC_MINISBLACK;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tt2p->pdf_sample=T2P_SAMPLE_YCBCR_TO_RGB;\n#ifdef JPEG_SUPPORT\n\t\t\tif(t2p->pdf_defaultcompression==T2P_COMPRESS_JPEG){\n\t\t\t\tt2p->pdf_sample=T2P_SAMPLE_NOTHING;\n\t\t\t}\n#endif\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_CIELAB:\n\t\t\tt2p->pdf_labrange[0]= -127;\n\t\t\tt2p->pdf_labrange[1]= 127;\n\t\t\tt2p->pdf_labrange[2]= -127;\n\t\t\tt2p->pdf_labrange[3]= 127;\n\t\t\tt2p->pdf_sample=T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED;\n\t\t\tt2p->pdf_colorspace=T2P_CS_LAB;\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_ICCLAB:\n\t\t\tt2p->pdf_labrange[0]= 0;\n\t\t\tt2p->pdf_labrange[1]= 255;\n\t\t\tt2p->pdf_labrange[2]= 0;\n\t\t\tt2p->pdf_labrange[3]= 255;\n\t\t\tt2p->pdf_colorspace=T2P_CS_LAB;\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_ITULAB:\n\t\t\tt2p->pdf_labrange[0]=-85;\n\t\t\tt2p->pdf_labrange[1]=85;\n\t\t\tt2p->pdf_labrange[2]=-75;\n\t\t\tt2p->pdf_labrange[3]=124;\n\t\t\tt2p->pdf_sample=T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED;\n\t\t\tt2p->pdf_colorspace=T2P_CS_LAB;\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_LOGL:\n\t\tcase PHOTOMETRIC_LOGLUV:\n\t\t\tTIFFError(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"No support for %s with photometric interpretation LogL/LogLuv",\n\t\t\t\tTIFFFileName(input));\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn;\n\t\tdefault:\n\t\t\tTIFFError(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"No support for %s with photometric interpretation %u",\n\t\t\t\tTIFFFileName(input),\n\t\t\t\tt2p->tiff_photometric);\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn;\n\t}\n\tif(TIFFGetField(input, TIFFTAG_PLANARCONFIG, &(t2p->tiff_planar))){\n\t\tswitch(t2p->tiff_planar){\n\t\t\tcase 0:\n\t\t\t\tTIFFWarning(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Image %s has planar configuration 0, assuming 1",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->tiff_planar=PLANARCONFIG_CONTIG;\n\t\t\tcase PLANARCONFIG_CONTIG:\n\t\t\t\tbreak;\n\t\t\tcase PLANARCONFIG_SEPARATE:\n\t\t\t\tt2p->pdf_sample=T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG;\n\t\t\t\tif(t2p->tiff_bitspersample!=8){\n\t\t\t\t\tTIFFError(\n\t\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t\t"No support for %s with separated planar configuration and %u bits per sample",\n\t\t\t\t\t\tTIFFFileName(input),\n\t\t\t\t\t\tt2p->tiff_bitspersample);\n\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"No support for %s with planar configuration %u",\n\t\t\t\t\tTIFFFileName(input),\n\t\t\t\t\tt2p->tiff_planar);\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t}\n\t}\n TIFFGetFieldDefaulted(input, TIFFTAG_ORIENTATION,\n &(t2p->tiff_orientation));\n if(t2p->tiff_orientation>8){\n TIFFWarning(TIFF2PDF_MODULE,\n "Image %s has orientation %u, assuming 0",\n TIFFFileName(input), t2p->tiff_orientation);\n t2p->tiff_orientation=0;\n }\n if(TIFFGetField(input, TIFFTAG_XRESOLUTION, &(t2p->tiff_xres) ) == 0){\n t2p->tiff_xres=0.0;\n }\n if(TIFFGetField(input, TIFFTAG_YRESOLUTION, &(t2p->tiff_yres) ) == 0){\n t2p->tiff_yres=0.0;\n }\n\tTIFFGetFieldDefaulted(input, TIFFTAG_RESOLUTIONUNIT,\n\t\t\t &(t2p->tiff_resunit));\n\tif(t2p->tiff_resunit == RESUNIT_CENTIMETER) {\n\t\tt2p->tiff_xres *= 2.54F;\n\t\tt2p->tiff_yres *= 2.54F;\n\t} else if (t2p->tiff_resunit != RESUNIT_INCH\n\t\t && t2p->pdf_centimeters != 0) {\n\t\tt2p->tiff_xres *= 2.54F;\n\t\tt2p->tiff_yres *= 2.54F;\n\t}\n\tt2p_compose_pdf_page(t2p);\n\tt2p->pdf_transcode = T2P_TRANSCODE_ENCODE;\n\tif(t2p->pdf_nopassthrough==0){\n#ifdef CCITT_SUPPORT\n\t\tif(t2p->tiff_compression==COMPRESSION_CCITTFAX4\n\t\t\t){\n\t\t\tif(TIFFIsTiled(input) || (TIFFNumberOfStrips(input)==1) ){\n\t\t\t\tt2p->pdf_transcode = T2P_TRANSCODE_RAW;\n\t\t\t\tt2p->pdf_compression=T2P_COMPRESS_G4;\n\t\t\t}\n\t\t}\n#endif\n#ifdef ZIP_SUPPORT\n\t\tif(t2p->tiff_compression== COMPRESSION_ADOBE_DEFLATE\n\t\t\t|| t2p->tiff_compression==COMPRESSION_DEFLATE){\n\t\t\tif(TIFFIsTiled(input) || (TIFFNumberOfStrips(input)==1) ){\n\t\t\t\tt2p->pdf_transcode = T2P_TRANSCODE_RAW;\n\t\t\t\tt2p->pdf_compression=T2P_COMPRESS_ZIP;\n\t\t\t}\n\t\t}\n#endif\n#ifdef OJPEG_SUPPORT\n\t\tif(t2p->tiff_compression==COMPRESSION_OJPEG){\n\t\t\tt2p->pdf_transcode = T2P_TRANSCODE_RAW;\n\t\t\tt2p->pdf_compression=T2P_COMPRESS_JPEG;\n\t\t\tt2p_process_ojpeg_tables(t2p, input);\n\t\t}\n#endif\n#ifdef JPEG_SUPPORT\n\t\tif(t2p->tiff_compression==COMPRESSION_JPEG){\n\t\t\tt2p->pdf_transcode = T2P_TRANSCODE_RAW;\n\t\t\tt2p->pdf_compression=T2P_COMPRESS_JPEG;\n\t\t}\n#endif\n\t\t(void)0;\n\t}\n\tif(t2p->pdf_transcode!=T2P_TRANSCODE_RAW){\n\t\tt2p->pdf_compression = t2p->pdf_defaultcompression;\n\t}\n#ifdef JPEG_SUPPORT\n\tif(t2p->pdf_defaultcompression==T2P_COMPRESS_JPEG){\n\t\tif(t2p->pdf_colorspace & T2P_CS_PALETTE){\n\t\t\tt2p->pdf_sample|=T2P_SAMPLE_REALIZE_PALETTE;\n\t\t\tt2p->pdf_colorspace ^= T2P_CS_PALETTE;\n\t\t\tt2p->tiff_pages[t2p->pdf_page].page_extra--;\n\t\t}\n\t}\n\tif(t2p->tiff_compression==COMPRESSION_JPEG){\n\t\tif(t2p->tiff_planar==PLANARCONFIG_SEPARATE){\n\t\t\tTIFFError(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"No support for %s with JPEG compression and separated planar configuration",\n\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\treturn;\n\t\t}\n\t}\n#endif\n#ifdef OJPEG_SUPPORT\n\tif(t2p->tiff_compression==COMPRESSION_OJPEG){\n\t\tif(t2p->tiff_planar==PLANARCONFIG_SEPARATE){\n\t\t\tTIFFError(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"No support for %s with OJPEG compression and separated planar configuration",\n\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\treturn;\n\t\t}\n\t}\n#endif\n\tif(t2p->pdf_sample & T2P_SAMPLE_REALIZE_PALETTE){\n\t\tif(t2p->pdf_colorspace & T2P_CS_CMYK){\n\t\t\tt2p->tiff_samplesperpixel=4;\n\t\t\tt2p->tiff_photometric=PHOTOMETRIC_SEPARATED;\n\t\t} else {\n\t\t\tt2p->tiff_samplesperpixel=3;\n\t\t\tt2p->tiff_photometric=PHOTOMETRIC_RGB;\n\t\t}\n\t}\n\tif (TIFFGetField(input, TIFFTAG_TRANSFERFUNCTION,\n\t\t\t &(t2p->tiff_transferfunction[0]),\n\t\t\t &(t2p->tiff_transferfunction[1]),\n\t\t\t &(t2p->tiff_transferfunction[2]))) {\n\t\tif(t2p->tiff_transferfunction[1] !=\n\t\t t2p->tiff_transferfunction[0]) {\n\t\t\tt2p->tiff_transferfunctioncount=3;\n\t\t} else {\n\t\t\tt2p->tiff_transferfunctioncount=1;\n\t\t}\n\t} else {\n\t\tt2p->tiff_transferfunctioncount=0;\n\t}\n\tif(TIFFGetField(input, TIFFTAG_WHITEPOINT, &xfloatp)!=0){\n\t\tt2p->tiff_whitechromaticities[0]=xfloatp[0];\n\t\tt2p->tiff_whitechromaticities[1]=xfloatp[1];\n\t\tif(t2p->pdf_colorspace & T2P_CS_GRAY){\n\t\t\tt2p->pdf_colorspace |= T2P_CS_CALGRAY;\n\t\t}\n\t\tif(t2p->pdf_colorspace & T2P_CS_RGB){\n\t\t\tt2p->pdf_colorspace |= T2P_CS_CALRGB;\n\t\t}\n\t}\n\tif(TIFFGetField(input, TIFFTAG_PRIMARYCHROMATICITIES, &xfloatp)!=0){\n\t\tt2p->tiff_primarychromaticities[0]=xfloatp[0];\n\t\tt2p->tiff_primarychromaticities[1]=xfloatp[1];\n\t\tt2p->tiff_primarychromaticities[2]=xfloatp[2];\n\t\tt2p->tiff_primarychromaticities[3]=xfloatp[3];\n\t\tt2p->tiff_primarychromaticities[4]=xfloatp[4];\n\t\tt2p->tiff_primarychromaticities[5]=xfloatp[5];\n\t\tif(t2p->pdf_colorspace & T2P_CS_RGB){\n\t\t\tt2p->pdf_colorspace |= T2P_CS_CALRGB;\n\t\t}\n\t}\n\tif(t2p->pdf_colorspace & T2P_CS_LAB){\n\t\tif(TIFFGetField(input, TIFFTAG_WHITEPOINT, &xfloatp) != 0){\n\t\t\tt2p->tiff_whitechromaticities[0]=xfloatp[0];\n\t\t\tt2p->tiff_whitechromaticities[1]=xfloatp[1];\n\t\t} else {\n\t\t\tt2p->tiff_whitechromaticities[0]=0.3457F;\n\t\t\tt2p->tiff_whitechromaticities[1]=0.3585F;\n\t\t}\n\t}\n\tif(TIFFGetField(input,\n\t\tTIFFTAG_ICCPROFILE,\n\t\t&(t2p->tiff_iccprofilelength),\n\t\t&(t2p->tiff_iccprofile))!=0){\n\t\tt2p->pdf_colorspace |= T2P_CS_ICCBASED;\n\t} else {\n\t\tt2p->tiff_iccprofilelength=0;\n\t\tt2p->tiff_iccprofile=NULL;\n\t}\n#ifdef CCITT_SUPPORT\n\tif( t2p->tiff_bitspersample==1 &&\n\t\tt2p->tiff_samplesperpixel==1){\n\t\tt2p->pdf_compression = T2P_COMPRESS_G4;\n\t}\n#endif\n\treturn;\n}', 'int\nTIFFSetDirectory(TIFF* tif, uint16 dirn)\n{\n\tuint64 nextdir;\n\tuint16 n;\n\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\tnextdir = tif->tif_header.classic.tiff_diroff;\n\telse\n\t\tnextdir = tif->tif_header.big.tiff_diroff;\n\tfor (n = dirn; n > 0 && nextdir != 0; n--)\n\t\tif (!TIFFAdvanceDirectory(tif, &nextdir, NULL))\n\t\t\treturn (0);\n\ttif->tif_nextdiroff = nextdir;\n\ttif->tif_curdir = (dirn - n) - 1;\n\ttif->tif_dirnumber = 0;\n\treturn (TIFFReadDirectory(tif));\n}', 'void t2p_read_tiff_size(T2P* t2p, TIFF* input){\n\tuint64* sbc=NULL;\n#if defined(JPEG_SUPPORT) || defined (OJPEG_SUPPORT)\n\tunsigned char* jpt=NULL;\n\ttstrip_t i=0;\n\ttstrip_t stripcount=0;\n#endif\n uint64 k = 0;\n\tif(t2p->pdf_transcode == T2P_TRANSCODE_RAW){\n#ifdef CCITT_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_G4 ){\n\t\t\tTIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);\n\t\t\tt2p->tiff_datasize=(tmsize_t)sbc[0];\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef ZIP_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_ZIP){\n\t\t\tTIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);\n\t\t\tt2p->tiff_datasize=(tmsize_t)sbc[0];\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef OJPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_OJPEG){\n\t\t\tif(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tk = checkAdd64(k, sbc[i], t2p);\n\t\t\t}\n\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGIFOFFSET, &(t2p->tiff_dataoffset))){\n\t\t\t\tif(t2p->tiff_dataoffset != 0){\n\t\t\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGIFBYTECOUNT, &(t2p->tiff_datasize))!=0){\n\t\t\t\t\t\tif((uint64)t2p->tiff_datasize < k) {\n\t\t\t\t\t\t\tTIFFWarning(TIFF2PDF_MODULE,\n\t\t\t\t\t\t\t\t"Input file %s has short JPEG interchange file byte count",\n\t\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\tt2p->pdf_ojpegiflength=t2p->tiff_datasize;\n\t\t\t\t\t\t\tk = checkAdd64(k, t2p->tiff_datasize, t2p);\n\t\t\t\t\t\t\tk = checkAdd64(k, 6, t2p);\n\t\t\t\t\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\t\t\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\t\t\t\t\tt2p->tiff_datasize = (tsize_t) k;\n\t\t\t\t\t\t\tif ((uint64) t2p->tiff_datasize != k) {\n\t\t\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\t\t\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}else {\n\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t\t\t"Input file %s missing field: TIFFTAG_JPEGIFBYTECOUNT",\n\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\tk = checkAdd64(k, 2048, t2p);\n\t\t\tt2p->tiff_datasize = (tsize_t) k;\n\t\t\tif ((uint64) t2p->tiff_datasize != k) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t}\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef JPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_JPEG) {\n\t\t\tuint32 count = 0;\n\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0 ){\n\t\t\t\tif(count > 4){\n\t\t\t\t\tk += count;\n\t\t\t\t\tk -= 2;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tk = 2;\n\t\t\t}\n\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\tif(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tk = checkAdd64(k, sbc[i], t2p);\n\t\t\t\tk -=4;\n\t\t\t}\n\t\t\tk = checkAdd64(k, 2, t2p);\n\t\t\tt2p->tiff_datasize = (tsize_t) k;\n\t\t\tif ((uint64) t2p->tiff_datasize != k) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t}\n\t\t\treturn;\n\t\t}\n#endif\n\t\t(void) 0;\n\t}\n\tk = checkMultiply64(TIFFScanlineSize(input), t2p->tiff_length, t2p);\n\tif(t2p->tiff_planar==PLANARCONFIG_SEPARATE){\n\t\tk = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p);\n\t}\n\tif (k == 0) {\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t}\n\tt2p->tiff_datasize = (tsize_t) k;\n\tif ((uint64) t2p->tiff_datasize != k) {\n\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t}\n\treturn;\n}', 'uint32\nTIFFNumberOfStrips(TIFF* tif)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 nstrips;\n\tnstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :\n\t TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\tnstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,\n\t\t "TIFFNumberOfStrips");\n\treturn (nstrips);\n}']
35,550
0
https://github.com/openssl/openssl/blob/16bce0e08b16b28a1953795bde3f913957b08ef2/ssl/t1_lib.c/#L3996
DH *ssl_get_auto_dh(SSL *s) { int dh_secbits = 80; if (s->cert->dh_tmp_auto == 2) return DH_get_1024_160(); if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { if (s->s3->tmp.new_cipher->strength_bits == 256) dh_secbits = 128; else dh_secbits = 80; } else { CERT_PKEY *cpk = ssl_get_server_send_pkey(s); dh_secbits = EVP_PKEY_security_bits(cpk->privatekey); } if (dh_secbits >= 128) { DH *dhp = DH_new(); BIGNUM *p, *g; if (dhp == NULL) return NULL; g = BN_new(); if (g != NULL) BN_set_word(g, 2); if (dh_secbits >= 192) p = BN_get_rfc3526_prime_8192(NULL); else p = BN_get_rfc3526_prime_3072(NULL); if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) { DH_free(dhp); BN_free(p); BN_free(g); return NULL; } return dhp; } if (dh_secbits >= 112) return DH_get_2048_224(); return DH_get_1024_160(); }
['DH *ssl_get_auto_dh(SSL *s)\n{\n int dh_secbits = 80;\n if (s->cert->dh_tmp_auto == 2)\n return DH_get_1024_160();\n if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {\n if (s->s3->tmp.new_cipher->strength_bits == 256)\n dh_secbits = 128;\n else\n dh_secbits = 80;\n } else {\n CERT_PKEY *cpk = ssl_get_server_send_pkey(s);\n dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);\n }\n if (dh_secbits >= 128) {\n DH *dhp = DH_new();\n BIGNUM *p, *g;\n if (dhp == NULL)\n return NULL;\n g = BN_new();\n if (g != NULL)\n BN_set_word(g, 2);\n if (dh_secbits >= 192)\n p = BN_get_rfc3526_prime_8192(NULL);\n else\n p = BN_get_rfc3526_prime_3072(NULL);\n if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {\n DH_free(dhp);\n BN_free(p);\n BN_free(g);\n return NULL;\n }\n return dhp;\n }\n if (dh_secbits >= 112)\n return DH_get_2048_224();\n return DH_get_1024_160();\n}', 'CERT_PKEY *ssl_get_server_send_pkey(SSL *s)\n{\n CERT *c;\n int i;\n c = s->cert;\n if (!s->s3 || !s->s3->tmp.new_cipher)\n return NULL;\n ssl_set_masks(s);\n i = ssl_get_server_cert_index(s);\n if (i < 0)\n return NULL;\n return &c->pkeys[i];\n}', 'static int ssl_get_server_cert_index(const SSL *s)\n{\n int idx;\n idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);\n if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)\n idx = SSL_PKEY_RSA_SIGN;\n if (idx == SSL_PKEY_GOST_EC) {\n if (s->cert->pkeys[SSL_PKEY_GOST12_512].x509)\n idx = SSL_PKEY_GOST12_512;\n else if (s->cert->pkeys[SSL_PKEY_GOST12_256].x509)\n idx = SSL_PKEY_GOST12_256;\n else if (s->cert->pkeys[SSL_PKEY_GOST01].x509)\n idx = SSL_PKEY_GOST01;\n else\n idx = -1;\n }\n if (idx == -1)\n SSLerr(SSL_F_SSL_GET_SERVER_CERT_INDEX, ERR_R_INTERNAL_ERROR);\n return idx;\n}', 'int ssl_cipher_get_cert_index(const SSL_CIPHER *c)\n{\n uint32_t alg_a;\n alg_a = c->algorithm_auth;\n if (alg_a & SSL_aECDSA)\n return SSL_PKEY_ECC;\n else if (alg_a & SSL_aDSS)\n return SSL_PKEY_DSA_SIGN;\n else if (alg_a & SSL_aRSA)\n return SSL_PKEY_RSA_ENC;\n else if (alg_a & SSL_aGOST12)\n return SSL_PKEY_GOST_EC;\n else if (alg_a & SSL_aGOST01)\n return SSL_PKEY_GOST01;\n return -1;\n}']
35,551
0
https://github.com/openssl/openssl/blob/141d7325e7e513102fb8c123dab62dbbd61e846d/test/evp_extra_test.c/#L263
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; md_ctx = EVP_MD_CTX_new(); md_ctx_verify = EVP_MD_CTX_new(); if (md_ctx == NULL || md_ctx_verify == NULL) goto out; pkey = load_example_rsa_key(); if (pkey == NULL || !EVP_DigestSignInit(md_ctx, NULL, EVP_sha256(), NULL, pkey) || !EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))) { goto out; } if (!EVP_DigestSignFinal(md_ctx, NULL, &sig_len)) { goto out; } if (sig_len != (size_t)EVP_PKEY_size(pkey)) { fprintf(stderr, "sig_len mismatch\n"); goto out; } sig = OPENSSL_malloc(sig_len); if (sig == NULL || !EVP_DigestSignFinal(md_ctx, sig, &sig_len)) { goto out; } if (!EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sha256(), NULL, pkey) || !EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg)) || !EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)) { goto out; } ret = 1; out: if (!ret) { ERR_print_errors_fp(stderr); } 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;\n md_ctx = EVP_MD_CTX_new();\n md_ctx_verify = EVP_MD_CTX_new();\n if (md_ctx == NULL || md_ctx_verify == NULL)\n goto out;\n pkey = load_example_rsa_key();\n if (pkey == NULL ||\n !EVP_DigestSignInit(md_ctx, NULL, EVP_sha256(), NULL, pkey) ||\n !EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))) {\n goto out;\n }\n if (!EVP_DigestSignFinal(md_ctx, NULL, &sig_len)) {\n goto out;\n }\n if (sig_len != (size_t)EVP_PKEY_size(pkey)) {\n fprintf(stderr, "sig_len mismatch\\n");\n goto out;\n }\n sig = OPENSSL_malloc(sig_len);\n if (sig == NULL || !EVP_DigestSignFinal(md_ctx, sig, &sig_len)) {\n goto out;\n }\n if (!EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sha256(), NULL, pkey)\n || !EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))\n || !EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)) {\n goto out;\n }\n ret = 1;\n out:\n if (!ret) {\n ERR_print_errors_fp(stderr);\n }\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 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}', 'void ERR_print_errors_fp(FILE *fp)\n{\n BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);\n if (bio == NULL)\n return;\n ERR_print_errors_cb(print_bio, bio);\n BIO_free(bio);\n}', 'BIO *BIO_new_fp(FILE *stream, int close_flag)\n{\n BIO *ret;\n if ((ret = BIO_new(BIO_s_file())) == NULL)\n return (NULL);\n BIO_set_flags(ret, BIO_FLAGS_UPLINK);\n BIO_set_fp(ret, stream, close_flag);\n return (ret);\n}', 'const BIO_METHOD *BIO_s_file(void)\n{\n return (&methods_filep);\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}']
35,552
0
https://github.com/openssl/openssl/blob/b2b4dfcca6cf2230107a711f7af1cd8ee3f74229/crypto/err/err.c/#L778
int ERR_set_mark(void) { ERR_STATE *es; es = ERR_get_state(); if (es == NULL) return 0; if (es->bottom == es->top) return 0; es->err_flags[es->top] |= ERR_FLAG_MARK; return 1; }
['int ERR_set_mark(void)\n{\n ERR_STATE *es;\n es = ERR_get_state();\n if (es == NULL)\n return 0;\n if (es->bottom == es->top)\n return 0;\n es->err_flags[es->top] |= ERR_FLAG_MARK;\n return 1;\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 if (!OPENSSL_init_crypto(0, NULL))\n return NULL;\n state = CRYPTO_THREAD_get_local(&err_thread_local);\n if (state == NULL) {\n if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) {\n return NULL;\n }\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 *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)\n{\n return pthread_getspecific(*key);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)\n{\n if (pthread_setspecific(*key, val) != 0)\n return 0;\n return 1;\n}']
35,553
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L828
static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride) { int y; PREDICT_8x8_LOAD_TOP; src[0] = t0; src[1] = t1; src[2] = t2; src[3] = t3; src[4] = t4; src[5] = t5; src[6] = t6; src[7] = t7; for( y = 1; y < 8; y++ ) *(uint64_t*)(src+y*stride) = *(uint64_t*)src; }
['static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)\n{\n int y;\n PREDICT_8x8_LOAD_TOP;\n src[0] = t0;\n src[1] = t1;\n src[2] = t2;\n src[3] = t3;\n src[4] = t4;\n src[5] = t5;\n src[6] = t6;\n src[7] = t7;\n for( y = 1; y < 8; y++ )\n *(uint64_t*)(src+y*stride) = *(uint64_t*)src;\n}']
35,554
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavformat/oggparsespeex.c/#L49
static int speex_header(AVFormatContext *s, int idx) { ogg_t *ogg = s->priv_data; ogg_stream_t *os = ogg->streams + idx; AVStream *st = s->streams[idx]; uint8_t *p = os->buf + os->pstart; if (os->psize < 80) return 1; st->codec->codec_type = CODEC_TYPE_AUDIO; st->codec->codec_id = CODEC_ID_SPEEX; st->codec->sample_rate = AV_RL32(p + 36); st->codec->channels = AV_RL32(p + 48); st->codec->extradata_size = os->psize; st->codec->extradata = av_malloc(st->codec->extradata_size); memcpy(st->codec->extradata, p, st->codec->extradata_size); st->time_base.num = 1; st->time_base.den = st->codec->sample_rate; return 0; }
['static int speex_header(AVFormatContext *s, int idx) {\n ogg_t *ogg = s->priv_data;\n ogg_stream_t *os = ogg->streams + idx;\n AVStream *st = s->streams[idx];\n uint8_t *p = os->buf + os->pstart;\n if (os->psize < 80)\n return 1;\n st->codec->codec_type = CODEC_TYPE_AUDIO;\n st->codec->codec_id = CODEC_ID_SPEEX;\n st->codec->sample_rate = AV_RL32(p + 36);\n st->codec->channels = AV_RL32(p + 48);\n st->codec->extradata_size = os->psize;\n st->codec->extradata = av_malloc(st->codec->extradata_size);\n memcpy(st->codec->extradata, p, st->codec->extradata_size);\n st->time_base.num = 1;\n st->time_base.den = st->codec->sample_rate;\n return 0;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr;\n#ifdef CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#ifdef CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif defined (HAVE_MEMALIGN)\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
35,555
0
https://github.com/nginx/nginx/blob/030a1f959c9c673258fe53f968fab04fc9214b86/src/http/ngx_http_upstream.c/#L5044
static ngx_int_t ngx_http_upstream_addr_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { u_char *p; size_t len; ngx_uint_t i; ngx_http_upstream_state_t *state; v->valid = 1; v->no_cacheable = 0; v->not_found = 0; if (r->upstream_states == NULL || r->upstream_states->nelts == 0) { v->not_found = 1; return NGX_OK; } len = 0; state = r->upstream_states->elts; for (i = 0; i < r->upstream_states->nelts; i++) { if (state[i].peer) { len += state[i].peer->len + 2; } else { len += 3; } } p = ngx_pnalloc(r->pool, len); if (p == NULL) { return NGX_ERROR; } v->data = p; i = 0; for ( ;; ) { if (state[i].peer) { p = ngx_cpymem(p, state[i].peer->data, state[i].peer->len); } if (++i == r->upstream_states->nelts) { break; } if (state[i].peer) { *p++ = ','; *p++ = ' '; } else { *p++ = ' '; *p++ = ':'; *p++ = ' '; if (++i == r->upstream_states->nelts) { break; } continue; } } v->len = p - v->data; return NGX_OK; }
["static ngx_int_t\nngx_http_upstream_addr_variable(ngx_http_request_t *r,\n ngx_http_variable_value_t *v, uintptr_t data)\n{\n u_char *p;\n size_t len;\n ngx_uint_t i;\n ngx_http_upstream_state_t *state;\n v->valid = 1;\n v->no_cacheable = 0;\n v->not_found = 0;\n if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {\n v->not_found = 1;\n return NGX_OK;\n }\n len = 0;\n state = r->upstream_states->elts;\n for (i = 0; i < r->upstream_states->nelts; i++) {\n if (state[i].peer) {\n len += state[i].peer->len + 2;\n } else {\n len += 3;\n }\n }\n p = ngx_pnalloc(r->pool, len);\n if (p == NULL) {\n return NGX_ERROR;\n }\n v->data = p;\n i = 0;\n for ( ;; ) {\n if (state[i].peer) {\n p = ngx_cpymem(p, state[i].peer->data, state[i].peer->len);\n }\n if (++i == r->upstream_states->nelts) {\n break;\n }\n if (state[i].peer) {\n *p++ = ',';\n *p++ = ' ';\n } else {\n *p++ = ' ';\n *p++ = ':';\n *p++ = ' ';\n if (++i == r->upstream_states->nelts) {\n break;\n }\n continue;\n }\n }\n v->len = p - v->data;\n return NGX_OK;\n}", 'void *\nngx_pnalloc(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, 0);\n }\n#endif\n return ngx_palloc_large(pool, size);\n}', 'static void *\nngx_palloc_large(ngx_pool_t *pool, size_t size)\n{\n void *p;\n ngx_uint_t n;\n ngx_pool_large_t *large;\n p = ngx_alloc(size, pool->log);\n if (p == NULL) {\n return NULL;\n }\n n = 0;\n for (large = pool->large; large; large = large->next) {\n if (large->alloc == NULL) {\n large->alloc = p;\n return p;\n }\n if (n++ > 3) {\n break;\n }\n }\n large = ngx_palloc_small(pool, sizeof(ngx_pool_large_t), 1);\n if (large == NULL) {\n ngx_free(p);\n return NULL;\n }\n large->alloc = p;\n large->next = pool->large;\n pool->large = large;\n return p;\n}', 'void *\nngx_alloc(size_t size, ngx_log_t *log)\n{\n void *p;\n p = malloc(size);\n if (p == NULL) {\n ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,\n "malloc(%uz) failed", size);\n }\n ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0, "malloc: %p:%uz", p, size);\n return p;\n}']
35,556
0
https://github.com/openssl/openssl/blob/e3713c365c2657236439fea00822a43aa396d112/crypto/bn/bn_lib.c/#L260
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['static ASN1_INTEGER *next_serial(const char *serialfile)\n{\n int ret = 0;\n BIO *in = NULL;\n ASN1_INTEGER *serial = NULL;\n BIGNUM *bn = NULL;\n if ((serial = ASN1_INTEGER_new()) == NULL)\n goto err;\n if ((in = BIO_new_file(serialfile, "r")) == NULL) {\n ERR_clear_error();\n BIO_printf(bio_err, "Warning: could not open file %s for "\n "reading, using serial number: 1\\n", serialfile);\n if (!ASN1_INTEGER_set(serial, 1))\n goto err;\n } else {\n char buf[1024];\n if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {\n BIO_printf(bio_err, "unable to load number from %s\\n",\n serialfile);\n goto err;\n }\n if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL)\n goto err;\n ASN1_INTEGER_free(serial);\n serial = NULL;\n if (!BN_add_word(bn, 1))\n goto err;\n if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)\n goto err;\n }\n ret = 1;\n err:\n if (!ret) {\n ASN1_INTEGER_free(serial);\n serial = NULL;\n }\n BIO_free_all(in);\n BN_free(bn);\n return serial;\n}', 'BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)\n{\n return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);\n}', 'static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,\n int itype)\n{\n BIGNUM *ret;\n if ((ai->type & ~V_ASN1_NEG) != itype) {\n ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_WRONG_INTEGER_TYPE);\n return NULL;\n }\n ret = BN_bin2bn(ai->data, ai->length, bn);\n if (ret == NULL) {\n ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_BN_LIB);\n return NULL;\n }\n if (ai->type & V_ASN1_NEG)\n BN_set_negative(ret, 1);\n return ret;\n}', 'int BN_add_word(BIGNUM *a, BN_ULONG w)\n{\n BN_ULONG l;\n int i;\n bn_check_top(a);\n w &= BN_MASK2;\n if (!w)\n return 1;\n if (BN_is_zero(a))\n return BN_set_word(a, w);\n if (a->neg) {\n a->neg = 0;\n i = BN_sub_word(a, w);\n if (!BN_is_zero(a))\n a->neg = !(a->neg);\n return (i);\n }\n for (i = 0; w != 0 && i < a->top; i++) {\n a->d[i] = l = (a->d[i] + w) & BN_MASK2;\n w = (w > l) ? 1 : 0;\n }\n if (w && i == a->top) {\n if (bn_wexpand(a, a->top + 1) == NULL)\n return 0;\n a->top++;\n a->d[i] = w;\n }\n bn_check_top(a);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
35,557
0
https://github.com/openssl/openssl/blob/72bb2f64fc67dc644f302a07953900becced011f/crypto/bn/bn_ctx.c/#L328
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int ossl_ecdh_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,\n EC_KEY *ecdh,\n void *(*KDF) (const void *in, size_t inlen,\n void *out, size_t *outlen))\n{\n BN_CTX *ctx;\n EC_POINT *tmp = NULL;\n BIGNUM *x = NULL, *y = NULL;\n const BIGNUM *priv_key;\n const EC_GROUP *group;\n int ret = -1;\n size_t buflen, len;\n unsigned char *buf = NULL;\n if (outlen > INT_MAX) {\n ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n priv_key = EC_KEY_get0_private_key(ecdh);\n if (priv_key == NULL) {\n ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, EC_R_NO_PRIVATE_VALUE);\n goto err;\n }\n group = EC_KEY_get0_group(ecdh);\n if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {\n if (!EC_GROUP_get_cofactor(group, x, ctx) ||\n !BN_mul(x, x, priv_key, ctx)) {\n ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n priv_key = x;\n }\n if ((tmp = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {\n ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);\n goto err;\n }\n if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==\n NID_X9_62_prime_field) {\n if (!EC_POINT_get_affine_coordinates_GFp(group, tmp, x, y, ctx)) {\n ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);\n goto err;\n }\n }\n#ifndef OPENSSL_NO_EC2M\n else {\n if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp, x, y, ctx)) {\n ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);\n goto err;\n }\n }\n#endif\n buflen = (EC_GROUP_get_degree(group) + 7) / 8;\n len = BN_num_bytes(x);\n if (len > buflen) {\n ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((buf = OPENSSL_malloc(buflen)) == NULL) {\n ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memset(buf, 0, buflen - len);\n if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {\n ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, ERR_R_BN_LIB);\n goto err;\n }\n if (KDF != 0) {\n if (KDF(buf, buflen, out, &outlen) == NULL) {\n ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, EC_R_KDF_FAILED);\n goto err;\n }\n ret = outlen;\n } else {\n if (outlen > buflen)\n outlen = buflen;\n memcpy(out, buf, outlen);\n ret = outlen;\n }\n err:\n EC_POINT_free(tmp);\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n OPENSSL_free(buf);\n return (ret);\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
35,558
0
https://github.com/libav/libav/blob/f3a094f2da26f62f3461e78a22b115ab8a700b8d/libavcodec/vp5.c/#L240
static void vp5_parse_coeff(VP56Context *s) { VP56RangeCoder *c = &s->c; VP56Model *model = s->modelp; uint8_t *permute = s->scantable.permutated; uint8_t *model1, *model2; int coeff, sign, coeff_idx; int b, i, cg, idx, ctx, ctx_last; int pt = 0; for (b=0; b<6; b++) { int ct = 1; if (b > 3) pt = 1; ctx = 6*s->coeff_ctx[ff_vp56_b6to4[b]][0] + s->above_blocks[s->above_block_idx[b]].not_null_dc; model1 = model->coeff_dccv[pt]; model2 = model->coeff_dcct[pt][ctx]; coeff_idx = 0; for (;;) { if (vp56_rac_get_prob(c, model2[0])) { if (vp56_rac_get_prob(c, model2[2])) { if (vp56_rac_get_prob(c, model2[3])) { s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 4; idx = vp56_rac_get_tree(c, ff_vp56_pc_tree, model1); sign = vp56_rac_get(c); coeff = ff_vp56_coeff_bias[idx+5]; for (i=ff_vp56_coeff_bit_length[idx]; i>=0; i--) coeff += vp56_rac_get_prob(c, ff_vp56_coeff_parse_table[idx][i]) << i; } else { if (vp56_rac_get_prob(c, model2[4])) { coeff = 3 + vp56_rac_get_prob(c, model1[5]); s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 3; } else { coeff = 2; s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 2; } sign = vp56_rac_get(c); } ct = 2; } else { ct = 1; s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 1; sign = vp56_rac_get(c); coeff = 1; } coeff = (coeff ^ -sign) + sign; if (coeff_idx) coeff *= s->dequant_ac; s->block_coeff[b][permute[coeff_idx]] = coeff; } else { if (ct && !vp56_rac_get_prob(c, model2[1])) break; ct = 0; s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 0; } coeff_idx++; if (coeff_idx >= 64) break; cg = vp5_coeff_groups[coeff_idx]; ctx = s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx]; model1 = model->coeff_ract[pt][ct][cg]; model2 = cg > 2 ? model1 : model->coeff_acct[pt][ct][cg][ctx]; } ctx_last = FFMIN(s->coeff_ctx_last[ff_vp56_b6to4[b]], 24); s->coeff_ctx_last[ff_vp56_b6to4[b]] = coeff_idx; if (coeff_idx < ctx_last) for (i=coeff_idx; i<=ctx_last; i++) s->coeff_ctx[ff_vp56_b6to4[b]][i] = 5; s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[ff_vp56_b6to4[b]][0]; } }
['static void vp5_parse_coeff(VP56Context *s)\n{\n VP56RangeCoder *c = &s->c;\n VP56Model *model = s->modelp;\n uint8_t *permute = s->scantable.permutated;\n uint8_t *model1, *model2;\n int coeff, sign, coeff_idx;\n int b, i, cg, idx, ctx, ctx_last;\n int pt = 0;\n for (b=0; b<6; b++) {\n int ct = 1;\n if (b > 3) pt = 1;\n ctx = 6*s->coeff_ctx[ff_vp56_b6to4[b]][0]\n + s->above_blocks[s->above_block_idx[b]].not_null_dc;\n model1 = model->coeff_dccv[pt];\n model2 = model->coeff_dcct[pt][ctx];\n coeff_idx = 0;\n for (;;) {\n if (vp56_rac_get_prob(c, model2[0])) {\n if (vp56_rac_get_prob(c, model2[2])) {\n if (vp56_rac_get_prob(c, model2[3])) {\n s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 4;\n idx = vp56_rac_get_tree(c, ff_vp56_pc_tree, model1);\n sign = vp56_rac_get(c);\n coeff = ff_vp56_coeff_bias[idx+5];\n for (i=ff_vp56_coeff_bit_length[idx]; i>=0; i--)\n coeff += vp56_rac_get_prob(c, ff_vp56_coeff_parse_table[idx][i]) << i;\n } else {\n if (vp56_rac_get_prob(c, model2[4])) {\n coeff = 3 + vp56_rac_get_prob(c, model1[5]);\n s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 3;\n } else {\n coeff = 2;\n s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 2;\n }\n sign = vp56_rac_get(c);\n }\n ct = 2;\n } else {\n ct = 1;\n s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 1;\n sign = vp56_rac_get(c);\n coeff = 1;\n }\n coeff = (coeff ^ -sign) + sign;\n if (coeff_idx)\n coeff *= s->dequant_ac;\n s->block_coeff[b][permute[coeff_idx]] = coeff;\n } else {\n if (ct && !vp56_rac_get_prob(c, model2[1]))\n break;\n ct = 0;\n s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 0;\n }\n coeff_idx++;\n if (coeff_idx >= 64)\n break;\n cg = vp5_coeff_groups[coeff_idx];\n ctx = s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx];\n model1 = model->coeff_ract[pt][ct][cg];\n model2 = cg > 2 ? model1 : model->coeff_acct[pt][ct][cg][ctx];\n }\n ctx_last = FFMIN(s->coeff_ctx_last[ff_vp56_b6to4[b]], 24);\n s->coeff_ctx_last[ff_vp56_b6to4[b]] = coeff_idx;\n if (coeff_idx < ctx_last)\n for (i=coeff_idx; i<=ctx_last; i++)\n s->coeff_ctx[ff_vp56_b6to4[b]][i] = 5;\n s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[ff_vp56_b6to4[b]][0];\n }\n}']
35,559
0
https://github.com/libav/libav/blob/452a398fd6bdca3f301c5c8af3bc241bc16a777e/libavformat/flvdec.c/#L159
static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, unsigned int max_pos, int depth) { AVCodecContext *acodec, *vcodec; ByteIOContext *ioc; AMFDataType amf_type; char str_val[256]; double num_val; num_val = 0; ioc = s->pb; amf_type = get_byte(ioc); switch(amf_type) { case AMF_DATA_TYPE_NUMBER: num_val = av_int2dbl(get_be64(ioc)); break; case AMF_DATA_TYPE_BOOL: num_val = get_byte(ioc); break; case AMF_DATA_TYPE_STRING: if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0) return -1; break; case AMF_DATA_TYPE_OBJECT: { unsigned int keylen; while(url_ftell(ioc) < max_pos - 2 && (keylen = get_be16(ioc))) { url_fskip(ioc, keylen); if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0) return -1; } if(get_byte(ioc) != AMF_END_OF_OBJECT) return -1; } break; case AMF_DATA_TYPE_NULL: case AMF_DATA_TYPE_UNDEFINED: case AMF_DATA_TYPE_UNSUPPORTED: break; case AMF_DATA_TYPE_MIXEDARRAY: url_fskip(ioc, 4); while(url_ftell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) { if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0) return -1; } if(get_byte(ioc) != AMF_END_OF_OBJECT) return -1; break; case AMF_DATA_TYPE_ARRAY: { unsigned int arraylen, i; arraylen = get_be32(ioc); for(i = 0; i < arraylen && url_ftell(ioc) < max_pos - 1; i++) { if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0) return -1; } } break; case AMF_DATA_TYPE_DATE: url_fskip(ioc, 8 + 2); break; default: return -1; } if(depth == 1 && key) { acodec = astream ? astream->codec : NULL; vcodec = vstream ? vstream->codec : NULL; if(amf_type == AMF_DATA_TYPE_BOOL) { if(!strcmp(key, "stereo") && acodec) acodec->channels = num_val > 0 ? 2 : 1; } else if(amf_type == AMF_DATA_TYPE_NUMBER) { if(!strcmp(key, "duration")) s->duration = num_val * AV_TIME_BASE; else if(!strcmp(key, "audiocodecid") && acodec && 0 <= (int)num_val) flv_set_audio_codec(s, astream, (int)num_val << FLV_AUDIO_CODECID_OFFSET); else if(!strcmp(key, "videocodecid") && vcodec && 0 <= (int)num_val) flv_set_video_codec(s, vstream, (int)num_val); else if(!strcmp(key, "audiosamplesize") && acodec && 0 < (int)num_val) { acodec->bits_per_sample = num_val; if(num_val == 8 && (acodec->codec_id == CODEC_ID_PCM_S16BE || acodec->codec_id == CODEC_ID_PCM_S16LE)) acodec->codec_id = CODEC_ID_PCM_S8; } else if(!strcmp(key, "audiosamplerate") && acodec && num_val >= 0) { if (!acodec->sample_rate) { switch((int)num_val) { case 44000: acodec->sample_rate = 44100 ; break; case 22000: acodec->sample_rate = 22050 ; break; case 11000: acodec->sample_rate = 11025 ; break; case 5000 : acodec->sample_rate = 5512 ; break; default : acodec->sample_rate = num_val; } } } } } return 0; }
['static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)\n{\n int ret, i, type, size, flags, is_audio, next, pos;\n unsigned dts;\n AVStream *st = NULL;\n retry:\n for(;;){\n pos = url_ftell(s->pb);\n url_fskip(s->pb, 4);\n type = get_byte(s->pb);\n size = get_be24(s->pb);\n dts = get_be24(s->pb);\n dts |= get_byte(s->pb) << 24;\n if (url_feof(s->pb))\n return AVERROR(EIO);\n url_fskip(s->pb, 3);\n flags = 0;\n if(size == 0)\n continue;\n next= size + url_ftell(s->pb);\n if (type == FLV_TAG_TYPE_AUDIO) {\n is_audio=1;\n flags = get_byte(s->pb);\n } else if (type == FLV_TAG_TYPE_VIDEO) {\n is_audio=0;\n flags = get_byte(s->pb);\n } else {\n if (type == FLV_TAG_TYPE_META && size > 13+1+4)\n flv_read_metabody(s, next);\n else\n av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\\n", type, size, flags);\n url_fseek(s->pb, next, SEEK_SET);\n continue;\n }\n for(i=0;i<s->nb_streams;i++) {\n st = s->streams[i];\n if (st->id == is_audio)\n break;\n }\n if(i == s->nb_streams){\n av_log(NULL, AV_LOG_ERROR, "invalid stream\\n");\n st= create_stream(s, is_audio);\n s->ctx_flags &= ~AVFMTCTX_NOHEADER;\n }\n if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))\n ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))\n || st->discard >= AVDISCARD_ALL\n ){\n url_fseek(s->pb, next, SEEK_SET);\n continue;\n }\n if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)\n av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);\n break;\n }\n if(!url_is_streamed(s->pb) && s->duration==AV_NOPTS_VALUE){\n int size;\n const int pos= url_ftell(s->pb);\n const int fsize= url_fsize(s->pb);\n url_fseek(s->pb, fsize-4, SEEK_SET);\n size= get_be32(s->pb);\n url_fseek(s->pb, fsize-3-size, SEEK_SET);\n if(size == get_be24(s->pb) + 11){\n s->duration= get_be24(s->pb) * (int64_t)AV_TIME_BASE / 1000;\n }\n url_fseek(s->pb, pos, SEEK_SET);\n }\n if(is_audio){\n if(!st->codec->sample_rate || !st->codec->bits_per_sample || (!st->codec->codec_id && !st->codec->codec_tag)) {\n st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;\n if((flags & FLV_AUDIO_CODECID_MASK) == FLV_CODECID_NELLYMOSER_8HZ_MONO)\n st->codec->sample_rate= 8000;\n else\n st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);\n st->codec->bits_per_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;\n flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);\n }\n }else{\n size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);\n }\n if (st->codec->codec_id == CODEC_ID_AAC ||\n st->codec->codec_id == CODEC_ID_H264) {\n int type = get_byte(s->pb);\n size--;\n if (st->codec->codec_id == CODEC_ID_H264) {\n get_be24(s->pb);\n }\n if (type == 0) {\n if ((ret = flv_get_extradata(s, st, size - 1)) < 0)\n return ret;\n goto retry;\n }\n }\n ret= av_get_packet(s->pb, pkt, size - 1);\n if (ret <= 0) {\n return AVERROR(EIO);\n }\n pkt->size = ret;\n pkt->dts = dts;\n pkt->stream_index = st->index;\n if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))\n pkt->flags |= PKT_FLAG_KEY;\n return ret;\n}', 'unsigned int get_be24(ByteIOContext *s)\n{\n unsigned int val;\n val = get_be16(s) << 8;\n val |= get_byte(s);\n return val;\n}', 'unsigned int get_be16(ByteIOContext *s)\n{\n unsigned int val;\n val = get_byte(s) << 8;\n val |= get_byte(s);\n return val;\n}', 'int get_byte(ByteIOContext *s)\n{\n if (s->buf_ptr < s->buf_end) {\n return *s->buf_ptr++;\n } else {\n fill_buffer(s);\n if (s->buf_ptr < s->buf_end)\n return *s->buf_ptr++;\n else\n return 0;\n }\n}', 'static int flv_read_metabody(AVFormatContext *s, unsigned int next_pos) {\n AMFDataType type;\n AVStream *stream, *astream, *vstream;\n ByteIOContext *ioc;\n int i, keylen;\n char buffer[11];\n astream = NULL;\n vstream = NULL;\n keylen = 0;\n ioc = s->pb;\n type = get_byte(ioc);\n if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData"))\n return -1;\n for(i = 0; i < s->nb_streams; i++) {\n stream = s->streams[i];\n if (stream->codec->codec_type == CODEC_TYPE_AUDIO) astream = stream;\n else if(stream->codec->codec_type == CODEC_TYPE_VIDEO) vstream = stream;\n }\n if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)\n return -1;\n return 0;\n}', 'static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, unsigned int max_pos, int depth) {\n AVCodecContext *acodec, *vcodec;\n ByteIOContext *ioc;\n AMFDataType amf_type;\n char str_val[256];\n double num_val;\n num_val = 0;\n ioc = s->pb;\n amf_type = get_byte(ioc);\n switch(amf_type) {\n case AMF_DATA_TYPE_NUMBER:\n num_val = av_int2dbl(get_be64(ioc)); break;\n case AMF_DATA_TYPE_BOOL:\n num_val = get_byte(ioc); break;\n case AMF_DATA_TYPE_STRING:\n if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)\n return -1;\n break;\n case AMF_DATA_TYPE_OBJECT: {\n unsigned int keylen;\n while(url_ftell(ioc) < max_pos - 2 && (keylen = get_be16(ioc))) {\n url_fskip(ioc, keylen);\n if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)\n return -1;\n }\n if(get_byte(ioc) != AMF_END_OF_OBJECT)\n return -1;\n }\n break;\n case AMF_DATA_TYPE_NULL:\n case AMF_DATA_TYPE_UNDEFINED:\n case AMF_DATA_TYPE_UNSUPPORTED:\n break;\n case AMF_DATA_TYPE_MIXEDARRAY:\n url_fskip(ioc, 4);\n while(url_ftell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {\n if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)\n return -1;\n }\n if(get_byte(ioc) != AMF_END_OF_OBJECT)\n return -1;\n break;\n case AMF_DATA_TYPE_ARRAY: {\n unsigned int arraylen, i;\n arraylen = get_be32(ioc);\n for(i = 0; i < arraylen && url_ftell(ioc) < max_pos - 1; i++) {\n if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)\n return -1;\n }\n }\n break;\n case AMF_DATA_TYPE_DATE:\n url_fskip(ioc, 8 + 2);\n break;\n default:\n return -1;\n }\n if(depth == 1 && key) {\n acodec = astream ? astream->codec : NULL;\n vcodec = vstream ? vstream->codec : NULL;\n if(amf_type == AMF_DATA_TYPE_BOOL) {\n if(!strcmp(key, "stereo") && acodec) acodec->channels = num_val > 0 ? 2 : 1;\n } else if(amf_type == AMF_DATA_TYPE_NUMBER) {\n if(!strcmp(key, "duration")) s->duration = num_val * AV_TIME_BASE;\n else if(!strcmp(key, "audiocodecid") && acodec && 0 <= (int)num_val)\n flv_set_audio_codec(s, astream, (int)num_val << FLV_AUDIO_CODECID_OFFSET);\n else if(!strcmp(key, "videocodecid") && vcodec && 0 <= (int)num_val)\n flv_set_video_codec(s, vstream, (int)num_val);\n else if(!strcmp(key, "audiosamplesize") && acodec && 0 < (int)num_val) {\n acodec->bits_per_sample = num_val;\n if(num_val == 8 && (acodec->codec_id == CODEC_ID_PCM_S16BE || acodec->codec_id == CODEC_ID_PCM_S16LE))\n acodec->codec_id = CODEC_ID_PCM_S8;\n }\n else if(!strcmp(key, "audiosamplerate") && acodec && num_val >= 0) {\n if (!acodec->sample_rate) {\n switch((int)num_val) {\n case 44000: acodec->sample_rate = 44100 ; break;\n case 22000: acodec->sample_rate = 22050 ; break;\n case 11000: acodec->sample_rate = 11025 ; break;\n case 5000 : acodec->sample_rate = 5512 ; break;\n default : acodec->sample_rate = num_val;\n }\n }\n }\n }\n }\n return 0;\n}']
35,560
0
https://github.com/libav/libav/blob/608be2acef3b69070f66dd539edd2197f93d6daf/libavcodec/twinvq.c/#L1024
static av_cold void init_bitstream_params(TwinContext *tctx) { const ModeTab *mtab = tctx->mtab; int n_ch = tctx->avctx->channels; int total_fr_bits = tctx->avctx->bit_rate*mtab->size/ tctx->avctx->sample_rate; int lsp_bits_per_block = n_ch*(mtab->lsp_bit0 + mtab->lsp_bit1 + mtab->lsp_split*mtab->lsp_bit2); int ppc_bits = n_ch*(mtab->pgain_bit + mtab->ppc_shape_bit + mtab->ppc_period_bit); int bsize_no_main_cb[3]; int bse_bits[3]; int i; for (i=0; i < 3; i++) bse_bits[i] = n_ch * (mtab->fmode[i].bark_n_coef * mtab->fmode[i].bark_n_bit + 1); bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits + WINDOW_TYPE_BITS + n_ch*GAIN_BITS; for (i=0; i < 2; i++) bsize_no_main_cb[i] = lsp_bits_per_block + n_ch*GAIN_BITS + WINDOW_TYPE_BITS + mtab->fmode[i].sub*(bse_bits[i] + n_ch*SUB_GAIN_BITS); for (i=0; i < 4; i++) { int bit_size; int vect_size; int rounded_up, rounded_down, num_rounded_down, num_rounded_up; if (i == 3) { bit_size = n_ch * mtab->ppc_shape_bit; vect_size = n_ch * mtab->ppc_shape_len; } else { bit_size = total_fr_bits - bsize_no_main_cb[i]; vect_size = n_ch * mtab->size; } tctx->n_div[i] = (bit_size + 13) / 14; rounded_up = (bit_size + tctx->n_div[i] - 1)/tctx->n_div[i]; rounded_down = (bit_size )/tctx->n_div[i]; num_rounded_down = rounded_up * tctx->n_div[i] - bit_size; num_rounded_up = tctx->n_div[i] - num_rounded_down; tctx->bits_main_spec[0][i][0] = (rounded_up + 1)/2; tctx->bits_main_spec[1][i][0] = (rounded_up )/2; tctx->bits_main_spec[0][i][1] = (rounded_down + 1)/2; tctx->bits_main_spec[1][i][1] = (rounded_down )/2; tctx->bits_main_spec_change[i] = num_rounded_up; rounded_up = (vect_size + tctx->n_div[i] - 1)/tctx->n_div[i]; rounded_down = (vect_size )/tctx->n_div[i]; num_rounded_down = rounded_up * tctx->n_div[i] - vect_size; num_rounded_up = tctx->n_div[i] - num_rounded_down; tctx->length[i][0] = rounded_up; tctx->length[i][1] = rounded_down; tctx->length_change[i] = num_rounded_up; } for (i=0; i < 4; i++) construct_perm_table(tctx, i); }
['static av_cold void init_bitstream_params(TwinContext *tctx)\n{\n const ModeTab *mtab = tctx->mtab;\n int n_ch = tctx->avctx->channels;\n int total_fr_bits = tctx->avctx->bit_rate*mtab->size/\n tctx->avctx->sample_rate;\n int lsp_bits_per_block = n_ch*(mtab->lsp_bit0 + mtab->lsp_bit1 +\n mtab->lsp_split*mtab->lsp_bit2);\n int ppc_bits = n_ch*(mtab->pgain_bit + mtab->ppc_shape_bit +\n mtab->ppc_period_bit);\n int bsize_no_main_cb[3];\n int bse_bits[3];\n int i;\n for (i=0; i < 3; i++)\n bse_bits[i] = n_ch *\n (mtab->fmode[i].bark_n_coef * mtab->fmode[i].bark_n_bit + 1);\n bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +\n WINDOW_TYPE_BITS + n_ch*GAIN_BITS;\n for (i=0; i < 2; i++)\n bsize_no_main_cb[i] =\n lsp_bits_per_block + n_ch*GAIN_BITS + WINDOW_TYPE_BITS +\n mtab->fmode[i].sub*(bse_bits[i] + n_ch*SUB_GAIN_BITS);\n for (i=0; i < 4; i++) {\n int bit_size;\n int vect_size;\n int rounded_up, rounded_down, num_rounded_down, num_rounded_up;\n if (i == 3) {\n bit_size = n_ch * mtab->ppc_shape_bit;\n vect_size = n_ch * mtab->ppc_shape_len;\n } else {\n bit_size = total_fr_bits - bsize_no_main_cb[i];\n vect_size = n_ch * mtab->size;\n }\n tctx->n_div[i] = (bit_size + 13) / 14;\n rounded_up = (bit_size + tctx->n_div[i] - 1)/tctx->n_div[i];\n rounded_down = (bit_size )/tctx->n_div[i];\n num_rounded_down = rounded_up * tctx->n_div[i] - bit_size;\n num_rounded_up = tctx->n_div[i] - num_rounded_down;\n tctx->bits_main_spec[0][i][0] = (rounded_up + 1)/2;\n tctx->bits_main_spec[1][i][0] = (rounded_up )/2;\n tctx->bits_main_spec[0][i][1] = (rounded_down + 1)/2;\n tctx->bits_main_spec[1][i][1] = (rounded_down )/2;\n tctx->bits_main_spec_change[i] = num_rounded_up;\n rounded_up = (vect_size + tctx->n_div[i] - 1)/tctx->n_div[i];\n rounded_down = (vect_size )/tctx->n_div[i];\n num_rounded_down = rounded_up * tctx->n_div[i] - vect_size;\n num_rounded_up = tctx->n_div[i] - num_rounded_down;\n tctx->length[i][0] = rounded_up;\n tctx->length[i][1] = rounded_down;\n tctx->length_change[i] = num_rounded_up;\n }\n for (i=0; i < 4; i++)\n construct_perm_table(tctx, i);\n}']
35,561
0
https://github.com/openssl/openssl/blob/a68d8c7b77a3d46d591b89cfd0ecd2a2242e4613/crypto/x509v3/pcy_tree.c/#L235
static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs, unsigned int flags) { X509_POLICY_TREE *tree; X509_POLICY_LEVEL *level; const X509_POLICY_CACHE *cache; X509_POLICY_DATA *data = NULL; int ret = X509_PCY_TREE_VALID; int n = sk_X509_num(certs) - 1; int explicit_policy = (flags & X509_V_FLAG_EXPLICIT_POLICY) ? 0 : n+1; int any_skip = (flags & X509_V_FLAG_INHIBIT_ANY) ? 0 : n+1; int map_skip = (flags & X509_V_FLAG_INHIBIT_MAP) ? 0 : n+1; int i; *ptree = NULL; if (n == 0) return X509_PCY_TREE_EMPTY; for (i = n - 1; i >= 0; i--) { X509 *x = sk_X509_value(certs, i); X509_check_purpose(x, -1, 0); if (policy_cache_set(x) == NULL) return X509_PCY_TREE_INTERNAL; } for (i = n - 1; i >= 0 && (explicit_policy > 0 || (ret & X509_PCY_TREE_EMPTY) == 0); i--) { X509 *x = sk_X509_value(certs, i); uint32_t ex_flags = X509_get_extension_flags(x); if (ex_flags & EXFLAG_INVALID_POLICY) return X509_PCY_TREE_INVALID; cache = policy_cache_set(x); if ((ret & X509_PCY_TREE_VALID) && cache->data == NULL) ret = X509_PCY_TREE_EMPTY; if (explicit_policy > 0) { if (!(ex_flags & EXFLAG_SI)) explicit_policy--; if ((cache->explicit_skip >= 0) && (cache->explicit_skip < explicit_policy)) explicit_policy = cache->explicit_skip; } } if (explicit_policy == 0) ret |= X509_PCY_TREE_EXPLICIT; if ((ret & X509_PCY_TREE_VALID) == 0) return ret; if ((tree = OPENSSL_zalloc(sizeof(*tree))) == NULL) return X509_PCY_TREE_INTERNAL; if ((tree->levels = OPENSSL_zalloc(sizeof(*tree->levels)*(n+1))) == NULL) { OPENSSL_free(tree); return X509_PCY_TREE_INTERNAL; } tree->nlevel = n+1; level = tree->levels; if ((data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0)) == NULL) goto bad_tree; if (level_add_node(level, data, NULL, tree) == NULL) { policy_data_free(data); goto bad_tree; } for (i = n - 1; i >= 0; i--) { X509 *x = sk_X509_value(certs, i); uint32_t ex_flags = X509_get_extension_flags(x); cache = policy_cache_set(x); X509_up_ref(x); (++level)->cert = x; if (!cache->anyPolicy) level->flags |= X509_V_FLAG_INHIBIT_ANY; if (any_skip == 0) { if (!(ex_flags & EXFLAG_SI) || (i == 0)) level->flags |= X509_V_FLAG_INHIBIT_ANY; } else { if (!(ex_flags & EXFLAG_SI)) any_skip--; if ((cache->any_skip >= 0) && (cache->any_skip < any_skip)) any_skip = cache->any_skip; } if (map_skip == 0) level->flags |= X509_V_FLAG_INHIBIT_MAP; else { if (!(ex_flags & EXFLAG_SI)) map_skip--; if ((cache->map_skip >= 0) && (cache->map_skip < map_skip)) map_skip = cache->map_skip; } } *ptree = tree; return ret; bad_tree: X509_policy_tree_free(tree); return X509_PCY_TREE_INTERNAL; }
['static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,\n unsigned int flags)\n{\n X509_POLICY_TREE *tree;\n X509_POLICY_LEVEL *level;\n const X509_POLICY_CACHE *cache;\n X509_POLICY_DATA *data = NULL;\n int ret = X509_PCY_TREE_VALID;\n int n = sk_X509_num(certs) - 1;\n int explicit_policy = (flags & X509_V_FLAG_EXPLICIT_POLICY) ? 0 : n+1;\n int any_skip = (flags & X509_V_FLAG_INHIBIT_ANY) ? 0 : n+1;\n int map_skip = (flags & X509_V_FLAG_INHIBIT_MAP) ? 0 : n+1;\n int i;\n *ptree = NULL;\n if (n == 0)\n return X509_PCY_TREE_EMPTY;\n for (i = n - 1; i >= 0; i--) {\n X509 *x = sk_X509_value(certs, i);\n X509_check_purpose(x, -1, 0);\n if (policy_cache_set(x) == NULL)\n return X509_PCY_TREE_INTERNAL;\n }\n for (i = n - 1;\n i >= 0 && (explicit_policy > 0 || (ret & X509_PCY_TREE_EMPTY) == 0);\n i--) {\n X509 *x = sk_X509_value(certs, i);\n uint32_t ex_flags = X509_get_extension_flags(x);\n if (ex_flags & EXFLAG_INVALID_POLICY)\n return X509_PCY_TREE_INVALID;\n cache = policy_cache_set(x);\n if ((ret & X509_PCY_TREE_VALID) && cache->data == NULL)\n ret = X509_PCY_TREE_EMPTY;\n if (explicit_policy > 0) {\n if (!(ex_flags & EXFLAG_SI))\n explicit_policy--;\n if ((cache->explicit_skip >= 0)\n && (cache->explicit_skip < explicit_policy))\n explicit_policy = cache->explicit_skip;\n }\n }\n if (explicit_policy == 0)\n ret |= X509_PCY_TREE_EXPLICIT;\n if ((ret & X509_PCY_TREE_VALID) == 0)\n return ret;\n if ((tree = OPENSSL_zalloc(sizeof(*tree))) == NULL)\n return X509_PCY_TREE_INTERNAL;\n if ((tree->levels = OPENSSL_zalloc(sizeof(*tree->levels)*(n+1))) == NULL) {\n OPENSSL_free(tree);\n return X509_PCY_TREE_INTERNAL;\n }\n tree->nlevel = n+1;\n level = tree->levels;\n if ((data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0)) == NULL)\n goto bad_tree;\n if (level_add_node(level, data, NULL, tree) == NULL) {\n policy_data_free(data);\n goto bad_tree;\n }\n for (i = n - 1; i >= 0; i--) {\n X509 *x = sk_X509_value(certs, i);\n uint32_t ex_flags = X509_get_extension_flags(x);\n cache = policy_cache_set(x);\n X509_up_ref(x);\n (++level)->cert = x;\n if (!cache->anyPolicy)\n level->flags |= X509_V_FLAG_INHIBIT_ANY;\n if (any_skip == 0) {\n if (!(ex_flags & EXFLAG_SI) || (i == 0))\n level->flags |= X509_V_FLAG_INHIBIT_ANY;\n } else {\n if (!(ex_flags & EXFLAG_SI))\n any_skip--;\n if ((cache->any_skip >= 0) && (cache->any_skip < any_skip))\n any_skip = cache->any_skip;\n }\n if (map_skip == 0)\n level->flags |= X509_V_FLAG_INHIBIT_MAP;\n else {\n if (!(ex_flags & EXFLAG_SI))\n map_skip--;\n if ((cache->map_skip >= 0) && (cache->map_skip < map_skip))\n map_skip = cache->map_skip;\n }\n }\n *ptree = tree;\n return ret;\n bad_tree:\n X509_policy_tree_free(tree);\n return X509_PCY_TREE_INTERNAL;\n}', 'DEFINE_STACK_OF(X509)', 'int OPENSSL_sk_num(const OPENSSL_STACK *st)\n{\n if (st == NULL)\n return -1;\n return st->num;\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}', 'ASN1_OBJECT *OBJ_nid2obj(int n)\n{\n ADDED_OBJ ad, *adp;\n ASN1_OBJECT ob;\n if ((n >= 0) && (n < NUM_NID)) {\n if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {\n OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);\n return (NULL);\n }\n return ((ASN1_OBJECT *)&(nid_objs[n]));\n } else if (added == NULL)\n return (NULL);\n else {\n ad.type = ADDED_NID;\n ad.obj = &ob;\n ob.nid = n;\n adp = lh_ADDED_OBJ_retrieve(added, &ad);\n if (adp != NULL)\n return (adp->obj);\n else {\n OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);\n return (NULL);\n }\n }\n}']
35,562
0
https://github.com/libav/libav/blob/80bdf7e0b7975956c42f17589cb21a5531f179ef/libavcodec/vc1dec.c/#L733
static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir) { MpegEncContext *s = &v->s; DSPContext *dsp = &v->s.dsp; uint8_t *srcY; int dxy, mx, my, src_x, src_y; int off; int fieldmv = (v->fcm == 1) ? v->blk_mv_type[s->block_index[n]] : 0; int v_edge_pos = s->v_edge_pos >> v->field_mode; if(!v->field_mode && !v->s.last_picture.f.data[0])return; mx = s->mv[dir][n][0]; my = s->mv[dir][n][1]; if (!dir) { if (v->field_mode) { if ((v->cur_field_type != v->ref_field_type[dir]) && v->cur_field_type) srcY = s->current_picture.f.data[0]; else srcY = s->last_picture.f.data[0]; } else srcY = s->last_picture.f.data[0]; } else srcY = s->next_picture.f.data[0]; if (v->field_mode) { if (v->cur_field_type != v->ref_field_type[dir]) my = my - 2 + 4 * v->cur_field_type; } if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) { int same_count = 0, opp_count = 0, k; int chosen_mv[2][4][2], f; int tx, ty; for (k = 0; k < 4; k++) { f = v->mv_f[0][s->block_index[k] + v->blocks_off]; chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0]; chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1]; opp_count += f; same_count += 1 - f; } f = opp_count > same_count; switch (f ? opp_count : same_count) { case 4: tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0], chosen_mv[f][3][0]); ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1], chosen_mv[f][3][1]); break; case 3: tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]); ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]); break; case 2: tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2; ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2; break; } s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx; s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty; for (k = 0; k < 4; k++) v->mv_f[1][s->block_index[k] + v->blocks_off] = f; } if (v->fcm == 1) { int qx, qy; int width = s->avctx->coded_width; int height = s->avctx->coded_height >> 1; qx = (s->mb_x * 16) + (mx >> 2); qy = (s->mb_y * 8) + (my >> 3); if (qx < -17) mx -= 4 * (qx + 17); else if (qx > width) mx -= 4 * (qx - width); if (qy < -18) my -= 8 * (qy + 18); else if (qy > height + 1) my -= 8 * (qy - height - 1); } if ((v->fcm == 1) && fieldmv) off = ((n>1) ? s->linesize : 0) + (n&1) * 8; else off = s->linesize * 4 * (n&2) + (n&1) * 8; if (v->field_mode && v->cur_field_type) off += s->current_picture_ptr->f.linesize[0]; src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2); if (!fieldmv) src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2); else src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2); if(v->profile != PROFILE_ADVANCED){ src_x = av_clip( src_x, -16, s->mb_width * 16); src_y = av_clip( src_y, -16, s->mb_height * 16); }else{ src_x = av_clip( src_x, -17, s->avctx->coded_width); if (v->fcm == 1) { if (src_y & 1) src_y = av_clip( src_y, -17, s->avctx->coded_height + 1); else src_y = av_clip( src_y, -18, s->avctx->coded_height); } else { src_y = av_clip( src_y, -18, s->avctx->coded_height + 1); } } srcY += src_y * s->linesize + src_x; if (v->field_mode && v->ref_field_type[dir]) srcY += s->current_picture_ptr->f.linesize[0]; if (fieldmv && !(src_y & 1)) v_edge_pos--; if (fieldmv && (src_y & 1) && src_y < 4) src_y--; if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP) || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2 || (unsigned)(src_y - (s->mspel<<fieldmv)) > v_edge_pos - (my&3) - ((8 + s->mspel*2)<<fieldmv)){ srcY -= s->mspel * (1 + (s->linesize << fieldmv)); s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, (9+s->mspel*2) << fieldmv, src_x - s->mspel, src_y - (s->mspel << fieldmv), s->h_edge_pos, v_edge_pos); srcY = s->edge_emu_buffer; if(v->rangeredfrm) { int i, j; uint8_t *src; src = srcY; for(j = 0; j < 9 + s->mspel*2; j++) { for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128; src += s->linesize << fieldmv; } } if(v->mv_mode == MV_PMODE_INTENSITY_COMP) { int i, j; uint8_t *src; src = srcY; for(j = 0; j < 9 + s->mspel*2; j++) { for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]]; src += s->linesize << fieldmv; } } srcY += s->mspel * (1 + (s->linesize << fieldmv)); } if(s->mspel) { dxy = ((my & 3) << 2) | (mx & 3); v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd); } else { dxy = (my & 2) | ((mx & 2) >> 1); if(!v->rnd) dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8); else dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8); } }
['static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)\n{\n MpegEncContext *s = &v->s;\n DSPContext *dsp = &v->s.dsp;\n uint8_t *srcY;\n int dxy, mx, my, src_x, src_y;\n int off;\n int fieldmv = (v->fcm == 1) ? v->blk_mv_type[s->block_index[n]] : 0;\n int v_edge_pos = s->v_edge_pos >> v->field_mode;\n if(!v->field_mode && !v->s.last_picture.f.data[0])return;\n mx = s->mv[dir][n][0];\n my = s->mv[dir][n][1];\n if (!dir) {\n if (v->field_mode) {\n if ((v->cur_field_type != v->ref_field_type[dir]) && v->cur_field_type)\n srcY = s->current_picture.f.data[0];\n else\n srcY = s->last_picture.f.data[0];\n } else\n srcY = s->last_picture.f.data[0];\n } else\n srcY = s->next_picture.f.data[0];\n if (v->field_mode) {\n if (v->cur_field_type != v->ref_field_type[dir])\n my = my - 2 + 4 * v->cur_field_type;\n }\n if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {\n int same_count = 0, opp_count = 0, k;\n int chosen_mv[2][4][2], f;\n int tx, ty;\n for (k = 0; k < 4; k++) {\n f = v->mv_f[0][s->block_index[k] + v->blocks_off];\n chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];\n chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];\n opp_count += f;\n same_count += 1 - f;\n }\n f = opp_count > same_count;\n switch (f ? opp_count : same_count) {\n case 4:\n tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0], chosen_mv[f][3][0]);\n ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1], chosen_mv[f][3][1]);\n break;\n case 3:\n tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);\n ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);\n break;\n case 2:\n tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;\n ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;\n break;\n }\n s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;\n s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;\n for (k = 0; k < 4; k++) v->mv_f[1][s->block_index[k] + v->blocks_off] = f;\n }\n if (v->fcm == 1) {\n int qx, qy;\n int width = s->avctx->coded_width;\n int height = s->avctx->coded_height >> 1;\n qx = (s->mb_x * 16) + (mx >> 2);\n qy = (s->mb_y * 8) + (my >> 3);\n if (qx < -17)\n mx -= 4 * (qx + 17);\n else if (qx > width)\n mx -= 4 * (qx - width);\n if (qy < -18)\n my -= 8 * (qy + 18);\n else if (qy > height + 1)\n my -= 8 * (qy - height - 1);\n }\n if ((v->fcm == 1) && fieldmv)\n off = ((n>1) ? s->linesize : 0) + (n&1) * 8;\n else\n off = s->linesize * 4 * (n&2) + (n&1) * 8;\n if (v->field_mode && v->cur_field_type)\n off += s->current_picture_ptr->f.linesize[0];\n src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);\n if (!fieldmv)\n src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);\n else\n src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);\n if(v->profile != PROFILE_ADVANCED){\n src_x = av_clip( src_x, -16, s->mb_width * 16);\n src_y = av_clip( src_y, -16, s->mb_height * 16);\n }else{\n src_x = av_clip( src_x, -17, s->avctx->coded_width);\n if (v->fcm == 1) {\n if (src_y & 1)\n src_y = av_clip( src_y, -17, s->avctx->coded_height + 1);\n else\n src_y = av_clip( src_y, -18, s->avctx->coded_height);\n } else {\n src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);\n }\n }\n srcY += src_y * s->linesize + src_x;\n if (v->field_mode && v->ref_field_type[dir])\n srcY += s->current_picture_ptr->f.linesize[0];\n if (fieldmv && !(src_y & 1))\n v_edge_pos--;\n if (fieldmv && (src_y & 1) && src_y < 4)\n src_y--;\n if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)\n || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2\n || (unsigned)(src_y - (s->mspel<<fieldmv)) > v_edge_pos - (my&3) - ((8 + s->mspel*2)<<fieldmv)){\n srcY -= s->mspel * (1 + (s->linesize << fieldmv));\n s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, (9+s->mspel*2) << fieldmv, src_x - s->mspel,\n src_y - (s->mspel << fieldmv), s->h_edge_pos, v_edge_pos);\n srcY = s->edge_emu_buffer;\n if(v->rangeredfrm) {\n int i, j;\n uint8_t *src;\n src = srcY;\n for(j = 0; j < 9 + s->mspel*2; j++) {\n for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;\n src += s->linesize << fieldmv;\n }\n }\n if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {\n int i, j;\n uint8_t *src;\n src = srcY;\n for(j = 0; j < 9 + s->mspel*2; j++) {\n for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];\n src += s->linesize << fieldmv;\n }\n }\n srcY += s->mspel * (1 + (s->linesize << fieldmv));\n }\n if(s->mspel) {\n dxy = ((my & 3) << 2) | (mx & 3);\n v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);\n } else {\n dxy = (my & 2) | ((mx & 2) >> 1);\n if(!v->rnd)\n dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);\n else\n dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);\n }\n}']
35,563
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L711
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) { int i; BN_ULONG aa, bb; aa = a[n - 1]; bb = b[n - 1]; if (aa != bb) return ((aa > bb) ? 1 : -1); for (i = n - 2; i >= 0; i--) { aa = a[i]; bb = b[i]; if (aa != bb) return ((aa > bb) ? 1 : -1); } return 0; }
['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}', '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_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 = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n int tna, int tnb, BN_ULONG *t)\n{\n int i, j, n2 = n * 2;\n int c1, c2, neg;\n BN_ULONG ln, lo, *p;\n if (n < 8) {\n bn_mul_normal(r, a, n + tna, b, n + tnb);\n return;\n }\n c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);\n c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);\n neg = 0;\n switch (c1 * 3 + c2) {\n case -4:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n break;\n case -3:\n case -2:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n neg = 1;\n break;\n case -1:\n case 0:\n case 1:\n case 2:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n neg = 1;\n break;\n case 3:\n case 4:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n break;\n }\n# if 0\n if (n == 4) {\n bn_mul_comba4(&(t[n2]), t, &(t[n]));\n bn_mul_comba4(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);\n memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));\n } else\n# endif\n if (n == 8) {\n bn_mul_comba8(&(t[n2]), t, &(t[n]));\n bn_mul_comba8(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));\n } else {\n p = &(t[n2 * 2]);\n bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);\n bn_mul_recursive(r, a, b, n, 0, 0, p);\n i = n / 2;\n if (tna > tnb)\n j = tna - i;\n else\n j = tnb - i;\n if (j == 0) {\n bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));\n } else if (j > 0) {\n bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&(r[n2 + tna + tnb]), 0,\n sizeof(BN_ULONG) * (n2 - tna - tnb));\n } else {\n memset(&r[n2], 0, sizeof(*r) * n2);\n if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n } else {\n for (;;) {\n i /= 2;\n if (i < tna || i < tnb) {\n bn_mul_part_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n } else if (i == tna || i == tnb) {\n bn_mul_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n }\n }\n }\n }\n }\n c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));\n if (neg) {\n c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));\n } else {\n c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));\n }\n c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));\n if (c1) {\n p = &(r[n + n2]);\n lo = *p;\n ln = (lo + c1) & BN_MASK2;\n *p = ln;\n if (ln < (BN_ULONG)c1) {\n do {\n p++;\n lo = *p;\n ln = (lo + 1) & BN_MASK2;\n *p = ln;\n } while (ln == 0);\n }\n }\n}', 'int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)\n{\n int n, i;\n n = cl - 1;\n if (dl < 0) {\n for (i = dl; i < 0; i++) {\n if (b[n - i] != 0)\n return -1;\n }\n }\n if (dl > 0) {\n for (i = dl; i > 0; i--) {\n if (a[n + i] != 0)\n return 1;\n }\n }\n return bn_cmp_words(a, b, cl);\n}', 'int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)\n{\n int i;\n BN_ULONG aa, bb;\n aa = a[n - 1];\n bb = b[n - 1];\n if (aa != bb)\n return ((aa > bb) ? 1 : -1);\n for (i = n - 2; i >= 0; i--) {\n aa = a[i];\n bb = b[i];\n if (aa != bb)\n return ((aa > bb) ? 1 : -1);\n }\n return 0;\n}']
35,564
0
https://github.com/openssl/openssl/blob/49cd47eaababc8c57871b929080fc1357e2ad7b8/crypto/cms/cms_sd.c/#L849
int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain) { ASN1_OCTET_STRING *os = NULL; EVP_MD_CTX *mctx = EVP_MD_CTX_new(); EVP_PKEY_CTX *pkctx = NULL; int r = -1; unsigned char mval[EVP_MAX_MD_SIZE]; unsigned int mlen; if (mctx == NULL) { CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE); goto err; } if (CMS_signed_get_attr_count(si) >= 0) { os = CMS_signed_get0_data_by_OBJ(si, OBJ_nid2obj(NID_pkcs9_messageDigest), -3, V_ASN1_OCTET_STRING); if (!os) { CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE); goto err; } } if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm)) goto err; if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) { CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, CMS_R_UNABLE_TO_FINALIZE_CONTEXT); goto err; } if (os) { if (mlen != (unsigned int)os->length) { CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH); goto err; } if (memcmp(mval, os->data, mlen)) { CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, CMS_R_VERIFICATION_FAILURE); r = 0; } else r = 1; } else { const EVP_MD *md = EVP_MD_CTX_md(mctx); pkctx = EVP_PKEY_CTX_new(si->pkey, NULL); if (pkctx == NULL) goto err; if (EVP_PKEY_verify_init(pkctx) <= 0) goto err; if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0) goto err; si->pctx = pkctx; if (!cms_sd_asn1_ctrl(si, 1)) goto err; r = EVP_PKEY_verify(pkctx, si->signature->data, si->signature->length, mval, mlen); if (r <= 0) { CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, CMS_R_VERIFICATION_FAILURE); r = 0; } } err: EVP_PKEY_CTX_free(pkctx); EVP_MD_CTX_free(mctx); return r; }
['int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)\n{\n ASN1_OCTET_STRING *os = NULL;\n EVP_MD_CTX *mctx = EVP_MD_CTX_new();\n EVP_PKEY_CTX *pkctx = NULL;\n int r = -1;\n unsigned char mval[EVP_MAX_MD_SIZE];\n unsigned int mlen;\n if (mctx == NULL) {\n CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (CMS_signed_get_attr_count(si) >= 0) {\n os = CMS_signed_get0_data_by_OBJ(si,\n OBJ_nid2obj(NID_pkcs9_messageDigest),\n -3, V_ASN1_OCTET_STRING);\n if (!os) {\n CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,\n CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);\n goto err;\n }\n }\n if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))\n goto err;\n if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {\n CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,\n CMS_R_UNABLE_TO_FINALIZE_CONTEXT);\n goto err;\n }\n if (os) {\n if (mlen != (unsigned int)os->length) {\n CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,\n CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);\n goto err;\n }\n if (memcmp(mval, os->data, mlen)) {\n CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,\n CMS_R_VERIFICATION_FAILURE);\n r = 0;\n } else\n r = 1;\n } else {\n const EVP_MD *md = EVP_MD_CTX_md(mctx);\n pkctx = EVP_PKEY_CTX_new(si->pkey, NULL);\n if (pkctx == NULL)\n goto err;\n if (EVP_PKEY_verify_init(pkctx) <= 0)\n goto err;\n if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)\n goto err;\n si->pctx = pkctx;\n if (!cms_sd_asn1_ctrl(si, 1))\n goto err;\n r = EVP_PKEY_verify(pkctx, si->signature->data,\n si->signature->length, mval, mlen);\n if (r <= 0) {\n CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,\n CMS_R_VERIFICATION_FAILURE);\n r = 0;\n }\n }\n err:\n EVP_PKEY_CTX_free(pkctx);\n EVP_MD_CTX_free(mctx);\n return r;\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 INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int CMS_signed_get_attr_count(const CMS_SignerInfo *si)\n{\n return X509at_get_attr_count(si->signedAttrs);\n}', 'int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)\n{\n return sk_X509_ATTRIBUTE_num(x);\n}', 'int OPENSSL_sk_num(const OPENSSL_STACK *st)\n{\n return st == NULL ? -1 : st->num;\n}', 'int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,\n X509_ALGOR *mdalg)\n{\n int nid;\n const ASN1_OBJECT *mdoid;\n X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);\n nid = OBJ_obj2nid(mdoid);\n for (;;) {\n EVP_MD_CTX *mtmp;\n chain = BIO_find_type(chain, BIO_TYPE_MD);\n if (chain == NULL) {\n CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,\n CMS_R_NO_MATCHING_DIGEST);\n return 0;\n }\n BIO_get_md_ctx(chain, &mtmp);\n if (EVP_MD_CTX_type(mtmp) == nid\n || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)\n return EVP_MD_CTX_copy_ex(mctx, mtmp);\n chain = BIO_next(chain);\n }\n}', 'void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype,\n const void **ppval, const X509_ALGOR *algor)\n{\n if (paobj)\n *paobj = algor->algorithm;\n if (pptype) {\n if (algor->parameter == NULL) {\n *pptype = V_ASN1_UNDEF;\n return;\n } else\n *pptype = algor->parameter->type;\n if (ppval)\n *ppval = algor->parameter->value.ptr;\n }\n}', 'int OBJ_obj2nid(const ASN1_OBJECT *a)\n{\n const unsigned int *op;\n ADDED_OBJ ad, *adp;\n if (a == NULL)\n return NID_undef;\n if (a->nid != 0)\n return a->nid;\n if (a->length == 0)\n return NID_undef;\n if (added != NULL) {\n ad.type = ADDED_DATA;\n ad.obj = (ASN1_OBJECT *)a;\n adp = lh_ADDED_OBJ_retrieve(added, &ad);\n if (adp != NULL)\n return adp->obj->nid;\n }\n op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);\n if (op == NULL)\n return NID_undef;\n return nid_objs[*op].nid;\n}', 'DEFINE_LHASH_OF(ADDED_OBJ)', 'void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_retrieve_miss++;\n return NULL;\n } else {\n ret = (*rn)->data;\n lh->num_retrieve++;\n }\n return ret;\n}', 'void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n if (ctx->pmeth && ctx->pmeth->cleanup)\n ctx->pmeth->cleanup(ctx);\n EVP_PKEY_free(ctx->pkey);\n EVP_PKEY_free(ctx->peerkey);\n#ifndef OPENSSL_NO_ENGINE\n ENGINE_finish(ctx->engine);\n#endif\n OPENSSL_free(ctx);\n}', 'void 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 INCREMENT(free_count);\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
35,565
0
https://github.com/openssl/openssl/blob/84c15db551ce1d167b901a3bde2b21880b084384/crypto/bn/bn_asm.c/#L545
void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) { #ifdef BN_LLONG BN_ULLONG t; #else BN_ULONG bl,bh; #endif BN_ULONG t1,t2; BN_ULONG c1,c2,c3; c1=0; c2=0; c3=0; mul_add_c(a[0],b[0],c1,c2,c3); r[0]=c1; c1=0; mul_add_c(a[0],b[1],c2,c3,c1); mul_add_c(a[1],b[0],c2,c3,c1); r[1]=c2; c2=0; mul_add_c(a[2],b[0],c3,c1,c2); mul_add_c(a[1],b[1],c3,c1,c2); mul_add_c(a[0],b[2],c3,c1,c2); r[2]=c3; c3=0; mul_add_c(a[0],b[3],c1,c2,c3); mul_add_c(a[1],b[2],c1,c2,c3); mul_add_c(a[2],b[1],c1,c2,c3); mul_add_c(a[3],b[0],c1,c2,c3); r[3]=c1; c1=0; mul_add_c(a[4],b[0],c2,c3,c1); mul_add_c(a[3],b[1],c2,c3,c1); mul_add_c(a[2],b[2],c2,c3,c1); mul_add_c(a[1],b[3],c2,c3,c1); mul_add_c(a[0],b[4],c2,c3,c1); r[4]=c2; c2=0; mul_add_c(a[0],b[5],c3,c1,c2); mul_add_c(a[1],b[4],c3,c1,c2); mul_add_c(a[2],b[3],c3,c1,c2); mul_add_c(a[3],b[2],c3,c1,c2); mul_add_c(a[4],b[1],c3,c1,c2); mul_add_c(a[5],b[0],c3,c1,c2); r[5]=c3; c3=0; mul_add_c(a[6],b[0],c1,c2,c3); mul_add_c(a[5],b[1],c1,c2,c3); mul_add_c(a[4],b[2],c1,c2,c3); mul_add_c(a[3],b[3],c1,c2,c3); mul_add_c(a[2],b[4],c1,c2,c3); mul_add_c(a[1],b[5],c1,c2,c3); mul_add_c(a[0],b[6],c1,c2,c3); r[6]=c1; c1=0; mul_add_c(a[0],b[7],c2,c3,c1); mul_add_c(a[1],b[6],c2,c3,c1); mul_add_c(a[2],b[5],c2,c3,c1); mul_add_c(a[3],b[4],c2,c3,c1); mul_add_c(a[4],b[3],c2,c3,c1); mul_add_c(a[5],b[2],c2,c3,c1); mul_add_c(a[6],b[1],c2,c3,c1); mul_add_c(a[7],b[0],c2,c3,c1); r[7]=c2; c2=0; mul_add_c(a[7],b[1],c3,c1,c2); mul_add_c(a[6],b[2],c3,c1,c2); mul_add_c(a[5],b[3],c3,c1,c2); mul_add_c(a[4],b[4],c3,c1,c2); mul_add_c(a[3],b[5],c3,c1,c2); mul_add_c(a[2],b[6],c3,c1,c2); mul_add_c(a[1],b[7],c3,c1,c2); r[8]=c3; c3=0; mul_add_c(a[2],b[7],c1,c2,c3); mul_add_c(a[3],b[6],c1,c2,c3); mul_add_c(a[4],b[5],c1,c2,c3); mul_add_c(a[5],b[4],c1,c2,c3); mul_add_c(a[6],b[3],c1,c2,c3); mul_add_c(a[7],b[2],c1,c2,c3); r[9]=c1; c1=0; mul_add_c(a[7],b[3],c2,c3,c1); mul_add_c(a[6],b[4],c2,c3,c1); mul_add_c(a[5],b[5],c2,c3,c1); mul_add_c(a[4],b[6],c2,c3,c1); mul_add_c(a[3],b[7],c2,c3,c1); r[10]=c2; c2=0; mul_add_c(a[4],b[7],c3,c1,c2); mul_add_c(a[5],b[6],c3,c1,c2); mul_add_c(a[6],b[5],c3,c1,c2); mul_add_c(a[7],b[4],c3,c1,c2); r[11]=c3; c3=0; mul_add_c(a[7],b[5],c1,c2,c3); mul_add_c(a[6],b[6],c1,c2,c3); mul_add_c(a[5],b[7],c1,c2,c3); r[12]=c1; c1=0; mul_add_c(a[6],b[7],c2,c3,c1); mul_add_c(a[7],b[6],c2,c3,c1); r[13]=c2; c2=0; mul_add_c(a[7],b[7],c3,c1,c2); r[14]=c3; r[15]=c1; }
['DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,\n\t int *counter_ret, unsigned long *h_ret, void (*callback)(),\n\t char *cb_arg)\n\t{\n\tint ok=0;\n\tunsigned char seed[SHA_DIGEST_LENGTH];\n\tunsigned char md[SHA_DIGEST_LENGTH];\n\tunsigned char buf[SHA_DIGEST_LENGTH],buf2[SHA_DIGEST_LENGTH];\n\tBIGNUM *r0,*W,*X,*c,*test;\n\tBIGNUM *g=NULL,*q=NULL,*p=NULL;\n\tBN_MONT_CTX *mont=NULL;\n\tint k,n=0,i,b,m=0;\n\tint counter=0;\n\tBN_CTX *ctx=NULL,*ctx2=NULL;\n\tunsigned int h=2;\n\tDSA *ret=NULL;\n\tif (bits < 512) bits=512;\n\tbits=(bits+63)/64*64;\n\tif ((seed_in != NULL) && (seed_len == 20))\n\t\tmemcpy(seed,seed_in,seed_len);\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tif ((ctx2=BN_CTX_new()) == NULL) goto err;\n\tif ((ret=DSA_new()) == NULL) goto err;\n\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\tr0= &(ctx2->bn[0]);\n\tg= &(ctx2->bn[1]);\n\tW= &(ctx2->bn[2]);\n\tq= &(ctx2->bn[3]);\n\tX= &(ctx2->bn[4]);\n\tc= &(ctx2->bn[5]);\n\tp= &(ctx2->bn[6]);\n\ttest= &(ctx2->bn[7]);\n\tBN_lshift(test,BN_value_one(),bits-1);\n\tfor (;;)\n\t\t{\n\t\tfor (;;)\n\t\t\t{\n\t\t\tif (callback != NULL) callback(0,m++,cb_arg);\n\t\t\tif (!seed_len)\n\t\t\t\tRAND_bytes(seed,SHA_DIGEST_LENGTH);\n\t\t\telse\n\t\t\t\tseed_len=0;\n\t\t\tmemcpy(buf,seed,SHA_DIGEST_LENGTH);\n\t\t\tmemcpy(buf2,seed,SHA_DIGEST_LENGTH);\n\t\t\tfor (i=SHA_DIGEST_LENGTH-1; i >= 0; i--)\n\t\t\t\t{\n\t\t\t\tbuf[i]++;\n\t\t\t\tif (buf[i] != 0) break;\n\t\t\t\t}\n\t\t\tHASH(seed,SHA_DIGEST_LENGTH,md);\n\t\t\tHASH(buf,SHA_DIGEST_LENGTH,buf2);\n\t\t\tfor (i=0; i<SHA_DIGEST_LENGTH; i++)\n\t\t\t\tmd[i]^=buf2[i];\n\t\t\tmd[0]|=0x80;\n\t\t\tmd[SHA_DIGEST_LENGTH-1]|=0x01;\n\t\t\tif (!BN_bin2bn(md,SHA_DIGEST_LENGTH,q)) abort();\n\t\t\tif (DSA_is_prime(q,callback,cb_arg) > 0) break;\n\t\t\t}\n\t\tif (callback != NULL) callback(2,0,cb_arg);\n\t\tif (callback != NULL) callback(3,0,cb_arg);\n\t\tcounter=0;\n\t\tn=(bits-1)/160;\n\t\tb=(bits-1)-n*160;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tBN_zero(W);\n\t\t\tfor (k=0; k<=n; k++)\n\t\t\t\t{\n\t\t\t\tfor (i=SHA_DIGEST_LENGTH-1; i >= 0; i--)\n\t\t\t\t\t{\n\t\t\t\t\tbuf[i]++;\n\t\t\t\t\tif (buf[i] != 0) break;\n\t\t\t\t\t}\n\t\t\t\tHASH(buf,SHA_DIGEST_LENGTH,md);\n\t\t\t\tif (!BN_bin2bn(md,SHA_DIGEST_LENGTH,r0)) abort();\n\t\t\t\tBN_lshift(r0,r0,160*k);\n\t\t\t\tBN_add(W,W,r0);\n\t\t\t\t}\n\t\t\tBN_mask_bits(W,bits-1);\n\t\t\tBN_copy(X,W);\n\t\t\tBN_add(X,X,test);\n\t\t\tBN_lshift1(r0,q);\n\t\t\tBN_mod(c,X,r0,ctx);\n\t\t\tBN_sub(r0,c,BN_value_one());\n\t\t\tBN_sub(p,X,r0);\n\t\t\tif (BN_cmp(p,test) >= 0)\n\t\t\t\t{\n\t\t\t\tif (DSA_is_prime(p,callback,cb_arg) > 0)\n\t\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tcounter++;\n\t\t\tif (counter >= 4096) break;\n\t\t\tif (callback != NULL) callback(0,counter,cb_arg);\n\t\t\t}\n\t\t}\nend:\n\tif (callback != NULL) callback(2,1,cb_arg);\n BN_sub(test,p,BN_value_one());\n BN_div(r0,NULL,test,q,ctx);\n\tBN_set_word(test,h);\n\tBN_MONT_CTX_set(mont,p,ctx);\n\tfor (;;)\n\t\t{\n\t\tBN_mod_exp_mont(g,test,r0,p,ctx,mont);\n\t\tif (!BN_is_one(g)) break;\n\t\tBN_add(test,test,BN_value_one());\n\t\th++;\n\t\t}\n\tif (callback != NULL) callback(3,1,cb_arg);\n\tok=1;\nerr:\n\tif (!ok)\n\t\t{\n\t\tif (ret != NULL) DSA_free(ret);\n\t\t}\n\telse\n\t\t{\n\t\tret->p=BN_dup(p);\n\t\tret->q=BN_dup(q);\n\t\tret->g=BN_dup(g);\n\t\tif ((m > 1) && (seed_in != NULL)) memcpy(seed_in,seed,20);\n\t\tif (counter_ret != NULL) *counter_ret=counter;\n\t\tif (h_ret != NULL) *h_ret=h;\n\t\t}\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\tif (ctx != NULL) BN_CTX_free(ctx2);\n\tif (mont != NULL) BN_MONT_CTX_free(mont);\n\treturn(ok?ret:NULL);\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 BN_RECURSION_MONT\n\tif (mont->N.top < BN_MONT_CTX_SET_SIZE_WORD)\n#endif\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tmont->use_word=1;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,BN_BITS2);\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.d=buf;\n\t\ttmod.top=1;\n\t\ttmod.max=mod->max;\n\t\ttmod.neg=mod->neg;\n\t\tif ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,BN_BITS2);\n\t\tif (!BN_is_zero(&Ri))\n\t\t\t{\n#if 1\n\t\t\tBN_sub_word(&Ri,1);\n#else\n\t\t\tBN_usub(&Ri,&Ri,BN_value_one());\n#endif\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBN_set_word(&Ri,BN_MASK2);\n\t\t\t}\n\t\tBN_div(&Ri,NULL,&Ri,&tmod,ctx);\n\t\tmont->n0=Ri.d[0];\n\t\tBN_free(&Ri);\n\t\t}\n#ifdef BN_RECURSION_MONT\n\telse\n\t\t{\n\t\tmont->use_word=0;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n#if 1\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,mont->ri);\n#else\n\t\tBN_lshift(R,BN_value_one(),mont->ri);\n#endif\n\t\tif ((BN_mod_inverse(&Ri,R,mod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,mont->ri);\n#if 1\n\t\tBN_sub_word(&Ri,1);\n#else\n\t\tBN_usub(&Ri,&Ri,BN_value_one());\n#endif\n\t\tBN_div(&(mont->Ni),NULL,&Ri,mod,ctx);\n\t\tBN_free(&Ri);\n\t\t}\n#endif\n#if 1\n\tBN_zero(&(mont->RR));\n\tBN_set_bit(&(mont->RR),mont->ri*2);\n#else\n\tBN_lshift(mont->RR,BN_value_one(),mont->ri*2);\n#endif\n\tBN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx);\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tint i,n;\n\tif (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0);\n\tn=sizeof(BN_ULONG)/BN_BYTES;\n\ta->neg=0;\n\ta->top=0;\n\ta->d[0]=(BN_ULONG)w&BN_MASK2;\n\tif (a->d[0] != 0) a->top=1;\n\tfor (i=1; i<n; i++)\n\t\t{\n#ifndef SIXTY_FOUR_BIT\n\t\tw>>=BN_BITS4;\n\t\tw>>=BN_BITS4;\n#else\n\t\tw=0;\n#endif\n\t\ta->d[i]=(BN_ULONG)w&BN_MASK2;\n\t\tif (a->d[i] != 0) a->top=i+1;\n\t\t}\n\treturn(1);\n\t}', 'int BN_set_bit(BIGNUM *a, int n)\n\t{\n\tint i,j,k;\n\ti=n/BN_BITS2;\n\tj=n%BN_BITS2;\n\tif (a->top <= i)\n\t\t{\n\t\tif (bn_wexpand(a,i+1) == NULL) return(0);\n\t\tfor(k=a->top; k<i+1; k++)\n\t\t\ta->d[k]=0;\n\t\ta->top=i+1;\n\t\t}\n\ta->d[i]|=(((BN_ULONG)1)<<j);\n\treturn(1);\n\t}', 'int BN_mod_exp_mont(BIGNUM *rr, 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,ts=0;\n\tBIGNUM *d,*r;\n\tBIGNUM *aa;\n\tBIGNUM val[TABLE_SIZE];\n\tBN_MONT_CTX *mont=NULL;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\td= &(ctx->bn[ctx->tos++]);\n\tr= &(ctx->bn[ctx->tos++]);\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tBN_one(r);\n\t\treturn(1);\n\t\t}\n#if 1\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n#endif\n\t\t{\n\t\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\t\tif (!BN_MONT_CTX_set(mont,m,ctx)) goto err;\n\t\t}\n\tBN_init(&val[0]);\n\tts=1;\n\tif (BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tBN_mod(&(val[0]),a,m,ctx);\n\t\taa= &(val[0]);\n\t\t}\n\telse\n\t\taa=a;\n\tif (!BN_to_montgomery(&(val[0]),aa,mont,ctx)) goto err;\n\tif (!BN_mod_mul_montgomery(d,&(val[0]),&(val[0]),mont,ctx)) goto err;\n\tif (bits <= 20)\n\t\twindow=1;\n\telse if (bits >= 256)\n\t\twindow=5;\n\telse if (bits >= 128)\n\t\twindow=4;\n\telse\n\t\twindow=3;\n\tj=1<<(window-1);\n\tfor (i=1; i<j; i++)\n\t\t{\n\t\tBN_init(&(val[i]));\n\t\tif (!BN_mod_mul_montgomery(&(val[i]),&(val[i-1]),d,mont,ctx))\n\t\t\tgoto err;\n\t\t}\n\tts=i;\n\tstart=1;\n\twvalue=0;\n\twstart=bits-1;\n\twend=0;\n if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;\n\tfor (;;)\n\t\t{\n\t\tif (BN_is_bit_set(p,wstart) == 0)\n\t\t\t{\n\t\t\tif (!start)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (wstart == 0) break;\n\t\t\twstart--;\n\t\t\tcontinue;\n\t\t\t}\n\t\tj=wstart;\n\t\twvalue=1;\n\t\twend=0;\n\t\tfor (i=1; i<window; i++)\n\t\t\t{\n\t\t\tif (wstart-i < 0) break;\n\t\t\tif (BN_is_bit_set(p,wstart-i))\n\t\t\t\t{\n\t\t\t\twvalue<<=(i-wend);\n\t\t\t\twvalue|=1;\n\t\t\t\twend=i;\n\t\t\t\t}\n\t\t\t}\n\t\tj=wend+1;\n\t\tif (!start)\n\t\t\tfor (i=0; i<j; i++)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul_montgomery(r,r,&(val[wvalue>>1]),mont,ctx))\n\t\t\tgoto err;\n\t\twstart-=wend+1;\n\t\twvalue=0;\n\t\tstart=0;\n\t\tif (wstart < 0) break;\n\t\t}\n\tBN_from_montgomery(rr,r,mont,ctx);\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tctx->tos-=2;\n\tfor (i=0; i<ts; i++)\n\t\tBN_clear_free(&(val[i]));\n\treturn(ret);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,\n\t\t\t BN_MONT_CTX *mont, BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp,*tmp2;\n tmp= &(ctx->bn[ctx->tos]);\n tmp2= &(ctx->bn[ctx->tos]);\n\tctx->tos+=2;\n\tbn_check_top(tmp);\n\tbn_check_top(tmp2);\n\tif (a == b)\n\t\t{\n#if 0\n\t\tbn_wexpand(tmp,a->top*2);\n\t\tbn_wexpand(tmp2,a->top*4);\n\t\tbn_sqr_recursive(tmp->d,a->d,a->top,tmp2->d);\n\t\ttmp->top=a->top*2;\n\t\tif (tmp->d[tmp->top-1] == 0)\n\t\t\ttmp->top--;\n#else\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n\tctx->tos-=2;\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}', 'void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,\n\t BN_ULONG *t)\n\t{\n\tint n=n2/2,c1,c2;\n\tunsigned int neg,zero;\n\tBN_ULONG ln,lo,*p;\n#ifdef BN_COUNT\nprintf(" bn_mul_recursive %d * %d\\n",n2,n2);\n#endif\n#ifdef BN_MUL_COMBA\n if (n2 == 8)\n\t\t{\n\t\tbn_mul_comba8(r,a,b);\n\t\treturn;\n\t\t}\n#endif\n\tif (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t{\n\t\tbn_mul_normal(r,a,n2,b,n2);\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_words(a,&(a[n]),n);\n\tc2=bn_cmp_words(&(b[n]),b,n);\n\tzero=neg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tbreak;\n\tcase -3:\n\t\tzero=1;\n\t\tbreak;\n\tcase -2:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\t\tzero=1;\n\t\tbreak;\n\tcase 2:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\t\tzero=1;\n\t\tbreak;\n\tcase 4:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tbreak;\n\t\t}\n#ifdef BN_MUL_COMBA\n\tif (n == 4)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,8*sizeof(BN_ULONG));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_comba4(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse if (n == 8)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,16*sizeof(BN_ULONG));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_comba8(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse\n#endif\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tif (!zero)\n\t\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,p);\n\t\telse\n\t\t\tmemset(&(t[n2]),0,n2*sizeof(BN_ULONG));\n\t\tbn_mul_recursive(r,a,b,n,p);\n\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,p);\n\t\t}\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tif (neg)\n\t\t{\n\t\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\t\t}\n\telse\n\t\t{\n\t\tc1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));\n\t\t}\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < (BN_ULONG)c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)\n\t{\n#ifdef BN_LLONG\n\tBN_ULLONG t;\n#else\n\tBN_ULONG bl,bh;\n#endif\n\tBN_ULONG t1,t2;\n\tBN_ULONG c1,c2,c3;\n\tc1=0;\n\tc2=0;\n\tc3=0;\n\tmul_add_c(a[0],b[0],c1,c2,c3);\n\tr[0]=c1;\n\tc1=0;\n\tmul_add_c(a[0],b[1],c2,c3,c1);\n\tmul_add_c(a[1],b[0],c2,c3,c1);\n\tr[1]=c2;\n\tc2=0;\n\tmul_add_c(a[2],b[0],c3,c1,c2);\n\tmul_add_c(a[1],b[1],c3,c1,c2);\n\tmul_add_c(a[0],b[2],c3,c1,c2);\n\tr[2]=c3;\n\tc3=0;\n\tmul_add_c(a[0],b[3],c1,c2,c3);\n\tmul_add_c(a[1],b[2],c1,c2,c3);\n\tmul_add_c(a[2],b[1],c1,c2,c3);\n\tmul_add_c(a[3],b[0],c1,c2,c3);\n\tr[3]=c1;\n\tc1=0;\n\tmul_add_c(a[4],b[0],c2,c3,c1);\n\tmul_add_c(a[3],b[1],c2,c3,c1);\n\tmul_add_c(a[2],b[2],c2,c3,c1);\n\tmul_add_c(a[1],b[3],c2,c3,c1);\n\tmul_add_c(a[0],b[4],c2,c3,c1);\n\tr[4]=c2;\n\tc2=0;\n\tmul_add_c(a[0],b[5],c3,c1,c2);\n\tmul_add_c(a[1],b[4],c3,c1,c2);\n\tmul_add_c(a[2],b[3],c3,c1,c2);\n\tmul_add_c(a[3],b[2],c3,c1,c2);\n\tmul_add_c(a[4],b[1],c3,c1,c2);\n\tmul_add_c(a[5],b[0],c3,c1,c2);\n\tr[5]=c3;\n\tc3=0;\n\tmul_add_c(a[6],b[0],c1,c2,c3);\n\tmul_add_c(a[5],b[1],c1,c2,c3);\n\tmul_add_c(a[4],b[2],c1,c2,c3);\n\tmul_add_c(a[3],b[3],c1,c2,c3);\n\tmul_add_c(a[2],b[4],c1,c2,c3);\n\tmul_add_c(a[1],b[5],c1,c2,c3);\n\tmul_add_c(a[0],b[6],c1,c2,c3);\n\tr[6]=c1;\n\tc1=0;\n\tmul_add_c(a[0],b[7],c2,c3,c1);\n\tmul_add_c(a[1],b[6],c2,c3,c1);\n\tmul_add_c(a[2],b[5],c2,c3,c1);\n\tmul_add_c(a[3],b[4],c2,c3,c1);\n\tmul_add_c(a[4],b[3],c2,c3,c1);\n\tmul_add_c(a[5],b[2],c2,c3,c1);\n\tmul_add_c(a[6],b[1],c2,c3,c1);\n\tmul_add_c(a[7],b[0],c2,c3,c1);\n\tr[7]=c2;\n\tc2=0;\n\tmul_add_c(a[7],b[1],c3,c1,c2);\n\tmul_add_c(a[6],b[2],c3,c1,c2);\n\tmul_add_c(a[5],b[3],c3,c1,c2);\n\tmul_add_c(a[4],b[4],c3,c1,c2);\n\tmul_add_c(a[3],b[5],c3,c1,c2);\n\tmul_add_c(a[2],b[6],c3,c1,c2);\n\tmul_add_c(a[1],b[7],c3,c1,c2);\n\tr[8]=c3;\n\tc3=0;\n\tmul_add_c(a[2],b[7],c1,c2,c3);\n\tmul_add_c(a[3],b[6],c1,c2,c3);\n\tmul_add_c(a[4],b[5],c1,c2,c3);\n\tmul_add_c(a[5],b[4],c1,c2,c3);\n\tmul_add_c(a[6],b[3],c1,c2,c3);\n\tmul_add_c(a[7],b[2],c1,c2,c3);\n\tr[9]=c1;\n\tc1=0;\n\tmul_add_c(a[7],b[3],c2,c3,c1);\n\tmul_add_c(a[6],b[4],c2,c3,c1);\n\tmul_add_c(a[5],b[5],c2,c3,c1);\n\tmul_add_c(a[4],b[6],c2,c3,c1);\n\tmul_add_c(a[3],b[7],c2,c3,c1);\n\tr[10]=c2;\n\tc2=0;\n\tmul_add_c(a[4],b[7],c3,c1,c2);\n\tmul_add_c(a[5],b[6],c3,c1,c2);\n\tmul_add_c(a[6],b[5],c3,c1,c2);\n\tmul_add_c(a[7],b[4],c3,c1,c2);\n\tr[11]=c3;\n\tc3=0;\n\tmul_add_c(a[7],b[5],c1,c2,c3);\n\tmul_add_c(a[6],b[6],c1,c2,c3);\n\tmul_add_c(a[5],b[7],c1,c2,c3);\n\tr[12]=c1;\n\tc1=0;\n\tmul_add_c(a[6],b[7],c2,c3,c1);\n\tmul_add_c(a[7],b[6],c2,c3,c1);\n\tr[13]=c2;\n\tc2=0;\n\tmul_add_c(a[7],b[7],c3,c1,c2);\n\tr[14]=c3;\n\tr[15]=c1;\n\t}']
35,566
0
https://github.com/openssl/openssl/blob/67dc995eaf538ea309c6292a1a5073465201f55b/ssl/packet.c/#L48
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) { if (!ossl_assert(pkt->subs != NULL && len != 0)) return 0; if (pkt->maxsize - pkt->written < len) return 0; if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) { size_t newlen; size_t reflen; reflen = (len > pkt->buf->length) ? len : pkt->buf->length; if (reflen > SIZE_MAX / 2) { newlen = SIZE_MAX; } else { newlen = reflen * 2; if (newlen < DEFAULT_BUF_SIZE) newlen = DEFAULT_BUF_SIZE; } if (BUF_MEM_grow(pkt->buf, newlen) == 0) return 0; } if (allocbytes != NULL) *allocbytes = WPACKET_get_curr(pkt); return 1; }
['EXT_RETURN tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, unsigned int context,\n X509 *x, size_t chainidx, int *al)\n{\n s->s3->alpn_sent = 0;\n if (s->ext.alpn == NULL || !SSL_IS_FIRST_HANDSHAKE(s))\n return EXT_RETURN_NOT_SENT;\n if (!WPACKET_put_bytes_u16(pkt,\n TLSEXT_TYPE_application_layer_protocol_negotiation)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_sub_memcpy_u16(pkt, s->ext.alpn, s->ext.alpn_len)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_ALPN, ERR_R_INTERNAL_ERROR);\n return EXT_RETURN_FAIL;\n }\n s->s3->alpn_sent = 1;\n return EXT_RETURN_SENT;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n WPACKET_SUB *sub;\n unsigned char *lenchars;\n if (!ossl_assert(pkt->subs != NULL))\n return 0;\n sub = OPENSSL_zalloc(sizeof(*sub));\n if (sub == NULL)\n return 0;\n sub->parent = pkt->subs;\n pkt->subs = sub;\n sub->pwritten = pkt->written + lenbytes;\n sub->lenbytes = lenbytes;\n if (lenbytes == 0) {\n sub->packet_len = 0;\n return 1;\n }\n if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n return 0;\n sub->packet_len = lenchars - GETBUF(pkt);\n return 1;\n}', 'int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,\n size_t lenbytes)\n{\n if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)\n || !WPACKET_memcpy(pkt, src, len)\n || !WPACKET_close(pkt))\n return 0;\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!ossl_assert(pkt->subs != NULL && len != 0))\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {\n size_t newlen;\n size_t reflen;\n reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n if (reflen > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = reflen * 2;\n if (newlen < DEFAULT_BUF_SIZE)\n newlen = DEFAULT_BUF_SIZE;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n if (allocbytes != NULL)\n *allocbytes = WPACKET_get_curr(pkt);\n return 1;\n}']
35,567
0
https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/x509v3/v3_lib.c/#L127
int X509V3_EXT_add_alias(int nid_to, int nid_from) { X509V3_EXT_METHOD *ext, *tmpext; if(!(ext = X509V3_EXT_get_nid(nid_from))) { X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,X509V3_R_EXTENSION_NOT_FOUND); return 0; } if(!(tmpext = (X509V3_EXT_METHOD *)Malloc(sizeof(X509V3_EXT_METHOD)))) { X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,ERR_R_MALLOC_FAILURE); return 0; } *tmpext = *ext; tmpext->ext_nid = nid_to; tmpext->ext_flags |= X509V3_EXT_DYNAMIC; return 1; }
['int X509V3_EXT_add_alias(int nid_to, int nid_from)\n{\n\tX509V3_EXT_METHOD *ext, *tmpext;\n\tif(!(ext = X509V3_EXT_get_nid(nid_from))) {\n\t\tX509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,X509V3_R_EXTENSION_NOT_FOUND);\n\t\treturn 0;\n\t}\n\tif(!(tmpext = (X509V3_EXT_METHOD *)Malloc(sizeof(X509V3_EXT_METHOD)))) {\n\t\tX509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,ERR_R_MALLOC_FAILURE);\n\t\treturn 0;\n\t}\n\t*tmpext = *ext;\n\ttmpext->ext_nid = nid_to;\n\ttmpext->ext_flags |= X509V3_EXT_DYNAMIC;\n\treturn 1;\n}', 'X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid)\n{\n\tX509V3_EXT_METHOD tmp;\n\tint idx;\n\ttmp.ext_nid = nid;\n\tif(!ext_list || (tmp.ext_nid < 0) ) return NULL;\n\tidx = sk_find(ext_list, (char *)&tmp);\n\tif(idx == -1) return NULL;\n\treturn (X509V3_EXT_METHOD *)sk_value(ext_list, idx);\n}', 'char *sk_value(STACK *st, int i)\n{\n\tif(st == NULL) return NULL;\n\treturn st->data[i];\n}']
35,568
0
https://github.com/openssl/openssl/blob/eb47aae5d33de3d39379c15e20cbbd12ea8a29be/crypto/lhash/lhash.c/#L208
void *lh_delete(_LHASH *lh, const void *data) { unsigned long hash; LHASH_NODE *nn, **rn; void *ret; lh->error = 0; rn = getrn(lh, data, &hash); if (*rn == NULL) { lh->num_no_delete++; return (NULL); } else { nn = *rn; *rn = nn->next; ret = nn->data; OPENSSL_free(nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) contract(lh); return (ret); }
['static int ssl_free(BIO *a)\n{\n BIO_SSL *bs;\n if (a == NULL)\n return (0);\n bs = BIO_get_data(a);\n if (bs->ssl != NULL)\n SSL_shutdown(bs->ssl);\n if (BIO_get_shutdown(a)) {\n if (BIO_get_init(a))\n SSL_free(bs->ssl);\n BIO_clear_flags(a, ~0);\n BIO_set_init(a, 0);\n }\n OPENSSL_free(bs);\n return 1;\n}', 'void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n CRYPTO_atomic_add(&s->references, -1, &i, s->lock);\n REF_PRINT_COUNT("SSL", s);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(s->param);\n dane_final(&s->dane);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n if (s->bbio != NULL) {\n if (s->bbio == s->wbio) {\n s->wbio = BIO_pop(s->wbio);\n }\n BIO_free(s->bbio);\n s->bbio = NULL;\n }\n BIO_free_all(s->rbio);\n if (s->wbio != s->rbio)\n BIO_free_all(s->wbio);\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->tlsext_hostname);\n SSL_CTX_free(s->initial_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->tlsext_ecpointformatlist);\n OPENSSL_free(s->tlsext_ellipticcurvelist);\n#endif\n sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free);\n sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->tlsext_scts);\n#endif\n OPENSSL_free(s->tlsext_ocsp_resp);\n OPENSSL_free(s->alpn_client_proto_list);\n sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);\n sk_X509_pop_free(s->verified_chain, X509_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n RECORD_LAYER_release(&s->rlayer);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->next_proto_negotiated);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', 'int ssl_clear_bad_session(SSL *s)\n{\n if ((s->session != NULL) &&\n !(s->shutdown & SSL_SENT_SHUTDOWN) &&\n !(SSL_in_init(s) || SSL_in_before(s))) {\n SSL_CTX_remove_session(s->ctx, s->session);\n return (1);\n } else\n return (0);\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_THREAD_write_lock(ctx->lock);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, c);\n SSL_SESSION_list_remove(ctx, c);\n }\n if (lck)\n CRYPTO_THREAD_unlock(ctx->lock);\n if (ret) {\n r->not_resumable = 1;\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, r);\n SSL_SESSION_free(r);\n }\n } else\n ret = 0;\n return (ret);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *lh_delete(_LHASH *lh, const void *data)\n{\n unsigned long hash;\n LHASH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return (NULL);\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return (ret);\n}']
35,569
0
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/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); }
['int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)\n{\n int i, n = 0, len, nid, first, use_bn;\n BIGNUM *bl;\n unsigned long l;\n const unsigned char *p;\n char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];\n if (buf && buf_len > 0)\n buf[0] = \'\\0\';\n if ((a == NULL) || (a->data == NULL))\n return (0);\n if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {\n const char *s;\n s = OBJ_nid2ln(nid);\n if (s == NULL)\n s = OBJ_nid2sn(nid);\n if (s) {\n if (buf)\n OPENSSL_strlcpy(buf, s, buf_len);\n n = strlen(s);\n return n;\n }\n }\n len = a->length;\n p = a->data;\n first = 1;\n bl = NULL;\n while (len > 0) {\n l = 0;\n use_bn = 0;\n for (;;) {\n unsigned char c = *p++;\n len--;\n if ((len == 0) && (c & 0x80))\n goto err;\n if (use_bn) {\n if (!BN_add_word(bl, c & 0x7f))\n goto err;\n } else\n l |= c & 0x7f;\n if (!(c & 0x80))\n break;\n if (!use_bn && (l > (ULONG_MAX >> 7L))) {\n if (bl == NULL && (bl = BN_new()) == NULL)\n goto err;\n if (!BN_set_word(bl, l))\n goto err;\n use_bn = 1;\n }\n if (use_bn) {\n if (!BN_lshift(bl, bl, 7))\n goto err;\n } else\n l <<= 7L;\n }\n if (first) {\n first = 0;\n if (l >= 80) {\n i = 2;\n if (use_bn) {\n if (!BN_sub_word(bl, 80))\n goto err;\n } else\n l -= 80;\n } else {\n i = (int)(l / 40);\n l -= (long)(i * 40);\n }\n if (buf && (buf_len > 1)) {\n *buf++ = i + \'0\';\n *buf = \'\\0\';\n buf_len--;\n }\n n++;\n }\n if (use_bn) {\n char *bndec;\n bndec = BN_bn2dec(bl);\n if (!bndec)\n goto err;\n i = strlen(bndec);\n if (buf) {\n if (buf_len > 1) {\n *buf++ = \'.\';\n *buf = \'\\0\';\n buf_len--;\n }\n OPENSSL_strlcpy(buf, bndec, buf_len);\n if (i > buf_len) {\n buf += buf_len;\n buf_len = 0;\n } else {\n buf += i;\n buf_len -= i;\n }\n }\n n++;\n n += i;\n OPENSSL_free(bndec);\n } else {\n BIO_snprintf(tbuf, sizeof tbuf, ".%lu", l);\n i = strlen(tbuf);\n if (buf && (buf_len > 0)) {\n OPENSSL_strlcpy(buf, tbuf, buf_len);\n if (i > buf_len) {\n buf += buf_len;\n buf_len = 0;\n } else {\n buf += i;\n buf_len -= i;\n }\n }\n n += i;\n l = 0;\n }\n }\n BN_free(bl);\n return n;\n err:\n BN_free(bl);\n return -1;\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
35,570
0
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/x509/x509_att.c/#L323
int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, const void *data, int len) { ASN1_TYPE *ttmp; ASN1_STRING *stmp = NULL; int atype = 0; if (!attr) return 0; if (attrtype & MBSTRING_FLAG) { stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype, OBJ_obj2nid(attr->object)); if (!stmp) { X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_ASN1_LIB); return 0; } atype = stmp->type; } else if (len != -1) { if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL) goto err; if (!ASN1_STRING_set(stmp, data, len)) goto err; atype = attrtype; } if (attrtype == 0) return 1; if ((ttmp = ASN1_TYPE_new()) == NULL) goto err; if ((len == -1) && !(attrtype & MBSTRING_FLAG)) { if (!ASN1_TYPE_set1(ttmp, attrtype, data)) goto err; } else ASN1_TYPE_set(ttmp, atype, stmp); if (!sk_ASN1_TYPE_push(attr->set, ttmp)) goto err; return 1; err: X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE); return 0; }
['int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,\n const void *data, int len)\n{\n ASN1_TYPE *ttmp;\n ASN1_STRING *stmp = NULL;\n int atype = 0;\n if (!attr)\n return 0;\n if (attrtype & MBSTRING_FLAG) {\n stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,\n OBJ_obj2nid(attr->object));\n if (!stmp) {\n X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_ASN1_LIB);\n return 0;\n }\n atype = stmp->type;\n } else if (len != -1) {\n if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL)\n goto err;\n if (!ASN1_STRING_set(stmp, data, len))\n goto err;\n atype = attrtype;\n }\n if (attrtype == 0)\n return 1;\n if ((ttmp = ASN1_TYPE_new()) == NULL)\n goto err;\n if ((len == -1) && !(attrtype & MBSTRING_FLAG)) {\n if (!ASN1_TYPE_set1(ttmp, attrtype, data))\n goto err;\n } else\n ASN1_TYPE_set(ttmp, atype, stmp);\n if (!sk_ASN1_TYPE_push(attr->set, ttmp))\n goto err;\n return 1;\n err:\n X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE);\n return 0;\n}', 'ASN1_STRING *ASN1_STRING_type_new(int type)\n{\n ASN1_STRING *ret;\n ret = OPENSSL_zalloc(sizeof(*ret));\n if (ret == NULL) {\n ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->type = type;\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 ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)\n{\n unsigned char *c;\n const char *data = _data;\n if (len < 0) {\n if (data == NULL)\n return (0);\n else\n len = strlen(data);\n }\n if ((str->length < len) || (str->data == NULL)) {\n c = str->data;\n str->data = OPENSSL_realloc(c, len + 1);\n if (str->data == NULL) {\n ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);\n str->data = c;\n return (0);\n }\n }\n str->length = len;\n if (data != NULL) {\n memcpy(str->data, data, len);\n str->data[len] = '\\0';\n }\n return (1);\n}", 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n if (str == NULL)\n return CRYPTO_malloc(num, file, line);\n if (num == 0) {\n CRYPTO_free(str);\n return NULL;\n }\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n void *ret;\n CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n ret = realloc(str, num);\n CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n return ret;\n }\n#else\n (void)file;\n (void)line;\n#endif\n return realloc(str, num);\n}']
35,571
0
https://github.com/libav/libav/blob/dad7a9c7c0ae8ebc56f2e3a24e6fa4da5c2cd491/libavcodec/bitstream.h/#L139
static inline uint64_t get_val(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1); bc->bits >>= n; #else uint64_t ret = bc->bits >> (64 - n); bc->bits <<= n; #endif bc->bits_left -= n; return ret; }
['int\nff_rdt_parse_header(const uint8_t *buf, int len,\n int *pset_id, int *pseq_no, int *pstream_id,\n int *pis_keyframe, uint32_t *ptimestamp)\n{\n BitstreamContext bc;\n int consumed = 0, set_id, seq_no, stream_id, is_keyframe,\n len_included, need_reliable;\n uint32_t timestamp;\n while (len >= 5 && buf[1] == 0xFF ) {\n int pkt_len;\n if (!(buf[0] & 0x80))\n return -1;\n pkt_len = AV_RB16(buf+3);\n buf += pkt_len;\n len -= pkt_len;\n consumed += pkt_len;\n }\n if (len < 16)\n return -1;\n bitstream_init8(&bc, buf, len);\n len_included = bitstream_read_bit(&bc);\n need_reliable = bitstream_read_bit(&bc);\n set_id = bitstream_read(&bc, 5);\n bitstream_skip(&bc, 1);\n seq_no = bitstream_read(&bc, 16);\n if (len_included)\n bitstream_skip(&bc, 16);\n bitstream_skip(&bc, 2);\n stream_id = bitstream_read(&bc, 5);\n is_keyframe = !bitstream_read_bit(&bc);\n timestamp = bitstream_read(&bc, 32);\n if (set_id == 0x1f)\n set_id = bitstream_read(&bc, 16);\n if (need_reliable)\n bitstream_skip(&bc, 16);\n if (stream_id == 0x1f)\n stream_id = bitstream_read(&bc, 16);\n if (pset_id) *pset_id = set_id;\n if (pseq_no) *pseq_no = seq_no;\n if (pstream_id) *pstream_id = stream_id;\n if (pis_keyframe) *pis_keyframe = is_keyframe;\n if (ptimestamp) *ptimestamp = timestamp;\n return consumed + (bitstream_tell(&bc) >> 3);\n}', 'static inline int bitstream_init8(BitstreamContext *bc, const uint8_t *buffer,\n unsigned byte_size)\n{\n if (byte_size > INT_MAX / 8)\n return AVERROR_INVALIDDATA;\n return bitstream_init(bc, buffer, byte_size * 8);\n}', 'static inline unsigned bitstream_read_bit(BitstreamContext *bc)\n{\n if (!bc->bits_left)\n refill_64(bc);\n return get_val(bc, 1);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
35,572
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L331
static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){ LOAD_LEFT_EDGE src[0+0*stride]=(l0 + l1 + 1)>>1; src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2; src[2+0*stride]= src[0+1*stride]=(l1 + l2 + 1)>>1; src[3+0*stride]= src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2; src[2+1*stride]= src[0+2*stride]=(l2 + l3 + 1)>>1; src[3+1*stride]= src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2; src[3+2*stride]= src[1+3*stride]= src[0+3*stride]= src[2+2*stride]= src[2+3*stride]= src[3+3*stride]=l3; }
['static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){\n LOAD_LEFT_EDGE\n src[0+0*stride]=(l0 + l1 + 1)>>1;\n src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;\n src[2+0*stride]=\n src[0+1*stride]=(l1 + l2 + 1)>>1;\n src[3+0*stride]=\n src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;\n src[2+1*stride]=\n src[0+2*stride]=(l2 + l3 + 1)>>1;\n src[3+1*stride]=\n src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;\n src[3+2*stride]=\n src[1+3*stride]=\n src[0+3*stride]=\n src[2+2*stride]=\n src[2+3*stride]=\n src[3+3*stride]=l3;\n}']
35,573
0
https://github.com/openssl/openssl/blob/9f519addc09b2005fa8c6cde36e3267de02577bb/crypto/bio/b_print.c/#L425
static int _dopr(char **sbuffer, char **buffer, size_t *maxlen, size_t *retlen, int *truncated, const char *format, va_list args) { char ch; LLONG value; LDOUBLE fvalue; char *strvalue; int min; int max; int state; int flags; int cflags; size_t currlen; state = DP_S_DEFAULT; flags = currlen = cflags = min = 0; max = -1; ch = *format++; while (state != DP_S_DONE) { if (ch == '\0' || (buffer == NULL && currlen >= *maxlen)) state = DP_S_DONE; switch (state) { case DP_S_DEFAULT: if (ch == '%') state = DP_S_FLAGS; else if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch)) return 0; ch = *format++; break; case DP_S_FLAGS: switch (ch) { case '-': flags |= DP_F_MINUS; ch = *format++; break; case '+': flags |= DP_F_PLUS; ch = *format++; break; case ' ': flags |= DP_F_SPACE; ch = *format++; break; case '#': flags |= DP_F_NUM; ch = *format++; break; case '0': flags |= DP_F_ZERO; ch = *format++; break; default: state = DP_S_MIN; break; } break; case DP_S_MIN: if (isdigit((unsigned char)ch)) { min = 10 * min + char_to_int(ch); ch = *format++; } else if (ch == '*') { min = va_arg(args, int); ch = *format++; state = DP_S_DOT; } else state = DP_S_DOT; break; case DP_S_DOT: if (ch == '.') { state = DP_S_MAX; ch = *format++; } else state = DP_S_MOD; break; case DP_S_MAX: if (isdigit((unsigned char)ch)) { if (max < 0) max = 0; max = 10 * max + char_to_int(ch); ch = *format++; } else if (ch == '*') { max = va_arg(args, int); ch = *format++; state = DP_S_MOD; } else state = DP_S_MOD; break; case DP_S_MOD: switch (ch) { case 'h': cflags = DP_C_SHORT; ch = *format++; break; case 'l': if (*format == 'l') { cflags = DP_C_LLONG; format++; } else cflags = DP_C_LONG; ch = *format++; break; case 'q': cflags = DP_C_LLONG; ch = *format++; break; case 'L': cflags = DP_C_LDOUBLE; ch = *format++; break; default: break; } state = DP_S_CONV; break; case DP_S_CONV: switch (ch) { case 'd': case 'i': switch (cflags) { case DP_C_SHORT: value = (short int)va_arg(args, int); break; case DP_C_LONG: value = va_arg(args, long int); break; case DP_C_LLONG: value = va_arg(args, LLONG); break; default: value = va_arg(args, int); break; } if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min, max, flags)) return 0; break; case 'X': flags |= DP_F_UP; case 'x': case 'o': case 'u': flags |= DP_F_UNSIGNED; switch (cflags) { case DP_C_SHORT: value = (unsigned short int)va_arg(args, unsigned int); break; case DP_C_LONG: value = (LLONG) va_arg(args, unsigned long int); break; case DP_C_LLONG: value = va_arg(args, unsigned LLONG); break; default: value = (LLONG) va_arg(args, unsigned int); break; } if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, ch == 'o' ? 8 : (ch == 'u' ? 10 : 16), min, max, flags)) return 0; break; case 'f': if (cflags == DP_C_LDOUBLE) fvalue = va_arg(args, LDOUBLE); else fvalue = va_arg(args, double); if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max, flags)) return 0; break; case 'E': flags |= DP_F_UP; case 'e': if (cflags == DP_C_LDOUBLE) fvalue = va_arg(args, LDOUBLE); else fvalue = va_arg(args, double); break; case 'G': flags |= DP_F_UP; case 'g': if (cflags == DP_C_LDOUBLE) fvalue = va_arg(args, LDOUBLE); else fvalue = va_arg(args, double); break; case 'c': if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, va_arg(args, int))) return 0; break; case 's': strvalue = va_arg(args, char *); if (max < 0) { if (buffer) max = INT_MAX; else max = *maxlen; } if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue, flags, min, max)) return 0; break; case 'p': value = (size_t)va_arg(args, void *); if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 16, min, max, flags | DP_F_NUM)) return 0; break; case 'n': if (cflags == DP_C_SHORT) { short int *num; num = va_arg(args, short int *); *num = currlen; } else if (cflags == DP_C_LONG) { long int *num; num = va_arg(args, long int *); *num = (long int)currlen; } else if (cflags == DP_C_LLONG) { LLONG *num; num = va_arg(args, LLONG *); *num = (LLONG) currlen; } else { int *num; num = va_arg(args, int *); *num = currlen; } break; case '%': if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch)) return 0; break; case 'w': ch = *format++; break; default: break; } ch = *format++; state = DP_S_DEFAULT; flags = cflags = min = 0; max = -1; break; case DP_S_DONE: break; default: break; } } *truncated = (currlen > *maxlen - 1); if (*truncated) currlen = *maxlen - 1; if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0')) return 0; *retlen = currlen - 1; return 1; }
['long BIO_debug_callback(BIO *bio, int cmd, const char *argp,\n int argi, long argl, long ret)\n{\n BIO *b;\n char buf[256];\n char *p;\n long r = 1;\n int len;\n size_t p_maxlen;\n if (BIO_CB_RETURN & cmd)\n r = ret;\n len = BIO_snprintf(buf,sizeof buf,"BIO[%p]: ",(void *)bio);\n if (len < 0)\n len = 0;\n p = buf + len;\n p_maxlen = sizeof(buf) - len;\n switch (cmd) {\n case BIO_CB_FREE:\n BIO_snprintf(p, p_maxlen, "Free - %s\\n", bio->method->name);\n break;\n case BIO_CB_READ:\n if (bio->method->type & BIO_TYPE_DESCRIPTOR)\n BIO_snprintf(p, p_maxlen, "read(%d,%lu) - %s fd=%d\\n",\n bio->num, (unsigned long)argi,\n bio->method->name, bio->num);\n else\n BIO_snprintf(p, p_maxlen, "read(%d,%lu) - %s\\n",\n bio->num, (unsigned long)argi, bio->method->name);\n break;\n case BIO_CB_WRITE:\n if (bio->method->type & BIO_TYPE_DESCRIPTOR)\n BIO_snprintf(p, p_maxlen, "write(%d,%lu) - %s fd=%d\\n",\n bio->num, (unsigned long)argi,\n bio->method->name, bio->num);\n else\n BIO_snprintf(p, p_maxlen, "write(%d,%lu) - %s\\n",\n bio->num, (unsigned long)argi, bio->method->name);\n break;\n case BIO_CB_PUTS:\n BIO_snprintf(p, p_maxlen, "puts() - %s\\n", bio->method->name);\n break;\n case BIO_CB_GETS:\n BIO_snprintf(p, p_maxlen, "gets(%lu) - %s\\n", (unsigned long)argi,\n bio->method->name);\n break;\n case BIO_CB_CTRL:\n BIO_snprintf(p, p_maxlen, "ctrl(%lu) - %s\\n", (unsigned long)argi,\n bio->method->name);\n break;\n case BIO_CB_RETURN | BIO_CB_READ:\n BIO_snprintf(p, p_maxlen, "read return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_WRITE:\n BIO_snprintf(p, p_maxlen, "write return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_GETS:\n BIO_snprintf(p, p_maxlen, "gets return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_PUTS:\n BIO_snprintf(p, p_maxlen, "puts return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_CTRL:\n BIO_snprintf(p, p_maxlen, "ctrl return %ld\\n", ret);\n break;\n default:\n BIO_snprintf(p, p_maxlen, "bio callback - unknown type (%d)\\n", cmd);\n break;\n }\n b = (BIO *)bio->cb_arg;\n if (b != NULL)\n BIO_write(b, buf, strlen(buf));\n#if !defined(OPENSSL_NO_STDIO)\n else\n fputs(buf, stderr);\n#endif\n return (r);\n}', 'int BIO_snprintf(char *buf, size_t n, const char *format, ...)\n{\n va_list args;\n int ret;\n va_start(args, format);\n ret = BIO_vsnprintf(buf, n, format, args);\n va_end(args);\n return (ret);\n}', 'int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)\n{\n size_t retlen;\n int truncated;\n if(!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))\n return -1;\n if (truncated)\n return -1;\n else\n return (retlen <= INT_MAX) ? (int)retlen : -1;\n}', "static int\n_dopr(char **sbuffer,\n char **buffer,\n size_t *maxlen,\n size_t *retlen, int *truncated, const char *format, va_list args)\n{\n char ch;\n LLONG value;\n LDOUBLE fvalue;\n char *strvalue;\n int min;\n int max;\n int state;\n int flags;\n int cflags;\n size_t currlen;\n state = DP_S_DEFAULT;\n flags = currlen = cflags = min = 0;\n max = -1;\n ch = *format++;\n while (state != DP_S_DONE) {\n if (ch == '\\0' || (buffer == NULL && currlen >= *maxlen))\n state = DP_S_DONE;\n switch (state) {\n case DP_S_DEFAULT:\n if (ch == '%')\n state = DP_S_FLAGS;\n else\n if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))\n return 0;\n ch = *format++;\n break;\n case DP_S_FLAGS:\n switch (ch) {\n case '-':\n flags |= DP_F_MINUS;\n ch = *format++;\n break;\n case '+':\n flags |= DP_F_PLUS;\n ch = *format++;\n break;\n case ' ':\n flags |= DP_F_SPACE;\n ch = *format++;\n break;\n case '#':\n flags |= DP_F_NUM;\n ch = *format++;\n break;\n case '0':\n flags |= DP_F_ZERO;\n ch = *format++;\n break;\n default:\n state = DP_S_MIN;\n break;\n }\n break;\n case DP_S_MIN:\n if (isdigit((unsigned char)ch)) {\n min = 10 * min + char_to_int(ch);\n ch = *format++;\n } else if (ch == '*') {\n min = va_arg(args, int);\n ch = *format++;\n state = DP_S_DOT;\n } else\n state = DP_S_DOT;\n break;\n case DP_S_DOT:\n if (ch == '.') {\n state = DP_S_MAX;\n ch = *format++;\n } else\n state = DP_S_MOD;\n break;\n case DP_S_MAX:\n if (isdigit((unsigned char)ch)) {\n if (max < 0)\n max = 0;\n max = 10 * max + char_to_int(ch);\n ch = *format++;\n } else if (ch == '*') {\n max = va_arg(args, int);\n ch = *format++;\n state = DP_S_MOD;\n } else\n state = DP_S_MOD;\n break;\n case DP_S_MOD:\n switch (ch) {\n case 'h':\n cflags = DP_C_SHORT;\n ch = *format++;\n break;\n case 'l':\n if (*format == 'l') {\n cflags = DP_C_LLONG;\n format++;\n } else\n cflags = DP_C_LONG;\n ch = *format++;\n break;\n case 'q':\n cflags = DP_C_LLONG;\n ch = *format++;\n break;\n case 'L':\n cflags = DP_C_LDOUBLE;\n ch = *format++;\n break;\n default:\n break;\n }\n state = DP_S_CONV;\n break;\n case DP_S_CONV:\n switch (ch) {\n case 'd':\n case 'i':\n switch (cflags) {\n case DP_C_SHORT:\n value = (short int)va_arg(args, int);\n break;\n case DP_C_LONG:\n value = va_arg(args, long int);\n break;\n case DP_C_LLONG:\n value = va_arg(args, LLONG);\n break;\n default:\n value = va_arg(args, int);\n break;\n }\n if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,\n max, flags))\n return 0;\n break;\n case 'X':\n flags |= DP_F_UP;\n case 'x':\n case 'o':\n case 'u':\n flags |= DP_F_UNSIGNED;\n switch (cflags) {\n case DP_C_SHORT:\n value = (unsigned short int)va_arg(args, unsigned int);\n break;\n case DP_C_LONG:\n value = (LLONG) va_arg(args, unsigned long int);\n break;\n case DP_C_LLONG:\n value = va_arg(args, unsigned LLONG);\n break;\n default:\n value = (LLONG) va_arg(args, unsigned int);\n break;\n }\n if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,\n ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),\n min, max, flags))\n return 0;\n break;\n case 'f':\n if (cflags == DP_C_LDOUBLE)\n fvalue = va_arg(args, LDOUBLE);\n else\n fvalue = va_arg(args, double);\n if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,\n flags))\n return 0;\n break;\n case 'E':\n flags |= DP_F_UP;\n case 'e':\n if (cflags == DP_C_LDOUBLE)\n fvalue = va_arg(args, LDOUBLE);\n else\n fvalue = va_arg(args, double);\n break;\n case 'G':\n flags |= DP_F_UP;\n case 'g':\n if (cflags == DP_C_LDOUBLE)\n fvalue = va_arg(args, LDOUBLE);\n else\n fvalue = va_arg(args, double);\n break;\n case 'c':\n if(!doapr_outch(sbuffer, buffer, &currlen, maxlen,\n va_arg(args, int)))\n return 0;\n break;\n case 's':\n strvalue = va_arg(args, char *);\n if (max < 0) {\n if (buffer)\n max = INT_MAX;\n else\n max = *maxlen;\n }\n if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,\n flags, min, max))\n return 0;\n break;\n case 'p':\n value = (size_t)va_arg(args, void *);\n if (!fmtint(sbuffer, buffer, &currlen, maxlen,\n value, 16, min, max, flags | DP_F_NUM))\n return 0;\n break;\n case 'n':\n if (cflags == DP_C_SHORT) {\n short int *num;\n num = va_arg(args, short int *);\n *num = currlen;\n } else if (cflags == DP_C_LONG) {\n long int *num;\n num = va_arg(args, long int *);\n *num = (long int)currlen;\n } else if (cflags == DP_C_LLONG) {\n LLONG *num;\n num = va_arg(args, LLONG *);\n *num = (LLONG) currlen;\n } else {\n int *num;\n num = va_arg(args, int *);\n *num = currlen;\n }\n break;\n case '%':\n if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))\n return 0;\n break;\n case 'w':\n ch = *format++;\n break;\n default:\n break;\n }\n ch = *format++;\n state = DP_S_DEFAULT;\n flags = cflags = min = 0;\n max = -1;\n break;\n case DP_S_DONE:\n break;\n default:\n break;\n }\n }\n *truncated = (currlen > *maxlen - 1);\n if (*truncated)\n currlen = *maxlen - 1;\n if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\\0'))\n return 0;\n *retlen = currlen - 1;\n return 1;\n}"]
35,574
0
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/x509/x509_vfy.c/#L584
static int check_chain_extensions(X509_STORE_CTX *ctx) { #ifdef OPENSSL_NO_CHAIN_VERIFY return 1; #else int i, ok = 0, must_be_ca, plen = 0; X509 *x; int (*cb) (int xok, X509_STORE_CTX *xctx); int proxy_path_length = 0; int purpose; int allow_proxy_certs; cb = ctx->verify_cb; must_be_ca = -1; if (ctx->parent) { allow_proxy_certs = 0; purpose = X509_PURPOSE_CRL_SIGN; } else { allow_proxy_certs = ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS); if (getenv("OPENSSL_ALLOW_PROXY_CERTS")) allow_proxy_certs = 1; purpose = ctx->param->purpose; } for (i = 0; i < ctx->last_untrusted; i++) { int ret; x = sk_X509_value(ctx->chain, i); if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) && (x->ex_flags & EXFLAG_CRITICAL)) { ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) { ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } ret = X509_check_ca(x); switch (must_be_ca) { case -1: if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) && (ret != 1) && (ret != 0)) { ret = 0; ctx->error = X509_V_ERR_INVALID_CA; } else ret = 1; break; case 0: if (ret != 0) { ret = 0; ctx->error = X509_V_ERR_INVALID_NON_CA; } else ret = 1; break; default: if ((ret == 0) || ((ctx->param->flags & X509_V_FLAG_X509_STRICT) && (ret != 1))) { ret = 0; ctx->error = X509_V_ERR_INVALID_CA; } else ret = 1; break; } if (ret == 0) { ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } if (ctx->param->purpose > 0) { ret = X509_check_purpose(x, purpose, must_be_ca > 0); if ((ret == 0) || ((ctx->param->flags & X509_V_FLAG_X509_STRICT) && (ret != 1))) { ctx->error = X509_V_ERR_INVALID_PURPOSE; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } } if ((i > 1) && !(x->ex_flags & EXFLAG_SI) && (x->ex_pathlen != -1) && (plen > (x->ex_pathlen + proxy_path_length + 1))) { ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } if (!(x->ex_flags & EXFLAG_SI)) plen++; if (x->ex_flags & EXFLAG_PROXY) { if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) { ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } proxy_path_length++; must_be_ca = 0; } else must_be_ca = 1; } ok = 1; end: return ok; #endif }
['static int check_chain_extensions(X509_STORE_CTX *ctx)\n{\n#ifdef OPENSSL_NO_CHAIN_VERIFY\n return 1;\n#else\n int i, ok = 0, must_be_ca, plen = 0;\n X509 *x;\n int (*cb) (int xok, X509_STORE_CTX *xctx);\n int proxy_path_length = 0;\n int purpose;\n int allow_proxy_certs;\n cb = ctx->verify_cb;\n must_be_ca = -1;\n if (ctx->parent) {\n allow_proxy_certs = 0;\n purpose = X509_PURPOSE_CRL_SIGN;\n } else {\n allow_proxy_certs =\n ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);\n if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))\n allow_proxy_certs = 1;\n purpose = ctx->param->purpose;\n }\n for (i = 0; i < ctx->last_untrusted; i++) {\n int ret;\n x = sk_X509_value(ctx->chain, i);\n if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)\n && (x->ex_flags & EXFLAG_CRITICAL)) {\n ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;\n ctx->error_depth = i;\n ctx->current_cert = x;\n ok = cb(0, ctx);\n if (!ok)\n goto end;\n }\n if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {\n ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;\n ctx->error_depth = i;\n ctx->current_cert = x;\n ok = cb(0, ctx);\n if (!ok)\n goto end;\n }\n ret = X509_check_ca(x);\n switch (must_be_ca) {\n case -1:\n if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n && (ret != 1) && (ret != 0)) {\n ret = 0;\n ctx->error = X509_V_ERR_INVALID_CA;\n } else\n ret = 1;\n break;\n case 0:\n if (ret != 0) {\n ret = 0;\n ctx->error = X509_V_ERR_INVALID_NON_CA;\n } else\n ret = 1;\n break;\n default:\n if ((ret == 0)\n || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n && (ret != 1))) {\n ret = 0;\n ctx->error = X509_V_ERR_INVALID_CA;\n } else\n ret = 1;\n break;\n }\n if (ret == 0) {\n ctx->error_depth = i;\n ctx->current_cert = x;\n ok = cb(0, ctx);\n if (!ok)\n goto end;\n }\n if (ctx->param->purpose > 0) {\n ret = X509_check_purpose(x, purpose, must_be_ca > 0);\n if ((ret == 0)\n || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n && (ret != 1))) {\n ctx->error = X509_V_ERR_INVALID_PURPOSE;\n ctx->error_depth = i;\n ctx->current_cert = x;\n ok = cb(0, ctx);\n if (!ok)\n goto end;\n }\n }\n if ((i > 1) && !(x->ex_flags & EXFLAG_SI)\n && (x->ex_pathlen != -1)\n && (plen > (x->ex_pathlen + proxy_path_length + 1))) {\n ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;\n ctx->error_depth = i;\n ctx->current_cert = x;\n ok = cb(0, ctx);\n if (!ok)\n goto end;\n }\n if (!(x->ex_flags & EXFLAG_SI))\n plen++;\n if (x->ex_flags & EXFLAG_PROXY) {\n if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {\n ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;\n ctx->error_depth = i;\n ctx->current_cert = x;\n ok = cb(0, ctx);\n if (!ok)\n goto end;\n }\n proxy_path_length++;\n must_be_ca = 0;\n } else\n must_be_ca = 1;\n }\n ok = 1;\n end:\n return ok;\n#endif\n}', 'void *sk_value(const _STACK *st, int i)\n{\n if (!st || (i < 0) || (i >= st->num))\n return NULL;\n return st->data[i];\n}']
35,575
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L536
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); }
['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_pseudo_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}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return (ret);\n}', 'int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n BN_MONT_CTX *mont = NULL;\n int b, bits, ret = 0;\n int r_is_one;\n BN_ULONG w, next_w;\n BIGNUM *d, *r, *t;\n BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n (BN_mul_word(r, (w)) && \\\n ( \\\n (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return -1;\n }\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n if (m->top == 1)\n a %= m->d[0];\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n if (a == 0) {\n BN_zero(rr);\n ret = 1;\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (d == NULL || r == NULL || t == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n r_is_one = 1;\n w = a;\n for (b = bits - 2; b >= 0; b--) {\n next_w = w * w;\n if ((next_w / w) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = 1;\n }\n w = next_w;\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (BN_is_bit_set(p, b)) {\n next_w = w * a;\n if ((next_w / a) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = a;\n }\n w = next_w;\n }\n }\n if (w != 1) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n }\n if (r_is_one) {\n if (!BN_one(rr))\n goto err;\n } else {\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n }\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return (ret);\n}']
35,576
0
https://github.com/libav/libav/blob/c5254755c0154dcc7bb1191a84e6e7cf0106343b/libavformat/utils.c/#L2570
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); }
['void ff_rtsp_close_streams(AVFormatContext *s)\n{\n RTSPState *rt = s->priv_data;\n int i;\n RTSPStream *rtsp_st;\n ff_rtsp_undo_setup(s);\n for (i = 0; i < rt->nb_rtsp_streams; i++) {\n rtsp_st = rt->rtsp_streams[i];\n if (rtsp_st) {\n if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)\n rtsp_st->dynamic_handler->free(\n rtsp_st->dynamic_protocol_context);\n av_free(rtsp_st);\n }\n }\n av_free(rt->rtsp_streams);\n if (rt->asf_ctx) {\n avformat_close_input(&rt->asf_ctx);\n }\n av_free(rt->p);\n av_free(rt->recvbuf);\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}']
35,577
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L231
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int ec_GFp_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}', '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}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
35,578
0
https://github.com/openssl/openssl/blob/b90506e995d44dee0ef4dd0324b56b59154256c2/ssl/statem/statem_clnt.c/#L2165
static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt, int *al) { #ifndef OPENSSL_NO_RSA unsigned char *encdata = NULL; EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *pctx = NULL; size_t enclen; unsigned char *pms = NULL; size_t pmslen = 0; if (s->session->peer == NULL) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); return 0; } pkey = X509_get0_pubkey(s->session->peer); if (EVP_PKEY_get0_RSA(pkey) == NULL) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); return 0; } pmslen = SSL_MAX_MASTER_KEY_LENGTH; pms = OPENSSL_malloc(pmslen); if (pms == NULL) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE); *al = SSL_AD_INTERNAL_ERROR; return 0; } pms[0] = s->client_version >> 8; pms[1] = s->client_version & 0xff; if (RAND_bytes(pms + 2, pmslen - 2) <= 0) { goto err; } if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); goto err; } pctx = EVP_PKEY_CTX_new(pkey, NULL); if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB); goto err; } if (!WPACKET_allocate_bytes(pkt, enclen, &encdata) || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT); goto err; } EVP_PKEY_CTX_free(pctx); pctx = NULL; # ifdef PKCS1_CHECK if (s->options & SSL_OP_PKCS1_CHECK_1) (*p)[1]++; if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0] = 0x70; # endif if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); goto err; } s->s3->tmp.pms = pms; s->s3->tmp.pmslen = pmslen; return 1; err: OPENSSL_clear_free(pms, pmslen); EVP_PKEY_CTX_free(pctx); return 0; #else SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); *al = SSL_AD_INTERNAL_ERROR; return 0; #endif }
['static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt, int *al)\n{\n#ifndef OPENSSL_NO_RSA\n unsigned char *encdata = NULL;\n EVP_PKEY *pkey = NULL;\n EVP_PKEY_CTX *pctx = NULL;\n size_t enclen;\n unsigned char *pms = NULL;\n size_t pmslen = 0;\n if (s->session->peer == NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n pkey = X509_get0_pubkey(s->session->peer);\n if (EVP_PKEY_get0_RSA(pkey) == NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n pmslen = SSL_MAX_MASTER_KEY_LENGTH;\n pms = OPENSSL_malloc(pmslen);\n if (pms == NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE);\n *al = SSL_AD_INTERNAL_ERROR;\n return 0;\n }\n pms[0] = s->client_version >> 8;\n pms[1] = s->client_version & 0xff;\n if (RAND_bytes(pms + 2, pmslen - 2) <= 0) {\n goto err;\n }\n if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n pctx = EVP_PKEY_CTX_new(pkey, NULL);\n if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0\n || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB);\n goto err;\n }\n if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)\n || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT);\n goto err;\n }\n EVP_PKEY_CTX_free(pctx);\n pctx = NULL;\n# ifdef PKCS1_CHECK\n if (s->options & SSL_OP_PKCS1_CHECK_1)\n (*p)[1]++;\n if (s->options & SSL_OP_PKCS1_CHECK_2)\n tmp_buf[0] = 0x70;\n# endif\n if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n s->s3->tmp.pms = pms;\n s->s3->tmp.pmslen = pmslen;\n return 1;\n err:\n OPENSSL_clear_free(pms, pmslen);\n EVP_PKEY_CTX_free(pctx);\n return 0;\n#else\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n *al = SSL_AD_INTERNAL_ERROR;\n return 0;\n#endif\n}', 'EVP_PKEY *X509_get0_pubkey(const X509 *x)\n{\n if (x == NULL)\n return NULL;\n return X509_PUBKEY_get0(x->cert_info.key);\n}', 'EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key)\n{\n EVP_PKEY *ret = NULL;\n if (key == NULL || key->public_key == NULL)\n return NULL;\n if (key->pkey != NULL)\n return key->pkey;\n x509_pubkey_decode(&ret, key);\n if (ret != NULL) {\n X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);\n EVP_PKEY_free(ret);\n }\n return NULL;\n}', 'static int x509_pubkey_decode(EVP_PKEY **ppkey, X509_PUBKEY *key)\n {\n EVP_PKEY *pkey = EVP_PKEY_new();\n if (pkey == NULL) {\n X509err(X509_F_X509_PUBKEY_DECODE, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {\n X509err(X509_F_X509_PUBKEY_DECODE, X509_R_UNSUPPORTED_ALGORITHM);\n goto error;\n }\n if (pkey->ameth->pub_decode) {\n if (!pkey->ameth->pub_decode(pkey, key)) {\n X509err(X509_F_X509_PUBKEY_DECODE, X509_R_PUBLIC_KEY_DECODE_ERROR);\n goto error;\n }\n } else {\n X509err(X509_F_X509_PUBKEY_DECODE, X509_R_METHOD_NOT_SUPPORTED);\n goto error;\n }\n *ppkey = pkey;\n return 1;\n error:\n EVP_PKEY_free(pkey);\n return 0;\n}', 'RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)\n{\n if (pkey->type != EVP_PKEY_RSA) {\n EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);\n return NULL;\n }\n return pkey->pkey.rsa;\n}']
35,579
0
https://github.com/openssl/openssl/blob/b2e54eb834e2d5a79d03f12a818d68f82c0e3d13/crypto/err/err.c/#L728
void ERR_add_error_vdata(int num, va_list args) { int i, n, s; char *str, *p, *a; s = 80; str = OPENSSL_malloc(s + 1); if (str == NULL) return; str[0] = '\0'; n = 0; for (i = 0; i < num; i++) { a = va_arg(args, char *); if (a != NULL) { n += strlen(a); if (n > s) { s = n + 20; p = OPENSSL_realloc(str, s + 1); if (p == NULL) { OPENSSL_free(str); return; } str = p; } OPENSSL_strlcat(str, a, (size_t)s + 1); } } ERR_set_error_data(str, ERR_TXT_MALLOCED | ERR_TXT_STRING); }
["void ERR_add_error_vdata(int num, va_list args)\n{\n int i, n, s;\n char *str, *p, *a;\n s = 80;\n str = OPENSSL_malloc(s + 1);\n if (str == NULL)\n return;\n str[0] = '\\0';\n n = 0;\n for (i = 0; i < num; i++) {\n a = va_arg(args, char *);\n if (a != NULL) {\n n += strlen(a);\n if (n > s) {\n s = n + 20;\n p = OPENSSL_realloc(str, s + 1);\n if (p == NULL) {\n OPENSSL_free(str);\n return;\n }\n str = p;\n }\n OPENSSL_strlcat(str, a, (size_t)s + 1);\n }\n }\n ERR_set_error_data(str, ERR_TXT_MALLOCED | ERR_TXT_STRING);\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}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)\n return realloc_impl(str, num, file, line);\n if (str == NULL)\n return CRYPTO_malloc(num, file, line);\n if (num == 0) {\n CRYPTO_free(str, file, line);\n return NULL;\n }\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n void *ret;\n CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n ret = realloc(str, num);\n CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n return ret;\n }\n#else\n osslargused(file); osslargused(line);\n#endif\n return realloc(str, num);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
35,580
0
https://github.com/openssl/openssl/blob/a5a95f8d65c2c616ebee13ae4b33eacde34bb2d3/crypto/async/async.c/#L352
int ASYNC_init_thread(size_t max_size, size_t init_size) { async_pool *pool; size_t curr_size = 0; if (init_size > max_size) { ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_INVALID_POOL_SIZE); return 0; } if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL)) { return 0; } if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ASYNC)) { return 0; } pool = OPENSSL_zalloc(sizeof *pool); if (pool == NULL) { ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE); return 0; } pool->jobs = sk_ASYNC_JOB_new_null(); if (pool->jobs == NULL) { ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE); OPENSSL_free(pool); return 0; } pool->max_size = max_size; while (init_size--) { ASYNC_JOB *job; job = async_job_new(); if (job == NULL || !async_fibre_makecontext(&job->fibrectx)) { async_job_free(job); break; } job->funcargs = NULL; sk_ASYNC_JOB_push(pool->jobs, job); curr_size++; } pool->curr_size = curr_size; if (!CRYPTO_THREAD_set_local(&poolkey, pool)) { ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_FAILED_TO_SET_POOL); goto err; } return 1; err: async_free_pool_internal(pool); return 0; }
['int ASYNC_init_thread(size_t max_size, size_t init_size)\n{\n async_pool *pool;\n size_t curr_size = 0;\n if (init_size > max_size) {\n ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_INVALID_POOL_SIZE);\n return 0;\n }\n if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL)) {\n return 0;\n }\n if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ASYNC)) {\n return 0;\n }\n pool = OPENSSL_zalloc(sizeof *pool);\n if (pool == NULL) {\n ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n pool->jobs = sk_ASYNC_JOB_new_null();\n if (pool->jobs == NULL) {\n ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(pool);\n return 0;\n }\n pool->max_size = max_size;\n while (init_size--) {\n ASYNC_JOB *job;\n job = async_job_new();\n if (job == NULL || !async_fibre_makecontext(&job->fibrectx)) {\n async_job_free(job);\n break;\n }\n job->funcargs = NULL;\n sk_ASYNC_JOB_push(pool->jobs, job);\n curr_size++;\n }\n pool->curr_size = curr_size;\n if (!CRYPTO_THREAD_set_local(&poolkey, pool)) {\n ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_FAILED_TO_SET_POOL);\n goto err;\n }\n return 1;\nerr:\n async_free_pool_internal(pool);\n return 0;\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}', 'DEFINE_STACK_OF(ASYNC_JOB)', 'OPENSSL_STACK *OPENSSL_sk_new_null(void)\n{\n return OPENSSL_sk_new((OPENSSL_sk_compfunc)NULL);\n}', 'int async_fibre_makecontext(async_fibre *fibre)\n{\n fibre->env_init = 0;\n if (getcontext(&fibre->fibre) == 0) {\n fibre->fibre.uc_stack.ss_sp = OPENSSL_malloc(STACKSIZE);\n if (fibre->fibre.uc_stack.ss_sp != NULL) {\n fibre->fibre.uc_stack.ss_size = STACKSIZE;\n fibre->fibre.uc_link = NULL;\n makecontext(&fibre->fibre, async_start_func, 0);\n return 1;\n }\n } else {\n fibre->fibre.uc_stack.ss_sp = NULL;\n }\n return 0;\n}', 'int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)\n{\n return (OPENSSL_sk_insert(st, data, st->num));\n}']
35,581
0
https://github.com/openssl/openssl/blob/3d81ec5b92e1141762eb72caf2aeb9b2cd019a78/crypto/constant_time_locl.h/#L167
static inline unsigned int constant_time_is_zero(unsigned int a) { return constant_time_msb(~a & (a - 1)); }
['static int i2b_PVK(unsigned char **out, EVP_PKEY*pk, int enclevel,\n\t\tpem_password_cb *cb, void *u)\n\t{\n\tint outlen = 24, pklen;\n\tunsigned char *p, *salt = NULL;\n\tEVP_CIPHER_CTX cctx;\n\tEVP_CIPHER_CTX_init(&cctx);\n\tif (enclevel)\n\t\toutlen += PVK_SALTLEN;\n\tpklen = do_i2b(NULL, pk, 0);\n\tif (pklen < 0)\n\t\treturn -1;\n\toutlen += pklen;\n\tif (!out)\n\t\treturn outlen;\n\tif (*out)\n\t\tp = *out;\n\telse\n\t\t{\n\t\tp = OPENSSL_malloc(outlen);\n\t\tif (!p)\n\t\t\t{\n\t\t\tPEMerr(PEM_F_I2B_PVK,ERR_R_MALLOC_FAILURE);\n\t\t\treturn -1;\n\t\t\t}\n\t\t*out = p;\n\t\t}\n\twrite_ledword(&p, MS_PVKMAGIC);\n\twrite_ledword(&p, 0);\n\tif (pk->type == EVP_PKEY_DSA)\n\t\twrite_ledword(&p, MS_KEYTYPE_SIGN);\n\telse\n\t\twrite_ledword(&p, MS_KEYTYPE_KEYX);\n\twrite_ledword(&p, enclevel ? 1 : 0);\n\twrite_ledword(&p, enclevel ? PVK_SALTLEN: 0);\n\twrite_ledword(&p, pklen);\n\tif (enclevel)\n\t\t{\n\t\tif (RAND_bytes(p, PVK_SALTLEN) <= 0)\n\t\t\tgoto error;\n\t\tsalt = p;\n\t\tp += PVK_SALTLEN;\n\t\t}\n\tdo_i2b(&p, pk, 0);\n\tif (enclevel == 0)\n\t\treturn outlen;\n\telse\n\t\t{\n\t\tchar psbuf[PEM_BUFSIZE];\n\t\tunsigned char keybuf[20];\n\t\tint enctmplen, inlen;\n\t\tif (cb)\n\t\t\tinlen=cb(psbuf,PEM_BUFSIZE,1,u);\n\t\telse\n\t\t\tinlen=PEM_def_callback(psbuf,PEM_BUFSIZE,1,u);\n\t\tif (inlen <= 0)\n\t\t\t{\n\t\t\tPEMerr(PEM_F_I2B_PVK,PEM_R_BAD_PASSWORD_READ);\n\t\t\tgoto error;\n\t\t\t}\n\t\tif (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,\n\t\t\t (unsigned char *)psbuf, inlen))\n\t\t\tgoto error;\n\t\tif (enclevel == 1)\n\t\t\tmemset(keybuf + 5, 0, 11);\n\t\tp = salt + PVK_SALTLEN + 8;\n\t\tif (!EVP_EncryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))\n\t\t\tgoto error;\n\t\tOPENSSL_cleanse(keybuf, 20);\n\t\tif (!EVP_DecryptUpdate(&cctx, p, &enctmplen, p, pklen - 8))\n\t\t\tgoto error;\n\t\tif (!EVP_DecryptFinal_ex(&cctx, p + enctmplen, &enctmplen))\n\t\t\tgoto error;\n\t\t}\n\tEVP_CIPHER_CTX_cleanup(&cctx);\n\treturn outlen;\n\terror:\n\tEVP_CIPHER_CTX_cleanup(&cctx);\n\treturn -1;\n\t}', 'int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n\t const unsigned char *in, int inl)\n\t{\n\tint fix_len;\n\tunsigned int b;\n\tif (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER)\n\t\t{\n\t\tfix_len = ctx->cipher->do_cipher(ctx, out, in, inl);\n\t\tif (fix_len < 0)\n\t\t\t{\n\t\t\t*outl = 0;\n\t\t\treturn 0;\n\t\t\t}\n\t\telse\n\t\t\t*outl = fix_len;\n\t\treturn 1;\n\t\t}\n\tif (inl <= 0)\n\t\t{\n\t\t*outl = 0;\n\t\treturn inl == 0;\n\t\t}\n\tif (ctx->flags & EVP_CIPH_NO_PADDING)\n\t\treturn EVP_EncryptUpdate(ctx, out, outl, in, inl);\n\tb=ctx->cipher->block_size;\n\tOPENSSL_assert(b <= sizeof ctx->final);\n\tif(ctx->final_used)\n\t\t{\n\t\tmemcpy(out,ctx->final,b);\n\t\tout+=b;\n\t\tfix_len = 1;\n\t\t}\n\telse\n\t\tfix_len = 0;\n\tif(!EVP_EncryptUpdate(ctx,out,outl,in,inl))\n\t\treturn 0;\n\tif (b > 1 && !ctx->buf_len)\n\t\t{\n\t\t*outl-=b;\n\t\tctx->final_used=1;\n\t\tmemcpy(ctx->final,&out[*outl],b);\n\t\t}\n\telse\n\t\tctx->final_used = 0;\n\tif (fix_len)\n\t\t*outl += b;\n\treturn 1;\n\t}', 'int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)\n\t{\n\tunsigned int i, b;\n unsigned char pad, padding_good;\n\t*outl=0;\n\tif (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER)\n\t\t{\n\t\tint ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);\n\t\tif (ret < 0)\n\t\t\treturn 0;\n\t\telse\n\t\t\t*outl = ret;\n\t\treturn 1;\n\t\t}\n\tb=(unsigned int)(ctx->cipher->block_size);\n\tif (ctx->flags & EVP_CIPH_NO_PADDING)\n\t\t{\n\t\tif(ctx->buf_len)\n\t\t\t{\n\t\t\tEVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);\n\t\t\treturn 0;\n\t\t\t}\n\t\t*outl = 0;\n\t\treturn 1;\n\t\t}\n\tif (b > 1)\n\t\t{\n\t\tif (ctx->buf_len || !ctx->final_used)\n\t\t\t{\n\t\t\tEVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_WRONG_FINAL_BLOCK_LENGTH);\n\t\t\treturn(0);\n\t\t\t}\n\t\tOPENSSL_assert(b <= sizeof ctx->final);\n\t\tpad=ctx->final[b-1];\n\t\tpadding_good = (unsigned char)(~constant_time_is_zero_8(pad));\n\t\tpadding_good &= constant_time_ge_8(b, pad);\n for (i = 1; i < b; ++i)\n\t\t\t{\n\t\t\tunsigned char is_pad_index = constant_time_lt_8(i, pad);\n\t\t\tunsigned char pad_byte_good = constant_time_eq_8(ctx->final[b-i-1], pad);\n\t\t\tpadding_good &= constant_time_select_8(is_pad_index, pad_byte_good, 0xff);\n\t\t\t}\n\t\tfor (i = 0; i < b - 1; ++i)\n\t\t\tout[i] = ctx->final[i] & padding_good;\n\t\t*outl = padding_good & ((unsigned char)(b - pad));\n\t\treturn padding_good & 1;\n\t\t}\n\telse\n\t\t{\n\t\t*outl = 0;\n\t\treturn 1;\n\t\t}\n\t}', 'static inline unsigned char constant_time_is_zero_8(unsigned int a)\n\t{\n\treturn (unsigned char)(constant_time_is_zero(a));\n\t}', 'static inline unsigned int constant_time_is_zero(unsigned int a)\n\t{\n\treturn constant_time_msb(~a & (a - 1));\n\t}']
35,582
0
https://github.com/libav/libav/blob/645d26520a1a1900a89f2811eb78a5d637ca7877/libavcodec/ivi_dsp.c/#L92
void ff_ivi_recompose53(const IVIPlaneDesc *plane, uint8_t *dst, const int dst_pitch, const int num_bands) { int x, y, indx; int32_t p0, p1, p2, p3, tmp0, tmp1, tmp2; int32_t b0_1, b0_2, b1_1, b1_2, b1_3, b2_1, b2_2, b2_3, b2_4, b2_5, b2_6; int32_t b3_1, b3_2, b3_3, b3_4, b3_5, b3_6, b3_7, b3_8, b3_9; uint32_t pitch, back_pitch; const IDWTELEM *b0_ptr, *b1_ptr, *b2_ptr, *b3_ptr; pitch = plane->bands[0].pitch; back_pitch = 0; b0_ptr = plane->bands[0].buf; b1_ptr = plane->bands[1].buf; b2_ptr = plane->bands[2].buf; b3_ptr = plane->bands[3].buf; for (y = 0; y < plane->height; y += 2) { if (num_bands > 0) { b0_1 = b0_ptr[0]; b0_2 = b0_ptr[pitch]; } if (num_bands > 1) { b1_1 = b1_ptr[back_pitch]; b1_2 = b1_ptr[0]; b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch]; } if (num_bands > 2) { b2_2 = b2_ptr[0]; b2_3 = b2_2; b2_5 = b2_ptr[pitch]; b2_6 = b2_5; } if (num_bands > 3) { b3_2 = b3_ptr[back_pitch]; b3_3 = b3_2; b3_5 = b3_ptr[0]; b3_6 = b3_5; b3_8 = b3_2 - b3_5*6 + b3_ptr[pitch]; b3_9 = b3_8; } for (x = 0, indx = 0; x < plane->width; x+=2, indx++) { b2_1 = b2_2; b2_2 = b2_3; b2_4 = b2_5; b2_5 = b2_6; b3_1 = b3_2; b3_2 = b3_3; b3_4 = b3_5; b3_5 = b3_6; b3_7 = b3_8; b3_8 = b3_9; p0 = p1 = p2 = p3 = 0; if (num_bands > 0) { tmp0 = b0_1; tmp2 = b0_2; b0_1 = b0_ptr[indx+1]; b0_2 = b0_ptr[pitch+indx+1]; tmp1 = tmp0 + b0_1; p0 = tmp0 << 4; p1 = tmp1 << 3; p2 = (tmp0 + tmp2) << 3; p3 = (tmp1 + tmp2 + b0_2) << 2; } if (num_bands > 1) { tmp0 = b1_2; tmp1 = b1_1; b1_2 = b1_ptr[indx+1]; b1_1 = b1_ptr[back_pitch+indx+1]; tmp2 = tmp1 - tmp0*6 + b1_3; b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch+indx+1]; p0 += (tmp0 + tmp1) << 3; p1 += (tmp0 + tmp1 + b1_1 + b1_2) << 2; p2 += tmp2 << 2; p3 += (tmp2 + b1_3) << 1; } if (num_bands > 2) { b2_3 = b2_ptr[indx+1]; b2_6 = b2_ptr[pitch+indx+1]; tmp0 = b2_1 + b2_2; tmp1 = b2_1 - b2_2*6 + b2_3; p0 += tmp0 << 3; p1 += tmp1 << 2; p2 += (tmp0 + b2_4 + b2_5) << 2; p3 += (tmp1 + b2_4 - b2_5*6 + b2_6) << 1; } if (num_bands > 3) { b3_6 = b3_ptr[indx+1]; b3_3 = b3_ptr[back_pitch+indx+1]; tmp0 = b3_1 + b3_4; tmp1 = b3_2 + b3_5; tmp2 = b3_3 + b3_6; b3_9 = b3_3 - b3_6*6 + b3_ptr[pitch+indx+1]; p0 += (tmp0 + tmp1) << 2; p1 += (tmp0 - tmp1*6 + tmp2) << 1; p2 += (b3_7 + b3_8) << 1; p3 += b3_7 - b3_8*6 + b3_9; } dst[x] = av_clip_uint8((p0 >> 6) + 128); dst[x+1] = av_clip_uint8((p1 >> 6) + 128); dst[dst_pitch+x] = av_clip_uint8((p2 >> 6) + 128); dst[dst_pitch+x+1] = av_clip_uint8((p3 >> 6) + 128); } dst += dst_pitch << 1; back_pitch = -pitch; b0_ptr += pitch; b1_ptr += pitch; b2_ptr += pitch; b3_ptr += pitch; } }
['void ff_ivi_recompose53(const IVIPlaneDesc *plane, uint8_t *dst,\n const int dst_pitch, const int num_bands)\n{\n int x, y, indx;\n int32_t p0, p1, p2, p3, tmp0, tmp1, tmp2;\n int32_t b0_1, b0_2, b1_1, b1_2, b1_3, b2_1, b2_2, b2_3, b2_4, b2_5, b2_6;\n int32_t b3_1, b3_2, b3_3, b3_4, b3_5, b3_6, b3_7, b3_8, b3_9;\n uint32_t pitch, back_pitch;\n const IDWTELEM *b0_ptr, *b1_ptr, *b2_ptr, *b3_ptr;\n pitch = plane->bands[0].pitch;\n back_pitch = 0;\n b0_ptr = plane->bands[0].buf;\n b1_ptr = plane->bands[1].buf;\n b2_ptr = plane->bands[2].buf;\n b3_ptr = plane->bands[3].buf;\n for (y = 0; y < plane->height; y += 2) {\n if (num_bands > 0) {\n b0_1 = b0_ptr[0];\n b0_2 = b0_ptr[pitch];\n }\n if (num_bands > 1) {\n b1_1 = b1_ptr[back_pitch];\n b1_2 = b1_ptr[0];\n b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch];\n }\n if (num_bands > 2) {\n b2_2 = b2_ptr[0];\n b2_3 = b2_2;\n b2_5 = b2_ptr[pitch];\n b2_6 = b2_5;\n }\n if (num_bands > 3) {\n b3_2 = b3_ptr[back_pitch];\n b3_3 = b3_2;\n b3_5 = b3_ptr[0];\n b3_6 = b3_5;\n b3_8 = b3_2 - b3_5*6 + b3_ptr[pitch];\n b3_9 = b3_8;\n }\n for (x = 0, indx = 0; x < plane->width; x+=2, indx++) {\n b2_1 = b2_2;\n b2_2 = b2_3;\n b2_4 = b2_5;\n b2_5 = b2_6;\n b3_1 = b3_2;\n b3_2 = b3_3;\n b3_4 = b3_5;\n b3_5 = b3_6;\n b3_7 = b3_8;\n b3_8 = b3_9;\n p0 = p1 = p2 = p3 = 0;\n if (num_bands > 0) {\n tmp0 = b0_1;\n tmp2 = b0_2;\n b0_1 = b0_ptr[indx+1];\n b0_2 = b0_ptr[pitch+indx+1];\n tmp1 = tmp0 + b0_1;\n p0 = tmp0 << 4;\n p1 = tmp1 << 3;\n p2 = (tmp0 + tmp2) << 3;\n p3 = (tmp1 + tmp2 + b0_2) << 2;\n }\n if (num_bands > 1) {\n tmp0 = b1_2;\n tmp1 = b1_1;\n b1_2 = b1_ptr[indx+1];\n b1_1 = b1_ptr[back_pitch+indx+1];\n tmp2 = tmp1 - tmp0*6 + b1_3;\n b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch+indx+1];\n p0 += (tmp0 + tmp1) << 3;\n p1 += (tmp0 + tmp1 + b1_1 + b1_2) << 2;\n p2 += tmp2 << 2;\n p3 += (tmp2 + b1_3) << 1;\n }\n if (num_bands > 2) {\n b2_3 = b2_ptr[indx+1];\n b2_6 = b2_ptr[pitch+indx+1];\n tmp0 = b2_1 + b2_2;\n tmp1 = b2_1 - b2_2*6 + b2_3;\n p0 += tmp0 << 3;\n p1 += tmp1 << 2;\n p2 += (tmp0 + b2_4 + b2_5) << 2;\n p3 += (tmp1 + b2_4 - b2_5*6 + b2_6) << 1;\n }\n if (num_bands > 3) {\n b3_6 = b3_ptr[indx+1];\n b3_3 = b3_ptr[back_pitch+indx+1];\n tmp0 = b3_1 + b3_4;\n tmp1 = b3_2 + b3_5;\n tmp2 = b3_3 + b3_6;\n b3_9 = b3_3 - b3_6*6 + b3_ptr[pitch+indx+1];\n p0 += (tmp0 + tmp1) << 2;\n p1 += (tmp0 - tmp1*6 + tmp2) << 1;\n p2 += (b3_7 + b3_8) << 1;\n p3 += b3_7 - b3_8*6 + b3_9;\n }\n dst[x] = av_clip_uint8((p0 >> 6) + 128);\n dst[x+1] = av_clip_uint8((p1 >> 6) + 128);\n dst[dst_pitch+x] = av_clip_uint8((p2 >> 6) + 128);\n dst[dst_pitch+x+1] = av_clip_uint8((p3 >> 6) + 128);\n }\n dst += dst_pitch << 1;\n back_pitch = -pitch;\n b0_ptr += pitch;\n b1_ptr += pitch;\n b2_ptr += pitch;\n b3_ptr += pitch;\n }\n}']
35,583
0
https://github.com/libav/libav/blob/df84d7d9bdf6b8e6896c711880f130b72738c828/libavcodec/mpegaudiodec.c/#L711
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}']
35,584
0
https://github.com/openssl/openssl/blob/a8b728445c6d2d3f1d3ef568b8bff2b651aa0b52/apps/ca.c/#L2994
int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, char *str) { char *tmp = NULL; char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p; int reason_code = -1; int i, ret = 0; 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, 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 i, ret = 0;\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\tchar *ret;\n\tint n;\n\tif (str == NULL) return(NULL);\n\tn=strlen(str);\n\tret=OPENSSL_malloc(n+1);\n\tif (ret == NULL)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_STRDUP,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tmemcpy(ret,str,n+1);\n\treturn(ret);\n\t}', 'void *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_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'void CRYPTO_free(void *str)\n\t{\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(str, 0);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\\n", str);\n#endif\n\tfree_func(str);\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(NULL, 1);\n\t}']
35,585
0
https://github.com/openssl/openssl/blob/6882a964460cfbe350219236fb6aa141eb4f1c97/apps/ca.c/#L2142
static int do_revoke(X509 *x509, TXT_DB *db) { ASN1_UTCTIME *tm=NULL; char *row[DB_NUMBER],**rrow,**irow; int ok=-1,i; for (i=0; i<DB_NUMBER; i++) row[i]=NULL; row[DB_name]=X509_NAME_oneline(x509->cert_info->subject,NULL,0); row[DB_serial]=BN_bn2hex(ASN1_INTEGER_to_BN(x509->cert_info->serialNumber,NULL)); if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) { BIO_printf(bio_err,"Malloc failure\n"); goto err; } rrow=TXT_DB_get_by_index(db,DB_name,row); if (rrow == NULL) { BIO_printf(bio_err,"Adding Entry to DB for %s\n", row[DB_name]); row[DB_type]=(char *)Malloc(2); tm=X509_get_notAfter(x509); row[DB_exp_date]=(char *)Malloc(tm->length+1); memcpy(row[DB_exp_date],tm->data,tm->length); row[DB_exp_date][tm->length]='\0'; row[DB_rev_date]=NULL; row[DB_file]=(char *)Malloc(8); if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) || (row[DB_file] == NULL)) { BIO_printf(bio_err,"Malloc failure\n"); goto err; } strcpy(row[DB_file],"unknown"); row[DB_type][0]='V'; row[DB_type][1]='\0'; if ((irow=(char **)Malloc(sizeof(char *)*(DB_NUMBER+1))) == NULL) { BIO_printf(bio_err,"Malloc failure\n"); goto err; } for (i=0; i<DB_NUMBER; i++) { irow[i]=row[i]; row[i]=NULL; } irow[DB_NUMBER]=NULL; if (!TXT_DB_insert(db,irow)) { BIO_printf(bio_err,"failed to update database\n"); BIO_printf(bio_err,"TXT_DB error number %ld\n",db->error); goto err; } do_revoke(x509,db); ok=1; goto err; } else if (index_serial_cmp(row,rrow)) { BIO_printf(bio_err,"ERROR:no same serial number %s\n", row[DB_serial]); goto err; } else if (rrow[DB_type][0]=='R') { BIO_printf(bio_err,"ERROR:Already revoked, serial number %s\n", row[DB_serial]); goto err; } else { BIO_printf(bio_err,"Revoking Certificate %s.\n", rrow[DB_serial]); tm=X509_gmtime_adj(tm,0); rrow[DB_type][0]='R'; rrow[DB_type][1]='\0'; rrow[DB_rev_date]=(char *)Malloc(tm->length+1); memcpy(rrow[DB_rev_date],tm->data,tm->length); rrow[DB_rev_date][tm->length]='\0'; } ok=1; err: for (i=0; i<DB_NUMBER; i++) { if (row[i] != NULL) Free(row[i]); } ASN1_UTCTIME_free(tm); return(ok); }
['static int do_revoke(X509 *x509, TXT_DB *db)\n{\n\tASN1_UTCTIME *tm=NULL;\n\tchar *row[DB_NUMBER],**rrow,**irow;\n\tint ok=-1,i;\n\tfor (i=0; i<DB_NUMBER; i++)\n\t\trow[i]=NULL;\n\trow[DB_name]=X509_NAME_oneline(x509->cert_info->subject,NULL,0);\n\trow[DB_serial]=BN_bn2hex(ASN1_INTEGER_to_BN(x509->cert_info->serialNumber,NULL));\n\tif ((row[DB_name] == NULL) || (row[DB_serial] == NULL))\n\t\t{\n\t\tBIO_printf(bio_err,"Malloc failure\\n");\n\t\tgoto err;\n\t\t}\n\trrow=TXT_DB_get_by_index(db,DB_name,row);\n\tif (rrow == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"Adding Entry to DB for %s\\n", row[DB_name]);\n\t\trow[DB_type]=(char *)Malloc(2);\n\t\ttm=X509_get_notAfter(x509);\n\t\trow[DB_exp_date]=(char *)Malloc(tm->length+1);\n\t\tmemcpy(row[DB_exp_date],tm->data,tm->length);\n\t\trow[DB_exp_date][tm->length]=\'\\0\';\n\t\trow[DB_rev_date]=NULL;\n\t\trow[DB_file]=(char *)Malloc(8);\n\t\tif ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||\n\t\t\t(row[DB_file] == NULL))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"Malloc failure\\n");\n\t\t\tgoto err;\n\t\t\t}\n\t\tstrcpy(row[DB_file],"unknown");\n\t\trow[DB_type][0]=\'V\';\n\t\trow[DB_type][1]=\'\\0\';\n\t\tif ((irow=(char **)Malloc(sizeof(char *)*(DB_NUMBER+1))) == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"Malloc failure\\n");\n\t\t\tgoto err;\n\t\t\t}\n\t\tfor (i=0; i<DB_NUMBER; i++)\n\t\t\t{\n\t\t\tirow[i]=row[i];\n\t\t\trow[i]=NULL;\n\t\t\t}\n\t\tirow[DB_NUMBER]=NULL;\n\t\tif (!TXT_DB_insert(db,irow))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"failed to update database\\n");\n\t\t\tBIO_printf(bio_err,"TXT_DB error number %ld\\n",db->error);\n\t\t\tgoto err;\n\t\t\t}\n\t\tdo_revoke(x509,db);\n\t\tok=1;\n\t\tgoto err;\n\t\t}\n\telse if (index_serial_cmp(row,rrow))\n\t\t{\n\t\tBIO_printf(bio_err,"ERROR:no same serial number %s\\n",\n\t\t\t row[DB_serial]);\n\t\tgoto err;\n\t\t}\n\telse if (rrow[DB_type][0]==\'R\')\n\t\t{\n\t\tBIO_printf(bio_err,"ERROR:Already revoked, serial number %s\\n",\n\t\t\t row[DB_serial]);\n\t\tgoto err;\n\t\t}\n\telse\n\t\t{\n\t\tBIO_printf(bio_err,"Revoking Certificate %s.\\n", rrow[DB_serial]);\n\t\ttm=X509_gmtime_adj(tm,0);\n\t\trrow[DB_type][0]=\'R\';\n\t\trrow[DB_type][1]=\'\\0\';\n\t\trrow[DB_rev_date]=(char *)Malloc(tm->length+1);\n\t\tmemcpy(rrow[DB_rev_date],tm->data,tm->length);\n\t\trrow[DB_rev_date][tm->length]=\'\\0\';\n\t\t}\n\tok=1;\nerr:\n\tfor (i=0; i<DB_NUMBER; i++)\n\t\t{\n\t\tif (row[i] != NULL)\n\t\t\tFree(row[i]);\n\t\t}\n\tASN1_UTCTIME_free(tm);\n\treturn(ok);\n}', 'char *X509_NAME_oneline(X509_NAME *a, char *buf, int len)\n\t{\n\tX509_NAME_ENTRY *ne;\nint i;\n\tint n,lold,l,l1,l2,num,j,type;\n\tconst char *s;\n\tchar *p;\n\tunsigned char *q;\n\tBUF_MEM *b=NULL;\n\tstatic char hex[17]="0123456789ABCDEF";\n\tint gs_doit[4];\n\tchar tmp_buf[80];\n#ifdef CHARSET_EBCDIC\n\tchar ebcdic_buf[1024];\n#endif\n\tif (buf == NULL)\n\t\t{\n\t\tif ((b=BUF_MEM_new()) == NULL) goto err;\n\t\tif (!BUF_MEM_grow(b,200)) goto err;\n\t\tb->data[0]=\'\\0\';\n\t\tlen=200;\n\t\t}\n\tif (a == NULL)\n\t {\n\t if(b)\n\t\t{\n\t\tbuf=b->data;\n\t\tFree(b);\n\t\t}\n\t strncpy(buf,"NO X509_NAME",len);\n\t return buf;\n\t }\n\tlen--;\n\tl=0;\n\tfor (i=0; i<sk_X509_NAME_ENTRY_num(a->entries); i++)\n\t\t{\n\t\tne=sk_X509_NAME_ENTRY_value(a->entries,i);\n\t\tn=OBJ_obj2nid(ne->object);\n\t\tif ((n == NID_undef) || ((s=OBJ_nid2sn(n)) == NULL))\n\t\t\t{\n\t\t\ti2t_ASN1_OBJECT(tmp_buf,sizeof(tmp_buf),ne->object);\n\t\t\ts=tmp_buf;\n\t\t\t}\n\t\tl1=strlen(s);\n\t\ttype=ne->value->type;\n\t\tnum=ne->value->length;\n\t\tq=ne->value->data;\n#ifdef CHARSET_EBCDIC\n if (type == V_ASN1_GENERALSTRING ||\n\t\t type == V_ASN1_VISIBLESTRING ||\n\t\t type == V_ASN1_PRINTABLESTRING ||\n\t\t type == V_ASN1_TELETEXSTRING ||\n\t\t type == V_ASN1_VISIBLESTRING ||\n\t\t type == V_ASN1_IA5STRING) {\n ascii2ebcdic(ebcdic_buf, q,\n\t\t\t\t (num > sizeof ebcdic_buf)\n\t\t\t\t ? sizeof ebcdic_buf : num);\n q=ebcdic_buf;\n\t\t}\n#endif\n\t\tif ((type == V_ASN1_GENERALSTRING) && ((num%4) == 0))\n\t\t\t{\n\t\t\tgs_doit[0]=gs_doit[1]=gs_doit[2]=gs_doit[3]=0;\n\t\t\tfor (j=0; j<num; j++)\n\t\t\t\tif (q[j] != 0) gs_doit[j&3]=1;\n\t\t\tif (gs_doit[0]|gs_doit[1]|gs_doit[2])\n\t\t\t\tgs_doit[0]=gs_doit[1]=gs_doit[2]=gs_doit[3]=1;\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tgs_doit[0]=gs_doit[1]=gs_doit[2]=0;\n\t\t\t\tgs_doit[3]=1;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n\t\t\tgs_doit[0]=gs_doit[1]=gs_doit[2]=gs_doit[3]=1;\n\t\tfor (l2=j=0; j<num; j++)\n\t\t\t{\n\t\t\tif (!gs_doit[j&3]) continue;\n\t\t\tl2++;\n#ifndef CHARSET_EBCDIC\n\t\t\tif ((q[j] < \' \') || (q[j] > \'~\')) l2+=3;\n#else\n\t\t\tif ((os_toascii[q[j]] < os_toascii[\' \']) ||\n\t\t\t (os_toascii[q[j]] > os_toascii[\'~\'])) l2+=3;\n#endif\n\t\t\t}\n\t\tlold=l;\n\t\tl+=1+l1+1+l2;\n\t\tif (b != NULL)\n\t\t\t{\n\t\t\tif (!BUF_MEM_grow(b,l+1)) goto err;\n\t\t\tp= &(b->data[lold]);\n\t\t\t}\n\t\telse if (l > len)\n\t\t\t{\n\t\t\tbreak;\n\t\t\t}\n\t\telse\n\t\t\tp= &(buf[lold]);\n\t\t*(p++)=\'/\';\n\t\tmemcpy(p,s,(unsigned int)l1); p+=l1;\n\t\t*(p++)=\'=\';\n#ifndef CHARSET_EBCDIC\n\t\tq=ne->value->data;\n#endif\n\t\tfor (j=0; j<num; j++)\n\t\t\t{\n\t\t\tif (!gs_doit[j&3]) continue;\n#ifndef CHARSET_EBCDIC\n\t\t\tn=q[j];\n\t\t\tif ((n < \' \') || (n > \'~\'))\n\t\t\t\t{\n\t\t\t\t*(p++)=\'\\\\\';\n\t\t\t\t*(p++)=\'x\';\n\t\t\t\t*(p++)=hex[(n>>4)&0x0f];\n\t\t\t\t*(p++)=hex[n&0x0f];\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t*(p++)=n;\n#else\n\t\t\tn=os_toascii[q[j]];\n\t\t\tif ((n < os_toascii[\' \']) ||\n\t\t\t (n > os_toascii[\'~\']))\n\t\t\t\t{\n\t\t\t\t*(p++)=\'\\\\\';\n\t\t\t\t*(p++)=\'x\';\n\t\t\t\t*(p++)=hex[(n>>4)&0x0f];\n\t\t\t\t*(p++)=hex[n&0x0f];\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t*(p++)=q[j];\n#endif\n\t\t\t}\n\t\t*p=\'\\0\';\n\t\t}\n\tif (b != NULL)\n\t\t{\n\t\tp=b->data;\n\t\tFree(b);\n\t\t}\n\telse\n\t\tp=buf;\n\treturn(p);\nerr:\n\tX509err(X509_F_X509_NAME_ONELINE,ERR_R_MALLOC_FAILURE);\n\tif (b != NULL) BUF_MEM_free(b);\n\treturn(NULL);\n\t}']
35,586
0
https://github.com/openssl/openssl/blob/9f9442918aeaed5dc2442d81ab8d29fe3e1fb906/crypto/bn/bn_lib.c/#L333
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) { bn_check_top(b); if (a == b) return a; if (bn_wexpand(a, b->top) == NULL) return NULL; if (b->top > 0) memcpy(a->d, b->d, sizeof(b->d[0]) * b->top); if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0) BN_set_flags(a, BN_FLG_CONSTTIME); a->top = b->top; a->neg = b->neg; bn_check_top(a); return a; }
['static int 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, j, k, 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 j = BN_num_bytes(ret);\n i = BN_bn2bin(ret, &(to[num - j]));\n for (k = 0; k < (num - i); k++)\n to[k] = 0;\n r = 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}', '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_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\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 ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n 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_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(a, BN_FLG_CONSTTIME);\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
35,587
0
https://github.com/openssl/openssl/blob/3ba25ee86a3758cc659c954b59718d8397030768/crypto/lhash/lhash.c/#L240
void *lh_delete(LHASH *lh, const void *data) { unsigned long hash; LHASH_NODE *nn,**rn; const void *ret; lh->error=0; rn=getrn(lh,data,&hash); if (*rn == NULL) { lh->num_no_delete++; return(NULL); } else { nn= *rn; *rn=nn->next; ret=nn->data; OPENSSL_free(nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))) contract(lh); return((void *)ret); }
['static long ssl_ctrl(BIO *b, int cmd, long num, void *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\t{\n#if 0\n\t\tBIOerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n\t\tret = -1;\n#else\n\t\tret=0;\n#endif\n\t\t}\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#else\n\tif (s->new_session)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CLEAR,SSL_R_INTERNAL_ERROR);\n\t\treturn 0;\n\t\t}\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#if 0\n\ts->read_ahead=s->ctx->read_ahead;\n#endif\n\tif (s->init_buf != NULL)\n\t\t{\n\t\tBUF_MEM_free(s->init_buf);\n\t\ts->init_buf=NULL;\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tif (ssl_clear_bad_session(s))\n\t\t{\n\t\tSSL_SESSION_free(s->session);\n\t\ts->session=NULL;\n\t\t}\n\ts->first_packet=0;\n#if 1\n\tif ((s->session == NULL) && (s->method != s->ctx->method))\n\t\t{\n\t\ts->method->ssl_free(s);\n\t\ts->method=s->ctx->method;\n\t\tif (!s->method->ssl_new(s))\n\t\t\treturn(0);\n\t\t}\n\telse\n#endif\n\t\ts->method->ssl_clear(s);\n\treturn(1);\n\t}', 'int ssl_clear_bad_session(SSL *s)\n\t{\n\tif (\t(s->session != NULL) &&\n\t\t!(s->shutdown & SSL_SENT_SHUTDOWN) &&\n\t\t!(SSL_in_init(s) || SSL_in_before(s)))\n\t\t{\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\treturn(1);\n\t\t}\n\telse\n\t\treturn(0);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'void *lh_delete(LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tconst void *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tOPENSSL_free(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn((void *)ret);\n\t}']
35,588
0
https://github.com/libav/libav/blob/dad7a9c7c0ae8ebc56f2e3a24e6fa4da5c2cd491/libavformat/rtmpproto.c/#L1854
static int write_begin(URLContext *s) { RTMPContext *rt = s->priv_data; PutByteContext pbc; RTMPPacket spkt = { 0 }; int ret; if ((ret = ff_rtmp_packet_create(&spkt, RTMP_NETWORK_CHANNEL, RTMP_PT_USER_CONTROL, 0, 6)) < 0) { av_log(s, AV_LOG_ERROR, "Unable to create response packet\n"); return ret; } bytestream2_init_writer(&pbc, spkt.data, spkt.size); bytestream2_put_be16(&pbc, 0); bytestream2_put_be32(&pbc, rt->nb_streamid); ret = ff_rtmp_packet_write(rt->stream, &spkt, rt->out_chunk_size, &rt->prev_pkt[1], &rt->nb_prev_pkt[1]); ff_rtmp_packet_destroy(&spkt); return ret; }
['static int write_begin(URLContext *s)\n{\n RTMPContext *rt = s->priv_data;\n PutByteContext pbc;\n RTMPPacket spkt = { 0 };\n int ret;\n if ((ret = ff_rtmp_packet_create(&spkt, RTMP_NETWORK_CHANNEL,\n RTMP_PT_USER_CONTROL, 0, 6)) < 0) {\n av_log(s, AV_LOG_ERROR, "Unable to create response packet\\n");\n return ret;\n }\n bytestream2_init_writer(&pbc, spkt.data, spkt.size);\n bytestream2_put_be16(&pbc, 0);\n bytestream2_put_be32(&pbc, rt->nb_streamid);\n ret = ff_rtmp_packet_write(rt->stream, &spkt, rt->out_chunk_size,\n &rt->prev_pkt[1], &rt->nb_prev_pkt[1]);\n ff_rtmp_packet_destroy(&spkt);\n return ret;\n}', 'int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,\n int timestamp, int size)\n{\n if (size) {\n pkt->data = av_realloc(NULL, size);\n if (!pkt->data)\n return AVERROR(ENOMEM);\n }\n pkt->size = size;\n pkt->channel_id = channel_id;\n pkt->type = type;\n pkt->timestamp = timestamp;\n pkt->extra = 0;\n pkt->ts_field = 0;\n return 0;\n}', 'void *av_realloc(void *ptr, size_t size)\n{\n if (size > (INT_MAX - 16))\n return NULL;\n#if HAVE_ALIGNED_MALLOC\n return _aligned_realloc(ptr, size, 32);\n#else\n return realloc(ptr, size);\n#endif\n}', 'static av_always_inline void bytestream2_init_writer(PutByteContext *p,\n uint8_t *buf,\n int buf_size)\n{\n p->buffer = buf;\n p->buffer_start = buf;\n p->buffer_end = buf + buf_size;\n p->eof = 0;\n}', 'DEF(unsigned int, be16, 2, AV_RB16, AV_WB16)', 'DEF(unsigned int, be32, 4, AV_RB32, AV_WB32)', 'void ff_rtmp_packet_destroy(RTMPPacket *pkt)\n{\n if (!pkt)\n return;\n av_freep(&pkt->data);\n pkt->size = 0;\n}']
35,589
0
https://github.com/libav/libav/blob/fc322d6a70189da24dbd445c710bb214eb031ce7/libavcodec/bitstream.h/#L139
static inline uint64_t get_val(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1); bc->bits >>= n; #else uint64_t ret = bc->bits >> (64 - n); bc->bits <<= n; #endif bc->bits_left -= n; return ret; }
['static int read_access_unit(AVCodecContext *avctx, void* data,\n int *got_frame_ptr, AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n MLPDecodeContext *m = avctx->priv_data;\n BitstreamContext bc;\n unsigned int length, substr;\n unsigned int substream_start;\n unsigned int header_size = 4;\n unsigned int substr_header_size = 0;\n uint8_t substream_parity_present[MAX_SUBSTREAMS];\n uint16_t substream_data_len[MAX_SUBSTREAMS];\n uint8_t parity_bits;\n int ret;\n if (buf_size < 4)\n return 0;\n length = (AV_RB16(buf) & 0xfff) * 2;\n if (length < 4 || length > buf_size)\n return AVERROR_INVALIDDATA;\n bitstream_init(&bc, (buf + 4), (length - 4) * 8);\n m->is_major_sync_unit = 0;\n if (bitstream_peek(&bc, 31) == (0xf8726fba >> 1)) {\n if (read_major_sync(m, &bc) < 0)\n goto error;\n m->is_major_sync_unit = 1;\n header_size += m->major_sync_header_size;\n }\n if (!m->params_valid) {\n av_log(m->avctx, AV_LOG_WARNING,\n "Stream parameters not seen; skipping frame.\\n");\n *got_frame_ptr = 0;\n return length;\n }\n substream_start = 0;\n for (substr = 0; substr < m->num_substreams; substr++) {\n int extraword_present, checkdata_present, end, nonrestart_substr;\n extraword_present = bitstream_read_bit(&bc);\n nonrestart_substr = bitstream_read_bit(&bc);\n checkdata_present = bitstream_read_bit(&bc);\n bitstream_skip(&bc, 1);\n end = bitstream_read(&bc, 12) * 2;\n substr_header_size += 2;\n if (extraword_present) {\n if (m->avctx->codec_id == AV_CODEC_ID_MLP) {\n av_log(m->avctx, AV_LOG_ERROR, "There must be no extraword for MLP.\\n");\n goto error;\n }\n bitstream_skip(&bc, 16);\n substr_header_size += 2;\n }\n if (!(nonrestart_substr ^ m->is_major_sync_unit)) {\n av_log(m->avctx, AV_LOG_ERROR, "Invalid nonrestart_substr.\\n");\n goto error;\n }\n if (end + header_size + substr_header_size > length) {\n av_log(m->avctx, AV_LOG_ERROR,\n "Indicated length of substream %d data goes off end of "\n "packet.\\n", substr);\n end = length - header_size - substr_header_size;\n }\n if (end < substream_start) {\n av_log(avctx, AV_LOG_ERROR,\n "Indicated end offset of substream %d data "\n "is smaller than calculated start offset.\\n",\n substr);\n goto error;\n }\n if (substr > m->max_decoded_substream)\n continue;\n substream_parity_present[substr] = checkdata_present;\n substream_data_len[substr] = end - substream_start;\n substream_start = end;\n }\n parity_bits = ff_mlp_calculate_parity(buf, 4);\n parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);\n if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {\n av_log(avctx, AV_LOG_ERROR, "Parity check failed.\\n");\n goto error;\n }\n buf += header_size + substr_header_size;\n for (substr = 0; substr <= m->max_decoded_substream; substr++) {\n SubStream *s = &m->substream[substr];\n bitstream_init(&bc, buf, substream_data_len[substr] * 8);\n m->matrix_changed = 0;\n memset(m->filter_changed, 0, sizeof(m->filter_changed));\n s->blockpos = 0;\n do {\n if (bitstream_read_bit(&bc)) {\n if (bitstream_read_bit(&bc)) {\n if (read_restart_header(m, &bc, buf, substr) < 0)\n goto next_substr;\n s->restart_seen = 1;\n }\n if (!s->restart_seen)\n goto next_substr;\n if (read_decoding_params(m, &bc, substr) < 0)\n goto next_substr;\n }\n if (!s->restart_seen)\n goto next_substr;\n if ((ret = read_block_data(m, &bc, substr)) < 0)\n return ret;\n if (bitstream_tell(&bc) >= substream_data_len[substr] * 8)\n goto substream_length_mismatch;\n } while (!bitstream_read_bit(&bc));\n bitstream_skip(&bc, (-bitstream_tell(&bc)) & 15);\n if (substream_data_len[substr] * 8 - bitstream_tell(&bc) >= 32) {\n int shorten_by;\n if (bitstream_read(&bc, 16) != 0xD234)\n return AVERROR_INVALIDDATA;\n shorten_by = bitstream_read(&bc, 16);\n if (m->avctx->codec_id == AV_CODEC_ID_TRUEHD && shorten_by & 0x2000)\n s->blockpos -= FFMIN(shorten_by & 0x1FFF, s->blockpos);\n else if (m->avctx->codec_id == AV_CODEC_ID_MLP && shorten_by != 0xD234)\n return AVERROR_INVALIDDATA;\n if (substr == m->max_decoded_substream)\n av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\\n");\n }\n if (substream_parity_present[substr]) {\n uint8_t parity, checksum;\n if (substream_data_len[substr] * 8 - bitstream_tell(&bc) != 16)\n goto substream_length_mismatch;\n parity = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);\n checksum = ff_mlp_checksum8 (buf, substream_data_len[substr] - 2);\n if ((bitstream_read(&bc, 8) ^ parity) != 0xa9)\n av_log(m->avctx, AV_LOG_ERROR, "Substream %d parity check failed.\\n", substr);\n if (bitstream_read(&bc, 8) != checksum)\n av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\\n" , substr);\n }\n if (substream_data_len[substr] * 8 != bitstream_tell(&bc))\n goto substream_length_mismatch;\nnext_substr:\n if (!s->restart_seen)\n av_log(m->avctx, AV_LOG_ERROR,\n "No restart header present in substream %d.\\n", substr);\n buf += substream_data_len[substr];\n }\n rematrix_channels(m, m->max_decoded_substream);\n if ((ret = output_data(m, m->max_decoded_substream, data, got_frame_ptr)) < 0)\n return ret;\n return length;\nsubstream_length_mismatch:\n av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\\n", substr);\n return AVERROR_INVALIDDATA;\nerror:\n m->params_valid = 0;\n return AVERROR_INVALIDDATA;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
35,590
0
https://github.com/nginx/nginx/blob/917c47a9185f8317a7b165f2b1ce866682cacaca/src/http/ngx_http_request.c/#L2339
void ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc) { ngx_connection_t *c; ngx_http_request_t *pr; ngx_http_core_loc_conf_t *clcf; c = r->connection; ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0, "http finalize request: %d, \"%V?%V\" a:%d, c:%d", rc, &r->uri, &r->args, r == c->data, r->main->count); if (rc == NGX_DONE) { ngx_http_finalize_connection(r); return; } if (rc == NGX_OK && r->filter_finalize) { c->error = 1; } if (rc == NGX_DECLINED) { r->content_handler = NULL; r->write_event_handler = ngx_http_core_run_phases; ngx_http_core_run_phases(r); return; } if (r != r->main && r->post_subrequest) { rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc); } if (rc == NGX_ERROR || rc == NGX_HTTP_REQUEST_TIME_OUT || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST || c->error) { if (ngx_http_post_action(r) == NGX_OK) { return; } if (r->main->blocked) { r->write_event_handler = ngx_http_request_finalizer; } ngx_http_terminate_request(r, rc); return; } if (rc >= NGX_HTTP_SPECIAL_RESPONSE || rc == NGX_HTTP_CREATED || rc == NGX_HTTP_NO_CONTENT) { if (rc == NGX_HTTP_CLOSE) { ngx_http_terminate_request(r, rc); return; } if (r == r->main) { if (c->read->timer_set) { ngx_del_timer(c->read); } if (c->write->timer_set) { ngx_del_timer(c->write); } } c->read->handler = ngx_http_request_handler; c->write->handler = ngx_http_request_handler; ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc)); return; } if (r != r->main) { if (r->buffered || r->postponed) { if (ngx_http_set_write_handler(r) != NGX_OK) { ngx_http_terminate_request(r, 0); } return; } pr = r->parent; if (r == c->data) { r->main->count--; r->main->subrequests++; if (!r->logged) { clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); if (clcf->log_subrequest) { ngx_http_log_request(r); } r->logged = 1; } else { ngx_log_error(NGX_LOG_ALERT, c->log, 0, "subrequest: \"%V?%V\" logged again", &r->uri, &r->args); } r->done = 1; if (pr->postponed && pr->postponed->request == r) { pr->postponed = pr->postponed->next; } c->data = pr; } else { ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "http finalize non-active request: \"%V?%V\"", &r->uri, &r->args); r->write_event_handler = ngx_http_request_finalizer; if (r->waited) { r->done = 1; } } if (ngx_http_post_request(pr, NULL) != NGX_OK) { r->main->count++; ngx_http_terminate_request(r, 0); return; } ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "http wake parent request: \"%V?%V\"", &pr->uri, &pr->args); return; } if (r->buffered || c->buffered || r->postponed || r->blocked) { if (ngx_http_set_write_handler(r) != NGX_OK) { ngx_http_terminate_request(r, 0); } return; } if (r != c->data) { ngx_log_error(NGX_LOG_ALERT, c->log, 0, "http finalize non-active request: \"%V?%V\"", &r->uri, &r->args); return; } r->done = 1; r->write_event_handler = ngx_http_request_empty_handler; if (!r->post_action) { r->request_complete = 1; } if (ngx_http_post_action(r) == NGX_OK) { return; } if (c->read->timer_set) { ngx_del_timer(c->read); } if (c->write->timer_set) { c->write->delayed = 0; ngx_del_timer(c->write); } if (c->read->eof) { ngx_http_close_request(r, 0); return; } ngx_http_finalize_connection(r); }
['static void\nngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx)\n{\n ngx_connection_t *c;\n ngx_http_request_t *r;\n ngx_http_upstream_t *u;\n ngx_http_upstream_resolved_t *ur;\n r = ctx->data;\n c = r->connection;\n u = r->upstream;\n ur = u->resolved;\n ngx_http_set_log_request(c->log, r);\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http upstream resolve: \\"%V?%V\\"", &r->uri, &r->args);\n if (ctx->state) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n "%V could not be resolved (%i: %s)",\n &ctx->name, ctx->state,\n ngx_resolver_strerror(ctx->state));\n ngx_http_upstream_finalize_request(r, u, NGX_HTTP_BAD_GATEWAY);\n goto failed;\n }\n ur->naddrs = ctx->naddrs;\n ur->addrs = ctx->addrs;\n#if (NGX_DEBUG)\n {\n u_char text[NGX_SOCKADDR_STRLEN];\n ngx_str_t addr;\n ngx_uint_t i;\n addr.data = text;\n for (i = 0; i < ctx->naddrs; i++) {\n addr.len = ngx_sock_ntop(ur->addrs[i].sockaddr, ur->addrs[i].socklen,\n text, NGX_SOCKADDR_STRLEN, 0);\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "name was resolved to %V", &addr);\n }\n }\n#endif\n if (ngx_http_upstream_create_round_robin_peer(r, ur) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n goto failed;\n }\n ngx_resolve_name_done(ctx);\n ur->ctx = NULL;\n u->peer.start_time = ngx_current_msec;\n if (u->conf->next_upstream_tries\n && u->peer.tries > u->conf->next_upstream_tries)\n {\n u->peer.tries = u->conf->next_upstream_tries;\n }\n ngx_http_upstream_connect(r, u);\nfailed:\n ngx_http_run_posted_requests(c);\n}', 'void\nngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,\n const char *fmt, ...)\n#else\nvoid\nngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,\n const char *fmt, va_list args)\n#endif\n{\n#if (NGX_HAVE_VARIADIC_MACROS)\n va_list args;\n#endif\n u_char *p, *last, *msg;\n ssize_t n;\n ngx_uint_t wrote_stderr, debug_connection;\n u_char errstr[NGX_MAX_ERROR_STR];\n last = errstr + NGX_MAX_ERROR_STR;\n ngx_memcpy(errstr, ngx_cached_err_log_time.data,\n ngx_cached_err_log_time.len);\n p = errstr + ngx_cached_err_log_time.len;\n p = ngx_slprintf(p, last, " [%V] ", &err_levels[level]);\n p = ngx_slprintf(p, last, "%P#" NGX_TID_T_FMT ": ",\n ngx_log_pid, ngx_log_tid);\n if (log->connection) {\n p = ngx_slprintf(p, last, "*%uA ", log->connection);\n }\n msg = p;\n#if (NGX_HAVE_VARIADIC_MACROS)\n va_start(args, fmt);\n p = ngx_vslprintf(p, last, fmt, args);\n va_end(args);\n#else\n p = ngx_vslprintf(p, last, fmt, args);\n#endif\n if (err) {\n p = ngx_log_errno(p, last, err);\n }\n if (level != NGX_LOG_DEBUG && log->handler) {\n p = log->handler(log, p, last - p);\n }\n if (p > last - NGX_LINEFEED_SIZE) {\n p = last - NGX_LINEFEED_SIZE;\n }\n ngx_linefeed(p);\n wrote_stderr = 0;\n debug_connection = (log->log_level & NGX_LOG_DEBUG_CONNECTION) != 0;\n while (log) {\n if (log->log_level < level && !debug_connection) {\n break;\n }\n if (log->writer) {\n log->writer(log, level, errstr, p - errstr);\n goto next;\n }\n if (ngx_time() == log->disk_full_time) {\n goto next;\n }\n n = ngx_write_fd(log->file->fd, errstr, p - errstr);\n if (n == -1 && ngx_errno == NGX_ENOSPC) {\n log->disk_full_time = ngx_time();\n }\n if (log->file->fd == ngx_stderr) {\n wrote_stderr = 1;\n }\n next:\n log = log->next;\n }\n if (!ngx_use_stderr\n || level > NGX_LOG_WARN\n || wrote_stderr)\n {\n return;\n }\n msg -= (7 + err_levels[level].len + 3);\n (void) ngx_sprintf(msg, "nginx: [%V] ", &err_levels[level]);\n (void) ngx_write_console(ngx_stderr, msg, p - msg);\n}', 'static void\nngx_http_upstream_finalize_request(ngx_http_request_t *r,\n ngx_http_upstream_t *u, ngx_int_t rc)\n{\n ngx_uint_t flush;\n ngx_time_t *tp;\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "finalize http upstream request: %i", rc);\n if (u->cleanup) {\n *u->cleanup = NULL;\n u->cleanup = NULL;\n }\n if (u->resolved && u->resolved->ctx) {\n ngx_resolve_name_done(u->resolved->ctx);\n u->resolved->ctx = NULL;\n }\n if (u->state && u->state->response_sec) {\n tp = ngx_timeofday();\n u->state->response_sec = tp->sec - u->state->response_sec;\n u->state->response_msec = tp->msec - u->state->response_msec;\n if (u->pipe && u->pipe->read_length) {\n u->state->response_length = u->pipe->read_length;\n }\n }\n u->finalize_request(r, rc);\n if (u->peer.free && u->peer.sockaddr) {\n u->peer.free(&u->peer, u->peer.data, 0);\n u->peer.sockaddr = NULL;\n }\n if (u->peer.connection) {\n#if (NGX_HTTP_SSL)\n if (u->peer.connection->ssl) {\n u->peer.connection->ssl->no_wait_shutdown = 1;\n (void) ngx_ssl_shutdown(u->peer.connection);\n }\n#endif\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "close http upstream connection: %d",\n u->peer.connection->fd);\n if (u->peer.connection->pool) {\n ngx_destroy_pool(u->peer.connection->pool);\n }\n ngx_close_connection(u->peer.connection);\n }\n u->peer.connection = NULL;\n if (u->pipe && u->pipe->temp_file) {\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http upstream temp fd: %d",\n u->pipe->temp_file->file.fd);\n }\n if (u->store && u->pipe && u->pipe->temp_file\n && u->pipe->temp_file->file.fd != NGX_INVALID_FILE)\n {\n if (ngx_delete_file(u->pipe->temp_file->file.name.data)\n == NGX_FILE_ERROR)\n {\n ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,\n ngx_delete_file_n " \\"%s\\" failed",\n u->pipe->temp_file->file.name.data);\n }\n }\n#if (NGX_HTTP_CACHE)\n if (r->cache) {\n if (u->cacheable) {\n if (rc == NGX_HTTP_BAD_GATEWAY || rc == NGX_HTTP_GATEWAY_TIME_OUT) {\n time_t valid;\n valid = ngx_http_file_cache_valid(u->conf->cache_valid, rc);\n if (valid) {\n r->cache->valid_sec = ngx_time() + valid;\n r->cache->error = rc;\n }\n }\n }\n ngx_http_file_cache_free(r->cache, u->pipe->temp_file);\n }\n#endif\n if (r->subrequest_in_memory\n && u->headers_in.status_n >= NGX_HTTP_SPECIAL_RESPONSE)\n {\n u->buffer.last = u->buffer.pos;\n }\n if (rc == NGX_DECLINED) {\n return;\n }\n r->connection->log->action = "sending to client";\n if (!u->header_sent\n || rc == NGX_HTTP_REQUEST_TIME_OUT\n || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST)\n {\n ngx_http_finalize_request(r, rc);\n return;\n }\n flush = 0;\n if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {\n rc = NGX_ERROR;\n flush = 1;\n }\n if (r->header_only) {\n ngx_http_finalize_request(r, rc);\n return;\n }\n if (rc == 0) {\n rc = ngx_http_send_special(r, NGX_HTTP_LAST);\n } else if (flush) {\n r->keepalive = 0;\n rc = ngx_http_send_special(r, NGX_HTTP_FLUSH);\n }\n ngx_http_finalize_request(r, rc);\n}', 'void\nngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)\n{\n ngx_connection_t *c;\n ngx_http_request_t *pr;\n ngx_http_core_loc_conf_t *clcf;\n c = r->connection;\n ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http finalize request: %d, \\"%V?%V\\" a:%d, c:%d",\n rc, &r->uri, &r->args, r == c->data, r->main->count);\n if (rc == NGX_DONE) {\n ngx_http_finalize_connection(r);\n return;\n }\n if (rc == NGX_OK && r->filter_finalize) {\n c->error = 1;\n }\n if (rc == NGX_DECLINED) {\n r->content_handler = NULL;\n r->write_event_handler = ngx_http_core_run_phases;\n ngx_http_core_run_phases(r);\n return;\n }\n if (r != r->main && r->post_subrequest) {\n rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);\n }\n if (rc == NGX_ERROR\n || rc == NGX_HTTP_REQUEST_TIME_OUT\n || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST\n || c->error)\n {\n if (ngx_http_post_action(r) == NGX_OK) {\n return;\n }\n if (r->main->blocked) {\n r->write_event_handler = ngx_http_request_finalizer;\n }\n ngx_http_terminate_request(r, rc);\n return;\n }\n if (rc >= NGX_HTTP_SPECIAL_RESPONSE\n || rc == NGX_HTTP_CREATED\n || rc == NGX_HTTP_NO_CONTENT)\n {\n if (rc == NGX_HTTP_CLOSE) {\n ngx_http_terminate_request(r, rc);\n return;\n }\n if (r == r->main) {\n if (c->read->timer_set) {\n ngx_del_timer(c->read);\n }\n if (c->write->timer_set) {\n ngx_del_timer(c->write);\n }\n }\n c->read->handler = ngx_http_request_handler;\n c->write->handler = ngx_http_request_handler;\n ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));\n return;\n }\n if (r != r->main) {\n if (r->buffered || r->postponed) {\n if (ngx_http_set_write_handler(r) != NGX_OK) {\n ngx_http_terminate_request(r, 0);\n }\n return;\n }\n pr = r->parent;\n if (r == c->data) {\n r->main->count--;\n r->main->subrequests++;\n if (!r->logged) {\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (clcf->log_subrequest) {\n ngx_http_log_request(r);\n }\n r->logged = 1;\n } else {\n ngx_log_error(NGX_LOG_ALERT, c->log, 0,\n "subrequest: \\"%V?%V\\" logged again",\n &r->uri, &r->args);\n }\n r->done = 1;\n if (pr->postponed && pr->postponed->request == r) {\n pr->postponed = pr->postponed->next;\n }\n c->data = pr;\n } else {\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http finalize non-active request: \\"%V?%V\\"",\n &r->uri, &r->args);\n r->write_event_handler = ngx_http_request_finalizer;\n if (r->waited) {\n r->done = 1;\n }\n }\n if (ngx_http_post_request(pr, NULL) != NGX_OK) {\n r->main->count++;\n ngx_http_terminate_request(r, 0);\n return;\n }\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http wake parent request: \\"%V?%V\\"",\n &pr->uri, &pr->args);\n return;\n }\n if (r->buffered || c->buffered || r->postponed || r->blocked) {\n if (ngx_http_set_write_handler(r) != NGX_OK) {\n ngx_http_terminate_request(r, 0);\n }\n return;\n }\n if (r != c->data) {\n ngx_log_error(NGX_LOG_ALERT, c->log, 0,\n "http finalize non-active request: \\"%V?%V\\"",\n &r->uri, &r->args);\n return;\n }\n r->done = 1;\n r->write_event_handler = ngx_http_request_empty_handler;\n if (!r->post_action) {\n r->request_complete = 1;\n }\n if (ngx_http_post_action(r) == NGX_OK) {\n return;\n }\n if (c->read->timer_set) {\n ngx_del_timer(c->read);\n }\n if (c->write->timer_set) {\n c->write->delayed = 0;\n ngx_del_timer(c->write);\n }\n if (c->read->eof) {\n ngx_http_close_request(r, 0);\n return;\n }\n ngx_http_finalize_connection(r);\n}']
35,591
0
https://github.com/openssl/openssl/blob/9dd4ac8cf17f2afd636e85ae0111d1df4104a475/crypto/err/err.c/#L691
void ERR_set_error_data(char *data, int flags) { ERR_STATE *es; int i; es = ERR_get_state(); i = es->top; if (i == 0) i = ERR_NUM_ERRORS - 1; err_clear_data(es, i); es->err_data[i] = data; es->err_data_flags[i] = flags; }
['void ERR_set_error_data(char *data, int flags)\n{\n ERR_STATE *es;\n int i;\n es = ERR_get_state();\n i = es->top;\n if (i == 0)\n i = ERR_NUM_ERRORS - 1;\n err_clear_data(es, i);\n es->err_data[i] = data;\n es->err_data_flags[i] = flags;\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 (!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 ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE);\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 *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)\n{\n return pthread_getspecific(*key);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
35,592
0
https://github.com/libav/libav/blob/2c4b87ea0e61cc762de7536dbae743effc12ba08/libavcodec/wmavoice.c/#L1641
static int check_bits_for_superframe(GetBitContext *orig_gb, WMAVoiceContext *s) { GetBitContext s_gb, *gb = &s_gb; int n, need_bits, bd_idx; const struct frame_type_desc *frame_desc; init_get_bits(gb, orig_gb->buffer, orig_gb->size_in_bits); skip_bits_long(gb, get_bits_count(orig_gb)); assert(get_bits_left(gb) == get_bits_left(orig_gb)); if (get_bits_left(gb) < 14) return 1; if (!get_bits1(gb)) return -1; if (get_bits1(gb)) skip_bits(gb, 12); if (s->has_residual_lsps) { if (get_bits_left(gb) < s->sframe_lsp_bitsize) return 1; skip_bits_long(gb, s->sframe_lsp_bitsize); } for (n = 0; n < MAX_FRAMES; n++) { int aw_idx_is_ext = 0; if (!s->has_residual_lsps) { if (get_bits_left(gb) < s->frame_lsp_bitsize) return 1; skip_bits_long(gb, s->frame_lsp_bitsize); } bd_idx = s->vbm_tree[get_vlc2(gb, frame_type_vlc.table, 6, 3)]; if (bd_idx < 0) return -1; frame_desc = &frame_descs[bd_idx]; if (frame_desc->acb_type == ACB_TYPE_ASYMMETRIC) { if (get_bits_left(gb) < s->pitch_nbits) return 1; skip_bits_long(gb, s->pitch_nbits); } if (frame_desc->fcb_type == FCB_TYPE_SILENCE) { skip_bits(gb, 8); } else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) { int tmp = get_bits(gb, 6); if (tmp >= 0x36) { skip_bits(gb, 2); aw_idx_is_ext = 1; } } if (frame_desc->acb_type == ACB_TYPE_HAMMING) { need_bits = s->block_pitch_nbits + (frame_desc->n_blocks - 1) * s->block_delta_pitch_nbits; } else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) { need_bits = 2 * !aw_idx_is_ext; } else need_bits = 0; need_bits += frame_desc->frame_size; if (get_bits_left(gb) < need_bits) return 1; skip_bits_long(gb, need_bits); } return 0; }
['static int check_bits_for_superframe(GetBitContext *orig_gb,\n WMAVoiceContext *s)\n{\n GetBitContext s_gb, *gb = &s_gb;\n int n, need_bits, bd_idx;\n const struct frame_type_desc *frame_desc;\n init_get_bits(gb, orig_gb->buffer, orig_gb->size_in_bits);\n skip_bits_long(gb, get_bits_count(orig_gb));\n assert(get_bits_left(gb) == get_bits_left(orig_gb));\n if (get_bits_left(gb) < 14)\n return 1;\n if (!get_bits1(gb))\n return -1;\n if (get_bits1(gb)) skip_bits(gb, 12);\n if (s->has_residual_lsps) {\n if (get_bits_left(gb) < s->sframe_lsp_bitsize)\n return 1;\n skip_bits_long(gb, s->sframe_lsp_bitsize);\n }\n for (n = 0; n < MAX_FRAMES; n++) {\n int aw_idx_is_ext = 0;\n if (!s->has_residual_lsps) {\n if (get_bits_left(gb) < s->frame_lsp_bitsize) return 1;\n skip_bits_long(gb, s->frame_lsp_bitsize);\n }\n bd_idx = s->vbm_tree[get_vlc2(gb, frame_type_vlc.table, 6, 3)];\n if (bd_idx < 0)\n return -1;\n frame_desc = &frame_descs[bd_idx];\n if (frame_desc->acb_type == ACB_TYPE_ASYMMETRIC) {\n if (get_bits_left(gb) < s->pitch_nbits)\n return 1;\n skip_bits_long(gb, s->pitch_nbits);\n }\n if (frame_desc->fcb_type == FCB_TYPE_SILENCE) {\n skip_bits(gb, 8);\n } else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) {\n int tmp = get_bits(gb, 6);\n if (tmp >= 0x36) {\n skip_bits(gb, 2);\n aw_idx_is_ext = 1;\n }\n }\n if (frame_desc->acb_type == ACB_TYPE_HAMMING) {\n need_bits = s->block_pitch_nbits +\n (frame_desc->n_blocks - 1) * s->block_delta_pitch_nbits;\n } else if (frame_desc->fcb_type == FCB_TYPE_AW_PULSES) {\n need_bits = 2 * !aw_idx_is_ext;\n } else\n need_bits = 0;\n need_bits += frame_desc->frame_size;\n if (get_bits_left(gb) < need_bits)\n return 1;\n skip_bits_long(gb, need_bits);\n }\n return 0;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size= (bit_size+7)>>3;\n if(buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer= buffer;\n s->size_in_bits= bit_size;\n s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index=0;\n#elif defined 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 inline int get_bits_count(const GetBitContext *s){\n return s->index;\n}', 'static inline void skip_bits_long(GetBitContext *s, int n){\n s->index += n;\n}', 'static inline int get_bits_left(GetBitContext *gb)\n{\n return gb->size_in_bits - get_bits_count(gb);\n}', 'static inline unsigned int get_bits1(GetBitContext *s){\n#ifdef ALT_BITSTREAM_READER\n unsigned int index= s->index;\n uint8_t result= s->buffer[ index>>3 ];\n#ifdef ALT_BITSTREAM_READER_LE\n result>>= (index&0x07);\n result&= 1;\n#else\n result<<= (index&0x07);\n result>>= 8 - 1;\n#endif\n index++;\n s->index= index;\n return result;\n#else\n return get_bits(s, 1);\n#endif\n}']
35,593
0
https://github.com/openssl/openssl/blob/daea0ff8a960237e0f9a71301721824c25ad3b25/crypto/asn1/t_x509.c/#L404
int ASN1_GENERALIZEDTIME_print(BIO *bp, ASN1_GENERALIZEDTIME *tm) { char *v; int gmt=0; int i; int y=0,M=0,d=0,h=0,m=0,s=0; i=tm->length; v=(char *)tm->data; if (i < 12) goto err; if (v[i-1] == 'Z') gmt=1; for (i=0; i<12; i++) if ((v[i] > '9') || (v[i] < '0')) goto err; y= (v[0]-'0')*1000+(v[1]-'0')*100 + (v[2]-'0')*10+(v[3]-'0'); M= (v[4]-'0')*10+(v[5]-'0'); if ((M > 12) || (M < 1)) goto err; d= (v[6]-'0')*10+(v[7]-'0'); h= (v[8]-'0')*10+(v[9]-'0'); m= (v[10]-'0')*10+(v[11]-'0'); if ( (v[12] >= '0') && (v[12] <= '9') && (v[13] >= '0') && (v[13] <= '9')) s= (v[12]-'0')*10+(v[13]-'0'); if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s", mon[M-1],d,h,m,s,y,(gmt)?" GMT":"") <= 0) return(0); else return(1); err: BIO_write(bp,"Bad time value",14); return(0); }
['static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,\n\t const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy, TXT_DB *db,\n\t BIGNUM *serial, char *startdate, char *enddate, int days,\n\t int batch, char *ext_sect, LHASH *lconf, int verbose)\n\t{\n\tX509 *req=NULL;\n\tX509_REQ *rreq=NULL;\n\tBIO *in=NULL;\n\tEVP_PKEY *pktmp=NULL;\n\tint ok= -1,i;\n\tin=BIO_new(BIO_s_file());\n\tif (BIO_read_filename(in,infile) <= 0)\n\t\t{\n\t\tperror(infile);\n\t\tgoto err;\n\t\t}\n\tif ((req=PEM_read_bio_X509(in,NULL,NULL,NULL)) == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"Error reading self signed certificate in %s\\n",infile);\n\t\tgoto err;\n\t\t}\n\tif (verbose)\n\t\tX509_print(bio_err,req);\n\tBIO_printf(bio_err,"Check that the request matches the signature\\n");\n\tif ((pktmp=X509_get_pubkey(req)) == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"error unpacking public key\\n");\n\t\tgoto err;\n\t\t}\n\ti=X509_verify(req,pktmp);\n\tEVP_PKEY_free(pktmp);\n\tif (i < 0)\n\t\t{\n\t\tok=0;\n\t\tBIO_printf(bio_err,"Signature verification problems....\\n");\n\t\tgoto err;\n\t\t}\n\tif (i == 0)\n\t\t{\n\t\tok=0;\n\t\tBIO_printf(bio_err,"Signature did not match the certificate\\n");\n\t\tgoto err;\n\t\t}\n\telse\n\t\tBIO_printf(bio_err,"Signature ok\\n");\n\tif ((rreq=X509_to_X509_REQ(req,NULL,EVP_md5())) == NULL)\n\t\tgoto err;\n\tok=do_body(xret,pkey,x509,dgst,policy,db,serial,startdate,enddate,days,\n\t\tbatch,verbose,rreq,ext_sect,lconf);\nerr:\n\tif (rreq != NULL) X509_REQ_free(rreq);\n\tif (req != NULL) X509_free(req);\n\tif (in != NULL) BIO_free(in);\n\treturn(ok);\n\t}', 'IMPLEMENT_PEM_rw(X509, X509, PEM_STRING_X509, X509)', 'char *PEM_ASN1_read_bio(char *(*d2i)(), const char *name, BIO *bp, char **x,\n\t pem_password_cb *cb, void *u)\n\t{\n\tEVP_CIPHER_INFO cipher;\n\tchar *nm=NULL,*header=NULL;\n\tunsigned char *p=NULL,*data=NULL;\n\tlong len;\n\tchar *ret=NULL;\n\tfor (;;)\n\t\t{\n\t\tif (!PEM_read_bio(bp,&nm,&header,&data,&len)) {\n\t\t\tif(ERR_GET_REASON(ERR_peek_error()) ==\n\t\t\t\tPEM_R_NO_START_LINE)\n\t\t\t\tERR_add_error_data(2, "Expecting: ", name);\n\t\t\treturn(NULL);\n\t\t}\n\t\tif(check_pem(nm, name)) break;\n\t\tOPENSSL_free(nm);\n\t\tOPENSSL_free(header);\n\t\tOPENSSL_free(data);\n\t\t}\n\tif (!PEM_get_EVP_CIPHER_INFO(header,&cipher)) goto err;\n\tif (!PEM_do_header(&cipher,data,&len,cb,u)) goto err;\n\tp=data;\n\tif (strcmp(name,PEM_STRING_EVP_PKEY) == 0) {\n\t\tif (strcmp(nm,PEM_STRING_RSA) == 0)\n\t\t\tret=d2i(EVP_PKEY_RSA,x,&p,len);\n\t\telse if (strcmp(nm,PEM_STRING_DSA) == 0)\n\t\t\tret=d2i(EVP_PKEY_DSA,x,&p,len);\n\t\telse if (strcmp(nm,PEM_STRING_PKCS8INF) == 0) {\n\t\t\tPKCS8_PRIV_KEY_INFO *p8inf;\n\t\t\tp8inf=d2i_PKCS8_PRIV_KEY_INFO(\n\t\t\t\t\t(PKCS8_PRIV_KEY_INFO **) x, &p, len);\n\t\t\tret = (char *)EVP_PKCS82PKEY(p8inf);\n\t\t\tPKCS8_PRIV_KEY_INFO_free(p8inf);\n\t\t} else if (strcmp(nm,PEM_STRING_PKCS8) == 0) {\n\t\t\tPKCS8_PRIV_KEY_INFO *p8inf;\n\t\t\tX509_SIG *p8;\n\t\t\tint klen;\n\t\t\tchar psbuf[PEM_BUFSIZE];\n\t\t\tp8 = d2i_X509_SIG(NULL, &p, len);\n\t\t\tif(!p8) goto p8err;\n\t\t\tif (cb) klen=cb(psbuf,PEM_BUFSIZE,0,u);\n\t\t\telse klen=def_callback(psbuf,PEM_BUFSIZE,0,u);\n\t\t\tif (klen <= 0) {\n\t\t\t\tPEMerr(PEM_F_PEM_ASN1_READ_BIO,\n\t\t\t\t\t\tPEM_R_BAD_PASSWORD_READ);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t\tp8inf = M_PKCS8_decrypt(p8, psbuf, klen);\n\t\t\tX509_SIG_free(p8);\n\t\t\tif(!p8inf) goto p8err;\n\t\t\tret = (char *)EVP_PKCS82PKEY(p8inf);\n\t\t\tif(x) {\n\t\t\t\tif(*x) EVP_PKEY_free((EVP_PKEY *)*x);\n\t\t\t\t*x = ret;\n\t\t\t}\n\t\t\tPKCS8_PRIV_KEY_INFO_free(p8inf);\n\t\t}\n\t} else\tret=d2i(x,&p,len);\np8err:\n\tif (ret == NULL)\n\t\tPEMerr(PEM_F_PEM_ASN1_READ_BIO,ERR_R_ASN1_LIB);\nerr:\n\tOPENSSL_free(nm);\n\tOPENSSL_free(header);\n\tOPENSSL_free(data);\n\treturn(ret);\n\t}', 'int X509_print(BIO *bp, X509 *x)\n{\n\treturn X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);\n}', 'int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)\n\t{\n\tlong l;\n\tint ret=0,i,j,n;\n\tchar *m=NULL,*s, mlch = \' \';\n\tint nmindent = 0;\n\tX509_CINF *ci;\n\tASN1_INTEGER *bs;\n\tEVP_PKEY *pkey=NULL;\n\tconst char *neg;\n\tX509_EXTENSION *ex;\n\tASN1_STRING *str=NULL;\n\tif((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {\n\t\t\tmlch = \'\\n\';\n\t\t\tnmindent = 16;\n\t}\n\tif(nmflags == X509_FLAG_COMPAT)\n\t\tnmindent = 16;\n\tci=x->cert_info;\n\tif(!(cflag & X509_FLAG_NO_HEADER))\n\t\t{\n\t\tif (BIO_write(bp,"Certificate:\\n",13) <= 0) goto err;\n\t\tif (BIO_write(bp," Data:\\n",10) <= 0) goto err;\n\t\t}\n\tif(!(cflag & X509_FLAG_NO_VERSION))\n\t\t{\n\t\tl=X509_get_version(x);\n\t\tif (BIO_printf(bp,"%8sVersion: %lu (0x%lx)\\n","",l+1,l) <= 0) goto err;\n\t\t}\n\tif(!(cflag & X509_FLAG_NO_SERIAL))\n\t\t{\n\t\tif (BIO_write(bp," Serial Number:",22) <= 0) goto err;\n\t\tbs=X509_get_serialNumber(x);\n\t\tif (bs->length <= 4)\n\t\t\t{\n\t\t\tl=ASN1_INTEGER_get(bs);\n\t\t\tif (l < 0)\n\t\t\t\t{\n\t\t\t\tl= -l;\n\t\t\t\tneg="-";\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tneg="";\n\t\t\tif (BIO_printf(bp," %s%lu (%s0x%lx)\\n",neg,l,neg,l) <= 0)\n\t\t\t\tgoto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tneg=(bs->type == V_ASN1_NEG_INTEGER)?" (Negative)":"";\n\t\t\tif (BIO_printf(bp,"\\n%12s%s","",neg) <= 0) goto err;\n\t\t\tfor (i=0; i<bs->length; i++)\n\t\t\t\t{\n\t\t\t\tif (BIO_printf(bp,"%02x%c",bs->data[i],\n\t\t\t\t\t((i+1 == bs->length)?\'\\n\':\':\')) <= 0)\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif(!(cflag & X509_FLAG_NO_SIGNAME))\n\t\t{\n\t\ti=OBJ_obj2nid(ci->signature->algorithm);\n\t\tif (BIO_printf(bp,"%8sSignature Algorithm: %s\\n","",\n\t\t\t(i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0)\n\t\t\tgoto err;\n\t\t}\n\tif(!(cflag & X509_FLAG_NO_ISSUER))\n\t\t{\n\t\tif (BIO_printf(bp," Issuer:%c",mlch) <= 0) goto err;\n\t\tif (!X509_NAME_print_ex(bp,X509_get_issuer_name(x),nmindent, nmflags)) goto err;\n\t\t}\n\tif(!(cflag & X509_FLAG_NO_VALIDITY))\n\t\t{\n\t\tif (BIO_write(bp,"\\n Validity\\n",18) <= 0) goto err;\n\t\tif (BIO_write(bp," Not Before: ",24) <= 0) goto err;\n\t\tif (!ASN1_TIME_print(bp,X509_get_notBefore(x))) goto err;\n\t\tif (BIO_write(bp,"\\n Not After : ",25) <= 0) goto err;\n\t\tif (!ASN1_TIME_print(bp,X509_get_notAfter(x))) goto err;\n\t\tif (BIO_write(bp,"\\n",1) <= 0) goto err;\n\t\t}\n\tif(!(cflag & X509_FLAG_NO_SUBJECT))\n\t\t{\n\t\tif (BIO_printf(bp," Subject:%c",mlch) <= 0) goto err;\n\t\tif (!X509_NAME_print_ex(bp,X509_get_subject_name(x),nmindent, nmflags)) goto err;\n\t\t}\n\tif(!(cflag & X509_FLAG_NO_PUBKEY))\n\t\t{\n\t\tif (BIO_write(bp,"\\n Subject Public Key Info:\\n",34) <= 0)\n\t\t\tgoto err;\n\t\ti=OBJ_obj2nid(ci->key->algor->algorithm);\n\t\tif (BIO_printf(bp,"%12sPublic Key Algorithm: %s\\n","",\n\t\t\t(i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0) goto err;\n\t\tpkey=X509_get_pubkey(x);\n\t\tif (pkey == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bp,"%12sUnable to load Public Key\\n","");\n\t\t\tERR_print_errors(bp);\n\t\t\t}\n\t\telse\n#ifndef NO_RSA\n\t\tif (pkey->type == EVP_PKEY_RSA)\n\t\t\t{\n\t\t\tBIO_printf(bp,"%12sRSA Public Key: (%d bit)\\n","",\n\t\t\tBN_num_bits(pkey->pkey.rsa->n));\n\t\t\tRSA_print(bp,pkey->pkey.rsa,16);\n\t\t\t}\n\t\telse\n#endif\n#ifndef NO_DSA\n\t\tif (pkey->type == EVP_PKEY_DSA)\n\t\t\t{\n\t\t\tBIO_printf(bp,"%12sDSA Public Key:\\n","");\n\t\t\tDSA_print(bp,pkey->pkey.dsa,16);\n\t\t\t}\n\t\telse\n#endif\n\t\t\tBIO_printf(bp,"%12sUnknown Public Key:\\n","");\n\t\tEVP_PKEY_free(pkey);\n\t\t}\n\tif (cflag & X509_FLAG_NO_EXTENSIONS)\n\t\tn = 0;\n\telse\n\t\tn=X509_get_ext_count(x);\n\tif (n > 0)\n\t\t{\n\t\tBIO_printf(bp,"%8sX509v3 extensions:\\n","");\n\t\tfor (i=0; i<n; i++)\n\t\t\t{\n\t\t\tASN1_OBJECT *obj;\n\t\t\tex=X509_get_ext(x,i);\n\t\t\tif (BIO_printf(bp,"%12s","") <= 0) goto err;\n\t\t\tobj=X509_EXTENSION_get_object(ex);\n\t\t\ti2a_ASN1_OBJECT(bp,obj);\n\t\t\tj=X509_EXTENSION_get_critical(ex);\n\t\t\tif (BIO_printf(bp,": %s\\n",j?"critical":"","") <= 0)\n\t\t\t\tgoto err;\n\t\t\tif(!X509V3_EXT_print(bp, ex, cflag, 16))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bp, "%16s", "");\n\t\t\t\tM_ASN1_OCTET_STRING_print(bp,ex->value);\n\t\t\t\t}\n\t\t\tif (BIO_write(bp,"\\n",1) <= 0) goto err;\n\t\t\t}\n\t\t}\n\tif(!(cflag & X509_FLAG_NO_SIGDUMP))\n\t\t{\n\t\ti=OBJ_obj2nid(x->sig_alg->algorithm);\n\t\tif (BIO_printf(bp,"%4sSignature Algorithm: %s","",\n\t\t\t(i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0) goto err;\n\t\tn=x->signature->length;\n\t\ts=(char *)x->signature->data;\n\t\tfor (i=0; i<n; i++)\n\t\t\t{\n\t\t\tif ((i%18) == 0)\n\t\t\t\tif (BIO_write(bp,"\\n ",9) <= 0) goto err;\n\t\t\tif (BIO_printf(bp,"%02x%s",(unsigned char)s[i],\n\t\t\t\t((i+1) == n)?"":":") <= 0) goto err;\n\t\t\t}\n\t\tif (BIO_write(bp,"\\n",1) != 1) goto err;\n\t\t}\n\tif(!(cflag & X509_FLAG_NO_AUX))\n\t\t{\n\t\tif (!X509_CERT_AUX_print(bp, x->aux, 0)) goto err;\n\t\t}\n\tret=1;\nerr:\n\tif (str != NULL) ASN1_STRING_free(str);\n\tif (m != NULL) OPENSSL_free(m);\n\treturn(ret);\n\t}', 'int ASN1_TIME_print(BIO *bp, ASN1_TIME *tm)\n{\n\tif(tm->type == V_ASN1_UTCTIME) return ASN1_UTCTIME_print(bp, tm);\n\tif(tm->type == V_ASN1_GENERALIZEDTIME)\n\t\t\t\treturn ASN1_GENERALIZEDTIME_print(bp, tm);\n\tBIO_write(bp,"Bad time value",14);\n\treturn(0);\n}', 'int ASN1_GENERALIZEDTIME_print(BIO *bp, ASN1_GENERALIZEDTIME *tm)\n\t{\n\tchar *v;\n\tint gmt=0;\n\tint i;\n\tint y=0,M=0,d=0,h=0,m=0,s=0;\n\ti=tm->length;\n\tv=(char *)tm->data;\n\tif (i < 12) goto err;\n\tif (v[i-1] == \'Z\') gmt=1;\n\tfor (i=0; i<12; i++)\n\t\tif ((v[i] > \'9\') || (v[i] < \'0\')) goto err;\n\ty= (v[0]-\'0\')*1000+(v[1]-\'0\')*100 + (v[2]-\'0\')*10+(v[3]-\'0\');\n\tM= (v[4]-\'0\')*10+(v[5]-\'0\');\n\tif ((M > 12) || (M < 1)) goto err;\n\td= (v[6]-\'0\')*10+(v[7]-\'0\');\n\th= (v[8]-\'0\')*10+(v[9]-\'0\');\n\tm= (v[10]-\'0\')*10+(v[11]-\'0\');\n\tif (\t(v[12] >= \'0\') && (v[12] <= \'9\') &&\n\t\t(v[13] >= \'0\') && (v[13] <= \'9\'))\n\t\ts= (v[12]-\'0\')*10+(v[13]-\'0\');\n\tif (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s",\n\t\tmon[M-1],d,h,m,s,y,(gmt)?" GMT":"") <= 0)\n\t\treturn(0);\n\telse\n\t\treturn(1);\nerr:\n\tBIO_write(bp,"Bad time value",14);\n\treturn(0);\n\t}']
35,594
0
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_mul.c/#L657
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb) { BN_ULONG *rr; if (na < nb) { int itmp; BN_ULONG *ltmp; itmp = na; na = nb; nb = itmp; ltmp = a; a = b; b = ltmp; } rr = &(r[na]); if (nb <= 0) { (void)bn_mul_words(r, a, na, 0); return; } else rr[0] = bn_mul_words(r, a, na, b[0]); for (;;) { if (--nb <= 0) return; rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]); if (--nb <= 0) return; rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]); if (--nb <= 0) return; rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]); if (--nb <= 0) return; rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]); rr += 4; r += 4; b += 4; } }
['static EVP_PKEY *b2i_dss(const unsigned char **in,\n unsigned int bitlen, int ispub)\n{\n const unsigned char *p = *in;\n EVP_PKEY *ret = NULL;\n DSA *dsa = NULL;\n BN_CTX *ctx = NULL;\n unsigned int nbyte;\n BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL;\n BIGNUM *pub_key = NULL;\n nbyte = (bitlen + 7) >> 3;\n dsa = DSA_new();\n ret = EVP_PKEY_new();\n if (dsa == NULL || ret == NULL)\n goto memerr;\n if (!read_lebn(&p, nbyte, &pbn))\n goto memerr;\n if (!read_lebn(&p, 20, &qbn))\n goto memerr;\n if (!read_lebn(&p, nbyte, &gbn))\n goto memerr;\n if (ispub) {\n if (!read_lebn(&p, nbyte, &pub_key))\n goto memerr;\n } else {\n if (!read_lebn(&p, 20, &priv_key))\n goto memerr;\n pub_key = BN_new();\n if (pub_key == NULL)\n goto memerr;\n if ((ctx = BN_CTX_new()) == NULL)\n goto memerr;\n if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx))\n goto memerr;\n BN_CTX_free(ctx);\n ctx = NULL;\n }\n if (!DSA_set0_pqg(dsa, pbn, qbn, gbn))\n goto memerr;\n pbn = qbn = gbn = NULL;\n if (!DSA_set0_key(dsa, pub_key, priv_key))\n goto memerr;\n pub_key = priv_key = NULL;\n if (!EVP_PKEY_set1_DSA(ret, dsa))\n goto memerr;\n DSA_free(dsa);\n *in = p;\n return ret;\n memerr:\n PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);\n DSA_free(dsa);\n BN_free(pbn);\n BN_free(qbn);\n BN_free(gbn);\n BN_free(pub_key);\n BN_free(priv_key);\n EVP_PKEY_free(ret);\n BN_CTX_free(ctx);\n return NULL;\n}', 'static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)\n{\n *r = BN_lebin2bn(*in, nbyte, NULL);\n if (*r == NULL)\n return 0;\n *in += nbyte;\n return 1;\n}', 'BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return NULL;\n bn_check_top(ret);\n s += len;\n for ( ; len > 0 && s[-1] == 0; s--, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return ret;\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n s--;\n l = (l << 8L) | *s;\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return ret;\n}', 'int BN_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_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_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx);\n}', 'int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!bn_sqr_fixed_top(tmp, a, ctx))\n goto err;\n } else {\n if (!bn_mul_fixed_top(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!bn_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int bn_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_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n int tna, int tnb, BN_ULONG *t)\n{\n int i, j, n2 = n * 2;\n int c1, c2, neg;\n BN_ULONG ln, lo, *p;\n if (n < 8) {\n bn_mul_normal(r, a, n + tna, b, n + tnb);\n return;\n }\n c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);\n c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);\n neg = 0;\n switch (c1 * 3 + c2) {\n case -4:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n break;\n case -3:\n case -2:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n neg = 1;\n break;\n case -1:\n case 0:\n case 1:\n case 2:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n neg = 1;\n break;\n case 3:\n case 4:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n break;\n }\n# if 0\n if (n == 4) {\n bn_mul_comba4(&(t[n2]), t, &(t[n]));\n bn_mul_comba4(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);\n memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));\n } else\n# endif\n if (n == 8) {\n bn_mul_comba8(&(t[n2]), t, &(t[n]));\n bn_mul_comba8(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));\n } else {\n p = &(t[n2 * 2]);\n bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);\n bn_mul_recursive(r, a, b, n, 0, 0, p);\n i = n / 2;\n if (tna > tnb)\n j = tna - i;\n else\n j = tnb - i;\n if (j == 0) {\n bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));\n } else if (j > 0) {\n bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&(r[n2 + tna + tnb]), 0,\n sizeof(BN_ULONG) * (n2 - tna - tnb));\n } else {\n memset(&r[n2], 0, sizeof(*r) * n2);\n if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n } else {\n for (;;) {\n i /= 2;\n if (i < tna || i < tnb) {\n bn_mul_part_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n } else if (i == tna || i == tnb) {\n bn_mul_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n }\n }\n }\n }\n }\n c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));\n if (neg) {\n c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));\n } else {\n c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));\n }\n c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));\n if (c1) {\n p = &(r[n + n2]);\n lo = *p;\n ln = (lo + c1) & BN_MASK2;\n *p = ln;\n if (ln < (BN_ULONG)c1) {\n do {\n p++;\n lo = *p;\n ln = (lo + 1) & BN_MASK2;\n *p = ln;\n } while (ln == 0);\n }\n }\n}', 'void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)\n{\n BN_ULONG *rr;\n if (na < nb) {\n int itmp;\n BN_ULONG *ltmp;\n itmp = na;\n na = nb;\n nb = itmp;\n ltmp = a;\n a = b;\n b = ltmp;\n }\n rr = &(r[na]);\n if (nb <= 0) {\n (void)bn_mul_words(r, a, na, 0);\n return;\n } else\n rr[0] = bn_mul_words(r, a, na, b[0]);\n for (;;) {\n if (--nb <= 0)\n return;\n rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);\n if (--nb <= 0)\n return;\n rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);\n if (--nb <= 0)\n return;\n rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);\n if (--nb <= 0)\n return;\n rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);\n rr += 4;\n r += 4;\n b += 4;\n }\n}']
35,595
0
https://github.com/libav/libav/blob/1e8b9738fa70e20967ddb542d2f9d5552fc51ec6/libavcodec/h264.c/#L3765
static av_always_inline void fill_filter_caches_inter(H264Context *h, int mb_type, int top_xy, int left_xy[LEFT_MBS], int top_type, int left_type[LEFT_MBS], int mb_xy, int list) { int b_stride = h->b_stride; int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]]; int8_t *ref_cache = &h->ref_cache[list][scan8[0]]; if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) { if (USES_LIST(top_type, list)) { const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride; const int b8_xy = 4 * top_xy + 2; int (*ref2frm)[64] = h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2); AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]); ref_cache[0 - 1 * 8] = ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 0]]; ref_cache[2 - 1 * 8] = ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 1]]; } else { AV_ZERO128(mv_dst - 1 * 8); AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); } if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) { 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; int (*ref2frm)[64] = h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2); AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]); AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]); AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]); AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]); ref_cache[-1 + 0] = ref_cache[-1 + 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 0]]; ref_cache[-1 + 16] = ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 1]]; } else { AV_ZERO32(mv_dst - 1 + 0); AV_ZERO32(mv_dst - 1 + 8); AV_ZERO32(mv_dst - 1 + 16); AV_ZERO32(mv_dst - 1 + 24); ref_cache[-1 + 0] = ref_cache[-1 + 8] = ref_cache[-1 + 16] = ref_cache[-1 + 24] = LIST_NOT_USED; } } } if (!USES_LIST(mb_type, list)) { fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4); AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); return; } { int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy]; int (*ref2frm)[64] = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2); uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101; uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101; AV_WN32A(&ref_cache[0 * 8], ref01); AV_WN32A(&ref_cache[1 * 8], ref01); AV_WN32A(&ref_cache[2 * 8], ref23); AV_WN32A(&ref_cache[3 * 8], ref23); } { int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * h->mb_x + 4 * h->mb_y * b_stride]; AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride); AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride); AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride); AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride); } }
['static int fill_filter_caches(H264Context *h, int mb_type)\n{\n const int mb_xy = h->mb_xy;\n int top_xy, left_xy[LEFT_MBS];\n int top_type, left_type[LEFT_MBS];\n uint8_t *nnz;\n uint8_t *nnz_cache;\n top_xy = mb_xy - (h->mb_stride << MB_FIELD(h));\n left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;\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 (h->mb_y & 1) {\n if (left_mb_field_flag != curr_mb_field_flag)\n left_xy[LTOP] -= h->mb_stride;\n } else {\n if (curr_mb_field_flag)\n top_xy += h->mb_stride &\n (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);\n if (left_mb_field_flag != curr_mb_field_flag)\n left_xy[LBOT] += h->mb_stride;\n }\n }\n h->top_mb_xy = top_xy;\n h->left_mb_xy[LTOP] = left_xy[LTOP];\n h->left_mb_xy[LBOT] = left_xy[LBOT];\n {\n int qp_thresh = h->qp_thresh;\n int qp = h->cur_pic.qscale_table[mb_xy];\n if (qp <= qp_thresh &&\n (left_xy[LTOP] < 0 ||\n ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&\n (top_xy < 0 ||\n ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {\n if (!FRAME_MBAFF(h))\n return 1;\n if ((left_xy[LTOP] < 0 ||\n ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&\n (top_xy < h->mb_stride ||\n ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))\n return 1;\n }\n }\n top_type = h->cur_pic.mb_type[top_xy];\n left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];\n left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];\n if (h->deblocking_filter == 2) {\n if (h->slice_table[top_xy] != h->slice_num)\n top_type = 0;\n if (h->slice_table[left_xy[LBOT]] != h->slice_num)\n left_type[LTOP] = left_type[LBOT] = 0;\n } else {\n if (h->slice_table[top_xy] == 0xFFFF)\n top_type = 0;\n if (h->slice_table[left_xy[LBOT]] == 0xFFFF)\n left_type[LTOP] = left_type[LBOT] = 0;\n }\n h->top_type = top_type;\n h->left_type[LTOP] = left_type[LTOP];\n h->left_type[LBOT] = left_type[LBOT];\n if (IS_INTRA(mb_type))\n return 0;\n fill_filter_caches_inter(h, mb_type, top_xy, left_xy,\n top_type, left_type, mb_xy, 0);\n if (h->list_count == 2)\n fill_filter_caches_inter(h, mb_type, top_xy, left_xy,\n top_type, left_type, mb_xy, 1);\n nnz = h->non_zero_count[mb_xy];\n nnz_cache = h->non_zero_count_cache;\n AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);\n AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);\n AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);\n AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);\n h->cbp = h->cbp_table[mb_xy];\n if (top_type) {\n nnz = h->non_zero_count[top_xy];\n AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);\n }\n if (left_type[LTOP]) {\n nnz = h->non_zero_count[left_xy[LTOP]];\n nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];\n nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];\n nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];\n nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];\n }\n if (!CABAC(h) && h->pps.transform_8x8_mode) {\n if (IS_8x8DCT(top_type)) {\n nnz_cache[4 + 8 * 0] =\n nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;\n nnz_cache[6 + 8 * 0] =\n nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;\n }\n if (IS_8x8DCT(left_type[LTOP])) {\n nnz_cache[3 + 8 * 1] =\n nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12;\n }\n if (IS_8x8DCT(left_type[LBOT])) {\n nnz_cache[3 + 8 * 3] =\n nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12;\n }\n if (IS_8x8DCT(mb_type)) {\n nnz_cache[scan8[0]] =\n nnz_cache[scan8[1]] =\n nnz_cache[scan8[2]] =\n nnz_cache[scan8[3]] = (h->cbp & 0x1000) >> 12;\n nnz_cache[scan8[0 + 4]] =\n nnz_cache[scan8[1 + 4]] =\n nnz_cache[scan8[2 + 4]] =\n nnz_cache[scan8[3 + 4]] = (h->cbp & 0x2000) >> 12;\n nnz_cache[scan8[0 + 8]] =\n nnz_cache[scan8[1 + 8]] =\n nnz_cache[scan8[2 + 8]] =\n nnz_cache[scan8[3 + 8]] = (h->cbp & 0x4000) >> 12;\n nnz_cache[scan8[0 + 12]] =\n nnz_cache[scan8[1 + 12]] =\n nnz_cache[scan8[2 + 12]] =\n nnz_cache[scan8[3 + 12]] = (h->cbp & 0x8000) >> 12;\n }\n }\n return 0;\n}', 'static av_always_inline void fill_filter_caches_inter(H264Context *h,\n int mb_type, int top_xy,\n int left_xy[LEFT_MBS],\n int top_type,\n int left_type[LEFT_MBS],\n int mb_xy, int list)\n{\n int b_stride = h->b_stride;\n int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]];\n int8_t *ref_cache = &h->ref_cache[list][scan8[0]];\n if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {\n if (USES_LIST(top_type, list)) {\n const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;\n const int b8_xy = 4 * top_xy + 2;\n int (*ref2frm)[64] = h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2);\n AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);\n ref_cache[0 - 1 * 8] =\n ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 0]];\n ref_cache[2 - 1 * 8] =\n ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 1]];\n } else {\n AV_ZERO128(mv_dst - 1 * 8);\n AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);\n }\n if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {\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 int (*ref2frm)[64] = h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2);\n AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);\n AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);\n AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);\n AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);\n ref_cache[-1 + 0] =\n ref_cache[-1 + 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 0]];\n ref_cache[-1 + 16] =\n ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 1]];\n } else {\n AV_ZERO32(mv_dst - 1 + 0);\n AV_ZERO32(mv_dst - 1 + 8);\n AV_ZERO32(mv_dst - 1 + 16);\n AV_ZERO32(mv_dst - 1 + 24);\n ref_cache[-1 + 0] =\n ref_cache[-1 + 8] =\n ref_cache[-1 + 16] =\n ref_cache[-1 + 24] = LIST_NOT_USED;\n }\n }\n }\n if (!USES_LIST(mb_type, list)) {\n fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);\n AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);\n AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);\n AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);\n AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);\n return;\n }\n {\n int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];\n int (*ref2frm)[64] = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2);\n uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;\n uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;\n AV_WN32A(&ref_cache[0 * 8], ref01);\n AV_WN32A(&ref_cache[1 * 8], ref01);\n AV_WN32A(&ref_cache[2 * 8], ref23);\n AV_WN32A(&ref_cache[3 * 8], ref23);\n }\n {\n int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * h->mb_x + 4 * h->mb_y * b_stride];\n AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);\n AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);\n AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);\n AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);\n }\n}']
35,596
0
https://github.com/openssl/openssl/blob/38d1b3cc0271008b8bd130a2c4b442775b028a08/crypto/bn/bn_shift.c/#L110
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { int i, nw, lb, rb; BN_ULONG *t, *f; BN_ULONG l; bn_check_top(r); bn_check_top(a); if (n < 0) { BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT); return 0; } 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); }
['char *SRP_create_verifier(const char *user, const char *pass, char **salt,\n char **verifier, const char *N, const char *g)\n{\n int len;\n char *result = NULL, *vf = NULL;\n const BIGNUM *N_bn = NULL, *g_bn = NULL;\n BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;\n unsigned char tmp[MAX_LEN];\n unsigned char tmp2[MAX_LEN];\n char *defgNid = NULL;\n int vfsize = 0;\n if ((user == NULL) ||\n (pass == NULL) || (salt == NULL) || (verifier == NULL))\n goto err;\n if (N) {\n if ((len = t_fromb64(tmp, sizeof(tmp), N)) <= 0)\n goto err;\n N_bn_alloc = BN_bin2bn(tmp, len, NULL);\n N_bn = N_bn_alloc;\n if ((len = t_fromb64(tmp, sizeof(tmp) ,g)) <= 0)\n goto err;\n g_bn_alloc = BN_bin2bn(tmp, len, NULL);\n g_bn = g_bn_alloc;\n defgNid = "*";\n } else {\n SRP_gN *gN = SRP_get_gN_by_id(g, NULL);\n if (gN == NULL)\n goto err;\n N_bn = gN->N;\n g_bn = gN->g;\n defgNid = gN->id;\n }\n if (*salt == NULL) {\n if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n goto err;\n s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n } else {\n if ((len = t_fromb64(tmp2, sizeof(tmp2), *salt)) <= 0)\n goto err;\n s = BN_bin2bn(tmp2, len, NULL);\n }\n if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))\n goto err;\n BN_bn2bin(v, tmp);\n vfsize = BN_num_bytes(v) * 2;\n if (((vf = OPENSSL_malloc(vfsize)) == NULL))\n goto err;\n t_tob64(vf, tmp, BN_num_bytes(v));\n if (*salt == NULL) {\n char *tmp_salt;\n if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {\n goto err;\n }\n t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);\n *salt = tmp_salt;\n }\n *verifier = vf;\n vf = NULL;\n result = defgNid;\n err:\n BN_free(N_bn_alloc);\n BN_free(g_bn_alloc);\n OPENSSL_clear_free(vf, vfsize);\n BN_clear_free(s);\n BN_clear_free(v);\n return result;\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,\n BIGNUM **verifier, const BIGNUM *N,\n const BIGNUM *g)\n{\n int result = 0;\n BIGNUM *x = NULL;\n BN_CTX *bn_ctx = BN_CTX_new();\n unsigned char tmp2[MAX_LEN];\n BIGNUM *salttmp = NULL;\n if ((user == NULL) ||\n (pass == NULL) ||\n (salt == NULL) ||\n (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))\n goto err;\n if (*salt == NULL) {\n if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n goto err;\n salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n } else {\n salttmp = *salt;\n }\n x = SRP_Calc_x(salttmp, user, pass);\n *verifier = BN_new();\n if (*verifier == NULL)\n goto err;\n if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {\n BN_clear_free(*verifier);\n goto err;\n }\n result = 1;\n *salt = salttmp;\n err:\n if (salt != NULL && *salt != salttmp)\n BN_clear_free(salttmp);\n BN_clear_free(x);\n BN_CTX_free(bn_ctx);\n return result;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return (ret);\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (!d || !r || !val[0])\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (BN_is_zero(aa)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n bn_correct_top(r);\n } else\n#endif\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return (ret);\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n top = m->top;\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_mod(&am, a, m, ctx))\n goto err;\n if (!BN_to_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits >= 0) {\n if (bits < stride)\n stride = bits + 1;\n bits -= stride;\n wvalue = bn_get_bits(p, bits + 1);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7)\n while (bits >= 0) {\n for (wvalue = 0, i = 0; i < 5; i++, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n } else {\n while (bits >= 0) {\n wvalue = bn_get_bits5(p->d, bits - 4);\n bits -= 5;\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n bits--;\n for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n while (bits >= 0) {\n wvalue = 0;\n for (i = 0; i < window; i++, bits--) {\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n }\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
35,597
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L231
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; }
['static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n BIGNUM *r1, *m1, *vrfy, *r2, *m[RSA_MAX_PRIME_NUM - 2];\n int ret = 0, i, ex_primes = 0;\n RSA_PRIME_INFO *pinfo;\n BN_CTX_start(ctx);\n r1 = BN_CTX_get(ctx);\n r2 = BN_CTX_get(ctx);\n m1 = BN_CTX_get(ctx);\n vrfy = BN_CTX_get(ctx);\n if (vrfy == NULL)\n goto err;\n if (rsa->version == RSA_ASN1_VERSION_MULTI\n && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0\n || ex_primes > RSA_MAX_PRIME_NUM - 2))\n goto err;\n {\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 if (ex_primes > 0) {\n BIGNUM *r = BN_new();\n if (r == NULL) {\n BN_free(p);\n BN_free(q);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(r, pinfo->r, BN_FLG_CONSTTIME);\n if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, r, ctx)) {\n BN_free(p);\n BN_free(q);\n BN_free(r);\n goto err;\n }\n }\n BN_free(r);\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 (ex_primes > 0) {\n BIGNUM *di = BN_new(), *cc = BN_new();\n if (cc == NULL || di == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n if ((m[i] = BN_CTX_get(ctx)) == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(cc, I, BN_FLG_CONSTTIME);\n BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, cc, pinfo->r, ctx)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n }\n BN_free(cc);\n BN_free(di);\n }\n if (!BN_sub(r0, r0, m1))\n goto err;\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->iqmp, ctx))\n goto err;\n {\n BIGNUM *pr1 = BN_new();\n if (pr1 == NULL)\n goto err;\n BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);\n if (!BN_mod(r0, pr1, rsa->p, ctx)) {\n BN_free(pr1);\n goto err;\n }\n BN_free(pr1);\n }\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->q, ctx))\n goto err;\n if (!BN_add(r0, r1, m1))\n goto err;\n if (ex_primes > 0) {\n BIGNUM *pr2 = BN_new();\n if (pr2 == NULL)\n goto err;\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n if (!BN_sub(r1, m[i], r0)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r2, r1, pinfo->t, ctx)) {\n BN_free(pr2);\n goto err;\n }\n BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, pr2, pinfo->r, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (BN_is_negative(r1))\n if (!BN_add(r1, r1, pinfo->r)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r1, r1, pinfo->pp, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_add(r0, r0, r1)) {\n BN_free(pr2);\n goto err;\n }\n }\n BN_free(pr2);\n }\n 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}', '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}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n BN_ULONG t1, t2, borrow, *rp;\n const BN_ULONG *ap, *bp;\n bn_check_top(a);\n bn_check_top(b);\n max = a->top;\n min = b->top;\n dif = max - min;\n if (dif < 0) {\n BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n return 0;\n }\n if (bn_wexpand(r, max) == NULL)\n return 0;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n borrow = bn_sub_words(rp, ap, bp, min);\n ap += min;\n rp += min;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 - borrow) & BN_MASK2;\n *(rp++) = t2;\n borrow &= (t1 == 0);\n }\n while (max && *--rp == 0)\n max--;\n r->top = max;\n r->neg = 0;\n bn_pollute(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
35,598
0
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/evp/evp_enc.c/#L329
int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, size_t inl) { size_t i; size_t bl; size_t j; if (inl <= 0) { *outl = 0; return inl == 0; } if(ctx->buf_len == 0 && (inl&(ctx->block_mask)) == 0) { if(ctx->cipher->do_cipher(ctx,out,in,inl)) { *outl=inl; return 1; } else { *outl=0; return 0; } } i=ctx->buf_len; bl=ctx->cipher->block_size; OPENSSL_assert(bl <= (int)sizeof(ctx->buf)); if (i != 0) { if (i+inl < bl) { memcpy(&(ctx->buf[i]),in,inl); ctx->buf_len+=inl; *outl=0; return 1; } else { j=bl-i; memcpy(&(ctx->buf[i]),in,j); if(!ctx->cipher->do_cipher(ctx,out,ctx->buf,bl)) return 0; inl-=j; in+=j; out+=bl; *outl=bl; } } else *outl = 0; i=inl&(bl-1); inl-=i; if (inl > 0) { if(!ctx->cipher->do_cipher(ctx,out,in,inl)) return 0; *outl+=inl; } if (i != 0) memcpy(ctx->buf,&(in[inl]),i); ctx->buf_len=i; return 1; }
['static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,\n\t\t const unsigned char *iv,int in,\n\t\t const unsigned char *plaintext,int pn,\n\t\t const unsigned char *ciphertext,int cn,\n\t\t int encdec)\n {\n EVP_CIPHER_CTX ctx;\n unsigned char out[4096];\n int outl,outl2;\n printf("Testing cipher %s%s\\n",EVP_CIPHER_name(c),\n\t (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));\n hexdump(stdout,"Key",key,kn);\n if(in)\n\thexdump(stdout,"IV",iv,in);\n hexdump(stdout,"Plaintext",plaintext,pn);\n hexdump(stdout,"Ciphertext",ciphertext,cn);\n if(kn != c->key_len)\n\t{\n\tfprintf(stderr,"Key length doesn\'t match, got %d expected %lu\\n",kn,\n\t\t(unsigned long)c->key_len);\n\ttest1_exit(5);\n\t}\n EVP_CIPHER_CTX_init(&ctx);\n if (encdec != 0)\n {\n\tif(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv))\n\t {\n\t fprintf(stderr,"EncryptInit failed\\n");\n\t ERR_print_errors_fp(stderr);\n\t test1_exit(10);\n\t }\n\tEVP_CIPHER_CTX_set_padding(&ctx,0);\n\tif(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn))\n\t {\n\t fprintf(stderr,"Encrypt failed\\n");\n\t ERR_print_errors_fp(stderr);\n\t test1_exit(6);\n\t }\n\tif(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2))\n\t {\n\t fprintf(stderr,"EncryptFinal failed\\n");\n\t ERR_print_errors_fp(stderr);\n\t test1_exit(7);\n\t }\n\tif(outl+outl2 != cn)\n\t {\n\t fprintf(stderr,"Ciphertext length mismatch got %d expected %d\\n",\n\t\t outl+outl2,cn);\n\t test1_exit(8);\n\t }\n\tif(memcmp(out,ciphertext,cn))\n\t {\n\t fprintf(stderr,"Ciphertext mismatch\\n");\n\t hexdump(stderr,"Got",out,cn);\n\t hexdump(stderr,"Expected",ciphertext,cn);\n\t test1_exit(9);\n\t }\n\t}\n if (encdec <= 0)\n {\n\tif(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv))\n\t {\n\t fprintf(stderr,"DecryptInit failed\\n");\n\t ERR_print_errors_fp(stderr);\n\t test1_exit(11);\n\t }\n\tEVP_CIPHER_CTX_set_padding(&ctx,0);\n\tif(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn))\n\t {\n\t fprintf(stderr,"Decrypt failed\\n");\n\t ERR_print_errors_fp(stderr);\n\t test1_exit(6);\n\t }\n\tif(!EVP_DecryptFinal_ex(&ctx,out+outl,&outl2))\n\t {\n\t fprintf(stderr,"DecryptFinal failed\\n");\n\t ERR_print_errors_fp(stderr);\n\t test1_exit(7);\n\t }\n\tif(outl+outl2 != cn)\n\t {\n\t fprintf(stderr,"Plaintext length mismatch got %d expected %d\\n",\n\t\t outl+outl2,cn);\n\t test1_exit(8);\n\t }\n\tif(memcmp(out,plaintext,cn))\n\t {\n\t fprintf(stderr,"Plaintext mismatch\\n");\n\t hexdump(stderr,"Got",out,cn);\n\t hexdump(stderr,"Expected",plaintext,cn);\n\t test1_exit(9);\n\t }\n\t}\n EVP_CIPHER_CTX_cleanup(&ctx);\n printf("\\n");\n }', 'int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n\t\t const unsigned char *in, size_t inl)\n\t{\n\tsize_t i;\n\tsize_t bl;\n\tsize_t j;\n\tif (inl <= 0)\n\t\t{\n\t\t*outl = 0;\n\t\treturn inl == 0;\n\t\t}\n\tif(ctx->buf_len == 0 && (inl&(ctx->block_mask)) == 0)\n\t\t{\n\t\tif(ctx->cipher->do_cipher(ctx,out,in,inl))\n\t\t\t{\n\t\t\t*outl=inl;\n\t\t\treturn 1;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\t*outl=0;\n\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\ti=ctx->buf_len;\n\tbl=ctx->cipher->block_size;\n\tOPENSSL_assert(bl <= (int)sizeof(ctx->buf));\n\tif (i != 0)\n\t\t{\n\t\tif (i+inl < bl)\n\t\t\t{\n\t\t\tmemcpy(&(ctx->buf[i]),in,inl);\n\t\t\tctx->buf_len+=inl;\n\t\t\t*outl=0;\n\t\t\treturn 1;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tj=bl-i;\n\t\t\tmemcpy(&(ctx->buf[i]),in,j);\n\t\t\tif(!ctx->cipher->do_cipher(ctx,out,ctx->buf,bl)) return 0;\n\t\t\tinl-=j;\n\t\t\tin+=j;\n\t\t\tout+=bl;\n\t\t\t*outl=bl;\n\t\t\t}\n\t\t}\n\telse\n\t\t*outl = 0;\n\ti=inl&(bl-1);\n\tinl-=i;\n\tif (inl > 0)\n\t\t{\n\t\tif(!ctx->cipher->do_cipher(ctx,out,in,inl)) return 0;\n\t\t*outl+=inl;\n\t\t}\n\tif (i != 0)\n\t\tmemcpy(ctx->buf,&(in[inl]),i);\n\tctx->buf_len=i;\n\treturn 1;\n\t}']
35,599
0
https://gitlab.com/libtiff/libtiff/blob/3adc33842b7533066daea2516741832edc44d5fd/libtiff/tif_tile.c/#L156
uint32 TIFFNumberOfTiles(TIFF* tif) { TIFFDirectory *td = &tif->tif_dir; uint32 dx = td->td_tilewidth; uint32 dy = td->td_tilelength; uint32 dz = td->td_tiledepth; uint32 ntiles; if (dx == (uint32) -1) dx = td->td_imagewidth; if (dy == (uint32) -1) dy = td->td_imagelength; if (dz == (uint32) -1) dz = td->td_imagedepth; ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 : multiply_32(tif, multiply_32(tif, TIFFhowmany_32(td->td_imagewidth, dx), TIFFhowmany_32(td->td_imagelength, dy), "TIFFNumberOfTiles"), TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles"); if (td->td_planarconfig == PLANARCONFIG_SEPARATE) ntiles = multiply_32(tif, ntiles, td->td_samplesperpixel, "TIFFNumberOfTiles"); return (ntiles); }
['static int\nwriteSelections(TIFF *in, TIFF **out, struct crop_mask *crop,\n struct image_data *image, struct dump_opts *dump,\n struct buffinfo seg_buffs[], char *mp, char *filename,\n unsigned int *page, unsigned int total_pages)\n {\n int i, page_count;\n int autoindex = 0;\n unsigned char *crop_buff = NULL;\n switch (crop->exp_mode)\n {\n case ONE_FILE_COMPOSITE:\n autoindex = 0;\n crop_buff = seg_buffs[0].buffer;\n if (update_output_file (out, mp, autoindex, filename, page))\n return (1);\n page_count = total_pages;\n if (writeCroppedImage(in, *out, image, dump,\n crop->combined_width,\n crop->combined_length,\n crop_buff, *page, total_pages))\n {\n TIFFError("writeRegions", "Unable to write new image");\n return (-1);\n }\n\t break;\n case ONE_FILE_SEPARATED:\n autoindex = 0;\n if (update_output_file (out, mp, autoindex, filename, page))\n return (1);\n page_count = crop->selections * total_pages;\n for (i = 0; i < crop->selections; i++)\n {\n crop_buff = seg_buffs[i].buffer;\n if (writeCroppedImage(in, *out, image, dump,\n crop->regionlist[i].width,\n crop->regionlist[i].length,\n crop_buff, *page, page_count))\n {\n TIFFError("writeRegions", "Unable to write new image");\n return (-1);\n }\n\t }\n break;\n case FILE_PER_IMAGE_COMPOSITE:\n autoindex = 1;\n if (update_output_file (out, mp, autoindex, filename, page))\n return (1);\n crop_buff = seg_buffs[0].buffer;\n if (writeCroppedImage(in, *out, image, dump,\n crop->combined_width,\n crop->combined_length,\n crop_buff, *page, total_pages))\n {\n TIFFError("writeRegions", "Unable to write new image");\n return (-1);\n }\n break;\n case FILE_PER_IMAGE_SEPARATED:\n autoindex = 1;\n page_count = crop->selections;\n if (update_output_file (out, mp, autoindex, filename, page))\n return (1);\n for (i = 0; i < crop->selections; i++)\n {\n crop_buff = seg_buffs[i].buffer;\n if (writeCroppedImage(in, *out, image, dump,\n crop->regionlist[i].width,\n crop->regionlist[i].length,\n crop_buff, *page, page_count))\n {\n TIFFError("writeRegions", "Unable to write new image");\n return (-1);\n }\n }\n break;\n case FILE_PER_SELECTION:\n autoindex = 1;\n\t page_count = 1;\n for (i = 0; i < crop->selections; i++)\n {\n if (update_output_file (out, mp, autoindex, filename, page))\n return (1);\n crop_buff = seg_buffs[i].buffer;\n if (writeCroppedImage(in, *out, image, dump,\n crop->regionlist[i].width,\n crop->regionlist[i].length,\n crop_buff, *page, page_count))\n {\n TIFFError("writeRegions", "Unable to write new image");\n return (-1);\n }\n }\n\t break;\n default: return (1);\n }\n return (0);\n }', 'static int\nwriteCroppedImage(TIFF *in, TIFF *out, struct image_data *image,\n struct dump_opts *dump, uint32 width, uint32 length,\n unsigned char *crop_buff, int pagenum, int total_pages)\n {\n uint16 bps, spp;\n uint16 input_compression, input_photometric;\n uint16 input_jpeg_colormode, input_planar;\n struct cpTag* p;\n TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &input_photometric);\n TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &spp);\n TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &bps);\n TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);\n TIFFSetField(out, TIFFTAG_IMAGELENGTH, length);\n CopyField(TIFFTAG_BITSPERSAMPLE, bps);\n CopyField(TIFFTAG_SAMPLESPERPIXEL, spp);\n TIFFGetField(in, TIFFTAG_COMPRESSION, &input_compression);\n if (compression != (uint16)-1)\n TIFFSetField(out, TIFFTAG_COMPRESSION, compression);\n else\n {\n if (input_compression == COMPRESSION_OJPEG)\n {\n TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_JPEG);\n compression = COMPRESSION_JPEG;\n }\n else\n CopyField(TIFFTAG_COMPRESSION, compression);\n }\n TIFFGetField(in, TIFFTAG_JPEGCOLORMODE, &input_jpeg_colormode);\n#ifdef DEBUG2\n TIFFError("writeCroppedImage", "Input compression: %s",\n\t (input_compression == COMPRESSION_OJPEG) ? "Old Jpeg" :\n\t ((input_compression == COMPRESSION_JPEG) ? "New Jpeg" : "Non Jpeg"));\n#endif\n if (compression == COMPRESSION_JPEG)\n {\n if ((input_photometric == PHOTOMETRIC_PALETTE) ||\n (input_photometric == PHOTOMETRIC_MASK))\n {\n TIFFError ("writeCroppedImage",\n "JPEG compression cannot be used with %s image data",\n \t (input_photometric == PHOTOMETRIC_PALETTE) ?\n "palette" : "mask");\n return (-1);\n }\n if (input_photometric == PHOTOMETRIC_RGB)\n {\n if (jpegcolormode == JPEGCOLORMODE_RGB)\n\tTIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);\n else\n TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);\n }\n else\n TIFFSetField(out, TIFFTAG_PHOTOMETRIC, input_photometric);\n }\n else\n {\n if (compression == COMPRESSION_SGILOG || compression == COMPRESSION_SGILOG24)\n {\n TIFFSetField(out, TIFFTAG_PHOTOMETRIC, spp == 1 ?\n\t\t\tPHOTOMETRIC_LOGL : PHOTOMETRIC_LOGLUV);\n }\n else\n {\n if (input_compression == COMPRESSION_SGILOG ||\n input_compression == COMPRESSION_SGILOG24)\n {\n TIFFSetField(out, TIFFTAG_PHOTOMETRIC, spp == 1 ?\n\t\t\t PHOTOMETRIC_LOGL : PHOTOMETRIC_LOGLUV);\n }\n else\n TIFFSetField(out, TIFFTAG_PHOTOMETRIC, image->photometric);\n }\n }\n if (((input_photometric == PHOTOMETRIC_LOGL) ||\n (input_photometric == PHOTOMETRIC_LOGLUV)) &&\n ((compression != COMPRESSION_SGILOG) &&\n (compression != COMPRESSION_SGILOG24)))\n {\n TIFFError("writeCroppedImage",\n "LogL and LogLuv source data require SGI_LOG or SGI_LOG24 compression");\n return (-1);\n }\n if (fillorder != 0)\n TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);\n else\n CopyTag(TIFFTAG_FILLORDER, 1, TIFF_SHORT);\n TIFFSetField(out, TIFFTAG_ORIENTATION, image->orientation);\n if (outtiled == -1)\n outtiled = TIFFIsTiled(in);\n if (outtiled) {\n if (tilewidth == (uint32) 0)\n TIFFGetField(in, TIFFTAG_TILEWIDTH, &tilewidth);\n if (tilelength == (uint32) 0)\n TIFFGetField(in, TIFFTAG_TILELENGTH, &tilelength);\n if (tilewidth == 0 || tilelength == 0)\n TIFFDefaultTileSize(out, &tilewidth, &tilelength);\n TIFFSetField(out, TIFFTAG_TILEWIDTH, tilewidth);\n TIFFSetField(out, TIFFTAG_TILELENGTH, tilelength);\n } else {\n\tif (rowsperstrip == (uint32) 0)\n {\n\t if (!TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip))\n\t rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);\n if (compression != COMPRESSION_JPEG)\n {\n \t if (rowsperstrip > length)\n\t rowsperstrip = length;\n\t }\n\t }\n\telse\n if (rowsperstrip == (uint32) -1)\n\t rowsperstrip = length;\n\tTIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);\n\t}\n TIFFGetFieldDefaulted(in, TIFFTAG_PLANARCONFIG, &input_planar);\n if (config != (uint16) -1)\n TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);\n else\n CopyField(TIFFTAG_PLANARCONFIG, config);\n if (spp <= 4)\n CopyTag(TIFFTAG_TRANSFERFUNCTION, 4, TIFF_SHORT);\n CopyTag(TIFFTAG_COLORMAP, 4, TIFF_SHORT);\n switch (compression) {\n case COMPRESSION_JPEG:\n if (((bps % 8) == 0) || ((bps % 12) == 0))\n\t {\n TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);\n\t TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);\n }\n else\n {\n\t TIFFError("writeCroppedImage",\n "JPEG compression requires 8 or 12 bits per sample");\n return (-1);\n }\n\t break;\n case COMPRESSION_LZW:\n case COMPRESSION_ADOBE_DEFLATE:\n case COMPRESSION_DEFLATE:\n\tif (predictor != (uint16)-1)\n TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);\n\telse\n\t CopyField(TIFFTAG_PREDICTOR, predictor);\n\tbreak;\n case COMPRESSION_CCITTFAX3:\n case COMPRESSION_CCITTFAX4:\n if (bps != 1)\n {\n\t TIFFError("writeCroppedImage",\n "Group 3/4 compression is not usable with bps > 1");\n return (-1);\n\t }\n\tif (compression == COMPRESSION_CCITTFAX3) {\n if (g3opts != (uint32) -1)\n\t TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, g3opts);\n\t else\n\t CopyField(TIFFTAG_GROUP3OPTIONS, g3opts);\n\t} else\n\t CopyTag(TIFFTAG_GROUP4OPTIONS, 1, TIFF_LONG);\n\t CopyTag(TIFFTAG_BADFAXLINES, 1, TIFF_LONG);\n\t CopyTag(TIFFTAG_CLEANFAXDATA, 1, TIFF_LONG);\n\t CopyTag(TIFFTAG_CONSECUTIVEBADFAXLINES, 1, TIFF_LONG);\n\t CopyTag(TIFFTAG_FAXRECVPARAMS, 1, TIFF_LONG);\n\t CopyTag(TIFFTAG_FAXRECVTIME, 1, TIFF_LONG);\n\t CopyTag(TIFFTAG_FAXSUBADDRESS, 1, TIFF_ASCII);\n\t break;\n case COMPRESSION_NONE:\n break;\n default: break;\n }\n { uint32 len32;\n void** data;\n if (TIFFGetField(in, TIFFTAG_ICCPROFILE, &len32, &data))\n TIFFSetField(out, TIFFTAG_ICCPROFILE, len32, data);\n }\n { uint16 ninks;\n const char* inknames;\n if (TIFFGetField(in, TIFFTAG_NUMBEROFINKS, &ninks)) {\n TIFFSetField(out, TIFFTAG_NUMBEROFINKS, ninks);\n if (TIFFGetField(in, TIFFTAG_INKNAMES, &inknames)) {\n\t int inknameslen = strlen(inknames) + 1;\n\t const char* cp = inknames;\n\t while (ninks > 1) {\n\t cp = strchr(cp, \'\\0\');\n\t if (cp) {\n\t cp++;\n\t inknameslen += (strlen(cp) + 1);\n\t }\n\t ninks--;\n }\n\t TIFFSetField(out, TIFFTAG_INKNAMES, inknameslen, inknames);\n }\n }\n }\n {\n unsigned short pg0, pg1;\n if (TIFFGetField(in, TIFFTAG_PAGENUMBER, &pg0, &pg1)) {\n TIFFSetField(out, TIFFTAG_PAGENUMBER, pagenum, total_pages);\n }\n }\n for (p = tags; p < &tags[NTAGS]; p++)\n\t\tCopyTag(p->tag, p->count, p->type);\n if (outtiled)\n {\n if (config == PLANARCONFIG_CONTIG)\n {\n if (writeBufferToContigTiles (out, crop_buff, length, width, spp, dump))\n TIFFError("","Unable to write contiguous tile data for page %d", pagenum);\n }\n else\n {\n if (writeBufferToSeparateTiles (out, crop_buff, length, width, spp, dump))\n TIFFError("","Unable to write separate tile data for page %d", pagenum);\n }\n }\n else\n {\n if (config == PLANARCONFIG_CONTIG)\n {\n if (writeBufferToContigStrips (out, crop_buff, length))\n TIFFError("","Unable to write contiguous strip data for page %d", pagenum);\n }\n else\n {\n if (writeBufferToSeparateStrips(out, crop_buff, length, width, spp, dump))\n TIFFError("","Unable to write separate strip data for page %d", pagenum);\n }\n }\n if (!TIFFWriteDirectory(out))\n {\n TIFFError("","Failed to write IFD for page number %d", pagenum);\n TIFFClose(out);\n return (-1);\n }\n return (0);\n }', 'static int writeBufferToContigTiles (TIFF* out, uint8* buf, uint32 imagelength,\n\t\t\t\t uint32 imagewidth, tsample_t spp,\n struct dump_opts* dump)\n {\n uint16 bps;\n uint32 tl, tw;\n uint32 row, col, nrow, ncol;\n uint32 src_rowsize, col_offset;\n uint32 tile_rowsize = TIFFTileRowSize(out);\n uint8* bufp = (uint8*) buf;\n tsize_t tile_buffsize = 0;\n tsize_t tilesize = TIFFTileSize(out);\n unsigned char *tilebuf = NULL;\n TIFFGetField(out, TIFFTAG_TILELENGTH, &tl);\n TIFFGetField(out, TIFFTAG_TILEWIDTH, &tw);\n TIFFGetField(out, TIFFTAG_BITSPERSAMPLE, &bps);\n tile_buffsize = tilesize;\n if (tilesize < (tsize_t)(tl * tile_rowsize))\n {\n#ifdef DEBUG2\n TIFFError("writeBufferToContigTiles",\n\t "Tilesize %lu is too small, using alternate calculation %u",\n tilesize, tl * tile_rowsize);\n#endif\n tile_buffsize = tl * tile_rowsize;\n }\n tilebuf = _TIFFmalloc(tile_buffsize);\n if (tilebuf == 0)\n return 1;\n src_rowsize = ((imagewidth * spp * bps) + 7) / 8;\n for (row = 0; row < imagelength; row += tl)\n {\n nrow = (row + tl > imagelength) ? imagelength - row : tl;\n for (col = 0; col < imagewidth; col += tw)\n {\n if (col + tw > imagewidth)\n\tncol = imagewidth - col;\n else\n ncol = tw;\n col_offset = (((col * bps * spp) + 7) / 8);\n bufp = buf + (row * src_rowsize) + col_offset;\n if (extractContigSamplesToTileBuffer(tilebuf, bufp, nrow, ncol, imagewidth,\n\t\t\t\t\t tw, 0, spp, spp, bps, dump) > 0)\n {\n\tTIFFError("writeBufferToContigTiles",\n "Unable to extract data to tile for row %lu, col %lu",\n (unsigned long) row, (unsigned long)col);\n\t_TIFFfree(tilebuf);\n\treturn 1;\n }\n if (TIFFWriteTile(out, tilebuf, col, row, 0, 0) < 0)\n {\n\tTIFFError("writeBufferToContigTiles",\n\t "Cannot write tile at %lu %lu",\n\t (unsigned long) col, (unsigned long) row);\n\t _TIFFfree(tilebuf);\n\treturn 1;\n\t}\n }\n }\n _TIFFfree(tilebuf);\n return 0;\n }', 'tmsize_t\nTIFFWriteTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)\n{\n\tif (!TIFFCheckTile(tif, x, y, z, s))\n\t\treturn ((tmsize_t)(-1));\n\treturn (TIFFWriteEncodedTile(tif,\n\t TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));\n}', 'int\nTIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tif (x >= td->td_imagewidth) {\n\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t "%lu: Col out of range, max %lu",\n\t\t\t (unsigned long) x,\n\t\t\t (unsigned long) (td->td_imagewidth - 1));\n\t\treturn (0);\n\t}\n\tif (y >= td->td_imagelength) {\n\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t "%lu: Row out of range, max %lu",\n\t\t\t (unsigned long) y,\n\t\t\t (unsigned long) (td->td_imagelength - 1));\n\t\treturn (0);\n\t}\n\tif (z >= td->td_imagedepth) {\n\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t "%lu: Depth out of range, max %lu",\n\t\t\t (unsigned long) z,\n\t\t\t (unsigned long) (td->td_imagedepth - 1));\n\t\treturn (0);\n\t}\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE &&\n\t s >= td->td_samplesperpixel) {\n\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t "%lu: Sample out of range, max %lu",\n\t\t\t (unsigned long) s,\n\t\t\t (unsigned long) (td->td_samplesperpixel - 1));\n\t\treturn (0);\n\t}\n\treturn (1);\n}', 'uint32\nTIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 dx = td->td_tilewidth;\n\tuint32 dy = td->td_tilelength;\n\tuint32 dz = td->td_tiledepth;\n\tuint32 tile = 1;\n\tif (td->td_imagedepth == 1)\n\t\tz = 0;\n\tif (dx == (uint32) -1)\n\t\tdx = td->td_imagewidth;\n\tif (dy == (uint32) -1)\n\t\tdy = td->td_imagelength;\n\tif (dz == (uint32) -1)\n\t\tdz = td->td_imagedepth;\n\tif (dx != 0 && dy != 0 && dz != 0) {\n\t\tuint32 xpt = TIFFhowmany_32(td->td_imagewidth, dx);\n\t\tuint32 ypt = TIFFhowmany_32(td->td_imagelength, dy);\n\t\tuint32 zpt = TIFFhowmany_32(td->td_imagedepth, dz);\n\t\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\t\ttile = (xpt*ypt*zpt)*s +\n\t\t\t (xpt*ypt)*(z/dz) +\n\t\t\t xpt*(y/dy) +\n\t\t\t x/dx;\n\t\telse\n\t\t\ttile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx;\n\t}\n\treturn (tile);\n}', 'tmsize_t\nTIFFWriteEncodedTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)\n{\n\tstatic const char module[] = "TIFFWriteEncodedTile";\n\tTIFFDirectory *td;\n\tuint16 sample;\n\tif (!WRITECHECKTILES(tif, module))\n\t\treturn ((tmsize_t)(-1));\n\ttd = &tif->tif_dir;\n\tif (tile >= td->td_nstrips) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range, max %lu",\n\t\t (unsigned long) tile, (unsigned long) td->td_nstrips);\n\t\treturn ((tmsize_t)(-1));\n\t}\n\tif (!BUFFERCHECK(tif))\n\t\treturn ((tmsize_t)(-1));\n tif->tif_flags |= TIFF_BUF4WRITE;\n\ttif->tif_curtile = tile;\n\ttif->tif_rawcc = 0;\n\ttif->tif_rawcp = tif->tif_rawdata;\n\tif( td->td_stripbytecount[tile] > 0 )\n {\n tif->tif_curoff = 0;\n }\n\ttif->tif_row = (tile % TIFFhowmany_32(td->td_imagelength, td->td_tilelength))\n\t\t* td->td_tilelength;\n\ttif->tif_col = (tile % TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth))\n\t\t* td->td_tilewidth;\n\tif ((tif->tif_flags & TIFF_CODERSETUP) == 0) {\n\t\tif (!(*tif->tif_setupencode)(tif))\n\t\t\treturn ((tmsize_t)(-1));\n\t\ttif->tif_flags |= TIFF_CODERSETUP;\n\t}\n\ttif->tif_flags &= ~TIFF_POSTENCODE;\n\tsample = (uint16)(tile/td->td_stripsperimage);\n\tif (!(*tif->tif_preencode)(tif, sample))\n\t\treturn ((tmsize_t)(-1));\n\tif ( cc < 1 || cc > tif->tif_tilesize)\n\t\tcc = tif->tif_tilesize;\n\ttif->tif_postdecode( tif, (uint8*) data, cc );\n\tif (!(*tif->tif_encodetile)(tif, (uint8*) data, cc, sample))\n\t\treturn (0);\n\tif (!(*tif->tif_postencode)(tif))\n\t\treturn ((tmsize_t)(-1));\n\tif (!isFillOrder(tif, td->td_fillorder) &&\n\t (tif->tif_flags & TIFF_NOBITREV) == 0)\n\t\tTIFFReverseBits((uint8*)tif->tif_rawdata, tif->tif_rawcc);\n\tif (tif->tif_rawcc > 0 && !TIFFAppendToStrip(tif, tile,\n\t tif->tif_rawdata, tif->tif_rawcc))\n\t\treturn ((tmsize_t)(-1));\n\ttif->tif_rawcc = 0;\n\ttif->tif_rawcp = tif->tif_rawdata;\n\treturn (cc);\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 stripped image" :\n\t\t "Can not write scanlines to a tiled image");\n\t\treturn (0);\n\t}\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 == 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}', 'int\nTIFFSetupStrips(TIFF* tif)\n{\n\tTIFFDirectory* td = &tif->tif_dir;\n\tif (isTiled(tif))\n\t\ttd->td_stripsperimage =\n\t\t isUnspecified(tif, FIELD_TILEDIMENSIONS) ?\n\t\t\ttd->td_samplesperpixel : TIFFNumberOfTiles(tif);\n\telse\n\t\ttd->td_stripsperimage =\n\t\t isUnspecified(tif, FIELD_ROWSPERSTRIP) ?\n\t\t\ttd->td_samplesperpixel : TIFFNumberOfStrips(tif);\n\ttd->td_nstrips = td->td_stripsperimage;\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\ttd->td_stripsperimage /= td->td_samplesperpixel;\n\ttd->td_stripoffset = (uint64 *)\n\t _TIFFmalloc(td->td_nstrips * sizeof (uint64));\n\ttd->td_stripbytecount = (uint64 *)\n\t _TIFFmalloc(td->td_nstrips * sizeof (uint64));\n\tif (td->td_stripoffset == NULL || td->td_stripbytecount == NULL)\n\t\treturn (0);\n\t_TIFFmemset(td->td_stripoffset, 0, td->td_nstrips*sizeof (uint64));\n\t_TIFFmemset(td->td_stripbytecount, 0, td->td_nstrips*sizeof (uint64));\n\tTIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);\n\tTIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);\n\treturn (1);\n}', 'uint32\nTIFFNumberOfTiles(TIFF* tif)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 dx = td->td_tilewidth;\n\tuint32 dy = td->td_tilelength;\n\tuint32 dz = td->td_tiledepth;\n\tuint32 ntiles;\n\tif (dx == (uint32) -1)\n\t\tdx = td->td_imagewidth;\n\tif (dy == (uint32) -1)\n\t\tdy = td->td_imagelength;\n\tif (dz == (uint32) -1)\n\t\tdz = td->td_imagedepth;\n\tntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :\n\t multiply_32(tif, multiply_32(tif, TIFFhowmany_32(td->td_imagewidth, dx),\n\t TIFFhowmany_32(td->td_imagelength, dy),\n\t "TIFFNumberOfTiles"),\n\t TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\tntiles = multiply_32(tif, ntiles, td->td_samplesperpixel,\n\t\t "TIFFNumberOfTiles");\n\treturn (ntiles);\n}']
35,600
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/ac3enc.c/#L235
static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n) { int sum, i; sum = 0; for(i=0;i<n;i++) { sum += abs(exp1[i] - exp2[i]); } return sum; }
['static void compute_exp_strategy(uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],\n uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],\n int ch, int is_lfe)\n{\n int i, j;\n int exp_diff;\n exp_strategy[0][ch] = EXP_NEW;\n for(i=1;i<NB_BLOCKS;i++) {\n exp_diff = calc_exp_diff(exp[i][ch], exp[i-1][ch], N/2);\n#ifdef DEBUG\n av_log(NULL, AV_LOG_DEBUG, "exp_diff=%d\\n", exp_diff);\n#endif\n if (exp_diff > EXP_DIFF_THRESHOLD)\n exp_strategy[i][ch] = EXP_NEW;\n else\n exp_strategy[i][ch] = EXP_REUSE;\n }\n if (is_lfe)\n return;\n i = 0;\n while (i < NB_BLOCKS) {\n j = i + 1;\n while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE)\n j++;\n switch(j - i) {\n case 1:\n exp_strategy[i][ch] = EXP_D45;\n break;\n case 2:\n case 3:\n exp_strategy[i][ch] = EXP_D25;\n break;\n default:\n exp_strategy[i][ch] = EXP_D15;\n break;\n }\n i = j;\n }\n}', 'static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n)\n{\n int sum, i;\n sum = 0;\n for(i=0;i<n;i++) {\n sum += abs(exp1[i] - exp2[i]);\n }\n return sum;\n}']