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
|
---|---|---|---|---|
1,201 | 0 |
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/bn/bn_ctx.c/#L353
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int test_mod(BIO *bp, BN_CTX *ctx)\n{\n BIGNUM *a, *b, *c, *d, *e;\n int i;\n a = BN_new();\n b = BN_new();\n c = BN_new();\n d = BN_new();\n e = BN_new();\n BN_bntest_rand(a, 1024, 0, 0);\n for (i = 0; i < num0; i++) {\n BN_bntest_rand(b, 450 + i * 10, 0, 0);\n a->neg = rand_neg();\n b->neg = rand_neg();\n BN_mod(c, a, b, ctx);\n if (bp != NULL) {\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " % ");\n BN_print(bp, b);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, c);\n BIO_puts(bp, "\\n");\n }\n BN_div(d, e, a, b, ctx);\n BN_sub(e, e, c);\n if (!BN_is_zero(e)) {\n fprintf(stderr, "Modulo test failed!\\n");\n return 0;\n }\n }\n BN_free(a);\n BN_free(b);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n return (1);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
1,202 | 0 |
https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/crypto/bn/bn_ctx.c/#L276
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)\n{\n int ok = 0;\n BIGNUM *tmp = NULL;\n BN_CTX *ctx = NULL;\n *ret = 0;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL || !BN_set_word(tmp, 1))\n goto err;\n if (BN_cmp(pub_key, tmp) <= 0)\n *ret |= DH_CHECK_PUBKEY_TOO_SMALL;\n if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))\n goto err;\n if (BN_cmp(pub_key, tmp) >= 0)\n *ret |= DH_CHECK_PUBKEY_TOO_LARGE;\n if (dh->q != NULL) {\n if (!BN_mod_exp(tmp, pub_key, dh->q, dh->p, ctx))\n goto err;\n if (!BN_is_one(tmp))\n *ret |= DH_CHECK_PUBKEY_INVALID;\n }\n ok = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n }\n return ok;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_RECP_CTX_free(&recp);\n bn_check_top(r);\n return ret;\n}', 'int BN_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_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,\n BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *a;\n const BIGNUM *ca;\n BN_CTX_start(ctx);\n if ((a = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (y != NULL) {\n if (x == y) {\n if (!BN_sqr(a, x, ctx))\n goto err;\n } else {\n if (!BN_mul(a, x, y, ctx))\n goto err;\n }\n ca = a;\n } else\n ca = x;\n ret = BN_div_recp(NULL, r, ca, recp, ctx);\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return ret;\n}', 'int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,\n BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n int i, j, ret = 0;\n BIGNUM *a, *b, *d, *r;\n BN_CTX_start(ctx);\n d = (dv != NULL) ? dv : BN_CTX_get(ctx);\n r = (rem != NULL) ? rem : BN_CTX_get(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (b == NULL)\n goto err;\n if (BN_ucmp(m, &(recp->N)) < 0) {\n BN_zero(d);\n if (!BN_copy(r, m)) {\n BN_CTX_end(ctx);\n return 0;\n }\n BN_CTX_end(ctx);\n return 1;\n }\n i = BN_num_bits(m);\n j = recp->num_bits << 1;\n if (j > i)\n i = j;\n if (i != recp->shift)\n recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);\n if (recp->shift == -1)\n goto err;\n if (!BN_rshift(a, m, recp->num_bits))\n goto err;\n if (!BN_mul(b, a, &(recp->Nr), ctx))\n goto err;\n if (!BN_rshift(d, b, i - recp->num_bits))\n goto err;\n d->neg = 0;\n if (!BN_mul(b, &(recp->N), d, ctx))\n goto err;\n if (!BN_usub(r, m, b))\n goto err;\n r->neg = 0;\n j = 0;\n while (BN_ucmp(r, &(recp->N)) >= 0) {\n if (j++ > 2) {\n BNerr(BN_F_BN_DIV_RECP, BN_R_BAD_RECIPROCAL);\n goto err;\n }\n if (!BN_usub(r, r, &(recp->N)))\n goto err;\n if (!BN_add_word(d, 1))\n goto err;\n }\n r->neg = BN_is_zero(r) ? 0 : m->neg;\n d->neg = m->neg ^ recp->N.neg;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(dv);\n bn_check_top(rem);\n return ret;\n}', '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}']
|
1,203 | 0 |
https://github.com/libav/libav/blob/4860abb116674c7be31e825db05cdcfd30f3aff2/libavcodec/flac.c/#L152
|
void ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
const uint8_t *buffer)
{
GetBitContext gb;
init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
s->min_blocksize = get_bits(&gb, 16);
s->max_blocksize = get_bits(&gb, 16);
skip_bits(&gb, 24);
s->max_framesize = get_bits_long(&gb, 24);
s->samplerate = get_bits_long(&gb, 20);
s->channels = get_bits(&gb, 3) + 1;
s->bps = get_bits(&gb, 5) + 1;
avctx->channels = s->channels;
avctx->sample_rate = s->samplerate;
skip_bits(&gb, 36);
skip_bits(&gb, 64);
skip_bits(&gb, 64);
dump_headers(avctx, s);
}
|
['void ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,\n const uint8_t *buffer)\n{\n GetBitContext gb;\n init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);\n s->min_blocksize = get_bits(&gb, 16);\n s->max_blocksize = get_bits(&gb, 16);\n skip_bits(&gb, 24);\n s->max_framesize = get_bits_long(&gb, 24);\n s->samplerate = get_bits_long(&gb, 20);\n s->channels = get_bits(&gb, 3) + 1;\n s->bps = get_bits(&gb, 5) + 1;\n avctx->channels = s->channels;\n avctx->sample_rate = s->samplerate;\n skip_bits(&gb, 36);\n skip_bits(&gb, 64);\n skip_bits(&gb, 64);\n dump_headers(avctx, s);\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 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}']
|
1,204 | 0 |
https://gitlab.com/libtiff/libtiff/blob/574447a0b041f3ab8d38aaec2b2d58b7f5dfb2f1/libtiff/tif_read.c/#L1012
|
static int
TIFFStartTile(TIFF* tif, uint32 tile)
{
static const char module[] = "TIFFStartTile";
TIFFDirectory *td = &tif->tif_dir;
uint32 howmany32;
if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
return 0;
if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
if (!(*tif->tif_setupdecode)(tif))
return (0);
tif->tif_flags |= TIFF_CODERSETUP;
}
tif->tif_curtile = tile;
howmany32=TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
if (howmany32 == 0) {
TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
return 0;
}
tif->tif_row = (tile % howmany32) * td->td_tilelength;
howmany32=TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
if (howmany32 == 0) {
TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
return 0;
}
tif->tif_col = (tile % howmany32) * td->td_tilewidth;
tif->tif_flags &= ~TIFF_BUF4WRITE;
if (tif->tif_flags&TIFF_NOREADRAW)
{
tif->tif_rawcp = NULL;
tif->tif_rawcc = 0;
}
else
{
tif->tif_rawcp = tif->tif_rawdata;
tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[tile];
}
return ((*tif->tif_predecode)(tif,
(uint16)(tile/td->td_stripsperimage)));
}
|
['tsize_t t2p_write_pdf(T2P* t2p, TIFF* input, TIFF* output){\n\ttsize_t written=0;\n\tttile_t i2=0;\n\ttsize_t streamlen=0;\n\tuint16 i=0;\n\tt2p_read_tiff_init(t2p, input);\n\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\tt2p->pdf_xrefoffsets= (uint32*) _TIFFmalloc(TIFFSafeMultiply(tmsize_t,t2p->pdf_xrefcount,sizeof(uint32)) );\n\tif(t2p->pdf_xrefoffsets==NULL){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"Can\'t allocate %u bytes of memory for t2p_write_pdf",\n\t\t\t(unsigned int) (t2p->pdf_xrefcount * sizeof(uint32)) );\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn(written);\n\t}\n\tt2p->pdf_xrefcount=0;\n\tt2p->pdf_catalog=1;\n\tt2p->pdf_info=2;\n\tt2p->pdf_pages=3;\n\twritten += t2p_write_pdf_header(t2p, output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_catalog=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_catalog(t2p, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_info=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_info(t2p, input, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_pages=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_pages(t2p, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tfor(t2p->pdf_page=0;t2p->pdf_page<t2p->tiff_pagecount;t2p->pdf_page++){\n\t\tt2p_read_tiff_data(t2p, input);\n\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_page(t2p->pdf_xrefcount, t2p, output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\twritten += t2p_write_pdf_stream_dict(0, t2p->pdf_xrefcount+1, output);\n\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\twritten += t2p_write_pdf_stream_start(output);\n\t\tstreamlen=written;\n\t\twritten += t2p_write_pdf_page_content_stream(t2p, output);\n\t\tstreamlen=written-streamlen;\n\t\twritten += t2p_write_pdf_stream_end(output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tif(t2p->tiff_transferfunctioncount != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_transfer(t2p, output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\tfor(i=0; i < t2p->tiff_transferfunctioncount; i++){\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\t\twritten += t2p_write_pdf_transfer_dict(t2p, output, i);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\t\twritten += t2p_write_pdf_transfer_stream(t2p, output, i);\n\t\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t}\n\t\t}\n\t\tif( (t2p->pdf_colorspace & T2P_CS_PALETTE) != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\tt2p->pdf_palettecs=t2p->pdf_xrefcount;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_stream_dict(t2p->pdf_palettesize, 0, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_palettecs_stream(t2p, output);\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t\tif( (t2p->pdf_colorspace & T2P_CS_ICCBASED) != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\tt2p->pdf_icccs=t2p->pdf_xrefcount;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_icccs_dict(t2p, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_icccs_stream(t2p, output);\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t\tif(t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount !=0){\n\t\t\tfor(i2=0;i2<t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount;i2++){\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\t\twritten += t2p_write_pdf_xobject_stream_dict(\n\t\t\t\t\ti2+1,\n\t\t\t\t\tt2p,\n\t\t\t\t\toutput);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\t\tstreamlen=written;\n\t\t\t\tt2p_read_tiff_size_tile(t2p, input, i2);\n\t\t\t\twritten += t2p_readwrite_pdf_image_tile(t2p, input, output, i2);\n\t\t\t\tt2p_write_advance_directory(t2p, output);\n\t\t\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\t\t\tstreamlen=written-streamlen;\n\t\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t}\n\t\t} else {\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_stream_dict(\n\t\t\t\t0,\n\t\t\t\tt2p,\n\t\t\t\toutput);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\tstreamlen=written;\n\t\t\tt2p_read_tiff_size(t2p, input);\n\t\t\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_init(T2P* t2p, TIFF* input){\n\ttdir_t directorycount=0;\n\ttdir_t i=0;\n\tuint16 pagen=0;\n\tuint16 paged=0;\n\tuint16 xuint16=0;\n\tdirectorycount=TIFFNumberOfDirectories(input);\n\tt2p->tiff_pages = (T2P_PAGE*) _TIFFmalloc(TIFFSafeMultiply(tmsize_t,directorycount,sizeof(T2P_PAGE)));\n\tif(t2p->tiff_pages==NULL){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"Can\'t allocate %lu bytes of memory for tiff_pages array, %s",\n\t\t\t(unsigned long) directorycount * sizeof(T2P_PAGE),\n\t\t\tTIFFFileName(input));\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn;\n\t}\n\t_TIFFmemset( t2p->tiff_pages, 0x00, directorycount * sizeof(T2P_PAGE));\n\tt2p->tiff_tiles = (T2P_TILES*) _TIFFmalloc(TIFFSafeMultiply(tmsize_t,directorycount,sizeof(T2P_TILES)));\n\tif(t2p->tiff_tiles==NULL){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"Can\'t allocate %lu bytes of memory for tiff_tiles array, %s",\n\t\t\t(unsigned long) directorycount * sizeof(T2P_TILES),\n\t\t\tTIFFFileName(input));\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn;\n\t}\n\t_TIFFmemset( t2p->tiff_tiles, 0x00, directorycount * sizeof(T2P_TILES));\n\tfor(i=0;i<directorycount;i++){\n\t\tuint32 subfiletype = 0;\n\t\tif(!TIFFSetDirectory(input, i)){\n\t\t\tTIFFError(\n\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t"Can\'t set directory %u of input file %s",\n\t\t\t\ti,\n\t\t\t\tTIFFFileName(input));\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn;\n\t\t}\n\t\tif(TIFFGetField(input, TIFFTAG_PAGENUMBER, &pagen, &paged)){\n\t\t\tif((pagen>paged) && (paged != 0)){\n\t\t\t\tt2p->tiff_pages[t2p->tiff_pagecount].page_number =\n\t\t\t\t\tpaged;\n\t\t\t} else {\n\t\t\t\tt2p->tiff_pages[t2p->tiff_pagecount].page_number =\n\t\t\t\t\tpagen;\n\t\t\t}\n\t\t\tgoto ispage2;\n\t\t}\n\t\tif(TIFFGetField(input, TIFFTAG_SUBFILETYPE, &subfiletype)){\n\t\t\tif ( ((subfiletype & FILETYPE_PAGE) != 0)\n || (subfiletype == 0)){\n\t\t\t\tgoto ispage;\n\t\t\t} else {\n\t\t\t\tgoto isnotpage;\n\t\t\t}\n\t\t}\n\t\tif(TIFFGetField(input, TIFFTAG_OSUBFILETYPE, &subfiletype)){\n\t\t\tif ((subfiletype == OFILETYPE_IMAGE)\n\t\t\t\t|| (subfiletype == OFILETYPE_PAGE)\n\t\t\t\t|| (subfiletype == 0) ){\n\t\t\t\tgoto ispage;\n\t\t\t} else {\n\t\t\t\tgoto isnotpage;\n\t\t\t}\n\t\t}\n\t\tispage:\n\t\tt2p->tiff_pages[t2p->tiff_pagecount].page_number=t2p->tiff_pagecount;\n\t\tispage2:\n\t\tt2p->tiff_pages[t2p->tiff_pagecount].page_directory=i;\n\t\tif(TIFFIsTiled(input)){\n\t\t\tt2p->tiff_pages[t2p->tiff_pagecount].page_tilecount =\n\t\t\t\tTIFFNumberOfTiles(input);\n\t\t}\n\t\tt2p->tiff_pagecount++;\n\t\tisnotpage:\n\t\t(void)0;\n\t}\n\tqsort((void*) t2p->tiff_pages, t2p->tiff_pagecount,\n sizeof(T2P_PAGE), t2p_cmp_t2p_page);\n\tfor(i=0;i<t2p->tiff_pagecount;i++){\n\t\tt2p->pdf_xrefcount += 5;\n\t\tTIFFSetDirectory(input, t2p->tiff_pages[i].page_directory );\n\t\tif((TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &xuint16)\n && (xuint16==PHOTOMETRIC_PALETTE))\n\t\t || TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)) {\n\t\t\tt2p->tiff_pages[i].page_extra++;\n\t\t\tt2p->pdf_xrefcount++;\n\t\t}\n#ifdef ZIP_SUPPORT\n\t\tif (TIFFGetField(input, TIFFTAG_COMPRESSION, &xuint16)) {\n if( (xuint16== COMPRESSION_DEFLATE ||\n xuint16== COMPRESSION_ADOBE_DEFLATE) &&\n ((t2p->tiff_pages[i].page_tilecount != 0)\n || TIFFNumberOfStrips(input)==1) &&\n (t2p->pdf_nopassthrough==0)\t){\n if(t2p->pdf_minorversion<2){t2p->pdf_minorversion=2;}\n }\n }\n#endif\n\t\tif (TIFFGetField(input, TIFFTAG_TRANSFERFUNCTION,\n &(t2p->tiff_transferfunction[0]),\n &(t2p->tiff_transferfunction[1]),\n &(t2p->tiff_transferfunction[2]))) {\n\t\t\tif((t2p->tiff_transferfunction[1] != (float*) NULL) &&\n (t2p->tiff_transferfunction[2] != (float*) NULL) &&\n (t2p->tiff_transferfunction[1] !=\n t2p->tiff_transferfunction[0])) {\n\t\t\t\tt2p->tiff_transferfunctioncount = 3;\n\t\t\t\tt2p->tiff_pages[i].page_extra += 4;\n\t\t\t\tt2p->pdf_xrefcount += 4;\n\t\t\t} else {\n\t\t\t\tt2p->tiff_transferfunctioncount = 1;\n\t\t\t\tt2p->tiff_pages[i].page_extra += 2;\n\t\t\t\tt2p->pdf_xrefcount += 2;\n\t\t\t}\n\t\t\tif(t2p->pdf_minorversion < 2)\n\t\t\t\tt2p->pdf_minorversion = 2;\n } else {\n\t\t\tt2p->tiff_transferfunctioncount=0;\n\t\t}\n\t\tif( TIFFGetField(\n\t\t\tinput,\n\t\t\tTIFFTAG_ICCPROFILE,\n\t\t\t&(t2p->tiff_iccprofilelength),\n\t\t\t&(t2p->tiff_iccprofile)) != 0){\n\t\t\tt2p->tiff_pages[i].page_extra++;\n\t\t\tt2p->pdf_xrefcount++;\n\t\t\tif(t2p->pdf_minorversion<3){t2p->pdf_minorversion=3;}\n\t\t}\n\t\tt2p->tiff_tiles[i].tiles_tilecount=\n\t\t\tt2p->tiff_pages[i].page_tilecount;\n\t\tif( (TIFFGetField(input, TIFFTAG_PLANARCONFIG, &xuint16) != 0)\n\t\t\t&& (xuint16 == PLANARCONFIG_SEPARATE ) ){\n\t\t\t\tif( !TIFFGetField(input, TIFFTAG_SAMPLESPERPIXEL, &xuint16) )\n\t\t\t\t{\n\t\t\t\t\tTIFFError(\n TIFF2PDF_MODULE,\n "Missing SamplesPerPixel, %s",\n TIFFFileName(input));\n t2p->t2p_error = T2P_ERR_ERROR;\n return;\n\t\t\t\t}\n if( (t2p->tiff_tiles[i].tiles_tilecount % xuint16) != 0 )\n {\n TIFFError(\n TIFF2PDF_MODULE,\n "Invalid tile count, %s",\n TIFFFileName(input));\n t2p->t2p_error = T2P_ERR_ERROR;\n return;\n }\n\t\t\t\tt2p->tiff_tiles[i].tiles_tilecount/= xuint16;\n\t\t}\n\t\tif( t2p->tiff_tiles[i].tiles_tilecount > 0){\n\t\t\tt2p->pdf_xrefcount +=\n\t\t\t\t(t2p->tiff_tiles[i].tiles_tilecount -1)*2;\n\t\t\tTIFFGetField(input,\n\t\t\t\tTIFFTAG_TILEWIDTH,\n\t\t\t\t&( t2p->tiff_tiles[i].tiles_tilewidth) );\n\t\t\tTIFFGetField(input,\n\t\t\t\tTIFFTAG_TILELENGTH,\n\t\t\t\t&( t2p->tiff_tiles[i].tiles_tilelength) );\n\t\t\tt2p->tiff_tiles[i].tiles_tiles =\n\t\t\t(T2P_TILE*) _TIFFmalloc(TIFFSafeMultiply(tmsize_t,t2p->tiff_tiles[i].tiles_tilecount,\n sizeof(T2P_TILE)) );\n\t\t\tif( t2p->tiff_tiles[i].tiles_tiles == NULL){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory for t2p_read_tiff_init, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_tiles[i].tiles_tilecount * sizeof(T2P_TILE),\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\treturn;\n}', '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\tif( t2p->tiff_bitspersample != 8 )\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t TIFFError(\n\t\t\t\t\t\t\t\t TIFF2PDF_MODULE,\n\t\t\t\t\t\t\t\t "No support for BitsPerSample=%d for RGBA",\n\t\t\t\t\t\t\t\t t2p->tiff_bitspersample);\n\t\t\t\t\t\t\t t2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\t\t return;\n\t\t\t\t\t\t\t}\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\tif( t2p->tiff_bitspersample != 8 )\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t TIFFError(\n\t\t\t\t\t\t\t\t TIFF2PDF_MODULE,\n\t\t\t\t\t\t\t\t "No support for BitsPerSample=%d for RGBA",\n\t\t\t\t\t\t\t\t t2p->tiff_bitspersample);\n\t\t\t\t\t\t\t t2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\t\t return;\n\t\t\t\t\t\t\t}\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(TIFFSafeMultiply(tmsize_t,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(TIFFSafeMultiply(tmsize_t,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 if( t2p->tiff_samplesperpixel != 3){\n TIFFError(\n TIFF2PDF_MODULE,\n "Unsupported samplesperpixel = %d for CIELAB",\n t2p->tiff_samplesperpixel);\n t2p->t2p_error = T2P_ERR_ERROR;\n return;\n }\n if( t2p->tiff_bitspersample != 8){\n TIFFError(\n TIFF2PDF_MODULE,\n "Invalid bitspersample = %d for CIELAB",\n t2p->tiff_bitspersample);\n t2p->t2p_error = T2P_ERR_ERROR;\n return;\n }\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 if( t2p->tiff_samplesperpixel != 3){\n TIFFError(\n TIFF2PDF_MODULE,\n "Unsupported samplesperpixel = %d for ITULAB",\n t2p->tiff_samplesperpixel);\n t2p->t2p_error = T2P_ERR_ERROR;\n return;\n }\n if( t2p->tiff_bitspersample != 8){\n TIFFError(\n TIFF2PDF_MODULE,\n "Invalid bitspersample = %d for ITULAB",\n t2p->tiff_bitspersample);\n t2p->t2p_error = T2P_ERR_ERROR;\n return;\n }\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 if( t2p->t2p_error == T2P_ERR_ERROR )\n\t return;\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] != (float*) NULL) &&\n (t2p->tiff_transferfunction[2] != (float*) NULL) &&\n (t2p->tiff_transferfunction[1] !=\n 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_tile(T2P* t2p, TIFF* input, ttile_t tile){\n\tuint64* tbc = NULL;\n\tuint16 edge=0;\n#ifdef JPEG_SUPPORT\n\tunsigned char* jpt;\n#endif\n uint64 k;\n\tedge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile);\n\tedge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile);\n\tif(t2p->pdf_transcode==T2P_TRANSCODE_RAW){\n\t\tif(edge\n#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT)\n\t\t&& !(t2p->pdf_compression==T2P_COMPRESS_JPEG)\n#endif\n\t\t){\n\t\t\tt2p->tiff_datasize=TIFFTileSize(input);\n\t\t\tif (t2p->tiff_datasize == 0) {\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t}\n\t\t\treturn;\n\t\t} else {\n\t\t\tTIFFGetField(input, TIFFTAG_TILEBYTECOUNTS, &tbc);\n\t\t\tk=tbc[tile];\n#ifdef OJPEG_SUPPORT\n\t\t\tif(t2p->tiff_compression==COMPRESSION_OJPEG){\n\t\t\t \tk = checkAdd64(k, 2048, t2p);\n\t\t\t}\n#endif\n#ifdef JPEG_SUPPORT\n\t\t\tif(t2p->tiff_compression==COMPRESSION_JPEG) {\n\t\t\t\tuint32 count = 0;\n\t\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt)!=0){\n\t\t\t\t\tif(count > 4){\n\t\t\t\t\t\tk = checkAdd64(k, count, t2p);\n\t\t\t\t\t\tk -= 2;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n#endif\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\t}\n\tk = TIFFTileSize(input);\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}', 'tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_t tile){\n\tuint16 edge=0;\n\ttsize_t written=0;\n\tunsigned char* buffer=NULL;\n\ttsize_t bufferoffset=0;\n\tunsigned char* samplebuffer=NULL;\n\ttsize_t samplebufferoffset=0;\n\ttsize_t read=0;\n\tuint16 i=0;\n\tttile_t tilecount=0;\n\tttile_t septilecount=0;\n\ttsize_t septilesize=0;\n#ifdef JPEG_SUPPORT\n\tunsigned char* jpt;\n\tfloat* xfloatp;\n\tuint32 xuint32=0;\n#endif\n\tif (t2p->t2p_error != T2P_ERR_OK)\n\t\treturn(0);\n\tedge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile);\n\tedge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile);\n\tif( (t2p->pdf_transcode == T2P_TRANSCODE_RAW) && ((edge == 0)\n#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT)\n\t\t|| (t2p->pdf_compression == T2P_COMPRESS_JPEG)\n#endif\n\t)\n\t){\n#ifdef CCITT_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_G4){\n\t\t\tbuffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory "\n "for t2p_readwrite_pdf_image_tile, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tTIFFReadRawTile(input, tile, (tdata_t) buffer, t2p->tiff_datasize);\n\t\t\tif (t2p->tiff_fillorder==FILLORDER_LSB2MSB){\n\t\t\t\t\tTIFFReverseBits(buffer, t2p->tiff_datasize);\n\t\t\t}\n\t\t\tt2pWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(t2p->tiff_datasize);\n\t\t}\n#endif\n#ifdef ZIP_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_ZIP){\n\t\t\tbuffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory "\n "for t2p_readwrite_pdf_image_tile, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tTIFFReadRawTile(input, tile, (tdata_t) buffer, t2p->tiff_datasize);\n\t\t\tif (t2p->tiff_fillorder==FILLORDER_LSB2MSB){\n\t\t\t\t\tTIFFReverseBits(buffer, t2p->tiff_datasize);\n\t\t\t}\n\t\t\tt2pWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(t2p->tiff_datasize);\n\t\t}\n#endif\n#ifdef OJPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_OJPEG){\n\t\t\tif(! t2p->pdf_ojpegdata){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"No support for OJPEG image %s with "\n "bad tables",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tbuffer=(unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory "\n "for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\t_TIFFmemcpy(buffer, t2p->pdf_ojpegdata, t2p->pdf_ojpegdatalength);\n\t\t\tif(edge!=0){\n\t\t\t\tif(t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile)){\n\t\t\t\t\tbuffer[7]=\n\t\t\t\t\t\t(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength >> 8) & 0xff;\n\t\t\t\t\tbuffer[8]=\n\t\t\t\t\t\t(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength ) & 0xff;\n\t\t\t\t}\n\t\t\t\tif(t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile)){\n\t\t\t\t\tbuffer[9]=\n\t\t\t\t\t\t(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth >> 8) & 0xff;\n\t\t\t\t\tbuffer[10]=\n\t\t\t\t\t\t(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth ) & 0xff;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbufferoffset=t2p->pdf_ojpegdatalength;\n\t\t\tbufferoffset+=TIFFReadRawTile(input,\n\t\t\t\t\ttile,\n\t\t\t\t\t(tdata_t) &(((unsigned char*)buffer)[bufferoffset]),\n\t\t\t\t\t-1);\n\t\t\t((unsigned char*)buffer)[bufferoffset++]=0xff;\n\t\t\t((unsigned char*)buffer)[bufferoffset++]=0xd9;\n\t\t\tt2pWriteFile(output, (tdata_t) buffer, bufferoffset);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(bufferoffset);\n\t\t}\n#endif\n#ifdef JPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_JPEG){\n\t\t\tunsigned char table_end[2];\n\t\t\tuint32 count = 0;\n\t\t\tbuffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory "\n "for t2p_readwrite_pdf_image_tile, %s",\n\t\t\t\t\tt2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) {\n\t\t\t\tif (count > 0) {\n\t\t\t\t\t_TIFFmemcpy(buffer, jpt, count);\n\t\t\t\t\tbufferoffset += count - 2;\n\t\t\t\t\ttable_end[0] = buffer[bufferoffset-2];\n\t\t\t\t\ttable_end[1] = buffer[bufferoffset-1];\n\t\t\t\t}\n\t\t\t\tif (count > 0) {\n\t\t\t\t\txuint32 = bufferoffset;\n\t\t\t\t\tbufferoffset += TIFFReadRawTile(\n\t\t\t\t\t\tinput,\n\t\t\t\t\t\ttile,\n\t\t\t\t\t\t(tdata_t) &(((unsigned char*)buffer)[bufferoffset-2]),\n\t\t\t\t\t\t-1);\n\t\t\t\t\t\tbuffer[xuint32-2]=table_end[0];\n\t\t\t\t\t\tbuffer[xuint32-1]=table_end[1];\n\t\t\t\t} else {\n\t\t\t\t\tbufferoffset += TIFFReadRawTile(\n\t\t\t\t\t\tinput,\n\t\t\t\t\t\ttile,\n\t\t\t\t\t\t(tdata_t) &(((unsigned char*)buffer)[bufferoffset]),\n\t\t\t\t\t\t-1);\n\t\t\t\t}\n\t\t\t}\n\t\t\tt2pWriteFile(output, (tdata_t) buffer, bufferoffset);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(bufferoffset);\n\t\t}\n#endif\n\t\t(void)0;\n\t}\n\tif(t2p->pdf_sample==T2P_SAMPLE_NOTHING){\n\t\tbuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\tif(buffer==NULL){\n\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t"Can\'t allocate %lu bytes of memory for "\n "t2p_readwrite_pdf_image_tile, %s",\n\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\tTIFFFileName(input));\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn(0);\n\t\t}\n\t\tread = TIFFReadEncodedTile(\n\t\t\tinput,\n\t\t\ttile,\n\t\t\t(tdata_t) &buffer[bufferoffset],\n\t\t\tt2p->tiff_datasize);\n\t\tif(read==-1){\n\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t"Error on decoding tile %u of %s",\n\t\t\t\ttile,\n\t\t\t\tTIFFFileName(input));\n\t\t\t_TIFFfree(buffer);\n\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\treturn(0);\n\t\t}\n\t} else {\n\t\tif(t2p->pdf_sample == T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG){\n\t\t\tseptilesize=TIFFTileSize(input);\n\t\t\tseptilecount=TIFFNumberOfTiles(input);\n\t\t\ttilecount=septilecount/t2p->tiff_samplesperpixel;\n\t\t\tbuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory "\n "for t2p_readwrite_pdf_image_tile, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tsamplebuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(samplebuffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory "\n "for t2p_readwrite_pdf_image_tile, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tsamplebufferoffset=0;\n\t\t\tfor(i=0;i<t2p->tiff_samplesperpixel;i++){\n\t\t\t\tread =\n\t\t\t\t\tTIFFReadEncodedTile(input,\n\t\t\t\t\t\ttile + i*tilecount,\n\t\t\t\t\t\t(tdata_t) &(samplebuffer[samplebufferoffset]),\n\t\t\t\t\t\tseptilesize);\n\t\t\t\tif(read==-1){\n\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t\t"Error on decoding tile %u of %s",\n\t\t\t\t\t\ttile + i*tilecount,\n\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t_TIFFfree(samplebuffer);\n\t\t\t\t\t\t_TIFFfree(buffer);\n\t\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\t\t\treturn(0);\n\t\t\t\t}\n\t\t\t\tsamplebufferoffset+=read;\n\t\t\t}\n\t\t\tt2p_sample_planar_separate_to_contig(\n\t\t\t\tt2p,\n\t\t\t\t&(buffer[bufferoffset]),\n\t\t\t\tsamplebuffer,\n\t\t\t\tsamplebufferoffset);\n\t\t\tbufferoffset+=samplebufferoffset;\n\t\t\t_TIFFfree(samplebuffer);\n\t\t}\n\t\tif(buffer==NULL){\n\t\t\tbuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory "\n "for t2p_readwrite_pdf_image_tile, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tread = TIFFReadEncodedTile(\n\t\t\t\tinput,\n\t\t\t\ttile,\n\t\t\t\t(tdata_t) &buffer[bufferoffset],\n\t\t\t\tt2p->tiff_datasize);\n\t\t\tif(read==-1){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Error on decoding tile %u of %s",\n\t\t\t\t\ttile,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_RGBA_TO_RGB){\n\t\t\tt2p->tiff_datasize=t2p_sample_rgba_to_rgb(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth\n\t\t\t\t*t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_RGBAA_TO_RGB){\n\t\t\tt2p->tiff_datasize=t2p_sample_rgbaa_to_rgb(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth\n\t\t\t\t*t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_YCBCR_TO_RGB){\n\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t"No support for YCbCr to RGB in tile for %s",\n\t\t\t\tTIFFFileName(input));\n\t\t\t_TIFFfree(buffer);\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn(0);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED){\n\t\t\tt2p->tiff_datasize=t2p_sample_lab_signed_to_unsigned(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth\n\t\t\t\t*t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength);\n\t\t}\n\t}\n\tif(t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile) != 0){\n\t\tt2p_tile_collapse_left(\n\t\t\tbuffer,\n\t\t\tTIFFTileRowSize(input),\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilelength);\n\t}\n\tt2p_disable(output);\n\tTIFFSetField(output, TIFFTAG_PHOTOMETRIC, t2p->tiff_photometric);\n\tTIFFSetField(output, TIFFTAG_BITSPERSAMPLE, t2p->tiff_bitspersample);\n\tTIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, t2p->tiff_samplesperpixel);\n\tif(t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile) == 0){\n\t\tTIFFSetField(\n\t\t\toutput,\n\t\t\tTIFFTAG_IMAGEWIDTH,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth);\n\t} else {\n\t\tTIFFSetField(\n\t\t\toutput,\n\t\t\tTIFFTAG_IMAGEWIDTH,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth);\n\t}\n\tif(t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile) == 0){\n\t\tTIFFSetField(\n\t\t\toutput,\n\t\t\tTIFFTAG_IMAGELENGTH,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilelength);\n\t\tTIFFSetField(\n\t\t\toutput,\n\t\t\tTIFFTAG_ROWSPERSTRIP,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilelength);\n\t} else {\n\t\tTIFFSetField(\n\t\t\toutput,\n\t\t\tTIFFTAG_IMAGELENGTH,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength);\n\t\tTIFFSetField(\n\t\t\toutput,\n\t\t\tTIFFTAG_ROWSPERSTRIP,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength);\n\t}\n\tTIFFSetField(output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);\n\tTIFFSetField(output, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);\n\tswitch(t2p->pdf_compression){\n\tcase T2P_COMPRESS_NONE:\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_NONE);\n\t\tbreak;\n#ifdef CCITT_SUPPORT\n\tcase T2P_COMPRESS_G4:\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);\n\t\tbreak;\n#endif\n#ifdef JPEG_SUPPORT\n\tcase T2P_COMPRESS_JPEG:\n\t\tif (t2p->tiff_photometric==PHOTOMETRIC_YCBCR) {\n\t\t\tuint16 hor = 0, ver = 0;\n\t\t\tif (TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &hor, &ver)!=0) {\n\t\t\t\tif (hor != 0 && ver != 0) {\n\t\t\t\t\tTIFFSetField(output, TIFFTAG_YCBCRSUBSAMPLING, hor, ver);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(TIFFGetField(input, TIFFTAG_REFERENCEBLACKWHITE, &xfloatp)!=0){\n\t\t\t\tTIFFSetField(output, TIFFTAG_REFERENCEBLACKWHITE, xfloatp);\n\t\t\t}\n\t\t}\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_JPEG);\n\t\tTIFFSetField(output, TIFFTAG_JPEGTABLESMODE, 0);\n\t\tif(t2p->pdf_colorspace & (T2P_CS_RGB | T2P_CS_LAB)){\n\t\t\tTIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);\n\t\t\tif(t2p->tiff_photometric != PHOTOMETRIC_YCBCR){\n\t\t\t\tTIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);\n\t\t\t} else {\n\t\t\t\tTIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RAW);\n\t\t\t}\n\t\t}\n\t\tif(t2p->pdf_colorspace & T2P_CS_GRAY){\n\t\t\t(void)0;\n\t\t}\n\t\tif(t2p->pdf_colorspace & T2P_CS_CMYK){\n\t\t\t(void)0;\n\t\t}\n\t\tif(t2p->pdf_defaultcompressionquality != 0){\n\t\t\tTIFFSetField(output,\n\t\t\t\tTIFFTAG_JPEGQUALITY,\n\t\t\t\tt2p->pdf_defaultcompressionquality);\n\t\t}\n\t\tbreak;\n#endif\n#ifdef ZIP_SUPPORT\n\tcase T2P_COMPRESS_ZIP:\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);\n\t\tif(t2p->pdf_defaultcompressionquality%100 != 0){\n\t\t\tTIFFSetField(output,\n\t\t\t\tTIFFTAG_PREDICTOR,\n\t\t\t\tt2p->pdf_defaultcompressionquality % 100);\n\t\t}\n\t\tif(t2p->pdf_defaultcompressionquality/100 != 0){\n\t\t\tTIFFSetField(output,\n\t\t\t\tTIFFTAG_ZIPQUALITY,\n\t\t\t\t(t2p->pdf_defaultcompressionquality / 100));\n\t\t}\n\t\tbreak;\n#endif\n\tdefault:\n\t\tbreak;\n\t}\n\tt2p_enable(output);\n\tt2p->outputwritten = 0;\n\tbufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t) 0, buffer,\n\t\t\t\t\t TIFFStripSize(output));\n\tif (buffer != NULL) {\n\t\t_TIFFfree(buffer);\n\t\tbuffer = NULL;\n\t}\n\tif (bufferoffset == -1) {\n\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t "Error writing encoded tile to output PDF %s",\n\t\t\t TIFFFileName(output));\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn(0);\n\t}\n\twritten = t2p->outputwritten;\n\treturn(written);\n}', 'tmsize_t\nTIFFReadEncodedTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)\n{\n\tstatic const char module[] = "TIFFReadEncodedTile";\n\tTIFFDirectory *td = &tif->tif_dir;\n\ttmsize_t tilesize = tif->tif_tilesize;\n\tif (!TIFFCheckRead(tif, 1))\n\t\treturn ((tmsize_t)(-1));\n\tif (tile >= td->td_nstrips) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "%lu: Tile 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 (size == (tmsize_t)(-1))\n\t\tsize = tilesize;\n\telse if (size > tilesize)\n\t\tsize = tilesize;\n\tif (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif,\n\t (uint8*) buf, size, (uint16)(tile/td->td_stripsperimage))) {\n\t\t(*tif->tif_postdecode)(tif, (uint8*) buf, size);\n\t\treturn (size);\n\t} else\n\t\treturn ((tmsize_t)(-1));\n}', 'int\nTIFFFillTile(TIFF* tif, uint32 tile)\n{\n\tstatic const char module[] = "TIFFFillTile";\n\tTIFFDirectory *td = &tif->tif_dir;\n if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)\n return 0;\n\tif ((tif->tif_flags&TIFF_NOREADRAW)==0)\n\t{\n\t\tuint64 bytecount = td->td_stripbytecount[tile];\n\t\tif ((int64)bytecount <= 0) {\n#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t"%I64u: Invalid tile byte count, tile %lu",\n\t\t\t\t (unsigned __int64) bytecount,\n\t\t\t\t (unsigned long) tile);\n#else\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t"%llu: Invalid tile byte count, tile %lu",\n\t\t\t\t (unsigned long long) bytecount,\n\t\t\t\t (unsigned long) tile);\n#endif\n\t\t\treturn (0);\n\t\t}\n\t\tif (isMapped(tif) &&\n\t\t (isFillOrder(tif, td->td_fillorder)\n\t\t || (tif->tif_flags & TIFF_NOBITREV))) {\n\t\t\tif ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {\n\t\t\t\t_TIFFfree(tif->tif_rawdata);\n\t\t\t\ttif->tif_rawdata = NULL;\n\t\t\t\ttif->tif_rawdatasize = 0;\n\t\t\t}\n\t\t\ttif->tif_flags &= ~TIFF_MYBUFFER;\n\t\t\tif (bytecount > (uint64)tif->tif_size ||\n\t\t\t td->td_stripoffset[tile] > (uint64)tif->tif_size - bytecount) {\n\t\t\t\ttif->tif_curtile = NOTILE;\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t\ttif->tif_rawdatasize = (tmsize_t)bytecount;\n\t\t\ttif->tif_rawdata =\n\t\t\t\ttif->tif_base + (tmsize_t)td->td_stripoffset[tile];\n tif->tif_rawdataoff = 0;\n tif->tif_rawdataloaded = (tmsize_t) bytecount;\n\t\t\ttif->tif_flags |= TIFF_BUFFERMMAP;\n\t\t} else {\n\t\t\ttmsize_t bytecountm;\n\t\t\tbytecountm=(tmsize_t)bytecount;\n\t\t\tif ((uint64)bytecountm!=bytecount)\n\t\t\t{\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tif (bytecountm > tif->tif_rawdatasize) {\n\t\t\t\ttif->tif_curtile = NOTILE;\n\t\t\t\tif ((tif->tif_flags & TIFF_MYBUFFER) == 0) {\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Data buffer too small to hold tile %lu",\n\t\t\t\t\t (unsigned long) tile);\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tif (!TIFFReadBufferSetup(tif, 0, bytecountm))\n\t\t\t\t\treturn (0);\n\t\t\t}\n\t\t\tif (tif->tif_flags&TIFF_BUFFERMMAP) {\n\t\t\t\ttif->tif_curtile = NOTILE;\n\t\t\t\tif (!TIFFReadBufferSetup(tif, 0, bytecountm))\n\t\t\t\t\treturn (0);\n\t\t\t}\n\t\t\tif (TIFFReadRawTile1(tif, tile, tif->tif_rawdata,\n\t\t\t bytecountm, module) != bytecountm)\n\t\t\t\treturn (0);\n tif->tif_rawdataoff = 0;\n tif->tif_rawdataloaded = bytecountm;\n\t\t\tif (!isFillOrder(tif, td->td_fillorder) &&\n\t\t\t (tif->tif_flags & TIFF_NOBITREV) == 0)\n\t\t\t\tTIFFReverseBits(tif->tif_rawdata,\n tif->tif_rawdataloaded);\n\t\t}\n\t}\n\treturn (TIFFStartTile(tif, tile));\n}', 'static int\nTIFFStartTile(TIFF* tif, uint32 tile)\n{\n static const char module[] = "TIFFStartTile";\n\tTIFFDirectory *td = &tif->tif_dir;\n uint32 howmany32;\n if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)\n return 0;\n\tif ((tif->tif_flags & TIFF_CODERSETUP) == 0) {\n\t\tif (!(*tif->tif_setupdecode)(tif))\n\t\t\treturn (0);\n\t\ttif->tif_flags |= TIFF_CODERSETUP;\n\t}\n\ttif->tif_curtile = tile;\n howmany32=TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);\n if (howmany32 == 0) {\n TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");\n return 0;\n }\n\ttif->tif_row = (tile % howmany32) * td->td_tilelength;\n howmany32=TIFFhowmany_32(td->td_imagelength, td->td_tilelength);\n if (howmany32 == 0) {\n TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");\n return 0;\n }\n\ttif->tif_col = (tile % howmany32) * td->td_tilewidth;\n tif->tif_flags &= ~TIFF_BUF4WRITE;\n\tif (tif->tif_flags&TIFF_NOREADRAW)\n\t{\n\t\ttif->tif_rawcp = NULL;\n\t\ttif->tif_rawcc = 0;\n\t}\n\telse\n\t{\n\t\ttif->tif_rawcp = tif->tif_rawdata;\n\t\ttif->tif_rawcc = (tmsize_t)td->td_stripbytecount[tile];\n\t}\n\treturn ((*tif->tif_predecode)(tif,\n\t\t\t(uint16)(tile/td->td_stripsperimage)));\n}']
|
1,205 | 0 |
https://github.com/openssl/openssl/blob/02cba628daa7fea959c561531a8a984756bdf41c/crypto/bn/bn_lib.c/#L289
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *A, *a = NULL;
const BN_ULONG *B;
int i;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = A = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = A = OPENSSL_zalloc(words * sizeof(*a));
if (A == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
#if 1
B = b->d;
if (B != NULL) {
for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
BN_ULONG a0, a1, a2, a3;
a0 = B[0];
a1 = B[1];
a2 = B[2];
a3 = B[3];
A[0] = a0;
A[1] = a1;
A[2] = a2;
A[3] = a3;
}
switch (b->top & 3) {
case 3:
A[2] = B[2];
case 2:
A[1] = B[1];
case 1:
A[0] = B[0];
case 0:
;
}
}
#else
memset(A, 0, sizeof(*A) * words);
memcpy(A, b->d, sizeof(b->d[0]) * b->top);
#endif
return (a);
}
|
["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}", 'BIGNUM *BN_dup(const BIGNUM *a)\n{\n BIGNUM *t;\n if (a == NULL)\n return NULL;\n bn_check_top(a);\n t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();\n if (t == NULL)\n return NULL;\n if (!BN_copy(t, a)) {\n BN_free(t);\n return NULL;\n }\n bn_check_top(t);\n return t;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n int i;\n BN_ULONG *A;\n const BN_ULONG *B;\n bn_check_top(b);\n if (a == b)\n return (a);\n if (bn_wexpand(a, b->top) == NULL)\n return (NULL);\n#if 1\n A = a->d;\n B = b->d;\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:;\n }\n#else\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n#endif\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return (a);\n}', 'BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)\n{\n BN_ULONG ret = 0;\n int i, j;\n bn_check_top(a);\n w &= BN_MASK2;\n if (!w)\n return (BN_ULONG)-1;\n if (a->top == 0)\n return 0;\n j = BN_BITS2 - BN_num_bits_word(w);\n w <<= j;\n if (!BN_lshift(a, a, j))\n return (BN_ULONG)-1;\n for (i = a->top - 1; i >= 0; i--) {\n BN_ULONG l, d;\n l = a->d[i];\n d = bn_div_words(ret, l, w);\n ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;\n a->d[i] = d;\n }\n if ((a->top > 0) && (a->d[a->top - 1] == 0))\n a->top--;\n ret >>= j;\n if (!a->top)\n a->neg = 0;\n bn_check_top(a);\n return (ret);\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}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = A = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = A = OPENSSL_zalloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
|
1,206 | 0 |
https://github.com/openssl/openssl/blob/54d00677f305375eee65a0c9edb5f0980c5f020f/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;
}
}
|
['int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!bn_sqr_fixed_top(tmp, a, ctx))\n goto err;\n } else {\n if (!bn_mul_fixed_top(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!bn_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n 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_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', '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_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,\n int dna, int dnb, BN_ULONG *t)\n{\n int n = n2 / 2, c1, c2;\n int tna = n + dna, tnb = n + dnb;\n unsigned int neg, zero;\n BN_ULONG ln, lo, *p;\n# ifdef BN_MUL_COMBA\n# if 0\n if (n2 == 4) {\n bn_mul_comba4(r, a, b);\n return;\n }\n# endif\n if (n2 == 8 && dna == 0 && dnb == 0) {\n bn_mul_comba8(r, a, b);\n return;\n }\n# endif\n if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {\n bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);\n if ((dna + dnb) < 0)\n memset(&r[2 * n2 + dna + dnb], 0,\n sizeof(BN_ULONG) * -(dna + dnb));\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 zero = 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 zero = 1;\n break;\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 zero = 1;\n break;\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 zero = 1;\n break;\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# ifdef BN_MUL_COMBA\n if (n == 4 && dna == 0 && dnb == 0) {\n if (!zero)\n bn_mul_comba4(&(t[n2]), t, &(t[n]));\n else\n memset(&t[n2], 0, sizeof(*t) * 8);\n bn_mul_comba4(r, a, b);\n bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));\n } else if (n == 8 && dna == 0 && dnb == 0) {\n if (!zero)\n bn_mul_comba8(&(t[n2]), t, &(t[n]));\n else\n memset(&t[n2], 0, sizeof(*t) * 16);\n bn_mul_comba8(r, a, b);\n bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));\n } else\n# endif\n {\n p = &(t[n2 * 2]);\n if (!zero)\n bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);\n else\n memset(&t[n2], 0, sizeof(*t) * n2);\n bn_mul_recursive(r, a, b, n, 0, 0, p);\n bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);\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}']
|
1,207 | 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;
}
|
['static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,\n const BIGNUM *rem, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *t1, *qadd, *q;\n bits--;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n qadd = BN_CTX_get(ctx);\n if (qadd == NULL)\n goto err;\n if (!BN_rshift1(qadd, padd))\n goto err;\n if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))\n goto err;\n if (!BN_mod(t1, q, qadd, ctx))\n goto err;\n if (!BN_sub(q, q, t1))\n goto err;\n if (rem == NULL) {\n if (!BN_add_word(q, 1))\n goto err;\n } else {\n if (!BN_rshift1(t1, rem))\n goto err;\n if (!BN_add(q, q, t1))\n goto err;\n }\n if (!BN_lshift1(p, q))\n goto err;\n if (!BN_add_word(p, 1))\n goto err;\n loop:\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);\n BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);\n if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)\n goto err;\n if (pmod == 0 || qmod == 0) {\n if (!BN_add(p, p, padd))\n goto err;\n if (!BN_add(q, q, qadd))\n goto err;\n goto loop;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(p);\n return (ret);\n}', '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_rshift1(BIGNUM *r, const BIGNUM *a)\n{\n BN_ULONG *ap, *rp, t, c;\n int i, j;\n bn_check_top(r);\n bn_check_top(a);\n if (BN_is_zero(a)) {\n BN_zero(r);\n return (1);\n }\n i = a->top;\n ap = a->d;\n j = i - (ap[i - 1] == 1);\n if (a != r) {\n if (bn_wexpand(r, j) == NULL)\n return (0);\n r->neg = a->neg;\n }\n rp = r->d;\n t = ap[--i];\n c = (t & 1) ? BN_TBIT : 0;\n if (t >>= 1)\n rp[i] = t;\n while (i > 0) {\n t = ap[--i];\n rp[i] = ((t >> 1) & BN_MASK2) | c;\n c = (t & 1) ? BN_TBIT : 0;\n }\n r->top = j;\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return (1);\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
|
1,208 | 0 |
https://github.com/openssl/openssl/blob/6fda11ae5a06e28fd9463e5afb60735d074904b3/crypto/evp/evp_enc.c/#L475
|
int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
{
PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
(diff > (0 - (PTRDIFF_T)len)));
return overlapped;
}
|
['static int enc_read(BIO *b, char *out, int outl)\n{\n int ret = 0, i, blocksize;\n BIO_ENC_CTX *ctx;\n BIO *next;\n if (out == NULL)\n return 0;\n ctx = BIO_get_data(b);\n next = BIO_next(b);\n if ((ctx == NULL) || (next == NULL))\n return 0;\n if (ctx->buf_len > 0) {\n i = ctx->buf_len - ctx->buf_off;\n if (i > outl)\n i = outl;\n memcpy(out, &(ctx->buf[ctx->buf_off]), i);\n ret = i;\n out += i;\n outl -= i;\n ctx->buf_off += i;\n if (ctx->buf_len == ctx->buf_off) {\n ctx->buf_len = 0;\n ctx->buf_off = 0;\n }\n }\n blocksize = EVP_CIPHER_CTX_block_size(ctx->cipher);\n if (blocksize == 1)\n blocksize = 0;\n while (outl > 0) {\n if (ctx->cont <= 0)\n break;\n if (ctx->read_start == ctx->read_end) {\n ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);\n i = BIO_read(next, ctx->read_start, ENC_BLOCK_SIZE);\n if (i > 0)\n ctx->read_end += i;\n } else {\n i = ctx->read_end - ctx->read_start;\n }\n if (i <= 0) {\n if (!BIO_should_retry(next)) {\n ctx->cont = i;\n i = EVP_CipherFinal_ex(ctx->cipher,\n ctx->buf, &(ctx->buf_len));\n ctx->ok = i;\n ctx->buf_off = 0;\n } else {\n ret = (ret == 0) ? i : ret;\n break;\n }\n } else {\n if (outl > ENC_MIN_CHUNK) {\n int j = outl - blocksize, buf_len;\n if (!EVP_CipherUpdate(ctx->cipher,\n (unsigned char *)out, &buf_len,\n ctx->read_start, i > j ? j : i)) {\n BIO_clear_retry_flags(b);\n return 0;\n }\n ret += buf_len;\n out += buf_len;\n outl -= buf_len;\n if ((i -= j) <= 0) {\n ctx->read_start = ctx->read_end;\n continue;\n }\n ctx->read_start += j;\n }\n if (i > ENC_MIN_CHUNK)\n i = ENC_MIN_CHUNK;\n if (!EVP_CipherUpdate(ctx->cipher,\n ctx->buf, &ctx->buf_len,\n ctx->read_start, i)) {\n BIO_clear_retry_flags(b);\n ctx->ok = 0;\n return 0;\n }\n ctx->read_start += i;\n ctx->cont = 1;\n if (ctx->buf_len == 0)\n continue;\n }\n if (ctx->buf_len <= outl)\n i = ctx->buf_len;\n else\n i = outl;\n if (i <= 0)\n break;\n memcpy(out, ctx->buf, i);\n ret += i;\n ctx->buf_off = i;\n outl -= i;\n out += i;\n }\n BIO_clear_retry_flags(b);\n BIO_copy_next_retry(b);\n return ((ret == 0) ? ctx->cont : ret);\n}', 'int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n const unsigned char *in, int inl)\n{\n if (ctx->encrypt)\n return EVP_EncryptUpdate(ctx, out, outl, in, inl);\n else\n return EVP_DecryptUpdate(ctx, out, outl, in, inl);\n}', 'int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n const unsigned char *in, int inl)\n{\n int fix_len, cmpl = inl, ret;\n unsigned int b;\n size_t soutl;\n int blocksize;\n if (ctx->encrypt) {\n EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_INVALID_OPERATION);\n return 0;\n }\n if (ctx->cipher == NULL || ctx->cipher->prov == NULL)\n goto legacy;\n blocksize = EVP_CIPHER_CTX_block_size(ctx);\n if (ctx->cipher->cupdate == NULL || blocksize < 1) {\n EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_UPDATE_ERROR);\n return 0;\n }\n ret = ctx->cipher->cupdate(ctx->provctx, out, &soutl,\n inl + (blocksize == 1 ? 0 : blocksize), in,\n (size_t)inl);\n if (ret) {\n if (soutl > INT_MAX) {\n EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_UPDATE_ERROR);\n return 0;\n }\n *outl = soutl;\n }\n return ret;\n legacy:\n b = ctx->cipher->block_size;\n if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))\n cmpl = (cmpl + 7) / 8;\n if (inl <= 0) {\n *outl = 0;\n return inl == 0;\n }\n if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {\n if (b == 1 && is_partially_overlapping(out, in, cmpl)) {\n EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);\n if (fix_len < 0) {\n *outl = 0;\n return 0;\n } else\n *outl = fix_len;\n return 1;\n }\n if (ctx->flags & EVP_CIPH_NO_PADDING)\n return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);\n OPENSSL_assert(b <= sizeof(ctx->final));\n if (ctx->final_used) {\n if (((PTRDIFF_T)out == (PTRDIFF_T)in)\n || is_partially_overlapping(out, in, b)) {\n EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n memcpy(out, ctx->final, b);\n out += b;\n fix_len = 1;\n } else\n fix_len = 0;\n if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))\n return 0;\n if (b > 1 && !ctx->buf_len) {\n *outl -= b;\n ctx->final_used = 1;\n memcpy(ctx->final, &out[*outl], b);\n } else\n ctx->final_used = 0;\n if (fix_len)\n *outl += b;\n return 1;\n}', 'int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)\n{\n PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;\n int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |\n (diff > (0 - (PTRDIFF_T)len)));\n return overlapped;\n}']
|
1,209 | 0 |
https://github.com/libav/libav/blob/870a6f4044f639bbcbdb3f724331199651e777d1/libavformat/rmdec.c/#L804
|
int
ff_rm_retrieve_cache (AVFormatContext *s, ByteIOContext *pb,
AVStream *st, RMStream *ast, AVPacket *pkt)
{
RMDemuxContext *rm = s->priv_data;
assert (rm->audio_pkt_cnt > 0);
if (st->codec->codec_id == CODEC_ID_AAC)
av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
else {
av_new_packet(pkt, st->codec->block_align);
memcpy(pkt->data, ast->pkt.data + st->codec->block_align *
(ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
st->codec->block_align);
}
rm->audio_pkt_cnt--;
if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
ast->audiotimestamp = AV_NOPTS_VALUE;
pkt->flags = AV_PKT_FLAG_KEY;
} else
pkt->flags = 0;
pkt->stream_index = st->index;
return rm->audio_pkt_cnt;
}
|
['int\nff_rm_retrieve_cache (AVFormatContext *s, ByteIOContext *pb,\n AVStream *st, RMStream *ast, AVPacket *pkt)\n{\n RMDemuxContext *rm = s->priv_data;\n assert (rm->audio_pkt_cnt > 0);\n if (st->codec->codec_id == CODEC_ID_AAC)\n av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);\n else {\n av_new_packet(pkt, st->codec->block_align);\n memcpy(pkt->data, ast->pkt.data + st->codec->block_align *\n (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),\n st->codec->block_align);\n }\n rm->audio_pkt_cnt--;\n if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {\n ast->audiotimestamp = AV_NOPTS_VALUE;\n pkt->flags = AV_PKT_FLAG_KEY;\n } else\n pkt->flags = 0;\n pkt->stream_index = st->index;\n return rm->audio_pkt_cnt;\n}', 'int av_new_packet(AVPacket *pkt, int size)\n{\n uint8_t *data= NULL;\n if((unsigned)size < (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)\n data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);\n if (data){\n memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);\n }else\n size=0;\n av_init_packet(pkt);\n pkt->data = data;\n pkt->size = size;\n pkt->destruct = av_destruct_packet;\n if(!data)\n return AVERROR(ENOMEM);\n return 0;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'void av_init_packet(AVPacket *pkt)\n{\n pkt->pts = AV_NOPTS_VALUE;\n pkt->dts = AV_NOPTS_VALUE;\n pkt->pos = -1;\n pkt->duration = 0;\n pkt->convergence_duration = 0;\n pkt->flags = 0;\n pkt->stream_index = 0;\n pkt->destruct= NULL;\n}']
|
1,210 | 0 |
https://github.com/libav/libav/blob/b767b9cd4b1b95b1bcd500b77f7446eb2a06bcba/libavformat/rtsp.c/#L923
|
static int
rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
{
RTSPState *rt = s->priv_data;
AVStream *st = NULL;
if (rtsp_st->stream_index >= 0)
st = s->streams[rtsp_st->stream_index];
if (!st)
s->ctx_flags |= AVFMTCTX_NOHEADER;
if (rt->transport == RTSP_TRANSPORT_RDT)
rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
rtsp_st->dynamic_protocol_context,
rtsp_st->dynamic_handler);
else
rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle,
rtsp_st->sdp_payload_type,
&rtsp_st->rtp_payload_data);
if (!rtsp_st->transport_priv) {
return AVERROR(ENOMEM);
} else if (rt->transport != RTSP_TRANSPORT_RDT) {
if(rtsp_st->dynamic_handler) {
rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
rtsp_st->dynamic_protocol_context,
rtsp_st->dynamic_handler);
}
}
return 0;
}
|
['static int\nrtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)\n{\n RTSPState *rt = s->priv_data;\n AVStream *st = NULL;\n if (rtsp_st->stream_index >= 0)\n st = s->streams[rtsp_st->stream_index];\n if (!st)\n s->ctx_flags |= AVFMTCTX_NOHEADER;\n if (rt->transport == RTSP_TRANSPORT_RDT)\n rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,\n rtsp_st->dynamic_protocol_context,\n rtsp_st->dynamic_handler);\n else\n rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle,\n rtsp_st->sdp_payload_type,\n &rtsp_st->rtp_payload_data);\n if (!rtsp_st->transport_priv) {\n return AVERROR(ENOMEM);\n } else if (rt->transport != RTSP_TRANSPORT_RDT) {\n if(rtsp_st->dynamic_handler) {\n rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,\n rtsp_st->dynamic_protocol_context,\n rtsp_st->dynamic_handler);\n }\n }\n return 0;\n}']
|
1,211 | 0 |
https://github.com/openssl/openssl/blob/5c98b2caf5ce545fbf77611431c7084979da8177/crypto/bn/bn_ctx.c/#L353
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int\tBN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[], BN_CTX *ctx)\n\t{\n\tint ret = 0;\n\tBIGNUM *u;\n\tbn_check_top(a);\n\tif (!p[0])\n\t\t{\n\t\tBN_zero(r);\n\t\treturn 1;\n\t\t}\n\tBN_CTX_start(ctx);\n\tif ((u = BN_CTX_get(ctx)) == NULL) goto err;\n\tif (!BN_set_bit(u, p[0] - 1)) goto err;\n\tret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);\n\tbn_check_top(r);\nerr:\n\tBN_CTX_end(ctx);\n\treturn ret;\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_GET,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
|
1,212 | 0 |
https://github.com/libav/libav/blob/dad7a9c7c0ae8ebc56f2e3a24e6fa4da5c2cd491/libavcodec/bitstream.h/#L139
|
static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
bc->bits >>= n;
#else
uint64_t ret = bc->bits >> (64 - n);
bc->bits <<= n;
#endif
bc->bits_left -= n;
return ret;
}
|
['static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h,\n int stride[3])\n{\n int x, y, p;\n int16_t *sample[4][2];\n int lbd = s->avctx->bits_per_raw_sample <= 8;\n int bits = s->avctx->bits_per_raw_sample > 0\n ? s->avctx->bits_per_raw_sample\n : 8;\n int offset = 1 << bits;\n for (x = 0; x < 4; x++) {\n sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;\n sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;\n }\n s->run_index = 0;\n memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));\n for (y = 0; y < h; y++) {\n for (p = 0; p < 3 + s->transparency; p++) {\n int16_t *temp = sample[p][0];\n sample[p][0] = sample[p][1];\n sample[p][1] = temp;\n sample[p][1][-1] = sample[p][0][0];\n sample[p][0][w] = sample[p][0][w - 1];\n if (lbd)\n decode_line(s, w, sample[p], (p + 1) / 2, 9);\n else\n decode_line(s, w, sample[p], (p + 1) / 2, bits + 1);\n }\n for (x = 0; x < w; x++) {\n int g = sample[0][1][x];\n int b = sample[1][1][x];\n int r = sample[2][1][x];\n int a = sample[3][1][x];\n b -= offset;\n r -= offset;\n g -= (b + r) >> 2;\n b += g;\n r += g;\n if (lbd)\n *((uint32_t *)(src[0] + x * 4 + stride[0] * y)) = b +\n (g << 8) + (r << 16) + (a << 24);\n else {\n *((uint16_t *)(src[0] + x * 2 + stride[0] * y)) = b;\n *((uint16_t *)(src[1] + x * 2 + stride[1] * y)) = g;\n *((uint16_t *)(src[2] + x * 2 + stride[2] * y)) = r;\n }\n }\n }\n}', 'static av_always_inline void decode_line(FFV1Context *s, int w,\n int16_t *sample[2],\n int plane_index, int bits)\n{\n PlaneContext *const p = &s->plane[plane_index];\n RangeCoder *const c = &s->c;\n int x;\n int run_count = 0;\n int run_mode = 0;\n int run_index = s->run_index;\n for (x = 0; x < w; x++) {\n int diff, context, sign;\n context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);\n if (context < 0) {\n context = -context;\n sign = 1;\n } else\n sign = 0;\n av_assert2(context < p->context_count);\n if (s->ac != AC_GOLOMB_RICE) {\n diff = get_symbol_inline(c, p->state[context], 1);\n } else {\n if (context == 0 && run_mode == 0)\n run_mode = 1;\n if (run_mode) {\n if (run_count == 0 && run_mode == 1) {\n if (bitstream_read_bit(&s->bc)) {\n run_count = 1 << ff_log2_run[run_index];\n if (x + run_count <= w)\n run_index++;\n } else {\n if (ff_log2_run[run_index])\n run_count = bitstream_read(&s->bc, ff_log2_run[run_index]);\n else\n run_count = 0;\n if (run_index)\n run_index--;\n run_mode = 2;\n }\n }\n run_count--;\n if (run_count < 0) {\n run_mode = 0;\n run_count = 0;\n diff = get_vlc_symbol(&s->bc, &p->vlc_state[context],\n bits);\n if (diff >= 0)\n diff++;\n } else\n diff = 0;\n } else\n diff = get_vlc_symbol(&s->bc, &p->vlc_state[context], bits);\n ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\\n",\n run_count, run_index, run_mode, x, bitstream_tell(&s->bc));\n }\n if (sign)\n diff = -diff;\n sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &\n ((1 << bits) - 1);\n }\n s->run_index = run_index;\n}', 'static inline int get_vlc_symbol(BitstreamContext *bc, VlcState *const state,\n int bits)\n{\n int k, i, v, ret;\n i = state->count;\n k = 0;\n while (i < state->error_sum) {\n k++;\n i += i;\n }\n assert(k <= 8);\n v = get_sr_golomb(bc, k, 12, bits);\n ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",\n v, state->bias, state->error_sum, state->drift, state->count, k);\n v ^= ((2 * state->drift + state->count) >> 31);\n ret = fold(v + state->bias, bits);\n update_vlc_state(state, v);\n return ret;\n}', 'static inline int get_sr_golomb(BitstreamContext *bc, int k, int limit,\n int esc_len)\n{\n int v = get_ur_golomb(bc, k, limit, esc_len);\n v++;\n if (v & 1)\n return v >> 1;\n else\n return -(v >> 1);\n}', 'static inline int get_ur_golomb(BitstreamContext *bc, int k, int limit,\n int esc_len)\n{\n unsigned int buf;\n int log;\n buf = bitstream_peek(bc, 32);\n log = av_log2(buf);\n if (log > 31 - limit) {\n buf >>= log - k;\n buf += (30 - log) << k;\n bitstream_skip(bc, 32 + k - log);\n return buf;\n } else {\n bitstream_skip(bc, limit);\n buf = bitstream_read(bc, esc_len);\n return buf + limit - 1;\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 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}']
|
1,213 | 0 |
https://github.com/libav/libav/blob/ab3554e1a7c04a5ea30f9c905de92348478ef7c8/libavutil/buffer.c/#L195
|
int av_buffer_realloc(AVBufferRef **pbuf, int size)
{
AVBufferRef *buf = *pbuf;
uint8_t *tmp;
if (!buf) {
uint8_t *data = av_realloc(NULL, size);
if (!data)
return AVERROR(ENOMEM);
buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
if (!buf) {
av_freep(&data);
return AVERROR(ENOMEM);
}
buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;
*pbuf = buf;
return 0;
} else if (buf->size == size)
return 0;
if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||
!av_buffer_is_writable(buf) || buf->data != buf->buffer->data) {
AVBufferRef *new = NULL;
av_buffer_realloc(&new, size);
if (!new)
return AVERROR(ENOMEM);
memcpy(new->data, buf->data, FFMIN(size, buf->size));
av_buffer_unref(pbuf);
*pbuf = new;
return 0;
}
tmp = av_realloc(buf->buffer->data, size);
if (!tmp)
return AVERROR(ENOMEM);
buf->buffer->data = buf->data = tmp;
buf->buffer->size = buf->size = size;
return 0;
}
|
['int av_buffer_realloc(AVBufferRef **pbuf, int size)\n{\n AVBufferRef *buf = *pbuf;\n uint8_t *tmp;\n if (!buf) {\n uint8_t *data = av_realloc(NULL, size);\n if (!data)\n return AVERROR(ENOMEM);\n buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);\n if (!buf) {\n av_freep(&data);\n return AVERROR(ENOMEM);\n }\n buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;\n *pbuf = buf;\n return 0;\n } else if (buf->size == size)\n return 0;\n if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||\n !av_buffer_is_writable(buf) || buf->data != buf->buffer->data) {\n AVBufferRef *new = NULL;\n av_buffer_realloc(&new, size);\n if (!new)\n return AVERROR(ENOMEM);\n memcpy(new->data, buf->data, FFMIN(size, buf->size));\n av_buffer_unref(pbuf);\n *pbuf = new;\n return 0;\n }\n tmp = av_realloc(buf->buffer->data, size);\n if (!tmp)\n return AVERROR(ENOMEM);\n buf->buffer->data = buf->data = tmp;\n buf->buffer->size = buf->size = size;\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}']
|
1,214 | 0 |
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bio/b_sock.c/#L279
|
static struct hostent *ghbn_dup(struct hostent *a)
{
struct hostent *ret;
int i,j;
MemCheck_off();
ret=(struct hostent *)Malloc(sizeof(struct hostent));
if (ret == NULL) return(NULL);
memset(ret,0,sizeof(struct hostent));
for (i=0; a->h_aliases[i] != NULL; i++)
;
i++;
ret->h_aliases=(char **)Malloc(sizeof(char *)*i);
memset(ret->h_aliases,0,sizeof(char *)*i);
if (ret == NULL) goto err;
for (i=0; a->h_addr_list[i] != NULL; i++)
;
i++;
ret->h_addr_list=(char **)Malloc(sizeof(char *)*i);
memset(ret->h_addr_list,0,sizeof(char *)*i);
if (ret->h_addr_list == NULL) goto err;
j=strlen(a->h_name)+1;
if ((ret->h_name=Malloc(j)) == NULL) goto err;
memcpy((char *)ret->h_name,a->h_name,j+1);
for (i=0; a->h_aliases[i] != NULL; i++)
{
j=strlen(a->h_aliases[i])+1;
if ((ret->h_aliases[i]=Malloc(j)) == NULL) goto err;
memcpy(ret->h_aliases[i],a->h_aliases[i],j+1);
}
ret->h_length=a->h_length;
ret->h_addrtype=a->h_addrtype;
for (i=0; a->h_addr_list[i] != NULL; i++)
{
if ((ret->h_addr_list[i]=Malloc(a->h_length)) == NULL)
goto err;
memcpy(ret->h_addr_list[i],a->h_addr_list[i],a->h_length);
}
if (0)
{
err:
if (ret != NULL)
ghbn_free(ret);
ret=NULL;
}
MemCheck_on();
return(ret);
}
|
['static struct hostent *ghbn_dup(struct hostent *a)\n\t{\n\tstruct hostent *ret;\n\tint i,j;\n\tMemCheck_off();\n\tret=(struct hostent *)Malloc(sizeof(struct hostent));\n\tif (ret == NULL) return(NULL);\n\tmemset(ret,0,sizeof(struct hostent));\n\tfor (i=0; a->h_aliases[i] != NULL; i++)\n\t\t;\n\ti++;\n\tret->h_aliases=(char **)Malloc(sizeof(char *)*i);\n\tmemset(ret->h_aliases,0,sizeof(char *)*i);\n\tif (ret == NULL) goto err;\n\tfor (i=0; a->h_addr_list[i] != NULL; i++)\n\t\t;\n\ti++;\n\tret->h_addr_list=(char **)Malloc(sizeof(char *)*i);\n\tmemset(ret->h_addr_list,0,sizeof(char *)*i);\n\tif (ret->h_addr_list == NULL) goto err;\n\tj=strlen(a->h_name)+1;\n\tif ((ret->h_name=Malloc(j)) == NULL) goto err;\n\tmemcpy((char *)ret->h_name,a->h_name,j+1);\n\tfor (i=0; a->h_aliases[i] != NULL; i++)\n\t\t{\n\t\tj=strlen(a->h_aliases[i])+1;\n\t\tif ((ret->h_aliases[i]=Malloc(j)) == NULL) goto err;\n\t\tmemcpy(ret->h_aliases[i],a->h_aliases[i],j+1);\n\t\t}\n\tret->h_length=a->h_length;\n\tret->h_addrtype=a->h_addrtype;\n\tfor (i=0; a->h_addr_list[i] != NULL; i++)\n\t\t{\n\t\tif ((ret->h_addr_list[i]=Malloc(a->h_length)) == NULL)\n\t\t\tgoto err;\n\t\tmemcpy(ret->h_addr_list[i],a->h_addr_list[i],a->h_length);\n\t\t}\n\tif (0)\n\t\t{\nerr:\n\t\tif (ret != NULL)\n\t\t\tghbn_free(ret);\n\t\tret=NULL;\n\t\t}\n\tMemCheck_on();\n\treturn(ret);\n\t}']
|
1,215 | 0 |
https://github.com/openssl/openssl/blob/7144c4212a18e01bf805169ad1f3fdd885975759/apps/ca.c/#L2886
|
int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, const char *str)
{
char *tmp = NULL;
char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
int reason_code = -1;
int ret = 0;
unsigned int i;
ASN1_OBJECT *hold = NULL;
ASN1_GENERALIZEDTIME *comp_time = NULL;
tmp = BUF_strdup(str);
p = strchr(tmp, ',');
rtime_str = tmp;
if (p)
{
*p = '\0';
p++;
reason_str = p;
p = strchr(p, ',');
if (p)
{
*p = '\0';
arg_str = p + 1;
}
}
if (prevtm)
{
*prevtm = ASN1_UTCTIME_new();
if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str))
{
BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str);
goto err;
}
}
if (reason_str)
{
for (i = 0; i < NUM_REASONS; i++)
{
if(!strcasecmp(reason_str, crl_reasons[i]))
{
reason_code = i;
break;
}
}
if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS)
{
BIO_printf(bio_err, "invalid reason code %s\n", reason_str);
goto err;
}
if (reason_code == 7)
reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
else if (reason_code == 8)
{
if (!arg_str)
{
BIO_printf(bio_err, "missing hold instruction\n");
goto err;
}
reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
hold = OBJ_txt2obj(arg_str, 0);
if (!hold)
{
BIO_printf(bio_err, "invalid object identifier %s\n", arg_str);
goto err;
}
if (phold) *phold = hold;
}
else if ((reason_code == 9) || (reason_code == 10))
{
if (!arg_str)
{
BIO_printf(bio_err, "missing compromised time\n");
goto err;
}
comp_time = ASN1_GENERALIZEDTIME_new();
if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str))
{
BIO_printf(bio_err, "invalid compromised time %s\n", arg_str);
goto err;
}
if (reason_code == 9)
reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
else
reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
}
}
if (preason) *preason = reason_code;
if (pinvtm) *pinvtm = comp_time;
else ASN1_GENERALIZEDTIME_free(comp_time);
ret = 1;
err:
if (tmp) OPENSSL_free(tmp);
if (!phold) ASN1_OBJECT_free(hold);
if (!pinvtm) ASN1_GENERALIZEDTIME_free(comp_time);
return ret;
}
|
['int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, const char *str)\n\t{\n\tchar *tmp = NULL;\n\tchar *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;\n\tint reason_code = -1;\n\tint ret = 0;\n\tunsigned int i;\n\tASN1_OBJECT *hold = NULL;\n\tASN1_GENERALIZEDTIME *comp_time = NULL;\n\ttmp = BUF_strdup(str);\n\tp = strchr(tmp, \',\');\n\trtime_str = tmp;\n\tif (p)\n\t\t{\n\t\t*p = \'\\0\';\n\t\tp++;\n\t\treason_str = p;\n\t\tp = strchr(p, \',\');\n\t\tif (p)\n\t\t\t{\n\t\t\t*p = \'\\0\';\n\t\t\targ_str = p + 1;\n\t\t\t}\n\t\t}\n\tif (prevtm)\n\t\t{\n\t\t*prevtm = ASN1_UTCTIME_new();\n\t\tif (!ASN1_UTCTIME_set_string(*prevtm, rtime_str))\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "invalid revocation date %s\\n", rtime_str);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (reason_str)\n\t\t{\n\t\tfor (i = 0; i < NUM_REASONS; i++)\n\t\t\t{\n\t\t\tif(!strcasecmp(reason_str, crl_reasons[i]))\n\t\t\t\t{\n\t\t\t\treason_code = i;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tif (reason_code == OCSP_REVOKED_STATUS_NOSTATUS)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "invalid reason code %s\\n", reason_str);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (reason_code == 7)\n\t\t\treason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;\n\t\telse if (reason_code == 8)\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing hold instruction\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\treason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;\n\t\t\thold = OBJ_txt2obj(arg_str, 0);\n\t\t\tif (!hold)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid object identifier %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (phold) *phold = hold;\n\t\t\t}\n\t\telse if ((reason_code == 9) || (reason_code == 10))\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing compromised time\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tcomp_time = ASN1_GENERALIZEDTIME_new();\n\t\t\tif (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid compromised time %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (reason_code == 9)\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;\n\t\t\telse\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;\n\t\t\t}\n\t\t}\n\tif (preason) *preason = reason_code;\n\tif (pinvtm) *pinvtm = comp_time;\n\telse ASN1_GENERALIZEDTIME_free(comp_time);\n\tret = 1;\n\terr:\n\tif (tmp) OPENSSL_free(tmp);\n\tif (!phold) ASN1_OBJECT_free(hold);\n\tif (!pinvtm) ASN1_GENERALIZEDTIME_free(comp_time);\n\treturn ret;\n\t}', 'char *BUF_strdup(const char *str)\n\t{\n\tif (str == NULL) return(NULL);\n\treturn BUF_strndup(str, strlen(str));\n\t}', 'char *BUF_strndup(const char *str, size_t siz)\n\t{\n\tchar *ret;\n\tif (str == NULL) return(NULL);\n\tret=OPENSSL_malloc(siz+1);\n\tif (ret == NULL)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_STRNDUP,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tBUF_strlcpy(ret,str,siz+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_flags[es->top]=0;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', '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}']
|
1,216 | 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;
}
|
['static int test_div_recip()\n{\n BIGNUM *a, *b, *c, *d, *e;\n BN_RECP_CTX *recp;\n int i;\n recp = BN_RECP_CTX_new();\n a = BN_new();\n b = BN_new();\n c = BN_new();\n d = BN_new();\n e = BN_new();\n for (i = 0; i < NUM0 + NUM1; i++) {\n if (i < NUM1) {\n BN_bntest_rand(a, 400, 0, 0);\n BN_copy(b, a);\n BN_lshift(a, a, i);\n BN_add_word(a, i);\n } else\n BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);\n a->neg = rand_neg();\n b->neg = rand_neg();\n BN_RECP_CTX_set(recp, b, ctx);\n BN_div_recp(d, c, a, recp, ctx);\n BN_mul(e, d, b, ctx);\n BN_add(d, e, c);\n BN_sub(d, d, a);\n if (!BN_is_zero(d)) {\n printf("Reciprocal division test failed!\\n");\n printf("a=");\n BN_print_fp(stdout, a);\n printf("\\nb=");\n BN_print_fp(stdout, b);\n printf("\\n");\n return 0;\n }\n }\n BN_free(a);\n BN_free(b);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n BN_RECP_CTX_free(recp);\n return 1;\n}', 'int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!BN_copy(&(recp->N), d))\n return 0;\n BN_zero(&(recp->Nr));\n recp->num_bits = BN_num_bits(d);\n recp->shift = 0;\n return (1);\n}', 'int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,\n BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n int i, j, ret = 0;\n BIGNUM *a, *b, *d, *r;\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (dv != NULL)\n d = dv;\n else\n d = BN_CTX_get(ctx);\n if (rem != NULL)\n r = rem;\n else\n r = BN_CTX_get(ctx);\n if (a == NULL || b == NULL || d == NULL || r == NULL)\n goto err;\n if (BN_ucmp(m, &(recp->N)) < 0) {\n BN_zero(d);\n if (!BN_copy(r, m)) {\n BN_CTX_end(ctx);\n return 0;\n }\n BN_CTX_end(ctx);\n return (1);\n }\n i = BN_num_bits(m);\n j = recp->num_bits << 1;\n if (j > i)\n i = j;\n if (i != recp->shift)\n recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);\n if (recp->shift == -1)\n goto err;\n if (!BN_rshift(a, m, recp->num_bits))\n goto err;\n if (!BN_mul(b, a, &(recp->Nr), ctx))\n goto err;\n if (!BN_rshift(d, b, i - recp->num_bits))\n goto err;\n d->neg = 0;\n if (!BN_mul(b, &(recp->N), d, ctx))\n goto err;\n if (!BN_usub(r, m, b))\n goto err;\n r->neg = 0;\n j = 0;\n while (BN_ucmp(r, &(recp->N)) >= 0) {\n if (j++ > 2) {\n BNerr(BN_F_BN_DIV_RECP, BN_R_BAD_RECIPROCAL);\n goto err;\n }\n if (!BN_usub(r, r, &(recp->N)))\n goto err;\n if (!BN_add_word(d, 1))\n goto err;\n }\n r->neg = BN_is_zero(r) ? 0 : m->neg;\n d->neg = m->neg ^ recp->N.neg;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(dv);\n bn_check_top(rem);\n return (ret);\n}', 'int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx)\n{\n int ret = -1;\n BIGNUM *t;\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_set_bit(t, len))\n goto err;\n if (!BN_div(r, NULL, t, m, ctx))\n goto err;\n ret = len;\n err:\n bn_check_top(r);\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}', '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}']
|
1,217 | 0 |
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L454
|
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
int i;
BN_ULONG *A;
const BN_ULONG *B;
bn_check_top(b);
if (a == b)
return (a);
if (bn_wexpand(a, b->top) == NULL)
return (NULL);
#if 1
A = a->d;
B = b->d;
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
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
#endif
a->top = b->top;
a->neg = b->neg;
bn_check_top(a);
return (a);
}
|
['int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,\n const EC_POINT *b, BN_CTX *ctx)\n{\n int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,\n const BIGNUM *, BN_CTX *);\n int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);\n const BIGNUM *p;\n BN_CTX *new_ctx = NULL;\n BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;\n int ret = 0;\n if (a == b)\n return EC_POINT_dbl(group, r, a, ctx);\n if (EC_POINT_is_at_infinity(group, a))\n return EC_POINT_copy(r, b);\n if (EC_POINT_is_at_infinity(group, b))\n return EC_POINT_copy(r, a);\n field_mul = group->meth->field_mul;\n field_sqr = group->meth->field_sqr;\n p = group->field;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n n0 = BN_CTX_get(ctx);\n n1 = BN_CTX_get(ctx);\n n2 = BN_CTX_get(ctx);\n n3 = BN_CTX_get(ctx);\n n4 = BN_CTX_get(ctx);\n n5 = BN_CTX_get(ctx);\n n6 = BN_CTX_get(ctx);\n if (n6 == NULL)\n goto end;\n if (b->Z_is_one) {\n if (!BN_copy(n1, a->X))\n goto end;\n if (!BN_copy(n2, a->Y))\n goto end;\n } else {\n if (!field_sqr(group, n0, b->Z, ctx))\n goto end;\n if (!field_mul(group, n1, a->X, n0, ctx))\n goto end;\n if (!field_mul(group, n0, n0, b->Z, ctx))\n goto end;\n if (!field_mul(group, n2, a->Y, n0, ctx))\n goto end;\n }\n if (a->Z_is_one) {\n if (!BN_copy(n3, b->X))\n goto end;\n if (!BN_copy(n4, b->Y))\n goto end;\n } else {\n if (!field_sqr(group, n0, a->Z, ctx))\n goto end;\n if (!field_mul(group, n3, b->X, n0, ctx))\n goto end;\n if (!field_mul(group, n0, n0, a->Z, ctx))\n goto end;\n if (!field_mul(group, n4, b->Y, n0, ctx))\n goto end;\n }\n if (!BN_mod_sub_quick(n5, n1, n3, p))\n goto end;\n if (!BN_mod_sub_quick(n6, n2, n4, p))\n goto end;\n if (BN_is_zero(n5)) {\n if (BN_is_zero(n6)) {\n BN_CTX_end(ctx);\n ret = EC_POINT_dbl(group, r, a, ctx);\n ctx = NULL;\n goto end;\n } else {\n BN_zero(r->Z);\n r->Z_is_one = 0;\n ret = 1;\n goto end;\n }\n }\n if (!BN_mod_add_quick(n1, n1, n3, p))\n goto end;\n if (!BN_mod_add_quick(n2, n2, n4, p))\n goto end;\n if (a->Z_is_one && b->Z_is_one) {\n if (!BN_copy(r->Z, n5))\n goto end;\n } else {\n if (a->Z_is_one) {\n if (!BN_copy(n0, b->Z))\n goto end;\n } else if (b->Z_is_one) {\n if (!BN_copy(n0, a->Z))\n goto end;\n } else {\n if (!field_mul(group, n0, a->Z, b->Z, ctx))\n goto end;\n }\n if (!field_mul(group, r->Z, n0, n5, ctx))\n goto end;\n }\n r->Z_is_one = 0;\n if (!field_sqr(group, n0, n6, ctx))\n goto end;\n if (!field_sqr(group, n4, n5, ctx))\n goto end;\n if (!field_mul(group, n3, n1, n4, ctx))\n goto end;\n if (!BN_mod_sub_quick(r->X, n0, n3, p))\n goto end;\n if (!BN_mod_lshift1_quick(n0, r->X, p))\n goto end;\n if (!BN_mod_sub_quick(n0, n3, n0, p))\n goto end;\n if (!field_mul(group, n0, n0, n6, ctx))\n goto end;\n if (!field_mul(group, n5, n4, n5, ctx))\n goto end;\n if (!field_mul(group, n1, n2, n5, ctx))\n goto end;\n if (!BN_mod_sub_quick(n0, n0, n1, p))\n goto end;\n if (BN_is_odd(n0))\n if (!BN_add(n0, n0, p))\n goto end;\n if (!BN_rshift1(r->Y, n0))\n goto end;\n ret = 1;\n end:\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n int i;\n BN_ULONG *A;\n const BN_ULONG *B;\n bn_check_top(b);\n if (a == b)\n return (a);\n if (bn_wexpand(a, b->top) == NULL)\n return (NULL);\n#if 1\n A = a->d;\n B = b->d;\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:;\n }\n#else\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n#endif\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return (a);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
|
1,218 | 0 |
https://github.com/libav/libav/blob/b12b16c5d35adaba0979a7c2fa76b88e48f5f839/libavcodec/vorbis.c/#L42
|
unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
{
unsigned int ret = 0, i, j;
do {
++ret;
for (i = 0, j = ret; i < n - 1; i++)
j *= ret;
} while (j <= x);
return ret - 1;
}
|
['static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc)\n{\n uint_fast16_t cb;\n uint8_t *tmp_vlc_bits;\n uint32_t *tmp_vlc_codes;\n GetBitContext *gb = &vc->gb;\n uint_fast16_t *codebook_multiplicands;\n vc->codebook_count = get_bits(gb, 8) + 1;\n AV_DEBUG(" Codebooks: %d \\n", vc->codebook_count);\n vc->codebooks = av_mallocz(vc->codebook_count * sizeof(vorbis_codebook));\n tmp_vlc_bits = av_mallocz(V_MAX_VLCS * sizeof(uint8_t));\n tmp_vlc_codes = av_mallocz(V_MAX_VLCS * sizeof(uint32_t));\n codebook_multiplicands = av_malloc(V_MAX_VLCS * sizeof(*codebook_multiplicands));\n for (cb = 0; cb < vc->codebook_count; ++cb) {\n vorbis_codebook *codebook_setup = &vc->codebooks[cb];\n uint_fast8_t ordered;\n uint_fast32_t t, used_entries = 0;\n uint_fast32_t entries;\n AV_DEBUG(" %d. Codebook \\n", cb);\n if (get_bits(gb, 24) != 0x564342) {\n av_log(vc->avccontext, AV_LOG_ERROR, " %"PRIdFAST16". Codebook setup data corrupt. \\n", cb);\n goto error;\n }\n codebook_setup->dimensions=get_bits(gb, 16);\n if (codebook_setup->dimensions > 16 || codebook_setup->dimensions == 0) {\n av_log(vc->avccontext, AV_LOG_ERROR, " %"PRIdFAST16". Codebook\'s dimension is invalid (%d). \\n", cb, codebook_setup->dimensions);\n goto error;\n }\n entries = get_bits(gb, 24);\n if (entries > V_MAX_VLCS) {\n av_log(vc->avccontext, AV_LOG_ERROR, " %"PRIdFAST16". Codebook has too many entries (%"PRIdFAST32"). \\n", cb, entries);\n goto error;\n }\n ordered = get_bits1(gb);\n AV_DEBUG(" codebook_dimensions %d, codebook_entries %d \\n", codebook_setup->dimensions, entries);\n if (!ordered) {\n uint_fast16_t ce;\n uint_fast8_t flag;\n uint_fast8_t sparse = get_bits1(gb);\n AV_DEBUG(" not ordered \\n");\n if (sparse) {\n AV_DEBUG(" sparse \\n");\n used_entries = 0;\n for (ce = 0; ce < entries; ++ce) {\n flag = get_bits1(gb);\n if (flag) {\n tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;\n ++used_entries;\n } else\n tmp_vlc_bits[ce] = 0;\n }\n } else {\n AV_DEBUG(" not sparse \\n");\n used_entries = entries;\n for (ce = 0; ce < entries; ++ce)\n tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;\n }\n } else {\n uint_fast16_t current_entry = 0;\n uint_fast8_t current_length = get_bits(gb, 5)+1;\n AV_DEBUG(" ordered, current length: %d \\n", current_length);\n used_entries = entries;\n for (; current_entry < used_entries && current_length <= 32; ++current_length) {\n uint_fast16_t i, number;\n AV_DEBUG(" number bits: %d ", ilog(entries - current_entry));\n number = get_bits(gb, ilog(entries - current_entry));\n AV_DEBUG(" number: %d \\n", number);\n for (i = current_entry; i < number+current_entry; ++i)\n if (i < used_entries)\n tmp_vlc_bits[i] = current_length;\n current_entry+=number;\n }\n if (current_entry>used_entries) {\n av_log(vc->avccontext, AV_LOG_ERROR, " More codelengths than codes in codebook. \\n");\n goto error;\n }\n }\n codebook_setup->lookup_type = get_bits(gb, 4);\n AV_DEBUG(" lookup type: %d : %s \\n", codebook_setup->lookup_type, codebook_setup->lookup_type ? "vq" : "no lookup");\n if (codebook_setup->lookup_type == 1) {\n uint_fast16_t i, j, k;\n uint_fast16_t codebook_lookup_values = ff_vorbis_nth_root(entries, codebook_setup->dimensions);\n float codebook_minimum_value = vorbisfloat2float(get_bits_long(gb, 32));\n float codebook_delta_value = vorbisfloat2float(get_bits_long(gb, 32));\n uint_fast8_t codebook_value_bits = get_bits(gb, 4)+1;\n uint_fast8_t codebook_sequence_p = get_bits1(gb);\n AV_DEBUG(" We expect %d numbers for building the codevectors. \\n", codebook_lookup_values);\n AV_DEBUG(" delta %f minmum %f \\n", codebook_delta_value, codebook_minimum_value);\n for (i = 0; i < codebook_lookup_values; ++i) {\n codebook_multiplicands[i] = get_bits(gb, codebook_value_bits);\n AV_DEBUG(" multiplicands*delta+minmum : %e \\n", (float)codebook_multiplicands[i]*codebook_delta_value+codebook_minimum_value);\n AV_DEBUG(" multiplicand %d \\n", codebook_multiplicands[i]);\n }\n codebook_setup->codevectors = used_entries ? av_mallocz(used_entries*codebook_setup->dimensions * sizeof(float)) : NULL;\n for (j = 0, i = 0; i < entries; ++i) {\n uint_fast8_t dim = codebook_setup->dimensions;\n if (tmp_vlc_bits[i]) {\n float last = 0.0;\n uint_fast32_t lookup_offset = i;\n#ifdef V_DEBUG\n av_log(vc->avccontext, AV_LOG_INFO, "Lookup offset %d ,", i);\n#endif\n for (k = 0; k < dim; ++k) {\n uint_fast32_t multiplicand_offset = lookup_offset % codebook_lookup_values;\n codebook_setup->codevectors[j * dim + k] = codebook_multiplicands[multiplicand_offset] * codebook_delta_value + codebook_minimum_value + last;\n if (codebook_sequence_p)\n last = codebook_setup->codevectors[j * dim + k];\n lookup_offset/=codebook_lookup_values;\n }\n tmp_vlc_bits[j] = tmp_vlc_bits[i];\n#ifdef V_DEBUG\n av_log(vc->avccontext, AV_LOG_INFO, "real lookup offset %d, vector: ", j);\n for (k = 0; k < dim; ++k)\n av_log(vc->avccontext, AV_LOG_INFO, " %f ", codebook_setup->codevectors[j * dim + k]);\n av_log(vc->avccontext, AV_LOG_INFO, "\\n");\n#endif\n ++j;\n }\n }\n if (j != used_entries) {\n av_log(vc->avccontext, AV_LOG_ERROR, "Bug in codevector vector building code. \\n");\n goto error;\n }\n entries = used_entries;\n } else if (codebook_setup->lookup_type >= 2) {\n av_log(vc->avccontext, AV_LOG_ERROR, "Codebook lookup type not supported. \\n");\n goto error;\n }\n if (ff_vorbis_len2vlc(tmp_vlc_bits, tmp_vlc_codes, entries)) {\n av_log(vc->avccontext, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \\n");\n goto error;\n }\n codebook_setup->maxdepth = 0;\n for (t = 0; t < entries; ++t)\n if (tmp_vlc_bits[t] >= codebook_setup->maxdepth)\n codebook_setup->maxdepth = tmp_vlc_bits[t];\n if (codebook_setup->maxdepth > 3 * V_NB_BITS)\n codebook_setup->nb_bits = V_NB_BITS2;\n else\n codebook_setup->nb_bits = V_NB_BITS;\n codebook_setup->maxdepth = (codebook_setup->maxdepth+codebook_setup->nb_bits - 1) / codebook_setup->nb_bits;\n if (init_vlc(&codebook_setup->vlc, codebook_setup->nb_bits, entries, tmp_vlc_bits, sizeof(*tmp_vlc_bits), sizeof(*tmp_vlc_bits), tmp_vlc_codes, sizeof(*tmp_vlc_codes), sizeof(*tmp_vlc_codes), INIT_VLC_LE)) {\n av_log(vc->avccontext, AV_LOG_ERROR, " Error generating vlc tables. \\n");\n goto error;\n }\n }\n av_free(tmp_vlc_bits);\n av_free(tmp_vlc_codes);\n av_free(codebook_multiplicands);\n return 0;\nerror:\n av_free(tmp_vlc_bits);\n av_free(tmp_vlc_codes);\n av_free(codebook_multiplicands);\n return -1;\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}', 'static av_always_inline av_const uint32_t av_bswap32(uint32_t x)\n{\n x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);\n x= (x>>16) | (x<<16);\n return x;\n}', 'unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)\n{\n unsigned int ret = 0, i, j;\n do {\n ++ret;\n for (i = 0, j = ret; i < n - 1; i++)\n j *= ret;\n } while (j <= x);\n return ret - 1;\n}']
|
1,219 | 0 |
https://github.com/libav/libav/blob/df84d7d9bdf6b8e6896c711880f130b72738c828/libavcodec/mpegaudiodec.c/#L721
|
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}']
|
1,220 | 0 |
https://github.com/libav/libav/blob/447a5b1996805e9e91acc5cb459ea1a047db12a1/libavfilter/formats.c/#L122
|
void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
{
*ref = f;
f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
f->refs[f->refcount-1] = ref;
}
|
['void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)\n{\n *ref = f;\n f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);\n f->refs[f->refcount-1] = ref;\n}', 'void *av_realloc(void *ptr, unsigned int 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) return av_malloc(size);\n diff= ((char*)ptr)[-1];\n return (char*)realloc((char*)ptr - diff, size + diff) + diff;\n#else\n return realloc(ptr, size);\n#endif\n}']
|
1,221 | 0 |
https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/crypto/bn/bn_ctx.c/#L273
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['static int file_modsqrt(STANZA *s)\n{\n BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;\n int st = 0;\n if (!TEST_ptr(a = getBN(s, "A"))\n || !TEST_ptr(p = getBN(s, "P"))\n || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))\n || !TEST_ptr(ret = BN_new())\n || !TEST_ptr(ret2 = BN_new()))\n goto err;\n if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))\n || !TEST_true(BN_sub(ret2, p, ret)))\n goto err;\n if (BN_cmp(ret2, mod_sqrt) != 0\n && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))\n goto err;\n st = 1;\nerr:\n BN_free(a);\n BN_free(p);\n BN_free(mod_sqrt);\n BN_free(ret);\n BN_free(ret2);\n return st;\n}', 'BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *ret = in;\n int err = 1;\n int r;\n BIGNUM *A, *b, *q, *t, *x, *y;\n int e, i, j;\n if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {\n if (BN_abs_is_word(p, 2)) {\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {\n if (ret != in)\n BN_free(ret);\n return NULL;\n }\n bn_check_top(ret);\n return ret;\n }\n BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n return (NULL);\n }\n if (BN_is_zero(a) || BN_is_one(a)) {\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_set_word(ret, BN_is_one(a))) {\n if (ret != in)\n BN_free(ret);\n return NULL;\n }\n bn_check_top(ret);\n return ret;\n }\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto end;\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_nnmod(A, a, p, ctx))\n goto end;\n e = 1;\n while (!BN_is_bit_set(p, e))\n e++;\n if (e == 1) {\n if (!BN_rshift(q, p, 2))\n goto end;\n q->neg = 0;\n if (!BN_add_word(q, 1))\n goto end;\n if (!BN_mod_exp(ret, A, q, p, ctx))\n goto end;\n err = 0;\n goto vrfy;\n }\n if (e == 2) {\n if (!BN_mod_lshift1_quick(t, A, p))\n goto end;\n if (!BN_rshift(q, p, 3))\n goto end;\n q->neg = 0;\n if (!BN_mod_exp(b, t, q, p, ctx))\n goto end;\n if (!BN_mod_sqr(y, b, p, ctx))\n goto end;\n if (!BN_mod_mul(t, t, y, p, ctx))\n goto end;\n if (!BN_sub_word(t, 1))\n goto end;\n if (!BN_mod_mul(x, A, b, p, ctx))\n goto end;\n if (!BN_mod_mul(x, x, t, p, ctx))\n goto end;\n if (!BN_copy(ret, x))\n goto end;\n err = 0;\n goto vrfy;\n }\n if (!BN_copy(q, p))\n goto end;\n q->neg = 0;\n i = 2;\n do {\n if (i < 22) {\n if (!BN_set_word(y, i))\n goto end;\n } else {\n if (!BN_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}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
1,222 | 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 group_order_tests(EC_GROUP *group)\n{\n BIGNUM *n1 = NULL, *n2 = NULL, *order = NULL;\n EC_POINT *P = NULL, *Q = NULL, *R = NULL, *S = NULL;\n const EC_POINT *G = NULL;\n BN_CTX *ctx = NULL;\n int i = 0, r = 0;\n if (!TEST_ptr(n1 = BN_new())\n || !TEST_ptr(n2 = BN_new())\n || !TEST_ptr(order = BN_new())\n || !TEST_ptr(ctx = BN_CTX_new())\n || !TEST_ptr(G = EC_GROUP_get0_generator(group))\n || !TEST_ptr(P = EC_POINT_new(group))\n || !TEST_ptr(Q = EC_POINT_new(group))\n || !TEST_ptr(R = EC_POINT_new(group))\n || !TEST_ptr(S = EC_POINT_new(group)))\n goto err;\n if (!TEST_true(EC_GROUP_get_order(group, order, ctx))\n || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, Q))\n || !TEST_true(EC_GROUP_precompute_mult(group, ctx))\n || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, Q))\n || !TEST_true(EC_POINT_copy(P, G))\n || !TEST_true(BN_one(n1))\n || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))\n || !TEST_true(BN_sub(n1, order, n1))\n || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))\n || !TEST_true(EC_POINT_invert(group, Q, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))\n goto err;\n for (i = 1; i <= 2; i++) {\n const BIGNUM *scalars[6];\n const EC_POINT *points[6];\n if (!TEST_true(BN_set_word(n1, i))\n || !TEST_true(EC_POINT_mul(group, P, n1, NULL, NULL, ctx))\n || (i == 1 && !TEST_int_eq(0, EC_POINT_cmp(group, P, G, ctx)))\n || !TEST_true(BN_one(n1))\n || !TEST_true(BN_sub(n1, n1, order))\n || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n1, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))\n || !TEST_true(BN_add(n2, order, BN_value_one()))\n || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))\n || !TEST_true(BN_mul(n2, n1, n2, ctx))\n || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))\n goto err;\n BN_set_negative(n2, 0);\n if (!TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))\n || !TEST_true(EC_POINT_add(group, Q, Q, P, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, Q))\n || !TEST_false(EC_POINT_is_at_infinity(group, P)))\n goto err;\n scalars[0] = scalars[1] = BN_value_one();\n points[0] = points[1] = P;\n if (!TEST_true(EC_POINTs_mul(group, R, NULL, 2, points, scalars, ctx))\n || !TEST_true(EC_POINT_dbl(group, S, points[0], ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, R, S, ctx)))\n goto err;\n scalars[0] = n1;\n points[0] = Q;\n scalars[1] = n2;\n points[1] = P;\n scalars[2] = n1;\n points[2] = Q;\n scalars[3] = n2;\n points[3] = Q;\n scalars[4] = n1;\n points[4] = P;\n scalars[5] = n2;\n points[5] = Q;\n if (!TEST_true(EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, P)))\n goto err;\n }\n r = 1;\nerr:\n if (r == 0 && i != 0)\n TEST_info(i == 1 ? "allowing precomputation" :\n "without precomputation");\n EC_POINT_free(P);\n EC_POINT_free(Q);\n EC_POINT_free(R);\n EC_POINT_free(S);\n BN_free(n1);\n BN_free(n2);\n BN_free(order);\n BN_CTX_free(ctx);\n return r;\n}', 'int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[],\n const BIGNUM *scalars[], BN_CTX *ctx)\n{\n int ret = 0;\n size_t i = 0;\n BN_CTX *new_ctx = NULL;\n if ((scalar == NULL) && (num == 0)) {\n return EC_POINT_set_to_infinity(group, r);\n }\n if (!ec_point_is_compat(r, group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n for (i = 0; i < num; i++) {\n if (!ec_point_is_compat(points[i], group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n }\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {\n ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (group->meth->mul != NULL)\n ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);\n else\n ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = bn_mul_fixed_top(r, a, b, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_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}']
|
1,223 | 0 |
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bn/bn_lib.c/#L620
|
int BN_bn2bin(BIGNUM *a, unsigned char *to)
{
int n,i;
BN_ULONG l;
n=i=BN_num_bytes(a);
while (i-- > 0)
{
l=a->d[i/BN_BYTES];
*(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff;
}
return(n);
}
|
['int DSA_print(BIO *bp, DSA *x, int off)\n\t{\n\tchar str[128];\n\tunsigned char *m=NULL;\n\tint i,ret=0;\n\tBIGNUM *bn=NULL;\n\tif (x->p != NULL)\n\t\tbn=x->p;\n\telse if (x->priv_key != NULL)\n\t\tbn=x->priv_key;\n\telse if (x->pub_key != NULL)\n\t\tbn=x->pub_key;\n\tif (bn != NULL)\n\t\ti=BN_num_bytes(bn)*2;\n\telse\n\t\ti=256;\n\tm=(unsigned char *)Malloc((unsigned int)i+10);\n\tif (m == NULL)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_PRINT,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tif (off)\n\t\t{\n\t\tif (off > 128) off=128;\n\t\tmemset(str,\' \',off);\n\t\t}\n\tif (x->priv_key != NULL)\n\t\t{\n\t\tif (off && (BIO_write(bp,str,off) <= 0)) goto err;\n\t\tif (BIO_printf(bp,"Private-Key: (%d bit)\\n",BN_num_bits(x->p))\n\t\t\t<= 0) goto err;\n\t\t}\n\tif ((x->priv_key != NULL) && !print(bp,"priv:",x->priv_key,m,off))\n\t\tgoto err;\n\tif ((x->pub_key != NULL) && !print(bp,"pub: ",x->pub_key,m,off))\n\t\tgoto err;\n\tif ((x->p != NULL) && !print(bp,"P: ",x->p,m,off)) goto err;\n\tif ((x->q != NULL) && !print(bp,"Q: ",x->q,m,off)) goto err;\n\tif ((x->g != NULL) && !print(bp,"G: ",x->g,m,off)) goto err;\n\tret=1;\nerr:\n\tif (m != NULL) Free((char *)m);\n\treturn(ret);\n\t}', 'static int print(BIO *bp, const char *number, BIGNUM *num, unsigned char *buf,\n\t int off)\n\t{\n\tint n,i;\n\tchar str[128];\n\tconst char *neg;\n\tif (num == NULL) return(1);\n\tneg=(num->neg)?"-":"";\n\tif (off)\n\t\t{\n\t\tif (off > 128) off=128;\n\t\tmemset(str,\' \',off);\n\t\tif (BIO_write(bp,str,off) <= 0) return(0);\n\t\t}\n\tif (BN_num_bytes(num) <= BN_BYTES)\n\t\t{\n\t\tif (BIO_printf(bp,"%s %s%lu (%s0x%lx)\\n",number,neg,\n\t\t\t(unsigned long)num->d[0],neg,(unsigned long)num->d[0])\n\t\t\t<= 0) return(0);\n\t\t}\n\telse\n\t\t{\n\t\tbuf[0]=0;\n\t\tif (BIO_printf(bp,"%s%s",number,\n\t\t\t(neg[0] == \'-\')?" (Negative)":"") <= 0)\n\t\t\treturn(0);\n\t\tn=BN_bn2bin(num,&buf[1]);\n\t\tif (buf[1] & 0x80)\n\t\t\tn++;\n\t\telse\tbuf++;\n\t\tfor (i=0; i<n; i++)\n\t\t\t{\n\t\t\tif ((i%15) == 0)\n\t\t\t\t{\n\t\t\t\tstr[0]=\'\\n\';\n\t\t\t\tmemset(&(str[1]),\' \',off+4);\n\t\t\t\tif (BIO_write(bp,str,off+1+4) <= 0) return(0);\n\t\t\t\t}\n\t\t\tif (BIO_printf(bp,"%02x%s",buf[i],((i+1) == n)?"":":")\n\t\t\t\t<= 0) return(0);\n\t\t\t}\n\t\tif (BIO_write(bp,"\\n",1) <= 0) return(0);\n\t\t}\n\treturn(1);\n\t}', 'int BN_bn2bin(BIGNUM *a, unsigned char *to)\n\t{\n\tint n,i;\n\tBN_ULONG l;\n\tn=i=BN_num_bytes(a);\n\twhile (i-- > 0)\n\t\t{\n\t\tl=a->d[i/BN_BYTES];\n\t\t*(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff;\n\t\t}\n\treturn(n);\n\t}']
|
1,224 | 0 |
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/asn1/asn1_lib.c/#L248
|
static void asn1_put_length(unsigned char **pp, int length)
{
unsigned char *p = *pp;
int i, l;
if (length <= 127)
*(p++) = (unsigned char)length;
else {
l = length;
for (i = 0; l > 0; i++)
l >>= 8;
*(p++) = i | 0x80;
l = i;
while (i-- > 0) {
p[i] = length & 0xff;
length >>= 8;
}
p += l;
}
*pp = p;
}
|
['int RSA_sign(int type, const unsigned char *m, unsigned int m_len,\n unsigned char *sigret, unsigned int *siglen, RSA *rsa)\n{\n X509_SIG sig;\n ASN1_TYPE parameter;\n int i, j, ret = 1;\n unsigned char *p, *tmps = NULL;\n const unsigned char *s = NULL;\n X509_ALGOR algor;\n ASN1_OCTET_STRING digest;\n if (rsa->meth->rsa_sign) {\n return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa);\n }\n if (type == NID_md5_sha1) {\n if (m_len != SSL_SIG_LENGTH) {\n RSAerr(RSA_F_RSA_SIGN, RSA_R_INVALID_MESSAGE_LENGTH);\n return (0);\n }\n i = SSL_SIG_LENGTH;\n s = m;\n } else {\n sig.algor = &algor;\n sig.algor->algorithm = OBJ_nid2obj(type);\n if (sig.algor->algorithm == NULL) {\n RSAerr(RSA_F_RSA_SIGN, RSA_R_UNKNOWN_ALGORITHM_TYPE);\n return (0);\n }\n if (OBJ_length(sig.algor->algorithm) == 0) {\n RSAerr(RSA_F_RSA_SIGN,\n RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);\n return (0);\n }\n parameter.type = V_ASN1_NULL;\n parameter.value.ptr = NULL;\n sig.algor->parameter = ¶meter;\n sig.digest = &digest;\n sig.digest->data = (unsigned char *)m;\n sig.digest->length = m_len;\n i = i2d_X509_SIG(&sig, NULL);\n }\n j = RSA_size(rsa);\n if (i > (j - RSA_PKCS1_PADDING_SIZE)) {\n RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);\n return (0);\n }\n if (type != NID_md5_sha1) {\n tmps = OPENSSL_malloc((unsigned int)j + 1);\n if (tmps == NULL) {\n RSAerr(RSA_F_RSA_SIGN, ERR_R_MALLOC_FAILURE);\n return (0);\n }\n p = tmps;\n i2d_X509_SIG(&sig, &p);\n s = tmps;\n }\n i = RSA_private_encrypt(i, s, sigret, rsa, RSA_PKCS1_PADDING);\n if (i <= 0)\n ret = 0;\n else\n *siglen = i;\n if (type != NID_md5_sha1)\n OPENSSL_clear_free(tmps, (unsigned int)j + 1);\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#ifdef CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'IMPLEMENT_ASN1_FUNCTIONS(X509_SIG)', '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}', 'int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,\n const ASN1_ITEM *it, int tag, int aclass)\n{\n const ASN1_TEMPLATE *tt = NULL;\n int i, seqcontlen, seqlen, ndef = 1;\n const ASN1_EXTERN_FUNCS *ef;\n const ASN1_AUX *aux = it->funcs;\n ASN1_aux_cb *asn1_cb = 0;\n if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)\n return 0;\n if (aux && aux->asn1_cb)\n asn1_cb = aux->asn1_cb;\n switch (it->itype) {\n case ASN1_ITYPE_PRIMITIVE:\n if (it->templates)\n return asn1_template_ex_i2d(pval, out, it->templates,\n tag, aclass);\n return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);\n case ASN1_ITYPE_MSTRING:\n return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);\n case ASN1_ITYPE_CHOICE:\n if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))\n return 0;\n i = asn1_get_choice_selector(pval, it);\n if ((i >= 0) && (i < it->tcount)) {\n ASN1_VALUE **pchval;\n const ASN1_TEMPLATE *chtt;\n chtt = it->templates + i;\n pchval = asn1_get_field_ptr(pval, chtt);\n return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);\n }\n if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))\n return 0;\n break;\n case ASN1_ITYPE_EXTERN:\n ef = it->funcs;\n return ef->asn1_ex_i2d(pval, out, it, tag, aclass);\n case ASN1_ITYPE_NDEF_SEQUENCE:\n if (aclass & ASN1_TFLG_NDEF)\n ndef = 2;\n case ASN1_ITYPE_SEQUENCE:\n i = asn1_enc_restore(&seqcontlen, out, pval, it);\n if (i < 0)\n return 0;\n if (i > 0)\n return seqcontlen;\n seqcontlen = 0;\n if (tag == -1) {\n tag = V_ASN1_SEQUENCE;\n aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)\n | V_ASN1_UNIVERSAL;\n }\n if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))\n return 0;\n for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {\n const ASN1_TEMPLATE *seqtt;\n ASN1_VALUE **pseqval;\n seqtt = asn1_do_adb(pval, tt, 1);\n if (!seqtt)\n return 0;\n pseqval = asn1_get_field_ptr(pval, seqtt);\n seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt,\n -1, aclass);\n }\n seqlen = ASN1_object_size(ndef, seqcontlen, tag);\n if (!out)\n return seqlen;\n ASN1_put_object(out, ndef, seqcontlen, tag, aclass);\n for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {\n const ASN1_TEMPLATE *seqtt;\n ASN1_VALUE **pseqval;\n seqtt = asn1_do_adb(pval, tt, 1);\n if (!seqtt)\n return 0;\n pseqval = asn1_get_field_ptr(pval, seqtt);\n asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);\n }\n if (ndef == 2)\n ASN1_put_eoc(out);\n if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))\n return 0;\n return seqlen;\n default:\n return 0;\n }\n return 0;\n}', 'static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,\n const ASN1_ITEM *it, int tag, int aclass)\n{\n int len;\n int utype;\n int usetag;\n int ndef = 0;\n utype = it->utype;\n len = asn1_ex_i2c(pval, NULL, &utype, it);\n if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||\n (utype == V_ASN1_OTHER))\n usetag = 0;\n else\n usetag = 1;\n if (len == -1)\n return 0;\n if (len == -2) {\n ndef = 2;\n len = 0;\n }\n if (tag == -1)\n tag = utype;\n if (out) {\n if (usetag)\n ASN1_put_object(out, ndef, len, tag, aclass);\n asn1_ex_i2c(pval, *out, &utype, it);\n if (ndef)\n ASN1_put_eoc(out);\n else\n *out += len;\n }\n if (usetag)\n return ASN1_object_size(ndef, len, tag);\n return len;\n}', 'void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,\n int xclass)\n{\n unsigned char *p = *pp;\n int i, ttag;\n i = (constructed) ? V_ASN1_CONSTRUCTED : 0;\n i |= (xclass & V_ASN1_PRIVATE);\n if (tag < 31)\n *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);\n else {\n *(p++) = i | V_ASN1_PRIMITIVE_TAG;\n for (i = 0, ttag = tag; ttag > 0; i++)\n ttag >>= 7;\n ttag = i;\n while (i-- > 0) {\n p[i] = tag & 0x7f;\n if (i != (ttag - 1))\n p[i] |= 0x80;\n tag >>= 7;\n }\n p += ttag;\n }\n if (constructed == 2)\n *(p++) = 0x80;\n else\n asn1_put_length(&p, length);\n *pp = p;\n}', 'static void asn1_put_length(unsigned char **pp, int length)\n{\n unsigned char *p = *pp;\n int i, l;\n if (length <= 127)\n *(p++) = (unsigned char)length;\n else {\n l = length;\n for (i = 0; l > 0; i++)\n l >>= 8;\n *(p++) = i | 0x80;\n l = i;\n while (i-- > 0) {\n p[i] = length & 0xff;\n length >>= 8;\n }\n p += l;\n }\n *pp = p;\n}']
|
1,225 | 0 |
https://github.com/libav/libav/blob/953302656aaaeb1bc737b59fae2d5a11b75d9f7d/libavcodec/motion_est_template.c/#L342
|
static int qpel_motion_search(MpegEncContext * s,
int *mx_ptr, int *my_ptr, int dmin,
int src_index, int ref_index,
int size, int h)
{
MotionEstContext * const c= &s->me;
const int mx = *mx_ptr;
const int my = *my_ptr;
const int penalty_factor= c->sub_penalty_factor;
const int map_generation= c->map_generation;
const int subpel_quality= c->avctx->me_subpel_quality;
uint32_t *map= c->map;
me_cmp_func cmpf, chroma_cmpf;
me_cmp_func cmp_sub, chroma_cmp_sub;
LOAD_COMMON
int flags= c->sub_flags;
cmpf= s->dsp.me_cmp[size];
chroma_cmpf= s->dsp.me_cmp[size+1];
cmp_sub= s->dsp.me_sub_cmp[size];
chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
if(c->skip){
*mx_ptr = 0;
*my_ptr = 0;
return dmin;
}
if(c->avctx->me_cmp != c->avctx->me_sub_cmp){
dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
if(mx || my || size>0)
dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
}
if (mx > xmin && mx < xmax &&
my > ymin && my < ymax) {
int bx=4*mx, by=4*my;
int d= dmin;
int i, nx, ny;
const int index= (my<<ME_MAP_SHIFT) + mx;
const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
const int c= score_map[(index )&(ME_MAP_SIZE-1)];
int best[8];
int best_pos[8][2];
memset(best, 64, sizeof(int)*8);
if(s->me.dia_size>=2){
const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
for(ny= -3; ny <= 3; ny++){
for(nx= -3; nx <= 3; nx++){
const int64_t t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
const int64_t c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;
const int64_t b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
int score= (ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2 + 512)>>10;
int i;
if((nx&3)==0 && (ny&3)==0) continue;
score += (mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
for(i=0; i<8; i++){
if(score < best[i]){
memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
best[i]= score;
best_pos[i][0]= nx + 4*mx;
best_pos[i][1]= ny + 4*my;
break;
}
}
}
}
}else{
int tl;
const int cx = 4*(r - l);
const int cx2= r + l - 2*c;
const int cy = 4*(b - t);
const int cy2= b + t - 2*c;
int cxy;
if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){
tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
}else{
tl= cmp(s, mx-1, my-1, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);
}
cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;
assert(16*cx2 + 4*cx + 32*c == 32*r);
assert(16*cx2 - 4*cx + 32*c == 32*l);
assert(16*cy2 + 4*cy + 32*c == 32*b);
assert(16*cy2 - 4*cy + 32*c == 32*t);
assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
for(ny= -3; ny <= 3; ny++){
for(nx= -3; nx <= 3; nx++){
int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c;
int i;
if((nx&3)==0 && (ny&3)==0) continue;
score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
for(i=0; i<8; i++){
if(score < best[i]){
memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
best[i]= score;
best_pos[i][0]= nx + 4*mx;
best_pos[i][1]= ny + 4*my;
break;
}
}
}
}
}
for(i=0; i<subpel_quality; i++){
nx= best_pos[i][0];
ny= best_pos[i][1];
CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
}
assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
*mx_ptr = bx;
*my_ptr = by;
}else{
*mx_ptr =4*mx;
*my_ptr =4*my;
}
return dmin;
}
|
['static int qpel_motion_search(MpegEncContext * s,\n int *mx_ptr, int *my_ptr, int dmin,\n int src_index, int ref_index,\n int size, int h)\n{\n MotionEstContext * const c= &s->me;\n const int mx = *mx_ptr;\n const int my = *my_ptr;\n const int penalty_factor= c->sub_penalty_factor;\n const int map_generation= c->map_generation;\n const int subpel_quality= c->avctx->me_subpel_quality;\n uint32_t *map= c->map;\n me_cmp_func cmpf, chroma_cmpf;\n me_cmp_func cmp_sub, chroma_cmp_sub;\n LOAD_COMMON\n int flags= c->sub_flags;\n cmpf= s->dsp.me_cmp[size];\n chroma_cmpf= s->dsp.me_cmp[size+1];\n cmp_sub= s->dsp.me_sub_cmp[size];\n chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];\n if(c->skip){\n *mx_ptr = 0;\n *my_ptr = 0;\n return dmin;\n }\n if(c->avctx->me_cmp != c->avctx->me_sub_cmp){\n dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);\n if(mx || my || size>0)\n dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;\n }\n if (mx > xmin && mx < xmax &&\n my > ymin && my < ymax) {\n int bx=4*mx, by=4*my;\n int d= dmin;\n int i, nx, ny;\n const int index= (my<<ME_MAP_SHIFT) + mx;\n const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];\n const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];\n const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];\n const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];\n const int c= score_map[(index )&(ME_MAP_SIZE-1)];\n int best[8];\n int best_pos[8][2];\n memset(best, 64, sizeof(int)*8);\n if(s->me.dia_size>=2){\n const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];\n const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];\n const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];\n const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];\n for(ny= -3; ny <= 3; ny++){\n for(nx= -3; nx <= 3; nx++){\n const int64_t t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;\n const int64_t c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;\n const int64_t b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;\n int score= (ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2 + 512)>>10;\n int i;\n if((nx&3)==0 && (ny&3)==0) continue;\n score += (mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;\n for(i=0; i<8; i++){\n if(score < best[i]){\n memmove(&best[i+1], &best[i], sizeof(int)*(7-i));\n memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));\n best[i]= score;\n best_pos[i][0]= nx + 4*mx;\n best_pos[i][1]= ny + 4*my;\n break;\n }\n }\n }\n }\n }else{\n int tl;\n const int cx = 4*(r - l);\n const int cx2= r + l - 2*c;\n const int cy = 4*(b - t);\n const int cy2= b + t - 2*c;\n int cxy;\n if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){\n tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];\n }else{\n tl= cmp(s, mx-1, my-1, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\n }\n cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;\n assert(16*cx2 + 4*cx + 32*c == 32*r);\n assert(16*cx2 - 4*cx + 32*c == 32*l);\n assert(16*cy2 + 4*cy + 32*c == 32*b);\n assert(16*cy2 - 4*cy + 32*c == 32*t);\n assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);\n for(ny= -3; ny <= 3; ny++){\n for(nx= -3; nx <= 3; nx++){\n int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c;\n int i;\n if((nx&3)==0 && (ny&3)==0) continue;\n score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;\n for(i=0; i<8; i++){\n if(score < best[i]){\n memmove(&best[i+1], &best[i], sizeof(int)*(7-i));\n memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));\n best[i]= score;\n best_pos[i][0]= nx + 4*mx;\n best_pos[i][1]= ny + 4*my;\n break;\n }\n }\n }\n }\n }\n for(i=0; i<subpel_quality; i++){\n nx= best_pos[i][0];\n ny= best_pos[i][1];\n CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)\n }\n assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);\n *mx_ptr = bx;\n *my_ptr = by;\n }else{\n *mx_ptr =4*mx;\n *my_ptr =4*my;\n }\n return dmin;\n}']
|
1,226 | 0 |
https://gitlab.com/libtiff/libtiff/blob/709e93ded0000128625a23838756a408ea30745d/tools/tiffcrop.c/#L7162
|
static int
createImageSection(uint32 sectsize, unsigned char **sect_buff_ptr)
{
unsigned char *sect_buff = NULL;
unsigned char *new_buff = NULL;
static uint32 prev_sectsize = 0;
sect_buff = *sect_buff_ptr;
if (!sect_buff)
{
sect_buff = (unsigned char *)_TIFFmalloc(sectsize);
*sect_buff_ptr = sect_buff;
_TIFFmemset(sect_buff, 0, sectsize);
}
else
{
if (prev_sectsize < sectsize)
{
new_buff = _TIFFrealloc(sect_buff, sectsize);
if (!new_buff)
{
free (sect_buff);
sect_buff = (unsigned char *)_TIFFmalloc(sectsize);
}
else
sect_buff = new_buff;
_TIFFmemset(sect_buff, 0, sectsize);
}
}
if (!sect_buff)
{
TIFFError("createImageSection", "Unable to allocate/reallocate section buffer");
return (-1);
}
prev_sectsize = sectsize;
*sect_buff_ptr = sect_buff;
return (0);
}
|
['static int\ncreateImageSection(uint32 sectsize, unsigned char **sect_buff_ptr)\n {\n unsigned char *sect_buff = NULL;\n unsigned char *new_buff = NULL;\n static uint32 prev_sectsize = 0;\n sect_buff = *sect_buff_ptr;\n if (!sect_buff)\n {\n sect_buff = (unsigned char *)_TIFFmalloc(sectsize);\n *sect_buff_ptr = sect_buff;\n _TIFFmemset(sect_buff, 0, sectsize);\n }\n else\n {\n if (prev_sectsize < sectsize)\n {\n new_buff = _TIFFrealloc(sect_buff, sectsize);\n if (!new_buff)\n {\n\tfree (sect_buff);\n sect_buff = (unsigned char *)_TIFFmalloc(sectsize);\n }\n else\n sect_buff = new_buff;\n _TIFFmemset(sect_buff, 0, sectsize);\n }\n }\n if (!sect_buff)\n {\n TIFFError("createImageSection", "Unable to allocate/reallocate section buffer");\n return (-1);\n }\n prev_sectsize = sectsize;\n *sect_buff_ptr = sect_buff;\n return (0);\n }', 'void*\n_TIFFrealloc(void* p, tmsize_t s)\n{\n\treturn (realloc(p, (size_t) s));\n}', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n\treturn (malloc((size_t) s));\n}', 'void\n_TIFFmemset(void* p, int v, tmsize_t c)\n{\n\tmemset(p, v, (size_t) c);\n}']
|
1,227 | 0 |
https://gitlab.com/libtiff/libtiff/blob/5b06ac3f2851cf84ec425f1a0c3ddcf954e625aa/tools/tiff2pdf.c/#L2021
|
void t2p_read_tiff_size_tile(T2P* t2p, TIFF* input, ttile_t tile){
uint64* tbc = NULL;
uint16 edge=0;
#ifdef JPEG_SUPPORT
unsigned char* jpt;
#endif
uint64 k;
edge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile);
edge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile);
if(t2p->pdf_transcode==T2P_TRANSCODE_RAW){
if(edge
#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT)
&& !(t2p->pdf_compression==T2P_COMPRESS_JPEG)
#endif
){
t2p->tiff_datasize=TIFFTileSize(input);
if (t2p->tiff_datasize == 0) {
t2p->t2p_error = T2P_ERR_ERROR;
}
return;
} else {
TIFFGetField(input, TIFFTAG_TILEBYTECOUNTS, &tbc);
k=tbc[tile];
#ifdef OJPEG_SUPPORT
if(t2p->tiff_compression==COMPRESSION_OJPEG){
k = checkAdd64(k, 2048, t2p);
}
#endif
#ifdef JPEG_SUPPORT
if(t2p->tiff_compression==COMPRESSION_JPEG) {
uint32 count = 0;
if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt)!=0){
if(count > 4){
k = checkAdd64(k, count, t2p);
k -= 2;
}
}
}
#endif
t2p->tiff_datasize = (tsize_t) k;
if ((uint64) t2p->tiff_datasize != k) {
TIFFError(TIFF2PDF_MODULE, "Integer overflow");
t2p->t2p_error = T2P_ERR_ERROR;
}
return;
}
}
k = TIFFTileSize(input);
if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){
k = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p);
}
if (k == 0) {
t2p->t2p_error = T2P_ERR_ERROR;
}
t2p->tiff_datasize = (tsize_t) k;
if ((uint64) t2p->tiff_datasize != k) {
TIFFError(TIFF2PDF_MODULE, "Integer overflow");
t2p->t2p_error = T2P_ERR_ERROR;
}
return;
}
|
['void t2p_read_tiff_size_tile(T2P* t2p, TIFF* input, ttile_t tile){\n\tuint64* tbc = NULL;\n\tuint16 edge=0;\n#ifdef JPEG_SUPPORT\n\tunsigned char* jpt;\n#endif\n uint64 k;\n\tedge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile);\n\tedge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile);\n\tif(t2p->pdf_transcode==T2P_TRANSCODE_RAW){\n\t\tif(edge\n#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT)\n\t\t&& !(t2p->pdf_compression==T2P_COMPRESS_JPEG)\n#endif\n\t\t){\n\t\t\tt2p->tiff_datasize=TIFFTileSize(input);\n\t\t\tif (t2p->tiff_datasize == 0) {\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t}\n\t\t\treturn;\n\t\t} else {\n\t\t\tTIFFGetField(input, TIFFTAG_TILEBYTECOUNTS, &tbc);\n\t\t\tk=tbc[tile];\n#ifdef OJPEG_SUPPORT\n\t\t\tif(t2p->tiff_compression==COMPRESSION_OJPEG){\n\t\t\t \tk = checkAdd64(k, 2048, t2p);\n\t\t\t}\n#endif\n#ifdef JPEG_SUPPORT\n\t\t\tif(t2p->tiff_compression==COMPRESSION_JPEG) {\n\t\t\t\tuint32 count = 0;\n\t\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt)!=0){\n\t\t\t\t\tif(count > 4){\n\t\t\t\t\t\tk = checkAdd64(k, count, t2p);\n\t\t\t\t\t\tk -= 2;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n#endif\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\t}\n\tk = TIFFTileSize(input);\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}', 'int t2p_tile_is_right_edge(T2P_TILES tiles, ttile_t tile){\n\tif( ((tile+1) % tiles.tiles_tilecountx == 0)\n\t\t&& (tiles.tiles_edgetilewidth != 0) ){\n\t\treturn(1);\n\t} else {\n\t\treturn(0);\n\t}\n}', 'int t2p_tile_is_bottom_edge(T2P_TILES tiles, ttile_t tile){\n\tif( ((tile+1) > (tiles.tiles_tilecount-tiles.tiles_tilecountx) )\n\t\t&& (tiles.tiles_edgetilelength != 0) ){\n\t\treturn(1);\n\t} else {\n\t\treturn(0);\n\t}\n}', 'int\nTIFFGetField(TIFF* tif, uint32 tag, ...)\n{\n\tint status;\n\tva_list ap;\n\tva_start(ap, tag);\n\tstatus = TIFFVGetField(tif, tag, ap);\n\tva_end(ap);\n\treturn (status);\n}']
|
1,228 | 0 |
https://github.com/openssl/openssl/blob/6fda11ae5a06e28fd9463e5afb60735d074904b3/providers/common/ciphers/aes.c/#L301
|
IMPLEMENT_new_ctx(cfb, CFB, 128)
|
['IMPLEMENT_new_ctx(cfb, CFB, 128)', '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#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE)\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
|
1,229 | 0 |
https://github.com/openssl/openssl/blob/9b340281873643d2b8a33047dc8bfa607f7e0c3c/crypto/lhash/lhash.c/#L191
|
static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
OPENSSL_LH_DOALL_FUNC func,
OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
{
int i;
OPENSSL_LH_NODE *a, *n;
if (lh == NULL)
return;
for (i = lh->num_nodes - 1; i >= 0; i--) {
a = lh->b[i];
while (a != NULL) {
n = a->next;
if (use_arg)
func_arg(a->data, arg);
else
func(a->data);
a = n;
}
}
}
|
['static int rev_body(int s, int stype, int prot, unsigned char *context)\n{\n char *buf = NULL;\n int i;\n int ret = 1;\n SSL *con;\n BIO *io, *ssl_bio, *sbio;\n buf = app_malloc(bufsize, "server rev buffer");\n io = BIO_new(BIO_f_buffer());\n ssl_bio = BIO_new(BIO_f_ssl());\n if ((io == NULL) || (ssl_bio == NULL))\n goto err;\n if (!BIO_set_write_buffer_size(io, bufsize))\n goto err;\n if ((con = SSL_new(ctx)) == NULL)\n goto err;\n if (s_tlsextdebug) {\n SSL_set_tlsext_debug_callback(con, tlsext_cb);\n SSL_set_tlsext_debug_arg(con, bio_s_out);\n }\n if (context != NULL\n && !SSL_set_session_id_context(con, context,\n strlen((char *)context))) {\n SSL_free(con);\n ERR_print_errors(bio_err);\n goto err;\n }\n sbio = BIO_new_socket(s, BIO_NOCLOSE);\n SSL_set_bio(con, sbio, sbio);\n SSL_set_accept_state(con);\n BIO_set_ssl(ssl_bio, con, BIO_CLOSE);\n BIO_push(io, ssl_bio);\n#ifdef CHARSET_EBCDIC\n io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);\n#endif\n if (s_debug) {\n BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);\n BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);\n }\n if (s_msg) {\n#ifndef OPENSSL_NO_SSL_TRACE\n if (s_msg == 2)\n SSL_set_msg_callback(con, SSL_trace);\n else\n#endif\n SSL_set_msg_callback(con, msg_cb);\n SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);\n }\n for (;;) {\n i = BIO_do_handshake(io);\n if (i > 0)\n break;\n if (!BIO_should_retry(io)) {\n BIO_puts(bio_err, "CONNECTION FAILURE\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n#ifndef OPENSSL_NO_SRP\n if (BIO_should_io_special(io)\n && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {\n BIO_printf(bio_s_out, "LOOKUP renego during accept\\n");\n SRP_user_pwd_free(srp_callback_parm.user);\n srp_callback_parm.user =\n SRP_VBASE_get1_by_user(srp_callback_parm.vb,\n srp_callback_parm.login);\n if (srp_callback_parm.user)\n BIO_printf(bio_s_out, "LOOKUP done %s\\n",\n srp_callback_parm.user->info);\n else\n BIO_printf(bio_s_out, "LOOKUP not successful\\n");\n continue;\n }\n#endif\n }\n BIO_printf(bio_err, "CONNECTION ESTABLISHED\\n");\n print_ssl_summary(con);\n for (;;) {\n i = BIO_gets(io, buf, bufsize - 1);\n if (i < 0) {\n if (!BIO_should_retry(io)) {\n if (!s_quiet)\n ERR_print_errors(bio_err);\n goto err;\n } else {\n BIO_printf(bio_s_out, "read R BLOCK\\n");\n#ifndef OPENSSL_NO_SRP\n if (BIO_should_io_special(io)\n && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {\n BIO_printf(bio_s_out, "LOOKUP renego during read\\n");\n SRP_user_pwd_free(srp_callback_parm.user);\n srp_callback_parm.user =\n SRP_VBASE_get1_by_user(srp_callback_parm.vb,\n srp_callback_parm.login);\n if (srp_callback_parm.user)\n BIO_printf(bio_s_out, "LOOKUP done %s\\n",\n srp_callback_parm.user->info);\n else\n BIO_printf(bio_s_out, "LOOKUP not successful\\n");\n continue;\n }\n#endif\n#if !defined(OPENSSL_SYS_MSDOS)\n sleep(1);\n#endif\n continue;\n }\n } else if (i == 0) {\n ret = 1;\n BIO_printf(bio_err, "CONNECTION CLOSED\\n");\n goto end;\n } else {\n char *p = buf + i - 1;\n while (i && (*p == \'\\n\' || *p == \'\\r\')) {\n p--;\n i--;\n }\n if (!s_ign_eof && (i == 5) && (strncmp(buf, "CLOSE", 5) == 0)) {\n ret = 1;\n BIO_printf(bio_err, "CONNECTION CLOSED\\n");\n goto end;\n }\n BUF_reverse((unsigned char *)buf, NULL, i);\n buf[i] = \'\\n\';\n BIO_write(io, buf, i + 1);\n for (;;) {\n i = BIO_flush(io);\n if (i > 0)\n break;\n if (!BIO_should_retry(io))\n goto end;\n }\n }\n }\n end:\n SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);\n err:\n OPENSSL_free(buf);\n BIO_free_all(io);\n return ret;\n}', 'SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return NULL;\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return NULL;\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->references = 1;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n OPENSSL_free(s);\n s = NULL;\n goto err;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->max_early_data = ctx->max_early_data;\n s->recv_max_early_data = ctx->recv_max_early_data;\n s->num_tickets = ctx->num_tickets;\n s->pha_enabled = ctx->pha_enabled;\n s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);\n if (s->tls13_ciphersuites == NULL)\n goto err;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->record_padding_cb = ctx->record_padding_cb;\n s->record_padding_arg = ctx->record_padding_arg;\n s->block_padding = ctx->block_padding;\n s->sid_ctx_length = ctx->sid_ctx_length;\n if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))\n goto err;\n memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));\n s->verify_callback = ctx->default_verify_callback;\n s->generate_session_id = ctx->generate_session_id;\n s->param = X509_VERIFY_PARAM_new();\n if (s->param == NULL)\n goto err;\n X509_VERIFY_PARAM_inherit(s->param, ctx->param);\n s->quiet_shutdown = ctx->quiet_shutdown;\n s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;\n s->max_send_fragment = ctx->max_send_fragment;\n s->split_send_fragment = ctx->split_send_fragment;\n s->max_pipelines = ctx->max_pipelines;\n if (s->max_pipelines > 1)\n RECORD_LAYER_set_read_ahead(&s->rlayer, 1);\n if (ctx->default_read_buf_len > 0)\n SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);\n SSL_CTX_up_ref(ctx);\n s->ctx = ctx;\n s->ext.debug_cb = 0;\n s->ext.debug_arg = NULL;\n s->ext.ticket_expected = 0;\n s->ext.status_type = ctx->ext.status_type;\n s->ext.status_expected = 0;\n s->ext.ocsp.ids = NULL;\n s->ext.ocsp.exts = NULL;\n s->ext.ocsp.resp = NULL;\n s->ext.ocsp.resp_len = 0;\n SSL_CTX_up_ref(ctx);\n s->session_ctx = ctx;\n#ifndef OPENSSL_NO_EC\n if (ctx->ext.ecpointformats) {\n s->ext.ecpointformats =\n OPENSSL_memdup(ctx->ext.ecpointformats,\n ctx->ext.ecpointformats_len);\n if (!s->ext.ecpointformats)\n goto err;\n s->ext.ecpointformats_len =\n ctx->ext.ecpointformats_len;\n }\n if (ctx->ext.supportedgroups) {\n s->ext.supportedgroups =\n OPENSSL_memdup(ctx->ext.supportedgroups,\n ctx->ext.supportedgroups_len\n * sizeof(*ctx->ext.supportedgroups));\n if (!s->ext.supportedgroups)\n goto err;\n s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n s->ext.npn = NULL;\n#endif\n if (s->ctx->ext.alpn) {\n s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);\n if (s->ext.alpn == NULL)\n goto err;\n memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);\n s->ext.alpn_len = s->ctx->ext.alpn_len;\n }\n s->verified_chain = NULL;\n s->verify_result = X509_V_OK;\n s->default_passwd_callback = ctx->default_passwd_callback;\n s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;\n s->method = ctx->method;\n s->key_update = SSL_KEY_UPDATE_NONE;\n s->allow_early_data_cb = ctx->allow_early_data_cb;\n s->allow_early_data_cb_data = ctx->allow_early_data_cb_data;\n if (!s->method->ssl_new(s))\n goto err;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))\n goto err;\n#ifndef OPENSSL_NO_PSK\n s->psk_client_callback = ctx->psk_client_callback;\n s->psk_server_callback = ctx->psk_server_callback;\n#endif\n s->psk_find_session_cb = ctx->psk_find_session_cb;\n s->psk_use_session_cb = ctx->psk_use_session_cb;\n s->job = NULL;\n#ifndef OPENSSL_NO_CT\n if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,\n ctx->ct_validation_callback_arg))\n goto err;\n#endif\n return s;\n err:\n SSL_free(s);\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n}', 'void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n CRYPTO_DOWN_REF(&s->references, &i, s->lock);\n REF_PRINT_COUNT("SSL", s);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(s->param);\n dane_final(&s->dane);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n RECORD_LAYER_release(&s->rlayer);\n ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n s->wbio = NULL;\n BIO_free_all(s->rbio);\n s->rbio = NULL;\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n sk_SSL_CIPHER_free(s->tls13_ciphersuites);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n SSL_SESSION_free(s->psksession);\n OPENSSL_free(s->psksession_id);\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->ext.hostname);\n SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->ext.ecpointformats);\n OPENSSL_free(s->ext.supportedgroups);\n#endif\n sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->ext.scts);\n#endif\n OPENSSL_free(s->ext.ocsp.resp);\n OPENSSL_free(s->ext.alpn);\n OPENSSL_free(s->ext.tls13_cookie);\n OPENSSL_free(s->clienthello);\n OPENSSL_free(s->pha_context);\n EVP_MD_CTX_free(s->pha_dgst);\n sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);\n sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free);\n sk_X509_pop_free(s->verified_chain, X509_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->ext.npn);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', 'void SSL_CTX_free(SSL_CTX *a)\n{\n int i;\n if (a == NULL)\n return;\n CRYPTO_DOWN_REF(&a->references, &i, a->lock);\n REF_PRINT_COUNT("SSL_CTX", a);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(a->param);\n dane_ctx_final(&a->dane);\n if (a->sessions != NULL)\n SSL_CTX_flush_sessions(a, 0);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);\n lh_SSL_SESSION_free(a->sessions);\n X509_STORE_free(a->cert_store);\n#ifndef OPENSSL_NO_CT\n CTLOG_STORE_free(a->ctlog_store);\n#endif\n sk_SSL_CIPHER_free(a->cipher_list);\n sk_SSL_CIPHER_free(a->cipher_list_by_id);\n sk_SSL_CIPHER_free(a->tls13_ciphersuites);\n ssl_cert_free(a->cert);\n sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);\n sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);\n sk_X509_pop_free(a->extra_certs, X509_free);\n a->comp_methods = NULL;\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);\n#endif\n#ifndef OPENSSL_NO_SRP\n SSL_CTX_SRP_CTX_free(a);\n#endif\n#ifndef OPENSSL_NO_ENGINE\n ENGINE_finish(a->client_cert_engine);\n#endif\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(a->ext.ecpointformats);\n OPENSSL_free(a->ext.supportedgroups);\n#endif\n OPENSSL_free(a->ext.alpn);\n OPENSSL_secure_free(a->ext.secure);\n CRYPTO_THREAD_lock_free(a->lock);\n OPENSSL_free(a);\n}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n{\n unsigned long i;\n TIMEOUT_PARAM tp;\n tp.ctx = s;\n tp.cache = s->sessions;\n if (tp.cache == NULL)\n return;\n tp.time = t;\n CRYPTO_THREAD_write_lock(s->lock);\n i = lh_SSL_SESSION_get_down_load(s->sessions);\n lh_SSL_SESSION_set_down_load(s->sessions, 0);\n lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);\n lh_SSL_SESSION_set_down_load(s->sessions, i);\n CRYPTO_THREAD_unlock(s->lock);\n}', 'IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM)', 'void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)\n{\n doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);\n}', 'static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,\n OPENSSL_LH_DOALL_FUNC func,\n OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)\n{\n int i;\n OPENSSL_LH_NODE *a, *n;\n if (lh == NULL)\n return;\n for (i = lh->num_nodes - 1; i >= 0; i--) {\n a = lh->b[i];\n while (a != NULL) {\n n = a->next;\n if (use_arg)\n func_arg(a->data, arg);\n else\n func(a->data);\n a = n;\n }\n }\n}']
|
1,230 | 0 |
https://github.com/openssl/openssl/blob/2d5d70b15559f9813054ddb11b30b816daf62ebe/crypto/bn/bn_ctx.c/#L332
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,\n EC_POINT *points[], BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp, *tmp_Z;\n BIGNUM **prod_Z = NULL;\n size_t i;\n int ret = 0;\n if (num == 0)\n return 1;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n tmp_Z = BN_CTX_get(ctx);\n if (tmp == NULL || tmp_Z == NULL)\n goto err;\n prod_Z = OPENSSL_malloc(num * sizeof prod_Z[0]);\n if (prod_Z == NULL)\n goto err;\n for (i = 0; i < num; i++) {\n prod_Z[i] = BN_new();\n if (prod_Z[i] == NULL)\n goto err;\n }\n if (!BN_is_zero(points[0]->Z)) {\n if (!BN_copy(prod_Z[0], points[0]->Z))\n goto err;\n } else {\n if (group->meth->field_set_to_one != 0) {\n if (!group->meth->field_set_to_one(group, prod_Z[0], ctx))\n goto err;\n } else {\n if (!BN_one(prod_Z[0]))\n goto err;\n }\n }\n for (i = 1; i < num; i++) {\n if (!BN_is_zero(points[i]->Z)) {\n if (!group->\n meth->field_mul(group, prod_Z[i], prod_Z[i - 1], points[i]->Z,\n ctx))\n goto err;\n } else {\n if (!BN_copy(prod_Z[i], prod_Z[i - 1]))\n goto err;\n }\n }\n if (!BN_mod_inverse(tmp, prod_Z[num - 1], group->field, ctx)) {\n ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);\n goto err;\n }\n if (group->meth->field_encode != 0) {\n if (!group->meth->field_encode(group, tmp, tmp, ctx))\n goto err;\n if (!group->meth->field_encode(group, tmp, tmp, ctx))\n goto err;\n }\n for (i = num - 1; i > 0; --i) {\n if (!BN_is_zero(points[i]->Z)) {\n if (!group->\n meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp, tmp, points[i]->Z, ctx))\n goto err;\n if (!BN_copy(points[i]->Z, tmp_Z))\n goto err;\n }\n }\n if (!BN_is_zero(points[0]->Z)) {\n if (!BN_copy(points[0]->Z, tmp))\n goto err;\n }\n for (i = 0; i < num; i++) {\n EC_POINT *p = points[i];\n if (!BN_is_zero(p->Z)) {\n if (!group->meth->field_sqr(group, tmp, p->Z, ctx))\n goto err;\n if (!group->meth->field_mul(group, p->X, p->X, tmp, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp, tmp, p->Z, ctx))\n goto err;\n if (!group->meth->field_mul(group, p->Y, p->Y, tmp, ctx))\n goto err;\n if (group->meth->field_set_to_one != 0) {\n if (!group->meth->field_set_to_one(group, p->Z, ctx))\n goto err;\n } else {\n if (!BN_one(p->Z))\n goto err;\n }\n p->Z_is_one = 1;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n if (prod_Z != NULL) {\n for (i = 0; i < num; i++) {\n if (prod_Z[i] == NULL)\n break;\n BN_clear_free(prod_Z[i]);\n }\n OPENSSL_free(prod_Z);\n }\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048))) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM local_A, local_B;\n BIGNUM *pA, *pB;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n pB = &local_B;\n BN_with_flags(pB, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, pB, A, ctx))\n goto err;\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n pA = &local_A;\n BN_with_flags(pA, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, pA, B, ctx))\n goto err;\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
1,231 | 0 |
https://github.com/libav/libav/blob/490a022d86ef1c506a79744c5a95368af356fc69/libavcodec/imgconvert.c/#L1008
|
static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
int width, int height)
{
uint8_t *src_m1, *src_0, *src_p1, *src_p2;
int y;
uint8_t *buf;
buf = (uint8_t*)av_malloc(width);
src_m1 = src1;
memcpy(buf,src_m1,width);
src_0=&src_m1[src_wrap];
src_p1=&src_0[src_wrap];
src_p2=&src_p1[src_wrap];
for(y=0;y<(height-2);y+=2) {
deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
src_m1 = src_p1;
src_0 = src_p2;
src_p1 += 2*src_wrap;
src_p2 += 2*src_wrap;
}
deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
av_free(buf);
}
|
['static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,\n int width, int height)\n{\n uint8_t *src_m1, *src_0, *src_p1, *src_p2;\n int y;\n uint8_t *buf;\n buf = (uint8_t*)av_malloc(width);\n src_m1 = src1;\n memcpy(buf,src_m1,width);\n src_0=&src_m1[src_wrap];\n src_p1=&src_0[src_wrap];\n src_p2=&src_p1[src_wrap];\n for(y=0;y<(height-2);y+=2) {\n deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);\n src_m1 = src_p1;\n src_0 = src_p2;\n src_p1 += 2*src_wrap;\n src_p2 += 2*src_wrap;\n }\n deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);\n av_free(buf);\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
|
1,232 | 0 |
https://github.com/libav/libav/blob/60a42ef44cc02a8b6a0f0b92ab92c6f9a4ddc838/libavformat/mpegts.c/#L680
|
static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)
{
GetBitContext gb;
int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
int dts_flag = -1, cts_flag = -1;
int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
init_get_bits(&gb, buf, buf_size*8);
if (sl->use_au_start)
au_start_flag = get_bits1(&gb);
if (sl->use_au_end)
au_end_flag = get_bits1(&gb);
if (!sl->use_au_start && !sl->use_au_end)
au_start_flag = au_end_flag = 1;
if (sl->ocr_len > 0)
ocr_flag = get_bits1(&gb);
if (sl->use_idle)
idle_flag = get_bits1(&gb);
if (sl->use_padding)
padding_flag = get_bits1(&gb);
if (padding_flag)
padding_bits = get_bits(&gb, 3);
if (!idle_flag && (!padding_flag || padding_bits != 0)) {
if (sl->packet_seq_num_len)
skip_bits_long(&gb, sl->packet_seq_num_len);
if (sl->degr_prior_len)
if (get_bits1(&gb))
skip_bits(&gb, sl->degr_prior_len);
if (ocr_flag)
skip_bits_long(&gb, sl->ocr_len);
if (au_start_flag) {
if (sl->use_rand_acc_pt)
get_bits1(&gb);
if (sl->au_seq_num_len > 0)
skip_bits_long(&gb, sl->au_seq_num_len);
if (sl->use_timestamps) {
dts_flag = get_bits1(&gb);
cts_flag = get_bits1(&gb);
}
}
if (sl->inst_bitrate_len)
inst_bitrate_flag = get_bits1(&gb);
if (dts_flag == 1)
dts = get_bits64(&gb, sl->timestamp_len);
if (cts_flag == 1)
cts = get_bits64(&gb, sl->timestamp_len);
if (sl->au_len > 0)
skip_bits_long(&gb, sl->au_len);
if (inst_bitrate_flag)
skip_bits_long(&gb, sl->inst_bitrate_len);
}
if (dts != AV_NOPTS_VALUE)
pes->dts = dts;
if (cts != AV_NOPTS_VALUE)
pes->pts = cts;
if (sl->timestamp_len && sl->timestamp_res)
avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);
return (get_bits_count(&gb) + 7) >> 3;
}
|
['static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)\n{\n GetBitContext gb;\n int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;\n int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;\n int dts_flag = -1, cts_flag = -1;\n int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;\n init_get_bits(&gb, buf, buf_size*8);\n if (sl->use_au_start)\n au_start_flag = get_bits1(&gb);\n if (sl->use_au_end)\n au_end_flag = get_bits1(&gb);\n if (!sl->use_au_start && !sl->use_au_end)\n au_start_flag = au_end_flag = 1;\n if (sl->ocr_len > 0)\n ocr_flag = get_bits1(&gb);\n if (sl->use_idle)\n idle_flag = get_bits1(&gb);\n if (sl->use_padding)\n padding_flag = get_bits1(&gb);\n if (padding_flag)\n padding_bits = get_bits(&gb, 3);\n if (!idle_flag && (!padding_flag || padding_bits != 0)) {\n if (sl->packet_seq_num_len)\n skip_bits_long(&gb, sl->packet_seq_num_len);\n if (sl->degr_prior_len)\n if (get_bits1(&gb))\n skip_bits(&gb, sl->degr_prior_len);\n if (ocr_flag)\n skip_bits_long(&gb, sl->ocr_len);\n if (au_start_flag) {\n if (sl->use_rand_acc_pt)\n get_bits1(&gb);\n if (sl->au_seq_num_len > 0)\n skip_bits_long(&gb, sl->au_seq_num_len);\n if (sl->use_timestamps) {\n dts_flag = get_bits1(&gb);\n cts_flag = get_bits1(&gb);\n }\n }\n if (sl->inst_bitrate_len)\n inst_bitrate_flag = get_bits1(&gb);\n if (dts_flag == 1)\n dts = get_bits64(&gb, sl->timestamp_len);\n if (cts_flag == 1)\n cts = get_bits64(&gb, sl->timestamp_len);\n if (sl->au_len > 0)\n skip_bits_long(&gb, sl->au_len);\n if (inst_bitrate_flag)\n skip_bits_long(&gb, sl->inst_bitrate_len);\n }\n if (dts != AV_NOPTS_VALUE)\n pes->dts = dts;\n if (cts != AV_NOPTS_VALUE)\n pes->pts = cts;\n if (sl->timestamp_len && sl->timestamp_res)\n avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);\n return (get_bits_count(&gb) + 7) >> 3;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size;\n int ret = 0;\n if (bit_size > INT_MAX - 7 || bit_size <= 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n ret = AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n return ret;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}']
|
1,233 | 0 |
https://github.com/openssl/openssl/blob/0ca6d7c6b1e73d17ca67b7ffd8435bde43bf50af/ssl/s3_clnt.c/#L1645
|
static int ssl3_check_cert_and_algorithm(SSL *s)
{
int i,idx;
long algs;
EVP_PKEY *pkey=NULL;
SESS_CERT *sc;
#ifndef NO_RSA
RSA *rsa;
#endif
#ifndef NO_DH
DH *dh;
#endif
sc=s->session->sess_cert;
if (sc == NULL)
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_INTERNAL_ERROR);
goto err;
}
algs=s->s3->tmp.new_cipher->algorithms;
if (algs & (SSL_aDH|SSL_aNULL))
return(1);
#ifndef NO_RSA
rsa=s->session->sess_cert->peer_rsa_tmp;
#endif
#ifndef NO_DH
dh=s->session->sess_cert->peer_dh_tmp;
#endif
idx=sc->peer_cert_type;
pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
EVP_PKEY_free(pkey);
if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
goto f_err;
}
#ifndef NO_DSA
else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
goto f_err;
}
#endif
#ifndef NO_RSA
if ((algs & SSL_kRSA) &&
!(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
goto f_err;
}
#endif
#ifndef NO_DH
if ((algs & SSL_kEDH) &&
!(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
goto f_err;
}
else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
goto f_err;
}
#ifndef NO_DSA
else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
goto f_err;
}
#endif
#endif
if (SSL_IS_EXPORT(algs) && !has_bits(i,EVP_PKT_EXP))
{
#ifndef NO_RSA
if (algs & SSL_kRSA)
{
if (rsa == NULL
|| RSA_size(rsa) > SSL_EXPORT_PKEYLENGTH(algs))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
goto f_err;
}
}
else
#endif
#ifndef NO_DH
if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
{
if (dh == NULL
|| DH_size(dh) > SSL_EXPORT_PKEYLENGTH(algs))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
goto f_err;
}
}
else
#endif
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
goto f_err;
}
}
return(1);
f_err:
ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
err:
return(0);
}
|
['static int ssl3_check_cert_and_algorithm(SSL *s)\n\t{\n\tint i,idx;\n\tlong algs;\n\tEVP_PKEY *pkey=NULL;\n\tSESS_CERT *sc;\n#ifndef NO_RSA\n\tRSA *rsa;\n#endif\n#ifndef NO_DH\n\tDH *dh;\n#endif\n\tsc=s->session->sess_cert;\n\tif (sc == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_INTERNAL_ERROR);\n\t\tgoto err;\n\t\t}\n\talgs=s->s3->tmp.new_cipher->algorithms;\n\tif (algs & (SSL_aDH|SSL_aNULL))\n\t\treturn(1);\n#ifndef NO_RSA\n\trsa=s->session->sess_cert->peer_rsa_tmp;\n#endif\n#ifndef NO_DH\n\tdh=s->session->sess_cert->peer_dh_tmp;\n#endif\n\tidx=sc->peer_cert_type;\n\tpkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);\n\ti=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);\n\tEVP_PKEY_free(pkey);\n\tif ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);\n\t\tgoto f_err;\n\t\t}\n#ifndef NO_DSA\n\telse if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);\n\t\tgoto f_err;\n\t\t}\n#endif\n#ifndef NO_RSA\n\tif ((algs & SSL_kRSA) &&\n\t\t!(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);\n\t\tgoto f_err;\n\t\t}\n#endif\n#ifndef NO_DH\n\tif ((algs & SSL_kEDH) &&\n\t\t!(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);\n\t\tgoto f_err;\n\t\t}\n\telse if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);\n\t\tgoto f_err;\n\t\t}\n#ifndef NO_DSA\n\telse if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);\n\t\tgoto f_err;\n\t\t}\n#endif\n#endif\n\tif (SSL_IS_EXPORT(algs) && !has_bits(i,EVP_PKT_EXP))\n\t\t{\n#ifndef NO_RSA\n\t\tif (algs & SSL_kRSA)\n\t\t\t{\n\t\t\tif (rsa == NULL\n\t\t\t || RSA_size(rsa) > SSL_EXPORT_PKEYLENGTH(algs))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n#ifndef NO_DH\n\t\t\tif (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))\n\t\t\t {\n\t\t\t if (dh == NULL\n\t\t\t\t|| DH_size(dh) > SSL_EXPORT_PKEYLENGTH(algs))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);\nerr:\n\treturn(0);\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}', 'void EVP_PKEY_free(EVP_PKEY *x)\n\t{\n\tint i;\n\tif (x == NULL) return;\n\ti=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_EVP_PKEY);\n#ifdef REF_PRINT\n\tREF_PRINT("EVP_PKEY",x);\n#endif\n\tif (i > 0) return;\n#ifdef REF_CHECK\n\tif (i < 0)\n\t\t{\n\t\tfprintf(stderr,"EVP_PKEY_free, bad reference count\\n");\n\t\tabort();\n\t\t}\n#endif\n\tEVP_PKEY_free_it(x);\n\tFree((char *)x);\n\t}', 'int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,\n\t int line)\n\t{\n\tint ret;\n\tif (add_lock_callback != NULL)\n\t\t{\n#ifdef LOCK_DEBUG\n\t\tint before= *pointer;\n#endif\n\t\tret=add_lock_callback(pointer,amount,type,file,line);\n#ifdef LOCK_DEBUG\n\t\tfprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n\t\t\tCRYPTO_thread_id(),\n\t\t\tbefore,amount,ret,\n\t\t\tCRYPTO_get_lock_name(type),\n\t\t\tfile,line);\n#endif\n\t\t*pointer=ret;\n\t\t}\n\telse\n\t\t{\n\t\tCRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,file,line);\n\t\tret= *pointer+amount;\n#ifdef LOCK_DEBUG\n\t\tfprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n\t\t\tCRYPTO_thread_id(),\n\t\t\t*pointer,amount,ret,\n\t\t\tCRYPTO_get_lock_name(type),\n\t\t\tfile,line);\n#endif\n\t\t*pointer=ret;\n\t\tCRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,file,line);\n\t\t}\n\treturn(ret);\n\t}', 'void CRYPTO_lock(int mode, int type, const char *file, int line)\n\t{\n#ifdef LOCK_DEBUG\n\t\t{\n\t\tchar *rw_text,*operation_text;\n\t\tif (mode & CRYPTO_LOCK)\n\t\t\toperation_text="lock ";\n\t\telse if (mode & CRYPTO_UNLOCK)\n\t\t\toperation_text="unlock";\n\t\telse\n\t\t\toperation_text="ERROR ";\n\t\tif (mode & CRYPTO_READ)\n\t\t\trw_text="r";\n\t\telse if (mode & CRYPTO_WRITE)\n\t\t\trw_text="w";\n\t\telse\n\t\t\trw_text="ERROR";\n\t\tfprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\\n",\n\t\t\tCRYPTO_thread_id(), rw_text, operation_text,\n\t\t\tCRYPTO_get_lock_name(type), file, line);\n\t\t}\n#endif\n\tif (locking_callback != NULL)\n\t\tlocking_callback(mode,type,file,line);\n\t}']
|
1,234 | 0 |
https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/crypto/bn/bn_lib.c/#L232
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['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_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 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 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}', '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}']
|
1,235 | 0 |
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/motion_est_template.c/#L888
|
static int sab_diamond_search(MpegEncContext * s, int *best, int dmin,
int src_index, int ref_index, int const penalty_factor,
int size, int h, int flags)
{
MotionEstContext * const c= &s->me;
me_cmp_func cmpf, chroma_cmpf;
Minima minima[MAX_SAB_SIZE];
const int minima_count= FFABS(c->dia_size);
int i, j;
LOAD_COMMON
LOAD_COMMON2
int map_generation= c->map_generation;
cmpf= s->dsp.me_cmp[size];
chroma_cmpf= s->dsp.me_cmp[size+1];
for(j=i=0; i<ME_MAP_SIZE && j<MAX_SAB_SIZE; i++){
uint32_t key= map[i];
key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
minima[j].height= score_map[i];
minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
if( minima[j].x > xmax || minima[j].x < xmin
|| minima[j].y > ymax || minima[j].y < ymin)
continue;
minima[j].checked=0;
if(minima[j].x || minima[j].y)
minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
j++;
}
qsort(minima, j, sizeof(Minima), minima_cmp);
for(; j<minima_count; j++){
minima[j].height=256*256*256*64;
minima[j].checked=0;
minima[j].x= minima[j].y=0;
}
for(i=0; i<minima_count; i++){
const int x= minima[i].x;
const int y= minima[i].y;
int d;
if(minima[i].checked) continue;
if( x >= xmax || x <= xmin
|| y >= ymax || y <= ymin)
continue;
SAB_CHECK_MV(x-1, y)
SAB_CHECK_MV(x+1, y)
SAB_CHECK_MV(x , y-1)
SAB_CHECK_MV(x , y+1)
minima[i].checked= 1;
}
best[0]= minima[0].x;
best[1]= minima[0].y;
dmin= minima[0].height;
if( best[0] < xmax && best[0] > xmin
&& best[1] < ymax && best[1] > ymin){
int d;
CHECK_MV(best[0]-1, best[1])
CHECK_MV(best[0]+1, best[1])
CHECK_MV(best[0], best[1]-1)
CHECK_MV(best[0], best[1]+1)
}
return dmin;
}
|
['static int sab_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{\n MotionEstContext * const c= &s->me;\n me_cmp_func cmpf, chroma_cmpf;\n Minima minima[MAX_SAB_SIZE];\n const int minima_count= FFABS(c->dia_size);\n int i, j;\n LOAD_COMMON\n LOAD_COMMON2\n int map_generation= c->map_generation;\n cmpf= s->dsp.me_cmp[size];\n chroma_cmpf= s->dsp.me_cmp[size+1];\n for(j=i=0; i<ME_MAP_SIZE && j<MAX_SAB_SIZE; i++){\n uint32_t key= map[i];\n key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));\n if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;\n minima[j].height= score_map[i];\n minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;\n minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);\n minima[j].x-= (1<<(ME_MAP_MV_BITS-1));\n minima[j].y-= (1<<(ME_MAP_MV_BITS-1));\n if( minima[j].x > xmax || minima[j].x < xmin\n || minima[j].y > ymax || minima[j].y < ymin)\n continue;\n minima[j].checked=0;\n if(minima[j].x || minima[j].y)\n minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;\n j++;\n }\n qsort(minima, j, sizeof(Minima), minima_cmp);\n for(; j<minima_count; j++){\n minima[j].height=256*256*256*64;\n minima[j].checked=0;\n minima[j].x= minima[j].y=0;\n }\n for(i=0; i<minima_count; i++){\n const int x= minima[i].x;\n const int y= minima[i].y;\n int d;\n if(minima[i].checked) continue;\n if( x >= xmax || x <= xmin\n || y >= ymax || y <= ymin)\n continue;\n SAB_CHECK_MV(x-1, y)\n SAB_CHECK_MV(x+1, y)\n SAB_CHECK_MV(x , y-1)\n SAB_CHECK_MV(x , y+1)\n minima[i].checked= 1;\n }\n best[0]= minima[0].x;\n best[1]= minima[0].y;\n dmin= minima[0].height;\n if( best[0] < xmax && best[0] > xmin\n && best[1] < ymax && best[1] > ymin){\n int d;\n CHECK_MV(best[0]-1, best[1])\n CHECK_MV(best[0]+1, best[1])\n CHECK_MV(best[0], best[1]-1)\n CHECK_MV(best[0], best[1]+1)\n }\n return dmin;\n}']
|
1,236 | 0 |
https://github.com/openssl/openssl/blob/8f58ede09572dcc6a7e6c01280dd348240199568/test/exdatatest.c/#L138
|
static MYOBJ *MYOBJ_new(void)
{
static int count = 0;
MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
obj->id = ++count;
obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
return obj;
}
|
['static MYOBJ *MYOBJ_new(void)\n{\n static int count = 0;\n MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));\n obj->id = ++count;\n obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);\n return obj;\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}']
|
1,237 | 0 |
https://github.com/openssl/openssl/blob/2d5d70b15559f9813054ddb11b30b816daf62ebe/crypto/lhash/lhash.c/#L229
|
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);
}
|
['SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return (NULL);\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return (NULL);\n }\n s = OPENSSL_malloc(sizeof(*s));\n if (s == NULL)\n goto err;\n memset(s, 0, sizeof(*s));\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->sid_ctx_length = ctx->sid_ctx_length;\n OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);\n memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));\n s->verify_callback = ctx->default_verify_callback;\n s->generate_session_id = ctx->generate_session_id;\n s->param = X509_VERIFY_PARAM_new();\n if (!s->param)\n goto err;\n X509_VERIFY_PARAM_inherit(s->param, ctx->param);\n s->quiet_shutdown = ctx->quiet_shutdown;\n s->max_send_fragment = ctx->max_send_fragment;\n CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);\n s->ctx = ctx;\n s->tlsext_debug_cb = 0;\n s->tlsext_debug_arg = NULL;\n s->tlsext_ticket_expected = 0;\n s->tlsext_status_type = -1;\n s->tlsext_status_expected = 0;\n s->tlsext_ocsp_ids = NULL;\n s->tlsext_ocsp_exts = NULL;\n s->tlsext_ocsp_resp = NULL;\n s->tlsext_ocsp_resplen = -1;\n CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);\n s->initial_ctx = ctx;\n# ifndef OPENSSL_NO_EC\n if (ctx->tlsext_ecpointformatlist) {\n s->tlsext_ecpointformatlist =\n BUF_memdup(ctx->tlsext_ecpointformatlist,\n ctx->tlsext_ecpointformatlist_length);\n if (!s->tlsext_ecpointformatlist)\n goto err;\n s->tlsext_ecpointformatlist_length =\n ctx->tlsext_ecpointformatlist_length;\n }\n if (ctx->tlsext_ellipticcurvelist) {\n s->tlsext_ellipticcurvelist =\n BUF_memdup(ctx->tlsext_ellipticcurvelist,\n ctx->tlsext_ellipticcurvelist_length);\n if (!s->tlsext_ellipticcurvelist)\n goto err;\n s->tlsext_ellipticcurvelist_length =\n ctx->tlsext_ellipticcurvelist_length;\n }\n# endif\n# ifndef OPENSSL_NO_NEXTPROTONEG\n s->next_proto_negotiated = NULL;\n# endif\n if (s->ctx->alpn_client_proto_list) {\n s->alpn_client_proto_list =\n OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);\n if (s->alpn_client_proto_list == NULL)\n goto err;\n memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,\n s->ctx->alpn_client_proto_list_len);\n s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;\n }\n s->verify_result = X509_V_OK;\n s->method = ctx->method;\n if (!s->method->ssl_new(s))\n goto err;\n s->references = 1;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n#ifndef OPENSSL_NO_PSK\n s->psk_client_callback = ctx->psk_client_callback;\n s->psk_server_callback = ctx->psk_server_callback;\n#endif\n return (s);\n err:\n SSL_free(s);\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n}', 'void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL);\n#ifdef REF_PRINT\n REF_PRINT("SSL", s);\n#endif\n if (i > 0)\n return;\n#ifdef REF_CHECK\n if (i < 0) {\n fprintf(stderr, "SSL_free, bad reference count\\n");\n abort();\n }\n#endif\n X509_VERIFY_PARAM_free(s->param);\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 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 if (s->method != NULL)\n s->method->ssl_free(s);\n RECORD_LAYER_release(&s->rlayer);\n SSL_CTX_free(s->ctx);\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 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_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}', '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}']
|
1,238 | 0 |
https://github.com/libav/libav/blob/688417399c69aadd4c287bdb0dec82ef8799011c/libavcodec/hevcdsp_template.c/#L901
|
PUT_HEVC_QPEL_HV(1, 1)
|
['QPEL(8)', 'PUT_HEVC_QPEL_HV(1, 1)']
|
1,239 | 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;
}
|
['int i2d_RSA_NET(const RSA *a, unsigned char **pp,\n\t\tint (*cb)(char *buf, int len, const char *prompt, int verify),\n\t\tint sgckey)\n\t{\n\tint i, j, ret = 0;\n\tint rsalen, pkeylen, olen;\n\tNETSCAPE_PKEY *pkey = NULL;\n\tNETSCAPE_ENCRYPTED_PKEY *enckey = NULL;\n\tunsigned char buf[256],*zz;\n\tunsigned char key[EVP_MAX_KEY_LENGTH];\n\tEVP_CIPHER_CTX ctx;\n\tif (a == NULL) return(0);\n\tif ((pkey=NETSCAPE_PKEY_new()) == NULL) goto err;\n\tif ((enckey=NETSCAPE_ENCRYPTED_PKEY_new()) == NULL) goto err;\n\tpkey->version = 0;\n\tpkey->algor->algorithm=OBJ_nid2obj(NID_rsaEncryption);\n\tif ((pkey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;\n\tpkey->algor->parameter->type=V_ASN1_NULL;\n\trsalen = i2d_RSAPrivateKey(a, NULL);\n\tpkey->private_key->length=rsalen;\n\tpkeylen=i2d_NETSCAPE_PKEY(pkey,NULL);\n\tenckey->enckey->digest->length = pkeylen;\n\tenckey->os->length = 11;\n\tenckey->enckey->algor->algorithm=OBJ_nid2obj(NID_rc4);\n\tif ((enckey->enckey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;\n\tenckey->enckey->algor->parameter->type=V_ASN1_NULL;\n\tif (pp == NULL)\n\t\t{\n\t\tolen = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, NULL);\n\t\tNETSCAPE_PKEY_free(pkey);\n\t\tNETSCAPE_ENCRYPTED_PKEY_free(enckey);\n\t\treturn olen;\n\t\t}\n\tif ((zz=(unsigned char *)OPENSSL_malloc(rsalen)) == NULL)\n\t\t{\n\t\tASN1err(ASN1_F_I2D_RSA_NET,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tpkey->private_key->data = zz;\n\ti2d_RSAPrivateKey(a,&zz);\n\tif ((zz=OPENSSL_malloc(pkeylen)) == NULL)\n\t\t{\n\t\tASN1err(ASN1_F_I2D_RSA_NET,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tif (!ASN1_STRING_set(enckey->os, "private-key", -1))\n\t\t{\n\t\tASN1err(ASN1_F_I2D_RSA_NET,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tenckey->enckey->digest->data = zz;\n\ti2d_NETSCAPE_PKEY(pkey,&zz);\n\tOPENSSL_cleanse(pkey->private_key->data, rsalen);\n\tif (cb == NULL)\n\t\tcb=EVP_read_pw_string;\n\ti=cb((char *)buf,256,"Enter Private Key password:",1);\n\tif (i != 0)\n\t\t{\n\t\tASN1err(ASN1_F_I2D_RSA_NET,ASN1_R_BAD_PASSWORD_READ);\n\t\tgoto err;\n\t\t}\n\ti = strlen((char *)buf);\n\tif(sgckey) {\n\t\tEVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL);\n\t\tmemcpy(buf + 16, "SGCKEYSALT", 10);\n\t\ti = 26;\n\t}\n\tEVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL);\n\tOPENSSL_cleanse(buf,256);\n\tzz = enckey->enckey->digest->data;\n\tEVP_CIPHER_CTX_init(&ctx);\n\tEVP_EncryptInit_ex(&ctx,EVP_rc4(),NULL,key,NULL);\n\tEVP_EncryptUpdate(&ctx,zz,&i,zz,pkeylen);\n\tEVP_EncryptFinal_ex(&ctx,zz + i,&j);\n\tEVP_CIPHER_CTX_cleanup(&ctx);\n\tret = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, pp);\nerr:\n\tNETSCAPE_ENCRYPTED_PKEY_free(enckey);\n\tNETSCAPE_PKEY_free(pkey);\n\treturn(ret);\n\t}', '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}']
|
1,240 | 0 |
https://github.com/openssl/openssl/blob/8807a2dfc417e332abeaa23a3a885917ce88425b/crypto/pkcs7/pk7_doit.c/#L1102
|
PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
{
STACK_OF(PKCS7_RECIP_INFO) *rsk;
PKCS7_RECIP_INFO *ri;
int i;
i=OBJ_obj2nid(p7->type);
if (i != NID_pkcs7_signedAndEnveloped)
return NULL;
if (p7->d.signed_and_enveloped == NULL)
return NULL;
rsk=p7->d.signed_and_enveloped->recipientinfo;
if (rsk == NULL)
return NULL;
ri=sk_PKCS7_RECIP_INFO_value(rsk,0);
if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) return(NULL);
ri=sk_PKCS7_RECIP_INFO_value(rsk,idx);
return(ri->issuer_and_serial);
}
|
['PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)\n\t{\n\tSTACK_OF(PKCS7_RECIP_INFO) *rsk;\n\tPKCS7_RECIP_INFO *ri;\n\tint i;\n\ti=OBJ_obj2nid(p7->type);\n\tif (i != NID_pkcs7_signedAndEnveloped)\n\t\treturn NULL;\n\tif (p7->d.signed_and_enveloped == NULL)\n\t\treturn NULL;\n\trsk=p7->d.signed_and_enveloped->recipientinfo;\n\tif (rsk == NULL)\n\t\treturn NULL;\n\tri=sk_PKCS7_RECIP_INFO_value(rsk,0);\n\tif (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) return(NULL);\n\tri=sk_PKCS7_RECIP_INFO_value(rsk,idx);\n\treturn(ri->issuer_and_serial);\n\t}', 'int OBJ_obj2nid(const ASN1_OBJECT *a)\n\t{\n\tASN1_OBJECT **op;\n\tADDED_OBJ ad,*adp;\n\tif (a == NULL)\n\t\treturn(NID_undef);\n\tif (a->nid != 0)\n\t\treturn(a->nid);\n\tif (added != NULL)\n\t\t{\n\t\tad.type=ADDED_DATA;\n\t\tad.obj=(ASN1_OBJECT *)a;\n\t\tadp=(ADDED_OBJ *)lh_retrieve(added,&ad);\n\t\tif (adp != NULL) return (adp->obj->nid);\n\t\t}\n\top=(ASN1_OBJECT **)OBJ_bsearch((const char *)&a,(const char *)obj_objs,\n\t\tNUM_OBJ, sizeof(ASN1_OBJECT *),obj_cmp);\n\tif (op == NULL)\n\t\treturn(NID_undef);\n\treturn((*op)->nid);\n\t}', 'void *lh_retrieve(LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE **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_retrieve_miss++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tret= (*rn)->data;\n\t\tlh->num_retrieve++;\n\t\t}\n\treturn(ret);\n\t}', 'char *sk_value(const STACK *st, int i)\n{\n\tif(!st || (i < 0) || (i >= st->num)) return NULL;\n\treturn st->data[i];\n}', 'int sk_num(const STACK *st)\n{\n\tif(st == NULL) return -1;\n\treturn st->num;\n}']
|
1,241 | 0 |
https://github.com/openssl/openssl/blob/5c98b2caf5ce545fbf77611431c7084979da8177/crypto/bn/bn_ctx.c/#L353
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['static int generate_key(DH *dh)\n\t{\n\tint ok=0;\n\tint generate_new_key=0;\n\tunsigned l;\n\tBN_CTX *ctx;\n\tBN_MONT_CTX *mont;\n\tBIGNUM *pub_key=NULL,*priv_key=NULL;\n\tctx = BN_CTX_new();\n\tif (ctx == NULL) goto err;\n\tif (dh->priv_key == NULL)\n\t\t{\n\t\tpriv_key=BN_new();\n\t\tif (priv_key == NULL) goto err;\n\t\tgenerate_new_key=1;\n\t\t}\n\telse\n\t\tpriv_key=dh->priv_key;\n\tif (dh->pub_key == NULL)\n\t\t{\n\t\tpub_key=BN_new();\n\t\tif (pub_key == NULL) goto err;\n\t\t}\n\telse\n\t\tpub_key=dh->pub_key;\n\tif ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P))\n\t\t{\n\t\tif ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)\n\t\t\tif (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p,\n\t\t\t\tdh->p,ctx)) goto err;\n\t\t}\n\tmont=(BN_MONT_CTX *)dh->method_mont_p;\n\tif (generate_new_key)\n\t\t{\n\t\tl = dh->length ? dh->length : BN_num_bits(dh->p)-1;\n\t\tif (!BN_rand(priv_key, l, 0, 0)) goto err;\n\t\t}\n\tif (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, priv_key,dh->p,ctx,mont))\n\t\tgoto err;\n\tdh->pub_key=pub_key;\n\tdh->priv_key=priv_key;\n\tok=1;\nerr:\n\tif (ok != 1)\n\t\tDHerr(DH_F_DH_GENERATE_KEY,ERR_R_BN_LIB);\n\tif ((pub_key != NULL) && (dh->pub_key == NULL)) BN_free(pub_key);\n\tif ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key);\n\tBN_CTX_free(ctx);\n\treturn(ok);\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\tmont->N.neg = 0;\n#ifdef MONT_WORD\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,BN_BITS2))) goto err;\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.dmax=2;\n\t\ttmod.neg=0;\n\t\tif ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(&Ri,&Ri,BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(&Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(&Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_set_word(&Ri,BN_MASK2)) goto err;\n\t\t\t}\n\t\tif (!BN_div(&Ri,NULL,&Ri,&tmod,ctx)) goto err;\n\t\tmont->n0 = (Ri.top > 0) ? Ri.d[0] : 0;\n\t\tBN_free(&Ri);\n\t\t}\n#else\n\t\t{\n\t\tmont->ri=BN_num_bits(&mont->N);\n\t\tBN_zero(R);\n\t\tif (!BN_set_bit(R,mont->ri)) goto err;\n\t\tif ((BN_mod_inverse(&Ri,R,&mont->N,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(&Ri,&Ri,mont->ri)) goto err;\n\t\tif (!BN_sub_word(&Ri,1)) goto err;\n\t\tif (!BN_div(&(mont->Ni),NULL,&Ri,&mont->N,ctx)) goto err;\n\t\tBN_free(&Ri);\n\t\t}\n#endif\n\tBN_zero(&(mont->RR));\n\tif (!BN_set_bit(&(mont->RR),mont->ri*2)) goto err;\n\tif (!BN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx)) goto err;\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tif (!BN_nnmod(B, B, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\tif (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))\n\t\t{\n\t\tint shift;\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(B, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(X))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(X, X, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(X, X)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(B, B, shift)) goto err;\n\t\t\t\t}\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(A, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(Y))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(Y, Y, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(Y, Y)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(A, A, shift)) goto err;\n\t\t\t\t}\n\t\t\tif (BN_ucmp(B, A) >= 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(X, X, Y)) goto err;\n\t\t\t\tif (!BN_usub(B, B, A)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(Y, Y, X)) goto err;\n\t\t\t\tif (!BN_usub(A, A, B)) goto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tBIGNUM *tmp;\n\t\t\tif (BN_num_bits(A) == BN_num_bits(B))\n\t\t\t\t{\n\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t}\n\t\t\telse if (BN_num_bits(A) == BN_num_bits(B) + 1)\n\t\t\t\t{\n\t\t\t\tif (!BN_lshift1(T,B)) goto err;\n\t\t\t\tif (BN_ucmp(A,T) < 0)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_sub(M,A,T)) goto err;\n\t\t\t\t\tif (!BN_add(D,T,B)) goto err;\n\t\t\t\t\tif (BN_ucmp(A,D) < 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,2)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,3)) goto err;\n\t\t\t\t\t\tif (!BN_sub(M,M,B)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\t\t\t}\n\t\t\ttmp=A;\n\t\t\tA=B;\n\t\t\tB=M;\n\t\t\tif (BN_is_one(D))\n\t\t\t\t{\n\t\t\t\tif (!BN_add(tmp,X,Y)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (BN_is_word(D,2))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift1(tmp,X)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (BN_is_word(D,4))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift(tmp,X,2)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (D->top == 1)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_copy(tmp,X)) goto err;\n\t\t\t\t\tif (!BN_mul_word(tmp,D->d[0])) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\t\t\t}\n\t\t\tM=Y;\n\t\t\tY=X;\n\t\t\tX=tmp;\n\t\t\tsign = -sign;\n\t\t\t}\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tif (ret)\n\t\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_GET,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n\tif (!(BN_mod(r,m,d,ctx)))\n\t\treturn 0;\n\tif (!r->neg)\n\t\treturn 1;\n\treturn (d->neg ? BN_sub : BN_add)(r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tif (dv)\n\t\tbn_check_top(dv);\n\tif (rm)\n\t\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tif (rm)\n\t\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
|
1,242 | 0 |
https://github.com/libav/libav/blob/124c21d79f2124d028890022e98ea853a834a964/libavcodec/simple_idct.c/#L113
|
static inline void idctRowCondDC (DCTELEM * row)
{
int a0, a1, a2, a3, b0, b1, b2, b3;
#ifdef HAVE_FAST_64BIT
uint64_t temp;
#else
uint32_t temp;
#endif
#ifdef HAVE_FAST_64BIT
#ifdef WORDS_BIGENDIAN
#define ROW0_MASK 0xffff000000000000LL
#else
#define ROW0_MASK 0xffffLL
#endif
if(sizeof(DCTELEM)==2){
if ( ((((uint64_t *)row)[0] & ~ROW0_MASK) |
((uint64_t *)row)[1]) == 0) {
temp = (row[0] << 3) & 0xffff;
temp += temp << 16;
temp += temp << 32;
((uint64_t *)row)[0] = temp;
((uint64_t *)row)[1] = temp;
return;
}
}else{
if (!(row[1]|row[2]|row[3]|row[4]|row[5]|row[6]|row[7])) {
row[0]=row[1]=row[2]=row[3]=row[4]=row[5]=row[6]=row[7]= row[0] << 3;
return;
}
}
#else
if(sizeof(DCTELEM)==2){
if (!(((uint32_t*)row)[1] |
((uint32_t*)row)[2] |
((uint32_t*)row)[3] |
row[1])) {
temp = (row[0] << 3) & 0xffff;
temp += temp << 16;
((uint32_t*)row)[0]=((uint32_t*)row)[1] =
((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;
return;
}
}else{
if (!(row[1]|row[2]|row[3]|row[4]|row[5]|row[6]|row[7])) {
row[0]=row[1]=row[2]=row[3]=row[4]=row[5]=row[6]=row[7]= row[0] << 3;
return;
}
}
#endif
a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
a1 = a0;
a2 = a0;
a3 = a0;
a0 += W2 * row[2];
a1 += W6 * row[2];
a2 -= W6 * row[2];
a3 -= W2 * row[2];
MUL16(b0, W1, row[1]);
MAC16(b0, W3, row[3]);
MUL16(b1, W3, row[1]);
MAC16(b1, -W7, row[3]);
MUL16(b2, W5, row[1]);
MAC16(b2, -W1, row[3]);
MUL16(b3, W7, row[1]);
MAC16(b3, -W5, row[3]);
#ifdef HAVE_FAST_64BIT
temp = ((uint64_t*)row)[1];
#else
temp = ((uint32_t*)row)[2] | ((uint32_t*)row)[3];
#endif
if (temp != 0) {
a0 += W4*row[4] + W6*row[6];
a1 += - W4*row[4] - W2*row[6];
a2 += - W4*row[4] + W2*row[6];
a3 += W4*row[4] - W6*row[6];
MAC16(b0, W5, row[5]);
MAC16(b0, W7, row[7]);
MAC16(b1, -W1, row[5]);
MAC16(b1, -W5, row[7]);
MAC16(b2, W7, row[5]);
MAC16(b2, W3, row[7]);
MAC16(b3, W3, row[5]);
MAC16(b3, -W1, row[7]);
}
row[0] = (a0 + b0) >> ROW_SHIFT;
row[7] = (a0 - b0) >> ROW_SHIFT;
row[1] = (a1 + b1) >> ROW_SHIFT;
row[6] = (a1 - b1) >> ROW_SHIFT;
row[2] = (a2 + b2) >> ROW_SHIFT;
row[5] = (a2 - b2) >> ROW_SHIFT;
row[3] = (a3 + b3) >> ROW_SHIFT;
row[4] = (a3 - b3) >> ROW_SHIFT;
}
|
['void ff_wmv2_add_mb(MpegEncContext *s, DCTELEM block1[6][64], uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr){\n Wmv2Context * const w= (Wmv2Context*)s;\n wmv2_add_block(w, block1[0], dest_y , s->linesize, 0);\n wmv2_add_block(w, block1[1], dest_y + 8 , s->linesize, 1);\n wmv2_add_block(w, block1[2], dest_y + 8*s->linesize, s->linesize, 2);\n wmv2_add_block(w, block1[3], dest_y + 8 + 8*s->linesize, s->linesize, 3);\n if(s->flags&CODEC_FLAG_GRAY) return;\n wmv2_add_block(w, block1[4], dest_cb , s->uvlinesize, 4);\n wmv2_add_block(w, block1[5], dest_cr , s->uvlinesize, 5);\n}', 'static void wmv2_add_block(Wmv2Context *w, DCTELEM *block1, uint8_t *dst, int stride, int n){\n MpegEncContext * const s= &w->s;\n if (s->block_last_index[n] >= 0) {\n switch(w->abt_type_table[n]){\n case 0:\n s->dsp.idct_add (dst, stride, block1);\n break;\n case 1:\n ff_simple_idct84_add(dst , stride, block1);\n ff_simple_idct84_add(dst + 4*stride, stride, w->abt_block2[n]);\n s->dsp.clear_block(w->abt_block2[n]);\n break;\n case 2:\n ff_simple_idct48_add(dst , stride, block1);\n ff_simple_idct48_add(dst + 4 , stride, w->abt_block2[n]);\n s->dsp.clear_block(w->abt_block2[n]);\n break;\n default:\n av_log(s->avctx, AV_LOG_ERROR, "internal error in WMV2 abt\\n");\n }\n }\n}', 'void ff_simple_idct84_add(uint8_t *dest, int line_size, DCTELEM *block)\n{\n int i;\n for(i=0; i<4; i++) {\n idctRowCondDC(block + i*8);\n }\n for(i=0;i<8;i++) {\n idct4col_add(dest + i, line_size, block + i);\n }\n}', 'static inline void idctRowCondDC (DCTELEM * row)\n{\n int a0, a1, a2, a3, b0, b1, b2, b3;\n#ifdef HAVE_FAST_64BIT\n uint64_t temp;\n#else\n uint32_t temp;\n#endif\n#ifdef HAVE_FAST_64BIT\n#ifdef WORDS_BIGENDIAN\n#define ROW0_MASK 0xffff000000000000LL\n#else\n#define ROW0_MASK 0xffffLL\n#endif\n if(sizeof(DCTELEM)==2){\n if ( ((((uint64_t *)row)[0] & ~ROW0_MASK) |\n ((uint64_t *)row)[1]) == 0) {\n temp = (row[0] << 3) & 0xffff;\n temp += temp << 16;\n temp += temp << 32;\n ((uint64_t *)row)[0] = temp;\n ((uint64_t *)row)[1] = temp;\n return;\n }\n }else{\n if (!(row[1]|row[2]|row[3]|row[4]|row[5]|row[6]|row[7])) {\n row[0]=row[1]=row[2]=row[3]=row[4]=row[5]=row[6]=row[7]= row[0] << 3;\n return;\n }\n }\n#else\n if(sizeof(DCTELEM)==2){\n if (!(((uint32_t*)row)[1] |\n ((uint32_t*)row)[2] |\n ((uint32_t*)row)[3] |\n row[1])) {\n temp = (row[0] << 3) & 0xffff;\n temp += temp << 16;\n ((uint32_t*)row)[0]=((uint32_t*)row)[1] =\n ((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;\n return;\n }\n }else{\n if (!(row[1]|row[2]|row[3]|row[4]|row[5]|row[6]|row[7])) {\n row[0]=row[1]=row[2]=row[3]=row[4]=row[5]=row[6]=row[7]= row[0] << 3;\n return;\n }\n }\n#endif\n a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));\n a1 = a0;\n a2 = a0;\n a3 = a0;\n a0 += W2 * row[2];\n a1 += W6 * row[2];\n a2 -= W6 * row[2];\n a3 -= W2 * row[2];\n MUL16(b0, W1, row[1]);\n MAC16(b0, W3, row[3]);\n MUL16(b1, W3, row[1]);\n MAC16(b1, -W7, row[3]);\n MUL16(b2, W5, row[1]);\n MAC16(b2, -W1, row[3]);\n MUL16(b3, W7, row[1]);\n MAC16(b3, -W5, row[3]);\n#ifdef HAVE_FAST_64BIT\n temp = ((uint64_t*)row)[1];\n#else\n temp = ((uint32_t*)row)[2] | ((uint32_t*)row)[3];\n#endif\n if (temp != 0) {\n a0 += W4*row[4] + W6*row[6];\n a1 += - W4*row[4] - W2*row[6];\n a2 += - W4*row[4] + W2*row[6];\n a3 += W4*row[4] - W6*row[6];\n MAC16(b0, W5, row[5]);\n MAC16(b0, W7, row[7]);\n MAC16(b1, -W1, row[5]);\n MAC16(b1, -W5, row[7]);\n MAC16(b2, W7, row[5]);\n MAC16(b2, W3, row[7]);\n MAC16(b3, W3, row[5]);\n MAC16(b3, -W1, row[7]);\n }\n row[0] = (a0 + b0) >> ROW_SHIFT;\n row[7] = (a0 - b0) >> ROW_SHIFT;\n row[1] = (a1 + b1) >> ROW_SHIFT;\n row[6] = (a1 - b1) >> ROW_SHIFT;\n row[2] = (a2 + b2) >> ROW_SHIFT;\n row[5] = (a2 - b2) >> ROW_SHIFT;\n row[3] = (a3 + b3) >> ROW_SHIFT;\n row[4] = (a3 - b3) >> ROW_SHIFT;\n}']
|
1,243 | 0 |
https://github.com/openssl/openssl/blob/427e91d928ce7a1c583e4bba761cb17a85ac95b4/ssl/ssl_lib.c/#L4385
|
EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)
{
ssl_clear_hash_ctx(hash);
*hash = EVP_MD_CTX_new();
if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) {
EVP_MD_CTX_free(*hash);
*hash = NULL;
return NULL;
}
return *hash;
}
|
['EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)\n{\n ssl_clear_hash_ctx(hash);\n *hash = EVP_MD_CTX_new();\n if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) {\n EVP_MD_CTX_free(*hash);\n *hash = NULL;\n return NULL;\n }\n return *hash;\n}', 'void ssl_clear_hash_ctx(EVP_MD_CTX **hash)\n{\n EVP_MD_CTX_free(*hash);\n *hash = NULL;\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}', '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}']
|
1,244 | 0 |
https://github.com/libav/libav/blob/12f0388f9cb32016ac0dacaeca631b088b29bb96/libavcodec/simple_idct_template.c/#L147
|
static inline void FUNC(idctRowCondDC)(int16_t *row, int extra_shift)
{
int a0, a1, a2, a3, b0, b1, b2, b3;
#if HAVE_FAST_64BIT
#define ROW0_MASK (0xffffLL << 48 * HAVE_BIGENDIAN)
if (((((uint64_t *)row)[0] & ~ROW0_MASK) | ((uint64_t *)row)[1]) == 0) {
uint64_t temp;
if (DC_SHIFT - extra_shift > 0) {
temp = (row[0] << (DC_SHIFT - extra_shift)) & 0xffff;
} else {
temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;
}
temp += temp << 16;
temp += temp << 32;
((uint64_t *)row)[0] = temp;
((uint64_t *)row)[1] = temp;
return;
}
#else
if (!(((uint32_t*)row)[1] |
((uint32_t*)row)[2] |
((uint32_t*)row)[3] |
row[1])) {
uint32_t temp;
if (DC_SHIFT - extra_shift > 0) {
temp = (row[0] << (DC_SHIFT - extra_shift)) & 0xffff;
} else {
temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;
}
temp += temp << 16;
((uint32_t*)row)[0]=((uint32_t*)row)[1] =
((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;
return;
}
#endif
a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
a1 = a0;
a2 = a0;
a3 = a0;
a0 += W2 * row[2];
a1 += W6 * row[2];
a2 -= W6 * row[2];
a3 -= W2 * row[2];
b0 = MUL(W1, row[1]);
MAC(b0, W3, row[3]);
b1 = MUL(W3, row[1]);
MAC(b1, -W7, row[3]);
b2 = MUL(W5, row[1]);
MAC(b2, -W1, row[3]);
b3 = MUL(W7, row[1]);
MAC(b3, -W5, row[3]);
if (AV_RN64A(row + 4)) {
a0 += W4*row[4] + W6*row[6];
a1 += - W4*row[4] - W2*row[6];
a2 += - W4*row[4] + W2*row[6];
a3 += W4*row[4] - W6*row[6];
MAC(b0, W5, row[5]);
MAC(b0, W7, row[7]);
MAC(b1, -W1, row[5]);
MAC(b1, -W5, row[7]);
MAC(b2, W7, row[5]);
MAC(b2, W3, row[7]);
MAC(b3, W3, row[5]);
MAC(b3, -W1, row[7]);
}
row[0] = (a0 + b0) >> (ROW_SHIFT + extra_shift);
row[7] = (a0 - b0) >> (ROW_SHIFT + extra_shift);
row[1] = (a1 + b1) >> (ROW_SHIFT + extra_shift);
row[6] = (a1 - b1) >> (ROW_SHIFT + extra_shift);
row[2] = (a2 + b2) >> (ROW_SHIFT + extra_shift);
row[5] = (a2 - b2) >> (ROW_SHIFT + extra_shift);
row[3] = (a3 + b3) >> (ROW_SHIFT + extra_shift);
row[4] = (a3 - b3) >> (ROW_SHIFT + extra_shift);
}
|
['void ff_wmv2_add_mb(MpegEncContext *s, int16_t block1[6][64],\n uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr)\n{\n Wmv2Context *const w = (Wmv2Context *) s;\n wmv2_add_block(w, block1[0], dest_y, s->linesize, 0);\n wmv2_add_block(w, block1[1], dest_y + 8, s->linesize, 1);\n wmv2_add_block(w, block1[2], dest_y + 8 * s->linesize, s->linesize, 2);\n wmv2_add_block(w, block1[3], dest_y + 8 + 8 * s->linesize, s->linesize, 3);\n if (s->flags & CODEC_FLAG_GRAY)\n return;\n wmv2_add_block(w, block1[4], dest_cb, s->uvlinesize, 4);\n wmv2_add_block(w, block1[5], dest_cr, s->uvlinesize, 5);\n}', 'static void wmv2_add_block(Wmv2Context *w, int16_t *block1,\n uint8_t *dst, int stride, int n)\n{\n MpegEncContext *const s = &w->s;\n if (s->block_last_index[n] >= 0) {\n switch (w->abt_type_table[n]) {\n case 0:\n w->wdsp.idct_add(dst, stride, block1);\n break;\n case 1:\n ff_simple_idct84_add(dst, stride, block1);\n ff_simple_idct84_add(dst + 4 * stride, stride, w->abt_block2[n]);\n s->bdsp.clear_block(w->abt_block2[n]);\n break;\n case 2:\n ff_simple_idct48_add(dst, stride, block1);\n ff_simple_idct48_add(dst + 4, stride, w->abt_block2[n]);\n s->bdsp.clear_block(w->abt_block2[n]);\n break;\n default:\n av_log(s->avctx, AV_LOG_ERROR, "internal error in WMV2 abt\\n");\n }\n }\n}', 'void ff_simple_idct84_add(uint8_t *dest, int line_size, int16_t *block)\n{\n int i;\n for(i=0; i<4; i++) {\n idctRowCondDC_8(block + i*8, 0);\n }\n for(i=0;i<8;i++) {\n idct4col_add(dest + i, line_size, block + i);\n }\n}', 'static inline void FUNC(idctRowCondDC)(int16_t *row, int extra_shift)\n{\n int a0, a1, a2, a3, b0, b1, b2, b3;\n#if HAVE_FAST_64BIT\n#define ROW0_MASK (0xffffLL << 48 * HAVE_BIGENDIAN)\n if (((((uint64_t *)row)[0] & ~ROW0_MASK) | ((uint64_t *)row)[1]) == 0) {\n uint64_t temp;\n if (DC_SHIFT - extra_shift > 0) {\n temp = (row[0] << (DC_SHIFT - extra_shift)) & 0xffff;\n } else {\n temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;\n }\n temp += temp << 16;\n temp += temp << 32;\n ((uint64_t *)row)[0] = temp;\n ((uint64_t *)row)[1] = temp;\n return;\n }\n#else\n if (!(((uint32_t*)row)[1] |\n ((uint32_t*)row)[2] |\n ((uint32_t*)row)[3] |\n row[1])) {\n uint32_t temp;\n if (DC_SHIFT - extra_shift > 0) {\n temp = (row[0] << (DC_SHIFT - extra_shift)) & 0xffff;\n } else {\n temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;\n }\n temp += temp << 16;\n ((uint32_t*)row)[0]=((uint32_t*)row)[1] =\n ((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;\n return;\n }\n#endif\n a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));\n a1 = a0;\n a2 = a0;\n a3 = a0;\n a0 += W2 * row[2];\n a1 += W6 * row[2];\n a2 -= W6 * row[2];\n a3 -= W2 * row[2];\n b0 = MUL(W1, row[1]);\n MAC(b0, W3, row[3]);\n b1 = MUL(W3, row[1]);\n MAC(b1, -W7, row[3]);\n b2 = MUL(W5, row[1]);\n MAC(b2, -W1, row[3]);\n b3 = MUL(W7, row[1]);\n MAC(b3, -W5, row[3]);\n if (AV_RN64A(row + 4)) {\n a0 += W4*row[4] + W6*row[6];\n a1 += - W4*row[4] - W2*row[6];\n a2 += - W4*row[4] + W2*row[6];\n a3 += W4*row[4] - W6*row[6];\n MAC(b0, W5, row[5]);\n MAC(b0, W7, row[7]);\n MAC(b1, -W1, row[5]);\n MAC(b1, -W5, row[7]);\n MAC(b2, W7, row[5]);\n MAC(b2, W3, row[7]);\n MAC(b3, W3, row[5]);\n MAC(b3, -W1, row[7]);\n }\n row[0] = (a0 + b0) >> (ROW_SHIFT + extra_shift);\n row[7] = (a0 - b0) >> (ROW_SHIFT + extra_shift);\n row[1] = (a1 + b1) >> (ROW_SHIFT + extra_shift);\n row[6] = (a1 - b1) >> (ROW_SHIFT + extra_shift);\n row[2] = (a2 + b2) >> (ROW_SHIFT + extra_shift);\n row[5] = (a2 - b2) >> (ROW_SHIFT + extra_shift);\n row[3] = (a3 + b3) >> (ROW_SHIFT + extra_shift);\n row[4] = (a3 - b3) >> (ROW_SHIFT + extra_shift);\n}']
|
1,245 | 0 |
https://gitlab.com/libtiff/libtiff/blob/771a4ea0a98c7a218c9f3add9a05e08d29625758/libtiff/tif_dirread.c/#L4227
|
static int
EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
{
static const char module[] = "EstimateStripByteCounts";
TIFFDirEntry *dp;
TIFFDirectory *td = &tif->tif_dir;
uint32 strip;
if (td->td_stripbytecount)
_TIFFfree(td->td_stripbytecount);
td->td_stripbytecount = (uint64*)
_TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
"for \"StripByteCounts\" array");
if( td->td_stripbytecount == NULL )
return -1;
if (td->td_compression != COMPRESSION_NONE) {
uint64 space;
uint64 filesize;
uint16 n;
filesize = TIFFGetFileSize(tif);
if (!(tif->tif_flags&TIFF_BIGTIFF))
space=sizeof(TIFFHeaderClassic)+2+dircount*12+4;
else
space=sizeof(TIFFHeaderBig)+8+dircount*20+8;
for (dp = dir, n = dircount; n > 0; n--, dp++)
{
uint32 typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
uint64 datasize;
typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
if (typewidth == 0) {
TIFFErrorExt(tif->tif_clientdata, module,
"Cannot determine size of unknown tag type %d",
dp->tdir_type);
return -1;
}
datasize=(uint64)typewidth*dp->tdir_count;
if (!(tif->tif_flags&TIFF_BIGTIFF))
{
if (datasize<=4)
datasize=0;
}
else
{
if (datasize<=8)
datasize=0;
}
space+=datasize;
}
space = filesize - space;
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
space /= td->td_samplesperpixel;
for (strip = 0; strip < td->td_nstrips; strip++)
td->td_stripbytecount[strip] = space;
strip--;
if (td->td_stripoffset[strip]+td->td_stripbytecount[strip] > filesize)
td->td_stripbytecount[strip] = filesize - td->td_stripoffset[strip];
} else if (isTiled(tif)) {
uint64 bytespertile = TIFFTileSize64(tif);
for (strip = 0; strip < td->td_nstrips; strip++)
td->td_stripbytecount[strip] = bytespertile;
} else {
uint64 rowbytes = TIFFScanlineSize64(tif);
uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
for (strip = 0; strip < td->td_nstrips; strip++)
td->td_stripbytecount[strip] = rowbytes * rowsperstrip;
}
TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
td->td_rowsperstrip = td->td_imagelength;
return 1;
}
|
['int\nmain(int argc, char* argv[])\n{\n\tTIFF *in, *out;\n\tif (argc < 2) {\n fprintf(stderr, "%s\\n\\n", TIFFGetVersion());\n\t\tfprintf(stderr, "usage: tiffsplit input.tif [prefix]\\n");\n\t\treturn (-3);\n\t}\n\tif (argc > 2) {\n\t\tstrncpy(fname, argv[2], sizeof(fname));\n\t\tfname[sizeof(fname) - 1] = \'\\0\';\n\t}\n\tin = TIFFOpen(argv[1], "r");\n\tif (in != NULL) {\n\t\tdo {\n\t\t\tsize_t path_len;\n\t\t\tchar *path;\n\t\t\tnewfilename();\n\t\t\tpath_len = strlen(fname) + sizeof(TIFF_SUFFIX);\n\t\t\tpath = (char *) _TIFFmalloc(path_len);\n\t\t\tstrncpy(path, fname, path_len);\n\t\t\tpath[path_len - 1] = \'\\0\';\n\t\t\tstrncat(path, TIFF_SUFFIX, path_len - strlen(path) - 1);\n\t\t\tout = TIFFOpen(path, TIFFIsBigEndian(in)?"wb":"wl");\n\t\t\t_TIFFfree(path);\n\t\t\tif (out == NULL)\n\t\t\t\treturn (-2);\n\t\t\tif (!tiffcp(in, out))\n\t\t\t\treturn (-1);\n\t\t\tTIFFClose(out);\n\t\t} while (TIFFReadDirectory(in));\n\t\t(void) TIFFClose(in);\n\t}\n\treturn (0);\n}', 'static int\ntiffcp(TIFF* in, TIFF* out)\n{\n\tuint16 bitspersample, samplesperpixel, compression, shortv, *shortav;\n\tuint32 w, l;\n\tfloat floatv;\n\tchar *stringv;\n\tuint32 longv;\n\tCopyField(TIFFTAG_SUBFILETYPE, longv);\n\tCopyField(TIFFTAG_TILEWIDTH, w);\n\tCopyField(TIFFTAG_TILELENGTH, l);\n\tCopyField(TIFFTAG_IMAGEWIDTH, w);\n\tCopyField(TIFFTAG_IMAGELENGTH, l);\n\tCopyField(TIFFTAG_BITSPERSAMPLE, bitspersample);\n\tCopyField(TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);\n\tCopyField(TIFFTAG_COMPRESSION, compression);\n\tif (compression == COMPRESSION_JPEG) {\n\t\tuint16 count = 0;\n\t\tvoid *table = NULL;\n\t\tif (TIFFGetField(in, TIFFTAG_JPEGTABLES, &count, &table)\n\t\t && count > 0 && table) {\n\t\t TIFFSetField(out, TIFFTAG_JPEGTABLES, count, table);\n\t\t}\n\t}\n CopyField(TIFFTAG_PHOTOMETRIC, shortv);\n\tCopyField(TIFFTAG_PREDICTOR, shortv);\n\tCopyField(TIFFTAG_THRESHHOLDING, shortv);\n\tCopyField(TIFFTAG_FILLORDER, shortv);\n\tCopyField(TIFFTAG_ORIENTATION, shortv);\n\tCopyField(TIFFTAG_MINSAMPLEVALUE, shortv);\n\tCopyField(TIFFTAG_MAXSAMPLEVALUE, shortv);\n\tCopyField(TIFFTAG_XRESOLUTION, floatv);\n\tCopyField(TIFFTAG_YRESOLUTION, floatv);\n\tCopyField(TIFFTAG_GROUP3OPTIONS, longv);\n\tCopyField(TIFFTAG_GROUP4OPTIONS, longv);\n\tCopyField(TIFFTAG_RESOLUTIONUNIT, shortv);\n\tCopyField(TIFFTAG_PLANARCONFIG, shortv);\n\tCopyField(TIFFTAG_ROWSPERSTRIP, longv);\n\tCopyField(TIFFTAG_XPOSITION, floatv);\n\tCopyField(TIFFTAG_YPOSITION, floatv);\n\tCopyField(TIFFTAG_IMAGEDEPTH, longv);\n\tCopyField(TIFFTAG_TILEDEPTH, longv);\n\tCopyField(TIFFTAG_SAMPLEFORMAT, longv);\n\tCopyField2(TIFFTAG_EXTRASAMPLES, shortv, shortav);\n\t{ uint16 *red, *green, *blue;\n\t CopyField3(TIFFTAG_COLORMAP, red, green, blue);\n\t}\n\t{ uint16 shortv2;\n\t CopyField2(TIFFTAG_PAGENUMBER, shortv, shortv2);\n\t}\n\tCopyField(TIFFTAG_ARTIST, stringv);\n\tCopyField(TIFFTAG_IMAGEDESCRIPTION, stringv);\n\tCopyField(TIFFTAG_MAKE, stringv);\n\tCopyField(TIFFTAG_MODEL, stringv);\n\tCopyField(TIFFTAG_SOFTWARE, stringv);\n\tCopyField(TIFFTAG_DATETIME, stringv);\n\tCopyField(TIFFTAG_HOSTCOMPUTER, stringv);\n\tCopyField(TIFFTAG_PAGENAME, stringv);\n\tCopyField(TIFFTAG_DOCUMENTNAME, stringv);\n\tCopyField(TIFFTAG_BADFAXLINES, longv);\n\tCopyField(TIFFTAG_CLEANFAXDATA, longv);\n\tCopyField(TIFFTAG_CONSECUTIVEBADFAXLINES, longv);\n\tCopyField(TIFFTAG_FAXRECVPARAMS, longv);\n\tCopyField(TIFFTAG_FAXRECVTIME, longv);\n\tCopyField(TIFFTAG_FAXSUBADDRESS, stringv);\n\tCopyField(TIFFTAG_FAXDCS, stringv);\n\tif (TIFFIsTiled(in))\n\t\treturn (cpTiles(in, out));\n\telse\n\t\treturn (cpStrips(in, out));\n}', 'static int\ncpTiles(TIFF* in, TIFF* out)\n{\n\ttmsize_t bufsize = TIFFTileSize(in);\n\tunsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);\n\tif (buf) {\n\t\tttile_t t, nt = TIFFNumberOfTiles(in);\n\t\tuint64 *bytecounts;\n\t\tTIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts);\n\t\tfor (t = 0; t < nt; t++) {\n\t\t\tif (bytecounts[t] > (uint64) bufsize) {\n\t\t\t\tbuf = (unsigned char *)_TIFFrealloc(buf, bytecounts[t]);\n\t\t\t\tif (!buf)\n\t\t\t\t\treturn (0);\n\t\t\t\tbufsize = bytecounts[t];\n\t\t\t}\n\t\t\tif (TIFFReadRawTile(in, t, buf, bytecounts[t]) < 0 ||\n\t\t\t TIFFWriteRawTile(out, t, buf, bytecounts[t]) < 0) {\n\t\t\t\t_TIFFfree(buf);\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\t_TIFFfree(buf);\n\t\treturn (1);\n\t}\n\treturn (0);\n}', 'int\nTIFFReadDirectory(TIFF* tif)\n{\n\tstatic const char module[] = "TIFFReadDirectory";\n\tTIFFDirEntry* dir;\n\tuint16 dircount;\n\tTIFFDirEntry* dp;\n\tuint16 di;\n\tconst TIFFField* fip;\n\tuint32 fii;\n toff_t nextdiroff;\n\ttif->tif_diroff=tif->tif_nextdiroff;\n\tif (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))\n\t\treturn 0;\n\t(*tif->tif_cleanup)(tif);\n\ttif->tif_curdir++;\n nextdiroff = tif->tif_nextdiroff;\n\tdircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);\n\tif (!dircount)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,\n\t\t "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);\n\t\treturn 0;\n\t}\n\tTIFFReadDirectoryCheckOrder(tif,dir,dircount);\n\ttif->tif_flags &= ~TIFF_BEENWRITING;\n\ttif->tif_flags &= ~TIFF_BUF4WRITE;\n\tTIFFFreeDirectory(tif);\n\tTIFFDefaultDirectory(tif);\n\tTIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);\n\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);\n\tif (dp)\n\t{\n\t\tif (!TIFFFetchNormalTag(tif,dp,0))\n\t\t\tgoto bad;\n\t\tdp->tdir_tag=IGNORE;\n\t}\n\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);\n\tif (dp)\n\t{\n\t\tuint16 value;\n\t\tenum TIFFReadDirEntryErr err;\n\t\terr=TIFFReadDirEntryShort(tif,dp,&value);\n\t\tif (err==TIFFReadDirEntryErrCount)\n\t\t\terr=TIFFReadDirEntryPersampleShort(tif,dp,&value);\n\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t{\n\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);\n\t\t\tgoto bad;\n\t\t}\n\t\tif (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))\n\t\t\tgoto bad;\n\t\tdp->tdir_tag=IGNORE;\n\t}\n\telse\n\t{\n\t\tif (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))\n\t\t\tgoto bad;\n\t}\n\tfor (di=0, dp=dir; di<dircount; di++, dp++)\n\t{\n\t\tif (dp->tdir_tag!=IGNORE)\n\t\t{\n\t\t\tTIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);\n\t\t\tif (fii==(uint32)(-1))\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t\t "Unknown field with tag %d (0x%x) encountered",\n\t\t\t\t dp->tdir_tag,dp->tdir_tag);\n\t\t\t\tif (!_TIFFMergeFields(tif,\n\t\t\t\t\t_TIFFCreateAnonField(tif,\n\t\t\t\t\t\tdp->tdir_tag,\n\t\t\t\t\t\t(TIFFDataType) dp->tdir_type),\n\t\t\t\t\t1)) {\n\t\t\t\t\tTIFFWarningExt(tif->tif_clientdata,\n\t\t\t\t\t module,\n\t\t\t\t\t "Registering anonymous field with tag %d (0x%x) failed",\n\t\t\t\t\t dp->tdir_tag,\n\t\t\t\t\t dp->tdir_tag);\n\t\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\t\t} else {\n\t\t\t\t\tTIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);\n\t\t\t\t\tassert(fii!=(uint32)(-1));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dp->tdir_tag!=IGNORE)\n\t\t{\n\t\t\tfip=tif->tif_fields[fii];\n\t\t\tif (fip->field_bit==FIELD_IGNORE)\n\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\telse\n\t\t\t{\n\t\t\t\tswitch (dp->tdir_tag)\n\t\t\t\t{\n\t\t\t\t\tcase TIFFTAG_STRIPOFFSETS:\n\t\t\t\t\tcase TIFFTAG_STRIPBYTECOUNTS:\n\t\t\t\t\tcase TIFFTAG_TILEOFFSETS:\n\t\t\t\t\tcase TIFFTAG_TILEBYTECOUNTS:\n\t\t\t\t\t\tTIFFSetFieldBit(tif,fip->field_bit);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase TIFFTAG_IMAGEWIDTH:\n\t\t\t\t\tcase TIFFTAG_IMAGELENGTH:\n\t\t\t\t\tcase TIFFTAG_IMAGEDEPTH:\n\t\t\t\t\tcase TIFFTAG_TILELENGTH:\n\t\t\t\t\tcase TIFFTAG_TILEWIDTH:\n\t\t\t\t\tcase TIFFTAG_TILEDEPTH:\n\t\t\t\t\tcase TIFFTAG_PLANARCONFIG:\n\t\t\t\t\tcase TIFFTAG_ROWSPERSTRIP:\n\t\t\t\t\tcase TIFFTAG_EXTRASAMPLES:\n\t\t\t\t\t\tif (!TIFFFetchNormalTag(tif,dp,0))\n\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&\n\t (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))\n\t{\n\t\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);\n\t\tif ((dp!=0)&&(dp->tdir_count==1))\n\t\t{\n\t\t\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,\n\t\t\t TIFFTAG_STRIPBYTECOUNTS);\n\t\t\tif ((dp!=0)&&(dp->tdir_count==1))\n\t\t\t{\n\t\t\t\ttif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "Planarconfig tag value assumed incorrect, "\n\t\t\t\t "assuming data is contig instead of chunky");\n\t\t\t}\n\t\t}\n\t}\n\tif (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))\n\t{\n\t\tMissingRequired(tif,"ImageLength");\n\t\tgoto bad;\n\t}\n\tif (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {\n\t\ttif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);\n\t\ttif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;\n\t\ttif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;\n\t\ttif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;\n\t\ttif->tif_flags &= ~TIFF_ISTILED;\n\t} else {\n\t\ttif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);\n\t\ttif->tif_flags |= TIFF_ISTILED;\n\t}\n\tif (!tif->tif_dir.td_nstrips) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "Cannot handle zero number of %s",\n\t\t isTiled(tif) ? "tiles" : "strips");\n\t\tgoto bad;\n\t}\n\ttif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;\n\tif (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\ttif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;\n\tif (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {\n\t\tif ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&\n\t\t (isTiled(tif)==0) &&\n\t\t (tif->tif_dir.td_nstrips==1)) {\n\t\t\tTIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);\n\t\t} else {\n\t\t\tMissingRequired(tif,\n\t\t\t\tisTiled(tif) ? "TileOffsets" : "StripOffsets");\n\t\t\tgoto bad;\n\t\t}\n\t}\n\tfor (di=0, dp=dir; di<dircount; di++, dp++)\n\t{\n\t\tswitch (dp->tdir_tag)\n\t\t{\n\t\t\tcase IGNORE:\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_MINSAMPLEVALUE:\n\t\t\tcase TIFFTAG_MAXSAMPLEVALUE:\n\t\t\tcase TIFFTAG_BITSPERSAMPLE:\n\t\t\tcase TIFFTAG_DATATYPE:\n\t\t\tcase TIFFTAG_SAMPLEFORMAT:\n\t\t\t\t{\n\t\t\t\t\tuint16 value;\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\terr=TIFFReadDirEntryShort(tif,dp,&value);\n\t\t\t\t\tif (err==TIFFReadDirEntryErrCount)\n\t\t\t\t\t\terr=TIFFReadDirEntryPersampleShort(tif,dp,&value);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,TIFFFieldWithTag(tif,dp->tdir_tag)->field_name,0);\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t}\n\t\t\t\t\tif (!TIFFSetField(tif,dp->tdir_tag,value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_SMINSAMPLEVALUE:\n\t\t\tcase TIFFTAG_SMAXSAMPLEVALUE:\n\t\t\t\t{\n\t\t\t\t\tdouble value;\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\terr=TIFFReadDirEntryPersampleDouble(tif,dp,&value);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,TIFFFieldWithTag(tif,dp->tdir_tag)->field_name,0);\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t}\n\t\t\t\t\tif (!TIFFSetField(tif,dp->tdir_tag,value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_STRIPOFFSETS:\n\t\t\tcase TIFFTAG_TILEOFFSETS:\n\t\t\t\tif (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripoffset))\n\t\t\t\t\tgoto bad;\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_STRIPBYTECOUNTS:\n\t\t\tcase TIFFTAG_TILEBYTECOUNTS:\n\t\t\t\tif (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripbytecount))\n\t\t\t\t\tgoto bad;\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_COLORMAP:\n\t\t\tcase TIFFTAG_TRANSFERFUNCTION:\n\t\t\t\t{\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\tuint32 countpersample;\n\t\t\t\t\tuint32 countrequired;\n\t\t\t\t\tuint32 incrementpersample;\n\t\t\t\t\tuint16* value;\n\t\t\t\t\tcountpersample=(1L<<tif->tif_dir.td_bitspersample);\n\t\t\t\t\tif ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))\n\t\t\t\t\t{\n\t\t\t\t\t\tcountrequired=countpersample;\n\t\t\t\t\t\tincrementpersample=0;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tcountrequired=3*countpersample;\n\t\t\t\t\t\tincrementpersample=countpersample;\n\t\t\t\t\t}\n\t\t\t\t\tif (dp->tdir_count!=(uint64)countrequired)\n\t\t\t\t\t\terr=TIFFReadDirEntryErrCount;\n\t\t\t\t\telse\n\t\t\t\t\t\terr=TIFFReadDirEntryShortArray(tif,dp,&value);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,TIFFFieldWithTag(tif,dp->tdir_tag)->field_name,1);\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tTIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);\n\t\t\t\t\t\t_TIFFfree(value);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_OSUBFILETYPE:\n\t\t\t\t{\n\t\t\t\t\tuint16 valueo;\n\t\t\t\t\tuint32 value;\n\t\t\t\t\tif (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tswitch (valueo)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcase OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;\n\t\t\t\t\t\t\tcase OFILETYPE_PAGE: value=FILETYPE_PAGE; break;\n\t\t\t\t\t\t\tdefault: value=0; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (value!=0)\n\t\t\t\t\t\t\tTIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\t(void) TIFFFetchNormalTag(tif, dp, TRUE);\n\t\t\t\tbreak;\n\t\t}\n\t}\n\tif (tif->tif_dir.td_compression==COMPRESSION_OJPEG)\n\t{\n\t\tif (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))\n\t\t{\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Photometric tag is missing, assuming data is YCbCr");\n\t\t\tif (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))\n\t\t\t\tgoto bad;\n\t\t}\n\t\telse if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)\n\t\t{\n\t\t\ttif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Photometric tag value assumed incorrect, "\n\t\t\t "assuming data is YCbCr instead of RGB");\n\t\t}\n\t\tif (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))\n\t\t{\n\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t "BitsPerSample tag is missing, assuming 8 bits per sample");\n\t\t\tif (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))\n\t\t\t\tgoto bad;\n\t\t}\n\t\tif (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))\n\t\t{\n\t\t\tif ((tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)\n\t\t\t || (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR))\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "SamplesPerPixel tag is missing, "\n\t\t\t\t "assuming correct SamplesPerPixel value is 3");\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\telse if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)\n\t\t\t\t || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "SamplesPerPixel tag is missing, "\n\t\t\t\t "assuming correct SamplesPerPixel value is 1");\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t}\n\t}\n\tif (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&\n\t !TIFFFieldSet(tif, FIELD_COLORMAP)) {\n\t\tMissingRequired(tif, "Colormap");\n\t\tgoto bad;\n\t}\n\tif (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)\n\t{\n\t\tif (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {\n\t\t\tif ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&\n\t\t\t tif->tif_dir.td_nstrips > 1) ||\n\t\t\t (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&\n\t\t\t tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {\n\t\t\t MissingRequired(tif, "StripByteCounts");\n\t\t\t goto bad;\n\t\t\t}\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t\t"TIFF directory is missing required "\n\t\t\t\t"\\"%s\\" field, calculating from imagelength",\n\t\t\t\tTIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);\n\t\t\tif (EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n\t\t#define\tBYTECOUNTLOOKSBAD \\\n\t\t ( (tif->tif_dir.td_stripbytecount[0] == 0 && tif->tif_dir.td_stripoffset[0] != 0) || \\\n\t\t (tif->tif_dir.td_compression == COMPRESSION_NONE && \\\n\t\t tif->tif_dir.td_stripbytecount[0] > TIFFGetFileSize(tif) - tif->tif_dir.td_stripoffset[0]) || \\\n\t\t (tif->tif_mode == O_RDONLY && \\\n\t\t tif->tif_dir.td_compression == COMPRESSION_NONE && \\\n\t\t tif->tif_dir.td_stripbytecount[0] < TIFFScanlineSize64(tif) * tif->tif_dir.td_imagelength) )\n\t\t} else if (tif->tif_dir.td_nstrips == 1\n\t\t\t && tif->tif_dir.td_stripoffset[0] != 0\n\t\t\t && BYTECOUNTLOOKSBAD) {\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Bogus \\"%s\\" field, ignoring and calculating from imagelength",\n\t\t\t TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);\n\t\t\tif(EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n\t\t} else if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG\n\t\t\t && tif->tif_dir.td_nstrips > 2\n\t\t\t && tif->tif_dir.td_compression == COMPRESSION_NONE\n\t\t\t && tif->tif_dir.td_stripbytecount[0] != tif->tif_dir.td_stripbytecount[1]\n\t\t\t && tif->tif_dir.td_stripbytecount[0] != 0\n\t\t\t && tif->tif_dir.td_stripbytecount[1] != 0 ) {\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Wrong \\"%s\\" field, ignoring and calculating from imagelength",\n\t\t\t TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);\n\t\t\tif (EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n\t\t}\n\t}\n\tif (dir)\n\t{\n\t\t_TIFFfree(dir);\n\t\tdir=NULL;\n\t}\n\tif (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))\n\t{\n\t\tif (tif->tif_dir.td_bitspersample>=16)\n\t\t\ttif->tif_dir.td_maxsamplevalue=0xFFFF;\n\t\telse\n\t\t\ttif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);\n\t}\n\tif (tif->tif_dir.td_nstrips > 1) {\n\t\tuint32 strip;\n\t\ttif->tif_dir.td_stripbytecountsorted = 1;\n\t\tfor (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {\n\t\t\tif (tif->tif_dir.td_stripoffset[strip - 1] >\n\t\t\t tif->tif_dir.td_stripoffset[strip]) {\n\t\t\t\ttif->tif_dir.td_stripbytecountsorted = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\t(*tif->tif_fixuptags)(tif);\n\tif ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&\n\t (tif->tif_dir.td_nstrips==1)&&\n\t (tif->tif_dir.td_compression==COMPRESSION_NONE)&&\n\t ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))\n\t\tChopUpSingleUncompressedStrip(tif);\n\ttif->tif_flags &= ~TIFF_DIRTYDIRECT;\n\ttif->tif_flags &= ~TIFF_DIRTYSTRIP;\n\ttif->tif_row = (uint32) -1;\n\ttif->tif_curstrip = (uint32) -1;\n\ttif->tif_col = (uint32) -1;\n\ttif->tif_curtile = (uint32) -1;\n\ttif->tif_tilesize = (tmsize_t) -1;\n\ttif->tif_scanlinesize = TIFFScanlineSize(tif);\n\tif (!tif->tif_scanlinesize) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "Cannot handle zero scanline size");\n\t\treturn (0);\n\t}\n\tif (isTiled(tif)) {\n\t\ttif->tif_tilesize = TIFFTileSize(tif);\n\t\tif (!tif->tif_tilesize) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Cannot handle zero tile size");\n\t\t\treturn (0);\n\t\t}\n\t} else {\n\t\tif (!TIFFStripSize(tif)) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Cannot handle zero strip size");\n\t\t\treturn (0);\n\t\t}\n\t}\n\treturn (1);\nbad:\n\tif (dir)\n\t\t_TIFFfree(dir);\n\treturn (0);\n}', 'int\nTIFFDefaultDirectory(TIFF* tif)\n{\n\tregister TIFFDirectory* td = &tif->tif_dir;\n\tconst TIFFFieldArray* tiffFieldArray;\n\ttiffFieldArray = _TIFFGetFields();\n\t_TIFFSetupFields(tif, tiffFieldArray);\n\t_TIFFmemset(td, 0, sizeof (*td));\n\ttd->td_fillorder = FILLORDER_MSB2LSB;\n\ttd->td_bitspersample = 1;\n\ttd->td_threshholding = THRESHHOLD_BILEVEL;\n\ttd->td_orientation = ORIENTATION_TOPLEFT;\n\ttd->td_samplesperpixel = 1;\n\ttd->td_rowsperstrip = (uint32) -1;\n\ttd->td_tilewidth = 0;\n\ttd->td_tilelength = 0;\n\ttd->td_tiledepth = 1;\n\ttd->td_stripbytecountsorted = 1;\n\ttd->td_resolutionunit = RESUNIT_INCH;\n\ttd->td_sampleformat = SAMPLEFORMAT_UINT;\n\ttd->td_imagedepth = 1;\n\ttd->td_ycbcrsubsampling[0] = 2;\n\ttd->td_ycbcrsubsampling[1] = 2;\n\ttd->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;\n\ttif->tif_postdecode = _TIFFNoPostDecode;\n\ttif->tif_foundfield = NULL;\n\ttif->tif_tagmethods.vsetfield = _TIFFVSetField;\n\ttif->tif_tagmethods.vgetfield = _TIFFVGetField;\n\ttif->tif_tagmethods.printdir = NULL;\n\tif (_TIFFextender)\n\t\t(*_TIFFextender)(tif);\n\t(void) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);\n\ttif->tif_flags &= ~TIFF_DIRTYDIRECT;\n\ttif->tif_flags &= ~TIFF_ISTILED;\n\treturn (1);\n}', 'static int\nEstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)\n{\n\tstatic const char module[] = "EstimateStripByteCounts";\n\tTIFFDirEntry *dp;\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 strip;\n\tif (td->td_stripbytecount)\n\t\t_TIFFfree(td->td_stripbytecount);\n\ttd->td_stripbytecount = (uint64*)\n\t _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),\n\t\t"for \\"StripByteCounts\\" array");\n if( td->td_stripbytecount == NULL )\n return -1;\n\tif (td->td_compression != COMPRESSION_NONE) {\n\t\tuint64 space;\n\t\tuint64 filesize;\n\t\tuint16 n;\n\t\tfilesize = TIFFGetFileSize(tif);\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\tspace=sizeof(TIFFHeaderClassic)+2+dircount*12+4;\n\t\telse\n\t\t\tspace=sizeof(TIFFHeaderBig)+8+dircount*20+8;\n\t\tfor (dp = dir, n = dircount; n > 0; n--, dp++)\n\t\t{\n\t\t\tuint32 typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);\n\t\t\tuint64 datasize;\n\t\t\ttypewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);\n\t\t\tif (typewidth == 0) {\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t "Cannot determine size of unknown tag type %d",\n\t\t\t\t dp->tdir_type);\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t\tdatasize=(uint64)typewidth*dp->tdir_count;\n\t\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\t{\n\t\t\t\tif (datasize<=4)\n\t\t\t\t\tdatasize=0;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (datasize<=8)\n\t\t\t\t\tdatasize=0;\n\t\t\t}\n\t\t\tspace+=datasize;\n\t\t}\n\t\tspace = filesize - space;\n\t\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\t\tspace /= td->td_samplesperpixel;\n\t\tfor (strip = 0; strip < td->td_nstrips; strip++)\n\t\t\ttd->td_stripbytecount[strip] = space;\n\t\tstrip--;\n\t\tif (td->td_stripoffset[strip]+td->td_stripbytecount[strip] > filesize)\n\t\t\ttd->td_stripbytecount[strip] = filesize - td->td_stripoffset[strip];\n\t} else if (isTiled(tif)) {\n\t\tuint64 bytespertile = TIFFTileSize64(tif);\n\t\tfor (strip = 0; strip < td->td_nstrips; strip++)\n\t\t td->td_stripbytecount[strip] = bytespertile;\n\t} else {\n\t\tuint64 rowbytes = TIFFScanlineSize64(tif);\n\t\tuint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;\n\t\tfor (strip = 0; strip < td->td_nstrips; strip++)\n\t\t\ttd->td_stripbytecount[strip] = rowbytes * rowsperstrip;\n\t}\n\tTIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);\n\tif (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))\n\t\ttd->td_rowsperstrip = td->td_imagelength;\n\treturn 1;\n}']
|
1,246 | 0 |
https://github.com/libav/libav/blob/32a15a2441c4bfc83b2934f617d64d196492bdde/libavcodec/imgconvert.c/#L1187
|
static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
int width, int height)
{
uint8_t *src_m1, *src_0, *src_p1, *src_p2;
int y;
uint8_t *buf;
buf = (uint8_t*)av_malloc(width);
src_m1 = src1;
memcpy(buf,src_m1,width);
src_0=&src_m1[src_wrap];
src_p1=&src_0[src_wrap];
src_p2=&src_p1[src_wrap];
for(y=0;y<(height-2);y+=2) {
deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
src_m1 = src_p1;
src_0 = src_p2;
src_p1 += 2*src_wrap;
src_p2 += 2*src_wrap;
}
deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
av_free(buf);
}
|
['static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,\n int width, int height)\n{\n uint8_t *src_m1, *src_0, *src_p1, *src_p2;\n int y;\n uint8_t *buf;\n buf = (uint8_t*)av_malloc(width);\n src_m1 = src1;\n memcpy(buf,src_m1,width);\n src_0=&src_m1[src_wrap];\n src_p1=&src_0[src_wrap];\n src_p2=&src_p1[src_wrap];\n for(y=0;y<(height-2);y+=2) {\n deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);\n src_m1 = src_p1;\n src_0 = src_p2;\n src_p1 += 2*src_wrap;\n src_p2 += 2*src_wrap;\n }\n deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);\n av_free(buf);\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
|
1,247 | 0 |
https://github.com/libav/libav/blob/28240a60c1b5ce276e947ba013271ec009adc078/libavcodec/hevc.c/#L281
|
static void pred_weight_table(HEVCContext *s, GetBitContext *gb)
{
int i = 0;
int j = 0;
uint8_t luma_weight_l0_flag[16];
uint8_t chroma_weight_l0_flag[16];
uint8_t luma_weight_l1_flag[16];
uint8_t chroma_weight_l1_flag[16];
s->sh.luma_log2_weight_denom = get_ue_golomb_long(gb);
if (s->sps->chroma_format_idc != 0) {
int delta = get_se_golomb(gb);
s->sh.chroma_log2_weight_denom = av_clip_c(s->sh.luma_log2_weight_denom + delta, 0, 7);
}
for (i = 0; i < s->sh.nb_refs[L0]; i++) {
luma_weight_l0_flag[i] = get_bits1(gb);
if (!luma_weight_l0_flag[i]) {
s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
s->sh.luma_offset_l0[i] = 0;
}
}
if (s->sps->chroma_format_idc != 0) {
for (i = 0; i < s->sh.nb_refs[L0]; i++)
chroma_weight_l0_flag[i] = get_bits1(gb);
} else {
for (i = 0; i < s->sh.nb_refs[L0]; i++)
chroma_weight_l0_flag[i] = 0;
}
for (i = 0; i < s->sh.nb_refs[L0]; i++) {
if (luma_weight_l0_flag[i]) {
int delta_luma_weight_l0 = get_se_golomb(gb);
s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
s->sh.luma_offset_l0[i] = get_se_golomb(gb);
}
if (chroma_weight_l0_flag[i]) {
for (j = 0; j < 2; j++) {
int delta_chroma_weight_l0 = get_se_golomb(gb);
int delta_chroma_offset_l0 = get_se_golomb(gb);
s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
s->sh.chroma_offset_l0[i][j] = av_clip_c((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
>> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
}
} else {
s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
s->sh.chroma_offset_l0[i][0] = 0;
s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
s->sh.chroma_offset_l0[i][1] = 0;
}
}
if (s->sh.slice_type == B_SLICE) {
for (i = 0; i < s->sh.nb_refs[L1]; i++) {
luma_weight_l1_flag[i] = get_bits1(gb);
if (!luma_weight_l1_flag[i]) {
s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
s->sh.luma_offset_l1[i] = 0;
}
}
if (s->sps->chroma_format_idc != 0) {
for (i = 0; i < s->sh.nb_refs[L1]; i++)
chroma_weight_l1_flag[i] = get_bits1(gb);
} else {
for (i = 0; i < s->sh.nb_refs[L1]; i++)
chroma_weight_l1_flag[i] = 0;
}
for (i = 0; i < s->sh.nb_refs[L1]; i++) {
if (luma_weight_l1_flag[i]) {
int delta_luma_weight_l1 = get_se_golomb(gb);
s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
s->sh.luma_offset_l1[i] = get_se_golomb(gb);
}
if (chroma_weight_l1_flag[i]) {
for (j = 0; j < 2; j++) {
int delta_chroma_weight_l1 = get_se_golomb(gb);
int delta_chroma_offset_l1 = get_se_golomb(gb);
s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
s->sh.chroma_offset_l1[i][j] = av_clip_c((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
>> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
}
} else {
s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
s->sh.chroma_offset_l1[i][0] = 0;
s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
s->sh.chroma_offset_l1[i][1] = 0;
}
}
}
}
|
['static void pred_weight_table(HEVCContext *s, GetBitContext *gb)\n{\n int i = 0;\n int j = 0;\n uint8_t luma_weight_l0_flag[16];\n uint8_t chroma_weight_l0_flag[16];\n uint8_t luma_weight_l1_flag[16];\n uint8_t chroma_weight_l1_flag[16];\n s->sh.luma_log2_weight_denom = get_ue_golomb_long(gb);\n if (s->sps->chroma_format_idc != 0) {\n int delta = get_se_golomb(gb);\n s->sh.chroma_log2_weight_denom = av_clip_c(s->sh.luma_log2_weight_denom + delta, 0, 7);\n }\n for (i = 0; i < s->sh.nb_refs[L0]; i++) {\n luma_weight_l0_flag[i] = get_bits1(gb);\n if (!luma_weight_l0_flag[i]) {\n s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;\n s->sh.luma_offset_l0[i] = 0;\n }\n }\n if (s->sps->chroma_format_idc != 0) {\n for (i = 0; i < s->sh.nb_refs[L0]; i++)\n chroma_weight_l0_flag[i] = get_bits1(gb);\n } else {\n for (i = 0; i < s->sh.nb_refs[L0]; i++)\n chroma_weight_l0_flag[i] = 0;\n }\n for (i = 0; i < s->sh.nb_refs[L0]; i++) {\n if (luma_weight_l0_flag[i]) {\n int delta_luma_weight_l0 = get_se_golomb(gb);\n s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;\n s->sh.luma_offset_l0[i] = get_se_golomb(gb);\n }\n if (chroma_weight_l0_flag[i]) {\n for (j = 0; j < 2; j++) {\n int delta_chroma_weight_l0 = get_se_golomb(gb);\n int delta_chroma_offset_l0 = get_se_golomb(gb);\n s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;\n s->sh.chroma_offset_l0[i][j] = av_clip_c((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])\n >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);\n }\n } else {\n s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;\n s->sh.chroma_offset_l0[i][0] = 0;\n s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;\n s->sh.chroma_offset_l0[i][1] = 0;\n }\n }\n if (s->sh.slice_type == B_SLICE) {\n for (i = 0; i < s->sh.nb_refs[L1]; i++) {\n luma_weight_l1_flag[i] = get_bits1(gb);\n if (!luma_weight_l1_flag[i]) {\n s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;\n s->sh.luma_offset_l1[i] = 0;\n }\n }\n if (s->sps->chroma_format_idc != 0) {\n for (i = 0; i < s->sh.nb_refs[L1]; i++)\n chroma_weight_l1_flag[i] = get_bits1(gb);\n } else {\n for (i = 0; i < s->sh.nb_refs[L1]; i++)\n chroma_weight_l1_flag[i] = 0;\n }\n for (i = 0; i < s->sh.nb_refs[L1]; i++) {\n if (luma_weight_l1_flag[i]) {\n int delta_luma_weight_l1 = get_se_golomb(gb);\n s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;\n s->sh.luma_offset_l1[i] = get_se_golomb(gb);\n }\n if (chroma_weight_l1_flag[i]) {\n for (j = 0; j < 2; j++) {\n int delta_chroma_weight_l1 = get_se_golomb(gb);\n int delta_chroma_offset_l1 = get_se_golomb(gb);\n s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;\n s->sh.chroma_offset_l1[i][j] = av_clip_c((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])\n >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);\n }\n } else {\n s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;\n s->sh.chroma_offset_l1[i][0] = 0;\n s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;\n s->sh.chroma_offset_l1[i][1] = 0;\n }\n }\n }\n}']
|
1,248 | 0 |
https://github.com/openssl/openssl/blob/a69de3f2014ab55329f43633714c9c153cb5cb30/test/testutil/tests.c/#L411
|
int test_BN_abs_eq_word(const char *file, int line, const char *bns,
const char *ws, const BIGNUM *a, BN_ULONG w)
{
BIGNUM *bw, *aa;
if (a != NULL && BN_abs_is_word(a, w))
return 1;
bw = BN_new();
aa = BN_dup(a);
BN_set_negative(aa, 0);
BN_set_word(bw, w);
test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "abs==",
aa, bw);
BN_free(bw);
BN_free(aa);
return 0;
}
|
['int test_BN_abs_eq_word(const char *file, int line, const char *bns,\n const char *ws, const BIGNUM *a, BN_ULONG w)\n{\n BIGNUM *bw, *aa;\n if (a != NULL && BN_abs_is_word(a, w))\n return 1;\n bw = BN_new();\n aa = BN_dup(a);\n BN_set_negative(aa, 0);\n BN_set_word(bw, w);\n test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "abs==",\n aa, bw);\n BN_free(bw);\n BN_free(aa);\n return 0;\n}', 'int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)\n{\n return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));\n}', 'BIGNUM *BN_dup(const BIGNUM *a)\n{\n BIGNUM *t;\n if (a == NULL)\n return NULL;\n bn_check_top(a);\n t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();\n if (t == NULL)\n return NULL;\n if (!BN_copy(t, a)) {\n BN_free(t);\n return NULL;\n }\n bn_check_top(t);\n return t;\n}', 'int BN_get_flags(const BIGNUM *b, int n)\n{\n return b->flags & n;\n}', 'void BN_set_negative(BIGNUM *a, int b)\n{\n if (b && !BN_is_zero(a))\n a->neg = 1;\n else\n a->neg = 0;\n}']
|
1,249 | 0 |
https://github.com/libav/libav/blob/cb48e245e6e770f146220fac0a8bd4dc1a5e006c/ffmpeg.c/#L1252
|
static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
int frame_size)
{
AVCodecContext *enc;
int frame_number;
double ti1, bitrate, avg_bitrate;
if (!vstats_file) {
vstats_file = fopen(vstats_filename, "w");
if (!vstats_file) {
perror("fopen");
ffmpeg_exit(1);
}
}
enc = ost->st->codec;
if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
frame_number = ost->frame_number;
fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);
if (enc->flags&CODEC_FLAG_PSNR)
fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
fprintf(vstats_file,"f_size= %6d ", frame_size);
ti1 = ost->sync_opts * av_q2d(enc->time_base);
if (ti1 < 0.01)
ti1 = 0.01;
bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
(double)video_size / 1024, ti1, bitrate, avg_bitrate);
fprintf(vstats_file,"type= %c\n", av_get_pict_type_char(enc->coded_frame->pict_type));
}
}
|
['static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,\n int frame_size)\n{\n AVCodecContext *enc;\n int frame_number;\n double ti1, bitrate, avg_bitrate;\n if (!vstats_file) {\n vstats_file = fopen(vstats_filename, "w");\n if (!vstats_file) {\n perror("fopen");\n ffmpeg_exit(1);\n }\n }\n enc = ost->st->codec;\n if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {\n frame_number = ost->frame_number;\n fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);\n if (enc->flags&CODEC_FLAG_PSNR)\n fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));\n fprintf(vstats_file,"f_size= %6d ", frame_size);\n ti1 = ost->sync_opts * av_q2d(enc->time_base);\n if (ti1 < 0.01)\n ti1 = 0.01;\n bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;\n avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;\n fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",\n (double)video_size / 1024, ti1, bitrate, avg_bitrate);\n fprintf(vstats_file,"type= %c\\n", av_get_pict_type_char(enc->coded_frame->pict_type));\n }\n}']
|
1,250 | 0 |
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/asn1/x_cinf.c/#L173
|
X509_CINF *X509_CINF_new(void)
{
X509_CINF *ret=NULL;
ASN1_CTX c;
M_ASN1_New_Malloc(ret,X509_CINF);
ret->version=NULL;
M_ASN1_New(ret->serialNumber,ASN1_INTEGER_new);
M_ASN1_New(ret->signature,X509_ALGOR_new);
M_ASN1_New(ret->issuer,X509_NAME_new);
M_ASN1_New(ret->validity,X509_VAL_new);
M_ASN1_New(ret->subject,X509_NAME_new);
M_ASN1_New(ret->key,X509_PUBKEY_new);
ret->issuerUID=NULL;
ret->subjectUID=NULL;
ret->extensions=NULL;
return(ret);
M_ASN1_New_Error(ASN1_F_X509_CINF_NEW);
}
|
['X509_CINF *X509_CINF_new(void)\n\t{\n\tX509_CINF *ret=NULL;\n\tASN1_CTX c;\n\tM_ASN1_New_Malloc(ret,X509_CINF);\n\tret->version=NULL;\n\tM_ASN1_New(ret->serialNumber,ASN1_INTEGER_new);\n\tM_ASN1_New(ret->signature,X509_ALGOR_new);\n\tM_ASN1_New(ret->issuer,X509_NAME_new);\n\tM_ASN1_New(ret->validity,X509_VAL_new);\n\tM_ASN1_New(ret->subject,X509_NAME_new);\n\tM_ASN1_New(ret->key,X509_PUBKEY_new);\n\tret->issuerUID=NULL;\n\tret->subjectUID=NULL;\n\tret->extensions=NULL;\n\treturn(ret);\n\tM_ASN1_New_Error(ASN1_F_X509_CINF_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}', 'X509_ALGOR *X509_ALGOR_new(void)\n\t{\n\tX509_ALGOR *ret=NULL;\n\tASN1_CTX c;\n\tM_ASN1_New_Malloc(ret,X509_ALGOR);\n\tret->algorithm=OBJ_nid2obj(NID_undef);\n\tret->parameter=NULL;\n\treturn(ret);\n\tM_ASN1_New_Error(ASN1_F_X509_ALGOR_NEW);\n\t}', 'ASN1_OBJECT *OBJ_nid2obj(int n)\n\t{\n\tADDED_OBJ ad,*adp;\n\tASN1_OBJECT ob;\n\tif ((n >= 0) && (n < NUM_NID))\n\t\t{\n\t\tif ((n != NID_undef) && (nid_objs[n].nid == NID_undef))\n\t\t\t{\n\t\t\tOBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\treturn((ASN1_OBJECT *)&(nid_objs[n]));\n\t\t}\n\telse if (added == NULL)\n\t\treturn(NULL);\n\telse\n\t\t{\n\t\tad.type=ADDED_NID;\n\t\tad.obj= &ob;\n\t\tob.nid=n;\n\t\tadp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);\n\t\tif (adp != NULL)\n\t\t\treturn(adp->obj);\n\t\telse\n\t\t\t{\n\t\t\tOBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\t}\n\t}']
|
1,251 | 0 |
https://github.com/libav/libav/blob/d6dc5d15af0d8617611281a34a2c3f9ced149ccf/libavcodec/h264_refs.c/#L154
|
static void h264_initialise_ref_list(H264Context *h, H264SliceContext *sl)
{
int i, len;
if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
H264Picture *sorted[32];
int cur_poc, list;
int lens[2];
if (FIELD_PICTURE(h))
cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD];
else
cur_poc = h->cur_pic_ptr->poc;
for (list = 0; list < 2; list++) {
len = add_sorted(sorted, h->short_ref, h->short_ref_count, cur_poc, 1 ^ list);
len += add_sorted(sorted + len, h->short_ref, h->short_ref_count, cur_poc, 0 ^ list);
assert(len <= 32);
len = build_def_list(sl->ref_list[list], FF_ARRAY_ELEMS(sl->ref_list[0]),
sorted, len, 0, h->picture_structure);
len += build_def_list(sl->ref_list[list] + len,
FF_ARRAY_ELEMS(sl->ref_list[0]) - len,
h->long_ref, 16, 1, h->picture_structure);
if (len < sl->ref_count[list])
memset(&sl->ref_list[list][len], 0, sizeof(H264Ref) * (sl->ref_count[list] - len));
lens[list] = len;
}
if (lens[0] == lens[1] && lens[1] > 1) {
for (i = 0; i < lens[0] &&
sl->ref_list[0][i].parent->f->buf[0]->buffer ==
sl->ref_list[1][i].parent->f->buf[0]->buffer; i++);
if (i == lens[0]) {
FFSWAP(H264Ref, sl->ref_list[1][0], sl->ref_list[1][1]);
}
}
} else {
len = build_def_list(sl->ref_list[0], FF_ARRAY_ELEMS(sl->ref_list[0]),
h->short_ref, h->short_ref_count, 0, h->picture_structure);
len += build_def_list(sl->ref_list[0] + len,
FF_ARRAY_ELEMS(sl->ref_list[0]) - len,
h-> long_ref, 16, 1, h->picture_structure);
if (len < sl->ref_count[0])
memset(&sl->ref_list[0][len], 0, sizeof(H264Ref) * (sl->ref_count[0] - len));
}
#ifdef TRACE
for (i = 0; i < sl->ref_count[0]; i++) {
ff_tlog(h->avctx, "List0: %s fn:%d 0x%p\n",
(sl->ref_list[0][i].long_ref ? "LT" : "ST"),
sl->ref_list[0][i].pic_id,
sl->ref_list[0][i].f->data[0]);
}
if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
for (i = 0; i < sl->ref_count[1]; i++) {
ff_tlog(h->avctx, "List1: %s fn:%d 0x%p\n",
(sl->ref_list[1][i].long_ref ? "LT" : "ST"),
sl->ref_list[1][i].pic_id,
sl->ref_list[1][i].f->data[0]);
}
}
#endif
}
|
['static void h264_initialise_ref_list(H264Context *h, H264SliceContext *sl)\n{\n int i, len;\n if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {\n H264Picture *sorted[32];\n int cur_poc, list;\n int lens[2];\n if (FIELD_PICTURE(h))\n cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD];\n else\n cur_poc = h->cur_pic_ptr->poc;\n for (list = 0; list < 2; list++) {\n len = add_sorted(sorted, h->short_ref, h->short_ref_count, cur_poc, 1 ^ list);\n len += add_sorted(sorted + len, h->short_ref, h->short_ref_count, cur_poc, 0 ^ list);\n assert(len <= 32);\n len = build_def_list(sl->ref_list[list], FF_ARRAY_ELEMS(sl->ref_list[0]),\n sorted, len, 0, h->picture_structure);\n len += build_def_list(sl->ref_list[list] + len,\n FF_ARRAY_ELEMS(sl->ref_list[0]) - len,\n h->long_ref, 16, 1, h->picture_structure);\n if (len < sl->ref_count[list])\n memset(&sl->ref_list[list][len], 0, sizeof(H264Ref) * (sl->ref_count[list] - len));\n lens[list] = len;\n }\n if (lens[0] == lens[1] && lens[1] > 1) {\n for (i = 0; i < lens[0] &&\n sl->ref_list[0][i].parent->f->buf[0]->buffer ==\n sl->ref_list[1][i].parent->f->buf[0]->buffer; i++);\n if (i == lens[0]) {\n FFSWAP(H264Ref, sl->ref_list[1][0], sl->ref_list[1][1]);\n }\n }\n } else {\n len = build_def_list(sl->ref_list[0], FF_ARRAY_ELEMS(sl->ref_list[0]),\n h->short_ref, h->short_ref_count, 0, h->picture_structure);\n len += build_def_list(sl->ref_list[0] + len,\n FF_ARRAY_ELEMS(sl->ref_list[0]) - len,\n h-> long_ref, 16, 1, h->picture_structure);\n if (len < sl->ref_count[0])\n memset(&sl->ref_list[0][len], 0, sizeof(H264Ref) * (sl->ref_count[0] - len));\n }\n#ifdef TRACE\n for (i = 0; i < sl->ref_count[0]; i++) {\n ff_tlog(h->avctx, "List0: %s fn:%d 0x%p\\n",\n (sl->ref_list[0][i].long_ref ? "LT" : "ST"),\n sl->ref_list[0][i].pic_id,\n sl->ref_list[0][i].f->data[0]);\n }\n if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {\n for (i = 0; i < sl->ref_count[1]; i++) {\n ff_tlog(h->avctx, "List1: %s fn:%d 0x%p\\n",\n (sl->ref_list[1][i].long_ref ? "LT" : "ST"),\n sl->ref_list[1][i].pic_id,\n sl->ref_list[1][i].f->data[0]);\n }\n }\n#endif\n}']
|
1,252 | 0 |
https://github.com/openssl/openssl/blob/361512da0d900ba276096cbd152e304d402aca65/crypto/bn/bn_asm.c/#L406
|
BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
{
BN_ULONG t1,t2;
int c=0;
assert(n >= 0);
if (n <= 0) return((BN_ULONG)0);
#ifndef OPENSSL_SMALL_FOOTPRINT
while (n&~3)
{
t1=a[0]; t2=b[0];
r[0]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
t1=a[1]; t2=b[1];
r[1]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
t1=a[2]; t2=b[2];
r[2]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
t1=a[3]; t2=b[3];
r[3]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
a+=4; b+=4; r+=4; n-=4;
}
#endif
while (n)
{
t1=a[0]; t2=b[0];
r[0]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
a++; b++; r++; n--;
}
return(c);
}
|
['int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,bits,ret=0,window,wvalue;\n\tint top;\n \tBIGNUM *r;\n\tBN_MONT_CTX *mont=NULL;\n\tint numPowers;\n\tunsigned char *powerbufFree=NULL;\n\tint powerbufLen = 0;\n\tunsigned char *powerbuf=NULL;\n\tBIGNUM computeTemp, *am=NULL;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\ttop = m->top;\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tret = BN_one(rr);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\tr = BN_CTX_get(ctx);\n\tif (r == NULL) goto err;\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n\t\t{\n\t\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\t\tif (!BN_MONT_CTX_set(mont,m,ctx)) goto err;\n\t\t}\n\twindow = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(OPENSSL_BN_ASM_MONT5)\n\tif (window==6 && bits<=1024) window=5;\n#endif\n \tbits = ((bits+window-1)/window)*window;\n\tnumPowers = 1 << window;\n\tpowerbufLen = sizeof(m->d[0])*(top*numPowers +\n\t\t\t\t(top>numPowers?top:numPowers));\n\tif (powerbufLen < 3072)\n\t\tpowerbufFree = alloca(powerbufLen+MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n\telse if ((powerbufFree=(unsigned char*)OPENSSL_malloc(powerbufLen+MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL)\n\t\tgoto err;\n\tpowerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n\tmemset(powerbuf, 0, powerbufLen);\n\tif (powerbufLen < 3072)\n\t\tpowerbufFree = NULL;\n\tcomputeTemp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0])*top*numPowers);\n\tcomputeTemp.top = computeTemp.dmax = top;\n\tcomputeTemp.neg = 0;\n\tcomputeTemp.flags = BN_FLG_STATIC_DATA;\n \tif (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;\n\tam = BN_CTX_get(ctx);\n\tif (am==NULL) goto err;\n\tif (a->neg || BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tif (!BN_mod(am,a,m,ctx))\t\tgoto err;\n\t\tif (!BN_to_montgomery(am,am,mont,ctx))\tgoto err;\n\t\t}\n\telse\tif (!BN_to_montgomery(am,a,mont,ctx))\tgoto err;\n\tif (!BN_copy(&computeTemp, am)) goto err;\n\tif (bn_wexpand(am,top)==NULL ||\tbn_wexpand(r,top)==NULL)\n\t\tgoto err;\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window==5)\n\t{\n\tvoid bn_mul_mont_gather5(BN_ULONG *rp,const BN_ULONG *ap,\n\t\t\tconst void *table,const BN_ULONG *np,\n\t\t\tconst BN_ULONG *n0,int num,int power);\n\tvoid bn_scatter5(const BN_ULONG *inp,size_t num,\n\t\t\tvoid *table,size_t power);\n\tBN_ULONG *acc, *np=mont->N.d, *n0=mont->n0;\n\tbn_scatter5(r->d,r->top,powerbuf,0);\n\tbn_scatter5(am->d,am->top,powerbuf,1);\n\tacc = computeTemp.d;\n#if 0\n\tfor (i=2; i<32; i++)\n\t\t{\n\t\tbn_mul_mont_gather5(acc,am->d,powerbuf,np,n0,top,i-1);\n\t\tbn_scatter5(acc,top,powerbuf,i);\n\t\t}\n#else\n\tfor (i=2; i<32; i*=2)\n\t\t{\n\t\tbn_mul_mont(acc,acc,acc,np,n0,top);\n\t\tbn_scatter5(acc,top,powerbuf,i);\n\t\t}\n\tfor (i=3; i<8; i+=2)\n\t\t{\n\t\tint j;\n\t\tbn_mul_mont_gather5(acc,am->d,powerbuf,np,n0,top,i-1);\n\t\tbn_scatter5(acc,top,powerbuf,i);\n\t\tfor (j=2*i; j<32; j*=2)\n\t\t\t{\n\t\t\tbn_mul_mont(acc,acc,acc,np,n0,top);\n\t\t\tbn_scatter5(acc,top,powerbuf,j);\n\t\t\t}\n\t\t}\n\tfor (; i<16; i+=2)\n\t\t{\n\t\tbn_mul_mont_gather5(acc,am->d,powerbuf,np,n0,top,i-1);\n\t\tbn_scatter5(acc,top,powerbuf,i);\n\t\tbn_mul_mont(acc,acc,acc,np,n0,top);\n\t\tbn_scatter5(acc,top,powerbuf,2*i);\n\t\t}\n\tfor (; i<32; i+=2)\n\t\t{\n\t\tbn_mul_mont_gather5(acc,am->d,powerbuf,np,n0,top,i-1);\n\t\tbn_scatter5(acc,top,powerbuf,i);\n\t\t}\n#endif\n\tacc = r->d;\n\tbits--;\n\twhile (bits >= 0)\n\t\t{\n\t\tfor (wvalue=0, i=0; i<5; i++,bits--)\n\t\t\twvalue = (wvalue<<1)+BN_is_bit_set(p,bits);\n\t\tbn_mul_mont(acc,acc,acc,np,n0,top);\n\t\tbn_mul_mont(acc,acc,acc,np,n0,top);\n\t\tbn_mul_mont(acc,acc,acc,np,n0,top);\n\t\tbn_mul_mont(acc,acc,acc,np,n0,top);\n\t\tbn_mul_mont(acc,acc,acc,np,n0,top);\n\t\tbn_mul_mont_gather5(acc,acc,powerbuf,np,n0,top,wvalue);\n\t\t}\n\tr->top=top;\n\tbn_correct_top(r);\n\t}\n else\n#endif\n\t{\n\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(r, top, powerbuf, 0, numPowers)) goto err;\n\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(am, top, powerbuf, 1, numPowers)) goto err;\n\tif (window > 1)\n\t\t{\n\t\tfor (i=2; i<numPowers; i++)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(&computeTemp,am,&computeTemp,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(&computeTemp, top, powerbuf, i, numPowers)) goto err;\n\t\t\t}\n\t\t}\n\tbits--;\n \twhile (bits >= 0)\n \t\t{\n \t\twvalue=0;\n \t\tfor (i=0; i<window; i++,bits--)\n \t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\tgoto err;\n\t\t\twvalue = (wvalue<<1)+BN_is_bit_set(p,bits);\n \t\t\t}\n\t\tif (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&computeTemp, top, powerbuf, wvalue, numPowers)) goto err;\n \t\tif (!BN_mod_mul_montgomery(r,r,&computeTemp,mont,ctx)) goto err;\n \t\t}\n\t}\n\tif (!BN_from_montgomery(rr,r,mont,ctx)) goto err;\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tif (powerbuf!=NULL)\n\t\t{\n\t\tOPENSSL_cleanse(powerbuf,powerbufLen);\n\t\tif (powerbufFree) OPENSSL_free(powerbufFree);\n\t\t}\n \tif (am!=NULL) BN_clear(am);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'const BIGNUM *BN_value_one(void)\n\t{\n\tstatic const BN_ULONG data_one=1L;\n\tstatic const BIGNUM const_one={(BN_ULONG *)&data_one,1,1,0,BN_FLG_STATIC_DATA};\n\treturn(&const_one);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n\t\t\t BN_MONT_CTX *mont, BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp;\n\tint ret=0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n\tint num = mont->N.top;\n\tif (num>1 && a->top==num && b->top==num)\n\t\t{\n\t\tif (bn_wexpand(r,num) == NULL) return(0);\n\t\tif (bn_mul_mont(r->d,a->d,b->d,mont->N.d,mont->n0,num))\n\t\t\t{\n\t\t\tr->neg = a->neg^b->neg;\n\t\t\tr->top = num;\n\t\t\tbn_correct_top(r);\n\t\t\treturn(1);\n\t\t\t}\n\t\t}\n#endif\n\tBN_CTX_start(ctx);\n\ttmp = BN_CTX_get(ctx);\n\tif (tmp == NULL) goto err;\n\tbn_check_top(tmp);\n\tif (a == b)\n\t\t{\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n#ifdef MONT_WORD\n\tif (!BN_from_montgomery_word(r,tmp,mont)) goto err;\n#else\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n#endif\n\tbn_check_top(r);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tint top,al,bl;\n\tBIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tint i;\n#endif\n#ifdef BN_RECURSION\n\tBIGNUM *t=NULL;\n\tint j=0,k;\n#endif\n#ifdef BN_COUNT\n\tfprintf(stderr,"BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tBN_CTX_start(ctx);\n\tif ((r == a) || (r == b))\n\t\t{\n\t\tif ((rr = BN_CTX_get(ctx)) == NULL) goto err;\n\t\t}\n\telse\n\t\trr = r;\n\trr->neg=a->neg^b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\ti = al-bl;\n#endif\n#ifdef BN_MUL_COMBA\n\tif (i == 0)\n\t\t{\n# if 0\n\t\tif (al == 4)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,8) == NULL) goto err;\n\t\t\trr->top=8;\n\t\t\tbn_mul_comba4(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n# endif\n\t\tif (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) goto err;\n\t\t\trr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\tif ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (i >= -1 && i <= 1)\n\t\t\t{\n\t\t\tif (i >= 0)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)al);\n\t\t\t\t}\n\t\t\tif (i == -1)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)bl);\n\t\t\t\t}\n\t\t\tj = 1<<(j-1);\n\t\t\tassert(j <= al || j <= bl);\n\t\t\tk = j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (t == NULL)\n\t\t\t\tgoto err;\n\t\t\tif (al > j || bl > j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#if 0\n\t\tif (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)b;\n\t\t\tif (bn_wexpand(tmp_bn,al) == NULL) goto err;\n\t\t\ttmp_bn->d[bl]=0;\n\t\t\tbl++;\n\t\t\ti--;\n\t\t\t}\n\t\telse if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)a;\n\t\t\tif (bn_wexpand(tmp_bn,bl) == NULL) goto err;\n\t\t\ttmp_bn->d[al]=0;\n\t\t\tal++;\n\t\t\ti++;\n\t\t\t}\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\t\tj=1<<(j-1);\n\t\t\tk=j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (al == j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#endif\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) goto err;\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_correct_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\tret=1;\nerr:\n\tbn_check_top(r);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n\t int tna, int tnb, BN_ULONG *t)\n\t{\n\tint i,j,n2=n*2;\n\tint c1,c2,neg;\n\tBN_ULONG ln,lo,*p;\n# ifdef BN_COUNT\n\tfprintf(stderr," bn_mul_part_recursive (%d%+d) * (%d%+d)\\n",\n\t\tn, tna, n, tnb);\n# endif\n\tif (n < 8)\n\t\t{\n\t\tbn_mul_normal(r,a,n+tna,b,n+tnb);\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_part_words(a,&(a[n]),tna,n-tna);\n\tc2=bn_cmp_part_words(&(b[n]),b,tnb,tnb-n);\n\tneg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tbreak;\n\tcase -3:\n\tcase -2:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\tcase 2:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\tcase 4:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tbreak;\n\t\t}\n# if 0\n\tif (n == 4)\n\t\t{\n\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn);\n\t\tmemset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2));\n\t\t}\n\telse\n# endif\n\tif (n == 8)\n\t\t{\n\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb);\n\t\tmemset(&(r[n2+tna+tnb]),0,sizeof(BN_ULONG)*(n2-tna-tnb));\n\t\t}\n\telse\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,0,0,p);\n\t\tbn_mul_recursive(r,a,b,n,0,0,p);\n\t\ti=n/2;\n\t\tif (tna > tnb)\n\t\t\tj = tna - i;\n\t\telse\n\t\t\tj = tnb - i;\n\t\tif (j == 0)\n\t\t\t{\n\t\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),\n\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\tmemset(&(r[n2+i*2]),0,sizeof(BN_ULONG)*(n2-i*2));\n\t\t\t}\n\t\telse if (j > 0)\n\t\t\t\t{\n\t\t\t\tbn_mul_part_recursive(&(r[n2]),&(a[n]),&(b[n]),\n\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\tmemset(&(r[n2+tna+tnb]),0,\n\t\t\t\t\tsizeof(BN_ULONG)*(n2-tna-tnb));\n\t\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tmemset(&(r[n2]),0,sizeof(BN_ULONG)*n2);\n\t\t\tif (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n\t\t\t\t&& tnb < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t\t\t{\n\t\t\t\tbn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tfor (;;)\n\t\t\t\t\t{\n\t\t\t\t\ti/=2;\n\t\t\t\t\tif (i < tna || i < tnb)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tbn_mul_part_recursive(&(r[n2]),\n\t\t\t\t\t\t\t&(a[n]),&(b[n]),\n\t\t\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\telse if (i == tna || i == tnb)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tbn_mul_recursive(&(r[n2]),\n\t\t\t\t\t\t\t&(a[n]),&(b[n]),\n\t\t\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\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}', 'int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b,\n\tint cl, int dl)\n\t{\n\tint n,i;\n\tn = cl-1;\n\tif (dl < 0)\n\t\t{\n\t\tfor (i=dl; i<0; i++)\n\t\t\t{\n\t\t\tif (b[n-i] != 0)\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t}\n\tif (dl > 0)\n\t\t{\n\t\tfor (i=dl; i>0; i--)\n\t\t\t{\n\t\t\tif (a[n+i] != 0)\n\t\t\t\treturn 1;\n\t\t\t}\n\t\t}\n\treturn bn_cmp_words(a,b,cl);\n\t}', 'BN_ULONG bn_sub_part_words(BN_ULONG *r,\n\tconst BN_ULONG *a, const BN_ULONG *b,\n\tint cl, int dl)\n\t{\n\tBN_ULONG c, t;\n\tassert(cl >= 0);\n\tc = bn_sub_words(r, a, b, cl);\n\tif (dl == 0)\n\t\treturn c;\n\tr += cl;\n\ta += cl;\n\tb += cl;\n\tif (dl < 0)\n\t\t{\n#ifdef BN_COUNT\n\t\tfprintf(stderr, " bn_sub_part_words %d + %d (dl < 0, c = %d)\\n", cl, dl, c);\n#endif\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt = b[0];\n\t\t\tr[0] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tt = b[1];\n\t\t\tr[1] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tt = b[2];\n\t\t\tr[2] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tt = b[3];\n\t\t\tr[3] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tb += 4;\n\t\t\tr += 4;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tint save_dl = dl;\n#ifdef BN_COUNT\n\t\tfprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, c = %d)\\n", cl, dl, c);\n#endif\n\t\twhile(c)\n\t\t\t{\n\t\t\tt = a[0];\n\t\t\tr[0] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tt = a[1];\n\t\t\tr[1] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tt = a[2];\n\t\t\tr[2] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tt = a[3];\n\t\t\tr[3] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tsave_dl = dl;\n\t\t\ta += 4;\n\t\t\tr += 4;\n\t\t\t}\n\t\tif (dl > 0)\n\t\t\t{\n#ifdef BN_COUNT\n\t\t\tfprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, c == 0)\\n", cl, dl);\n#endif\n\t\t\tif (save_dl > dl)\n\t\t\t\t{\n\t\t\t\tswitch (save_dl - dl)\n\t\t\t\t\t{\n\t\t\t\tcase 1:\n\t\t\t\t\tr[1] = a[1];\n\t\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tcase 2:\n\t\t\t\t\tr[2] = a[2];\n\t\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tcase 3:\n\t\t\t\t\tr[3] = a[3];\n\t\t\t\t\tif (--dl <= 0) break;\n\t\t\t\t\t}\n\t\t\t\ta += 4;\n\t\t\t\tr += 4;\n\t\t\t\t}\n\t\t\t}\n\t\tif (dl > 0)\n\t\t\t{\n#ifdef BN_COUNT\n\t\t\tfprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, copy)\\n", cl, dl);\n#endif\n\t\t\tfor(;;)\n\t\t\t\t{\n\t\t\t\tr[0] = a[0];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tr[1] = a[1];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tr[2] = a[2];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tr[3] = a[3];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\ta += 4;\n\t\t\t\tr += 4;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\treturn c;\n\t}', 'BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)\n {\n\tBN_ULONG t1,t2;\n\tint c=0;\n\tassert(n >= 0);\n\tif (n <= 0) return((BN_ULONG)0);\n#ifndef OPENSSL_SMALL_FOOTPRINT\n\twhile (n&~3)\n\t\t{\n\t\tt1=a[0]; t2=b[0];\n\t\tr[0]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tt1=a[1]; t2=b[1];\n\t\tr[1]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tt1=a[2]; t2=b[2];\n\t\tr[2]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tt1=a[3]; t2=b[3];\n\t\tr[3]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\ta+=4; b+=4; r+=4; n-=4;\n\t\t}\n#endif\n\twhile (n)\n\t\t{\n\t\tt1=a[0]; t2=b[0];\n\t\tr[0]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\ta++; b++; r++; n--;\n\t\t}\n\treturn(c);\n\t}']
|
1,253 | 0 |
https://github.com/libav/libav/blob/df84d7d9bdf6b8e6896c711880f130b72738c828/libavcodec/mpegaudiodec.c/#L887
|
void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
MPA_INT *window, int *dither_state,
OUT_INT *samples, int incr,
int32_t sb_samples[SBLIMIT])
{
register MPA_INT *synth_buf;
register const MPA_INT *w, *w2, *p;
int j, offset;
OUT_INT *samples2;
#if FRAC_BITS <= 15
int32_t tmp[32];
int sum, sum2;
#else
int64_t sum, sum2;
#endif
offset = *synth_buf_offset;
synth_buf = synth_buf_ptr + offset;
#if FRAC_BITS <= 15
dct32(tmp, sb_samples);
for(j=0;j<32;j++) {
synth_buf[j] = av_clip_int16(tmp[j]);
}
#else
dct32(synth_buf, sb_samples);
#endif
memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
samples2 = samples + 31 * incr;
w = window;
w2 = window + 31;
sum = *dither_state;
p = synth_buf + 16;
SUM8(MACS, sum, w, p);
p = synth_buf + 48;
SUM8(MLSS, sum, w + 32, p);
*samples = round_sample(&sum);
samples += incr;
w++;
for(j=1;j<16;j++) {
sum2 = 0;
p = synth_buf + 16 + j;
SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
p = synth_buf + 48 - j;
SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
*samples = round_sample(&sum);
samples += incr;
sum += sum2;
*samples2 = round_sample(&sum);
samples2 -= incr;
w++;
w2--;
}
p = synth_buf + 32;
SUM8(MLSS, sum, w + 32, p);
*samples = round_sample(&sum);
*dither_state= sum;
offset = (offset - 32) & 511;
*synth_buf_offset = offset;
}
|
['static void mpc_synth(MPCContext *c, int16_t *out)\n{\n int dither_state = 0;\n int i, ch;\n OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE], *samples_ptr;\n for(ch = 0; ch < 2; ch++){\n samples_ptr = samples + ch;\n for(i = 0; i < SAMPLES_PER_BAND; i++) {\n ff_mpa_synth_filter(c->synth_buf[ch], &(c->synth_buf_offset[ch]),\n 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}']
|
1,254 | 0 |
https://github.com/openssl/openssl/blob/47bbaa5b607f592009ed40f5678fde21c10a873c/crypto/bn/bn_ctx.c/#L328
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,\n point_conversion_form_t form,\n unsigned char *buf, size_t len, BN_CTX *ctx)\n{\n size_t ret;\n BN_CTX *new_ctx = NULL;\n int used_ctx = 0;\n BIGNUM *x, *y;\n size_t field_len, i, skip;\n if ((form != POINT_CONVERSION_COMPRESSED)\n && (form != POINT_CONVERSION_UNCOMPRESSED)\n && (form != POINT_CONVERSION_HYBRID)) {\n ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);\n goto err;\n }\n if (EC_POINT_is_at_infinity(group, point)) {\n if (buf != NULL) {\n if (len < 1) {\n ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);\n return 0;\n }\n buf[0] = 0;\n }\n return 1;\n }\n field_len = BN_num_bytes(group->field);\n ret =\n (form ==\n POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;\n if (buf != NULL) {\n if (len < ret) {\n ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);\n goto err;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n used_ctx = 1;\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx))\n goto err;\n if ((form == POINT_CONVERSION_COMPRESSED\n || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))\n buf[0] = form + 1;\n else\n buf[0] = form;\n i = 1;\n skip = field_len - BN_num_bytes(x);\n if (skip > field_len) {\n ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n while (skip > 0) {\n buf[i++] = 0;\n skip--;\n }\n skip = BN_bn2bin(x, buf + i);\n i += skip;\n if (i != 1 + field_len) {\n ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (form == POINT_CONVERSION_UNCOMPRESSED\n || form == POINT_CONVERSION_HYBRID) {\n skip = field_len - BN_num_bytes(y);\n if (skip > field_len) {\n ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n while (skip > 0) {\n buf[i++] = 0;\n skip--;\n }\n skip = BN_bn2bin(y, buf + i);\n i += skip;\n }\n if (i != ret) {\n ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n }\n if (used_ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n err:\n if (used_ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
1,255 | 0 |
https://github.com/openssl/openssl/blob/18e1e302452e6dea4500b6f981cee7e151294dea/crypto/bn/bn_ctx.c/#L268
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,\n point_conversion_form_t form,\n unsigned char *buf, size_t len, BN_CTX *ctx)\n{\n size_t ret;\n BN_CTX *new_ctx = NULL;\n int used_ctx = 0;\n BIGNUM *x, *y, *yxi;\n size_t field_len, i, skip;\n if ((form != POINT_CONVERSION_COMPRESSED)\n && (form != POINT_CONVERSION_UNCOMPRESSED)\n && (form != POINT_CONVERSION_HYBRID)) {\n ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);\n goto err;\n }\n if (EC_POINT_is_at_infinity(group, point)) {\n if (buf != NULL) {\n if (len < 1) {\n ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);\n return 0;\n }\n buf[0] = 0;\n }\n return 1;\n }\n field_len = (EC_GROUP_get_degree(group) + 7) / 8;\n ret =\n (form ==\n POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;\n if (buf != NULL) {\n if (len < ret) {\n ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);\n goto err;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n used_ctx = 1;\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n yxi = BN_CTX_get(ctx);\n if (yxi == NULL)\n goto err;\n if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))\n goto err;\n buf[0] = form;\n if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) {\n if (!group->meth->field_div(group, yxi, y, x, ctx))\n goto err;\n if (BN_is_odd(yxi))\n buf[0]++;\n }\n i = 1;\n skip = field_len - BN_num_bytes(x);\n if (skip > field_len) {\n ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n while (skip > 0) {\n buf[i++] = 0;\n skip--;\n }\n skip = BN_bn2bin(x, buf + i);\n i += skip;\n if (i != 1 + field_len) {\n ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (form == POINT_CONVERSION_UNCOMPRESSED\n || form == POINT_CONVERSION_HYBRID) {\n skip = field_len - BN_num_bytes(y);\n if (skip > field_len) {\n ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n while (skip > 0) {\n buf[i++] = 0;\n skip--;\n }\n skip = BN_bn2bin(y, buf + i);\n i += skip;\n }\n if (i != ret) {\n ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n }\n if (used_ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n err:\n if (used_ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(new_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}']
|
1,256 | 0 |
https://github.com/libav/libav/blob/169fb94f0f65dcb18549f6f25fb29ea58d7eaf92/libavformat/rtsp.c/#L2048
|
static int sdp_read_header(AVFormatContext *s)
{
RTSPState *rt = s->priv_data;
RTSPStream *rtsp_st;
int size, i, err;
char *content;
char url[1024];
if (!ff_network_init())
return AVERROR(EIO);
if (s->max_delay < 0)
s->max_delay = DEFAULT_REORDERING_DELAY;
if (rt->rtsp_flags & RTSP_FLAG_CUSTOM_IO)
rt->lower_transport = RTSP_LOWER_TRANSPORT_CUSTOM;
content = av_malloc(SDP_MAX_SIZE);
size = avio_read(s->pb, content, SDP_MAX_SIZE - 1);
if (size <= 0) {
av_free(content);
return AVERROR_INVALIDDATA;
}
content[size] ='\0';
err = ff_sdp_parse(s, content);
av_free(content);
if (err) goto fail;
for (i = 0; i < rt->nb_rtsp_streams; i++) {
char namebuf[50];
rtsp_st = rt->rtsp_streams[i];
if (!(rt->rtsp_flags & RTSP_FLAG_CUSTOM_IO)) {
getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip),
namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
ff_url_join(url, sizeof(url), "rtp", NULL,
namebuf, rtsp_st->sdp_port,
"?localport=%d&ttl=%d&connect=%d", rtsp_st->sdp_port,
rtsp_st->sdp_ttl,
rt->rtsp_flags & RTSP_FLAG_FILTER_SRC ? 1 : 0);
if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE,
&s->interrupt_callback, NULL) < 0) {
err = AVERROR_INVALIDDATA;
goto fail;
}
}
if ((err = ff_rtsp_open_transport_ctx(s, rtsp_st)))
goto fail;
}
return 0;
fail:
ff_rtsp_close_streams(s);
ff_network_close();
return err;
}
|
['static int sdp_read_header(AVFormatContext *s)\n{\n RTSPState *rt = s->priv_data;\n RTSPStream *rtsp_st;\n int size, i, err;\n char *content;\n char url[1024];\n if (!ff_network_init())\n return AVERROR(EIO);\n if (s->max_delay < 0)\n s->max_delay = DEFAULT_REORDERING_DELAY;\n if (rt->rtsp_flags & RTSP_FLAG_CUSTOM_IO)\n rt->lower_transport = RTSP_LOWER_TRANSPORT_CUSTOM;\n content = av_malloc(SDP_MAX_SIZE);\n size = avio_read(s->pb, content, SDP_MAX_SIZE - 1);\n if (size <= 0) {\n av_free(content);\n return AVERROR_INVALIDDATA;\n }\n content[size] =\'\\0\';\n err = ff_sdp_parse(s, content);\n av_free(content);\n if (err) goto fail;\n for (i = 0; i < rt->nb_rtsp_streams; i++) {\n char namebuf[50];\n rtsp_st = rt->rtsp_streams[i];\n if (!(rt->rtsp_flags & RTSP_FLAG_CUSTOM_IO)) {\n getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip),\n namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);\n ff_url_join(url, sizeof(url), "rtp", NULL,\n namebuf, rtsp_st->sdp_port,\n "?localport=%d&ttl=%d&connect=%d", rtsp_st->sdp_port,\n rtsp_st->sdp_ttl,\n rt->rtsp_flags & RTSP_FLAG_FILTER_SRC ? 1 : 0);\n if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE,\n &s->interrupt_callback, NULL) < 0) {\n err = AVERROR_INVALIDDATA;\n goto fail;\n }\n }\n if ((err = ff_rtsp_open_transport_ctx(s, rtsp_st)))\n goto fail;\n }\n return 0;\nfail:\n ff_rtsp_close_streams(s);\n ff_network_close();\n return err;\n}', 'int ff_network_init(void)\n{\n#if HAVE_WINSOCK2_H\n WSADATA wsaData;\n#endif\n if (!ff_network_inited_globally)\n av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "\n "network initialization. Please use "\n "avformat_network_init(), this will "\n "become mandatory later.\\n");\n#if HAVE_WINSOCK2_H\n if (WSAStartup(MAKEWORD(1,1), &wsaData))\n return 0;\n#endif\n return 1;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if (size > (INT_MAX - 32) || !size)\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size + 32);\n if (!ptr)\n return ptr;\n diff = ((-(long)ptr - 1) & 31) + 1;\n ptr = (char *)ptr + diff;\n ((char *)ptr)[-1] = diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr, 32, size))\n ptr = NULL;\n#elif HAVE_ALIGNED_MALLOC\n ptr = _aligned_malloc(size, 32);\n#elif HAVE_MEMALIGN\n ptr = memalign(32, size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
|
1,257 | 0 |
https://github.com/openssl/openssl/blob/1145e03870dd82eae00bb45e0b2162494b9b2f38/apps/crl2p7.c/#L145
|
int MAIN(int argc, char **argv)
{
int i,badops=0;
BIO *in=NULL,*out=NULL;
int informat,outformat;
char *infile,*outfile,*prog,*certfile;
PKCS7 *p7 = NULL;
PKCS7_SIGNED *p7s = NULL;
X509_CRL *crl=NULL;
STACK *certflst=NULL;
STACK_OF(X509_CRL) *crl_stack=NULL;
STACK_OF(X509) *cert_stack=NULL;
int ret=1,nocrl=0;
apps_startup();
if (bio_err == NULL)
if ((bio_err=BIO_new(BIO_s_file())) != NULL)
BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
infile=NULL;
outfile=NULL;
informat=FORMAT_PEM;
outformat=FORMAT_PEM;
prog=argv[0];
argc--;
argv++;
while (argc >= 1)
{
if (strcmp(*argv,"-inform") == 0)
{
if (--argc < 1) goto bad;
informat=str2fmt(*(++argv));
}
else if (strcmp(*argv,"-outform") == 0)
{
if (--argc < 1) goto bad;
outformat=str2fmt(*(++argv));
}
else if (strcmp(*argv,"-in") == 0)
{
if (--argc < 1) goto bad;
infile= *(++argv);
}
else if (strcmp(*argv,"-nocrl") == 0)
{
nocrl=1;
}
else if (strcmp(*argv,"-out") == 0)
{
if (--argc < 1) goto bad;
outfile= *(++argv);
}
else if (strcmp(*argv,"-certfile") == 0)
{
if (--argc < 1) goto bad;
if(!certflst) certflst = sk_new_null();
sk_push(certflst,*(++argv));
}
else
{
BIO_printf(bio_err,"unknown option %s\n",*argv);
badops=1;
break;
}
argc--;
argv++;
}
if (badops)
{
bad:
BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
BIO_printf(bio_err,"where options are\n");
BIO_printf(bio_err," -inform arg input format - DER or PEM\n");
BIO_printf(bio_err," -outform arg output format - DER or PEM\n");
BIO_printf(bio_err," -in arg input file\n");
BIO_printf(bio_err," -out arg output file\n");
BIO_printf(bio_err," -certfile arg certificates file of chain to a trusted CA\n");
BIO_printf(bio_err," (can be used more than once)\n");
BIO_printf(bio_err," -nocrl no crl to load, just certs from '-certfile'\n");
ret = 1;
goto end;
}
ERR_load_crypto_strings();
in=BIO_new(BIO_s_file());
out=BIO_new(BIO_s_file());
if ((in == NULL) || (out == NULL))
{
ERR_print_errors(bio_err);
goto end;
}
if (!nocrl)
{
if (infile == NULL)
BIO_set_fp(in,stdin,BIO_NOCLOSE);
else
{
if (BIO_read_filename(in,infile) <= 0)
{
perror(infile);
goto end;
}
}
if (informat == FORMAT_ASN1)
crl=d2i_X509_CRL_bio(in,NULL);
else if (informat == FORMAT_PEM)
crl=PEM_read_bio_X509_CRL(in,NULL,NULL,NULL);
else {
BIO_printf(bio_err,"bad input format specified for input crl\n");
goto end;
}
if (crl == NULL)
{
BIO_printf(bio_err,"unable to load CRL\n");
ERR_print_errors(bio_err);
goto end;
}
}
if ((p7=PKCS7_new()) == NULL) goto end;
if ((p7s=PKCS7_SIGNED_new()) == NULL) goto end;
p7->type=OBJ_nid2obj(NID_pkcs7_signed);
p7->d.sign=p7s;
p7s->contents->type=OBJ_nid2obj(NID_pkcs7_data);
if (!ASN1_INTEGER_set(p7s->version,1)) goto end;
if ((crl_stack=sk_X509_CRL_new_null()) == NULL) goto end;
p7s->crl=crl_stack;
if (crl != NULL)
{
sk_X509_CRL_push(crl_stack,crl);
crl=NULL;
}
if ((cert_stack=sk_X509_new_null()) == NULL) goto end;
p7s->cert=cert_stack;
if(certflst) for(i = 0; i < sk_num(certflst); i++) {
certfile = sk_value(certflst, i);
if (add_certs_from_file(cert_stack,certfile) < 0)
{
BIO_printf(bio_err, "error loading certificates\n");
ERR_print_errors(bio_err);
goto end;
}
}
sk_free(certflst);
if (outfile == NULL)
{
BIO_set_fp(out,stdout,BIO_NOCLOSE);
#ifdef OPENSSL_SYS_VMS
{
BIO *tmpbio = BIO_new(BIO_f_linebuffer());
out = BIO_push(tmpbio, out);
}
#endif
}
else
{
if (BIO_write_filename(out,outfile) <= 0)
{
perror(outfile);
goto end;
}
}
if (outformat == FORMAT_ASN1)
i=i2d_PKCS7_bio(out,p7);
else if (outformat == FORMAT_PEM)
i=PEM_write_bio_PKCS7(out,p7);
else {
BIO_printf(bio_err,"bad output format specified for outfile\n");
goto end;
}
if (!i)
{
BIO_printf(bio_err,"unable to write pkcs7 object\n");
ERR_print_errors(bio_err);
goto end;
}
ret=0;
end:
if (in != NULL) BIO_free(in);
if (out != NULL) BIO_free_all(out);
if (p7 != NULL) PKCS7_free(p7);
if (crl != NULL) X509_CRL_free(crl);
apps_shutdown();
OPENSSL_EXIT(ret);
}
|
['int MAIN(int argc, char **argv)\n\t{\n\tint i,badops=0;\n\tBIO *in=NULL,*out=NULL;\n\tint informat,outformat;\n\tchar *infile,*outfile,*prog,*certfile;\n\tPKCS7 *p7 = NULL;\n\tPKCS7_SIGNED *p7s = NULL;\n\tX509_CRL *crl=NULL;\n\tSTACK *certflst=NULL;\n\tSTACK_OF(X509_CRL) *crl_stack=NULL;\n\tSTACK_OF(X509) *cert_stack=NULL;\n\tint ret=1,nocrl=0;\n\tapps_startup();\n\tif (bio_err == NULL)\n\t\tif ((bio_err=BIO_new(BIO_s_file())) != NULL)\n\t\t\tBIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);\n\tinfile=NULL;\n\toutfile=NULL;\n\tinformat=FORMAT_PEM;\n\toutformat=FORMAT_PEM;\n\tprog=argv[0];\n\targc--;\n\targv++;\n\twhile (argc >= 1)\n\t\t{\n\t\tif \t(strcmp(*argv,"-inform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-outform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-in") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-nocrl") == 0)\n\t\t\t{\n\t\t\tnocrl=1;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-out") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-certfile") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tif(!certflst) certflst = sk_new_null();\n\t\t\tsk_push(certflst,*(++argv));\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unknown option %s\\n",*argv);\n\t\t\tbadops=1;\n\t\t\tbreak;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (badops)\n\t\t{\nbad:\n\t\tBIO_printf(bio_err,"%s [options] <infile >outfile\\n",prog);\n\t\tBIO_printf(bio_err,"where options are\\n");\n\t\tBIO_printf(bio_err," -inform arg input format - DER or PEM\\n");\n\t\tBIO_printf(bio_err," -outform arg output format - DER or PEM\\n");\n\t\tBIO_printf(bio_err," -in arg input file\\n");\n\t\tBIO_printf(bio_err," -out arg output file\\n");\n\t\tBIO_printf(bio_err," -certfile arg certificates file of chain to a trusted CA\\n");\n\t\tBIO_printf(bio_err," (can be used more than once)\\n");\n\t\tBIO_printf(bio_err," -nocrl no crl to load, just certs from \'-certfile\'\\n");\n\t\tret = 1;\n\t\tgoto end;\n\t\t}\n\tERR_load_crypto_strings();\n\tin=BIO_new(BIO_s_file());\n\tout=BIO_new(BIO_s_file());\n\tif ((in == NULL) || (out == NULL))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (!nocrl)\n\t\t{\n\t\tif (infile == NULL)\n\t\t\tBIO_set_fp(in,stdin,BIO_NOCLOSE);\n\t\telse\n\t\t\t{\n\t\t\tif (BIO_read_filename(in,infile) <= 0)\n\t\t\t\t{\n\t\t\t\tperror(infile);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\tif \t(informat == FORMAT_ASN1)\n\t\t\tcrl=d2i_X509_CRL_bio(in,NULL);\n\t\telse if (informat == FORMAT_PEM)\n\t\t\tcrl=PEM_read_bio_X509_CRL(in,NULL,NULL,NULL);\n\t\telse\t{\n\t\t\tBIO_printf(bio_err,"bad input format specified for input crl\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (crl == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to load CRL\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif ((p7=PKCS7_new()) == NULL) goto end;\n\tif ((p7s=PKCS7_SIGNED_new()) == NULL) goto end;\n\tp7->type=OBJ_nid2obj(NID_pkcs7_signed);\n\tp7->d.sign=p7s;\n\tp7s->contents->type=OBJ_nid2obj(NID_pkcs7_data);\n\tif (!ASN1_INTEGER_set(p7s->version,1)) goto end;\n\tif ((crl_stack=sk_X509_CRL_new_null()) == NULL) goto end;\n\tp7s->crl=crl_stack;\n\tif (crl != NULL)\n\t\t{\n\t\tsk_X509_CRL_push(crl_stack,crl);\n\t\tcrl=NULL;\n\t\t}\n\tif ((cert_stack=sk_X509_new_null()) == NULL) goto end;\n\tp7s->cert=cert_stack;\n\tif(certflst) for(i = 0; i < sk_num(certflst); i++) {\n\t\tcertfile = sk_value(certflst, i);\n\t\tif (add_certs_from_file(cert_stack,certfile) < 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "error loading certificates\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t}\n\tsk_free(certflst);\n\tif (outfile == NULL)\n\t\t{\n\t\tBIO_set_fp(out,stdout,BIO_NOCLOSE);\n#ifdef OPENSSL_SYS_VMS\n\t\t{\n\t\tBIO *tmpbio = BIO_new(BIO_f_linebuffer());\n\t\tout = BIO_push(tmpbio, out);\n\t\t}\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tif (BIO_write_filename(out,outfile) <= 0)\n\t\t\t{\n\t\t\tperror(outfile);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif \t(outformat == FORMAT_ASN1)\n\t\ti=i2d_PKCS7_bio(out,p7);\n\telse if (outformat == FORMAT_PEM)\n\t\ti=PEM_write_bio_PKCS7(out,p7);\n\telse\t{\n\t\tBIO_printf(bio_err,"bad output format specified for outfile\\n");\n\t\tgoto end;\n\t\t}\n\tif (!i)\n\t\t{\n\t\tBIO_printf(bio_err,"unable to write pkcs7 object\\n");\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tret=0;\nend:\n\tif (in != NULL) BIO_free(in);\n\tif (out != NULL) BIO_free_all(out);\n\tif (p7 != NULL) PKCS7_free(p7);\n\tif (crl != NULL) X509_CRL_free(crl);\n\tapps_shutdown();\n\tOPENSSL_EXIT(ret);\n\t}', 'BIO_METHOD *BIO_s_file(void)\n\t{\n\treturn(&methods_filep);\n\t}', 'BIO *BIO_new(BIO_METHOD *method)\n\t{\n\tBIO *ret=NULL;\n\tret=(BIO *)OPENSSL_malloc(sizeof(BIO));\n\tif (ret == NULL)\n\t\t{\n\t\tBIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tif (!BIO_set(ret,method))\n\t\t{\n\t\tOPENSSL_free(ret);\n\t\tret=NULL;\n\t\t}\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\textern unsigned char cleanse_ctr;\n\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n if(ret && (num > 2048))\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_flags[es->top]=0;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'STACK *sk_new_null(void)\n\t{\n\treturn sk_new((int (*)(const char * const *, const char * const *))0);\n\t}', 'STACK *sk_new(int (*c)(const char * const *, const char * const *))\n\t{\n\tSTACK *ret;\n\tint i;\n\tif ((ret=(STACK *)OPENSSL_malloc(sizeof(STACK))) == NULL)\n\t\tgoto err;\n\tif ((ret->data=(char **)OPENSSL_malloc(sizeof(char *)*MIN_NODES)) == NULL)\n\t\tgoto err;\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);\nerr:\n\tif(ret)\n\t\tOPENSSL_free(ret);\n\treturn(NULL);\n\t}', 'int sk_push(STACK *st, char *data)\n\t{\n\treturn(sk_insert(st,data,st->num));\n\t}']
|
1,258 | 0 |
https://github.com/libav/libav/blob/452a398fd6bdca3f301c5c8af3bc241bc16a777e/libavcodec/vc1.c/#L3342
|
static int vc1_decode_p_mb(VC1Context *v)
{
MpegEncContext *s = &v->s;
GetBitContext *gb = &s->gb;
int i, j;
int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
int cbp;
int mqdiff, mquant;
int ttmb = v->ttfrm;
static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
offset_table[6] = { 0, 1, 3, 7, 15, 31 };
int mb_has_coeffs = 1;
int dmv_x, dmv_y;
int index, index1;
int val, sign;
int first_block = 1;
int dst_idx, off;
int skipped, fourmv;
int block_cbp = 0, pat;
mquant = v->pq;
if (v->mv_type_is_raw)
fourmv = get_bits1(gb);
else
fourmv = v->mv_type_mb_plane[mb_pos];
if (v->skip_is_raw)
skipped = get_bits1(gb);
else
skipped = v->s.mbskip_table[mb_pos];
s->dsp.clear_blocks(s->block[0]);
if (!fourmv)
{
if (!skipped)
{
GET_MVDATA(dmv_x, dmv_y);
if (s->mb_intra) {
s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
}
s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
if (s->mb_intra && !mb_has_coeffs)
{
GET_MQUANT();
s->ac_pred = get_bits1(gb);
cbp = 0;
}
else if (mb_has_coeffs)
{
if (s->mb_intra) s->ac_pred = get_bits1(gb);
cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
GET_MQUANT();
}
else
{
mquant = v->pq;
cbp = 0;
}
s->current_picture.qscale_table[mb_pos] = mquant;
if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
VC1_TTMB_VLC_BITS, 2);
if(!s->mb_intra) vc1_mc_1mv(v, 0);
dst_idx = 0;
for (i=0; i<6; i++)
{
s->dc_val[0][s->block_index[i]] = 0;
dst_idx += i >> 2;
val = ((cbp >> (5 - i)) & 1);
off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
v->mb_type[0][s->block_index[i]] = s->mb_intra;
if(s->mb_intra) {
v->a_avail = v->c_avail = 0;
if(i == 2 || i == 3 || !s->first_slice_line)
v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
if(i == 1 || i == 3 || s->mb_x)
v->c_avail = v->mb_type[0][s->block_index[i] - 1];
vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
s->dsp.vc1_inv_trans_8x8(s->block[i]);
if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
if(v->pq >= 9 && v->overlap) {
if(v->c_avail)
s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
if(v->a_avail)
s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
}
if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
int left_cbp, top_cbp;
if(i & 4){
left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
}else{
left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
}
if(left_cbp & 0xC)
vc1_loop_filter(s->dest[dst_idx] + off, 1, i & 4 ? s->uvlinesize : s->linesize, 8, mquant);
if(top_cbp & 0xA)
vc1_loop_filter(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, 1, 8, mquant);
}
block_cbp |= 0xF << (i << 2);
} else if(val) {
int left_cbp = 0, top_cbp = 0, filter = 0;
if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
filter = 1;
if(i & 4){
left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
}else{
left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
}
}
pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
block_cbp |= pat << (i << 2);
if(!v->ttmbf && ttmb < 8) ttmb = -1;
first_block = 0;
}
}
}
else
{
s->mb_intra = 0;
for(i = 0; i < 6; i++) {
v->mb_type[0][s->block_index[i]] = 0;
s->dc_val[0][s->block_index[i]] = 0;
}
s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
s->current_picture.qscale_table[mb_pos] = 0;
vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
vc1_mc_1mv(v, 0);
return 0;
}
}
else
{
if (!skipped )
{
int intra_count = 0, coded_inter = 0;
int is_intra[6], is_coded[6];
cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
for (i=0; i<6; i++)
{
val = ((cbp >> (5 - i)) & 1);
s->dc_val[0][s->block_index[i]] = 0;
s->mb_intra = 0;
if(i < 4) {
dmv_x = dmv_y = 0;
s->mb_intra = 0;
mb_has_coeffs = 0;
if(val) {
GET_MVDATA(dmv_x, dmv_y);
}
vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
intra_count += s->mb_intra;
is_intra[i] = s->mb_intra;
is_coded[i] = mb_has_coeffs;
}
if(i&4){
is_intra[i] = (intra_count >= 3);
is_coded[i] = val;
}
if(i == 4) vc1_mc_4mv_chroma(v);
v->mb_type[0][s->block_index[i]] = is_intra[i];
if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
}
if(!intra_count && !coded_inter) return 0;
dst_idx = 0;
GET_MQUANT();
s->current_picture.qscale_table[mb_pos] = mquant;
{
int intrapred = 0;
for(i=0; i<6; i++)
if(is_intra[i]) {
if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
|| ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
intrapred = 1;
break;
}
}
if(intrapred)s->ac_pred = get_bits1(gb);
else s->ac_pred = 0;
}
if (!v->ttmbf && coded_inter)
ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
for (i=0; i<6; i++)
{
dst_idx += i >> 2;
off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
s->mb_intra = is_intra[i];
if (is_intra[i]) {
v->a_avail = v->c_avail = 0;
if(i == 2 || i == 3 || !s->first_slice_line)
v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
if(i == 1 || i == 3 || s->mb_x)
v->c_avail = v->mb_type[0][s->block_index[i] - 1];
vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
s->dsp.vc1_inv_trans_8x8(s->block[i]);
if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
if(v->pq >= 9 && v->overlap) {
if(v->c_avail)
s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
if(v->a_avail)
s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
}
if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
int left_cbp, top_cbp;
if(i & 4){
left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
}else{
left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
}
if(left_cbp & 0xC)
vc1_loop_filter(s->dest[dst_idx] + off, 1, i & 4 ? s->uvlinesize : s->linesize, 8, mquant);
if(top_cbp & 0xA)
vc1_loop_filter(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, 1, 8, mquant);
}
block_cbp |= 0xF << (i << 2);
} else if(is_coded[i]) {
int left_cbp = 0, top_cbp = 0, filter = 0;
if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
filter = 1;
if(i & 4){
left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
}else{
left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
}
}
pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
block_cbp |= pat << (i << 2);
if(!v->ttmbf && ttmb < 8) ttmb = -1;
first_block = 0;
}
}
return 0;
}
else
{
s->mb_intra = 0;
s->current_picture.qscale_table[mb_pos] = 0;
for (i=0; i<6; i++) {
v->mb_type[0][s->block_index[i]] = 0;
s->dc_val[0][s->block_index[i]] = 0;
}
for (i=0; i<4; i++)
{
vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
vc1_mc_4mv_luma(v, i);
}
vc1_mc_4mv_chroma(v);
s->current_picture.qscale_table[mb_pos] = 0;
return 0;
}
}
v->cbp[s->mb_x] = block_cbp;
return -1;
}
|
['static int vc1_decode_p_mb(VC1Context *v)\n{\n MpegEncContext *s = &v->s;\n GetBitContext *gb = &s->gb;\n int i, j;\n int mb_pos = s->mb_x + s->mb_y * s->mb_stride;\n int cbp;\n int mqdiff, mquant;\n int ttmb = v->ttfrm;\n static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },\n offset_table[6] = { 0, 1, 3, 7, 15, 31 };\n int mb_has_coeffs = 1;\n int dmv_x, dmv_y;\n int index, index1;\n int val, sign;\n int first_block = 1;\n int dst_idx, off;\n int skipped, fourmv;\n int block_cbp = 0, pat;\n mquant = v->pq;\n if (v->mv_type_is_raw)\n fourmv = get_bits1(gb);\n else\n fourmv = v->mv_type_mb_plane[mb_pos];\n if (v->skip_is_raw)\n skipped = get_bits1(gb);\n else\n skipped = v->s.mbskip_table[mb_pos];\n s->dsp.clear_blocks(s->block[0]);\n if (!fourmv)\n {\n if (!skipped)\n {\n GET_MVDATA(dmv_x, dmv_y);\n if (s->mb_intra) {\n s->current_picture.motion_val[1][s->block_index[0]][0] = 0;\n s->current_picture.motion_val[1][s->block_index[0]][1] = 0;\n }\n s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;\n vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);\n if (s->mb_intra && !mb_has_coeffs)\n {\n GET_MQUANT();\n s->ac_pred = get_bits1(gb);\n cbp = 0;\n }\n else if (mb_has_coeffs)\n {\n if (s->mb_intra) s->ac_pred = get_bits1(gb);\n cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);\n GET_MQUANT();\n }\n else\n {\n mquant = v->pq;\n cbp = 0;\n }\n s->current_picture.qscale_table[mb_pos] = mquant;\n if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)\n ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,\n VC1_TTMB_VLC_BITS, 2);\n if(!s->mb_intra) vc1_mc_1mv(v, 0);\n dst_idx = 0;\n for (i=0; i<6; i++)\n {\n s->dc_val[0][s->block_index[i]] = 0;\n dst_idx += i >> 2;\n val = ((cbp >> (5 - i)) & 1);\n off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);\n v->mb_type[0][s->block_index[i]] = s->mb_intra;\n if(s->mb_intra) {\n v->a_avail = v->c_avail = 0;\n if(i == 2 || i == 3 || !s->first_slice_line)\n v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];\n if(i == 1 || i == 3 || s->mb_x)\n v->c_avail = v->mb_type[0][s->block_index[i] - 1];\n vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);\n if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;\n s->dsp.vc1_inv_trans_8x8(s->block[i]);\n if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;\n s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));\n if(v->pq >= 9 && v->overlap) {\n if(v->c_avail)\n s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));\n if(v->a_avail)\n s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));\n }\n if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){\n int left_cbp, top_cbp;\n if(i & 4){\n left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);\n top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);\n }else{\n left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));\n top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));\n }\n if(left_cbp & 0xC)\n vc1_loop_filter(s->dest[dst_idx] + off, 1, i & 4 ? s->uvlinesize : s->linesize, 8, mquant);\n if(top_cbp & 0xA)\n vc1_loop_filter(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, 1, 8, mquant);\n }\n block_cbp |= 0xF << (i << 2);\n } else if(val) {\n int left_cbp = 0, top_cbp = 0, filter = 0;\n if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){\n filter = 1;\n if(i & 4){\n left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);\n top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);\n }else{\n left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));\n top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));\n }\n }\n pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);\n block_cbp |= pat << (i << 2);\n if(!v->ttmbf && ttmb < 8) ttmb = -1;\n first_block = 0;\n }\n }\n }\n else\n {\n s->mb_intra = 0;\n for(i = 0; i < 6; i++) {\n v->mb_type[0][s->block_index[i]] = 0;\n s->dc_val[0][s->block_index[i]] = 0;\n }\n s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;\n s->current_picture.qscale_table[mb_pos] = 0;\n vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);\n vc1_mc_1mv(v, 0);\n return 0;\n }\n }\n else\n {\n if (!skipped )\n {\n int intra_count = 0, coded_inter = 0;\n int is_intra[6], is_coded[6];\n cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);\n for (i=0; i<6; i++)\n {\n val = ((cbp >> (5 - i)) & 1);\n s->dc_val[0][s->block_index[i]] = 0;\n s->mb_intra = 0;\n if(i < 4) {\n dmv_x = dmv_y = 0;\n s->mb_intra = 0;\n mb_has_coeffs = 0;\n if(val) {\n GET_MVDATA(dmv_x, dmv_y);\n }\n vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);\n if(!s->mb_intra) vc1_mc_4mv_luma(v, i);\n intra_count += s->mb_intra;\n is_intra[i] = s->mb_intra;\n is_coded[i] = mb_has_coeffs;\n }\n if(i&4){\n is_intra[i] = (intra_count >= 3);\n is_coded[i] = val;\n }\n if(i == 4) vc1_mc_4mv_chroma(v);\n v->mb_type[0][s->block_index[i]] = is_intra[i];\n if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];\n }\n if(!intra_count && !coded_inter) return 0;\n dst_idx = 0;\n GET_MQUANT();\n s->current_picture.qscale_table[mb_pos] = mquant;\n {\n int intrapred = 0;\n for(i=0; i<6; i++)\n if(is_intra[i]) {\n if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])\n || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {\n intrapred = 1;\n break;\n }\n }\n if(intrapred)s->ac_pred = get_bits1(gb);\n else s->ac_pred = 0;\n }\n if (!v->ttmbf && coded_inter)\n ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);\n for (i=0; i<6; i++)\n {\n dst_idx += i >> 2;\n off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);\n s->mb_intra = is_intra[i];\n if (is_intra[i]) {\n v->a_avail = v->c_avail = 0;\n if(i == 2 || i == 3 || !s->first_slice_line)\n v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];\n if(i == 1 || i == 3 || s->mb_x)\n v->c_avail = v->mb_type[0][s->block_index[i] - 1];\n vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);\n if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;\n s->dsp.vc1_inv_trans_8x8(s->block[i]);\n if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;\n s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);\n if(v->pq >= 9 && v->overlap) {\n if(v->c_avail)\n s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));\n if(v->a_avail)\n s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));\n }\n if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){\n int left_cbp, top_cbp;\n if(i & 4){\n left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);\n top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);\n }else{\n left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));\n top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));\n }\n if(left_cbp & 0xC)\n vc1_loop_filter(s->dest[dst_idx] + off, 1, i & 4 ? s->uvlinesize : s->linesize, 8, mquant);\n if(top_cbp & 0xA)\n vc1_loop_filter(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, 1, 8, mquant);\n }\n block_cbp |= 0xF << (i << 2);\n } else if(is_coded[i]) {\n int left_cbp = 0, top_cbp = 0, filter = 0;\n if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){\n filter = 1;\n if(i & 4){\n left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);\n top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);\n }else{\n left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));\n top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));\n }\n }\n pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);\n block_cbp |= pat << (i << 2);\n if(!v->ttmbf && ttmb < 8) ttmb = -1;\n first_block = 0;\n }\n }\n return 0;\n }\n else\n {\n s->mb_intra = 0;\n s->current_picture.qscale_table[mb_pos] = 0;\n for (i=0; i<6; i++) {\n v->mb_type[0][s->block_index[i]] = 0;\n s->dc_val[0][s->block_index[i]] = 0;\n }\n for (i=0; i<4; i++)\n {\n vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);\n vc1_mc_4mv_luma(v, i);\n }\n vc1_mc_4mv_chroma(v);\n s->current_picture.qscale_table[mb_pos] = 0;\n return 0;\n }\n }\n v->cbp[s->mb_x] = block_cbp;\n return -1;\n}']
|
1,259 | 0 |
https://github.com/libav/libav/blob/cb72230dfadb28651e036d717dc12d33b18a6893/libavcodec/h264.c/#L348
|
static void await_references(H264Context *h){
MpegEncContext * const s = &h->s;
const int mb_xy= h->mb_xy;
const int mb_type = s->current_picture.f.mb_type[mb_xy];
int refs[2][48];
int nrefs[2] = {0};
int ref, list;
memset(refs, -1, sizeof(refs));
if(IS_16X16(mb_type)){
get_lowest_part_y(h, refs, 0, 16, 0,
IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
}else if(IS_16X8(mb_type)){
get_lowest_part_y(h, refs, 0, 8, 0,
IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
get_lowest_part_y(h, refs, 8, 8, 8,
IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
}else if(IS_8X16(mb_type)){
get_lowest_part_y(h, refs, 0, 16, 0,
IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
get_lowest_part_y(h, refs, 4, 16, 0,
IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
}else{
int i;
assert(IS_8X8(mb_type));
for(i=0; i<4; i++){
const int sub_mb_type= h->sub_mb_type[i];
const int n= 4*i;
int y_offset= (i&2)<<2;
if(IS_SUB_8X8(sub_mb_type)){
get_lowest_part_y(h, refs, n , 8, y_offset,
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
}else if(IS_SUB_8X4(sub_mb_type)){
get_lowest_part_y(h, refs, n , 4, y_offset,
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
get_lowest_part_y(h, refs, n+2, 4, y_offset+4,
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
}else if(IS_SUB_4X8(sub_mb_type)){
get_lowest_part_y(h, refs, n , 8, y_offset,
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
get_lowest_part_y(h, refs, n+1, 8, y_offset,
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
}else{
int j;
assert(IS_SUB_4X4(sub_mb_type));
for(j=0; j<4; j++){
int sub_y_offset= y_offset + 2*(j&2);
get_lowest_part_y(h, refs, n+j, 4, sub_y_offset,
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
}
}
}
}
for(list=h->list_count-1; list>=0; list--){
for(ref=0; ref<48 && nrefs[list]; ref++){
int row = refs[list][ref];
if(row >= 0){
Picture *ref_pic = &h->ref_list[list][ref];
int ref_field = ref_pic->f.reference - 1;
int ref_field_picture = ref_pic->field_picture;
int pic_height = 16*s->mb_height >> ref_field_picture;
row <<= MB_MBAFF;
nrefs[list]--;
if(!FIELD_PICTURE && ref_field_picture){
ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) - !(row&1), pic_height-1), 1);
ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) , pic_height-1), 0);
}else if(FIELD_PICTURE && !ref_field_picture){
ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row*2 + ref_field , pic_height-1), 0);
}else if(FIELD_PICTURE){
ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), ref_field);
}else{
ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), 0);
}
}
}
}
}
|
['int ff_rv34_decode_frame(AVCodecContext *avctx,\n void *data, int *data_size,\n AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n RV34DecContext *r = avctx->priv_data;\n MpegEncContext *s = &r->s;\n AVFrame *pict = data;\n SliceInfo si;\n int i;\n int slice_count;\n const uint8_t *slices_hdr = NULL;\n int last = 0;\n if (buf_size == 0) {\n if (s->low_delay==0 && s->next_picture_ptr) {\n *pict = *(AVFrame*)s->next_picture_ptr;\n s->next_picture_ptr = NULL;\n *data_size = sizeof(AVFrame);\n }\n return 0;\n }\n if(!avctx->slice_count){\n slice_count = (*buf++) + 1;\n slices_hdr = buf + 4;\n buf += 8 * slice_count;\n buf_size -= 1 + 8 * slice_count;\n }else\n slice_count = avctx->slice_count;\n if(get_slice_offset(avctx, slices_hdr, 0) < 0 ||\n get_slice_offset(avctx, slices_hdr, 0) > buf_size){\n av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\\n");\n return -1;\n }\n init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, 0), (buf_size-get_slice_offset(avctx, slices_hdr, 0))*8);\n if(r->parse_slice_header(r, &r->s.gb, &si) < 0 || si.start){\n av_log(avctx, AV_LOG_ERROR, "First slice header is incorrect\\n");\n return -1;\n }\n if ((!s->last_picture_ptr || !s->last_picture_ptr->f.data[0]) && si.type == AV_PICTURE_TYPE_B)\n return -1;\n if( (avctx->skip_frame >= AVDISCARD_NONREF && si.type==AV_PICTURE_TYPE_B)\n || (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=AV_PICTURE_TYPE_I)\n || avctx->skip_frame >= AVDISCARD_ALL)\n return avpkt->size;\n for(i = 0; i < slice_count; i++){\n int offset = get_slice_offset(avctx, slices_hdr, i);\n int size;\n if(i+1 == slice_count)\n size = buf_size - offset;\n else\n size = get_slice_offset(avctx, slices_hdr, i+1) - offset;\n if(offset < 0 || offset > buf_size){\n av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\\n");\n break;\n }\n r->si.end = s->mb_width * s->mb_height;\n if(i+1 < slice_count){\n if (get_slice_offset(avctx, slices_hdr, i+1) < 0 ||\n get_slice_offset(avctx, slices_hdr, i+1) > buf_size) {\n av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\\n");\n break;\n }\n init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, i+1), (buf_size-get_slice_offset(avctx, slices_hdr, i+1))*8);\n if(r->parse_slice_header(r, &r->s.gb, &si) < 0){\n if(i+2 < slice_count)\n size = get_slice_offset(avctx, slices_hdr, i+2) - offset;\n else\n size = buf_size - offset;\n }else\n r->si.end = si.start;\n }\n if (size < 0 || size > buf_size - offset) {\n av_log(avctx, AV_LOG_ERROR, "Slice size is invalid\\n");\n break;\n }\n last = rv34_decode_slice(r, r->si.end, buf + offset, size);\n s->mb_num_left = r->s.mb_x + r->s.mb_y*r->s.mb_width - r->si.start;\n if(last)\n break;\n }\n if(last && s->current_picture_ptr){\n if(r->loop_filter)\n r->loop_filter(r, s->mb_height - 1);\n ff_er_frame_end(s);\n MPV_frame_end(s);\n if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {\n *pict = *(AVFrame*)s->current_picture_ptr;\n } else if (s->last_picture_ptr != NULL) {\n *pict = *(AVFrame*)s->last_picture_ptr;\n }\n if(s->last_picture_ptr || s->low_delay){\n *data_size = sizeof(AVFrame);\n ff_print_debug_info(s, pict);\n }\n s->current_picture_ptr = NULL;\n }\n return avpkt->size;\n}', 'void ff_er_frame_end(MpegEncContext *s){\n int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;\n int distance;\n int threshold_part[4]= {100,100,100};\n int threshold= 50;\n int is_intra_likely;\n int size = s->b8_stride * 2 * s->mb_height;\n Picture *pic= s->current_picture_ptr;\n if(!s->error_recognition || s->error_count==0 || s->avctx->lowres ||\n s->avctx->hwaccel ||\n s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||\n s->picture_structure != PICT_FRAME ||\n s->error_count==3*s->mb_width*(s->avctx->skip_top + s->avctx->skip_bottom)) return;\n if (s->current_picture.f.motion_val[0] == NULL) {\n av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\\n");\n for(i=0; i<2; i++){\n pic->f.ref_index[i] = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));\n pic->motion_val_base[i]= av_mallocz((size+4) * 2 * sizeof(uint16_t));\n pic->f.motion_val[i] = pic->motion_val_base[i] + 4;\n }\n pic->f.motion_subsample_log2 = 3;\n s->current_picture= *s->current_picture_ptr;\n }\n if(s->avctx->debug&FF_DEBUG_ER){\n for(mb_y=0; mb_y<s->mb_height; mb_y++){\n for(mb_x=0; mb_x<s->mb_width; mb_x++){\n int status= s->error_status_table[mb_x + mb_y*s->mb_stride];\n av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);\n }\n av_log(s->avctx, AV_LOG_DEBUG, "\\n");\n }\n }\n for(error_type=1; error_type<=3; error_type++){\n int end_ok=0;\n for(i=s->mb_num-1; i>=0; i--){\n const int mb_xy= s->mb_index2xy[i];\n int error= s->error_status_table[mb_xy];\n if(error&(1<<error_type))\n end_ok=1;\n if(error&(8<<error_type))\n end_ok=1;\n if(!end_ok)\n s->error_status_table[mb_xy]|= 1<<error_type;\n if(error&VP_START)\n end_ok=0;\n }\n }\n if(s->partitioned_frame){\n int end_ok=0;\n for(i=s->mb_num-1; i>=0; i--){\n const int mb_xy= s->mb_index2xy[i];\n int error= s->error_status_table[mb_xy];\n if(error&AC_END)\n end_ok=0;\n if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))\n end_ok=1;\n if(!end_ok)\n s->error_status_table[mb_xy]|= AC_ERROR;\n if(error&VP_START)\n end_ok=0;\n }\n }\n if(s->error_recognition>=4){\n int end_ok=1;\n for(i=s->mb_num-2; i>=s->mb_width+100; i--){\n const int mb_xy= s->mb_index2xy[i];\n int error1= s->error_status_table[mb_xy ];\n int error2= s->error_status_table[s->mb_index2xy[i+1]];\n if(error1&VP_START)\n end_ok=1;\n if( error2==(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)\n && error1!=(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)\n && ((error1&AC_END) || (error1&DC_END) || (error1&MV_END))){\n end_ok=0;\n }\n if(!end_ok)\n s->error_status_table[mb_xy]|= DC_ERROR|AC_ERROR|MV_ERROR;\n }\n }\n distance=9999999;\n for(error_type=1; error_type<=3; error_type++){\n for(i=s->mb_num-1; i>=0; i--){\n const int mb_xy= s->mb_index2xy[i];\n int error= s->error_status_table[mb_xy];\n if(!s->mbskip_table[mb_xy])\n distance++;\n if(error&(1<<error_type))\n distance= 0;\n if(s->partitioned_frame){\n if(distance < threshold_part[error_type-1])\n s->error_status_table[mb_xy]|= 1<<error_type;\n }else{\n if(distance < threshold)\n s->error_status_table[mb_xy]|= 1<<error_type;\n }\n if(error&VP_START)\n distance= 9999999;\n }\n }\n error=0;\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[i];\n int old_error= s->error_status_table[mb_xy];\n if(old_error&VP_START)\n error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);\n else{\n error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);\n s->error_status_table[mb_xy]|= error;\n }\n }\n if(!s->partitioned_frame){\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[i];\n error= s->error_status_table[mb_xy];\n if(error&(AC_ERROR|DC_ERROR|MV_ERROR))\n error|= AC_ERROR|DC_ERROR|MV_ERROR;\n s->error_status_table[mb_xy]= error;\n }\n }\n dc_error= ac_error= mv_error=0;\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[i];\n error= s->error_status_table[mb_xy];\n if(error&DC_ERROR) dc_error ++;\n if(error&AC_ERROR) ac_error ++;\n if(error&MV_ERROR) mv_error ++;\n }\n av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\\n", dc_error, ac_error, mv_error);\n is_intra_likely= is_intra_more_likely(s);\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[i];\n error= s->error_status_table[mb_xy];\n if(!((error&DC_ERROR) && (error&MV_ERROR)))\n continue;\n if(is_intra_likely)\n s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;\n else\n s->current_picture.f.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;\n }\n if (!s->last_picture.f.data[0] && !s->next_picture.f.data[0])\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[i];\n if (!IS_INTRA(s->current_picture.f.mb_type[mb_xy]))\n s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;\n }\n for(mb_y=0; mb_y<s->mb_height; mb_y++){\n for(mb_x=0; mb_x<s->mb_width; mb_x++){\n const int mb_xy= mb_x + mb_y * s->mb_stride;\n const int mb_type= s->current_picture.f.mb_type[mb_xy];\n int dir = !s->last_picture.f.data[0];\n error= s->error_status_table[mb_xy];\n if(IS_INTRA(mb_type)) continue;\n if(error&MV_ERROR) continue;\n if(!(error&AC_ERROR)) continue;\n s->mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;\n s->mb_intra=0;\n s->mb_skipped=0;\n if(IS_8X8(mb_type)){\n int mb_index= mb_x*2 + mb_y*2*s->b8_stride;\n int j;\n s->mv_type = MV_TYPE_8X8;\n for(j=0; j<4; j++){\n s->mv[0][j][0] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];\n s->mv[0][j][1] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];\n }\n }else{\n s->mv_type = MV_TYPE_16X16;\n s->mv[0][0][0] = s->current_picture.f.motion_val[dir][ mb_x*2 + mb_y*2*s->b8_stride ][0];\n s->mv[0][0][1] = s->current_picture.f.motion_val[dir][ mb_x*2 + mb_y*2*s->b8_stride ][1];\n }\n s->dsp.clear_blocks(s->block[0]);\n s->mb_x= mb_x;\n s->mb_y= mb_y;\n decode_mb(s, 0 );\n }\n }\n if(s->pict_type==AV_PICTURE_TYPE_B){\n for(mb_y=0; mb_y<s->mb_height; mb_y++){\n for(mb_x=0; mb_x<s->mb_width; mb_x++){\n int xy= mb_x*2 + mb_y*2*s->b8_stride;\n const int mb_xy= mb_x + mb_y * s->mb_stride;\n const int mb_type= s->current_picture.f.mb_type[mb_xy];\n error= s->error_status_table[mb_xy];\n if(IS_INTRA(mb_type)) continue;\n if(!(error&MV_ERROR)) continue;\n if(!(error&AC_ERROR)) continue;\n s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;\n if(!s->last_picture.f.data[0]) s->mv_dir &= ~MV_DIR_FORWARD;\n if(!s->next_picture.f.data[0]) s->mv_dir &= ~MV_DIR_BACKWARD;\n s->mb_intra=0;\n s->mv_type = MV_TYPE_16X16;\n s->mb_skipped=0;\n if(s->pp_time){\n int time_pp= s->pp_time;\n int time_pb= s->pb_time;\n if (s->avctx->codec_id == CODEC_ID_H264) {\n } else {\n ff_thread_await_progress((AVFrame *) s->next_picture_ptr,\n mb_y, 0);\n }\n s->mv[0][0][0] = s->next_picture.f.motion_val[0][xy][0] * time_pb / time_pp;\n s->mv[0][0][1] = s->next_picture.f.motion_val[0][xy][1] * time_pb / time_pp;\n s->mv[1][0][0] = s->next_picture.f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;\n s->mv[1][0][1] = s->next_picture.f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;\n }else{\n s->mv[0][0][0]= 0;\n s->mv[0][0][1]= 0;\n s->mv[1][0][0]= 0;\n s->mv[1][0][1]= 0;\n }\n s->dsp.clear_blocks(s->block[0]);\n s->mb_x= mb_x;\n s->mb_y= mb_y;\n decode_mb(s, 0);\n }\n }\n }else\n guess_mv(s);\n if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)\n goto ec_clean;\n for(mb_y=0; mb_y<s->mb_height; mb_y++){\n for(mb_x=0; mb_x<s->mb_width; mb_x++){\n int dc, dcu, dcv, y, n;\n int16_t *dc_ptr;\n uint8_t *dest_y, *dest_cb, *dest_cr;\n const int mb_xy= mb_x + mb_y * s->mb_stride;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n error= s->error_status_table[mb_xy];\n if(IS_INTRA(mb_type) && s->partitioned_frame) continue;\n dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;\n dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;\n dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;\n dc_ptr= &s->dc_val[0][mb_x*2 + mb_y*2*s->b8_stride];\n for(n=0; n<4; n++){\n dc=0;\n for(y=0; y<8; y++){\n int x;\n for(x=0; x<8; x++){\n dc+= dest_y[x + (n&1)*8 + (y + (n>>1)*8)*s->linesize];\n }\n }\n dc_ptr[(n&1) + (n>>1)*s->b8_stride]= (dc+4)>>3;\n }\n dcu=dcv=0;\n for(y=0; y<8; y++){\n int x;\n for(x=0; x<8; x++){\n dcu+=dest_cb[x + y*(s->uvlinesize)];\n dcv+=dest_cr[x + y*(s->uvlinesize)];\n }\n }\n s->dc_val[1][mb_x + mb_y*s->mb_stride]= (dcu+4)>>3;\n s->dc_val[2][mb_x + mb_y*s->mb_stride]= (dcv+4)>>3;\n }\n }\n guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);\n guess_dc(s, s->dc_val[1], s->mb_width , s->mb_height , s->mb_stride, 0);\n guess_dc(s, s->dc_val[2], s->mb_width , s->mb_height , s->mb_stride, 0);\n filter181(s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride);\n for(mb_y=0; mb_y<s->mb_height; mb_y++){\n for(mb_x=0; mb_x<s->mb_width; mb_x++){\n uint8_t *dest_y, *dest_cb, *dest_cr;\n const int mb_xy= mb_x + mb_y * s->mb_stride;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n error= s->error_status_table[mb_xy];\n if(IS_INTER(mb_type)) continue;\n if(!(error&AC_ERROR)) continue;\n dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;\n dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;\n dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;\n put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);\n }\n }\n if(s->avctx->error_concealment&FF_EC_DEBLOCK){\n h_block_filter(s, s->current_picture.f.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);\n h_block_filter(s, s->current_picture.f.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);\n h_block_filter(s, s->current_picture.f.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);\n v_block_filter(s, s->current_picture.f.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);\n v_block_filter(s, s->current_picture.f.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);\n v_block_filter(s, s->current_picture.f.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);\n }\nec_clean:\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[i];\n int error= s->error_status_table[mb_xy];\n if(s->pict_type!=AV_PICTURE_TYPE_B && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){\n s->mbskip_table[mb_xy]=0;\n }\n s->mbintra_table[mb_xy]=1;\n }\n}', 'static int is_intra_more_likely(MpegEncContext *s){\n int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;\n if (!s->last_picture_ptr || !s->last_picture_ptr->f.data[0]) return 1;\n undamaged_count=0;\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[i];\n const int error= s->error_status_table[mb_xy];\n if(!((error&DC_ERROR) && (error&MV_ERROR)))\n undamaged_count++;\n }\n if(s->codec_id == CODEC_ID_H264){\n H264Context *h= (void*)s;\n if (h->list_count <= 0 || h->ref_count[0] <= 0 || !h->ref_list[0][0].f.data[0])\n return 1;\n }\n if(undamaged_count < 5) return 0;\n if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration && s->pict_type == AV_PICTURE_TYPE_I)\n return 1;\n skip_amount= FFMAX(undamaged_count/50, 1);\n is_intra_likely=0;\n j=0;\n for(mb_y= 0; mb_y<s->mb_height-1; mb_y++){\n for(mb_x= 0; mb_x<s->mb_width; mb_x++){\n int error;\n const int mb_xy= mb_x + mb_y*s->mb_stride;\n error= s->error_status_table[mb_xy];\n if((error&DC_ERROR) && (error&MV_ERROR))\n continue;\n j++;\n if((j%skip_amount) != 0) continue;\n if(s->pict_type==AV_PICTURE_TYPE_I){\n uint8_t *mb_ptr = s->current_picture.f.data[0] + mb_x*16 + mb_y*16*s->linesize;\n uint8_t *last_mb_ptr= s->last_picture.f.data [0] + mb_x*16 + mb_y*16*s->linesize;\n if (s->avctx->codec_id == CODEC_ID_H264) {\n } else {\n ff_thread_await_progress((AVFrame *) s->last_picture_ptr,\n mb_y, 0);\n }\n is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr , s->linesize, 16);\n is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16);\n }else{\n if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))\n is_intra_likely++;\n else\n is_intra_likely--;\n }\n }\n }\n return is_intra_likely > 0;\n}', 'static void decode_mb(MpegEncContext *s, int ref){\n s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;\n s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);\n s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);\n if(CONFIG_H264_DECODER && s->codec_id == CODEC_ID_H264){\n H264Context *h= (void*)s;\n h->mb_xy= s->mb_x + s->mb_y*s->mb_stride;\n memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));\n assert(ref>=0);\n if(ref >= h->ref_count[0])\n ref=0;\n fill_rectangle(&s->current_picture.f.ref_index[0][4*h->mb_xy], 2, 2, 2, ref, 1);\n fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);\n fill_rectangle(h->mv_cache[0][ scan8[0] ], 4, 4, 8, pack16to32(s->mv[0][0][0],s->mv[0][0][1]), 4);\n assert(!FRAME_MBAFF);\n ff_h264_hl_decode_mb(h);\n }else{\n assert(ref==0);\n MPV_decode_mb(s, s->block);\n }\n}', 'void ff_h264_hl_decode_mb(H264Context *h){\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;\n if (CHROMA444) {\n if(is_complex || h->pixel_shift)\n hl_decode_mb_444_complex(h);\n else\n hl_decode_mb_444_simple(h);\n } else if (is_complex) {\n hl_decode_mb_complex(h);\n } else if (h->pixel_shift) {\n hl_decode_mb_simple_16(h);\n } else\n hl_decode_mb_simple_8(h);\n}', 'static void av_noinline hl_decode_mb_444_complex(H264Context *h){\n hl_decode_mb_444_internal(h, 0, h->pixel_shift);\n}', 'static av_always_inline void hl_decode_mb_444_internal(H264Context *h, int simple, int pixel_shift){\n MpegEncContext * const s = &h->s;\n const int mb_x= s->mb_x;\n const int mb_y= s->mb_y;\n const int mb_xy= h->mb_xy;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n uint8_t *dest[3];\n int linesize;\n int i, j, p;\n int *block_offset = &h->block_offset[0];\n const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);\n const int plane_count = (simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) ? 3 : 1;\n for (p = 0; p < plane_count; p++)\n {\n dest[p] = s->current_picture.f.data[p] + ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;\n s->dsp.prefetch(dest[p] + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);\n }\n h->list_counts[mb_xy]= h->list_count;\n if (!simple && MB_FIELD) {\n linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize * 2;\n block_offset = &h->block_offset[48];\n if(mb_y&1)\n for (p = 0; p < 3; p++)\n dest[p] -= s->linesize*15;\n if(FRAME_MBAFF) {\n int list;\n for(list=0; list<h->list_count; list++){\n if(!USES_LIST(mb_type, list))\n continue;\n if(IS_16X16(mb_type)){\n int8_t *ref = &h->ref_cache[list][scan8[0]];\n fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);\n }else{\n for(i=0; i<16; i+=4){\n int ref = h->ref_cache[list][scan8[i]];\n if(ref >= 0)\n fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);\n }\n }\n }\n }\n } else {\n linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize;\n }\n if (!simple && IS_INTRA_PCM(mb_type)) {\n if (pixel_shift) {\n const int bit_depth = h->sps.bit_depth_luma;\n GetBitContext gb;\n init_get_bits(&gb, (uint8_t*)h->mb, 768*bit_depth);\n for (p = 0; p < plane_count; p++) {\n for (i = 0; i < 16; i++) {\n uint16_t *tmp = (uint16_t*)(dest[p] + i*linesize);\n for (j = 0; j < 16; j++)\n tmp[j] = get_bits(&gb, bit_depth);\n }\n }\n } else {\n for (p = 0; p < plane_count; p++) {\n for (i = 0; i < 16; i++) {\n memcpy(dest[p] + i*linesize, h->mb + p*128 + i*8, 16);\n }\n }\n }\n } else {\n if(IS_INTRA(mb_type)){\n if(h->deblocking_filter)\n xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 1, 1, simple, pixel_shift);\n for (p = 0; p < plane_count; p++)\n hl_decode_mb_predict_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);\n if(h->deblocking_filter)\n xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 0, 1, simple, pixel_shift);\n }else{\n hl_motion(h, dest[0], dest[1], dest[2],\n s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,\n s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,\n h->h264dsp.weight_h264_pixels_tab,\n h->h264dsp.biweight_h264_pixels_tab, pixel_shift, 3);\n }\n for (p = 0; p < plane_count; p++)\n hl_decode_mb_idct_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);\n }\n if(h->cbp || IS_INTRA(mb_type))\n {\n s->dsp.clear_blocks(h->mb);\n s->dsp.clear_blocks(h->mb+(24*16<<pixel_shift));\n }\n}', 'static av_always_inline void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,\n qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),\n qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),\n h264_weight_func *weight_op, h264_biweight_func *weight_avg,\n int pixel_shift, int chroma_idc)\n{\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n assert(IS_INTER(mb_type));\n if(HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))\n await_references(h);\n prefetch_motion(h, 0, pixel_shift, chroma_idc);\n if(IS_16X16(mb_type)){\n mc_part(h, 0, 1, 16, 0, dest_y, dest_cb, dest_cr, 0, 0,\n qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],\n weight_op, weight_avg,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),\n pixel_shift, chroma_idc);\n }else if(IS_16X8(mb_type)){\n mc_part(h, 0, 0, 8, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 0,\n qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],\n weight_op, weight_avg,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),\n pixel_shift, chroma_idc);\n mc_part(h, 8, 0, 8, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 4,\n qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],\n weight_op, weight_avg,\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),\n pixel_shift, chroma_idc);\n }else if(IS_8X16(mb_type)){\n mc_part(h, 0, 0, 16, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,\n qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],\n &weight_op[1], &weight_avg[1],\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),\n pixel_shift, chroma_idc);\n mc_part(h, 4, 0, 16, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,\n qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],\n &weight_op[1], &weight_avg[1],\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),\n pixel_shift, chroma_idc);\n }else{\n int i;\n assert(IS_8X8(mb_type));\n for(i=0; i<4; i++){\n const int sub_mb_type= h->sub_mb_type[i];\n const int n= 4*i;\n int x_offset= (i&1)<<2;\n int y_offset= (i&2)<<1;\n if(IS_SUB_8X8(sub_mb_type)){\n mc_part(h, n, 1, 8, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,\n qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],\n &weight_op[1], &weight_avg[1],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma_idc);\n }else if(IS_SUB_8X4(sub_mb_type)){\n mc_part(h, n , 0, 4, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset,\n qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],\n &weight_op[1], &weight_avg[1],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma_idc);\n mc_part(h, n+2, 0, 4, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,\n qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],\n &weight_op[1], &weight_avg[1],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma_idc);\n }else if(IS_SUB_4X8(sub_mb_type)){\n mc_part(h, n , 0, 8, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,\n qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],\n &weight_op[2], &weight_avg[2],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma_idc);\n mc_part(h, n+1, 0, 8, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,\n qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],\n &weight_op[2], &weight_avg[2],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma_idc);\n }else{\n int j;\n assert(IS_SUB_4X4(sub_mb_type));\n for(j=0; j<4; j++){\n int sub_x_offset= x_offset + 2*(j&1);\n int sub_y_offset= y_offset + (j&2);\n mc_part(h, n+j, 1, 4, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,\n qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],\n &weight_op[2], &weight_avg[2],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma_idc);\n }\n }\n }\n }\n prefetch_motion(h, 1, pixel_shift, chroma_idc);\n}', 'static void await_references(H264Context *h){\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n int refs[2][48];\n int nrefs[2] = {0};\n int ref, list;\n memset(refs, -1, sizeof(refs));\n if(IS_16X16(mb_type)){\n get_lowest_part_y(h, refs, 0, 16, 0,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);\n }else if(IS_16X8(mb_type)){\n get_lowest_part_y(h, refs, 0, 8, 0,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, 8, 8, 8,\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);\n }else if(IS_8X16(mb_type)){\n get_lowest_part_y(h, refs, 0, 16, 0,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, 4, 16, 0,\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);\n }else{\n int i;\n assert(IS_8X8(mb_type));\n for(i=0; i<4; i++){\n const int sub_mb_type= h->sub_mb_type[i];\n const int n= 4*i;\n int y_offset= (i&2)<<2;\n if(IS_SUB_8X8(sub_mb_type)){\n get_lowest_part_y(h, refs, n , 8, y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n }else if(IS_SUB_8X4(sub_mb_type)){\n get_lowest_part_y(h, refs, n , 4, y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, n+2, 4, y_offset+4,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n }else if(IS_SUB_4X8(sub_mb_type)){\n get_lowest_part_y(h, refs, n , 8, y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, n+1, 8, y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n }else{\n int j;\n assert(IS_SUB_4X4(sub_mb_type));\n for(j=0; j<4; j++){\n int sub_y_offset= y_offset + 2*(j&2);\n get_lowest_part_y(h, refs, n+j, 4, sub_y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n }\n }\n }\n }\n for(list=h->list_count-1; list>=0; list--){\n for(ref=0; ref<48 && nrefs[list]; ref++){\n int row = refs[list][ref];\n if(row >= 0){\n Picture *ref_pic = &h->ref_list[list][ref];\n int ref_field = ref_pic->f.reference - 1;\n int ref_field_picture = ref_pic->field_picture;\n int pic_height = 16*s->mb_height >> ref_field_picture;\n row <<= MB_MBAFF;\n nrefs[list]--;\n if(!FIELD_PICTURE && ref_field_picture){\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) - !(row&1), pic_height-1), 1);\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) , pic_height-1), 0);\n }else if(FIELD_PICTURE && !ref_field_picture){\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row*2 + ref_field , pic_height-1), 0);\n }else if(FIELD_PICTURE){\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), ref_field);\n }else{\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), 0);\n }\n }\n }\n }\n}']
|
1,260 | 0 |
https://github.com/openssl/openssl/blob/9f9442918aeaed5dc2442d81ab8d29fe3e1fb906/crypto/bn/bn_lib.c/#L333
|
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
bn_check_top(b);
if (a == b)
return a;
if (bn_wexpand(a, b->top) == NULL)
return NULL;
if (b->top > 0)
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)
BN_set_flags(a, BN_FLG_CONSTTIME);
a->top = b->top;
a->neg = b->neg;
bn_check_top(a);
return a;
}
|
['static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)\n{\n DH *dh = NULL;\n DH_PKEY_CTX *dctx = ctx->data;\n BN_GENCB *pcb;\n int ret;\n if (dctx->rfc5114_param) {\n switch (dctx->rfc5114_param) {\n case 1:\n dh = DH_get_1024_160();\n break;\n case 2:\n dh = DH_get_2048_224();\n break;\n case 3:\n dh = DH_get_2048_256();\n break;\n default:\n return -2;\n }\n EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);\n return 1;\n }\n if (ctx->pkey_gencb) {\n pcb = BN_GENCB_new();\n if (pcb == NULL)\n return 0;\n evp_pkey_set_cb_translate(pcb, ctx);\n } else\n pcb = NULL;\n#ifndef OPENSSL_NO_DSA\n if (dctx->use_dsa) {\n DSA *dsa_dh;\n dsa_dh = dsa_dh_generate(dctx, pcb);\n BN_GENCB_free(pcb);\n if (dsa_dh == NULL)\n return 0;\n dh = DSA_dup_DH(dsa_dh);\n DSA_free(dsa_dh);\n if (!dh)\n return 0;\n EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);\n return 1;\n }\n#endif\n dh = DH_new();\n if (dh == NULL) {\n BN_GENCB_free(pcb);\n return 0;\n }\n ret = DH_generate_parameters_ex(dh,\n dctx->prime_len, dctx->generator, pcb);\n BN_GENCB_free(pcb);\n if (ret)\n EVP_PKEY_assign_DH(pkey, dh);\n else\n DH_free(dh);\n return ret;\n}', 'static DSA *dsa_dh_generate(DH_PKEY_CTX *dctx, BN_GENCB *pcb)\n{\n DSA *ret;\n int rv = 0;\n int prime_len = dctx->prime_len;\n int subprime_len = dctx->subprime_len;\n const EVP_MD *md = dctx->md;\n if (dctx->use_dsa > 2)\n return NULL;\n ret = DSA_new();\n if (ret == NULL)\n return NULL;\n if (subprime_len == -1) {\n if (prime_len >= 2048)\n subprime_len = 256;\n else\n subprime_len = 160;\n }\n if (md == NULL) {\n if (prime_len >= 2048)\n md = EVP_sha256();\n else\n md = EVP_sha1();\n }\n if (dctx->use_dsa == 1)\n rv = dsa_builtin_paramgen(ret, prime_len, subprime_len, md,\n NULL, 0, NULL, NULL, NULL, pcb);\n else if (dctx->use_dsa == 2)\n rv = dsa_builtin_paramgen2(ret, prime_len, subprime_len, md,\n NULL, 0, -1, NULL, NULL, NULL, pcb);\n if (rv <= 0) {\n DSA_free(ret);\n return NULL;\n }\n return ret;\n}', 'int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,\n const EVP_MD *evpmd, const unsigned char *seed_in,\n size_t seed_len, int idx, unsigned char *seed_out,\n int *counter_ret, unsigned long *h_ret,\n BN_GENCB *cb)\n{\n int ok = -1;\n unsigned char *seed = NULL, *seed_tmp = NULL;\n unsigned char md[EVP_MAX_MD_SIZE];\n int mdsize;\n BIGNUM *r0, *W, *X, *c, *test;\n BIGNUM *g = NULL, *q = NULL, *p = NULL;\n BN_MONT_CTX *mont = NULL;\n int i, k, n = 0, m = 0, qsize = N >> 3;\n int counter = 0;\n int r = 0;\n BN_CTX *ctx = NULL;\n EVP_MD_CTX *mctx = EVP_MD_CTX_new();\n unsigned int h = 2;\n if (mctx == NULL)\n goto err;\n if (evpmd == NULL) {\n if (N == 160)\n evpmd = EVP_sha1();\n else if (N == 224)\n evpmd = EVP_sha224();\n else\n evpmd = EVP_sha256();\n }\n mdsize = EVP_MD_size(evpmd);\n if (!ret->p || !ret->q || idx >= 0) {\n if (seed_len == 0)\n seed_len = mdsize;\n seed = OPENSSL_malloc(seed_len);\n if (seed_out)\n seed_tmp = seed_out;\n else\n seed_tmp = OPENSSL_malloc(seed_len);\n if (seed == NULL || seed_tmp == NULL)\n goto err;\n if (seed_in)\n memcpy(seed, seed_in, seed_len);\n }\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n r0 = BN_CTX_get(ctx);\n g = BN_CTX_get(ctx);\n W = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n c = BN_CTX_get(ctx);\n test = BN_CTX_get(ctx);\n if (test == NULL)\n goto err;\n if (ret->p && ret->q) {\n p = ret->p;\n q = ret->q;\n if (idx >= 0)\n memcpy(seed_tmp, seed, seed_len);\n goto g_only;\n } else {\n p = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n if (q == NULL)\n goto err;\n }\n if (!BN_lshift(test, BN_value_one(), L - 1))\n goto err;\n for (;;) {\n for (;;) {\n unsigned char *pmd;\n if (!BN_GENCB_call(cb, 0, m++))\n goto err;\n if (!seed_in) {\n if (RAND_bytes(seed, seed_len) <= 0)\n goto err;\n }\n if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))\n goto err;\n if (mdsize > qsize)\n pmd = md + mdsize - qsize;\n else\n pmd = md;\n if (mdsize < qsize)\n memset(md + mdsize, 0, qsize - mdsize);\n pmd[0] |= 0x80;\n pmd[qsize - 1] |= 0x01;\n if (!BN_bin2bn(pmd, qsize, q))\n goto err;\n r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,\n seed_in ? 1 : 0, cb);\n if (r > 0)\n break;\n if (r != 0)\n goto err;\n if (seed_in) {\n ok = 0;\n DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);\n goto err;\n }\n }\n if (seed_out)\n memcpy(seed_out, seed, seed_len);\n if (!BN_GENCB_call(cb, 2, 0))\n goto err;\n if (!BN_GENCB_call(cb, 3, 0))\n goto err;\n counter = 0;\n n = (L - 1) / (mdsize << 3);\n for (;;) {\n if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))\n goto err;\n BN_zero(W);\n for (k = 0; k <= n; k++) {\n for (i = seed_len - 1; i >= 0; i--) {\n seed[i]++;\n if (seed[i] != 0)\n break;\n }\n if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))\n goto err;\n if (!BN_bin2bn(md, mdsize, r0))\n goto err;\n if (!BN_lshift(r0, r0, (mdsize << 3) * k))\n goto err;\n if (!BN_add(W, W, r0))\n goto err;\n }\n if (!BN_mask_bits(W, L - 1))\n goto err;\n if (!BN_copy(X, W))\n goto err;\n if (!BN_add(X, X, test))\n goto err;\n if (!BN_lshift1(r0, q))\n goto err;\n if (!BN_mod(c, X, r0, ctx))\n goto err;\n if (!BN_sub(r0, c, BN_value_one()))\n goto err;\n if (!BN_sub(p, X, r0))\n goto err;\n if (BN_cmp(p, test) >= 0) {\n r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);\n if (r > 0)\n goto end;\n if (r != 0)\n goto err;\n }\n counter++;\n if (counter >= (int)(4 * L))\n break;\n }\n if (seed_in) {\n ok = 0;\n DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);\n goto err;\n }\n }\n end:\n if (!BN_GENCB_call(cb, 2, 1))\n goto err;\n g_only:\n if (!BN_sub(test, p, BN_value_one()))\n goto err;\n if (!BN_div(r0, NULL, test, q, ctx))\n goto err;\n if (idx < 0) {\n if (!BN_set_word(test, h))\n goto err;\n } else\n h = 1;\n if (!BN_MONT_CTX_set(mont, p, ctx))\n goto err;\n for (;;) {\n static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };\n if (idx >= 0) {\n md[0] = idx & 0xff;\n md[1] = (h >> 8) & 0xff;\n md[2] = h & 0xff;\n if (!EVP_DigestInit_ex(mctx, evpmd, NULL))\n goto err;\n if (!EVP_DigestUpdate(mctx, seed_tmp, seed_len))\n goto err;\n if (!EVP_DigestUpdate(mctx, ggen, sizeof(ggen)))\n goto err;\n if (!EVP_DigestUpdate(mctx, md, 3))\n goto err;\n if (!EVP_DigestFinal_ex(mctx, md, NULL))\n goto err;\n if (!BN_bin2bn(md, mdsize, test))\n goto err;\n }\n if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))\n goto err;\n if (!BN_is_one(g))\n break;\n if (idx < 0 && !BN_add(test, test, BN_value_one()))\n goto err;\n h++;\n if (idx >= 0 && h > 0xffff)\n goto err;\n }\n if (!BN_GENCB_call(cb, 3, 1))\n goto err;\n ok = 1;\n err:\n if (ok == 1) {\n if (p != ret->p) {\n BN_free(ret->p);\n ret->p = BN_dup(p);\n }\n if (q != ret->q) {\n BN_free(ret->q);\n ret->q = BN_dup(q);\n }\n BN_free(ret->g);\n ret->g = BN_dup(g);\n if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {\n ok = -1;\n goto err;\n }\n if (counter_ret != NULL)\n *counter_ret = counter;\n if (h_ret != NULL)\n *h_ret = h;\n }\n OPENSSL_free(seed);\n if (seed_out != seed_tmp)\n OPENSSL_free(seed_tmp);\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n BN_MONT_CTX_free(mont);\n EVP_MD_CTX_free(mctx);\n return ok;\n}', 'DH *DSA_dup_DH(const DSA *r)\n{\n DH *ret = NULL;\n BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL, *priv_key = NULL;\n if (r == NULL)\n goto err;\n ret = DH_new();\n if (ret == NULL)\n goto err;\n if (r->p != NULL || r->g != NULL || r->q != NULL) {\n if (r->p == NULL || r->g == NULL || r->q == NULL) {\n goto err;\n }\n p = BN_dup(r->p);\n g = BN_dup(r->g);\n q = BN_dup(r->q);\n if (p == NULL || g == NULL || q == NULL || !DH_set0_pqg(ret, p, q, g))\n goto err;\n p = g = q = NULL;\n }\n if (r->pub_key != NULL) {\n pub_key = BN_dup(r->pub_key);\n if (pub_key == NULL)\n goto err;\n if (r->priv_key != NULL) {\n priv_key = BN_dup(r->priv_key);\n if (priv_key == NULL)\n goto err;\n }\n if (!DH_set0_key(ret, pub_key, priv_key))\n goto err;\n } else if (r->priv_key != NULL) {\n goto err;\n }\n return ret;\n err:\n BN_free(p);\n BN_free(g);\n BN_free(q);\n BN_free(pub_key);\n BN_free(priv_key);\n DH_free(ret);\n return NULL;\n}', 'BIGNUM *BN_dup(const BIGNUM *a)\n{\n BIGNUM *t;\n if (a == NULL)\n return NULL;\n bn_check_top(a);\n t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();\n if (t == NULL)\n return NULL;\n if (!BN_copy(t, a)) {\n BN_free(t);\n return NULL;\n }\n bn_check_top(t);\n return t;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(a, BN_FLG_CONSTTIME);\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
|
1,261 | 0 |
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_shift.c/#L112
|
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return 0;
r->neg = a->neg;
lb = n % BN_BITS2;
rb = BN_BITS2 - lb;
f = a->d;
t = r->d;
t[a->top + nw] = 0;
if (lb == 0)
for (i = a->top - 1; i >= 0; i--)
t[nw + i] = f[i];
else
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
t[nw + i + 1] |= (l >> rb) & BN_MASK2;
t[nw + i] = (l << lb) & BN_MASK2;
}
memset(t, 0, sizeof(*t) * nw);
r->top = a->top + nw + 1;
bn_correct_top(r);
bn_check_top(r);
return 1;
}
|
['int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, j, ret = -1;\n int k;\n BN_CTX *ctx = NULL;\n BIGNUM *A1, *A1_odd, *check;\n BN_MONT_CTX *mont = NULL;\n if (BN_cmp(a, BN_value_one()) <= 0)\n return 0;\n if (checks == BN_prime_checks)\n checks = BN_prime_checks_for_size(BN_num_bits(a));\n if (!BN_is_odd(a))\n return BN_is_word(a, 2);\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG mod = BN_mod_word(a, primes[i]);\n if (mod == (BN_ULONG)-1)\n goto err;\n if (mod == 0)\n return BN_is_word(a, primes[i]);\n }\n if (!BN_GENCB_call(cb, 1, -1))\n goto err;\n }\n if (ctx_passed != NULL)\n ctx = ctx_passed;\n else if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n A1 = BN_CTX_get(ctx);\n A1_odd = BN_CTX_get(ctx);\n check = BN_CTX_get(ctx);\n if (check == NULL)\n goto err;\n if (!BN_copy(A1, a))\n goto err;\n if (!BN_sub_word(A1, 1))\n goto err;\n if (BN_is_zero(A1)) {\n ret = 0;\n goto err;\n }\n k = 1;\n while (!BN_is_bit_set(A1, k))\n k++;\n if (!BN_rshift(A1_odd, A1, k))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, a, ctx))\n goto err;\n for (i = 0; i < checks; i++) {\n if (!BN_priv_rand_range(check, A1))\n goto err;\n if (!BN_add_word(check, 1))\n goto err;\n j = witness(check, a, A1, A1_odd, k, ctx, mont);\n if (j == -1)\n goto err;\n if (j) {\n ret = 0;\n goto err;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n if (ctx_passed == NULL)\n BN_CTX_free(ctx);\n }\n BN_MONT_CTX_free(mont);\n return ret;\n}', '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}', 'static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,\n const BIGNUM *a1_odd, int k, BN_CTX *ctx,\n BN_MONT_CTX *mont)\n{\n if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont))\n return -1;\n if (BN_is_one(w))\n return 0;\n if (BN_cmp(w, a1) == 0)\n return 0;\n while (--k) {\n if (!BN_mod_mul(w, w, w, a, ctx))\n return -1;\n if (BN_is_one(w))\n return 1;\n if (BN_cmp(w, a1) == 0)\n return 0;\n }\n bn_check_top(w);\n return 1;\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (BN_is_zero(aa)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n bn_correct_top(r);\n } else\n#endif\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n top = m->top;\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_mod(&am, a, m, ctx))\n goto err;\n if (!BN_to_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits >= 0) {\n if (bits < stride)\n stride = bits + 1;\n bits -= stride;\n wvalue = bn_get_bits(p, bits + 1);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7)\n while (bits >= 0) {\n for (wvalue = 0, i = 0; i < 5; i++, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n } else {\n while (bits >= 0) {\n wvalue = bn_get_bits5(p->d, bits - 4);\n bits -= 5;\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n bits--;\n for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n while (bits >= 0) {\n wvalue = 0;\n for (i = 0; i < window; i++, bits--) {\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n }\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return 0;\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
|
1,262 | 0 |
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/bn/bn_add.c/#L219
|
int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
{
int max, min, dif;
register BN_ULONG t1, t2, *rp;
register const BN_ULONG *ap, *bp;
int i, carry;
bn_check_top(a);
bn_check_top(b);
max = a->top;
min = b->top;
dif = max - min;
if (dif < 0) {
BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);
return (0);
}
if (bn_wexpand(r, max) == NULL)
return (0);
ap = a->d;
bp = b->d;
rp = r->d;
#if 1
carry = 0;
for (i = min; i != 0; i--) {
t1 = *(ap++);
t2 = *(bp++);
if (carry) {
carry = (t1 <= t2);
t1 = (t1 - t2 - 1) & BN_MASK2;
} else {
carry = (t1 < t2);
t1 = (t1 - t2) & BN_MASK2;
}
*(rp++) = t1 & BN_MASK2;
}
#else
carry = bn_sub_words(rp, ap, bp, min);
ap += min;
bp += min;
rp += min;
#endif
if (carry) {
if (!dif)
return 0;
while (dif) {
dif--;
t1 = *(ap++);
t2 = (t1 - 1) & BN_MASK2;
*(rp++) = t2;
if (t1)
break;
}
}
#if 0
memcpy(rp, ap, sizeof(*rp) * (max - i));
#else
if (rp != ap) {
for (;;) {
if (!dif--)
break;
rp[0] = ap[0];
if (!dif--)
break;
rp[1] = ap[1];
if (!dif--)
break;
rp[2] = ap[2];
if (!dif--)
break;
rp[3] = ap[3];
rp += 4;
ap += 4;
}
}
#endif
r->top = max;
r->neg = 0;
bn_correct_top(r);
return (1);
}
|
['int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const BIGNUM *m)\n{\n if (!BN_uadd(r, a, b))\n return 0;\n if (BN_ucmp(r, m) >= 0)\n return BN_usub(r, r, m);\n return 1;\n}', 'int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n const BN_ULONG *ap, *bp;\n BN_ULONG *rp, carry, t1, t2;\n const BIGNUM *tmp;\n bn_check_top(a);\n bn_check_top(b);\n if (a->top < b->top) {\n tmp = a;\n a = b;\n b = tmp;\n }\n max = a->top;\n min = b->top;\n dif = max - min;\n if (bn_wexpand(r, max + 1) == NULL)\n return 0;\n r->top = max;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n carry = bn_add_words(rp, ap, bp, min);\n rp += min;\n ap += min;\n bp += min;\n if (carry) {\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 + 1) & BN_MASK2;\n *(rp++) = t2;\n if (t2) {\n carry = 0;\n break;\n }\n }\n if (carry) {\n *rp = 1;\n r->top++;\n }\n }\n if (dif && rp != ap)\n while (dif--)\n *(rp++) = *(ap++);\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', 'int BN_ucmp(const BIGNUM *a, const BIGNUM *b)\n{\n int i;\n BN_ULONG t1, t2, *ap, *bp;\n bn_check_top(a);\n bn_check_top(b);\n i = a->top - b->top;\n if (i != 0)\n return (i);\n ap = a->d;\n bp = b->d;\n for (i = a->top - 1; i >= 0; i--) {\n t1 = ap[i];\n t2 = bp[i];\n if (t1 != t2)\n return ((t1 > t2) ? 1 : -1);\n }\n return (0);\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n register BN_ULONG t1, t2, *rp;\n register const BN_ULONG *ap, *bp;\n int i, carry;\n bn_check_top(a);\n bn_check_top(b);\n max = a->top;\n min = b->top;\n dif = max - min;\n if (dif < 0) {\n BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n return (0);\n }\n if (bn_wexpand(r, max) == NULL)\n return (0);\n ap = a->d;\n bp = b->d;\n rp = r->d;\n#if 1\n carry = 0;\n for (i = min; i != 0; i--) {\n t1 = *(ap++);\n t2 = *(bp++);\n if (carry) {\n carry = (t1 <= t2);\n t1 = (t1 - t2 - 1) & BN_MASK2;\n } else {\n carry = (t1 < t2);\n t1 = (t1 - t2) & BN_MASK2;\n }\n *(rp++) = t1 & BN_MASK2;\n }\n#else\n carry = bn_sub_words(rp, ap, bp, min);\n ap += min;\n bp += min;\n rp += min;\n#endif\n if (carry) {\n if (!dif)\n return 0;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 - 1) & BN_MASK2;\n *(rp++) = t2;\n if (t1)\n break;\n }\n }\n#if 0\n memcpy(rp, ap, sizeof(*rp) * (max - i));\n#else\n if (rp != ap) {\n for (;;) {\n if (!dif--)\n break;\n rp[0] = ap[0];\n if (!dif--)\n break;\n rp[1] = ap[1];\n if (!dif--)\n break;\n rp[2] = ap[2];\n if (!dif--)\n break;\n rp[3] = ap[3];\n rp += 4;\n ap += 4;\n }\n }\n#endif\n r->top = max;\n r->neg = 0;\n bn_correct_top(r);\n return (1);\n}']
|
1,263 | 0 |
https://github.com/openssl/openssl/blob/0350ef69add8758dd180e73cbc7c1961bf64e503/crypto/bn/bn_ctx.c/#L353
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['static int generate_key(DH *dh)\n{\n int ok = 0;\n int generate_new_key = 0;\n unsigned l;\n BN_CTX *ctx;\n BN_MONT_CTX *mont = NULL;\n BIGNUM *pub_key = NULL, *priv_key = NULL;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n if (dh->priv_key == NULL) {\n priv_key = BN_new();\n if (priv_key == NULL)\n goto err;\n generate_new_key = 1;\n } else\n priv_key = dh->priv_key;\n if (dh->pub_key == NULL) {\n pub_key = BN_new();\n if (pub_key == NULL)\n goto err;\n } else\n pub_key = dh->pub_key;\n if (dh->flags & DH_FLAG_CACHE_MONT_P) {\n mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,\n CRYPTO_LOCK_DH, dh->p, ctx);\n if (!mont)\n goto err;\n }\n if (generate_new_key) {\n if (dh->q) {\n do {\n if (!BN_rand_range(priv_key, dh->q))\n goto err;\n }\n while (BN_is_zero(priv_key) || BN_is_one(priv_key));\n } else {\n l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;\n if (!BN_rand(priv_key, l, 0, 0))\n goto err;\n }\n }\n {\n BIGNUM *local_prk = NULL;\n BIGNUM *prk;\n if ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) == 0) {\n local_prk = prk = BN_new();\n BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);\n } else\n prk = priv_key;\n if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {\n if (local_prk)\n BN_free(local_prk);\n goto err;\n }\n if (local_prk)\n BN_free(local_prk);\n }\n dh->pub_key = pub_key;\n dh->priv_key = priv_key;\n ok = 1;\n err:\n if (ok != 1)\n DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);\n if ((pub_key != NULL) && (dh->pub_key == NULL))\n BN_free(pub_key);\n if ((priv_key != NULL) && (dh->priv_key == NULL))\n BN_free(priv_key);\n BN_CTX_free(ctx);\n return (ok);\n}', 'BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,\n const BIGNUM *mod, BN_CTX *ctx)\n{\n BN_MONT_CTX *ret;\n CRYPTO_r_lock(lock);\n ret = *pmont;\n CRYPTO_r_unlock(lock);\n if (ret)\n return ret;\n ret = BN_MONT_CTX_new();\n if (!ret)\n return NULL;\n if (!BN_MONT_CTX_set(ret, mod, ctx)) {\n BN_MONT_CTX_free(ret);\n return NULL;\n }\n CRYPTO_w_lock(lock);\n if (*pmont) {\n BN_MONT_CTX_free(ret);\n ret = *pmont;\n } else\n *pmont = ret;\n CRYPTO_w_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 BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n BN_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048))) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM local_A, local_B;\n BIGNUM *pA, *pB;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n pB = &local_B;\n BN_with_flags(pB, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, pB, A, ctx))\n goto err;\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n pA = &local_A;\n BN_with_flags(pA, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, pA, B, ctx))\n goto err;\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
1,264 | 0 |
https://github.com/openssl/openssl/blob/55525742f4c2bf416013fc3a75ec642775d97f80/crypto/bn/bn_ctx.c/#L353
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int test_sqrt(BIO *bp, BN_CTX *ctx)\n\t{\n\tBN_GENCB cb;\n\tBIGNUM *a,*p,*r;\n\tint i, j;\n\tint ret = 0;\n\ta = BN_new();\n\tp = BN_new();\n\tr = BN_new();\n\tif (a == NULL || p == NULL || r == NULL) goto err;\n\tBN_GENCB_set(&cb, genprime_cb, NULL);\n\tfor (i = 0; i < 16; i++)\n\t\t{\n\t\tif (i < 8)\n\t\t\t{\n\t\t\tunsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };\n\t\t\tif (!BN_set_word(p, primes[i])) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_set_word(a, 32)) goto err;\n\t\t\tif (!BN_set_word(r, 2*i + 1)) goto err;\n\t\t\tif (!BN_generate_prime_ex(p, 256, 0, a, r, &cb)) goto err;\n\t\t\tputc(\'\\n\', stderr);\n\t\t\t}\n\t\tp->neg = rand_neg();\n\t\tfor (j = 0; j < num2; j++)\n\t\t\t{\n\t\t\tif (!BN_bntest_rand(r, 256, 0, 3)) goto err;\n\t\t\tif (!BN_nnmod(r, r, p, ctx)) goto err;\n\t\t\tif (!BN_mod_sqr(r, r, p, ctx)) goto err;\n\t\t\tif (!BN_bntest_rand(a, 256, 0, 3)) goto err;\n\t\t\tif (!BN_nnmod(a, a, p, ctx)) goto err;\n\t\t\tif (!BN_mod_sqr(a, a, p, ctx)) goto err;\n\t\t\tif (!BN_mul(a, a, r, ctx)) goto err;\n\t\t\tif (rand_neg())\n\t\t\t\tif (!BN_sub(a, a, p)) goto err;\n\t\t\tif (!BN_mod_sqrt(r, a, p, ctx)) goto err;\n\t\t\tif (!BN_mod_sqr(r, r, p, ctx)) goto err;\n\t\t\tif (!BN_nnmod(a, a, p, ctx)) goto err;\n\t\t\tif (BN_cmp(a, r) != 0)\n\t\t\t\t{\n\t\t\t\tfprintf(stderr, "BN_mod_sqrt failed: a = ");\n\t\t\t\tBN_print_fp(stderr, a);\n\t\t\t\tfprintf(stderr, ", r = ");\n\t\t\t\tBN_print_fp(stderr, r);\n\t\t\t\tfprintf(stderr, ", p = ");\n\t\t\t\tBN_print_fp(stderr, p);\n\t\t\t\tfprintf(stderr, "\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tputc(\'.\', stderr);\n\t\t\tfflush(stderr);\n\t\t\t}\n\t\tputc(\'\\n\', stderr);\n\t\tfflush(stderr);\n\t\t}\n\tret = 1;\n err:\n\tif (a != NULL) BN_free(a);\n\tif (p != NULL) BN_free(p);\n\tif (r != NULL) BN_free(r);\n\treturn ret;\n\t}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n\tif (!(BN_mod(r,m,d,ctx)))\n\t\treturn 0;\n\tif (!r->neg)\n\t\treturn 1;\n\treturn (d->neg ? BN_sub : BN_add)(r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_div_no_branch(dv, rm, num, divisor, ctx);\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV_NO_BRANCH,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_START,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
|
1,265 | 0 |
https://github.com/openssl/openssl/blob/8ccc237720d59cdf249c2c901d19f1fec739e66e/test/lhash_test.c/#L188
|
static int test_stress(void)
{
LHASH_OF(int) *h = lh_int_new(&stress_hash, &int_cmp);
const unsigned int n = 2500000;
unsigned int i;
int testresult = 0, *p;
if (!TEST_ptr(h))
goto end;
for (i = 0; i < n; i++) {
p = OPENSSL_malloc(sizeof(i));
if (!TEST_ptr(p)) {
TEST_info("lhash stress out of memory %d", i);
goto end;
}
*p = 3 * i + 1;
lh_int_insert(h, p);
}
if (!TEST_int_eq(lh_int_num_items(h), n))
goto end;
fprintf(stderr, "hash full statistics:\n");
OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);
fprintf(stderr, "\nhash full node usage:\n");
OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);
for (i = 0; i < n; i++) {
const int j = (7 * i + 4) % n * 3 + 1;
if (!TEST_ptr(p = lh_int_delete(h, &j))) {
TEST_info("lhash stress delete %d\n", i);
goto end;
}
if (!TEST_int_eq(*p, j)) {
TEST_info("lhash stress bad value %d", i);
goto end;
}
OPENSSL_free(p);
}
fprintf(stderr, "\nhash empty statistics:\n");
OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);
fprintf(stderr, "\nhash empty node usage:\n");
OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);
testresult = 1;
end:
lh_int_free(h);
return testresult;
}
|
['static int test_stress(void)\n{\n LHASH_OF(int) *h = lh_int_new(&stress_hash, &int_cmp);\n const unsigned int n = 2500000;\n unsigned int i;\n int testresult = 0, *p;\n if (!TEST_ptr(h))\n goto end;\n for (i = 0; i < n; i++) {\n p = OPENSSL_malloc(sizeof(i));\n if (!TEST_ptr(p)) {\n TEST_info("lhash stress out of memory %d", i);\n goto end;\n }\n *p = 3 * i + 1;\n lh_int_insert(h, p);\n }\n if (!TEST_int_eq(lh_int_num_items(h), n))\n goto end;\n fprintf(stderr, "hash full statistics:\\n");\n OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);\n fprintf(stderr, "\\nhash full node usage:\\n");\n OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);\n for (i = 0; i < n; i++) {\n const int j = (7 * i + 4) % n * 3 + 1;\n if (!TEST_ptr(p = lh_int_delete(h, &j))) {\n TEST_info("lhash stress delete %d\\n", i);\n goto end;\n }\n if (!TEST_int_eq(*p, j)) {\n TEST_info("lhash stress bad value %d", i);\n goto end;\n }\n OPENSSL_free(p);\n }\n fprintf(stderr, "\\nhash empty statistics:\\n");\n OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);\n fprintf(stderr, "\\nhash empty node usage:\\n");\n OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);\n testresult = 1;\nend:\n lh_int_free(h);\n return testresult;\n}', 'DEFINE_LHASH_OF(int)', '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 [%p] != NULL", s, p);\n return 0;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))\n return NULL;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {\n lh->error++;\n return (NULL);\n }\n nn->data = data;\n nn->next = NULL;\n nn->hash = hash;\n *rn = nn;\n ret = NULL;\n lh->num_insert++;\n lh->num_items++;\n } else {\n ret = (*rn)->data;\n (*rn)->data = data;\n lh->num_replace++;\n }\n return (ret);\n}']
|
1,266 | 0 |
https://github.com/libav/libav/blob/8f935b9271052be8f97d655081b94b68b6c23bfb/libavformat/apetag.c/#L108
|
void ff_ape_parse_tag(AVFormatContext *s)
{
AVIOContext *pb = s->pb;
int file_size = url_fsize(pb);
uint32_t val, fields, tag_bytes;
uint8_t buf[8];
int i;
if (file_size < APE_TAG_FOOTER_BYTES)
return;
url_fseek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
avio_read(pb, buf, 8);
if (strncmp(buf, "APETAGEX", 8)) {
return;
}
val = avio_rl32(pb);
if (val > APE_TAG_VERSION) {
av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
return;
}
tag_bytes = avio_rl32(pb);
if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
return;
}
fields = avio_rl32(pb);
if (fields > 65536) {
av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
return;
}
val = avio_rl32(pb);
if (val & APE_TAG_FLAG_IS_HEADER) {
av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
return;
}
url_fseek(pb, file_size - tag_bytes, SEEK_SET);
for (i=0; i<fields; i++)
if (ape_tag_read_field(s) < 0) break;
}
|
['void ff_ape_parse_tag(AVFormatContext *s)\n{\n AVIOContext *pb = s->pb;\n int file_size = url_fsize(pb);\n uint32_t val, fields, tag_bytes;\n uint8_t buf[8];\n int i;\n if (file_size < APE_TAG_FOOTER_BYTES)\n return;\n url_fseek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);\n avio_read(pb, buf, 8);\n if (strncmp(buf, "APETAGEX", 8)) {\n return;\n }\n val = avio_rl32(pb);\n if (val > APE_TAG_VERSION) {\n av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\\n", APE_TAG_VERSION);\n return;\n }\n tag_bytes = avio_rl32(pb);\n if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {\n av_log(s, AV_LOG_ERROR, "Tag size is way too big\\n");\n return;\n }\n fields = avio_rl32(pb);\n if (fields > 65536) {\n av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\\n", fields);\n return;\n }\n val = avio_rl32(pb);\n if (val & APE_TAG_FLAG_IS_HEADER) {\n av_log(s, AV_LOG_ERROR, "APE Tag is a header\\n");\n return;\n }\n url_fseek(pb, file_size - tag_bytes, SEEK_SET);\n for (i=0; i<fields; i++)\n if (ape_tag_read_field(s) < 0) break;\n}', 'int64_t url_fsize(AVIOContext *s)\n{\n int64_t size;\n if(!s)\n return AVERROR(EINVAL);\n if (!s->seek)\n return AVERROR(ENOSYS);\n size = s->seek(s->opaque, 0, AVSEEK_SIZE);\n if(size<0){\n if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)\n return size;\n size++;\n s->seek(s->opaque, s->pos, SEEK_SET);\n }\n return size;\n}', 'unsigned int avio_rl32(AVIOContext *s)\n{\n unsigned int val;\n val = avio_rl16(s);\n val |= avio_rl16(s) << 16;\n return val;\n}', 'unsigned int avio_rl16(AVIOContext *s)\n{\n unsigned int val;\n val = avio_r8(s);\n val |= avio_r8(s) << 8;\n return val;\n}', 'int avio_r8(AVIOContext *s)\n{\n if (s->buf_ptr >= s->buf_end)\n fill_buffer(s);\n if (s->buf_ptr < s->buf_end)\n return *s->buf_ptr++;\n return 0;\n}']
|
1,267 | 0 |
https://github.com/openssl/openssl/blob/2d5d70b15559f9813054ddb11b30b816daf62ebe/crypto/bn/bn_ctx.c/#L332
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)\n{\n BIGNUM *i, *j, *k, *l, *m;\n BN_CTX *ctx;\n int r;\n int ret = 1;\n if (!key->p || !key->q || !key->n || !key->e || !key->d) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);\n return 0;\n }\n i = BN_new();\n j = BN_new();\n k = BN_new();\n l = BN_new();\n m = BN_new();\n ctx = BN_CTX_new();\n if (i == NULL || j == NULL || k == NULL || l == NULL ||\n m == NULL || ctx == NULL) {\n ret = -1;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n r = BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb);\n if (r != 1) {\n ret = r;\n if (r != 0)\n goto err;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);\n }\n r = BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb);\n if (r != 1) {\n ret = r;\n if (r != 0)\n goto err;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);\n }\n r = BN_mul(i, key->p, key->q, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, key->n) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);\n }\n r = BN_sub(i, key->p, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_sub(j, key->q, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mul(l, i, j, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_gcd(m, i, j, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_div(k, NULL, l, m, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mod_mul(i, key->d, key->e, k, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (!BN_is_one(i)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);\n }\n if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {\n r = BN_sub(i, key->p, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mod(j, key->d, i, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmp1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);\n }\n r = BN_sub(i, key->q, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mod(j, key->d, i, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmq1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, key->q, key->p, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, key->iqmp) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);\n }\n }\n err:\n BN_free(i);\n BN_free(j);\n BN_free(k);\n BN_free(l);\n BN_free(m);\n BN_CTX_free(ctx);\n return (ret);\n}', 'int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n BN_GENCB *cb)\n{\n return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);\n}', 'int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, j, ret = -1;\n int k;\n BN_CTX *ctx = NULL;\n BIGNUM *A1, *A1_odd, *check;\n BN_MONT_CTX *mont = NULL;\n const BIGNUM *A = NULL;\n if (BN_cmp(a, BN_value_one()) <= 0)\n return 0;\n if (checks == BN_prime_checks)\n checks = BN_prime_checks_for_size(BN_num_bits(a));\n if (!BN_is_odd(a))\n return BN_is_word(a, 2);\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++)\n if (BN_mod_word(a, primes[i]) == 0)\n return 0;\n if (!BN_GENCB_call(cb, 1, -1))\n goto err;\n }\n if (ctx_passed != NULL)\n ctx = ctx_passed;\n else if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n if (a->neg) {\n BIGNUM *t;\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n BN_copy(t, a);\n t->neg = 0;\n A = t;\n } else\n A = a;\n A1 = BN_CTX_get(ctx);\n A1_odd = BN_CTX_get(ctx);\n check = BN_CTX_get(ctx);\n if (check == NULL)\n goto err;\n if (!BN_copy(A1, A))\n goto err;\n if (!BN_sub_word(A1, 1))\n goto err;\n if (BN_is_zero(A1)) {\n ret = 0;\n goto err;\n }\n k = 1;\n while (!BN_is_bit_set(A1, k))\n k++;\n if (!BN_rshift(A1_odd, A1, k))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, A, ctx))\n goto err;\n for (i = 0; i < checks; i++) {\n if (!BN_pseudo_rand_range(check, A1))\n goto err;\n if (!BN_add_word(check, 1))\n goto err;\n j = witness(check, A, A1, A1_odd, k, ctx, mont);\n if (j == -1)\n goto err;\n if (j) {\n ret = 0;\n goto err;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n if (ctx_passed == NULL)\n BN_CTX_free(ctx);\n }\n BN_MONT_CTX_free(mont);\n return (ret);\n}', 'int BN_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 rr->neg = a->neg ^ b->neg;\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 bn_correct_top(rr);\n if (r != rr)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)\n{\n BIGNUM *a, *b, *t;\n int ret = 0;\n bn_check_top(in_a);\n bn_check_top(in_b);\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (a == NULL || b == NULL)\n goto err;\n if (BN_copy(a, in_a) == NULL)\n goto err;\n if (BN_copy(b, in_b) == NULL)\n goto err;\n a->neg = 0;\n b->neg = 0;\n if (BN_cmp(a, b) < 0) {\n t = a;\n a = b;\n b = t;\n }\n t = euclid(a, b);\n if (t == NULL)\n goto err;\n if (BN_copy(r, t) == NULL)\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return (ret);\n}', '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}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', '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}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
1,268 | 1 |
https://github.com/openssl/openssl/blob/04761b557a53f026630dd5916b2b8522d94579db/crypto/lhash/lhash.c/#L221
|
static int expand(OPENSSL_LHASH *lh)
{
OPENSSL_LH_NODE **n, **n1, **n2, *np;
unsigned int p, i, j;
unsigned long hash, nni;
lh->num_nodes++;
lh->num_expands++;
p = (int)lh->p++;
n1 = &(lh->b[p]);
n2 = &(lh->b[p + (int)lh->pmax]);
*n2 = NULL;
nni = lh->num_alloc_nodes;
for (np = *n1; np != NULL;) {
hash = np->hash;
if ((hash % nni) != p) {
*n1 = (*n1)->next;
np->next = *n2;
*n2 = np;
} else
n1 = &((*n1)->next);
np = *n1;
}
if ((lh->p) >= lh->pmax) {
j = (int)lh->num_alloc_nodes * 2;
n = OPENSSL_realloc(lh->b, (int)(sizeof(OPENSSL_LH_NODE *) * j));
if (n == NULL) {
lh->error++;
lh->num_nodes--;
lh->p = 0;
return 0;
}
for (i = (int)lh->num_alloc_nodes; i < j; i++)
n[i] = NULL;
lh->pmax = lh->num_alloc_nodes;
lh->num_alloc_nodes = j;
lh->num_expand_reallocs++;
lh->p = 0;
lh->b = n;
}
return 1;
}
|
['static int test_stress(void)\n{\n LHASH_OF(int) *h = lh_int_new(&stress_hash, &int_cmp);\n const unsigned int n = 2500000;\n unsigned int i;\n int testresult = 0, *p;\n if (!TEST_ptr(h))\n goto end;\n for (i = 0; i < n; i++) {\n p = OPENSSL_malloc(sizeof(i));\n if (!TEST_ptr(p)) {\n TEST_info("lhash stress out of memory %d", i);\n goto end;\n }\n *p = 3 * i + 1;\n lh_int_insert(h, p);\n }\n if (!TEST_int_eq(lh_int_num_items(h), n))\n goto end;\n TEST_info("hash full statistics:");\n OPENSSL_LH_stats_bio((OPENSSL_LHASH *)h, bio_err);\n TEST_note("hash full node usage:");\n OPENSSL_LH_node_usage_stats_bio((OPENSSL_LHASH *)h, bio_err);\n for (i = 0; i < n; i++) {\n const int j = (7 * i + 4) % n * 3 + 1;\n if (!TEST_ptr(p = lh_int_delete(h, &j))) {\n TEST_info("lhash stress delete %d\\n", i);\n goto end;\n }\n if (!TEST_int_eq(*p, j)) {\n TEST_info("lhash stress bad value %d", i);\n goto end;\n }\n OPENSSL_free(p);\n }\n TEST_info("hash empty statistics:");\n OPENSSL_LH_stats_bio((OPENSSL_LHASH *)h, bio_err);\n TEST_note("hash empty node usage:");\n OPENSSL_LH_node_usage_stats_bio((OPENSSL_LHASH *)h, bio_err);\n testresult = 1;\nend:\n lh_int_free(h);\n return testresult;\n}', 'DEFINE_LHASH_OF(int)', 'OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)\n{\n OPENSSL_LHASH *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)\n return NULL;\n if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)\n goto err;\n ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);\n ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);\n ret->num_nodes = MIN_NODES / 2;\n ret->num_alloc_nodes = MIN_NODES;\n ret->pmax = MIN_NODES / 2;\n ret->up_load = UP_LOAD;\n ret->down_load = DOWN_LOAD;\n return ret;\nerr:\n OPENSSL_free(ret->b);\n OPENSSL_free(ret);\n return NULL;\n}', 'void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))\n return NULL;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {\n lh->error++;\n return NULL;\n }\n nn->data = data;\n nn->next = NULL;\n nn->hash = hash;\n *rn = nn;\n ret = NULL;\n lh->num_insert++;\n lh->num_items++;\n } else {\n ret = (*rn)->data;\n (*rn)->data = data;\n lh->num_replace++;\n }\n return ret;\n}', 'static int expand(OPENSSL_LHASH *lh)\n{\n OPENSSL_LH_NODE **n, **n1, **n2, *np;\n unsigned int p, i, j;\n unsigned long hash, nni;\n lh->num_nodes++;\n lh->num_expands++;\n p = (int)lh->p++;\n n1 = &(lh->b[p]);\n n2 = &(lh->b[p + (int)lh->pmax]);\n *n2 = NULL;\n nni = lh->num_alloc_nodes;\n for (np = *n1; np != NULL;) {\n hash = np->hash;\n if ((hash % nni) != p) {\n *n1 = (*n1)->next;\n np->next = *n2;\n *n2 = np;\n } else\n n1 = &((*n1)->next);\n np = *n1;\n }\n if ((lh->p) >= lh->pmax) {\n j = (int)lh->num_alloc_nodes * 2;\n n = OPENSSL_realloc(lh->b, (int)(sizeof(OPENSSL_LH_NODE *) * j));\n if (n == NULL) {\n lh->error++;\n lh->num_nodes--;\n lh->p = 0;\n return 0;\n }\n for (i = (int)lh->num_alloc_nodes; i < j; i++)\n n[i] = NULL;\n lh->pmax = lh->num_alloc_nodes;\n lh->num_alloc_nodes = j;\n lh->num_expand_reallocs++;\n lh->p = 0;\n lh->b = n;\n }\n return 1;\n}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n INCREMENT(realloc_count);\n if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)\n return realloc_impl(str, num, file, line);\n FAILTEST();\n if (str == NULL)\n return CRYPTO_malloc(num, file, line);\n if (num == 0) {\n CRYPTO_free(str, file, line);\n return NULL;\n }\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n void *ret;\n CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n ret = realloc(str, num);\n CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n return ret;\n }\n#else\n (void)(file); (void)(line);\n#endif\n return realloc(str, num);\n}']
|
1,269 | 0 |
https://github.com/openssl/openssl/blob/2d5d70b15559f9813054ddb11b30b816daf62ebe/ssl/record/rec_layer_d1.c/#L693
|
int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
int len, int peek)
{
int al, i, j, ret;
unsigned int n;
SSL3_RECORD *rr;
void (*cb) (const SSL *ssl, int type2, int val) = NULL;
if (!SSL3_BUFFER_is_initialised(&s->rlayer.rbuf)) {
if (!ssl3_setup_buffers(s))
return (-1);
}
if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
(type != SSL3_RT_HANDSHAKE)) ||
(peek && (type != SSL3_RT_APPLICATION_DATA))) {
SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
return -1;
}
if ((ret = have_handshake_fragment(s, type, buf, len, peek)))
return ret;
#ifndef OPENSSL_NO_SCTP
if ((!s->in_handshake && SSL_in_init(s)) ||
(BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
(s->state == DTLS1_SCTP_ST_SR_READ_SOCK
|| s->state == DTLS1_SCTP_ST_CR_READ_SOCK)
&& s->s3->in_read_app_data != 2))
#else
if (!s->in_handshake && SSL_in_init(s))
#endif
{
i = s->handshake_func(s);
if (i < 0)
return (i);
if (i == 0) {
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
return (-1);
}
}
start:
s->rwstate = SSL_NOTHING;
rr = &s->rlayer.rrec;
if (s->state == SSL_ST_OK && SSL3_RECORD_get_length(rr) == 0) {
pitem *item;
item = pqueue_pop(s->rlayer.d->buffered_app_data.q);
if (item) {
#ifndef OPENSSL_NO_SCTP
if (BIO_dgram_is_sctp(SSL_get_rbio(s))) {
DTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *)item->data;
BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO,
sizeof(rdata->recordinfo), &rdata->recordinfo);
}
#endif
dtls1_copy_record(s, item);
OPENSSL_free(item->data);
pitem_free(item);
}
}
if (dtls1_handle_timeout(s) > 0)
goto start;
if ((SSL3_RECORD_get_length(rr) == 0)
|| (s->rlayer.rstate == SSL_ST_READ_BODY)) {
ret = dtls1_get_record(s);
if (ret <= 0) {
ret = dtls1_read_failed(s, ret);
if (ret <= 0)
return (ret);
else
goto start;
}
}
if (s->d1->listen && rr->type != SSL3_RT_HANDSHAKE) {
SSL3_RECORD_set_length(rr, 0);
goto start;
}
if (s->s3->change_cipher_spec
&& (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
if (dtls1_buffer_record(s, &(s->rlayer.d->buffered_app_data),
SSL3_RECORD_get_seq_num(rr)) < 0) {
SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
return -1;
}
SSL3_RECORD_set_length(rr, 0);
goto start;
}
if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
SSL3_RECORD_set_length(rr, 0);
s->rwstate = SSL_NOTHING;
return (0);
}
if (type == SSL3_RECORD_get_type(rr)
|| (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
&& type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {
if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
(s->enc_read_ctx == NULL)) {
al = SSL_AD_UNEXPECTED_MESSAGE;
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);
goto f_err;
}
if (recvd_type != NULL)
*recvd_type = SSL3_RECORD_get_type(rr);
if (len <= 0)
return (len);
if ((unsigned int)len > SSL3_RECORD_get_length(rr))
n = SSL3_RECORD_get_length(rr);
else
n = (unsigned int)len;
memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);
if (!peek) {
SSL3_RECORD_add_length(rr, -n);
SSL3_RECORD_add_off(rr, n);
if (SSL3_RECORD_get_length(rr) == 0) {
s->rlayer.rstate = SSL_ST_READ_HEADER;
SSL3_RECORD_set_off(rr, 0);
}
}
#ifndef OPENSSL_NO_SCTP
if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
SSL3_RECORD_get_type(rr) == SSL3_RT_APPLICATION_DATA &&
(s->state == DTLS1_SCTP_ST_SR_READ_SOCK
|| s->state == DTLS1_SCTP_ST_CR_READ_SOCK)) {
s->rwstate = SSL_READING;
BIO_clear_retry_flags(SSL_get_rbio(s));
BIO_set_retry_read(SSL_get_rbio(s));
}
if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
s->d1->shutdown_received
&& !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
s->shutdown |= SSL_RECEIVED_SHUTDOWN;
return (0);
}
#endif
return (n);
}
{
unsigned int k, dest_maxlen = 0;
unsigned char *dest = NULL;
unsigned int *dest_len = NULL;
if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) {
dest_maxlen = sizeof s->rlayer.d->handshake_fragment;
dest = s->rlayer.d->handshake_fragment;
dest_len = &s->rlayer.d->handshake_fragment_len;
} else if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {
dest_maxlen = sizeof(s->rlayer.d->alert_fragment);
dest = s->rlayer.d->alert_fragment;
dest_len = &s->rlayer.d->alert_fragment_len;
}
#ifndef OPENSSL_NO_HEARTBEATS
else if (SSL3_RECORD_get_type(rr) == TLS1_RT_HEARTBEAT) {
if (dtls1_process_heartbeat(s, SSL3_RECORD_get_data(rr),
SSL3_RECORD_get_length(rr)) < 0) {
return -1;
}
SSL3_RECORD_set_length(rr, 0);
s->rwstate = SSL_READING;
BIO_clear_retry_flags(SSL_get_rbio(s));
BIO_set_retry_read(SSL_get_rbio(s));
return (-1);
}
#endif
else if (SSL3_RECORD_get_type(rr) != SSL3_RT_CHANGE_CIPHER_SPEC) {
if (SSL3_RECORD_get_type(rr) == SSL3_RT_APPLICATION_DATA) {
BIO *bio;
s->s3->in_read_app_data = 2;
bio = SSL_get_rbio(s);
s->rwstate = SSL_READING;
BIO_clear_retry_flags(bio);
BIO_set_retry_read(bio);
return (-1);
}
al = SSL_AD_UNEXPECTED_MESSAGE;
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
goto f_err;
}
if (dest_maxlen > 0) {
if (SSL3_RECORD_get_length(rr) < dest_maxlen) {
#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
FIX ME
#endif
s->rlayer.rstate = SSL_ST_READ_HEADER;
SSL3_RECORD_set_length(rr, 0);
goto start;
}
for (k = 0; k < dest_maxlen; k++) {
dest[k] = SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)];
SSL3_RECORD_add_off(rr, 1);
SSL3_RECORD_add_length(rr, -1);
}
*dest_len = dest_maxlen;
}
}
if ((!s->server) &&
(s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
(s->rlayer.d->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
(s->session != NULL) && (s->session->cipher != NULL)) {
s->rlayer.d->handshake_fragment_len = 0;
if ((s->rlayer.d->handshake_fragment[1] != 0) ||
(s->rlayer.d->handshake_fragment[2] != 0) ||
(s->rlayer.d->handshake_fragment[3] != 0)) {
al = SSL_AD_DECODE_ERROR;
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);
goto f_err;
}
if (s->msg_callback)
s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
s->rlayer.d->handshake_fragment, 4, s,
s->msg_callback_arg);
if (SSL_is_init_finished(s) &&
!(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
!s->s3->renegotiate) {
s->d1->handshake_read_seq++;
s->new_session = 1;
ssl3_renegotiate(s);
if (ssl3_renegotiate_check(s)) {
i = s->handshake_func(s);
if (i < 0)
return (i);
if (i == 0) {
SSLerr(SSL_F_DTLS1_READ_BYTES,
SSL_R_SSL_HANDSHAKE_FAILURE);
return (-1);
}
if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
BIO *bio;
s->rwstate = SSL_READING;
bio = SSL_get_rbio(s);
BIO_clear_retry_flags(bio);
BIO_set_retry_read(bio);
return (-1);
}
}
}
}
goto start;
}
if (s->rlayer.d->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH) {
int alert_level = s->rlayer.d->alert_fragment[0];
int alert_descr = s->rlayer.d->alert_fragment[1];
s->rlayer.d->alert_fragment_len = 0;
if (s->msg_callback)
s->msg_callback(0, s->version, SSL3_RT_ALERT,
s->rlayer.d->alert_fragment, 2, s,
s->msg_callback_arg);
if (s->info_callback != NULL)
cb = s->info_callback;
else if (s->ctx->info_callback != NULL)
cb = s->ctx->info_callback;
if (cb != NULL) {
j = (alert_level << 8) | alert_descr;
cb(s, SSL_CB_READ_ALERT, j);
}
if (alert_level == SSL3_AL_WARNING) {
s->s3->warn_alert = alert_descr;
if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
#ifndef OPENSSL_NO_SCTP
if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
s->d1->shutdown_received = 1;
s->rwstate = SSL_READING;
BIO_clear_retry_flags(SSL_get_rbio(s));
BIO_set_retry_read(SSL_get_rbio(s));
return -1;
}
#endif
s->shutdown |= SSL_RECEIVED_SHUTDOWN;
return (0);
}
#if 0
if (alert_descr == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) {
unsigned short seq;
unsigned int frag_off;
unsigned char *p = &(s->rlayer.d->alert_fragment[2]);
n2s(p, seq);
n2l3(p, frag_off);
dtls1_retransmit_message(s,
dtls1_get_queue_priority
(frag->msg_header.seq, 0), frag_off,
&found);
if (!found && SSL_in_init(s)) {
ssl3_send_alert(s, SSL3_AL_WARNING,
DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
}
}
#endif
} else if (alert_level == SSL3_AL_FATAL) {
char tmp[16];
s->rwstate = SSL_NOTHING;
s->s3->fatal_alert = alert_descr;
SSLerr(SSL_F_DTLS1_READ_BYTES,
SSL_AD_REASON_OFFSET + alert_descr);
BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
ERR_add_error_data(2, "SSL alert number ", tmp);
s->shutdown |= SSL_RECEIVED_SHUTDOWN;
SSL_CTX_remove_session(s->ctx, s->session);
return (0);
} else {
al = SSL_AD_ILLEGAL_PARAMETER;
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
goto f_err;
}
goto start;
}
if (s->shutdown & SSL_SENT_SHUTDOWN) {
s->rwstate = SSL_NOTHING;
SSL3_RECORD_set_length(rr, 0);
return (0);
}
if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
SSL3_RECORD_set_length(rr, 0);
goto start;
}
if ((s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
!s->in_handshake) {
struct hm_header_st msg_hdr;
dtls1_get_message_header(rr->data, &msg_hdr);
if (SSL3_RECORD_get_epoch(rr) != s->rlayer.d->r_epoch) {
SSL3_RECORD_set_length(rr, 0);
goto start;
}
if (msg_hdr.type == SSL3_MT_FINISHED) {
if (dtls1_check_timeout_num(s) < 0)
return -1;
dtls1_retransmit_buffered_messages(s);
SSL3_RECORD_set_length(rr, 0);
goto start;
}
if (((s->state & SSL_ST_MASK) == SSL_ST_OK) &&
!(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {
s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
s->renegotiate = 1;
s->new_session = 1;
}
i = s->handshake_func(s);
if (i < 0)
return (i);
if (i == 0) {
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
return (-1);
}
if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
BIO *bio;
s->rwstate = SSL_READING;
bio = SSL_get_rbio(s);
BIO_clear_retry_flags(bio);
BIO_set_retry_read(bio);
return (-1);
}
}
goto start;
}
switch (SSL3_RECORD_get_type(rr)) {
default:
if (s->version == TLS1_VERSION) {
SSL3_RECORD_set_length(rr, 0);
goto start;
}
al = SSL_AD_UNEXPECTED_MESSAGE;
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
goto f_err;
case SSL3_RT_CHANGE_CIPHER_SPEC:
case SSL3_RT_ALERT:
case SSL3_RT_HANDSHAKE:
al = SSL_AD_UNEXPECTED_MESSAGE;
SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
goto f_err;
case SSL3_RT_APPLICATION_DATA:
if (s->s3->in_read_app_data &&
(s->s3->total_renegotiations != 0) &&
(((s->state & SSL_ST_CONNECT) &&
(s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&
(s->state <= SSL3_ST_CR_SRVR_HELLO_A)
) || ((s->state & SSL_ST_ACCEPT) &&
(s->state <= SSL3_ST_SW_HELLO_REQ_A) &&
(s->state >= SSL3_ST_SR_CLNT_HELLO_A)
)
)) {
s->s3->in_read_app_data = 2;
return (-1);
} else {
al = SSL_AD_UNEXPECTED_MESSAGE;
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
goto f_err;
}
}
f_err:
ssl3_send_alert(s, SSL3_AL_FATAL, al);
return (-1);
}
|
['int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,\n int len, int peek)\n{\n int al, i, j, ret;\n unsigned int n;\n SSL3_RECORD *rr;\n void (*cb) (const SSL *ssl, int type2, int val) = NULL;\n if (!SSL3_BUFFER_is_initialised(&s->rlayer.rbuf)) {\n if (!ssl3_setup_buffers(s))\n return (-1);\n }\n if ((type && (type != SSL3_RT_APPLICATION_DATA) &&\n (type != SSL3_RT_HANDSHAKE)) ||\n (peek && (type != SSL3_RT_APPLICATION_DATA))) {\n SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);\n return -1;\n }\n if ((ret = have_handshake_fragment(s, type, buf, len, peek)))\n return ret;\n#ifndef OPENSSL_NO_SCTP\n if ((!s->in_handshake && SSL_in_init(s)) ||\n (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n (s->state == DTLS1_SCTP_ST_SR_READ_SOCK\n || s->state == DTLS1_SCTP_ST_CR_READ_SOCK)\n && s->s3->in_read_app_data != 2))\n#else\n if (!s->in_handshake && SSL_in_init(s))\n#endif\n {\n i = s->handshake_func(s);\n if (i < 0)\n return (i);\n if (i == 0) {\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);\n return (-1);\n }\n }\n start:\n s->rwstate = SSL_NOTHING;\n rr = &s->rlayer.rrec;\n if (s->state == SSL_ST_OK && SSL3_RECORD_get_length(rr) == 0) {\n pitem *item;\n item = pqueue_pop(s->rlayer.d->buffered_app_data.q);\n if (item) {\n#ifndef OPENSSL_NO_SCTP\n if (BIO_dgram_is_sctp(SSL_get_rbio(s))) {\n DTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *)item->data;\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO,\n sizeof(rdata->recordinfo), &rdata->recordinfo);\n }\n#endif\n dtls1_copy_record(s, item);\n OPENSSL_free(item->data);\n pitem_free(item);\n }\n }\n if (dtls1_handle_timeout(s) > 0)\n goto start;\n if ((SSL3_RECORD_get_length(rr) == 0)\n || (s->rlayer.rstate == SSL_ST_READ_BODY)) {\n ret = dtls1_get_record(s);\n if (ret <= 0) {\n ret = dtls1_read_failed(s, ret);\n if (ret <= 0)\n return (ret);\n else\n goto start;\n }\n }\n if (s->d1->listen && rr->type != SSL3_RT_HANDSHAKE) {\n SSL3_RECORD_set_length(rr, 0);\n goto start;\n }\n if (s->s3->change_cipher_spec\n && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {\n if (dtls1_buffer_record(s, &(s->rlayer.d->buffered_app_data),\n SSL3_RECORD_get_seq_num(rr)) < 0) {\n SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);\n return -1;\n }\n SSL3_RECORD_set_length(rr, 0);\n goto start;\n }\n if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {\n SSL3_RECORD_set_length(rr, 0);\n s->rwstate = SSL_NOTHING;\n return (0);\n }\n if (type == SSL3_RECORD_get_type(rr)\n || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC\n && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {\n if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&\n (s->enc_read_ctx == NULL)) {\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);\n goto f_err;\n }\n if (recvd_type != NULL)\n *recvd_type = SSL3_RECORD_get_type(rr);\n if (len <= 0)\n return (len);\n if ((unsigned int)len > SSL3_RECORD_get_length(rr))\n n = SSL3_RECORD_get_length(rr);\n else\n n = (unsigned int)len;\n memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);\n if (!peek) {\n SSL3_RECORD_add_length(rr, -n);\n SSL3_RECORD_add_off(rr, n);\n if (SSL3_RECORD_get_length(rr) == 0) {\n s->rlayer.rstate = SSL_ST_READ_HEADER;\n SSL3_RECORD_set_off(rr, 0);\n }\n }\n#ifndef OPENSSL_NO_SCTP\n if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n SSL3_RECORD_get_type(rr) == SSL3_RT_APPLICATION_DATA &&\n (s->state == DTLS1_SCTP_ST_SR_READ_SOCK\n || s->state == DTLS1_SCTP_ST_CR_READ_SOCK)) {\n s->rwstate = SSL_READING;\n BIO_clear_retry_flags(SSL_get_rbio(s));\n BIO_set_retry_read(SSL_get_rbio(s));\n }\n if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n s->d1->shutdown_received\n && !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {\n s->shutdown |= SSL_RECEIVED_SHUTDOWN;\n return (0);\n }\n#endif\n return (n);\n }\n {\n unsigned int k, dest_maxlen = 0;\n unsigned char *dest = NULL;\n unsigned int *dest_len = NULL;\n if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) {\n dest_maxlen = sizeof s->rlayer.d->handshake_fragment;\n dest = s->rlayer.d->handshake_fragment;\n dest_len = &s->rlayer.d->handshake_fragment_len;\n } else if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {\n dest_maxlen = sizeof(s->rlayer.d->alert_fragment);\n dest = s->rlayer.d->alert_fragment;\n dest_len = &s->rlayer.d->alert_fragment_len;\n }\n#ifndef OPENSSL_NO_HEARTBEATS\n else if (SSL3_RECORD_get_type(rr) == TLS1_RT_HEARTBEAT) {\n if (dtls1_process_heartbeat(s, SSL3_RECORD_get_data(rr),\n SSL3_RECORD_get_length(rr)) < 0) {\n return -1;\n }\n SSL3_RECORD_set_length(rr, 0);\n s->rwstate = SSL_READING;\n BIO_clear_retry_flags(SSL_get_rbio(s));\n BIO_set_retry_read(SSL_get_rbio(s));\n return (-1);\n }\n#endif\n else if (SSL3_RECORD_get_type(rr) != SSL3_RT_CHANGE_CIPHER_SPEC) {\n if (SSL3_RECORD_get_type(rr) == SSL3_RT_APPLICATION_DATA) {\n BIO *bio;\n s->s3->in_read_app_data = 2;\n bio = SSL_get_rbio(s);\n s->rwstate = SSL_READING;\n BIO_clear_retry_flags(bio);\n BIO_set_retry_read(bio);\n return (-1);\n }\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);\n goto f_err;\n }\n if (dest_maxlen > 0) {\n if (SSL3_RECORD_get_length(rr) < dest_maxlen) {\n#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE\n FIX ME\n#endif\n s->rlayer.rstate = SSL_ST_READ_HEADER;\n SSL3_RECORD_set_length(rr, 0);\n goto start;\n }\n for (k = 0; k < dest_maxlen; k++) {\n dest[k] = SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)];\n SSL3_RECORD_add_off(rr, 1);\n SSL3_RECORD_add_length(rr, -1);\n }\n *dest_len = dest_maxlen;\n }\n }\n if ((!s->server) &&\n (s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&\n (s->rlayer.d->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&\n (s->session != NULL) && (s->session->cipher != NULL)) {\n s->rlayer.d->handshake_fragment_len = 0;\n if ((s->rlayer.d->handshake_fragment[1] != 0) ||\n (s->rlayer.d->handshake_fragment[2] != 0) ||\n (s->rlayer.d->handshake_fragment[3] != 0)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);\n goto f_err;\n }\n if (s->msg_callback)\n s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,\n s->rlayer.d->handshake_fragment, 4, s,\n s->msg_callback_arg);\n if (SSL_is_init_finished(s) &&\n !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&\n !s->s3->renegotiate) {\n s->d1->handshake_read_seq++;\n s->new_session = 1;\n ssl3_renegotiate(s);\n if (ssl3_renegotiate_check(s)) {\n i = s->handshake_func(s);\n if (i < 0)\n return (i);\n if (i == 0) {\n SSLerr(SSL_F_DTLS1_READ_BYTES,\n SSL_R_SSL_HANDSHAKE_FAILURE);\n return (-1);\n }\n if (!(s->mode & SSL_MODE_AUTO_RETRY)) {\n if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {\n BIO *bio;\n s->rwstate = SSL_READING;\n bio = SSL_get_rbio(s);\n BIO_clear_retry_flags(bio);\n BIO_set_retry_read(bio);\n return (-1);\n }\n }\n }\n }\n goto start;\n }\n if (s->rlayer.d->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH) {\n int alert_level = s->rlayer.d->alert_fragment[0];\n int alert_descr = s->rlayer.d->alert_fragment[1];\n s->rlayer.d->alert_fragment_len = 0;\n if (s->msg_callback)\n s->msg_callback(0, s->version, SSL3_RT_ALERT,\n s->rlayer.d->alert_fragment, 2, s,\n s->msg_callback_arg);\n if (s->info_callback != NULL)\n cb = s->info_callback;\n else if (s->ctx->info_callback != NULL)\n cb = s->ctx->info_callback;\n if (cb != NULL) {\n j = (alert_level << 8) | alert_descr;\n cb(s, SSL_CB_READ_ALERT, j);\n }\n if (alert_level == SSL3_AL_WARNING) {\n s->s3->warn_alert = alert_descr;\n if (alert_descr == SSL_AD_CLOSE_NOTIFY) {\n#ifndef OPENSSL_NO_SCTP\n if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {\n s->d1->shutdown_received = 1;\n s->rwstate = SSL_READING;\n BIO_clear_retry_flags(SSL_get_rbio(s));\n BIO_set_retry_read(SSL_get_rbio(s));\n return -1;\n }\n#endif\n s->shutdown |= SSL_RECEIVED_SHUTDOWN;\n return (0);\n }\n#if 0\n if (alert_descr == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) {\n unsigned short seq;\n unsigned int frag_off;\n unsigned char *p = &(s->rlayer.d->alert_fragment[2]);\n n2s(p, seq);\n n2l3(p, frag_off);\n dtls1_retransmit_message(s,\n dtls1_get_queue_priority\n (frag->msg_header.seq, 0), frag_off,\n &found);\n if (!found && SSL_in_init(s)) {\n ssl3_send_alert(s, SSL3_AL_WARNING,\n DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);\n }\n }\n#endif\n } else if (alert_level == SSL3_AL_FATAL) {\n char tmp[16];\n s->rwstate = SSL_NOTHING;\n s->s3->fatal_alert = alert_descr;\n SSLerr(SSL_F_DTLS1_READ_BYTES,\n SSL_AD_REASON_OFFSET + alert_descr);\n BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);\n ERR_add_error_data(2, "SSL alert number ", tmp);\n s->shutdown |= SSL_RECEIVED_SHUTDOWN;\n SSL_CTX_remove_session(s->ctx, s->session);\n return (0);\n } else {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);\n goto f_err;\n }\n goto start;\n }\n if (s->shutdown & SSL_SENT_SHUTDOWN) {\n s->rwstate = SSL_NOTHING;\n SSL3_RECORD_set_length(rr, 0);\n return (0);\n }\n if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {\n SSL3_RECORD_set_length(rr, 0);\n goto start;\n }\n if ((s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&\n !s->in_handshake) {\n struct hm_header_st msg_hdr;\n dtls1_get_message_header(rr->data, &msg_hdr);\n if (SSL3_RECORD_get_epoch(rr) != s->rlayer.d->r_epoch) {\n SSL3_RECORD_set_length(rr, 0);\n goto start;\n }\n if (msg_hdr.type == SSL3_MT_FINISHED) {\n if (dtls1_check_timeout_num(s) < 0)\n return -1;\n dtls1_retransmit_buffered_messages(s);\n SSL3_RECORD_set_length(rr, 0);\n goto start;\n }\n if (((s->state & SSL_ST_MASK) == SSL_ST_OK) &&\n !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {\n s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;\n s->renegotiate = 1;\n s->new_session = 1;\n }\n i = s->handshake_func(s);\n if (i < 0)\n return (i);\n if (i == 0) {\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);\n return (-1);\n }\n if (!(s->mode & SSL_MODE_AUTO_RETRY)) {\n if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {\n BIO *bio;\n s->rwstate = SSL_READING;\n bio = SSL_get_rbio(s);\n BIO_clear_retry_flags(bio);\n BIO_set_retry_read(bio);\n return (-1);\n }\n }\n goto start;\n }\n switch (SSL3_RECORD_get_type(rr)) {\n default:\n if (s->version == TLS1_VERSION) {\n SSL3_RECORD_set_length(rr, 0);\n goto start;\n }\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);\n goto f_err;\n case SSL3_RT_CHANGE_CIPHER_SPEC:\n case SSL3_RT_ALERT:\n case SSL3_RT_HANDSHAKE:\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);\n goto f_err;\n case SSL3_RT_APPLICATION_DATA:\n if (s->s3->in_read_app_data &&\n (s->s3->total_renegotiations != 0) &&\n (((s->state & SSL_ST_CONNECT) &&\n (s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&\n (s->state <= SSL3_ST_CR_SRVR_HELLO_A)\n ) || ((s->state & SSL_ST_ACCEPT) &&\n (s->state <= SSL3_ST_SW_HELLO_REQ_A) &&\n (s->state >= SSL3_ST_SR_CLNT_HELLO_A)\n )\n )) {\n s->s3->in_read_app_data = 2;\n return (-1);\n } else {\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);\n goto f_err;\n }\n }\n f_err:\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\n return (-1);\n}']
|
1,270 | 0 |
https://github.com/openssl/openssl/blob/84c15db551ce1d167b901a3bde2b21880b084384/crypto/bn/bn_lib.c/#L447
|
BIGNUM *bn_expand2(BIGNUM *b, int words)
{
BN_ULONG *A,*a;
const BN_ULONG *B;
int i;
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);
}
#if 1
B=b->d;
if (B != NULL)
{
#if 0
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:
;
}
#else
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: ;
}
#endif
Free(b->d);
}
b->d=a;
b->max=words;
A= &(b->d[b->top]);
for (i=(b->max - b->top)>>3; i>0; i--,A+=8)
{
A[0]=0; A[1]=0; A[2]=0; A[3]=0;
A[4]=0; A[5]=0; A[6]=0; A[7]=0;
}
for (i=(b->max - b->top)&7; i>0; i--,A++)
A[0]=0;
#else
memset(A,0,sizeof(BN_ULONG)*(words+1));
memcpy(A,b->d,sizeof(b->d[0])*b->top);
b->d=a;
b->max=words;
#endif
}
return(b);
}
|
['int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)\n\t{\n\tBIGNUM *A,*Ai;\n\tBN_CTX *ctx;\n\tint ret=0;\n\tif (p_ctx == NULL)\n\t\t{\n\t\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\t\t}\n\telse\n\t\tctx=p_ctx;\n\tif (rsa->blinding != NULL)\n\t\tBN_BLINDING_free(rsa->blinding);\n\tA= &(ctx->bn[0]);\n\tctx->tos++;\n\tif (!BN_rand(A,BN_num_bits(rsa->n)-1,1,0)) goto err;\n\tif ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;\n\tif (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))\n\t goto err;\n\trsa->blinding=BN_BLINDING_new(A,Ai,rsa->n);\n\tctx->tos--;\n\trsa->flags|=RSA_FLAG_BLINDING;\n\tBN_free(Ai);\n\tret=1;\nerr:\n\tif (ctx != p_ctx) BN_CTX_free(ctx);\n\treturn(ret);\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}', 'BN_BLINDING *BN_BLINDING_new(BIGNUM *A, BIGNUM *Ai, BIGNUM *mod)\n\t{\n\tBN_BLINDING *ret=NULL;\n\tbn_check_top(Ai);\n\tbn_check_top(mod);\n\tif ((ret=(BN_BLINDING *)Malloc(sizeof(BN_BLINDING))) == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_BLINDING_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tmemset(ret,0,sizeof(BN_BLINDING));\n\tif ((ret->A=BN_new()) == NULL) goto err;\n\tif ((ret->Ai=BN_new()) == NULL) goto err;\n\tif (!BN_copy(ret->A,A)) goto err;\n\tif (!BN_copy(ret->Ai,Ai)) goto err;\n\tret->mod=mod;\n\treturn(ret);\nerr:\n\tif (ret != NULL) BN_BLINDING_free(ret);\n\treturn(NULL);\n\t}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n\t{\n\tint i;\n\tBN_ULONG *A;\n\tconst BN_ULONG *B;\n\tbn_check_top(b);\n\tif (a == b) return(a);\n\tif (bn_wexpand(a,b->top) == NULL) return(NULL);\n#if 1\n\tA=a->d;\n\tB=b->d;\n\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t{\n\t\tBN_ULONG a0,a1,a2,a3;\n\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t}\n\tswitch (b->top&3)\n\t\t{\n\t\tcase 3: A[2]=B[2];\n\t\tcase 2: A[1]=B[1];\n\t\tcase 1: A[0]=B[0];\n\t\tcase 0: ;\n\t\t}\n#else\n\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\ta->top=b->top;\n\tif ((a->top == 0) && (a->d != NULL))\n\t\ta->d[0]=0;\n\ta->neg=b->neg;\n\treturn(a);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*a;\n\tconst BN_ULONG *B;\n\tint i;\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}\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n#if 0\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#else\n\t\t\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t\t\t{\n\t\t\t\tBN_ULONG a0,a1,a2,a3;\n\t\t\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\t\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t\t\t}\n\t\t\tswitch (b->top&3)\n\t\t\t\t{\n\t\t\t\tcase 3:\tA[2]=B[2];\n\t\t\t\tcase 2:\tA[1]=B[1];\n\t\t\t\tcase 1:\tA[0]=B[0];\n\t\t\t\tcase 0:\t;\n\t\t\t\t}\n#endif\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tA= &(b->d[b->top]);\n\t\tfor (i=(b->max - b->top)>>3; i>0; i--,A+=8)\n\t\t\t{\n\t\t\tA[0]=0; A[1]=0; A[2]=0; A[3]=0;\n\t\t\tA[4]=0; A[5]=0; A[6]=0; A[7]=0;\n\t\t\t}\n\t\tfor (i=(b->max - b->top)&7; i>0; i--,A++)\n\t\t\tA[0]=0;\n#else\n\t\t\tmemset(A,0,sizeof(BN_ULONG)*(words+1));\n\t\t\tmemcpy(A,b->d,sizeof(b->d[0])*b->top);\n\t\t\tb->d=a;\n\t\t\tb->max=words;\n#endif\n\t\t}\n\treturn(b);\n\t}']
|
1,271 | 0 |
https://github.com/openssl/openssl/blob/1c3d2b94be3ed7e55c7c7c8ce8c91b1521f59489/crypto/x509/x509_vfy.c/#L436
|
static int check_chain_extensions(X509_STORE_CTX *ctx)
{
#ifdef OPENSSL_NO_CHAIN_VERIFY
return 1;
#else
int i, ok=0, must_be_ca;
X509 *x;
int (*cb)(int xok,X509_STORE_CTX *xctx);
int proxy_path_length = 0;
int allow_proxy_certs =
!!(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
cb=ctx->verify_cb;
must_be_ca = -1;
if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
allow_proxy_certs = 1;
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, ctx->param->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_pathlen != -1)
&& (i > (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_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\treturn 1;\n#else\n\tint i, ok=0, must_be_ca;\n\tX509 *x;\n\tint (*cb)(int xok,X509_STORE_CTX *xctx);\n\tint proxy_path_length = 0;\n\tint allow_proxy_certs =\n\t\t!!(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);\n\tcb=ctx->verify_cb;\n\tmust_be_ca = -1;\n\tif (getenv("OPENSSL_ALLOW_PROXY_CERTS"))\n\t\tallow_proxy_certs = 1;\n\tfor (i = 0; i < ctx->last_untrusted; i++)\n\t\t{\n\t\tint ret;\n\t\tx = sk_X509_value(ctx->chain, i);\n\t\tif (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)\n\t\t\t&& (x->ex_flags & EXFLAG_CRITICAL))\n\t\t\t{\n\t\t\tctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;\n\t\t\tctx->error_depth = i;\n\t\t\tctx->current_cert = x;\n\t\t\tok=cb(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\tif (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY))\n\t\t\t{\n\t\t\tctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;\n\t\t\tctx->error_depth = i;\n\t\t\tctx->current_cert = x;\n\t\t\tok=cb(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\tret = X509_check_ca(x);\n\t\tswitch(must_be_ca)\n\t\t\t{\n\t\tcase -1:\n\t\t\tif ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n\t\t\t\t&& (ret != 1) && (ret != 0))\n\t\t\t\t{\n\t\t\t\tret = 0;\n\t\t\t\tctx->error = X509_V_ERR_INVALID_CA;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tret = 1;\n\t\t\tbreak;\n\t\tcase 0:\n\t\t\tif (ret != 0)\n\t\t\t\t{\n\t\t\t\tret = 0;\n\t\t\t\tctx->error = X509_V_ERR_INVALID_NON_CA;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tret = 1;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tif ((ret == 0)\n\t\t\t\t|| ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n\t\t\t\t\t&& (ret != 1)))\n\t\t\t\t{\n\t\t\t\tret = 0;\n\t\t\t\tctx->error = X509_V_ERR_INVALID_CA;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tret = 1;\n\t\t\tbreak;\n\t\t\t}\n\t\tif (ret == 0)\n\t\t\t{\n\t\t\tctx->error_depth = i;\n\t\t\tctx->current_cert = x;\n\t\t\tok=cb(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\tif (ctx->param->purpose > 0)\n\t\t\t{\n\t\t\tret = X509_check_purpose(x, ctx->param->purpose,\n\t\t\t\tmust_be_ca > 0);\n\t\t\tif ((ret == 0)\n\t\t\t\t|| ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n\t\t\t\t\t&& (ret != 1)))\n\t\t\t\t{\n\t\t\t\tctx->error = X509_V_ERR_INVALID_PURPOSE;\n\t\t\t\tctx->error_depth = i;\n\t\t\t\tctx->current_cert = x;\n\t\t\t\tok=cb(0,ctx);\n\t\t\t\tif (!ok) goto end;\n\t\t\t\t}\n\t\t\t}\n\t\tif ((i > 1) && (x->ex_pathlen != -1)\n\t\t\t && (i > (x->ex_pathlen + proxy_path_length + 1)))\n\t\t\t{\n\t\t\tctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;\n\t\t\tctx->error_depth = i;\n\t\t\tctx->current_cert = x;\n\t\t\tok=cb(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\tif (x->ex_flags & EXFLAG_PROXY)\n\t\t\t{\n\t\t\tif (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen)\n\t\t\t\t{\n\t\t\t\tctx->error =\n\t\t\t\t\tX509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;\n\t\t\t\tctx->error_depth = i;\n\t\t\t\tctx->current_cert = x;\n\t\t\t\tok=cb(0,ctx);\n\t\t\t\tif (!ok) goto end;\n\t\t\t\t}\n\t\t\tproxy_path_length++;\n\t\t\tmust_be_ca = 0;\n\t\t\t}\n\t\telse\n\t\t\tmust_be_ca = 1;\n\t\t}\n\tok = 1;\n end:\n\treturn ok;\n#endif\n}', 'char *sk_value(const STACK *st, int i)\n{\n\tif(!st || (i < 0) || (i >= st->num)) return NULL;\n\treturn st->data[i];\n}']
|
1,272 | 0 |
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_lib.c/#L232
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,\n const BIGNUM *Xp, const BIGNUM *Xp1,\n const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx,\n BN_GENCB *cb)\n{\n int ret = 0;\n BIGNUM *t, *p1p2, *pm1;\n if (!BN_is_odd(e))\n return 0;\n BN_CTX_start(ctx);\n if (p1 == NULL)\n p1 = BN_CTX_get(ctx);\n if (p2 == NULL)\n p2 = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n p1p2 = BN_CTX_get(ctx);\n pm1 = BN_CTX_get(ctx);\n if (pm1 == NULL)\n goto err;\n if (!bn_x931_derive_pi(p1, Xp1, ctx, cb))\n goto err;\n if (!bn_x931_derive_pi(p2, Xp2, ctx, cb))\n goto err;\n if (!BN_mul(p1p2, p1, p2, ctx))\n goto err;\n if (!BN_mod_inverse(p, p2, p1, ctx))\n goto err;\n if (!BN_mul(p, p, p2, ctx))\n goto err;\n if (!BN_mod_inverse(t, p1, p2, ctx))\n goto err;\n if (!BN_mul(t, t, p1, ctx))\n goto err;\n if (!BN_sub(p, p, t))\n goto err;\n if (p->neg && !BN_add(p, p, p1p2))\n goto err;\n if (!BN_mod_sub(p, p, Xp, p1p2, ctx))\n goto err;\n if (!BN_add(p, p, Xp))\n goto err;\n for (;;) {\n int i = 1;\n BN_GENCB_call(cb, 0, i++);\n if (!BN_copy(pm1, p))\n goto err;\n if (!BN_sub_word(pm1, 1))\n goto err;\n if (!BN_gcd(t, pm1, e, ctx))\n goto err;\n if (BN_is_one(t)) {\n int r = BN_is_prime_fasttest_ex(p, 50, ctx, 1, cb);\n if (r < 0)\n goto err;\n if (r)\n break;\n }\n if (!BN_add(p, p, p1p2))\n goto err;\n }\n BN_GENCB_call(cb, 3, 0);\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 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}']
|
1,273 | 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 rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n BIGNUM *r1, *m1, *vrfy, *r2, *m[RSA_MAX_PRIME_NUM - 2];\n int ret = 0, i, ex_primes = 0, smooth = 0;\n RSA_PRIME_INFO *pinfo;\n BN_CTX_start(ctx);\n r1 = BN_CTX_get(ctx);\n r2 = BN_CTX_get(ctx);\n m1 = BN_CTX_get(ctx);\n vrfy = BN_CTX_get(ctx);\n if (vrfy == NULL)\n goto err;\n if (rsa->version == RSA_ASN1_VERSION_MULTI\n && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0\n || ex_primes > RSA_MAX_PRIME_NUM - 2))\n goto err;\n if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {\n BIGNUM *factor = BN_new();\n if (factor == NULL)\n goto err;\n if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),\n BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,\n factor, ctx))\n || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),\n BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,\n factor, ctx))) {\n BN_free(factor);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);\n if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {\n BN_free(factor);\n goto err;\n }\n }\n BN_free(factor);\n smooth = (ex_primes == 0)\n && (rsa->meth->bn_mod_exp == BN_mod_exp_mont)\n && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));\n }\n if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,\n rsa->n, ctx))\n goto err;\n if (smooth) {\n if (\n !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)\n || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)\n || !BN_mod_exp_mont_consttime(m1, m1, rsa->dmq1, rsa->q, ctx,\n rsa->_method_mod_q)\n || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)\n || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n || !BN_mod_exp_mont_consttime(r1, r1, rsa->dmp1, rsa->p, ctx,\n rsa->_method_mod_p)\n || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)\n || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,\n ctx)\n || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)\n || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))\n goto err;\n goto tail;\n }\n {\n BIGNUM *c = BN_new();\n if (c == NULL)\n goto err;\n BN_with_flags(c, I, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, c, rsa->q, ctx)) {\n BN_free(c);\n goto err;\n }\n {\n BIGNUM *dmq1 = BN_new();\n if (dmq1 == NULL) {\n BN_free(c);\n goto err;\n }\n BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,\n rsa->_method_mod_q)) {\n BN_free(c);\n BN_free(dmq1);\n goto err;\n }\n BN_free(dmq1);\n }\n if (!BN_mod(r1, c, rsa->p, ctx)) {\n BN_free(c);\n goto err;\n }\n BN_free(c);\n }\n {\n BIGNUM *dmp1 = BN_new();\n if (dmp1 == NULL)\n goto err;\n BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,\n rsa->_method_mod_p)) {\n BN_free(dmp1);\n goto err;\n }\n BN_free(dmp1);\n }\n if (ex_primes > 0) {\n BIGNUM *di = BN_new(), *cc = BN_new();\n if (cc == NULL || di == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n if ((m[i] = BN_CTX_get(ctx)) == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(cc, I, BN_FLG_CONSTTIME);\n BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, cc, pinfo->r, ctx)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n }\n BN_free(cc);\n BN_free(di);\n }\n if (!BN_sub(r0, r0, m1))\n goto err;\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->iqmp, ctx))\n goto err;\n {\n BIGNUM *pr1 = BN_new();\n if (pr1 == NULL)\n goto err;\n BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);\n if (!BN_mod(r0, pr1, rsa->p, ctx)) {\n BN_free(pr1);\n goto err;\n }\n BN_free(pr1);\n }\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->q, ctx))\n goto err;\n if (!BN_add(r0, r1, m1))\n goto err;\n if (ex_primes > 0) {\n BIGNUM *pr2 = BN_new();\n if (pr2 == NULL)\n goto err;\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n if (!BN_sub(r1, m[i], r0)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r2, r1, pinfo->t, ctx)) {\n BN_free(pr2);\n goto err;\n }\n BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, pr2, pinfo->r, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (BN_is_negative(r1))\n if (!BN_add(r1, r1, pinfo->r)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r1, r1, pinfo->pp, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_add(r0, r0, r1)) {\n BN_free(pr2);\n goto err;\n }\n }\n BN_free(pr2);\n }\n tail:\n if (rsa->e && rsa->n) {\n if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {\n if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n } else {\n bn_correct_top(r0);\n if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n }\n if (!BN_sub(vrfy, vrfy, I))\n goto err;\n if (BN_is_zero(vrfy)) {\n bn_correct_top(r0);\n ret = 1;\n goto err;\n }\n if (!BN_mod(vrfy, vrfy, rsa->n, ctx))\n goto err;\n if (BN_is_negative(vrfy))\n if (!BN_add(vrfy, vrfy, rsa->n))\n goto err;\n if (!BN_is_zero(vrfy)) {\n BIGNUM *d = BN_new();\n if (d == NULL)\n goto err;\n BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,\n rsa->_method_mod_n)) {\n BN_free(d);\n goto err;\n }\n BN_free(d);\n }\n }\n bn_correct_top(r0);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("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_from_mont_fixed_top(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n int retn = 0;\n#ifdef MONT_WORD\n BIGNUM *t;\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) && BN_copy(t, a)) {\n retn = bn_from_montgomery_word(ret, t, mont);\n }\n BN_CTX_end(ctx);\n#else\n BIGNUM *t1, *t2;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n t2 = BN_CTX_get(ctx);\n if (t2 == NULL)\n goto err;\n if (!BN_copy(t1, a))\n goto err;\n BN_mask_bits(t1, mont->ri);\n if (!BN_mul(t2, t1, &mont->Ni, ctx))\n goto err;\n BN_mask_bits(t2, mont->ri);\n if (!BN_mul(t1, t2, &mont->N, ctx))\n goto err;\n if (!BN_add(t2, a, t1))\n goto err;\n if (!BN_rshift(ret, t2, mont->ri))\n goto err;\n if (BN_ucmp(ret, &(mont->N)) >= 0) {\n if (!BN_usub(ret, ret, &(mont->N)))\n goto err;\n }\n retn = 1;\n bn_check_top(ret);\n err:\n BN_CTX_end(ctx);\n#endif\n return retn;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n 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}', 'int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx);\n}', 'int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!bn_sqr_fixed_top(tmp, a, ctx))\n goto err;\n } else {\n if (!bn_mul_fixed_top(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!bn_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue, wmask, window0;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n top = m->top;\n bits = p->top * BN_BITS2;\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n BIGNUM *reduced = BN_CTX_get(ctx);\n if (reduced == NULL\n || !BN_nnmod(reduced, a, m, ctx)) {\n goto err;\n }\n a = reduced;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!bn_to_mont_fixed_top(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (!bn_to_mont_fixed_top(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits > 0) {\n if (bits < stride)\n stride = bits;\n bits -= stride;\n wvalue = bn_get_bits(p, bits);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7) {\n while (bits > 0) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n } else {\n while (bits > 0) {\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
1,274 | 0 |
https://github.com/libav/libav/blob/b4cfb8254eeeb2fc0aa2c0c36a5ede208af47a79/libavcodec/simple_idct_template.c/#L105
|
static inline void FUNC(idctRowCondDC)(DCTELEM *row)
{
int a0, a1, a2, a3, b0, b1, b2, b3;
#if HAVE_FAST_64BIT
#define ROW0_MASK (0xffffLL << 48 * HAVE_BIGENDIAN)
if (((((uint64_t *)row)[0] & ~ROW0_MASK) | ((uint64_t *)row)[1]) == 0) {
uint64_t temp = (row[0] << DC_SHIFT) & 0xffff;
temp += temp << 16;
temp += temp << 32;
((uint64_t *)row)[0] = temp;
((uint64_t *)row)[1] = temp;
return;
}
#else
if (!(((uint32_t*)row)[1] |
((uint32_t*)row)[2] |
((uint32_t*)row)[3] |
row[1])) {
uint32_t temp = (row[0] << DC_SHIFT) & 0xffff;
temp += temp << 16;
((uint32_t*)row)[0]=((uint32_t*)row)[1] =
((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;
return;
}
#endif
a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
a1 = a0;
a2 = a0;
a3 = a0;
a0 += W2 * row[2];
a1 += W6 * row[2];
a2 -= W6 * row[2];
a3 -= W2 * row[2];
b0 = MUL(W1, row[1]);
MAC(b0, W3, row[3]);
b1 = MUL(W3, row[1]);
MAC(b1, -W7, row[3]);
b2 = MUL(W5, row[1]);
MAC(b2, -W1, row[3]);
b3 = MUL(W7, row[1]);
MAC(b3, -W5, row[3]);
if (AV_RN64A(row + 4)) {
a0 += W4*row[4] + W6*row[6];
a1 += - W4*row[4] - W2*row[6];
a2 += - W4*row[4] + W2*row[6];
a3 += W4*row[4] - W6*row[6];
MAC(b0, W5, row[5]);
MAC(b0, W7, row[7]);
MAC(b1, -W1, row[5]);
MAC(b1, -W5, row[7]);
MAC(b2, W7, row[5]);
MAC(b2, W3, row[7]);
MAC(b3, W3, row[5]);
MAC(b3, -W1, row[7]);
}
row[0] = (a0 + b0) >> ROW_SHIFT;
row[7] = (a0 - b0) >> ROW_SHIFT;
row[1] = (a1 + b1) >> ROW_SHIFT;
row[6] = (a1 - b1) >> ROW_SHIFT;
row[2] = (a2 + b2) >> ROW_SHIFT;
row[5] = (a2 - b2) >> ROW_SHIFT;
row[3] = (a3 + b3) >> ROW_SHIFT;
row[4] = (a3 - b3) >> ROW_SHIFT;
}
|
['void ff_wmv2_add_mb(MpegEncContext *s, DCTELEM block1[6][64], uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr){\n Wmv2Context * const w= (Wmv2Context*)s;\n wmv2_add_block(w, block1[0], dest_y , s->linesize, 0);\n wmv2_add_block(w, block1[1], dest_y + 8 , s->linesize, 1);\n wmv2_add_block(w, block1[2], dest_y + 8*s->linesize, s->linesize, 2);\n wmv2_add_block(w, block1[3], dest_y + 8 + 8*s->linesize, s->linesize, 3);\n if(s->flags&CODEC_FLAG_GRAY) return;\n wmv2_add_block(w, block1[4], dest_cb , s->uvlinesize, 4);\n wmv2_add_block(w, block1[5], dest_cr , s->uvlinesize, 5);\n}', 'static void wmv2_add_block(Wmv2Context *w, DCTELEM *block1, uint8_t *dst, int stride, int n){\n MpegEncContext * const s= &w->s;\n if (s->block_last_index[n] >= 0) {\n switch(w->abt_type_table[n]){\n case 0:\n s->dsp.idct_add (dst, stride, block1);\n break;\n case 1:\n ff_simple_idct84_add(dst , stride, block1);\n ff_simple_idct84_add(dst + 4*stride, stride, w->abt_block2[n]);\n s->dsp.clear_block(w->abt_block2[n]);\n break;\n case 2:\n ff_simple_idct48_add(dst , stride, block1);\n ff_simple_idct48_add(dst + 4 , stride, w->abt_block2[n]);\n s->dsp.clear_block(w->abt_block2[n]);\n break;\n default:\n av_log(s->avctx, AV_LOG_ERROR, "internal error in WMV2 abt\\n");\n }\n }\n}', 'void ff_simple_idct84_add(uint8_t *dest, int line_size, DCTELEM *block)\n{\n int i;\n for(i=0; i<4; i++) {\n idctRowCondDC_8(block + i*8);\n }\n for(i=0;i<8;i++) {\n idct4col_add(dest + i, line_size, block + i);\n }\n}', 'static inline void FUNC(idctRowCondDC)(DCTELEM *row)\n{\n int a0, a1, a2, a3, b0, b1, b2, b3;\n#if HAVE_FAST_64BIT\n#define ROW0_MASK (0xffffLL << 48 * HAVE_BIGENDIAN)\n if (((((uint64_t *)row)[0] & ~ROW0_MASK) | ((uint64_t *)row)[1]) == 0) {\n uint64_t temp = (row[0] << DC_SHIFT) & 0xffff;\n temp += temp << 16;\n temp += temp << 32;\n ((uint64_t *)row)[0] = temp;\n ((uint64_t *)row)[1] = temp;\n return;\n }\n#else\n if (!(((uint32_t*)row)[1] |\n ((uint32_t*)row)[2] |\n ((uint32_t*)row)[3] |\n row[1])) {\n uint32_t temp = (row[0] << DC_SHIFT) & 0xffff;\n temp += temp << 16;\n ((uint32_t*)row)[0]=((uint32_t*)row)[1] =\n ((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;\n return;\n }\n#endif\n a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));\n a1 = a0;\n a2 = a0;\n a3 = a0;\n a0 += W2 * row[2];\n a1 += W6 * row[2];\n a2 -= W6 * row[2];\n a3 -= W2 * row[2];\n b0 = MUL(W1, row[1]);\n MAC(b0, W3, row[3]);\n b1 = MUL(W3, row[1]);\n MAC(b1, -W7, row[3]);\n b2 = MUL(W5, row[1]);\n MAC(b2, -W1, row[3]);\n b3 = MUL(W7, row[1]);\n MAC(b3, -W5, row[3]);\n if (AV_RN64A(row + 4)) {\n a0 += W4*row[4] + W6*row[6];\n a1 += - W4*row[4] - W2*row[6];\n a2 += - W4*row[4] + W2*row[6];\n a3 += W4*row[4] - W6*row[6];\n MAC(b0, W5, row[5]);\n MAC(b0, W7, row[7]);\n MAC(b1, -W1, row[5]);\n MAC(b1, -W5, row[7]);\n MAC(b2, W7, row[5]);\n MAC(b2, W3, row[7]);\n MAC(b3, W3, row[5]);\n MAC(b3, -W1, row[7]);\n }\n row[0] = (a0 + b0) >> ROW_SHIFT;\n row[7] = (a0 - b0) >> ROW_SHIFT;\n row[1] = (a1 + b1) >> ROW_SHIFT;\n row[6] = (a1 - b1) >> ROW_SHIFT;\n row[2] = (a2 + b2) >> ROW_SHIFT;\n row[5] = (a2 - b2) >> ROW_SHIFT;\n row[3] = (a3 + b3) >> ROW_SHIFT;\n row[4] = (a3 - b3) >> ROW_SHIFT;\n}']
|
1,275 | 0 |
https://github.com/libav/libav/blob/587874ef1c94a9b863d2f2db0e5d341e086ee232/libavformat/utils.c/#L587
|
static void 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) {
pd->buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
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;
}
}
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);
}
}
}
}
|
['static void 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 pd->buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);\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;\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}', '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}']
|
1,276 | 0 |
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_gf2m.c/#L471
|
int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[], BN_CTX *ctx)
{
int i, ret = 0;
BIGNUM *s;
bn_check_top(a);
BN_CTX_start(ctx);
if ((s = BN_CTX_get(ctx)) == NULL) return 0;
if (!bn_wexpand(s, 2 * a->top)) goto err;
for (i = a->top - 1; i >= 0; i--)
{
s->d[2*i+1] = SQR1(a->d[i]);
s->d[2*i ] = SQR0(a->d[i]);
}
s->top = 2 * a->top;
bn_correct_top(s);
if (!BN_GF2m_mod_arr(r, s, p)) goto err;
bn_check_top(r);
ret = 1;
err:
BN_CTX_end(ctx);
return ret;
}
|
['int\tBN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const int p[], BN_CTX *ctx)\n\t{\n\tint ret = 0, i, n;\n\tBIGNUM *u;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tif (BN_is_zero(b))\n\t\treturn(BN_one(r));\n\tif (BN_abs_is_word(b, 1))\n\t\treturn (BN_copy(r, a) != NULL);\n\tBN_CTX_start(ctx);\n\tif ((u = BN_CTX_get(ctx)) == NULL) goto err;\n\tif (!BN_GF2m_mod_arr(u, a, p)) goto err;\n\tn = BN_num_bits(b) - 1;\n\tfor (i = n - 1; i >= 0; i--)\n\t\t{\n\t\tif (!BN_GF2m_mod_sqr_arr(u, u, p, ctx)) goto err;\n\t\tif (BN_is_bit_set(b, i))\n\t\t\t{\n\t\t\tif (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx)) goto err;\n\t\t\t}\n\t\t}\n\tif (!BN_copy(r, u)) goto err;\n\tbn_check_top(r);\n\tret = 1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn ret;\n\t}', 'int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[])\n\t{\n\tint j, k;\n\tint n, dN, d0, d1;\n\tBN_ULONG zz, *z;\n\tbn_check_top(a);\n\tif (!p[0])\n\t\t{\n\t\tBN_zero(r);\n\t\treturn 1;\n\t\t}\n\tif (a != r)\n\t\t{\n\t\tif (!bn_wexpand(r, a->top)) return 0;\n\t\tfor (j = 0; j < a->top; j++)\n\t\t\t{\n\t\t\tr->d[j] = a->d[j];\n\t\t\t}\n\t\tr->top = a->top;\n\t\t}\n\tz = r->d;\n\tdN = p[0] / BN_BITS2;\n\tfor (j = r->top - 1; j > dN;)\n\t\t{\n\t\tzz = z[j];\n\t\tif (z[j] == 0) { j--; continue; }\n\t\tz[j] = 0;\n\t\tfor (k = 1; p[k] != 0; k++)\n\t\t\t{\n\t\t\tn = p[0] - p[k];\n\t\t\td0 = n % BN_BITS2; d1 = BN_BITS2 - d0;\n\t\t\tn /= BN_BITS2;\n\t\t\tz[j-n] ^= (zz>>d0);\n\t\t\tif (d0) z[j-n-1] ^= (zz<<d1);\n\t\t\t}\n\t\tn = dN;\n\t\td0 = p[0] % BN_BITS2;\n\t\td1 = BN_BITS2 - d0;\n\t\tz[j-n] ^= (zz >> d0);\n\t\tif (d0) z[j-n-1] ^= (zz << d1);\n\t\t}\n\twhile (j == dN)\n\t\t{\n\t\td0 = p[0] % BN_BITS2;\n\t\tzz = z[dN] >> d0;\n\t\tif (zz == 0) break;\n\t\td1 = BN_BITS2 - d0;\n\t\tif (d0)\n\t\t\tz[dN] = (z[dN] << d1) >> d1;\n\t\telse\n\t\t\tz[dN] = 0;\n\t\tz[0] ^= zz;\n\t\tfor (k = 1; p[k] != 0; k++)\n\t\t\t{\n\t\t\tBN_ULONG tmp_ulong;\n\t\t\tn = p[k] / BN_BITS2;\n\t\t\td0 = p[k] % BN_BITS2;\n\t\t\td1 = BN_BITS2 - d0;\n\t\t\tz[n] ^= (zz << d0);\n\t\t\ttmp_ulong = zz >> d1;\n if (d0 && tmp_ulong)\n z[n+1] ^= tmp_ulong;\n\t\t\t}\n\t\t}\n\tbn_correct_top(r);\n\treturn 1;\n\t}', 'int\tBN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[], BN_CTX *ctx)\n\t{\n\tint i, ret = 0;\n\tBIGNUM *s;\n\tbn_check_top(a);\n\tBN_CTX_start(ctx);\n\tif ((s = BN_CTX_get(ctx)) == NULL) return 0;\n\tif (!bn_wexpand(s, 2 * a->top)) goto err;\n\tfor (i = a->top - 1; i >= 0; i--)\n\t\t{\n\t\ts->d[2*i+1] = SQR1(a->d[i]);\n\t\ts->d[2*i ] = SQR0(a->d[i]);\n\t\t}\n\ts->top = 2 * a->top;\n\tbn_correct_top(s);\n\tif (!BN_GF2m_mod_arr(r, s, p)) goto err;\n\tbn_check_top(r);\n\tret = 1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn ret;\n\t}']
|
1,277 | 0 |
https://github.com/libav/libav/blob/4b728b4712403058ac4dc45daa8b5c03a688fadf/libavcodec/celp_filters.c/#L68
|
int ff_celp_lp_synthesis_filter(int16_t *out, const int16_t *filter_coeffs,
const int16_t *in, int buffer_length,
int filter_length, int stop_on_overflow,
int shift, int rounder)
{
int i,n;
for (n = 0; n < buffer_length; n++) {
int sum = -rounder, sum1;
for (i = 1; i <= filter_length; i++)
sum += filter_coeffs[i-1] * out[n-i];
sum1 = ((-sum >> 12) + in[n]) >> shift;
sum = av_clip_int16(sum1);
if (stop_on_overflow && sum != sum1)
return 1;
out[n] = sum;
}
return 0;
}
|
['static int ra144_decode_frame(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 static const uint8_t sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};\n unsigned int refl_rms[NBLOCKS];\n uint16_t block_coefs[NBLOCKS][LPC_ORDER];\n unsigned int lpc_refl[LPC_ORDER];\n int i, j;\n int ret;\n int16_t *samples;\n unsigned int energy;\n RA144Context *ractx = avctx->priv_data;\n GetBitContext gb;\n ractx->frame.nb_samples = NBLOCKS * BLOCKSIZE;\n if ((ret = avctx->get_buffer(avctx, &ractx->frame)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n samples = (int16_t *)ractx->frame.data[0];\n if(buf_size < FRAMESIZE) {\n av_log(avctx, AV_LOG_ERROR,\n "Frame too small (%d bytes). Truncated file?\\n", buf_size);\n *got_frame_ptr = 0;\n return buf_size;\n }\n init_get_bits(&gb, buf, FRAMESIZE * 8);\n for (i = 0; i < LPC_ORDER; i++)\n lpc_refl[i] = ff_lpc_refl_cb[i][get_bits(&gb, sizes[i])];\n ff_eval_coefs(ractx->lpc_coef[0], lpc_refl);\n ractx->lpc_refl_rms[0] = ff_rms(lpc_refl);\n energy = ff_energy_tab[get_bits(&gb, 5)];\n refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);\n refl_rms[1] = ff_interp(ractx, block_coefs[1], 2,\n energy <= ractx->old_energy,\n ff_t_sqrt(energy*ractx->old_energy) >> 12);\n refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy);\n refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy);\n ff_int_to_int16(block_coefs[3], ractx->lpc_coef[0]);\n for (i=0; i < NBLOCKS; i++) {\n do_output_subblock(ractx, block_coefs[i], refl_rms[i], &gb);\n for (j=0; j < BLOCKSIZE; j++)\n *samples++ = av_clip_int16(ractx->curr_sblock[j + 10] << 2);\n }\n ractx->old_energy = energy;\n ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];\n FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);\n *got_frame_ptr = 1;\n *(AVFrame *)data = ractx->frame;\n return FRAMESIZE;\n}', 'static void do_output_subblock(RA144Context *ractx, const uint16_t *lpc_coefs,\n int gval, GetBitContext *gb)\n{\n int cba_idx = get_bits(gb, 7);\n int gain = get_bits(gb, 8);\n int cb1_idx = get_bits(gb, 7);\n int cb2_idx = get_bits(gb, 7);\n ff_subblock_synthesis(ractx, lpc_coefs, cba_idx, cb1_idx, cb2_idx, gval,\n gain);\n}', 'void ff_subblock_synthesis(RA144Context *ractx, const uint16_t *lpc_coefs,\n int cba_idx, int cb1_idx, int cb2_idx,\n int gval, int gain)\n{\n uint16_t buffer_a[BLOCKSIZE];\n uint16_t *block;\n int m[3];\n if (cba_idx) {\n cba_idx += BLOCKSIZE/2 - 1;\n ff_copy_and_dup(buffer_a, ractx->adapt_cb, cba_idx);\n m[0] = (ff_irms(buffer_a) * gval) >> 12;\n } else {\n m[0] = 0;\n }\n m[1] = (ff_cb1_base[cb1_idx] * gval) >> 8;\n m[2] = (ff_cb2_base[cb2_idx] * gval) >> 8;\n memmove(ractx->adapt_cb, ractx->adapt_cb + BLOCKSIZE,\n (BUFFERSIZE - BLOCKSIZE) * sizeof(*ractx->adapt_cb));\n block = ractx->adapt_cb + BUFFERSIZE - BLOCKSIZE;\n ff_add_wav(block, gain, cba_idx, m, cba_idx? buffer_a: NULL,\n ff_cb1_vects[cb1_idx], ff_cb2_vects[cb2_idx]);\n memcpy(ractx->curr_sblock, ractx->curr_sblock + BLOCKSIZE,\n LPC_ORDER*sizeof(*ractx->curr_sblock));\n if (ff_celp_lp_synthesis_filter(ractx->curr_sblock + LPC_ORDER, lpc_coefs,\n block, BLOCKSIZE, LPC_ORDER, 1, 0, 0xfff))\n memset(ractx->curr_sblock, 0, (LPC_ORDER+BLOCKSIZE)*sizeof(*ractx->curr_sblock));\n}', 'int ff_celp_lp_synthesis_filter(int16_t *out, const int16_t *filter_coeffs,\n const int16_t *in, int buffer_length,\n int filter_length, int stop_on_overflow,\n int shift, int rounder)\n{\n int i,n;\n for (n = 0; n < buffer_length; n++) {\n int sum = -rounder, sum1;\n for (i = 1; i <= filter_length; i++)\n sum += filter_coeffs[i-1] * out[n-i];\n sum1 = ((-sum >> 12) + in[n]) >> shift;\n sum = av_clip_int16(sum1);\n if (stop_on_overflow && sum != sum1)\n return 1;\n out[n] = sum;\n }\n return 0;\n}']
|
1,278 | 1 |
https://github.com/libav/libav/blob/ed434be106a4615e0419b3ac7664220741afda2d/libswscale/utils.c/#L209
|
int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
{
return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
format_entries[pix_fmt].is_supported_in : 0;
}
|
['static int query_formats(AVFilterContext *ctx)\n{\n AVFilterFormats *formats;\n enum AVPixelFormat pix_fmt;\n int ret;\n if (ctx->inputs[0]) {\n const AVPixFmtDescriptor *desc = NULL;\n formats = NULL;\n while ((desc = av_pix_fmt_desc_next(desc))) {\n pix_fmt = av_pix_fmt_desc_get_id(desc);\n if ((sws_isSupportedInput(pix_fmt) ||\n sws_isSupportedEndiannessConversion(pix_fmt))\n && (ret = ff_add_format(&formats, pix_fmt)) < 0) {\n ff_formats_unref(&formats);\n return ret;\n }\n }\n ff_formats_ref(formats, &ctx->inputs[0]->out_formats);\n }\n if (ctx->outputs[0]) {\n const AVPixFmtDescriptor *desc = NULL;\n formats = NULL;\n while ((desc = av_pix_fmt_desc_next(desc))) {\n pix_fmt = av_pix_fmt_desc_get_id(desc);\n if ((sws_isSupportedOutput(pix_fmt) ||\n sws_isSupportedEndiannessConversion(pix_fmt))\n && (ret = ff_add_format(&formats, pix_fmt)) < 0) {\n ff_formats_unref(&formats);\n return ret;\n }\n }\n ff_formats_ref(formats, &ctx->outputs[0]->in_formats);\n }\n return 0;\n}', 'enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)\n{\n if (desc < av_pix_fmt_descriptors ||\n desc >= av_pix_fmt_descriptors + FF_ARRAY_ELEMS(av_pix_fmt_descriptors))\n return AV_PIX_FMT_NONE;\n return desc - av_pix_fmt_descriptors;\n}', 'int sws_isSupportedInput(enum AVPixelFormat pix_fmt)\n{\n return (unsigned)pix_fmt < AV_PIX_FMT_NB ?\n format_entries[pix_fmt].is_supported_in : 0;\n}']
|
1,279 | 0 |
https://github.com/openssl/openssl/blob/daea0ff8a960237e0f9a71301721824c25ad3b25/crypto/mem_dbg.c/#L574
|
static void print_leak(MEM *m, MEM_LEAK *l)
{
char buf[1024];
char *bufp = buf;
APP_INFO *amip;
int ami_cnt;
struct tm *lcl = NULL;
unsigned long ti;
if(m->addr == (char *)l->bio)
return;
if (options & V_CRYPTO_MDEBUG_TIME)
{
lcl = localtime(&m->time);
sprintf(bufp, "[%02d:%02d:%02d] ",
lcl->tm_hour,lcl->tm_min,lcl->tm_sec);
bufp += strlen(bufp);
}
sprintf(bufp, "%5lu file=%s, line=%d, ",
m->order,m->file,m->line);
bufp += strlen(bufp);
if (options & V_CRYPTO_MDEBUG_THREAD)
{
sprintf(bufp, "thread=%lu, ", m->thread);
bufp += strlen(bufp);
}
sprintf(bufp, "number=%d, address=%08lX\n",
m->num,(unsigned long)m->addr);
bufp += strlen(bufp);
BIO_puts(l->bio,buf);
l->chunks++;
l->bytes+=m->num;
amip=m->app_info;
ami_cnt=0;
if (!amip)
return;
ti=amip->thread;
do
{
int buf_len;
int info_len;
ami_cnt++;
memset(buf,'>',ami_cnt);
sprintf(buf + ami_cnt,
" thread=%lu, file=%s, line=%d, info=\"",
amip->thread, amip->file, amip->line);
buf_len=strlen(buf);
info_len=strlen(amip->info);
if (128 - buf_len - 3 < info_len)
{
memcpy(buf + buf_len, amip->info, 128 - buf_len - 3);
buf_len = 128 - 3;
}
else
{
strcpy(buf + buf_len, amip->info);
buf_len = strlen(buf);
}
sprintf(buf + buf_len, "\"\n");
BIO_puts(l->bio,buf);
amip = amip->next;
}
while(amip && amip->thread == ti);
#ifdef LEVITTE_DEBUG_MEM
if (amip)
{
fprintf(stderr, "Thread switch detected in backtrace!!!!\n");
abort();
}
#endif
}
|
['static void print_leak(MEM *m, MEM_LEAK *l)\n\t{\n\tchar buf[1024];\n\tchar *bufp = buf;\n\tAPP_INFO *amip;\n\tint ami_cnt;\n\tstruct tm *lcl = NULL;\n\tunsigned long ti;\n\tif(m->addr == (char *)l->bio)\n\t return;\n\tif (options & V_CRYPTO_MDEBUG_TIME)\n\t\t{\n\t\tlcl = localtime(&m->time);\n\t\tsprintf(bufp, "[%02d:%02d:%02d] ",\n\t\t\tlcl->tm_hour,lcl->tm_min,lcl->tm_sec);\n\t\tbufp += strlen(bufp);\n\t\t}\n\tsprintf(bufp, "%5lu file=%s, line=%d, ",\n\t\tm->order,m->file,m->line);\n\tbufp += strlen(bufp);\n\tif (options & V_CRYPTO_MDEBUG_THREAD)\n\t\t{\n\t\tsprintf(bufp, "thread=%lu, ", m->thread);\n\t\tbufp += strlen(bufp);\n\t\t}\n\tsprintf(bufp, "number=%d, address=%08lX\\n",\n\t\tm->num,(unsigned long)m->addr);\n\tbufp += strlen(bufp);\n\tBIO_puts(l->bio,buf);\n\tl->chunks++;\n\tl->bytes+=m->num;\n\tamip=m->app_info;\n\tami_cnt=0;\n\tif (!amip)\n\t\treturn;\n\tti=amip->thread;\n\tdo\n\t\t{\n\t\tint buf_len;\n\t\tint info_len;\n\t\tami_cnt++;\n\t\tmemset(buf,\'>\',ami_cnt);\n\t\tsprintf(buf + ami_cnt,\n\t\t\t" thread=%lu, file=%s, line=%d, info=\\"",\n\t\t\tamip->thread, amip->file, amip->line);\n\t\tbuf_len=strlen(buf);\n\t\tinfo_len=strlen(amip->info);\n\t\tif (128 - buf_len - 3 < info_len)\n\t\t\t{\n\t\t\tmemcpy(buf + buf_len, amip->info, 128 - buf_len - 3);\n\t\t\tbuf_len = 128 - 3;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tstrcpy(buf + buf_len, amip->info);\n\t\t\tbuf_len = strlen(buf);\n\t\t\t}\n\t\tsprintf(buf + buf_len, "\\"\\n");\n\t\tBIO_puts(l->bio,buf);\n\t\tamip = amip->next;\n\t\t}\n\twhile(amip && amip->thread == ti);\n#ifdef LEVITTE_DEBUG_MEM\n\tif (amip)\n\t\t{\n\t\tfprintf(stderr, "Thread switch detected in backtrace!!!!\\n");\n\t\tabort();\n\t\t}\n#endif\n\t}']
|
1,280 | 0 |
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/bn/bn_ctx.c/#L273
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n int i, bits, ret = 0;\n BIGNUM *v, *rr;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return -1;\n }\n BN_CTX_start(ctx);\n if ((r == a) || (r == p))\n rr = BN_CTX_get(ctx);\n else\n rr = r;\n v = BN_CTX_get(ctx);\n if (rr == NULL || v == NULL)\n goto err;\n if (BN_copy(v, a) == NULL)\n goto err;\n bits = BN_num_bits(p);\n if (BN_is_odd(p)) {\n if (BN_copy(rr, a) == NULL)\n goto err;\n } else {\n if (!BN_one(rr))\n goto err;\n }\n for (i = 1; i < bits; i++) {\n if (!BN_sqr(v, v, ctx))\n goto err;\n if (BN_is_bit_set(p, i)) {\n if (!BN_mul(rr, rr, v, ctx))\n goto err;\n }\n }\n if (r != rr)\n BN_copy(r, rr);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return (ret);\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (!rr || !tmp)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (rr != r)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
1,281 | 0 |
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/test/bntest.c/#L566
|
int test_div_word(BIO *bp)
{
BIGNUM *a, *b;
BN_ULONG r, s;
int i;
a = BN_new();
b = BN_new();
for (i = 0; i < num0; i++) {
do {
BN_bntest_rand(a, 512, -1, 0);
BN_bntest_rand(b, BN_BITS2, -1, 0);
} while (BN_is_zero(b));
s = b->d[0];
BN_copy(b, a);
r = BN_div_word(b, s);
if (bp != NULL) {
if (!results) {
BN_print(bp, a);
BIO_puts(bp, " / ");
print_word(bp, s);
BIO_puts(bp, " - ");
}
BN_print(bp, b);
BIO_puts(bp, "\n");
if (!results) {
BN_print(bp, a);
BIO_puts(bp, " % ");
print_word(bp, s);
BIO_puts(bp, " - ");
}
print_word(bp, r);
BIO_puts(bp, "\n");
}
BN_mul_word(b, s);
BN_add_word(b, r);
BN_sub(b, a, b);
if (!BN_is_zero(b)) {
fprintf(stderr, "Division (word) test failed!\n");
return 0;
}
}
BN_free(a);
BN_free(b);
return (1);
}
|
['int test_div_word(BIO *bp)\n{\n BIGNUM *a, *b;\n BN_ULONG r, s;\n int i;\n a = BN_new();\n b = BN_new();\n for (i = 0; i < num0; i++) {\n do {\n BN_bntest_rand(a, 512, -1, 0);\n BN_bntest_rand(b, BN_BITS2, -1, 0);\n } while (BN_is_zero(b));\n s = b->d[0];\n BN_copy(b, a);\n r = BN_div_word(b, s);\n if (bp != NULL) {\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " / ");\n print_word(bp, s);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, b);\n BIO_puts(bp, "\\n");\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " % ");\n print_word(bp, s);\n BIO_puts(bp, " - ");\n }\n print_word(bp, r);\n BIO_puts(bp, "\\n");\n }\n BN_mul_word(b, s);\n BN_add_word(b, r);\n BN_sub(b, a, b);\n if (!BN_is_zero(b)) {\n fprintf(stderr, "Division (word) test failed!\\n");\n return 0;\n }\n }\n BN_free(a);\n BN_free(b);\n return (1);\n}', 'BIGNUM *BN_new(void)\n{\n BIGNUM *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->flags = BN_FLG_MALLOCED;\n bn_check_top(ret);\n return (ret);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'void BN_free(BIGNUM *a)\n{\n if (a == NULL)\n return;\n bn_check_top(a);\n if (!BN_get_flags(a, BN_FLG_STATIC_DATA))\n bn_free_d(a);\n if (a->flags & BN_FLG_MALLOCED)\n OPENSSL_free(a);\n else {\n#if OPENSSL_API_COMPAT < 0x00908000L\n a->flags |= BN_FLG_FREE;\n#endif\n a->d = NULL;\n }\n}', 'int BN_get_flags(const BIGNUM *b, int n)\n{\n return b->flags & n;\n}']
|
1,282 | 1 |
https://github.com/openssl/openssl/blob/3da2e9c4ee45989a426ff513dc6c6250d1e460de/crypto/bn/bn_lib.c/#L365
|
int BN_set_word(BIGNUM *a, BN_ULONG w)
{
bn_check_top(a);
if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
return 0;
a->neg = 0;
a->d[0] = w;
a->top = (w ? 1 : 0);
a->flags &= ~BN_FLG_FIXED_TOP;
bn_check_top(a);
return 1;
}
|
['int ec_GFp_simple_group_set_curve(EC_GROUP *group,\n const BIGNUM *p, const BIGNUM *a,\n const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp_a;\n if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {\n ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD);\n return 0;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n tmp_a = BN_CTX_get(ctx);\n if (tmp_a == NULL)\n goto err;\n if (!BN_copy(group->field, p))\n goto err;\n BN_set_negative(group->field, 0);\n if (!BN_nnmod(tmp_a, a, p, ctx))\n goto err;\n if (group->meth->field_encode) {\n if (!group->meth->field_encode(group, group->a, tmp_a, ctx))\n goto err;\n } else if (!BN_copy(group->a, tmp_a))\n goto err;\n if (!BN_nnmod(group->b, b, p, ctx))\n goto err;\n if (group->meth->field_encode)\n if (!group->meth->field_encode(group, group->b, group->b, ctx))\n goto err;\n if (!BN_add_word(tmp_a, 3))\n goto err;\n group->a_is_minus3 = (0 == BN_cmp(tmp_a, group->field));\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n 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_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, j, 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)\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 l0 = bn_sub_words(wnum.d, wnum.d, 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.d, wnum.d, tmp->d, div_n);\n (*wnump) += l0;\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, j, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l, tmp;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n rb = n % BN_BITS2;\n lb = BN_BITS2 - rb;\n if (nw >= a->top || a->top == 0) {\n BN_zero(r);\n return 1;\n }\n i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;\n if (r != a) {\n if (bn_wexpand(r, i) == NULL)\n return 0;\n r->neg = a->neg;\n } else {\n if (n == 0)\n return 1;\n }\n f = &(a->d[nw]);\n t = r->d;\n j = a->top - nw;\n r->top = i;\n if (rb == 0) {\n for (i = j; i != 0; i--)\n *(t++) = *(f++);\n } else {\n l = *(f++);\n for (i = j - 1; i != 0; i--) {\n tmp = (l >> rb) & BN_MASK2;\n l = *(f++);\n *(t++) = (tmp | (l << lb)) & BN_MASK2;\n }\n if ((l = (l >> rb) & BN_MASK2))\n *(t) = l;\n }\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}']
|
1,283 | 0 |
https://github.com/openssl/openssl/blob/9ee0f7b7e0fe6e4baa6bc688c7b8fe013da9ce4e/crypto/lhash/lhash.c/#L240
|
void *lh_delete(LHASH *lh, const void *data)
{
unsigned long hash;
LHASH_NODE *nn,**rn;
void *ret;
lh->error=0;
rn=getrn(lh,data,&hash);
if (*rn == NULL)
{
lh->num_no_delete++;
return(NULL);
}
else
{
nn= *rn;
*rn=nn->next;
ret=nn->data;
OPENSSL_free(nn);
lh->num_delete++;
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))
contract(lh);
return(ret);
}
|
['int ssl3_accept(SSL *s)\n\t{\n\tBUF_MEM *buf;\n\tunsigned long l,Time=(unsigned long)time(NULL);\n\tvoid (*cb)(const SSL *ssl,int type,int val)=NULL;\n\tlong num1;\n\tint ret= -1;\n\tint new_state,state,skip=0;\n\tRAND_add(&Time,sizeof(Time),0);\n\tERR_clear_error();\n\tclear_sys_error();\n\tif (s->info_callback != NULL)\n\t\tcb=s->info_callback;\n\telse if (s->ctx->info_callback != NULL)\n\t\tcb=s->ctx->info_callback;\n\ts->in_handshake++;\n\tif (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);\n\tif (s->cert == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);\n\t\treturn(-1);\n\t\t}\n\tfor (;;)\n\t\t{\n\t\tstate=s->state;\n\t\tswitch (s->state)\n\t\t\t{\n\t\tcase SSL_ST_RENEGOTIATE:\n\t\t\ts->new_session=1;\n\t\tcase SSL_ST_BEFORE:\n\t\tcase SSL_ST_ACCEPT:\n\t\tcase SSL_ST_BEFORE|SSL_ST_ACCEPT:\n\t\tcase SSL_ST_OK|SSL_ST_ACCEPT:\n\t\t\ts->server=1;\n\t\t\tif (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);\n\t\t\tif ((s->version>>8) != 3)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);\n\t\t\t\treturn -1;\n\t\t\t\t}\n\t\t\ts->type=SSL_ST_ACCEPT;\n\t\t\tif (s->init_buf == NULL)\n\t\t\t\t{\n\t\t\t\tif ((buf=BUF_MEM_new()) == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tret= -1;\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tif (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))\n\t\t\t\t\t{\n\t\t\t\t\tret= -1;\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\ts->init_buf=buf;\n\t\t\t\t}\n\t\t\tif (!ssl3_setup_buffers(s))\n\t\t\t\t{\n\t\t\t\tret= -1;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\ts->init_num=0;\n\t\t\tif (s->state != SSL_ST_RENEGOTIATE)\n\t\t\t\t{\n\t\t\t\tif (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }\n\t\t\t\tssl3_init_finished_mac(s);\n\t\t\t\ts->state=SSL3_ST_SR_CLNT_HELLO_A;\n\t\t\t\ts->ctx->stats.sess_accept++;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts->ctx->stats.sess_accept_renegotiate++;\n\t\t\t\ts->state=SSL3_ST_SW_HELLO_REQ_A;\n\t\t\t\t}\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_HELLO_REQ_A:\n\t\tcase SSL3_ST_SW_HELLO_REQ_B:\n\t\t\ts->shutdown=0;\n\t\t\tret=ssl3_send_hello_request(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;\n\t\t\ts->state=SSL3_ST_SW_FLUSH;\n\t\t\ts->init_num=0;\n\t\t\tssl3_init_finished_mac(s);\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_HELLO_REQ_C:\n\t\t\ts->state=SSL_ST_OK;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SR_CLNT_HELLO_A:\n\t\tcase SSL3_ST_SR_CLNT_HELLO_B:\n\t\tcase SSL3_ST_SR_CLNT_HELLO_C:\n\t\t\ts->shutdown=0;\n\t\t\tret=ssl3_get_client_hello(s);\n\t\t\tif (ret <= 0) goto end;\n#ifndef OPENSSL_NO_TLSEXT\n\t\t\t{\n\t\t\t\tint al;\n\t\t\t\tif (ssl_check_tlsext(s,&al) <= 0)\n\t\t\t\t\t{\n\t\t\t\t\tssl3_send_alert(s,SSL3_AL_FATAL,al);\n\t\t\t\t\tSSLerr(SSL_F_SSL3_ACCEPT,SSL_R_CLIENTHELLO_TLS_EXT);\n\t\t\t\t\tret = -1;\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t}\n#endif\n\t\t\ts->new_session = 2;\n\t\t\ts->state=SSL3_ST_SW_SRVR_HELLO_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_SRVR_HELLO_A:\n\t\tcase SSL3_ST_SW_SRVR_HELLO_B:\n\t\t\tret=ssl3_send_server_hello(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (s->hit)\n\t\t\t\ts->state=SSL3_ST_SW_CHANGE_A;\n\t\t\telse\n\t\t\t\ts->state=SSL3_ST_SW_CERT_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_CERT_A:\n\t\tcase SSL3_ST_SW_CERT_B:\n\t\t\tif (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))\n\t\t\t\t{\n\t\t\t\tret=ssl3_send_server_certificate(s);\n\t\t\t\tif (ret <= 0) goto end;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tskip=1;\n\t\t\ts->state=SSL3_ST_SW_KEY_EXCH_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_KEY_EXCH_A:\n\t\tcase SSL3_ST_SW_KEY_EXCH_B:\n\t\t\tl=s->s3->tmp.new_cipher->algorithms;\n\t\t\tif ((s->options & SSL_OP_EPHEMERAL_RSA)\n#ifndef OPENSSL_NO_KRB5\n\t\t\t\t&& !(l & SSL_KRB5)\n#endif\n\t\t\t\t)\n\t\t\t\ts->s3->tmp.use_rsa_tmp=1;\n\t\t\telse\n\t\t\t\ts->s3->tmp.use_rsa_tmp=0;\n\t\t\tif (s->s3->tmp.use_rsa_tmp\n\t\t\t || (l & SSL_kECDHE)\n\t\t\t || (l & (SSL_DH|SSL_kFZA))\n\t\t\t || ((l & SSL_kRSA)\n\t\t\t\t&& (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL\n\t\t\t\t || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)\n\t\t\t\t\t&& EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)\n\t\t\t\t\t)\n\t\t\t\t )\n\t\t\t\t)\n\t\t\t )\n\t\t\t\t{\n\t\t\t\tret=ssl3_send_server_key_exchange(s);\n\t\t\t\tif (ret <= 0) goto end;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tskip=1;\n\t\t\ts->state=SSL3_ST_SW_CERT_REQ_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_CERT_REQ_A:\n\t\tcase SSL3_ST_SW_CERT_REQ_B:\n\t\t\tif (\n\t\t\t\t!(s->verify_mode & SSL_VERIFY_PEER) ||\n\t\t\t\t((s->session->peer != NULL) &&\n\t\t\t\t (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||\n\t\t\t\t((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&\n\t\t\t\t !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||\n (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))\n\t\t\t\t{\n\t\t\t\tskip=1;\n\t\t\t\ts->s3->tmp.cert_request=0;\n\t\t\t\ts->state=SSL3_ST_SW_SRVR_DONE_A;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts->s3->tmp.cert_request=1;\n\t\t\t\tret=ssl3_send_certificate_request(s);\n\t\t\t\tif (ret <= 0) goto end;\n#ifndef NETSCAPE_HANG_BUG\n\t\t\t\ts->state=SSL3_ST_SW_SRVR_DONE_A;\n#else\n\t\t\t\ts->state=SSL3_ST_SW_FLUSH;\n\t\t\t\ts->s3->tmp.next_state=SSL3_ST_SR_CERT_A;\n#endif\n\t\t\t\ts->init_num=0;\n\t\t\t\t}\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_SRVR_DONE_A:\n\t\tcase SSL3_ST_SW_SRVR_DONE_B:\n\t\t\tret=ssl3_send_server_done(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->s3->tmp.next_state=SSL3_ST_SR_CERT_A;\n\t\t\ts->state=SSL3_ST_SW_FLUSH;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_FLUSH:\n\t\t\tnum1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);\n\t\t\tif (num1 > 0)\n\t\t\t\t{\n\t\t\t\ts->rwstate=SSL_WRITING;\n\t\t\t\tnum1=BIO_flush(s->wbio);\n\t\t\t\tif (num1 <= 0) { ret= -1; goto end; }\n\t\t\t\ts->rwstate=SSL_NOTHING;\n\t\t\t\t}\n\t\t\ts->state=s->s3->tmp.next_state;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SR_CERT_A:\n\t\tcase SSL3_ST_SR_CERT_B:\n\t\t\tret = ssl3_check_client_hello(s);\n\t\t\tif (ret <= 0)\n\t\t\t\tgoto end;\n\t\t\tif (ret == 2)\n\t\t\t\ts->state = SSL3_ST_SR_CLNT_HELLO_C;\n\t\t\telse {\n\t\t\t\tif (s->s3->tmp.cert_request)\n\t\t\t\t\t{\n\t\t\t\t\tret=ssl3_get_client_certificate(s);\n\t\t\t\t\tif (ret <= 0) goto end;\n\t\t\t\t\t}\n\t\t\t\ts->init_num=0;\n\t\t\t\ts->state=SSL3_ST_SR_KEY_EXCH_A;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase SSL3_ST_SR_KEY_EXCH_A:\n\t\tcase SSL3_ST_SR_KEY_EXCH_B:\n\t\t\tret=ssl3_get_client_key_exchange(s);\n\t\t\tif (ret <= 0)\n\t\t\t\tgoto end;\n\t\t\tif (ret == 2)\n\t\t\t\t{\n\t\t\t\ts->state=SSL3_ST_SR_FINISHED_A;\n\t\t\t\ts->init_num = 0;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts->state=SSL3_ST_SR_CERT_VRFY_A;\n\t\t\t\ts->init_num=0;\n\t\t\t\ts->method->ssl3_enc->cert_verify_mac(s,\n\t\t\t\t &(s->s3->finish_dgst1),\n\t\t\t\t &(s->s3->tmp.cert_verify_md[0]));\n\t\t\t\ts->method->ssl3_enc->cert_verify_mac(s,\n\t\t\t\t &(s->s3->finish_dgst2),\n\t\t\t\t &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));\n\t\t\t\t}\n\t\t\tbreak;\n\t\tcase SSL3_ST_SR_CERT_VRFY_A:\n\t\tcase SSL3_ST_SR_CERT_VRFY_B:\n\t\t\tret=ssl3_get_cert_verify(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_SR_FINISHED_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SR_FINISHED_A:\n\t\tcase SSL3_ST_SR_FINISHED_B:\n\t\t\tret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,\n\t\t\t\tSSL3_ST_SR_FINISHED_B);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (s->hit)\n\t\t\t\ts->state=SSL_ST_OK;\n\t\t\telse\n\t\t\t\ts->state=SSL3_ST_SW_CHANGE_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_CHANGE_A:\n\t\tcase SSL3_ST_SW_CHANGE_B:\n\t\t\ts->session->cipher=s->s3->tmp.new_cipher;\n\t\t\tif (!s->method->ssl3_enc->setup_key_block(s))\n\t\t\t\t{ ret= -1; goto end; }\n\t\t\tret=ssl3_send_change_cipher_spec(s,\n\t\t\t\tSSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_SW_FINISHED_A;\n\t\t\ts->init_num=0;\n\t\t\tif (!s->method->ssl3_enc->change_cipher_state(s,\n\t\t\t\tSSL3_CHANGE_CIPHER_SERVER_WRITE))\n\t\t\t\t{\n\t\t\t\tret= -1;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_FINISHED_A:\n\t\tcase SSL3_ST_SW_FINISHED_B:\n\t\t\tret=ssl3_send_finished(s,\n\t\t\t\tSSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,\n\t\t\t\ts->method->ssl3_enc->server_finished_label,\n\t\t\t\ts->method->ssl3_enc->server_finished_label_len);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_SW_FLUSH;\n\t\t\tif (s->hit)\n\t\t\t\ts->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;\n\t\t\telse\n\t\t\t\ts->s3->tmp.next_state=SSL_ST_OK;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL_ST_OK:\n\t\t\tssl3_cleanup_key_block(s);\n\t\t\tBUF_MEM_free(s->init_buf);\n\t\t\ts->init_buf=NULL;\n\t\t\tssl_free_wbio_buffer(s);\n\t\t\ts->init_num=0;\n\t\t\tif (s->new_session == 2)\n\t\t\t\t{\n\t\t\t\ts->new_session=0;\n\t\t\t\tssl_update_cache(s,SSL_SESS_CACHE_SERVER);\n\t\t\t\ts->ctx->stats.sess_accept_good++;\n\t\t\t\ts->handshake_func=ssl3_accept;\n\t\t\t\tif (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);\n\t\t\t\t}\n\t\t\tret = 1;\n\t\t\tgoto end;\n\t\tdefault:\n\t\t\tSSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);\n\t\t\tret= -1;\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (!s->s3->tmp.reuse_message && !skip)\n\t\t\t{\n\t\t\tif (s->debug)\n\t\t\t\t{\n\t\t\t\tif ((ret=BIO_flush(s->wbio)) <= 0)\n\t\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tif ((cb != NULL) && (s->state != state))\n\t\t\t\t{\n\t\t\t\tnew_state=s->state;\n\t\t\t\ts->state=state;\n\t\t\t\tcb(s,SSL_CB_ACCEPT_LOOP,1);\n\t\t\t\ts->state=new_state;\n\t\t\t\t}\n\t\t\t}\n\t\tskip=0;\n\t\t}\nend:\n\ts->in_handshake--;\n\tif (cb != NULL)\n\t\tcb(s,SSL_CB_ACCEPT_EXIT,ret);\n\treturn(ret);\n\t}', 'int ssl3_get_client_hello(SSL *s)\n\t{\n\tint i,j,ok,al,ret= -1;\n\tunsigned int cookie_len;\n\tlong n;\n\tunsigned long id;\n\tunsigned char *p,*d,*q;\n\tSSL_CIPHER *c;\n#ifndef OPENSSL_NO_COMP\n\tSSL_COMP *comp=NULL;\n#endif\n\tSTACK_OF(SSL_CIPHER) *ciphers=NULL;\n\tif (s->state == SSL3_ST_SR_CLNT_HELLO_A)\n\t\t{\n\t\ts->first_packet=1;\n\t\ts->state=SSL3_ST_SR_CLNT_HELLO_B;\n\t\t}\n\tn=s->method->ssl_get_message(s,\n\t\tSSL3_ST_SR_CLNT_HELLO_B,\n\t\tSSL3_ST_SR_CLNT_HELLO_C,\n\t\tSSL3_MT_CLIENT_HELLO,\n\t\tSSL3_RT_MAX_PLAIN_LENGTH,\n\t\t&ok);\n\tif (!ok) return((int)n);\n\td=p=(unsigned char *)s->init_msg;\n\ts->client_version=(((int)p[0])<<8)|(int)p[1];\n\tp+=2;\n\tif (s->client_version < s->version)\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);\n\t\tif ((s->client_version>>8) == SSL3_VERSION_MAJOR)\n\t\t\t{\n\t\t\ts->version = s->client_version;\n\t\t\t}\n\t\tal = SSL_AD_PROTOCOL_VERSION;\n\t\tgoto f_err;\n\t\t}\n\tmemcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);\n\tp+=SSL3_RANDOM_SIZE;\n\tj= *(p++);\n\ts->hit=0;\n\tif (j == 0 || (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))\n\t\t{\n\t\tif (!ssl_get_new_session(s,1))\n\t\t\tgoto err;\n\t\t}\n\telse\n\t\t{\n\t\ti=ssl_get_prev_session(s,p,j);\n\t\tif (i == 1)\n\t\t\t{\n\t\t\ts->hit=1;\n\t\t\t}\n\t\telse if (i == -1)\n\t\t\tgoto err;\n\t\telse\n\t\t\t{\n\t\t\tif (!ssl_get_new_session(s,1))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tp+=j;\n\tif (SSL_version(s) == DTLS1_VERSION)\n\t\t{\n\t\tcookie_len = *(p++);\n\t\tif ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&\n\t\t\ts->d1->send_cookie == 0)\n\t\t\t{\n\t\t\tif ( cookie_len != s->d1->cookie_len)\n\t\t\t\t{\n\t\t\t\tal = SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\tif ( cookie_len > sizeof(s->d1->rcvd_cookie))\n\t\t\t{\n\t\t\tal = SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&\n\t\t\tcookie_len > 0)\n\t\t\t{\n\t\t\tmemcpy(s->d1->rcvd_cookie, p, cookie_len);\n\t\t\tif ( s->ctx->app_verify_cookie_cb != NULL)\n\t\t\t\t{\n\t\t\t\tif ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,\n\t\t\t\t\tcookie_len) == 0)\n\t\t\t\t\t{\n\t\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,\n\t\t\t\t\t\tSSL_R_COOKIE_MISMATCH);\n\t\t\t\t\tgoto f_err;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie,\n\t\t\t\t\t\t s->d1->cookie_len) != 0)\n\t\t\t\t{\n\t\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,\n\t\t\t\t\t\tSSL_R_COOKIE_MISMATCH);\n\t\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\tp += cookie_len;\n\t\t}\n\tn2s(p,i);\n\tif ((i == 0) && (j != 0))\n\t\t{\n\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);\n\t\tgoto f_err;\n\t\t}\n\tif ((p+i) >= (d+n))\n\t\t{\n\t\tal=SSL_AD_DECODE_ERROR;\n\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);\n\t\tgoto f_err;\n\t\t}\n\tif ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))\n\t\t== NULL))\n\t\t{\n\t\tgoto err;\n\t\t}\n\tp+=i;\n\tif ((s->hit) && (i > 0))\n\t\t{\n\t\tj=0;\n\t\tid=s->session->cipher->id;\n#ifdef CIPHER_DEBUG\n\t\tprintf("client sent %d ciphers\\n",sk_num(ciphers));\n#endif\n\t\tfor (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)\n\t\t\t{\n\t\t\tc=sk_SSL_CIPHER_value(ciphers,i);\n#ifdef CIPHER_DEBUG\n\t\t\tprintf("client [%2d of %2d]:%s\\n",\n\t\t\t\ti,sk_num(ciphers),SSL_CIPHER_get_name(c));\n#endif\n\t\t\tif (c->id == id)\n\t\t\t\t{\n\t\t\t\tj=1;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tif (j == 0)\n\t\t\t{\n\t\t\tif ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))\n\t\t\t\t{\n\t\t\t\ts->session->cipher=sk_SSL_CIPHER_value(ciphers, 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_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\ti= *(p++);\n\tif ((p+i) > (d+n))\n\t\t{\n\t\tal=SSL_AD_DECODE_ERROR;\n\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);\n\t\tgoto f_err;\n\t\t}\n\tq=p;\n\tfor (j=0; j<i; j++)\n\t\t{\n\t\tif (p[j] == 0) break;\n\t\t}\n\tp+=i;\n\tif (j >= i)\n\t\t{\n\t\tal=SSL_AD_DECODE_ERROR;\n\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);\n\t\tgoto f_err;\n\t\t}\n\ts->s3->tmp.new_compression=NULL;\n#ifndef OPENSSL_NO_COMP\n\tif (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods)\n\t\t{\n\t\tint m,nn,o,v,done=0;\n\t\tnn=sk_SSL_COMP_num(s->ctx->comp_methods);\n\t\tfor (m=0; m<nn; m++)\n\t\t\t{\n\t\t\tcomp=sk_SSL_COMP_value(s->ctx->comp_methods,m);\n\t\t\tv=comp->id;\n\t\t\tfor (o=0; o<i; o++)\n\t\t\t\t{\n\t\t\t\tif (v == q[o])\n\t\t\t\t\t{\n\t\t\t\t\tdone=1;\n\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\tif (done) break;\n\t\t\t}\n\t\tif (done)\n\t\t\ts->s3->tmp.new_compression=comp;\n\t\telse\n\t\t\tcomp=NULL;\n\t\t}\n#endif\n#ifndef OPENSSL_NO_TLSEXT\n\tif (s->version > SSL3_VERSION)\n\t\t{\n\t\tif (!ssl_parse_clienthello_tlsext(s,&p,d,n, &al))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_PARSE_TLS_EXT);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n#endif\n\tif (!s->hit)\n\t\t{\n#ifdef OPENSSL_NO_COMP\n\t\ts->session->compress_meth=0;\n#else\n\t\ts->session->compress_meth=(comp == NULL)?0:comp->id;\n#endif\n\t\tif (s->session->ciphers != NULL)\n\t\t\tsk_SSL_CIPHER_free(s->session->ciphers);\n\t\ts->session->ciphers=ciphers;\n\t\tif (ciphers == NULL)\n\t\t\t{\n\t\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tciphers=NULL;\n\t\tc=ssl3_choose_cipher(s,s->session->ciphers,\n\t\t\t\t SSL_get_ciphers(s));\n\t\tif (c == NULL)\n\t\t\t{\n\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\ts->s3->tmp.new_cipher=c;\n\t\t}\n\telse\n\t\t{\n#ifdef REUSE_CIPHER_BUG\n\t\tSTACK_OF(SSL_CIPHER) *sk;\n\t\tSSL_CIPHER *nc=NULL;\n\t\tSSL_CIPHER *ec=NULL;\n\t\tif (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)\n\t\t\t{\n\t\t\tsk=s->session->ciphers;\n\t\t\tfor (i=0; i<sk_SSL_CIPHER_num(sk); i++)\n\t\t\t\t{\n\t\t\t\tc=sk_SSL_CIPHER_value(sk,i);\n\t\t\t\tif (c->algorithms & SSL_eNULL)\n\t\t\t\t\tnc=c;\n\t\t\t\tif (SSL_C_IS_EXPORT(c))\n\t\t\t\t\tec=c;\n\t\t\t\t}\n\t\t\tif (nc != NULL)\n\t\t\t\ts->s3->tmp.new_cipher=nc;\n\t\t\telse if (ec != NULL)\n\t\t\t\ts->s3->tmp.new_cipher=ec;\n\t\t\telse\n\t\t\t\ts->s3->tmp.new_cipher=s->session->cipher;\n\t\t\t}\n\t\telse\n#endif\n\t\ts->s3->tmp.new_cipher=s->session->cipher;\n\t\t}\n\tret=1;\n\tif (0)\n\t\t{\nf_err:\n\t\tssl3_send_alert(s,SSL3_AL_FATAL,al);\n\t\t}\nerr:\n\tif (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);\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 (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)\n\t\tdesc = SSL_AD_HANDSHAKE_FAILURE;\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\ts->method->ssl_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\tif ((r = (SSL_SESSION *)lh_retrieve(ctx->sessions,c)) == c)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,c);\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'void *lh_delete(LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tvoid *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tOPENSSL_free(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}']
|
1,284 | 1 |
https://github.com/openssl/openssl/blob/98c03302fb7b855647aa14022f61f5fb272e514a/crypto/rsa/rsa_chk.c/#L226
|
int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)
{
BIGNUM *i, *j, *k, *l, *m;
BN_CTX *ctx;
int ret = 1, ex_primes = 0, idx;
RSA_PRIME_INFO *pinfo;
if (key->p == NULL || key->q == NULL || key->n == NULL
|| key->e == NULL || key->d == NULL) {
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);
return 0;
}
if (key->version == RSA_ASN1_VERSION_MULTI) {
ex_primes = sk_RSA_PRIME_INFO_num(key->prime_infos);
if (ex_primes <= 0
|| (ex_primes + 2) > rsa_multip_cap(BN_num_bits(key->n))) {
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_INVALID_MULTI_PRIME_KEY);
return 0;
}
}
i = BN_new();
j = BN_new();
k = BN_new();
l = BN_new();
m = BN_new();
ctx = BN_CTX_new();
if (i == NULL || j == NULL || k == NULL || l == NULL
|| m == NULL || ctx == NULL) {
ret = -1;
RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);
goto err;
}
if (BN_is_one(key->e)) {
ret = 0;
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);
}
if (!BN_is_odd(key->e)) {
ret = 0;
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);
}
if (BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb) != 1) {
ret = 0;
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);
}
if (BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb) != 1) {
ret = 0;
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);
}
for (idx = 0; idx < ex_primes; idx++) {
pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
if (BN_is_prime_ex(pinfo->r, BN_prime_checks, NULL, cb) != 1) {
ret = 0;
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_R_NOT_PRIME);
}
}
if (!BN_mul(i, key->p, key->q, ctx)) {
ret = -1;
goto err;
}
for (idx = 0; idx < ex_primes; idx++) {
pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
if (!BN_mul(i, i, pinfo->r, ctx)) {
ret = -1;
goto err;
}
}
if (BN_cmp(i, key->n) != 0) {
ret = 0;
if (ex_primes)
RSAerr(RSA_F_RSA_CHECK_KEY_EX,
RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES);
else
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);
}
if (!BN_sub(i, key->p, BN_value_one())) {
ret = -1;
goto err;
}
if (!BN_sub(j, key->q, BN_value_one())) {
ret = -1;
goto err;
}
if (!BN_mul(l, i, j, ctx)) {
ret = -1;
goto err;
}
if (!BN_gcd(m, i, j, ctx)) {
ret = -1;
goto err;
}
for (idx = 0; idx < ex_primes; idx++) {
pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
if (!BN_sub(k, pinfo->r, BN_value_one())) {
ret = -1;
goto err;
}
if (!BN_mul(l, l, k, ctx)) {
ret = -1;
goto err;
}
if (!BN_gcd(m, m, k, ctx)) {
ret = -1;
goto err;
}
}
if (!BN_div(k, NULL, l, m, ctx)) {
ret = -1;
goto err;
}
if (!BN_mod_mul(i, key->d, key->e, k, ctx)) {
ret = -1;
goto err;
}
if (!BN_is_one(i)) {
ret = 0;
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);
}
if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {
if (!BN_sub(i, key->p, BN_value_one())) {
ret = -1;
goto err;
}
if (!BN_mod(j, key->d, i, ctx)) {
ret = -1;
goto err;
}
if (BN_cmp(j, key->dmp1) != 0) {
ret = 0;
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);
}
if (!BN_sub(i, key->q, BN_value_one())) {
ret = -1;
goto err;
}
if (!BN_mod(j, key->d, i, ctx)) {
ret = -1;
goto err;
}
if (BN_cmp(j, key->dmq1) != 0) {
ret = 0;
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);
}
if (!BN_mod_inverse(i, key->q, key->p, ctx)) {
ret = -1;
goto err;
}
if (BN_cmp(i, key->iqmp) != 0) {
ret = 0;
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);
}
}
for (idx = 0; idx < ex_primes; idx++) {
pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
if (!BN_sub(i, pinfo->r, BN_value_one())) {
ret = -1;
goto err;
}
if (!BN_mod(j, key->d, i, ctx)) {
ret = -1;
goto err;
}
if (BN_cmp(j, pinfo->d) != 0) {
ret = 0;
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D);
}
if (!BN_mod_inverse(i, pinfo->pp, pinfo->r, ctx)) {
ret = -1;
goto err;
}
if (BN_cmp(i, pinfo->t) != 0) {
ret = 0;
RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R);
}
}
err:
BN_free(i);
BN_free(j);
BN_free(k);
BN_free(l);
BN_free(m);
BN_CTX_free(ctx);
return ret;
}
|
['int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)\n{\n BIGNUM *i, *j, *k, *l, *m;\n BN_CTX *ctx;\n int ret = 1, ex_primes = 0, idx;\n RSA_PRIME_INFO *pinfo;\n if (key->p == NULL || key->q == NULL || key->n == NULL\n || key->e == NULL || key->d == NULL) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);\n return 0;\n }\n if (key->version == RSA_ASN1_VERSION_MULTI) {\n ex_primes = sk_RSA_PRIME_INFO_num(key->prime_infos);\n if (ex_primes <= 0\n || (ex_primes + 2) > rsa_multip_cap(BN_num_bits(key->n))) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_INVALID_MULTI_PRIME_KEY);\n return 0;\n }\n }\n i = BN_new();\n j = BN_new();\n k = BN_new();\n l = BN_new();\n m = BN_new();\n ctx = BN_CTX_new();\n if (i == NULL || j == NULL || k == NULL || l == NULL\n || m == NULL || ctx == NULL) {\n ret = -1;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (BN_is_one(key->e)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);\n }\n if (!BN_is_odd(key->e)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);\n }\n if (BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);\n }\n if (BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (BN_is_prime_ex(pinfo->r, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_R_NOT_PRIME);\n }\n }\n if (!BN_mul(i, key->p, key->q, ctx)) {\n ret = -1;\n goto err;\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_mul(i, i, pinfo->r, ctx)) {\n ret = -1;\n goto err;\n }\n }\n if (BN_cmp(i, key->n) != 0) {\n ret = 0;\n if (ex_primes)\n RSAerr(RSA_F_RSA_CHECK_KEY_EX,\n RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES);\n else\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);\n }\n if (!BN_sub(i, key->p, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_sub(j, key->q, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mul(l, i, j, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_gcd(m, i, j, ctx)) {\n ret = -1;\n goto err;\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_sub(k, pinfo->r, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mul(l, l, k, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_gcd(m, m, k, ctx)) {\n ret = -1;\n goto err;\n }\n }\n if (!BN_div(k, NULL, l, m, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_mod_mul(i, key->d, key->e, k, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_is_one(i)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);\n }\n if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {\n if (!BN_sub(i, key->p, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmp1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_sub(i, key->q, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmq1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, key->q, key->p, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, key->iqmp) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);\n }\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_sub(i, pinfo->r, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, pinfo->d) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, pinfo->pp, pinfo->r, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, pinfo->t) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R);\n }\n }\n err:\n BN_free(i);\n BN_free(j);\n BN_free(k);\n BN_free(l);\n BN_free(m);\n BN_CTX_free(ctx);\n return ret;\n}', 'void BN_CTX_free(BN_CTX *ctx)\n{\n#ifdef BN_CTX_DEBUG\n {\n BN_POOL_ITEM *pool = ctx->pool.head;\n fprintf(stderr, "BN_CTX_free, stack-size=%d, pool-bignums=%d\\n",\n ctx->stack.size, ctx->pool.size);\n fprintf(stderr, "dmaxs: ");\n while (pool) {\n unsigned loop = 0;\n while (loop < BN_CTX_POOL_SIZE)\n fprintf(stderr, "%02x ", pool->vals[loop++].dmax);\n pool = pool->next;\n }\n fprintf(stderr, "\\n");\n }\n#endif\n BN_STACK_finish(&ctx->stack);\n BN_POOL_finish(&ctx->pool);\n OPENSSL_free(ctx);\n}', 'static void BN_STACK_finish(BN_STACK *st)\n{\n OPENSSL_free(st->indexes);\n st->indexes = NULL;\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}', 'static void BN_POOL_finish(BN_POOL *p)\n{\n unsigned int loop;\n BIGNUM *bn;\n while (p->head) {\n for (loop = 0, bn = p->head->vals; loop++ < BN_CTX_POOL_SIZE; bn++)\n if (bn->d)\n BN_clear_free(bn);\n p->current = p->head->next;\n OPENSSL_free(p->head);\n p->head = p->current;\n }\n}']
|
1,285 | 0 |
https://github.com/openssl/openssl/blob/b2e54eb834e2d5a79d03f12a818d68f82c0e3d13/crypto/err/err.c/#L746
|
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 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 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}']
|
1,286 | 0 |
https://github.com/libav/libav/blob/1e8b9738fa70e20967ddb542d2f9d5552fc51ec6/libavcodec/h264.c/#L1525
|
static void copy_parameter_set(void **to, void **from, int count, int size)
{
int i;
for (i = 0; i < count; i++) {
if (to[i] && !from[i])
av_freep(&to[i]);
else if (from[i] && !to[i])
to[i] = av_malloc(size);
if (from[i])
memcpy(to[i], from[i], size);
}
}
|
['static void copy_parameter_set(void **to, void **from, int count, int size)\n{\n int i;\n for (i = 0; i < count; i++) {\n if (to[i] && !from[i])\n av_freep(&to[i]);\n else if (from[i] && !to[i])\n to[i] = av_malloc(size);\n if (from[i])\n memcpy(to[i], from[i], size);\n }\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if (size > (INT_MAX - 32) || !size)\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size + 32);\n if (!ptr)\n return ptr;\n diff = ((-(long)ptr - 1) & 31) + 1;\n ptr = (char *)ptr + diff;\n ((char *)ptr)[-1] = diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr, 32, size))\n ptr = NULL;\n#elif HAVE_ALIGNED_MALLOC\n ptr = _aligned_malloc(size, 32);\n#elif HAVE_MEMALIGN\n ptr = memalign(32, size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
|
1,287 | 0 |
https://github.com/libav/libav/blob/e3ec6fe7bb2a622a863e3912181717a659eb1bad/avconv.c/#L1498
|
static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
{
InputStream *ist = s->opaque;
const enum AVPixelFormat *p;
int ret;
for (p = pix_fmts; *p != -1; p++) {
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
const HWAccel *hwaccel;
if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
break;
hwaccel = get_hwaccel(*p);
if (!hwaccel ||
(ist->active_hwaccel_id && ist->active_hwaccel_id != hwaccel->id) ||
(ist->hwaccel_id != HWACCEL_AUTO && ist->hwaccel_id != hwaccel->id))
continue;
ret = hwaccel->init(s);
if (ret < 0) {
if (ist->hwaccel_id == hwaccel->id) {
av_log(NULL, AV_LOG_FATAL,
"%s hwaccel requested for input stream #%d:%d, "
"but cannot be initialized.\n", hwaccel->name,
ist->file_index, ist->st->index);
return AV_PIX_FMT_NONE;
}
continue;
}
ist->active_hwaccel_id = hwaccel->id;
ist->hwaccel_pix_fmt = *p;
break;
}
return *p;
}
|
['static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)\n{\n InputStream *ist = s->opaque;\n const enum AVPixelFormat *p;\n int ret;\n for (p = pix_fmts; *p != -1; p++) {\n const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);\n const HWAccel *hwaccel;\n if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))\n break;\n hwaccel = get_hwaccel(*p);\n if (!hwaccel ||\n (ist->active_hwaccel_id && ist->active_hwaccel_id != hwaccel->id) ||\n (ist->hwaccel_id != HWACCEL_AUTO && ist->hwaccel_id != hwaccel->id))\n continue;\n ret = hwaccel->init(s);\n if (ret < 0) {\n if (ist->hwaccel_id == hwaccel->id) {\n av_log(NULL, AV_LOG_FATAL,\n "%s hwaccel requested for input stream #%d:%d, "\n "but cannot be initialized.\\n", hwaccel->name,\n ist->file_index, ist->st->index);\n return AV_PIX_FMT_NONE;\n }\n continue;\n }\n ist->active_hwaccel_id = hwaccel->id;\n ist->hwaccel_pix_fmt = *p;\n break;\n }\n return *p;\n}', 'const AVPixFmtDescriptor *av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)\n{\n if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)\n return NULL;\n return &av_pix_fmt_descriptors[pix_fmt];\n}']
|
1,288 | 0 |
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/ssl/ssltest.c/#L2898
|
static int app_verify_callback(X509_STORE_CTX *ctx, void *arg)
{
int ok = 1;
struct app_verify_arg *cb_arg = arg;
unsigned int letters[26];
if (cb_arg->app_verify) {
char *s = NULL, buf[256];
fprintf(stderr, "In app_verify_callback, allowing cert. ");
fprintf(stderr, "Arg is: %s\n", cb_arg->string);
fprintf(stderr,
"Finished printing do we have a context? 0x%p a cert? 0x%p\n",
(void *)ctx, (void *)ctx->cert);
if (ctx->cert)
s = X509_NAME_oneline(X509_get_subject_name(ctx->cert), buf, 256);
if (s != NULL) {
fprintf(stderr, "cert depth=%d %s\n", ctx->error_depth, buf);
}
return (1);
}
if (cb_arg->proxy_auth) {
int found_any = 0, i;
char *sp;
for (i = 0; i < 26; i++)
letters[i] = 0;
for (sp = cb_arg->proxy_auth; *sp; sp++) {
int c = *sp;
if (isascii(c) && isalpha(c)) {
if (islower(c))
c = toupper(c);
letters[c - 'A'] = 1;
}
}
fprintf(stderr, " Initial proxy rights = ");
for (i = 0; i < 26; i++)
if (letters[i]) {
fprintf(stderr, "%c", i + 'A');
found_any = 1;
}
if (!found_any)
fprintf(stderr, "none");
fprintf(stderr, "\n");
X509_STORE_CTX_set_ex_data(ctx,
get_proxy_auth_ex_data_idx(), letters);
}
if (cb_arg->allow_proxy_certs) {
X509_STORE_CTX_set_flags(ctx, X509_V_FLAG_ALLOW_PROXY_CERTS);
}
#ifndef OPENSSL_NO_X509_VERIFY
ok = X509_verify_cert(ctx);
#endif
if (cb_arg->proxy_auth) {
if (ok > 0) {
const char *cond_end = NULL;
ok = process_proxy_cond(letters, cb_arg->proxy_cond, &cond_end);
if (ok < 0)
EXIT(3);
if (*cond_end) {
fprintf(stderr,
"Stopped processing condition before it's end.\n");
ok = 0;
}
if (!ok)
fprintf(stderr,
"Proxy rights check with condition '%s' proved invalid\n",
cb_arg->proxy_cond);
else
fprintf(stderr,
"Proxy rights check with condition '%s' proved valid\n",
cb_arg->proxy_cond);
}
}
return (ok);
}
|
['static int app_verify_callback(X509_STORE_CTX *ctx, void *arg)\n{\n int ok = 1;\n struct app_verify_arg *cb_arg = arg;\n unsigned int letters[26];\n if (cb_arg->app_verify) {\n char *s = NULL, buf[256];\n fprintf(stderr, "In app_verify_callback, allowing cert. ");\n fprintf(stderr, "Arg is: %s\\n", cb_arg->string);\n fprintf(stderr,\n "Finished printing do we have a context? 0x%p a cert? 0x%p\\n",\n (void *)ctx, (void *)ctx->cert);\n if (ctx->cert)\n s = X509_NAME_oneline(X509_get_subject_name(ctx->cert), buf, 256);\n if (s != NULL) {\n fprintf(stderr, "cert depth=%d %s\\n", ctx->error_depth, buf);\n }\n return (1);\n }\n if (cb_arg->proxy_auth) {\n int found_any = 0, i;\n char *sp;\n for (i = 0; i < 26; i++)\n letters[i] = 0;\n for (sp = cb_arg->proxy_auth; *sp; sp++) {\n int c = *sp;\n if (isascii(c) && isalpha(c)) {\n if (islower(c))\n c = toupper(c);\n letters[c - \'A\'] = 1;\n }\n }\n fprintf(stderr, " Initial proxy rights = ");\n for (i = 0; i < 26; i++)\n if (letters[i]) {\n fprintf(stderr, "%c", i + \'A\');\n found_any = 1;\n }\n if (!found_any)\n fprintf(stderr, "none");\n fprintf(stderr, "\\n");\n X509_STORE_CTX_set_ex_data(ctx,\n get_proxy_auth_ex_data_idx(), letters);\n }\n if (cb_arg->allow_proxy_certs) {\n X509_STORE_CTX_set_flags(ctx, X509_V_FLAG_ALLOW_PROXY_CERTS);\n }\n#ifndef OPENSSL_NO_X509_VERIFY\n ok = X509_verify_cert(ctx);\n#endif\n if (cb_arg->proxy_auth) {\n if (ok > 0) {\n const char *cond_end = NULL;\n ok = process_proxy_cond(letters, cb_arg->proxy_cond, &cond_end);\n if (ok < 0)\n EXIT(3);\n if (*cond_end) {\n fprintf(stderr,\n "Stopped processing condition before it\'s end.\\n");\n ok = 0;\n }\n if (!ok)\n fprintf(stderr,\n "Proxy rights check with condition \'%s\' proved invalid\\n",\n cb_arg->proxy_cond);\n else\n fprintf(stderr,\n "Proxy rights check with condition \'%s\' proved valid\\n",\n cb_arg->proxy_cond);\n }\n }\n return (ok);\n}']
|
1,289 | 0 |
https://github.com/openssl/openssl/blob/270a4bba49849de7f928f4fab186205abd132411/crypto/mem.c/#L281
|
void CRYPTO_free(void *str, const char *file, int line)
{
if (free_impl != NULL && free_impl != &CRYPTO_free) {
free_impl(str, file, line);
return;
}
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
if (call_malloc_debug) {
CRYPTO_mem_debug_free(str, 0, file, line);
free(str);
CRYPTO_mem_debug_free(str, 1, file, line);
} else {
free(str);
}
#else
free(str);
#endif
}
|
['static int test_int_stack(int reserve)\n{\n static int v[] = { 1, 2, -4, 16, 999, 1, -173, 1, 9 };\n static int notpresent = -1;\n const int n = OSSL_NELEM(v);\n static struct {\n int value;\n int unsorted;\n int sorted;\n int ex;\n } finds[] = {\n { 2, 1, 5, 5 },\n { 9, 7, 6, 6 },\n { -173, 5, 0, 0 },\n { 999, 3, 8, 8 },\n { 0, -1, -1, 1 }\n };\n const int n_finds = OSSL_NELEM(finds);\n static struct {\n int value;\n int ex;\n } exfinds[] = {\n { 3, 5 },\n { 1000, 8 },\n { 20, 8 },\n { -999, 0 },\n { -5, 0 },\n { 8, 5 }\n };\n const int n_exfinds = OSSL_NELEM(exfinds);\n STACK_OF(sint) *s = sk_sint_new_null();\n int i;\n int testresult = 0;\n if (!TEST_ptr(s)\n || (reserve > 0 && !TEST_true(sk_sint_reserve(s, 5 * reserve))))\n goto end;\n for (i = 0; i < n; i++) {\n if (!TEST_int_eq(sk_sint_num(s), i)) {\n TEST_info("int stack size %d", i);\n goto end;\n }\n sk_sint_push(s, v + i);\n }\n if (!TEST_int_eq(sk_sint_num(s), n))\n goto end;\n for (i = 0; i < n; i++)\n if (!TEST_ptr_eq(sk_sint_value(s, i), v + i)) {\n TEST_info("int value %d", i);\n goto end;\n }\n for (i = 0; i < n_finds; i++) {\n int *val = (finds[i].unsorted == -1) ? ¬present\n : v + finds[i].unsorted;\n if (!TEST_int_eq(sk_sint_find(s, val), finds[i].unsorted)) {\n TEST_info("int unsorted find %d", i);\n goto end;\n }\n }\n for (i = 0; i < n_finds; i++) {\n int *val = (finds[i].unsorted == -1) ? ¬present\n : v + finds[i].unsorted;\n if (!TEST_int_eq(sk_sint_find_ex(s, val), finds[i].unsorted)) {\n TEST_info("int unsorted find_ex %d", i);\n goto end;\n }\n }\n if (!TEST_false(sk_sint_is_sorted(s)))\n goto end;\n sk_sint_set_cmp_func(s, &int_compare);\n sk_sint_sort(s);\n if (!TEST_true(sk_sint_is_sorted(s)))\n goto end;\n for (i = 0; i < n_finds; i++)\n if (!TEST_int_eq(sk_sint_find(s, &finds[i].value), finds[i].sorted)) {\n TEST_info("int sorted find %d", i);\n goto end;\n }\n for (i = 0; i < n_finds; i++)\n if (!TEST_int_eq(sk_sint_find_ex(s, &finds[i].value), finds[i].ex)) {\n TEST_info("int sorted find_ex present %d", i);\n goto end;\n }\n for (i = 0; i < n_exfinds; i++)\n if (!TEST_int_eq(sk_sint_find_ex(s, &exfinds[i].value), exfinds[i].ex)){\n TEST_info("int sorted find_ex absent %d", i);\n goto end;\n }\n if (!TEST_ptr_eq(sk_sint_shift(s), v + 6))\n goto end;\n testresult = 1;\nend:\n sk_sint_free(s);\n return testresult;\n}', 'DEFINE_SPECIAL_STACK_OF(sint, int)', 'int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)\n{\n if (st == NULL)\n return -1;\n return OPENSSL_sk_insert(st, data, st->num);\n}', 'int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)\n{\n if (st == NULL || st->num < 0 || st->num == max_nodes)\n return 0;\n if (!sk_reserve(st, 1, 0))\n return 0;\n if ((loc >= st->num) || (loc < 0)) {\n st->data[st->num] = data;\n } else {\n memmove(&st->data[loc + 1], &st->data[loc],\n sizeof(st->data[0]) * (st->num - loc));\n st->data[loc] = data;\n }\n st->num++;\n st->sorted = 0;\n return st->num;\n}', 'static int sk_reserve(OPENSSL_STACK *st, int n, int exact)\n{\n const void **tmpdata;\n int num_alloc;\n if (n > max_nodes - st->num)\n return 0;\n num_alloc = st->num + n;\n if (num_alloc < min_nodes)\n num_alloc = min_nodes;\n if (st->data == NULL) {\n st->data = OPENSSL_zalloc(sizeof(void *) * num_alloc);\n if (st->data == NULL)\n return 0;\n st->num_alloc = num_alloc;\n return 1;\n }\n if (!exact) {\n if (num_alloc <= st->num_alloc)\n return 1;\n num_alloc = compute_growth(num_alloc, st->num_alloc);\n if (num_alloc == 0)\n return 0;\n } else if (num_alloc == st->num_alloc) {\n return 1;\n }\n tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);\n if (tmpdata == NULL)\n return 0;\n st->data = tmpdata;\n st->num_alloc = num_alloc;\n return 1;\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 FAILTEST();\n if (str == NULL)\n return CRYPTO_malloc(num, file, line);\n if (num == 0) {\n CRYPTO_free(str, file, line);\n return NULL;\n }\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n void *ret;\n CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n ret = realloc(str, num);\n CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n return ret;\n }\n#else\n (void)(file); (void)(line);\n#endif\n return realloc(str, num);\n}', '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}']
|
1,290 | 0 |
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L195
|
static void pred4x4_down_left_rv40_c(uint8_t *src, uint8_t *topright, int stride){
LOAD_TOP_EDGE
LOAD_TOP_RIGHT_EDGE
LOAD_LEFT_EDGE
LOAD_DOWN_LEFT_EDGE
src[0+0*stride]=(t0 + t2 + 2*t1 + 2 + l0 + l2 + 2*l1 + 2)>>3;
src[1+0*stride]=
src[0+1*stride]=(t1 + t3 + 2*t2 + 2 + l1 + l3 + 2*l2 + 2)>>3;
src[2+0*stride]=
src[1+1*stride]=
src[0+2*stride]=(t2 + t4 + 2*t3 + 2 + l2 + l4 + 2*l3 + 2)>>3;
src[3+0*stride]=
src[2+1*stride]=
src[1+2*stride]=
src[0+3*stride]=(t3 + t5 + 2*t4 + 2 + l3 + l5 + 2*l4 + 2)>>3;
src[3+1*stride]=
src[2+2*stride]=
src[1+3*stride]=(t4 + t6 + 2*t5 + 2 + l4 + l6 + 2*l5 + 2)>>3;
src[3+2*stride]=
src[2+3*stride]=(t5 + t7 + 2*t6 + 2 + l5 + l7 + 2*l6 + 2)>>3;
src[3+3*stride]=(t6 + t7 + 1 + l6 + l7 + 1)>>2;
}
|
['static void pred4x4_down_left_rv40_c(uint8_t *src, uint8_t *topright, int stride){\n LOAD_TOP_EDGE\n LOAD_TOP_RIGHT_EDGE\n LOAD_LEFT_EDGE\n LOAD_DOWN_LEFT_EDGE\n src[0+0*stride]=(t0 + t2 + 2*t1 + 2 + l0 + l2 + 2*l1 + 2)>>3;\n src[1+0*stride]=\n src[0+1*stride]=(t1 + t3 + 2*t2 + 2 + l1 + l3 + 2*l2 + 2)>>3;\n src[2+0*stride]=\n src[1+1*stride]=\n src[0+2*stride]=(t2 + t4 + 2*t3 + 2 + l2 + l4 + 2*l3 + 2)>>3;\n src[3+0*stride]=\n src[2+1*stride]=\n src[1+2*stride]=\n src[0+3*stride]=(t3 + t5 + 2*t4 + 2 + l3 + l5 + 2*l4 + 2)>>3;\n src[3+1*stride]=\n src[2+2*stride]=\n src[1+3*stride]=(t4 + t6 + 2*t5 + 2 + l4 + l6 + 2*l5 + 2)>>3;\n src[3+2*stride]=\n src[2+3*stride]=(t5 + t7 + 2*t6 + 2 + l5 + l7 + 2*l6 + 2)>>3;\n src[3+3*stride]=(t6 + t7 + 1 + l6 + l7 + 1)>>2;\n}']
|
1,291 | 0 |
https://github.com/openssl/openssl/blob/cf56663fb71ce279eb8ea603faf0a3c98cc7bc47/apps/apps.c/#L1421
|
char *make_config_name()
{
const char *t=X509_get_default_cert_area();
char *p;
p=OPENSSL_malloc(strlen(t)+strlen(OPENSSL_CONF)+2);
strcpy(p,t);
#ifndef OPENSSL_SYS_VMS
strcat(p,"/");
#endif
strcat(p,OPENSSL_CONF);
return p;
}
|
['char *make_config_name()\n\t{\n\tconst char *t=X509_get_default_cert_area();\n\tchar *p;\n\tp=OPENSSL_malloc(strlen(t)+strlen(OPENSSL_CONF)+2);\n\tstrcpy(p,t);\n#ifndef OPENSSL_SYS_VMS\n\tstrcat(p,"/");\n#endif\n\tstrcat(p,OPENSSL_CONF);\n\treturn p;\n\t}', 'const char *X509_get_default_cert_area(void)\n\t{ return(X509_CERT_AREA); }', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\textern unsigned char cleanse_ctr;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n if(ret && (num > 2048))\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\treturn ret;\n\t}']
|
1,292 | 0 |
https://github.com/libav/libav/blob/7f1b427018ecff59e0e14031eecc79aac0d91ec8/libavcodec/snow.c/#L354
|
void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, int stride, int sx, int sy, int b_w, int b_h, BlockNode *block, int plane_index, int w, int h){
if(block->type & BLOCK_INTRA){
int x, y;
const int color = block->color[plane_index];
const int color4= color*0x01010101;
if(b_w==32){
for(y=0; y < b_h; y++){
*(uint32_t*)&dst[0 + y*stride]= color4;
*(uint32_t*)&dst[4 + y*stride]= color4;
*(uint32_t*)&dst[8 + y*stride]= color4;
*(uint32_t*)&dst[12+ y*stride]= color4;
*(uint32_t*)&dst[16+ y*stride]= color4;
*(uint32_t*)&dst[20+ y*stride]= color4;
*(uint32_t*)&dst[24+ y*stride]= color4;
*(uint32_t*)&dst[28+ y*stride]= color4;
}
}else if(b_w==16){
for(y=0; y < b_h; y++){
*(uint32_t*)&dst[0 + y*stride]= color4;
*(uint32_t*)&dst[4 + y*stride]= color4;
*(uint32_t*)&dst[8 + y*stride]= color4;
*(uint32_t*)&dst[12+ y*stride]= color4;
}
}else if(b_w==8){
for(y=0; y < b_h; y++){
*(uint32_t*)&dst[0 + y*stride]= color4;
*(uint32_t*)&dst[4 + y*stride]= color4;
}
}else if(b_w==4){
for(y=0; y < b_h; y++){
*(uint32_t*)&dst[0 + y*stride]= color4;
}
}else{
for(y=0; y < b_h; y++){
for(x=0; x < b_w; x++){
dst[x + y*stride]= color;
}
}
}
}else{
uint8_t *src= s->last_picture[block->ref].data[plane_index];
const int scale= plane_index ? s->mv_scale : 2*s->mv_scale;
int mx= block->mx*scale;
int my= block->my*scale;
const int dx= mx&15;
const int dy= my&15;
const int tab_index= 3 - (b_w>>2) + (b_w>>4);
sx += (mx>>4) - (HTAPS_MAX/2-1);
sy += (my>>4) - (HTAPS_MAX/2-1);
src += sx + sy*stride;
if( (unsigned)sx >= w - b_w - (HTAPS_MAX-2)
|| (unsigned)sy >= h - b_h - (HTAPS_MAX-2)){
s->dsp.emulated_edge_mc(tmp + MB_SIZE, src, stride, b_w+HTAPS_MAX-1, b_h+HTAPS_MAX-1, sx, sy, w, h);
src= tmp + MB_SIZE;
}
assert(b_w>1 && b_h>1);
assert((tab_index>=0 && tab_index<4) || b_w==32);
if((dx&3) || (dy&3) || !(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h) || (b_w&(b_w-1)) || !s->plane[plane_index].fast_mc )
mc_block(&s->plane[plane_index], dst, src, stride, b_w, b_h, dx, dy);
else if(b_w==32){
int y;
for(y=0; y<b_h; y+=16){
s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + y*stride, src + 3 + (y+3)*stride,stride);
s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + 16 + y*stride, src + 19 + (y+3)*stride,stride);
}
}else if(b_w==b_h)
s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst,src + 3 + 3*stride,stride);
else if(b_w==2*b_h){
s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst ,src + 3 + 3*stride,stride);
s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst+b_h,src + 3 + b_h + 3*stride,stride);
}else{
assert(2*b_w==b_h);
s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst ,src + 3 + 3*stride ,stride);
s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst+b_w*stride,src + 3 + 3*stride+b_w*stride,stride);
}
}
}
|
['void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, int stride, int sx, int sy, int b_w, int b_h, BlockNode *block, int plane_index, int w, int h){\n if(block->type & BLOCK_INTRA){\n int x, y;\n const int color = block->color[plane_index];\n const int color4= color*0x01010101;\n if(b_w==32){\n for(y=0; y < b_h; y++){\n *(uint32_t*)&dst[0 + y*stride]= color4;\n *(uint32_t*)&dst[4 + y*stride]= color4;\n *(uint32_t*)&dst[8 + y*stride]= color4;\n *(uint32_t*)&dst[12+ y*stride]= color4;\n *(uint32_t*)&dst[16+ y*stride]= color4;\n *(uint32_t*)&dst[20+ y*stride]= color4;\n *(uint32_t*)&dst[24+ y*stride]= color4;\n *(uint32_t*)&dst[28+ y*stride]= color4;\n }\n }else if(b_w==16){\n for(y=0; y < b_h; y++){\n *(uint32_t*)&dst[0 + y*stride]= color4;\n *(uint32_t*)&dst[4 + y*stride]= color4;\n *(uint32_t*)&dst[8 + y*stride]= color4;\n *(uint32_t*)&dst[12+ y*stride]= color4;\n }\n }else if(b_w==8){\n for(y=0; y < b_h; y++){\n *(uint32_t*)&dst[0 + y*stride]= color4;\n *(uint32_t*)&dst[4 + y*stride]= color4;\n }\n }else if(b_w==4){\n for(y=0; y < b_h; y++){\n *(uint32_t*)&dst[0 + y*stride]= color4;\n }\n }else{\n for(y=0; y < b_h; y++){\n for(x=0; x < b_w; x++){\n dst[x + y*stride]= color;\n }\n }\n }\n }else{\n uint8_t *src= s->last_picture[block->ref].data[plane_index];\n const int scale= plane_index ? s->mv_scale : 2*s->mv_scale;\n int mx= block->mx*scale;\n int my= block->my*scale;\n const int dx= mx&15;\n const int dy= my&15;\n const int tab_index= 3 - (b_w>>2) + (b_w>>4);\n sx += (mx>>4) - (HTAPS_MAX/2-1);\n sy += (my>>4) - (HTAPS_MAX/2-1);\n src += sx + sy*stride;\n if( (unsigned)sx >= w - b_w - (HTAPS_MAX-2)\n || (unsigned)sy >= h - b_h - (HTAPS_MAX-2)){\n s->dsp.emulated_edge_mc(tmp + MB_SIZE, src, stride, b_w+HTAPS_MAX-1, b_h+HTAPS_MAX-1, sx, sy, w, h);\n src= tmp + MB_SIZE;\n }\n assert(b_w>1 && b_h>1);\n assert((tab_index>=0 && tab_index<4) || b_w==32);\n if((dx&3) || (dy&3) || !(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h) || (b_w&(b_w-1)) || !s->plane[plane_index].fast_mc )\n mc_block(&s->plane[plane_index], dst, src, stride, b_w, b_h, dx, dy);\n else if(b_w==32){\n int y;\n for(y=0; y<b_h; y+=16){\n s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + y*stride, src + 3 + (y+3)*stride,stride);\n s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + 16 + y*stride, src + 19 + (y+3)*stride,stride);\n }\n }else if(b_w==b_h)\n s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst,src + 3 + 3*stride,stride);\n else if(b_w==2*b_h){\n s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst ,src + 3 + 3*stride,stride);\n s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst+b_h,src + 3 + b_h + 3*stride,stride);\n }else{\n assert(2*b_w==b_h);\n s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst ,src + 3 + 3*stride ,stride);\n s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst+b_w*stride,src + 3 + 3*stride+b_w*stride,stride);\n }\n }\n}']
|
1,293 | 0 |
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/error_resilience.c/#L389
|
static void guess_mv(MpegEncContext *s){
uint8_t fixed[s->mb_stride * s->mb_height];
#define MV_FROZEN 3
#define MV_CHANGED 2
#define MV_UNCHANGED 1
const int mb_stride = s->mb_stride;
const int mb_width = s->mb_width;
const int mb_height= s->mb_height;
int i, depth, num_avail;
int mb_x, mb_y;
num_avail=0;
for(i=0; i<s->mb_num; i++){
const int mb_xy= s->mb_index2xy[ i ];
int f=0;
int error= s->error_status_table[mb_xy];
if(IS_INTRA(s->current_picture.mb_type[mb_xy])) f=MV_FROZEN;
if(!(error&MV_ERROR)) f=MV_FROZEN;
fixed[mb_xy]= f;
if(f==MV_FROZEN)
num_avail++;
}
if((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) || num_avail <= mb_width/2){
for(mb_y=0; mb_y<s->mb_height; mb_y++){
for(mb_x=0; mb_x<s->mb_width; mb_x++){
const int mb_xy= mb_x + mb_y*s->mb_stride;
if(IS_INTRA(s->current_picture.mb_type[mb_xy])) continue;
if(!(s->error_status_table[mb_xy]&MV_ERROR)) continue;
s->mv_dir = MV_DIR_FORWARD;
s->mb_intra=0;
s->mv_type = MV_TYPE_16X16;
s->mb_skipped=0;
s->dsp.clear_blocks(s->block[0]);
s->mb_x= mb_x;
s->mb_y= mb_y;
s->mv[0][0][0]= 0;
s->mv[0][0][1]= 0;
decode_mb(s);
}
}
return;
}
for(depth=0;; depth++){
int changed, pass, none_left;
none_left=1;
changed=1;
for(pass=0; (changed || pass<2) && pass<10; pass++){
int mb_x, mb_y;
int score_sum=0;
changed=0;
for(mb_y=0; mb_y<s->mb_height; mb_y++){
for(mb_x=0; mb_x<s->mb_width; mb_x++){
const int mb_xy= mb_x + mb_y*s->mb_stride;
int mv_predictor[8][2]={{0}};
int pred_count=0;
int j;
int best_score=256*256*256*64;
int best_pred=0;
const int mot_stride= s->b8_stride;
const int mot_index= mb_x*2 + mb_y*2*mot_stride;
int prev_x= s->current_picture.motion_val[0][mot_index][0];
int prev_y= s->current_picture.motion_val[0][mot_index][1];
if((mb_x^mb_y^pass)&1) continue;
if(fixed[mb_xy]==MV_FROZEN) continue;
assert(!IS_INTRA(s->current_picture.mb_type[mb_xy]));
assert(s->last_picture_ptr && s->last_picture_ptr->data[0]);
j=0;
if(mb_x>0 && fixed[mb_xy-1 ]==MV_FROZEN) j=1;
if(mb_x+1<mb_width && fixed[mb_xy+1 ]==MV_FROZEN) j=1;
if(mb_y>0 && fixed[mb_xy-mb_stride]==MV_FROZEN) j=1;
if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_FROZEN) j=1;
if(j==0) continue;
j=0;
if(mb_x>0 && fixed[mb_xy-1 ]==MV_CHANGED) j=1;
if(mb_x+1<mb_width && fixed[mb_xy+1 ]==MV_CHANGED) j=1;
if(mb_y>0 && fixed[mb_xy-mb_stride]==MV_CHANGED) j=1;
if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_CHANGED) j=1;
if(j==0 && pass>1) continue;
none_left=0;
if(mb_x>0 && fixed[mb_xy-1]){
mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - 2][0];
mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - 2][1];
pred_count++;
}
if(mb_x+1<mb_width && fixed[mb_xy+1]){
mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + 2][0];
mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + 2][1];
pred_count++;
}
if(mb_y>0 && fixed[mb_xy-mb_stride]){
mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - mot_stride*2][0];
mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - mot_stride*2][1];
pred_count++;
}
if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + mot_stride*2][0];
mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + mot_stride*2][1];
pred_count++;
}
if(pred_count==0) continue;
if(pred_count>1){
int sum_x=0, sum_y=0;
int max_x, max_y, min_x, min_y;
for(j=0; j<pred_count; j++){
sum_x+= mv_predictor[j][0];
sum_y+= mv_predictor[j][1];
}
mv_predictor[pred_count][0] = sum_x/j;
mv_predictor[pred_count][1] = sum_y/j;
if(pred_count>=3){
min_y= min_x= 99999;
max_y= max_x=-99999;
}else{
min_x=min_y=max_x=max_y=0;
}
for(j=0; j<pred_count; j++){
max_x= FFMAX(max_x, mv_predictor[j][0]);
max_y= FFMAX(max_y, mv_predictor[j][1]);
min_x= FFMIN(min_x, mv_predictor[j][0]);
min_y= FFMIN(min_y, mv_predictor[j][1]);
}
mv_predictor[pred_count+1][0] = sum_x - max_x - min_x;
mv_predictor[pred_count+1][1] = sum_y - max_y - min_y;
if(pred_count==4){
mv_predictor[pred_count+1][0] /= 2;
mv_predictor[pred_count+1][1] /= 2;
}
pred_count+=2;
}
pred_count++;
mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index][0];
mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index][1];
pred_count++;
s->mv_dir = MV_DIR_FORWARD;
s->mb_intra=0;
s->mv_type = MV_TYPE_16X16;
s->mb_skipped=0;
s->dsp.clear_blocks(s->block[0]);
s->mb_x= mb_x;
s->mb_y= mb_y;
for(j=0; j<pred_count; j++){
int score=0;
uint8_t *src= s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[j][0];
s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[j][1];
decode_mb(s);
if(mb_x>0 && fixed[mb_xy-1]){
int k;
for(k=0; k<16; k++)
score += FFABS(src[k*s->linesize-1 ]-src[k*s->linesize ]);
}
if(mb_x+1<mb_width && fixed[mb_xy+1]){
int k;
for(k=0; k<16; k++)
score += FFABS(src[k*s->linesize+15]-src[k*s->linesize+16]);
}
if(mb_y>0 && fixed[mb_xy-mb_stride]){
int k;
for(k=0; k<16; k++)
score += FFABS(src[k-s->linesize ]-src[k ]);
}
if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
int k;
for(k=0; k<16; k++)
score += FFABS(src[k+s->linesize*15]-src[k+s->linesize*16]);
}
if(score <= best_score){
best_score= score;
best_pred= j;
}
}
score_sum+= best_score;
s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[best_pred][0];
s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[best_pred][1];
decode_mb(s);
if(s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y){
fixed[mb_xy]=MV_CHANGED;
changed++;
}else
fixed[mb_xy]=MV_UNCHANGED;
}
}
}
if(none_left)
return;
for(i=0; i<s->mb_num; i++){
int mb_xy= s->mb_index2xy[i];
if(fixed[mb_xy])
fixed[mb_xy]=MV_FROZEN;
}
}
}
|
['static void guess_mv(MpegEncContext *s){\n uint8_t fixed[s->mb_stride * s->mb_height];\n#define MV_FROZEN 3\n#define MV_CHANGED 2\n#define MV_UNCHANGED 1\n const int mb_stride = s->mb_stride;\n const int mb_width = s->mb_width;\n const int mb_height= s->mb_height;\n int i, depth, num_avail;\n int mb_x, mb_y;\n num_avail=0;\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[ i ];\n int f=0;\n int error= s->error_status_table[mb_xy];\n if(IS_INTRA(s->current_picture.mb_type[mb_xy])) f=MV_FROZEN;\n if(!(error&MV_ERROR)) f=MV_FROZEN;\n fixed[mb_xy]= f;\n if(f==MV_FROZEN)\n num_avail++;\n }\n if((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) || num_avail <= mb_width/2){\n for(mb_y=0; mb_y<s->mb_height; mb_y++){\n for(mb_x=0; mb_x<s->mb_width; mb_x++){\n const int mb_xy= mb_x + mb_y*s->mb_stride;\n if(IS_INTRA(s->current_picture.mb_type[mb_xy])) continue;\n if(!(s->error_status_table[mb_xy]&MV_ERROR)) continue;\n s->mv_dir = MV_DIR_FORWARD;\n s->mb_intra=0;\n s->mv_type = MV_TYPE_16X16;\n s->mb_skipped=0;\n s->dsp.clear_blocks(s->block[0]);\n s->mb_x= mb_x;\n s->mb_y= mb_y;\n s->mv[0][0][0]= 0;\n s->mv[0][0][1]= 0;\n decode_mb(s);\n }\n }\n return;\n }\n for(depth=0;; depth++){\n int changed, pass, none_left;\n none_left=1;\n changed=1;\n for(pass=0; (changed || pass<2) && pass<10; pass++){\n int mb_x, mb_y;\nint score_sum=0;\n changed=0;\n for(mb_y=0; mb_y<s->mb_height; mb_y++){\n for(mb_x=0; mb_x<s->mb_width; mb_x++){\n const int mb_xy= mb_x + mb_y*s->mb_stride;\n int mv_predictor[8][2]={{0}};\n int pred_count=0;\n int j;\n int best_score=256*256*256*64;\n int best_pred=0;\n const int mot_stride= s->b8_stride;\n const int mot_index= mb_x*2 + mb_y*2*mot_stride;\n int prev_x= s->current_picture.motion_val[0][mot_index][0];\n int prev_y= s->current_picture.motion_val[0][mot_index][1];\n if((mb_x^mb_y^pass)&1) continue;\n if(fixed[mb_xy]==MV_FROZEN) continue;\n assert(!IS_INTRA(s->current_picture.mb_type[mb_xy]));\n assert(s->last_picture_ptr && s->last_picture_ptr->data[0]);\n j=0;\n if(mb_x>0 && fixed[mb_xy-1 ]==MV_FROZEN) j=1;\n if(mb_x+1<mb_width && fixed[mb_xy+1 ]==MV_FROZEN) j=1;\n if(mb_y>0 && fixed[mb_xy-mb_stride]==MV_FROZEN) j=1;\n if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_FROZEN) j=1;\n if(j==0) continue;\n j=0;\n if(mb_x>0 && fixed[mb_xy-1 ]==MV_CHANGED) j=1;\n if(mb_x+1<mb_width && fixed[mb_xy+1 ]==MV_CHANGED) j=1;\n if(mb_y>0 && fixed[mb_xy-mb_stride]==MV_CHANGED) j=1;\n if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_CHANGED) j=1;\n if(j==0 && pass>1) continue;\n none_left=0;\n if(mb_x>0 && fixed[mb_xy-1]){\n mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - 2][0];\n mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - 2][1];\n pred_count++;\n }\n if(mb_x+1<mb_width && fixed[mb_xy+1]){\n mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + 2][0];\n mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + 2][1];\n pred_count++;\n }\n if(mb_y>0 && fixed[mb_xy-mb_stride]){\n mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - mot_stride*2][0];\n mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - mot_stride*2][1];\n pred_count++;\n }\n if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){\n mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + mot_stride*2][0];\n mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + mot_stride*2][1];\n pred_count++;\n }\n if(pred_count==0) continue;\n if(pred_count>1){\n int sum_x=0, sum_y=0;\n int max_x, max_y, min_x, min_y;\n for(j=0; j<pred_count; j++){\n sum_x+= mv_predictor[j][0];\n sum_y+= mv_predictor[j][1];\n }\n mv_predictor[pred_count][0] = sum_x/j;\n mv_predictor[pred_count][1] = sum_y/j;\n if(pred_count>=3){\n min_y= min_x= 99999;\n max_y= max_x=-99999;\n }else{\n min_x=min_y=max_x=max_y=0;\n }\n for(j=0; j<pred_count; j++){\n max_x= FFMAX(max_x, mv_predictor[j][0]);\n max_y= FFMAX(max_y, mv_predictor[j][1]);\n min_x= FFMIN(min_x, mv_predictor[j][0]);\n min_y= FFMIN(min_y, mv_predictor[j][1]);\n }\n mv_predictor[pred_count+1][0] = sum_x - max_x - min_x;\n mv_predictor[pred_count+1][1] = sum_y - max_y - min_y;\n if(pred_count==4){\n mv_predictor[pred_count+1][0] /= 2;\n mv_predictor[pred_count+1][1] /= 2;\n }\n pred_count+=2;\n }\n pred_count++;\n mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index][0];\n mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index][1];\n pred_count++;\n s->mv_dir = MV_DIR_FORWARD;\n s->mb_intra=0;\n s->mv_type = MV_TYPE_16X16;\n s->mb_skipped=0;\n s->dsp.clear_blocks(s->block[0]);\n s->mb_x= mb_x;\n s->mb_y= mb_y;\n for(j=0; j<pred_count; j++){\n int score=0;\n uint8_t *src= s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;\n s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[j][0];\n s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[j][1];\n decode_mb(s);\n if(mb_x>0 && fixed[mb_xy-1]){\n int k;\n for(k=0; k<16; k++)\n score += FFABS(src[k*s->linesize-1 ]-src[k*s->linesize ]);\n }\n if(mb_x+1<mb_width && fixed[mb_xy+1]){\n int k;\n for(k=0; k<16; k++)\n score += FFABS(src[k*s->linesize+15]-src[k*s->linesize+16]);\n }\n if(mb_y>0 && fixed[mb_xy-mb_stride]){\n int k;\n for(k=0; k<16; k++)\n score += FFABS(src[k-s->linesize ]-src[k ]);\n }\n if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){\n int k;\n for(k=0; k<16; k++)\n score += FFABS(src[k+s->linesize*15]-src[k+s->linesize*16]);\n }\n if(score <= best_score){\n best_score= score;\n best_pred= j;\n }\n }\nscore_sum+= best_score;\n s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[best_pred][0];\n s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[best_pred][1];\n decode_mb(s);\n if(s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y){\n fixed[mb_xy]=MV_CHANGED;\n changed++;\n }else\n fixed[mb_xy]=MV_UNCHANGED;\n }\n }\n }\n if(none_left)\n return;\n for(i=0; i<s->mb_num; i++){\n int mb_xy= s->mb_index2xy[i];\n if(fixed[mb_xy])\n fixed[mb_xy]=MV_FROZEN;\n }\n }\n}']
|
1,294 | 0 |
https://github.com/openssl/openssl/blob/4b8515baa6edef1a771f9e4e3fbc0395b4a629e8/crypto/bn/bn_ctx.c/#L273
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['static int test_kronecker()\n{\n BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;\n int i, legendre, kronecker, st = 0;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_ptr(r = BN_new())\n || !TEST_ptr(t = BN_new()))\n goto err;\n if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))\n goto err;\n b->neg = rand_neg();\n for (i = 0; i < NUM0; i++) {\n if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))\n goto err;\n a->neg = rand_neg();\n if (!TEST_true(BN_copy(t, b)))\n goto err;\n t->neg = 0;\n if (!TEST_true(BN_sub_word(t, 1)))\n goto err;\n if (!TEST_true(BN_rshift1(t, t)))\n goto err;\n b->neg = 0;\n if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))\n goto err;\n b->neg = 1;\n if (BN_is_word(r, 1))\n legendre = 1;\n else if (BN_is_zero(r))\n legendre = 0;\n else {\n if (!TEST_true(BN_add_word(r, 1)))\n goto err;\n if (!TEST_int_eq(BN_ucmp(r, b), 0)) {\n TEST_info("Legendre symbol computation failed");\n goto err;\n }\n legendre = -1;\n }\n if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))\n goto err;\n if (a->neg && b->neg)\n kronecker = -kronecker;\n if (!TEST_int_eq(legendre, kronecker))\n goto err;\n }\n st = 1;\n err:\n BN_free(a);\n BN_free(b);\n BN_free(r);\n BN_free(t);\n return st;\n}', 'int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int i;\n int ret = -2;\n int err = 0;\n BIGNUM *A, *B, *tmp;\n static const int tab[8] = { 0, 1, 0, -1, 0, -1, 0, 1 };\n bn_check_top(a);\n bn_check_top(b);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n if (B == NULL)\n goto end;\n err = !BN_copy(A, a);\n if (err)\n goto end;\n err = !BN_copy(B, b);\n if (err)\n goto end;\n if (BN_is_zero(B)) {\n ret = BN_abs_is_word(A, 1);\n goto end;\n }\n if (!BN_is_odd(A) && !BN_is_odd(B)) {\n ret = 0;\n goto end;\n }\n i = 0;\n while (!BN_is_bit_set(B, i))\n i++;\n err = !BN_rshift(B, B, i);\n if (err)\n goto end;\n if (i & 1) {\n ret = tab[BN_lsw(A) & 7];\n } else {\n ret = 1;\n }\n if (B->neg) {\n B->neg = 0;\n if (A->neg)\n ret = -ret;\n }\n while (1) {\n if (BN_is_zero(A)) {\n ret = BN_is_one(B) ? ret : 0;\n goto end;\n }\n i = 0;\n while (!BN_is_bit_set(A, i))\n i++;\n err = !BN_rshift(A, A, i);\n if (err)\n goto end;\n if (i & 1) {\n ret = ret * tab[BN_lsw(B) & 7];\n }\n if ((A->neg ? ~BN_lsw(A) : BN_lsw(A)) & BN_lsw(B) & 2)\n ret = -ret;\n err = !BN_nnmod(B, B, A, ctx);\n if (err)\n goto end;\n tmp = A;\n A = B;\n B = tmp;\n tmp->neg = 0;\n }\n end:\n BN_CTX_end(ctx);\n if (err)\n return -2;\n else\n return ret;\n}', '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}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_RECP_CTX_free(&recp);\n bn_check_top(r);\n return (ret);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
1,295 | 0 |
https://github.com/openssl/openssl/blob/daea0ff8a960237e0f9a71301721824c25ad3b25/crypto/asn1/tasn_dec.c/#L94
|
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it)
{
ASN1_TLC c;
ASN1_VALUE *ptmpval = NULL;
if(!pval) pval = &ptmpval;
asn1_tlc_clear(&c);
if(ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
return *pval;
return NULL;
}
|
['ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it)\n{\n\tASN1_TLC c;\n\tASN1_VALUE *ptmpval = NULL;\n\tif(!pval) pval = &ptmpval;\n\tasn1_tlc_clear(&c);\n\tif(ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)\n\t\treturn *pval;\n\treturn NULL;\n}']
|
1,296 | 0 |
https://github.com/openssl/openssl/blob/a4af39ac4482355ffdd61fb61231a0c79b96997b/crypto/pkcs12/p12_npas.c/#L136
|
static int newpass_p12(PKCS12 *p12, char *oldpass, char *newpass)
{
STACK *asafes, *newsafes, *bags;
int i, bagnid, pbe_nid, pbe_iter, pbe_saltlen;
PKCS7 *p7, *p7new;
ASN1_OCTET_STRING *p12_data_tmp = NULL, *macnew = NULL;
unsigned char mac[EVP_MAX_MD_SIZE];
unsigned int maclen;
if (!(asafes = M_PKCS12_unpack_authsafes(p12))) return 0;
if(!(newsafes = sk_new(NULL))) return 0;
for (i = 0; i < sk_num (asafes); i++) {
p7 = (PKCS7 *) sk_value(asafes, i);
bagnid = OBJ_obj2nid(p7->type);
if (bagnid == NID_pkcs7_data) {
bags = M_PKCS12_unpack_p7data(p7);
} else if (bagnid == NID_pkcs7_encrypted) {
bags = M_PKCS12_unpack_p7encdata(p7, oldpass, -1);
alg_get(p7->d.encrypted->enc_data->algorithm,
&pbe_nid, &pbe_iter, &pbe_saltlen);
} else continue;
if (!bags) {
sk_pop_free(asafes, PKCS7_free);
return 0;
}
if (!newpass_bags(bags, oldpass, newpass)) {
sk_pop_free(bags, PKCS12_SAFEBAG_free);
sk_pop_free(asafes, PKCS7_free);
return 0;
}
if (bagnid == NID_pkcs7_data) p7new = PKCS12_pack_p7data(bags);
else p7new = PKCS12_pack_p7encdata(pbe_nid, newpass, -1, NULL,
pbe_saltlen, pbe_iter, bags);
sk_pop_free(bags, PKCS12_SAFEBAG_free);
if(!p7new) {
sk_pop_free(asafes, PKCS7_free);
return 0;
}
sk_push(newsafes, (char *)p7new);
}
sk_pop_free(asafes, PKCS7_free);
p12_data_tmp = p12->authsafes->d.data;
if(!(p12->authsafes->d.data = ASN1_OCTET_STRING_new())) goto saferr;
if(!M_PKCS12_pack_authsafes(p12, newsafes)) goto saferr;
if(!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen)) goto saferr;
if(!(macnew = ASN1_OCTET_STRING_new())) goto saferr;
if(!ASN1_OCTET_STRING_set(macnew, mac, maclen)) goto saferr;
ASN1_OCTET_STRING_free(p12->mac->dinfo->digest);
p12->mac->dinfo->digest = macnew;
ASN1_OCTET_STRING_free(p12_data_tmp);
return 1;
saferr:
ASN1_OCTET_STRING_free(p12->authsafes->d.data);
ASN1_OCTET_STRING_free(macnew);
p12->authsafes->d.data = p12_data_tmp;
return 0;
}
|
['static int newpass_p12(PKCS12 *p12, char *oldpass, char *newpass)\n{\n\tSTACK *asafes, *newsafes, *bags;\n\tint i, bagnid, pbe_nid, pbe_iter, pbe_saltlen;\n\tPKCS7 *p7, *p7new;\n\tASN1_OCTET_STRING *p12_data_tmp = NULL, *macnew = NULL;\n\tunsigned char mac[EVP_MAX_MD_SIZE];\n\tunsigned int maclen;\n\tif (!(asafes = M_PKCS12_unpack_authsafes(p12))) return 0;\n\tif(!(newsafes = sk_new(NULL))) return 0;\n\tfor (i = 0; i < sk_num (asafes); i++) {\n\t\tp7 = (PKCS7 *) sk_value(asafes, i);\n\t\tbagnid = OBJ_obj2nid(p7->type);\n\t\tif (bagnid == NID_pkcs7_data) {\n\t\t\tbags = M_PKCS12_unpack_p7data(p7);\n\t\t} else if (bagnid == NID_pkcs7_encrypted) {\n\t\t\tbags = M_PKCS12_unpack_p7encdata(p7, oldpass, -1);\n\t\t\talg_get(p7->d.encrypted->enc_data->algorithm,\n\t\t\t\t&pbe_nid, &pbe_iter, &pbe_saltlen);\n\t\t} else continue;\n\t\tif (!bags) {\n\t\t\tsk_pop_free(asafes, PKCS7_free);\n\t\t\treturn 0;\n\t\t}\n\t \tif (!newpass_bags(bags, oldpass, newpass)) {\n\t\t\tsk_pop_free(bags, PKCS12_SAFEBAG_free);\n\t\t\tsk_pop_free(asafes, PKCS7_free);\n\t\t\treturn 0;\n\t\t}\n\t\tif (bagnid == NID_pkcs7_data) p7new = PKCS12_pack_p7data(bags);\n\t\telse p7new = PKCS12_pack_p7encdata(pbe_nid, newpass, -1, NULL,\n\t\t\t\t\t\t pbe_saltlen, pbe_iter, bags);\n\t\tsk_pop_free(bags, PKCS12_SAFEBAG_free);\n\t\tif(!p7new) {\n\t\t\tsk_pop_free(asafes, PKCS7_free);\n\t\t\treturn 0;\n\t\t}\n\t\tsk_push(newsafes, (char *)p7new);\n\t}\n\tsk_pop_free(asafes, PKCS7_free);\n\tp12_data_tmp = p12->authsafes->d.data;\n\tif(!(p12->authsafes->d.data = ASN1_OCTET_STRING_new())) goto saferr;\n\tif(!M_PKCS12_pack_authsafes(p12, newsafes)) goto saferr;\n\tif(!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen)) goto saferr;\n\tif(!(macnew = ASN1_OCTET_STRING_new())) goto saferr;\n\tif(!ASN1_OCTET_STRING_set(macnew, mac, maclen)) goto saferr;\n\tASN1_OCTET_STRING_free(p12->mac->dinfo->digest);\n\tp12->mac->dinfo->digest = macnew;\n\tASN1_OCTET_STRING_free(p12_data_tmp);\n\treturn 1;\n\tsaferr:\n\tASN1_OCTET_STRING_free(p12->authsafes->d.data);\n\tASN1_OCTET_STRING_free(macnew);\n\tp12->authsafes->d.data = p12_data_tmp;\n\treturn 0;\n}']
|
1,297 | 0 |
https://github.com/nginx/nginx/blob/1c906828aee64d8ac7eb4df57f9134e27e709a3d/src/core/ngx_string.c/#L938
|
ngx_int_t
ngx_atoi(u_char *line, size_t n)
{
ngx_int_t value, cutoff, cutlim;
if (n == 0) {
return NGX_ERROR;
}
cutoff = NGX_MAX_INT_T_VALUE / 10;
cutlim = NGX_MAX_INT_T_VALUE % 10;
for (value = 0; n--; line++) {
if (*line < '0' || *line > '9') {
return NGX_ERROR;
}
if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {
return NGX_ERROR;
}
value = value * 10 + (*line - '0');
}
return value;
}
|
['static char *\nngx_http_scgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)\n{\n ngx_http_scgi_loc_conf_t *scf = conf;\n ngx_url_t u;\n ngx_str_t *value, *url;\n ngx_uint_t n;\n ngx_http_core_loc_conf_t *clcf;\n ngx_http_script_compile_t sc;\n if (scf->upstream.upstream || scf->scgi_lengths) {\n return "is duplicate";\n }\n clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);\n clcf->handler = ngx_http_scgi_handler;\n value = cf->args->elts;\n url = &value[1];\n n = ngx_http_script_variables_count(url);\n if (n) {\n ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));\n sc.cf = cf;\n sc.source = url;\n sc.lengths = &scf->scgi_lengths;\n sc.values = &scf->scgi_values;\n sc.variables = n;\n sc.complete_lengths = 1;\n sc.complete_values = 1;\n if (ngx_http_script_compile(&sc) != NGX_OK) {\n return NGX_CONF_ERROR;\n }\n return NGX_CONF_OK;\n }\n ngx_memzero(&u, sizeof(ngx_url_t));\n u.url = value[1];\n u.no_resolve = 1;\n scf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);\n if (scf->upstream.upstream == NULL) {\n return NGX_CONF_ERROR;\n }\n if (clcf->name.len && clcf->name.data[clcf->name.len - 1] == \'/\') {\n clcf->auto_redirect = 1;\n }\n return NGX_CONF_OK;\n}', 'ngx_http_upstream_srv_conf_t *\nngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags)\n{\n ngx_uint_t i;\n ngx_http_upstream_server_t *us;\n ngx_http_upstream_srv_conf_t *uscf, **uscfp;\n ngx_http_upstream_main_conf_t *umcf;\n if (!(flags & NGX_HTTP_UPSTREAM_CREATE)) {\n if (ngx_parse_url(cf->pool, u) != NGX_OK) {\n if (u->err) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "%s in upstream \\"%V\\"", u->err, &u->url);\n }\n return NULL;\n }\n }\n umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module);\n uscfp = umcf->upstreams.elts;\n for (i = 0; i < umcf->upstreams.nelts; i++) {\n if (uscfp[i]->host.len != u->host.len\n || ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len)\n != 0)\n {\n continue;\n }\n if ((flags & NGX_HTTP_UPSTREAM_CREATE)\n && (uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE))\n {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "duplicate upstream \\"%V\\"", &u->host);\n return NULL;\n }\n if ((uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE) && !u->no_port) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "upstream \\"%V\\" may not have port %d",\n &u->host, u->port);\n return NULL;\n }\n if ((flags & NGX_HTTP_UPSTREAM_CREATE) && !uscfp[i]->no_port) {\n ngx_log_error(NGX_LOG_EMERG, cf->log, 0,\n "upstream \\"%V\\" may not have port %d in %s:%ui",\n &u->host, uscfp[i]->port,\n uscfp[i]->file_name, uscfp[i]->line);\n return NULL;\n }\n if (uscfp[i]->port && u->port\n && uscfp[i]->port != u->port)\n {\n continue;\n }\n if (flags & NGX_HTTP_UPSTREAM_CREATE) {\n uscfp[i]->flags = flags;\n uscfp[i]->port = 0;\n }\n return uscfp[i];\n }\n uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t));\n if (uscf == NULL) {\n return NULL;\n }\n uscf->flags = flags;\n uscf->host = u->host;\n uscf->file_name = cf->conf_file->file.name.data;\n uscf->line = cf->conf_file->line;\n uscf->port = u->port;\n uscf->no_port = u->no_port;\n if (u->naddrs == 1 && (u->port || u->family == AF_UNIX)) {\n uscf->servers = ngx_array_create(cf->pool, 1,\n sizeof(ngx_http_upstream_server_t));\n if (uscf->servers == NULL) {\n return NULL;\n }\n us = ngx_array_push(uscf->servers);\n if (us == NULL) {\n return NULL;\n }\n ngx_memzero(us, sizeof(ngx_http_upstream_server_t));\n us->addrs = u->addrs;\n us->naddrs = 1;\n }\n uscfp = ngx_array_push(&umcf->upstreams);\n if (uscfp == NULL) {\n return NULL;\n }\n *uscfp = uscf;\n return uscf;\n}', 'ngx_int_t\nngx_parse_url(ngx_pool_t *pool, ngx_url_t *u)\n{\n u_char *p;\n size_t len;\n p = u->url.data;\n len = u->url.len;\n if (len >= 5 && ngx_strncasecmp(p, (u_char *) "unix:", 5) == 0) {\n return ngx_parse_unix_domain_url(pool, u);\n }\n if (len && p[0] == \'[\') {\n return ngx_parse_inet6_url(pool, u);\n }\n return ngx_parse_inet_url(pool, u);\n}', 'static ngx_int_t\nngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u)\n{\n u_char *host, *port, *last, *uri, *args, *dash;\n size_t len;\n ngx_int_t n;\n struct sockaddr_in *sin;\n u->socklen = sizeof(struct sockaddr_in);\n sin = (struct sockaddr_in *) &u->sockaddr;\n sin->sin_family = AF_INET;\n u->family = AF_INET;\n host = u->url.data;\n last = host + u->url.len;\n port = ngx_strlchr(host, last, \':\');\n uri = ngx_strlchr(host, last, \'/\');\n args = ngx_strlchr(host, last, \'?\');\n if (args) {\n if (uri == NULL || args < uri) {\n uri = args;\n }\n }\n if (uri) {\n if (u->listen || !u->uri_part) {\n u->err = "invalid host";\n return NGX_ERROR;\n }\n u->uri.len = last - uri;\n u->uri.data = uri;\n last = uri;\n if (uri < port) {\n port = NULL;\n }\n }\n if (port) {\n port++;\n len = last - port;\n if (u->listen) {\n dash = ngx_strlchr(port, last, \'-\');\n if (dash) {\n dash++;\n n = ngx_atoi(dash, last - dash);\n if (n < 1 || n > 65535) {\n u->err = "invalid port";\n return NGX_ERROR;\n }\n u->last_port = (in_port_t) n;\n len = dash - port - 1;\n }\n }\n n = ngx_atoi(port, len);\n if (n < 1 || n > 65535) {\n u->err = "invalid port";\n return NGX_ERROR;\n }\n if (u->last_port && n > u->last_port) {\n u->err = "invalid port range";\n return NGX_ERROR;\n }\n u->port = (in_port_t) n;\n sin->sin_port = htons((in_port_t) n);\n u->port_text.len = last - port;\n u->port_text.data = port;\n last = port - 1;\n } else {\n if (uri == NULL) {\n if (u->listen) {\n len = last - host;\n dash = ngx_strlchr(host, last, \'-\');\n if (dash) {\n dash++;\n n = ngx_atoi(dash, last - dash);\n if (n == NGX_ERROR) {\n goto no_port;\n }\n if (n < 1 || n > 65535) {\n u->err = "invalid port";\n } else {\n u->last_port = (in_port_t) n;\n }\n len = dash - host - 1;\n }\n n = ngx_atoi(host, len);\n if (n != NGX_ERROR) {\n if (u->err) {\n return NGX_ERROR;\n }\n if (n < 1 || n > 65535) {\n u->err = "invalid port";\n return NGX_ERROR;\n }\n if (u->last_port && n > u->last_port) {\n u->err = "invalid port range";\n return NGX_ERROR;\n }\n u->port = (in_port_t) n;\n sin->sin_port = htons((in_port_t) n);\n sin->sin_addr.s_addr = INADDR_ANY;\n u->port_text.len = last - host;\n u->port_text.data = host;\n u->wildcard = 1;\n return ngx_inet_add_addr(pool, u, &u->sockaddr.sockaddr,\n u->socklen, 1);\n }\n }\n }\nno_port:\n u->err = NULL;\n u->no_port = 1;\n u->port = u->default_port;\n sin->sin_port = htons(u->default_port);\n u->last_port = 0;\n }\n len = last - host;\n if (len == 0) {\n u->err = "no host";\n return NGX_ERROR;\n }\n u->host.len = len;\n u->host.data = host;\n if (u->listen && len == 1 && *host == \'*\') {\n sin->sin_addr.s_addr = INADDR_ANY;\n u->wildcard = 1;\n return ngx_inet_add_addr(pool, u, &u->sockaddr.sockaddr, u->socklen, 1);\n }\n sin->sin_addr.s_addr = ngx_inet_addr(host, len);\n if (sin->sin_addr.s_addr != INADDR_NONE) {\n if (sin->sin_addr.s_addr == INADDR_ANY) {\n u->wildcard = 1;\n }\n return ngx_inet_add_addr(pool, u, &u->sockaddr.sockaddr, u->socklen, 1);\n }\n if (u->no_resolve) {\n return NGX_OK;\n }\n if (ngx_inet_resolve_host(pool, u) != NGX_OK) {\n return NGX_ERROR;\n }\n u->family = u->addrs[0].sockaddr->sa_family;\n u->socklen = u->addrs[0].socklen;\n ngx_memcpy(&u->sockaddr, u->addrs[0].sockaddr, u->addrs[0].socklen);\n u->wildcard = ngx_inet_wildcard(&u->sockaddr.sockaddr);\n return NGX_OK;\n}', 'static ngx_inline u_char *\nngx_strlchr(u_char *p, u_char *last, u_char c)\n{\n while (p < last) {\n if (*p == c) {\n return p;\n }\n p++;\n }\n return NULL;\n}', "ngx_int_t\nngx_atoi(u_char *line, size_t n)\n{\n ngx_int_t value, cutoff, cutlim;\n if (n == 0) {\n return NGX_ERROR;\n }\n cutoff = NGX_MAX_INT_T_VALUE / 10;\n cutlim = NGX_MAX_INT_T_VALUE % 10;\n for (value = 0; n--; line++) {\n if (*line < '0' || *line > '9') {\n return NGX_ERROR;\n }\n if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {\n return NGX_ERROR;\n }\n value = value * 10 + (*line - '0');\n }\n return value;\n}"]
|
1,298 | 0 |
https://github.com/openssl/openssl/blob/e3713c365c2657236439fea00822a43aa396d112/test/danetest.c/#L337
|
static int test_tlsafile(SSL_CTX *ctx, const char *base_name,
BIO *f, const char *path)
{
char *line;
int testno = 0;
int ret = 1;
SSL *ssl;
while (ret > 0 && (line = read_to_eol(f)) != NULL) {
STACK_OF(X509) *chain;
int ntlsa;
int ncert;
int noncheck;
int want;
int want_depth;
int off;
int i;
int ok;
int err;
int mdpth;
if (*line == '\0' || *line == '#')
continue;
++testno;
if (sscanf(line, "%d %d %d %d %d%n",
&ntlsa, &ncert, &noncheck, &want, &want_depth, &off) != 5
|| !allws(line + off)) {
TEST_error("Malformed line for test %d", testno);
return 0;
}
if (!TEST_ptr(ssl = SSL_new(ctx)))
return 0;
SSL_set_connect_state(ssl);
if (SSL_dane_enable(ssl, base_name) <= 0) {
SSL_free(ssl);
return 0;
}
if (noncheck)
SSL_dane_set_flags(ssl, DANE_FLAG_NO_DANE_EE_NAMECHECKS);
for (i = 0; i < ntlsa; ++i) {
if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {
SSL_free(ssl);
return 0;
}
}
ERR_clear_error();
if (!TEST_ptr(chain = load_chain(f, ncert))) {
SSL_free(ssl);
return 0;
}
ok = verify_chain(ssl, chain);
sk_X509_pop_free(chain, X509_free);
err = SSL_get_verify_result(ssl);
SSL_set_verify_result(ssl, X509_V_OK);
mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);
SSL_set_verify_result(ssl, err);
SSL_free(ssl);
if (!TEST_int_eq(err, want)) {
if (want == X509_V_OK)
TEST_info("Verification failure in test %d: %d=%s",
testno, err, X509_verify_cert_error_string(err));
else
TEST_info("Unexpected error in test %d", testno);
ret = 0;
continue;
}
if (!TEST_false(want == 0 && ok == 0)) {
TEST_info("Verification failure in test %d: ok=0", testno);
ret = 0;
continue;
}
if (!TEST_int_eq(mdpth, want_depth)) {
TEST_info("In test test %d", testno);
ret = 0;
}
}
ERR_clear_error();
return ret;
}
|
['static int test_tlsafile(SSL_CTX *ctx, const char *base_name,\n BIO *f, const char *path)\n{\n char *line;\n int testno = 0;\n int ret = 1;\n SSL *ssl;\n while (ret > 0 && (line = read_to_eol(f)) != NULL) {\n STACK_OF(X509) *chain;\n int ntlsa;\n int ncert;\n int noncheck;\n int want;\n int want_depth;\n int off;\n int i;\n int ok;\n int err;\n int mdpth;\n if (*line == \'\\0\' || *line == \'#\')\n continue;\n ++testno;\n if (sscanf(line, "%d %d %d %d %d%n",\n &ntlsa, &ncert, &noncheck, &want, &want_depth, &off) != 5\n || !allws(line + off)) {\n TEST_error("Malformed line for test %d", testno);\n return 0;\n }\n if (!TEST_ptr(ssl = SSL_new(ctx)))\n return 0;\n SSL_set_connect_state(ssl);\n if (SSL_dane_enable(ssl, base_name) <= 0) {\n SSL_free(ssl);\n return 0;\n }\n if (noncheck)\n SSL_dane_set_flags(ssl, DANE_FLAG_NO_DANE_EE_NAMECHECKS);\n for (i = 0; i < ntlsa; ++i) {\n if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) {\n SSL_free(ssl);\n return 0;\n }\n }\n ERR_clear_error();\n if (!TEST_ptr(chain = load_chain(f, ncert))) {\n SSL_free(ssl);\n return 0;\n }\n ok = verify_chain(ssl, chain);\n sk_X509_pop_free(chain, X509_free);\n err = SSL_get_verify_result(ssl);\n SSL_set_verify_result(ssl, X509_V_OK);\n mdpth = SSL_get0_dane_authority(ssl, NULL, NULL);\n SSL_set_verify_result(ssl, err);\n SSL_free(ssl);\n if (!TEST_int_eq(err, want)) {\n if (want == X509_V_OK)\n TEST_info("Verification failure in test %d: %d=%s",\n testno, err, X509_verify_cert_error_string(err));\n else\n TEST_info("Unexpected error in test %d", testno);\n ret = 0;\n continue;\n }\n if (!TEST_false(want == 0 && ok == 0)) {\n TEST_info("Verification failure in test %d: ok=0", testno);\n ret = 0;\n continue;\n }\n if (!TEST_int_eq(mdpth, want_depth)) {\n TEST_info("In test test %d", testno);\n ret = 0;\n }\n }\n ERR_clear_error();\n return ret;\n}', 'int test_ptr(const char *file, int line, const char *s, const void *p)\n{\n if (p != NULL)\n return 1;\n test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);\n return 0;\n}', 'void SSL_set_connect_state(SSL *s)\n{\n s->server = 0;\n s->shutdown = 0;\n ossl_statem_clear(s);\n s->handshake_func = s->method->ssl_connect;\n clear_ciphers(s);\n}', 'void ossl_statem_clear(SSL *s)\n{\n s->statem.state = MSG_FLOW_UNINITED;\n s->statem.hand_state = TLS_ST_BEFORE;\n s->statem.in_init = 1;\n s->statem.no_cert_verify = 0;\n}', 'int SSL_dane_enable(SSL *s, const char *basedomain)\n{\n SSL_DANE *dane = &s->dane;\n if (s->ctx->dane.mdmax == 0) {\n SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_CONTEXT_NOT_DANE_ENABLED);\n return 0;\n }\n if (dane->trecs != NULL) {\n SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_DANE_ALREADY_ENABLED);\n return 0;\n }\n if (s->ext.hostname == NULL) {\n if (!SSL_set_tlsext_host_name(s, basedomain)) {\n SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);\n return -1;\n }\n }\n if (!X509_VERIFY_PARAM_set1_host(s->param, basedomain, 0)) {\n SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);\n return -1;\n }\n dane->mdpth = -1;\n dane->pdpth = -1;\n dane->dctx = &s->ctx->dane;\n dane->trecs = sk_danetls_record_new_null();\n if (dane->trecs == NULL) {\n SSLerr(SSL_F_SSL_DANE_ENABLE, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n return 1;\n}', 'int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,\n const char *name, size_t namelen)\n{\n return int_x509_param_set_hosts(param, SET_HOST, name, namelen);\n}', 'DEFINE_STACK_OF(danetls_record)', 'OPENSSL_STACK *OPENSSL_sk_new_null(void)\n{\n return OPENSSL_zalloc(sizeof(OPENSSL_STACK));\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 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}', 'unsigned long SSL_dane_set_flags(SSL *ssl, unsigned long flags)\n{\n unsigned long orig = ssl->dane.flags;\n ssl->dane.flags |= flags;\n return orig;\n}']
|
1,299 | 0 |
https://github.com/libav/libav/blob/e5b0fc170f85b00f7dd0ac514918fb5c95253d39/libavcodec/bitstream.h/#L139
|
static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
bc->bits >>= n;
#else
uint64_t ret = bc->bits >> (64 - n);
bc->bits <<= n;
#endif
bc->bits_left -= n;
return ret;
}
|
['static int decode_tonal_components(BitstreamContext *bc,\n TonalComponent *components, int num_bands)\n{\n int i, b, c, m;\n int nb_components, coding_mode_selector, coding_mode;\n int band_flags[4], mantissa[8];\n int component_count = 0;\n nb_components = bitstream_read(bc, 5);\n if (nb_components == 0)\n return 0;\n coding_mode_selector = bitstream_read(bc, 2);\n if (coding_mode_selector == 2)\n return AVERROR_INVALIDDATA;\n coding_mode = coding_mode_selector & 1;\n for (i = 0; i < nb_components; i++) {\n int coded_values_per_component, quant_step_index;\n for (b = 0; b <= num_bands; b++)\n band_flags[b] = bitstream_read_bit(bc);\n coded_values_per_component = bitstream_read(bc, 3);\n quant_step_index = bitstream_read(bc, 3);\n if (quant_step_index <= 1)\n return AVERROR_INVALIDDATA;\n if (coding_mode_selector == 3)\n coding_mode = bitstream_read_bit(bc);\n for (b = 0; b < (num_bands + 1) * 4; b++) {\n int coded_components;\n if (band_flags[b >> 2] == 0)\n continue;\n coded_components = bitstream_read(bc, 3);\n for (c = 0; c < coded_components; c++) {\n TonalComponent *cmp = &components[component_count];\n int sf_index, coded_values, max_coded_values;\n float scale_factor;\n sf_index = bitstream_read(bc, 6);\n if (component_count >= 64)\n return AVERROR_INVALIDDATA;\n cmp->pos = b * 64 + bitstream_read(bc, 6);\n max_coded_values = SAMPLES_PER_FRAME - cmp->pos;\n coded_values = coded_values_per_component + 1;\n coded_values = FFMIN(max_coded_values, coded_values);\n scale_factor = ff_atrac_sf_table[sf_index] *\n inv_max_quant[quant_step_index];\n read_quant_spectral_coeffs(bc, quant_step_index, coding_mode,\n mantissa, coded_values);\n cmp->num_coefs = coded_values;\n for (m = 0; m < coded_values; m++)\n cmp->coef[m] = mantissa[m] * scale_factor;\n component_count++;\n }\n }\n }\n return component_count;\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}']
|
1,300 | 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);
}
|
['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}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n int i;\n BN_ULONG *A;\n const BN_ULONG *B;\n bn_check_top(b);\n if (a == b)\n return (a);\n if (bn_wexpand(a, b->top) == NULL)\n return (NULL);\n#if 1\n A = a->d;\n B = b->d;\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:;\n }\n#else\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n#endif\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return (a);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n 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_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n 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}']
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.